1
+ # debugger API used if #undef DEBUG_IN_C (not yet enabled)
2
+
3
+ #TODO: parse comma delim args. avoid readline (whois loading or calling)
4
+ Debug = class (args):
5
+ /state = 0
6
+ /break = ()
7
+ /init = (): load "readline".
8
+ /read = (): readline("> ").
9
+ /write = (o): o print.
10
+
11
+ /init()
12
+ .
13
+
14
+ # API (from lua)
15
+ Debug getinfo = (level, name): .
16
+ Debug getlocal = (f, i): .
17
+ Debug setlocal = (f, i, name): .
18
+ Debug setupvalue = (f, i, name): .
19
+ Debug traceback = (f| level): .
20
+ Debug sethook = (hook, name): .
21
+
22
+ # state 0 for next/step
23
+ # 1 :c continue until break
24
+ # 5 :q exit debugger
25
+ # 6 :exit exit program
26
+ Debug loop = (src, proto):
27
+ if (self/state == 1):
28
+ if (self/break bsearch(src line)): return 1.
29
+ .
30
+ write = self/write
31
+ ("\ndebug line ", src file,":", src line string,
32
+ " (:h for help, :q,:c,:n)\n") join write
33
+ loop:
34
+ code = self/read()
35
+ if (code == ":c"):
36
+ "continue\n" write
37
+ self/state = 1
38
+ return 1.
39
+ if (code == ":q"):
40
+ "quit\n" write
41
+ return 5.
42
+ if (code == ":exit"):
43
+ "exit\n" write
44
+ return 6.
45
+ if (code == ":h"):
46
+ (":q quit debugger and continue",
47
+ ":exit quit debugger and exit",
48
+ ":c continue",
49
+ ":b line set breakpoint",
50
+ ":B line unset breakpoint",
51
+ ":n step to next line",
52
+ ":s step into function",
53
+ ":r regs",
54
+ ":l locals",
55
+ ":u upvals",
56
+ ":v values (paths)",
57
+ "expr eval expr",
58
+ "src current ast",
59
+ "proto current function\n") join("\n") write
60
+ continue.
61
+ if (code slice(0,2) == ":b"):
62
+ b = code slice(4, code length) number
63
+ if (self/break bsearch(b)): ("breakpoint ",b string," already defined\n") join write.
64
+ else: self/break = self/break push (b) sort.
65
+ continue.
66
+ if (code slice(0,2) == ":B"):
67
+ b = code slice(4, code length) number
68
+ i = self/break bsearch(b)
69
+ if (i):
70
+ self/break delete(i)
71
+ ("breakpoint ", b string, " deleted") join write.
72
+ continue.
73
+ if (code != ""):
74
+ code = code eval string write.
75
+ .
76
+ 2
77
+ .
78
+
79
+ debug = Debug
0 commit comments