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
28 '$' or '#'. If <data> starts with two characters followed by
29 ':', then the existing stubs interpret this as a sequence number.
31 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
32 checksum of <data>, the most significant nibble is sent first.
33 the hex digits 0-9,a-f are used.
35 Receiver responds with:
37 + - if CSUM is correct and ready for next packet
38 - - if CSUM is incorrect
41 All values are encoded in ascii hex digits.
46 reply XX....X Each byte of register data
47 is described by two hex digits.
48 Registers are in the internal order
49 for GDB, and the bytes in a register
50 are in the same order the machine uses.
53 write regs GXX..XX Each byte of register data
54 is described by two hex digits.
58 write reg Pn...=r... Write register n... with value r...,
59 which contains two hex digits for each
60 byte in the register (target byte
64 (not supported by all stubs).
66 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
67 reply XX..XX XX..XX is mem contents
68 Can be fewer bytes than requested
69 if able to read only part of the data.
72 write mem MAA..AA,LLLL:XX..XX
74 LLLL is number of bytes,
77 ENN for an error (this includes the case
78 where only part of the data was
81 cont cAA..AA AA..AA is address to resume
83 resume at same address.
85 step sAA..AA AA..AA is address to resume
87 resume at same address.
89 last signal ? Reply the current reason for stopping.
90 This is the same reply as is generated
91 for step or cont : SAA where AA is the
94 There is no immediate reply to step or cont.
95 The reply comes when the machine stops.
96 It is SAA AA is the "signal number"
98 or... TAAn...:r...;n:r...;n...:r...;
100 n... = register number
101 r... = register contents
102 or... WAA The process exited, and AA is
103 the exit status. This is only
104 applicable for certains sorts of
108 toggle debug d toggle debug flag (see 386 & 68k stubs)
109 reset r reset -- see sparc stub.
110 reserved <other> On other requests, the stub should
111 ignore the request and send an empty
112 response ($#<checksum>). This way
113 we can extend the protocol and GDB
114 can tell whether the stub it is
115 talking to uses the old or the new.
116 search tAA:PP,MM Search backwards starting at address
117 AA for a match with pattern PP and
118 mask MM. PP and MM are 4 bytes.
119 Not supported by all stubs.
121 general query qXXXX Request info about XXXX.
122 general set QXXXX=yyyy Set value of XXXX to yyyy.
123 query sect offs qOffsets Get section offsets. Reply is
124 Text=xxx;Data=yyy;Bss=zzz
126 Responses can be run-length encoded to save space. A '*' means that
127 the next two characters are hex digits giving a repeat count which
128 stands for that many repititions of the character preceding the '*'.
129 Note that this means that responses cannot contain '*'. Example:
130 "0*03" means the same as "0000". */
136 #include "inferior.h"
141 #include "terminal.h"
143 #include "objfiles.h"
144 #include "gdb-stabs.h"
148 #if !defined(DONT_USE_REMOTE)
150 #include <sys/types.h>
156 /* Prototypes for local functions */
159 remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
162 remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
165 remote_files_info PARAMS ((struct target_ops *ignore));
168 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
169 int should_write, struct target_ops *target));
172 remote_prepare_to_store PARAMS ((void));
175 remote_fetch_registers PARAMS ((int regno));
178 remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
181 remote_start_remote PARAMS ((char *dummy));
184 remote_open PARAMS ((char *name, int from_tty));
187 remote_close PARAMS ((int quitting));
190 remote_store_registers PARAMS ((int regno));
193 getpkt PARAMS ((char *buf, int forever));
196 putpkt PARAMS ((char *buf));
199 remote_send PARAMS ((char *buf));
202 readchar PARAMS ((void));
204 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
207 tohex PARAMS ((int nib));
210 fromhex PARAMS ((int a));
213 remote_detach PARAMS ((char *args, int from_tty));
216 remote_interrupt PARAMS ((int signo));
219 remote_interrupt_twice PARAMS ((int signo));
222 interrupt_query PARAMS ((void));
224 extern struct target_ops remote_ops; /* Forward decl */
226 /* This was 5 seconds, which is a long time to sit and wait.
227 Unless this is going though some terminal server or multiplexer or
228 other form of hairy serial connection, I would think 2 seconds would
230 static int timeout = 2;
236 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
237 remote_open knows that we don't have a file open when the program
239 serial_t remote_desc = NULL;
241 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
242 and i386-stub.c. Normally, no one would notice because it only matters
243 for writing large chunks of memory (e.g. in downloads). Also, this needs
244 to be more than 400 if required to hold the registers (see below, where
245 we round it up based on REGISTER_BYTES). */
248 /* Maximum number of bytes to read/write at once. The value here
249 is chosen to fill up a packet (the headers account for the 32). */
250 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
252 /* Round up PBUFSIZ to hold all the registers, at least. */
253 /* The blank line after the #if seems to be required to work around a
254 bug in HP's PA compiler. */
255 #if REGISTER_BYTES > MAXBUFBYTES
258 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
261 /* Should we try the 'P' request? If this is set to one when the stub
262 doesn't support 'P', the only consequence is some unnecessary traffic. */
263 static int stub_supports_P = 1;
266 /* Clean up connection to a remote debugger. */
270 remote_close (quitting)
274 SERIAL_CLOSE (remote_desc);
278 /* Query the remote side for the text, data and bss offsets. */
283 unsigned char buf[PBUFSIZ];
285 CORE_ADDR text_addr, data_addr, bss_addr;
286 struct section_offsets *offs;
292 if (buf[0] == '\000')
293 return; /* Return silently. Stub doesn't support this
297 warning ("Remote failure reply: %s", buf);
301 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
304 error ("Malformed response to offset query, %s", buf);
306 if (symfile_objfile == NULL)
309 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
310 + symfile_objfile->num_sections
311 * sizeof (offs->offsets));
312 memcpy (offs, symfile_objfile->section_offsets,
313 sizeof (struct section_offsets)
314 + symfile_objfile->num_sections
315 * sizeof (offs->offsets));
317 /* FIXME: This code assumes gdb-stabs.h is being used; it's broken
318 for xcoff, dwarf, sdb-coff, etc. But there is no simple
319 canonical representation for this stuff. (Just what does "text"
320 as seen by the stub mean, anyway? I think it means all sections
321 with SEC_CODE set, but we currently have no way to deal with that). */
323 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
325 /* This is a temporary kludge to force data and bss to use the same offsets
326 because that's what nlmconv does now. The real solution requires changes
327 to the stub and remote.c that I don't have time to do right now. */
329 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
330 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
332 objfile_relocate (symfile_objfile, offs);
335 /* Stub for catch_errors. */
338 remote_start_remote (dummy)
341 immediate_quit = 1; /* Allow user to interrupt it */
343 /* Ack any packet which the remote side has already sent. */
345 SERIAL_WRITE (remote_desc, "+", 1);
347 get_offsets (); /* Get text, data & bss offsets */
349 putpkt ("?"); /* initiate a query from remote machine */
352 start_remote (); /* Initialize gdb process mechanisms */
357 /* Open a connection to a remote debugger.
358 NAME is the filename used for communication. */
360 static DCACHE *remote_dcache;
363 remote_open (name, from_tty)
369 "To open a remote debug connection, you need to specify what serial\n\
370 device is attached to the remote system (e.g. /dev/ttya).");
372 target_preopen (from_tty);
374 unpush_target (&remote_ops);
376 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
378 remote_desc = SERIAL_OPEN (name);
380 perror_with_name (name);
384 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
386 SERIAL_CLOSE (remote_desc);
387 perror_with_name (name);
391 SERIAL_RAW (remote_desc);
393 /* If there is something sitting in the buffer we might take it as a
394 response to a command, which would be bad. */
395 SERIAL_FLUSH_INPUT (remote_desc);
399 puts_filtered ("Remote debugging using ");
400 puts_filtered (name);
401 puts_filtered ("\n");
403 push_target (&remote_ops); /* Switch to using remote target now */
405 /* Start out by trying the 'P' request to set registers. We set this each
406 time that we open a new target so that if the user switches from one
407 stub to another, we can (if the target is closed and reopened) cope. */
410 /* Start the remote connection; if error (0), discard this target.
411 In particular, if the user quits, be sure to discard it
412 (we'd be in an inconsistent state otherwise). */
413 if (!catch_errors (remote_start_remote, (char *)0,
414 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
419 takes a program previously attached to and detaches it.
420 We better not have left any breakpoints
421 in the program or it'll die when it hits one.
422 Close the open connection to the remote debugger.
423 Use this when you want to detach and do something else
427 remote_detach (args, from_tty)
432 error ("Argument given to \"detach\" when remotely debugging.");
436 puts_filtered ("Ending remote debugging.\n");
439 /* Convert hex digit A to a number. */
445 if (a >= '0' && a <= '9')
447 else if (a >= 'a' && a <= 'f')
450 error ("Reply contains invalid hex digit");
454 /* Convert number NIB to a hex digit. */
466 /* Tell the remote machine to resume. */
469 remote_resume (pid, step, siggnal)
471 enum target_signal siggnal;
477 target_terminal_ours_for_output ();
479 ("Can't send signals to a remote system. %s not sent.\n",
480 target_signal_to_name (siggnal));
481 target_terminal_inferior ();
484 dcache_flush (remote_dcache);
486 strcpy (buf, step ? "s": "c");
491 /* Send ^C to target to halt it. Target will respond, and send us a
495 remote_interrupt (signo)
498 /* If this doesn't work, try more severe steps. */
499 signal (signo, remote_interrupt_twice);
502 printf_unfiltered ("remote_interrupt called\n");
504 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
507 static void (*ofunc)();
509 /* The user typed ^C twice. */
511 remote_interrupt_twice (signo)
514 signal (signo, ofunc);
518 signal (signo, remote_interrupt);
521 /* Ask the user what to do when an interrupt is received. */
526 target_terminal_ours ();
528 if (query ("Interrupted while waiting for the program.\n\
529 Give up (and stop debugging it)? "))
531 target_mourn_inferior ();
532 return_to_top_level (RETURN_QUIT);
535 target_terminal_inferior ();
538 /* Wait until the remote machine stops, then return,
539 storing status in STATUS just as `wait' would.
540 Returns "pid" (though it's not clear what, if anything, that
541 means in the case of this target). */
544 remote_wait (pid, status)
546 struct target_waitstatus *status;
548 unsigned char buf[PBUFSIZ];
550 status->kind = TARGET_WAITKIND_EXITED;
551 status->value.integer = 0;
557 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
558 getpkt ((char *) buf, 1);
559 signal (SIGINT, ofunc);
562 warning ("Remote failure reply: %s", buf);
563 else if (buf[0] == 'T')
567 char regs[MAX_REGISTER_RAW_SIZE];
569 /* Expedited reply, containing Signal, {regno, reg} repeat */
570 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
572 n... = register number
573 r... = register contents
576 p = &buf[3]; /* after Txx */
582 regno = strtol (p, &p1, 16); /* Read the register number */
585 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
591 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
594 if (regno >= NUM_REGS)
595 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
598 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
600 if (p[0] == 0 || p[1] == 0)
601 warning ("Remote reply is too short: %s", buf);
602 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
607 warning ("Remote register badly formatted: %s", buf);
609 supply_register (regno, regs);
613 else if (buf[0] == 'W')
615 /* The remote process exited. */
616 status->kind = TARGET_WAITKIND_EXITED;
617 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
620 else if (buf[0] == 'S')
623 warning ("Invalid remote reply: %s", buf);
626 status->kind = TARGET_WAITKIND_STOPPED;
627 status->value.sig = (enum target_signal)
628 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
633 /* Number of bytes of registers this stub implements. */
634 static int register_bytes_found;
636 /* Read the remote registers into the block REGS. */
637 /* Currently we just read all the registers, so we don't use regno. */
640 remote_fetch_registers (regno)
646 char regs[REGISTER_BYTES];
651 /* Unimplemented registers read as all bits zero. */
652 memset (regs, 0, REGISTER_BYTES);
654 /* We can get out of synch in various cases. If the first character
655 in the buffer is not a hex character, assume that has happened
656 and try to fetch another packet to read. */
657 while ((buf[0] < '0' || buf[0] > '9')
658 && (buf[0] < 'a' || buf[0] > 'f'))
661 printf_unfiltered ("Bad register packet; fetching a new packet\n");
665 /* Reply describes registers byte by byte, each byte encoded as two
666 hex characters. Suck them all up, then supply them to the
667 register cacheing/storage mechanism. */
670 for (i = 0; i < REGISTER_BYTES; i++)
676 warning ("Remote reply is of odd length: %s", buf);
677 /* Don't change register_bytes_found in this case, and don't
678 print a second warning. */
681 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
685 if (i != register_bytes_found)
687 register_bytes_found = i;
688 #ifdef REGISTER_BYTES_OK
689 if (!REGISTER_BYTES_OK (i))
690 warning ("Remote reply is too short: %s", buf);
695 for (i = 0; i < NUM_REGS; i++)
696 supply_register (i, ®s[REGISTER_BYTE(i)]);
699 /* Prepare to store registers. Since we may send them all (using a
700 'G' request), we have to read out the ones we don't want to change
704 remote_prepare_to_store ()
706 /* Make sure the entire registers array is valid. */
707 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
710 /* Store register REGNO, or all registers if REGNO == -1, from the contents
711 of REGISTERS. FIXME: ignores errors. */
714 remote_store_registers (regno)
721 if (regno >= 0 && stub_supports_P)
723 /* Try storing a single register. */
726 sprintf (buf, "P%x=", regno);
727 p = buf + strlen (buf);
728 regp = ®isters[REGISTER_BYTE (regno)];
729 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
731 *p++ = tohex ((regp[i] >> 4) & 0xf);
732 *p++ = tohex (regp[i] & 0xf);
738 /* The stub understands the 'P' request. We are done. */
742 /* The stub does not support the 'P' request. Use 'G' instead,
743 and don't try using 'P' in the future (it will just waste our
750 /* Command describes registers byte by byte,
751 each byte encoded as two hex characters. */
754 /* remote_prepare_to_store insures that register_bytes_found gets set. */
755 for (i = 0; i < register_bytes_found; i++)
757 *p++ = tohex ((registers[i] >> 4) & 0xf);
758 *p++ = tohex (registers[i] & 0xf);
767 /* Use of the data cache is disabled because it loses for looking at
768 and changing hardware I/O ports and the like. Accepting `volatile'
769 would perhaps be one way to fix it, but a better way which would
770 win for more cases would be to use the executable file for the text
771 segment, like the `icache' code below but done cleanly (in some
772 target-independent place, perhaps in target_xfer_memory, perhaps
773 based on assigning each target a speed or perhaps by some simpler
776 /* Read a word from remote address ADDR and return it.
777 This goes through the data cache. */
780 remote_fetch_word (addr)
786 extern CORE_ADDR text_start, text_end;
788 if (addr >= text_start && addr < text_end)
791 xfer_core_file (addr, &buffer, sizeof (int));
796 return dcache_fetch (remote_dcache, addr);
799 /* Write a word WORD into remote address ADDR.
800 This goes through the data cache. */
803 remote_store_word (addr, word)
807 dcache_poke (remote_dcache, addr, word);
811 /* Write memory data directly to the remote machine.
812 This does not inform the data cache; the data cache uses this.
813 MEMADDR is the address in the remote memory space.
814 MYADDR is the address of the buffer in our space.
815 LEN is the number of bytes.
817 Returns number of bytes transferred, or 0 for error. */
820 remote_write_bytes (memaddr, myaddr, len)
822 unsigned char *myaddr;
829 /* FIXME-32x64: Need a version of print_address_numeric which puts the
830 result in a buffer like sprintf. */
831 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, len);
833 /* We send target system values byte by byte, in increasing byte addresses,
834 each byte encoded as two hex characters. */
836 p = buf + strlen (buf);
837 for (i = 0; i < len; i++)
839 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
840 *p++ = tohex (myaddr[i] & 0xf);
849 /* There is no correspondance between what the remote protocol uses
850 for errors and errno codes. We would like a cleaner way of
851 representing errors (big enough to include errno codes, bfd_error
852 codes, and others). But for now just return EIO. */
859 /* Read memory data directly from the remote machine.
860 This does not use the data cache; the data cache uses this.
861 MEMADDR is the address in the remote memory space.
862 MYADDR is the address of the buffer in our space.
863 LEN is the number of bytes.
865 Returns number of bytes transferred, or 0 for error. */
868 remote_read_bytes (memaddr, myaddr, len)
870 unsigned char *myaddr;
877 if (len > PBUFSIZ / 2 - 1)
880 /* FIXME-32x64: Need a version of print_address_numeric which puts the
881 result in a buffer like sprintf. */
882 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len);
888 /* There is no correspondance between what the remote protocol uses
889 for errors and errno codes. We would like a cleaner way of
890 representing errors (big enough to include errno codes, bfd_error
891 codes, and others). But for now just return EIO. */
896 /* Reply describes memory byte by byte,
897 each byte encoded as two hex characters. */
900 for (i = 0; i < len; i++)
902 if (p[0] == 0 || p[1] == 0)
903 /* Reply is short. This means that we were able to read only part
904 of what we wanted to. */
906 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
912 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
913 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
914 nonzero. Returns length of data written or read; 0 for error. */
918 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
923 struct target_ops *target; /* ignored */
927 int total_xferred = 0;
931 if (len > MAXBUFBYTES)
932 xfersize = MAXBUFBYTES;
937 bytes_xferred = remote_write_bytes (memaddr,
938 (unsigned char *)myaddr, xfersize);
940 bytes_xferred = remote_read_bytes (memaddr,
941 (unsigned char *)myaddr, xfersize);
943 /* If we get an error, we are done xferring. */
944 if (bytes_xferred == 0)
947 memaddr += bytes_xferred;
948 myaddr += bytes_xferred;
949 len -= bytes_xferred;
950 total_xferred += bytes_xferred;
952 return total_xferred;
956 /* Enable after 4.12. */
959 remote_search (len, data, mask, startaddr, increment, lorange, hirange
960 addr_found, data_found)
968 CORE_ADDR *addr_found;
971 if (increment == -4 && len == 4)
973 long mask_long, data_long;
974 long data_found_long;
975 CORE_ADDR addr_we_found;
977 long returned_long[2];
980 mask_long = extract_unsigned_integer (mask, len);
981 data_long = extract_unsigned_integer (data, len);
982 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
987 /* The stub doesn't support the 't' request. We might want to
988 remember this fact, but on the other hand the stub could be
989 switched on us. Maybe we should remember it only until
990 the next "target remote". */
991 generic_search (len, data, mask, startaddr, increment, lorange,
992 hirange, addr_found, data_found);
997 /* There is no correspondance between what the remote protocol uses
998 for errors and errno codes. We would like a cleaner way of
999 representing errors (big enough to include errno codes, bfd_error
1000 codes, and others). But for now just use EIO. */
1001 memory_error (EIO, startaddr);
1004 while (*p != '\0' && *p != ',')
1005 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1007 error ("Protocol error: short return for search");
1009 data_found_long = 0;
1010 while (*p != '\0' && *p != ',')
1011 data_found_long = (data_found_long << 4) + fromhex (*p++);
1012 /* Ignore anything after this comma, for future extensions. */
1014 if (addr_we_found < lorange || addr_we_found >= hirange)
1020 *addr_found = addr_we_found;
1021 *data_found = store_unsigned_integer (data_we_found, len);
1024 generic_search (len, data, mask, startaddr, increment, lorange,
1025 hirange, addr_found, data_found);
1030 remote_files_info (ignore)
1031 struct target_ops *ignore;
1033 puts_filtered ("Debugging a target over a serial line.\n");
1036 /* Stuff for dealing with the packets which are part of this protocol.
1037 See comment at top of file for details. */
1039 /* Read a single character from the remote end, masking it down to 7 bits. */
1046 ch = SERIAL_READCHAR (remote_desc, timeout);
1054 /* Send the command in BUF to the remote machine,
1055 and read the reply into BUF.
1056 Report an error if we get an error reply. */
1067 error ("Remote failure reply: %s", buf);
1070 /* Send a packet to the remote machine, with error checking.
1071 The data of the packet is in BUF. */
1078 unsigned char csum = 0;
1080 int cnt = strlen (buf);
1084 /* Copy the packet into buffer BUF2, encapsulating it
1085 and giving it a checksum. */
1087 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
1093 for (i = 0; i < cnt; i++)
1099 *p++ = tohex ((csum >> 4) & 0xf);
1100 *p++ = tohex (csum & 0xf);
1102 /* Send it over and over until we get a positive ack. */
1106 int started_error_output = 0;
1111 printf_unfiltered ("Sending packet: %s...", buf2);
1112 gdb_flush(gdb_stdout);
1114 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1115 perror_with_name ("putpkt: write failed");
1117 /* read until either a timeout occurs (-2) or '+' is read */
1127 case SERIAL_TIMEOUT:
1131 if (started_error_output)
1133 putc_unfiltered ('\n');
1134 started_error_output = 0;
1143 printf_unfiltered("Ack\n");
1145 case SERIAL_TIMEOUT:
1146 break; /* Retransmit buffer */
1148 perror_with_name ("putpkt: couldn't read ACK");
1150 error ("putpkt: EOF while trying to read ACK");
1153 unsigned char junkbuf[PBUFSIZ];
1155 /* It's probably an old response, and we're out of sync. Just
1156 gobble up the packet and ignore it. */
1157 getpkt (junkbuf, 0);
1158 continue; /* Now, go look for + */
1163 if (!started_error_output)
1165 started_error_output = 1;
1166 printf_unfiltered ("putpkt: Junk: ");
1168 putc_unfiltered (ch & 0177);
1172 break; /* Here to retransmit */
1176 /* This is wrong. If doing a long backtrace, the user should be
1177 able to get out next time we call QUIT, without anything as violent
1178 as interrupt_query. If we want to provide a way out of here
1179 without getting to the next QUIT, it should be based on hitting
1180 ^C twice as in remote_wait. */
1190 /* Read a packet from the remote machine, with error checking,
1191 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1192 If FOREVER, wait forever rather than timing out; this is used
1193 while the target is executing user code. */
1196 getpkt (retbuf, forever)
1203 unsigned char c1, c2;
1207 #define MAX_RETRIES 10
1212 /* This is wrong. If doing a long backtrace, the user should be
1213 able to get out time next we call QUIT, without anything as violent
1214 as interrupt_query. If we want to provide a way out of here
1215 without getting to the next QUIT, it should be based on hitting
1216 ^C twice as in remote_wait. */
1224 /* This can loop forever if the remote side sends us characters
1225 continuously, but if it pauses, we'll get a zero from readchar
1226 because of timeout. Then we'll count that as a retry. */
1229 if (c > 0 && c != '$')
1232 if (c == SERIAL_TIMEOUT)
1237 puts_filtered ("Timed out.\n");
1241 if (c == SERIAL_EOF)
1242 error ("Remote connection closed");
1243 if (c == SERIAL_ERROR)
1244 perror_with_name ("Remote communication error");
1246 /* Force csum to be zero here because of possible error retry. */
1253 if (c == SERIAL_TIMEOUT)
1256 puts_filtered ("Timeout in mid-packet, retrying\n");
1257 goto whole; /* Start a new packet, count retries */
1262 puts_filtered ("Saw new packet start in middle of old one\n");
1263 goto whole; /* Start a new packet, count retries */
1267 if (bp >= buf+PBUFSIZ-1)
1270 puts_filtered ("Remote packet too long: ");
1271 puts_filtered (buf);
1272 puts_filtered ("\n");
1280 c1 = fromhex (readchar ());
1281 c2 = fromhex (readchar ());
1282 if ((csum & 0xff) == (c1 << 4) + c2)
1284 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1285 (c1 << 4) + c2, csum & 0xff);
1286 puts_filtered (buf);
1287 puts_filtered ("\n");
1289 /* Try the whole thing again. */
1291 if (++retries < MAX_RETRIES)
1293 SERIAL_WRITE (remote_desc, "-", 1);
1297 printf_unfiltered ("Ignoring packet error, continuing...\n");
1302 /* Deal with run-length encoding. */
1305 char *dest = retbuf;
1311 if (src[1] == '\0' || src[2] == '\0')
1314 puts_filtered ("Packet too short, retrying\n");
1317 repeat = (fromhex (src[1]) << 4) + fromhex (src[2]);
1318 for (i = 0; i < repeat; ++i)
1328 } while (*src++ != '\0');
1331 SERIAL_WRITE (remote_desc, "+", 1);
1334 fprintf_unfiltered (gdb_stderr,"Packet received: %s\n", buf);
1341 /* Don't wait for it to die. I'm not really sure it matters whether
1342 we do or not. For the existing stubs, kill is a noop. */
1343 target_mourn_inferior ();
1349 unpush_target (&remote_ops);
1350 generic_mourn_inferior ();
1353 #ifdef REMOTE_BREAKPOINT
1355 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1356 than other targets. */
1357 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1359 /* Check that it fits in BREAKPOINT_MAX bytes. */
1360 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1362 #else /* No REMOTE_BREAKPOINT. */
1364 /* Same old breakpoint instruction. This code does nothing different
1365 than mem-break.c. */
1366 static unsigned char break_insn[] = BREAKPOINT;
1368 #endif /* No REMOTE_BREAKPOINT. */
1370 /* Insert a breakpoint on targets that don't have any better breakpoint
1371 support. We read the contents of the target location and stash it,
1372 then overwrite it with a breakpoint instruction. ADDR is the target
1373 location in the target machine. CONTENTS_CACHE is a pointer to
1374 memory allocated for saving the target contents. It is guaranteed
1375 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1376 is accomplished via BREAKPOINT_MAX). */
1379 remote_insert_breakpoint (addr, contents_cache)
1381 char *contents_cache;
1385 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1388 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1394 remote_remove_breakpoint (addr, contents_cache)
1396 char *contents_cache;
1398 return target_write_memory (addr, contents_cache, sizeof break_insn);
1401 /* Define the target subroutine names */
1403 struct target_ops remote_ops = {
1404 "remote", /* to_shortname */
1405 "Remote serial target in gdb-specific protocol", /* to_longname */
1406 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1407 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1408 remote_open, /* to_open */
1409 remote_close, /* to_close */
1410 NULL, /* to_attach */
1411 remote_detach, /* to_detach */
1412 remote_resume, /* to_resume */
1413 remote_wait, /* to_wait */
1414 remote_fetch_registers, /* to_fetch_registers */
1415 remote_store_registers, /* to_store_registers */
1416 remote_prepare_to_store, /* to_prepare_to_store */
1417 remote_xfer_memory, /* to_xfer_memory */
1418 remote_files_info, /* to_files_info */
1420 remote_insert_breakpoint, /* to_insert_breakpoint */
1421 remote_remove_breakpoint, /* to_remove_breakpoint */
1423 NULL, /* to_terminal_init */
1424 NULL, /* to_terminal_inferior */
1425 NULL, /* to_terminal_ours_for_output */
1426 NULL, /* to_terminal_ours */
1427 NULL, /* to_terminal_info */
1428 remote_kill, /* to_kill */
1429 generic_load, /* to_load */
1430 NULL, /* to_lookup_symbol */
1431 NULL, /* to_create_inferior */
1432 remote_mourn, /* to_mourn_inferior */
1434 0, /* to_notice_signals */
1435 process_stratum, /* to_stratum */
1437 1, /* to_has_all_memory */
1438 1, /* to_has_memory */
1439 1, /* to_has_stack */
1440 1, /* to_has_registers */
1441 1, /* to_has_execution */
1442 NULL, /* sections */
1443 NULL, /* sections_end */
1444 OPS_MAGIC /* to_magic */
1446 #endif /* Use remote. */
1449 _initialize_remote ()
1451 #if !defined(DONT_USE_REMOTE)
1452 add_target (&remote_ops);