forked from sergev/ejtagproxy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgdbproxy.c
2704 lines (2415 loc) · 76.5 KB
/
gdbproxy.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
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
/*
* Main remote proxy unit.
*
* Copyright (C) 1999-2001 Quality Quorum, Inc.
* Copyright (C) 2002 Chris Liechti and Steve Underwood
* Copyright (C) 2012 Serge Vakulenko
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* QQI can be contacted as [email protected]
*/
#define VERSION "1.0." GITCOUNT
#if defined(WIN32)
# include <windows.h>
#endif
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <getopt.h>
#include "gdbproxy.h"
/* Debug flag */
int debug_level = 0;
/* Flag to catch unexpected output from target */
static int rp_target_out_valid = FALSE;
static int rp_target_running = FALSE;
/* Current log */
static log_func rp_log = NULL;
/* Connection to debugger */
static int rp_putpkt(const char *buf);
static int rp_getpkt(char *buf, size_t buf_len, size_t *in_len, int timeout);
static void rp_console_output(const char *buf);
static void rp_data_output(const char *buf);
/* Decode/encode functions */
static int rp_decode_data(const char *in,
unsigned char *out,
size_t out_size,
size_t *len);
static int rp_decode_reg_assignment(const char *in,
unsigned int *reg_no,
unsigned char *out,
size_t out_size,
size_t *len);
static int rp_decode_mem(const char *in,
uint64_t *addr,
size_t *len);
static int rp_decode_process_query(const char *in,
unsigned int *mask,
rp_thread_ref *ref);
static int rp_decode_break(const char *in,
int *type,
uint64_t *addr,
unsigned int *len);
static int rp_decode_list_query(const char *in,
int *first,
size_t *max,
rp_thread_ref *arg);
static int rp_encode_regs(const unsigned char *data,
const unsigned char *avail,
size_t data_len,
char *out,
size_t out_size);
static int rp_encode_data(const unsigned char *data,
size_t data_len,
char *out,
size_t out_size);
static int rp_encode_process_query_response(unsigned int mask,
const rp_thread_ref *ref,
const rp_thread_info *info,
char *out,
size_t out_size);
static int rp_encode_list_query_response(size_t count,
int done,
const rp_thread_ref *arg,
const rp_thread_ref *found,
char *out,
size_t out_size);
static int rp_decode_nibble(const char *in, unsigned int *nibble);
static int rp_decode_byte(const char *in, unsigned int *byte_ptr);
static int rp_decode_4bytes(const char *in, uint32_t *val);
static int rp_decode_8bytes(const char *in, uint64_t *val);
static int rp_decode_uint32(char const **in, uint32_t *val, char break_char);
static int rp_decode_uint64(char const **in, uint64_t *val, char break_char);
static void rp_encode_byte(unsigned int val, char *out);
/* Usage */
static void rp_usage(void);
/* Funcions to stuff output value */
static void rp_write_retval(int ret, char *buf);
static int can_restart;
static int doing_daemon;
static int extended_protocol;
static char *name;
static void handle_search_memory_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_thread_commands(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_read_registers_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_write_registers_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_read_single_register_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_write_single_register_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_read_memory_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_write_memory_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_running_commands(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
char *status_string,
int status_string_len,
rp_target *t);
static int handle_kill_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static int handle_thread_alive_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static int handle_restart_target_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_detach_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_query_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_breakpoint_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t);
static void handle_search_memory_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
uint64_t addr;
uint32_t pattern;
uint32_t mask;
const char *in;
/* Format: taddr:PP,MM
Search backwards starting at address addr for a match with the
supplied pattern PP and mask MM. PP and MM are 4 bytes. addr
must be at least 3 digits. */
in = &in_buf[1];
if (!rp_decode_uint64(&in, &addr, ':'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
if (!rp_decode_uint32(&in, &pattern, ','))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
if (!rp_decode_uint32(&in, &mask, '\0'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
}
static void handle_thread_commands(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
rp_thread_ref ref;
const char *in;
if (in_len == 1 || in_buf[2] == '-')
{
/* Either short or an obsolete form */
return;
}
/* Set thread */
switch (in_buf[1])
{
case 'c':
in = &in_buf[2];
if (!rp_decode_uint64(&in, &ref.val, '\0'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
}
ret = t->set_ctrl_thread(&ref);
rp_write_retval(ret, out_buf);
break;
case 'g':
in = &in_buf[2];
if (!rp_decode_uint64(&in, &ref.val, '\0'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
}
ret = t->set_gen_thread(&ref);
rp_write_retval(ret, out_buf);
break;
default:
rp_log(RP_VAL_LOGLEVEL_ERR, "%s: Bad H command", name);
break;
}
}
static void handle_read_registers_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
size_t len;
unsigned char data_buf[RP_PARAM_DATABYTES_MAX];
unsigned char avail_buf[RP_PARAM_DATABYTES_MAX];
/* Get all registers. Format: 'g'. Note we do not do any
data caching - all caching is done by the debugger */
ret = t->read_registers(data_buf,
avail_buf,
sizeof(data_buf),
&len);
switch (ret)
{
case RP_VAL_TARGETRET_OK:
assert(len <= RP_PARAM_DATABYTES_MAX);
rp_encode_regs(data_buf,
avail_buf,
len,
out_buf,
out_buf_len);
break;
case RP_VAL_TARGETRET_ERR:
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
default:
/* This should not happen */
assert(0);
break;
}
}
static void handle_write_registers_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
size_t len;
unsigned char data_buf[RP_PARAM_DATABYTES_MAX];
/* Write all registers. Format: 'GXXXXXXXXXX' */
ret = rp_decode_data(&in_buf[1], data_buf, sizeof(data_buf), &len);
if (!ret)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
ret = t->write_registers(data_buf, len);
rp_write_retval(ret, out_buf);
}
static void handle_read_single_register_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
unsigned int reg_no;
size_t len;
unsigned char data_buf[RP_PARAM_DATABYTES_MAX];
unsigned char avail_buf[RP_PARAM_DATABYTES_MAX];
const char *in;
/* Get a single register. Format 'pNN' */
in = &in_buf[1];
if (! rp_decode_uint32(&in, ®_no, '\0')) {
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
ret = t->read_single_register(reg_no,
data_buf,
avail_buf,
sizeof(data_buf),
&len);
switch (ret)
{
case RP_VAL_TARGETRET_OK:
assert(len <= RP_PARAM_DATABYTES_MAX);
rp_encode_regs(data_buf,
avail_buf,
len,
out_buf,
out_buf_len);
break;
case RP_VAL_TARGETRET_ERR:
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
default:
/* This should not happen */
assert(0);
break;
}
}
static void handle_write_single_register_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
unsigned int reg_no;
size_t len;
unsigned char data_buf[RP_PARAM_DATABYTES_MAX];
/* Write a single register. Format: 'PNN=XXXXX' */
ret = rp_decode_reg_assignment(&in_buf[1],
®_no,
data_buf,
sizeof(data_buf),
&len);
if (!ret)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
//assert(len == 4);
assert(len < RP_PARAM_DATABYTES_MAX);
ret = t->write_single_register(reg_no, data_buf, len);
rp_write_retval(ret, out_buf);
}
static void handle_read_memory_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
size_t len;
uint64_t addr;
unsigned char data_buf[RP_PARAM_DATABYTES_MAX];
/* Read memory format: 'mAA..A,LL..LL' */
if (!(ret = rp_decode_mem(&in_buf[1], &addr, &len)))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
/* Limit it so buggy gdbs will not complain */
if (len > ((RP_VAL_DBG_PBUFSIZ - 32)/2))
len = (RP_VAL_DBG_PBUFSIZ - 32)/2;
ret = t->read_mem(addr, data_buf, len, &len);
switch (ret)
{
case RP_VAL_TARGETRET_OK:
assert(len <= RP_PARAM_DATABYTES_MAX);
rp_encode_data(data_buf, len, out_buf, out_buf_len);
break;
case RP_VAL_TARGETRET_ERR:
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
default:
/* This should not happen */
assert(0);
break;
}
}
static void handle_write_memory_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
char *cp;
size_t len;
size_t len1;
uint64_t addr;
unsigned char data_buf[RP_PARAM_DATABYTES_MAX];
/* Write memory format: 'mAA..A,LL..LL:XX..XX' */
if ((cp = strchr(&in_buf[1], ':')) == NULL)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
*cp = '\0';
ret = rp_decode_mem(&in_buf[1], &addr, &len);
if (!ret || len > RP_PARAM_DATABYTES_MAX)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
ret = rp_decode_data(cp + 1, data_buf, sizeof(data_buf), &len1);
if (!ret || len != len1)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
ret = t->write_mem(addr, data_buf, len);
rp_write_retval(ret, out_buf);
}
static void handle_running_commands(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
char *status_string,
int status_string_len,
rp_target *t)
{
int step;
uint32_t sig;
int go;
int more;
int implemented;
const char *addr_ptr;
uint64_t addr;
int ret;
const char *in;
/* 'w' go from address
* 'W' go from address with signal
* 's' step from address
* 'S' step from address with signal
* 'c' continue from address
* 'C' continue from address with signal
*/
step = (in_buf[0] == 'S' || in_buf[0] == 's');
go = (in_buf[0] == 'W' || in_buf[0] == 'w');
addr_ptr = NULL;
if (in_buf[0] == 'C' || in_buf[0] == 'S' || in_buf[0] == 'W')
{
/*
* Resume with signal.
* Format Csig[;AA..AA], Ssig[;AA..AA], or Wsig[;AA..AA]
*/
in = &in_buf[1];
if (strchr(in, ';'))
{
if (!rp_decode_uint32(&in, &sig, ';'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
addr_ptr = in;
}
else
{
if (!rp_decode_uint32(&in, &sig, '\0'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
}
}
else
{
sig = RP_VAL_TARGETSIG_0;
if (in_buf[1] != '\0')
addr_ptr = &in_buf[1];
}
if (go)
{
ret = t->go_waiting(sig);
}
else if (addr_ptr)
{
if (!rp_decode_uint64(&addr_ptr, &addr, '\0'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
ret = t->resume_from_addr(step, sig, addr);
}
else
{
ret = t->resume_from_current(step, sig);
}
if (ret != RP_VAL_TARGETRET_OK)
{
rp_write_retval(ret, out_buf);
return;
}
/* Now we have to wait for the target */
rp_target_running = TRUE;
rp_target_out_valid = TRUE;
/* Try a partial wait first */
ret = t->wait_partial(TRUE,
status_string,
status_string_len,
rp_console_output,
&implemented,
&more);
if (ret != RP_VAL_TARGETRET_OK)
{
rp_write_retval(ret, out_buf);
rp_target_out_valid = FALSE;
rp_target_running = FALSE;
return;
}
if (!implemented)
{
/* There is no pertial wait facility for this target, so use a
blocking wait */
ret = t->wait(status_string,
status_string_len,
rp_console_output,
&implemented);
assert(implemented);
if (ret == RP_VAL_TARGETRET_OK)
{
assert(strlen(status_string) < status_string_len);
strcpy(out_buf, status_string);
}
else
{
rp_write_retval(ret, out_buf);
}
rp_target_out_valid = FALSE;
rp_target_running = FALSE;
return;
}
if (!more)
{
/* We are done. The program has already stopped */
assert(strlen(status_string) < status_string_len);
strcpy(out_buf, status_string);
rp_target_out_valid = FALSE;
rp_target_running = FALSE;
}
}
static int handle_kill_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
t->kill();
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: session finished",
name);
if (!extended_protocol)
{
t->close();
dbg_sock_close();
if (!can_restart)
{
/* If the current target cannot restart, we have little choice but
to exit right now. */
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: exiting",
name);
dbg_sock_cleanup();
exit(0);
}
return FALSE;
}
/* Let us do our best while starting system */
if (!can_restart)
{
/* Even if restart is not supported it is still worth calling connect */
return -1;
}
ret = t->restart();
assert(ret != RP_VAL_TARGETRET_NOSUPP);
if (ret != RP_VAL_TARGETRET_OK)
{
/* There is no point in continuing */
rp_log(RP_VAL_LOGLEVEL_ERR,
"%s: unable to restart target %s",
name,
t->name);
t->close();
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
rp_putpkt(out_buf);
dbg_sock_close();
if (!can_restart)
{
dbg_sock_cleanup();
exit(1);
}
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: will wait for a new connection",
name);
return FALSE;
}
return TRUE;
}
static int handle_thread_alive_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
int alive;
rp_thread_ref ref;
const char *in;
/* Is thread alive? */
/* This is a deprecated feature of the remote debug protocol */
in = &in_buf[1];
if (!(ret = rp_decode_uint64(&in, &ref.val, '\0')))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return TRUE;
}
ret = t->is_thread_alive(&ref, &alive);
if (ret != RP_VAL_TARGETRET_OK)
{
rp_write_retval(ret, out_buf);
}
else
{
if (alive)
rp_write_retval(RP_VAL_TARGETRET_OK, out_buf);
else
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
}
return TRUE;
}
static int handle_restart_target_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
/* Restarting the target is only supported in the extended protocol. */
if (!extended_protocol)
return FALSE;
assert(can_restart);
/* Let us do our best to restart the system */
ret = t->restart();
if (ret != RP_VAL_TARGETRET_OK)
{
/* There is no point to continuing */
rp_log(RP_VAL_LOGLEVEL_ERR,
"%s: unable to restart target %s",
name,
t->name);
t->close();
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
rp_putpkt(out_buf);
dbg_sock_close();
if (!can_restart)
{
/* If the current target cannot restart, we have little choice but
to exit right now. */
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: target is not restartable. Exiting",
name);
dbg_sock_cleanup();
exit(1);
}
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: will wait for a new connection",
name);
return -1;
}
return TRUE;
}
static void handle_detach_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
t->disconnect();
/* Note: The current GDB does not expect a reply */
t->close();
rp_putpkt(out_buf);
dbg_sock_close();
rp_log(RP_VAL_LOGLEVEL_INFO, "%s: debugger detached", name);
if (!can_restart)
{
/* If the current target cannot restart, we have little choice but
to exit right now. */
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: target is not restartable. Exiting",
name);
dbg_sock_cleanup();
exit(0);
}
rp_log(RP_VAL_LOGLEVEL_INFO,
"%s: will wait for a new connection",
name);
}
static void handle_query_command(char * const in_buf,
int in_len,
char *out_buf,
int out_buf_len,
rp_target *t)
{
int ret;
rp_thread_ref ref;
rp_thread_info info;
unsigned int mask;
rp_thread_ref arg;
rp_thread_ref *found;
size_t max_found;
size_t count;
int done;
int first;
unsigned int len;
uint32_t val;
uint64_t addr;
const char *cp;
if (in_len == 1)
{
rp_log(RP_VAL_LOGLEVEL_ERR,
"%s: bad 'q' command received",
name);
return;
}
if (strncmp(in_buf + 1, "Offsets", 7) == 0)
{
uint64_t text;
uint64_t data;
uint64_t bss;
/* Get the program segment offsets */
ret = t->offsets_query(&text, &data, &bss);
if (ret == RP_VAL_TARGETRET_OK)
{
sprintf(out_buf,
"Text=%llx;Data=%llx;Bss=%llx",
(unsigned long long) text,
(unsigned long long) data,
(unsigned long long) bss);
}
else
{
rp_write_retval(ret, out_buf);
}
return;
}
if (strncmp(in_buf + 1, "CRC:", 4) == 0)
{
/* Find the CRC32 value of the specified memory area */
cp = &in_buf[5];
if (!rp_decode_uint64(&cp, &addr, ','))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
if (!rp_decode_uint32(&cp, &len, '\0'))
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
return;
}
ret = t->crc_query(addr, len, &val);
if (ret == RP_VAL_TARGETRET_OK)
sprintf(out_buf, "C%x", val);
else
rp_write_retval(ret, out_buf);
return;
}
if (strncmp(in_buf + 1, "Symbol:", 7) == 0)
{
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "TStatus:", 8) == 0)
{
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "ThreadExtraInfo,", 16) == 0)
{
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "fThreadInfo:", 12) == 0)
{
/* Get first string of thread info */
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "sThreadInfo:", 12) == 0)
{
/* Get subsequent string of thread info */
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "fProcessInfo", 12) == 0)
{
/* Get first string of process info */
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "sProcessInfo", 12) == 0)
{
/* Get subsequent string of process info */
rp_write_retval(RP_VAL_TARGETRET_NOSUPP, out_buf);
return;
}
if (strncmp(in_buf + 1, "Rcmd,", 5) == 0)
{
/* Remote command */
rp_target_out_valid = TRUE;
ret = t->remcmd(&in_buf[6],
rp_console_output,
rp_data_output);
rp_target_out_valid = FALSE;
rp_write_retval(ret, out_buf);
return;
}
switch (in_buf[1])
{
case 'C':
/* Current thread query */
ret = t->current_thread_query(&ref);
if (ret == RP_VAL_TARGETRET_OK)
sprintf(out_buf, "QC%llx", (unsigned long long) ref.val);
else
rp_write_retval(ret, out_buf);
break;
case 'L':
/* Thread list query */
ret = rp_decode_list_query(&in_buf[2],
&first,
&max_found,
&arg);
if (!ret || max_found > 255)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
}
if ((found = malloc(max_found*sizeof(rp_thread_ref))) == NULL)
{
rp_write_retval(RP_VAL_TARGETRET_ERR, out_buf);
break;
}
ret = t->list_query(first,
&arg,
found,
max_found,
&count,
&done);
if (ret != RP_VAL_TARGETRET_OK || count > max_found)
{
free(found);
rp_write_retval(ret, out_buf);
break;
}
ret = rp_encode_list_query_response(count,
done,
&arg,
found,
out_buf,
out_buf_len);