DebugContext.cpp 14 KB

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