forked from node-inspector/v8-debug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8-debug.js
276 lines (229 loc) · 8.77 KB
/
v8-debug.js
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
var binary = require('node-pre-gyp');
var fs = require('fs');
var path = require('path');
var binding_path = binary.find(path.resolve(path.join(__dirname,'./package.json')));
var binding = require(binding_path);
var EventEmitter = require('events').EventEmitter;
var inherits = require('util').inherits;
var extend = require('util')._extend;
var NODE_NEXT = require('./tools/NODE_NEXT');
// Don't cache debugger module
delete require.cache[module.id];
function InjectedScriptDir(link) {
return require.resolve(__dirname + '/InjectedScript/' + link);
};
var DebuggerScriptLink = InjectedScriptDir('DebuggerScript.js');
var InjectedScriptLink = InjectedScriptDir('InjectedScriptSource.js');
var InjectedScriptHostLink = InjectedScriptDir('InjectedScriptHost.js');
var overrides = {
extendedProcessDebugJSONRequestHandles_: {},
extendedProcessDebugJSONRequestAsyncHandles_: {},
extendedProcessDebugJSONRequest_: function(json_request) {
var request; // Current request.
var response; // Generated response.
try {
try {
// Convert the JSON string to an object.
request = JSON.parse(json_request);
var handle = this.extendedProcessDebugJSONRequestHandles_[request.command];
var asyncHandle = this.extendedProcessDebugJSONRequestAsyncHandles_[request.command];
var asyncResponse;
if (!handle && !asyncHandle) return;
// Create an initial response.
response = this.createResponse(request);
if (request.arguments) {
var args = request.arguments;
if (args.maxStringLength !== undefined) {
response.setOption('maxStringLength', args.maxStringLength);
}
if (args.asyncResponse) {
asyncResponse = args.asyncResponse;
}
}
if (asyncHandle) {
if (asyncResponse) return JSON.stringify(asyncResponse);
asyncHandle.call(this, request, response, function(error) {
sendCommand(request.command, {
asyncResponse: error || response
});
}.bind(this));
return '{"seq":0,"type":"response","success":true}';
}
handle.call(this, request, response);
} catch (e) {
// If there is no response object created one (without command).
if (!response) {
response = this.createResponse();
}
response.success = false;
response.message = e.toString();
}
// Return the response as a JSON encoded string.
try {
if (response.running !== undefined) {
// Response controls running state.
this.running_ = response.running;
}
response.running = this.running_;
return JSON.stringify(response);
} catch (e) {
// Failed to generate response - return generic error.
return '{"seq":' + response.seq + ',' +
'"request_seq":' + request.seq + ',' +
'"type":"response",' +
'"success":false,' +
'"message":"Internal error: ' + e.toString() + '"}';
}
} catch (e) {
// Failed in one of the catch blocks above - most generic error.
return '{"seq":0,"type":"response","success":false,"message":"Internal error"}';
}
},
processDebugRequest: function WRAPPED_BY_NODE_INSPECTOR(request) {
return (this.extendedProcessDebugJSONRequest_
&& this.extendedProcessDebugJSONRequest_(request))
|| this.processDebugJSONRequest(request);
}
};
inherits(V8Debug, EventEmitter);
function V8Debug() {
this._webkitProtocolEnabled = false;
// NOTE: Call `_setDebugEventListener` before all other changes in Debug Context.
// After node 0.12.0 this function serves to allocate Debug Context
// like a persistent value, that saves all our changes.
this._setDebugEventListener();
this._wrapDebugCommandProcessor();
this.once('close', function() {
this._unwrapDebugCommandProcessor();
this._unsetDebugEventListener();
process.nextTick(function() {
this.removeAllListeners();
}.bind(this));
});
}
V8Debug.prototype._setDebugEventListener = function() {
var Debug = this.get('Debug');
Debug.setListener(function(_, execState, event) {
// TODO(3y3): Handle events here
});
};
V8Debug.prototype._unsetDebugEventListener = function() {
var Debug = this.get('Debug');
Debug.setListener(null);
};
V8Debug.prototype._wrapDebugCommandProcessor = function() {
var proto = this.get('DebugCommandProcessor.prototype');
overrides.processDebugRequest_ = proto.processDebugRequest;
extend(proto, overrides);
overrides.extendedProcessDebugJSONRequestHandles_['disconnect'] = function(request, response) {
this.emit('close');
this.processDebugJSONRequest(request);
}.bind(this);
};
V8Debug.prototype._unwrapDebugCommandProcessor = function() {
var proto = this.get('DebugCommandProcessor.prototype');
proto.processDebugRequest = proto.processDebugRequest_;
delete proto.processDebugRequest_;
delete proto.extendedProcessDebugJSONRequest_;
delete proto.extendedProcessDebugJSONRequestHandles_;
delete proto.extendedProcessDebugJSONRequestAsyncHandles_;
};
V8Debug.prototype.register =
V8Debug.prototype.registerCommand = function(name, func) {
overrides.extendedProcessDebugJSONRequestHandles_[name] = func;
};
V8Debug.prototype.registerAsync =
V8Debug.prototype.registerAsyncCommand = function(name, func) {
overrides.extendedProcessDebugJSONRequestAsyncHandles_[name] = func;
};
V8Debug.prototype.command =
V8Debug.prototype.sendCommand =
V8Debug.prototype.emitEvent = sendCommand;
function sendCommand(name, attributes, userdata) {
var message = {
seq: 0,
type: 'request',
command: name,
arguments: attributes || {}
};
binding.sendCommand(JSON.stringify(message));
};
V8Debug.prototype.commandToEvent = function(request, response) {
response.type = 'event';
response.event = response.command;
response.body = request.arguments || {};
delete response.command;
delete response.request_seq;
};
V8Debug.prototype.registerEvent = function(name) {
overrides.extendedProcessDebugJSONRequestHandles_[name] = this.commandToEvent;
};
V8Debug.prototype.get =
V8Debug.prototype.runInDebugContext = function(script) {
if (typeof script == 'function') script = script.toString() + '()';
script = /\);$/.test(script) ? script : '(' + script + ');';
return binding.runScript(script);
};
V8Debug.prototype.getFromFrame = function(index, value) {
var result;
binding.call(function(execState) {
var _index = index + 1;
var _count = execState.frameCount();
if (_count > _index + 1 ) {
var frame = execState.frame(_index + 1);
_count = frame.scopeCount();
_index = 0;
while (_count --> 0) {
var scope = frame.scope(_index).scopeObject().value();
if (scope[value]) {
result = scope[value];
return;
}
}
}
});
return result;
};
V8Debug.prototype.enableWebkitProtocol = function() {
if (!NODE_NEXT) {
throw new Error('WebKit protocol is not supported on target node version (' + process.version + ')');
}
if (this._webkitProtocolEnabled) return;
var DebuggerScriptSource,
DebuggerScript,
InjectedScriptSource,
InjectedScript,
InjectedScriptHostSource,
InjectedScriptHost;
function prepareSource(source) {
return 'var ToggleMirrorCache = ToggleMirrorCache || function() {};\n' +
'(function() {' +
('' + source).replace(/^.*?"use strict";(\r?\n.*?)*\(/m, '\r\n"use strict";\nreturn (') +
'}());';
}
DebuggerScriptSource = prepareSource(fs.readFileSync(DebuggerScriptLink, 'utf8'));
DebuggerScript = this.runInDebugContext(DebuggerScriptSource);
InjectedScriptSource = prepareSource(fs.readFileSync(InjectedScriptLink, 'utf8'));
InjectedScript = this.runInDebugContext(InjectedScriptSource);
InjectedScriptHostSource = prepareSource(fs.readFileSync(InjectedScriptHostLink, 'utf8'));
InjectedScriptHost = this.runInDebugContext(InjectedScriptHostSource)(binding, DebuggerScript);
var injectedScript = InjectedScript(InjectedScriptHost, global, 1);
this.registerAgentCommand = function(command, parameters, callback) {
this.registerCommand(command, new WebkitProtocolCallback(parameters, callback));
};
this._webkitProtocolEnabled = true;
function WebkitProtocolCallback(argsList, callback) {
return function(request, response) {
InjectedScriptHost.execState = this.exec_state_;
var args = argsList.map(function(name) {
return request.arguments[name];
});
callback.call(this, args, response, injectedScript, DebuggerScript);
InjectedScriptHost.execState = null;
}
}
};
V8Debug.prototype.registerAgentCommand = function(command, parameters, callback) {
throw new Error('Use "enableWebkitProtocol" before using this method');
};
module.exports = new V8Debug();