DiagStackFrame.cpp 25 KB

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