1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 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..AA Continue with signal sig (hex signal
101 signal number). If ;AA..AA is omitted, resume
104 step with Ssig;AA..AA Like 'C' but step not continue.
107 last signal ? Reply the current reason for stopping.
108 This is the same reply as is generated
109 for step or cont : SAA where AA is the
114 There is no immediate reply to step or cont.
115 The reply comes when the machine stops.
116 It is SAA AA is the signal number.
118 or... TAAn...:r...;n...:r...;n...:r...;
120 n... = register number (hex)
121 r... = register contents
123 r... = thread process ID. This is
125 n... = other string not starting
126 with valid hex digit.
127 gdb should ignore this n,r pair
128 and go on to the next. This way
129 we can extend the protocol.
130 or... WAA The process exited, and AA is
131 the exit status. This is only
132 applicable for certains sorts of
134 or... XAA The process terminated with signal
136 or... OXX..XX XX..XX is hex encoding of ASCII data. This
137 can happen at any time while the program is
138 running and the debugger should
139 continue to wait for 'W', 'T', etc.
141 thread alive TXX Find out if the thread XX is alive.
142 reply OK thread is still alive
145 remote restart RXX Restart the remote server
147 extended ops ! Use the extended remote protocol.
148 Sticky -- only needs to be set once.
152 toggle debug d toggle debug flag (see 386 & 68k stubs)
153 reset r reset -- see sparc stub.
154 reserved <other> On other requests, the stub should
155 ignore the request and send an empty
156 response ($#<checksum>). This way
157 we can extend the protocol and GDB
158 can tell whether the stub it is
159 talking to uses the old or the new.
160 search tAA:PP,MM Search backwards starting at address
161 AA for a match with pattern PP and
162 mask MM. PP and MM are 4 bytes.
163 Not supported by all stubs.
165 general query qXXXX Request info about XXXX.
166 general set QXXXX=yyyy Set value of XXXX to yyyy.
167 query sect offs qOffsets Get section offsets. Reply is
168 Text=xxx;Data=yyy;Bss=zzz
170 Responses can be run-length encoded to save space. A '*' means that
171 the next character is an ASCII encoding giving a repeat count which
172 stands for that many repititions of the character preceding the '*'.
173 The encoding is n+29, yielding a printable character where n >=3
174 (which is where rle starts to win). Don't use an n > 126.
177 "0* " means the same as "0000". */
180 #include "gdb_string.h"
183 #include "inferior.h"
188 /*#include "terminal.h"*/
190 #include "objfiles.h"
191 #include "gdb-stabs.h"
192 #include "gdbthread.h"
197 #include <sys/types.h>
203 /* Prototypes for local functions */
205 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
206 char *myaddr, int len));
208 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
209 char *myaddr, int len));
211 static void remote_files_info PARAMS ((struct target_ops *ignore));
213 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
214 int len, int should_write,
215 struct target_ops *target));
217 static void remote_prepare_to_store PARAMS ((void));
219 static void remote_fetch_registers PARAMS ((int regno));
221 static void remote_resume PARAMS ((int pid, int step,
222 enum target_signal siggnal));
224 static int remote_start_remote PARAMS ((char *dummy));
226 static void remote_open PARAMS ((char *name, int from_tty));
228 static void extended_remote_open PARAMS ((char *name, int from_tty));
230 static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p));
232 static void remote_close PARAMS ((int quitting));
234 static void remote_store_registers PARAMS ((int regno));
236 static void remote_mourn PARAMS ((void));
238 static void extended_remote_restart PARAMS ((void));
240 static void extended_remote_mourn PARAMS ((void));
242 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
244 static void remote_mourn_1 PARAMS ((struct target_ops *));
246 static void remote_send PARAMS ((char *buf));
248 static int readchar PARAMS ((int timeout));
250 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
252 static void remote_kill PARAMS ((void));
254 static int tohex PARAMS ((int nib));
256 static void remote_detach PARAMS ((char *args, int from_tty));
258 static void remote_interrupt PARAMS ((int signo));
260 static void remote_interrupt_twice PARAMS ((int signo));
262 static void interrupt_query PARAMS ((void));
264 static void set_thread PARAMS ((int, int));
266 static int remote_thread_alive PARAMS ((int));
268 static void get_offsets PARAMS ((void));
270 static int read_frame PARAMS ((char *));
272 static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
274 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
276 static int hexnumlen PARAMS ((ULONGEST num));
278 /* exported functions */
280 extern int fromhex PARAMS ((int a));
281 extern void getpkt PARAMS ((char *buf, int forever));
282 extern int putpkt PARAMS ((char *buf));
284 /* Define the target subroutine names */
286 static struct target_ops remote_ops ;
288 static void init_remote_ops(void)
290 remote_ops.to_shortname = "remote";
291 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
292 remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
293 Specify the serial device it is connected to (e.g. /dev/ttya)." ;
294 remote_ops.to_open = remote_open;
295 remote_ops.to_close = remote_close;
296 remote_ops.to_attach = NULL;
297 remote_ops.to_detach = remote_detach;
298 remote_ops.to_resume = remote_resume;
299 remote_ops.to_wait = remote_wait;
300 remote_ops.to_fetch_registers = remote_fetch_registers;
301 remote_ops.to_store_registers = remote_store_registers;
302 remote_ops.to_prepare_to_store = remote_prepare_to_store;
303 remote_ops.to_xfer_memory = remote_xfer_memory;
304 remote_ops.to_files_info = remote_files_info;
305 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
306 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
307 remote_ops.to_terminal_init = NULL;
308 remote_ops.to_terminal_inferior = NULL;
309 remote_ops.to_terminal_ours_for_output = NULL;
310 remote_ops.to_terminal_ours = NULL;
311 remote_ops.to_terminal_info = NULL;
312 remote_ops.to_kill = remote_kill;
313 remote_ops.to_load = generic_load;
314 remote_ops.to_lookup_symbol = NULL;
315 remote_ops.to_create_inferior = NULL;
316 remote_ops.to_mourn_inferior = remote_mourn;
317 remote_ops.to_can_run = 0;
318 remote_ops.to_notice_signals = 0;
319 remote_ops.to_thread_alive = remote_thread_alive;
320 remote_ops.to_stop = 0;
321 remote_ops.to_stratum = process_stratum;
322 remote_ops.DONT_USE = NULL;
323 remote_ops.to_has_all_memory = 1;
324 remote_ops.to_has_memory = 1;
325 remote_ops.to_has_stack = 1;
326 remote_ops.to_has_registers = 1;
327 remote_ops.to_has_execution = 1;
328 remote_ops.to_sections = NULL;
329 remote_ops.to_sections_end = NULL;
330 remote_ops.to_magic = OPS_MAGIC ;
331 } /* init_remote_ops */
333 static struct target_ops extended_remote_ops ;
335 static void init_extended_remote_ops(void)
337 extended_remote_ops.to_shortname = "extended-remote";
338 extended_remote_ops.to_longname = "Extended remote serial target in gdb-specific protocol";
339 extended_remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
340 Specify the serial device it is connected to (e.g. /dev/ttya).",
341 extended_remote_ops.to_open = extended_remote_open;
342 extended_remote_ops.to_close = remote_close;
343 extended_remote_ops.to_attach = NULL;
344 extended_remote_ops.to_detach = remote_detach;
345 extended_remote_ops.to_resume = remote_resume;
346 extended_remote_ops.to_wait = remote_wait;
347 extended_remote_ops.to_fetch_registers = remote_fetch_registers;
348 extended_remote_ops.to_store_registers = remote_store_registers;
349 extended_remote_ops.to_prepare_to_store = remote_prepare_to_store;
350 extended_remote_ops.to_xfer_memory = remote_xfer_memory;
351 extended_remote_ops.to_files_info = remote_files_info;
352 extended_remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
353 extended_remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
354 extended_remote_ops.to_terminal_init = NULL;
355 extended_remote_ops.to_terminal_inferior = NULL;
356 extended_remote_ops.to_terminal_ours_for_output = NULL;
357 extended_remote_ops.to_terminal_ours = NULL;
358 extended_remote_ops.to_terminal_info = NULL;
359 extended_remote_ops.to_kill = remote_kill;
360 extended_remote_ops.to_load = generic_load;
361 extended_remote_ops.to_lookup_symbol = NULL;
362 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
363 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
364 extended_remote_ops.to_can_run = 0;
365 extended_remote_ops.to_notice_signals = 0;
366 extended_remote_ops.to_thread_alive = remote_thread_alive;
367 extended_remote_ops.to_stop = 0;
368 extended_remote_ops.to_stratum = process_stratum;
369 extended_remote_ops.DONT_USE = NULL;
370 extended_remote_ops.to_has_all_memory = 1;
371 extended_remote_ops.to_has_memory = 1;
372 extended_remote_ops.to_has_stack = 1;
373 extended_remote_ops.to_has_registers = 1;
374 extended_remote_ops.to_has_execution = 1;
375 extended_remote_ops.to_sections = NULL;
376 extended_remote_ops.to_sections_end = NULL;
377 extended_remote_ops.to_magic = OPS_MAGIC ;
381 /* This was 5 seconds, which is a long time to sit and wait.
382 Unless this is going though some terminal server or multiplexer or
383 other form of hairy serial connection, I would think 2 seconds would
386 /* Changed to allow option to set timeout value.
387 was static int remote_timeout = 2; */
388 extern int remote_timeout;
390 /* This variable chooses whether to send a ^C or a break when the user
391 requests program interruption. Although ^C is usually what remote
392 systems expect, and that is the default here, sometimes a break is
393 preferable instead. */
395 static int remote_break;
397 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
398 remote_open knows that we don't have a file open when the program
400 static serial_t remote_desc = NULL;
402 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
403 and i386-stub.c. Normally, no one would notice because it only matters
404 for writing large chunks of memory (e.g. in downloads). Also, this needs
405 to be more than 400 if required to hold the registers (see below, where
406 we round it up based on REGISTER_BYTES). */
409 /* Maximum number of bytes to read/write at once. The value here
410 is chosen to fill up a packet (the headers account for the 32). */
411 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
413 /* Round up PBUFSIZ to hold all the registers, at least. */
414 /* The blank line after the #if seems to be required to work around a
415 bug in HP's PA compiler. */
416 #if REGISTER_BYTES > MAXBUFBYTES
419 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
422 /* This variable sets the number of bytes to be written to the target
423 in a single packet. Normally PBUFSIZ is satisfactory, but some
424 targets need smaller values (perhaps because the receiving end
427 static int remote_write_size = PBUFSIZ;
429 /* This is the size (in chars) of the first response to the `g' command. This
430 is used to limit the size of the memory read and write commands to prevent
431 stub buffers from overflowing. The size does not include headers and
432 trailers, it is only the payload size. */
434 static int remote_register_buf_size = 0;
436 /* Should we try the 'P' request? If this is set to one when the stub
437 doesn't support 'P', the only consequence is some unnecessary traffic. */
438 static int stub_supports_P = 1;
440 /* These are pointers to hook functions that may be set in order to
441 modify resume/wait behavior for a particular architecture. */
443 void (*target_resume_hook) PARAMS ((void));
444 void (*target_wait_loop_hook) PARAMS ((void));
447 /* These are the threads which we last sent to the remote system. -1 for all
448 or -2 for not sent yet. */
458 int state = gen ? general_thread : cont_thread;
462 buf[1] = gen ? 'g' : 'c';
469 sprintf (&buf[2], "-%x", -th);
471 sprintf (&buf[2], "%x", th);
480 /* Return nonzero if the thread TH is still alive on the remote system. */
483 remote_thread_alive (th)
490 sprintf (&buf[1], "-%x", -th);
492 sprintf (&buf[1], "%x", th);
495 return (buf[0] == 'O' && buf[1] == 'K');
498 /* Restart the remote side; this is an extended protocol operation. */
501 extended_remote_restart ()
505 /* Send the restart command; for reasons I don't understand the
506 remote side really expects a number after the "R". */
508 sprintf (&buf[1], "%x", 0);
511 /* Now query for status so this looks just like we restarted
512 gdbserver from scratch. */
517 /* Clean up connection to a remote debugger. */
521 remote_close (quitting)
525 SERIAL_CLOSE (remote_desc);
529 /* Query the remote side for the text, data and bss offsets. */
534 char buf[PBUFSIZ], *ptr;
536 CORE_ADDR text_addr, data_addr, bss_addr;
537 struct section_offsets *offs;
543 if (buf[0] == '\000')
544 return; /* Return silently. Stub doesn't support this
548 warning ("Remote failure reply: %s", buf);
552 /* Pick up each field in turn. This used to be done with scanf, but
553 scanf will make trouble if CORE_ADDR size doesn't match
554 conversion directives correctly. The following code will work
555 with any size of CORE_ADDR. */
556 text_addr = data_addr = bss_addr = 0;
560 if (strncmp (ptr, "Text=", 5) == 0)
563 /* Don't use strtol, could lose on big values. */
564 while (*ptr && *ptr != ';')
565 text_addr = (text_addr << 4) + fromhex (*ptr++);
570 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
573 while (*ptr && *ptr != ';')
574 data_addr = (data_addr << 4) + fromhex (*ptr++);
579 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
582 while (*ptr && *ptr != ';')
583 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
589 error ("Malformed response to offset query, %s", buf);
591 if (symfile_objfile == NULL)
594 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
595 + symfile_objfile->num_sections
596 * sizeof (offs->offsets));
597 memcpy (offs, symfile_objfile->section_offsets,
598 sizeof (struct section_offsets)
599 + symfile_objfile->num_sections
600 * sizeof (offs->offsets));
602 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
604 /* This is a temporary kludge to force data and bss to use the same offsets
605 because that's what nlmconv does now. The real solution requires changes
606 to the stub and remote.c that I don't have time to do right now. */
608 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
609 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
611 objfile_relocate (symfile_objfile, offs);
614 /* Stub for catch_errors. */
617 remote_start_remote (dummy)
620 immediate_quit = 1; /* Allow user to interrupt it */
622 /* Ack any packet which the remote side has already sent. */
623 SERIAL_WRITE (remote_desc, "+", 1);
625 /* Let the stub know that we want it to return the thread. */
628 get_offsets (); /* Get text, data & bss offsets */
630 putpkt ("?"); /* initiate a query from remote machine */
633 start_remote (); /* Initialize gdb process mechanisms */
637 /* Open a connection to a remote debugger.
638 NAME is the filename used for communication. */
641 remote_open (name, from_tty)
645 remote_open_1 (name, from_tty, &remote_ops, 0);
648 /* Open a connection to a remote debugger using the extended
649 remote gdb protocol. NAME is the filename used for communication. */
652 extended_remote_open (name, from_tty)
656 remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/);
659 /* Generic code for opening a connection to a remote target. */
660 static DCACHE *remote_dcache;
663 remote_open_1 (name, from_tty, target, extended_p)
666 struct target_ops *target;
670 error ("To open a remote debug connection, you need to specify what serial\n\
671 device is attached to the remote system (e.g. /dev/ttya).");
673 target_preopen (from_tty);
675 unpush_target (target);
677 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
679 remote_desc = SERIAL_OPEN (name);
681 perror_with_name (name);
685 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
687 SERIAL_CLOSE (remote_desc);
688 perror_with_name (name);
693 SERIAL_RAW (remote_desc);
695 /* If there is something sitting in the buffer we might take it as a
696 response to a command, which would be bad. */
697 SERIAL_FLUSH_INPUT (remote_desc);
701 puts_filtered ("Remote debugging using ");
702 puts_filtered (name);
703 puts_filtered ("\n");
705 push_target (target); /* Switch to using remote target now */
707 /* Start out by trying the 'P' request to set registers. We set this each
708 time that we open a new target so that if the user switches from one
709 stub to another, we can (if the target is closed and reopened) cope. */
715 /* Without this, some commands which require an active target (such as kill)
716 won't work. This variable serves (at least) double duty as both the pid
717 of the target process (if it has such), and as a flag indicating that a
718 target is active. These functions should be split out into seperate
719 variables, especially since GDB will someday have a notion of debugging
720 several processes. */
722 inferior_pid = 42000;
723 /* Start the remote connection; if error (0), discard this target.
724 In particular, if the user quits, be sure to discard it
725 (we'd be in an inconsistent state otherwise). */
726 if (!catch_errors (remote_start_remote, (char *)0,
727 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
735 /* tell the remote that we're using the extended protocol. */
742 /* This takes a program previously attached to and detaches it. After
743 this is done, GDB can be used to debug some other program. We
744 better not have left any breakpoints in the target program or it'll
745 die when it hits one. */
748 remote_detach (args, from_tty)
755 error ("Argument given to \"detach\" when remotely debugging.");
757 /* Tell the remote target to detach. */
763 puts_filtered ("Ending remote debugging.\n");
766 /* Convert hex digit A to a number. */
772 if (a >= '0' && a <= '9')
774 else if (a >= 'a' && a <= 'f')
776 else if (a >= 'A' && a <= 'F')
779 error ("Reply contains invalid hex digit %d", a);
782 /* Convert number NIB to a hex digit. */
794 /* Tell the remote machine to resume. */
796 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
800 remote_resume (pid, step, siggnal)
802 enum target_signal siggnal;
807 set_thread (inferior_pid, 0);
811 dcache_flush (remote_dcache);
813 last_sent_signal = siggnal;
814 last_sent_step = step;
816 /* A hook for when we need to do something at the last moment before
818 if (target_resume_hook)
819 (*target_resume_hook) ();
821 if (siggnal != TARGET_SIGNAL_0)
823 buf[0] = step ? 'S' : 'C';
824 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
825 buf[2] = tohex ((int)siggnal & 0xf);
829 strcpy (buf, step ? "s": "c");
834 /* Send ^C to target to halt it. Target will respond, and send us a
838 remote_interrupt (signo)
841 /* If this doesn't work, try more severe steps. */
842 signal (signo, remote_interrupt_twice);
845 printf_unfiltered ("remote_interrupt called\n");
847 /* Send a break or a ^C, depending on user preference. */
849 SERIAL_SEND_BREAK (remote_desc);
851 SERIAL_WRITE (remote_desc, "\003", 1);
854 static void (*ofunc)();
856 /* The user typed ^C twice. */
858 remote_interrupt_twice (signo)
861 signal (signo, ofunc);
865 signal (signo, remote_interrupt);
868 /* Ask the user what to do when an interrupt is received. */
873 target_terminal_ours ();
875 if (query ("Interrupted while waiting for the program.\n\
876 Give up (and stop debugging it)? "))
878 target_mourn_inferior ();
879 return_to_top_level (RETURN_QUIT);
882 target_terminal_inferior ();
885 /* If nonzero, ignore the next kill. */
889 remote_console_output (msg)
894 for (p = msg; *p; p +=2)
897 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
900 if (target_output_hook)
901 target_output_hook (tb);
903 fputs_filtered (tb, gdb_stdout);
907 /* Wait until the remote machine stops, then return,
908 storing status in STATUS just as `wait' would.
909 Returns "pid" (though it's not clear what, if anything, that
910 means in the case of this target). */
913 remote_wait (pid, status)
915 struct target_waitstatus *status;
917 unsigned char buf[PBUFSIZ];
920 status->kind = TARGET_WAITKIND_EXITED;
921 status->value.integer = 0;
927 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
928 getpkt ((char *) buf, 1);
929 signal (SIGINT, ofunc);
931 /* This is a hook for when we need to do something (perhaps the
932 collection of trace data) every time the target stops. */
933 if (target_wait_loop_hook)
934 (*target_wait_loop_hook) ();
938 case 'E': /* Error of some sort */
939 warning ("Remote failure reply: %s", buf);
941 case 'T': /* Status with PC, SP, FP, ... */
945 char regs[MAX_REGISTER_RAW_SIZE];
947 /* Expedited reply, containing Signal, {regno, reg} repeat */
948 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
950 n... = register number
951 r... = register contents
953 p = &buf[3]; /* after Txx */
960 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
961 p1 = (unsigned char *)p_temp;
965 p1 = (unsigned char *) strchr ((const char *) p, ':');
967 warning ("Malformed packet (missing colon): %s\n\
970 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
972 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
973 p = (unsigned char *)p_temp;
981 warning ("Malformed packet (missing colon): %s\n\
985 if (regno >= NUM_REGS)
986 warning ("Remote sent bad register number %ld: %s\n\
990 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
992 if (p[0] == 0 || p[1] == 0)
993 warning ("Remote reply is too short: %s", buf);
994 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
997 supply_register (regno, regs);
1001 warning ("Remote register badly formatted: %s", buf);
1005 case 'S': /* Old style status, just signal only */
1006 status->kind = TARGET_WAITKIND_STOPPED;
1007 status->value.sig = (enum target_signal)
1008 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
1011 case 'W': /* Target exited */
1013 /* The remote process exited. */
1014 status->kind = TARGET_WAITKIND_EXITED;
1015 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
1019 status->kind = TARGET_WAITKIND_SIGNALLED;
1020 status->value.sig = (enum target_signal)
1021 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
1025 case 'O': /* Console output */
1026 remote_console_output (buf + 1);
1029 if (last_sent_signal != TARGET_SIGNAL_0)
1031 /* Zero length reply means that we tried 'S' or 'C' and
1032 the remote system doesn't support it. */
1033 target_terminal_ours_for_output ();
1035 ("Can't send signals to this remote system. %s not sent.\n",
1036 target_signal_to_name (last_sent_signal));
1037 last_sent_signal = TARGET_SIGNAL_0;
1038 target_terminal_inferior ();
1040 strcpy ((char *) buf, last_sent_step ? "s" : "c");
1041 putpkt ((char *) buf);
1044 /* else fallthrough */
1046 warning ("Invalid remote reply: %s", buf);
1051 if (thread_num != -1)
1053 /* Initial thread value can only be acquired via wait, so deal with
1054 this marker which is used before the first thread value is
1056 if (inferior_pid == 42000)
1058 inferior_pid = thread_num;
1059 add_thread (inferior_pid);
1063 return inferior_pid;
1066 /* Number of bytes of registers this stub implements. */
1067 static int register_bytes_found;
1069 /* Read the remote registers into the block REGS. */
1070 /* Currently we just read all the registers, so we don't use regno. */
1073 remote_fetch_registers (regno)
1079 char regs[REGISTER_BYTES];
1081 set_thread (inferior_pid, 1);
1086 if (remote_register_buf_size == 0)
1087 remote_register_buf_size = strlen (buf);
1089 /* Unimplemented registers read as all bits zero. */
1090 memset (regs, 0, REGISTER_BYTES);
1092 /* We can get out of synch in various cases. If the first character
1093 in the buffer is not a hex character, assume that has happened
1094 and try to fetch another packet to read. */
1095 while ((buf[0] < '0' || buf[0] > '9')
1096 && (buf[0] < 'a' || buf[0] > 'f'))
1099 printf_unfiltered ("Bad register packet; fetching a new packet\n");
1103 /* Reply describes registers byte by byte, each byte encoded as two
1104 hex characters. Suck them all up, then supply them to the
1105 register cacheing/storage mechanism. */
1108 for (i = 0; i < REGISTER_BYTES; i++)
1114 warning ("Remote reply is of odd length: %s", buf);
1115 /* Don't change register_bytes_found in this case, and don't
1116 print a second warning. */
1119 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1123 if (i != register_bytes_found)
1125 register_bytes_found = i;
1126 #ifdef REGISTER_BYTES_OK
1127 if (!REGISTER_BYTES_OK (i))
1128 warning ("Remote reply is too short: %s", buf);
1133 for (i = 0; i < NUM_REGS; i++)
1134 supply_register (i, ®s[REGISTER_BYTE(i)]);
1137 /* Prepare to store registers. Since we may send them all (using a
1138 'G' request), we have to read out the ones we don't want to change
1142 remote_prepare_to_store ()
1144 /* Make sure the entire registers array is valid. */
1145 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
1148 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1149 of REGISTERS. FIXME: ignores errors. */
1152 remote_store_registers (regno)
1159 set_thread (inferior_pid, 1);
1161 if (regno >= 0 && stub_supports_P)
1163 /* Try storing a single register. */
1166 sprintf (buf, "P%x=", regno);
1167 p = buf + strlen (buf);
1168 regp = ®isters[REGISTER_BYTE (regno)];
1169 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
1171 *p++ = tohex ((regp[i] >> 4) & 0xf);
1172 *p++ = tohex (regp[i] & 0xf);
1178 /* The stub understands the 'P' request. We are done. */
1182 /* The stub does not support the 'P' request. Use 'G' instead,
1183 and don't try using 'P' in the future (it will just waste our
1185 stub_supports_P = 0;
1190 /* Command describes registers byte by byte,
1191 each byte encoded as two hex characters. */
1194 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1195 for (i = 0; i < register_bytes_found; i++)
1197 *p++ = tohex ((registers[i] >> 4) & 0xf);
1198 *p++ = tohex (registers[i] & 0xf);
1206 Use of the data cache *used* to be disabled because it loses for looking at
1207 and changing hardware I/O ports and the like. Accepting `volatile'
1208 would perhaps be one way to fix it. Another idea would be to use the
1209 executable file for the text segment (for all SEC_CODE sections?
1210 For all SEC_READONLY sections?). This has problems if you want to
1211 actually see what the memory contains (e.g. self-modifying code,
1212 clobbered memory, user downloaded the wrong thing).
1214 Because it speeds so much up, it's now enabled, if you're playing
1215 with registers you turn it of (set remotecache 0)
1218 /* Read a word from remote address ADDR and return it.
1219 This goes through the data cache. */
1223 remote_fetch_word (addr)
1226 return dcache_fetch (remote_dcache, addr);
1229 /* Write a word WORD into remote address ADDR.
1230 This goes through the data cache. */
1233 remote_store_word (addr, word)
1237 dcache_poke (remote_dcache, addr, word);
1239 #endif /* 0 (unused?) */
1243 /* Return the number of hex digits in num. */
1251 for (i = 0; num != 0; i++)
1257 /* Write memory data directly to the remote machine.
1258 This does not inform the data cache; the data cache uses this.
1259 MEMADDR is the address in the remote memory space.
1260 MYADDR is the address of the buffer in our space.
1261 LEN is the number of bytes.
1263 Returns number of bytes transferred, or 0 for error. */
1266 remote_write_bytes (memaddr, myaddr, len)
1271 int max_buf_size; /* Max size of packet output buffer */
1274 /* Chop the transfer down if necessary */
1276 max_buf_size = min (remote_write_size, PBUFSIZ);
1277 if (remote_register_buf_size != 0)
1278 max_buf_size = min (max_buf_size, remote_register_buf_size);
1280 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
1281 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
1291 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
1293 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1294 result in a buffer like sprintf. */
1295 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo);
1297 /* We send target system values byte by byte, in increasing byte addresses,
1298 each byte encoded as two hex characters. */
1300 p = buf + strlen (buf);
1301 for (i = 0; i < todo; i++)
1303 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
1304 *p++ = tohex (myaddr[i] & 0xf);
1313 /* There is no correspondance between what the remote protocol uses
1314 for errors and errno codes. We would like a cleaner way of
1315 representing errors (big enough to include errno codes, bfd_error
1316 codes, and others). But for now just return EIO. */
1327 /* Read memory data directly from the remote machine.
1328 This does not use the data cache; the data cache uses this.
1329 MEMADDR is the address in the remote memory space.
1330 MYADDR is the address of the buffer in our space.
1331 LEN is the number of bytes.
1333 Returns number of bytes transferred, or 0 for error. */
1336 remote_read_bytes (memaddr, myaddr, len)
1341 int max_buf_size; /* Max size of packet output buffer */
1344 /* Chop the transfer down if necessary */
1346 max_buf_size = min (remote_write_size, PBUFSIZ);
1347 if (remote_register_buf_size != 0)
1348 max_buf_size = min (max_buf_size, remote_register_buf_size);
1358 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
1360 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1361 result in a buffer like sprintf. */
1362 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo);
1368 /* There is no correspondance between what the remote protocol uses
1369 for errors and errno codes. We would like a cleaner way of
1370 representing errors (big enough to include errno codes, bfd_error
1371 codes, and others). But for now just return EIO. */
1376 /* Reply describes memory byte by byte,
1377 each byte encoded as two hex characters. */
1380 for (i = 0; i < todo; i++)
1382 if (p[0] == 0 || p[1] == 0)
1383 /* Reply is short. This means that we were able to read only part
1384 of what we wanted to. */
1385 return i + (origlen - len);
1386 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1396 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1397 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1398 nonzero. Returns length of data written or read; 0 for error. */
1402 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1407 struct target_ops *target; /* ignored */
1409 #ifdef REMOTE_TRANSLATE_XFER_ADDRESS
1412 REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen);
1419 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
1424 /* Enable after 4.12. */
1427 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1428 addr_found, data_found)
1432 CORE_ADDR startaddr;
1436 CORE_ADDR *addr_found;
1439 if (increment == -4 && len == 4)
1441 long mask_long, data_long;
1442 long data_found_long;
1443 CORE_ADDR addr_we_found;
1445 long returned_long[2];
1448 mask_long = extract_unsigned_integer (mask, len);
1449 data_long = extract_unsigned_integer (data, len);
1450 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1455 /* The stub doesn't support the 't' request. We might want to
1456 remember this fact, but on the other hand the stub could be
1457 switched on us. Maybe we should remember it only until
1458 the next "target remote". */
1459 generic_search (len, data, mask, startaddr, increment, lorange,
1460 hirange, addr_found, data_found);
1465 /* There is no correspondance between what the remote protocol uses
1466 for errors and errno codes. We would like a cleaner way of
1467 representing errors (big enough to include errno codes, bfd_error
1468 codes, and others). But for now just use EIO. */
1469 memory_error (EIO, startaddr);
1472 while (*p != '\0' && *p != ',')
1473 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1475 error ("Protocol error: short return for search");
1477 data_found_long = 0;
1478 while (*p != '\0' && *p != ',')
1479 data_found_long = (data_found_long << 4) + fromhex (*p++);
1480 /* Ignore anything after this comma, for future extensions. */
1482 if (addr_we_found < lorange || addr_we_found >= hirange)
1488 *addr_found = addr_we_found;
1489 *data_found = store_unsigned_integer (data_we_found, len);
1492 generic_search (len, data, mask, startaddr, increment, lorange,
1493 hirange, addr_found, data_found);
1498 remote_files_info (ignore)
1499 struct target_ops *ignore;
1501 puts_filtered ("Debugging a target over a serial line.\n");
1504 /* Stuff for dealing with the packets which are part of this protocol.
1505 See comment at top of file for details. */
1507 /* Read a single character from the remote end, masking it down to 7 bits. */
1515 ch = SERIAL_READCHAR (remote_desc, timeout);
1520 error ("Remote connection closed");
1522 perror_with_name ("Remote communication error");
1523 case SERIAL_TIMEOUT:
1530 /* Send the command in BUF to the remote machine,
1531 and read the reply into BUF.
1532 Report an error if we get an error reply. */
1542 error ("Remote failure reply: %s", buf);
1545 /* Send a packet to the remote machine, with error checking.
1546 The data of the packet is in BUF. */
1553 unsigned char csum = 0;
1555 int cnt = strlen (buf);
1560 /* Copy the packet into buffer BUF2, encapsulating it
1561 and giving it a checksum. */
1563 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
1569 for (i = 0; i < cnt; i++)
1575 *p++ = tohex ((csum >> 4) & 0xf);
1576 *p++ = tohex (csum & 0xf);
1578 /* Send it over and over until we get a positive ack. */
1582 int started_error_output = 0;
1587 printf_unfiltered ("Sending packet: %s...", buf2);
1588 gdb_flush(gdb_stdout);
1590 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1591 perror_with_name ("putpkt: write failed");
1593 /* read until either a timeout occurs (-2) or '+' is read */
1596 ch = readchar (remote_timeout);
1603 case SERIAL_TIMEOUT:
1605 if (started_error_output)
1607 putchar_unfiltered ('\n');
1608 started_error_output = 0;
1617 printf_unfiltered("Ack\n");
1619 case SERIAL_TIMEOUT:
1623 break; /* Retransmit buffer */
1626 char junkbuf[PBUFSIZ];
1628 /* It's probably an old response, and we're out of sync. Just
1629 gobble up the packet and ignore it. */
1630 getpkt (junkbuf, 0);
1631 continue; /* Now, go look for + */
1636 if (!started_error_output)
1638 started_error_output = 1;
1639 printf_unfiltered ("putpkt: Junk: ");
1641 putchar_unfiltered (ch & 0177);
1645 break; /* Here to retransmit */
1649 /* This is wrong. If doing a long backtrace, the user should be
1650 able to get out next time we call QUIT, without anything as violent
1651 as interrupt_query. If we want to provide a way out of here
1652 without getting to the next QUIT, it should be based on hitting
1653 ^C twice as in remote_wait. */
1663 /* Come here after finding the start of the frame. Collect the rest into BUF,
1664 verifying the checksum, length, and handling run-length compression.
1665 Returns 0 on any error, 1 on success. */
1680 c = readchar (remote_timeout);
1684 case SERIAL_TIMEOUT:
1686 puts_filtered ("Timeout in mid-packet, retrying\n");
1690 puts_filtered ("Saw new packet start in middle of old one\n");
1691 return 0; /* Start a new packet, count retries */
1694 unsigned char pktcsum;
1698 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1699 pktcsum |= fromhex (readchar (remote_timeout));
1701 if (csum == pktcsum)
1706 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1708 puts_filtered (buf);
1709 puts_filtered ("\n");
1713 case '*': /* Run length encoding */
1715 c = readchar (remote_timeout);
1717 c = c - ' ' + 3; /* Compute repeat count */
1720 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
1722 memset (bp, *(bp - 1), c);
1728 printf_filtered ("Repeat count %d too large for buffer: ", c);
1729 puts_filtered (buf);
1730 puts_filtered ("\n");
1734 if (bp < buf + PBUFSIZ - 1)
1742 puts_filtered ("Remote packet too long: ");
1743 puts_filtered (buf);
1744 puts_filtered ("\n");
1751 /* Read a packet from the remote machine, with error checking,
1752 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1753 If FOREVER, wait forever rather than timing out; this is used
1754 while the target is executing user code. */
1757 getpkt (buf, forever)
1766 strcpy (buf,"timeout");
1770 #ifdef MAINTENANCE_CMDS
1771 timeout = watchdog > 0 ? watchdog : -1;
1778 timeout = remote_timeout;
1782 for (tries = 1; tries <= MAX_TRIES; tries++)
1784 /* This can loop forever if the remote side sends us characters
1785 continuously, but if it pauses, we'll get a zero from readchar
1786 because of timeout. Then we'll count that as a retry. */
1788 /* Note that we will only wait forever prior to the start of a packet.
1789 After that, we expect characters to arrive at a brisk pace. They
1790 should show up within remote_timeout intervals. */
1794 c = readchar (timeout);
1796 if (c == SERIAL_TIMEOUT)
1798 #ifdef MAINTENANCE_CMDS
1799 if (forever) /* Watchdog went off. Kill the target. */
1801 target_mourn_inferior ();
1802 error ("Watchdog has expired. Target detached.\n");
1806 puts_filtered ("Timed out.\n");
1812 /* We've found the start of a packet, now collect the data. */
1814 val = read_frame (buf);
1819 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
1820 SERIAL_WRITE (remote_desc, "+", 1);
1824 /* Try the whole thing again. */
1826 SERIAL_WRITE (remote_desc, "-", 1);
1829 /* We have tried hard enough, and just can't receive the packet. Give up. */
1831 printf_unfiltered ("Ignoring packet error, continuing...\n");
1832 SERIAL_WRITE (remote_desc, "+", 1);
1838 /* For some mysterious reason, wait_for_inferior calls kill instead of
1839 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1843 target_mourn_inferior ();
1847 /* Use catch_errors so the user can quit from gdb even when we aren't on
1848 speaking terms with the remote system. */
1849 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
1851 /* Don't wait for it to die. I'm not really sure it matters whether
1852 we do or not. For the existing stubs, kill is a noop. */
1853 target_mourn_inferior ();
1859 remote_mourn_1 (&remote_ops);
1863 extended_remote_mourn ()
1865 /* We do _not_ want to mourn the target like this; this will
1866 remove the extended remote target from the target stack,
1867 and the next time the user says "run" it'll fail.
1869 FIXME: What is the right thing to do here? */
1871 remote_mourn_1 (&extended_remote_ops);
1875 /* Worker function for remote_mourn. */
1877 remote_mourn_1 (target)
1878 struct target_ops *target;
1880 unpush_target (target);
1881 generic_mourn_inferior ();
1884 /* In the extended protocol we want to be able to do things like
1885 "run" and have them basically work as expected. So we need
1886 a special create_inferior function.
1888 FIXME: One day add support for changing the exec file
1889 we're debugging, arguments and an environment. */
1892 extended_remote_create_inferior (exec_file, args, env)
1897 /* Rip out the breakpoints; we'll reinsert them after restarting
1898 the remote server. */
1899 remove_breakpoints ();
1901 /* Now restart the remote server. */
1902 extended_remote_restart ();
1904 /* Now put the breakpoints back in. This way we're safe if the
1905 restart function works via a unix fork on the remote side. */
1906 insert_breakpoints ();
1908 /* Clean up from the last time we were running. */
1909 clear_proceed_status ();
1911 /* Let the remote process run. */
1912 proceed (-1, TARGET_SIGNAL_0, 0);
1916 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1917 than other targets; in those use REMOTE_BREAKPOINT instead of just
1918 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1919 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1920 the standard routines that are in mem-break.c. */
1922 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1923 the choice of breakpoint instruction affects target program design and
1924 vice versa, and by making it user-tweakable, the special code here
1925 goes away and we need fewer special GDB configurations. */
1927 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1928 #define REMOTE_BREAKPOINT
1931 #ifdef REMOTE_BREAKPOINT
1933 /* If the target isn't bi-endian, just pretend it is. */
1934 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1935 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1936 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1939 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
1940 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
1942 #endif /* REMOTE_BREAKPOINT */
1944 /* Insert a breakpoint on targets that don't have any better breakpoint
1945 support. We read the contents of the target location and stash it,
1946 then overwrite it with a breakpoint instruction. ADDR is the target
1947 location in the target machine. CONTENTS_CACHE is a pointer to
1948 memory allocated for saving the target contents. It is guaranteed
1949 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1950 is accomplished via BREAKPOINT_MAX). */
1953 remote_insert_breakpoint (addr, contents_cache)
1955 char *contents_cache;
1957 #ifdef REMOTE_BREAKPOINT
1960 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
1964 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1965 val = target_write_memory (addr, (char *) big_break_insn,
1966 sizeof big_break_insn);
1968 val = target_write_memory (addr, (char *) little_break_insn,
1969 sizeof little_break_insn);
1974 return memory_insert_breakpoint (addr, contents_cache);
1975 #endif /* REMOTE_BREAKPOINT */
1979 remote_remove_breakpoint (addr, contents_cache)
1981 char *contents_cache;
1983 #ifdef REMOTE_BREAKPOINT
1984 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
1986 return memory_remove_breakpoint (addr, contents_cache);
1987 #endif /* REMOTE_BREAKPOINT */
1990 /* Some targets are only capable of doing downloads, and afterwards they switch
1991 to the remote serial protocol. This function provides a clean way to get
1992 from the download target to the remote target. It's basically just a
1993 wrapper so that we don't have to expose any of the internal workings of
1996 Prior to calling this routine, you should shutdown the current target code,
1997 else you will get the "A program is being debugged already..." message.
1998 Usually a call to pop_target() suffices.
2002 push_remote_target (name, from_tty)
2006 printf_filtered ("Switching to remote protocol\n");
2007 remote_open (name, from_tty);
2010 /* Other targets want to use the entire remote serial module but with
2011 certain remote_ops overridden. */
2014 open_remote_target (name, from_tty, target, extended_p)
2017 struct target_ops *target;
2020 printf_filtered ("Selecting the %sremote protocol\n",
2021 (extended_p ? "extended-" : ""));
2022 remote_open_1 (name, from_tty, target, extended_p);
2025 /* Remote target communications for serial-line targets in custom GDB protocol
2026 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
2028 This file is part of GDB.
2030 This program is free software; you can redistribute it and/or modify
2031 it under the terms of the GNU General Public License as published by
2032 the Free Software Foundation; either version 2 of the License, or
2033 (at your option) any later version.
2035 This program is distributed in the hope that it will be useful,
2036 but WITHOUT ANY WARRANTY; without even the implied warranty of
2037 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2038 GNU General Public License for more details.
2040 You should have received a copy of the GNU General Public License
2041 along with this program; if not, write to the Free Software
2042 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
2044 /* Remote communication protocol.
2046 A debug packet whose contents are <data>
2047 is encapsulated for transmission in the form:
2049 $ <data> # CSUM1 CSUM2
2051 <data> must be ASCII alphanumeric and cannot include characters
2052 '$' or '#'. If <data> starts with two characters followed by
2053 ':', then the existing stubs interpret this as a sequence number.
2055 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
2056 checksum of <data>, the most significant nibble is sent first.
2057 the hex digits 0-9,a-f are used.
2059 Receiver responds with:
2061 + - if CSUM is correct and ready for next packet
2062 - - if CSUM is incorrect
2064 <data> is as follows:
2065 Most values are encoded in ascii hex digits. Signal numbers are according
2066 to the numbering in target.h.
2070 set thread Hct... Set thread for subsequent operations.
2071 c = 'c' for thread used in step and
2072 continue; t... can be -1 for all
2074 c = 'g' for thread used in other
2075 operations. If zero, pick a thread,
2077 reply OK for success
2081 reply XX....X Each byte of register data
2082 is described by two hex digits.
2083 Registers are in the internal order
2084 for GDB, and the bytes in a register
2085 are in the same order the machine uses.
2086 or ENN for an error.
2088 write regs GXX..XX Each byte of register data
2089 is described by two hex digits.
2090 reply OK for success
2093 write reg Pn...=r... Write register n... with value r...,
2094 which contains two hex digits for each
2095 byte in the register (target byte
2097 reply OK for success
2099 (not supported by all stubs).
2101 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
2102 reply XX..XX XX..XX is mem contents
2103 Can be fewer bytes than requested
2104 if able to read only part of the data.
2107 write mem MAA..AA,LLLL:XX..XX
2109 LLLL is number of bytes,
2111 reply OK for success
2112 ENN for an error (this includes the case
2113 where only part of the data was
2116 continue cAA..AA AA..AA is address to resume
2117 If AA..AA is omitted,
2118 resume at same address.
2120 step sAA..AA AA..AA is address to resume
2121 If AA..AA is omitted,
2122 resume at same address.
2124 continue with Csig;AA..AA Continue with signal sig (hex signal
2125 signal number). If ;AA..AA is omitted, resume
2128 step with Ssig;AA..AA Like 'C' but step not continue.
2131 last signal ? Reply the current reason for stopping.
2132 This is the same reply as is generated
2133 for step or cont : SAA where AA is the
2138 There is no immediate reply to step or cont.
2139 The reply comes when the machine stops.
2140 It is SAA AA is the signal number.
2142 or... TAAn...:r...;n...:r...;n...:r...;
2144 n... = register number (hex)
2145 r... = register contents
2147 r... = thread process ID. This is
2149 n... = other string not starting
2150 with valid hex digit.
2151 gdb should ignore this n,r pair
2152 and go on to the next. This way
2153 we can extend the protocol.
2154 or... WAA The process exited, and AA is
2155 the exit status. This is only
2156 applicable for certains sorts of
2158 or... XAA The process terminated with signal
2160 or... OXX..XX XX..XX is hex encoding of ASCII data. This
2161 can happen at any time while the program is
2162 running and the debugger should
2163 continue to wait for 'W', 'T', etc.
2165 thread alive TXX Find out if the thread XX is alive.
2166 reply OK thread is still alive
2169 remote restart RXX Restart the remote server
2171 extended ops ! Use the extended remote protocol.
2172 Sticky -- only needs to be set once.
2176 toggle debug d toggle debug flag (see 386 & 68k stubs)
2177 reset r reset -- see sparc stub.
2178 reserved <other> On other requests, the stub should
2179 ignore the request and send an empty
2180 response ($#<checksum>). This way
2181 we can extend the protocol and GDB
2182 can tell whether the stub it is
2183 talking to uses the old or the new.
2184 search tAA:PP,MM Search backwards starting at address
2185 AA for a match with pattern PP and
2186 mask MM. PP and MM are 4 bytes.
2187 Not supported by all stubs.
2189 general query qXXXX Request info about XXXX.
2190 general set QXXXX=yyyy Set value of XXXX to yyyy.
2191 query sect offs qOffsets Get section offsets. Reply is
2192 Text=xxx;Data=yyy;Bss=zzz
2194 Responses can be run-length encoded to save space. A '*' means that
2195 the next character is an ASCII encoding giving a repeat count which
2196 stands for that many repititions of the character preceding the '*'.
2197 The encoding is n+29, yielding a printable character where n >=3
2198 (which is where rle starts to win). Don't use an n > 126.
2201 "0* " means the same as "0000". */
2204 #include "gdb_string.h"
2207 #include "inferior.h"
2209 #include "symfile.h"
2212 /*#include "terminal.h"*/
2214 #include "objfiles.h"
2215 #include "gdb-stabs.h"
2216 #include "gdbthread.h"
2221 #include <sys/types.h>
2227 /* Prototypes for local functions */
2229 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
2230 char *myaddr, int len));
2232 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
2233 char *myaddr, int len));
2235 static void remote_files_info PARAMS ((struct target_ops *ignore));
2237 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
2238 int len, int should_write,
2239 struct target_ops *target));
2241 static void remote_prepare_to_store PARAMS ((void));
2243 static void remote_fetch_registers PARAMS ((int regno));
2245 static void remote_resume PARAMS ((int pid, int step,
2246 enum target_signal siggnal));
2248 static int remote_start_remote PARAMS ((char *dummy));
2250 static void remote_open PARAMS ((char *name, int from_tty));
2252 static void extended_remote_open PARAMS ((char *name, int from_tty));
2254 static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p));
2256 static void remote_close PARAMS ((int quitting));
2258 static void remote_store_registers PARAMS ((int regno));
2260 static void remote_mourn PARAMS ((void));
2262 static void extended_remote_restart PARAMS ((void));
2264 static void extended_remote_mourn PARAMS ((void));
2266 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
2268 static void remote_mourn_1 PARAMS ((struct target_ops *));
2270 static void remote_send PARAMS ((char *buf));
2272 static int readchar PARAMS ((int timeout));
2274 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
2276 static void remote_kill PARAMS ((void));
2278 static int tohex PARAMS ((int nib));
2280 static void remote_detach PARAMS ((char *args, int from_tty));
2282 static void remote_interrupt PARAMS ((int signo));
2284 static void remote_interrupt_twice PARAMS ((int signo));
2286 static void interrupt_query PARAMS ((void));
2288 static void set_thread PARAMS ((int, int));
2290 static int remote_thread_alive PARAMS ((int));
2292 static void get_offsets PARAMS ((void));
2294 static int read_frame PARAMS ((char *));
2296 static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
2298 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
2300 static int hexnumlen PARAMS ((ULONGEST num));
2302 /* exported functions */
2304 extern int fromhex PARAMS ((int a));
2305 extern void getpkt PARAMS ((char *buf, int forever));
2306 extern int putpkt PARAMS ((char *buf));
2308 /* Define the target subroutine names */
2310 static struct target_ops remote_ops ;
2312 static void init_remote_ops(void)
2314 remote_ops.to_shortname = "remote";
2315 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
2316 remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
2317 Specify the serial device it is connected to (e.g. /dev/ttya)." ;
2318 remote_ops.to_open = remote_open;
2319 remote_ops.to_close = remote_close;
2320 remote_ops.to_attach = NULL;
2321 remote_ops.to_detach = remote_detach;
2322 remote_ops.to_resume = remote_resume;
2323 remote_ops.to_wait = remote_wait;
2324 remote_ops.to_fetch_registers = remote_fetch_registers;
2325 remote_ops.to_store_registers = remote_store_registers;
2326 remote_ops.to_prepare_to_store = remote_prepare_to_store;
2327 remote_ops.to_xfer_memory = remote_xfer_memory;
2328 remote_ops.to_files_info = remote_files_info;
2329 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
2330 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
2331 remote_ops.to_terminal_init = NULL;
2332 remote_ops.to_terminal_inferior = NULL;
2333 remote_ops.to_terminal_ours_for_output = NULL;
2334 remote_ops.to_terminal_ours = NULL;
2335 remote_ops.to_terminal_info = NULL;
2336 remote_ops.to_kill = remote_kill;
2337 remote_ops.to_load = generic_load;
2338 remote_ops.to_lookup_symbol = NULL;
2339 remote_ops.to_create_inferior = NULL;
2340 remote_ops.to_mourn_inferior = remote_mourn;
2341 remote_ops.to_can_run = 0;
2342 remote_ops.to_notice_signals = 0;
2343 remote_ops.to_thread_alive = remote_thread_alive;
2344 remote_ops.to_stop = 0;
2345 remote_ops.to_stratum = process_stratum;
2346 remote_ops.DONT_USE = NULL;
2347 remote_ops.to_has_all_memory = 1;
2348 remote_ops.to_has_memory = 1;
2349 remote_ops.to_has_stack = 1;
2350 remote_ops.to_has_registers = 1;
2351 remote_ops.to_has_execution = 1;
2352 remote_ops.to_sections = NULL;
2353 remote_ops.to_sections_end = NULL;
2354 remote_ops.to_magic = OPS_MAGIC ;
2355 } /* init_remote_ops */
2357 static struct target_ops extended_remote_ops ;
2359 static void init_extended_remote_ops(void)
2361 extended_remote_ops.to_shortname = "extended-remote";
2362 extended_remote_ops.to_longname = "Extended remote serial target in gdb-specific protocol";
2363 extended_remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
2364 Specify the serial device it is connected to (e.g. /dev/ttya).",
2365 extended_remote_ops.to_open = extended_remote_open;
2366 extended_remote_ops.to_close = remote_close;
2367 extended_remote_ops.to_attach = NULL;
2368 extended_remote_ops.to_detach = remote_detach;
2369 extended_remote_ops.to_resume = remote_resume;
2370 extended_remote_ops.to_wait = remote_wait;
2371 extended_remote_ops.to_fetch_registers = remote_fetch_registers;
2372 extended_remote_ops.to_store_registers = remote_store_registers;
2373 extended_remote_ops.to_prepare_to_store = remote_prepare_to_store;
2374 extended_remote_ops.to_xfer_memory = remote_xfer_memory;
2375 extended_remote_ops.to_files_info = remote_files_info;
2376 extended_remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
2377 extended_remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
2378 extended_remote_ops.to_terminal_init = NULL;
2379 extended_remote_ops.to_terminal_inferior = NULL;
2380 extended_remote_ops.to_terminal_ours_for_output = NULL;
2381 extended_remote_ops.to_terminal_ours = NULL;
2382 extended_remote_ops.to_terminal_info = NULL;
2383 extended_remote_ops.to_kill = remote_kill;
2384 extended_remote_ops.to_load = generic_load;
2385 extended_remote_ops.to_lookup_symbol = NULL;
2386 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
2387 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2388 extended_remote_ops.to_can_run = 0;
2389 extended_remote_ops.to_notice_signals = 0;
2390 extended_remote_ops.to_thread_alive = remote_thread_alive;
2391 extended_remote_ops.to_stop = 0;
2392 extended_remote_ops.to_stratum = process_stratum;
2393 extended_remote_ops.DONT_USE = NULL;
2394 extended_remote_ops.to_has_all_memory = 1;
2395 extended_remote_ops.to_has_memory = 1;
2396 extended_remote_ops.to_has_stack = 1;
2397 extended_remote_ops.to_has_registers = 1;
2398 extended_remote_ops.to_has_execution = 1;
2399 extended_remote_ops.to_sections = NULL;
2400 extended_remote_ops.to_sections_end = NULL;
2401 extended_remote_ops.to_magic = OPS_MAGIC ;
2405 /* This was 5 seconds, which is a long time to sit and wait.
2406 Unless this is going though some terminal server or multiplexer or
2407 other form of hairy serial connection, I would think 2 seconds would
2410 /* Changed to allow option to set timeout value.
2411 was static int remote_timeout = 2; */
2412 extern int remote_timeout;
2414 /* This variable chooses whether to send a ^C or a break when the user
2415 requests program interruption. Although ^C is usually what remote
2416 systems expect, and that is the default here, sometimes a break is
2417 preferable instead. */
2419 static int remote_break;
2421 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
2422 remote_open knows that we don't have a file open when the program
2424 static serial_t remote_desc = NULL;
2426 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
2427 and i386-stub.c. Normally, no one would notice because it only matters
2428 for writing large chunks of memory (e.g. in downloads). Also, this needs
2429 to be more than 400 if required to hold the registers (see below, where
2430 we round it up based on REGISTER_BYTES). */
2433 /* Maximum number of bytes to read/write at once. The value here
2434 is chosen to fill up a packet (the headers account for the 32). */
2435 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
2437 /* Round up PBUFSIZ to hold all the registers, at least. */
2438 /* The blank line after the #if seems to be required to work around a
2439 bug in HP's PA compiler. */
2440 #if REGISTER_BYTES > MAXBUFBYTES
2443 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
2446 /* This variable sets the number of bytes to be written to the target
2447 in a single packet. Normally PBUFSIZ is satisfactory, but some
2448 targets need smaller values (perhaps because the receiving end
2451 static int remote_write_size = PBUFSIZ;
2453 /* This is the size (in chars) of the first response to the `g' command. This
2454 is used to limit the size of the memory read and write commands to prevent
2455 stub buffers from overflowing. The size does not include headers and
2456 trailers, it is only the payload size. */
2458 static int remote_register_buf_size = 0;
2460 /* Should we try the 'P' request? If this is set to one when the stub
2461 doesn't support 'P', the only consequence is some unnecessary traffic. */
2462 static int stub_supports_P = 1;
2464 /* These are pointers to hook functions that may be set in order to
2465 modify resume/wait behavior for a particular architecture. */
2467 void (*target_resume_hook) PARAMS ((void));
2468 void (*target_wait_loop_hook) PARAMS ((void));
2471 /* These are the threads which we last sent to the remote system. -1 for all
2472 or -2 for not sent yet. */
2477 set_thread (th, gen)
2482 int state = gen ? general_thread : cont_thread;
2486 buf[1] = gen ? 'g' : 'c';
2493 sprintf (&buf[2], "-%x", -th);
2495 sprintf (&buf[2], "%x", th);
2499 general_thread = th;
2504 /* Return nonzero if the thread TH is still alive on the remote system. */
2507 remote_thread_alive (th)
2514 sprintf (&buf[1], "-%x", -th);
2516 sprintf (&buf[1], "%x", th);
2519 return (buf[0] == 'O' && buf[1] == 'K');
2522 /* Restart the remote side; this is an extended protocol operation. */
2525 extended_remote_restart ()
2529 /* Send the restart command; for reasons I don't understand the
2530 remote side really expects a number after the "R". */
2532 sprintf (&buf[1], "%x", 0);
2535 /* Now query for status so this looks just like we restarted
2536 gdbserver from scratch. */
2541 /* Clean up connection to a remote debugger. */
2545 remote_close (quitting)
2549 SERIAL_CLOSE (remote_desc);
2553 /* Query the remote side for the text, data and bss offsets. */
2558 char buf[PBUFSIZ], *ptr;
2560 CORE_ADDR text_addr, data_addr, bss_addr;
2561 struct section_offsets *offs;
2563 putpkt ("qOffsets");
2567 if (buf[0] == '\000')
2568 return; /* Return silently. Stub doesn't support this
2572 warning ("Remote failure reply: %s", buf);
2576 /* Pick up each field in turn. This used to be done with scanf, but
2577 scanf will make trouble if CORE_ADDR size doesn't match
2578 conversion directives correctly. The following code will work
2579 with any size of CORE_ADDR. */
2580 text_addr = data_addr = bss_addr = 0;
2584 if (strncmp (ptr, "Text=", 5) == 0)
2587 /* Don't use strtol, could lose on big values. */
2588 while (*ptr && *ptr != ';')
2589 text_addr = (text_addr << 4) + fromhex (*ptr++);
2594 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
2597 while (*ptr && *ptr != ';')
2598 data_addr = (data_addr << 4) + fromhex (*ptr++);
2603 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
2606 while (*ptr && *ptr != ';')
2607 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
2613 error ("Malformed response to offset query, %s", buf);
2615 if (symfile_objfile == NULL)
2618 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
2619 + symfile_objfile->num_sections
2620 * sizeof (offs->offsets));
2621 memcpy (offs, symfile_objfile->section_offsets,
2622 sizeof (struct section_offsets)
2623 + symfile_objfile->num_sections
2624 * sizeof (offs->offsets));
2626 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
2628 /* This is a temporary kludge to force data and bss to use the same offsets
2629 because that's what nlmconv does now. The real solution requires changes
2630 to the stub and remote.c that I don't have time to do right now. */
2632 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
2633 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
2635 objfile_relocate (symfile_objfile, offs);
2638 /* Stub for catch_errors. */
2641 remote_start_remote (dummy)
2644 immediate_quit = 1; /* Allow user to interrupt it */
2646 /* Ack any packet which the remote side has already sent. */
2647 SERIAL_WRITE (remote_desc, "+", 1);
2649 /* Let the stub know that we want it to return the thread. */
2652 get_offsets (); /* Get text, data & bss offsets */
2654 putpkt ("?"); /* initiate a query from remote machine */
2657 start_remote (); /* Initialize gdb process mechanisms */
2661 /* Open a connection to a remote debugger.
2662 NAME is the filename used for communication. */
2665 remote_open (name, from_tty)
2669 remote_open_1 (name, from_tty, &remote_ops, 0);
2672 /* Open a connection to a remote debugger using the extended
2673 remote gdb protocol. NAME is the filename used for communication. */
2676 extended_remote_open (name, from_tty)
2680 remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/);
2683 /* Generic code for opening a connection to a remote target. */
2684 static DCACHE *remote_dcache;
2687 remote_open_1 (name, from_tty, target, extended_p)
2690 struct target_ops *target;
2694 error ("To open a remote debug connection, you need to specify what serial\n\
2695 device is attached to the remote system (e.g. /dev/ttya).");
2697 target_preopen (from_tty);
2699 unpush_target (target);
2701 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
2703 remote_desc = SERIAL_OPEN (name);
2705 perror_with_name (name);
2707 if (baud_rate != -1)
2709 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
2711 SERIAL_CLOSE (remote_desc);
2712 perror_with_name (name);
2717 SERIAL_RAW (remote_desc);
2719 /* If there is something sitting in the buffer we might take it as a
2720 response to a command, which would be bad. */
2721 SERIAL_FLUSH_INPUT (remote_desc);
2725 puts_filtered ("Remote debugging using ");
2726 puts_filtered (name);
2727 puts_filtered ("\n");
2729 push_target (target); /* Switch to using remote target now */
2731 /* Start out by trying the 'P' request to set registers. We set this each
2732 time that we open a new target so that if the user switches from one
2733 stub to another, we can (if the target is closed and reopened) cope. */
2734 stub_supports_P = 1;
2736 general_thread = -2;
2739 /* Without this, some commands which require an active target (such as kill)
2740 won't work. This variable serves (at least) double duty as both the pid
2741 of the target process (if it has such), and as a flag indicating that a
2742 target is active. These functions should be split out into seperate
2743 variables, especially since GDB will someday have a notion of debugging
2744 several processes. */
2746 inferior_pid = 42000;
2747 /* Start the remote connection; if error (0), discard this target.
2748 In particular, if the user quits, be sure to discard it
2749 (we'd be in an inconsistent state otherwise). */
2750 if (!catch_errors (remote_start_remote, (char *)0,
2751 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
2759 /* tell the remote that we're using the extended protocol. */
2766 /* This takes a program previously attached to and detaches it. After
2767 this is done, GDB can be used to debug some other program. We
2768 better not have left any breakpoints in the target program or it'll
2769 die when it hits one. */
2772 remote_detach (args, from_tty)
2779 error ("Argument given to \"detach\" when remotely debugging.");
2781 /* Tell the remote target to detach. */
2787 puts_filtered ("Ending remote debugging.\n");
2790 /* Convert hex digit A to a number. */
2796 if (a >= '0' && a <= '9')
2798 else if (a >= 'a' && a <= 'f')
2799 return a - 'a' + 10;
2800 else if (a >= 'A' && a <= 'F')
2801 return a - 'A' + 10;
2803 error ("Reply contains invalid hex digit %d", a);
2806 /* Convert number NIB to a hex digit. */
2818 /* Tell the remote machine to resume. */
2820 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
2824 remote_resume (pid, step, siggnal)
2826 enum target_signal siggnal;
2831 set_thread (inferior_pid, 0);
2833 set_thread (pid, 0);
2835 dcache_flush (remote_dcache);
2837 last_sent_signal = siggnal;
2838 last_sent_step = step;
2840 /* A hook for when we need to do something at the last moment before
2842 if (target_resume_hook)
2843 (*target_resume_hook) ();
2845 if (siggnal != TARGET_SIGNAL_0)
2847 buf[0] = step ? 'S' : 'C';
2848 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
2849 buf[2] = tohex ((int)siggnal & 0xf);
2853 strcpy (buf, step ? "s": "c");
2858 /* Send ^C to target to halt it. Target will respond, and send us a
2862 remote_interrupt (signo)
2865 /* If this doesn't work, try more severe steps. */
2866 signal (signo, remote_interrupt_twice);
2869 printf_unfiltered ("remote_interrupt called\n");
2871 /* Send a break or a ^C, depending on user preference. */
2873 SERIAL_SEND_BREAK (remote_desc);
2875 SERIAL_WRITE (remote_desc, "\003", 1);
2878 static void (*ofunc)();
2880 /* The user typed ^C twice. */
2882 remote_interrupt_twice (signo)
2885 signal (signo, ofunc);
2889 signal (signo, remote_interrupt);
2892 /* Ask the user what to do when an interrupt is received. */
2897 target_terminal_ours ();
2899 if (query ("Interrupted while waiting for the program.\n\
2900 Give up (and stop debugging it)? "))
2902 target_mourn_inferior ();
2903 return_to_top_level (RETURN_QUIT);
2906 target_terminal_inferior ();
2909 /* If nonzero, ignore the next kill. */
2913 remote_console_output (msg)
2918 for (p = msg; *p; p +=2)
2921 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
2924 if (target_output_hook)
2925 target_output_hook (tb);
2927 fputs_filtered (tb, gdb_stdout);
2931 /* Wait until the remote machine stops, then return,
2932 storing status in STATUS just as `wait' would.
2933 Returns "pid" (though it's not clear what, if anything, that
2934 means in the case of this target). */
2937 remote_wait (pid, status)
2939 struct target_waitstatus *status;
2941 unsigned char buf[PBUFSIZ];
2942 int thread_num = -1;
2944 status->kind = TARGET_WAITKIND_EXITED;
2945 status->value.integer = 0;
2951 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
2952 getpkt ((char *) buf, 1);
2953 signal (SIGINT, ofunc);
2955 /* This is a hook for when we need to do something (perhaps the
2956 collection of trace data) every time the target stops. */
2957 if (target_wait_loop_hook)
2958 (*target_wait_loop_hook) ();
2962 case 'E': /* Error of some sort */
2963 warning ("Remote failure reply: %s", buf);
2965 case 'T': /* Status with PC, SP, FP, ... */
2969 char regs[MAX_REGISTER_RAW_SIZE];
2971 /* Expedited reply, containing Signal, {regno, reg} repeat */
2972 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
2974 n... = register number
2975 r... = register contents
2977 p = &buf[3]; /* after Txx */
2984 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
2985 p1 = (unsigned char *)p_temp;
2989 p1 = (unsigned char *) strchr ((const char *) p, ':');
2991 warning ("Malformed packet (missing colon): %s\n\
2994 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
2996 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
2997 p = (unsigned char *)p_temp;
3005 warning ("Malformed packet (missing colon): %s\n\
3009 if (regno >= NUM_REGS)
3010 warning ("Remote sent bad register number %ld: %s\n\
3014 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
3016 if (p[0] == 0 || p[1] == 0)
3017 warning ("Remote reply is too short: %s", buf);
3018 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3021 supply_register (regno, regs);
3025 warning ("Remote register badly formatted: %s", buf);
3029 case 'S': /* Old style status, just signal only */
3030 status->kind = TARGET_WAITKIND_STOPPED;
3031 status->value.sig = (enum target_signal)
3032 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3035 case 'W': /* Target exited */
3037 /* The remote process exited. */
3038 status->kind = TARGET_WAITKIND_EXITED;
3039 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
3043 status->kind = TARGET_WAITKIND_SIGNALLED;
3044 status->value.sig = (enum target_signal)
3045 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
3049 case 'O': /* Console output */
3050 remote_console_output (buf + 1);
3053 if (last_sent_signal != TARGET_SIGNAL_0)
3055 /* Zero length reply means that we tried 'S' or 'C' and
3056 the remote system doesn't support it. */
3057 target_terminal_ours_for_output ();
3059 ("Can't send signals to this remote system. %s not sent.\n",
3060 target_signal_to_name (last_sent_signal));
3061 last_sent_signal = TARGET_SIGNAL_0;
3062 target_terminal_inferior ();
3064 strcpy ((char *) buf, last_sent_step ? "s" : "c");
3065 putpkt ((char *) buf);
3068 /* else fallthrough */
3070 warning ("Invalid remote reply: %s", buf);
3075 if (thread_num != -1)
3077 /* Initial thread value can only be acquired via wait, so deal with
3078 this marker which is used before the first thread value is
3080 if (inferior_pid == 42000)
3082 inferior_pid = thread_num;
3083 add_thread (inferior_pid);
3087 return inferior_pid;
3090 /* Number of bytes of registers this stub implements. */
3091 static int register_bytes_found;
3093 /* Read the remote registers into the block REGS. */
3094 /* Currently we just read all the registers, so we don't use regno. */
3097 remote_fetch_registers (regno)
3103 char regs[REGISTER_BYTES];
3105 set_thread (inferior_pid, 1);
3110 if (remote_register_buf_size == 0)
3111 remote_register_buf_size = strlen (buf);
3113 /* Unimplemented registers read as all bits zero. */
3114 memset (regs, 0, REGISTER_BYTES);
3116 /* We can get out of synch in various cases. If the first character
3117 in the buffer is not a hex character, assume that has happened
3118 and try to fetch another packet to read. */
3119 while ((buf[0] < '0' || buf[0] > '9')
3120 && (buf[0] < 'a' || buf[0] > 'f'))
3123 printf_unfiltered ("Bad register packet; fetching a new packet\n");
3127 /* Reply describes registers byte by byte, each byte encoded as two
3128 hex characters. Suck them all up, then supply them to the
3129 register cacheing/storage mechanism. */
3132 for (i = 0; i < REGISTER_BYTES; i++)
3138 warning ("Remote reply is of odd length: %s", buf);
3139 /* Don't change register_bytes_found in this case, and don't
3140 print a second warning. */
3143 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3147 if (i != register_bytes_found)
3149 register_bytes_found = i;
3150 #ifdef REGISTER_BYTES_OK
3151 if (!REGISTER_BYTES_OK (i))
3152 warning ("Remote reply is too short: %s", buf);
3157 for (i = 0; i < NUM_REGS; i++)
3158 supply_register (i, ®s[REGISTER_BYTE(i)]);
3161 /* Prepare to store registers. Since we may send them all (using a
3162 'G' request), we have to read out the ones we don't want to change
3166 remote_prepare_to_store ()
3168 /* Make sure the entire registers array is valid. */
3169 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
3172 /* Store register REGNO, or all registers if REGNO == -1, from the contents
3173 of REGISTERS. FIXME: ignores errors. */
3176 remote_store_registers (regno)
3183 set_thread (inferior_pid, 1);
3185 if (regno >= 0 && stub_supports_P)
3187 /* Try storing a single register. */
3190 sprintf (buf, "P%x=", regno);
3191 p = buf + strlen (buf);
3192 regp = ®isters[REGISTER_BYTE (regno)];
3193 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
3195 *p++ = tohex ((regp[i] >> 4) & 0xf);
3196 *p++ = tohex (regp[i] & 0xf);
3202 /* The stub understands the 'P' request. We are done. */
3206 /* The stub does not support the 'P' request. Use 'G' instead,
3207 and don't try using 'P' in the future (it will just waste our
3209 stub_supports_P = 0;
3214 /* Command describes registers byte by byte,
3215 each byte encoded as two hex characters. */
3218 /* remote_prepare_to_store insures that register_bytes_found gets set. */
3219 for (i = 0; i < register_bytes_found; i++)
3221 *p++ = tohex ((registers[i] >> 4) & 0xf);
3222 *p++ = tohex (registers[i] & 0xf);
3230 Use of the data cache *used* to be disabled because it loses for looking at
3231 and changing hardware I/O ports and the like. Accepting `volatile'
3232 would perhaps be one way to fix it. Another idea would be to use the
3233 executable file for the text segment (for all SEC_CODE sections?
3234 For all SEC_READONLY sections?). This has problems if you want to
3235 actually see what the memory contains (e.g. self-modifying code,
3236 clobbered memory, user downloaded the wrong thing).
3238 Because it speeds so much up, it's now enabled, if you're playing
3239 with registers you turn it of (set remotecache 0)
3242 /* Read a word from remote address ADDR and return it.
3243 This goes through the data cache. */
3247 remote_fetch_word (addr)
3250 return dcache_fetch (remote_dcache, addr);
3253 /* Write a word WORD into remote address ADDR.
3254 This goes through the data cache. */
3257 remote_store_word (addr, word)
3261 dcache_poke (remote_dcache, addr, word);
3263 #endif /* 0 (unused?) */
3267 /* Return the number of hex digits in num. */
3275 for (i = 0; num != 0; i++)
3281 /* Write memory data directly to the remote machine.
3282 This does not inform the data cache; the data cache uses this.
3283 MEMADDR is the address in the remote memory space.
3284 MYADDR is the address of the buffer in our space.
3285 LEN is the number of bytes.
3287 Returns number of bytes transferred, or 0 for error. */
3290 remote_write_bytes (memaddr, myaddr, len)
3295 int max_buf_size; /* Max size of packet output buffer */
3298 /* Chop the transfer down if necessary */
3300 max_buf_size = min (remote_write_size, PBUFSIZ);
3301 if (remote_register_buf_size != 0)
3302 max_buf_size = min (max_buf_size, remote_register_buf_size);
3304 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
3305 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
3315 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3317 /* FIXME-32x64: Need a version of print_address_numeric which puts the
3318 result in a buffer like sprintf. */
3319 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo);
3321 /* We send target system values byte by byte, in increasing byte addresses,
3322 each byte encoded as two hex characters. */
3324 p = buf + strlen (buf);
3325 for (i = 0; i < todo; i++)
3327 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
3328 *p++ = tohex (myaddr[i] & 0xf);
3337 /* There is no correspondance between what the remote protocol uses
3338 for errors and errno codes. We would like a cleaner way of
3339 representing errors (big enough to include errno codes, bfd_error
3340 codes, and others). But for now just return EIO. */
3351 /* Read memory data directly from the remote machine.
3352 This does not use the data cache; the data cache uses this.
3353 MEMADDR is the address in the remote memory space.
3354 MYADDR is the address of the buffer in our space.
3355 LEN is the number of bytes.
3357 Returns number of bytes transferred, or 0 for error. */
3360 remote_read_bytes (memaddr, myaddr, len)
3365 int max_buf_size; /* Max size of packet output buffer */
3368 /* Chop the transfer down if necessary */
3370 max_buf_size = min (remote_write_size, PBUFSIZ);
3371 if (remote_register_buf_size != 0)
3372 max_buf_size = min (max_buf_size, remote_register_buf_size);
3382 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
3384 /* FIXME-32x64: Need a version of print_address_numeric which puts the
3385 result in a buffer like sprintf. */
3386 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo);
3392 /* There is no correspondance between what the remote protocol uses
3393 for errors and errno codes. We would like a cleaner way of
3394 representing errors (big enough to include errno codes, bfd_error
3395 codes, and others). But for now just return EIO. */
3400 /* Reply describes memory byte by byte,
3401 each byte encoded as two hex characters. */
3404 for (i = 0; i < todo; i++)
3406 if (p[0] == 0 || p[1] == 0)
3407 /* Reply is short. This means that we were able to read only part
3408 of what we wanted to. */
3409 return i + (origlen - len);
3410 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
3420 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
3421 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
3422 nonzero. Returns length of data written or read; 0 for error. */
3426 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
3431 struct target_ops *target; /* ignored */
3433 #ifdef REMOTE_TRANSLATE_XFER_ADDRESS
3436 REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen);
3443 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
3448 /* Enable after 4.12. */
3451 remote_search (len, data, mask, startaddr, increment, lorange, hirange
3452 addr_found, data_found)
3456 CORE_ADDR startaddr;
3460 CORE_ADDR *addr_found;
3463 if (increment == -4 && len == 4)
3465 long mask_long, data_long;
3466 long data_found_long;
3467 CORE_ADDR addr_we_found;
3469 long returned_long[2];
3472 mask_long = extract_unsigned_integer (mask, len);
3473 data_long = extract_unsigned_integer (data, len);
3474 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
3479 /* The stub doesn't support the 't' request. We might want to
3480 remember this fact, but on the other hand the stub could be
3481 switched on us. Maybe we should remember it only until
3482 the next "target remote". */
3483 generic_search (len, data, mask, startaddr, increment, lorange,
3484 hirange, addr_found, data_found);
3489 /* There is no correspondance between what the remote protocol uses
3490 for errors and errno codes. We would like a cleaner way of
3491 representing errors (big enough to include errno codes, bfd_error
3492 codes, and others). But for now just use EIO. */
3493 memory_error (EIO, startaddr);
3496 while (*p != '\0' && *p != ',')
3497 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
3499 error ("Protocol error: short return for search");
3501 data_found_long = 0;
3502 while (*p != '\0' && *p != ',')
3503 data_found_long = (data_found_long << 4) + fromhex (*p++);
3504 /* Ignore anything after this comma, for future extensions. */
3506 if (addr_we_found < lorange || addr_we_found >= hirange)
3512 *addr_found = addr_we_found;
3513 *data_found = store_unsigned_integer (data_we_found, len);
3516 generic_search (len, data, mask, startaddr, increment, lorange,
3517 hirange, addr_found, data_found);
3522 remote_files_info (ignore)
3523 struct target_ops *ignore;
3525 puts_filtered ("Debugging a target over a serial line.\n");
3528 /* Stuff for dealing with the packets which are part of this protocol.
3529 See comment at top of file for details. */
3531 /* Read a single character from the remote end, masking it down to 7 bits. */
3539 ch = SERIAL_READCHAR (remote_desc, timeout);
3544 error ("Remote connection closed");
3546 perror_with_name ("Remote communication error");
3547 case SERIAL_TIMEOUT:
3554 /* Send the command in BUF to the remote machine,
3555 and read the reply into BUF.
3556 Report an error if we get an error reply. */
3566 error ("Remote failure reply: %s", buf);
3569 /* Send a packet to the remote machine, with error checking.
3570 The data of the packet is in BUF. */
3577 unsigned char csum = 0;
3579 int cnt = strlen (buf);
3584 /* Copy the packet into buffer BUF2, encapsulating it
3585 and giving it a checksum. */
3587 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
3593 for (i = 0; i < cnt; i++)
3599 *p++ = tohex ((csum >> 4) & 0xf);
3600 *p++ = tohex (csum & 0xf);
3602 /* Send it over and over until we get a positive ack. */
3606 int started_error_output = 0;
3611 printf_unfiltered ("Sending packet: %s...", buf2);
3612 gdb_flush(gdb_stdout);
3614 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
3615 perror_with_name ("putpkt: write failed");
3617 /* read until either a timeout occurs (-2) or '+' is read */
3620 ch = readchar (remote_timeout);
3627 case SERIAL_TIMEOUT:
3629 if (started_error_output)
3631 putchar_unfiltered ('\n');
3632 started_error_output = 0;
3641 printf_unfiltered("Ack\n");
3643 case SERIAL_TIMEOUT:
3647 break; /* Retransmit buffer */
3650 char junkbuf[PBUFSIZ];
3652 /* It's probably an old response, and we're out of sync. Just
3653 gobble up the packet and ignore it. */
3654 getpkt (junkbuf, 0);
3655 continue; /* Now, go look for + */
3660 if (!started_error_output)
3662 started_error_output = 1;
3663 printf_unfiltered ("putpkt: Junk: ");
3665 putchar_unfiltered (ch & 0177);
3669 break; /* Here to retransmit */
3673 /* This is wrong. If doing a long backtrace, the user should be
3674 able to get out next time we call QUIT, without anything as violent
3675 as interrupt_query. If we want to provide a way out of here
3676 without getting to the next QUIT, it should be based on hitting
3677 ^C twice as in remote_wait. */
3687 /* Come here after finding the start of the frame. Collect the rest into BUF,
3688 verifying the checksum, length, and handling run-length compression.
3689 Returns 0 on any error, 1 on success. */
3704 c = readchar (remote_timeout);
3708 case SERIAL_TIMEOUT:
3710 puts_filtered ("Timeout in mid-packet, retrying\n");
3714 puts_filtered ("Saw new packet start in middle of old one\n");
3715 return 0; /* Start a new packet, count retries */
3718 unsigned char pktcsum;
3722 pktcsum = fromhex (readchar (remote_timeout)) << 4;
3723 pktcsum |= fromhex (readchar (remote_timeout));
3725 if (csum == pktcsum)
3730 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
3732 puts_filtered (buf);
3733 puts_filtered ("\n");
3737 case '*': /* Run length encoding */
3739 c = readchar (remote_timeout);
3741 c = c - ' ' + 3; /* Compute repeat count */
3744 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
3746 memset (bp, *(bp - 1), c);
3752 printf_filtered ("Repeat count %d too large for buffer: ", c);
3753 puts_filtered (buf);
3754 puts_filtered ("\n");
3758 if (bp < buf + PBUFSIZ - 1)
3766 puts_filtered ("Remote packet too long: ");
3767 puts_filtered (buf);
3768 puts_filtered ("\n");
3775 /* Read a packet from the remote machine, with error checking,
3776 and store it in BUF. BUF is expected to be of size PBUFSIZ.
3777 If FOREVER, wait forever rather than timing out; this is used
3778 while the target is executing user code. */
3781 getpkt (buf, forever)
3790 strcpy (buf,"timeout");
3794 #ifdef MAINTENANCE_CMDS
3795 timeout = watchdog > 0 ? watchdog : -1;
3802 timeout = remote_timeout;
3806 for (tries = 1; tries <= MAX_TRIES; tries++)
3808 /* This can loop forever if the remote side sends us characters
3809 continuously, but if it pauses, we'll get a zero from readchar
3810 because of timeout. Then we'll count that as a retry. */
3812 /* Note that we will only wait forever prior to the start of a packet.
3813 After that, we expect characters to arrive at a brisk pace. They
3814 should show up within remote_timeout intervals. */
3818 c = readchar (timeout);
3820 if (c == SERIAL_TIMEOUT)
3822 #ifdef MAINTENANCE_CMDS
3823 if (forever) /* Watchdog went off. Kill the target. */
3825 target_mourn_inferior ();
3826 error ("Watchdog has expired. Target detached.\n");
3830 puts_filtered ("Timed out.\n");
3836 /* We've found the start of a packet, now collect the data. */
3838 val = read_frame (buf);
3843 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
3844 SERIAL_WRITE (remote_desc, "+", 1);
3848 /* Try the whole thing again. */
3850 SERIAL_WRITE (remote_desc, "-", 1);
3853 /* We have tried hard enough, and just can't receive the packet. Give up. */
3855 printf_unfiltered ("Ignoring packet error, continuing...\n");
3856 SERIAL_WRITE (remote_desc, "+", 1);
3862 /* For some mysterious reason, wait_for_inferior calls kill instead of
3863 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
3867 target_mourn_inferior ();
3871 /* Use catch_errors so the user can quit from gdb even when we aren't on
3872 speaking terms with the remote system. */
3873 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
3875 /* Don't wait for it to die. I'm not really sure it matters whether
3876 we do or not. For the existing stubs, kill is a noop. */
3877 target_mourn_inferior ();
3883 remote_mourn_1 (&remote_ops);
3887 extended_remote_mourn ()
3889 /* We do _not_ want to mourn the target like this; this will
3890 remove the extended remote target from the target stack,
3891 and the next time the user says "run" it'll fail.
3893 FIXME: What is the right thing to do here? */
3895 remote_mourn_1 (&extended_remote_ops);
3899 /* Worker function for remote_mourn. */
3901 remote_mourn_1 (target)
3902 struct target_ops *target;
3904 unpush_target (target);
3905 generic_mourn_inferior ();
3908 /* In the extended protocol we want to be able to do things like
3909 "run" and have them basically work as expected. So we need
3910 a special create_inferior function.
3912 FIXME: One day add support for changing the exec file
3913 we're debugging, arguments and an environment. */
3916 extended_remote_create_inferior (exec_file, args, env)
3921 /* Rip out the breakpoints; we'll reinsert them after restarting
3922 the remote server. */
3923 remove_breakpoints ();
3925 /* Now restart the remote server. */
3926 extended_remote_restart ();
3928 /* Now put the breakpoints back in. This way we're safe if the
3929 restart function works via a unix fork on the remote side. */
3930 insert_breakpoints ();
3932 /* Clean up from the last time we were running. */
3933 clear_proceed_status ();
3935 /* Let the remote process run. */
3936 proceed (-1, TARGET_SIGNAL_0, 0);
3940 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
3941 than other targets; in those use REMOTE_BREAKPOINT instead of just
3942 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
3943 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
3944 the standard routines that are in mem-break.c. */
3946 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
3947 the choice of breakpoint instruction affects target program design and
3948 vice versa, and by making it user-tweakable, the special code here
3949 goes away and we need fewer special GDB configurations. */
3951 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
3952 #define REMOTE_BREAKPOINT
3955 #ifdef REMOTE_BREAKPOINT
3957 /* If the target isn't bi-endian, just pretend it is. */
3958 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
3959 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3960 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
3963 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
3964 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
3966 #endif /* REMOTE_BREAKPOINT */
3968 /* Insert a breakpoint on targets that don't have any better breakpoint
3969 support. We read the contents of the target location and stash it,
3970 then overwrite it with a breakpoint instruction. ADDR is the target
3971 location in the target machine. CONTENTS_CACHE is a pointer to
3972 memory allocated for saving the target contents. It is guaranteed
3973 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
3974 is accomplished via BREAKPOINT_MAX). */
3977 remote_insert_breakpoint (addr, contents_cache)
3979 char *contents_cache;
3981 #ifdef REMOTE_BREAKPOINT
3984 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
3988 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
3989 val = target_write_memory (addr, (char *) big_break_insn,
3990 sizeof big_break_insn);
3992 val = target_write_memory (addr, (char *) little_break_insn,
3993 sizeof little_break_insn);
3998 return memory_insert_breakpoint (addr, contents_cache);
3999 #endif /* REMOTE_BREAKPOINT */
4003 remote_remove_breakpoint (addr, contents_cache)
4005 char *contents_cache;
4007 #ifdef REMOTE_BREAKPOINT
4008 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
4010 return memory_remove_breakpoint (addr, contents_cache);
4011 #endif /* REMOTE_BREAKPOINT */
4014 /* Some targets are only capable of doing downloads, and afterwards they switch
4015 to the remote serial protocol. This function provides a clean way to get
4016 from the download target to the remote target. It's basically just a
4017 wrapper so that we don't have to expose any of the internal workings of
4020 Prior to calling this routine, you should shutdown the current target code,
4021 else you will get the "A program is being debugged already..." message.
4022 Usually a call to pop_target() suffices.
4026 push_remote_target (name, from_tty)
4030 printf_filtered ("Switching to remote protocol\n");
4031 remote_open (name, from_tty);
4034 /* Other targets want to use the entire remote serial module but with
4035 certain remote_ops overridden. */
4038 open_remote_target (name, from_tty, target, extended_p)
4041 struct target_ops *target;
4044 printf_filtered ("Selecting the %sremote protocol\n",
4045 (extended_p ? "extended-" : ""));
4046 remote_open_1 (name, from_tty, target, extended_p);
4049 /* Table used by the crc32 function to calcuate the checksum. */
4050 static unsigned long crc32_table[256] = {0, 0};
4052 static unsigned long
4053 crc32 (buf, len, crc)
4058 if (! crc32_table[1])
4060 /* Initialize the CRC table and the decoding table. */
4064 for (i = 0; i < 256; i++)
4066 for (c = i << 24, j = 8; j > 0; --j)
4067 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
4074 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
4080 /* compare-sections command
4082 With no arguments, compares each loadable section in the exec bfd
4083 with the same memory range on the target, and reports mismatches.
4084 Useful for verifying the image on the target against the exec file.
4085 Depends on the target understanding the new "qCRC:" request. */
4088 remote_compare_command (args, from_tty)
4093 unsigned long host_crc, target_crc;
4094 extern bfd *exec_bfd;
4095 struct cleanup *old_chain;
4096 char *tmp, *sectdata, *sectname, buf[PBUFSIZ];
4102 error ("command cannot be used without an exec file");
4103 if (!current_target.to_shortname ||
4104 strcmp (current_target.to_shortname, "remote") != 0)
4105 error ("command can only be used with remote target");
4107 for (s = exec_bfd->sections; s; s = s->next)
4109 if (!(s->flags & SEC_LOAD))
4110 continue; /* skip non-loadable section */
4112 size = bfd_get_section_size_before_reloc (s);
4114 continue; /* skip zero-length section */
4116 sectname = (char *) bfd_get_section_name (exec_bfd, s);
4117 if (args && strcmp (args, sectname) != 0)
4118 continue; /* not the section selected by user */
4120 matched = 1; /* do this section */
4122 /* FIXME: assumes lma can fit into long */
4123 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
4126 /* be clever; compute the host_crc before waiting for target reply */
4127 sectdata = xmalloc (size);
4128 old_chain = make_cleanup (free, sectdata);
4129 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
4130 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
4134 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
4135 sectname, lma, lma + size);
4137 error ("remote target does not support this operation");
4139 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
4140 target_crc = target_crc * 16 + fromhex (*tmp);
4142 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
4143 sectname, lma, lma + size);
4144 if (host_crc == target_crc)
4145 printf_filtered ("matched.\n");
4147 printf_filtered ("MIS-MATCHED!\n");
4149 do_cleanups (old_chain);
4151 if (args && !matched)
4152 printf_filtered ("No loaded section named '%s'.\n", args);
4157 _initialize_remote ()
4160 init_extended_remote_ops() ;
4161 add_target (&remote_ops);
4162 add_target (&extended_remote_ops);
4164 /* Remote target communications for serial-line targets in custom GDB protocol
4165 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
4167 This file is part of GDB.
4169 This program is free software; you can redistribute it and/or modify
4170 it under the terms of the GNU General Public License as published by
4171 the Free Software Foundation; either version 2 of the License, or
4172 (at your option) any later version.
4174 This program is distributed in the hope that it will be useful,
4175 but WITHOUT ANY WARRANTY; without even the implied warranty of
4176 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4177 GNU General Public License for more details.
4179 You should have received a copy of the GNU General Public License
4180 along with this program; if not, write to the Free Software
4181 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
4183 /* Remote communication protocol.
4185 A debug packet whose contents are <data>
4186 is encapsulated for transmission in the form:
4188 $ <data> # CSUM1 CSUM2
4190 <data> must be ASCII alphanumeric and cannot include characters
4191 '$' or '#'. If <data> starts with two characters followed by
4192 ':', then the existing stubs interpret this as a sequence number.
4194 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
4195 checksum of <data>, the most significant nibble is sent first.
4196 the hex digits 0-9,a-f are used.
4198 Receiver responds with:
4200 + - if CSUM is correct and ready for next packet
4201 - - if CSUM is incorrect
4203 <data> is as follows:
4204 Most values are encoded in ascii hex digits. Signal numbers are according
4205 to the numbering in target.h.
4209 set thread Hct... Set thread for subsequent operations.
4210 c = 'c' for thread used in step and
4211 continue; t... can be -1 for all
4213 c = 'g' for thread used in other
4214 operations. If zero, pick a thread,
4216 reply OK for success
4220 reply XX....X Each byte of register data
4221 is described by two hex digits.
4222 Registers are in the internal order
4223 for GDB, and the bytes in a register
4224 are in the same order the machine uses.
4225 or ENN for an error.
4227 write regs GXX..XX Each byte of register data
4228 is described by two hex digits.
4229 reply OK for success
4232 write reg Pn...=r... Write register n... with value r...,
4233 which contains two hex digits for each
4234 byte in the register (target byte
4236 reply OK for success
4238 (not supported by all stubs).
4240 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
4241 reply XX..XX XX..XX is mem contents
4242 Can be fewer bytes than requested
4243 if able to read only part of the data.
4246 write mem MAA..AA,LLLL:XX..XX
4248 LLLL is number of bytes,
4250 reply OK for success
4251 ENN for an error (this includes the case
4252 where only part of the data was
4255 continue cAA..AA AA..AA is address to resume
4256 If AA..AA is omitted,
4257 resume at same address.
4259 step sAA..AA AA..AA is address to resume
4260 If AA..AA is omitted,
4261 resume at same address.
4263 continue with Csig;AA..AA Continue with signal sig (hex signal
4264 signal number). If ;AA..AA is omitted, resume
4267 step with Ssig;AA..AA Like 'C' but step not continue.
4270 last signal ? Reply the current reason for stopping.
4271 This is the same reply as is generated
4272 for step or cont : SAA where AA is the
4277 There is no immediate reply to step or cont.
4278 The reply comes when the machine stops.
4279 It is SAA AA is the signal number.
4281 or... TAAn...:r...;n...:r...;n...:r...;
4283 n... = register number (hex)
4284 r... = register contents
4286 r... = thread process ID. This is
4288 n... = other string not starting
4289 with valid hex digit.
4290 gdb should ignore this n,r pair
4291 and go on to the next. This way
4292 we can extend the protocol.
4293 or... WAA The process exited, and AA is
4294 the exit status. This is only
4295 applicable for certains sorts of
4297 or... XAA The process terminated with signal
4299 or... OXX..XX XX..XX is hex encoding of ASCII data. This
4300 can happen at any time while the program is
4301 running and the debugger should
4302 continue to wait for 'W', 'T', etc.
4304 thread alive TXX Find out if the thread XX is alive.
4305 reply OK thread is still alive
4308 remote restart RXX Restart the remote server
4310 extended ops ! Use the extended remote protocol.
4311 Sticky -- only needs to be set once.
4315 toggle debug d toggle debug flag (see 386 & 68k stubs)
4316 reset r reset -- see sparc stub.
4317 reserved <other> On other requests, the stub should
4318 ignore the request and send an empty
4319 response ($#<checksum>). This way
4320 we can extend the protocol and GDB
4321 can tell whether the stub it is
4322 talking to uses the old or the new.
4323 search tAA:PP,MM Search backwards starting at address
4324 AA for a match with pattern PP and
4325 mask MM. PP and MM are 4 bytes.
4326 Not supported by all stubs.
4328 general query qXXXX Request info about XXXX.
4329 general set QXXXX=yyyy Set value of XXXX to yyyy.
4330 query sect offs qOffsets Get section offsets. Reply is
4331 Text=xxx;Data=yyy;Bss=zzz
4333 Responses can be run-length encoded to save space. A '*' means that
4334 the next character is an ASCII encoding giving a repeat count which
4335 stands for that many repititions of the character preceding the '*'.
4336 The encoding is n+29, yielding a printable character where n >=3
4337 (which is where rle starts to win). Don't use an n > 126.
4340 "0* " means the same as "0000". */
4343 #include "gdb_string.h"
4346 #include "inferior.h"
4348 #include "symfile.h"
4351 /*#include "terminal.h"*/
4353 #include "objfiles.h"
4354 #include "gdb-stabs.h"
4355 #include "gdbthread.h"
4360 #include <sys/types.h>
4366 /* Prototypes for local functions */
4368 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
4369 char *myaddr, int len));
4371 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
4372 char *myaddr, int len));
4374 static void remote_files_info PARAMS ((struct target_ops *ignore));
4376 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
4377 int len, int should_write,
4378 struct target_ops *target));
4380 static void remote_prepare_to_store PARAMS ((void));
4382 static void remote_fetch_registers PARAMS ((int regno));
4384 static void remote_resume PARAMS ((int pid, int step,
4385 enum target_signal siggnal));
4387 static int remote_start_remote PARAMS ((char *dummy));
4389 static void remote_open PARAMS ((char *name, int from_tty));
4391 static void extended_remote_open PARAMS ((char *name, int from_tty));
4393 static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p));
4395 static void remote_close PARAMS ((int quitting));
4397 static void remote_store_registers PARAMS ((int regno));
4399 static void remote_mourn PARAMS ((void));
4401 static void extended_remote_restart PARAMS ((void));
4403 static void extended_remote_mourn PARAMS ((void));
4405 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
4407 static void remote_mourn_1 PARAMS ((struct target_ops *));
4409 static void remote_send PARAMS ((char *buf));
4411 static int readchar PARAMS ((int timeout));
4413 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
4415 static void remote_kill PARAMS ((void));
4417 static int tohex PARAMS ((int nib));
4419 static void remote_detach PARAMS ((char *args, int from_tty));
4421 static void remote_interrupt PARAMS ((int signo));
4423 static void remote_interrupt_twice PARAMS ((int signo));
4425 static void interrupt_query PARAMS ((void));
4427 static void set_thread PARAMS ((int, int));
4429 static int remote_thread_alive PARAMS ((int));
4431 static void get_offsets PARAMS ((void));
4433 static int read_frame PARAMS ((char *));
4435 static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
4437 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
4439 static int hexnumlen PARAMS ((ULONGEST num));
4441 /* exported functions */
4443 extern int fromhex PARAMS ((int a));
4444 extern void getpkt PARAMS ((char *buf, int forever));
4445 extern int putpkt PARAMS ((char *buf));
4447 /* Define the target subroutine names */
4449 static struct target_ops remote_ops ;
4451 static void init_remote_ops(void)
4453 remote_ops.to_shortname = "remote";
4454 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
4455 remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
4456 Specify the serial device it is connected to (e.g. /dev/ttya)." ;
4457 remote_ops.to_open = remote_open;
4458 remote_ops.to_close = remote_close;
4459 remote_ops.to_attach = NULL;
4460 remote_ops.to_detach = remote_detach;
4461 remote_ops.to_resume = remote_resume;
4462 remote_ops.to_wait = remote_wait;
4463 remote_ops.to_fetch_registers = remote_fetch_registers;
4464 remote_ops.to_store_registers = remote_store_registers;
4465 remote_ops.to_prepare_to_store = remote_prepare_to_store;
4466 remote_ops.to_xfer_memory = remote_xfer_memory;
4467 remote_ops.to_files_info = remote_files_info;
4468 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
4469 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
4470 remote_ops.to_terminal_init = NULL;
4471 remote_ops.to_terminal_inferior = NULL;
4472 remote_ops.to_terminal_ours_for_output = NULL;
4473 remote_ops.to_terminal_ours = NULL;
4474 remote_ops.to_terminal_info = NULL;
4475 remote_ops.to_kill = remote_kill;
4476 remote_ops.to_load = generic_load;
4477 remote_ops.to_lookup_symbol = NULL;
4478 remote_ops.to_create_inferior = NULL;
4479 remote_ops.to_mourn_inferior = remote_mourn;
4480 remote_ops.to_can_run = 0;
4481 remote_ops.to_notice_signals = 0;
4482 remote_ops.to_thread_alive = remote_thread_alive;
4483 remote_ops.to_stop = 0;
4484 remote_ops.to_stratum = process_stratum;
4485 remote_ops.DONT_USE = NULL;
4486 remote_ops.to_has_all_memory = 1;
4487 remote_ops.to_has_memory = 1;
4488 remote_ops.to_has_stack = 1;
4489 remote_ops.to_has_registers = 1;
4490 remote_ops.to_has_execution = 1;
4491 remote_ops.to_sections = NULL;
4492 remote_ops.to_sections_end = NULL;
4493 remote_ops.to_magic = OPS_MAGIC ;
4494 } /* init_remote_ops */
4496 static struct target_ops extended_remote_ops ;
4498 static void init_extended_remote_ops(void)
4500 extended_remote_ops.to_shortname = "extended-remote";
4501 extended_remote_ops.to_longname = "Extended remote serial target in gdb-specific protocol";
4502 extended_remote_ops.to_doc = "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
4503 Specify the serial device it is connected to (e.g. /dev/ttya).",
4504 extended_remote_ops.to_open = extended_remote_open;
4505 extended_remote_ops.to_close = remote_close;
4506 extended_remote_ops.to_attach = NULL;
4507 extended_remote_ops.to_detach = remote_detach;
4508 extended_remote_ops.to_resume = remote_resume;
4509 extended_remote_ops.to_wait = remote_wait;
4510 extended_remote_ops.to_fetch_registers = remote_fetch_registers;
4511 extended_remote_ops.to_store_registers = remote_store_registers;
4512 extended_remote_ops.to_prepare_to_store = remote_prepare_to_store;
4513 extended_remote_ops.to_xfer_memory = remote_xfer_memory;
4514 extended_remote_ops.to_files_info = remote_files_info;
4515 extended_remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
4516 extended_remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
4517 extended_remote_ops.to_terminal_init = NULL;
4518 extended_remote_ops.to_terminal_inferior = NULL;
4519 extended_remote_ops.to_terminal_ours_for_output = NULL;
4520 extended_remote_ops.to_terminal_ours = NULL;
4521 extended_remote_ops.to_terminal_info = NULL;
4522 extended_remote_ops.to_kill = remote_kill;
4523 extended_remote_ops.to_load = generic_load;
4524 extended_remote_ops.to_lookup_symbol = NULL;
4525 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
4526 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
4527 extended_remote_ops.to_can_run = 0;
4528 extended_remote_ops.to_notice_signals = 0;
4529 extended_remote_ops.to_thread_alive = remote_thread_alive;
4530 extended_remote_ops.to_stop = 0;
4531 extended_remote_ops.to_stratum = process_stratum;
4532 extended_remote_ops.DONT_USE = NULL;
4533 extended_remote_ops.to_has_all_memory = 1;
4534 extended_remote_ops.to_has_memory = 1;
4535 extended_remote_ops.to_has_stack = 1;
4536 extended_remote_ops.to_has_registers = 1;
4537 extended_remote_ops.to_has_execution = 1;
4538 extended_remote_ops.to_sections = NULL;
4539 extended_remote_ops.to_sections_end = NULL;
4540 extended_remote_ops.to_magic = OPS_MAGIC ;
4544 /* This was 5 seconds, which is a long time to sit and wait.
4545 Unless this is going though some terminal server or multiplexer or
4546 other form of hairy serial connection, I would think 2 seconds would
4549 /* Changed to allow option to set timeout value.
4550 was static int remote_timeout = 2; */
4551 extern int remote_timeout;
4553 /* This variable chooses whether to send a ^C or a break when the user
4554 requests program interruption. Although ^C is usually what remote
4555 systems expect, and that is the default here, sometimes a break is
4556 preferable instead. */
4558 static int remote_break;
4560 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
4561 remote_open knows that we don't have a file open when the program
4563 static serial_t remote_desc = NULL;
4565 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
4566 and i386-stub.c. Normally, no one would notice because it only matters
4567 for writing large chunks of memory (e.g. in downloads). Also, this needs
4568 to be more than 400 if required to hold the registers (see below, where
4569 we round it up based on REGISTER_BYTES). */
4572 /* Maximum number of bytes to read/write at once. The value here
4573 is chosen to fill up a packet (the headers account for the 32). */
4574 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
4576 /* Round up PBUFSIZ to hold all the registers, at least. */
4577 /* The blank line after the #if seems to be required to work around a
4578 bug in HP's PA compiler. */
4579 #if REGISTER_BYTES > MAXBUFBYTES
4582 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
4585 /* This variable sets the number of bytes to be written to the target
4586 in a single packet. Normally PBUFSIZ is satisfactory, but some
4587 targets need smaller values (perhaps because the receiving end
4590 static int remote_write_size = PBUFSIZ;
4592 /* This is the size (in chars) of the first response to the `g' command. This
4593 is used to limit the size of the memory read and write commands to prevent
4594 stub buffers from overflowing. The size does not include headers and
4595 trailers, it is only the payload size. */
4597 static int remote_register_buf_size = 0;
4599 /* Should we try the 'P' request? If this is set to one when the stub
4600 doesn't support 'P', the only consequence is some unnecessary traffic. */
4601 static int stub_supports_P = 1;
4603 /* These are pointers to hook functions that may be set in order to
4604 modify resume/wait behavior for a particular architecture. */
4606 void (*target_resume_hook) PARAMS ((void));
4607 void (*target_wait_loop_hook) PARAMS ((void));
4610 /* These are the threads which we last sent to the remote system. -1 for all
4611 or -2 for not sent yet. */
4616 set_thread (th, gen)
4621 int state = gen ? general_thread : cont_thread;
4625 buf[1] = gen ? 'g' : 'c';
4632 sprintf (&buf[2], "-%x", -th);
4634 sprintf (&buf[2], "%x", th);
4638 general_thread = th;
4643 /* Return nonzero if the thread TH is still alive on the remote system. */
4646 remote_thread_alive (th)
4653 sprintf (&buf[1], "-%x", -th);
4655 sprintf (&buf[1], "%x", th);
4658 return (buf[0] == 'O' && buf[1] == 'K');
4661 /* Restart the remote side; this is an extended protocol operation. */
4664 extended_remote_restart ()
4668 /* Send the restart command; for reasons I don't understand the
4669 remote side really expects a number after the "R". */
4671 sprintf (&buf[1], "%x", 0);
4674 /* Now query for status so this looks just like we restarted
4675 gdbserver from scratch. */
4680 /* Clean up connection to a remote debugger. */
4684 remote_close (quitting)
4688 SERIAL_CLOSE (remote_desc);
4692 /* Query the remote side for the text, data and bss offsets. */
4697 char buf[PBUFSIZ], *ptr;
4699 CORE_ADDR text_addr, data_addr, bss_addr;
4700 struct section_offsets *offs;
4702 putpkt ("qOffsets");
4706 if (buf[0] == '\000')
4707 return; /* Return silently. Stub doesn't support this
4711 warning ("Remote failure reply: %s", buf);
4715 /* Pick up each field in turn. This used to be done with scanf, but
4716 scanf will make trouble if CORE_ADDR size doesn't match
4717 conversion directives correctly. The following code will work
4718 with any size of CORE_ADDR. */
4719 text_addr = data_addr = bss_addr = 0;
4723 if (strncmp (ptr, "Text=", 5) == 0)
4726 /* Don't use strtol, could lose on big values. */
4727 while (*ptr && *ptr != ';')
4728 text_addr = (text_addr << 4) + fromhex (*ptr++);
4733 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
4736 while (*ptr && *ptr != ';')
4737 data_addr = (data_addr << 4) + fromhex (*ptr++);
4742 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
4745 while (*ptr && *ptr != ';')
4746 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
4752 error ("Malformed response to offset query, %s", buf);
4754 if (symfile_objfile == NULL)
4757 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
4758 + symfile_objfile->num_sections
4759 * sizeof (offs->offsets));
4760 memcpy (offs, symfile_objfile->section_offsets,
4761 sizeof (struct section_offsets)
4762 + symfile_objfile->num_sections
4763 * sizeof (offs->offsets));
4765 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
4767 /* This is a temporary kludge to force data and bss to use the same offsets
4768 because that's what nlmconv does now. The real solution requires changes
4769 to the stub and remote.c that I don't have time to do right now. */
4771 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
4772 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
4774 objfile_relocate (symfile_objfile, offs);
4777 /* Stub for catch_errors. */
4780 remote_start_remote (dummy)
4783 immediate_quit = 1; /* Allow user to interrupt it */
4785 /* Ack any packet which the remote side has already sent. */
4786 SERIAL_WRITE (remote_desc, "+", 1);
4788 /* Let the stub know that we want it to return the thread. */
4791 get_offsets (); /* Get text, data & bss offsets */
4793 putpkt ("?"); /* initiate a query from remote machine */
4796 start_remote (); /* Initialize gdb process mechanisms */
4800 /* Open a connection to a remote debugger.
4801 NAME is the filename used for communication. */
4804 remote_open (name, from_tty)
4808 remote_open_1 (name, from_tty, &remote_ops, 0);
4811 /* Open a connection to a remote debugger using the extended
4812 remote gdb protocol. NAME is the filename used for communication. */
4815 extended_remote_open (name, from_tty)
4819 remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/);
4822 /* Generic code for opening a connection to a remote target. */
4823 static DCACHE *remote_dcache;
4826 remote_open_1 (name, from_tty, target, extended_p)
4829 struct target_ops *target;
4833 error ("To open a remote debug connection, you need to specify what serial\n\
4834 device is attached to the remote system (e.g. /dev/ttya).");
4836 target_preopen (from_tty);
4838 unpush_target (target);
4840 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
4842 remote_desc = SERIAL_OPEN (name);
4844 perror_with_name (name);
4846 if (baud_rate != -1)
4848 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
4850 SERIAL_CLOSE (remote_desc);
4851 perror_with_name (name);
4856 SERIAL_RAW (remote_desc);
4858 /* If there is something sitting in the buffer we might take it as a
4859 response to a command, which would be bad. */
4860 SERIAL_FLUSH_INPUT (remote_desc);
4864 puts_filtered ("Remote debugging using ");
4865 puts_filtered (name);
4866 puts_filtered ("\n");
4868 push_target (target); /* Switch to using remote target now */
4870 /* Start out by trying the 'P' request to set registers. We set this each
4871 time that we open a new target so that if the user switches from one
4872 stub to another, we can (if the target is closed and reopened) cope. */
4873 stub_supports_P = 1;
4875 general_thread = -2;
4878 /* Without this, some commands which require an active target (such as kill)
4879 won't work. This variable serves (at least) double duty as both the pid
4880 of the target process (if it has such), and as a flag indicating that a
4881 target is active. These functions should be split out into seperate
4882 variables, especially since GDB will someday have a notion of debugging
4883 several processes. */
4885 inferior_pid = 42000;
4886 /* Start the remote connection; if error (0), discard this target.
4887 In particular, if the user quits, be sure to discard it
4888 (we'd be in an inconsistent state otherwise). */
4889 if (!catch_errors (remote_start_remote, (char *)0,
4890 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
4898 /* tell the remote that we're using the extended protocol. */
4905 /* This takes a program previously attached to and detaches it. After
4906 this is done, GDB can be used to debug some other program. We
4907 better not have left any breakpoints in the target program or it'll
4908 die when it hits one. */
4911 remote_detach (args, from_tty)
4918 error ("Argument given to \"detach\" when remotely debugging.");
4920 /* Tell the remote target to detach. */
4926 puts_filtered ("Ending remote debugging.\n");
4929 /* Convert hex digit A to a number. */
4935 if (a >= '0' && a <= '9')
4937 else if (a >= 'a' && a <= 'f')
4938 return a - 'a' + 10;
4939 else if (a >= 'A' && a <= 'F')
4940 return a - 'A' + 10;
4942 error ("Reply contains invalid hex digit %d", a);
4945 /* Convert number NIB to a hex digit. */
4957 /* Tell the remote machine to resume. */
4959 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
4963 remote_resume (pid, step, siggnal)
4965 enum target_signal siggnal;
4970 set_thread (inferior_pid, 0);
4972 set_thread (pid, 0);
4974 dcache_flush (remote_dcache);
4976 last_sent_signal = siggnal;
4977 last_sent_step = step;
4979 /* A hook for when we need to do something at the last moment before
4981 if (target_resume_hook)
4982 (*target_resume_hook) ();
4984 if (siggnal != TARGET_SIGNAL_0)
4986 buf[0] = step ? 'S' : 'C';
4987 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
4988 buf[2] = tohex ((int)siggnal & 0xf);
4992 strcpy (buf, step ? "s": "c");
4997 /* Send ^C to target to halt it. Target will respond, and send us a
5001 remote_interrupt (signo)
5004 /* If this doesn't work, try more severe steps. */
5005 signal (signo, remote_interrupt_twice);
5008 printf_unfiltered ("remote_interrupt called\n");
5010 /* Send a break or a ^C, depending on user preference. */
5012 SERIAL_SEND_BREAK (remote_desc);
5014 SERIAL_WRITE (remote_desc, "\003", 1);
5017 static void (*ofunc)();
5019 /* The user typed ^C twice. */
5021 remote_interrupt_twice (signo)
5024 signal (signo, ofunc);
5028 signal (signo, remote_interrupt);
5031 /* Ask the user what to do when an interrupt is received. */
5036 target_terminal_ours ();
5038 if (query ("Interrupted while waiting for the program.\n\
5039 Give up (and stop debugging it)? "))
5041 target_mourn_inferior ();
5042 return_to_top_level (RETURN_QUIT);
5045 target_terminal_inferior ();
5048 /* If nonzero, ignore the next kill. */
5052 remote_console_output (msg)
5057 for (p = msg; *p; p +=2)
5060 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
5063 if (target_output_hook)
5064 target_output_hook (tb);
5066 fputs_filtered (tb, gdb_stdout);
5070 /* Wait until the remote machine stops, then return,
5071 storing status in STATUS just as `wait' would.
5072 Returns "pid" (though it's not clear what, if anything, that
5073 means in the case of this target). */
5076 remote_wait (pid, status)
5078 struct target_waitstatus *status;
5080 unsigned char buf[PBUFSIZ];
5081 int thread_num = -1;
5083 status->kind = TARGET_WAITKIND_EXITED;
5084 status->value.integer = 0;
5090 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
5091 getpkt ((char *) buf, 1);
5092 signal (SIGINT, ofunc);
5094 /* This is a hook for when we need to do something (perhaps the
5095 collection of trace data) every time the target stops. */
5096 if (target_wait_loop_hook)
5097 (*target_wait_loop_hook) ();
5101 case 'E': /* Error of some sort */
5102 warning ("Remote failure reply: %s", buf);
5104 case 'T': /* Status with PC, SP, FP, ... */
5108 char regs[MAX_REGISTER_RAW_SIZE];
5110 /* Expedited reply, containing Signal, {regno, reg} repeat */
5111 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
5113 n... = register number
5114 r... = register contents
5116 p = &buf[3]; /* after Txx */
5123 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
5124 p1 = (unsigned char *)p_temp;
5128 p1 = (unsigned char *) strchr ((const char *) p, ':');
5130 warning ("Malformed packet (missing colon): %s\n\
5133 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
5135 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
5136 p = (unsigned char *)p_temp;
5144 warning ("Malformed packet (missing colon): %s\n\
5148 if (regno >= NUM_REGS)
5149 warning ("Remote sent bad register number %ld: %s\n\
5153 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
5155 if (p[0] == 0 || p[1] == 0)
5156 warning ("Remote reply is too short: %s", buf);
5157 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
5160 supply_register (regno, regs);
5164 warning ("Remote register badly formatted: %s", buf);
5168 case 'S': /* Old style status, just signal only */
5169 status->kind = TARGET_WAITKIND_STOPPED;
5170 status->value.sig = (enum target_signal)
5171 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
5174 case 'W': /* Target exited */
5176 /* The remote process exited. */
5177 status->kind = TARGET_WAITKIND_EXITED;
5178 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
5182 status->kind = TARGET_WAITKIND_SIGNALLED;
5183 status->value.sig = (enum target_signal)
5184 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
5188 case 'O': /* Console output */
5189 remote_console_output (buf + 1);
5192 if (last_sent_signal != TARGET_SIGNAL_0)
5194 /* Zero length reply means that we tried 'S' or 'C' and
5195 the remote system doesn't support it. */
5196 target_terminal_ours_for_output ();
5198 ("Can't send signals to this remote system. %s not sent.\n",
5199 target_signal_to_name (last_sent_signal));
5200 last_sent_signal = TARGET_SIGNAL_0;
5201 target_terminal_inferior ();
5203 strcpy ((char *) buf, last_sent_step ? "s" : "c");
5204 putpkt ((char *) buf);
5207 /* else fallthrough */
5209 warning ("Invalid remote reply: %s", buf);
5214 if (thread_num != -1)
5216 /* Initial thread value can only be acquired via wait, so deal with
5217 this marker which is used before the first thread value is
5219 if (inferior_pid == 42000)
5221 inferior_pid = thread_num;
5222 add_thread (inferior_pid);
5226 return inferior_pid;
5229 /* Number of bytes of registers this stub implements. */
5230 static int register_bytes_found;
5232 /* Read the remote registers into the block REGS. */
5233 /* Currently we just read all the registers, so we don't use regno. */
5236 remote_fetch_registers (regno)
5242 char regs[REGISTER_BYTES];
5244 set_thread (inferior_pid, 1);
5249 if (remote_register_buf_size == 0)
5250 remote_register_buf_size = strlen (buf);
5252 /* Unimplemented registers read as all bits zero. */
5253 memset (regs, 0, REGISTER_BYTES);
5255 /* We can get out of synch in various cases. If the first character
5256 in the buffer is not a hex character, assume that has happened
5257 and try to fetch another packet to read. */
5258 while ((buf[0] < '0' || buf[0] > '9')
5259 && (buf[0] < 'a' || buf[0] > 'f'))
5262 printf_unfiltered ("Bad register packet; fetching a new packet\n");
5266 /* Reply describes registers byte by byte, each byte encoded as two
5267 hex characters. Suck them all up, then supply them to the
5268 register cacheing/storage mechanism. */
5271 for (i = 0; i < REGISTER_BYTES; i++)
5277 warning ("Remote reply is of odd length: %s", buf);
5278 /* Don't change register_bytes_found in this case, and don't
5279 print a second warning. */
5282 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
5286 if (i != register_bytes_found)
5288 register_bytes_found = i;
5289 #ifdef REGISTER_BYTES_OK
5290 if (!REGISTER_BYTES_OK (i))
5291 warning ("Remote reply is too short: %s", buf);
5296 for (i = 0; i < NUM_REGS; i++)
5297 supply_register (i, ®s[REGISTER_BYTE(i)]);
5300 /* Prepare to store registers. Since we may send them all (using a
5301 'G' request), we have to read out the ones we don't want to change
5305 remote_prepare_to_store ()
5307 /* Make sure the entire registers array is valid. */
5308 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
5311 /* Store register REGNO, or all registers if REGNO == -1, from the contents
5312 of REGISTERS. FIXME: ignores errors. */
5315 remote_store_registers (regno)
5322 set_thread (inferior_pid, 1);
5324 if (regno >= 0 && stub_supports_P)
5326 /* Try storing a single register. */
5329 sprintf (buf, "P%x=", regno);
5330 p = buf + strlen (buf);
5331 regp = ®isters[REGISTER_BYTE (regno)];
5332 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
5334 *p++ = tohex ((regp[i] >> 4) & 0xf);
5335 *p++ = tohex (regp[i] & 0xf);
5341 /* The stub understands the 'P' request. We are done. */
5345 /* The stub does not support the 'P' request. Use 'G' instead,
5346 and don't try using 'P' in the future (it will just waste our
5348 stub_supports_P = 0;
5353 /* Command describes registers byte by byte,
5354 each byte encoded as two hex characters. */
5357 /* remote_prepare_to_store insures that register_bytes_found gets set. */
5358 for (i = 0; i < register_bytes_found; i++)
5360 *p++ = tohex ((registers[i] >> 4) & 0xf);
5361 *p++ = tohex (registers[i] & 0xf);
5369 Use of the data cache *used* to be disabled because it loses for looking at
5370 and changing hardware I/O ports and the like. Accepting `volatile'
5371 would perhaps be one way to fix it. Another idea would be to use the
5372 executable file for the text segment (for all SEC_CODE sections?
5373 For all SEC_READONLY sections?). This has problems if you want to
5374 actually see what the memory contains (e.g. self-modifying code,
5375 clobbered memory, user downloaded the wrong thing).
5377 Because it speeds so much up, it's now enabled, if you're playing
5378 with registers you turn it of (set remotecache 0)
5381 /* Read a word from remote address ADDR and return it.
5382 This goes through the data cache. */
5386 remote_fetch_word (addr)
5389 return dcache_fetch (remote_dcache, addr);
5392 /* Write a word WORD into remote address ADDR.
5393 This goes through the data cache. */
5396 remote_store_word (addr, word)
5400 dcache_poke (remote_dcache, addr, word);
5402 #endif /* 0 (unused?) */
5406 /* Return the number of hex digits in num. */
5414 for (i = 0; num != 0; i++)
5420 /* Write memory data directly to the remote machine.
5421 This does not inform the data cache; the data cache uses this.
5422 MEMADDR is the address in the remote memory space.
5423 MYADDR is the address of the buffer in our space.
5424 LEN is the number of bytes.
5426 Returns number of bytes transferred, or 0 for error. */
5429 remote_write_bytes (memaddr, myaddr, len)
5434 int max_buf_size; /* Max size of packet output buffer */
5437 /* Chop the transfer down if necessary */
5439 max_buf_size = min (remote_write_size, PBUFSIZ);
5440 if (remote_register_buf_size != 0)
5441 max_buf_size = min (max_buf_size, remote_register_buf_size);
5443 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
5444 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
5454 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
5456 /* FIXME-32x64: Need a version of print_address_numeric which puts the
5457 result in a buffer like sprintf. */
5458 sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo);
5460 /* We send target system values byte by byte, in increasing byte addresses,
5461 each byte encoded as two hex characters. */
5463 p = buf + strlen (buf);
5464 for (i = 0; i < todo; i++)
5466 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
5467 *p++ = tohex (myaddr[i] & 0xf);
5476 /* There is no correspondance between what the remote protocol uses
5477 for errors and errno codes. We would like a cleaner way of
5478 representing errors (big enough to include errno codes, bfd_error
5479 codes, and others). But for now just return EIO. */
5490 /* Read memory data directly from the remote machine.
5491 This does not use the data cache; the data cache uses this.
5492 MEMADDR is the address in the remote memory space.
5493 MYADDR is the address of the buffer in our space.
5494 LEN is the number of bytes.
5496 Returns number of bytes transferred, or 0 for error. */
5499 remote_read_bytes (memaddr, myaddr, len)
5504 int max_buf_size; /* Max size of packet output buffer */
5507 /* Chop the transfer down if necessary */
5509 max_buf_size = min (remote_write_size, PBUFSIZ);
5510 if (remote_register_buf_size != 0)
5511 max_buf_size = min (max_buf_size, remote_register_buf_size);
5521 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
5523 /* FIXME-32x64: Need a version of print_address_numeric which puts the
5524 result in a buffer like sprintf. */
5525 sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo);
5531 /* There is no correspondance between what the remote protocol uses
5532 for errors and errno codes. We would like a cleaner way of
5533 representing errors (big enough to include errno codes, bfd_error
5534 codes, and others). But for now just return EIO. */
5539 /* Reply describes memory byte by byte,
5540 each byte encoded as two hex characters. */
5543 for (i = 0; i < todo; i++)
5545 if (p[0] == 0 || p[1] == 0)
5546 /* Reply is short. This means that we were able to read only part
5547 of what we wanted to. */
5548 return i + (origlen - len);
5549 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
5559 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
5560 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
5561 nonzero. Returns length of data written or read; 0 for error. */
5565 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
5570 struct target_ops *target; /* ignored */
5572 #ifdef REMOTE_TRANSLATE_XFER_ADDRESS
5575 REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen);
5582 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
5587 /* Enable after 4.12. */
5590 remote_search (len, data, mask, startaddr, increment, lorange, hirange
5591 addr_found, data_found)
5595 CORE_ADDR startaddr;
5599 CORE_ADDR *addr_found;
5602 if (increment == -4 && len == 4)
5604 long mask_long, data_long;
5605 long data_found_long;
5606 CORE_ADDR addr_we_found;
5608 long returned_long[2];
5611 mask_long = extract_unsigned_integer (mask, len);
5612 data_long = extract_unsigned_integer (data, len);
5613 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
5618 /* The stub doesn't support the 't' request. We might want to
5619 remember this fact, but on the other hand the stub could be
5620 switched on us. Maybe we should remember it only until
5621 the next "target remote". */
5622 generic_search (len, data, mask, startaddr, increment, lorange,
5623 hirange, addr_found, data_found);
5628 /* There is no correspondance between what the remote protocol uses
5629 for errors and errno codes. We would like a cleaner way of
5630 representing errors (big enough to include errno codes, bfd_error
5631 codes, and others). But for now just use EIO. */
5632 memory_error (EIO, startaddr);
5635 while (*p != '\0' && *p != ',')
5636 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
5638 error ("Protocol error: short return for search");
5640 data_found_long = 0;
5641 while (*p != '\0' && *p != ',')
5642 data_found_long = (data_found_long << 4) + fromhex (*p++);
5643 /* Ignore anything after this comma, for future extensions. */
5645 if (addr_we_found < lorange || addr_we_found >= hirange)
5651 *addr_found = addr_we_found;
5652 *data_found = store_unsigned_integer (data_we_found, len);
5655 generic_search (len, data, mask, startaddr, increment, lorange,
5656 hirange, addr_found, data_found);
5661 remote_files_info (ignore)
5662 struct target_ops *ignore;
5664 puts_filtered ("Debugging a target over a serial line.\n");
5667 /* Stuff for dealing with the packets which are part of this protocol.
5668 See comment at top of file for details. */
5670 /* Read a single character from the remote end, masking it down to 7 bits. */
5678 ch = SERIAL_READCHAR (remote_desc, timeout);
5683 error ("Remote connection closed");
5685 perror_with_name ("Remote communication error");
5686 case SERIAL_TIMEOUT:
5693 /* Send the command in BUF to the remote machine,
5694 and read the reply into BUF.
5695 Report an error if we get an error reply. */
5705 error ("Remote failure reply: %s", buf);
5708 /* Send a packet to the remote machine, with error checking.
5709 The data of the packet is in BUF. */
5716 unsigned char csum = 0;
5718 int cnt = strlen (buf);
5723 /* Copy the packet into buffer BUF2, encapsulating it
5724 and giving it a checksum. */
5726 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
5732 for (i = 0; i < cnt; i++)
5738 *p++ = tohex ((csum >> 4) & 0xf);
5739 *p++ = tohex (csum & 0xf);
5741 /* Send it over and over until we get a positive ack. */
5745 int started_error_output = 0;
5750 printf_unfiltered ("Sending packet: %s...", buf2);
5751 gdb_flush(gdb_stdout);
5753 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
5754 perror_with_name ("putpkt: write failed");
5756 /* read until either a timeout occurs (-2) or '+' is read */
5759 ch = readchar (remote_timeout);
5766 case SERIAL_TIMEOUT:
5768 if (started_error_output)
5770 putchar_unfiltered ('\n');
5771 started_error_output = 0;
5780 printf_unfiltered("Ack\n");
5782 case SERIAL_TIMEOUT:
5786 break; /* Retransmit buffer */
5789 char junkbuf[PBUFSIZ];
5791 /* It's probably an old response, and we're out of sync. Just
5792 gobble up the packet and ignore it. */
5793 getpkt (junkbuf, 0);
5794 continue; /* Now, go look for + */
5799 if (!started_error_output)
5801 started_error_output = 1;
5802 printf_unfiltered ("putpkt: Junk: ");
5804 putchar_unfiltered (ch & 0177);
5808 break; /* Here to retransmit */
5812 /* This is wrong. If doing a long backtrace, the user should be
5813 able to get out next time we call QUIT, without anything as violent
5814 as interrupt_query. If we want to provide a way out of here
5815 without getting to the next QUIT, it should be based on hitting
5816 ^C twice as in remote_wait. */
5826 /* Come here after finding the start of the frame. Collect the rest into BUF,
5827 verifying the checksum, length, and handling run-length compression.
5828 Returns 0 on any error, 1 on success. */
5843 c = readchar (remote_timeout);
5847 case SERIAL_TIMEOUT:
5849 puts_filtered ("Timeout in mid-packet, retrying\n");
5853 puts_filtered ("Saw new packet start in middle of old one\n");
5854 return 0; /* Start a new packet, count retries */
5857 unsigned char pktcsum;
5861 pktcsum = fromhex (readchar (remote_timeout)) << 4;
5862 pktcsum |= fromhex (readchar (remote_timeout));
5864 if (csum == pktcsum)
5869 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
5871 puts_filtered (buf);
5872 puts_filtered ("\n");
5876 case '*': /* Run length encoding */
5878 c = readchar (remote_timeout);
5880 c = c - ' ' + 3; /* Compute repeat count */
5883 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
5885 memset (bp, *(bp - 1), c);
5891 printf_filtered ("Repeat count %d too large for buffer: ", c);
5892 puts_filtered (buf);
5893 puts_filtered ("\n");
5897 if (bp < buf + PBUFSIZ - 1)
5905 puts_filtered ("Remote packet too long: ");
5906 puts_filtered (buf);
5907 puts_filtered ("\n");
5914 /* Read a packet from the remote machine, with error checking,
5915 and store it in BUF. BUF is expected to be of size PBUFSIZ.
5916 If FOREVER, wait forever rather than timing out; this is used
5917 while the target is executing user code. */
5920 getpkt (buf, forever)
5929 strcpy (buf,"timeout");
5933 #ifdef MAINTENANCE_CMDS
5934 timeout = watchdog > 0 ? watchdog : -1;
5941 timeout = remote_timeout;
5945 for (tries = 1; tries <= MAX_TRIES; tries++)
5947 /* This can loop forever if the remote side sends us characters
5948 continuously, but if it pauses, we'll get a zero from readchar
5949 because of timeout. Then we'll count that as a retry. */
5951 /* Note that we will only wait forever prior to the start of a packet.
5952 After that, we expect characters to arrive at a brisk pace. They
5953 should show up within remote_timeout intervals. */
5957 c = readchar (timeout);
5959 if (c == SERIAL_TIMEOUT)
5961 #ifdef MAINTENANCE_CMDS
5962 if (forever) /* Watchdog went off. Kill the target. */
5964 target_mourn_inferior ();
5965 error ("Watchdog has expired. Target detached.\n");
5969 puts_filtered ("Timed out.\n");
5975 /* We've found the start of a packet, now collect the data. */
5977 val = read_frame (buf);
5982 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
5983 SERIAL_WRITE (remote_desc, "+", 1);
5987 /* Try the whole thing again. */
5989 SERIAL_WRITE (remote_desc, "-", 1);
5992 /* We have tried hard enough, and just can't receive the packet. Give up. */
5994 printf_unfiltered ("Ignoring packet error, continuing...\n");
5995 SERIAL_WRITE (remote_desc, "+", 1);
6001 /* For some mysterious reason, wait_for_inferior calls kill instead of
6002 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
6006 target_mourn_inferior ();
6010 /* Use catch_errors so the user can quit from gdb even when we aren't on
6011 speaking terms with the remote system. */
6012 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
6014 /* Don't wait for it to die. I'm not really sure it matters whether
6015 we do or not. For the existing stubs, kill is a noop. */
6016 target_mourn_inferior ();
6022 remote_mourn_1 (&remote_ops);
6026 extended_remote_mourn ()
6028 /* We do _not_ want to mourn the target like this; this will
6029 remove the extended remote target from the target stack,
6030 and the next time the user says "run" it'll fail.
6032 FIXME: What is the right thing to do here? */
6034 remote_mourn_1 (&extended_remote_ops);
6038 /* Worker function for remote_mourn. */
6040 remote_mourn_1 (target)
6041 struct target_ops *target;
6043 unpush_target (target);
6044 generic_mourn_inferior ();
6047 /* In the extended protocol we want to be able to do things like
6048 "run" and have them basically work as expected. So we need
6049 a special create_inferior function.
6051 FIXME: One day add support for changing the exec file
6052 we're debugging, arguments and an environment. */
6055 extended_remote_create_inferior (exec_file, args, env)
6060 /* Rip out the breakpoints; we'll reinsert them after restarting
6061 the remote server. */
6062 remove_breakpoints ();
6064 /* Now restart the remote server. */
6065 extended_remote_restart ();
6067 /* Now put the breakpoints back in. This way we're safe if the
6068 restart function works via a unix fork on the remote side. */
6069 insert_breakpoints ();
6071 /* Clean up from the last time we were running. */
6072 clear_proceed_status ();
6074 /* Let the remote process run. */
6075 proceed (-1, TARGET_SIGNAL_0, 0);
6079 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
6080 than other targets; in those use REMOTE_BREAKPOINT instead of just
6081 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
6082 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
6083 the standard routines that are in mem-break.c. */
6085 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
6086 the choice of breakpoint instruction affects target program design and
6087 vice versa, and by making it user-tweakable, the special code here
6088 goes away and we need fewer special GDB configurations. */
6090 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
6091 #define REMOTE_BREAKPOINT
6094 #ifdef REMOTE_BREAKPOINT
6096 /* If the target isn't bi-endian, just pretend it is. */
6097 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
6098 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
6099 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
6102 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
6103 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
6105 #endif /* REMOTE_BREAKPOINT */
6107 /* Insert a breakpoint on targets that don't have any better breakpoint
6108 support. We read the contents of the target location and stash it,
6109 then overwrite it with a breakpoint instruction. ADDR is the target
6110 location in the target machine. CONTENTS_CACHE is a pointer to
6111 memory allocated for saving the target contents. It is guaranteed
6112 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
6113 is accomplished via BREAKPOINT_MAX). */
6116 remote_insert_breakpoint (addr, contents_cache)
6118 char *contents_cache;
6120 #ifdef REMOTE_BREAKPOINT
6123 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
6127 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
6128 val = target_write_memory (addr, (char *) big_break_insn,
6129 sizeof big_break_insn);
6131 val = target_write_memory (addr, (char *) little_break_insn,
6132 sizeof little_break_insn);
6137 return memory_insert_breakpoint (addr, contents_cache);
6138 #endif /* REMOTE_BREAKPOINT */
6142 remote_remove_breakpoint (addr, contents_cache)
6144 char *contents_cache;
6146 #ifdef REMOTE_BREAKPOINT
6147 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
6149 return memory_remove_breakpoint (addr, contents_cache);
6150 #endif /* REMOTE_BREAKPOINT */
6153 /* Some targets are only capable of doing downloads, and afterwards they switch
6154 to the remote serial protocol. This function provides a clean way to get
6155 from the download target to the remote target. It's basically just a
6156 wrapper so that we don't have to expose any of the internal workings of
6159 Prior to calling this routine, you should shutdown the current target code,
6160 else you will get the "A program is being debugged already..." message.
6161 Usually a call to pop_target() suffices.
6165 push_remote_target (name, from_tty)
6169 printf_filtered ("Switching to remote protocol\n");
6170 remote_open (name, from_tty);
6173 /* Other targets want to use the entire remote serial module but with
6174 certain remote_ops overridden. */
6177 open_remote_target (name, from_tty, target, extended_p)
6180 struct target_ops *target;
6183 printf_filtered ("Selecting the %sremote protocol\n",
6184 (extended_p ? "extended-" : ""));
6185 remote_open_1 (name, from_tty, target, extended_p);
6188 /* Table used by the crc32 function to calcuate the checksum. */
6189 static unsigned long crc32_table[256] = {0, 0};
6191 static unsigned long
6192 crc32 (buf, len, crc)
6197 if (! crc32_table[1])
6199 /* Initialize the CRC table and the decoding table. */
6203 for (i = 0; i < 256; i++)
6205 for (c = i << 24, j = 8; j > 0; --j)
6206 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
6213 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
6219 /* compare-sections command
6221 With no arguments, compares each loadable section in the exec bfd
6222 with the same memory range on the target, and reports mismatches.
6223 Useful for verifying the image on the target against the exec file.
6224 Depends on the target understanding the new "qCRC:" request. */
6227 remote_compare_command (args, from_tty)
6232 unsigned long host_crc, target_crc;
6233 extern bfd *exec_bfd;
6234 struct cleanup *old_chain;
6235 char *tmp, *sectdata, *sectname, buf[PBUFSIZ];
6241 error ("command cannot be used without an exec file");
6242 if (!current_target.to_shortname ||
6243 strcmp (current_target.to_shortname, "remote") != 0)
6244 error ("command can only be used with remote target");
6246 for (s = exec_bfd->sections; s; s = s->next)
6248 if (!(s->flags & SEC_LOAD))
6249 continue; /* skip non-loadable section */
6251 size = bfd_get_section_size_before_reloc (s);
6253 continue; /* skip zero-length section */
6255 sectname = (char *) bfd_get_section_name (exec_bfd, s);
6256 if (args && strcmp (args, sectname) != 0)
6257 continue; /* not the section selected by user */
6259 matched = 1; /* do this section */
6261 /* FIXME: assumes lma can fit into long */
6262 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
6265 /* be clever; compute the host_crc before waiting for target reply */
6266 sectdata = xmalloc (size);
6267 old_chain = make_cleanup (free, sectdata);
6268 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
6269 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
6273 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
6274 sectname, lma, lma + size);
6276 error ("remote target does not support this operation");
6278 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
6279 target_crc = target_crc * 16 + fromhex (*tmp);
6281 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
6282 sectname, lma, lma + size);
6283 if (host_crc == target_crc)
6284 printf_filtered ("matched.\n");
6286 printf_filtered ("MIS-MATCHED!\n");
6288 do_cleanups (old_chain);
6290 if (args && !matched)
6291 printf_filtered ("No loaded section named '%s'.\n", args);
6296 With no arguments, compares each loadable section on the target
6297 with the binary image in the current exec bfd. Sections that
6298 are not identical are downloaded to the target. Depends on the
6299 target understanding the "qCRC:" request.
6301 Optionally accepts the name of a section as an argument, and
6302 downloads that section; in this case no comparison is done --
6303 the section is downloaded unconditionally. */
6306 remote_reload_command (args, from_tty)
6311 unsigned long host_crc, target_crc;
6312 extern bfd *exec_bfd;
6313 struct cleanup *old_chain;
6314 char *tmp, *sectdata, *sectname, buf[PBUFSIZ];
6317 unsigned long sent, len, l;
6322 error ("command cannot be used without an exec file");
6324 for (s = exec_bfd->sections; s; s = s->next)
6326 if (!(s->flags & SEC_LOAD))
6327 continue; /* skip non-loadable section */
6329 size = bfd_get_section_size_before_reloc (s);
6331 continue; /* skip zero-length section */
6333 sectname = (char *) bfd_get_section_name (exec_bfd, s);
6334 if (args && strcmp (args, sectname) != 0)
6335 continue; /* not the section selected by user */
6337 matched = 1; /* do this section */
6339 sectdata = xmalloc (size);
6340 old_chain = make_cleanup (free, sectdata);
6341 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
6346 * Compare all sections, and reload those that don't match.
6349 if (!current_target.to_shortname ||
6350 strcmp (current_target.to_shortname, "remote") != 0)
6351 error ("command can only be used with remote target");
6353 /* FIXME: assumes lma can fit into long */
6354 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
6357 /* be clever; compute the host_crc before waiting for target reply */
6358 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
6362 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
6363 sectname, lma, lma + size);
6365 error ("remote target does not support this operation");
6367 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
6368 target_crc = target_crc * 16 + fromhex (*tmp);
6371 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
6372 sectname, lma, lma + size);
6374 if (args != 0 || /* section specified -- reload unconditionally */
6375 host_crc != target_crc) /* section changed on target */
6377 printf_filtered ("being reloaded now.\n");
6379 l = l > 100 ? l : 100; /* chunk size; at least 100 */
6383 len = (size - sent) < l ? (size - sent) : l;
6385 err = target_write_memory (lma, sectdata, len);
6389 while (err == 0 && sent < size);
6392 printf_filtered ("unchanged.\n");
6394 do_cleanups (old_chain);
6396 if (args && !matched)
6397 printf_filtered ("No loaded section named '%s'.\n", args);
6401 _initialize_remote ()
6404 init_extended_remote_ops() ;
6405 add_target (&remote_ops);
6406 add_target (&extended_remote_ops);
6408 add_cmd ("compare-sections", class_obscure, remote_compare_command,
6409 "Compare section data on remote target to the exec file.\n\
6410 Optional argument is a single section name (default: all loadable sections).",
6413 add_show_from_set (add_set_cmd ("remotetimeout", no_class,
6414 var_integer, (char *)&remote_timeout,
6415 "Set timeout value for remote read.\n", &setlist),
6418 add_show_from_set (add_set_cmd ("remotebreak", no_class,
6419 var_integer, (char *)&remote_break,
6420 "Set whether to send break if interrupted.\n", &setlist),
6423 add_show_from_set (add_set_cmd ("remotewritesize", no_class,
6424 var_integer, (char *)&remote_write_size,
6425 "Set the maximum number of bytes in each memory write packet.\n", &setlist),