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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
#include <setjmp.h>
#include <stdarg.h>
#include <wchar.h>
#include "config.h"
#include "lib.h"
#include "debug.h"
#include "gc.h"
#include "unwind.h"
#include "stream.h"
#include "parser.h"
int opt_debugger;
int debug_depth;
val debug_block_s;
static int step_mode;
static int next_depth = -1;
static val breakpoints;
static val last_command;
static int cols = 80;
static void help(val stream)
{
put_string(lit("commands:\n"
"? - help s - step into form\n"
"h - help n - step over form\n"
"c - continue f - finish form\n"
"v - show variable binding environment s - show current form\n"
"b - set breakpoint by line number i - show current data\n"
"d - delete breakpoint w - backtrace\n"
"l - list breakpoints\n"),
stream);
}
static void show_bindings(val env, val stream)
{
val level = zero;
put_string(lit("bindings:\n"), stream);
for (;; level = plus(level, one)) {
if (nullp(env))
break;
else if (consp(env)) {
format(stream, lit("~s: ~s\n"), level, env, nao);
break;
} else if (type(env) == ENV) {
format(stream, lit("~s: ~s\n"), level, env->e.vbindings, nao);
env = env->e.up_env;
} else {
format(stream, lit("invalid environment object: ~s\n"), env, nao);
break;
}
}
}
val debug(val form, val bindings, val data, val line, val pos, val base)
{
uses_or2;
val lineno = source_loc(form);
if (!step_mode && !memqual(lineno, breakpoints)
&& (debug_depth > next_depth))
{
return nil;
} else {
val print_form = t;
val print_data = t;
for (;;) {
val input, command;
if (print_form) {
format(std_output, lit("stopped at line ~a\n"), lineno, nao);
format(std_output, lit("form: ~s\n"), form, nao);
format(std_output, lit("depth: ~s\n"), num(debug_depth), nao);
print_form = nil;
}
if (print_data) {
if (data && pos) {
val half = num((cols - 8) / 2);
val full = num((cols - 8));
val prefix, suffix;
if (lt(pos, half)) {
prefix = sub_str(data, zero, pos);
suffix = sub_str(data, pos, full);
} else {
prefix = sub_str(data, minus(pos, half), pos);
suffix = sub_str(data, pos, plus(pos, half));
}
format(std_output, lit("data (~s:~s):\n~s . ~s\n"),
line, plus(pos, base), prefix, suffix, nao);
} else {
format(std_output, lit("data (~s):\n~s\n"), line, data, nao);
}
print_data = nil;
}
format(std_output, lit("txr> "), nao);
flush_stream(std_output);
input = split_str_set(or2(get_line(std_input), lit("q")), lit("\t "));
command = if3(equal(first(input), null_string),
or2(last_command, lit("")), first(input));
last_command = command;
if (equal(command, lit("?")) || equal(command, lit("h"))) {
help(std_output);
continue;
} else if (equal(command, null_string)) {
continue;
} else if (equal(command, lit("c"))) {
step_mode = 0;
next_depth = -1;
return nil;
} else if (equal(command, lit("s"))) {
step_mode = 1;
return nil;
} else if (equal(command, lit("n"))) {
step_mode = 0;
next_depth = debug_depth;
return nil;
} else if (equal(command, lit("f"))) {
step_mode = 0;
next_depth = debug_depth - 1;
return nil;
} else if (equal(command, lit("v"))) {
show_bindings(bindings, std_output);
} else if (equal(command, lit("s"))) {
print_form = t;
} else if (equal(command, lit("i"))) {
print_data = t;
} else if (equal(command, lit("b")) || equal(command, lit("d"))) {
if (!rest(input)) {
format(std_output, lit("b needs argument\n"), nao);
continue;
} else {
long n = wcstol(c_str(second(input)), NULL, 10);
if (equal(command, lit("b")))
push(num(n), &breakpoints);
}
} else if (equal(command, lit("l"))) {
format(std_output, lit("breakpoints: ~s\n"), breakpoints, nao);
} else if (equal(command, lit("w"))) {
format(std_output, lit("backtrace:\n"), nao);
{
uw_frame_t *iter;
for (iter = uw_current_frame(); iter != 0; iter = iter->uw.up) {
if (iter->uw.type == UW_DBG) {
if (iter->db.ub_p_a_pairs)
format(std_output, lit("(~s ~s ~s)\n"), iter->db.func,
iter->db.args, iter->db.ub_p_a_pairs, nao);
else
format(std_output, lit("(~s ~s)\n"), iter->db.func,
iter->db.args, nao);
}
}
}
} else if (equal(command, lit("q"))) {
uw_throwf(query_error_s, lit("terminated via debugger"), nao);
} else {
format(std_output, lit("unrecognized command: ~a\n"), command, nao);
}
}
return nil;
}
}
void debug_init(void)
{
step_mode = 1;
protect(&breakpoints, &last_command, (val *) 0);
debug_block_s = intern(lit("debug-block"), system_package);
{
char *columns = getenv("COLUMNS");
if (columns)
cols = atoi(columns);
if (cols < 40)
cols = 40;
}
}
|