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
*));
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 getpkt
PARAMS ((char *buf
, int forever
));
248 static int putpkt
PARAMS ((char *buf
));
250 static void remote_send
PARAMS ((char *buf
));
252 static int readchar
PARAMS ((int timeout
));
254 static int remote_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
256 static void remote_kill
PARAMS ((void));
258 static int tohex
PARAMS ((int nib
));
260 static int fromhex
PARAMS ((int a
));
262 static void remote_detach
PARAMS ((char *args
, int from_tty
));
264 static void remote_interrupt
PARAMS ((int signo
));
266 static void remote_interrupt_twice
PARAMS ((int signo
));
268 static void interrupt_query
PARAMS ((void));
270 static void set_thread
PARAMS ((int, int));
272 static int remote_thread_alive
PARAMS ((int));
274 static void get_offsets
PARAMS ((void));
276 static int read_frame
PARAMS ((char *));
278 static int remote_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
280 static int remote_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
282 static struct target_ops remote_ops
; /* Forward decl */
283 static struct target_ops extended_remote_ops
; /* Forward decl */
285 /* This was 5 seconds, which is a long time to sit and wait.
286 Unless this is going though some terminal server or multiplexer or
287 other form of hairy serial connection, I would think 2 seconds would
290 /* Changed to allow option to set timeout value.
291 was static int remote_timeout = 2; */
292 extern int remote_timeout
;
294 /* This variable chooses whether to send a ^C or a break when the user
295 requests program interruption. Although ^C is usually what remote
296 systems expect, and that is the default here, sometimes a break is
297 preferable instead. */
299 static int remote_break
;
301 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
302 remote_open knows that we don't have a file open when the program
304 static serial_t remote_desc
= NULL
;
306 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
307 and i386-stub.c. Normally, no one would notice because it only matters
308 for writing large chunks of memory (e.g. in downloads). Also, this needs
309 to be more than 400 if required to hold the registers (see below, where
310 we round it up based on REGISTER_BYTES). */
313 /* Maximum number of bytes to read/write at once. The value here
314 is chosen to fill up a packet (the headers account for the 32). */
315 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
317 /* Round up PBUFSIZ to hold all the registers, at least. */
318 /* The blank line after the #if seems to be required to work around a
319 bug in HP's PA compiler. */
320 #if REGISTER_BYTES > MAXBUFBYTES
323 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
326 /* This variable sets the number of bytes to be written to the target
327 in a single packet. Normally PBUFSIZ is satisfactory, but some
328 targets need smaller values (perhaps because the receiving end
331 static int remote_write_size
= PBUFSIZ
;
333 /* This is the size (in chars) of the first response to the `g' command. This
334 is used to limit the size of the memory read and write commands to prevent
335 stub buffers from overflowing. */
337 static int remote_register_buf_size
= 0;
339 /* Should we try the 'P' request? If this is set to one when the stub
340 doesn't support 'P', the only consequence is some unnecessary traffic. */
341 static int stub_supports_P
= 1;
343 /* These are pointers to hook functions that may be set in order to
344 modify resume/wait behavior for a particular architecture. */
346 void (*target_resume_hook
) PARAMS ((void));
347 void (*target_wait_loop_hook
) PARAMS ((void));
350 /* These are the threads which we last sent to the remote system. -1 for all
351 or -2 for not sent yet. */
361 int state
= gen
? general_thread
: cont_thread
;
365 buf
[1] = gen
? 'g' : 'c';
372 sprintf (&buf
[2], "-%x", -th
);
374 sprintf (&buf
[2], "%x", th
);
383 /* Return nonzero if the thread TH is still alive on the remote system. */
386 remote_thread_alive (th
)
393 sprintf (&buf
[1], "-%x", -th
);
395 sprintf (&buf
[1], "%x", th
);
398 return (buf
[0] == 'O' && buf
[1] == 'K');
401 /* Restart the remote side; this is an extended protocol operation. */
404 extended_remote_restart ()
408 /* Send the restart command; for reasons I don't understand the
409 remote side really expects a number after the "R". */
411 sprintf (&buf
[1], "%x", 0);
414 /* Now query for status so this looks just like we restarted
415 gdbserver from scratch. */
420 /* Clean up connection to a remote debugger. */
424 remote_close (quitting
)
428 SERIAL_CLOSE (remote_desc
);
432 /* Query the remote side for the text, data and bss offsets. */
439 CORE_ADDR text_addr
, data_addr
, bss_addr
;
440 struct section_offsets
*offs
;
446 if (buf
[0] == '\000')
447 return; /* Return silently. Stub doesn't support this
451 warning ("Remote failure reply: %s", buf
);
455 nvals
= sscanf (buf
, "Text=%lx;Data=%lx;Bss=%lx", &text_addr
, &data_addr
,
458 error ("Malformed response to offset query, %s", buf
);
460 if (symfile_objfile
== NULL
)
463 offs
= (struct section_offsets
*) alloca (sizeof (struct section_offsets
)
464 + symfile_objfile
->num_sections
465 * sizeof (offs
->offsets
));
466 memcpy (offs
, symfile_objfile
->section_offsets
,
467 sizeof (struct section_offsets
)
468 + symfile_objfile
->num_sections
469 * sizeof (offs
->offsets
));
471 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
473 /* This is a temporary kludge to force data and bss to use the same offsets
474 because that's what nlmconv does now. The real solution requires changes
475 to the stub and remote.c that I don't have time to do right now. */
477 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
478 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
480 objfile_relocate (symfile_objfile
, offs
);
483 /* Stub for catch_errors. */
486 remote_start_remote (dummy
)
489 immediate_quit
= 1; /* Allow user to interrupt it */
491 /* Ack any packet which the remote side has already sent. */
492 SERIAL_WRITE (remote_desc
, "+", 1);
494 /* Let the stub know that we want it to return the thread. */
497 get_offsets (); /* Get text, data & bss offsets */
499 putpkt ("?"); /* initiate a query from remote machine */
502 start_remote (); /* Initialize gdb process mechanisms */
506 /* Open a connection to a remote debugger.
507 NAME is the filename used for communication. */
510 remote_open (name
, from_tty
)
514 remote_open_1 (name
, from_tty
, &remote_ops
);
517 /* Open a connection to a remote debugger using the extended
518 remote gdb protocol. NAME is the filename used for communication. */
521 extended_remote_open (name
, from_tty
)
527 /* Do the basic remote open stuff. */
528 remote_open_1 (name
, from_tty
, &extended_remote_ops
);
530 /* Now tell the remote that we're using the extended protocol. */
536 /* Generic code for opening a connection to a remote target. */
537 static DCACHE
*remote_dcache
;
540 remote_open_1 (name
, from_tty
, target
)
543 struct target_ops
*target
;
546 error ("To open a remote debug connection, you need to specify what serial\n\
547 device is attached to the remote system (e.g. /dev/ttya).");
549 target_preopen (from_tty
);
551 unpush_target (target
);
553 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
555 remote_desc
= SERIAL_OPEN (name
);
557 perror_with_name (name
);
561 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
563 SERIAL_CLOSE (remote_desc
);
564 perror_with_name (name
);
569 SERIAL_RAW (remote_desc
);
571 /* If there is something sitting in the buffer we might take it as a
572 response to a command, which would be bad. */
573 SERIAL_FLUSH_INPUT (remote_desc
);
577 puts_filtered ("Remote debugging using ");
578 puts_filtered (name
);
579 puts_filtered ("\n");
581 push_target (target
); /* Switch to using remote target now */
583 /* Start out by trying the 'P' request to set registers. We set this each
584 time that we open a new target so that if the user switches from one
585 stub to another, we can (if the target is closed and reopened) cope. */
591 /* Without this, some commands which require an active target (such as kill)
592 won't work. This variable serves (at least) double duty as both the pid
593 of the target process (if it has such), and as a flag indicating that a
594 target is active. These functions should be split out into seperate
595 variables, especially since GDB will someday have a notion of debugging
596 several processes. */
598 inferior_pid
= 42000;
599 /* Start the remote connection; if error (0), discard this target.
600 In particular, if the user quits, be sure to discard it
601 (we'd be in an inconsistent state otherwise). */
602 if (!catch_errors (remote_start_remote
, (char *)0,
603 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL
))
607 /* This takes a program previously attached to and detaches it. After
608 this is done, GDB can be used to debug some other program. We
609 better not have left any breakpoints in the target program or it'll
610 die when it hits one. */
613 remote_detach (args
, from_tty
)
620 error ("Argument given to \"detach\" when remotely debugging.");
622 /* Tell the remote target to detach. */
628 puts_filtered ("Ending remote debugging.\n");
631 /* Convert hex digit A to a number. */
637 if (a
>= '0' && a
<= '9')
639 else if (a
>= 'a' && a
<= 'f')
642 error ("Reply contains invalid hex digit %d", a
);
645 /* Convert number NIB to a hex digit. */
657 /* Tell the remote machine to resume. */
659 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
663 remote_resume (pid
, step
, siggnal
)
665 enum target_signal siggnal
;
670 set_thread (inferior_pid
, 0);
674 dcache_flush (remote_dcache
);
676 last_sent_signal
= siggnal
;
677 last_sent_step
= step
;
679 /* A hook for when we need to do something at the last moment before
681 if (target_resume_hook
)
682 (*target_resume_hook
) ();
684 if (siggnal
!= TARGET_SIGNAL_0
)
686 buf
[0] = step
? 'S' : 'C';
687 buf
[1] = tohex (((int)siggnal
>> 4) & 0xf);
688 buf
[2] = tohex ((int)siggnal
& 0xf);
692 strcpy (buf
, step
? "s": "c");
697 /* Send ^C to target to halt it. Target will respond, and send us a
701 remote_interrupt (signo
)
704 /* If this doesn't work, try more severe steps. */
705 signal (signo
, remote_interrupt_twice
);
708 printf_unfiltered ("remote_interrupt called\n");
710 /* Send a break or a ^C, depending on user preference. */
712 SERIAL_SEND_BREAK (remote_desc
);
714 SERIAL_WRITE (remote_desc
, "\003", 1);
717 static void (*ofunc
)();
719 /* The user typed ^C twice. */
721 remote_interrupt_twice (signo
)
724 signal (signo
, ofunc
);
728 signal (signo
, remote_interrupt
);
731 /* Ask the user what to do when an interrupt is received. */
736 target_terminal_ours ();
738 if (query ("Interrupted while waiting for the program.\n\
739 Give up (and stop debugging it)? "))
741 target_mourn_inferior ();
742 return_to_top_level (RETURN_QUIT
);
745 target_terminal_inferior ();
748 /* If nonzero, ignore the next kill. */
751 /* Wait until the remote machine stops, then return,
752 storing status in STATUS just as `wait' would.
753 Returns "pid" (though it's not clear what, if anything, that
754 means in the case of this target). */
757 remote_wait (pid
, status
)
759 struct target_waitstatus
*status
;
761 unsigned char buf
[PBUFSIZ
];
764 status
->kind
= TARGET_WAITKIND_EXITED
;
765 status
->value
.integer
= 0;
771 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
772 getpkt ((char *) buf
, 1);
773 signal (SIGINT
, ofunc
);
775 /* This is a hook for when we need to do something (perhaps the
776 collection of trace data) every time the target stops. */
777 if (target_wait_loop_hook
)
778 (*target_wait_loop_hook
) ();
782 case 'E': /* Error of some sort */
783 warning ("Remote failure reply: %s", buf
);
785 case 'T': /* Status with PC, SP, FP, ... */
789 char regs
[MAX_REGISTER_RAW_SIZE
];
791 /* Expedited reply, containing Signal, {regno, reg} repeat */
792 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
794 n... = register number
795 r... = register contents
798 p
= &buf
[3]; /* after Txx */
805 regno
= strtol ((const char *) p
, &p_temp
, 16); /* Read the register number */
806 p1
= (unsigned char *)p_temp
;
810 p1
= (unsigned char *) strchr ((const char *) p
, ':');
812 warning ("Malformed packet (missing colon): %s\n\
815 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
817 thread_num
= strtol ((const char *) ++p1
, &p_temp
, 16);
818 p
= (unsigned char *)p_temp
;
826 warning ("Malformed packet (missing colon): %s\n\
830 if (regno
>= NUM_REGS
)
831 warning ("Remote sent bad register number %ld: %s\n\
835 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
837 if (p
[0] == 0 || p
[1] == 0)
838 warning ("Remote reply is too short: %s", buf
);
839 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
842 supply_register (regno
, regs
);
846 warning ("Remote register badly formatted: %s", buf
);
850 case 'S': /* Old style status, just signal only */
851 status
->kind
= TARGET_WAITKIND_STOPPED
;
852 status
->value
.sig
= (enum target_signal
)
853 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
856 case 'W': /* Target exited */
858 /* The remote process exited. */
859 status
->kind
= TARGET_WAITKIND_EXITED
;
860 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
864 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
865 status
->value
.sig
= (enum target_signal
)
866 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
870 case 'O': /* Console output */
871 for (p
= buf
+ 1; *p
; p
+=2)
874 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
877 if (target_output_hook
)
878 target_output_hook (tb
);
880 fputs_filtered (tb
, gdb_stdout
);
884 if (last_sent_signal
!= TARGET_SIGNAL_0
)
886 /* Zero length reply means that we tried 'S' or 'C' and
887 the remote system doesn't support it. */
888 target_terminal_ours_for_output ();
890 ("Can't send signals to this remote system. %s not sent.\n",
891 target_signal_to_name (last_sent_signal
));
892 last_sent_signal
= TARGET_SIGNAL_0
;
893 target_terminal_inferior ();
895 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
896 putpkt ((char *) buf
);
899 /* else fallthrough */
901 warning ("Invalid remote reply: %s", buf
);
906 if (thread_num
!= -1)
908 /* Initial thread value can only be acquired via wait, so deal with
909 this marker which is used before the first thread value is
911 if (inferior_pid
== 42000)
913 inferior_pid
= thread_num
;
914 add_thread (inferior_pid
);
921 /* Number of bytes of registers this stub implements. */
922 static int register_bytes_found
;
924 /* Read the remote registers into the block REGS. */
925 /* Currently we just read all the registers, so we don't use regno. */
928 remote_fetch_registers (regno
)
934 char regs
[REGISTER_BYTES
];
936 set_thread (inferior_pid
, 1);
941 if (remote_register_buf_size
== 0)
942 remote_register_buf_size
= strlen (buf
);
944 /* Unimplemented registers read as all bits zero. */
945 memset (regs
, 0, REGISTER_BYTES
);
947 /* We can get out of synch in various cases. If the first character
948 in the buffer is not a hex character, assume that has happened
949 and try to fetch another packet to read. */
950 while ((buf
[0] < '0' || buf
[0] > '9')
951 && (buf
[0] < 'a' || buf
[0] > 'f'))
954 printf_unfiltered ("Bad register packet; fetching a new packet\n");
958 /* Reply describes registers byte by byte, each byte encoded as two
959 hex characters. Suck them all up, then supply them to the
960 register cacheing/storage mechanism. */
963 for (i
= 0; i
< REGISTER_BYTES
; i
++)
969 warning ("Remote reply is of odd length: %s", buf
);
970 /* Don't change register_bytes_found in this case, and don't
971 print a second warning. */
974 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
978 if (i
!= register_bytes_found
)
980 register_bytes_found
= i
;
981 #ifdef REGISTER_BYTES_OK
982 if (!REGISTER_BYTES_OK (i
))
983 warning ("Remote reply is too short: %s", buf
);
988 for (i
= 0; i
< NUM_REGS
; i
++)
989 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
992 /* Prepare to store registers. Since we may send them all (using a
993 'G' request), we have to read out the ones we don't want to change
997 remote_prepare_to_store ()
999 /* Make sure the entire registers array is valid. */
1000 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
1003 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1004 of REGISTERS. FIXME: ignores errors. */
1007 remote_store_registers (regno
)
1014 set_thread (inferior_pid
, 1);
1016 if (regno
>= 0 && stub_supports_P
)
1018 /* Try storing a single register. */
1021 sprintf (buf
, "P%x=", regno
);
1022 p
= buf
+ strlen (buf
);
1023 regp
= ®isters
[REGISTER_BYTE (regno
)];
1024 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
1026 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
1027 *p
++ = tohex (regp
[i
] & 0xf);
1033 /* The stub understands the 'P' request. We are done. */
1037 /* The stub does not support the 'P' request. Use 'G' instead,
1038 and don't try using 'P' in the future (it will just waste our
1040 stub_supports_P
= 0;
1045 /* Command describes registers byte by byte,
1046 each byte encoded as two hex characters. */
1049 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1050 for (i
= 0; i
< register_bytes_found
; i
++)
1052 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
1053 *p
++ = tohex (registers
[i
] & 0xf);
1061 Use of the data cache *used* to be disabled because it loses for looking at
1062 and changing hardware I/O ports and the like. Accepting `volatile'
1063 would perhaps be one way to fix it. Another idea would be to use the
1064 executable file for the text segment (for all SEC_CODE sections?
1065 For all SEC_READONLY sections?). This has problems if you want to
1066 actually see what the memory contains (e.g. self-modifying code,
1067 clobbered memory, user downloaded the wrong thing).
1069 Because it speeds so much up, it's now enabled, if you're playing
1070 with registers you turn it of (set remotecache 0)
1073 /* Read a word from remote address ADDR and return it.
1074 This goes through the data cache. */
1078 remote_fetch_word (addr
)
1081 return dcache_fetch (remote_dcache
, addr
);
1084 /* Write a word WORD into remote address ADDR.
1085 This goes through the data cache. */
1088 remote_store_word (addr
, word
)
1092 dcache_poke (remote_dcache
, addr
, word
);
1094 #endif /* 0 (unused?) */
1098 /* Return the number of hex digits in num. */
1106 for (i
= 0; num
!= 0; i
++)
1112 /* Write memory data directly to the remote machine.
1113 This does not inform the data cache; the data cache uses this.
1114 MEMADDR is the address in the remote memory space.
1115 MYADDR is the address of the buffer in our space.
1116 LEN is the number of bytes.
1118 Returns number of bytes transferred, or 0 for error. */
1121 remote_write_bytes (memaddr
, myaddr
, len
)
1126 int max_buf_size
; /* Max size of packet output buffer */
1129 /* Chop the transfer down if necessary */
1131 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
1132 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
1134 #define PACKET_OVERHEAD (1 + 1 + 1 + 2) /* $x#xx - Overhead for all types of packets */
1136 /* packet overhead + <memaddr>,<len>: */
1137 max_buf_size
-= PACKET_OVERHEAD
+ hexnumlen (memaddr
+ len
- 1) + 1 + hexnumlen (len
) + 1;
1147 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
1149 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1150 result in a buffer like sprintf. */
1151 sprintf (buf
, "M%lx,%x:", (unsigned long) memaddr
, todo
);
1153 /* We send target system values byte by byte, in increasing byte addresses,
1154 each byte encoded as two hex characters. */
1156 p
= buf
+ strlen (buf
);
1157 for (i
= 0; i
< todo
; i
++)
1159 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
1160 *p
++ = tohex (myaddr
[i
] & 0xf);
1169 /* There is no correspondance between what the remote protocol uses
1170 for errors and errno codes. We would like a cleaner way of
1171 representing errors (big enough to include errno codes, bfd_error
1172 codes, and others). But for now just return EIO. */
1183 /* Read memory data directly from the remote machine.
1184 This does not use the data cache; the data cache uses this.
1185 MEMADDR is the address in the remote memory space.
1186 MYADDR is the address of the buffer in our space.
1187 LEN is the number of bytes.
1189 Returns number of bytes transferred, or 0 for error. */
1192 remote_read_bytes (memaddr
, myaddr
, len
)
1197 int max_buf_size
; /* Max size of packet output buffer */
1200 /* Chop the transfer down if necessary */
1202 max_buf_size
= min (remote_write_size
, PBUFSIZ
);
1203 max_buf_size
= min (max_buf_size
, remote_register_buf_size
);
1205 /* packet overhead */
1206 max_buf_size
-= PACKET_OVERHEAD
;
1216 todo
= min (len
, max_buf_size
/ 2); /* num bytes that will fit */
1218 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1219 result in a buffer like sprintf. */
1220 sprintf (buf
, "m%lx,%x", (unsigned long) memaddr
, todo
);
1226 /* There is no correspondance between what the remote protocol uses
1227 for errors and errno codes. We would like a cleaner way of
1228 representing errors (big enough to include errno codes, bfd_error
1229 codes, and others). But for now just return EIO. */
1234 /* Reply describes memory byte by byte,
1235 each byte encoded as two hex characters. */
1238 for (i
= 0; i
< todo
; i
++)
1240 if (p
[0] == 0 || p
[1] == 0)
1241 /* Reply is short. This means that we were able to read only part
1242 of what we wanted to. */
1243 return i
+ (origlen
- len
);
1244 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1254 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1255 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1256 nonzero. Returns length of data written or read; 0 for error. */
1260 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
1265 struct target_ops
*target
; /* ignored */
1267 return dcache_xfer_memory (remote_dcache
, memaddr
, myaddr
, len
, should_write
);
1272 /* Enable after 4.12. */
1275 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
1276 addr_found
, data_found
)
1280 CORE_ADDR startaddr
;
1284 CORE_ADDR
*addr_found
;
1287 if (increment
== -4 && len
== 4)
1289 long mask_long
, data_long
;
1290 long data_found_long
;
1291 CORE_ADDR addr_we_found
;
1293 long returned_long
[2];
1296 mask_long
= extract_unsigned_integer (mask
, len
);
1297 data_long
= extract_unsigned_integer (data
, len
);
1298 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
1303 /* The stub doesn't support the 't' request. We might want to
1304 remember this fact, but on the other hand the stub could be
1305 switched on us. Maybe we should remember it only until
1306 the next "target remote". */
1307 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1308 hirange
, addr_found
, data_found
);
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 use EIO. */
1317 memory_error (EIO
, startaddr
);
1320 while (*p
!= '\0' && *p
!= ',')
1321 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
1323 error ("Protocol error: short return for search");
1325 data_found_long
= 0;
1326 while (*p
!= '\0' && *p
!= ',')
1327 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
1328 /* Ignore anything after this comma, for future extensions. */
1330 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
1336 *addr_found
= addr_we_found
;
1337 *data_found
= store_unsigned_integer (data_we_found
, len
);
1340 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1341 hirange
, addr_found
, data_found
);
1346 remote_files_info (ignore
)
1347 struct target_ops
*ignore
;
1349 puts_filtered ("Debugging a target over a serial line.\n");
1352 /* Stuff for dealing with the packets which are part of this protocol.
1353 See comment at top of file for details. */
1355 /* Read a single character from the remote end, masking it down to 7 bits. */
1363 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
1368 error ("Remote connection closed");
1370 perror_with_name ("Remote communication error");
1371 case SERIAL_TIMEOUT
:
1378 /* Send the command in BUF to the remote machine,
1379 and read the reply into BUF.
1380 Report an error if we get an error reply. */
1390 error ("Remote failure reply: %s", buf
);
1393 /* Send a packet to the remote machine, with error checking.
1394 The data of the packet is in BUF. */
1401 unsigned char csum
= 0;
1403 int cnt
= strlen (buf
);
1408 /* Copy the packet into buffer BUF2, encapsulating it
1409 and giving it a checksum. */
1411 if (cnt
> (int) sizeof (buf2
) - 5) /* Prosanity check */
1417 for (i
= 0; i
< cnt
; i
++)
1423 *p
++ = tohex ((csum
>> 4) & 0xf);
1424 *p
++ = tohex (csum
& 0xf);
1426 /* Send it over and over until we get a positive ack. */
1430 int started_error_output
= 0;
1435 printf_unfiltered ("Sending packet: %s...", buf2
);
1436 gdb_flush(gdb_stdout
);
1438 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
1439 perror_with_name ("putpkt: write failed");
1441 /* read until either a timeout occurs (-2) or '+' is read */
1444 ch
= readchar (remote_timeout
);
1451 case SERIAL_TIMEOUT
:
1453 if (started_error_output
)
1455 putchar_unfiltered ('\n');
1456 started_error_output
= 0;
1465 printf_unfiltered("Ack\n");
1467 case SERIAL_TIMEOUT
:
1471 break; /* Retransmit buffer */
1474 char junkbuf
[PBUFSIZ
];
1476 /* It's probably an old response, and we're out of sync. Just
1477 gobble up the packet and ignore it. */
1478 getpkt (junkbuf
, 0);
1479 continue; /* Now, go look for + */
1484 if (!started_error_output
)
1486 started_error_output
= 1;
1487 printf_unfiltered ("putpkt: Junk: ");
1489 putchar_unfiltered (ch
& 0177);
1493 break; /* Here to retransmit */
1497 /* This is wrong. If doing a long backtrace, the user should be
1498 able to get out next time we call QUIT, without anything as violent
1499 as interrupt_query. If we want to provide a way out of here
1500 without getting to the next QUIT, it should be based on hitting
1501 ^C twice as in remote_wait. */
1511 /* Come here after finding the start of the frame. Collect the rest into BUF,
1512 verifying the checksum, length, and handling run-length compression.
1513 Returns 0 on any error, 1 on success. */
1528 c
= readchar (remote_timeout
);
1532 case SERIAL_TIMEOUT
:
1534 puts_filtered ("Timeout in mid-packet, retrying\n");
1538 puts_filtered ("Saw new packet start in middle of old one\n");
1539 return 0; /* Start a new packet, count retries */
1542 unsigned char pktcsum
;
1546 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
1547 pktcsum
|= fromhex (readchar (remote_timeout
));
1549 if (csum
== pktcsum
)
1554 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1556 puts_filtered (buf
);
1557 puts_filtered ("\n");
1561 case '*': /* Run length encoding */
1563 c
= readchar (remote_timeout
);
1565 c
= c
- ' ' + 3; /* Compute repeat count */
1568 if (c
> 0 && c
< 255 && bp
+ c
- 1 < buf
+ PBUFSIZ
- 1)
1570 memset (bp
, *(bp
- 1), c
);
1576 printf_filtered ("Repeat count %d too large for buffer: ", c
);
1577 puts_filtered (buf
);
1578 puts_filtered ("\n");
1582 if (bp
< buf
+ PBUFSIZ
- 1)
1590 puts_filtered ("Remote packet too long: ");
1591 puts_filtered (buf
);
1592 puts_filtered ("\n");
1599 /* Read a packet from the remote machine, with error checking,
1600 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1601 If FOREVER, wait forever rather than timing out; this is used
1602 while the target is executing user code. */
1605 getpkt (buf
, forever
)
1614 strcpy (buf
,"timeout");
1618 #ifdef MAINTENANCE_CMDS
1619 timeout
= watchdog
> 0 ? watchdog
: -1;
1626 timeout
= remote_timeout
;
1630 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
1632 /* This can loop forever if the remote side sends us characters
1633 continuously, but if it pauses, we'll get a zero from readchar
1634 because of timeout. Then we'll count that as a retry. */
1636 /* Note that we will only wait forever prior to the start of a packet.
1637 After that, we expect characters to arrive at a brisk pace. They
1638 should show up within remote_timeout intervals. */
1642 c
= readchar (timeout
);
1644 if (c
== SERIAL_TIMEOUT
)
1646 #ifdef MAINTENANCE_CMDS
1647 if (forever
) /* Watchdog went off. Kill the target. */
1649 target_mourn_inferior ();
1650 error ("Watchdog has expired. Target detached.\n");
1654 puts_filtered ("Timed out.\n");
1660 /* We've found the start of a packet, now collect the data. */
1662 val
= read_frame (buf
);
1667 fprintf_unfiltered (gdb_stderr
, "Packet received: %s\n", buf
);
1668 SERIAL_WRITE (remote_desc
, "+", 1);
1672 /* Try the whole thing again. */
1674 SERIAL_WRITE (remote_desc
, "-", 1);
1677 /* We have tried hard enough, and just can't receive the packet. Give up. */
1679 printf_unfiltered ("Ignoring packet error, continuing...\n");
1680 SERIAL_WRITE (remote_desc
, "+", 1);
1686 /* For some mysterious reason, wait_for_inferior calls kill instead of
1687 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1691 target_mourn_inferior ();
1695 /* Use catch_errors so the user can quit from gdb even when we aren't on
1696 speaking terms with the remote system. */
1697 catch_errors (putpkt
, "k", "", RETURN_MASK_ERROR
);
1699 /* Don't wait for it to die. I'm not really sure it matters whether
1700 we do or not. For the existing stubs, kill is a noop. */
1701 target_mourn_inferior ();
1707 remote_mourn_1 (&remote_ops
);
1711 extended_remote_mourn ()
1713 /* We do _not_ want to mourn the target like this; this will
1714 remove the extended remote target from the target stack,
1715 and the next time the user says "run" it'll fail.
1717 FIXME: What is the right thing to do here? */
1719 remote_mourn_1 (&extended_remote_ops
);
1723 /* Worker function for remote_mourn. */
1725 remote_mourn_1 (target
)
1726 struct target_ops
*target
;
1728 unpush_target (target
);
1729 generic_mourn_inferior ();
1732 /* In the extended protocol we want to be able to do things like
1733 "run" and have them basically work as expected. So we need
1734 a special create_inferior function.
1736 FIXME: One day add support for changing the exec file
1737 we're debugging, arguments and an environment. */
1740 extended_remote_create_inferior (exec_file
, args
, env
)
1745 /* Rip out the breakpoints; we'll reinsert them after restarting
1746 the remote server. */
1747 remove_breakpoints ();
1749 /* Now restart the remote server. */
1750 extended_remote_restart ();
1752 /* Now put the breakpoints back in. This way we're safe if the
1753 restart function works via a unix fork on the remote side. */
1754 insert_breakpoints ();
1756 /* Clean up from the last time we were running. */
1757 clear_proceed_status ();
1759 /* Let the remote process run. */
1760 proceed (-1, TARGET_SIGNAL_0
, 0);
1764 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1765 than other targets; in those use REMOTE_BREAKPOINT instead of just
1766 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1767 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1768 the standard routines that are in mem-break.c. */
1770 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1771 the choice of breakpoint instruction affects target program design and
1772 vice versa, and by making it user-tweakable, the special code here
1773 goes away and we need fewer special GDB configurations. */
1775 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1776 #define REMOTE_BREAKPOINT
1779 #ifdef REMOTE_BREAKPOINT
1781 /* If the target isn't bi-endian, just pretend it is. */
1782 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1783 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1784 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1787 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
1788 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
1790 #endif /* REMOTE_BREAKPOINT */
1792 /* Insert a breakpoint on targets that don't have any better breakpoint
1793 support. We read the contents of the target location and stash it,
1794 then overwrite it with a breakpoint instruction. ADDR is the target
1795 location in the target machine. CONTENTS_CACHE is a pointer to
1796 memory allocated for saving the target contents. It is guaranteed
1797 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1798 is accomplished via BREAKPOINT_MAX). */
1801 remote_insert_breakpoint (addr
, contents_cache
)
1803 char *contents_cache
;
1805 #ifdef REMOTE_BREAKPOINT
1808 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
1812 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1813 val
= target_write_memory (addr
, (char *) big_break_insn
,
1814 sizeof big_break_insn
);
1816 val
= target_write_memory (addr
, (char *) little_break_insn
,
1817 sizeof little_break_insn
);
1822 return memory_insert_breakpoint (addr
, contents_cache
);
1823 #endif /* REMOTE_BREAKPOINT */
1827 remote_remove_breakpoint (addr
, contents_cache
)
1829 char *contents_cache
;
1831 #ifdef REMOTE_BREAKPOINT
1832 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
1834 return memory_remove_breakpoint (addr
, contents_cache
);
1835 #endif /* REMOTE_BREAKPOINT */
1838 /* Define the target subroutine names */
1840 static struct target_ops remote_ops
=
1842 "remote", /* to_shortname */
1843 "Remote serial target in gdb-specific protocol", /* to_longname */
1844 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1845 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1846 remote_open
, /* to_open */
1847 remote_close
, /* to_close */
1848 NULL
, /* to_attach */
1849 remote_detach
, /* to_detach */
1850 remote_resume
, /* to_resume */
1851 remote_wait
, /* to_wait */
1852 remote_fetch_registers
, /* to_fetch_registers */
1853 remote_store_registers
, /* to_store_registers */
1854 remote_prepare_to_store
, /* to_prepare_to_store */
1855 remote_xfer_memory
, /* to_xfer_memory */
1856 remote_files_info
, /* to_files_info */
1857 remote_insert_breakpoint
, /* to_insert_breakpoint */
1858 remote_remove_breakpoint
, /* to_remove_breakpoint */
1859 NULL
, /* to_terminal_init */
1860 NULL
, /* to_terminal_inferior */
1861 NULL
, /* to_terminal_ours_for_output */
1862 NULL
, /* to_terminal_ours */
1863 NULL
, /* to_terminal_info */
1864 remote_kill
, /* to_kill */
1865 generic_load
, /* to_load */
1866 NULL
, /* to_lookup_symbol */
1867 NULL
, /* to_create_inferior */
1868 remote_mourn
, /* to_mourn_inferior */
1870 0, /* to_notice_signals */
1871 remote_thread_alive
, /* to_thread_alive */
1873 process_stratum
, /* to_stratum */
1875 1, /* to_has_all_memory */
1876 1, /* to_has_memory */
1877 1, /* to_has_stack */
1878 1, /* to_has_registers */
1879 1, /* to_has_execution */
1880 NULL
, /* sections */
1881 NULL
, /* sections_end */
1882 OPS_MAGIC
/* to_magic */
1885 static struct target_ops extended_remote_ops
=
1887 "extended-remote", /* to_shortname */
1888 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1889 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1890 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1891 extended_remote_open
, /* to_open */
1892 remote_close
, /* to_close */
1893 NULL
, /* to_attach */
1894 remote_detach
, /* to_detach */
1895 remote_resume
, /* to_resume */
1896 remote_wait
, /* to_wait */
1897 remote_fetch_registers
, /* to_fetch_registers */
1898 remote_store_registers
, /* to_store_registers */
1899 remote_prepare_to_store
, /* to_prepare_to_store */
1900 remote_xfer_memory
, /* to_xfer_memory */
1901 remote_files_info
, /* to_files_info */
1903 remote_insert_breakpoint
, /* to_insert_breakpoint */
1904 remote_remove_breakpoint
, /* to_remove_breakpoint */
1906 NULL
, /* to_terminal_init */
1907 NULL
, /* to_terminal_inferior */
1908 NULL
, /* to_terminal_ours_for_output */
1909 NULL
, /* to_terminal_ours */
1910 NULL
, /* to_terminal_info */
1911 remote_kill
, /* to_kill */
1912 generic_load
, /* to_load */
1913 NULL
, /* to_lookup_symbol */
1914 extended_remote_create_inferior
,/* to_create_inferior */
1915 extended_remote_mourn
, /* to_mourn_inferior */
1917 0, /* to_notice_signals */
1918 remote_thread_alive
, /* to_thread_alive */
1920 process_stratum
, /* to_stratum */
1922 1, /* to_has_all_memory */
1923 1, /* to_has_memory */
1924 1, /* to_has_stack */
1925 1, /* to_has_registers */
1926 1, /* to_has_execution */
1927 NULL
, /* sections */
1928 NULL
, /* sections_end */
1929 OPS_MAGIC
/* to_magic */
1932 /* Some targets are only capable of doing downloads, and afterwards they switch
1933 to the remote serial protocol. This function provides a clean way to get
1934 from the download target to the remote target. It's basically just a
1935 wrapper so that we don't have to expose any of the internal workings of
1938 Prior to calling this routine, you should shutdown the current target code,
1939 else you will get the "A program is being debugged already..." message.
1940 Usually a call to pop_target() suffices.
1944 push_remote_target (name
, from_tty
)
1948 printf_filtered ("Switching to remote protocol\n");
1949 remote_open (name
, from_tty
);
1953 _initialize_remote ()
1955 add_target (&remote_ops
);
1956 add_target (&extended_remote_ops
);
1958 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1959 var_integer
, (char *)&remote_timeout
,
1960 "Set timeout value for remote read.\n", &setlist
),
1963 add_show_from_set (add_set_cmd ("remotebreak", no_class
,
1964 var_integer
, (char *)&remote_break
,
1965 "Set whether to send break if interrupted.\n", &setlist
),
1968 add_show_from_set (add_set_cmd ("remotewritesize", no_class
,
1969 var_integer
, (char *)&remote_write_size
,
1970 "Set the maximum number of bytes in each memory write packet.\n", &setlist
),