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 /* Display a null-terminated packet on stdout, for debugging, using C
1548 print_packet (char *buf)
1550 puts_filtered ("\"");
1552 gdb_printchar (*buf++, gdb_stdout, '"');
1553 puts_filtered ("\"");
1557 /* Send a packet to the remote machine, with error checking.
1558 The data of the packet is in BUF. */
1565 unsigned char csum = 0;
1567 int cnt = strlen (buf);
1572 /* Copy the packet into buffer BUF2, encapsulating it
1573 and giving it a checksum. */
1575 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
1581 for (i = 0; i < cnt; i++)
1587 *p++ = tohex ((csum >> 4) & 0xf);
1588 *p++ = tohex (csum & 0xf);
1590 /* Send it over and over until we get a positive ack. */
1594 int started_error_output = 0;
1599 printf_unfiltered ("Sending packet: %s...", buf2);
1600 gdb_flush(gdb_stdout);
1602 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1603 perror_with_name ("putpkt: write failed");
1605 /* read until either a timeout occurs (-2) or '+' is read */
1608 ch = readchar (remote_timeout);
1615 case SERIAL_TIMEOUT:
1617 if (started_error_output)
1619 putchar_unfiltered ('\n');
1620 started_error_output = 0;
1629 printf_unfiltered("Ack\n");
1631 case SERIAL_TIMEOUT:
1635 break; /* Retransmit buffer */
1638 char junkbuf[PBUFSIZ];
1640 /* It's probably an old response, and we're out of sync. Just
1641 gobble up the packet and ignore it. */
1642 getpkt (junkbuf, 0);
1643 continue; /* Now, go look for + */
1648 if (!started_error_output)
1650 started_error_output = 1;
1651 printf_unfiltered ("putpkt: Junk: ");
1653 putchar_unfiltered (ch & 0177);
1657 break; /* Here to retransmit */
1661 /* This is wrong. If doing a long backtrace, the user should be
1662 able to get out next time we call QUIT, without anything as violent
1663 as interrupt_query. If we want to provide a way out of here
1664 without getting to the next QUIT, it should be based on hitting
1665 ^C twice as in remote_wait. */
1675 /* Come here after finding the start of the frame. Collect the rest into BUF,
1676 verifying the checksum, length, and handling run-length compression.
1677 Returns 0 on any error, 1 on success. */
1692 c = readchar (remote_timeout);
1696 case SERIAL_TIMEOUT:
1698 puts_filtered ("Timeout in mid-packet, retrying\n");
1702 puts_filtered ("Saw new packet start in middle of old one\n");
1703 return 0; /* Start a new packet, count retries */
1706 unsigned char pktcsum;
1710 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1711 pktcsum |= fromhex (readchar (remote_timeout));
1713 if (csum == pktcsum)
1718 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1720 puts_filtered (buf);
1721 puts_filtered ("\n");
1725 case '*': /* Run length encoding */
1727 c = readchar (remote_timeout);
1729 c = c - ' ' + 3; /* Compute repeat count */
1732 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
1734 memset (bp, *(bp - 1), c);
1740 printf_filtered ("Repeat count %d too large for buffer: ", c);
1741 puts_filtered (buf);
1742 puts_filtered ("\n");
1746 if (bp < buf + PBUFSIZ - 1)
1754 puts_filtered ("Remote packet too long: ");
1755 puts_filtered (buf);
1756 puts_filtered ("\n");
1763 /* Read a packet from the remote machine, with error checking,
1764 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1765 If FOREVER, wait forever rather than timing out; this is used
1766 while the target is executing user code. */
1769 getpkt (buf, forever)
1778 strcpy (buf,"timeout");
1782 #ifdef MAINTENANCE_CMDS
1783 timeout = watchdog > 0 ? watchdog : -1;
1790 timeout = remote_timeout;
1794 for (tries = 1; tries <= MAX_TRIES; tries++)
1796 /* This can loop forever if the remote side sends us characters
1797 continuously, but if it pauses, we'll get a zero from readchar
1798 because of timeout. Then we'll count that as a retry. */
1800 /* Note that we will only wait forever prior to the start of a packet.
1801 After that, we expect characters to arrive at a brisk pace. They
1802 should show up within remote_timeout intervals. */
1806 c = readchar (timeout);
1808 if (c == SERIAL_TIMEOUT)
1810 #ifdef MAINTENANCE_CMDS
1811 if (forever) /* Watchdog went off. Kill the target. */
1813 target_mourn_inferior ();
1814 error ("Watchdog has expired. Target detached.\n");
1818 puts_filtered ("Timed out.\n");
1824 /* We've found the start of a packet, now collect the data. */
1826 val = read_frame (buf);
1831 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
1832 SERIAL_WRITE (remote_desc, "+", 1);
1836 /* Try the whole thing again. */
1838 SERIAL_WRITE (remote_desc, "-", 1);
1841 /* We have tried hard enough, and just can't receive the packet. Give up. */
1843 printf_unfiltered ("Ignoring packet error, continuing...\n");
1844 SERIAL_WRITE (remote_desc, "+", 1);
1850 /* For some mysterious reason, wait_for_inferior calls kill instead of
1851 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1855 target_mourn_inferior ();
1859 /* Use catch_errors so the user can quit from gdb even when we aren't on
1860 speaking terms with the remote system. */
1861 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
1863 /* Don't wait for it to die. I'm not really sure it matters whether
1864 we do or not. For the existing stubs, kill is a noop. */
1865 target_mourn_inferior ();
1871 remote_mourn_1 (&remote_ops);
1875 extended_remote_mourn ()
1877 /* We do _not_ want to mourn the target like this; this will
1878 remove the extended remote target from the target stack,
1879 and the next time the user says "run" it'll fail.
1881 FIXME: What is the right thing to do here? */
1883 remote_mourn_1 (&extended_remote_ops);
1887 /* Worker function for remote_mourn. */
1889 remote_mourn_1 (target)
1890 struct target_ops *target;
1892 unpush_target (target);
1893 generic_mourn_inferior ();
1896 /* In the extended protocol we want to be able to do things like
1897 "run" and have them basically work as expected. So we need
1898 a special create_inferior function.
1900 FIXME: One day add support for changing the exec file
1901 we're debugging, arguments and an environment. */
1904 extended_remote_create_inferior (exec_file, args, env)
1909 /* Rip out the breakpoints; we'll reinsert them after restarting
1910 the remote server. */
1911 remove_breakpoints ();
1913 /* Now restart the remote server. */
1914 extended_remote_restart ();
1916 /* Now put the breakpoints back in. This way we're safe if the
1917 restart function works via a unix fork on the remote side. */
1918 insert_breakpoints ();
1920 /* Clean up from the last time we were running. */
1921 clear_proceed_status ();
1923 /* Let the remote process run. */
1924 proceed (-1, TARGET_SIGNAL_0, 0);
1928 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1929 than other targets; in those use REMOTE_BREAKPOINT instead of just
1930 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1931 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1932 the standard routines that are in mem-break.c. */
1934 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1935 the choice of breakpoint instruction affects target program design and
1936 vice versa, and by making it user-tweakable, the special code here
1937 goes away and we need fewer special GDB configurations. */
1939 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1940 #define REMOTE_BREAKPOINT
1943 #ifdef REMOTE_BREAKPOINT
1945 /* If the target isn't bi-endian, just pretend it is. */
1946 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1947 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1948 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1951 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
1952 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
1954 #endif /* REMOTE_BREAKPOINT */
1956 /* Insert a breakpoint on targets that don't have any better breakpoint
1957 support. We read the contents of the target location and stash it,
1958 then overwrite it with a breakpoint instruction. ADDR is the target
1959 location in the target machine. CONTENTS_CACHE is a pointer to
1960 memory allocated for saving the target contents. It is guaranteed
1961 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1962 is accomplished via BREAKPOINT_MAX). */
1965 remote_insert_breakpoint (addr, contents_cache)
1967 char *contents_cache;
1969 #ifdef REMOTE_BREAKPOINT
1972 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
1976 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1977 val = target_write_memory (addr, (char *) big_break_insn,
1978 sizeof big_break_insn);
1980 val = target_write_memory (addr, (char *) little_break_insn,
1981 sizeof little_break_insn);
1986 return memory_insert_breakpoint (addr, contents_cache);
1987 #endif /* REMOTE_BREAKPOINT */
1991 remote_remove_breakpoint (addr, contents_cache)
1993 char *contents_cache;
1995 #ifdef REMOTE_BREAKPOINT
1996 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
1998 return memory_remove_breakpoint (addr, contents_cache);
1999 #endif /* REMOTE_BREAKPOINT */
2002 /* Some targets are only capable of doing downloads, and afterwards they switch
2003 to the remote serial protocol. This function provides a clean way to get
2004 from the download target to the remote target. It's basically just a
2005 wrapper so that we don't have to expose any of the internal workings of
2008 Prior to calling this routine, you should shutdown the current target code,
2009 else you will get the "A program is being debugged already..." message.
2010 Usually a call to pop_target() suffices.
2014 push_remote_target (name, from_tty)
2018 printf_filtered ("Switching to remote protocol\n");
2019 remote_open (name, from_tty);
2022 /* Other targets want to use the entire remote serial module but with
2023 certain remote_ops overridden. */
2026 open_remote_target (name, from_tty, target, extended_p)
2029 struct target_ops *target;
2032 printf_filtered ("Selecting the %sremote protocol\n",
2033 (extended_p ? "extended-" : ""));
2034 remote_open_1 (name, from_tty, target, extended_p);
2039 /* Table used by the crc32 function to calcuate the checksum. */
2040 static unsigned long crc32_table[256] = {0, 0};
2042 static unsigned long
2043 crc32 (buf, len, crc)
2048 if (! crc32_table[1])
2050 /* Initialize the CRC table and the decoding table. */
2054 for (i = 0; i < 256; i++)
2056 for (c = i << 24, j = 8; j > 0; --j)
2057 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
2064 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
2071 compare_sections_command (args, from_tty)
2076 unsigned long host_crc, target_crc;
2077 extern bfd *exec_bfd;
2078 struct cleanup *old_chain;
2079 char *tmp, *sectdata, *sectname, buf[PBUFSIZ];
2086 error ("command cannot be used without an exec file");
2087 if (!current_target.to_shortname ||
2088 strcmp (current_target.to_shortname, "remote") != 0)
2089 error ("command can only be used with remote target");
2091 for (s = exec_bfd->sections; s; s = s->next)
2093 if (!(s->flags & SEC_LOAD))
2094 continue; /* skip non-loadable section */
2096 size = bfd_get_section_size_before_reloc (s);
2098 continue; /* skip zero-length section */
2100 sectname = (char *) bfd_get_section_name (exec_bfd, s);
2101 if (args && strcmp (args, sectname) != 0)
2102 continue; /* not the section selected by user */
2104 matched = 1; /* do this section */
2106 /* FIXME: assumes lma can fit into long */
2107 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
2110 /* be clever; compute the host_crc before waiting for target reply */
2111 sectdata = xmalloc (size);
2112 old_chain = make_cleanup (free, sectdata);
2113 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
2114 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
2118 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
2119 sectname, lma, lma + size);
2121 error ("remote target does not support this operation");
2123 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
2124 target_crc = target_crc * 16 + fromhex (*tmp);
2126 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
2127 sectname, lma, lma + size);
2128 if (host_crc == target_crc)
2129 printf_filtered ("matched.\n");
2132 printf_filtered ("MIS-MATCHED!\n");
2136 do_cleanups (old_chain);
2139 warning ("One or more sections of the remote executable does not match\nthe loaded file\n");
2140 if (args && !matched)
2141 printf_filtered ("No loaded section named '%s'.\n", args);
2146 packet_command (args, from_tty)
2154 if (!current_target.to_shortname ||
2155 strcmp (current_target.to_shortname, "remote") != 0)
2156 error ("command can only be used with remote target");
2159 error ("remote-packet command requires packet text as argument");
2161 puts_filtered ("sending: ");
2162 print_packet (args);
2163 puts_filtered ("\n");
2167 puts_filtered ("received: ");
2169 puts_filtered ("\n");
2173 _initialize_remote ()
2176 init_extended_remote_ops() ;
2177 add_target (&remote_ops);
2178 add_target (&extended_remote_ops);
2180 add_cmd ("compare-sections", class_obscure, compare_sections_command,
2181 "Compare section data on target to the exec file.\n\
2182 Argument is a single section name (default: all loaded sections).",
2185 add_cmd ("packet", class_maintenance, packet_command,
2186 "Send an arbitrary packet to a remote target.\n\
2187 maintenance packet TEXT\n\
2188 If GDB is talking to an inferior via the GDB serial protocol, then\n\
2189 this command sends the string TEXT to the inferior, and displays the\n\
2190 response packet. GDB supplies the initial `$' character, and the\n\
2191 terminating `#' character and checksum. This command was originally\n\
2192 provided for use by the gdb.emc test suite.",
2195 add_show_from_set (add_set_cmd ("remotetimeout", no_class,
2196 var_integer, (char *)&remote_timeout,
2197 "Set timeout value for remote read.\n", &setlist),
2200 add_show_from_set (add_set_cmd ("remotebreak", no_class,
2201 var_integer, (char *)&remote_break,
2202 "Set whether to send break if interrupted.\n", &setlist),
2205 add_show_from_set (add_set_cmd ("remotewritesize", no_class,
2206 var_integer, (char *)&remote_write_size,
2207 "Set the maximum number of bytes in each memory write packet.\n", &setlist),