-
Notifications
You must be signed in to change notification settings - Fork 23
/
Main.hx
438 lines (418 loc) · 11.7 KB
/
Main.hx
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
package hld;
#if debug
@:jsRequire('source-map-support')
extern class SMS {
public static function install() : Void;
}
class SMSInstall {
static function __init__() : Void {
SMS.install();
}
}
#end
/**
Commandline interface - GDB like
**/
class Main {
var args = Sys.args();
var debugPort = 5001;
var file = null;
var pid : Null<Int>;
var breaks = [];
var dbg : hld.Debugger;
var stdin = Sys.stdin();
var isCi = false;
#if nodejs
var process : js.node.child_process.ChildProcess;
#else
var process : sys.io.Process;
#end
function new() {
}
function error( msg ) {
Sys.stderr().writeString(msg + "\n");
Sys.exit(1);
}
function init() {
var cmd = "hl";
while( args.length > 0 && args[0].charCodeAt(0) == '-'.code ) {
var param = args.shift();
switch( param ) {
case "-port":
param = args.shift();
if( param == null || (debugPort = Std.parseInt(param)) == 0 )
error("Require port int value");
case "-attach":
param = args.shift();
if( param == null || (pid = Std.parseInt(param)) == null )
error("Require attach process id value");
case "--cmd":
cmd = args.shift();
case "--cwd":
Sys.setCwd(args.shift());
case "--input":
var file = args.shift();
var inputArgs = ~/[ \n\r\t]+/g.split(StringTools.trim(sys.io.File.getContent(file)));
while( inputArgs.length > 0 ) {
var a = inputArgs.pop();
if( StringTools.endsWith(a,'"') ) {
if( StringTools.startsWith(a,'"') ) {
args.unshift(a.substr(1,a.length-2));
continue;
}
var arg = [a.substr(0,a.length-1)];
while( true ) {
var a = inputArgs.pop();
if( a == null ) break;
if( StringTools.startsWith(a,'"') ) {
arg.unshift(a.substr(1));
break;
}
arg.unshift(a);
}
args.unshift(arg.join(" "));
continue;
}
args.unshift(a);
}
case "--ci":
isCi = true;
case "--debug":
Debugger.DEBUG = true;
default:
error("Unsupported parameter " + param);
}
}
file = args.shift();
if( file == null ) {
Sys.println("hldebug [-port <port>] [--cwd <path>] <file.hl> [<commands>]");
Sys.exit(1);
}
if( !sys.FileSystem.exists(file) )
error(file+" not found");
var hlArgs = [];
if( args[0] == "--args" ) {
args.shift();
while( true ) {
var a = args.shift();
if( a == null || a == "--" ) break;
hlArgs.push(a);
}
}
if( pid == null ) {
var args = ["--debug", "" + debugPort, "--debug-wait", file].concat(hlArgs);
#if nodejs
process = js.node.ChildProcess.spawn(cmd, args);
process.stdout.on("data", function(data:String) Sys.print(data));
process.stderr.on("data", function(data:String) Sys.stderr().writeString(data));
pid = process.pid;
#else
process = new sys.io.Process(cmd, args);
pid = process.getPid();
#end
}
dbg = new hld.Debugger();
dbg.loadModule(sys.io.File.getBytes(file));
function getAPI() : hld.Api {
#if hl
return new hld.HLDebugApi(pid, dbg.is64);
#elseif nodejs
return new hld.NodeDebugApiNative(pid, dbg.is64);
#else
throw "This platform does not have a debug API";
return null;
#end
}
dbg.connectTries("127.0.0.1", debugPort, 10, function(b) {
if( !b || !dbg.init(getAPI()) ) {
dumpProcessOut();
error("Failed to access process #" + pid + " on port " + debugPort + " for debugging");
return;
}
while( command() ) {
}
Sys.exit(0);
});
}
function frameStr( f : Debugger.StackInfo, ?debug ) {
if( f == null ) return "???";
return f.file+":" + f.line + (f.context == null ? "" : " ("+f.context.obj.name+"::"+f.context.field+")") + (debug ? " @"+f.ebp.toString():"");
}
function dumpProcessOut() {
if( process == null ) return;
#if nodejs
process.kill();
#else
if( process.exitCode(false) == null ) process.kill();
Sys.print(process.stdout.readAll().toString());
Sys.stderr().writeString(process.stderr.readAll().toString());
#end
}
function clearBP() {
var count = breaks.length;
for( b in breaks )
dbg.removeBreakpoint(b.file, b.line);
breaks = [];
Sys.println(count + " breakpoints removed");
}
function handleResult( r : hld.Api.WaitResult ) {
switch( r ) {
case Exit:
#if !nodejs
dbg.resume();
#end
Sys.println("Process has exit");
Sys.exit(0);
case Breakpoint:
var threadName = if( isCi ) "Thread" else dbg.getThreadName(dbg.stoppedThread);
Sys.println(threadName + " paused " + frameStr(dbg.getStackFrame()));
var exc = dbg.getException();
if( exc != null )
Sys.println("Exception: "+dbg.eval.valueStr(exc));
case Error:
Sys.println("*** an error has occured, paused ***");
case Watchbreak:
var w = dbg.watchBreak;
Sys.println("Watch change " + w.ptr.toString() + ":" + w.t.toString() + " = " + dbg.eval.valueStr(dbg.eval.fetch(w)) + " at "+frameStr(dbg.getStackFrame()));
case Timeout:
dbg.customTimeout = null;
handleResult(dbg.pause());
case StackOverflow:
Sys.println("*** stack overflow, paused ***");
default:
throw "assert "+r;
}
}
function command() {
#if !nodejs
if( process != null ) {
var ecode = process.exitCode(false);
if( ecode != null ) {
dumpProcessOut();
error("Process exit with code " + ecode);
}
}
#end
Sys.print("> ");
var r = args.shift();
if( r == null )
r = stdin.readLine();
else
Sys.println(r);
var args = ~/[ \t\r\n]+/g.split(r);
var cmd = args.shift();
inline function nextArg() {
var a = args.shift();
if( a == null ) throw cmd+" is mising argument";
return a;
}
switch( cmd ) {
case "q", "quit":
if( !isCi )
dumpProcessOut();
return false;
case "r", "run", "c", "continue":
var time = args.shift();
dbg.customTimeout = time == null ? null : Std.parseFloat(time);
while( true ) {
var r = dbg.run();
if( r == Handled ) continue;
handleResult(r);
break;
}
case "bt", "backtrace":
for( f in dbg.getBackTrace() )
Sys.println(frameStr(f));
case "btdebug":
for( f in dbg.getBackTrace() )
Sys.println(frameStr(f,true));
case "where":
Sys.println(frameStr(dbg.getStackFrame()));
case "frame","f":
if( args.length == 1 )
dbg.currentStackFrame = Std.parseInt(args[0]);
Sys.println(frameStr(dbg.getStackFrame()));
case "up":
dbg.currentStackFrame += args.length == 0 ? 1 : Std.parseInt(args[0]);
if( dbg.currentStackFrame >= dbg.stackFrameCount )
dbg.currentStackFrame = dbg.stackFrameCount - 1;
Sys.println(frameStr(dbg.getStackFrame()));
case "down":
dbg.currentStackFrame -= args.length == 0 ? 1 : Std.parseInt(args[0]);
if( dbg.currentStackFrame < 0 )
dbg.currentStackFrame = 0;
Sys.println(frameStr(dbg.getStackFrame()));
case "b", "break":
var fileLine = nextArg().split(":");
var line = Std.parseInt(fileLine.pop());
var file = fileLine.join(":");
var condition = args.shift();
line = dbg.addBreakpoint(file, line, condition);
if( line >= 0 ) {
breaks.push({file:file, line:line, condition:condition});
Sys.println("Breakpoint set line " + line + (condition == null ? "" : ", cond: " + condition));
} else
Sys.println("No breakpoint set");
case "p", "print", "global":
var expr = nextArg();
if( expr == null ) {
Sys.println("Requires expression");
return true;
}
var global = cmd == "global";
var v = if( Debugger.DEBUG ) dbg.getValue(expr, global) else try dbg.getValue(expr, global) catch( e : Dynamic ) {
Sys.println("Error " + e + haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
return true;
}
if( v == null ) {
Sys.println("Unknown var " + expr);
return true;
}
Sys.println(dbg.eval.valueStr(v) + " : " + v.t.toString());
var fields = dbg.eval.getFields(v);
if( fields != null )
for( f in fields ) {
var fv = dbg.eval.readField(v, f);
Sys.println(" " + f + " = " + dbg.eval.valueStr(fv) + " : " + fv.t.toString());
}
switch( v.v ) {
case VClosure(_):
var stack = dbg.getClosureStack(v.v);
for( s in stack )
Sys.println(" "+frameStr(s));
default:
}
case "watch", "rwatch":
var expr = nextArg();
var v = if( Debugger.DEBUG ) dbg.getRef(expr) else try dbg.getRef(expr) catch( e : Dynamic ) {
Sys.println("Error " + e + haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
return true;
};
if( v == null ) {
Sys.println("Unknown var " + expr);
return true;
}
if( v.ptr == null ) {
Sys.println("Can't watch undefined var");
return true;
}
try {
dbg.watch(v, cmd == "rwatch");
} catch( e : Dynamic ) {
Sys.println("Error " + e + haxe.CallStack.toString(haxe.CallStack.exceptionStack()));
return true;
}
Sys.println("Watching " + v.ptr.toString() + ":" + v.t.toString() + " " + dbg.eval.valueStr(dbg.eval.fetch(v)));
case "unwatch":
var param = nextArg();
var count = 0;
for( w in dbg.getWatches() )
if( param == null || w.ptr.toString() == param ) {
dbg.unwatch(w);
count++;
}
Sys.println("Unwatch " + count + " addresses");
case "clear":
switch( args.length ) {
case 0:
clearBP();
case 1:
var file = args[1];
var line = Std.parseInt(file);
var count = 0;
for( b in breaks.copy() )
if( b.file == file || (line != null && b.line == line) ) {
dbg.removeBreakpoint(b.file, b.line);
breaks.remove(b);
count++;
}
Sys.println(count + " breakpoints removed");
}
case "delete", "d":
clearBP();
case "next", "n":
handleResult(dbg.step(Next));
case "step", "s":
handleResult(dbg.step(Into));
case "finish":
handleResult(dbg.step(Out));
case "thread":
var arg = args.shift();
if( arg != null ) {
var tid = Std.parseInt(args.shift());
if( tid != null ) tid = dbg.getThreads()[tid];
if( tid != null ) dbg.setCurrentThread(tid);
}
Sys.println(dbg.getThreadName(dbg.currentThread));
case "info":
function printVar( name : String ) {
var v = dbg.getValue(name);
Sys.println(" " + name+" = " + (v == null ? "???" : dbg.eval.valueStr(v) + " : " + v.t.toString()));
}
switch( args.shift() ) {
case "args":
for( name in dbg.getCurrentVars(true) )
printVar(name);
case "locals":
for( name in dbg.getCurrentVars(false) )
printVar(name);
case "variables":
for( name in dbg.getCurrentVars(true).concat(dbg.getCurrentVars(false)) )
printVar(name);
case "threads":
var cur = dbg.currentThread;
var stack = dbg.currentStackFrame;
var index = 0;
for( tid in dbg.getThreads() ) {
dbg.setCurrentThread(tid);
Sys.println((tid == cur ? "*" : " ")+" Thread "+(index++)+"("+tid+") "+frameStr(dbg.getBackTrace()[0]));
}
dbg.setCurrentThread(cur);
dbg.currentStackFrame = stack;
case "statics":
var cl = dbg.getCurrentClass();
if( cl == null )
Sys.println("Class not found");
else {
Sys.println("Class "+cl);
var fields = dbg.getClassStatics(cl);
for( f in fields )
printVar(cl+"."+f);
}
default:
Sys.println("Unknown info request");
}
case "catch":
var arg = args.shift();
switch( arg ) {
case "throw":
dbg.breakOnThrow = true;
case "uncaught":
dbg.breakOnThrow = false;
default:
Sys.println("Invalid catch mode");
}
case "set":
var all = args.join(" ").split("=");
var expr = all.shift();
var value = all.join("=");
if( expr == "" || value == null || value == "" ) {
Sys.println("Syntax: set <expr>=<value>");
return true;
}
var value = dbg.setValue(expr,value);
if( value == null )
Sys.println("Failed to set expression");
else
Sys.println(dbg.eval.valueStr(value) + " : " + value.t.toString());
case "cd":
try Sys.setCwd(args.shift()) catch( e : Dynamic ) Sys.println(""+e);
default:
Sys.println("Unknown command " + r);
}
return true;
}
static function main() {
new Main().init();
}
}