1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994, 1995 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
111 There is no immediate reply to step or cont.
112 The reply comes when the machine stops.
113 It is SAA AA is the signal number.
115 or... TAAn...:r...;n...:r...;n...:r...;
117 n... = register number (hex)
118 r... = register contents
120 r... = thread process ID. This is
122 n... = other string not starting
123 with valid hex digit.
124 gdb should ignore this n,r pair
125 and go on to the next. This way
126 we can extend the protocol.
127 or... WAA The process exited, and AA is
128 the exit status. This is only
129 applicable for certains sorts of
131 or... XAA The process terminated with signal
133 or... OXX..XX XX..XX is hex encoding of ASCII data. This
134 can happen at any time while the program is
135 running and the debugger should
136 continue to wait for 'W', 'T', etc.
138 thread alive TXX Find out if the thread XX is alive.
139 reply OK thread is still alive
142 remote restart RXX Restart the remote server
144 extended ops ! Use the extended remote protocol.
145 Sticky -- only needs to be set once.
149 toggle debug d toggle debug flag (see 386 & 68k stubs)
150 reset r reset -- see sparc stub.
151 reserved <other> On other requests, the stub should
152 ignore the request and send an empty
153 response ($#<checksum>). This way
154 we can extend the protocol and GDB
155 can tell whether the stub it is
156 talking to uses the old or the new.
157 search tAA:PP,MM Search backwards starting at address
158 AA for a match with pattern PP and
159 mask MM. PP and MM are 4 bytes.
160 Not supported by all stubs.
162 general query qXXXX Request info about XXXX.
163 general set QXXXX=yyyy Set value of XXXX to yyyy.
164 query sect offs qOffsets Get section offsets. Reply is
165 Text=xxx;Data=yyy;Bss=zzz
167 Responses can be run-length encoded to save space. A '*' means that
168 the next character is an ASCII encoding giving a repeat count which
169 stands for that many repititions of the character preceding the '*'.
170 The encoding is n+29, yielding a printable character where n >=3
171 (which is where rle starts to win). Don't use an n > 126.
174 "0* " means the same as "0000". */
177 #include "gdb_string.h"
180 #include "inferior.h"
185 #include "terminal.h"
187 #include "objfiles.h"
188 #include "gdb-stabs.h"
194 #include <sys/types.h>
200 /* Prototypes for local functions */
202 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
203 char *myaddr, int len));
205 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
206 char *myaddr, int len));
208 static void remote_files_info PARAMS ((struct target_ops *ignore));
210 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
211 int len, int should_write,
212 struct target_ops *target));
214 static void remote_prepare_to_store PARAMS ((void));
216 static void remote_fetch_registers PARAMS ((int regno));
218 static void remote_resume PARAMS ((int pid, int step,
219 enum target_signal siggnal));
221 static int remote_start_remote PARAMS ((char *dummy));
223 static void remote_open PARAMS ((char *name, int from_tty));
225 static void extended_remote_open PARAMS ((char *name, int from_tty));
227 static void remote_open_1 PARAMS ((char *, int, struct target_ops *));
229 static void remote_close PARAMS ((int quitting));
231 static void remote_store_registers PARAMS ((int regno));
233 static void remote_mourn PARAMS ((void));
235 static void extended_remote_restart PARAMS ((void));
237 static void extended_remote_mourn PARAMS ((void));
239 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
241 static void remote_mourn_1 PARAMS ((struct target_ops *));
243 static void getpkt PARAMS ((char *buf, int forever));
245 static int putpkt PARAMS ((char *buf));
247 static void remote_send PARAMS ((char *buf));
249 static int readchar PARAMS ((int timeout));
251 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
253 static void remote_kill PARAMS ((void));
255 static int tohex PARAMS ((int nib));
257 static int fromhex PARAMS ((int a));
259 static void remote_detach PARAMS ((char *args, int from_tty));
261 static void remote_interrupt PARAMS ((int signo));
263 static void remote_interrupt_twice PARAMS ((int signo));
265 static void interrupt_query PARAMS ((void));
267 extern struct target_ops remote_ops; /* Forward decl */
268 extern struct target_ops extended_remote_ops; /* Forward decl */
270 /* This was 5 seconds, which is a long time to sit and wait.
271 Unless this is going though some terminal server or multiplexer or
272 other form of hairy serial connection, I would think 2 seconds would
275 static int remote_timeout = 2;
277 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
278 remote_open knows that we don't have a file open when the program
280 serial_t remote_desc = NULL;
282 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
283 and i386-stub.c. Normally, no one would notice because it only matters
284 for writing large chunks of memory (e.g. in downloads). Also, this needs
285 to be more than 400 if required to hold the registers (see below, where
286 we round it up based on REGISTER_BYTES). */
289 /* Maximum number of bytes to read/write at once. The value here
290 is chosen to fill up a packet (the headers account for the 32). */
291 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
293 /* Round up PBUFSIZ to hold all the registers, at least. */
294 /* The blank line after the #if seems to be required to work around a
295 bug in HP's PA compiler. */
296 #if REGISTER_BYTES > MAXBUFBYTES
299 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
302 /* Should we try the 'P' request? If this is set to one when the stub
303 doesn't support 'P', the only consequence is some unnecessary traffic. */
304 static int stub_supports_P = 1;
307 /* These are the threads which we last sent to the remote system. -1 for all
308 or -2 for not sent yet. */
318 int state = gen ? general_thread : cont_thread;
322 buf[1] = gen ? 'g' : 'c';
329 sprintf (&buf[2], "-%x", -th);
331 sprintf (&buf[2], "%x", th);
340 /* Return nonzero if the thread TH is still alive on the remote system. */
343 remote_thread_alive (th)
350 sprintf (&buf[1], "-%x", -th);
352 sprintf (&buf[1], "%x", th);
355 return (buf[0] == 'O' && buf[1] == 'K');
358 /* Restart the remote side; this is an extended protocol operation. */
361 extended_remote_restart ()
365 /* Send the restart command; for reasons I don't understand the
366 remote side really expects a number after the "R". */
368 sprintf (&buf[1], "%x", 0);
371 /* Now query for status so this looks just like we restarted
372 gdbserver from scratch. */
377 /* Clean up connection to a remote debugger. */
381 remote_close (quitting)
385 SERIAL_CLOSE (remote_desc);
389 /* Query the remote side for the text, data and bss offsets. */
396 CORE_ADDR text_addr, data_addr, bss_addr;
397 struct section_offsets *offs;
403 if (buf[0] == '\000')
404 return; /* Return silently. Stub doesn't support this
408 warning ("Remote failure reply: %s", buf);
412 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
415 error ("Malformed response to offset query, %s", buf);
417 if (symfile_objfile == NULL)
420 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
421 + symfile_objfile->num_sections
422 * sizeof (offs->offsets));
423 memcpy (offs, symfile_objfile->section_offsets,
424 sizeof (struct section_offsets)
425 + symfile_objfile->num_sections
426 * sizeof (offs->offsets));
428 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
430 /* This is a temporary kludge to force data and bss to use the same offsets
431 because that's what nlmconv does now. The real solution requires changes
432 to the stub and remote.c that I don't have time to do right now. */
434 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
435 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
437 objfile_relocate (symfile_objfile, offs);
440 /* Stub for catch_errors. */
443 remote_start_remote (dummy)
446 immediate_quit = 1; /* Allow user to interrupt it */
448 /* Ack any packet which the remote side has already sent. */
449 SERIAL_WRITE (remote_desc, "+", 1);
451 /* Let the stub know that we want it to return the thread. */
454 get_offsets (); /* Get text, data & bss offsets */
456 putpkt ("?"); /* initiate a query from remote machine */
459 start_remote (); /* Initialize gdb process mechanisms */
463 /* Open a connection to a remote debugger.
464 NAME is the filename used for communication. */
467 remote_open (name, from_tty)
471 remote_open_1 (name, from_tty, &remote_ops);
474 /* Open a connection to a remote debugger using the extended
475 remote gdb protocol. NAME is hte filename used for communication. */
478 extended_remote_open (name, from_tty)
484 /* Do the basic remote open stuff. */
485 remote_open_1 (name, from_tty, &extended_remote_ops);
487 /* Now tell the remote that we're using the extended protocol. */
493 /* Generic code for opening a connection to a remote target. */
494 static DCACHE *remote_dcache;
497 remote_open_1 (name, from_tty, target)
500 struct target_ops *target;
503 error ("To open a remote debug connection, you need to specify what serial\n\
504 device is attached to the remote system (e.g. /dev/ttya).");
506 target_preopen (from_tty);
508 unpush_target (target);
510 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
512 remote_desc = SERIAL_OPEN (name);
514 perror_with_name (name);
518 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
520 SERIAL_CLOSE (remote_desc);
521 perror_with_name (name);
526 SERIAL_RAW (remote_desc);
528 /* If there is something sitting in the buffer we might take it as a
529 response to a command, which would be bad. */
530 SERIAL_FLUSH_INPUT (remote_desc);
534 puts_filtered ("Remote debugging using ");
535 puts_filtered (name);
536 puts_filtered ("\n");
538 push_target (target); /* Switch to using remote target now */
540 /* Start out by trying the 'P' request to set registers. We set this each
541 time that we open a new target so that if the user switches from one
542 stub to another, we can (if the target is closed and reopened) cope. */
548 /* Without this, some commands which require an active target (such as kill)
549 won't work. This variable serves (at least) double duty as both the pid
550 of the target process (if it has such), and as a flag indicating that a
551 target is active. These functions should be split out into seperate
552 variables, especially since GDB will someday have a notion of debugging
553 several processes. */
555 inferior_pid = 42000;
556 /* Start the remote connection; if error (0), discard this target.
557 In particular, if the user quits, be sure to discard it
558 (we'd be in an inconsistent state otherwise). */
559 if (!catch_errors (remote_start_remote, (char *)0,
560 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
565 takes a program previously attached to and detaches it.
566 We better not have left any breakpoints
567 in the program or it'll die when it hits one.
568 Close the open connection to the remote debugger.
569 Use this when you want to detach and do something else
573 remote_detach (args, from_tty)
578 error ("Argument given to \"detach\" when remotely debugging.");
582 puts_filtered ("Ending remote debugging.\n");
585 /* Convert hex digit A to a number. */
591 if (a >= '0' && a <= '9')
593 else if (a >= 'a' && a <= 'f')
596 error ("Reply contains invalid hex digit %d", a);
599 /* Convert number NIB to a hex digit. */
611 /* Tell the remote machine to resume. */
613 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
617 remote_resume (pid, step, siggnal)
619 enum target_signal siggnal;
624 set_thread (inferior_pid, 0);
628 dcache_flush (remote_dcache);
630 last_sent_signal = siggnal;
631 last_sent_step = step;
633 if (siggnal != TARGET_SIGNAL_0)
635 buf[0] = step ? 'S' : 'C';
636 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
637 buf[2] = tohex ((int)siggnal & 0xf);
641 strcpy (buf, step ? "s": "c");
646 /* Send ^C to target to halt it. Target will respond, and send us a
650 remote_interrupt (signo)
653 /* If this doesn't work, try more severe steps. */
654 signal (signo, remote_interrupt_twice);
657 printf_unfiltered ("remote_interrupt called\n");
659 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
662 static void (*ofunc)();
664 /* The user typed ^C twice. */
666 remote_interrupt_twice (signo)
669 signal (signo, ofunc);
673 signal (signo, remote_interrupt);
676 /* Ask the user what to do when an interrupt is received. */
681 target_terminal_ours ();
683 if (query ("Interrupted while waiting for the program.\n\
684 Give up (and stop debugging it)? "))
686 target_mourn_inferior ();
687 return_to_top_level (RETURN_QUIT);
690 target_terminal_inferior ();
693 /* If nonzero, ignore the next kill. */
696 /* Wait until the remote machine stops, then return,
697 storing status in STATUS just as `wait' would.
698 Returns "pid" (though it's not clear what, if anything, that
699 means in the case of this target). */
702 remote_wait (pid, status)
704 struct target_waitstatus *status;
706 unsigned char buf[PBUFSIZ];
709 status->kind = TARGET_WAITKIND_EXITED;
710 status->value.integer = 0;
716 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
717 getpkt ((char *) buf, 1);
718 signal (SIGINT, ofunc);
722 case 'E': /* Error of some sort */
723 warning ("Remote failure reply: %s", buf);
725 case 'T': /* Status with PC, SP, FP, ... */
729 char regs[MAX_REGISTER_RAW_SIZE];
731 /* Expedited reply, containing Signal, {regno, reg} repeat */
732 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
734 n... = register number
735 r... = register contents
738 p = &buf[3]; /* after Txx */
744 unsigned LONGEST val;
746 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
747 p1 = (unsigned char *)p_temp;
751 p1 = (unsigned char *) strchr ((const char *) p, ':');
753 warning ("Malformed packet (missing colon): %s\n\
756 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
758 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
759 p = (unsigned char *)p_temp;
767 warning ("Malformed packet (missing colon): %s\n\
771 if (regno >= NUM_REGS)
772 warning ("Remote sent bad register number %ld: %s\n\
777 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
779 if (p[0] == 0 || p[1] == 0)
780 warning ("Remote reply is too short: %s", buf);
781 val = val * 256 + fromhex (p[0]) * 16 + fromhex (p[1]);
785 store_unsigned_integer (regs, REGISTER_RAW_SIZE (regno), val);
786 supply_register (regno, regs);
790 warning ("Remote register badly formatted: %s", buf);
794 case 'S': /* Old style status, just signal only */
795 status->kind = TARGET_WAITKIND_STOPPED;
796 status->value.sig = (enum target_signal)
797 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
800 case 'W': /* Target exited */
802 /* The remote process exited. */
803 status->kind = TARGET_WAITKIND_EXITED;
804 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
808 status->kind = TARGET_WAITKIND_SIGNALLED;
809 status->value.sig = (enum target_signal)
810 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
814 case 'O': /* Console output */
815 for (p = buf + 1; *p; p +=2)
818 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
821 if (target_output_hook)
822 target_output_hook (tb);
824 fputs_filtered (tb, gdb_stdout);
828 if (last_sent_signal != TARGET_SIGNAL_0)
830 /* Zero length reply means that we tried 'S' or 'C' and
831 the remote system doesn't support it. */
832 target_terminal_ours_for_output ();
834 ("Can't send signals to this remote system. %s not sent.\n",
835 target_signal_to_name (last_sent_signal));
836 last_sent_signal = TARGET_SIGNAL_0;
837 target_terminal_inferior ();
839 strcpy ((char *) buf, last_sent_step ? "s" : "c");
840 putpkt ((char *) buf);
843 /* else fallthrough */
845 warning ("Invalid remote reply: %s", buf);
850 if (thread_num != -1)
852 /* Initial thread value can only be acquired via wait, so deal with
853 this marker which is used before the first thread value is
855 if (inferior_pid == 42000)
857 inferior_pid = thread_num;
858 add_thread (inferior_pid);
865 /* Number of bytes of registers this stub implements. */
866 static int register_bytes_found;
868 /* Read the remote registers into the block REGS. */
869 /* Currently we just read all the registers, so we don't use regno. */
872 remote_fetch_registers (regno)
878 char regs[REGISTER_BYTES];
880 set_thread (inferior_pid, 1);
885 /* Unimplemented registers read as all bits zero. */
886 memset (regs, 0, REGISTER_BYTES);
888 /* We can get out of synch in various cases. If the first character
889 in the buffer is not a hex character, assume that has happened
890 and try to fetch another packet to read. */
891 while ((buf[0] < '0' || buf[0] > '9')
892 && (buf[0] < 'a' || buf[0] > 'f'))
895 printf_unfiltered ("Bad register packet; fetching a new packet\n");
899 /* Reply describes registers byte by byte, each byte encoded as two
900 hex characters. Suck them all up, then supply them to the
901 register cacheing/storage mechanism. */
904 for (i = 0; i < REGISTER_BYTES; i++)
910 warning ("Remote reply is of odd length: %s", buf);
911 /* Don't change register_bytes_found in this case, and don't
912 print a second warning. */
915 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
919 if (i != register_bytes_found)
921 register_bytes_found = i;
922 #ifdef REGISTER_BYTES_OK
923 if (!REGISTER_BYTES_OK (i))
924 warning ("Remote reply is too short: %s", buf);
929 for (i = 0; i < NUM_REGS; i++)
930 supply_register (i, ®s[REGISTER_BYTE(i)]);
933 /* Prepare to store registers. Since we may send them all (using a
934 'G' request), we have to read out the ones we don't want to change
938 remote_prepare_to_store ()
940 /* Make sure the entire registers array is valid. */
941 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
944 /* Store register REGNO, or all registers if REGNO == -1, from the contents
945 of REGISTERS. FIXME: ignores errors. */
948 remote_store_registers (regno)
955 set_thread (inferior_pid, 1);
957 if (regno >= 0 && stub_supports_P)
959 /* Try storing a single register. */
962 sprintf (buf, "P%x=", regno);
963 p = buf + strlen (buf);
964 regp = ®isters[REGISTER_BYTE (regno)];
965 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
967 *p++ = tohex ((regp[i] >> 4) & 0xf);
968 *p++ = tohex (regp[i] & 0xf);
974 /* The stub understands the 'P' request. We are done. */
978 /* The stub does not support the 'P' request. Use 'G' instead,
979 and don't try using 'P' in the future (it will just waste our
986 /* Command describes registers byte by byte,
987 each byte encoded as two hex characters. */
990 /* remote_prepare_to_store insures that register_bytes_found gets set. */
991 for (i = 0; i < register_bytes_found; i++)
993 *p++ = tohex ((registers[i] >> 4) & 0xf);
994 *p++ = tohex (registers[i] & 0xf);
1002 Use of the data cache *used* to be disabled because it loses for looking at
1003 and changing hardware I/O ports and the like. Accepting `volatile'
1004 would perhaps be one way to fix it. Another idea would be to use the
1005 executable file for the text segment (for all SEC_CODE sections?
1006 For all SEC_READONLY sections?). This has problems if you want to
1007 actually see what the memory contains (e.g. self-modifying code,
1008 clobbered memory, user downloaded the wrong thing).
1010 Because it speeds so much up, it's now enabled, if you're playing
1011 with registers you turn it of (set remotecache 0)
1014 /* Read a word from remote address ADDR and return it.
1015 This goes through the data cache. */
1019 remote_fetch_word (addr)
1022 return dcache_fetch (remote_dcache, addr);
1025 /* Write a word WORD into remote address ADDR.
1026 This goes through the data cache. */
1029 remote_store_word (addr, word)
1033 dcache_poke (remote_dcache, addr, word);
1035 #endif /* 0 (unused?) */
1038 /* Write memory data directly to the remote machine.
1039 This does not inform the data cache; the data cache uses this.
1040 MEMADDR is the address in the remote memory space.
1041 MYADDR is the address of the buffer in our space.
1042 LEN is the number of bytes.
1044 Returns number of bytes transferred, or 0 for error. */
1047 remote_write_bytes (memaddr, myaddr, len)
1056 /* Chop the transfer down if necessary */
1061 int todo = len - done;
1062 int cando = PBUFSIZ /2 - 32; /* number of bytes that will fit. */
1066 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1067 result in a buffer like sprintf. */
1068 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr + done, todo);
1070 /* We send target system values byte by byte, in increasing byte addresses,
1071 each byte encoded as two hex characters. */
1073 p = buf + strlen (buf);
1074 for (i = 0; i < todo; i++)
1076 *p++ = tohex ((myaddr[i + done] >> 4) & 0xf);
1077 *p++ = tohex (myaddr[i + done] & 0xf);
1086 /* There is no correspondance between what the remote protocol uses
1087 for errors and errno codes. We would like a cleaner way of
1088 representing errors (big enough to include errno codes, bfd_error
1089 codes, and others). But for now just return EIO. */
1098 /* Read memory data directly from the remote machine.
1099 This does not use the data cache; the data cache uses this.
1100 MEMADDR is the address in the remote memory space.
1101 MYADDR is the address of the buffer in our space.
1102 LEN is the number of bytes.
1104 Returns number of bytes transferred, or 0 for error. */
1107 remote_read_bytes (memaddr, myaddr, len)
1116 if (len > PBUFSIZ / 2 - 1)
1119 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1120 result in a buffer like sprintf. */
1121 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, len);
1127 /* There is no correspondance between what the remote protocol uses
1128 for errors and errno codes. We would like a cleaner way of
1129 representing errors (big enough to include errno codes, bfd_error
1130 codes, and others). But for now just return EIO. */
1135 /* Reply describes memory byte by byte,
1136 each byte encoded as two hex characters. */
1139 for (i = 0; i < len; i++)
1141 if (p[0] == 0 || p[1] == 0)
1142 /* Reply is short. This means that we were able to read only part
1143 of what we wanted to. */
1145 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1151 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1152 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1153 nonzero. Returns length of data written or read; 0 for error. */
1157 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1162 struct target_ops *target; /* ignored */
1164 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
1169 /* Enable after 4.12. */
1172 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1173 addr_found, data_found)
1177 CORE_ADDR startaddr;
1181 CORE_ADDR *addr_found;
1184 if (increment == -4 && len == 4)
1186 long mask_long, data_long;
1187 long data_found_long;
1188 CORE_ADDR addr_we_found;
1190 long returned_long[2];
1193 mask_long = extract_unsigned_integer (mask, len);
1194 data_long = extract_unsigned_integer (data, len);
1195 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1200 /* The stub doesn't support the 't' request. We might want to
1201 remember this fact, but on the other hand the stub could be
1202 switched on us. Maybe we should remember it only until
1203 the next "target remote". */
1204 generic_search (len, data, mask, startaddr, increment, lorange,
1205 hirange, addr_found, data_found);
1210 /* There is no correspondance between what the remote protocol uses
1211 for errors and errno codes. We would like a cleaner way of
1212 representing errors (big enough to include errno codes, bfd_error
1213 codes, and others). But for now just use EIO. */
1214 memory_error (EIO, startaddr);
1217 while (*p != '\0' && *p != ',')
1218 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1220 error ("Protocol error: short return for search");
1222 data_found_long = 0;
1223 while (*p != '\0' && *p != ',')
1224 data_found_long = (data_found_long << 4) + fromhex (*p++);
1225 /* Ignore anything after this comma, for future extensions. */
1227 if (addr_we_found < lorange || addr_we_found >= hirange)
1233 *addr_found = addr_we_found;
1234 *data_found = store_unsigned_integer (data_we_found, len);
1237 generic_search (len, data, mask, startaddr, increment, lorange,
1238 hirange, addr_found, data_found);
1243 remote_files_info (ignore)
1244 struct target_ops *ignore;
1246 puts_filtered ("Debugging a target over a serial line.\n");
1249 /* Stuff for dealing with the packets which are part of this protocol.
1250 See comment at top of file for details. */
1252 /* Read a single character from the remote end, masking it down to 7 bits. */
1260 ch = SERIAL_READCHAR (remote_desc, timeout);
1265 error ("Remote connection closed");
1267 perror_with_name ("Remote communication error");
1268 case SERIAL_TIMEOUT:
1275 /* Send the command in BUF to the remote machine,
1276 and read the reply into BUF.
1277 Report an error if we get an error reply. */
1287 error ("Remote failure reply: %s", buf);
1290 /* Send a packet to the remote machine, with error checking.
1291 The data of the packet is in BUF. */
1298 unsigned char csum = 0;
1300 int cnt = strlen (buf);
1305 /* Copy the packet into buffer BUF2, encapsulating it
1306 and giving it a checksum. */
1308 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
1314 for (i = 0; i < cnt; i++)
1320 *p++ = tohex ((csum >> 4) & 0xf);
1321 *p++ = tohex (csum & 0xf);
1323 /* Send it over and over until we get a positive ack. */
1327 int started_error_output = 0;
1332 printf_unfiltered ("Sending packet: %s...", buf2);
1333 gdb_flush(gdb_stdout);
1335 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1336 perror_with_name ("putpkt: write failed");
1338 /* read until either a timeout occurs (-2) or '+' is read */
1341 ch = readchar (remote_timeout);
1348 case SERIAL_TIMEOUT:
1350 if (started_error_output)
1352 putchar_unfiltered ('\n');
1353 started_error_output = 0;
1362 printf_unfiltered("Ack\n");
1364 case SERIAL_TIMEOUT:
1368 break; /* Retransmit buffer */
1371 char junkbuf[PBUFSIZ];
1373 /* It's probably an old response, and we're out of sync. Just
1374 gobble up the packet and ignore it. */
1375 getpkt (junkbuf, 0);
1376 continue; /* Now, go look for + */
1381 if (!started_error_output)
1383 started_error_output = 1;
1384 printf_unfiltered ("putpkt: Junk: ");
1386 putchar_unfiltered (ch & 0177);
1390 break; /* Here to retransmit */
1394 /* This is wrong. If doing a long backtrace, the user should be
1395 able to get out next time we call QUIT, without anything as violent
1396 as interrupt_query. If we want to provide a way out of here
1397 without getting to the next QUIT, it should be based on hitting
1398 ^C twice as in remote_wait. */
1408 /* Come here after finding the start of the frame. Collect the rest into BUF,
1409 verifying the checksum, length, and handling run-length compression.
1410 Returns 0 on any error, 1 on success. */
1425 c = readchar (remote_timeout);
1429 case SERIAL_TIMEOUT:
1431 puts_filtered ("Timeout in mid-packet, retrying\n");
1435 puts_filtered ("Saw new packet start in middle of old one\n");
1436 return 0; /* Start a new packet, count retries */
1439 unsigned char pktcsum;
1443 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1444 pktcsum |= fromhex (readchar (remote_timeout));
1446 if (csum == pktcsum)
1451 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1453 puts_filtered (buf);
1454 puts_filtered ("\n");
1458 case '*': /* Run length encoding */
1460 c = readchar (remote_timeout);
1462 c = c - ' ' + 3; /* Compute repeat count */
1465 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
1467 memset (bp, *(bp - 1), c);
1473 printf_filtered ("Repeat count %d too large for buffer: ", c);
1474 puts_filtered (buf);
1475 puts_filtered ("\n");
1479 if (bp < buf + PBUFSIZ - 1)
1487 puts_filtered ("Remote packet too long: ");
1488 puts_filtered (buf);
1489 puts_filtered ("\n");
1496 /* Read a packet from the remote machine, with error checking,
1497 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1498 If FOREVER, wait forever rather than timing out; this is used
1499 while the target is executing user code. */
1502 getpkt (buf, forever)
1511 strcpy (buf,"timeout");
1515 #ifdef MAINTENANCE_CMDS
1516 timeout = watchdog > 0 ? watchdog : -1;
1523 timeout = remote_timeout;
1527 for (tries = 1; tries <= MAX_TRIES; tries++)
1529 /* This can loop forever if the remote side sends us characters
1530 continuously, but if it pauses, we'll get a zero from readchar
1531 because of timeout. Then we'll count that as a retry. */
1533 /* Note that we will only wait forever prior to the start of a packet.
1534 After that, we expect characters to arrive at a brisk pace. They
1535 should show up within remote_timeout intervals. */
1539 c = readchar (timeout);
1541 if (c == SERIAL_TIMEOUT)
1543 #ifdef MAINTENANCE_CMDS
1544 if (forever) /* Watchdog went off. Kill the target. */
1546 target_mourn_inferior ();
1547 error ("Watchdog has expired. Target detached.\n");
1551 puts_filtered ("Timed out.\n");
1557 /* We've found the start of a packet, now collect the data. */
1559 val = read_frame (buf);
1564 fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
1565 SERIAL_WRITE (remote_desc, "+", 1);
1569 /* Try the whole thing again. */
1571 SERIAL_WRITE (remote_desc, "-", 1);
1574 /* We have tried hard enough, and just can't receive the packet. Give up. */
1576 printf_unfiltered ("Ignoring packet error, continuing...\n");
1577 SERIAL_WRITE (remote_desc, "+", 1);
1583 /* For some mysterious reason, wait_for_inferior calls kill instead of
1584 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1588 target_mourn_inferior ();
1592 /* Use catch_errors so the user can quit from gdb even when we aren't on
1593 speaking terms with the remote system. */
1594 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
1596 /* Don't wait for it to die. I'm not really sure it matters whether
1597 we do or not. For the existing stubs, kill is a noop. */
1598 target_mourn_inferior ();
1604 remote_mourn_1 (&remote_ops);
1608 extended_remote_mourn ()
1610 /* We do _not_ want to mourn the target like this; this will
1611 remove the extended remote target from the target stack,
1612 and the next time the user says "run" it'll fail.
1614 FIXME: What is the right thing to do here? */
1616 remote_mourn_1 (&extended_remote_ops);
1620 /* Worker function for remote_mourn. */
1622 remote_mourn_1 (target)
1623 struct target_ops *target;
1625 unpush_target (target);
1626 generic_mourn_inferior ();
1629 /* In the extended protocol we want to be able to do things like
1630 "run" and have them basically work as expected. So we need
1631 a special create_inferior function.
1633 FIXME: One day add support for changing the exec file
1634 we're debugging, arguments and an environment. */
1637 extended_remote_create_inferior (exec_file, args, env)
1642 /* Rip out the breakpoints; we'll reinsert them after restarting
1643 the remote server. */
1644 remove_breakpoints ();
1646 /* Now restart the remote server. */
1647 extended_remote_restart ();
1649 /* Now put the breakpoints back in. This way we're safe if the
1650 restart function works via a unix fork on the remote side. */
1651 insert_breakpoints ();
1653 /* Clean up from the last time we were running. */
1654 clear_proceed_status ();
1656 /* Let the remote process run. */
1657 proceed (-1, TARGET_SIGNAL_0, 0);
1661 #ifdef REMOTE_BREAKPOINT
1663 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1664 than other targets. */
1665 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1667 #else /* No REMOTE_BREAKPOINT. */
1669 /* Same old breakpoint instruction. This code does nothing different
1670 than mem-break.c. */
1671 static unsigned char break_insn[] = BREAKPOINT;
1673 #endif /* No REMOTE_BREAKPOINT. */
1675 /* Insert a breakpoint on targets that don't have any better breakpoint
1676 support. We read the contents of the target location and stash it,
1677 then overwrite it with a breakpoint instruction. ADDR is the target
1678 location in the target machine. CONTENTS_CACHE is a pointer to
1679 memory allocated for saving the target contents. It is guaranteed
1680 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1681 is accomplished via BREAKPOINT_MAX). */
1684 remote_insert_breakpoint (addr, contents_cache)
1686 char *contents_cache;
1690 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1693 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1699 remote_remove_breakpoint (addr, contents_cache)
1701 char *contents_cache;
1703 return target_write_memory (addr, contents_cache, sizeof break_insn);
1706 /* Define the target subroutine names */
1708 struct target_ops remote_ops = {
1709 "remote", /* to_shortname */
1710 "Remote serial target in gdb-specific protocol", /* to_longname */
1711 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1712 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1713 remote_open, /* to_open */
1714 remote_close, /* to_close */
1715 NULL, /* to_attach */
1716 remote_detach, /* to_detach */
1717 remote_resume, /* to_resume */
1718 remote_wait, /* to_wait */
1719 remote_fetch_registers, /* to_fetch_registers */
1720 remote_store_registers, /* to_store_registers */
1721 remote_prepare_to_store, /* to_prepare_to_store */
1722 remote_xfer_memory, /* to_xfer_memory */
1723 remote_files_info, /* to_files_info */
1725 remote_insert_breakpoint, /* to_insert_breakpoint */
1726 remote_remove_breakpoint, /* to_remove_breakpoint */
1728 NULL, /* to_terminal_init */
1729 NULL, /* to_terminal_inferior */
1730 NULL, /* to_terminal_ours_for_output */
1731 NULL, /* to_terminal_ours */
1732 NULL, /* to_terminal_info */
1733 remote_kill, /* to_kill */
1734 generic_load, /* to_load */
1735 NULL, /* to_lookup_symbol */
1736 NULL, /* to_create_inferior */
1737 remote_mourn, /* to_mourn_inferior */
1739 0, /* to_notice_signals */
1740 remote_thread_alive, /* to_thread_alive */
1742 process_stratum, /* to_stratum */
1744 1, /* to_has_all_memory */
1745 1, /* to_has_memory */
1746 1, /* to_has_stack */
1747 1, /* to_has_registers */
1748 1, /* to_has_execution */
1749 NULL, /* sections */
1750 NULL, /* sections_end */
1751 OPS_MAGIC /* to_magic */
1754 struct target_ops extended_remote_ops = {
1755 "extended-remote", /* to_shortname */
1756 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1757 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1758 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1759 extended_remote_open, /* to_open */
1760 remote_close, /* to_close */
1761 NULL, /* to_attach */
1762 remote_detach, /* to_detach */
1763 remote_resume, /* to_resume */
1764 remote_wait, /* to_wait */
1765 remote_fetch_registers, /* to_fetch_registers */
1766 remote_store_registers, /* to_store_registers */
1767 remote_prepare_to_store, /* to_prepare_to_store */
1768 remote_xfer_memory, /* to_xfer_memory */
1769 remote_files_info, /* to_files_info */
1771 remote_insert_breakpoint, /* to_insert_breakpoint */
1772 remote_remove_breakpoint, /* to_remove_breakpoint */
1774 NULL, /* to_terminal_init */
1775 NULL, /* to_terminal_inferior */
1776 NULL, /* to_terminal_ours_for_output */
1777 NULL, /* to_terminal_ours */
1778 NULL, /* to_terminal_info */
1779 remote_kill, /* to_kill */
1780 generic_load, /* to_load */
1781 NULL, /* to_lookup_symbol */
1782 extended_remote_create_inferior,/* to_create_inferior */
1783 extended_remote_mourn, /* to_mourn_inferior */
1785 0, /* to_notice_signals */
1786 remote_thread_alive, /* to_thread_alive */
1788 process_stratum, /* to_stratum */
1790 1, /* to_has_all_memory */
1791 1, /* to_has_memory */
1792 1, /* to_has_stack */
1793 1, /* to_has_registers */
1794 1, /* to_has_execution */
1795 NULL, /* sections */
1796 NULL, /* sections_end */
1797 OPS_MAGIC /* to_magic */
1801 _initialize_remote ()
1803 add_target (&remote_ops);
1804 add_target (&extended_remote_ops);