This repository was archived by the owner on Dec 23, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathgdb-server.c
More file actions
1955 lines (1665 loc) · 47.1 KB
/
gdb-server.c
File metadata and controls
1955 lines (1665 loc) · 47.1 KB
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
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ================================= *
* GDB Remote Serial Protocol Server *
* ================================= */
#include <arpa/inet.h>
#include <ctype.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <unistd.h>
#include "gdb-server.h"
/* buffer must fit largest target.xml and largest monitor command output */
#define GDB_BUFFER_SIZE 0x2000
enum gdb_state {
GDB_STATE_STOP,
GDB_STATE_RUN,
GDB_STATE_STEP_SETUP,
GDB_STATE_STEP,
GDB_STATE_KILL,
};
enum gdb_supported_flags {
GDB_S_ERROR_MESSAGE = 1 << 0,
GDB_S_HWBREAK = 1 << 1,
GDB_S_SWBREAK = 1 << 2,
GDB_S_VCONTSUPPORTED = 1 << 3,
};
enum gdb_breakpoint_type {
/* values same as used by protocol */
GDB_SWBREAK = 0,
GDB_HWBREAK = 1,
GDB_WATCH = 2,
GDB_RWATCH = 3,
GDB_AWATCH = 4,
GDB_BREAKPOINT_TYPE_MAX,
/* not actual breakpoint types, but other reasons for gdb_write_stop */
GDB_STOP_STEP,
GDB_STOP_CTRLC,
GDB_STOP_WHY,
};
struct gdb_breakpoint {
struct gdb_breakpoint *next;
enum gdb_breakpoint_type type;
unsigned long addr;
/* length of memory region, for watchpoints */
unsigned int kind;
/* set to true on r/w/a for watchpoints */
bool tripped;
/* the address that tripped this watchpoint */
unsigned long trip_addr;
};
struct gdb_server {
struct gdb_backend *b;
/* program execution state */
enum gdb_state state;
/* request Ctrl-C next time program is running */
bool ctrlc;
/* address breakpoints */
struct gdb_breakpoint *breakpoints;
/* the listening socket, if >= 0 */
int listen;
/* the connected socket, if >= 0 */
int client;
/* are we waiting on an ack? */
bool waiting_on_ack;
/* should we use acks? */
bool use_acks;
/* features advertised by gdb */
enum gdb_supported_flags supported;
/* input buffer and position */
char input_buf[GDB_BUFFER_SIZE];
char *input_next;
/* output buffer and position */
char output_buf[GDB_BUFFER_SIZE];
char *output_next;
/* true when we run out of buffer */
bool output_overflow;
};
/* ======================================= *
* Front-end Interface and socket handling *
* ======================================= */
static const char gdb_c_ack = '+';
static const char gdb_c_nack = '-';
static const char gdb_c_start = '$';
static const char gdb_c_end = '#';
static const char gdb_c_rle = '*';
static const char gdb_c_escape = '}';
static const char gdb_c_mangle = ' ';
static const char gdb_c_ctrlc = 0x03;
static bool gdb_server_parse_bind(char *bindstr, struct sockaddr_in6 *address);
static void gdb_server_close_listen(struct gdb_server *gdb);
static void gdb_server_close_client(struct gdb_server *gdb);
static void gdb_server_send_buffer(struct gdb_server *gdb);
static bool gdb_server_select(struct gdb_server *gdb, struct timeval *tv);
static char *gdb_server_handle_input(struct gdb_server *gdb);
/* actual debugging logic */
static void gdb_server_handle_packet(struct gdb_server *gdb, struct gdb_packet *p);
static void gdb_server_check_for_stop(struct gdb_server *gdb);
/* create a listening gdb server */
struct gdb_server *gdb_server_create(struct gdb_backend *backend, char *bindstr, bool stopped)
{
struct sockaddr_in6 bind_address;
int sock = -1;
if (!backend) {
goto fail;
}
if (!gdb_server_parse_bind(bindstr, &bind_address)) {
goto fail;
}
sock = socket(bind_address.sin6_family, SOCK_STREAM, 0);
if (sock < 0) {
goto fail;
}
/* allow re-use, so if we exit and restart the address
is still available */
int one = 1;
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
/* turn off Nagle's algorithm -- we always send() whole packets
and we want them to go out immediately (particularly acks) */
setsockopt(sock, SOL_TCP, TCP_NODELAY, &one, sizeof(one));
if (bind(sock, (struct sockaddr*)(&bind_address), sizeof(bind_address)) < 0) {
goto fail;
}
/* max one connection */
if (listen(sock, 1) < 0) {
goto fail;
}
struct gdb_server *gdb = calloc(1, sizeof(struct gdb_server));
if (!gdb) {
goto fail;
}
gdb->b = backend;
gdb->state = stopped ? GDB_STATE_STOP : GDB_STATE_RUN;
gdb->ctrlc = false;
gdb->breakpoints = NULL;
gdb->listen = sock;
gdb->client = -1;
gdb->waiting_on_ack = false;
gdb->use_acks = true;
gdb->supported = 0;
gdb->input_next = gdb->input_buf;
gdb->output_next = gdb->output_buf;
gdb->output_overflow = false;
return gdb;
fail:
if (sock >= 0) {
close(sock);
}
if (backend) {
if (backend->free) {
backend->free(backend->ctx);
}
free(backend);
}
return NULL;
}
/* free the gdb server */
void gdb_server_free(struct gdb_server *gdb)
{
if (gdb) {
gdb_server_close_client(gdb);
gdb_server_close_listen(gdb);
while (gdb->breakpoints) {
struct gdb_breakpoint *bp = gdb->breakpoints;
gdb->breakpoints = bp->next;
free(bp);
}
if (gdb->b->free) {
gdb->b->free(gdb->b->ctx);
}
free(gdb->b);
free(gdb);
}
}
/* call once per instruction, before stepping the cpu */
void gdb_server_step(struct gdb_server *gdb, volatile int *done)
{
do {
/* kill if requested */
if (gdb->state == GDB_STATE_KILL) {
if (done) {
*done = 1;
}
return;
}
/* 100ms when stopped, no blocking when running */
struct timeval tv = { 0, (gdb->state == GDB_STATE_STOP) ? 100000 : 0 };
if (!gdb_server_select(gdb, &tv)) {
/* server crashed, somehow. set done if we can */
if (done) {
*done = 1;
}
return;
}
if (gdb->state != GDB_STATE_STOP) {
/* evaluate breakpoints and other stop conditions */
gdb_server_check_for_stop(gdb);
}
} while (gdb->state == GDB_STATE_STOP && !(done && *done));
}
/* notify that memory has been accessed */
void gdb_server_notify(struct gdb_server *gdb, unsigned long addr, unsigned int len, bool write)
{
/* gdb only accesses us when stopped, cpu only when not stopped.
return here if stopped to prevent gdb from triggering watchpoints */
if (gdb->state == GDB_STATE_STOP) {
return;
}
/* trip any matching watchpoints */
for (struct gdb_breakpoint *bp = gdb->breakpoints; bp; bp = bp->next) {
if (bp->tripped) {
continue;
}
bool relevant =
(bp->type == GDB_WATCH && write) ||
(bp->type == GDB_RWATCH && !write) ||
(bp->type == GDB_AWATCH);
if (relevant && bp->addr < addr + len && addr < bp->addr + bp->kind) {
bp->tripped = true;
/* we want to use addr, but use bp->addr if addr out of range */
bp->trip_addr = addr >= bp->addr ? addr : bp->addr;
}
}
}
/* parse a [host]:<port> string into a socket address
if there is no colon, treat the whole thing as a port */
static bool gdb_server_parse_bind(char *bindstr, struct sockaddr_in6 *address)
{
char *hoststr = NULL;
char *portstr = bindstr;
char *colon = strrchr(bindstr, ':');
struct addrinfo hints = {0};
struct addrinfo *info = NULL;
struct addrinfo *chosen = NULL;
/* split the string at the colon */
if (colon) {
*colon = 0;
if (*bindstr) {
hoststr = bindstr;
}
portstr = colon + 1;
}
/* default to ipv4 loopback
unfortunately we need two sockets to listen on v4 and v6 loopback */
if (!hoststr) {
hoststr = "127.0.0.1";
}
hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICHOST;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* as a shorthand, address 0 means INADDR_ANY (via AI_PASSIVE) */
if (hoststr && strcmp(hoststr, "0") == 0) {
hoststr = NULL;
hints.ai_flags |= AI_PASSIVE;
}
getaddrinfo(hoststr, portstr, &hints, &info);
/* put the colon back, for error messages to use bindstr later */
if (colon) {
*colon = ':';
}
if (!info) {
return false;
}
/* choose the first address with the right family */
chosen = NULL;
for (struct addrinfo *addr = info; addr; addr = addr->ai_next) {
if (addr->ai_family != AF_INET && addr->ai_family != AF_INET6) {
continue;
}
/* if we haven't found an address yet, grab this one */
if (!chosen) {
chosen = addr;
}
/* if we used AI_PASSIVE for INADDR_ANY, prefer the ipv6 addresses
(this will accept both v4 and v6 connections) */
if (hints.ai_flags & AI_PASSIVE) {
if (chosen->ai_family == AF_INET && addr->ai_family == AF_INET6) {
chosen = addr;
}
}
}
if (chosen) {
memcpy(address, chosen->ai_addr, sizeof(*address));
}
freeaddrinfo(info);
return (chosen != NULL);
}
/* close and unset gdb->listen */
static void gdb_server_close_listen(struct gdb_server *gdb)
{
if (gdb->listen >= 0) {
close(gdb->listen);
gdb->listen = -1;
}
}
/* close and unset gdb->client */
static void gdb_server_close_client(struct gdb_server *gdb)
{
if (gdb->client >= 0) {
close(gdb->client);
gdb->client = -1;
}
}
/* send the current output buffer */
static void gdb_server_send_buffer(struct gdb_server *gdb)
{
if (gdb->client >= 0) {
size_t len = gdb->output_next - gdb->output_buf;
if (gdb->output_overflow || send(gdb->client, gdb->output_buf, len, 0) != len) {
/* error sending or overflow, disconnect */
gdb_server_close_client(gdb);
return;
}
gdb->waiting_on_ack = gdb->use_acks;
}
}
/* wait for socket events and dispatch them
returns true if the server is still alive */
static bool gdb_server_select(struct gdb_server *gdb, struct timeval *tv)
{
fd_set reads, excepts;
int maxfd;
FD_ZERO(&reads);
FD_ZERO(&excepts);
if (gdb->client >= 0) {
/* communicating */
FD_SET(gdb->client, &reads);
FD_SET(gdb->client, &excepts);
maxfd = gdb->client;
} else if (gdb->listen >= 0) {
/* listening */
FD_SET(gdb->listen, &reads);
FD_SET(gdb->listen, &excepts);
maxfd = gdb->listen;
} else {
/* server has died */
return false;
}
if (select(maxfd + 1, &reads, NULL, &excepts, tv) < 0) {
/* select error, close everything */
gdb_server_close_client(gdb);
gdb_server_close_listen(gdb);
}
if (FD_ISSET(gdb->listen, &excepts)) {
/* exception? close the socket */
gdb_server_close_listen(gdb);
}
if (FD_ISSET(gdb->client, &excepts)) {
/* exception? close the socket */
gdb_server_close_client(gdb);
}
if (FD_ISSET(gdb->listen, &reads)) {
/* connection waiting, accept it */
struct sockaddr_in6 client;
socklen_t client_len = sizeof(client);
int sock = accept(gdb->listen, (struct sockaddr*)(&client), &client_len);
if (sock >= 0) {
gdb->client = sock;
/* reset relevant variables */
gdb->ctrlc = false;
while (gdb->breakpoints) {
struct gdb_breakpoint *bp = gdb->breakpoints;
gdb->breakpoints = bp->next;
free(bp);
}
gdb->waiting_on_ack = false;
gdb->use_acks = true;
gdb->supported = 0;
gdb->input_next = gdb->input_buf;
gdb->output_next = gdb->output_buf;
gdb->output_overflow = false;
/* stop on connect */
gdb->state = GDB_STATE_STOP;
}
}
if (FD_ISSET(gdb->client, &reads)) {
ssize_t amount;
size_t avail = gdb->input_buf + GDB_BUFFER_SIZE - gdb->input_next;
amount = read(gdb->client, gdb->input_next, avail);
if (amount <= 0) {
/* connection closed on the other end, or we ran out of buffer */
gdb_server_close_client(gdb);
} else {
/* we received data */
gdb->input_next += amount;
/* handle repeatedly until nothing is consumed or nothing left */
while (gdb->input_next > gdb->input_buf) {
char *unconsumed = gdb_server_handle_input(gdb);
if (unconsumed == gdb->input_buf) {
break;
}
memmove(gdb->input_buf, unconsumed, gdb->input_next - unconsumed);
gdb->input_next -= unconsumed - gdb->input_buf;
}
}
}
/* server is still alive */
return true;
}
/* handle the data inside input[..input_len]
return a pointer to the first unconsumed byte
this also handles nacks and acks */
static char *gdb_server_handle_input(struct gdb_server *gdb)
{
/* look for the start of a packet, denoted by $ */
if (gdb->input_buf[0] == gdb_c_start) {
/* look for the end of packet, # */
uint8_t checksum = 0;
char *p;
for (p = gdb->input_buf + 1; p < gdb->input_next; p++) {
if (*p == gdb_c_end) {
break;
}
checksum += *p;
}
/* make sure we're at the end, and there are two checksum bytes */
if (*p != gdb_c_end || p + 3 > gdb->input_next) {
/* end not found, try again with more data later */
return gdb->input_buf;
}
p++;
/* only bother with the checksum if we use acks */
if (gdb->use_acks) {
unsigned int checksum_expected = 0x100;
sscanf(p, "%02x", &checksum_expected);
p += 2;
if (checksum != checksum_expected) {
/* bad checksum, send nack */
if (gdb->client >= 0) {
if (send(gdb->client, &gdb_c_nack, 1, 0) != 1) {
gdb_server_close_client(gdb);
return p;
}
}
/* eat this whole packet */
return p;
}
} else {
/* skip checksum */
p += 2;
}
/* we found a valid packet, ack it */
if (gdb->use_acks && gdb->client >= 0) {
if (send(gdb->client, &gdb_c_ack, 1, 0) != 1) {
gdb_server_close_client(gdb);
return p;
}
}
/* subtle business: we can write to buf[len] because it contains # */
struct gdb_packet packet = {
/* skip the $ */
gdb->input_buf + 1,
/* -1 for $, -3 for #CS */
p - 1 - 3 - gdb->input_buf,
gdb->input_buf[1],
};
/* handle the packet and eat it */
gdb_server_handle_packet(gdb, &packet);
return p;
} else {
for (char *p = gdb->input_buf; p < gdb->input_next; p++) {
if (gdb->use_acks && *p == gdb_c_ack) {
/* ack */
gdb->waiting_on_ack = false;
/* reset buffer */
gdb->output_overflow = false;
gdb->output_next = gdb->output_buf;
}
if (gdb->use_acks && *p == gdb_c_nack) {
/* nack */
if (!gdb->waiting_on_ack || gdb->output_next == gdb->output_buf) {
/* we don't have anything to resend, close connection */
gdb_server_close_client(gdb);
return p;
}
/* resend buffer */
gdb_server_send_buffer(gdb);
}
if (*p == gdb_c_ctrlc) {
/* Ctrl-C outside of a packet, request Ctrl-C in program */
gdb->ctrlc = true;
}
if (*p == gdb_c_start) {
/* found a packet start, discard it */
return p;
}
}
/* did not find a packet start, discard everything */
return gdb->input_next;
}
}
/* ============== *
* Writing to GDB *
* ============== */
enum gdb_error {
GDB_ERR_ARGUMENT,
GDB_ERR_NOT_FOUND,
GDB_ERR_NOT_IMPLEMENTED,
GDB_ERR_OUT_OF_MEMORY,
};
static void gdb_write_start(struct gdb_server *gdb);
static void gdb_write_end(struct gdb_server *gdb);
static void gdb_write_str(struct gdb_server *gdb, const char *str);
static void gdb_write_bin(struct gdb_server *gdb, const uint8_t *data, size_t len);
static void gdb_write_unsupported(struct gdb_server *gdb);
static void gdb_write_ok(struct gdb_server *gdb);
static void gdb_write_stop(struct gdb_server *gdb, enum gdb_breakpoint_type type, unsigned long addr);
static void gdb_write_err(struct gdb_server *gdb, enum gdb_error err);
static void gdb_write_errf(struct gdb_server *gdb, enum gdb_error err, const char *fmt, ...) GDB_ATTR_FORMAT(printf, 3, 4);
static void gdb_write_errfv(struct gdb_server *gdb, enum gdb_error err, const char *fmt, va_list args);
/* write a formatted string into the output buffer */
void gdb_writef(struct gdb_server *gdb, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
gdb_writefv(gdb, fmt, args);
va_end(args);
}
/* write a formatted string into the output buffer, using va_list */
void gdb_writefv(struct gdb_server *gdb, const char *fmt, va_list args)
{
size_t avail = gdb->output_buf + GDB_BUFFER_SIZE - gdb->output_next;
size_t amt = vsnprintf(gdb->output_next, avail, fmt, args);
if (amt >= avail) {
gdb->output_overflow = true;
} else {
gdb->output_next += amt;
}
}
/* start a new packet in the output buffer */
static void gdb_write_start(struct gdb_server *gdb)
{
if (gdb->use_acks && gdb->waiting_on_ack) {
/* can't send a new packet while waiting on the old one */
gdb_server_close_client(gdb);
return;
}
gdb->output_next = gdb->output_buf;
gdb->output_overflow = false;
*(gdb->output_next++) = gdb_c_start;
}
/* finish a packet, compute the checksum, and send it */
static void gdb_write_end(struct gdb_server *gdb)
{
if (gdb->output_next + 3 > gdb->output_buf + GDB_BUFFER_SIZE) {
/* overflow */
gdb->output_overflow = true;
} else {
/* tack on the packet end character and checksum */
uint8_t checksum = 0;
for (char *p = gdb->output_buf + 1; p < gdb->output_next; p++) {
checksum += *p;
}
gdb_writef(gdb, "%c%02x", gdb_c_end, checksum);
}
gdb_server_send_buffer(gdb);
}
/* put a 0-terminated string into the output buffer */
static void gdb_write_str(struct gdb_server *gdb, const char *str)
{
size_t len = strlen(str);
if (gdb->output_next + len > gdb->output_buf + GDB_BUFFER_SIZE) {
gdb->output_overflow = true;
return;
}
memcpy(gdb->output_next, str, len);
gdb->output_next += len;
}
/* put an escaped binary string into the output buffer */
static void gdb_write_bin(struct gdb_server *gdb, const uint8_t *data, size_t len)
{
char *output_end = gdb->output_buf + GDB_BUFFER_SIZE;
while (len) {
uint8_t val = *data;
if (val == gdb_c_start || val == gdb_c_end || val == gdb_c_rle || val == gdb_c_escape) {
val ^= gdb_c_mangle;
if (gdb->output_next + 1 > output_end) {
gdb->output_overflow = true;
break;
}
*(gdb->output_next++) = gdb_c_escape;
}
if (gdb->output_next + 1 > output_end) {
gdb->output_overflow = true;
break;
}
*(gdb->output_next++) = val;
data++;
len--;
}
}
/* write an entire empty packet, indicating last packet is unsupported */
static void gdb_write_unsupported(struct gdb_server *gdb)
{
gdb_write_start(gdb);
gdb_write_end(gdb);
}
/* write an entire ok packet */
static void gdb_write_ok(struct gdb_server *gdb)
{
gdb_write_start(gdb);
gdb_write_str(gdb, "OK");
gdb_write_end(gdb);
}
static void gdb_write_stop(struct gdb_server *gdb, enum gdb_breakpoint_type type, unsigned long addr)
{
/* unix signal values used here */
const unsigned int sigint = 0x02;
const unsigned int sigtrap = 0x05;
/* should we put the address in the stop reply? */
bool use_addr =
type == GDB_WATCH || type == GDB_RWATCH || type == GDB_AWATCH;
/* should we use a stop reply reason? */
bool use_reason =
(type == GDB_SWBREAK && (gdb->supported & GDB_S_SWBREAK)) ||
(type == GDB_HWBREAK && (gdb->supported & GDB_S_HWBREAK)) ||
use_addr;
/* use SIGTRAP by default for breakpoints */
unsigned int signal = sigtrap;
if (type == GDB_STOP_CTRLC) {
/* but use SIGINT for Ctrl-C */
signal = sigint;
}
/* stop the program */
gdb->state = GDB_STATE_STOP;
/* send gdb a reason why we stopped */
gdb_write_start(gdb);
if (use_reason) {
const char *reason = "unknown";
switch (type) {
case GDB_SWBREAK:
reason = "swbreak";
break;
case GDB_HWBREAK:
reason = "hwbreak";
break;
case GDB_WATCH:
reason = "watch";
break;
case GDB_RWATCH:
reason = "rwatch";
break;
case GDB_AWATCH:
reason = "awatch";
break;
default:
/* not reachable: use_reason is true */
break;
}
if (use_addr) {
gdb_writef(gdb, "T%02x%s:%lx;", signal, reason, addr);
} else {
gdb_writef(gdb, "T%02x%s:;", signal, reason);
}
} else {
gdb_writef(gdb, "S%02x", signal);
}
gdb_write_end(gdb);
}
/* write an entire error packet */
static void gdb_write_err(struct gdb_server *gdb, enum gdb_error err)
{
va_list args;
gdb_write_errfv(gdb, err, NULL, args);
}
/* write an entire error packet, with extra message */
static void gdb_write_errf(struct gdb_server *gdb, enum gdb_error err, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
gdb_write_errfv(gdb, err, fmt, args);
va_end(args);
}
/* gdb_write_errf but with va_list */
static void gdb_write_errfv(struct gdb_server *gdb, enum gdb_error err, const char *fmt, va_list args)
{
gdb_write_start(gdb);
if (gdb->supported & GDB_S_ERROR_MESSAGE) {
gdb_write_str(gdb, "E.");
gdb_writef(gdb, "error %02x: ", err);
const char *msg = "unknown";
switch (err) {
case GDB_ERR_ARGUMENT:
msg = "bad argument";
break;
case GDB_ERR_NOT_FOUND:
msg = "not found";
break;
case GDB_ERR_NOT_IMPLEMENTED:
msg = "not implemented";
break;
case GDB_ERR_OUT_OF_MEMORY:
msg = "out of memory";
break;
}
gdb_write_str(gdb, msg);
if (fmt) {
gdb_write_str(gdb, ": ");
gdb_writefv(gdb, fmt, args);
}
} else {
gdb_writef(gdb, "E%02x", err);
}
gdb_write_end(gdb);
}
/* ============== *
* Packet Parsing *
* ============== */
static void gdb_packet_restore(struct gdb_packet *p, struct gdb_packet *other);
static const char *gdb_packet_take(struct gdb_packet *p, size_t amt);
static bool gdb_packet_skip(struct gdb_packet *p, size_t amt);
static bool gdb_packet_match(struct gdb_packet *p, const char *prefix);
static const char *gdb_packet_until(struct gdb_packet *p, const char *delimiters);
static bool gdb_packet_binary(struct gdb_packet *p);
/* slightly different from scanf, used like:
int end;
gdb_packet_scanf(p, &end, "some format", [...]);
// ... or ...
_gdb_packet_scanf(p, &end, "some format%n", [...], &end);
never matches an empty string.
returns true and consumes up to end on success.
on failure, returns false and end == -1 */
bool _gdb_packet_scanf(struct gdb_packet *p, int *end, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
bool success = _gdb_packet_scanfv(p, end, fmt, args);
va_end(args);
return success;
}
/* _gdb_packet_scanf but with va_list */
bool _gdb_packet_scanfv(struct gdb_packet *p, int *end, const char *fmt, va_list args)
{
*end = -1;
/* never match an empty string */
if (!p->len) {
return false;
}
/* careful: p->len > 0, so setting p->buf[p->len] doesn't touch p->c */
char last = p->buf[p->len];
p->buf[p->len] = 0;
gdb_packet_restore(p, NULL);
if (vsscanf(p->buf, fmt, args) < 0 || *end <= 0) {
p->buf[p->len] = last;
*end = -1;
return false;
}
p->buf[p->len] = last;
gdb_packet_skip(p, *end);
return true;
}
/* restore packet state from backup.
other may be NULL, which only resets buf[0].
usage:
struct gdb_packet backup = *p;
...;
gdb_packet_restore(p, &backup); */
static void gdb_packet_restore(struct gdb_packet *p, struct gdb_packet *other)
{
p->buf[0] = p->c;
if (other) {
*p = *other;
}
}
/* consumes and returns amt bytes, or NULL if not available */
static const char *gdb_packet_take(struct gdb_packet *p, size_t amt)
{
if (p->len < amt) {
p->buf += p->len;
p->len = 0;
p->c = p->buf[0];
return NULL;
}
/* return the current buffer, restoring buf[0] */
gdb_packet_restore(p, NULL);
char *ret = p->buf;
/* advance by amt and drop a 0 */
p->buf += amt;
p->len -= amt;
p->c = p->buf[0];
p->buf[0] = 0;
return ret;
}
/* skips amt bytes. returns true if successful */
static bool gdb_packet_skip(struct gdb_packet *p, size_t amt)
{
if (p->len < amt) {
p->buf += p->len;
p->len = 0;
p->c = p->buf[0];
return false;
}
/* advance by amt, don't bother restoring buf[0] */
p->buf += amt;
p->len -= amt;
p->c = p->buf[0];
return true;
}
/* returns true if packet matches prefix. consumes prefix */
static bool gdb_packet_match(struct gdb_packet *p, const char *prefix)
{
size_t len = strlen(prefix);
gdb_packet_restore(p, NULL);
if (strncmp(p->buf, prefix, len) == 0) {
gdb_packet_skip(p, len);
return true;
}
return false;
}
/* return bytes up to (not including) any delimiter
or up to the end of the packet
on success, consumes up to and including the delimiter */
static const char *gdb_packet_until(struct gdb_packet *p, const char *delimiters)
{
size_t i;
gdb_packet_restore(p, NULL);
for (i = 0; i < p->len; i++) {
if (strchr(delimiters, p->buf[i])) {
break;
}
}
/* end of string or a delimiter is in p->buf[i] */
const char *ret = gdb_packet_take(p, i);
/* skip delimiter */
gdb_packet_skip(p, 1);
return ret;
}
/* convert the rest of the packet into unescaped binary data.
returns true on success and leaves data in p->buf / p->len */
static bool gdb_packet_binary(struct gdb_packet *p)
{
char *src = p->buf;
char *src_end = p->buf + p->len;
char *dest = p->buf;
gdb_packet_restore(p, NULL);
while (src < src_end) {
if (*src == gdb_c_escape) {
src++;
if (src >= src_end) {
/* unpaired escape */
return false;
}
*src ^= gdb_c_mangle;
p->len--;
}
*(dest++) = *(src++);
}
return true;
}
/* =============================== *
* Packet Handling and Debug Logic *
* =============================== */