DiagStackFrame.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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 "RuntimeDebugPch.h"
  6. #ifdef ENABLE_SCRIPT_DEBUGGING
  7. #include "Language/JavascriptFunctionArgIndex.h"
  8. #include "Language/InterpreterStackFrame.h"
  9. #include "Language/JavascriptStackWalker.h"
  10. namespace Js
  11. {
  12. DiagStackFrame::DiagStackFrame():
  13. isTopFrame(false)
  14. {
  15. }
  16. // Returns whether or not this frame is on the top of the callstack.
  17. bool DiagStackFrame::IsTopFrame()
  18. {
  19. return this->isTopFrame && GetScriptContext()->GetDebugContext()->GetProbeContainer()->IsPrimaryBrokenToDebuggerContext();
  20. }
  21. void DiagStackFrame::SetIsTopFrame()
  22. {
  23. this->isTopFrame = true;
  24. }
  25. ScriptFunction* DiagStackFrame::GetScriptFunction()
  26. {
  27. return ScriptFunction::FromVar(GetJavascriptFunction());
  28. }
  29. FunctionBody* DiagStackFrame::GetFunction()
  30. {
  31. return GetJavascriptFunction()->GetFunctionBody();
  32. }
  33. ScriptContext* DiagStackFrame::GetScriptContext()
  34. {
  35. return GetJavascriptFunction()->GetScriptContext();
  36. }
  37. PCWSTR DiagStackFrame::GetDisplayName()
  38. {
  39. return GetFunction()->GetExternalDisplayName();
  40. }
  41. bool DiagStackFrame::IsInterpreterFrame()
  42. {
  43. return false;
  44. }
  45. InterpreterStackFrame* DiagStackFrame::AsInterpreterFrame()
  46. {
  47. AssertMsg(FALSE, "AsInterpreterFrame called for non-interpreter frame.");
  48. return nullptr;
  49. }
  50. ArenaAllocator * DiagStackFrame::GetArena()
  51. {
  52. Assert(GetScriptContext() != NULL);
  53. return GetScriptContext()->GetThreadContext()->GetDebugManager()->GetDiagnosticArena()->Arena();
  54. }
  55. FrameDisplay * DiagStackFrame::GetFrameDisplay()
  56. {
  57. FrameDisplay *display = NULL;
  58. Assert(this->GetFunction() != NULL);
  59. RegSlot frameDisplayReg = this->GetFunction()->GetFrameDisplayRegister();
  60. if (frameDisplayReg != Js::Constants::NoRegister && frameDisplayReg != 0)
  61. {
  62. display = (FrameDisplay*)this->GetNonVarRegValue(frameDisplayReg);
  63. }
  64. else
  65. {
  66. display = this->GetScriptFunction()->GetEnvironment();
  67. }
  68. return display;
  69. }
  70. Var DiagStackFrame::GetScopeObjectFromFrameDisplay(uint index)
  71. {
  72. FrameDisplay * display = GetFrameDisplay();
  73. return (display != NULL && display->GetLength() > index) ? display->GetItem(index) : NULL;
  74. }
  75. Var DiagStackFrame::GetRootObject()
  76. {
  77. Assert(this->GetFunction());
  78. return this->GetFunction()->LoadRootObject();
  79. }
  80. BOOL DiagStackFrame::IsStrictMode()
  81. {
  82. Js::JavascriptFunction* scopeFunction = this->GetJavascriptFunction();
  83. return scopeFunction->IsStrictMode();
  84. }
  85. BOOL DiagStackFrame::IsThisAvailable()
  86. {
  87. Js::JavascriptFunction* scopeFunction = this->GetJavascriptFunction();
  88. return !scopeFunction->IsLambda() || scopeFunction->GetParseableFunctionInfo()->GetCapturesThis();
  89. }
  90. Js::Var DiagStackFrame::GetThisFromFrame(Js::IDiagObjectAddress ** ppOutAddress, Js::IDiagObjectModelWalkerBase * localsWalker)
  91. {
  92. Js::ScriptContext* scriptContext = this->GetScriptContext();
  93. Js::JavascriptFunction* scopeFunction = this->GetJavascriptFunction();
  94. Js::ModuleID moduleId = scopeFunction->IsScriptFunction() ? scopeFunction->GetFunctionBody()->GetModuleID() : 0;
  95. Js::Var varThis = scriptContext->GetLibrary()->GetNull();
  96. if (!scopeFunction->IsLambda())
  97. {
  98. Js::JavascriptStackWalker::GetThis(&varThis, moduleId, scopeFunction, scriptContext);
  99. }
  100. else
  101. {
  102. if (!scopeFunction->GetParseableFunctionInfo()->GetCapturesThis())
  103. {
  104. return nullptr;
  105. }
  106. else
  107. {
  108. // Emulate Js::JavascriptOperators::OP_GetThisScoped using a locals walker and assigning moduleId object if not found by locals walker
  109. if (localsWalker == nullptr)
  110. {
  111. ArenaAllocator *arena = scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena()->Arena();
  112. localsWalker = Anew(arena, Js::LocalsWalker, this, Js::FrameWalkerFlags::FW_EnumWithScopeAlso | Js::FrameWalkerFlags::FW_AllowLexicalThis);
  113. }
  114. bool unused = false;
  115. Js::IDiagObjectAddress* address = localsWalker->FindPropertyAddress(Js::PropertyIds::_this, unused);
  116. if (ppOutAddress != nullptr)
  117. {
  118. *ppOutAddress = address;
  119. }
  120. if (address != nullptr)
  121. {
  122. varThis = address->GetValue(FALSE);
  123. }
  124. else if (moduleId == kmodGlobal)
  125. {
  126. varThis = Js::JavascriptOperators::OP_LdRoot(scriptContext)->ToThis();
  127. }
  128. else
  129. {
  130. varThis = (Var)Js::JavascriptOperators::GetModuleRoot(moduleId, scriptContext);
  131. }
  132. }
  133. }
  134. Js::GlobalObject::UpdateThisForEval(varThis, moduleId, scriptContext, this->IsStrictMode());
  135. return varThis;
  136. }
  137. void DiagStackFrame::TryFetchValueAndAddress(const char16 *source, int sourceLength, Js::ResolvedObject * pOutResolvedObj)
  138. {
  139. Assert(source);
  140. Assert(pOutResolvedObj);
  141. Js::ScriptContext* scriptContext = this->GetScriptContext();
  142. Js::PropertyRecord const * propRecord;
  143. scriptContext->FindPropertyRecord(source, sourceLength, &propRecord);
  144. if (propRecord != nullptr)
  145. {
  146. ArenaAllocator *arena = scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena()->Arena();
  147. Js::IDiagObjectModelWalkerBase * localsWalker = Anew(arena, Js::LocalsWalker, this, Js::FrameWalkerFlags::FW_EnumWithScopeAlso);
  148. bool isConst = false;
  149. pOutResolvedObj->address = localsWalker->FindPropertyAddress(propRecord->GetPropertyId(), isConst);
  150. if (pOutResolvedObj->address != nullptr)
  151. {
  152. pOutResolvedObj->obj = pOutResolvedObj->address->GetValue(FALSE);
  153. pOutResolvedObj->isConst = isConst;
  154. }
  155. }
  156. }
  157. Js::ScriptFunction* DiagStackFrame::TryGetFunctionForEval(Js::ScriptContext* scriptContext, const char16 *source, int sourceLength, BOOL isLibraryCode /* = FALSE */)
  158. {
  159. // TODO: pass the real length of the source code instead of wcslen
  160. uint32 grfscr = fscrReturnExpression | fscrEval | fscrEvalCode | fscrGlobalCode | fscrConsoleScopeEval;
  161. if (!this->IsThisAvailable())
  162. {
  163. grfscr |= fscrDebuggerErrorOnGlobalThis;
  164. }
  165. if (isLibraryCode)
  166. {
  167. grfscr |= fscrIsLibraryCode;
  168. }
  169. return scriptContext->GetGlobalObject()->EvalHelper(scriptContext, source, sourceLength, kmodGlobal, grfscr, Js::Constants::EvalCode, FALSE, FALSE, this->IsStrictMode());
  170. }
  171. void DiagStackFrame::EvaluateImmediate(const char16 *source, int sourceLength, BOOL isLibraryCode, Js::ResolvedObject * resolvedObject)
  172. {
  173. this->TryFetchValueAndAddress(source, sourceLength, resolvedObject);
  174. if (resolvedObject->obj == nullptr)
  175. {
  176. Js::ScriptFunction* pfuncScript = this->TryGetFunctionForEval(this->GetScriptContext(), source, sourceLength, isLibraryCode);
  177. if (pfuncScript != nullptr)
  178. {
  179. // Passing the nonuser code state from the enclosing function to the current function.
  180. // Treat native library frame (no function body) as non-user code.
  181. Js::FunctionBody* body = this->GetFunction();
  182. if (!body || body->IsNonUserCode())
  183. {
  184. Js::FunctionBody *pCurrentFuncBody = pfuncScript->GetFunctionBody();
  185. if (pCurrentFuncBody != nullptr)
  186. {
  187. pCurrentFuncBody->SetIsNonUserCode(true);
  188. }
  189. }
  190. OUTPUT_TRACE(Js::ConsoleScopePhase, _u("EvaluateImmediate strict = %d, libraryCode = %d, source = '%s'\n"),
  191. this->IsStrictMode(), isLibraryCode, source);
  192. resolvedObject->obj = this->DoEval(pfuncScript);
  193. }
  194. }
  195. }
  196. #ifdef ENABLE_MUTATION_BREAKPOINT
  197. static void SetConditionalMutationBreakpointVariables(Js::DynamicObject * activeScopeObject, Js::ScriptContext * scriptContext)
  198. {
  199. // For Conditional Object Mutation Breakpoint user can access the new value, changing property name and mutation type using special variables
  200. // $newValue$, $propertyName$ and $mutationType$. Add this variables to activation object.
  201. Js::DebugManager* debugManager = scriptContext->GetDebugContext()->GetProbeContainer()->GetDebugManager();
  202. Js::MutationBreakpoint *mutationBreakpoint = debugManager->GetActiveMutationBreakpoint();
  203. if (mutationBreakpoint != nullptr)
  204. {
  205. if (Js::Constants::NoProperty == debugManager->mutationNewValuePid)
  206. {
  207. debugManager->mutationNewValuePid = scriptContext->GetOrAddPropertyIdTracked(_u("$newValue$"), 10);
  208. }
  209. if (Js::Constants::NoProperty == debugManager->mutationPropertyNamePid)
  210. {
  211. debugManager->mutationPropertyNamePid = scriptContext->GetOrAddPropertyIdTracked(_u("$propertyName$"), 14);
  212. }
  213. if (Js::Constants::NoProperty == debugManager->mutationTypePid)
  214. {
  215. debugManager->mutationTypePid = scriptContext->GetOrAddPropertyIdTracked(_u("$mutationType$"), 14);
  216. }
  217. AssertMsg(debugManager->mutationNewValuePid != Js::Constants::NoProperty, "Should have a valid mutationNewValuePid");
  218. AssertMsg(debugManager->mutationPropertyNamePid != Js::Constants::NoProperty, "Should have a valid mutationPropertyNamePid");
  219. AssertMsg(debugManager->mutationTypePid != Js::Constants::NoProperty, "Should have a valid mutationTypePid");
  220. Js::Var newValue = mutationBreakpoint->GetBreakNewValueVar();
  221. // Incase of MutationTypeDelete we won't have new value
  222. if (nullptr != newValue)
  223. {
  224. activeScopeObject->SetProperty(debugManager->mutationNewValuePid,
  225. mutationBreakpoint->GetBreakNewValueVar(),
  226. Js::PropertyOperationFlags::PropertyOperation_None,
  227. nullptr);
  228. }
  229. else
  230. {
  231. activeScopeObject->SetProperty(debugManager->mutationNewValuePid,
  232. scriptContext->GetLibrary()->GetUndefined(),
  233. Js::PropertyOperationFlags::PropertyOperation_None,
  234. nullptr);
  235. }
  236. // User should not be able to change $propertyName$ and $mutationType$ variables
  237. // Since we don't have address for $propertyName$ and $mutationType$ even if user change these varibales it won't be reflected after eval
  238. // But declaring these as const to prevent accidental typos by user so that we throw error in case user changes these variables
  239. Js::PropertyOperationFlags flags = static_cast<Js::PropertyOperationFlags>(Js::PropertyOperation_SpecialValue | Js::PropertyOperation_AllowUndecl);
  240. activeScopeObject->SetPropertyWithAttributes(debugManager->mutationPropertyNamePid,
  241. Js::JavascriptString::NewCopySz(mutationBreakpoint->GetBreakPropertyName(), scriptContext),
  242. PropertyConstDefaults, nullptr, flags);
  243. activeScopeObject->SetPropertyWithAttributes(debugManager->mutationTypePid,
  244. Js::JavascriptString::NewCopySz(mutationBreakpoint->GetMutationTypeForConditionalEval(mutationBreakpoint->GetBreakMutationType()), scriptContext),
  245. PropertyConstDefaults, nullptr, flags);
  246. }
  247. }
  248. #endif
  249. Js::Var DiagStackFrame::DoEval(Js::ScriptFunction* pfuncScript)
  250. {
  251. Js::Var varResult = nullptr;
  252. Js::JavascriptFunction* scopeFunction = this->GetJavascriptFunction();
  253. Js::ScriptContext* scriptContext = this->GetScriptContext();
  254. ArenaAllocator *arena = scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticArena()->Arena();
  255. Js::LocalsWalker *localsWalker = Anew(arena, Js::LocalsWalker, this,
  256. Js::FrameWalkerFlags::FW_EnumWithScopeAlso | Js::FrameWalkerFlags::FW_AllowLexicalThis | Js::FrameWalkerFlags::FW_AllowSuperReference | Js::FrameWalkerFlags::FW_DontAddGlobalsDirectly);
  257. // Store the diag address of a var to the map so that it will be used for editing the value.
  258. typedef JsUtil::BaseDictionary<Js::PropertyId, Js::IDiagObjectAddress*, ArenaAllocator, PrimeSizePolicy> PropIdToDiagAddressMap;
  259. PropIdToDiagAddressMap * propIdtoDiagAddressMap = Anew(arena, PropIdToDiagAddressMap, arena);
  260. // Create one scope object and init all scope properties in it, and push this object in front of the environment.
  261. Js::DynamicObject * activeScopeObject = localsWalker->CreateAndPopulateActivationObject(scriptContext, [propIdtoDiagAddressMap](Js::ResolvedObject& resolveObject)
  262. {
  263. if (!resolveObject.isConst)
  264. {
  265. propIdtoDiagAddressMap->AddNew(resolveObject.propId, resolveObject.address);
  266. }
  267. });
  268. if (!activeScopeObject)
  269. {
  270. activeScopeObject = scriptContext->GetLibrary()->CreateActivationObject();
  271. }
  272. #ifdef ENABLE_MUTATION_BREAKPOINT
  273. SetConditionalMutationBreakpointVariables(activeScopeObject, scriptContext);
  274. #endif
  275. #if DBG
  276. uint32 countForVerification = activeScopeObject->GetPropertyCount();
  277. #endif
  278. // Dummy scope object in the front, so that no new variable will be added to the scope.
  279. Js::DynamicObject * dummyObject = scriptContext->GetLibrary()->CreateActivationObject();
  280. // Remove its prototype object so that those item will not be visible to the expression evaluation.
  281. dummyObject->SetPrototype(scriptContext->GetLibrary()->GetNull());
  282. Js::DebugManager* debugManager = scriptContext->GetDebugContext()->GetProbeContainer()->GetDebugManager();
  283. Js::FrameDisplay* env = debugManager->GetFrameDisplay(scriptContext, dummyObject, activeScopeObject);
  284. pfuncScript->SetEnvironment(env);
  285. Js::Var varThis = this->GetThisFromFrame(nullptr, localsWalker);
  286. if (varThis == nullptr)
  287. {
  288. Assert(scopeFunction->IsLambda());
  289. Assert(!scopeFunction->GetParseableFunctionInfo()->GetCapturesThis());
  290. varThis = scriptContext->GetLibrary()->GetNull();
  291. }
  292. // If there was no 'this' binding found by the LocalsWalker, let's add the 'this' value we
  293. // collected from the frame to the scope. Eval loads 'this' by walking the scope chain so
  294. // it has to have some value in the chain or we'll be stuck with the global 'this' binding value.
  295. if (!activeScopeObject->HasProperty(Js::PropertyIds::_this))
  296. {
  297. activeScopeObject->SetPropertyWithAttributes(
  298. Js::PropertyIds::_this,
  299. JavascriptOperators::BoxStackInstance(varThis, scriptContext, /* allowStackFunction */ false, /* deepCopy */ false), //The value escapes, box if necessary.
  300. PropertyConstDefaults,
  301. nullptr);
  302. #if DBG
  303. countForVerification++;
  304. #endif
  305. }
  306. ThreadContext * threadContext = pfuncScript->GetScriptContext()->GetThreadContext();
  307. BEGIN_SAFE_REENTRANT_CALL(threadContext)
  308. {
  309. varResult = CALL_FUNCTION(threadContext, pfuncScript, CallInfo(1), varThis);
  310. }
  311. END_SAFE_REENTRANT_CALL
  312. debugManager->UpdateConsoleScope(dummyObject, scriptContext);
  313. // We need to find out the edits have been done to the dummy scope object during the eval. We need to apply those mutations to the actual vars.
  314. uint32 count = activeScopeObject->GetPropertyCount();
  315. #if DBG
  316. Assert(countForVerification == count);
  317. #endif
  318. for (uint32 i = 0; i < count; i++)
  319. {
  320. Js::PropertyId propertyId = activeScopeObject->GetPropertyId((Js::PropertyIndex)i);
  321. if (propertyId != Js::Constants::NoProperty)
  322. {
  323. Js::Var value = nullptr;
  324. if (Js::JavascriptOperators::GetProperty(activeScopeObject, propertyId, &value, scriptContext))
  325. {
  326. Js::IDiagObjectAddress * pAddress = nullptr;
  327. if (propIdtoDiagAddressMap->TryGetValue(propertyId, &pAddress))
  328. {
  329. Assert(pAddress);
  330. if (pAddress->GetValue(FALSE) != value)
  331. {
  332. pAddress->Set(value);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. return varResult;
  339. }
  340. Var DiagStackFrame::GetInnerScopeFromRegSlot(RegSlot location)
  341. {
  342. return GetNonVarRegValue(location);
  343. }
  344. DiagInterpreterStackFrame::DiagInterpreterStackFrame(InterpreterStackFrame* frame) :
  345. m_interpreterFrame(frame)
  346. {
  347. Assert(m_interpreterFrame != NULL);
  348. AssertMsg(m_interpreterFrame->GetScriptContext() && m_interpreterFrame->GetScriptContext()->IsScriptContextInDebugMode(),
  349. "This only supports interpreter stack frames running in debug mode.");
  350. }
  351. JavascriptFunction* DiagInterpreterStackFrame::GetJavascriptFunction()
  352. {
  353. return m_interpreterFrame->GetJavascriptFunction();
  354. }
  355. ScriptContext* DiagInterpreterStackFrame::GetScriptContext()
  356. {
  357. return m_interpreterFrame->GetScriptContext();
  358. }
  359. int DiagInterpreterStackFrame::GetByteCodeOffset()
  360. {
  361. return m_interpreterFrame->GetReader()->GetCurrentOffset();
  362. }
  363. // Address on stack that belongs to current frame.
  364. // Currently we only use this to determine which of given frames is above/below another one.
  365. DWORD_PTR DiagInterpreterStackFrame::GetStackAddress()
  366. {
  367. return m_interpreterFrame->GetStackAddress();
  368. }
  369. bool DiagInterpreterStackFrame::IsInterpreterFrame()
  370. {
  371. return true;
  372. }
  373. InterpreterStackFrame* DiagInterpreterStackFrame::AsInterpreterFrame()
  374. {
  375. return m_interpreterFrame;
  376. }
  377. Var DiagInterpreterStackFrame::GetRegValue(RegSlot slotId, bool allowTemp)
  378. {
  379. return m_interpreterFrame->GetReg(slotId);
  380. }
  381. Var DiagInterpreterStackFrame::GetNonVarRegValue(RegSlot slotId)
  382. {
  383. return m_interpreterFrame->GetNonVarReg(slotId);
  384. }
  385. void DiagInterpreterStackFrame::SetRegValue(RegSlot slotId, Var value)
  386. {
  387. m_interpreterFrame->SetReg(slotId, value);
  388. }
  389. Var DiagInterpreterStackFrame::GetArgumentsObject()
  390. {
  391. return m_interpreterFrame->GetArgumentsObject();
  392. }
  393. Var DiagInterpreterStackFrame::CreateHeapArguments()
  394. {
  395. return m_interpreterFrame->CreateHeapArguments(GetScriptContext());
  396. }
  397. FrameDisplay * DiagInterpreterStackFrame::GetFrameDisplay()
  398. {
  399. return m_interpreterFrame->GetFrameDisplayForNestedFunc();
  400. }
  401. Var DiagInterpreterStackFrame::GetInnerScopeFromRegSlot(RegSlot location)
  402. {
  403. return m_interpreterFrame->InnerScopeFromRegSlot(location);
  404. }
  405. #if ENABLE_NATIVE_CODEGEN
  406. DiagNativeStackFrame::DiagNativeStackFrame(
  407. ScriptFunction* function,
  408. int byteCodeOffset,
  409. void* stackAddr,
  410. void *codeAddr) :
  411. m_function(function),
  412. m_byteCodeOffset(byteCodeOffset),
  413. m_stackAddr(stackAddr),
  414. m_localVarSlotsOffset(InvalidOffset),
  415. m_localVarChangedOffset(InvalidOffset)
  416. {
  417. Assert(m_stackAddr != NULL);
  418. AssertMsg(m_function && m_function->GetScriptContext() && m_function->GetScriptContext()->IsScriptContextInDebugMode(),
  419. "This only supports functions in debug mode.");
  420. FunctionEntryPointInfo * entryPointInfo = GetFunction()->GetEntryPointFromNativeAddress((DWORD_PTR)codeAddr);
  421. if (entryPointInfo)
  422. {
  423. m_localVarSlotsOffset = entryPointInfo->localVarSlotsOffset;
  424. m_localVarChangedOffset = entryPointInfo->localVarChangedOffset;
  425. }
  426. else
  427. {
  428. AssertMsg(FALSE, "Failed to get entry point for native address. Most likely the frame is old/gone.");
  429. }
  430. OUTPUT_TRACE(Js::DebuggerPhase, _u("DiagNativeStackFrame::DiagNativeStackFrame: e.p(addr %p)=%p varOff=%d changedOff=%d\n"), codeAddr, entryPointInfo, m_localVarSlotsOffset, m_localVarChangedOffset);
  431. }
  432. JavascriptFunction* DiagNativeStackFrame::GetJavascriptFunction()
  433. {
  434. return m_function;
  435. }
  436. ScriptContext* DiagNativeStackFrame::GetScriptContext()
  437. {
  438. return m_function->GetScriptContext();
  439. }
  440. int DiagNativeStackFrame::GetByteCodeOffset()
  441. {
  442. return m_byteCodeOffset;
  443. }
  444. // Address on stack that belongs to current frame.
  445. // Currently we only use this to determine which of given frames is above/below another one.
  446. DWORD_PTR DiagNativeStackFrame::GetStackAddress()
  447. {
  448. return reinterpret_cast<DWORD_PTR>(m_stackAddr);
  449. }
  450. Var DiagNativeStackFrame::GetRegValue(RegSlot slotId, bool allowTemp)
  451. {
  452. Js::Var *varPtr = GetSlotOffsetLocation(slotId, allowTemp);
  453. return (varPtr != NULL) ? *varPtr : NULL;
  454. }
  455. Var * DiagNativeStackFrame::GetSlotOffsetLocation(RegSlot slotId, bool allowTemp)
  456. {
  457. Assert(GetFunction() != NULL);
  458. int32 slotOffset;
  459. if (GetFunction()->GetSlotOffset(slotId, &slotOffset, allowTemp))
  460. {
  461. Assert(m_localVarSlotsOffset != InvalidOffset);
  462. slotOffset = m_localVarSlotsOffset + slotOffset;
  463. // We will have the var offset only (which is always the Var size. With TypeSpecialization, below will change to accommodate double offset.
  464. return (Js::Var *)(((char *)m_stackAddr) + slotOffset);
  465. }
  466. Assert(false);
  467. return NULL;
  468. }
  469. Var DiagNativeStackFrame::GetNonVarRegValue(RegSlot slotId)
  470. {
  471. return GetRegValue(slotId);
  472. }
  473. void DiagNativeStackFrame::SetRegValue(RegSlot slotId, Var value)
  474. {
  475. Js::Var *varPtr = GetSlotOffsetLocation(slotId);
  476. Assert(varPtr != NULL);
  477. // First assign the value
  478. *varPtr = value;
  479. Assert(m_localVarChangedOffset != InvalidOffset);
  480. // Now change the bit in the stack which tells that current stack values got changed.
  481. char *stackOffset = (((char *)m_stackAddr) + m_localVarChangedOffset);
  482. Assert(*stackOffset == 0 || *stackOffset == FunctionBody::LocalsChangeDirtyValue);
  483. *stackOffset = FunctionBody::LocalsChangeDirtyValue;
  484. }
  485. Var DiagNativeStackFrame::GetArgumentsObject()
  486. {
  487. return (Var)((void **)m_stackAddr)[JavascriptFunctionArgIndex_ArgumentsObject];
  488. }
  489. Var DiagNativeStackFrame::CreateHeapArguments()
  490. {
  491. // We would be creating the arguments object if there is no default arguments object present.
  492. Assert(GetArgumentsObject() == NULL);
  493. CallInfo const * callInfo = (CallInfo const *)&(((void **)m_stackAddr)[JavascriptFunctionArgIndex_CallInfo]);
  494. // At the least we will have 'this' by default.
  495. Assert(callInfo->Count > 0);
  496. // Get the passed parameter's position (which is starting from 'this')
  497. Var * inParams = (Var *)&(((void **)m_stackAddr)[JavascriptFunctionArgIndex_This]);
  498. return JavascriptOperators::LoadHeapArguments(
  499. m_function,
  500. callInfo->Count - 1,
  501. &inParams[1],
  502. GetScriptContext()->GetLibrary()->GetNull(),
  503. (PropertyId*)GetScriptContext()->GetLibrary()->GetNull(),
  504. GetScriptContext(),
  505. /* formalsAreLetDecls */ false);
  506. }
  507. #endif
  508. DiagRuntimeStackFrame::DiagRuntimeStackFrame(JavascriptFunction* function, PCWSTR displayName, void* stackAddr):
  509. m_function(function),
  510. m_displayName(displayName),
  511. m_stackAddr(stackAddr)
  512. {
  513. }
  514. JavascriptFunction* DiagRuntimeStackFrame::GetJavascriptFunction()
  515. {
  516. return m_function;
  517. }
  518. PCWSTR DiagRuntimeStackFrame::GetDisplayName()
  519. {
  520. return m_displayName;
  521. }
  522. DWORD_PTR DiagRuntimeStackFrame::GetStackAddress()
  523. {
  524. return reinterpret_cast<DWORD_PTR>(m_stackAddr);
  525. }
  526. int DiagRuntimeStackFrame::GetByteCodeOffset()
  527. {
  528. return 0;
  529. }
  530. Var DiagRuntimeStackFrame::GetRegValue(RegSlot slotId, bool allowTemp)
  531. {
  532. return nullptr;
  533. }
  534. Var DiagRuntimeStackFrame::GetNonVarRegValue(RegSlot slotId)
  535. {
  536. return nullptr;
  537. }
  538. void DiagRuntimeStackFrame::SetRegValue(RegSlot slotId, Var value)
  539. {
  540. }
  541. Var DiagRuntimeStackFrame::GetArgumentsObject()
  542. {
  543. return nullptr;
  544. }
  545. Var DiagRuntimeStackFrame::CreateHeapArguments()
  546. {
  547. return nullptr;
  548. }
  549. } // namespace Js
  550. #endif