JsrtDebuggerObject.cpp 34 KB

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