1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 Most values are encoded in ascii hex digits. Signal numbers are according
42 to the numbering in target.h.
46 set thread Hct... Set thread for subsequent operations.
47 c = 'c' for thread used in step and
48 continue; t... can be -1 for all
50 c = 'g' for thread used in other
51 operations. If zero, pick a thread,
57 reply XX....X Each byte of register data
58 is described by two hex digits.
59 Registers are in the internal order
60 for GDB, and the bytes in a register
61 are in the same order the machine uses.
64 write regs GXX..XX Each byte of register data
65 is described by two hex digits.
69 write reg Pn...=r... Write register n... with value r...,
70 which contains two hex digits for each
71 byte in the register (target byte
75 (not supported by all stubs).
77 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
78 reply XX..XX XX..XX is mem contents
79 Can be fewer bytes than requested
80 if able to read only part of the data.
83 write mem MAA..AA,LLLL:XX..XX
85 LLLL is number of bytes,
88 ENN for an error (this includes the case
89 where only part of the data was
92 continue cAA..AA AA..AA is address to resume
94 resume at same address.
96 step sAA..AA AA..AA is address to resume
98 resume at same address.
100 continue with Csig;AA Continue with signal sig (hex signal
103 step with Ssig;AA Like 'C' but step not continue.
106 last signal ? Reply the current reason for stopping.
107 This is the same reply as is generated
108 for step or cont : SAA where AA is the
113 There is no immediate reply to step or cont.
114 The reply comes when the machine stops.
115 It is SAA AA is the signal number.
117 or... TAAn...:r...;n...:r...;n...:r...;
119 n... = register number (hex)
120 r... = register contents
122 r... = thread process ID. This is
124 n... = other string not starting
125 with valid hex digit.
126 gdb should ignore this n,r pair
127 and go on to the next. This way
128 we can extend the protocol.
129 or... WAA The process exited, and AA is
130 the exit status. This is only
131 applicable for certains sorts of
133 or... XAA The process terminated with signal
135 or... OXX..XX XX..XX is hex encoding of ASCII data. This
136 can happen at any time while the program is
137 running and the debugger should
138 continue to wait for 'W', 'T', etc.
140 thread alive TXX Find out if the thread XX is alive.
141 reply OK thread is still alive
144 remote restart RXX Restart the remote server
146 extended ops ! Use the extended remote protocol.
147 Sticky -- only needs to be set once.
151 toggle debug d toggle debug flag (see 386 & 68k stubs)
152 reset r reset -- see sparc stub.
153 reserved <other> On other requests, the stub should
154 ignore the request and send an empty
155 response ($#<checksum>). This way
156 we can extend the protocol and GDB
157 can tell whether the stub it is
158 talking to uses the old or the new.
159 search tAA:PP,MM Search backwards starting at address
160 AA for a match with pattern PP and
161 mask MM. PP and MM are 4 bytes.
162 Not supported by all stubs.
164 general query qXXXX Request info about XXXX.
165 general set QXXXX=yyyy Set value of XXXX to yyyy.
166 query sect offs qOffsets Get section offsets. Reply is
167 Text=xxx;Data=yyy;Bss=zzz
169 Responses can be run-length encoded to save space. A '*' means that
170 the next character is an ASCII encoding giving a repeat count which
171 stands for that many repititions of the character preceding the '*'.
172 The encoding is n+29, yielding a printable character where n >=3
173 (which is where rle starts to win). Don't use an n > 126.
176 "0* " means the same as "0000". */
179 #include "gdb_string.h"
182 #include "inferior.h"
187 /*#include "terminal.h"*/
189 #include "objfiles.h"
190 #include "gdb-stabs.h"
196 #include <sys/types.h>
202 /* Prototypes for local functions */
204 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
205 char *myaddr, int len));
207 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
208 char *myaddr, int len));
210 static void remote_files_info PARAMS ((struct target_ops *ignore));
212 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
213 int len, int should_write,
214 struct target_ops *target));
216 static void remote_prepare_to_store PARAMS ((void));
218 static void remote_fetch_registers PARAMS ((int regno));
220 static void remote_resume PARAMS ((int pid, int step,
221 enum target_signal siggnal));
223 static int remote_start_remote PARAMS ((char *dummy));
225 static void remote_open PARAMS ((char *name, int from_tty));
227 static void extended_remote_open PARAMS ((char *name, int from_tty));
229 static void remote_open_1 PARAMS ((char *, int, struct target_ops *));
231 static void remote_close PARAMS ((int quitting));
233 static void remote_store_registers PARAMS ((int regno));
235 static void remote_mourn PARAMS ((void));
237 static void extended_remote_restart PARAMS ((void));
239 static void extended_remote_mourn PARAMS ((void));
241 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
243 static void remote_mourn_1 PARAMS ((struct target_ops *));
245 static void getpkt PARAMS ((char *buf, int forever));
247 static int putpkt PARAMS ((char *buf));
249 static void remote_send PARAMS ((char *buf));
251 static int readchar PARAMS ((int timeout));
253 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
255 static void remote_kill PARAMS ((void));
257 static int tohex PARAMS ((int nib));
259 static int fromhex PARAMS ((int a));
261 static void remote_detach PARAMS ((char *args, int from_tty));
263 static void remote_interrupt PARAMS ((int signo));
265 static void remote_interrupt_twice PARAMS ((int signo));
267 static void interrupt_query PARAMS ((void));
269 extern struct target_ops remote_ops; /* Forward decl */
270 extern struct target_ops extended_remote_ops; /* Forward decl */
272 /* This was 5 seconds, which is a long time to sit and wait.
273 Unless this is going though some terminal server or multiplexer or
274 other form of hairy serial connection, I would think 2 seconds would
277 static int remote_timeout = 2;
279 /* This variable chooses whether to send a ^C or a break when the user
280 requests program interruption. Although ^C is usually what remote
281 systems expect, and that is the default here, sometimes a break is
282 preferable instead. */
284 static int remote_break;
286 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
287 remote_open knows that we don't have a file open when the program
289 serial_t remote_desc = NULL;
291 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
292 and i386-stub.c. Normally, no one would notice because it only matters
293 for writing large chunks of memory (e.g. in downloads). Also, this needs
294 to be more than 400 if required to hold the registers (see below, where
295 we round it up based on REGISTER_BYTES). */
298 /* Maximum number of bytes to read/write at once. The value here
299 is chosen to fill up a packet (the headers account for the 32). */
300 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
302 /* Round up PBUFSIZ to hold all the registers, at least. */
303 /* The blank line after the #if seems to be required to work around a
304 bug in HP's PA compiler. */
305 #if REGISTER_BYTES > MAXBUFBYTES
308 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
311 /* This variable sets the number of bytes to be written to the target
312 in a single packet. Normally PBUFSIZ is satisfactory, but some
313 targets need smaller values (perhaps because the receiving end
316 static int remote_write_size = PBUFSIZ;
318 /* Should we try the 'P' request? If this is set to one when the stub
319 doesn't support 'P', the only consequence is some unnecessary traffic. */
320 static int stub_supports_P = 1;
323 /* These are the threads which we last sent to the remote system. -1 for all
324 or -2 for not sent yet. */
334 int state = gen ? general_thread : cont_thread;
338 buf[1] = gen ? 'g' : 'c';
345 sprintf (&buf[2], "-%x", -th);
347 sprintf (&buf[2], "%x", th);
356 /* Return nonzero if the thread TH is still alive on the remote system. */
359 remote_thread_alive (th)
366 sprintf (&buf[1], "-%x", -th);
368 sprintf (&buf[1], "%x", th);
371 return (buf[0] == 'O' && buf[1] == 'K');
374 /* Restart the remote side; this is an extended protocol operation. */
377 extended_remote_restart ()
381 /* Send the restart command; for reasons I don't understand the
382 remote side really expects a number after the "R". */
384 sprintf (&buf[1], "%x", 0);
387 /* Now query for status so this looks just like we restarted
388 gdbserver from scratch. */
393 /* Clean up connection to a remote debugger. */
397 remote_close (quitting)
401 SERIAL_CLOSE (remote_desc);
405 /* Query the remote side for the text, data and bss offsets. */
412 CORE_ADDR text_addr, data_addr, bss_addr;
413 struct section_offsets *offs;
419 if (buf[0] == '\000')
420 return; /* Return silently. Stub doesn't support this
424 warning ("Remote failure reply: %s", buf);
428 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
431 error ("Malformed response to offset query, %s", buf);
433 if (symfile_objfile == NULL)
436 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
437 + symfile_objfile->num_sections
438 * sizeof (offs->offsets));
439 memcpy (offs, symfile_objfile->section_offsets,
440 sizeof (struct section_offsets)
441 + symfile_objfile->num_sections
442 * sizeof (offs->offsets));
444 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
446 /* This is a temporary kludge to force data and bss to use the same offsets
447 because that's what nlmconv does now. The real solution requires changes
448 to the stub and remote.c that I don't have time to do right now. */
450 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
451 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
453 objfile_relocate (symfile_objfile, offs);
456 /* Stub for catch_errors. */
459 remote_start_remote (dummy)
462 immediate_quit = 1; /* Allow user to interrupt it */
464 /* Ack any packet which the remote side has already sent. */
465 SERIAL_WRITE (remote_desc, "+", 1);
467 /* Let the stub know that we want it to return the thread. */
470 get_offsets (); /* Get text, data & bss offsets */
472 putpkt ("?"); /* initiate a query from remote machine */
475 start_remote (); /* Initialize gdb process mechanisms */
479 /* Open a connection to a remote debugger.
480 NAME is the filename used for communication. */
483 remote_open (name, from_tty)
487 remote_open_1 (name, from_tty, &remote_ops);
490 /* Open a connection to a remote debugger using the extended
491 remote gdb protocol. NAME is the filename used for communication. */
494 extended_remote_open (name, from_tty)
500 /* Do the basic remote open stuff. */
501 remote_open_1 (name, from_tty, &extended_remote_ops);
503 /* Now tell the remote that we're using the extended protocol. */
509 /* Generic code for opening a connection to a remote target. */
510 static DCACHE *remote_dcache;
513 remote_open_1 (name, from_tty, target)
516 struct target_ops *target;
519 error ("To open a remote debug connection, you need to specify what serial\n\
520 device is attached to the remote system (e.g. /dev/ttya).");
522 target_preopen (from_tty);
524 unpush_target (target);
526 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
528 remote_desc = SERIAL_OPEN (name);
530 perror_with_name (name);
534 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
536 SERIAL_CLOSE (remote_desc);
537 perror_with_name (name);
542 SERIAL_RAW (remote_desc);
544 /* If there is something sitting in the buffer we might take it as a
545 response to a command, which would be bad. */
546 SERIAL_FLUSH_INPUT (remote_desc);
550 puts_filtered ("Remote debugging using ");
551 puts_filtered (name);
552 puts_filtered ("\n");
554 push_target (target); /* Switch to using remote target now */
556 /* Start out by trying the 'P' request to set registers. We set this each
557 time that we open a new target so that if the user switches from one
558 stub to another, we can (if the target is closed and reopened) cope. */
564 /* Without this, some commands which require an active target (such as kill)
565 won't work. This variable serves (at least) double duty as both the pid
566 of the target process (if it has such), and as a flag indicating that a
567 target is active. These functions should be split out into seperate
568 variables, especially since GDB will someday have a notion of debugging
569 several processes. */
571 inferior_pid = 42000;
572 /* Start the remote connection; if error (0), discard this target.
573 In particular, if the user quits, be sure to discard it
574 (we'd be in an inconsistent state otherwise). */
575 if (!catch_errors (remote_start_remote, (char *)0,
576 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
580 /* This takes a program previously attached to and detaches it. After
581 this is done, GDB can be used to debug some other program. We
582 better not have left any breakpoints in the target program or it'll
583 die when it hits one. */
586 remote_detach (args, from_tty)
593 error ("Argument given to \"detach\" when remotely debugging.");
595 /* Tell the remote target to detach. */
601 puts_filtered ("Ending remote debugging.\n");
604 /* Convert hex digit A to a number. */
610 if (a >= '0' && a <= '9')
612 else if (a >= 'a' && a <= 'f')
615 error ("Reply contains invalid hex digit %d", a);
618 /* Convert number NIB to a hex digit. */
630 /* Tell the remote machine to resume. */
632 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
636 remote_resume (pid, step, siggnal)
638 enum target_signal siggnal;
643 set_thread (inferior_pid, 0);
647 dcache_flush (remote_dcache);
649 last_sent_signal = siggnal;
650 last_sent_step = step;
652 if (siggnal != TARGET_SIGNAL_0)
654 buf[0] = step ? 'S' : 'C';
655 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
656 buf[2] = tohex ((int)siggnal & 0xf);
660 strcpy (buf, step ? "s": "c");
665 /* Send ^C to target to halt it. Target will respond, and send us a
669 remote_interrupt (signo)
672 /* If this doesn't work, try more severe steps. */
673 signal (signo, remote_interrupt_twice);
676 printf_unfiltered ("remote_interrupt called\n");
678 /* Send a break or a ^C, depending on user preference. */
680 SERIAL_SEND_BREAK (remote_desc);
682 SERIAL_WRITE (remote_desc, "\003", 1);
685 static void (*ofunc)();
687 /* The user typed ^C twice. */
689 remote_interrupt_twice (signo)
692 signal (signo, ofunc);
696 signal (signo, remote_interrupt);
699 /* Ask the user what to do when an interrupt is received. */
704 target_terminal_ours ();
706 if (query ("Interrupted while waiting for the program.\n\
707 Give up (and stop debugging it)? "))
709 target_mourn_inferior ();
710 return_to_top_level (RETURN_QUIT);
713 target_terminal_inferior ();
716 /* If nonzero, ignore the next kill. */
719 /* Wait until the remote machine stops, then return,
720 storing status in STATUS just as `wait' would.
721 Returns "pid" (though it's not clear what, if anything, that
722 means in the case of this target). */
725 remote_wait (pid, status)
727 struct target_waitstatus *status;
729 unsigned char buf[PBUFSIZ];
732 status->kind = TARGET_WAITKIND_EXITED;
733 status->value.integer = 0;
739 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
740 getpkt ((char *) buf, 1);
741 signal (SIGINT, ofunc);
745 case 'E': /* Error of some sort */
746 warning ("Remote failure reply: %s", buf);
748 case 'T': /* Status with PC, SP, FP, ... */
752 char regs[MAX_REGISTER_RAW_SIZE];
754 /* Expedited reply, containing Signal, {regno, reg} repeat */
755 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
757 n... = register number
758 r... = register contents
761 p = &buf[3]; /* after Txx */
768 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
769 p1 = (unsigned char *)p_temp;
773 p1 = (unsigned char *) strchr ((const char *) p, ':');
775 warning ("Malformed packet (missing colon): %s\n\
778 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
780 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
781 p = (unsigned char *)p_temp;
789 warning ("Malformed packet (missing colon): %s\n\
793 if (regno >= NUM_REGS)
794 warning ("Remote sent bad register number %ld: %s\n\
798 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
800 if (p[0] == 0 || p[1] == 0)
801 warning ("Remote reply is too short: %s", buf);
802 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
805 supply_register (regno, regs);
809 warning ("Remote register badly formatted: %s", buf);
813 case 'S': /* Old style status, just signal only */
814 status->kind = TARGET_WAITKIND_STOPPED;
815 status->value.sig = (enum target_signal)
816 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
819 case 'W': /* Target exited */
821 /* The remote process exited. */
822 status->kind = TARGET_WAITKIND_EXITED;
823 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
827 status->kind = TARGET_WAITKIND_SIGNALLED;
828 status->value.sig = (enum target_signal)
829 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
833 case 'O': /* Console output */
834 for (p = buf + 1; *p; p +=2)
837 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
840 if (target_output_hook)
841 target_output_hook (tb);
843 fputs_filtered (tb, gdb_stdout);
847 if (last_sent_signal != TARGET_SIGNAL_0)
849 /* Zero length reply means that we tried 'S' or 'C' and
850 the remote system doesn't support it. */
851 target_terminal_ours_for_output ();
853 ("Can't send signals to this remote system. %s not sent.\n",
854 target_signal_to_name (last_sent_signal));
855 last_sent_signal = TARGET_SIGNAL_0;
856 target_terminal_inferior ();
858 strcpy ((char *) buf, last_sent_step ? "s" : "c");
859 putpkt ((char *) buf);
862 /* else fallthrough */
864 warning ("Invalid remote reply: %s", buf);
869 if (thread_num != -1)
871 /* Initial thread value can only be acquired via wait, so deal with
872 this marker which is used before the first thread value is
874 if (inferior_pid == 42000)
876 inferior_pid = thread_num;
877 add_thread (inferior_pid);
884 /* Number of bytes of registers this stub implements. */
885 static int register_bytes_found;
887 /* Read the remote registers into the block REGS. */
888 /* Currently we just read all the registers, so we don't use regno. */
891 remote_fetch_registers (regno)
897 char regs[REGISTER_BYTES];
899 set_thread (inferior_pid, 1);
904 /* Unimplemented registers read as all bits zero. */
905 memset (regs, 0, REGISTER_BYTES);
907 /* We can get out of synch in various cases. If the first character
908 in the buffer is not a hex character, assume that has happened
909 and try to fetch another packet to read. */
910 while ((buf[0] < '0' || buf[0] > '9')
911 && (buf[0] < 'a' || buf[0] > 'f'))
914 printf_unfiltered ("Bad register packet; fetching a new packet\n");
918 /* Reply describes registers byte by byte, each byte encoded as two
919 hex characters. Suck them all up, then supply them to the
920 register cacheing/storage mechanism. */
923 for (i = 0; i < REGISTER_BYTES; i++)
929 warning ("Remote reply is of odd length: %s", buf);
930 /* Don't change register_bytes_found in this case, and don't
931 print a second warning. */
934 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
938 if (i != register_bytes_found)
940 register_bytes_found = i;
941 #ifdef REGISTER_BYTES_OK
942 if (!REGISTER_BYTES_OK (i))
943 warning ("Remote reply is too short: %s", buf);
948 for (i = 0; i < NUM_REGS; i++)
949 supply_register (i, ®s[REGISTER_BYTE(i)]);
952 /* Prepare to store registers. Since we may send them all (using a
953 'G' request), we have to read out the ones we don't want to change
957 remote_prepare_to_store ()
959 /* Make sure the entire registers array is valid. */
960 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
963 /* Store register REGNO, or all registers if REGNO == -1, from the contents
964 of REGISTERS. FIXME: ignores errors. */
967 remote_store_registers (regno)
974 set_thread (inferior_pid, 1);
976 if (regno >= 0 && stub_supports_P)
978 /* Try storing a single register. */
981 sprintf (buf, "P%x=", regno);
982 p = buf + strlen (buf);
983 regp = ®isters[REGISTER_BYTE (regno)];
984 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
986 *p++ = tohex ((regp[i] >> 4) & 0xf);
987 *p++ = tohex (regp[i] & 0xf);
993 /* The stub understands the 'P' request. We are done. */
997 /* The stub does not support the 'P' request. Use 'G' instead,
998 and don't try using 'P' in the future (it will just waste our
1000 stub_supports_P = 0;
1005 /* Command describes registers byte by byte,
1006 each byte encoded as two hex characters. */
1009 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1010 for (i = 0; i < register_bytes_found; i++)
1012 *p++ = tohex ((registers[i] >> 4) & 0xf);
1013 *p++ = tohex (registers[i] & 0xf);
1021 Use of the data cache *used* to be disabled because it loses for looking at
1022 and changing hardware I/O ports and the like. Accepting `volatile'
1023 would perhaps be one way to fix it. Another idea would be to use the
1024 executable file for the text segment (for all SEC_CODE sections?
1025 For all SEC_READONLY sections?). This has problems if you want to
1026 actually see what the memory contains (e.g. self-modifying code,
1027 clobbered memory, user downloaded the wrong thing).
1029 Because it speeds so much up, it's now enabled, if you're playing
1030 with registers you turn it of (set remotecache 0)
1033 /* Read a word from remote address ADDR and return it.
1034 This goes through the data cache. */
1038 remote_fetch_word (addr)
1041 return dcache_fetch (remote_dcache, addr);
1044 /* Write a word WORD into remote address ADDR.
1045 This goes through the data cache. */
1048 remote_store_word (addr, word)
1052 dcache_poke (remote_dcache, addr, word);
1054 #endif /* 0 (unused?) */
1057 /* Write memory data directly to the remote machine.
1058 This does not inform the data cache; the data cache uses this.
1059 MEMADDR is the address in the remote memory space.
1060 MYADDR is the address of the buffer in our space.
1061 LEN is the number of bytes.
1063 Returns number of bytes transferred, or 0 for error. */
1066 remote_write_bytes (memaddr, myaddr, len)
1075 /* Chop the transfer down if necessary */
1080 int todo = len - done;
1081 int cando = min(remote_write_size, PBUFSIZ) / 2 - 32; /* num bytes that will fit */
1086 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1087 result in a buffer like sprintf. */
1088 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr + done, todo);
1090 /* We send target system values byte by byte, in increasing byte addresses,
1091 each byte encoded as two hex characters. */
1093 p = buf + strlen (buf);
1094 for (i = 0; i < todo; i++)
1096 *p++ = tohex ((myaddr[i + done] >> 4) & 0xf);
1097 *p++ = tohex (myaddr[i + done] & 0xf);
1106 /* There is no correspondance between what the remote protocol uses
1107 for errors and errno codes. We would like a cleaner way of
1108 representing errors (big enough to include errno codes, bfd_error
1109 codes, and others). But for now just return EIO. */
1118 /* Read memory data directly from the remote machine.
1119 This does not use the data cache; the data cache uses this.
1120 MEMADDR is the address in the remote memory space.
1121 MYADDR is the address of the buffer in our space.
1122 LEN is the number of bytes.
1124 Returns number of bytes transferred, or 0 for error. */
1127 remote_read_bytes (memaddr, myaddr, len)
1136 /* Chop transfer down if neccessary */
1139 /* FIXME: This is wrong for larger packets */
1140 if (len > PBUFSIZ / 2 - 1)
1146 int todo = len - done;
1147 int cando = PBUFSIZ / 2 - 32; /* number of bytes that will fit. */
1151 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1152 result in a buffer like sprintf. */
1153 sprintf (buf, "m%lx,%x", (unsigned long) memaddr + done, todo);
1159 /* There is no correspondance between what the remote protocol uses
1160 for errors and errno codes. We would like a cleaner way of
1161 representing errors (big enough to include errno codes, bfd_error
1162 codes, and others). But for now just return EIO. */
1167 /* Reply describes memory byte by byte,
1168 each byte encoded as two hex characters. */
1171 for (i = 0; i < todo; i++)
1173 if (p[0] == 0 || p[1] == 0)
1174 /* Reply is short. This means that we were able to read only part
1175 of what we wanted to. */
1177 myaddr[i + done] = fromhex (p[0]) * 16 + fromhex (p[1]);
1185 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1186 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1187 nonzero. Returns length of data written or read; 0 for error. */
1191 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1196 struct target_ops *target; /* ignored */
1198 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
1203 /* Enable after 4.12. */
1206 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1207 addr_found, data_found)
1211 CORE_ADDR startaddr;
1215 CORE_ADDR *addr_found;
1218 if (increment == -4 && len == 4)
1220 long mask_long, data_long;
1221 long data_found_long;
1222 CORE_ADDR addr_we_found;
1224 long returned_long[2];
1227 mask_long = extract_unsigned_integer (mask, len);
1228 data_long = extract_unsigned_integer (data, len);
1229 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1234 /* The stub doesn't support the 't' request. We might want to
1235 remember this fact, but on the other hand the stub could be
1236 switched on us. Maybe we should remember it only until
1237 the next "target remote". */
1238 generic_search (len, data, mask, startaddr, increment, lorange,
1239 hirange, addr_found, data_found);
1244 /* There is no correspondance between what the remote protocol uses
1245 for errors and errno codes. We would like a cleaner way of
1246 representing errors (big enough to include errno codes, bfd_error
1247 codes, and others). But for now just use EIO. */
1248 memory_error (EIO, startaddr);
1251 while (*p != '\0' && *p != ',')
1252 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1254 error ("Protocol error: short return for search");
1256 data_found_long = 0;
1257 while (*p != '\0' && *p != ',')
1258 data_found_long = (data_found_long << 4) + fromhex (*p++);
1259 /* Ignore anything after this comma, for future extensions. */
1261 if (addr_we_found < lorange || addr_we_found >= hirange)
1267 *addr_found = addr_we_found;
1268 *data_found = store_unsigned_integer (data_we_found, len);
1271 generic_search (len, data, mask, startaddr, increment, lorange,
1272 hirange, addr_found, data_found);
1277 remote_files_info (ignore)
1278 struct target_ops *ignore;
1280 puts_filtered ("Debugging a target over a serial line.\n");
1283 /* Stuff for dealing with the packets which are part of this protocol.
1284 See comment at top of file for details. */
1286 /* Read a single character from the remote end, masking it down to 7 bits. */
1294 ch = SERIAL_READCHAR (remote_desc, timeout);
1299 error ("Remote connection closed");
1301 perror_with_name ("Remote communication error");
1302 case SERIAL_TIMEOUT:
1309 /* Send the command in BUF to the remote machine,
1310 and read the reply into BUF.
1311 Report an error if we get an error reply. */
1321 error ("Remote failure reply: %s", buf);
1324 /* Send a packet to the remote machine, with error checking.
1325 The data of the packet is in BUF. */
1332 unsigned char csum = 0;
1334 int cnt = strlen (buf);
1339 /* Copy the packet into buffer BUF2, encapsulating it
1340 and giving it a checksum. */
1342 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
1348 for (i = 0; i < cnt; i++)
1354 *p++ = tohex ((csum >> 4) & 0xf);
1355 *p++ = tohex (csum & 0xf);
1357 /* Send it over and over until we get a positive ack. */
1361 int started_error_output = 0;
1366 printf_unfiltered ("Sending packet: %s...", buf2);
1367 gdb_flush(gdb_stdout);
1369 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1370 perror_with_name ("putpkt: write failed");
1372 /* read until either a timeout occurs (-2) or '+' is read */
1375 ch = readchar (remote_timeout);
1382 case SERIAL_TIMEOUT:
1384 if (started_error_output)
1386 putchar_unfiltered ('\n');
1387 started_error_output = 0;
1396 printf_unfiltered("Ack\n");
1398 case SERIAL_TIMEOUT:
1402 break; /* Retransmit buffer */
1405 char junkbuf[PBUFSIZ];
1407 /* It's probably an old response, and we're out of sync. Just
1408 gobble up the packet and ignore it. */
1409 getpkt (junkbuf, 0);
1410 continue; /* Now, go look for + */
1415 if (!started_error_output)
1417 started_error_output = 1;
1418 printf_unfiltered ("putpkt: Junk: ");
1420 putchar_unfiltered (ch & 0177);
1424 break; /* Here to retransmit */
1428 /* This is wrong. If doing a long backtrace, the user should be
1429 able to get out next time we call QUIT, without anything as violent
1430 as interrupt_query. If we want to provide a way out of here
1431 without getting to the next QUIT, it should be based on hitting
1432 ^C twice as in remote_wait. */
1442 /* Come here after finding the start of the frame. Collect the rest into BUF,
1443 verifying the checksum, length, and handling run-length compression.
1444 Returns 0 on any error, 1 on success. */
1459 c = readchar (remote_timeout);
1463 case SERIAL_TIMEOUT:
1465 puts_filtered ("Timeout in mid-packet, retrying\n");
1469 puts_filtered ("Saw new packet start in middle of old one\n");
1470 return 0; /* Start a new packet, count retries */
1473 unsigned char pktcsum;
1477 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1478 pktcsum |= fromhex (readchar (remote_timeout));
1480 if (csum == pktcsum)
1485 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1487 puts_filtered (buf);
1488 puts_filtered ("\n");
1492 case '*': /* Run length encoding */
1494 c = readchar (remote_timeout);
1496 c = c - ' ' + 3; /* Compute repeat count */
1499 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
1501 memset (bp, *(bp - 1), c);
1507 printf_filtered ("Repeat count %d too large for buffer: ", c);
1508 puts_filtered (buf);
1509 puts_filtered ("\n");
1513 if (bp < buf + PBUFSIZ - 1)
1521 puts_filtered ("Remote packet too long: ");
1522 puts_filtered (buf);
1523 puts_filtered ("\n");
1530 /* Read a packet from the remote machine, with error checking,
1531 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1532 If FOREVER, wait forever rather than timing out; this is used
1533 while the target is executing user code. */
1536 getpkt (buf, forever)
1545 strcpy (buf,"timeout");
1549 #ifdef MAINTENANCE_CMDS
1550 timeout = watchdog > 0 ? watchdog : -1;
1557 timeout = remote_timeout;
1561 for (tries = 1; tries <= MAX_TRIES; tries++)
1563 /* This can loop forever if the remote side sends us characters
1564 continuously, but if it pauses, we'll get a zero from readchar
1565 because of timeout. Then we'll count that as a retry. */
1567 /* Note that we will only wait forever prior to the start of a packet.
1568 After that, we expect characters to arrive at a brisk pace. They
1569 should show up within remote_timeout intervals. */
1573 c = readchar (timeout);
1575 if (c == SERIAL_TIMEOUT)
1577 #ifdef MAINTENANCE_CMDS
1578 if (forever) /* Watchdog went off. Kill the target. */
1580 target_mourn_inferior ();
1581 error ("Watchdog has expired. Target detached.\n");
1585 puts_filtered ("Timed out.\n");
1591 /* We've found the start of a packet, now collect the data. */
1593 val = read_frame (buf);
1598 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
1599 SERIAL_WRITE (remote_desc, "+", 1);
1603 /* Try the whole thing again. */
1605 SERIAL_WRITE (remote_desc, "-", 1);
1608 /* We have tried hard enough, and just can't receive the packet. Give up. */
1610 printf_unfiltered ("Ignoring packet error, continuing...\n");
1611 SERIAL_WRITE (remote_desc, "+", 1);
1617 /* For some mysterious reason, wait_for_inferior calls kill instead of
1618 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1622 target_mourn_inferior ();
1626 /* Use catch_errors so the user can quit from gdb even when we aren't on
1627 speaking terms with the remote system. */
1628 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
1630 /* Don't wait for it to die. I'm not really sure it matters whether
1631 we do or not. For the existing stubs, kill is a noop. */
1632 target_mourn_inferior ();
1638 remote_mourn_1 (&remote_ops);
1642 extended_remote_mourn ()
1644 /* We do _not_ want to mourn the target like this; this will
1645 remove the extended remote target from the target stack,
1646 and the next time the user says "run" it'll fail.
1648 FIXME: What is the right thing to do here? */
1650 remote_mourn_1 (&extended_remote_ops);
1654 /* Worker function for remote_mourn. */
1656 remote_mourn_1 (target)
1657 struct target_ops *target;
1659 unpush_target (target);
1660 generic_mourn_inferior ();
1663 /* In the extended protocol we want to be able to do things like
1664 "run" and have them basically work as expected. So we need
1665 a special create_inferior function.
1667 FIXME: One day add support for changing the exec file
1668 we're debugging, arguments and an environment. */
1671 extended_remote_create_inferior (exec_file, args, env)
1676 /* Rip out the breakpoints; we'll reinsert them after restarting
1677 the remote server. */
1678 remove_breakpoints ();
1680 /* Now restart the remote server. */
1681 extended_remote_restart ();
1683 /* Now put the breakpoints back in. This way we're safe if the
1684 restart function works via a unix fork on the remote side. */
1685 insert_breakpoints ();
1687 /* Clean up from the last time we were running. */
1688 clear_proceed_status ();
1690 /* Let the remote process run. */
1691 proceed (-1, TARGET_SIGNAL_0, 0);
1695 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1696 than other targets; in those use REMOTE_BREAKPOINT instead of just
1697 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1698 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1699 the standard routines that are in mem-break.c. */
1701 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1702 the choice of breakpoint instruction affects target program design and
1703 vice versa, and by making it user-tweakable, the special code here
1704 goes away and we need fewer special GDB configurations. */
1706 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1707 #define REMOTE_BREAKPOINT
1710 #ifdef REMOTE_BREAKPOINT
1712 /* If the target isn't bi-endian, just pretend it is. */
1713 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1714 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1715 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1718 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
1719 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
1721 #endif /* REMOTE_BREAKPOINT */
1723 /* Insert a breakpoint on targets that don't have any better breakpoint
1724 support. We read the contents of the target location and stash it,
1725 then overwrite it with a breakpoint instruction. ADDR is the target
1726 location in the target machine. CONTENTS_CACHE is a pointer to
1727 memory allocated for saving the target contents. It is guaranteed
1728 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1729 is accomplished via BREAKPOINT_MAX). */
1732 remote_insert_breakpoint (addr, contents_cache)
1734 char *contents_cache;
1736 #ifdef REMOTE_BREAKPOINT
1739 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
1743 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1744 val = target_write_memory (addr, (char *) big_break_insn,
1745 sizeof big_break_insn);
1747 val = target_write_memory (addr, (char *) little_break_insn,
1748 sizeof little_break_insn);
1753 return memory_insert_breakpoint (addr, contents_cache);
1754 #endif /* REMOTE_BREAKPOINT */
1758 remote_remove_breakpoint (addr, contents_cache)
1760 char *contents_cache;
1762 #ifdef REMOTE_BREAKPOINT
1763 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
1765 return memory_remove_breakpoint (addr, contents_cache);
1766 #endif /* REMOTE_BREAKPOINT */
1769 /* Define the target subroutine names */
1771 struct target_ops remote_ops = {
1772 "remote", /* to_shortname */
1773 "Remote serial target in gdb-specific protocol", /* to_longname */
1774 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1775 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1776 remote_open, /* to_open */
1777 remote_close, /* to_close */
1778 NULL, /* to_attach */
1779 remote_detach, /* to_detach */
1780 remote_resume, /* to_resume */
1781 remote_wait, /* to_wait */
1782 remote_fetch_registers, /* to_fetch_registers */
1783 remote_store_registers, /* to_store_registers */
1784 remote_prepare_to_store, /* to_prepare_to_store */
1785 remote_xfer_memory, /* to_xfer_memory */
1786 remote_files_info, /* to_files_info */
1787 remote_insert_breakpoint, /* to_insert_breakpoint */
1788 remote_remove_breakpoint, /* to_remove_breakpoint */
1789 NULL, /* to_terminal_init */
1790 NULL, /* to_terminal_inferior */
1791 NULL, /* to_terminal_ours_for_output */
1792 NULL, /* to_terminal_ours */
1793 NULL, /* to_terminal_info */
1794 remote_kill, /* to_kill */
1795 generic_load, /* to_load */
1796 NULL, /* to_lookup_symbol */
1797 NULL, /* to_create_inferior */
1798 remote_mourn, /* to_mourn_inferior */
1800 0, /* to_notice_signals */
1801 remote_thread_alive, /* to_thread_alive */
1803 process_stratum, /* to_stratum */
1805 1, /* to_has_all_memory */
1806 1, /* to_has_memory */
1807 1, /* to_has_stack */
1808 1, /* to_has_registers */
1809 1, /* to_has_execution */
1810 NULL, /* sections */
1811 NULL, /* sections_end */
1812 OPS_MAGIC /* to_magic */
1815 struct target_ops extended_remote_ops = {
1816 "extended-remote", /* to_shortname */
1817 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1818 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1819 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1820 extended_remote_open, /* to_open */
1821 remote_close, /* to_close */
1822 NULL, /* to_attach */
1823 remote_detach, /* to_detach */
1824 remote_resume, /* to_resume */
1825 remote_wait, /* to_wait */
1826 remote_fetch_registers, /* to_fetch_registers */
1827 remote_store_registers, /* to_store_registers */
1828 remote_prepare_to_store, /* to_prepare_to_store */
1829 remote_xfer_memory, /* to_xfer_memory */
1830 remote_files_info, /* to_files_info */
1832 remote_insert_breakpoint, /* to_insert_breakpoint */
1833 remote_remove_breakpoint, /* to_remove_breakpoint */
1835 NULL, /* to_terminal_init */
1836 NULL, /* to_terminal_inferior */
1837 NULL, /* to_terminal_ours_for_output */
1838 NULL, /* to_terminal_ours */
1839 NULL, /* to_terminal_info */
1840 remote_kill, /* to_kill */
1841 generic_load, /* to_load */
1842 NULL, /* to_lookup_symbol */
1843 extended_remote_create_inferior,/* to_create_inferior */
1844 extended_remote_mourn, /* to_mourn_inferior */
1846 0, /* to_notice_signals */
1847 remote_thread_alive, /* to_thread_alive */
1849 process_stratum, /* to_stratum */
1851 1, /* to_has_all_memory */
1852 1, /* to_has_memory */
1853 1, /* to_has_stack */
1854 1, /* to_has_registers */
1855 1, /* to_has_execution */
1856 NULL, /* sections */
1857 NULL, /* sections_end */
1858 OPS_MAGIC /* to_magic */
1862 _initialize_remote ()
1864 add_target (&remote_ops);
1865 add_target (&extended_remote_ops);
1867 add_show_from_set (add_set_cmd ("remotetimeout", no_class,
1868 var_integer, (char *)&remote_timeout,
1869 "Set timeout value for remote read.\n", &setlist),
1872 add_show_from_set (add_set_cmd ("remotebreak", no_class,
1873 var_integer, (char *)&remote_break,
1874 "Set whether to send break if interrupted.\n", &setlist),
1877 add_show_from_set (add_set_cmd ("remotewritesize", no_class,
1878 var_integer, (char *)&remote_write_size,
1879 "Set the maximum number of bytes in each memory write packet.\n", &setlist),