forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugger.cpp
More file actions
525 lines (417 loc) · 17.9 KB
/
Debugger.cpp
File metadata and controls
525 lines (417 loc) · 17.9 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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "stdafx.h"
#define MAX_BASELINE_SIZE (1024*1024*200)
void CHAKRA_CALLBACK Debugger::DebugEventHandler(_In_ JsDiagDebugEvent debugEvent, _In_ JsValueRef eventData, _In_opt_ void* callbackState)
{
Debugger* debugger = (Debugger*)callbackState;
debugger->HandleDebugEvent(debugEvent, eventData);
}
JsValueRef Debugger::GetSource(JsValueRef callee, bool isConstructCall, JsValueRef * arguments, unsigned short argumentCount, void * callbackState)
{
int scriptId;
JsValueRef source = JS_INVALID_REFERENCE;
if (argumentCount > 1)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &scriptId));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetSource(scriptId, &source));
}
return source;
}
JsValueRef Debugger::SetBreakpoint(JsValueRef callee, bool isConstructCall, JsValueRef * arguments, unsigned short argumentCount, void * callbackState)
{
int scriptId;
int line;
int column;
JsValueRef bpObject = JS_INVALID_REFERENCE;
if (argumentCount > 3)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &scriptId));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[2], &line));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[3], &column));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagSetBreakpoint(scriptId, line, column, &bpObject));
}
return bpObject;
}
JsValueRef Debugger::GetStackTrace(JsValueRef callee, bool isConstructCall, JsValueRef * arguments, unsigned short argumentCount, void * callbackState)
{
JsValueRef stackInfo = JS_INVALID_REFERENCE;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetStackTrace(&stackInfo));
return stackInfo;
}
JsValueRef Debugger::GetBreakpoints(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsValueRef breakpoints = JS_INVALID_REFERENCE;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetBreakpoints(&breakpoints));
return breakpoints;
}
JsValueRef Debugger::RemoveBreakpoint(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
int bpId;
if (argumentCount > 1)
{
JsValueRef numberValue;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsConvertValueToNumber(arguments[1], &numberValue));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(numberValue, &bpId));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagRemoveBreakpoint(bpId));
}
return JS_INVALID_REFERENCE;
}
JsValueRef Debugger::SetBreakOnException(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
int exceptionAttributes;
if (argumentCount > 1)
{
JsValueRef breakOnException;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsConvertValueToNumber(arguments[1], &breakOnException));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(breakOnException, &exceptionAttributes));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagSetBreakOnException(Debugger::GetRuntime(), (JsDiagBreakOnExceptionAttributes)exceptionAttributes));
}
return JS_INVALID_REFERENCE;
}
JsValueRef Debugger::GetBreakOnException(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsDiagBreakOnExceptionAttributes exceptionAttributes;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetBreakOnException(Debugger::GetRuntime(), &exceptionAttributes));
JsValueRef exceptionAttributesRef;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDoubleToNumber((double)exceptionAttributes, &exceptionAttributesRef));
return exceptionAttributesRef;
}
JsValueRef Debugger::SetStepType(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
int stepType;
if (argumentCount > 1)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &stepType));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagSetStepType((JsDiagStepType)stepType));
}
return JS_INVALID_REFERENCE;
}
JsValueRef Debugger::GetScripts(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsValueRef sourcesList = JS_INVALID_REFERENCE;
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetScripts(&sourcesList));
return sourcesList;
}
JsValueRef Debugger::GetStackProperties(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsValueRef properties = JS_INVALID_REFERENCE;
int stackFrameIndex;
if (argumentCount > 1)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &stackFrameIndex));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetStackProperties(stackFrameIndex, &properties));
}
return properties;
}
JsValueRef Debugger::GetProperties(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsValueRef properties = JS_INVALID_REFERENCE;
int objectHandle;
int fromCount;
int totalCount;
if (argumentCount > 3)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &objectHandle));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[2], &fromCount));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[3], &totalCount));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetProperties(objectHandle, fromCount, totalCount, &properties));
}
return properties;
}
JsValueRef Debugger::GetObjectFromHandle(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsValueRef properties = JS_INVALID_REFERENCE;
int objectHandle;
if (argumentCount > 1)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &objectHandle));
IfJsErrorFailLogAndRet(ChakraRTInterface::JsDiagGetObjectFromHandle(objectHandle, &properties));
}
return properties;
}
JsValueRef Debugger::Evaluate(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
int stackFrameIndex;
JsValueRef result = JS_INVALID_REFERENCE;
if (argumentCount > 2)
{
IfJsErrorFailLogAndRet(ChakraRTInterface::JsNumberToInt(arguments[1], &stackFrameIndex));
AutoString argstr(arguments[2]);
IfJsErrorFailLogAndRet(argstr.GetError());
ChakraRTInterface::JsDiagEvaluateUtf8(argstr.GetString(), stackFrameIndex, &result);
}
return result;
}
Debugger::Debugger(JsRuntimeHandle runtime)
{
this->m_runtime = runtime;
this->m_context = JS_INVALID_REFERENCE;
this->m_isDetached = true;
}
Debugger::~Debugger()
{
this->m_runtime = JS_INVALID_RUNTIME_HANDLE;
}
Debugger * Debugger::GetDebugger(JsRuntimeHandle runtime)
{
if (Debugger::debugger == nullptr)
{
Debugger::debugger = new Debugger(runtime);
Debugger::debugger->Initialize();
}
return Debugger::debugger;
}
void Debugger::CloseDebugger()
{
if (Debugger::debugger != nullptr)
{
delete Debugger::debugger;
Debugger::debugger = nullptr;
}
}
JsRuntimeHandle Debugger::GetRuntime()
{
return Debugger::debugger != nullptr ? Debugger::debugger->m_runtime : JS_INVALID_RUNTIME_HANDLE;
}
bool Debugger::Initialize()
{
// Create a new context and run dbgcontroller.js in that context
// setup dbgcontroller.js callbacks
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCreateContext(this->m_runtime, &this->m_context));
AutoRestoreContext autoRestoreContext(this->m_context);
JsValueRef globalFunc = JS_INVALID_REFERENCE;
JsValueRef scriptSource;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCreateExternalArrayBuffer(
(void*)controllerScript, (unsigned int)strlen(controllerScript),
nullptr, nullptr, &scriptSource));
JsValueRef fname;
ChakraRTInterface::JsCreateString(
"DbgController.js", strlen("DbgController.js"), &fname);
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsParse(scriptSource,
JS_SOURCE_CONTEXT_NONE, fname, JsParseScriptAttributeLibraryCode,
&globalFunc));
JsValueRef undefinedValue;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetUndefinedValue(&undefinedValue));
JsValueRef args[] = { undefinedValue };
JsValueRef result = JS_INVALID_REFERENCE;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCallFunction(globalFunc, args, _countof(args), &result));
JsValueRef globalObj = JS_INVALID_REFERENCE;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetGlobalObject(&globalObj));
JsPropertyIdRef hostDebugObjectPropId;
IfJsrtErrorFailLogAndRetFalse(CreatePropertyIdFromString("hostDebugObject", &hostDebugObjectPropId));
JsPropertyIdRef hostDebugObject;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetProperty(globalObj, hostDebugObjectPropId, &hostDebugObject));
this->InstallDebugCallbacks(hostDebugObject);
if (!WScriptJsrt::Initialize())
{
return false;
}
if (HostConfigFlags::flags.dbgbaselineIsEnabled)
{
this->SetBaseline();
}
if (HostConfigFlags::flags.InspectMaxStringLengthIsEnabled)
{
this->SetInspectMaxStringLength();
}
return true;
}
bool Debugger::InstallDebugCallbacks(JsValueRef hostDebugObject)
{
HRESULT hr = S_OK;
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetSource", Debugger::GetSource));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagSetBreakpoint", Debugger::SetBreakpoint));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetStackTrace", Debugger::GetStackTrace));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetBreakpoints", Debugger::GetBreakpoints));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagRemoveBreakpoint", Debugger::RemoveBreakpoint));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagSetBreakOnException", Debugger::SetBreakOnException));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetBreakOnException", Debugger::GetBreakOnException));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagSetStepType", Debugger::SetStepType));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetScripts", Debugger::GetScripts));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetStackProperties", Debugger::GetStackProperties));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetProperties", Debugger::GetProperties));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagGetObjectFromHandle", Debugger::GetObjectFromHandle));
IfFalseGo(WScriptJsrt::InstallObjectsOnObject(hostDebugObject, "JsDiagEvaluate", Debugger::Evaluate));
Error:
return hr != S_OK;
}
bool Debugger::SetBaseline()
{
#ifdef _WIN32
LPSTR script = nullptr;
FILE *file = nullptr;
int numChars = 0;
HRESULT hr = S_OK;
if (_wfopen_s(&file, HostConfigFlags::flags.dbgbaseline, _u("rb")) != 0)
{
Helpers::LogError(_u("opening baseline file '%s'"), HostConfigFlags::flags.dbgbaseline);
}
if(file != nullptr)
{
int fileSize = _filelength(_fileno(file));
if (fileSize <= MAX_BASELINE_SIZE)
{
script = new char[fileSize + 1];
numChars = static_cast<int>(fread(script, sizeof(script[0]), fileSize, file));
if (numChars == fileSize)
{
script[numChars] = '\0';
JsValueRef wideScriptRef;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCreateString(
script, strlen(script), &wideScriptRef));
this->CallFunctionNoResult("SetBaseline", wideScriptRef);
}
else
{
Helpers::LogError(_u("failed to read from baseline file"));
IfFailGo(E_FAIL);
}
}
else
{
Helpers::LogError(_u("baseline file too large"));
IfFailGo(E_FAIL);
}
}
Error:
if (script)
{
delete[] script;
}
if (file)
{
fclose(file);
}
return hr == S_OK;
#else
// xplat-todo: Implement this on Linux
return false;
#endif
}
bool Debugger::SetInspectMaxStringLength()
{
Assert(HostConfigFlags::flags.InspectMaxStringLength > 0);
JsValueRef maxStringRef;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsDoubleToNumber(HostConfigFlags::flags.InspectMaxStringLength, &maxStringRef));
return this->CallFunctionNoResult("SetInspectMaxStringLength", maxStringRef);
}
bool Debugger::CallFunction(char const * functionName, JsValueRef *result, JsValueRef arg1, JsValueRef arg2)
{
AutoRestoreContext autoRestoreContext(this->m_context);
// Get the global object
JsValueRef globalObj = JS_INVALID_REFERENCE;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetGlobalObject(&globalObj));
// Get a script string for the function name
JsPropertyIdRef targetFuncId = JS_INVALID_REFERENCE;
IfJsrtErrorFailLogAndRetFalse(CreatePropertyIdFromString(functionName, &targetFuncId));
// Get the target function
JsValueRef targetFunc = JS_INVALID_REFERENCE;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetProperty(globalObj, targetFuncId, &targetFunc));
static const unsigned short MaxArgs = 2;
JsValueRef args[MaxArgs + 1];
// Pass in undefined for 'this'
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetUndefinedValue(&args[0]));
unsigned short argCount = 1;
if (arg1 != JS_INVALID_REFERENCE)
{
args[argCount++] = arg1;
}
Assert(arg2 == JS_INVALID_REFERENCE || argCount != 1);
if (arg2 != JS_INVALID_REFERENCE)
{
args[argCount++] = arg2;
}
// Call the function
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsCallFunction(targetFunc, args, argCount, result));
return true;
}
bool Debugger::CallFunctionNoResult(char const * functionName, JsValueRef arg1, JsValueRef arg2)
{
JsValueRef result = JS_INVALID_REFERENCE;
return this->CallFunction(functionName, &result, arg1, arg2);
}
bool Debugger::DumpFunctionPosition(JsValueRef functionPosition)
{
return this->CallFunctionNoResult("DumpFunctionPosition", functionPosition);
}
bool Debugger::StartDebugging(JsRuntimeHandle runtime)
{
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsDiagStartDebugging(runtime, Debugger::DebugEventHandler, this));
this->m_isDetached = false;
return true;
}
bool Debugger::StopDebugging(JsRuntimeHandle runtime)
{
void* callbackState = nullptr;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsDiagStopDebugging(runtime, &callbackState));
Assert(callbackState == this);
this->m_isDetached = true;
return true;
}
bool Debugger::HandleDebugEvent(JsDiagDebugEvent debugEvent, JsValueRef eventData)
{
JsValueRef debugEventRef;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsDoubleToNumber(debugEvent, &debugEventRef));
return this->CallFunctionNoResult("HandleDebugEvent", debugEventRef, eventData);
}
bool Debugger::CompareOrWriteBaselineFile(LPCSTR fileName)
{
AutoRestoreContext autoRestoreContext(this->m_context);
// Pass in undefined for 'this'
JsValueRef undefinedRef;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsGetUndefinedValue(&undefinedRef));
JsValueRef result = JS_INVALID_REFERENCE;
bool testPassed = false;
if (HostConfigFlags::flags.dbgbaselineIsEnabled)
{
this->CallFunction("Verify", &result);
JsValueRef numberVal;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsConvertValueToNumber(result, &numberVal));
int val;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsNumberToInt(numberVal, &val));
testPassed = (val == 0);
}
if (!testPassed)
{
this->CallFunction("GetOutputJson", &result);
AutoString baselineData;
IfJsrtErrorFailLogAndRetFalse(baselineData.Initialize(result));
char16 baselineFilename[256];
swprintf_s(baselineFilename, HostConfigFlags::flags.dbgbaselineIsEnabled ? _u("%S.dbg.baseline.rebase") : _u("%S.dbg.baseline"), fileName);
FILE *file = nullptr;
if (_wfopen_s(&file, baselineFilename, _u("wt")) != 0)
{
return false;
}
if (file != nullptr)
{
int countWritten = static_cast<int>(fwrite(baselineData.GetString(), sizeof(char), baselineData.GetLength(), file));
Assert(baselineData.GetLength() <= INT_MAX);
if (countWritten != (int)baselineData.GetLength())
{
Assert(false);
return false;
}
fclose(file);
}
if (!HostConfigFlags::flags.dbgbaselineIsEnabled)
{
wprintf(_u("No baseline file specified, baseline created at '%s'\n"), baselineFilename);
}
else
{
Helpers::LogError(_u("Rebase file created at '%s'\n"), baselineFilename);
}
}
return true;
}
bool Debugger::SourceRunDown()
{
AutoRestoreContext autoRestoreContext(this->m_context);
JsValueRef sourcesList = JS_INVALID_REFERENCE;
IfJsrtErrorFailLogAndRetFalse(ChakraRTInterface::JsDiagGetScripts(&sourcesList));
return this->CallFunctionNoResult("HandleSourceRunDown", sourcesList);
}