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
171 lines (140 loc) · 4.79 KB
/
Debugger.cpp
File metadata and controls
171 lines (140 loc) · 4.79 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
//---------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
//----------------------------------------------------------------------------
#include "StdAfx.h"
#define MAX_INPUT_LENGTH 1024
Debugger::Debugger(JsRuntimeHandle debugRuntime)
{
this->debugRuntime = debugRuntime;
this->debuggerController = new DebuggerController(debugRuntime);
this->InstallUtilities();
}
Debugger::~Debugger()
{
delete this->debuggerController;
this->debuggerController = nullptr;
}
void Debugger::InstallUtilities()
{
this->debuggerController->InstallHostCallback(L"GetUserInput", &Debugger::GetUserInput, this);
this->debuggerController->InstallHostCallback(L"InsertBreakpoint", &Debugger::JsInsertBreakpoint, this);
this->debuggerController->InstallHostCallback(L"ResumeFromBreakpoint", &Debugger::JsResumeFromBreakpoint, this);
}
void CALLBACK Debugger::JsDiagDebugEventHandler(_In_ JsDiagDebugEvent debugEvent, _In_ JsValueRef eventData, _In_opt_ void* callbackState)
{
if (eventData != nullptr)
{
JsValueRef eventDataJson;
if (JsNoError == ChakraRTInterface::JsStringifyObject(eventData, &eventDataJson))
{
LPCWSTR eventJSON;
size_t length;
if (JsNoError == ChakraRTInterface::JsStringToPointer(eventDataJson, &eventJSON, &length))
{
debuggerController->ProcessDebugEvent(DebugEventString(debugEvent), eventJSON);
}
}
}
}
JsValueRef CALLBACK Debugger::GetUserInput(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
JsValueRef retVal = nullptr;
fwprintf(stdout, L"dbg> ");
wchar_t line[MAX_INPUT_LENGTH];
if (fgetws(line, MAX_INPUT_LENGTH, stdin) != nullptr)
{
IfJsrtError(ChakraRTInterface::JsPointerToString(line, wcslen(line), &retVal));
}
else
{
fwprintf(stdout, L"FATAL ERROR");
}
Error:
if (retVal == nullptr)
{
// Something went wrong - attempt to just return undefined.
ChakraRTInterface::JsGetUndefinedValue(&retVal);
}
return retVal;
}
JsValueRef CALLBACK Debugger::JsInsertBreakpoint(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
HRESULT hr = S_OK;
JsValueRef retVal = nullptr;
void *data;
IfFailGo(DebuggerScriptWrapper::GetExternalData(callee, &data));
Debugger *debugger = (Debugger*)data;
if (argumentCount != 4)
{
hr = E_FAIL;
goto Error;
}
// Marshal the arguments;
ULONG args[3];
for (int i = 1; i < argumentCount; i++)
{
double tmp;
JsValueRef numValue;
IfJsrtError(ChakraRTInterface::JsConvertValueToNumber(arguments[i], &numValue));
IfJsrtError(ChakraRTInterface::JsNumberToDouble(numValue, &tmp));
args[i - 1] = (ULONG)tmp;
}
unsigned int breakpointId = 0;
IfJsrtError(ChakraRTInterface::JsDiagSetBreakpoint(debugger->debugRuntime, args[0], args[1], args[2], &breakpointId));
IfJsrtError(ChakraRTInterface::JsDoubleToNumber(breakpointId, &retVal));
Error:
if (retVal == nullptr)
{
// Something went wrong - attempt to just return undefined.
ChakraRTInterface::JsGetUndefinedValue(&retVal);
}
return retVal;
}
JsValueRef CALLBACK Debugger::JsResumeFromBreakpoint(JsValueRef callee, bool isConstructCall, JsValueRef *arguments, unsigned short argumentCount, void *callbackState)
{
HRESULT hr = S_OK;
JsValueRef retVal = nullptr;
if (argumentCount != 2)
{
hr = E_FAIL;
goto Error;
}
// Marshal the arguments;
ULONG args;
double tmp;
JsValueRef numValue;
IfJsrtError(ChakraRTInterface::JsConvertValueToNumber(arguments[1], &numValue));
IfJsrtError(ChakraRTInterface::JsNumberToDouble(numValue, &tmp));
args = (ULONG)tmp;
JsDiagResumeType jsDiagResumeType = JsDiagResumeTypeStepIn;
if (args == 0)
{
jsDiagResumeType = JsDiagResumeTypeStepIn;
}
else if (args == 1)
{
jsDiagResumeType = JsDiagResumeTypeStepOver;
}
else if (args == 2)
{
jsDiagResumeType = JsDiagResumeTypeStepOut;
}
IfJsrtError(ChakraRTInterface::JsDiagResume(jsDiagResumeType));
Error:
if (retVal == nullptr)
{
// Something went wrong - attempt to just return undefined.
ChakraRTInterface::JsGetUndefinedValue(&retVal);
}
return retVal;
}
LPCWSTR Debugger::DebugEventString(JsDiagDebugEvent debugEvent)
{
switch (debugEvent)
{
case JsDiagDebugEventBreak: return L"Break";
case JsDiagDebugEventSourceCompilation: return L"SourceCompilation";
case JsDiagDebugEventCompileError: return L"CompileError";
}
return L"UNKNOWN";
}