1
2
3
4
5
6
7
8
9
10
11
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <errno.h>
16
17
18
19#ifndef alloca
20#ifdef __GNUC__
21#define alloca __builtin_alloca
22#else
23#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi)
24#include <alloca.h>
25#else
26#if defined (MSDOS) && !defined (__TURBOC__)
27#include <malloc.h>
28#else
29#if defined(_AIX)
30#include <malloc.h>
31 #pragma alloca
32#else
33#ifdef __hpux
34#endif
35#endif
36#endif
37#endif
38#endif
39#ifdef __cplusplus
40extern "C" {
41#endif
42 void* alloca(size_t);
43#ifdef __cplusplus
44}
45#endif
46#endif
47
48
49#include "serial.h"
50#include "error.h"
51#include "remote.h"
52#define REGISTER_BYTES 0
53#define fprintf_unfiltered fprintf
54#define fprintf_filtered fprintf
55#define fputs_unfiltered fputs
56#define fputs_filtered fputs
57#define fputc_unfiltered fputc
58#define fputc_filtered fputc
59#define printf_unfiltered printf
60#define printf_filtered printf
61#define puts_unfiltered puts
62#define puts_filtered puts
63#define putchar_unfiltered putchar
64#define putchar_filtered putchar
65#define fputstr_unfiltered(a,b,c) fputs((a), (c))
66#define gdb_stdlog stderr
67#define SERIAL_READCHAR(fd,timo) serialreadchar((fd), (timo))
68#define SERIAL_WRITE(fd, addr, len) serialwrite((fd), (addr), (len))
69#define error Error
70#define perror_with_name Perror
71#define gdb_flush fflush
72#define max(a,b) (((a)>(b))?(a):(b))
73#define min(a,b) (((a)<(b))?(a):(b))
74#define target_mourn_inferior() {}
75#define ULONGEST unsigned long
76#define CORE_ADDR unsigned long
77
78static int putpkt (char *);
79static int putpkt_binary(char *, int);
80static void getpkt (char *, int);
81
82static int remote_debug = 0, remote_register_buf_size = 0, watchdog = 0;
83
84int remote_desc = -1, remote_timeout = 10;
85
86static void
87fputstrn_unfiltered(char *s, int n, int x, FILE *fp)
88{
89 while (n-- > 0)
90 fputc(*s++, fp);
91}
92
93void
94remote_reset(void)
95{
96 SERIAL_WRITE(remote_desc, "+", 1);
97}
98
99void
100remote_continue(void)
101{
102 putpkt("c");
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
297static int remote_binary_download = 1;
298
299
300static int remote_binary_checked;
301
302
303
304#define MAXBUFBYTES(N) (((N)-32)/2)
305
306
307
308
309
310
311
312#define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
313 ? (REGISTER_BYTES * 2 + 32) \
314 : 400)
315
316
317
318
319
320
321
322static int remote_write_size = 0x7fffffff;
323
324
325
326
327
328
329
330
331
332
333
334
335static int remote_address_size;
336
337
338
339static int
340fromhex (int a)
341{
342 if (a >= '0' && a <= '9')
343 return a - '0';
344 else if (a >= 'a' && a <= 'f')
345 return a - 'a' + 10;
346 else if (a >= 'A' && a <= 'F')
347 return a - 'A' + 10;
348 else {
349 error ("Reply contains invalid hex digit %d", a);
350 return -1;
351 }
352}
353
354
355
356static int
357tohex (int nib)
358{
359 if (nib < 10)
360 return '0' + nib;
361 else
362 return 'a' + nib - 10;
363}
364
365
366
367static int
368hexnumlen (ULONGEST num)
369{
370 int i;
371
372 for (i = 0; num != 0; i++)
373 num >>= 4;
374
375 return max (i, 1);
376}
377
378
379
380static int
381hexnumstr (char *buf, ULONGEST num)
382{
383 int i;
384 int len = hexnumlen (num);
385
386 buf[len] = '\0';
387
388 for (i = len - 1; i >= 0; i--)
389 {
390 buf[i] = "0123456789abcdef"[(num & 0xf)];
391 num >>= 4;
392 }
393
394 return len;
395}
396
397
398
399static CORE_ADDR
400remote_address_masked (CORE_ADDR addr)
401{
402 if (remote_address_size > 0
403 && remote_address_size < (sizeof (ULONGEST) * 8))
404 {
405
406
407 ULONGEST mask = 1;
408 mask = (mask << remote_address_size) - 1;
409 addr &= mask;
410 }
411 return addr;
412}
413
414
415
416
417
418
419
420
421
422static void
423check_binary_download (CORE_ADDR addr)
424{
425 if (remote_binary_download && !remote_binary_checked)
426 {
427 char *buf = alloca (PBUFSIZ);
428 char *p;
429 remote_binary_checked = 1;
430
431 p = buf;
432 *p++ = 'X';
433 p += hexnumstr (p, (ULONGEST) addr);
434 *p++ = ',';
435 p += hexnumstr (p, (ULONGEST) 0);
436 *p++ = ':';
437 *p = '\0';
438
439 putpkt_binary (buf, (int) (p - buf));
440 getpkt (buf, 0);
441
442 if (buf[0] == '\0')
443 remote_binary_download = 0;
444 }
445
446 if (remote_debug)
447 {
448 if (remote_binary_download)
449 fprintf_unfiltered (gdb_stdlog,
450 "binary downloading suppported by target\n");
451 else
452 fprintf_unfiltered (gdb_stdlog,
453 "binary downloading NOT suppported by target\n");
454 }
455}
456
457
458
459
460
461
462
463
464
465int
466remote_write_bytes (memaddr, myaddr, len)
467 CORE_ADDR memaddr;
468 char *myaddr;
469 int len;
470{
471 unsigned char *buf = alloca (PBUFSIZ);
472 int max_buf_size;
473 int origlen;
474 extern int verbose;
475
476
477 check_binary_download (memaddr);
478
479
480
481 max_buf_size = min (remote_write_size, PBUFSIZ);
482 if (remote_register_buf_size != 0)
483 max_buf_size = min (max_buf_size, remote_register_buf_size);
484
485
486 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
487
488 origlen = len;
489 while (len > 0)
490 {
491 unsigned char *p, *plen;
492 int todo;
493 int i;
494
495
496
497 memaddr = remote_address_masked (memaddr);
498 p = buf;
499 if (remote_binary_download)
500 {
501 *p++ = 'X';
502 todo = min (len, max_buf_size);
503 }
504 else
505 {
506 *p++ = 'M';
507 todo = min (len, max_buf_size / 2);
508 }
509
510 p += hexnumstr ((char *)p, (ULONGEST) memaddr);
511 *p++ = ',';
512
513 plen = p;
514 p += hexnumstr ((char *)p, (ULONGEST) todo);
515 *p++ = ':';
516 *p = '\0';
517
518
519
520
521 if (remote_binary_download)
522 {
523 int escaped = 0;
524 for (i = 0;
525 (i < todo) && (i + escaped) < (max_buf_size - 2);
526 i++)
527 {
528 switch (myaddr[i] & 0xff)
529 {
530 case '$':
531 case '#':
532 case 0x7d:
533
534 escaped++;
535 *p++ = 0x7d;
536 *p++ = (myaddr[i] & 0xff) ^ 0x20;
537 break;
538 default:
539 *p++ = myaddr[i] & 0xff;
540 break;
541 }
542 }
543
544 if (i < todo)
545 {
546
547
548
549
550
551
552
553 plen += hexnumstr ((char *)plen, (ULONGEST) i);
554 *plen++ = ':';
555 }
556 }
557 else
558 {
559 for (i = 0; i < todo; i++)
560 {
561 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
562 *p++ = tohex (myaddr[i] & 0xf);
563 }
564 *p = '\0';
565 }
566
567 putpkt_binary ((char *)buf, (int) (p - buf));
568 getpkt ((char *)buf, 0);
569
570 if (buf[0] == 'E')
571 {
572
573
574
575
576 errno = EIO;
577 return 0;
578 }
579
580
581
582 myaddr += i;
583 memaddr += i;
584 len -= i;
585
586 if (verbose)
587 putc('.', stderr);
588 }
589 return origlen;
590}
591
592
593
594
595
596
597static int
598readchar (int timeout)
599{
600 int ch;
601
602 ch = SERIAL_READCHAR (remote_desc, timeout);
603
604 switch (ch)
605 {
606 case SERIAL_EOF:
607 error ("Remote connection closed");
608 case SERIAL_ERROR:
609 perror_with_name ("Remote communication error");
610 case SERIAL_TIMEOUT:
611 return ch;
612 default:
613 return ch & 0x7f;
614 }
615}
616
617static int
618putpkt (buf)
619 char *buf;
620{
621 return putpkt_binary (buf, strlen (buf));
622}
623
624
625
626
627
628
629static int
630putpkt_binary (buf, cnt)
631 char *buf;
632 int cnt;
633{
634 int i;
635 unsigned char csum = 0;
636 char *buf2 = alloca (PBUFSIZ);
637 char *junkbuf = alloca (PBUFSIZ);
638
639 int ch;
640 int tcount = 0;
641 char *p;
642
643
644
645
646 if (cnt > BUFSIZ - 5)
647 abort ();
648
649 p = buf2;
650 *p++ = '$';
651
652 for (i = 0; i < cnt; i++)
653 {
654 csum += buf[i];
655 *p++ = buf[i];
656 }
657 *p++ = '#';
658 *p++ = tohex ((csum >> 4) & 0xf);
659 *p++ = tohex (csum & 0xf);
660
661
662
663 while (1)
664 {
665 int started_error_output = 0;
666
667 if (remote_debug)
668 {
669 *p = '\0';
670 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
671 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
672 fprintf_unfiltered (gdb_stdlog, "...");
673 gdb_flush (gdb_stdlog);
674 }
675 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
676 perror_with_name ("putpkt: write failed");
677
678
679 while (1)
680 {
681 ch = readchar (remote_timeout);
682
683 if (remote_debug)
684 {
685 switch (ch)
686 {
687 case '+':
688 case SERIAL_TIMEOUT:
689 case '$':
690 if (started_error_output)
691 {
692 putchar_unfiltered ('\n');
693 started_error_output = 0;
694 }
695 }
696 }
697
698 switch (ch)
699 {
700 case '+':
701 if (remote_debug)
702 fprintf_unfiltered (gdb_stdlog, "Ack\n");
703 return 1;
704 case SERIAL_TIMEOUT:
705 tcount++;
706 if (tcount > 3)
707 return 0;
708 break;
709 case '$':
710 {
711
712
713 getpkt (junkbuf, 0);
714 continue;
715 }
716 default:
717 if (remote_debug)
718 {
719 if (!started_error_output)
720 {
721 started_error_output = 1;
722 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
723 }
724 fputc_unfiltered (ch & 0177, gdb_stdlog);
725 }
726 continue;
727 }
728 break;
729 }
730
731#if 0
732
733
734
735
736
737 if (quit_flag)
738 {
739 quit_flag = 0;
740 interrupt_query ();
741 }
742#endif
743 }
744}
745
746
747
748
749
750static int
751read_frame (char *buf)
752{
753 unsigned char csum;
754 char *bp;
755 int c;
756
757 csum = 0;
758 bp = buf;
759
760 while (1)
761 {
762 c = readchar (remote_timeout);
763
764 switch (c)
765 {
766 case SERIAL_TIMEOUT:
767 if (remote_debug)
768 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
769 return 0;
770 case '$':
771 if (remote_debug)
772 fputs_filtered ("Saw new packet start in middle of old one\n",
773 gdb_stdlog);
774 return 0;
775 case '#':
776 {
777 unsigned char pktcsum;
778
779 *bp = '\000';
780
781 pktcsum = fromhex (readchar (remote_timeout)) << 4;
782 pktcsum |= fromhex (readchar (remote_timeout));
783
784 if (csum == pktcsum)
785 {
786 return 1;
787 }
788
789 if (remote_debug)
790 {
791 fprintf_filtered (gdb_stdlog,
792 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
793 pktcsum, csum);
794 fputs_filtered (buf, gdb_stdlog);
795 fputs_filtered ("\n", gdb_stdlog);
796 }
797 return 0;
798 }
799 case '*':
800 csum += c;
801 c = readchar (remote_timeout);
802 csum += c;
803 c = c - ' ' + 3;
804
805 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
806 {
807 memset (bp, *(bp - 1), c);
808 bp += c;
809 continue;
810 }
811
812 *bp = '\0';
813 printf_filtered ("Repeat count %d too large for buffer: ", c);
814 puts_filtered (buf);
815 puts_filtered ("\n");
816 return 0;
817 default:
818 if (bp < buf + PBUFSIZ - 1)
819 {
820 *bp++ = c;
821 csum += c;
822 continue;
823 }
824
825 *bp = '\0';
826 puts_filtered ("Remote packet too long: ");
827 puts_filtered (buf);
828 puts_filtered ("\n");
829
830 return 0;
831 }
832 }
833}
834
835
836
837
838
839
840static void
841getpkt (buf, forever)
842 char *buf;
843 int forever;
844{
845 int c;
846 int tries;
847 int timeout;
848 int val;
849
850 strcpy (buf, "timeout");
851
852 if (forever)
853 {
854 timeout = watchdog > 0 ? watchdog : -1;
855 }
856
857 else
858 timeout = remote_timeout;
859
860#define MAX_TRIES 3
861
862 for (tries = 1; tries <= MAX_TRIES; tries++)
863 {
864
865
866
867
868
869
870
871
872 do
873 {
874 c = readchar (timeout);
875
876 if (c == SERIAL_TIMEOUT)
877 {
878 if (forever)
879 {
880 target_mourn_inferior ();
881 error ("Watchdog has expired. Target detached.\n");
882 }
883 if (remote_debug)
884 fputs_filtered ("Timed out.\n", gdb_stdlog);
885 goto retry;
886 }
887 }
888 while (c != '$');
889
890
891
892 val = read_frame (buf);
893
894 if (val == 1)
895 {
896 if (remote_debug)
897 {
898 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
899 fputstr_unfiltered (buf, 0, gdb_stdlog);
900 fprintf_unfiltered (gdb_stdlog, "\n");
901 }
902 SERIAL_WRITE (remote_desc, "+", 1);
903 return;
904 }
905
906
907 retry:
908 SERIAL_WRITE (remote_desc, "-", 1);
909 }
910
911
912
913 printf_unfiltered ("Ignoring packet error, continuing...\n");
914 SERIAL_WRITE (remote_desc, "+", 1);
915}
916