DiagStackFrame.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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. varResult = CALL_FUNCTION(pfuncScript->GetScriptContext()->GetThreadContext(), pfuncScript, CallInfo(1), varThis);
  307. debugManager->UpdateConsoleScope(dummyObject, scriptContext);
  308. // 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.
  309. uint32 count = activeScopeObject->GetPropertyCount();
  310. #if DBG
  311. Assert(countForVerification == count);
  312. #endif
  313. for (uint32 i = 0; i < count; i++)
  314. {
  315. Js::PropertyId propertyId = activeScopeObject->GetPropertyId((Js::PropertyIndex)i);
  316. if (propertyId != Js::Constants::NoProperty)
  317. {
  318. Js::Var value = nullptr;
  319. if (Js::JavascriptOperators::GetProperty(activeScopeObject, propertyId, &value, scriptContext))
  320. {
  321. Js::IDiagObjectAddress * pAddress = nullptr;
  322. if (propIdtoDiagAddressMap->TryGetValue(propertyId, &pAddress))
  323. {
  324. Assert(pAddress);
  325. if (pAddress->GetValue(FALSE) != value)
  326. {
  327. pAddress->Set(value);
  328. }
  329. }
  330. }
  331. }
  332. }
  333. return varResult;
  334. }
  335. Var DiagStackFrame::GetInnerScopeFromRegSlot(RegSlot location)
  336. {
  337. return GetNonVarRegValue(location);
  338. }
  339. DiagInterpreterStackFrame::DiagInterpreterStackFrame(InterpreterStackFrame* frame) :
  340. m_interpreterFrame(frame)
  341. {
  342. Assert(m_interpreterFrame != NULL);
  343. AssertMsg(m_interpreterFrame->GetScriptContext() && m_interpreterFrame->GetScriptContext()->IsScriptContextInDebugMode(),
  344. "This only supports interpreter stack frames running in debug mode.");
  345. }
  346. JavascriptFunction* DiagInterpreterStackFrame::GetJavascriptFunction()
  347. {
  348. return m_interpreterFrame->GetJavascriptFunction();
  349. }
  350. ScriptContext* DiagInterpreterStackFrame::GetScriptContext()
  351. {
  352. return m_interpreterFrame->GetScriptContext();
  353. }
  354. int DiagInterpreterStackFrame::GetByteCodeOffset()
  355. {
  356. return m_interpreterFrame->GetReader()->GetCurrentOffset();
  357. }
  358. // Address on stack that belongs to current frame.
  359. // Currently we only use this to determine which of given frames is above/below another one.
  360. DWORD_PTR DiagInterpreterStackFrame::GetStackAddress()
  361. {
  362. return m_interpreterFrame->GetStackAddress();
  363. }
  364. bool DiagInterpreterStackFrame::IsInterpreterFrame()
  365. {
  366. return true;
  367. }
  368. InterpreterStackFrame* DiagInterpreterStackFrame::AsInterpreterFrame()
  369. {
  370. return m_interpreterFrame;
  371. }
  372. Var DiagInterpreterStackFrame::GetRegValue(RegSlot slotId, bool allowTemp)
  373. {
  374. return m_interpreterFrame->GetReg(slotId);
  375. }
  376. Var DiagInterpreterStackFrame::GetNonVarRegValue(RegSlot slotId)
  377. {
  378. return m_interpreterFrame->GetNonVarReg(slotId);
  379. }
  380. void DiagInterpreterStackFrame::SetRegValue(RegSlot slotId, Var value)
  381. {
  382. m_interpreterFrame->SetReg(slotId, value);
  383. }
  384. Var DiagInterpreterStackFrame::GetArgumentsObject()
  385. {
  386. return m_interpreterFrame->GetArgumentsObject();
  387. }
  388. Var DiagInterpreterStackFrame::CreateHeapArguments()
  389. {
  390. return m_interpreterFrame->CreateHeapArguments(GetScriptContext());
  391. }
  392. FrameDisplay * DiagInterpreterStackFrame::GetFrameDisplay()
  393. {
  394. return m_interpreterFrame->GetFrameDisplayForNestedFunc();
  395. }
  396. Var DiagInterpreterStackFrame::GetInnerScopeFromRegSlot(RegSlot location)
  397. {
  398. return m_interpreterFrame->InnerScopeFromRegSlot(location);
  399. }
  400. #if ENABLE_NATIVE_CODEGEN
  401. DiagNativeStackFrame::DiagNativeStackFrame(
  402. ScriptFunction* function,
  403. int byteCodeOffset,
  404. void* stackAddr,
  405. void *codeAddr) :
  406. m_function(function),
  407. m_byteCodeOffset(byteCodeOffset),
  408. m_stackAddr(stackAddr),
  409. m_localVarSlotsOffset(InvalidOffset),
  410. m_localVarChangedOffset(InvalidOffset)
  411. {
  412. Assert(m_stackAddr != NULL);
  413. AssertMsg(m_function && m_function->GetScriptContext() && m_function->GetScriptContext()->IsScriptContextInDebugMode(),
  414. "This only supports functions in debug mode.");
  415. FunctionEntryPointInfo * entryPointInfo = GetFunction()->GetEntryPointFromNativeAddress((DWORD_PTR)codeAddr);
  416. if (entryPointInfo)
  417. {
  418. m_localVarSlotsOffset = entryPointInfo->localVarSlotsOffset;
  419. m_localVarChangedOffset = entryPointInfo->localVarChangedOffset;
  420. }
  421. else
  422. {
  423. AssertMsg(FALSE, "Failed to get entry point for native address. Most likely the frame is old/gone.");
  424. }
  425. OUTPUT_TRACE(Js::DebuggerPhase, _u("DiagNativeStackFrame::DiagNativeStackFrame: e.p(addr %p)=%p varOff=%d changedOff=%d\n"), codeAddr, entryPointInfo, m_localVarSlotsOffset, m_localVarChangedOffset);
  426. }
  427. JavascriptFunction* DiagNativeStackFrame::GetJavascriptFunction()
  428. {
  429. return m_function;
  430. }
  431. ScriptContext* DiagNativeStackFrame::GetScriptContext()
  432. {
  433. return m_function->GetScriptContext();
  434. }
  435. int DiagNativeStackFrame::GetByteCodeOffset()
  436. {
  437. return m_byteCodeOffset;
  438. }
  439. // Address on stack that belongs to current frame.
  440. // Currently we only use this to determine which of given frames is above/below another one.
  441. DWORD_PTR DiagNativeStackFrame::GetStackAddress()
  442. {
  443. return reinterpret_cast<DWORD_PTR>(m_stackAddr);
  444. }
  445. Var DiagNativeStackFrame::GetRegValue(RegSlot slotId, bool allowTemp)
  446. {
  447. Js::Var *varPtr = GetSlotOffsetLocation(slotId, allowTemp);
  448. return (varPtr != NULL) ? *varPtr : NULL;
  449. }
  450. Var * DiagNativeStackFrame::GetSlotOffsetLocation(RegSlot slotId, bool allowTemp)
  451. {
  452. Assert(GetFunction() != NULL);
  453. int32 slotOffset;
  454. if (GetFunction()->GetSlotOffset(slotId, &slotOffset, allowTemp))
  455. {
  456. Assert(m_localVarSlotsOffset != InvalidOffset);
  457. slotOffset = m_localVarSlotsOffset + slotOffset;
  458. // We will have the var offset only (which is always the Var size. With TypeSpecialization, below will change to accommodate double offset.
  459. return (Js::Var *)(((char *)m_stackAddr) + slotOffset);
  460. }
  461. Assert(false);
  462. return NULL;
  463. }
  464. Var DiagNativeStackFrame::GetNonVarRegValue(RegSlot slotId)
  465. {
  466. return GetRegValue(slotId);
  467. }
  468. void DiagNativeStackFrame::SetRegValue(RegSlot slotId, Var value)
  469. {
  470. Js::Var *varPtr = GetSlotOffsetLocation(slotId);
  471. Assert(varPtr != NULL);
  472. // First assign the value
  473. *varPtr = value;
  474. Assert(m_localVarChangedOffset != InvalidOffset);
  475. // Now change the bit in the stack which tells that current stack values got changed.
  476. char *stackOffset = (((char *)m_stackAddr) + m_localVarChangedOffset);
  477. Assert(*stackOffset == 0 || *stackOffset == FunctionBody::LocalsChangeDirtyValue);
  478. *stackOffset = FunctionBody::LocalsChangeDirtyValue;
  479. }
  480. Var DiagNativeStackFrame::GetArgumentsObject()
  481. {
  482. return (Var)((void **)m_stackAddr)[JavascriptFunctionArgIndex_ArgumentsObject];
  483. }
  484. Var DiagNativeStackFrame::CreateHeapArguments()
  485. {
  486. // We would be creating the arguments object if there is no default arguments object present.
  487. Assert(GetArgumentsObject() == NULL);
  488. CallInfo const * callInfo = (CallInfo const *)&(((void **)m_stackAddr)[JavascriptFunctionArgIndex_CallInfo]);
  489. // At the least we will have 'this' by default.
  490. Assert(callInfo->Count > 0);
  491. // Get the passed parameter's position (which is starting from 'this')
  492. Var * inParams = (Var *)&(((void **)m_stackAddr)[JavascriptFunctionArgIndex_This]);
  493. return JavascriptOperators::LoadHeapArguments(
  494. m_function,
  495. callInfo->Count - 1,
  496. &inParams[1],
  497. GetScriptContext()->GetLibrary()->GetNull(),
  498. (PropertyId*)GetScriptContext()->GetLibrary()->GetNull(),
  499. GetScriptContext(),
  500. /* formalsAreLetDecls */ false);
  501. }
  502. #endif
  503. DiagRuntimeStackFrame::DiagRuntimeStackFrame(JavascriptFunction* function, PCWSTR displayName, void* stackAddr):
  504. m_function(function),
  505. m_displayName(displayName),
  506. m_stackAddr(stackAddr)
  507. {
  508. }
  509. JavascriptFunction* DiagRuntimeStackFrame::GetJavascriptFunction()
  510. {
  511. return m_function;
  512. }
  513. PCWSTR DiagRuntimeStackFrame::GetDisplayName()
  514. {
  515. return m_displayName;
  516. }
  517. DWORD_PTR DiagRuntimeStackFrame::GetStackAddress()
  518. {
  519. return reinterpret_cast<DWORD_PTR>(m_stackAddr);
  520. }
  521. int DiagRuntimeStackFrame::GetByteCodeOffset()
  522. {
  523. return 0;
  524. }
  525. Var DiagRuntimeStackFrame::GetRegValue(RegSlot slotId, bool allowTemp)
  526. {
  527. return nullptr;
  528. }
  529. Var DiagRuntimeStackFrame::GetNonVarRegValue(RegSlot slotId)
  530. {
  531. return nullptr;
  532. }
  533. void DiagRuntimeStackFrame::SetRegValue(RegSlot slotId, Var value)
  534. {
  535. }
  536. Var DiagRuntimeStackFrame::GetArgumentsObject()
  537. {
  538. return nullptr;
  539. }
  540. Var DiagRuntimeStackFrame::CreateHeapArguments()
  541. {
  542. return nullptr;
  543. }
  544. } // namespace Js
  545. #endif