DebugDocument.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. DebugDocument::DebugDocument(Utf8SourceInfo* utf8SourceInfo, Js::FunctionBody* functionBody) :
  9. utf8SourceInfo(utf8SourceInfo),
  10. m_breakpointList(nullptr)
  11. {
  12. Assert(utf8SourceInfo != nullptr);
  13. if (functionBody != nullptr)
  14. {
  15. this->functionBody.Root(functionBody, this->utf8SourceInfo->GetScriptContext()->GetRecycler());
  16. }
  17. }
  18. DebugDocument::~DebugDocument()
  19. {
  20. Assert(this->utf8SourceInfo == nullptr);
  21. Assert(this->m_breakpointList == nullptr);
  22. }
  23. void DebugDocument::CloseDocument()
  24. {
  25. if (this->m_breakpointList != nullptr)
  26. {
  27. this->ClearAllBreakPoints();
  28. }
  29. Assert(this->utf8SourceInfo != nullptr);
  30. if (functionBody)
  31. {
  32. functionBody.Unroot(this->utf8SourceInfo->GetScriptContext()->GetRecycler());
  33. }
  34. this->utf8SourceInfo = nullptr;
  35. }
  36. BreakpointProbeList* DebugDocument::GetBreakpointList()
  37. {
  38. if (m_breakpointList != nullptr)
  39. {
  40. return m_breakpointList;
  41. }
  42. ScriptContext * scriptContext = this->utf8SourceInfo->GetScriptContext();
  43. if (scriptContext == nullptr || scriptContext->IsClosed())
  44. {
  45. return nullptr;
  46. }
  47. ArenaAllocator* diagnosticArena = scriptContext->AllocatorForDiagnostics();
  48. AssertMem(diagnosticArena);
  49. m_breakpointList = this->NewBreakpointList(diagnosticArena);
  50. return m_breakpointList;
  51. }
  52. BreakpointProbeList* DebugDocument::NewBreakpointList(ArenaAllocator* arena)
  53. {
  54. return BreakpointProbeList::New(arena);
  55. }
  56. HRESULT DebugDocument::SetBreakPoint(int32 ibos, BREAKPOINT_STATE breakpointState)
  57. {
  58. ScriptContext* scriptContext = this->utf8SourceInfo->GetScriptContext();
  59. if (scriptContext == nullptr || scriptContext->IsClosed())
  60. {
  61. return E_UNEXPECTED;
  62. }
  63. StatementLocation statement;
  64. if (!this->GetStatementLocation(ibos, &statement))
  65. {
  66. return E_FAIL;
  67. }
  68. this->SetBreakPoint(statement, breakpointState);
  69. return S_OK;
  70. }
  71. BreakpointProbe* DebugDocument::SetBreakPoint(StatementLocation statement, BREAKPOINT_STATE bps)
  72. {
  73. ScriptContext* scriptContext = this->utf8SourceInfo->GetScriptContext();
  74. if (scriptContext == nullptr || scriptContext->IsClosed())
  75. {
  76. return nullptr;
  77. }
  78. switch (bps)
  79. {
  80. default:
  81. AssertMsg(FALSE, "Bad breakpoint state");
  82. // Fall thru
  83. case BREAKPOINT_DISABLED:
  84. case BREAKPOINT_DELETED:
  85. {
  86. BreakpointProbeList* pBreakpointList = this->GetBreakpointList();
  87. if (pBreakpointList)
  88. {
  89. ArenaAllocator arena(_u("TemporaryBreakpointList"), scriptContext->GetThreadContext()->GetDebugManager()->GetDiagnosticPageAllocator(), Throw::OutOfMemory);
  90. BreakpointProbeList* pDeleteList = this->NewBreakpointList(&arena);
  91. pBreakpointList->Map([&statement, scriptContext, pDeleteList](int index, BreakpointProbe * breakpointProbe)
  92. {
  93. if (breakpointProbe->Matches(statement.function, statement.statement.begin))
  94. {
  95. scriptContext->GetDebugContext()->GetProbeContainer()->RemoveProbe(breakpointProbe);
  96. pDeleteList->Add(breakpointProbe);
  97. }
  98. });
  99. pDeleteList->Map([pBreakpointList](int index, BreakpointProbe * breakpointProbe)
  100. {
  101. pBreakpointList->Remove(breakpointProbe);
  102. });
  103. pDeleteList->Clear();
  104. }
  105. break;
  106. }
  107. case BREAKPOINT_ENABLED:
  108. {
  109. BreakpointProbe* pProbe = Anew(scriptContext->AllocatorForDiagnostics(), BreakpointProbe, this, statement,
  110. scriptContext->GetThreadContext()->GetDebugManager()->GetNextBreakpointId());
  111. scriptContext->GetDebugContext()->GetProbeContainer()->AddProbe(pProbe);
  112. BreakpointProbeList* pBreakpointList = this->GetBreakpointList();
  113. pBreakpointList->Add(pProbe);
  114. return pProbe;
  115. break;
  116. }
  117. }
  118. return nullptr;
  119. }
  120. void DebugDocument::RemoveBreakpointProbe(BreakpointProbe *probe)
  121. {
  122. Assert(probe);
  123. if (m_breakpointList)
  124. {
  125. m_breakpointList->Remove(probe);
  126. }
  127. }
  128. void DebugDocument::ClearAllBreakPoints(void)
  129. {
  130. if (m_breakpointList != nullptr)
  131. {
  132. m_breakpointList->Clear();
  133. m_breakpointList = nullptr;
  134. }
  135. }
  136. #if ENABLE_TTD
  137. BreakpointProbe* DebugDocument::SetBreakPoint_TTDWbpId(int64 bpId, StatementLocation statement)
  138. {
  139. ScriptContext* scriptContext = this->utf8SourceInfo->GetScriptContext();
  140. BreakpointProbe* pProbe = Anew(scriptContext->AllocatorForDiagnostics(), BreakpointProbe, this, statement, (uint32)bpId);
  141. scriptContext->GetDebugContext()->GetProbeContainer()->AddProbe(pProbe);
  142. BreakpointProbeList* pBreakpointList = this->GetBreakpointList();
  143. pBreakpointList->Add(pProbe);
  144. return pProbe;
  145. }
  146. #endif
  147. Js::BreakpointProbe* DebugDocument::FindBreakpoint(StatementLocation statement)
  148. {
  149. Js::BreakpointProbe* probe = nullptr;
  150. if (m_breakpointList != nullptr)
  151. {
  152. m_breakpointList->MapUntil([&](int index, BreakpointProbe* bpProbe) -> bool
  153. {
  154. if (bpProbe != nullptr && bpProbe->Matches(statement))
  155. {
  156. probe = bpProbe;
  157. return true;
  158. }
  159. return false;
  160. });
  161. }
  162. return probe;
  163. }
  164. bool DebugDocument::FindBPStatementLocation(UINT bpId, StatementLocation * statement)
  165. {
  166. bool foundStatement = false;
  167. if (m_breakpointList != nullptr)
  168. {
  169. m_breakpointList->MapUntil([&](int index, BreakpointProbe* bpProbe) -> bool
  170. {
  171. if (bpProbe != nullptr && bpProbe->GetId() == bpId)
  172. {
  173. bpProbe->GetStatementLocation(statement);
  174. foundStatement = true;
  175. return true;
  176. }
  177. return false;
  178. });
  179. }
  180. return foundStatement;
  181. }
  182. BOOL DebugDocument::GetStatementSpan(int32 ibos, StatementSpan* pStatement)
  183. {
  184. StatementLocation statement;
  185. if (GetStatementLocation(ibos, &statement))
  186. {
  187. pStatement->ich = statement.statement.begin;
  188. pStatement->cch = statement.statement.end - statement.statement.begin;
  189. return TRUE;
  190. }
  191. return FALSE;
  192. }
  193. FunctionBody * DebugDocument::GetFunctionBodyAt(int32 ibos)
  194. {
  195. StatementLocation location = {};
  196. if (GetStatementLocation(ibos, &location))
  197. {
  198. return location.function;
  199. }
  200. return nullptr;
  201. }
  202. BOOL DebugDocument::HasLineBreak(int32 _start, int32 _end)
  203. {
  204. return this->functionBody->HasLineBreak(_start, _end);
  205. }
  206. BOOL DebugDocument::GetStatementLocation(int32 ibos, StatementLocation* plocation)
  207. {
  208. if (ibos < 0)
  209. {
  210. return FALSE;
  211. }
  212. ScriptContext* scriptContext = this->utf8SourceInfo->GetScriptContext();
  213. if (scriptContext == nullptr || scriptContext->IsClosed())
  214. {
  215. return FALSE;
  216. }
  217. uint32 ubos = static_cast<uint32>(ibos);
  218. // Getting the appropriate statement on the asked position works on the heuristic which requires two
  219. // probable candidates. These candidates will be closest to the ibos where first.range.start < ibos and
  220. // second.range.start >= ibos. They will be fetched out by going into each FunctionBody.
  221. StatementLocation candidateMatch1 = {};
  222. StatementLocation candidateMatch2 = {};
  223. this->utf8SourceInfo->MapFunction([&](FunctionBody* pFuncBody)
  224. {
  225. uint32 functionStart = pFuncBody->StartInDocument();
  226. uint32 functionEnd = functionStart + pFuncBody->LengthInBytes();
  227. // For the first candidate, we should allow the current function to participate if its range
  228. // (instead of just start offset) is closer to the ubos compared to already found candidate1.
  229. if (candidateMatch1.function == nullptr ||
  230. ((candidateMatch1.statement.begin <= static_cast<int>(functionStart) ||
  231. candidateMatch1.statement.end <= static_cast<int>(functionEnd)) &&
  232. ubos > functionStart) ||
  233. candidateMatch2.function == nullptr ||
  234. (candidateMatch2.statement.begin > static_cast<int>(functionStart) &&
  235. ubos <= functionStart) ||
  236. (functionStart <= ubos &&
  237. ubos < functionEnd))
  238. {
  239. // We need to find out two possible candidate from the current FunctionBody.
  240. pFuncBody->FindClosestStatements(ibos, &candidateMatch1, &candidateMatch2);
  241. }
  242. });
  243. if (candidateMatch1.function == nullptr && candidateMatch2.function == nullptr)
  244. {
  245. return FALSE; // No Match found
  246. }
  247. if (candidateMatch1.function == nullptr || candidateMatch2.function == nullptr)
  248. {
  249. *plocation = (candidateMatch1.function == nullptr) ? candidateMatch2 : candidateMatch1;
  250. return TRUE;
  251. }
  252. // If one of the func is inner to another one, and ibos is in the inner one, disregard the outer one/let the inner one win.
  253. // See WinBlue 575634. Scenario is like this: var foo = function () {this;} -- and BP is set to 'this;' 'function'.
  254. if (candidateMatch1.function != candidateMatch2.function)
  255. {
  256. Assert(candidateMatch1.function && candidateMatch2.function);
  257. regex::Interval func1Range(candidateMatch1.function->StartInDocument());
  258. func1Range.End(func1Range.Begin() + candidateMatch1.function->LengthInBytes());
  259. regex::Interval func2Range(candidateMatch2.function->StartInDocument());
  260. func2Range.End(func2Range.Begin() + candidateMatch2.function->LengthInBytes());
  261. // If cursor (ibos) is just after the closing braces of the inner function then we can't
  262. // directly choose inner function and have to make line break check, so fallback
  263. // function foo(){function bar(){var y=1;}#var x=1;bar();}foo(); - ibos is #
  264. if (func1Range.Includes(func2Range) && func2Range.Includes(ibos) && func2Range.End() != ibos)
  265. {
  266. *plocation = candidateMatch2;
  267. return TRUE;
  268. }
  269. else if (func2Range.Includes(func1Range) && func1Range.Includes(ibos) && func1Range.End() != ibos)
  270. {
  271. *plocation = candidateMatch1;
  272. return TRUE;
  273. }
  274. }
  275. // At this point we have both candidate to consider.
  276. Assert(candidateMatch1.statement.begin < candidateMatch2.statement.begin);
  277. Assert(candidateMatch1.statement.begin < ibos);
  278. Assert(candidateMatch2.statement.begin >= ibos);
  279. // Default selection
  280. *plocation = candidateMatch1;
  281. // If the second candidate start at ibos or
  282. // if the first candidate has line break between ibos and the second candidate is on the same line as ibos
  283. // then consider the second one.
  284. BOOL fNextHasLineBreak = this->HasLineBreak(ibos, candidateMatch2.statement.begin);
  285. if ((candidateMatch2.statement.begin == ibos)
  286. || (this->HasLineBreak(candidateMatch1.statement.begin, ibos) && !fNextHasLineBreak))
  287. {
  288. *plocation = candidateMatch2;
  289. }
  290. // If ibos is out of the range of first candidate, choose second candidate if ibos is on the same line as second candidate
  291. // or ibos is not on the same line of the end of the first candidate.
  292. else if (candidateMatch1.statement.end < ibos && (!fNextHasLineBreak || this->HasLineBreak(candidateMatch1.statement.end, ibos)))
  293. {
  294. *plocation = candidateMatch2;
  295. }
  296. return TRUE;
  297. }
  298. }