1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Remote communication protocol.
22 A debug packet whose contents are <data>
23 is encapsulated for transmission in the form:
25 $ <data> # CSUM1 CSUM2
27 <data> must be ASCII alphanumeric and cannot include characters
30 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
31 checksum of <data>, the most significant nibble is sent first.
32 the hex digits 0-9,a-f are used.
34 Receiver responds with:
36 + - if CSUM is correct and ready for next packet
37 - - if CSUM is incorrect
40 All values are encoded in ascii hex digits.
45 reply XX....X Each byte of register data
46 is described by two hex digits.
47 Registers are in the internal order
48 for GDB, and the bytes in a register
49 are in the same order the machine uses.
52 write regs GXX..XX Each byte of register data
53 is described by two hex digits.
57 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
58 reply XX..XX XX..XX is mem contents
59 Can be fewer bytes than requested
60 if able to read only part of the data.
63 write mem MAA..AA,LLLL:XX..XX
65 LLLL is number of bytes,
68 ENN for an error (this includes the case
69 where only part of the data was
72 cont cAA..AA AA..AA is address to resume
74 resume at same address.
76 step sAA..AA AA..AA is address to resume
78 resume at same address.
80 last signal ? Reply the current reason for stopping.
81 This is the same reply as is generated
82 for step or cont : SAA where AA is the
85 There is no immediate reply to step or cont.
86 The reply comes when the machine stops.
87 It is SAA AA is the "signal number"
89 or... TAAn...:r...;n:r...;n...:r...;
91 n... = register number
92 r... = register contents
93 or... WAA The process exited, and AA is
94 the exit status. This is only
95 applicable for certains sorts of
99 toggle debug d toggle debug flag (see 386 & 68k stubs)
100 reset r reset -- see sparc stub.
101 reserved <other> On other requests, the stub should
102 ignore the request and send an empty
103 response ($#<checksum>). This way
104 we can extend the protocol and GDB
105 can tell whether the stub it is
106 talking to uses the old or the new.
108 search tAA:PP,MM Search backwards starting at address
110 search tAA:PP,MM Search backword starting at address
112 search tAA:PP,MM Search backward starting at address
114 AA for a match with pattern PP and
115 mask MM. PP and MM are 4 bytes.
116 Not supported by all stubs.
119 general query qXXXX Request info about XXXX.
120 general set QXXXX=yyyy Set value of XXXX to yyyy.
121 query sect offs qOffsets Get section offsets. Reply is
122 Text=xxx;Data=yyy;Bss=zzz
127 Responses can be run-length encoded to save space. A '*' means that
128 the next two characters are hex digits giving a repeat count which
129 stands for that many repititions of the character preceding the '*'.
130 Note that this means that responses cannot contain '*'. Example:
131 "0*03" means the same as "0000". */
138 #include "inferior.h"
143 #include "terminal.h"
145 #include "objfiles.h"
146 #include "gdb-stabs.h"
150 #if !defined(DONT_USE_REMOTE)
152 #include <sys/types.h>
158 /* Prototypes for local functions */
161 remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
164 remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
167 remote_files_info PARAMS ((struct target_ops *ignore));
170 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
171 int should_write, struct target_ops *target));
174 remote_prepare_to_store PARAMS ((void));
177 remote_fetch_registers PARAMS ((int regno));
180 remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
183 remote_start_remote PARAMS ((char *dummy));
186 remote_open PARAMS ((char *name, int from_tty));
189 remote_close PARAMS ((int quitting));
192 remote_store_registers PARAMS ((int regno));
195 getpkt PARAMS ((char *buf, int forever));
198 putpkt PARAMS ((char *buf));
201 remote_send PARAMS ((char *buf));
204 readchar PARAMS ((void));
206 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
209 tohex PARAMS ((int nib));
212 fromhex PARAMS ((int a));
215 remote_detach PARAMS ((char *args, int from_tty));
218 remote_interrupt PARAMS ((int signo));
221 remote_interrupt_twice PARAMS ((int signo));
224 interrupt_query PARAMS ((void));
226 extern struct target_ops remote_ops; /* Forward decl */
228 /* This was 5 seconds, which is a long time to sit and wait.
229 Unless this is going though some terminal server or multiplexer or
230 other form of hairy serial connection, I would think 2 seconds would
232 static int timeout = 2;
238 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
239 remote_open knows that we don't have a file open when the program
241 serial_t remote_desc = NULL;
243 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
244 and i386-stub.c. Normally, no one would notice because it only matters
245 for writing large chunks of memory (e.g. in downloads). Also, this needs
246 to be more than 400 if required to hold the registers (see below, where
247 we round it up based on REGISTER_BYTES). */
250 /* Maximum number of bytes to read/write at once. The value here
251 is chosen to fill up a packet (the headers account for the 32). */
252 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
254 /* Round up PBUFSIZ to hold all the registers, at least. */
255 #if REGISTER_BYTES > MAXBUFBYTES
257 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
260 /* Clean up connection to a remote debugger. */
264 remote_close (quitting)
268 SERIAL_CLOSE (remote_desc);
272 /* Query the remote side for the text, data and bss offsets. */
277 unsigned char buf [PBUFSIZ];
279 CORE_ADDR text_addr, data_addr, bss_addr;
280 struct section_offsets *offs;
288 warning ("Remote failure reply: %s", buf);
292 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
295 error ("Malformed response to offset query, %s", buf);
297 if (symfile_objfile == NULL)
300 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
301 + symfile_objfile->num_sections
302 * sizeof (offs->offsets));
303 memcpy (offs, symfile_objfile->section_offsets,
304 sizeof (struct section_offsets)
305 + symfile_objfile->num_sections
306 * sizeof (offs->offsets));
308 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
309 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
310 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
312 objfile_relocate (symfile_objfile, offs);
315 /* Stub for catch_errors. */
318 remote_start_remote (dummy)
321 immediate_quit = 1; /* Allow user to interrupt it */
323 /* Ack any packet which the remote side has already sent. */
325 SERIAL_WRITE (remote_desc, "+", 1);
327 get_offsets (); /* Get text, data & bss offsets */
329 putpkt ("?"); /* initiate a query from remote machine */
332 start_remote (); /* Initialize gdb process mechanisms */
337 /* Open a connection to a remote debugger.
338 NAME is the filename used for communication. */
340 static DCACHE *remote_dcache;
343 remote_open (name, from_tty)
349 "To open a remote debug connection, you need to specify what serial\n\
350 device is attached to the remote system (e.g. /dev/ttya).");
352 target_preopen (from_tty);
354 unpush_target (&remote_ops);
356 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
358 remote_desc = SERIAL_OPEN (name);
360 perror_with_name (name);
364 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
366 SERIAL_CLOSE (remote_desc);
367 perror_with_name (name);
371 SERIAL_RAW (remote_desc);
373 /* If there is something sitting in the buffer we might take it as a
374 response to a command, which would be bad. */
375 SERIAL_FLUSH_INPUT (remote_desc);
379 puts_filtered ("Remote debugging using ");
380 puts_filtered (name);
381 puts_filtered ("\n");
383 push_target (&remote_ops); /* Switch to using remote target now */
385 /* Start the remote connection; if error (0), discard this target.
386 In particular, if the user quits, be sure to discard it
387 (we'd be in an inconsistent state otherwise). */
388 if (!catch_errors (remote_start_remote, (char *)0,
389 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
394 takes a program previously attached to and detaches it.
395 We better not have left any breakpoints
396 in the program or it'll die when it hits one.
397 Close the open connection to the remote debugger.
398 Use this when you want to detach and do something else
402 remote_detach (args, from_tty)
407 error ("Argument given to \"detach\" when remotely debugging.");
411 puts_filtered ("Ending remote debugging.\n");
414 /* Convert hex digit A to a number. */
420 if (a >= '0' && a <= '9')
422 else if (a >= 'a' && a <= 'f')
425 error ("Reply contains invalid hex digit");
429 /* Convert number NIB to a hex digit. */
441 /* Tell the remote machine to resume. */
444 remote_resume (pid, step, siggnal)
446 enum target_signal siggnal;
453 target_terminal_ours_for_output ();
455 ("Can't send signals to a remote system. %s not sent.\n",
456 target_signal_to_name (siggnal));
457 target_terminal_inferior ();
460 dcache_flush (remote_dcache);
462 strcpy (buf, step ? "s": "c");
467 /* Send ^C to target to halt it. Target will respond, and send us a
471 remote_interrupt (signo)
474 /* If this doesn't work, try more severe steps. */
475 signal (signo, remote_interrupt_twice);
478 printf_unfiltered ("remote_interrupt called\n");
480 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
483 static void (*ofunc)();
485 /* The user typed ^C twice. */
487 remote_interrupt_twice (signo)
490 signal (signo, ofunc);
494 signal (signo, remote_interrupt);
497 /* Ask the user what to do when an interrupt is received. */
502 target_terminal_ours ();
504 if (query ("Interrupted while waiting for the program.\n\
505 Give up (and stop debugging it)? "))
507 target_mourn_inferior ();
508 return_to_top_level (RETURN_QUIT);
511 target_terminal_inferior ();
514 /* Wait until the remote machine stops, then return,
515 storing status in STATUS just as `wait' would.
516 Returns "pid" (though it's not clear what, if anything, that
517 means in the case of this target). */
520 remote_wait (pid, status)
522 struct target_waitstatus *status;
524 unsigned char buf[PBUFSIZ];
526 status->kind = TARGET_WAITKIND_EXITED;
527 status->value.integer = 0;
533 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
534 getpkt ((char *) buf, 1);
535 signal (SIGINT, ofunc);
538 warning ("Remote failure reply: %s", buf);
539 else if (buf[0] == 'T')
543 char regs[MAX_REGISTER_RAW_SIZE];
545 /* Expedited reply, containing Signal, {regno, reg} repeat */
546 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
548 n... = register number
549 r... = register contents
552 p = &buf[3]; /* after Txx */
558 regno = strtol (p, &p1, 16); /* Read the register number */
561 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
567 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
570 if (regno >= NUM_REGS)
571 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
574 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
576 if (p[0] == 0 || p[1] == 0)
577 warning ("Remote reply is too short: %s", buf);
578 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
583 warning ("Remote register badly formatted: %s", buf);
585 supply_register (regno, regs);
589 else if (buf[0] == 'W')
591 /* The remote process exited. */
592 status->kind = TARGET_WAITKIND_EXITED;
593 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
596 else if (buf[0] == 'S')
599 warning ("Invalid remote reply: %s", buf);
602 status->kind = TARGET_WAITKIND_STOPPED;
603 status->value.sig = (enum target_signal)
604 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
609 /* Number of bytes of registers this stub implements. */
610 static int register_bytes_found;
612 /* Read the remote registers into the block REGS. */
613 /* Currently we just read all the registers, so we don't use regno. */
616 remote_fetch_registers (regno)
622 char regs[REGISTER_BYTES];
627 /* Unimplemented registers read as all bits zero. */
628 memset (regs, 0, REGISTER_BYTES);
630 /* We can get out of synch in various cases. If the first character
631 in the buffer is not a hex character, assume that has happened
632 and try to fetch another packet to read. */
633 while ((buf[0] < '0' || buf[0] > '9')
634 && (buf[0] < 'a' || buf[0] > 'f'))
637 printf_unfiltered ("Bad register packet; fetching a new packet\n");
641 /* Reply describes registers byte by byte, each byte encoded as two
642 hex characters. Suck them all up, then supply them to the
643 register cacheing/storage mechanism. */
646 for (i = 0; i < REGISTER_BYTES; i++)
652 warning ("Remote reply is of odd length: %s", buf);
653 /* Don't change register_bytes_found in this case, and don't
654 print a second warning. */
657 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
661 if (i != register_bytes_found)
663 register_bytes_found = i;
664 #ifdef REGISTER_BYTES_OK
665 if (!REGISTER_BYTES_OK (i))
666 warning ("Remote reply is too short: %s", buf);
671 for (i = 0; i < NUM_REGS; i++)
672 supply_register (i, ®s[REGISTER_BYTE(i)]);
675 /* Prepare to store registers. Since we send them all, we have to
676 read out the ones we don't want to change first. */
679 remote_prepare_to_store ()
681 /* Make sure the entire registers array is valid. */
682 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
685 /* Store the remote registers from the contents of the block REGISTERS.
686 FIXME, eventually just store one register if that's all that is needed. */
690 remote_store_registers (regno)
699 /* Command describes registers byte by byte,
700 each byte encoded as two hex characters. */
703 /* remote_prepare_to_store insures that register_bytes_found gets set. */
704 for (i = 0; i < register_bytes_found; i++)
706 *p++ = tohex ((registers[i] >> 4) & 0xf);
707 *p++ = tohex (registers[i] & 0xf);
716 /* Use of the data cache is disabled because it loses for looking at
717 and changing hardware I/O ports and the like. Accepting `volatile'
718 would perhaps be one way to fix it, but a better way which would
719 win for more cases would be to use the executable file for the text
720 segment, like the `icache' code below but done cleanly (in some
721 target-independent place, perhaps in target_xfer_memory, perhaps
722 based on assigning each target a speed or perhaps by some simpler
725 /* Read a word from remote address ADDR and return it.
726 This goes through the data cache. */
729 remote_fetch_word (addr)
735 extern CORE_ADDR text_start, text_end;
737 if (addr >= text_start && addr < text_end)
740 xfer_core_file (addr, &buffer, sizeof (int));
745 return dcache_fetch (remote_dcache, addr);
748 /* Write a word WORD into remote address ADDR.
749 This goes through the data cache. */
752 remote_store_word (addr, word)
756 dcache_poke (remote_dcache, addr, word);
760 /* Write memory data directly to the remote machine.
761 This does not inform the data cache; the data cache uses this.
762 MEMADDR is the address in the remote memory space.
763 MYADDR is the address of the buffer in our space.
764 LEN is the number of bytes.
766 Returns number of bytes transferred, or 0 for error. */
769 remote_write_bytes (memaddr, myaddr, len)
771 unsigned char *myaddr;
778 sprintf (buf, "M%x,%x:", memaddr, len);
780 /* We send target system values byte by byte, in increasing byte addresses,
781 each byte encoded as two hex characters. */
783 p = buf + strlen (buf);
784 for (i = 0; i < len; i++)
786 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
787 *p++ = tohex (myaddr[i] & 0xf);
796 /* There is no correspondance between what the remote protocol uses
797 for errors and errno codes. We would like a cleaner way of
798 representing errors (big enough to include errno codes, bfd_error
799 codes, and others). But for now just return EIO. */
806 /* Read memory data directly from the remote machine.
807 This does not use the data cache; the data cache uses this.
808 MEMADDR is the address in the remote memory space.
809 MYADDR is the address of the buffer in our space.
810 LEN is the number of bytes.
812 Returns number of bytes transferred, or 0 for error. */
815 remote_read_bytes (memaddr, myaddr, len)
817 unsigned char *myaddr;
824 if (len > PBUFSIZ / 2 - 1)
827 sprintf (buf, "m%x,%x", memaddr, len);
833 /* There is no correspondance between what the remote protocol uses
834 for errors and errno codes. We would like a cleaner way of
835 representing errors (big enough to include errno codes, bfd_error
836 codes, and others). But for now just return EIO. */
841 /* Reply describes memory byte by byte,
842 each byte encoded as two hex characters. */
845 for (i = 0; i < len; i++)
847 if (p[0] == 0 || p[1] == 0)
848 /* Reply is short. This means that we were able to read only part
849 of what we wanted to. */
851 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
857 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
858 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
859 nonzero. Returns length of data written or read; 0 for error. */
863 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
868 struct target_ops *target; /* ignored */
872 int total_xferred = 0;
876 if (len > MAXBUFBYTES)
877 xfersize = MAXBUFBYTES;
882 bytes_xferred = remote_write_bytes (memaddr,
883 (unsigned char *)myaddr, xfersize);
885 bytes_xferred = remote_read_bytes (memaddr,
886 (unsigned char *)myaddr, xfersize);
888 /* If we get an error, we are done xferring. */
889 if (bytes_xferred == 0)
892 memaddr += bytes_xferred;
893 myaddr += bytes_xferred;
894 len -= bytes_xferred;
895 total_xferred += bytes_xferred;
897 return total_xferred;
901 /* Enable after 4.12. */
904 remote_search (len, data, mask, startaddr, increment, lorange, hirange
905 addr_found, data_found)
913 CORE_ADDR *addr_found;
916 if (increment == -4 && len == 4)
918 long mask_long, data_long;
919 long data_found_long;
920 CORE_ADDR addr_we_found;
922 long returned_long[2];
925 mask_long = extract_unsigned_integer (mask, len);
926 data_long = extract_unsigned_integer (data, len);
927 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
932 /* The stub doesn't support the 't' request. We might want to
933 remember this fact, but on the other hand the stub could be
934 switched on us. Maybe we should remember it only until
935 the next "target remote". */
936 generic_search (len, data, mask, startaddr, increment, lorange,
937 hirange, addr_found, data_found);
942 /* There is no correspondance between what the remote protocol uses
943 for errors and errno codes. We would like a cleaner way of
944 representing errors (big enough to include errno codes, bfd_error
945 codes, and others). But for now just use EIO. */
946 memory_error (EIO, startaddr);
949 while (*p != '\0' && *p != ',')
950 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
952 error ("Protocol error: short return for search");
955 while (*p != '\0' && *p != ',')
956 data_found_long = (data_found_long << 4) + fromhex (*p++);
957 /* Ignore anything after this comma, for future extensions. */
959 if (addr_we_found < lorange || addr_we_found >= hirange)
965 *addr_found = addr_we_found;
966 *data_found = store_unsigned_integer (data_we_found, len);
969 generic_search (len, data, mask, startaddr, increment, lorange,
970 hirange, addr_found, data_found);
975 remote_files_info (ignore)
976 struct target_ops *ignore;
978 puts_filtered ("Debugging a target over a serial line.\n");
981 /* Stuff for dealing with the packets which are part of this protocol.
982 See comment at top of file for details. */
984 /* Read a single character from the remote end, masking it down to 7 bits. */
991 ch = SERIAL_READCHAR (remote_desc, timeout);
999 /* Send the command in BUF to the remote machine,
1000 and read the reply into BUF.
1001 Report an error if we get an error reply. */
1012 error ("Remote failure reply: %s", buf);
1015 /* Send a packet to the remote machine, with error checking.
1016 The data of the packet is in BUF. */
1023 unsigned char csum = 0;
1025 int cnt = strlen (buf);
1029 /* Copy the packet into buffer BUF2, encapsulating it
1030 and giving it a checksum. */
1032 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
1038 for (i = 0; i < cnt; i++)
1044 *p++ = tohex ((csum >> 4) & 0xf);
1045 *p++ = tohex (csum & 0xf);
1047 /* Send it over and over until we get a positive ack. */
1054 printf_unfiltered ("Sending packet: %s...", buf2); gdb_flush(gdb_stdout);
1056 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1057 perror_with_name ("putpkt: write failed");
1059 /* read until either a timeout occurs (-2) or '+' is read */
1068 printf_unfiltered("Ack\n");
1070 case SERIAL_TIMEOUT:
1071 break; /* Retransmit buffer */
1073 perror_with_name ("putpkt: couldn't read ACK");
1075 error ("putpkt: EOF while trying to read ACK");
1078 printf_unfiltered ("%02X %c ", ch&0xFF, ch);
1081 break; /* Here to retransmit */
1085 /* This is wrong. If doing a long backtrace, the user should be
1086 able to get out next time we call QUIT, without anything as violent
1087 as interrupt_query. If we want to provide a way out of here
1088 without getting to the next QUIT, it should be based on hitting
1089 ^C twice as in remote_wait. */
1099 /* Read a packet from the remote machine, with error checking,
1100 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1101 If FOREVER, wait forever rather than timing out; this is used
1102 while the target is executing user code. */
1105 getpkt (retbuf, forever)
1112 unsigned char c1, c2;
1116 #define MAX_RETRIES 10
1121 /* This is wrong. If doing a long backtrace, the user should be
1122 able to get out time next we call QUIT, without anything as violent
1123 as interrupt_query. If we want to provide a way out of here
1124 without getting to the next QUIT, it should be based on hitting
1125 ^C twice as in remote_wait. */
1133 /* This can loop forever if the remote side sends us characters
1134 continuously, but if it pauses, we'll get a zero from readchar
1135 because of timeout. Then we'll count that as a retry. */
1138 if (c > 0 && c != '$')
1141 if (c == SERIAL_TIMEOUT)
1145 if (++retries >= MAX_RETRIES)
1146 if (remote_debug) puts_filtered ("Timed out.\n");
1150 if (c == SERIAL_EOF)
1151 error ("Remote connection closed");
1152 if (c == SERIAL_ERROR)
1153 perror_with_name ("Remote communication error");
1155 /* Force csum to be zero here because of possible error retry. */
1162 if (c == SERIAL_TIMEOUT)
1165 puts_filtered ("Timeout in mid-packet, retrying\n");
1166 goto whole; /* Start a new packet, count retries */
1171 puts_filtered ("Saw new packet start in middle of old one\n");
1172 goto whole; /* Start a new packet, count retries */
1176 if (bp >= buf+PBUFSIZ-1)
1179 puts_filtered ("Remote packet too long: ");
1180 puts_filtered (buf);
1181 puts_filtered ("\n");
1189 c1 = fromhex (readchar ());
1190 c2 = fromhex (readchar ());
1191 if ((csum & 0xff) == (c1 << 4) + c2)
1193 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1194 (c1 << 4) + c2, csum & 0xff);
1195 puts_filtered (buf);
1196 puts_filtered ("\n");
1198 /* Try the whole thing again. */
1200 if (++retries < MAX_RETRIES)
1202 SERIAL_WRITE (remote_desc, "-", 1);
1206 printf_unfiltered ("Ignoring packet error, continuing...\n");
1211 /* Deal with run-length encoding. */
1214 char *dest = retbuf;
1220 if (src[1] == '\0' || src[2] == '\0')
1223 puts_filtered ("Packet too short, retrying\n");
1226 repeat = (fromhex (src[1]) << 4) + fromhex (src[2]);
1227 for (i = 0; i < repeat; ++i)
1237 } while (*src++ != '\0');
1241 SERIAL_WRITE (remote_desc, "+", 1);
1244 fprintf_unfiltered (gdb_stderr,"Packet received: %s\n", buf);
1251 /* Don't wait for it to die. I'm not really sure it matters whether
1252 we do or not. For the existing stubs, kill is a noop. */
1253 target_mourn_inferior ();
1259 unpush_target (&remote_ops);
1260 generic_mourn_inferior ();
1263 #ifdef REMOTE_BREAKPOINT
1265 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1266 than other targets. */
1267 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1269 /* Check that it fits in BREAKPOINT_MAX bytes. */
1270 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1272 #else /* No REMOTE_BREAKPOINT. */
1274 /* Same old breakpoint instruction. This code does nothing different
1275 than mem-break.c. */
1276 static unsigned char break_insn[] = BREAKPOINT;
1278 #endif /* No REMOTE_BREAKPOINT. */
1280 /* Insert a breakpoint on targets that don't have any better breakpoint
1281 support. We read the contents of the target location and stash it,
1282 then overwrite it with a breakpoint instruction. ADDR is the target
1283 location in the target machine. CONTENTS_CACHE is a pointer to
1284 memory allocated for saving the target contents. It is guaranteed
1285 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1286 is accomplished via BREAKPOINT_MAX). */
1289 remote_insert_breakpoint (addr, contents_cache)
1291 char *contents_cache;
1295 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1298 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1304 remote_remove_breakpoint (addr, contents_cache)
1306 char *contents_cache;
1308 return target_write_memory (addr, contents_cache, sizeof break_insn);
1311 /* Define the target subroutine names */
1313 struct target_ops remote_ops = {
1314 "remote", /* to_shortname */
1315 "Remote serial target in gdb-specific protocol", /* to_longname */
1316 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1317 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1318 remote_open, /* to_open */
1319 remote_close, /* to_close */
1320 NULL, /* to_attach */
1321 remote_detach, /* to_detach */
1322 remote_resume, /* to_resume */
1323 remote_wait, /* to_wait */
1324 remote_fetch_registers, /* to_fetch_registers */
1325 remote_store_registers, /* to_store_registers */
1326 remote_prepare_to_store, /* to_prepare_to_store */
1327 remote_xfer_memory, /* to_xfer_memory */
1328 remote_files_info, /* to_files_info */
1330 remote_insert_breakpoint, /* to_insert_breakpoint */
1331 remote_remove_breakpoint, /* to_remove_breakpoint */
1333 NULL, /* to_terminal_init */
1334 NULL, /* to_terminal_inferior */
1335 NULL, /* to_terminal_ours_for_output */
1336 NULL, /* to_terminal_ours */
1337 NULL, /* to_terminal_info */
1338 remote_kill, /* to_kill */
1339 generic_load, /* to_load */
1340 NULL, /* to_lookup_symbol */
1341 NULL, /* to_create_inferior */
1342 remote_mourn, /* to_mourn_inferior */
1344 0, /* to_notice_signals */
1345 process_stratum, /* to_stratum */
1347 1, /* to_has_all_memory */
1348 1, /* to_has_memory */
1349 1, /* to_has_stack */
1350 1, /* to_has_registers */
1351 1, /* to_has_execution */
1352 NULL, /* sections */
1353 NULL, /* sections_end */
1354 OPS_MAGIC /* to_magic */
1356 #endif /* Use remote. */
1359 _initialize_remote ()
1361 #if !defined(DONT_USE_REMOTE)
1362 add_target (&remote_ops);