-
Notifications
You must be signed in to change notification settings - Fork 12
/
gdbstub.c
544 lines (423 loc) · 9.43 KB
/
gdbstub.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
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
#include <sys/types.h>
#include <sys/socket.h>
#include <err.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <poll.h>
#include <sysexits.h>
#include <unistd.h>
#include "emu.h"
#define PKT_BUF_MAX 0x2000
#define PKT_BUF_STR "2000"
// Only one client allowed to debug at a time...
int lsock = -1;
int csock = -1;
bool stepone; // Single-step?
bool execute; // Continue emu
GHashTable *breakpoints; // addr -> NULL
static void gdb_cmd(char *c, char *pound);
#define CMD_HANDLER(name) \
static void gdb_##name(const char *cmd, const void *extra)
CMD_HANDLER(getregs);
CMD_HANDLER(setregs);
CMD_HANDLER(readmem);
CMD_HANDLER(writemem);
CMD_HANDLER(addbreak);
CMD_HANDLER(rembreak);
CMD_HANDLER(conststring);
CMD_HANDLER(step);
CMD_HANDLER(bstep);
struct cmd_dispatch {
const char *cmd,
*extra;
void (*handler)(const char *cmd, const void *extra);
bool continue_exec;
};
static struct cmd_dispatch gdb_disp[] = {
{ "g" /* fetch reGisters */, NULL, gdb_getregs, false, },
{ "G" /* set reGisters */, NULL, gdb_setregs, false, },
{ "m" /* read Memory */, NULL, gdb_readmem, false, },
{ "M" /* write Memory */, NULL, gdb_writemem, false, },
{ "Z0" /* break */, NULL, gdb_addbreak, false, },
{ "z0" /* unbreak */, NULL, gdb_rembreak, false, },
{ "qAttached" /* initial attach */, "1", gdb_conststring, false, },
{ "qSupported" /* feature enquiry */,
"qRelocInsn-"
#if 0
";ConditionalBreakpoints+"
";StaticTracepoint+"
";ReverseContinue+"
#endif
";ReverseStep+"
";PacketSize=" PKT_BUF_STR, gdb_conststring, false, },
{ "?" /* wat */, "S05", gdb_conststring, false, },
{ "Hg" /* ??? */, "OK", gdb_conststring, false, },
{ "Hc" /* ??? */, "OK", gdb_conststring, false, },
{ "c" /* Continue */, NULL, NULL, true, },
{ "s" /* Step */, NULL, gdb_step, true, },
{ "bs" /* Backwards step */, NULL, gdb_bstep, true, },
{ 0 },
};
static inline bool
streq(const char *s1, const char *s2)
{
return strcmp(s1, s2) == 0;
}
static inline bool
startswith(const char *haystack, const char *prefix)
{
return strncmp(haystack, prefix, strlen(prefix)) == 0;
}
static uint8_t
gdb_cksum(void *v, size_t len)
{
uint8_t sum = 0, *s = v;
for (; len && *s; s++, len--)
sum += *s;
return sum;
}
static uint8_t
gdb_cksumstr(void *s)
{
return gdb_cksum(s, SIZE_MAX);
}
static void
gdb_sendraw(char *p, size_t len)
{
ssize_t wr;
ASSERT(csock != -1, "x");
while (len) {
wr = send(csock, p, len, 0);
ASSERT(wr > 0, "send");
len -= (unsigned)wr;
p += (unsigned)wr;
}
}
static inline void
gdb_sendrawstr(char *s)
{
gdb_sendraw(s, strlen(s));
}
static void
gdb_sendstr(const char *s)
{
unsigned chk, len;
char buf[4096], *p;
ASSERT(csock != -1, "csock");
chk = gdb_cksumstr((void*)s);
len = snprintf(buf, sizeof buf, "$%s#%02x", s, chk);
gdb_sendraw(buf, len);
}
#define GDBSTUB_PORT 3713
static int
gdbstub_accept(void)
{
int rc, flag, client;
client = accept(lsock, NULL, NULL);
if (client == -1)
err(EX_OSERR, "accept");
flag = 1;
rc = setsockopt(client, IPPROTO_TCP, TCP_NODELAY, (void*)&flag,
sizeof flag);
if (rc == -1)
err(EX_OSERR, "setsockopt");
rc = setsockopt(client, SOL_SOCKET, SO_KEEPALIVE, (void*)&flag,
sizeof flag);
if (rc == -1)
err(EX_OSERR, "setsockopt");
printf("GDB client connected.\n");
return client;
}
void
gdbstub_init(void)
{
int rc, optval;
struct sockaddr_in any = { 0 };
if (csock != -1)
close(csock);
csock = -1;
if (lsock != -1)
close(lsock);
lsock = -1;
lsock = socket(AF_INET, SOCK_STREAM, 0);
if (lsock == -1)
err(EX_OSERR, "socket");
optval = 1;
rc = setsockopt(lsock, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof
optval);
if (rc == -1)
err(EX_OSERR, "setsockopt(REUSEADDR)");
any.sin_family = PF_INET;
any.sin_port = htons(GDBSTUB_PORT);
any.sin_addr.s_addr = htonl(INADDR_ANY);
rc = bind(lsock, (void*)&any, sizeof any);
if (rc == -1)
err(EX_OSERR, "bind");
rc = listen(lsock, 20);
if (rc == -1)
err(EX_OSERR, "listen");
printf("GDB stub listening on [*]:%u\n", (uns)GDBSTUB_PORT);
printf("Waiting for client to connect...\n");
csock = gdbstub_accept();
ASSERT(csock != -1, "gdbstub_accept");
breakpoints = g_hash_table_new(NULL, NULL);
// Go to interactive before executing first instruction.
stepone = true;
}
// Called when emulator is stopped, awaiting remote commands. (Including
// initial state.)
char clientbuf[PKT_BUF_MAX];
size_t cblen;
void
gdbstub_interactive(void)
{
bool processed_any = true;
char *bp, *ep;
ssize_t rd;
int rc;
execute = false;
ASSERT(csock != -1, "x");
do {
if (!processed_any) {
struct pollfd pfd = { 0 };
pfd.fd = csock;
pfd.events = POLLIN;
rc = poll(&pfd, 1, -1);
ASSERT(rc >= 0, "poll: %s", strerror(errno));
}
rd = recv(csock, &clientbuf[cblen], sizeof clientbuf - cblen,
MSG_DONTWAIT);
if (rd == 0) {
printf("Client dropped, exiting.\n");
exit(0);
}
if (rd < 0) {
if (errno != EAGAIN && errno != EWOULDBLOCK)
err(1, "recv");
goto process;
}
cblen += (size_t)rd;
process:
processed_any = false;
bp = clientbuf;
ep = &clientbuf[cblen];
// We have cblen valid bytes of protocol at p.
// Process as many full packets as possible...
while (bp < ep) {
char *pound;
if (*bp == '+' || *bp == '-') {
bp++;
cblen--;
processed_any = true;
continue;
}
pound = memchr(bp, '#', ep - bp);
if (pound && (pound + 2) < ep) {
gdb_cmd(bp, pound);
processed_any = true;
bp = pound + 3;
if (execute)
break;
}
}
if (bp != clientbuf) {
cblen = ep - bp;
memmove(clientbuf, bp, cblen);
}
} while (!execute);
ASSERT(execute,
"we shouldn't leave GDB interactive until we are told to");
}
static void
gdb_cmd(char *c, char *pound)
{
unsigned ckcalc, cksend;
bool dispatched;
int rc;
ASSERT(*c != '+' && *c != '-', "should have been slurped already");
ckcalc = gdb_cksum(c+1, pound - c - 1);
rc = sscanf(pound, "#%2x", &cksend);
ASSERT(rc == 1, "proto");
if (ckcalc != cksend) {
gdb_sendrawstr("-"); // NAK
return;
}
gdb_sendrawstr("+"); // ACK
*pound = 0;
if (*c != '$') {
printf("XXX Got weird cmd: '%s'\n", c);
gdb_sendstr("");
return;
}
c++;
dispatched = false;
for (struct cmd_dispatch *d = gdb_disp; d->cmd; d++) {
if (startswith(c, d->cmd)) {
execute = d->continue_exec;
if (d->handler)
d->handler(c, d->extra);
dispatched = true;
break;
}
}
if (!dispatched) {
printf("XXX Got unhandled $cmd: '%s'\n", c);
gdb_sendstr("");
return;
}
}
// Called once per instruction
void
gdbstub_intr(void)
{
int rc;
bool interact = false;
if (csock == -1)
return;
if (stepone) {
interact = true;
stepone = false;
} else if (g_hash_table_contains(breakpoints, ptr(registers[PC]))) {
printf("Breakpoint @%04x\n", (uns)registers[PC]);
interact = true;
gdbstub_breakpoint();
}
// XXX IF we broke, or single-stepped after previous request, then we
// need to return control to GDB.
if (interact)
gdbstub_interactive();
// Anything else we need to watch on a per-instruction basis?
}
void
gdbstub_breakpoint(void)
{
ASSERT(csock != -1, "x");
gdb_sendstr("S05" /* trap */);
}
// Called when the program halts.
void
gdbstub_stopped(void)
{
if (csock == -1)
return;
close(csock);
csock = -1;
}
// char* cmd, void* extra
CMD_HANDLER(getregs)
{
char buf[16*4+1];
int rc;
(void)cmd;
(void)extra;
for (unsigned i = 0; i < 16; i++) {
rc = sprintf(&buf[i*4], "%02x%02x", registers[i] & 0xff,
registers[i] >> 8);
ASSERT(rc == 4, "x");
}
gdb_sendstr(buf);
}
CMD_HANDLER(setregs)
{
unsigned reglo, reghi, i;
int rc;
(void)extra;
cmd++;
for (i = 0; i < 16; i++) {
ASSERT(*cmd, "x");
rc = sscanf(cmd, "%02x%02x", ®lo, ®hi);
ASSERT(rc == 2, "x");
registers[i] = reglo | (reghi << 8);
cmd += 4;
}
gdb_sendstr("OK");
}
// Read/write memory as a stream of bytes
CMD_HANDLER(readmem)
{
unsigned start, rlen, slen, i;
char buffer[4096+1] = { 0 };
int rc;
(void)extra;
cmd++;
rc = sscanf(cmd, "%x,%x", &start, &rlen);
ASSERT(rc == 2, "x");
ASSERT(rlen*2 < sizeof buffer, "buffer overrun");
slen = 0;
for (i = 0; i < rlen; i++) {
rc = sprintf(&buffer[slen], "%02x", membyte(start + i));
ASSERT(rc == 2, "x");
slen += (unsigned)rc;
}
gdb_sendstr(buffer);
}
CMD_HANDLER(writemem)
{
unsigned start, len, byte;
const char *hexs;
int rc, hex;
(void)extra;
cmd++;
hex = 0;
rc = sscanf(cmd, "%x,%x:%n", &start, &len, &hex);
ASSERT(rc == 2 || rc == 3, "x");
if (hex == 0) {
printf("ill-formed write: %s\n", cmd);
exit(1);
}
hexs = cmd + hex;
for (unsigned i = 0; i < len; i++) {
rc = sscanf(hexs + 2*i, "%02x", &byte);
ASSERT(rc == 1, "x");
memory[(start + i) & 0xffff] = byte;
}
gdb_sendstr("OK");
}
CMD_HANDLER(addbreak)
{
int rc;
unsigned addr;
(void)extra;
cmd += strlen("Z0");
rc = sscanf(cmd, ",%x", &addr);
ASSERT(rc == 1, "x");
g_hash_table_insert(breakpoints, ptr(addr), NULL);
gdb_sendstr("OK");
}
CMD_HANDLER(rembreak)
{
int rc;
unsigned addr;
(void)extra;
cmd += strlen("z0");
rc = sscanf(cmd, ",%x", &addr);
ASSERT(rc == 1, "x");
g_hash_table_remove(breakpoints, ptr(addr));
gdb_sendstr("OK");
}
CMD_HANDLER(conststring)
{
(void)cmd;
gdb_sendstr(extra);
}
CMD_HANDLER(step)
{
(void)cmd;
(void)extra;
stepone = true;
gdb_sendstr("S05");
}
CMD_HANDLER(bstep)
{
(void)cmd;
(void)extra;
if (insns > 0) {
replay_mode = true;
insnreplaylim = insns - 1;
gdb_sendstr("S05");
} else {
// can't go backwards from 0!
gdb_sendstr("T05replaylog:begin;");
execute = false;
}
}