forked from PicoPET/thumbulator-stm32f0x
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrsp-server.c
More file actions
2550 lines (2143 loc) · 75.2 KB
/
rsp-server.c
File metadata and controls
2550 lines (2143 loc) · 75.2 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
/* rsp-server.c -- Remote Serial Protocol server for GDB
Copyright (C) 2008 Embecosm Limited
Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option)
any later version.
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>. */
/* This program is commented throughout in a fashion suitable for processing
with Doxygen. */
///* Autoconf and/or portability configuration */
//#include "config.h"
//#include "port.h"
/* System includes */
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <arpa/inet.h>
#include <poll.h>
#include <netinet/tcp.h>
#include <signal.h>
#include <string.h>
///* Package includes */
//#include "sim-config.h"
//#include "except.h"
//#include "opcode/or32.h"
//#include "spr-defs.h"
//#include "execute.h"
//#include "debug-unit.h"
//#include "sprs.h"
//#include "toplevel-support.h"
//#include "dcache-model.h"
//#include "icache-model.h"
#include "rsp-server.h"
#include "exmemwb.h"
#include "sim_support.h"
/* Define to log each packet */
#define RSP_TRACE 0
/*! Name of the Or1ksim RSP service */
#define OR1KSIM_RSP_SERVICE "or1ksim-rsp"
/*! Protocol used by Or1ksim */
#define OR1KSIM_RSP_PROTOCOL "tcp"
/*! Thread ID used by Or1ksim */
#define OR1KSIM_TID 1
#define WORDSBIGENDIAN
/* JVDW FIX */
#define MAX_GPRS 15
#define MAX_GRPS 15
/* Indices of GDB registers that are not GPRs. Must match GDB settings! */
#define CPSR_REGNUM 0x19
#define PC_REGNUM 15
#define NUM_REGS (MAX_GRPS+1) /*!< Total GDB registers */
/*! Trap instruction for OR32 */
#define OR1K_TRAP_INSTR 0x21000001
/*! Definition of GDB target signals. Data taken from the GDB 6.8
source. Only those we use defined here. */
enum target_signal {
TARGET_SIGNAL_NONE = 0,
TARGET_SIGNAL_INT = 2,
TARGET_SIGNAL_ILL = 4,
TARGET_SIGNAL_TRAP = 5,
TARGET_SIGNAL_FPE = 8,
TARGET_SIGNAL_BUS = 10,
TARGET_SIGNAL_SEGV = 11,
TARGET_SIGNAL_ALRM = 14,
TARGET_SIGNAL_USR2 = 31,
TARGET_SIGNAL_PWR = 32
};
/*! The maximum number of characters in inbound/outbound buffers. The largest
packets are the 'G' packet, which must hold the 'G' and all the registers
with two hex digits per byte and the 'g' reply, which must hold all the
registers, and (in our implementation) an end-of-string (0)
character. Adding the EOS allows us to print out the packet as a
string. So at least NUMREGBYTES*2 + 1 (for the 'G' or the EOS) are needed
for register packets */
#define GDB_BUF_MAX ((NUM_REGS) * 8 + 1)
/*! String to map hex digits to chars */
static const char hexchars[]="0123456789abcdef";
/*! Data structure for RSP buffers. Can't be null terminated, since it may
include zero bytes */
struct rsp_buf
{
char data[GDB_BUF_MAX];
int len;
};
struct RSP rsp;
/* Forward declarations of static functions */
static void rsp_get_client ();
static void rsp_client_request ();
static void rsp_client_close ();
static void put_packet (struct rsp_buf *buf);
static void put_str_packet (const char *str);
static struct rsp_buf *get_packet ();
static void put_rsp_char (char c);
static int get_rsp_char ();
static int rsp_unescape (char *data,
int len);
static void mp_hash_init ();
static void mp_hash_add (enum mp_type type,
unsigned long int addr,
unsigned long int instr);
static struct mp_entry *mp_hash_lookup (enum mp_type type,
unsigned long int addr);
static struct mp_entry *mp_hash_delete (enum mp_type type,
unsigned long int addr);
static int hex (int c);
static void reg2hex (unsigned long int val,
char *buf);
static unsigned long int hex2reg (char *buf);
static void ascii2hex (char *dest,
char *src);
static void hex2ascii (char *dest,
char *src);
static void set_npc (unsigned long int addr);
static void rsp_report_exception ();
static void rsp_continue (struct rsp_buf *buf);
static void rsp_continue_with_signal (struct rsp_buf *buf);
static void rsp_continue_generic (unsigned long int addr,
unsigned long int except);
static void rsp_read_all_regs ();
static void rsp_write_all_regs (struct rsp_buf *buf);
static void rsp_read_mem (struct rsp_buf *buf);
static void rsp_write_mem (struct rsp_buf *buf);
static void rsp_read_reg (struct rsp_buf *buf);
static void rsp_write_reg (struct rsp_buf *buf);
static void rsp_query (struct rsp_buf *buf);
static void rsp_command (struct rsp_buf *buf);
static void rsp_set (struct rsp_buf *buf);
static void rsp_restart ();
static void rsp_step (struct rsp_buf *buf);
static void rsp_step_with_signal (struct rsp_buf *buf);
static void rsp_step_generic (unsigned long int addr,
unsigned long int except);
static void rsp_vpkt (struct rsp_buf *buf);
static void rsp_write_mem_bin (struct rsp_buf *buf);
static void rsp_remove_matchpoint (struct rsp_buf *buf);
static void rsp_insert_matchpoint (struct rsp_buf *buf);
unsigned int get_pc()
{
return cpu.gpr[PC_REGNUM]-4;
}
void set_pc(unsigned int val)
{
cpu.gpr[PC_REGNUM] = (val+4) | 0x01;
}
/*---------------------------------------------------------------------------*/
/*!Initialize the Remote Serial Protocol connection
Set up the central data structures. */
/*---------------------------------------------------------------------------*/
void
rsp_init ()
{
/* Clear out the central data structure */
rsp.client_waiting = 0; /* GDB client is not waiting for us */
rsp.client_fd = -1; /* i.e. invalid */
rsp.sigval = 5; /* No exception */
//rsp.start_addr = EXCEPT_RESET; /* Default restart point */
//JVDW FIX
rsp.start_addr = 0;
rsp.port = 272727;
rsp.stalled = 1;
rsp.stepping = 0;
/* Set up the matchpoint hash table */
mp_hash_init ();
} /* rsp_init () */
/*---------------------------------------------------------------------------*/
/*!Look for action on RSP
This function is called when the processor has stalled, which, except for
initialization, must be due to an interrupt.
If we have no RSP client, we get one. We can make no progress until the
client is available.
Then if the cause is an exception following a step or continue command, and
the exception not been notified to GDB, a packet reporting the cause of the
exception is sent.
The next client request is then processed. */
/*---------------------------------------------------------------------------*/
void
handle_rsp ()
{
/* If we have no RSP client, wait until we get one. */
while (-1 == rsp.client_fd)
{
rsp_get_client ();
rsp.client_waiting = 0; /* No longer waiting */
}
/* If we have an unacknowledged exception tell the GDB client. If this
exception was a trap due to a memory breakpoint, then adjust the NPC. */
//if (rsp.client_waiting)
// {
// if ((TARGET_SIGNAL_TRAP == rsp.sigval) &&
// (NULL != mp_hash_lookup (BP_MEMORY, cpu_state.sprs[SPR_PPC])))
//{
// set_npc (cpu_state.sprs[SPR_PPC]);
//}
// rsp_report_exception();
// rsp.client_waiting = 0; /* No longer waiting */
// }
/* Get a RSP client request */
rsp_client_request ();
} /* handle_rsp () */
/*---------------------------------------------------------------------------*/
/*!Note an exception for future processing
The simulator has encountered an exception. Record it here, so that a
future call to handle_exception will report it back to the client. The
signal is supplied in Or1ksim form and recorded in GDB form.
We flag up a warning if an exception is already pending, and ignore the
earlier exception.
@param[in] except The exception (Or1ksim form) */
/*---------------------------------------------------------------------------*/
void
rsp_exception (unsigned long int except)
{
int sigval; /* GDB signal equivalent to exception */
switch (except)
{
//case EXCEPT_RESET: sigval = TARGET_SIGNAL_PWR; break;
//case EXCEPT_BUSERR: sigval = TARGET_SIGNAL_BUS; break;
//case EXCEPT_DPF: sigval = TARGET_SIGNAL_SEGV; break;
//case EXCEPT_IPF: sigval = TARGET_SIGNAL_SEGV; break;
//case EXCEPT_TICK: sigval = TARGET_SIGNAL_ALRM; break;
//case EXCEPT_ALIGN: sigval = TARGET_SIGNAL_BUS; break;
//case EXCEPT_ILLEGAL: sigval = TARGET_SIGNAL_ILL; break;
//case EXCEPT_INT: sigval = TARGET_SIGNAL_INT; break;
//case EXCEPT_DTLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
//case EXCEPT_ITLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
//case EXCEPT_RANGE: sigval = TARGET_SIGNAL_FPE; break;
//case EXCEPT_SYSCALL: sigval = TARGET_SIGNAL_USR2; break;
//case EXCEPT_FPE: sigval = TARGET_SIGNAL_FPE; break;
//case EXCEPT_TRAP: sigval = TARGET_SIGNAL_TRAP; break;
default:
fprintf (stderr, "Warning: Unknown RSP exception %lu: Ignored\n", except);
return;
}
if ((0 != rsp.sigval) && (sigval != rsp.sigval))
{
fprintf (stderr, "Warning: RSP signal %d received while signal "
"%d pending: Pending exception replaced\n", sigval, rsp.sigval);
}
rsp.sigval = sigval; /* Save the signal value */
} /* rsp_exception () */
/*---------------------------------------------------------------------------*/
/*!Get a new client connection.
Blocks until the client connection is available.
A lot of this code is copied from remote_open in gdbserver remote-utils.c.
This involves setting up a socket to listen on a socket for attempted
connections from a single GDB instance (we couldn't be talking to multiple
GDBs at once!).
The service is specified either as a port number in the Or1ksim configuration
(parameter rsp_port in section debug, default 51000) or as a service name
in the constant OR1KSIM_RSP_SERVICE.
The protocol used for communication is specified in OR1KSIM_RSP_PROTOCOL. */
/*---------------------------------------------------------------------------*/
static void
rsp_get_client ()
{
int tmp_fd; /* Temporary descriptor for socket */
int optval; /* Socket options */
struct sockaddr_in sock_addr; /* Socket address */
socklen_t len; /* Size of the socket address */
///* 0 is used as the RSP port number to indicate that we should use the
// service name instead. */
//if (0 == config.debug.rsp_port)
// {
// struct servent *service =
//getservbyname (OR1KSIM_RSP_SERVICE, "tcp");
// if (NULL == service)
//{
// fprintf (stderr, "Warning: RSP unable to find service \"%s\": %s\n",
// OR1KSIM_RSP_SERVICE, strerror (errno));
// return;
//}
// rsp.port = ntohs (service->s_port);
// }
/* Open a socket on which we'll listen for clients */
tmp_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
//tmp_fd = socket (AF_INET, SOCK_STREAM, 0);
if (tmp_fd < 0)
{
fprintf (stderr, "ERROR: Cannot open RSP socket\n");
//sim_done ();
return;
}
/* Allow rapid reuse of the port on this socket */
optval = 1;
setsockopt (tmp_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
sizeof (optval));
/* Bind the port to the socket */
sock_addr.sin_family = PF_INET;
sock_addr.sin_port = htons (rsp.port);
sock_addr.sin_addr.s_addr = INADDR_ANY;
if (bind (tmp_fd, (struct sockaddr *) &sock_addr, sizeof (sock_addr)))
{
fprintf (stderr, "ERROR: Cannot bind to RSP socket\n");
//sim_done ();
return;
}
/* Listen for (at most one) client */
if (listen (tmp_fd, 1))
{
fprintf (stderr, "ERROR: Cannot listen on RSP socket\n");
//sim_done ();
return;
}
printf ("Listening for RSP on port %d\n", rsp.port);
fflush (stdout);
/* Accept a client which connects */
len = sizeof (socklen_t); /* Bug fix by Julius Baxter */
rsp.client_fd = accept (tmp_fd, (struct sockaddr *)&sock_addr, &len);
if (-1 == rsp.client_fd)
{
fprintf (stderr, "Warning: Failed to accept RSP client\n");
return;
}
/* Enable TCP keep alive process */
optval = 1;
setsockopt (rsp.client_fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval,
sizeof (optval));
/* Don't delay small packets, for better interactive response (disable
Nagel's algorithm) */
optval = 1;
setsockopt (rsp.client_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
sizeof (optval));
///* Socket is no longer needed */
close (tmp_fd); /* No longer need this */
signal (SIGPIPE, SIG_IGN); /* So we don't exit if client dies */
printf ("Remote debugging from host %s\n", inet_ntoa (sock_addr.sin_addr));
/* If fcntl () is available, use it to enable async I/O
#if defined(F_SETFL) && defined (FASYNC)
save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
#if defined (F_SETOWN)
fcntl (remote_desc, F_SETOWN, getpid ());
#endif
#endif */
} /* rsp_get_client () */
/*---------------------------------------------------------------------------*/
/*!Deal with a request from the GDB client session
In general, apart from the simplest requests, this function replies on
other functions to implement the functionality. */
/*---------------------------------------------------------------------------*/
static void
rsp_client_request ()
{
struct rsp_buf *buf = get_packet (); /* Message sent to us */
// Null packet means we hit EOF or the link was closed for some other
// reason. Close the client and return
if (NULL == buf)
{
rsp_client_close ();
return;
}
#if RSP_TRACE
printf ("Packet received %s: %d chars\n", buf->data, buf->len );
fflush (stdout);
#endif
switch (buf->data[0])
{
case '!':
/* Request for extended remote mode */
put_str_packet ("OK");
return;
case '?':
/* Return last signal ID */
rsp_report_exception();
return;
case 'A':
/* Initialization of argv not supported */
fprintf (stderr, "Warning: RSP 'A' packet not supported: ignored\n");
put_str_packet ("E01");
return;
case 'b':
/* Setting baud rate is deprecated */
fprintf (stderr, "Warning: RSP 'b' packet is deprecated and not "
"supported: ignored\n");
return;
case 'B':
/* Breakpoints should be set using Z packets */
fprintf (stderr, "Warning: RSP 'B' packet is deprecated (use 'Z'/'z' "
"packets instead): ignored\n");
return;
case 'c':
/* Continue */
rsp_continue (buf);
return;
case 'C':
/* Continue with signal */
fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
return;
//rsp_continue_with_signal (buf);
return;
case 'd':
/* Disable debug using a general query */
fprintf (stderr, "Warning: RSP 'd' packet is deprecated (define a 'Q' "
"packet instead: ignored\n");
return;
case 'D':
/* Detach GDB. Do this by closing the client. The rules say that
execution should continue. TODO. Is this really then intended
meaning? Or does it just mean that only vAttach will be recognized
after this? */
put_str_packet ("OK");
//rsp_client_close ();
//set_stall_state (0);
rsp.sigval = TARGET_SIGNAL_NONE; /* No signal now */
return;
case 'F':
/* File I/O is not currently supported */
fprintf (stderr, "Warning: RSP file I/O not currently supported: 'F' "
"packet ignored\n");
return;
case 'g':
rsp_read_all_regs ();
return;
case 'G':
fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
return;
//rsp_write_all_regs (buf);
return;
case 'H':
/* Set the thread number of subsequent operations. For now ignore
silently and just reply "OK" */
put_str_packet ("OK");
return;
case 'i':
/* Single instruction step */
fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
"stopped immediately\n");
rsp.client_waiting = 1; /* Stop reply will be sent */
return;
case 'I':
/* Single instruction step with signal */
fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
"stopped immediately\n");
rsp.client_waiting = 1; /* Stop reply will be sent */
return;
case 'k':
/* Kill request. Do nothing for now. */
return;
case 'm':
//fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
//return;
/* Read memory (symbolic) */
rsp_read_mem (buf);
return;
case 'M':
fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
return;
/* Write memory (symbolic) */
//rsp_write_mem (buf);
return;
case 'p':
/* Read a register */
rsp_read_reg (buf);
return;
case 'P':
/* Write a register */
rsp_write_reg (buf);
return;
case 'q':
//fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
/* Any one of a number of query packets */
rsp_query (buf);
return;
case 'Q':
fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
return;
/* Any one of a number of set packets */
//rsp_set (buf);
return;
case 'r':
/* Reset the system. Deprecated (use 'R' instead) */
fprintf (stderr, "Warning: RSP 'r' packet is deprecated (use 'R' "
"packet instead): ignored\n");
return;
case 'R':
fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
return;
/* Restart the program being debugged. */
//rsp_restart ();
return;
case 's':
/* Single step (one high level instruction). This could be hard without
DWARF2 info */
rsp_step (buf);
return;
case 'S':
fprintf (stderr, "Warning: RSP %c not supported: ignored\n", buf->data[0]);
return;
/* Single step (one high level instruction) with signal. This could be
hard without DWARF2 info */
//rsp_step_with_signal (buf);
return;
case 't':
/* Search. This is not well defined in the manual and for now we don't
support it. No response is defined. */
fprintf (stderr, "Warning: RSP 't' packet not supported: ignored\n");
return;
case 'T':
/* Is the thread alive. We are bare metal, so don't have a thread
context. The answer is always "OK". */
put_str_packet ("OK");
return;
case 'v':
/* Any one of a number of packets to control execution */
rsp_vpkt (buf);
return;
case 'X':
/* Write memory (binary) */
rsp_write_mem_bin (buf);
return;
case 'z':
/* Remove a breakpoint/watchpoint. */
rsp_remove_matchpoint (buf);
return;
case 'Z':
/* Insert a breakpoint/watchpoint. */
rsp_insert_matchpoint (buf);
return;
default:
/* Unknown commands are ignored */
fprintf (stderr, "Warning: Unknown RSP request %s\n", buf->data);
return;
}
} /* rsp_client_request () */
/*---------------------------------------------------------------------------*/
/*!Close the connection to the client if it is open */
/*---------------------------------------------------------------------------*/
static void
rsp_client_close ()
{
if (-1 != rsp.client_fd)
{
close (rsp.client_fd);
rsp.client_fd = -1;
}
} /* rsp_client_close () */
/*---------------------------------------------------------------------------*/
/*!Send a packet to the GDB client
Modeled on the stub version supplied with GDB. Put out the data preceded by
a '$', followed by a '#' and a one byte checksum. '$', '#', '*' and '}' are
escaped by preceding them with '}' and then XORing the character with
0x20.
@param[in] buf The data to send */
/*---------------------------------------------------------------------------*/
static void
put_packet (struct rsp_buf *buf)
{
int ch; /* Ack char */
/* Construct $<packet info>#<checksum>. Repeat until the GDB client
acknowledges satisfactory receipt. */
do
{
unsigned char checksum = 0; /* Computed checksum */
int count = 0; /* Index into the buffer */
#if RSP_TRACE
printf ("Putting %s\n", buf->data);
fflush (stdout);
#endif
put_rsp_char ('$'); /* Start char */
/* Body of the packet */
for (count = 0; count < buf->len; count++)
{
unsigned char ch = buf->data[count];
/* Check for escaped chars */
if (('$' == ch) || ('#' == ch) || ('*' == ch) || ('}' == ch))
{
ch ^= 0x20;
checksum += (unsigned char)'}';
put_rsp_char ('}');
}
checksum += ch;
put_rsp_char (ch);
}
put_rsp_char ('#'); /* End char */
/* Computed checksum */
put_rsp_char (hexchars[checksum >> 4]);
put_rsp_char (hexchars[checksum % 16]);
/* Check for ack of connection failure */
ch = get_rsp_char ();
if (-1 == ch)
{
return; /* Fail the put silently. */
}
}
while ('+' != ch);
} /* put_packet () */
/*---------------------------------------------------------------------------*/
/*!Convenience to put a constant string packet
param[in] str The text of the packet */
/*---------------------------------------------------------------------------*/
static void
put_str_packet (const char *str)
{
struct rsp_buf buf;
int len = strlen (str);
/* Construct the packet to send, so long as string is not too big,
otherwise truncate. Add EOS at the end for convenient debug printout */
if (len >= GDB_BUF_MAX)
{
fprintf (stderr, "Warning: String %s too large for RSP packet: "
"truncated\n", str);
len = GDB_BUF_MAX - 1;
}
strncpy (buf.data, str, len);
buf.data[len] = 0;
buf.len = len;
put_packet (&buf);
} /* put_str_packet () */
void rsp_trap()
{
put_str_packet("S05");
}
/*---------------------------------------------------------------------------*/
/*!Get a packet from the GDB client
Modeled on the stub version supplied with GDB. The data is in a static
buffer. The data should be copied elsewhere if it is to be preserved across
a subsequent call to get_packet().
Unlike the reference implementation, we don't deal with sequence
numbers. GDB has never used them, and this implementation is only intended
for use with GDB 6.8 or later. Sequence numbers were removed from the RSP
standard at GDB 5.0.
@return A pointer to the static buffer containing the data */
/*---------------------------------------------------------------------------*/
static struct rsp_buf *
get_packet ()
{
static struct rsp_buf buf; /* Survives the return */
/* Keep getting packets, until one is found with a valid checksum */
while (1)
{
unsigned char checksum; /* The checksum we have computed */
int count; /* Index into the buffer */
int ch; /* Current character */
/* Wait around for the start character ('$'). Ignore all other
characters */
ch = get_rsp_char ();
while (ch != '$')
{
if (-1 == ch)
{
return NULL; /* Connection failed */
}
ch = get_rsp_char ();
}
/* Read until a '#' or end of buffer is found */
checksum = 0;
count = 0;
while (count < GDB_BUF_MAX - 1)
{
ch = get_rsp_char ();
/* Check for connection failure */
if (-1 == ch)
{
return NULL;
}
/* If we hit a start of line char begin all over again */
if ('$' == ch)
{
checksum = 0;
count = 0;
continue;
}
/* Break out if we get the end of line char */
if ('#' == ch)
{
break;
}
/* Update the checksum and add the char to the buffer */
checksum = checksum + (unsigned char)ch;
buf.data[count] = (char)ch;
count = count + 1;
}
/* Mark the end of the buffer with EOS - it's convenient for non-binary
data to be valid strings. */
buf.data[count] = 0;
buf.len = count;
/* If we have a valid end of packet char, validate the checksum */
if ('#' == ch)
{
unsigned char xmitcsum; /* The checksum in the packet */
ch = get_rsp_char ();
if (-1 == ch)
{
return NULL; /* Connection failed */
}
xmitcsum = hex (ch) << 4;
ch = get_rsp_char ();
if (-1 == ch)
{
return NULL; /* Connection failed */
}
xmitcsum += hex (ch);
/* If the checksums don't match print a warning, and put the
negative ack back to the client. Otherwise put a positive ack. */
if (checksum != xmitcsum)
{
fprintf (stderr, "Warning: Bad RSP checksum: Computed "
"0x%02x, received 0x%02x\n", checksum, xmitcsum);
put_rsp_char ('-'); /* Failed checksum */
}
else
{
put_rsp_char ('+'); /* successful transfer */
break;
}
}
else
{
fprintf (stderr, "Warning: RSP packet overran buffer\n");
}
}
return &buf; /* Success */
} /* get_packet () */
/*---------------------------------------------------------------------------*/
/*!Put a single character out onto the client socket
This should only be called if the client is open, but we check for safety.
@param[in] c The character to put out */
/*---------------------------------------------------------------------------*/
static void
put_rsp_char (char c)
{
if (-1 == rsp.client_fd)
{
fprintf (stderr, "Warning: Attempt to write '%c' to unopened RSP "
"client: Ignored\n", c);
return;
}
/* Write until successful (we retry after interrupts) or catastrophic
failure. */
while (1)
{
switch (write (rsp.client_fd, &c, sizeof (c)))
{
case -1:
/* Error: only allow interrupts or would block */
if ((EAGAIN != errno) && (EINTR != errno))
{
fprintf (stderr, "Warning: Failed to write to RSP client: "
"Closing client connection: %s\n",
strerror (errno));
rsp_client_close ();
return;
}
break;
case 0:
break; /* Nothing written! Try again */
default:
return; /* Success, we can return */
}
}
} /* put_rsp_char () */
/*---------------------------------------------------------------------------*/
/*!Get a single character from the client socket
This should only be called if the client is open, but we check for safety.
@return The character read, or -1 on failure */
/*---------------------------------------------------------------------------*/
static int
get_rsp_char ()
{
if (-1 == rsp.client_fd)
{
fprintf (stderr, "Warning: Attempt to read from unopened RSP "
"client: Ignored\n");
return -1;
}
/* Non-blocking read until successful (we retry after interrupts) or
catastrophic failure. */
while (1)
{
unsigned char c;
switch (read (rsp.client_fd, &c, sizeof (c)))
{
case -1:
/* Error: only allow interrupts */
if ((EAGAIN != errno) && (EINTR != errno))
{
fprintf (stderr, "Warning: Failed to read from RSP client: "
"Closing client connection: %s\n",
strerror (errno));
rsp_client_close ();
return -1;
}
break;
case 0:
// EOF
rsp_client_close ();
return -1;
default:
return c & 0xff; /* Success, we can return (no sign extend!) */
}
}
} /* get_rsp_char () */
/*---------------------------------------------------------------------------*/
/*!"Unescape" RSP binary data
'#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
This function reverses that, modifying the data in place.
@param[in] data The array of bytes to convert
@para[in] len The number of bytes to be converted
@return The number of bytes AFTER conversion */
/*---------------------------------------------------------------------------*/
static int
rsp_unescape (char *data,
int len)
{
int from_off = 0; /* Offset to source char */