2
0

JsrtDebuggerObject.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955
  1. //-------------------------------------------------------------------------------------------------------
  2. // Copyright (C) Microsoft. All rights reserved.
  3. // Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
  4. //-------------------------------------------------------------------------------------------------------
  5. #include "JsrtPch.h"
  6. #ifdef ENABLE_SCRIPT_DEBUGGING
  7. #include "JsrtDebuggerObject.h"
  8. #include "JsrtDebugUtils.h"
  9. #include "JsrtDebugManager.h"
  10. JsrtDebuggerObjectBase::JsrtDebuggerObjectBase(JsrtDebuggerObjectType type, JsrtDebuggerObjectsManager* debuggerObjectsManager) :
  11. type(type),
  12. debuggerObjectsManager(debuggerObjectsManager)
  13. {
  14. Assert(debuggerObjectsManager != nullptr);
  15. this->handle = debuggerObjectsManager->GetNextHandle();
  16. }
  17. JsrtDebuggerObjectBase::~JsrtDebuggerObjectBase()
  18. {
  19. this->debuggerObjectsManager = nullptr;
  20. }
  21. JsrtDebuggerObjectsManager * JsrtDebuggerObjectBase::GetDebuggerObjectsManager()
  22. {
  23. return this->debuggerObjectsManager;
  24. }
  25. Js::DynamicObject * JsrtDebuggerObjectBase::GetChildren(Js::ScriptContext * scriptContext, uint fromCount, uint totalCount)
  26. {
  27. AssertMsg(false, "Wrong type for GetChildren");
  28. return nullptr;
  29. }
  30. Js::DynamicObject * JsrtDebuggerObjectBase::GetChildren(WeakArenaReference<Js::IDiagObjectModelWalkerBase>* walkerRef, Js::ScriptContext * scriptContext, uint fromCount, uint totalCount)
  31. {
  32. Js::DynamicObject* childrensObject = scriptContext->GetLibrary()->CreateObject();
  33. uint propertiesArrayCount = 0;
  34. Js::JavascriptArray* propertiesArray = scriptContext->GetLibrary()->CreateArray();
  35. uint debuggerOnlyPropertiesArrayCount = 0;
  36. Js::JavascriptArray* debuggerOnlyPropertiesArray = scriptContext->GetLibrary()->CreateArray();
  37. Js::IDiagObjectModelWalkerBase* walker = walkerRef->GetStrongReference();
  38. uint32 childrensCount = 0;
  39. if (walker != nullptr)
  40. {
  41. try
  42. {
  43. childrensCount = walker->GetChildrenCount();
  44. }
  45. catch (const Js::JavascriptException& err)
  46. {
  47. err.GetAndClear(); // discard exception object
  48. }
  49. if (fromCount < childrensCount)
  50. {
  51. for (uint32 i = fromCount; i < childrensCount && (propertiesArrayCount + debuggerOnlyPropertiesArrayCount) < totalCount; ++i)
  52. {
  53. Js::ResolvedObject resolvedObject;
  54. try
  55. {
  56. walker->Get(i, &resolvedObject);
  57. }
  58. catch (const Js::JavascriptException& err)
  59. {
  60. Js::JavascriptExceptionObject* exception = err.GetAndClear();
  61. Js::Var error = exception->GetThrownObject(scriptContext);
  62. resolvedObject.obj = error;
  63. resolvedObject.address = nullptr;
  64. resolvedObject.scriptContext = exception->GetScriptContext();
  65. resolvedObject.typeId = Js::JavascriptOperators::GetTypeId(error);
  66. resolvedObject.name = _u("{error}");
  67. resolvedObject.propId = Js::Constants::NoProperty;
  68. }
  69. AutoPtr<WeakArenaReference<Js::IDiagObjectModelDisplay>> objectDisplayWeakRef(resolvedObject.GetObjectDisplay());
  70. Js::IDiagObjectModelDisplay* resolvedObjectDisplay = objectDisplayWeakRef->GetStrongReference();
  71. if (resolvedObjectDisplay != nullptr)
  72. {
  73. JsrtDebuggerObjectBase* debuggerObject = JsrtDebuggerObjectProperty::Make(this->GetDebuggerObjectsManager(), objectDisplayWeakRef);
  74. Js::DynamicObject* object = debuggerObject->GetJSONObject(resolvedObject.scriptContext, /* forceSetValueProp */ false);
  75. Js::Var marshaledObj = Js::CrossSite::MarshalVar(scriptContext, object);
  76. if (resolvedObjectDisplay->IsFake())
  77. {
  78. Js::JavascriptOperators::OP_SetElementI((Js::Var)debuggerOnlyPropertiesArray, Js::JavascriptNumber::ToVar(debuggerOnlyPropertiesArrayCount++, scriptContext), marshaledObj, scriptContext);
  79. }
  80. else
  81. {
  82. Js::JavascriptOperators::OP_SetElementI((Js::Var)propertiesArray, Js::JavascriptNumber::ToVar(propertiesArrayCount++, scriptContext), marshaledObj, scriptContext);
  83. }
  84. objectDisplayWeakRef->ReleaseStrongReference();
  85. objectDisplayWeakRef.Detach();
  86. }
  87. }
  88. }
  89. walkerRef->ReleaseStrongReference();
  90. }
  91. JsrtDebugUtils::AddPropertyToObject(childrensObject, JsrtDebugPropertyId::totalPropertiesOfObject, childrensCount, scriptContext);
  92. JsrtDebugUtils::AddPropertyToObject(childrensObject, JsrtDebugPropertyId::properties, propertiesArray, scriptContext);
  93. JsrtDebugUtils::AddPropertyToObject(childrensObject, JsrtDebugPropertyId::debuggerOnlyProperties, debuggerOnlyPropertiesArray, scriptContext);
  94. return childrensObject;
  95. }
  96. JsrtDebuggerObjectsManager::JsrtDebuggerObjectsManager(JsrtDebugManager* jsrtDebugManager) :
  97. handleId(0),
  98. jsrtDebugManager(jsrtDebugManager),
  99. handleToDebuggerObjectsDictionary(nullptr),
  100. dataToDebuggerObjectsDictionary(nullptr)
  101. {
  102. Assert(jsrtDebugManager != nullptr);
  103. }
  104. JsrtDebuggerObjectsManager::~JsrtDebuggerObjectsManager()
  105. {
  106. if (this->dataToDebuggerObjectsDictionary != nullptr)
  107. {
  108. AssertMsg(this->dataToDebuggerObjectsDictionary->Count() == 0, "Should have cleared all debugger objects by now?");
  109. Adelete(this->GetDebugObjectArena(), this->dataToDebuggerObjectsDictionary);
  110. this->dataToDebuggerObjectsDictionary = nullptr;
  111. }
  112. if (this->handleToDebuggerObjectsDictionary != nullptr)
  113. {
  114. AssertMsg(this->handleToDebuggerObjectsDictionary->Count() == 0, "Should have cleared all handle by now?");
  115. Adelete(this->GetDebugObjectArena(), this->handleToDebuggerObjectsDictionary);
  116. this->handleToDebuggerObjectsDictionary = nullptr;
  117. }
  118. }
  119. void JsrtDebuggerObjectsManager::ClearAll()
  120. {
  121. if (this->dataToDebuggerObjectsDictionary != nullptr)
  122. {
  123. this->dataToDebuggerObjectsDictionary->Clear();
  124. }
  125. if (this->handleToDebuggerObjectsDictionary != nullptr)
  126. {
  127. this->handleToDebuggerObjectsDictionary->Map([this](uint handle, JsrtDebuggerObjectBase* debuggerObject) {
  128. Adelete(this->GetDebugObjectArena(), debuggerObject);
  129. });
  130. this->handleToDebuggerObjectsDictionary->Clear();
  131. }
  132. this->handleId = 0;
  133. }
  134. ArenaAllocator * JsrtDebuggerObjectsManager::GetDebugObjectArena()
  135. {
  136. return this->GetJsrtDebugManager()->GetDebugObjectArena();
  137. }
  138. bool JsrtDebuggerObjectsManager::TryGetDebuggerObjectFromHandle(uint handle, JsrtDebuggerObjectBase ** debuggerObject)
  139. {
  140. if (this->handleToDebuggerObjectsDictionary == nullptr)
  141. {
  142. return false;
  143. }
  144. return this->handleToDebuggerObjectsDictionary->TryGetValue(handle, debuggerObject);
  145. }
  146. void JsrtDebuggerObjectsManager::AddToDebuggerObjectsDictionary(JsrtDebuggerObjectBase * debuggerObject)
  147. {
  148. Assert(debuggerObject != nullptr);
  149. uint handle = debuggerObject->GetHandle();
  150. Assert(handle > 0);
  151. if (this->handleToDebuggerObjectsDictionary == nullptr)
  152. {
  153. this->handleToDebuggerObjectsDictionary = Anew(this->GetDebugObjectArena(), DebuggerObjectsDictionary, this->GetDebugObjectArena(), 10);
  154. }
  155. Assert(!this->handleToDebuggerObjectsDictionary->ContainsKey(handle));
  156. int index = this->handleToDebuggerObjectsDictionary->Add(handle, debuggerObject);
  157. Assert(index != -1);
  158. }
  159. void JsrtDebuggerObjectsManager::AddToDataToDebuggerObjectsDictionary(void * data, JsrtDebuggerObjectBase * debuggerObject)
  160. {
  161. Assert(data != nullptr);
  162. Assert(debuggerObject != nullptr);
  163. if (this->dataToDebuggerObjectsDictionary == nullptr)
  164. {
  165. this->dataToDebuggerObjectsDictionary = Anew(this->GetDebugObjectArena(), DataToDebuggerObjectsDictionary, this->GetDebugObjectArena(), 10);
  166. }
  167. Assert(!this->dataToDebuggerObjectsDictionary->ContainsKey(data));
  168. int index = this->dataToDebuggerObjectsDictionary->Add(data, debuggerObject);
  169. Assert(index != -1);
  170. this->AddToDebuggerObjectsDictionary(debuggerObject);
  171. }
  172. bool JsrtDebuggerObjectsManager::TryGetDataFromDataToDebuggerObjectsDictionary(void * data, JsrtDebuggerObjectBase ** debuggerObject)
  173. {
  174. if (this->dataToDebuggerObjectsDictionary == nullptr)
  175. {
  176. return false;
  177. }
  178. return this->dataToDebuggerObjectsDictionary->TryGetValue(data, debuggerObject);
  179. }
  180. JsrtDebuggerStackFrame::JsrtDebuggerStackFrame(JsrtDebuggerObjectsManager * debuggerObjectsManager, Js::DiagStackFrame * stackFrame, uint frameIndex) :
  181. debuggerObjectsManager(debuggerObjectsManager),
  182. frameIndex(frameIndex),
  183. stackFrame(stackFrame)
  184. {
  185. Assert(this->stackFrame != nullptr);
  186. }
  187. JsrtDebuggerStackFrame::~JsrtDebuggerStackFrame()
  188. {
  189. this->debuggerObjectsManager = nullptr;
  190. this->stackFrame = nullptr;
  191. }
  192. Js::DynamicObject * JsrtDebuggerStackFrame::GetJSONObject(Js::ScriptContext* scriptContext)
  193. {
  194. Js::ScriptContext *frameScriptContext = stackFrame->GetScriptContext();
  195. Js::DynamicObject* stackTraceObject = frameScriptContext->GetLibrary()->CreateObject();
  196. Js::FunctionBody* functionBody = stackFrame->GetFunction();
  197. Js::Utf8SourceInfo* utf8SourceInfo = functionBody->GetUtf8SourceInfo();
  198. JsrtDebugUtils::AddPropertyToObject(stackTraceObject, JsrtDebugPropertyId::index, frameIndex, scriptContext);
  199. JsrtDebugUtils::AddScriptIdToObject(stackTraceObject, utf8SourceInfo);
  200. int currentByteCodeOffset = stackFrame->GetByteCodeOffset();
  201. if (stackFrame->IsInterpreterFrame() && frameIndex != 0)
  202. {
  203. // For non-leaf interpreter frames back up 1 instruction so we see the caller
  204. // rather than the statement after the caller
  205. currentByteCodeOffset--;
  206. }
  207. JsrtDebugUtils::AddLineColumnToObject(stackTraceObject, functionBody, currentByteCodeOffset);
  208. JsrtDebugUtils::AddSourceLengthAndTextToObject(stackTraceObject, functionBody, currentByteCodeOffset);
  209. Js::JavascriptFunction* javascriptFunction = stackFrame->GetJavascriptFunction();
  210. JsrtDebuggerObjectBase* functionObject = nullptr;
  211. if (!this->debuggerObjectsManager->TryGetDataFromDataToDebuggerObjectsDictionary(javascriptFunction, &functionObject))
  212. {
  213. functionObject = JsrtDebuggerObjectFunction::Make(this->debuggerObjectsManager, javascriptFunction);
  214. }
  215. JsrtDebugUtils::AddPropertyToObject(stackTraceObject, JsrtDebugPropertyId::functionHandle, functionObject->GetHandle(), frameScriptContext);
  216. return stackTraceObject;
  217. }
  218. Js::DynamicObject * JsrtDebuggerStackFrame::GetLocalsObject(Js::ScriptContext* scriptContext)
  219. {
  220. /*
  221. {
  222. "thisObject" : {},
  223. "exception" : {},
  224. "arguments" : {},
  225. "returnValue" : {},
  226. "functionCallsReturn" : [{}, {}],
  227. "locals" : [],
  228. "scopes" : [{}, {}],
  229. "globals" : {}
  230. }
  231. */
  232. Js::DynamicObject* propertiesObject = scriptContext->GetLibrary()->CreateObject();
  233. Js::Var returnValueObject = nullptr;
  234. uint functionCallsReturnCount = 0;
  235. Js::JavascriptArray* functionCallsReturn = scriptContext->GetLibrary()->CreateArray();
  236. uint totalLocalsCount = 0;
  237. Js::JavascriptArray* localsArray = scriptContext->GetLibrary()->CreateArray();
  238. uint scopesCount = 0;
  239. Js::JavascriptArray* scopesArray = scriptContext->GetLibrary()->CreateArray();
  240. Js::DynamicObject* globalsObject = nullptr;
  241. ReferencedArenaAdapter* pRefArena = scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena();
  242. Js::IDiagObjectModelDisplay* pLocalsDisplay = Anew(pRefArena->Arena(), Js::LocalsDisplay, this->stackFrame);
  243. WeakArenaReference<Js::IDiagObjectModelWalkerBase>* objectModelWalker = pLocalsDisplay->CreateWalker();
  244. if (objectModelWalker != nullptr)
  245. {
  246. Js::LocalsWalker* localsWalker = (Js::LocalsWalker*)objectModelWalker->GetStrongReference();
  247. if (localsWalker != nullptr)
  248. {
  249. // If 'this' is available add 'thisObject'
  250. Js::ResolvedObject thisResolvedObject;
  251. {
  252. ENFORCE_ENTRYEXITRECORD_HASCALLER(scriptContext);
  253. thisResolvedObject.obj = this->stackFrame->GetThisFromFrame(&thisResolvedObject.address, localsWalker);
  254. }
  255. if (thisResolvedObject.obj != nullptr)
  256. {
  257. thisResolvedObject.scriptContext = scriptContext;
  258. thisResolvedObject.name = _u("this");
  259. thisResolvedObject.typeId = Js::JavascriptOperators::GetTypeId(thisResolvedObject.obj);
  260. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectProperty>(this->debuggerObjectsManager, thisResolvedObject, this->stackFrame->GetScriptContext(), false, [&](Js::Var marshaledObj)
  261. {
  262. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::thisObject, marshaledObj, scriptContext);
  263. });
  264. }
  265. uint32 totalProperties = localsWalker->GetChildrenCount();
  266. if (totalProperties > 0)
  267. {
  268. int index = 0;
  269. Js::ResolvedObject resolvedObject;
  270. resolvedObject.scriptContext = this->stackFrame->GetScriptContext();
  271. // If we have a exception add 'exception'
  272. if (Js::VariableWalkerBase::GetExceptionObject(index, this->stackFrame, &resolvedObject))
  273. {
  274. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectProperty>(this->debuggerObjectsManager, resolvedObject, scriptContext, false, [&](Js::Var marshaledObj)
  275. {
  276. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::exception, marshaledObj, scriptContext);
  277. });
  278. }
  279. // If user have not explicitly defined 'arguments' add 'arguments'
  280. if (localsWalker->HasUserNotDefinedArguments() && localsWalker->CreateArgumentsObject(&resolvedObject))
  281. {
  282. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectProperty>(this->debuggerObjectsManager, resolvedObject, scriptContext, false, [&](Js::Var marshaledObj)
  283. {
  284. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::arguments, marshaledObj, scriptContext);
  285. });
  286. }
  287. Js::ReturnedValueList *returnedValueList = this->stackFrame->GetScriptContext()->GetDebugContext()->GetProbeContainer()->GetReturnedValueList();
  288. // If we have return value(s) add them to 'returnValue' or 'functionCallsReturn'
  289. if (returnedValueList != nullptr && returnedValueList->Count() > 0 && this->stackFrame->IsTopFrame())
  290. {
  291. for (int i = 0; i < returnedValueList->Count(); ++i)
  292. {
  293. Js::ReturnedValue * returnValue = returnedValueList->Item(i);
  294. Js::VariableWalkerBase::GetReturnedValueResolvedObject(returnValue, this->stackFrame, &resolvedObject);
  295. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectProperty>(debuggerObjectsManager, resolvedObject, scriptContext, false, [&](Js::Var marshaledObj)
  296. {
  297. if (returnValue->isValueOfReturnStatement)
  298. {
  299. returnValueObject = marshaledObj;
  300. }
  301. else
  302. {
  303. Js::JavascriptOperators::OP_SetElementI((Js::Var)functionCallsReturn, Js::JavascriptNumber::ToVar(functionCallsReturnCount, scriptContext), marshaledObj, scriptContext);
  304. functionCallsReturnCount++;
  305. }
  306. });
  307. }
  308. if (returnValueObject != nullptr)
  309. {
  310. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::returnValue, returnValueObject, scriptContext);
  311. }
  312. if (functionCallsReturnCount > 0)
  313. {
  314. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::functionCallsReturn, functionCallsReturn, scriptContext);
  315. }
  316. }
  317. // Add all locals variable(s) available under 'locals'
  318. uint32 localsCount = localsWalker->GetLocalVariablesCount();
  319. for (uint32 i = 0; i < localsCount; ++i)
  320. {
  321. if (!localsWalker->GetLocal(i, &resolvedObject))
  322. {
  323. break;
  324. }
  325. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectProperty>(debuggerObjectsManager, resolvedObject, scriptContext, false, [&](Js::Var marshaledObj)
  326. {
  327. Js::JavascriptOperators::OP_SetElementI((Js::Var)localsArray, Js::JavascriptNumber::ToVar(totalLocalsCount, scriptContext), marshaledObj, scriptContext);
  328. totalLocalsCount++;
  329. });
  330. }
  331. // Add all variable(s) captured under 'scopes'
  332. index = 0;
  333. BOOL foundGroup = TRUE;
  334. while (foundGroup)
  335. {
  336. foundGroup = localsWalker->GetScopeObject(index++, &resolvedObject);
  337. if (foundGroup == TRUE)
  338. {
  339. AutoPtr<WeakArenaReference<Js::IDiagObjectModelDisplay>> objectDisplayWeakRef(resolvedObject.GetObjectDisplay());
  340. JsrtDebuggerObjectBase* debuggerObject = JsrtDebuggerObjectScope::Make(debuggerObjectsManager, objectDisplayWeakRef, scopesCount);
  341. Js::DynamicObject* object = debuggerObject->GetJSONObject(resolvedObject.scriptContext, /* forceSetValueProp */ false);
  342. Assert(object != nullptr);
  343. Js::Var marshaledObj = Js::CrossSite::MarshalVar(scriptContext, object);
  344. Js::JavascriptOperators::OP_SetElementI((Js::Var)scopesArray, Js::JavascriptNumber::ToVar(scopesCount, scriptContext), marshaledObj, scriptContext);
  345. scopesCount++;
  346. objectDisplayWeakRef.Detach();
  347. }
  348. }
  349. // Add globals handle
  350. if (localsWalker->GetGlobalsObject(&resolvedObject))
  351. {
  352. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectGlobalsNode>(this->debuggerObjectsManager, resolvedObject, scriptContext, false, [&](Js::Var marshaledObj)
  353. {
  354. globalsObject = (Js::DynamicObject*)marshaledObj;
  355. });
  356. }
  357. }
  358. objectModelWalker->ReleaseStrongReference();
  359. HeapDelete(objectModelWalker);
  360. }
  361. Adelete(pRefArena->Arena(), pLocalsDisplay);
  362. }
  363. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::locals, localsArray, scriptContext);
  364. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::scopes, scopesArray, scriptContext);
  365. if (globalsObject == nullptr)
  366. {
  367. globalsObject = scriptContext->GetLibrary()->CreateObject();
  368. }
  369. JsrtDebugUtils::AddPropertyToObject(propertiesObject, JsrtDebugPropertyId::globals, globalsObject, scriptContext);
  370. return propertiesObject;
  371. }
  372. bool JsrtDebuggerStackFrame::Evaluate(Js::ScriptContext* scriptContext, const char16 *source, int sourceLength, bool isLibraryCode, bool forceSetValueProp, Js::DynamicObject** evalResult)
  373. {
  374. *evalResult = nullptr;
  375. bool success = false;
  376. if (this->stackFrame != nullptr)
  377. {
  378. Js::ResolvedObject resolvedObject;
  379. HRESULT hr = S_OK;
  380. Js::ScriptContext* frameScriptContext = this->stackFrame->GetScriptContext();
  381. Js::JavascriptExceptionObject *exceptionObject = nullptr;
  382. {
  383. BEGIN_JS_RUNTIME_CALL_EX_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NESTED(frameScriptContext, false)
  384. {
  385. ENFORCE_ENTRYEXITRECORD_HASCALLER(frameScriptContext);
  386. this->stackFrame->EvaluateImmediate(source, sourceLength, isLibraryCode, &resolvedObject);
  387. }
  388. END_JS_RUNTIME_CALL_AND_TRANSLATE_AND_GET_EXCEPTION_AND_ERROROBJECT_TO_HRESULT(hr, frameScriptContext, exceptionObject);
  389. }
  390. if (resolvedObject.obj == nullptr)
  391. {
  392. resolvedObject.name = _u("{exception}");
  393. resolvedObject.typeId = Js::TypeIds_Error;
  394. resolvedObject.address = nullptr;
  395. if (exceptionObject != nullptr)
  396. {
  397. resolvedObject.obj = exceptionObject->GetThrownObject(scriptContext);
  398. }
  399. else
  400. {
  401. resolvedObject.obj = scriptContext->GetLibrary()->GetUndefined();
  402. }
  403. }
  404. else
  405. {
  406. success = true;
  407. }
  408. if (resolvedObject.obj != nullptr)
  409. {
  410. resolvedObject.scriptContext = frameScriptContext;
  411. charcount_t len = Js::JavascriptString::GetBufferLength(source);
  412. resolvedObject.name = AnewNoThrowArray(this->debuggerObjectsManager->GetDebugObjectArena(), WCHAR, len + 1);
  413. if (resolvedObject.name != nullptr)
  414. {
  415. wcscpy_s((WCHAR*)resolvedObject.name, len + 1, source);
  416. }
  417. else
  418. {
  419. // len can be big, if we failed just have empty string
  420. resolvedObject.name = _u("");
  421. }
  422. resolvedObject.typeId = Js::JavascriptOperators::GetTypeId(resolvedObject.obj);
  423. JsrtDebuggerObjectBase::CreateDebuggerObject<JsrtDebuggerObjectProperty>(this->debuggerObjectsManager, resolvedObject, scriptContext, forceSetValueProp, [&](Js::Var marshaledObj)
  424. {
  425. *evalResult = (Js::DynamicObject*)marshaledObj;
  426. });
  427. }
  428. }
  429. return success;
  430. }
  431. JsrtDebuggerObjectProperty::JsrtDebuggerObjectProperty(JsrtDebuggerObjectsManager* debuggerObjectsManager, WeakArenaReference<Js::IDiagObjectModelDisplay>* objectDisplay) :
  432. JsrtDebuggerObjectBase(JsrtDebuggerObjectType::Property, debuggerObjectsManager),
  433. objectDisplay(objectDisplay),
  434. walkerRef(nullptr)
  435. {
  436. Assert(objectDisplay != nullptr);
  437. }
  438. JsrtDebuggerObjectProperty::~JsrtDebuggerObjectProperty()
  439. {
  440. if (this->objectDisplay != nullptr)
  441. {
  442. HeapDelete(this->objectDisplay);
  443. this->objectDisplay = nullptr;
  444. }
  445. if (this->walkerRef != nullptr)
  446. {
  447. HeapDelete(this->walkerRef);
  448. this->walkerRef = nullptr;
  449. }
  450. }
  451. JsrtDebuggerObjectBase * JsrtDebuggerObjectProperty::Make(JsrtDebuggerObjectsManager* debuggerObjectsManager, WeakArenaReference<Js::IDiagObjectModelDisplay>* objectDisplay)
  452. {
  453. JsrtDebuggerObjectBase* debuggerObject = Anew(debuggerObjectsManager->GetDebugObjectArena(), JsrtDebuggerObjectProperty, debuggerObjectsManager, objectDisplay);
  454. debuggerObjectsManager->AddToDebuggerObjectsDictionary(debuggerObject);
  455. return debuggerObject;
  456. }
  457. Js::DynamicObject * JsrtDebuggerObjectProperty::GetJSONObject(Js::ScriptContext* scriptContext, bool forceSetValueProp)
  458. {
  459. Js::IDiagObjectModelDisplay* objectDisplayRef = this->objectDisplay->GetStrongReference();
  460. Js::DynamicObject* propertyObject = nullptr;
  461. if (objectDisplayRef != nullptr)
  462. {
  463. propertyObject = scriptContext->GetLibrary()->CreateObject();
  464. LPCWSTR name = objectDisplayRef->Name();
  465. JsrtDebugUtils::AddPropertyToObject(propertyObject, JsrtDebugPropertyId::name, name, wcslen(name), scriptContext);
  466. JsrtDebugUtils::AddPropertyType(propertyObject, objectDisplayRef, scriptContext, forceSetValueProp); // Will add type, value, display, className, propertyAttributes
  467. JsrtDebugUtils::AddPropertyToObject(propertyObject, JsrtDebugPropertyId::handle, this->GetHandle(), scriptContext);
  468. this->objectDisplay->ReleaseStrongReference();
  469. }
  470. return propertyObject;
  471. }
  472. Js::DynamicObject* JsrtDebuggerObjectProperty::GetChildren(Js::ScriptContext* scriptContext, uint fromCount, uint totalCount)
  473. {
  474. Js::IDiagObjectModelDisplay* objectDisplayRef = objectDisplay->GetStrongReference();
  475. if (objectDisplayRef == nullptr)
  476. {
  477. return nullptr;
  478. }
  479. if (this->walkerRef == nullptr)
  480. {
  481. this->walkerRef = objectDisplayRef->CreateWalker();
  482. }
  483. Js::DynamicObject* childrens = __super::GetChildren(this->walkerRef, scriptContext, fromCount, totalCount);
  484. objectDisplay->ReleaseStrongReference();
  485. return childrens;
  486. }
  487. JsrtDebuggerObjectScope::JsrtDebuggerObjectScope(JsrtDebuggerObjectsManager * debuggerObjectsManager, WeakArenaReference<Js::IDiagObjectModelDisplay>* objectDisplay, uint index) :
  488. JsrtDebuggerObjectBase(JsrtDebuggerObjectType::Scope, debuggerObjectsManager),
  489. objectDisplay(objectDisplay),
  490. index(index),
  491. walkerRef(nullptr)
  492. {
  493. Assert(this->objectDisplay != nullptr);
  494. }
  495. JsrtDebuggerObjectScope::~JsrtDebuggerObjectScope()
  496. {
  497. if (this->objectDisplay != nullptr)
  498. {
  499. HeapDelete(this->objectDisplay);
  500. this->objectDisplay = nullptr;
  501. }
  502. if (this->walkerRef != nullptr)
  503. {
  504. HeapDelete(this->walkerRef);
  505. this->walkerRef = nullptr;
  506. }
  507. }
  508. JsrtDebuggerObjectBase * JsrtDebuggerObjectScope::Make(JsrtDebuggerObjectsManager * debuggerObjectsManager, WeakArenaReference<Js::IDiagObjectModelDisplay>* objectDisplay, uint index)
  509. {
  510. JsrtDebuggerObjectBase* debuggerObject = Anew(debuggerObjectsManager->GetDebugObjectArena(), JsrtDebuggerObjectScope, debuggerObjectsManager, objectDisplay, index);
  511. debuggerObjectsManager->AddToDebuggerObjectsDictionary(debuggerObject);
  512. return debuggerObject;
  513. }
  514. Js::DynamicObject * JsrtDebuggerObjectScope::GetJSONObject(Js::ScriptContext* scriptContext, bool forceSetValueProp)
  515. {
  516. Js::IDiagObjectModelDisplay* modelDisplay = this->objectDisplay->GetStrongReference();
  517. Js::DynamicObject* scopeObject = nullptr;
  518. if (modelDisplay != nullptr)
  519. {
  520. scopeObject = scriptContext->GetLibrary()->CreateObject();
  521. JsrtDebugUtils::AddPropertyToObject(scopeObject, JsrtDebugPropertyId::index, this->index, scriptContext);
  522. JsrtDebugUtils::AddPropertyToObject(scopeObject, JsrtDebugPropertyId::handle, this->GetHandle(), scriptContext);
  523. this->objectDisplay->ReleaseStrongReference();
  524. }
  525. return scopeObject;
  526. }
  527. Js::DynamicObject * JsrtDebuggerObjectScope::GetChildren(Js::ScriptContext * scriptContext, uint fromCount, uint totalCount)
  528. {
  529. Js::IDiagObjectModelDisplay* objectDisplayRef = objectDisplay->GetStrongReference();
  530. if (objectDisplayRef == nullptr)
  531. {
  532. return nullptr;
  533. }
  534. if (this->walkerRef == nullptr)
  535. {
  536. this->walkerRef = objectDisplayRef->CreateWalker();
  537. }
  538. Js::DynamicObject* childrens = __super::GetChildren(this->walkerRef, scriptContext, fromCount, totalCount);
  539. objectDisplay->ReleaseStrongReference();
  540. return childrens;
  541. }
  542. JsrtDebuggerObjectFunction::JsrtDebuggerObjectFunction(JsrtDebuggerObjectsManager* debuggerObjectsManager, Js::JavascriptFunction* javascriptFunction) :
  543. JsrtDebuggerObjectBase(JsrtDebuggerObjectType::Function, debuggerObjectsManager),
  544. javascriptFunction(javascriptFunction),
  545. objectDisplay(nullptr),
  546. walkerRef(nullptr)
  547. {
  548. }
  549. JsrtDebuggerObjectFunction::~JsrtDebuggerObjectFunction()
  550. {
  551. if (this->objectDisplay != nullptr)
  552. {
  553. HeapDelete(this->objectDisplay);
  554. this->objectDisplay = nullptr;
  555. }
  556. if (this->walkerRef != nullptr)
  557. {
  558. HeapDelete(this->walkerRef);
  559. this->walkerRef = nullptr;
  560. }
  561. this->javascriptFunction = nullptr;
  562. }
  563. JsrtDebuggerObjectBase * JsrtDebuggerObjectFunction::Make(JsrtDebuggerObjectsManager * debuggerObjectsManager, Js::JavascriptFunction* javascriptFunction)
  564. {
  565. JsrtDebuggerObjectBase* debuggerObject = nullptr;
  566. Assert(!debuggerObjectsManager->TryGetDataFromDataToDebuggerObjectsDictionary(javascriptFunction, &debuggerObject));
  567. debuggerObject = Anew(debuggerObjectsManager->GetDebugObjectArena(), JsrtDebuggerObjectFunction, debuggerObjectsManager, javascriptFunction);
  568. debuggerObjectsManager->AddToDataToDebuggerObjectsDictionary(javascriptFunction, debuggerObject);
  569. return debuggerObject;
  570. }
  571. Js::DynamicObject * JsrtDebuggerObjectFunction::GetJSONObject(Js::ScriptContext * scriptContext, bool forceSetValueProp)
  572. {
  573. Js::DynamicObject* functionObject = scriptContext->GetLibrary()->CreateObject();
  574. Js::FunctionBody* functionBody = this->javascriptFunction->GetFunctionBody();
  575. JsrtDebugUtils::AddScriptIdToObject(functionObject, functionBody->GetUtf8SourceInfo());
  576. JsrtDebugUtils::AddPropertyToObject(functionObject, JsrtDebugPropertyId::line, (uint32)functionBody->GetLineNumber(), scriptContext);
  577. JsrtDebugUtils::AddPropertyToObject(functionObject, JsrtDebugPropertyId::column, (uint32)functionBody->GetColumnNumber(), scriptContext);
  578. JsrtDebugUtils::AddPropertyToObject(functionObject, JsrtDebugPropertyId::name, functionBody->GetDisplayName(), functionBody->GetDisplayNameLength(), scriptContext);
  579. JsrtDebugUtils::AddPropertyToObject(functionObject, JsrtDebugPropertyId::type, scriptContext->GetLibrary()->GetFunctionTypeDisplayString(), scriptContext);
  580. JsrtDebugUtils::AddPropertyToObject(functionObject, JsrtDebugPropertyId::handle, this->GetHandle(), scriptContext);
  581. return functionObject;
  582. }
  583. Js::DynamicObject * JsrtDebuggerObjectFunction::GetChildren(Js::ScriptContext * scriptContext, uint fromCount, uint totalCount)
  584. {
  585. if (this->objectDisplay == nullptr)
  586. {
  587. Js::ResolvedObject functionResolvedObject;
  588. functionResolvedObject.obj = this->javascriptFunction;
  589. functionResolvedObject.scriptContext = scriptContext;
  590. functionResolvedObject.name = _u("Function");
  591. functionResolvedObject.typeId = Js::JavascriptOperators::GetTypeId(functionResolvedObject.obj);
  592. this->objectDisplay = functionResolvedObject.GetObjectDisplay();
  593. }
  594. Js::IDiagObjectModelDisplay* objectDisplayRef = this->objectDisplay->GetStrongReference();
  595. if (objectDisplayRef == nullptr)
  596. {
  597. return nullptr;
  598. }
  599. if (this->walkerRef == nullptr)
  600. {
  601. this->walkerRef = objectDisplayRef->CreateWalker();
  602. }
  603. Js::DynamicObject* childrens = __super::GetChildren(this->walkerRef, scriptContext, fromCount, totalCount);
  604. this->objectDisplay->ReleaseStrongReference();
  605. return childrens;
  606. }
  607. JsrtDebuggerObjectGlobalsNode::JsrtDebuggerObjectGlobalsNode(JsrtDebuggerObjectsManager* debuggerObjectsManager, WeakArenaReference<Js::IDiagObjectModelDisplay>* objectDisplay) :
  608. JsrtDebuggerObjectBase(JsrtDebuggerObjectType::Globals, debuggerObjectsManager),
  609. objectDisplay(objectDisplay),
  610. walkerRef(nullptr)
  611. {
  612. Assert(objectDisplay != nullptr);
  613. }
  614. JsrtDebuggerObjectGlobalsNode::~JsrtDebuggerObjectGlobalsNode()
  615. {
  616. if (this->objectDisplay != nullptr)
  617. {
  618. HeapDelete(this->objectDisplay);
  619. this->objectDisplay = nullptr;
  620. }
  621. if (this->walkerRef != nullptr)
  622. {
  623. HeapDelete(this->walkerRef);
  624. this->walkerRef = nullptr;
  625. }
  626. }
  627. JsrtDebuggerObjectBase * JsrtDebuggerObjectGlobalsNode::Make(JsrtDebuggerObjectsManager * debuggerObjectsManager, WeakArenaReference<Js::IDiagObjectModelDisplay>* objectDisplay)
  628. {
  629. JsrtDebuggerObjectBase* debuggerObject = Anew(debuggerObjectsManager->GetDebugObjectArena(), JsrtDebuggerObjectGlobalsNode, debuggerObjectsManager, objectDisplay);
  630. debuggerObjectsManager->AddToDebuggerObjectsDictionary(debuggerObject);
  631. return debuggerObject;
  632. }
  633. Js::DynamicObject * JsrtDebuggerObjectGlobalsNode::GetJSONObject(Js::ScriptContext * scriptContext, bool forceSetValueProp)
  634. {
  635. Js::IDiagObjectModelDisplay* objectDisplayRef = this->objectDisplay->GetStrongReference();
  636. Js::DynamicObject* globalsNode = nullptr;
  637. if (objectDisplayRef != nullptr)
  638. {
  639. globalsNode = scriptContext->GetLibrary()->CreateObject();
  640. JsrtDebugUtils::AddPropertyToObject(globalsNode, JsrtDebugPropertyId::handle, this->GetHandle(), scriptContext);
  641. this->objectDisplay->ReleaseStrongReference();
  642. }
  643. return globalsNode;
  644. }
  645. Js::DynamicObject * JsrtDebuggerObjectGlobalsNode::GetChildren(Js::ScriptContext * scriptContext, uint fromCount, uint totalCount)
  646. {
  647. Js::IDiagObjectModelDisplay* objectDisplayRef = objectDisplay->GetStrongReference();
  648. if (objectDisplayRef == nullptr)
  649. {
  650. return nullptr;
  651. }
  652. if (this->walkerRef == nullptr)
  653. {
  654. this->walkerRef = objectDisplayRef->CreateWalker();
  655. }
  656. Js::DynamicObject* childrens = __super::GetChildren(this->walkerRef, scriptContext, fromCount, totalCount);
  657. objectDisplay->ReleaseStrongReference();
  658. return childrens;
  659. }
  660. JsrtDebugStackFrames::JsrtDebugStackFrames(JsrtDebugManager* jsrtDebugManager):
  661. framesDictionary(nullptr)
  662. {
  663. Assert(jsrtDebugManager != nullptr);
  664. this->jsrtDebugManager = jsrtDebugManager;
  665. }
  666. JsrtDebugStackFrames::~JsrtDebugStackFrames()
  667. {
  668. if (this->framesDictionary != nullptr)
  669. {
  670. this->ClearFrameDictionary();
  671. Adelete(this->jsrtDebugManager->GetDebugObjectArena(), this->framesDictionary);
  672. this->framesDictionary = nullptr;
  673. }
  674. }
  675. static int __cdecl DiagStackFrameSorter(void * dispatchHaltFrameAddress, const void * diagStackFrame1, const void * diagStackFrame2)
  676. {
  677. const DWORD_PTR *p1 = reinterpret_cast<const DWORD_PTR*>(diagStackFrame1);
  678. const DWORD_PTR *p2 = reinterpret_cast<const DWORD_PTR*>(diagStackFrame2);
  679. Js::DiagStackFrame * pStackFrame1 = (Js::DiagStackFrame *)(*p1);
  680. Js::DiagStackFrame * pStackFrame2 = (Js::DiagStackFrame *)(*p2);
  681. DWORD_PTR stackAddress1 = pStackFrame1->GetStackAddress();
  682. DWORD_PTR stackAddress2 = pStackFrame2->GetStackAddress();
  683. return stackAddress1 > stackAddress2 ? 1 : -1;
  684. }
  685. Js::JavascriptArray * JsrtDebugStackFrames::StackFrames(Js::ScriptContext * scriptContext)
  686. {
  687. Js::JavascriptArray* stackTraceArray = nullptr;
  688. ThreadContext* threadContext = scriptContext->GetThreadContext();
  689. DWORD_PTR dispatchHaltFrameAddress = threadContext->GetDebugManager()->GetDispatchHaltFrameAddress();
  690. AssertMsg(dispatchHaltFrameAddress > 0, "Didn't set the dispatchHaltFrameAddress at time of break?");
  691. if (dispatchHaltFrameAddress != 0)
  692. {
  693. if (this->framesDictionary == nullptr)
  694. {
  695. this->framesDictionary = Anew(this->jsrtDebugManager->GetDebugObjectArena(), FramesDictionary, this->jsrtDebugManager->GetDebugObjectArena(), 10);
  696. }
  697. else
  698. {
  699. this->ClearFrameDictionary();
  700. }
  701. typedef JsUtil::List<Js::DiagStackFrame*, ArenaAllocator> DiagStackFrameList;
  702. DiagStackFrameList* stackList = Anew(this->jsrtDebugManager->GetDebugObjectArena(), DiagStackFrameList, this->jsrtDebugManager->GetDebugObjectArena(), 10);
  703. // Walk all the script contexts and collect the frames which are below the address when break was reported.
  704. for (Js::ScriptContext *tempScriptContext = threadContext->GetScriptContextList();
  705. tempScriptContext != nullptr && tempScriptContext->IsScriptContextInDebugMode();
  706. tempScriptContext = tempScriptContext->next)
  707. {
  708. Js::WeakDiagStack * framePointers = tempScriptContext->GetDebugContext()->GetProbeContainer()->GetFramePointers(dispatchHaltFrameAddress);
  709. if (framePointers != nullptr)
  710. {
  711. Js::DiagStack* stackFrames = framePointers->GetStrongReference();
  712. if (stackFrames != nullptr)
  713. {
  714. int count = stackFrames->Count();
  715. for (int frameIndex = 0; frameIndex < count; ++frameIndex)
  716. {
  717. Js::DiagStackFrame* stackFrame = stackFrames->Peek(frameIndex);
  718. stackList->Add(stackFrame);
  719. }
  720. }
  721. framePointers->ReleaseStrongReference();
  722. HeapDelete(framePointers);
  723. }
  724. }
  725. // Frames can be from multiple contexts, sort them based on stack address
  726. stackList->Sort(DiagStackFrameSorter, (void*)dispatchHaltFrameAddress);
  727. stackTraceArray = scriptContext->GetLibrary()->CreateArray(stackList->Count(), stackList->Count());
  728. stackList->Map([&](int index, Js::DiagStackFrame* stackFrame)
  729. {
  730. AssertMsg(index != 0 || stackFrame->IsTopFrame(), "Index 0 frame is not marked as top frame");
  731. Js::DynamicObject* stackTraceObject = this->GetStackFrame(stackFrame, index);
  732. Js::Var marshaledObj = Js::CrossSite::MarshalVar(scriptContext, stackTraceObject);
  733. stackTraceArray->DirectSetItemAt(index, marshaledObj);
  734. });
  735. Adelete(this->jsrtDebugManager->GetDebugObjectArena(), stackList);
  736. }
  737. else
  738. {
  739. // Empty array
  740. stackTraceArray = scriptContext->GetLibrary()->CreateArray(0, 0);
  741. }
  742. return stackTraceArray;
  743. }
  744. bool JsrtDebugStackFrames::TryGetFrameObjectFromFrameIndex(uint frameIndex, JsrtDebuggerStackFrame ** debuggerStackFrame)
  745. {
  746. if (this->framesDictionary != nullptr)
  747. {
  748. return this->framesDictionary->TryGetValue(frameIndex, debuggerStackFrame);
  749. }
  750. return false;
  751. }
  752. Js::DynamicObject * JsrtDebugStackFrames::GetStackFrame(Js::DiagStackFrame * stackFrame, uint frameIndex)
  753. {
  754. JsrtDebuggerStackFrame* debuggerStackFrame = Anew(this->jsrtDebugManager->GetDebugObjectArena(), JsrtDebuggerStackFrame, this->jsrtDebugManager->GetDebuggerObjectsManager(), stackFrame, frameIndex);
  755. Assert(this->framesDictionary != nullptr);
  756. this->framesDictionary->Add(frameIndex, debuggerStackFrame);
  757. return debuggerStackFrame->GetJSONObject(stackFrame->GetScriptContext());
  758. }
  759. void JsrtDebugStackFrames::ClearFrameDictionary()
  760. {
  761. if (this->framesDictionary != nullptr)
  762. {
  763. this->framesDictionary->Map([this](uint handle, JsrtDebuggerStackFrame* debuggerStackFrame) {
  764. Adelete(this->jsrtDebugManager->GetDebugObjectArena(), debuggerStackFrame);
  765. });
  766. this->framesDictionary->Clear();
  767. }
  768. }
  769. #endif