forked from RKX1209/nsemu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GdbStub.cpp
538 lines (491 loc) · 15.6 KB
/
GdbStub.cpp
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
/* nsemu - LGPL - Copyright 2018 rkx1209<[email protected]> */
/* TODO: Move host dependent functions to host_util.hpp */
#include "Nsemu.hpp"
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
namespace GdbStub {
static int server_fd, client_fd;
uint8_t cmd_buf[GDB_BUFFER_SIZE], last_packet[GDB_BUFFER_SIZE + 4];
static int buf_index, line_sum, checksum, last_packet_len;
static RSState state = RS_IDLE;
volatile bool enabled = false;
volatile bool step = false;
volatile bool cont = false;
std::vector<Breakpoint> bp_list;
std::vector<Watchpoint> wp_list;
Breakpoint::Breakpoint(uint64_t a, unsigned int l, int t) : addr(a), len(l), type(t) {
oldop = ARMv8::ReadInst (a);
}
Watchpoint::Watchpoint(uint64_t a, unsigned int l, int t) : addr(a), len(l), type(t) {
}
static inline bool IsXdigit(char ch) {
return ('a' <= ch && ch <= 'f') || ('A' <= ch && ch <= 'F') || ('0' <= ch && ch <= '9');
}
static inline int FromHex(int v)
{
if (v >= '0' && v <= '9')
return v - '0';
else if (v >= 'A' && v <= 'F')
return v - 'A' + 10;
else if (v >= 'a' && v <= 'f')
return v - 'a' + 10;
else
return 0;
}
static inline int ToHex(int v)
{
if (v < 10)
return v + '0';
else
return v - 10 + 'a';
}
static void MemToHex(char *buf, const uint8_t *mem, int len)
{
char *q;
q = buf;
for(int i = 0; i < len; i++) {
int c = mem[i];
*q++ = ToHex(c >> 4);
*q++ = ToHex(c & 0xf);
}
*q = '\0';
}
void Init() {
struct sockaddr_in addr;
server_fd = socket (AF_INET, SOCK_STREAM, 0);
if (server_fd < 0) {
ns_abort ("socket failed\n");
}
addr.sin_family = AF_INET;
addr.sin_port = htons(1234);
addr.sin_addr.s_addr = INADDR_ANY;
if (::bind(server_fd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
ns_abort ("bind failed");
}
if (listen(server_fd, 5) != 0) {
ns_abort ("listen failed");
}
client_fd = accept(server_fd, nullptr, nullptr);
if (client_fd < 0) {
ns_abort ("Failed to accept gdb client\n");
}
enabled = true;
}
uint8_t ReadByte() {
uint8_t byte;
auto size = recv (client_fd, &byte, 1, MSG_WAITALL);
if (size != 1) {
ns_abort ("Failed recv :%ld", size);
}
return byte;
}
void WriteBytes(uint8_t *buf, size_t len) {
if (send (client_fd, buf, len, 0) == -1) {
ns_abort ("Failed send\n");
}
}
void SendPacket(uint8_t *buf, size_t len) {
int csum;
uint8_t *p;
for (;;) {
p = last_packet;
*(p++) = '$';
memcpy(p, buf, len);
p += len;
csum = 0;
for(int i = 0; i < len; i++) {
csum += buf[i];
}
*(p++) = '#';
*(p++) = ToHex((csum >> 4) & 0xf);
*(p++) = ToHex((csum) & 0xf);
last_packet_len = p - last_packet;
WriteBytes (last_packet, last_packet_len);
break;
}
}
void WritePacket(std::string buf) {
SendPacket ((uint8_t *)buf.c_str(), buf.size());
}
static int IsQueryPacket (const char *p, const char *query, char separator)
{
unsigned int query_len = strlen(query);
return strncmp(p, query, query_len) == 0 && (p[query_len] == '\0' || p[query_len] == separator);
}
static int TargetMemoryRW(uint64_t addr, uint8_t *buf, int len, bool is_write) {
if ((int64_t)addr < 0) {
return -1; //FIXME: Correct validation is required
}
if (is_write) {
ARMv8::GdbWriteBytes (addr, buf, len);
} else {
ARMv8::GdbReadBytes (addr, buf, len);
}
return 0;
}
static int ReadRegister (uint8_t *buf, int reg) {
if (reg < GPR_ZERO) {
*(uint64_t*)buf = X(reg);
} else {
switch(reg) {
case 31:
*(uint64_t *)buf = X(GPR_SP);
break;
case 32:
*(uint64_t *)buf = X(PC_IDX);
break;
default:
*(uint64_t *)buf = 0xdeadbeef;
break;
}
}
return sizeof(uint64_t);
}
static void SendSignal(uint32_t signal) {
char buf[GDB_BUFFER_SIZE];
snprintf(buf, sizeof(buf), "T%02xthread:%02x;", signal, 1);
WritePacket(buf);
}
void Trap() {
cont = false;
SendSignal(GDB_SIGNAL_TRAP);
}
static void Stepi() {
step = true;
}
static int SwBreakpointInsert(unsigned long addr, unsigned long len, int type) {
Breakpoint bp(addr, len, type);
bp_list.push_back(bp);
ns_print("[Add bp] 0x%lx, %u, %d, (oldop: 0x%08lx)\n", addr, len, type, bp.oldop);
ARMv8::WriteU32(addr, BRK_0x0_INST);
return 0;
}
static int SwBreakpointRemove(unsigned long addr, unsigned long len, int type) {
if (bp_list.empty()) {
return -1;
}
auto bp_it = std::find(bp_list.begin(), bp_list.end(), Breakpoint(addr, len, type));
if (bp_it == bp_list.end()) {
ns_print ("Breakpoint not found\n");
return -1;
}
ns_print("[Remove bp] 0x%lx, %u, %d, (oldop: 0x%08lx)\n", addr, len, type, (*bp_it).oldop);
ARMv8::WriteU32((*bp_it).addr, (*bp_it).oldop); // Restore an original operation
bp_list.erase(bp_it);
return 0;
}
static int WatchpointInsert(unsigned long addr, unsigned long len, int type) {
Watchpoint wp(addr, len, type);
wp_list.push_back(wp);
ns_print("[Add wp] 0x%lx, %u, %d\n", addr, len, type);
return 0;
}
static int WatchpointRemove(unsigned long addr, unsigned long len, int type) {
if (wp_list.empty()) {
return -1;
}
auto wp_it = std::find(wp_list.begin(), wp_list.end(), Watchpoint(addr, len, type));
if (wp_it == wp_list.end()) {
ns_print ("Watchpoint not found\n");
return -1;
}
ns_print("[Remove wp] 0x%lx, %u, %d\n", addr, len, type);
wp_list.erase(wp_it);
return 0;
}
static void HitWatchpoint(unsigned long addr, int type) {
cont = false;
step = false;
char buf[GDB_BUFFER_SIZE];
const char *type_s;
if (type == GDB_WATCHPOINT_READ) {
type_s = "r";
} else if (type == GDB_WATCHPOINT_ACCESS) {
type_s = "a";
} else {
type_s = "";
}
snprintf(buf, sizeof(buf), "T%02xthread:%02x;%swatch:%lx;", GDB_SIGNAL_TRAP, 1, type_s, addr);
WritePacket(buf);
}
void NotifyMemAccess(unsigned long addr, size_t len, bool read) {
int type = read ? GDB_WATCHPOINT_READ : GDB_WATCHPOINT_WRITE;
for (int i = 0; i < wp_list.size(); i++) {
Watchpoint wp = wp_list[i];
if (addr <= wp.addr && wp.addr + wp.len <= addr + len) {
if (wp.type == GDB_WATCHPOINT_ACCESS || wp.type == type) {
HitWatchpoint (addr, wp.type);
return;
}
}
}
}
static int BreakpointInsert(unsigned long addr, unsigned long len, int type) {
int err = -1;
switch (type) {
case GDB_BREAKPOINT_SW:
case GDB_BREAKPOINT_HW:
return SwBreakpointInsert (addr, len, type);
case GDB_WATCHPOINT_WRITE:
case GDB_WATCHPOINT_READ:
case GDB_WATCHPOINT_ACCESS:
return WatchpointInsert (addr, len, type);
default:
break;
}
return err;
}
static int BreakpointRemove(unsigned long addr, unsigned long len, int type) {
int err = -1;
switch (type) {
case GDB_BREAKPOINT_SW:
case GDB_BREAKPOINT_HW:
return SwBreakpointRemove (addr, len, type);
case GDB_WATCHPOINT_WRITE:
case GDB_WATCHPOINT_READ:
case GDB_WATCHPOINT_ACCESS:
return WatchpointRemove (addr, len, type);
break;
default:
break;
}
return err;
}
static void Continue() {
cont = true;
}
static RSState HandleCommand(char *line_buf) {
const char *p;
uint32_t thread;
int ch, reg_size, type, res;
char buf[GDB_BUFFER_SIZE];
uint8_t mem_buf[GDB_BUFFER_SIZE];
unsigned long addr, len;
p = line_buf;
ch = *p++;
switch(ch) {
case '?':
/* XXX: Is it correct to fix thread id to '1'? */
SendSignal(GDB_SIGNAL_TRAP);
break;
case 'c':
if (*p != '\0') {
addr = strtol(p, (char **)&p, 16);
}
Continue ();
break;
case 'g':
len = 0;
for (addr = 0; addr < 33; addr++) {
reg_size = ReadRegister (mem_buf + len, addr);
len += reg_size;
}
MemToHex(buf, mem_buf, len);
WritePacket(buf);
break;
case 'm':
addr = strtoul(p, (char **)&p, 16);
if (*p == ',')
p++;
len = strtol(p, NULL, 16);
/* memtohex() doubles the required space */
if (len > GDB_BUFFER_SIZE / 2) {
WritePacket ("E22");
break;
}
if (TargetMemoryRW (addr, mem_buf, len, false) != 0) {
WritePacket ("E14");
} else {
MemToHex(buf, mem_buf, len);
WritePacket(buf);
}
break;
case 'p':
addr = strtol(p, (char **)&p, 16);
reg_size = ReadRegister (mem_buf, addr);
if (reg_size) {
MemToHex(buf, mem_buf, reg_size);
WritePacket(buf);
} else {
WritePacket("E14");
}
break;
case 'q':
case 'Q':
if (IsQueryPacket(p, "Supported", ':')) {
snprintf(buf, sizeof(buf), "PacketSize=%x", GDB_BUFFER_SIZE);
WritePacket(buf);
break;
} else if (strcmp(p, "C") == 0) {
WritePacket("QC1");
break;
}
goto unknown_command;
case 'z':
case 'Z':
type = strtol(p, (char **)&p, 16);
if (*p == ',')
p++;
addr = strtol(p, (char **)&p, 16);
if (*p == ',')
p++;
len = strtol(p, (char **)&p, 16);
if (ch == 'Z') {
res = BreakpointInsert(addr, len, type);
} else {
res = BreakpointRemove(addr, len, type);
}
if (res >= 0)
WritePacket("OK");
else
WritePacket("E22");
break;
case 's':
Stepi();
break;
case 'H':
type = *p++;
thread = strtol(p, (char **)&p, 16);
if (thread == -1 || thread == 0) {
WritePacket("OK");
break;
}
switch (type) {
case 'c':
WritePacket("OK");
break;
case 'g':
WritePacket("OK");
break;
default:
WritePacket("E22");
break;
}
break;
case 'T':
thread = strtol(p, (char **)&p, 16);
WritePacket("OK");
break;
default:
unknown_command:
/* put empty packet */
buf[0] = '\0';
WritePacket(buf);
break;
}
return RS_IDLE;
}
void HandlePacket() {
uint8_t ch = ReadByte();
uint8_t reply;
switch (state) {
case RS_IDLE:
if (ch == '$') {
/* start of command packet */
buf_index = 0;
line_sum = 0;
state = RS_GETLINE;
} else {
//printf("%s: received garbage between packets: 0x%x\n", __func__, ch);
}
break;
case RS_GETLINE:
if (ch == '}') {
state = RS_GETLINE_ESC;
line_sum += ch;
} else if (ch == '*') {
/* start run length encoding sequence */
state = RS_GETLINE_RLE;
line_sum += ch;
} else if (ch == '#') {
/* end of command, start of checksum*/
state = RS_CHKSUM1;
} else if (buf_index >= sizeof(cmd_buf) - 1) {
state = RS_IDLE;
ns_print ("Gdb: command buffer overrun, dropping command\n");
} else {
/* unescaped command character */
cmd_buf[buf_index++] = ch;
line_sum += ch;
}
break;
case RS_GETLINE_ESC:
if (ch == '#') {
/* unexpected end of command in escape sequence */
state = RS_CHKSUM1;
} else if (buf_index >= sizeof(cmd_buf) - 1) {
/* command buffer overrun */
state = RS_IDLE;
ns_print ("Gdb: command buffer overrun, dropping command\n");
} else {
/* parse escaped character and leave escape state */
cmd_buf[buf_index++] = ch ^ 0x20;
line_sum += ch;
state = RS_GETLINE;
}
break;
case RS_GETLINE_RLE:
if (ch < ' ') {
/* invalid RLE count encoding */
state = RS_GETLINE;
ns_print ("Gdb: got invalid RLE count: 0x%x\n", ch);
} else {
/* decode repeat length */
int repeat = (unsigned char)ch - ' ' + 3;
if (buf_index + repeat >= sizeof(cmd_buf) - 1) {
/* that many repeats would overrun the command buffer */
ns_print ("Gdb: command buffer overrun, dropping command\n");
state = RS_IDLE;
} else if (buf_index < 1) {
/* got a repeat but we have nothing to repeat */
ns_print ("Gdb: got invalid RLE sequence\n");
state = RS_GETLINE;
} else {
/* repeat the last character */
memset(cmd_buf + buf_index,
cmd_buf[buf_index - 1], repeat);
buf_index += repeat;
line_sum += ch;
state = RS_GETLINE;
}
}
break;
case RS_CHKSUM1:
/* get high hex digit of checksum */
if (!IsXdigit(ch)) {
ns_print("Gdb: got invalid command checksum digit\n");
state = RS_GETLINE;
break;
}
cmd_buf[buf_index] = '\0';
checksum = FromHex(ch) << 4;
state = RS_CHKSUM2;
break;
case RS_CHKSUM2:
/* get low hex digit of checksum */
if (!IsXdigit(ch)) {
ns_print("Gdb: got invalid command checksum digit\n");
state = RS_GETLINE;
break;
}
checksum |= FromHex(ch);
if (checksum != (line_sum & 0xff)) {
ns_print("Gdb: got command packet with incorrect checksum\n");
/* send NAK reply */
reply = '-';
WriteBytes (&reply, 1);
state = RS_IDLE;
} else {
/* send ACK reply */
reply = '+';
WriteBytes (&reply, 1);
state = HandleCommand ((char *)cmd_buf);
}
break;
default:
ns_print("Gdb: got unknown status\n");
break;
}
}
};