DebugContext.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. namespace Js
  8. {
  9. DebugContext::DebugContext(Js::ScriptContext * scriptContext) :
  10. scriptContext(scriptContext),
  11. hostDebugContext(nullptr),
  12. diagProbesContainer(nullptr),
  13. isClosed(false),
  14. debuggerMode(DebuggerMode::NotDebugging),
  15. isReparsingSource(false),
  16. isDebuggerRecording(true)
  17. {
  18. Assert(scriptContext != nullptr);
  19. }
  20. DebugContext::~DebugContext()
  21. {
  22. Assert(this->scriptContext != nullptr);
  23. Assert(this->hostDebugContext == nullptr);
  24. Assert(this->diagProbesContainer == nullptr);
  25. }
  26. void DebugContext::Initialize()
  27. {
  28. Assert(this->diagProbesContainer == nullptr);
  29. this->diagProbesContainer = HeapNew(ProbeContainer);
  30. this->diagProbesContainer->Initialize(this->scriptContext);
  31. }
  32. void DebugContext::Close()
  33. {
  34. if (this->isClosed)
  35. {
  36. return;
  37. }
  38. AssertMsg(this->scriptContext->IsActuallyClosed(), "Closing DebugContext before ScriptContext close might have consequences");
  39. this->isClosed = true;
  40. // Release all memory and do all cleanup. No operation should be done after isClosed is set
  41. Assert(this->scriptContext != nullptr);
  42. if (this->diagProbesContainer != nullptr)
  43. {
  44. this->diagProbesContainer->Close();
  45. HeapDelete(this->diagProbesContainer);
  46. this->diagProbesContainer = nullptr;
  47. }
  48. if (this->hostDebugContext != nullptr)
  49. {
  50. this->hostDebugContext->Delete();
  51. this->hostDebugContext = nullptr;
  52. }
  53. }
  54. bool DebugContext::IsSelfOrScriptContextClosed() const
  55. {
  56. return (this->IsClosed() || this->scriptContext->IsClosed());
  57. }
  58. void DebugContext::SetHostDebugContext(HostDebugContext * hostDebugContext)
  59. {
  60. Assert(this->hostDebugContext == nullptr);
  61. Assert(hostDebugContext != nullptr);
  62. this->hostDebugContext = hostDebugContext;
  63. }
  64. bool DebugContext::CanRegisterFunction() const
  65. {
  66. if (this->IsSelfOrScriptContextClosed() ||
  67. this->hostDebugContext == nullptr ||
  68. this->IsDebugContextInNonDebugMode())
  69. {
  70. return false;
  71. }
  72. return true;
  73. }
  74. void DebugContext::RegisterFunction(Js::ParseableFunctionInfo * func, LPCWSTR title)
  75. {
  76. if (!this->CanRegisterFunction())
  77. {
  78. return;
  79. }
  80. this->RegisterFunction(func, func->GetHostSourceContext(), title);
  81. }
  82. void DebugContext::RegisterFunction(Js::ParseableFunctionInfo * func, DWORD_PTR dwDebugSourceContext, LPCWSTR title)
  83. {
  84. if (!this->CanRegisterFunction())
  85. {
  86. return;
  87. }
  88. FunctionBody * functionBody = nullptr;
  89. if (func->IsDeferredParseFunction())
  90. {
  91. HRESULT hr = S_OK;
  92. Assert(!this->scriptContext->GetThreadContext()->IsScriptActive());
  93. BEGIN_JS_RUNTIME_CALL_EX_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NESTED(this->scriptContext, false)
  94. {
  95. functionBody = func->Parse();
  96. }
  97. END_JS_RUNTIME_CALL_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT(hr);
  98. if (FAILED(hr))
  99. {
  100. return;
  101. }
  102. }
  103. else
  104. {
  105. functionBody = func->GetFunctionBody();
  106. }
  107. this->RegisterFunction(functionBody, dwDebugSourceContext, title);
  108. }
  109. void DebugContext::RegisterFunction(Js::FunctionBody * functionBody, DWORD_PTR dwDebugSourceContext, LPCWSTR title)
  110. {
  111. if (!this->CanRegisterFunction())
  112. {
  113. return;
  114. }
  115. this->hostDebugContext->DbgRegisterFunction(this->scriptContext, functionBody, dwDebugSourceContext, title);
  116. }
  117. // Sets the specified mode for the debugger. The mode is used to inform
  118. // the runtime of whether or not functions should be JITed or interpreted
  119. // when they are defer parsed.
  120. // Note: Transitions back to NotDebugging are not allowed. Once the debugger
  121. // is in SourceRundown or Debugging mode, it can only transition between those
  122. // two modes.
  123. void DebugContext::SetDebuggerMode(DebuggerMode mode)
  124. {
  125. if (this->debuggerMode == mode)
  126. {
  127. // Already in this mode so return.
  128. return;
  129. }
  130. if (mode == DebuggerMode::NotDebugging)
  131. {
  132. AssertMsg(false, "Transitioning to non-debug mode is not allowed.");
  133. return;
  134. }
  135. this->debuggerMode = mode;
  136. }
  137. HRESULT DebugContext::RundownSourcesAndReparse(bool shouldPerformSourceRundown, bool shouldReparseFunctions)
  138. {
  139. struct AutoRestoreIsReparsingSource
  140. {
  141. AutoRestoreIsReparsingSource(DebugContext* debugContext, bool shouldReparseFunctions)
  142. : debugContext(debugContext)
  143. , shouldReparseFunctions(shouldReparseFunctions)
  144. {
  145. if (this->shouldReparseFunctions)
  146. {
  147. this->debugContext->isReparsingSource = true;
  148. }
  149. }
  150. ~AutoRestoreIsReparsingSource()
  151. {
  152. if (this->shouldReparseFunctions)
  153. {
  154. this->debugContext->isReparsingSource = false;
  155. }
  156. }
  157. private:
  158. DebugContext* debugContext;
  159. bool shouldReparseFunctions;
  160. } autoRestoreIsReparsingSource(this, shouldReparseFunctions);
  161. OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::RundownSourcesAndReparse scriptContext 0x%p, shouldPerformSourceRundown %d, shouldReparseFunctions %d\n"),
  162. this->scriptContext, shouldPerformSourceRundown, shouldReparseFunctions);
  163. Js::TempArenaAllocatorObject *tempAllocator = nullptr;
  164. JsUtil::List<Js::FunctionInfo *, Recycler>* pFunctionsToRegister = nullptr;
  165. JsUtil::List<Js::Utf8SourceInfo *, Recycler, false, Js::CopyRemovePolicy, RecyclerPointerComparer>* utf8SourceInfoList = nullptr;
  166. HRESULT hr = S_OK;
  167. ThreadContext* threadContext = this->scriptContext->GetThreadContext();
  168. BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
  169. tempAllocator = threadContext->GetTemporaryAllocator(_u("debuggerAlloc"));
  170. utf8SourceInfoList = JsUtil::List<Js::Utf8SourceInfo *, Recycler, false, Js::CopyRemovePolicy, RecyclerPointerComparer>::New(this->scriptContext->GetRecycler());
  171. this->MapUTF8SourceInfoUntil([&](Js::Utf8SourceInfo * sourceInfo) -> bool
  172. {
  173. WalkAndAddUtf8SourceInfo(sourceInfo, utf8SourceInfoList);
  174. return false;
  175. });
  176. END_TRANSLATE_OOM_TO_HRESULT(hr);
  177. if (FAILED(hr))
  178. {
  179. Assert(hr == E_OUTOFMEMORY);
  180. return hr;
  181. }
  182. utf8SourceInfoList->MapUntil([&](int index, Js::Utf8SourceInfo * sourceInfo) -> bool
  183. {
  184. if (this->IsSelfOrScriptContextClosed())
  185. {
  186. hr = E_FAIL;
  187. return true;
  188. }
  189. OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::RundownSourcesAndReparse scriptContext 0x%p, sourceInfo 0x%p, HasDebugDocument %d\n"),
  190. this->scriptContext, sourceInfo, sourceInfo->HasDebugDocument());
  191. if (sourceInfo->GetIsLibraryCode())
  192. {
  193. // Not putting the internal library code to the debug mode, but need to reinitialize execution mode limits of each
  194. // function body upon debugger detach, even for library code at the moment.
  195. if (shouldReparseFunctions)
  196. {
  197. sourceInfo->MapFunction([](Js::FunctionBody *const pFuncBody)
  198. {
  199. if (pFuncBody->IsFunctionParsed())
  200. {
  201. pFuncBody->ReinitializeExecutionModeAndLimits();
  202. }
  203. pFuncBody->UpdateEntryPointsOnDebugReparse();
  204. });
  205. }
  206. return false;
  207. }
  208. Assert(sourceInfo->GetSrcInfo() && sourceInfo->GetSrcInfo()->sourceContextInfo);
  209. #if DBG
  210. if (shouldPerformSourceRundown)
  211. {
  212. // We shouldn't have a debug document if we're running source rundown for the first time.
  213. Assert(!sourceInfo->HasDebugDocument());
  214. }
  215. #endif // DBG
  216. DWORD_PTR dwDebugHostSourceContext = Js::Constants::NoHostSourceContext;
  217. if (shouldPerformSourceRundown && this->hostDebugContext != nullptr)
  218. {
  219. dwDebugHostSourceContext = this->hostDebugContext->GetHostSourceContext(sourceInfo);
  220. }
  221. pFunctionsToRegister = sourceInfo->GetTopLevelFunctionInfoList();
  222. if (pFunctionsToRegister == nullptr || pFunctionsToRegister->Count() == 0)
  223. {
  224. // This could happen if there are no functions to re-compile.
  225. return false;
  226. }
  227. if (this->hostDebugContext != nullptr && sourceInfo->GetSourceContextInfo())
  228. {
  229. // This call goes out of engine
  230. this->hostDebugContext->SetThreadDescription(sourceInfo->GetSourceContextInfo()->url); // the HRESULT is omitted.
  231. }
  232. bool fHasDoneSourceRundown = false;
  233. for (int i = 0; i < pFunctionsToRegister->Count(); i++)
  234. {
  235. if (this->IsSelfOrScriptContextClosed())
  236. {
  237. hr = E_FAIL;
  238. return true;
  239. }
  240. Js::FunctionInfo *functionInfo = pFunctionsToRegister->Item(i);
  241. if (functionInfo == nullptr)
  242. {
  243. continue;
  244. }
  245. if (shouldReparseFunctions)
  246. {
  247. BEGIN_JS_RUNTIME_CALL_EX_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NESTED(this->scriptContext, false)
  248. {
  249. functionInfo->GetParseableFunctionInfo()->Parse();
  250. // This is the first call to the function, ensure dynamic profile info
  251. #if ENABLE_PROFILE_INFO
  252. functionInfo->GetFunctionBody()->EnsureDynamicProfileInfo();
  253. #endif
  254. }
  255. END_JS_RUNTIME_CALL_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT(hr);
  256. // Debugger attach/detach failure is catastrophic, take down the process
  257. DEBUGGER_ATTACHDETACH_FATAL_ERROR_IF_FAILED(hr);
  258. }
  259. // Parsing the function may change its FunctionProxy.
  260. Js::ParseableFunctionInfo *parseableFunctionInfo = functionInfo->GetParseableFunctionInfo();
  261. if (!fHasDoneSourceRundown && shouldPerformSourceRundown && !this->IsSelfOrScriptContextClosed())
  262. {
  263. BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
  264. {
  265. this->RegisterFunction(parseableFunctionInfo, dwDebugHostSourceContext, parseableFunctionInfo->GetSourceName());
  266. }
  267. END_TRANSLATE_OOM_TO_HRESULT(hr);
  268. DEBUGGER_ATTACHDETACH_FATAL_ERROR_IF_FAILED(hr);
  269. fHasDoneSourceRundown = true;
  270. }
  271. }
  272. if (shouldReparseFunctions)
  273. {
  274. sourceInfo->MapFunction([](Js::FunctionBody *const pFuncBody)
  275. {
  276. if (pFuncBody->IsFunctionParsed())
  277. {
  278. pFuncBody->ReinitializeExecutionModeAndLimits();
  279. }
  280. pFuncBody->UpdateEntryPointsOnDebugReparse();
  281. });
  282. }
  283. return false;
  284. });
  285. if (!this->IsSelfOrScriptContextClosed())
  286. {
  287. if (shouldPerformSourceRundown && this->scriptContext->HaveCalleeSources() && this->hostDebugContext != nullptr)
  288. {
  289. this->scriptContext->MapCalleeSources([=](Js::Utf8SourceInfo* calleeSourceInfo)
  290. {
  291. if (!this->IsSelfOrScriptContextClosed())
  292. {
  293. // This call goes out of engine
  294. this->hostDebugContext->ReParentToCaller(calleeSourceInfo);
  295. }
  296. });
  297. }
  298. }
  299. else
  300. {
  301. hr = E_FAIL;
  302. }
  303. threadContext->ReleaseTemporaryAllocator(tempAllocator);
  304. return hr;
  305. }
  306. // Create an ordered flat list of sources to reparse. Caller of a source should be added to the list before we add the source itself.
  307. void DebugContext::WalkAndAddUtf8SourceInfo(Js::Utf8SourceInfo* sourceInfo, JsUtil::List<Js::Utf8SourceInfo *, Recycler, false, Js::CopyRemovePolicy, RecyclerPointerComparer> *utf8SourceInfoList)
  308. {
  309. Js::Utf8SourceInfo* callerUtf8SourceInfo = sourceInfo->GetCallerUtf8SourceInfo();
  310. if (callerUtf8SourceInfo)
  311. {
  312. Js::ScriptContext* callerScriptContext = callerUtf8SourceInfo->GetScriptContext();
  313. OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::WalkAndAddUtf8SourceInfo scriptContext 0x%p, sourceInfo 0x%p, callerUtf8SourceInfo 0x%p, sourceInfo scriptContext 0x%p, callerUtf8SourceInfo scriptContext 0x%p\n"),
  314. this->scriptContext, sourceInfo, callerUtf8SourceInfo, sourceInfo->GetScriptContext(), callerScriptContext);
  315. if (sourceInfo->GetScriptContext() == callerScriptContext)
  316. {
  317. WalkAndAddUtf8SourceInfo(callerUtf8SourceInfo, utf8SourceInfoList);
  318. }
  319. else if (callerScriptContext->IsScriptContextInNonDebugMode())
  320. {
  321. // The caller scriptContext is not in run down/debug mode so let's save the relationship so that we can re-parent callees afterwards.
  322. callerScriptContext->AddCalleeSourceInfoToList(sourceInfo);
  323. }
  324. }
  325. if (!utf8SourceInfoList->Contains(sourceInfo))
  326. {
  327. OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::WalkAndAddUtf8SourceInfo Adding to utf8SourceInfoList scriptContext 0x%p, sourceInfo 0x%p, sourceInfo scriptContext 0x%p\n"),
  328. this->scriptContext, sourceInfo, sourceInfo->GetScriptContext());
  329. #if DBG
  330. bool found = false;
  331. this->MapUTF8SourceInfoUntil([&](Js::Utf8SourceInfo * sourceInfoTemp) -> bool
  332. {
  333. if (sourceInfoTemp == sourceInfo)
  334. {
  335. found = true;
  336. }
  337. return found;
  338. });
  339. AssertMsg(found, "Parented eval feature have extra source");
  340. #endif
  341. utf8SourceInfoList->Add(sourceInfo);
  342. }
  343. }
  344. template<class TMapFunction>
  345. void DebugContext::MapUTF8SourceInfoUntil(TMapFunction map)
  346. {
  347. this->scriptContext->MapScript([=](Js::Utf8SourceInfo* sourceInfo) -> bool {
  348. return map(sourceInfo);
  349. });
  350. }
  351. }
  352. #endif