forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugContext.cpp
More file actions
412 lines (354 loc) · 15.8 KB
/
DebugContext.cpp
File metadata and controls
412 lines (354 loc) · 15.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeDebugPch.h"
namespace Js
{
DebugContext::DebugContext(Js::ScriptContext * scriptContext) :
scriptContext(scriptContext),
hostDebugContext(nullptr),
diagProbesContainer(nullptr),
debuggerMode(DebuggerMode::NotDebugging)
{
Assert(scriptContext != nullptr);
}
DebugContext::~DebugContext()
{
Assert(this->scriptContext == nullptr);
Assert(this->hostDebugContext == nullptr);
Assert(this->diagProbesContainer == nullptr);
}
void DebugContext::Initialize()
{
Assert(this->diagProbesContainer == nullptr);
this->diagProbesContainer = HeapNew(ProbeContainer);
this->diagProbesContainer->Initialize(this->scriptContext);
}
void DebugContext::Close()
{
Assert(this->scriptContext != nullptr);
this->scriptContext = nullptr;
if (this->diagProbesContainer != nullptr)
{
this->diagProbesContainer->Close();
HeapDelete(this->diagProbesContainer);
this->diagProbesContainer = nullptr;
}
if (this->hostDebugContext != nullptr)
{
this->hostDebugContext->Delete();
this->hostDebugContext = nullptr;
}
}
void DebugContext::SetHostDebugContext(HostDebugContext * hostDebugContext)
{
Assert(this->hostDebugContext == nullptr);
Assert(hostDebugContext != nullptr);
this->hostDebugContext = hostDebugContext;
}
bool DebugContext::CanRegisterFunction() const
{
if (this->hostDebugContext == nullptr || this->scriptContext == nullptr || this->scriptContext->IsClosed() || this->IsDebugContextInNonDebugMode())
{
return false;
}
return true;
}
void DebugContext::RegisterFunction(Js::ParseableFunctionInfo * func, LPCWSTR title)
{
if (!this->CanRegisterFunction())
{
return;
}
FunctionBody * functionBody;
if (func->IsDeferredParseFunction())
{
functionBody = func->Parse();
}
else
{
functionBody = func->GetFunctionBody();
}
this->RegisterFunction(functionBody, functionBody->GetHostSourceContext(), title);
}
void DebugContext::RegisterFunction(Js::FunctionBody * functionBody, DWORD_PTR dwDebugSourceContext, LPCWSTR title)
{
if (!this->CanRegisterFunction())
{
return;
}
this->hostDebugContext->DbgRegisterFunction(this->scriptContext, functionBody, dwDebugSourceContext, title);
}
// Sets the specified mode for the debugger. The mode is used to inform
// the runtime of whether or not functions should be JITed or interpreted
// when they are defer parsed.
// Note: Transitions back to NotDebugging are not allowed. Once the debugger
// is in SourceRundown or Debugging mode, it can only transition between those
// two modes.
void DebugContext::SetDebuggerMode(DebuggerMode mode)
{
if (this->debuggerMode == mode)
{
// Already in this mode so return.
return;
}
if (mode == DebuggerMode::NotDebugging)
{
AssertMsg(false, "Transitioning to non-debug mode is not allowed.");
return;
}
this->debuggerMode = mode;
}
HRESULT DebugContext::RundownSourcesAndReparse(bool shouldPerformSourceRundown, bool shouldReparseFunctions)
{
OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::RundownSourcesAndReparse scriptContext 0x%p, shouldPerformSourceRundown %d, shouldReparseFunctions %d\n"),
this->scriptContext, shouldPerformSourceRundown, shouldReparseFunctions);
Js::TempArenaAllocatorObject *tempAllocator = nullptr;
JsUtil::List<Js::FunctionBody *, ArenaAllocator>* pFunctionsToRegister = nullptr;
JsUtil::List<Js::Utf8SourceInfo *, Recycler, false, Js::CopyRemovePolicy, RecyclerPointerComparer>* utf8SourceInfoList = nullptr;
HRESULT hr = S_OK;
ThreadContext* threadContext = this->scriptContext->GetThreadContext();
BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
tempAllocator = threadContext->GetTemporaryAllocator(_u("debuggerAlloc"));
pFunctionsToRegister = JsUtil::List<Js::FunctionBody*, ArenaAllocator>::New(tempAllocator->GetAllocator());
utf8SourceInfoList = JsUtil::List<Js::Utf8SourceInfo *, Recycler, false, Js::CopyRemovePolicy, RecyclerPointerComparer>::New(this->scriptContext->GetRecycler());
this->MapUTF8SourceInfoUntil([&](Js::Utf8SourceInfo * sourceInfo) -> bool
{
WalkAndAddUtf8SourceInfo(sourceInfo, utf8SourceInfoList);
return false;
});
END_TRANSLATE_OOM_TO_HRESULT(hr);
if (hr != S_OK)
{
Assert(FALSE);
return hr;
}
utf8SourceInfoList->MapUntil([&](int index, Js::Utf8SourceInfo * sourceInfo) -> bool
{
OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::RundownSourcesAndReparse scriptContext 0x%p, sourceInfo 0x%p, HasDebugDocument %d\n"),
this->scriptContext, sourceInfo, sourceInfo->HasDebugDocument());
if (sourceInfo->GetIsLibraryCode())
{
// Not putting the internal library code to the debug mode, but need to reinitialize execution mode limits of each
// function body upon debugger detach, even for library code at the moment.
if (shouldReparseFunctions)
{
sourceInfo->MapFunction([](Js::FunctionBody *const pFuncBody)
{
if (pFuncBody->IsFunctionParsed())
{
pFuncBody->ReinitializeExecutionModeAndLimits();
}
});
}
return false;
}
Assert(sourceInfo->GetSrcInfo() && sourceInfo->GetSrcInfo()->sourceContextInfo);
#if DBG
if (shouldPerformSourceRundown)
{
// We shouldn't have a debug document if we're running source rundown for the first time.
Assert(!sourceInfo->HasDebugDocument());
}
#endif // DBG
DWORD_PTR dwDebugHostSourceContext = Js::Constants::NoHostSourceContext;
if (shouldPerformSourceRundown && this->hostDebugContext != nullptr)
{
dwDebugHostSourceContext = this->hostDebugContext->GetHostSourceContext(sourceInfo);
}
this->FetchTopLevelFunction(pFunctionsToRegister, sourceInfo);
if (pFunctionsToRegister->Count() == 0)
{
// This could happen if there are no functions to re-compile.
return false;
}
if (this->hostDebugContext != nullptr && sourceInfo->GetSourceContextInfo())
{
this->hostDebugContext->SetThreadDescription(sourceInfo->GetSourceContextInfo()->url); // the HRESULT is omitted.
}
bool fHasDoneSourceRundown = false;
for (int i = 0; i < pFunctionsToRegister->Count(); i++)
{
Js::FunctionBody* pFuncBody = pFunctionsToRegister->Item(i);
if (pFuncBody == nullptr)
{
continue;
}
if (shouldReparseFunctions)
{
if (this->scriptContext == nullptr || this->scriptContext->IsClosed())
{
// scriptContext can be closed in previous call
hr = E_FAIL;
return true;
}
BEGIN_JS_RUNTIME_CALL_EX_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT_NESTED(this->scriptContext, false)
{
pFuncBody->Parse();
// This is the first call to the function, ensure dynamic profile info
#if ENABLE_PROFILE_INFO
pFuncBody->EnsureDynamicProfileInfo();
#endif
}
END_JS_RUNTIME_CALL_AND_TRANSLATE_EXCEPTION_AND_ERROROBJECT_TO_HRESULT(hr);
if (hr != S_OK)
{
break;
}
}
if (!fHasDoneSourceRundown && shouldPerformSourceRundown)
{
BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
{
this->RegisterFunction(pFuncBody, dwDebugHostSourceContext, pFuncBody->GetSourceName());
}
END_TRANSLATE_OOM_TO_HRESULT(hr);
fHasDoneSourceRundown = true;
}
}
if (shouldReparseFunctions)
{
sourceInfo->MapFunction([](Js::FunctionBody *const pFuncBody)
{
if (pFuncBody->IsFunctionParsed())
{
pFuncBody->ReinitializeExecutionModeAndLimits();
}
});
}
return false;
});
if (this->scriptContext != nullptr && !this->scriptContext->IsClosed())
{
if (shouldPerformSourceRundown && this->scriptContext->HaveCalleeSources())
{
this->scriptContext->MapCalleeSources([=](Js::Utf8SourceInfo* calleeSourceInfo)
{
if (this->hostDebugContext != nullptr)
{
this->hostDebugContext->ReParentToCaller(calleeSourceInfo);
}
});
}
}
threadContext->ReleaseTemporaryAllocator(tempAllocator);
return hr;
}
void DebugContext::FetchTopLevelFunction(JsUtil::List<Js::FunctionBody *, ArenaAllocator>* pFunctions, Js::Utf8SourceInfo * sourceInfo)
{
Assert(pFunctions != nullptr);
Assert(sourceInfo != nullptr);
HRESULT hr = S_OK;
// Get FunctionBodys which are distinctly parseable, i.e. they are not enclosed in any other function (finding
// out root node of the sub-tree, in which root node is not enclosed in any other available function) this is
// by walking over all function and comparing their range.
BEGIN_TRANSLATE_OOM_TO_HRESULT_NESTED
{
pFunctions->Clear();
sourceInfo->MapFunctionUntil([&](Js::FunctionBody* pFuncBody) -> bool
{
if (pFuncBody->GetIsGlobalFunc())
{
if (pFuncBody->IsFakeGlobalFunc(pFuncBody->GetGrfscr()))
{
// This is created due to 'Function' code or deferred parsed functions, there is nothing to
// re-compile in this function as this is just a place-holder/fake function.
Assert(pFuncBody->GetByteCode() == NULL);
return false;
}
if (!pFuncBody->GetIsTopLevel())
{
return false;
}
// If global function, there is no need to find out any other functions.
pFunctions->Clear();
pFunctions->Add(pFuncBody);
return true;
}
if (pFuncBody->IsFunctionParsed())
{
bool isNeedToAdd = true;
for (int i = 0; i < pFunctions->Count(); i++)
{
Js::FunctionBody *currentFunction = pFunctions->Item(i);
if (currentFunction != nullptr)
{
if (currentFunction->StartInDocument() > pFuncBody->StartInDocument() || !currentFunction->EndsAfter(pFuncBody->StartInDocument()))
{
if (pFuncBody->StartInDocument() <= currentFunction->StartInDocument() && pFuncBody->EndsAfter(currentFunction->StartInDocument()))
{
// The stored item has got the parent, remove current Item
pFunctions->Item(i, nullptr);
}
}
else
{
// Parent (the enclosing function) is already in the list
isNeedToAdd = false;
break;
}
}
}
if (isNeedToAdd)
{
pFunctions->Add(pFuncBody);
}
}
return false;
});
}
END_TRANSLATE_OOM_TO_HRESULT(hr);
Assert(hr == S_OK);
}
// 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.
void DebugContext::WalkAndAddUtf8SourceInfo(Js::Utf8SourceInfo* sourceInfo, JsUtil::List<Js::Utf8SourceInfo *, Recycler, false, Js::CopyRemovePolicy, RecyclerPointerComparer> *utf8SourceInfoList)
{
Js::Utf8SourceInfo* callerUtf8SourceInfo = sourceInfo->GetCallerUtf8SourceInfo();
if (callerUtf8SourceInfo)
{
Js::ScriptContext* callerScriptContext = callerUtf8SourceInfo->GetScriptContext();
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"),
this->scriptContext, sourceInfo, callerUtf8SourceInfo, sourceInfo->GetScriptContext(), callerScriptContext);
if (sourceInfo->GetScriptContext() == callerScriptContext)
{
WalkAndAddUtf8SourceInfo(callerUtf8SourceInfo, utf8SourceInfoList);
}
else if (callerScriptContext->IsScriptContextInNonDebugMode())
{
// The caller scriptContext is not in run down/debug mode so let's save the relationship so that we can re-parent callees afterwards.
callerScriptContext->AddCalleeSourceInfoToList(sourceInfo);
}
}
if (!utf8SourceInfoList->Contains(sourceInfo))
{
OUTPUT_TRACE(Js::DebuggerPhase, _u("DebugContext::WalkAndAddUtf8SourceInfo Adding to utf8SourceInfoList scriptContext 0x%p, sourceInfo 0x%p, sourceInfo scriptContext 0x%p\n"),
this->scriptContext, sourceInfo, sourceInfo->GetScriptContext());
#if DBG
bool found = false;
this->MapUTF8SourceInfoUntil([&](Js::Utf8SourceInfo * sourceInfoTemp) -> bool
{
if (sourceInfoTemp == sourceInfo)
{
found = true;
}
return found;
});
AssertMsg(found, "Parented eval feature have extra source");
#endif
utf8SourceInfoList->Add(sourceInfo);
}
}
template<class TMapFunction>
void DebugContext::MapUTF8SourceInfoUntil(TMapFunction map)
{
this->scriptContext->GetSourceList()->MapUntil([=](int i, RecyclerWeakReference<Js::Utf8SourceInfo>* sourceInfoWeakRef) -> bool {
Js::Utf8SourceInfo* sourceInfo = sourceInfoWeakRef->Get();
if (sourceInfo)
{
return map(sourceInfo);
}
return false;
});
}
}