-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.c
388 lines (343 loc) · 9.98 KB
/
debug.c
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
// Copyright (c) 2015, Sam Silberstein. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License").
// Author: [email protected]
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef USE_READLINE
#include <readline.h>
#else
#ifdef USE_EDITLINE
#include <editline/readline.h>
#error balk
#else
#include "readline.h"
#endif
#endif
#include "sim.h"
#include <sys/shm.h>
#include <sys/ipc.h>
#define MAX_ARGS 5
#define MAX_ARGLEN 30
#define MAX_BREAKPOINTS 100
static int ac;
static char av[MAX_ARGS][MAX_ARGLEN];
int strpcmp(char *shortstring, char *longstring) {
return strncmp(shortstring, longstring, strlen(shortstring));
}
int add_breakpoint(struct ami_machine *m, unsigned int addr) {
static int bpnum = 1;
struct breakpoint *b = malloc(sizeof(struct breakpoint));
b->id = bpnum++;
b->enabled = 1;
b->addr = addr;
b->next = m->breakpoints;
m->breakpoints = b;
return b->id;
}
void del_breakpoint(struct ami_machine *m, unsigned int id) {
struct breakpoint **pprev = &m->breakpoints;
struct breakpoint *b = *pprev;
while (b != NULL && b->id != id) {
pprev = &b->next;
b = *pprev;
}
if (b != NULL) {
*pprev = b->next;
free(b);
printf("breakpoint %d deleted\n", id);
} else {
printf("no such breakpoint %d\n", id);
}
}
int is_breakpoint(struct ami_machine *m, unsigned int addr) {
struct breakpoint *b = m->breakpoints;
while (b != NULL && b->addr != addr)
b = b->next;
if (!b) return 0;
if (b->enabled) return 1;
printf("skipping breakpoint at %d\n", addr);
b->enabled = 1;
return 0;
}
int find_breakpoint(struct ami_machine *m, unsigned int addr) {
struct breakpoint *b = m->breakpoints;
while (b != NULL && b->addr != addr)
b = b->next;
if (!b) return 0;
return b->id;
}
void skip_breakpoint(struct ami_machine *m) {
unsigned int addr = m->PC;
struct breakpoint *b = m->breakpoints;
while (b != NULL && b->addr != addr)
b = b->next;
if (b) b->enabled = 0;
}
void unskip_breakpoints(struct ami_machine *m) {
struct breakpoint *b = m->breakpoints;
while (b != NULL) {
b->enabled = 1;
b = b->next;
}
}
void dump_breakpoints(struct ami_machine *m) {
struct breakpoint *b = m->breakpoints;
if (b == NULL) printf("no breakpoints set\n");
while (b != NULL) {
printf("breakpoint %d at address %d\n", b->id, b->addr);
b = b->next;
}
}
void readcmd(struct ami_machine *m) {
static char *line = NULL;
if (line) free(line);
if (m->opt_graphical == 1) {
char buffer[80];
int buffered = 0;
while (*m->shm == '\0') {
nanosleep(100000, &buffered);
}
char *s;
for (s = m->shm; *s != '\0'; s++) {
buffer[buffered] = *s;
buffered++;
}
buffer[buffered] = '\0';
line = (char *) malloc(buffered);
strcpy(line, buffer);
int i;
s = m->shm;
for (i = 0; i < buffered; i++) {
*s = '\0';
s++;
}
buffered = 0;
} else {
line = readline("> ");
if (!line) {
ac = 1;
strcpy(av[0], "q");
return;
} else if (!*line) {
return; // do same command again
}
}
add_history(line);
char *tok = strtok(line, " ");
if (tok == NULL) {
free(line);
return; // do same command again
}
ac = 0;
while (tok != NULL && ac < MAX_ARGS) {
strncpy(av[ac], tok, MAX_ARGLEN-1);
av[ac++][MAX_ARGLEN-1] = '\0';
tok = strtok(NULL, " ");
}
}
void send_string_to_gui(struct ami_machine *m, char string[]) {
char *s;
int i, sleep_time;
//copy string to shared memory
s = m->shm + 1;
for (i = 0; i < strlen(string); i++) {
*s = string[i];
s++;
}
*s = '\0';
//set 'ready' flag
*m->shm = 'r';
while (*m->shm != '\0') {
nanosleep(100000, &sleep_time);
}
}
void update_gui(struct ami_machine *m) {
int i, buffer_size = 256, sent_chars = 0, sleep_time;
char *s;
char buffer[buffer_size];
s = m->shm;
for (i = 0; i < 256; i++) {
*s = '\0';
s++;
}
//build register string to send
sprintf(buffer, "pc: %i\nb: %i\n", m->PC, m->R[0]);
for (i = 1; i < m->reg_count; i++) {
if (strlen(buffer) + 5 > 255) {
break;
} else {
//add new line to string
sprintf(buffer, "%s%i: %i\n", buffer, i, m->R[i]);
}
}
//add delimiter to end of buffer
sprintf(buffer, "%s~", buffer);
//add stack info to string to send
for (i = 0; i < STACK_SIZE; i++) {
char *data = read_stack_entry(m, i);
if (strlen(buffer) + strlen(data) > 253) {
send_string_to_gui(m, buffer);
sprintf(buffer, "%s\n", data);
} else {
sprintf(buffer, "%s%s\n", buffer, data);
}
free(data);
}
//conditionally add console io to string to send
//(with another delimiter)
if (m->console_io_status == 1) {
if (strlen(buffer) + 2 > 253) {
send_string_to_gui(m, buffer);
sprintf(buffer, "~>");
} else {
sprintf(buffer, "%s~>", buffer);
}
} else if (m->console_io_status == 2) {
send_string_to_gui(m, buffer);
sprintf(buffer, "~-> %i\n", m->console_io_value);
m->console_io_status = 0;
} else if (m->console_io_status == 3) {
send_string_to_gui(m, buffer);
sprintf(buffer, "~Program is halted\n");
m->console_io_status = 0;
}
send_string_to_gui(m, buffer);
//set 'end' flag to inform gui no more data is coming
*m->shm = 'r';
*(m->shm + 1) = 'e';
//if we were waiting for input, capture the input sent from the gui
if (m->console_io_status == 1) {
while (*m->shm != 'i') {
nanosleep(100000);
}
m->console_io_value = atoi(m->shm + 1);
m->console_io_status = 0;
}
*m->shm = '\0';
}
void interactive_debug(struct ami_machine *m)
{
rl_initialize();
rl_bind_key('\t', rl_insert);
int err;
if (m->opt_graphical) {
update_gui(m);
}
printf("Welcome to the AMI simulator built-in debugger. Type 'help' for a listing of commands.");
printf("\n");
for (;;) {
if (m->opt_dumpreg) {
dump_registers(m);
}
if (m->opt_printstack != -1) {
dump_stack(m, m->opt_printstack);
}
err = 0;
readcmd(m);
if (ac == 0)
continue;
if (!strpcmp(av[0], "quit") || !strpcmp(av[0], "exit")) {
printf("exiting debugger\n");
exit(0);
} else if (!strpcmp(av[0], "continue")) {
err = run(m, 0);
} else if (!strpcmp(av[0], "step")) {
int steps = (ac == 1 ? 1 : atoi(av[1]));
if (steps <= 0)
printf("expected a positive integer, but got '%s' instead\n", av[1]);
else {
err = run(m, steps);
}
} else if (!strpcmp(av[0], "reset")) {
printf("Resetting program state\n");
unskip_breakpoints(m);
memset(m->R, 0, sizeof(m->R));
m->PC = m->nPC = 0;
m->halted = 0;
free_segments(m);
allocate_stack(m);
} else if (!strpcmp(av[0], "breakpoint")) {
if (ac != 2) {
printf("expected an address, but got %d arguments\n", ac-1);
} else {
unsigned int addr = atoi(av[1]);
if (addr == 0)
printf("expected an address, but got '%s' instead\n", av[1]);
else {
int id = add_breakpoint(m, addr);
printf("set breakpoint %d at address %d\n", id, addr);
}
}
} else if (!strpcmp(av[0], "delete")) {
if (ac != 2) {
printf("exepcted a breakpoint number, but got %d arguments\n", ac-1);
} else {
int id = atoi(av[1]);
if (id == 0)
printf("expected a breakpoint number, but got '%s' instead\n", av[1]);
else
del_breakpoint(m, id);
}
} else if (!strpcmp(av[0], "info")) {
if (ac == 1) {
printf("expected an argument, one of: breakpoints, stack, memory, or registers\n");
} else if (!strpcmp(av[1], "registers")) {
dump_registers(m);
} else if (!strpcmp(av[1], "stack")) {
int size = m->opt_printstack ? m->opt_printstack : 64;
if (ac > 2) size = atoi(av[2]);
if (size < 0) printf("expected a positive integer, but got '%s' instead\n", av[2]);
else dump_stack(m, size);
} else if (!strpcmp(av[1], "breakpoints")) {
dump_breakpoints(m);
} else if (!strpcmp(av[1], "memory")) {
dump_segments(m);
} else {
printf("don't know any info about '%s'; try help\n", av[1]);
}
} else if (!strpcmp(av[0], "display") || !strpcmp(av[0], "undisplay")) {
if (ac == 1) {
printf("expected an argument, one of: stack, registers, or disassembly\n");
} else if (!strpcmp(av[1], "registers")) {
m->opt_dumpreg = (av[0][0] == 'd');
} else if (!strpcmp(av[1], "stack")) {
if (av[0][0] == 'd') {
int size = 64;
if (ac > 2) size = atoi(av[2]);
if (size <= 0) printf("expected a positive integer, but got '%s' instead\n", av[2]);
else m->opt_printstack = size;
} else {
m->opt_printstack = -1;
}
} else {
printf("don't know how to display '%s'; try help\n", av[1]);
}
} else if (!strpcmp(av[0], "?") || !strpcmp(av[0], "help")) {
printf(
"quit -- quit the debugger\n"
"continue -- continue running the program until exit or breakpoint\n"
"step -- execute one step of the program\n"
"step <n> -- execute n steps of the program\n"
"reset -- reset the simulation state and restart execution of the program from the beginning\n"
"break <addr> -- set a breakpoint to occur after execution reaches <addr>\n"
"delete <i> -- delete the breakpoint <i>\n"
"info <thing> -- get info about <thing>, which can be 'breakpoints', 'stack', or 'registers'\n"
"display <thing> -- periodically display <thing>, which can be 'stack', or 'registers'\n"
" 'stack' takes an optional argument of how many words to display;\n"
"undisplay <thing> -- don't periodically display <thing> any more\n"
"[enter] -- repeat the last command\n"
"\n"
"Note: All commands can be abbreviated by their first letter or any prefix.\n"
"For example, 'i b' stands for 'info breakpoints,'\n"
"and 's 10' stands for 'step 10'.\n"
);
} else {
printf("unrecognized command '%s'\n", av[0]);
}
if (m->opt_graphical) {
update_gui(m);
}
}
}