-
Notifications
You must be signed in to change notification settings - Fork 11
/
SourceInspection.js
218 lines (188 loc) · 7.02 KB
/
SourceInspection.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
var EventEmitter = require('events').EventEmitter
, Debugger = require('./lib/debugger.js')
, util = require('util')
, ReferenceParser = require('./ReferenceParser.js');
// Output documentation:
// https://github.com/pgbovine/OnlinePythonTutor/blob/master/v3/docs/opt-trace-format.md
var SourceInspection = function(filename, proc, port) {
EventEmitter.call(this);
var stdout = '';
var traces = [];
var self = this;
var userScriptRef = null;
var last_line = 1;
var excludingVars =
{'__dirname': 1, '__filename': 1, 'exports': 1,
'module': 1, 'require': 1};
// it is expected, but not confirmed, that stdout will be in correct order.
proc.on('output', function(data) {
stdout += data;
});
console.log('[SourceInspection] Attach Debugger to port', port);
var dbgr = Debugger.attachDebugger(port);
dbgr.on('close', function() {
// need to correct the line number of the last trace
// because it is usually outside the program length
for (var i = traces.length - 2; i >= 0; i--) {
var stepData = traces[i];
if (!stepData['stack_to_render'] || stepData['stack_to_render'].length == 0) {
traces[traces.length - 1].line = stepData.line;
break;
}
}
// console.log(util.inspect(traces, false, null));
self.emit('done', traces);
});
dbgr.on('error', function(e) {
console.log('User Dbg: Error! ', e);
self.emit('error');
});
dbgr.on('exception', function(e) {
console.log('User Dbg: Exception!: ', e);
self.emit('error');
});
dbgr.on('connect', function() { console.log('User Dbg: Connected!'); });
dbgr.on('break', function(obj) {
// first stop when the code reach first line of user's code
// request backtrace
// - if not in user source -> step out
// - otherwise step in
// inspect obj.body.script.name
var scriptPath = obj.body.script.name;
// console.log('Break ', scriptPath, ': ', obj.body.sourceLine);
if (!isUserScript(scriptPath)) {
// if it is not user_program => step out
dbgr.request('continue', { arguments: { stepaction: 'out' } });
return;
}
// extract current state of the code: variables, closures
dbgr.request('backtrace', { arguments: { } }, function(resp) {
// all recorded data are for the source only.
// by default of V8debugger, there is maximum 10 stack frames.
// that should do it for now
extractSingleStep(resp, function() {
dbgr.request('continue', { arguments: { stepaction: 'in' } });
});
});
});
var extractSingleStep = function(backtraceData, callback) {
// console.log('======================================');
// each frame extract: local variable & argument
// need to fetch the value by reference
// each frame = 1 stack level
// TODO: extract scope information of a frame
var frames = backtraceData.body.frames;
var stepData = {};
var handles = [];
var refValues = {};
var variables = [];
var func_refs = [];
// placeholder. event can ben step_line or return
stepData['event'] = 'step_line';
stepData['line'] = frames[0].line + 1; // in OPT line number start from 1
// function name is contained in a ref!
// stepData['func_name'] = frames[0].func;
var processVar = function(v) {
variables[v.name] = v;
handles.push(v.value.ref);
};
for (var i = backtraceData.body.toFrame - 1; i >= 0; i--) {
var frame = frames[i];
// do not go into system code
var filepath = extractFileNameFromSource(frame.text);
if (!isUserScript(filepath)) {
continue;
}
// console.log(frame);
func_refs.push(frame.func);
handles.push(frame.func.ref);
var localVars = {};
if (frame.locals) {
for (var j = 0; j < frame.locals.length; j++) {
var v = frame.locals[j];
localVars[v.name] = v.value;
handles.push(v.value.ref);
}
}
if (frame.arguments) {
for (var j = 0; j < frame.arguments.length; j++) {
var v = frame.arguments[j];
if (!excludingVars[v.name]) {
localVars[v.name] = v.value;
handles.push(v.value.ref);
}
}
}
if (frame.atReturn) {
stepData['event'] = 'return';
localVars['__return__'] = frame.returnValue;
handles.push(frame.returnValue.ref);
}
variables.push(localVars);
}
var postProcessing = function() {
// parse heap & global / local values to fit the output format by OPT
var renderResult = ReferenceParser.renderOPTFormat(refValues, variables);
// console.log(' function names ', func_refs);
stepData['func_name'] =
ReferenceParser.extractFuncName(refValues[func_refs[0].ref].source);
stepData['ordered_globals'] = Object.keys(variables[0]).sort();
stepData['globals'] = renderResult.variableDicts[0];
stepData['heap'] = renderResult.heap;
var allStacks = [];
for (var i = 1; i < func_refs.length; i++) {
// higher stack level
var stackInfo = {};
// console.log(func_refs[i]);
// console.log(refValues[func_refs[i].ref]);
stackInfo['func_name'] =
ReferenceParser.extractFuncName(refValues[func_refs[i].ref].source);
stackInfo['encoded_locals'] = renderResult.variableDicts[i];
stackInfo['ordered_varnames'] = Object.keys(variables[i]).sort();
stackInfo['is_highlighted'] = (i == func_refs.length - 1);
stackInfo['frame_id'] = i;
stackInfo['unique_hash'] = stackInfo['func_name'] + '_f' + stackInfo['frame_id'];
// extra
stackInfo['parent_frame_id_list'] = [];
stackInfo['is_zombie'] = false;
stackInfo['is_parent'] = false;
allStacks.push(stackInfo);
}
stepData['stack_to_render'] = allStacks;
stepData['stdout'] = stdout;
traces.push(stepData);
// step forward
callback();
}
var processLookup = function(resp) {
var refs = [];
for (var refId in resp.body) {
refValues[refId] = resp.body[refId];
var innerRefs = ReferenceParser.extractRef(resp.body[refId]);
refs = refs.concat(innerRefs);
}
if (refs.length) {
// repeat the lookup, because the refence can be nested.
dbgr.request('lookup', { arguments: { handles: refs } }, processLookup);
} else {
// cleanup and step forward.
postProcessing();
}
}
// fetching actual value of variables
dbgr.request('lookup', { arguments: { handles: handles } }, processLookup);
};
// return true if the script in scriptPath is user's script
var isUserScript = function(scriptPath) {
var paths = scriptPath.split('/');
return (paths[paths.length - 1] == filename);
};
var extractFileNameFromSource = function(frameText) {
var pieces = frameText.split(' ');
// 7 = magic number by observing the data
var path = pieces[pieces.length - 7];
return path;
};
};
util.inherits(SourceInspection, EventEmitter);
module.exports = SourceInspection;