My favorites | Sign in
Project Home Wiki Issues Source
READ-ONLY: This project has been archived. For more information see this post.
Search
for
  Advanced search   Search tips   Subscriptions

Issue 233 attachment: DebugBase.hx (18.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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
package hxcpp;

import cpp.vm.Deque;
import cpp.vm.Debugger;
import cpp.vm.Thread;
import haxe.CallStack;

enum DebugToken
{
IDENT(name:String);
CONST(value:Dynamic);
DOT;
LPAREN;
RPAREN;
COMMA;
EQUALS;
LARRAY;
RARRAY;
}

enum DebugExpr
{
EXPR_VALUE(value:Dynamic);
EXPR_FIELD_REF(obj:Dynamic,member:String);
EXPR_ARRAY_REF(obj:Dynamic,index:Int);
EXPR_STACK_REF(name:String);
}



class DebugBase
{
var threadStopped:Bool;
var stillDebugging:Bool;
var inputThread:Thread;
var debugQueue:Deque<Dynamic>;
var files:Array<String>;
var frame:Int;
var stack:Array<StackItem>;
var vars:Array<String>;
var dbg:Dynamic;
var arrayLimit:Int;

public function new(inCreateStopped:Bool)
{
frame = -1;
stillDebugging = true;
stillDebugging = init();
arrayLimit = 100;
dbg = {};
if (stillDebugging)
{
files = Debugger.getFiles();
threadStopped = false;
Debugger.setThread();
Debugger.setHandler(onDebug);
debugQueue= new Deque<Dynamic>();
inputThread = Thread.create(inputLoop);
if (inCreateStopped)
Debugger.breakBad();
}
}

function init() : Bool
{
return false;
}

function onDebug()
{
threadStopped = true;
onStopped();
while(threadStopped && stillDebugging)
{
var job = debugQueue.pop(true);
job();
}
}

function onHelp()
{
sendString("help - print this message");
sendString("break [file line] - pause execution of one thread [when at certain point]");
sendString("breakpoints - list breakpoints");
sendString("delete N - delete breakpoint N");
sendString("cont - continue execution");
sendString("where - print call stack");
sendString("files - print file list that may be used with breakpoints");
sendString("vars - print local vars for frame");
sendString("array limit N - show at most N array elements");
sendString("mem - print memory usage");
sendString("collect - run Gc collection");
sendString("compact - reduce memory usage");
sendString("exit - exit programme");
sendString("bye - stop debugging, keep running");
}


function waitDebugger(inSendResult:Bool=true)
{
debugQueue.add( function() inputThread.sendMessage("ok") );
var result = Thread.readMessage(true);
if (inSendResult)
{
if (result!="ok")
onResult("Debugger out of sync");
else
onResult("ok");
}
}
function onCloseInput() { }

function onStopped() { }

function onRunning() { }

function showWhere() { }

function showFiles() { }

function showBreakpoints() { }

function onPrint(result:Dynamic) { }

function getNextCommand() : String { return "bye"; }

function onResult(inResult:String) { }

function addBreakpoint(inFile:String, inLine:String)
{
var id = Std.parseInt(inFile);
if (id==null)
{
for(idx in 0...files.length)
if (files[idx]==inFile)
{
id = idx;
break;
}
}

if (id==null)
onResult("Could not find file for " + inFile );
else
{
Debugger.addBreakpoint(id,Std.parseInt(inLine));
onResult("ok");
}

}

static var dot:Int = ".".charCodeAt(0);
static var quote:Int = "\"".charCodeAt(0);
static var comma:Int = ",".charCodeAt(0);
static var equals:Int = "=".charCodeAt(0);
static var minus:Int = "-".charCodeAt(0);
static var a_code:Int = "a".charCodeAt(0);
static var z_code:Int = "z".charCodeAt(0);
static var A_code:Int = "A".charCodeAt(0);
static var Z_code:Int = "Z".charCodeAt(0);
static var __code:Int = "_".charCodeAt(0);
static var num0_code:Int = "0".charCodeAt(0);
static var num9_code:Int = "9".charCodeAt(0);
static var space_code:Int = " ".charCodeAt(0);
static var lparent:Int = "(".charCodeAt(0);
static var rparent:Int = ")".charCodeAt(0);
static var larray:Int = "[".charCodeAt(0);
static var rarray:Int = "]".charCodeAt(0);


function tokenize(inString:String) : Array<DebugToken>
{
var len = inString.length;
var result = new Array<DebugToken>();

var idx = 0;
while(idx<len)
{
var code = inString.charCodeAt(idx);

// Identifier ...
if ( (code>=a_code && code<=z_code) || (code>=A_code && code<=Z_code) || code==__code )
{
var start = idx++;
while(idx<len)
{
code = inString.charCodeAt(idx);
if ( (code>=a_code && code<=z_code) || (code>=A_code && code<=Z_code) || code==__code ||
(code>=num0_code && code<num9_code) )
idx++;
else
break;
}
result.push( IDENT( inString.substr(start, idx-start) ) );
}
else if (code==minus || (code>=num0_code && code<=num9_code) )
{
var start = idx++;
while(idx<len)
{
code = inString.charCodeAt(idx);
if (code==dot || (code>=num0_code && code<=num9_code) )
idx++;
else
break;
}
var val = inString.substr(start, idx-start);
var num = Std.parseFloat(val);
if (!Math.isFinite(num))
throw ("Bad constant '" + val + "'");
result.push( CONST(num) );

}
else if (code==quote)
{
var start = ++idx;
while(idx<len)
{
code = inString.charCodeAt(idx);
if (code==quote)
break;
idx++;
}
var val = inString.substr(start, idx-start);
result.push( CONST(val) );
idx++;
}
else
{
if(code == space_code)
{
// do nothing
}
else if(code == lparent)
{
result.push( LPAREN );
}
else if(code == rparent)
{
result.push( RPAREN );
}
else if(code == larray)
{
result.push( LARRAY );
}
else if(code == rarray)
{
result.push( RARRAY );
}
else if(code == dot)
{
result.push( DOT );
}
else if(code == comma)
{
result.push( COMMA );
}
else if(code == equals)
{
result.push( EQUALS );
}
idx++;
}
}

return result;
}

function resolve(inName:String) : DebugExpr
{
var hasThis = false;
if (vars!=null)
{
for(v in vars)
{
if (v==inName)
return EXPR_STACK_REF(inName);
if (v=="this")
hasThis = true;
}
}

var cls = Type.resolveClass(inName);
if (cls!=null)
return EXPR_VALUE(cls);

if (hasThis)
{
var thiz:Dynamic = Debugger.getStackVar(frame,"this");
if (thiz!=null)
{
if (Reflect.hasField(thiz,inName))
return EXPR_FIELD_REF(thiz,inName);
var clazz = Type.getClass(thiz);
var fields = Type.getInstanceFields(clazz);
for(f in fields)
if (f==inName)
return EXPR_FIELD_REF(thiz,inName);

var fields = Type.getClassFields(clazz);
for(f in fields)
if (f==inName)
return EXPR_FIELD_REF(clazz,inName);
}
}

if (inName=="dbg")
return EXPR_VALUE(dbg);

return null;
}


function getExpression(inTokens:Array<DebugToken>) : DebugExpr
{
var classPath = "";
var expr:Dynamic = null;

var tok = 0;
var len = inTokens.length;
while(tok < len)
{
switch(inTokens[tok])
{
case IDENT(name):
if (expr!=null)
throw "Misplaced '" + name + "'";
expr = resolve(name);
if (expr==null)
classPath = name;
tok++;

case CONST(value):
if (expr!=null || classPath!="")
throw "Misplaced '" + value + "'";
expr = EXPR_VALUE(value);
tok++;

case DOT:
if (expr==null && classPath=="")
throw "Bad '.' after null value";
tok++;
switch(inTokens[tok])
{
case IDENT(name):
if (expr!=null)
expr = EXPR_FIELD_REF(exprToDynamic(expr),name);
else
{
var qname = classPath + "." + name;
expr = resolve(qname);
classPath = (expr==null) ? qname : "";
}
tok++;
default: throw "Expected field after '.'";
}

case LPAREN:
var args = new Array<Dynamic>();
var lastComma = tok;
var start = ++tok;
var parenOpen = 1;
var arrayOpen = 0;
while(tok<len && (parenOpen!=0 || arrayOpen!=0) )
{
switch(inTokens[tok])
{
case LPAREN: parenOpen++;
case RPAREN: parenOpen--;
case LARRAY: arrayOpen++;
case RARRAY: arrayOpen--;
case COMMA:
if (arrayOpen==0 && parenOpen==1 && expr!=null)
{
args.push( getValue( inTokens.slice(lastComma+1,tok) ) );
lastComma = tok;
}
default:
}
tok++;
}
if (parenOpen!=0 || arrayOpen!=0)
throw "Mismatched '(' "+parenOpen+"/"+arrayOpen;
// Not function call...
if (classPath!="")
throw "Unresolved " + classPath;
if (expr==null)
{
expr = EXPR_VALUE(getValue( inTokens.slice(start,tok-1) ));
}
else
{
if (lastComma+1 < tok-1)
args.push( getValue( inTokens.slice(lastComma+1,tok-1) ) );
expr = EXPR_VALUE( untyped expr.__Run( args ) );
}

case LARRAY:
var start = ++tok;
var parenOpen = 0;
var arrayOpen = 1;
while(tok<len && (parenOpen!=0 || arrayOpen!=0) )
{
switch(inTokens[tok])
{
case LPAREN: parenOpen++;
case RPAREN: parenOpen--;
case LARRAY: arrayOpen++;
case RARRAY: arrayOpen--;
default:
}
tok++;
}
if (parenOpen!=0 || arrayOpen!=0)
throw "Mismatched '['";
if (classPath!=null)
throw "Unresolved " + classPath;
if (expr==null)
throw "Error taking index of null object";
var val:Dynamic = getValue( inTokens.slice(start,tok) );
if ( !Std.is(val,Int) )
throw "Bad array index: " + val;
expr = EXPR_ARRAY_REF(exprToDynamic(expr), Std.int(val));

case RPAREN: throw "Misplaced ')'";
case COMMA: throw "Misplaced ','";
case EQUALS: throw("Misplaced '='");
case RARRAY: throw "Misplaced ']'";
}
}
if (classPath!="")
throw "Unresolved " + classPath;

return expr==null ? EXPR_VALUE(null) : expr;
}

function exprToDynamic(inExpr:DebugExpr)
{
switch(inExpr)
{
case EXPR_VALUE(value): return value;
case EXPR_FIELD_REF(obj,member): return Reflect.getProperty(obj,member);
case EXPR_ARRAY_REF(obj,index): return obj[index];
case EXPR_STACK_REF(name): return Debugger.getStackVar(frame,name);
}
}

function getValue(inTokens:Array<DebugToken>) : Dynamic
{
return exprToDynamic(getExpression(inTokens));
}


function print(inString:String)
{
var tokens:Array<DebugToken> = null;
try
{
tokens = tokenize(inString);
var result = getValue(tokens);
onPrint(result);
onResult("ok");
}
catch (e:Dynamic)
{
onResult("Error while printing : " + e);//+ ( tokens==null ? "" : " : " + tokens) );
}
}

function set(inString:String)
{
var tokens:Array<DebugToken> = null;
try
{
tokens = tokenize(inString);

var equals_pos = -1;
for(i in 0...tokens.length)
{
if (tokens[i]==EQUALS)
{
if (equals_pos>=0)
throw "more than one '='";
equals_pos = i;
}
}
if (equals_pos<0)
throw "use a = b syntax";
if (equals_pos==0 || equals_pos==tokens.length-1)
throw "Misplaced '='";

var lhs = getExpression( tokens.slice(0,equals_pos) );
var rhs = getValue( tokens.slice(equals_pos+1, tokens.length) );

switch(lhs)
{
case EXPR_VALUE(value): throw "left hand side can't be set";
case EXPR_FIELD_REF(obj,member): Reflect.setProperty(obj,member,rhs);
case EXPR_ARRAY_REF(obj,index): obj[index] = rhs;
case EXPR_STACK_REF(name): Debugger.setStackVar(frame,name,rhs);
}
}
catch (e:Dynamic)
{
onResult("Error while setting : " + e);//+ ( tokens==null ? "" : " : " + tokens) );
return;
}
onResult("ok");
}

function sendString(inString:String) { }


function setFrame(inFrame:Int)
{
if (stack!=null && inFrame>0 && inFrame <= stack.length )
{
frame = inFrame;
vars = Debugger.getStackVars(frame);
}
}

function getStack()
{
stack = haxe.CallStack.callStack();
setFrame(1);
}

function checkStack()
{
if (threadStopped && stack==null)
{
debugQueue.add( getStack );
waitDebugger(false);
}
}

function run()
{
stack = null;
vars = null;
debugQueue.add( function() { threadStopped = false; inputThread.sendMessage("running"); } );
var result = Thread.readMessage(true);
onRunning();
onResult("ok");
}

function inputLoop()
{
while(stillDebugging)
{
checkStack();
var command = getNextCommand();
var words = command.split(" ");
switch(words[0])
{
case "":
onResult("");
// Do nothing

case "bye":
stillDebugging = false;
debugQueue.add( function() { trace("bye"); } );
onResult("bye");

case "exit","quit":
onResult("ok");
Debugger.exit();

case "break","b":
if (words.length==1)
{
if (threadStopped)
onResult("already stopped.");
else
{
Debugger.setBreak(Debugger.BRK_ASAP);
waitDebugger();
}
}
else if (words.length==3)
{
addBreakpoint(words[1],words[2]);
}
else
onResult("Usage: break [file line] - pause execution of one thread [when at certain point]");


case "cont","c":
if (!threadStopped)
onResult("Already running.");
else
run();

case "vars","v":
if (!threadStopped)
onResult("Must break first.");
else if (vars==null)
{
onPrint("no vars");
onResult("ok");
}
else
{
onPrint(vars+"");
onResult("ok");
}

case "frame","f":
if (!threadStopped || stack==null )
onResult("Must break first.");
else
{
var f = Std.parseInt(words[1]);
if (f<1 || f>stack.length )
onResult("Stack out of range.");
else
{
debugQueue.add( function() setFrame(f) );
waitDebugger();
}
}

case "where","w":
if (!threadStopped || stack==null)
onResult("Must break first.");
else
{
showWhere();
onResult("ok");
}

case "print","p":
words.shift();
print(words.join(" "));

case "set","s":
words.shift();
set(words.join(" "));

case "array":
var limit = words[1]=="limit" ? words[2] : words[1];
var lim:Int = Std.parseInt(limit);
if (lim<1)
onResult("usage: array limit N");
else
{
arrayLimit = lim;
onResult("ok");
}

case "files","fi":
{
showFiles();
onResult("ok");
}

case "breakpoints","bp":
{
showBreakpoints();
onResult("ok");
}

case "delete","d":
if (words[1]==null)
{
onResult("Usage : delete N");
}
else
{
var i = Std.parseInt(words[1]);
Debugger.deleteBreakpoint(i);
onResult("ok");
}

case "mem":
sendString( cpp.vm.Gc.memUsage()+" bytes" );
onResult("ok");

case "compact":
sendString( cpp.vm.Gc.compact() + " bytes" );
onResult("ok");

case "collect":
cpp.vm.Gc.run(true);
sendString( cpp.vm.Gc.memUsage()+" bytes" );
onResult("ok");

case "help","h","?":
{
onHelp();
onResult("ok");
}

default:
onResult("Unknown command:" + command);
}
}
onCloseInput();
}

}


Powered by Google Project Hosting