-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdb.c
839 lines (658 loc) · 19.7 KB
/
gdb.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/*
Copyright (c) 2001 by William A. Gatliff
All rights reserved. [email protected]
See the file COPYING for details.
This file is provided "as-is", and without any express
or implied warranties, including, without limitation,
the implied warranties of merchantability and fitness
for a particular purpose.
The author welcomes feedback regarding this file.
*/
/* $Id: gdb.c,v 1.6 2003/03/02 04:21:13 bgat Exp $ */
/* The gdb remote communication protocol.
A debug packet whose contents are <data>
is encapsulated for transmission in the form:
$ <data> # CSUM1 CSUM2
<data> must be ASCII alphanumeric and cannot include characters
'$' or '#'. If <data> starts with two characters followed by
':', then the existing stubs interpret this as a sequence number.
CSUM1 and CSUM2 are ascii hex representation of an 8-bit
checksum of <data>, the most significant nibble is sent first.
the hex digits 0-9,a-f are used.
Receiver responds with:
+ - if CSUM is correct and ready for next packet
- - if CSUM is incorrect
<data> is as follows:
All values are encoded in ascii hex digits.
Request Packet
read registers g
reply XX....X Each byte of register data
is described by two hex digits.
Registers are in the internal order
for GDB, and the bytes in a register
are in the same order the machine uses.
or ENN for an error.
write regs GXX..XX Each byte of register data
is described by two hex digits.
reply OK for success
ENN for an error
write reg Pn...=r... Write register n... with value r...,
which contains two hex digits for each
byte in the register (target byte
order).
reply OK for success
ENN for an error
(not supported by all stubs).
read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
reply XX..XX XX..XX is mem contents
Can be fewer bytes than requested
if able to read only part of the data.
or ENN NN is errno
write mem MAA..AA,LLLL:XX..XX
AA..AA is address,
LLLL is number of bytes,
XX..XX is data
reply OK for success
ENN for an error (this includes the case
where only part of the data was
written).
write mem XAA..AA,LLLL:XX..XX
(binary) AA..AA is address,
LLLL is number of bytes,
XX..XX is binary data
reply OK for success
ENN for an error
cont cAA..AA AA..AA is address to resume
If AA..AA is omitted,
resume at same address.
step sAA..AA AA..AA is address to resume
If AA..AA is omitted,
resume at same address.
last signal ? Reply the current reason for stopping.
This is the same reply as is generated
for step or cont : SAA where AA is the
signal number.
There is no immediate reply to step or cont.
The reply comes when the machine stops.
It is SAA AA is the "signal number"
or... TAAn...:r...;n:r...;n...:r...;
AA = signal number
n... = register number
r... = register contents
or... WAA The process exited, and AA is
the exit status. This is only
applicable for certains sorts of
targets.
kill request k
toggle debug d toggle debug flag (see 386 & 68k stubs)
reset r reset -- see sparc stub.
reserved <other> On other requests, the stub should
ignore the request and send an empty
response ($#<checksum>). This way
we can extend the protocol and GDB
can tell whether the stub it is
talking to uses the old or the new.
search tAA:PP,MM Search backwards starting at address
AA for a match with pattern PP and
mask MM. PP and MM are 4 bytes.
Not supported by all stubs.
general query qXXXX Request info about XXXX.
general set QXXXX=yyyy Set value of XXXX to yyyy.
query sect offs qOffsets Get section offsets. Reply is
Text=xxx;Data=yyy;Bss=zzz
console output Otext Send text to stdout. Only comes from
remote target.
Responses can be run-length encoded to save space. A '*' means that
the next character is an ASCII encoding giving a repeat count which
stands for that many repititions of the character preceding the '*'.
The encoding is n+29, yielding a printable character where n >=3
(which is where rle starts to win). Don't use an n > 126.
So
"0* " means the same as "0000".
*/
#include "gdb.h"
#if !defined(GDB_RXBUFLEN)
#define GDB_RXBUFLEN 200
#endif
#define min(a,b) ((a) > (b) ? (b) : (a))
#define max(a,b) ((a) > (b) ? (a) : (b))
#define is_aligned_long(addr,len) \
(((len) >= sizeof(long)) && ((long)(addr) % sizeof(long) == 0))
#define is_aligned_short(addr,len) \
(((len) >= sizeof(short)) && ((long)(addr) % sizeof(short) == 0))
/* converts '[0-9,a-f,A-F]' to its integer equivalent */
static int hex_to_int (char h)
{
if (h >= 'a' && h <= 'f') return h - 'a' + 10;
if (h >= '0' && h <= '9') return h - '0';
if (h >= 'A' && h <= 'F') return h - 'A' + 10;
return 0;
}
/* converts the low nibble of i to its hex character equivalent */
static char lnibble_to_hex (char i)
{
static const char lnibble_to_hex_table[] = "0123456789abcdef";
return lnibble_to_hex_table[i & 0xf];
}
/* translates a delimited hex string to a long */
static const char* hargs_parse_long (const char* hargs, long* l, int delim)
{
*l = 0;
while (*hargs != delim) *l = (*l << 4) + hex_to_int(*hargs++)
;
return hargs + 1;
}
/*
TODO: the lcbuf unions assume and depend that lbuf and sbuf start at
the same address. Is this always correct? Is there a better way?
*/
/* Converts a memory region of length len bytes, starting at mem, into
a string of hex bytes. Returns the number of bytes placed into
hexbuf.
This function carefully preserves the endianness of the data,
because that's what gdb expects. This function also optimizes the
read process into the largest units possible, in case we're reading
a peripheral register that can't deal with unaligned or byte-wide
accesses. */
static int mem_to_hexbuf (const void* mem, char* hbuf, int len)
{
int i = 0;
union
{
long lbuf;
short sbuf;
char cbuf[sizeof(long)];
} lcbuf;
int cbuflen;
int retval = 0;
while (len > 0)
{
if (is_aligned_long(mem, len))
{
cbuflen = sizeof (long);
lcbuf.lbuf = *(long*)mem;
((long*)mem)++;
len -= sizeof (long);
}
else if (is_aligned_short(mem, len))
{
cbuflen = sizeof (short);
lcbuf.sbuf = *(short*)mem;
((short*)mem)++;
len -= sizeof (short);
}
else
{
cbuflen = sizeof (char);
lcbuf.cbuf[0] = *(char*)mem;
((char*)mem)++;
len -= sizeof (char);
}
for (i = 0; i < cbuflen; i++ )
{
*hbuf++ = lnibble_to_hex(lcbuf.cbuf[i] >> 4);
*hbuf++ = lnibble_to_hex(lcbuf.cbuf[i]);
retval += 2;
}
}
return retval;
}
/*
Reads (len * 2) hex digits from hbuf, converts them to binary,
writes them to mem. Returns a pointer to the first empty byte after
the region written.
Carefully preserves endianness, optimizes write accesses so as to be
hardware-friendly.
*/
static char* hexbuf_to_mem (const char* hbuf, void* mem, int len)
{
int i = 0;
union {
long lbuf;
short sbuf;
char cbuf[sizeof(long)];
} lcbuf;
void* cache_start = mem;
int cache_len = len;
while (len > 0)
{
if (is_aligned_long(mem, len))
{
for( i = 0; i < sizeof(long); i++ )
{
lcbuf.cbuf[i] = (hex_to_int(*hbuf++) << 4);
lcbuf.cbuf[i] += hex_to_int(*hbuf++);
}
*((long*)mem)++ = lcbuf.lbuf;
len -= sizeof(long);
}
else if (is_aligned_short(mem, len))
{
for( i = 0; i < sizeof(short); i++ )
{
lcbuf.cbuf[i] = (hex_to_int(*hbuf++) << 4);
lcbuf.cbuf[i] += hex_to_int(*hbuf++);
}
*((short*)mem)++ = lcbuf.sbuf;
len -= sizeof(short);
}
else
{
lcbuf.cbuf[0] = (hex_to_int(*hbuf++) << 4);
lcbuf.cbuf[0] += hex_to_int(*hbuf++);
*((char*)mem)++ = lcbuf.cbuf[0];
len -= sizeof(char);
}
}
gdb_flush_cache(cache_start, cache_len);
return mem;
}
static const void* xbin_to_bin( const void* xbin, char* bin)
{
if (*(char*)xbin == 0x7d)
{
((char*)xbin)++;
*bin = *((char*)xbin) ^ 0x20;
((char*)xbin)++;
}
else
*bin = *((char*)xbin)++;
return xbin;
}
/*
Converts the escaped-binary ('X' packet) array pointed to by buf
into binary, to be placed in mem. Returns a pointer to the first
empty byte after the region written.
*/
static char* xmem_to_mem (const char* xmem, void* mem, int len)
{
int i = 0;
union
{
long lbuf;
short sbuf;
char cbuf[sizeof(long)];
} lcbuf;
void* cache_start = mem;
int cache_len = len;
while (len > 0) {
if (is_aligned_long(mem, len))
{
for (i = 0; i < sizeof(long); i++)
xmem = xbin_to_bin(xmem, &lcbuf.cbuf[i]);
*((long*)mem)++ = lcbuf.lbuf;
len -= sizeof (long);
}
else if (is_aligned_short(mem, len))
{
for( i = 0; i < sizeof(short); i++ )
xmem = xbin_to_bin(xmem, &lcbuf.cbuf[i]);
*((short*)mem)++ = lcbuf.sbuf;
len -= sizeof (short);
}
else
{
xmem = xbin_to_bin(xmem, &lcbuf.cbuf[0]);
*((char*)mem)++ = lcbuf.cbuf[0];
len -= sizeof (char);
}
}
gdb_flush_cache(cache_start, cache_len);
return mem;
}
/*
Writes a buffer of length len to gdb_putc().
Returns the checksum of the bytes.
*/
static int putbuf (int len, const char* buf)
{
unsigned char sum = 0;
while (len--)
{
sum += *(unsigned char*)buf;
gdb_putc( *buf++ );
}
return sum;
}
/* Sends an RSP message */
static void putmsg (char c, const char *buf, int len)
{
unsigned char sum;
do
{
/* send the header */
gdb_putc('$');
/* send the message type, if specified */
if (c) gdb_putc(c);
/* send the data */
sum = c + putbuf(len, buf);
/* send the footer */
gdb_putc('#');
gdb_putc(lnibble_to_hex(sum >> 4));
gdb_putc(lnibble_to_hex(sum));
}
while ('+' != gdb_getc());
return;
}
/* Reads a message */
static int getmsg (char *rxbuf)
{
char c;
unsigned char sum;
unsigned char rx_sum;
char *buf;
get_msg:
/* wait around for start character, ignore all others */
while (gdb_getc() != '$');
/* start counting bytes */
buf = rxbuf;
sum = 0;
/* read until we see the '#' at the end of the packet */
do
{
*buf++ = c = gdb_getc();
if (c != '#') sum += c;
/* since the buffer is ascii, may as well terminate it */
*buf = 0;
}
while (c != '#');
/* receive checksum */
rx_sum = hex_to_int(gdb_getc());
rx_sum = (rx_sum << 4) + hex_to_int(gdb_getc());
/* if computed checksum doesn't match received checksum, then reject */
if (sum != rx_sum)
{
gdb_putc('-');
goto get_msg;
}
/* got the message ok */
else gdb_putc('+');
return 1;
}
/*
"last signal" message
"Sxx", where:
xx is the signal number
*/
static void last_signal (int sigval)
{
char tx_buf[2];
tx_buf[0] = lnibble_to_hex(sigval >> 4);
tx_buf[1] = lnibble_to_hex(sigval);
putmsg('S', tx_buf, 2);
return;
}
/*
"expedited response" message
"Txx..........."
*/
static void expedited (int sigval)
{
long val;
int id = 0;
int reglen;
int sum;
do
{
/* send header */
gdb_putc('$');
sum = gdb_putc('T');
/* signal number */
sum += gdb_putc(lnibble_to_hex(sigval >> 4));
sum += gdb_putc(lnibble_to_hex(sigval));
/* register values */
id = 0;
while ((reglen = gdb_peek_register_file(id, &val)) != 0)
{
if (id)
sum += gdb_putc(';');
/* register id */
sum += gdb_putc(lnibble_to_hex(id >> 4));
sum += gdb_putc(lnibble_to_hex(id));
sum += gdb_putc(':');
/* register value */
switch(reglen)
{
case 4:
sum += gdb_putc(lnibble_to_hex(val >> 28));
sum += gdb_putc(lnibble_to_hex(val >> 24));
case 3:
sum += gdb_putc(lnibble_to_hex(val >> 20));
sum += gdb_putc(lnibble_to_hex(val >> 16));
case 2:
sum += gdb_putc(lnibble_to_hex(val >> 12));
sum += gdb_putc(lnibble_to_hex(val >> 8));
case 1:
sum += gdb_putc(lnibble_to_hex(val >> 4));
sum += gdb_putc(lnibble_to_hex(val));
break;
}
/* try the next register */
id++;
}
/* terminate the message */
sum += gdb_putc(';');
/* send the message footer */
gdb_putc('#');
gdb_putc(lnibble_to_hex(sum >> 4));
gdb_putc(lnibble_to_hex(sum));
}
while ('+' != gdb_getc());
return;
}
static void read_memory (const char *hargs)
{
char tx_buf[sizeof(long) * 2];
long addr = 0, orig_addr = 0;
long len = 0, orig_len = 0;
int tx;
unsigned char sum = 0;
/* parse address, length */
hargs = hargs_parse_long(hargs, &addr, ',');
hargs = hargs_parse_long(hargs, &len, '#');
orig_addr = addr;
orig_len = len;
do
{
addr = orig_addr;
len = orig_len;
gdb_putc('$');
/* send the message a piece at a time, so we don't need much memory */
while (len)
{
tx = mem_to_hexbuf((void*)addr, tx_buf, min(len, sizeof(long)));
sum += putbuf(tx, tx_buf);
addr += tx / 2;
len -= min(tx / 2, len);
}
gdb_putc('#');
gdb_putc(lnibble_to_hex(sum >> 4));
gdb_putc(lnibble_to_hex(sum));
} while (gdb_getc() != '+');
return;
}
static void write_memory (const char *hargs)
{
long addr = 0;
long len = 0;
/* parse address, length */
hargs = hargs_parse_long(hargs, &addr, ',');
hargs = hargs_parse_long(hargs, &len, ':' );
/* write all requested bytes */
hexbuf_to_mem(hargs, (void*)addr, len);
putmsg(0, "OK", 2);
return;
}
static void write_xbin_memory (const char *hargs)
{
long addr = 0;
long len = 0;
/* parse address, length */
hargs = hargs_parse_long(hargs, &addr, ',');
hargs = hargs_parse_long(hargs, &len, ':' );
/* write all requested bytes */
xmem_to_mem(hargs, (void*)addr, len);
putmsg(0, "OK", 2);
return;
}
static void write_registers (char *hargs)
{
int id = 0;
long val;
int reglen;
while (*hargs != '#')
{
/* how big is this register? */
reglen = gdb_peek_register_file(id, &val);
if(reglen)
{
/* extract the register's value */
hexbuf_to_mem(hargs, &val, reglen);
hargs += sizeof(long) * 2;
/* stuff it into the register file */
gdb_poke_register_file(id++, val);
}
else break;
}
putmsg(0, "OK", 2);
return;
}
static void read_registers (void)
{
char tx_buf[sizeof(long) * 2];
long val;
int id = 0;
int reglen;
unsigned char sum;
do
{
gdb_putc('$');
sum = 0;
/* send register values */
id = 0;
while((reglen = gdb_peek_register_file(id++, &val)) != 0)
sum += putbuf(mem_to_hexbuf(&val, tx_buf, reglen), tx_buf);
/* send the message footer */
gdb_putc('#');
gdb_putc(lnibble_to_hex(sum >> 4));
gdb_putc(lnibble_to_hex(sum));
}
while ('+' != gdb_getc());
return;
}
static void write_register (char *hargs)
{
long id = 0;
long val = 0;
int reglen;
while (*hargs != '=') id = (id << 4) + hex_to_int(*hargs++)
;
hargs++;
reglen = gdb_peek_register_file(id, &val);
hexbuf_to_mem(hargs, &val, reglen);
gdb_poke_register_file(id, val);
putmsg(0, "OK", 2);
return;
}
void gdb_console_output (int len, const char *buf)
{
char tx_buf[2];
unsigned char sum;
gdb_putc('$');
sum = putbuf(1, "O");
while (len--)
{
tx_buf[0] = lnibble_to_hex(*buf >> 4);
tx_buf[1] = lnibble_to_hex(*buf++);
sum += putbuf(2, tx_buf);
}
/* send the message footer */
gdb_putc('#');
gdb_putc(lnibble_to_hex(sum >> 4));
gdb_putc(lnibble_to_hex(sum));
/* DON'T wait for response; we don't want to get hung
up here and halt the application if gdb has gone away! */
return;
}
/*
The gdb command processor.
*/
void gdb_monitor (int sigval)
{
char rxbuf[GDB_RXBUFLEN];
char *hargs;
long addr;
while (1)
{
getmsg(rxbuf);
hargs = rxbuf;
switch (*hargs++)
{
case '?':
last_signal(sigval);
break;
case 'c':
/* this call probably doesn't return */
hargs_parse_long(hargs, &addr, '#');
gdb_continue(addr);
/* if it does, exit back to interrupted code */
return;
case 'D':
/* detach from target, gdb is going away */
putmsg(0, "OK", 2);
gdb_detach();
break;
case 'g': read_registers(); break;
case 'G': write_registers(hargs); break;
case 'H':
/* set thread--- unimplemented, but gdb likes it */
putmsg(0, "OK", 2);
break;
case 'k':
/* kill program */
putmsg(0, "OK", 2);
gdb_kill();
break;
case 'm': read_memory(hargs); break;
case 'M': write_memory(hargs); break;
case 'P': write_register(hargs); break;
case 'q':
/* query */
/* TODO: finish query command in gdb_handle_exception. */
/* for now, only respond to "Offsets" query */
putmsg(0, "Text=0;Data=0;Bss=0", 19);
break;
case 's':
/* step (address optional) */
hargs_parse_long(hargs, &addr, '#');
gdb_step(addr);
/* exit back to interrupted code */
return;
case 'X':
/* write to memory (source in escaped-binary format) */
write_xbin_memory(hargs);
break;
default :
/* received a command we don't recognize---
send empty response per gdb spec */
putmsg(0, "", 0);
}
}
return;
}
void gdb_handle_exception (int sigval)
{
#if 1
/* for some reason, this seems to confuse gdb-5.0 */
/* tell the host why we're here */
expedited(sigval);
#else
last_signal(sigval);
#endif
/* ask gdb what to do next */
gdb_monitor(sigval);
/* return to the interrupted code, if gdb_monitor() didn't */
gdb_monitor_onexit();
gdb_return_from_exception();
return;
}