blob: f17bb06fb4c1fb1ca8a27e0337f0b53fabbe5005 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk4a5b6a32001-04-28 17:59:11 +00002/*
3 * taken from gdb/remote.c
4 *
5 * I am only interested in the write to memory stuff - everything else
6 * has been ripped out
7 *
8 * all the copyright notices etc have been left in
9 */
10
11/* enough so that it will compile */
12#include <stdio.h>
13#include <stdlib.h>
14#include <string.h>
15#include <errno.h>
16
17/*nicked from gcc..*/
18
19#ifndef alloca
20#ifdef __GNUC__
21#define alloca __builtin_alloca
22#else /* not GNU C. */
23#if (!defined (__STDC__) && defined (sparc)) || defined (__sparc__) || defined (__sparc) || defined (__sgi)
24#include <alloca.h>
25#else /* not sparc */
26#if defined (MSDOS) && !defined (__TURBOC__)
27#include <malloc.h>
28#else /* not MSDOS, or __TURBOC__ */
29#if defined(_AIX)
30#include <malloc.h>
31 #pragma alloca
32#else /* not MSDOS, __TURBOC__, or _AIX */
33#ifdef __hpux
34#endif /* __hpux */
35#endif /* not _AIX */
36#endif /* not MSDOS, or __TURBOC__ */
37#endif /* not sparc. */
38#endif /* not GNU C. */
39#ifdef __cplusplus
40extern "C" {
41#endif
42 void* alloca(size_t);
43#ifdef __cplusplus
44}
45#endif
46#endif /* alloca not defined. */
47
wdenk4a5b6a32001-04-28 17:59:11 +000048#include "serial.h"
49#include "error.h"
50#include "remote.h"
51#define REGISTER_BYTES 0
52#define fprintf_unfiltered fprintf
53#define fprintf_filtered fprintf
54#define fputs_unfiltered fputs
55#define fputs_filtered fputs
56#define fputc_unfiltered fputc
57#define fputc_filtered fputc
58#define printf_unfiltered printf
59#define printf_filtered printf
60#define puts_unfiltered puts
61#define puts_filtered puts
62#define putchar_unfiltered putchar
63#define putchar_filtered putchar
64#define fputstr_unfiltered(a,b,c) fputs((a), (c))
65#define gdb_stdlog stderr
66#define SERIAL_READCHAR(fd,timo) serialreadchar((fd), (timo))
67#define SERIAL_WRITE(fd, addr, len) serialwrite((fd), (addr), (len))
68#define error Error
69#define perror_with_name Perror
70#define gdb_flush fflush
71#define max(a,b) (((a)>(b))?(a):(b))
72#define min(a,b) (((a)<(b))?(a):(b))
73#define target_mourn_inferior() {}
74#define ULONGEST unsigned long
75#define CORE_ADDR unsigned long
76
77static int putpkt (char *);
78static int putpkt_binary(char *, int);
79static void getpkt (char *, int);
80
81static int remote_debug = 0, remote_register_buf_size = 0, watchdog = 0;
82
83int remote_desc = -1, remote_timeout = 10;
84
85static void
86fputstrn_unfiltered(char *s, int n, int x, FILE *fp)
87{
88 while (n-- > 0)
89 fputc(*s++, fp);
90}
91
92void
93remote_reset(void)
94{
95 SERIAL_WRITE(remote_desc, "+", 1);
96}
97
98void
99remote_continue(void)
100{
101 putpkt("c");
102}
103
104/* Remote target communications for serial-line targets in custom GDB protocol
105 Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 98, 1999
106 Free Software Foundation, Inc.
107
108 This file is part of GDB.
Wolfgang Denkd79de1d2013-07-08 09:37:19 +0200109 */
wdenk4a5b6a32001-04-28 17:59:11 +0000110/* *INDENT-OFF* */
111/* Remote communication protocol.
112
113 A debug packet whose contents are <data>
114 is encapsulated for transmission in the form:
115
116 $ <data> # CSUM1 CSUM2
117
118 <data> must be ASCII alphanumeric and cannot include characters
119 '$' or '#'. If <data> starts with two characters followed by
120 ':', then the existing stubs interpret this as a sequence number.
121
122 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
123 checksum of <data>, the most significant nibble is sent first.
124 the hex digits 0-9,a-f are used.
125
126 Receiver responds with:
127
128 + - if CSUM is correct and ready for next packet
129 - - if CSUM is incorrect
130
131 <data> is as follows:
132 Most values are encoded in ascii hex digits. Signal numbers are according
133 to the numbering in target.h.
134
135 Request Packet
136
137 set thread Hct... Set thread for subsequent operations.
138 c = 'c' for thread used in step and
139 continue; t... can be -1 for all
140 threads.
141 c = 'g' for thread used in other
142 operations. If zero, pick a thread,
143 any thread.
144 reply OK for success
145 ENN for an error.
146
147 read registers g
148 reply XX....X Each byte of register data
149 is described by two hex digits.
150 Registers are in the internal order
151 for GDB, and the bytes in a register
152 are in the same order the machine uses.
153 or ENN for an error.
154
155 write regs GXX..XX Each byte of register data
156 is described by two hex digits.
157 reply OK for success
158 ENN for an error
159
wdenk57b2d802003-06-27 21:31:46 +0000160 write reg Pn...=r... Write register n... with value r...,
wdenk4a5b6a32001-04-28 17:59:11 +0000161 which contains two hex digits for each
162 byte in the register (target byte
163 order).
164 reply OK for success
165 ENN for an error
166 (not supported by all stubs).
167
168 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
169 reply XX..XX XX..XX is mem contents
170 Can be fewer bytes than requested
171 if able to read only part of the data.
172 or ENN NN is errno
173
174 write mem MAA..AA,LLLL:XX..XX
175 AA..AA is address,
176 LLLL is number of bytes,
177 XX..XX is data
178 reply OK for success
179 ENN for an error (this includes the case
180 where only part of the data was
181 written).
182
wdenk57b2d802003-06-27 21:31:46 +0000183 write mem XAA..AA,LLLL:XX..XX
184 (binary) AA..AA is address,
185 LLLL is number of bytes,
186 XX..XX is binary data
187 reply OK for success
188 ENN for an error
wdenk4a5b6a32001-04-28 17:59:11 +0000189
190 continue cAA..AA AA..AA is address to resume
191 If AA..AA is omitted,
192 resume at same address.
193
194 step sAA..AA AA..AA is address to resume
195 If AA..AA is omitted,
196 resume at same address.
197
198 continue with Csig;AA..AA Continue with signal sig (hex signal
199 signal number). If ;AA..AA is omitted,
200 resume at same address.
201
202 step with Ssig;AA..AA Like 'C' but step not continue.
203 signal
204
205 last signal ? Reply the current reason for stopping.
wdenk57b2d802003-06-27 21:31:46 +0000206 This is the same reply as is generated
wdenk4a5b6a32001-04-28 17:59:11 +0000207 for step or cont : SAA where AA is the
208 signal number.
209
210 detach D Reply OK.
211
212 There is no immediate reply to step or cont.
213 The reply comes when the machine stops.
214 It is SAA AA is the signal number.
215
216 or... TAAn...:r...;n...:r...;n...:r...;
217 AA = signal number
218 n... = register number (hex)
219 r... = register contents
220 n... = `thread'
221 r... = thread process ID. This is
222 a hex integer.
223 n... = other string not starting
224 with valid hex digit.
225 gdb should ignore this n,r pair
226 and go on to the next. This way
227 we can extend the protocol.
228 or... WAA The process exited, and AA is
229 the exit status. This is only
230 applicable for certains sorts of
231 targets.
232 or... XAA The process terminated with signal
233 AA.
234 or (obsolete) NAA;tttttttt;dddddddd;bbbbbbbb
235 AA = signal number
236 tttttttt = address of symbol "_start"
237 dddddddd = base of data section
238 bbbbbbbb = base of bss section.
239 Note: only used by Cisco Systems
240 targets. The difference between this
241 reply and the "qOffsets" query is that
242 the 'N' packet may arrive spontaneously
243 whereas the 'qOffsets' is a query
244 initiated by the host debugger.
wdenk57b2d802003-06-27 21:31:46 +0000245 or... OXX..XX XX..XX is hex encoding of ASCII data. This
wdenk4a5b6a32001-04-28 17:59:11 +0000246 can happen at any time while the
247 program is running and the debugger
248 should continue to wait for
249 'W', 'T', etc.
250
251 thread alive TXX Find out if the thread XX is alive.
252 reply OK thread is still alive
253 ENN thread is dead
254
255 remote restart RXX Restart the remote server
256
Wolfgang Denka1be4762008-05-20 16:00:29 +0200257 extended ops ! Use the extended remote protocol.
wdenk4a5b6a32001-04-28 17:59:11 +0000258 Sticky -- only needs to be set once.
259
260 kill request k
261
262 toggle debug d toggle debug flag (see 386 & 68k stubs)
263 reset r reset -- see sparc stub.
264 reserved <other> On other requests, the stub should
265 ignore the request and send an empty
266 response ($#<checksum>). This way
267 we can extend the protocol and GDB
268 can tell whether the stub it is
269 talking to uses the old or the new.
270 search tAA:PP,MM Search backwards starting at address
271 AA for a match with pattern PP and
272 mask MM. PP and MM are 4 bytes.
273 Not supported by all stubs.
274
275 general query qXXXX Request info about XXXX.
276 general set QXXXX=yyyy Set value of XXXX to yyyy.
277 query sect offs qOffsets Get section offsets. Reply is
278 Text=xxx;Data=yyy;Bss=zzz
279
280 Responses can be run-length encoded to save space. A '*' means that
281 the next character is an ASCII encoding giving a repeat count which
282 stands for that many repititions of the character preceding the '*'.
283 The encoding is n+29, yielding a printable character where n >=3
284 (which is where rle starts to win). Don't use an n > 126.
285
286 So
287 "0* " means the same as "0000". */
288/* *INDENT-ON* */
289
290/* This variable (available to the user via "set remotebinarydownload")
291 dictates whether downloads are sent in binary (via the 'X' packet).
292 We assume that the stub can, and attempt to do it. This will be cleared if
293 the stub does not understand it. This switch is still needed, though
294 in cases when the packet is supported in the stub, but the connection
295 does not allow it (i.e., 7-bit serial connection only). */
296static int remote_binary_download = 1;
297
298/* Have we already checked whether binary downloads work? */
299static int remote_binary_checked;
300
301/* Maximum number of bytes to read/write at once. The value here
302 is chosen to fill up a packet (the headers account for the 32). */
303#define MAXBUFBYTES(N) (((N)-32)/2)
304
305/* Having this larger than 400 causes us to be incompatible with m68k-stub.c
306 and i386-stub.c. Normally, no one would notice because it only matters
307 for writing large chunks of memory (e.g. in downloads). Also, this needs
308 to be more than 400 if required to hold the registers (see below, where
309 we round it up based on REGISTER_BYTES). */
310/* Round up PBUFSIZ to hold all the registers, at least. */
311#define PBUFSIZ ((REGISTER_BYTES > MAXBUFBYTES (400)) \
312 ? (REGISTER_BYTES * 2 + 32) \
313 : 400)
314
wdenk4a5b6a32001-04-28 17:59:11 +0000315/* This variable sets the number of bytes to be written to the target
316 in a single packet. Normally PBUFSIZ is satisfactory, but some
317 targets need smaller values (perhaps because the receiving end
318 is slow). */
319
320static int remote_write_size = 0x7fffffff;
321
322/* This variable sets the number of bits in an address that are to be
323 sent in a memory ("M" or "m") packet. Normally, after stripping
324 leading zeros, the entire address would be sent. This variable
325 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
326 initial implementation of remote.c restricted the address sent in
327 memory packets to ``host::sizeof long'' bytes - (typically 32
328 bits). Consequently, for 64 bit targets, the upper 32 bits of an
329 address was never sent. Since fixing this bug may cause a break in
330 some remote targets this variable is principly provided to
331 facilitate backward compatibility. */
332
333static int remote_address_size;
334
335/* Convert hex digit A to a number. */
336
337static int
338fromhex (int a)
339{
340 if (a >= '0' && a <= '9')
341 return a - '0';
342 else if (a >= 'a' && a <= 'f')
343 return a - 'a' + 10;
344 else if (a >= 'A' && a <= 'F')
345 return a - 'A' + 10;
346 else {
347 error ("Reply contains invalid hex digit %d", a);
348 return -1;
349 }
350}
351
352/* Convert number NIB to a hex digit. */
353
354static int
355tohex (int nib)
356{
357 if (nib < 10)
358 return '0' + nib;
359 else
360 return 'a' + nib - 10;
361}
362
363/* Return the number of hex digits in num. */
364
365static int
366hexnumlen (ULONGEST num)
367{
368 int i;
369
370 for (i = 0; num != 0; i++)
371 num >>= 4;
372
373 return max (i, 1);
374}
375
376/* Set BUF to the hex digits representing NUM. */
377
378static int
379hexnumstr (char *buf, ULONGEST num)
380{
381 int i;
382 int len = hexnumlen (num);
383
384 buf[len] = '\0';
385
386 for (i = len - 1; i >= 0; i--)
387 {
388 buf[i] = "0123456789abcdef"[(num & 0xf)];
389 num >>= 4;
390 }
391
392 return len;
393}
394
395/* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
396
397static CORE_ADDR
398remote_address_masked (CORE_ADDR addr)
399{
400 if (remote_address_size > 0
401 && remote_address_size < (sizeof (ULONGEST) * 8))
402 {
403 /* Only create a mask when that mask can safely be constructed
wdenk57b2d802003-06-27 21:31:46 +0000404 in a ULONGEST variable. */
wdenk4a5b6a32001-04-28 17:59:11 +0000405 ULONGEST mask = 1;
406 mask = (mask << remote_address_size) - 1;
407 addr &= mask;
408 }
409 return addr;
410}
411
412/* Determine whether the remote target supports binary downloading.
413 This is accomplished by sending a no-op memory write of zero length
414 to the target at the specified address. It does not suffice to send
415 the whole packet, since many stubs strip the eighth bit and subsequently
416 compute a wrong checksum, which causes real havoc with remote_write_bytes.
417
418 NOTE: This can still lose if the serial line is not eight-bit clean. In
419 cases like this, the user should clear "remotebinarydownload". */
420static void
421check_binary_download (CORE_ADDR addr)
422{
423 if (remote_binary_download && !remote_binary_checked)
424 {
425 char *buf = alloca (PBUFSIZ);
426 char *p;
427 remote_binary_checked = 1;
428
429 p = buf;
430 *p++ = 'X';
431 p += hexnumstr (p, (ULONGEST) addr);
432 *p++ = ',';
433 p += hexnumstr (p, (ULONGEST) 0);
434 *p++ = ':';
435 *p = '\0';
436
437 putpkt_binary (buf, (int) (p - buf));
438 getpkt (buf, 0);
439
440 if (buf[0] == '\0')
441 remote_binary_download = 0;
442 }
443
444 if (remote_debug)
445 {
446 if (remote_binary_download)
447 fprintf_unfiltered (gdb_stdlog,
448 "binary downloading suppported by target\n");
449 else
450 fprintf_unfiltered (gdb_stdlog,
451 "binary downloading NOT suppported by target\n");
452 }
453}
454
455/* Write memory data directly to the remote machine.
456 This does not inform the data cache; the data cache uses this.
457 MEMADDR is the address in the remote memory space.
458 MYADDR is the address of the buffer in our space.
459 LEN is the number of bytes.
460
461 Returns number of bytes transferred, or 0 for error. */
462
463int
464remote_write_bytes (memaddr, myaddr, len)
465 CORE_ADDR memaddr;
466 char *myaddr;
467 int len;
468{
469 unsigned char *buf = alloca (PBUFSIZ);
470 int max_buf_size; /* Max size of packet output buffer */
471 int origlen;
472 extern int verbose;
473
474 /* Verify that the target can support a binary download */
475 check_binary_download (memaddr);
476
477 /* Chop the transfer down if necessary */
478
479 max_buf_size = min (remote_write_size, PBUFSIZ);
480 if (remote_register_buf_size != 0)
481 max_buf_size = min (max_buf_size, remote_register_buf_size);
482
483 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
484 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
485
486 origlen = len;
487 while (len > 0)
488 {
489 unsigned char *p, *plen;
490 int todo;
491 int i;
492
493 /* construct "M"<memaddr>","<len>":" */
494 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
495 memaddr = remote_address_masked (memaddr);
496 p = buf;
497 if (remote_binary_download)
498 {
499 *p++ = 'X';
500 todo = min (len, max_buf_size);
501 }
502 else
503 {
504 *p++ = 'M';
505 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
506 }
507
508 p += hexnumstr ((char *)p, (ULONGEST) memaddr);
509 *p++ = ',';
510
511 plen = p; /* remember where len field goes */
512 p += hexnumstr ((char *)p, (ULONGEST) todo);
513 *p++ = ':';
514 *p = '\0';
515
516 /* We send target system values byte by byte, in increasing byte
wdenk57b2d802003-06-27 21:31:46 +0000517 addresses, each byte encoded as two hex characters (or one
518 binary character). */
wdenk4a5b6a32001-04-28 17:59:11 +0000519 if (remote_binary_download)
520 {
521 int escaped = 0;
522 for (i = 0;
523 (i < todo) && (i + escaped) < (max_buf_size - 2);
524 i++)
525 {
526 switch (myaddr[i] & 0xff)
527 {
528 case '$':
529 case '#':
530 case 0x7d:
531 /* These must be escaped */
532 escaped++;
533 *p++ = 0x7d;
534 *p++ = (myaddr[i] & 0xff) ^ 0x20;
535 break;
536 default:
537 *p++ = myaddr[i] & 0xff;
538 break;
539 }
540 }
541
542 if (i < todo)
543 {
544 /* Escape chars have filled up the buffer prematurely,
wdenk57b2d802003-06-27 21:31:46 +0000545 and we have actually sent fewer bytes than planned.
546 Fix-up the length field of the packet. */
wdenk4a5b6a32001-04-28 17:59:11 +0000547
548 /* FIXME: will fail if new len is a shorter string than
wdenk57b2d802003-06-27 21:31:46 +0000549 old len. */
wdenk4a5b6a32001-04-28 17:59:11 +0000550
551 plen += hexnumstr ((char *)plen, (ULONGEST) i);
552 *plen++ = ':';
553 }
554 }
555 else
556 {
557 for (i = 0; i < todo; i++)
558 {
559 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
560 *p++ = tohex (myaddr[i] & 0xf);
561 }
562 *p = '\0';
563 }
564
565 putpkt_binary ((char *)buf, (int) (p - buf));
566 getpkt ((char *)buf, 0);
567
568 if (buf[0] == 'E')
569 {
570 /* There is no correspondance between what the remote protocol uses
571 for errors and errno codes. We would like a cleaner way of
572 representing errors (big enough to include errno codes, bfd_error
573 codes, and others). But for now just return EIO. */
574 errno = EIO;
575 return 0;
576 }
577
578 /* Increment by i, not by todo, in case escape chars
wdenk57b2d802003-06-27 21:31:46 +0000579 caused us to send fewer bytes than we'd planned. */
wdenk4a5b6a32001-04-28 17:59:11 +0000580 myaddr += i;
581 memaddr += i;
582 len -= i;
583
584 if (verbose)
585 putc('.', stderr);
586 }
587 return origlen;
588}
589
590/* Stuff for dealing with the packets which are part of this protocol.
591 See comment at top of file for details. */
592
593/* Read a single character from the remote end, masking it down to 7 bits. */
594
595static int
596readchar (int timeout)
597{
598 int ch;
599
600 ch = SERIAL_READCHAR (remote_desc, timeout);
601
602 switch (ch)
603 {
604 case SERIAL_EOF:
605 error ("Remote connection closed");
606 case SERIAL_ERROR:
607 perror_with_name ("Remote communication error");
608 case SERIAL_TIMEOUT:
609 return ch;
610 default:
611 return ch & 0x7f;
612 }
613}
614
615static int
616putpkt (buf)
617 char *buf;
618{
619 return putpkt_binary (buf, strlen (buf));
620}
621
622/* Send a packet to the remote machine, with error checking. The data
623 of the packet is in BUF. The string in BUF can be at most PBUFSIZ - 5
624 to account for the $, # and checksum, and for a possible /0 if we are
625 debugging (remote_debug) and want to print the sent packet as a string */
626
627static int
628putpkt_binary (buf, cnt)
629 char *buf;
630 int cnt;
631{
632 int i;
633 unsigned char csum = 0;
634 char *buf2 = alloca (PBUFSIZ);
635 char *junkbuf = alloca (PBUFSIZ);
636
637 int ch;
638 int tcount = 0;
639 char *p;
640
641 /* Copy the packet into buffer BUF2, encapsulating it
642 and giving it a checksum. */
643
644 if (cnt > BUFSIZ - 5) /* Prosanity check */
645 abort ();
646
647 p = buf2;
648 *p++ = '$';
649
650 for (i = 0; i < cnt; i++)
651 {
652 csum += buf[i];
653 *p++ = buf[i];
654 }
655 *p++ = '#';
656 *p++ = tohex ((csum >> 4) & 0xf);
657 *p++ = tohex (csum & 0xf);
658
659 /* Send it over and over until we get a positive ack. */
660
661 while (1)
662 {
663 int started_error_output = 0;
664
665 if (remote_debug)
666 {
667 *p = '\0';
668 fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
669 fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
670 fprintf_unfiltered (gdb_stdlog, "...");
671 gdb_flush (gdb_stdlog);
672 }
673 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
674 perror_with_name ("putpkt: write failed");
675
676 /* read until either a timeout occurs (-2) or '+' is read */
677 while (1)
678 {
679 ch = readchar (remote_timeout);
680
681 if (remote_debug)
682 {
683 switch (ch)
684 {
685 case '+':
686 case SERIAL_TIMEOUT:
687 case '$':
688 if (started_error_output)
689 {
690 putchar_unfiltered ('\n');
691 started_error_output = 0;
692 }
693 }
694 }
695
696 switch (ch)
697 {
698 case '+':
699 if (remote_debug)
700 fprintf_unfiltered (gdb_stdlog, "Ack\n");
701 return 1;
702 case SERIAL_TIMEOUT:
703 tcount++;
704 if (tcount > 3)
705 return 0;
706 break; /* Retransmit buffer */
707 case '$':
708 {
709 /* It's probably an old response, and we're out of sync.
710 Just gobble up the packet and ignore it. */
711 getpkt (junkbuf, 0);
712 continue; /* Now, go look for + */
713 }
714 default:
715 if (remote_debug)
716 {
717 if (!started_error_output)
718 {
719 started_error_output = 1;
720 fprintf_unfiltered (gdb_stdlog, "putpkt: Junk: ");
721 }
722 fputc_unfiltered (ch & 0177, gdb_stdlog);
723 }
724 continue;
725 }
726 break; /* Here to retransmit */
727 }
728
729#if 0
730 /* This is wrong. If doing a long backtrace, the user should be
wdenk57b2d802003-06-27 21:31:46 +0000731 able to get out next time we call QUIT, without anything as
732 violent as interrupt_query. If we want to provide a way out of
733 here without getting to the next QUIT, it should be based on
734 hitting ^C twice as in remote_wait. */
wdenk4a5b6a32001-04-28 17:59:11 +0000735 if (quit_flag)
736 {
737 quit_flag = 0;
738 interrupt_query ();
739 }
740#endif
741 }
742}
743
744/* Come here after finding the start of the frame. Collect the rest
745 into BUF, verifying the checksum, length, and handling run-length
746 compression. Returns 0 on any error, 1 on success. */
747
748static int
749read_frame (char *buf)
750{
751 unsigned char csum;
752 char *bp;
753 int c;
754
755 csum = 0;
756 bp = buf;
757
758 while (1)
759 {
760 c = readchar (remote_timeout);
761
762 switch (c)
763 {
764 case SERIAL_TIMEOUT:
765 if (remote_debug)
766 fputs_filtered ("Timeout in mid-packet, retrying\n", gdb_stdlog);
767 return 0;
768 case '$':
769 if (remote_debug)
770 fputs_filtered ("Saw new packet start in middle of old one\n",
771 gdb_stdlog);
772 return 0; /* Start a new packet, count retries */
773 case '#':
774 {
775 unsigned char pktcsum;
776
777 *bp = '\000';
778
779 pktcsum = fromhex (readchar (remote_timeout)) << 4;
780 pktcsum |= fromhex (readchar (remote_timeout));
781
782 if (csum == pktcsum)
783 {
784 return 1;
785 }
786
787 if (remote_debug)
788 {
789 fprintf_filtered (gdb_stdlog,
790 "Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
791 pktcsum, csum);
792 fputs_filtered (buf, gdb_stdlog);
793 fputs_filtered ("\n", gdb_stdlog);
794 }
795 return 0;
796 }
797 case '*': /* Run length encoding */
798 csum += c;
799 c = readchar (remote_timeout);
800 csum += c;
801 c = c - ' ' + 3; /* Compute repeat count */
802
803 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
804 {
805 memset (bp, *(bp - 1), c);
806 bp += c;
807 continue;
808 }
809
810 *bp = '\0';
811 printf_filtered ("Repeat count %d too large for buffer: ", c);
812 puts_filtered (buf);
813 puts_filtered ("\n");
814 return 0;
815 default:
816 if (bp < buf + PBUFSIZ - 1)
817 {
818 *bp++ = c;
819 csum += c;
820 continue;
821 }
822
823 *bp = '\0';
824 puts_filtered ("Remote packet too long: ");
825 puts_filtered (buf);
826 puts_filtered ("\n");
827
828 return 0;
829 }
830 }
831}
832
833/* Read a packet from the remote machine, with error checking, and
834 store it in BUF. BUF is expected to be of size PBUFSIZ. If
835 FOREVER, wait forever rather than timing out; this is used while
836 the target is executing user code. */
837
838static void
839getpkt (buf, forever)
840 char *buf;
841 int forever;
842{
843 int c;
844 int tries;
845 int timeout;
846 int val;
847
848 strcpy (buf, "timeout");
849
850 if (forever)
851 {
852 timeout = watchdog > 0 ? watchdog : -1;
853 }
854
855 else
856 timeout = remote_timeout;
857
858#define MAX_TRIES 3
859
860 for (tries = 1; tries <= MAX_TRIES; tries++)
861 {
862 /* This can loop forever if the remote side sends us characters
wdenk57b2d802003-06-27 21:31:46 +0000863 continuously, but if it pauses, we'll get a zero from readchar
864 because of timeout. Then we'll count that as a retry. */
wdenk4a5b6a32001-04-28 17:59:11 +0000865
866 /* Note that we will only wait forever prior to the start of a packet.
wdenk57b2d802003-06-27 21:31:46 +0000867 After that, we expect characters to arrive at a brisk pace. They
868 should show up within remote_timeout intervals. */
wdenk4a5b6a32001-04-28 17:59:11 +0000869
870 do
871 {
872 c = readchar (timeout);
873
874 if (c == SERIAL_TIMEOUT)
875 {
876 if (forever) /* Watchdog went off. Kill the target. */
877 {
878 target_mourn_inferior ();
879 error ("Watchdog has expired. Target detached.\n");
880 }
881 if (remote_debug)
882 fputs_filtered ("Timed out.\n", gdb_stdlog);
883 goto retry;
884 }
885 }
886 while (c != '$');
887
888 /* We've found the start of a packet, now collect the data. */
889
890 val = read_frame (buf);
891
892 if (val == 1)
893 {
894 if (remote_debug)
895 {
896 fprintf_unfiltered (gdb_stdlog, "Packet received: ");
897 fputstr_unfiltered (buf, 0, gdb_stdlog);
898 fprintf_unfiltered (gdb_stdlog, "\n");
899 }
900 SERIAL_WRITE (remote_desc, "+", 1);
901 return;
902 }
903
904 /* Try the whole thing again. */
905 retry:
906 SERIAL_WRITE (remote_desc, "-", 1);
907 }
908
909 /* We have tried hard enough, and just can't receive the packet. Give up. */
910
911 printf_unfiltered ("Ignoring packet error, continuing...\n");
912 SERIAL_WRITE (remote_desc, "+", 1);
913}