1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993, 1994, 1995, 1996 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20 /* Remote communication protocol.
22 A debug packet whose contents are <data>
23 is encapsulated for transmission in the form:
25 $ <data> # CSUM1 CSUM2
27 <data> must be ASCII alphanumeric and cannot include characters
28 '$' or '#'. If <data> starts with two characters followed by
29 ':', then the existing stubs interpret this as a sequence number.
31 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
32 checksum of <data>, the most significant nibble is sent first.
33 the hex digits 0-9,a-f are used.
35 Receiver responds with:
37 + - if CSUM is correct and ready for next packet
38 - - if CSUM is incorrect
41 Most values are encoded in ascii hex digits. Signal numbers are according
42 to the numbering in target.h.
46 set thread Hct... Set thread for subsequent operations.
47 c = 'c' for thread used in step and
48 continue; t... can be -1 for all
50 c = 'g' for thread used in other
51 operations. If zero, pick a thread,
57 reply XX....X Each byte of register data
58 is described by two hex digits.
59 Registers are in the internal order
60 for GDB, and the bytes in a register
61 are in the same order the machine uses.
64 write regs GXX..XX Each byte of register data
65 is described by two hex digits.
69 write reg Pn...=r... Write register n... with value r...,
70 which contains two hex digits for each
71 byte in the register (target byte
75 (not supported by all stubs).
77 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
78 reply XX..XX XX..XX is mem contents
79 Can be fewer bytes than requested
80 if able to read only part of the data.
83 write mem MAA..AA,LLLL:XX..XX
85 LLLL is number of bytes,
88 ENN for an error (this includes the case
89 where only part of the data was
92 continue cAA..AA AA..AA is address to resume
94 resume at same address.
96 step sAA..AA AA..AA is address to resume
98 resume at same address.
100 continue with Csig;AA Continue with signal sig (hex signal
103 step with Ssig;AA Like 'C' but step not continue.
106 last signal ? Reply the current reason for stopping.
107 This is the same reply as is generated
108 for step or cont : SAA where AA is the
113 There is no immediate reply to step or cont.
114 The reply comes when the machine stops.
115 It is SAA AA is the signal number.
117 or... TAAn...:r...;n...:r...;n...:r...;
119 n... = register number (hex)
120 r... = register contents
122 r... = thread process ID. This is
124 n... = other string not starting
125 with valid hex digit.
126 gdb should ignore this n,r pair
127 and go on to the next. This way
128 we can extend the protocol.
129 or... WAA The process exited, and AA is
130 the exit status. This is only
131 applicable for certains sorts of
133 or... XAA The process terminated with signal
135 or... OXX..XX XX..XX is hex encoding of ASCII data. This
136 can happen at any time while the program is
137 running and the debugger should
138 continue to wait for 'W', 'T', etc.
140 thread alive TXX Find out if the thread XX is alive.
141 reply OK thread is still alive
144 remote restart RXX Restart the remote server
146 extended ops ! Use the extended remote protocol.
147 Sticky -- only needs to be set once.
151 toggle debug d toggle debug flag (see 386 & 68k stubs)
152 reset r reset -- see sparc stub.
153 reserved <other> On other requests, the stub should
154 ignore the request and send an empty
155 response ($#<checksum>). This way
156 we can extend the protocol and GDB
157 can tell whether the stub it is
158 talking to uses the old or the new.
159 search tAA:PP,MM Search backwards starting at address
160 AA for a match with pattern PP and
161 mask MM. PP and MM are 4 bytes.
162 Not supported by all stubs.
164 general query qXXXX Request info about XXXX.
165 general set QXXXX=yyyy Set value of XXXX to yyyy.
166 query sect offs qOffsets Get section offsets. Reply is
167 Text=xxx;Data=yyy;Bss=zzz
169 Responses can be run-length encoded to save space. A '*' means that
170 the next character is an ASCII encoding giving a repeat count which
171 stands for that many repititions of the character preceding the '*'.
172 The encoding is n+29, yielding a printable character where n >=3
173 (which is where rle starts to win). Don't use an n > 126.
176 "0* " means the same as "0000". */
179 #include "gdb_string.h"
182 #include "inferior.h"
187 /*#include "terminal.h"*/
189 #include "objfiles.h"
190 #include "gdb-stabs.h"
191 #include "gdbthread.h"
196 #include <sys/types.h>
202 /* Prototypes for local functions */
204 static int remote_write_bytes
PARAMS ((CORE_ADDR memaddr
,
205 char *myaddr
, int len
));
207 static int remote_read_bytes
PARAMS ((CORE_ADDR memaddr
,
208 char *myaddr
, int len
));
210 static void remote_files_info
PARAMS ((struct target_ops
*ignore
));
212 static int remote_xfer_memory
PARAMS ((CORE_ADDR memaddr
, char *myaddr
,
213 int len
, int should_write
,
214 struct target_ops
*target
));
216 static void remote_prepare_to_store
PARAMS ((void));
218 static void remote_fetch_registers
PARAMS ((int regno
));
220 static void remote_resume
PARAMS ((int pid
, int step
,
221 enum target_signal siggnal
));
223 static int remote_start_remote
PARAMS ((char *dummy
));
225 static void remote_open
PARAMS ((char *name
, int from_tty
));
227 static void extended_remote_open
PARAMS ((char *name
, int from_tty
));
229 static void remote_open_1
PARAMS ((char *, int, struct target_ops
*));
231 static void remote_close
PARAMS ((int quitting
));
233 static void remote_store_registers
PARAMS ((int regno
));
235 static void remote_mourn
PARAMS ((void));
237 static void extended_remote_restart
PARAMS ((void));
239 static void extended_remote_mourn
PARAMS ((void));
241 static void extended_remote_create_inferior
PARAMS ((char *, char *, char **));
243 static void remote_mourn_1
PARAMS ((struct target_ops
*));
245 static void getpkt
PARAMS ((char *buf
, int forever
));
247 static int putpkt
PARAMS ((char *buf
));
249 static void remote_send
PARAMS ((char *buf
));
251 static int readchar
PARAMS ((int timeout
));
253 static int remote_wait
PARAMS ((int pid
, struct target_waitstatus
*status
));
255 static void remote_kill
PARAMS ((void));
257 static int tohex
PARAMS ((int nib
));
259 static int fromhex
PARAMS ((int a
));
261 static void remote_detach
PARAMS ((char *args
, int from_tty
));
263 static void remote_interrupt
PARAMS ((int signo
));
265 static void remote_interrupt_twice
PARAMS ((int signo
));
267 static void interrupt_query
PARAMS ((void));
269 static void set_thread
PARAMS ((int, int));
271 static int remote_thread_alive
PARAMS ((int));
273 static void get_offsets
PARAMS ((void));
275 static int read_frame
PARAMS ((char *));
277 static int remote_insert_breakpoint
PARAMS ((CORE_ADDR
, char *));
279 static int remote_remove_breakpoint
PARAMS ((CORE_ADDR
, char *));
281 extern struct target_ops remote_ops
; /* Forward decl */
282 extern struct target_ops extended_remote_ops
; /* Forward decl */
284 /* This was 5 seconds, which is a long time to sit and wait.
285 Unless this is going though some terminal server or multiplexer or
286 other form of hairy serial connection, I would think 2 seconds would
289 /* Changed to allow option to set timeout value.
290 was static int remote_timeout = 2; */
291 extern int remote_timeout
;
293 /* This variable chooses whether to send a ^C or a break when the user
294 requests program interruption. Although ^C is usually what remote
295 systems expect, and that is the default here, sometimes a break is
296 preferable instead. */
298 static int remote_break
;
300 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
301 remote_open knows that we don't have a file open when the program
303 serial_t remote_desc
= NULL
;
305 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
306 and i386-stub.c. Normally, no one would notice because it only matters
307 for writing large chunks of memory (e.g. in downloads). Also, this needs
308 to be more than 400 if required to hold the registers (see below, where
309 we round it up based on REGISTER_BYTES). */
312 /* Maximum number of bytes to read/write at once. The value here
313 is chosen to fill up a packet (the headers account for the 32). */
314 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
316 /* Round up PBUFSIZ to hold all the registers, at least. */
317 /* The blank line after the #if seems to be required to work around a
318 bug in HP's PA compiler. */
319 #if REGISTER_BYTES > MAXBUFBYTES
322 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
325 /* This variable sets the number of bytes to be written to the target
326 in a single packet. Normally PBUFSIZ is satisfactory, but some
327 targets need smaller values (perhaps because the receiving end
330 static int remote_write_size
= PBUFSIZ
;
332 /* Should we try the 'P' request? If this is set to one when the stub
333 doesn't support 'P', the only consequence is some unnecessary traffic. */
334 static int stub_supports_P
= 1;
337 /* These are the threads which we last sent to the remote system. -1 for all
338 or -2 for not sent yet. */
348 int state
= gen
? general_thread
: cont_thread
;
352 buf
[1] = gen
? 'g' : 'c';
359 sprintf (&buf
[2], "-%x", -th
);
361 sprintf (&buf
[2], "%x", th
);
370 /* Return nonzero if the thread TH is still alive on the remote system. */
373 remote_thread_alive (th
)
380 sprintf (&buf
[1], "-%x", -th
);
382 sprintf (&buf
[1], "%x", th
);
385 return (buf
[0] == 'O' && buf
[1] == 'K');
388 /* Restart the remote side; this is an extended protocol operation. */
391 extended_remote_restart ()
395 /* Send the restart command; for reasons I don't understand the
396 remote side really expects a number after the "R". */
398 sprintf (&buf
[1], "%x", 0);
401 /* Now query for status so this looks just like we restarted
402 gdbserver from scratch. */
407 /* Clean up connection to a remote debugger. */
411 remote_close (quitting
)
415 SERIAL_CLOSE (remote_desc
);
419 /* Query the remote side for the text, data and bss offsets. */
426 CORE_ADDR text_addr
, data_addr
, bss_addr
;
427 struct section_offsets
*offs
;
433 if (buf
[0] == '\000')
434 return; /* Return silently. Stub doesn't support this
438 warning ("Remote failure reply: %s", buf
);
442 nvals
= sscanf (buf
, "Text=%lx;Data=%lx;Bss=%lx", &text_addr
, &data_addr
,
445 error ("Malformed response to offset query, %s", buf
);
447 if (symfile_objfile
== NULL
)
450 offs
= (struct section_offsets
*) alloca (sizeof (struct section_offsets
)
451 + symfile_objfile
->num_sections
452 * sizeof (offs
->offsets
));
453 memcpy (offs
, symfile_objfile
->section_offsets
,
454 sizeof (struct section_offsets
)
455 + symfile_objfile
->num_sections
456 * sizeof (offs
->offsets
));
458 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
460 /* This is a temporary kludge to force data and bss to use the same offsets
461 because that's what nlmconv does now. The real solution requires changes
462 to the stub and remote.c that I don't have time to do right now. */
464 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
465 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
467 objfile_relocate (symfile_objfile
, offs
);
470 /* Stub for catch_errors. */
473 remote_start_remote (dummy
)
476 immediate_quit
= 1; /* Allow user to interrupt it */
478 /* Ack any packet which the remote side has already sent. */
479 SERIAL_WRITE (remote_desc
, "+", 1);
481 /* Let the stub know that we want it to return the thread. */
484 get_offsets (); /* Get text, data & bss offsets */
486 putpkt ("?"); /* initiate a query from remote machine */
489 start_remote (); /* Initialize gdb process mechanisms */
493 /* Open a connection to a remote debugger.
494 NAME is the filename used for communication. */
497 remote_open (name
, from_tty
)
501 remote_open_1 (name
, from_tty
, &remote_ops
);
504 /* Open a connection to a remote debugger using the extended
505 remote gdb protocol. NAME is the filename used for communication. */
508 extended_remote_open (name
, from_tty
)
514 /* Do the basic remote open stuff. */
515 remote_open_1 (name
, from_tty
, &extended_remote_ops
);
517 /* Now tell the remote that we're using the extended protocol. */
523 /* Generic code for opening a connection to a remote target. */
524 static DCACHE
*remote_dcache
;
527 remote_open_1 (name
, from_tty
, target
)
530 struct target_ops
*target
;
533 error ("To open a remote debug connection, you need to specify what serial\n\
534 device is attached to the remote system (e.g. /dev/ttya).");
536 target_preopen (from_tty
);
538 unpush_target (target
);
540 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
542 remote_desc
= SERIAL_OPEN (name
);
544 perror_with_name (name
);
548 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
550 SERIAL_CLOSE (remote_desc
);
551 perror_with_name (name
);
556 SERIAL_RAW (remote_desc
);
558 /* If there is something sitting in the buffer we might take it as a
559 response to a command, which would be bad. */
560 SERIAL_FLUSH_INPUT (remote_desc
);
564 puts_filtered ("Remote debugging using ");
565 puts_filtered (name
);
566 puts_filtered ("\n");
568 push_target (target
); /* Switch to using remote target now */
570 /* Start out by trying the 'P' request to set registers. We set this each
571 time that we open a new target so that if the user switches from one
572 stub to another, we can (if the target is closed and reopened) cope. */
578 /* Without this, some commands which require an active target (such as kill)
579 won't work. This variable serves (at least) double duty as both the pid
580 of the target process (if it has such), and as a flag indicating that a
581 target is active. These functions should be split out into seperate
582 variables, especially since GDB will someday have a notion of debugging
583 several processes. */
585 inferior_pid
= 42000;
586 /* Start the remote connection; if error (0), discard this target.
587 In particular, if the user quits, be sure to discard it
588 (we'd be in an inconsistent state otherwise). */
589 if (!catch_errors (remote_start_remote
, (char *)0,
590 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL
))
594 /* This takes a program previously attached to and detaches it. After
595 this is done, GDB can be used to debug some other program. We
596 better not have left any breakpoints in the target program or it'll
597 die when it hits one. */
600 remote_detach (args
, from_tty
)
607 error ("Argument given to \"detach\" when remotely debugging.");
609 /* Tell the remote target to detach. */
615 puts_filtered ("Ending remote debugging.\n");
618 /* Convert hex digit A to a number. */
624 if (a
>= '0' && a
<= '9')
626 else if (a
>= 'a' && a
<= 'f')
629 error ("Reply contains invalid hex digit %d", a
);
632 /* Convert number NIB to a hex digit. */
644 /* Tell the remote machine to resume. */
646 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
650 remote_resume (pid
, step
, siggnal
)
652 enum target_signal siggnal
;
657 set_thread (inferior_pid
, 0);
661 dcache_flush (remote_dcache
);
663 last_sent_signal
= siggnal
;
664 last_sent_step
= step
;
666 if (siggnal
!= TARGET_SIGNAL_0
)
668 buf
[0] = step
? 'S' : 'C';
669 buf
[1] = tohex (((int)siggnal
>> 4) & 0xf);
670 buf
[2] = tohex ((int)siggnal
& 0xf);
674 strcpy (buf
, step
? "s": "c");
679 /* Send ^C to target to halt it. Target will respond, and send us a
683 remote_interrupt (signo
)
686 /* If this doesn't work, try more severe steps. */
687 signal (signo
, remote_interrupt_twice
);
690 printf_unfiltered ("remote_interrupt called\n");
692 /* Send a break or a ^C, depending on user preference. */
694 SERIAL_SEND_BREAK (remote_desc
);
696 SERIAL_WRITE (remote_desc
, "\003", 1);
699 static void (*ofunc
)();
701 /* The user typed ^C twice. */
703 remote_interrupt_twice (signo
)
706 signal (signo
, ofunc
);
710 signal (signo
, remote_interrupt
);
713 /* Ask the user what to do when an interrupt is received. */
718 target_terminal_ours ();
720 if (query ("Interrupted while waiting for the program.\n\
721 Give up (and stop debugging it)? "))
723 target_mourn_inferior ();
724 return_to_top_level (RETURN_QUIT
);
727 target_terminal_inferior ();
730 /* If nonzero, ignore the next kill. */
733 /* Wait until the remote machine stops, then return,
734 storing status in STATUS just as `wait' would.
735 Returns "pid" (though it's not clear what, if anything, that
736 means in the case of this target). */
739 remote_wait (pid
, status
)
741 struct target_waitstatus
*status
;
743 unsigned char buf
[PBUFSIZ
];
746 status
->kind
= TARGET_WAITKIND_EXITED
;
747 status
->value
.integer
= 0;
753 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
754 getpkt ((char *) buf
, 1);
755 signal (SIGINT
, ofunc
);
759 case 'E': /* Error of some sort */
760 warning ("Remote failure reply: %s", buf
);
762 case 'T': /* Status with PC, SP, FP, ... */
766 char regs
[MAX_REGISTER_RAW_SIZE
];
768 /* Expedited reply, containing Signal, {regno, reg} repeat */
769 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
771 n... = register number
772 r... = register contents
775 p
= &buf
[3]; /* after Txx */
782 regno
= strtol ((const char *) p
, &p_temp
, 16); /* Read the register number */
783 p1
= (unsigned char *)p_temp
;
787 p1
= (unsigned char *) strchr ((const char *) p
, ':');
789 warning ("Malformed packet (missing colon): %s\n\
792 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
794 thread_num
= strtol ((const char *) ++p1
, &p_temp
, 16);
795 p
= (unsigned char *)p_temp
;
803 warning ("Malformed packet (missing colon): %s\n\
807 if (regno
>= NUM_REGS
)
808 warning ("Remote sent bad register number %ld: %s\n\
812 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
814 if (p
[0] == 0 || p
[1] == 0)
815 warning ("Remote reply is too short: %s", buf
);
816 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
819 supply_register (regno
, regs
);
823 warning ("Remote register badly formatted: %s", buf
);
827 case 'S': /* Old style status, just signal only */
828 status
->kind
= TARGET_WAITKIND_STOPPED
;
829 status
->value
.sig
= (enum target_signal
)
830 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
833 case 'W': /* Target exited */
835 /* The remote process exited. */
836 status
->kind
= TARGET_WAITKIND_EXITED
;
837 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
841 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
842 status
->value
.sig
= (enum target_signal
)
843 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
847 case 'O': /* Console output */
848 for (p
= buf
+ 1; *p
; p
+=2)
851 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
854 if (target_output_hook
)
855 target_output_hook (tb
);
857 fputs_filtered (tb
, gdb_stdout
);
861 if (last_sent_signal
!= TARGET_SIGNAL_0
)
863 /* Zero length reply means that we tried 'S' or 'C' and
864 the remote system doesn't support it. */
865 target_terminal_ours_for_output ();
867 ("Can't send signals to this remote system. %s not sent.\n",
868 target_signal_to_name (last_sent_signal
));
869 last_sent_signal
= TARGET_SIGNAL_0
;
870 target_terminal_inferior ();
872 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
873 putpkt ((char *) buf
);
876 /* else fallthrough */
878 warning ("Invalid remote reply: %s", buf
);
883 if (thread_num
!= -1)
885 /* Initial thread value can only be acquired via wait, so deal with
886 this marker which is used before the first thread value is
888 if (inferior_pid
== 42000)
890 inferior_pid
= thread_num
;
891 add_thread (inferior_pid
);
898 /* Number of bytes of registers this stub implements. */
899 static int register_bytes_found
;
901 /* Read the remote registers into the block REGS. */
902 /* Currently we just read all the registers, so we don't use regno. */
905 remote_fetch_registers (regno
)
911 char regs
[REGISTER_BYTES
];
913 set_thread (inferior_pid
, 1);
918 /* Unimplemented registers read as all bits zero. */
919 memset (regs
, 0, REGISTER_BYTES
);
921 /* We can get out of synch in various cases. If the first character
922 in the buffer is not a hex character, assume that has happened
923 and try to fetch another packet to read. */
924 while ((buf
[0] < '0' || buf
[0] > '9')
925 && (buf
[0] < 'a' || buf
[0] > 'f'))
928 printf_unfiltered ("Bad register packet; fetching a new packet\n");
932 /* Reply describes registers byte by byte, each byte encoded as two
933 hex characters. Suck them all up, then supply them to the
934 register cacheing/storage mechanism. */
937 for (i
= 0; i
< REGISTER_BYTES
; i
++)
943 warning ("Remote reply is of odd length: %s", buf
);
944 /* Don't change register_bytes_found in this case, and don't
945 print a second warning. */
948 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
952 if (i
!= register_bytes_found
)
954 register_bytes_found
= i
;
955 #ifdef REGISTER_BYTES_OK
956 if (!REGISTER_BYTES_OK (i
))
957 warning ("Remote reply is too short: %s", buf
);
962 for (i
= 0; i
< NUM_REGS
; i
++)
963 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
966 /* Prepare to store registers. Since we may send them all (using a
967 'G' request), we have to read out the ones we don't want to change
971 remote_prepare_to_store ()
973 /* Make sure the entire registers array is valid. */
974 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
977 /* Store register REGNO, or all registers if REGNO == -1, from the contents
978 of REGISTERS. FIXME: ignores errors. */
981 remote_store_registers (regno
)
988 set_thread (inferior_pid
, 1);
990 if (regno
>= 0 && stub_supports_P
)
992 /* Try storing a single register. */
995 sprintf (buf
, "P%x=", regno
);
996 p
= buf
+ strlen (buf
);
997 regp
= ®isters
[REGISTER_BYTE (regno
)];
998 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
1000 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
1001 *p
++ = tohex (regp
[i
] & 0xf);
1007 /* The stub understands the 'P' request. We are done. */
1011 /* The stub does not support the 'P' request. Use 'G' instead,
1012 and don't try using 'P' in the future (it will just waste our
1014 stub_supports_P
= 0;
1019 /* Command describes registers byte by byte,
1020 each byte encoded as two hex characters. */
1023 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1024 for (i
= 0; i
< register_bytes_found
; i
++)
1026 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
1027 *p
++ = tohex (registers
[i
] & 0xf);
1035 Use of the data cache *used* to be disabled because it loses for looking at
1036 and changing hardware I/O ports and the like. Accepting `volatile'
1037 would perhaps be one way to fix it. Another idea would be to use the
1038 executable file for the text segment (for all SEC_CODE sections?
1039 For all SEC_READONLY sections?). This has problems if you want to
1040 actually see what the memory contains (e.g. self-modifying code,
1041 clobbered memory, user downloaded the wrong thing).
1043 Because it speeds so much up, it's now enabled, if you're playing
1044 with registers you turn it of (set remotecache 0)
1047 /* Read a word from remote address ADDR and return it.
1048 This goes through the data cache. */
1052 remote_fetch_word (addr
)
1055 return dcache_fetch (remote_dcache
, addr
);
1058 /* Write a word WORD into remote address ADDR.
1059 This goes through the data cache. */
1062 remote_store_word (addr
, word
)
1066 dcache_poke (remote_dcache
, addr
, word
);
1068 #endif /* 0 (unused?) */
1071 /* Write memory data directly to the remote machine.
1072 This does not inform the data cache; the data cache uses this.
1073 MEMADDR is the address in the remote memory space.
1074 MYADDR is the address of the buffer in our space.
1075 LEN is the number of bytes.
1077 Returns number of bytes transferred, or 0 for error. */
1080 remote_write_bytes (memaddr
, myaddr
, len
)
1089 /* Chop the transfer down if necessary */
1094 int todo
= len
- done
;
1095 int cando
= min(remote_write_size
, PBUFSIZ
) / 2 - 32; /* num bytes that will fit */
1100 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1101 result in a buffer like sprintf. */
1102 sprintf (buf
, "M%lx,%x:", (unsigned long) memaddr
+ done
, todo
);
1104 /* We send target system values byte by byte, in increasing byte addresses,
1105 each byte encoded as two hex characters. */
1107 p
= buf
+ strlen (buf
);
1108 for (i
= 0; i
< todo
; i
++)
1110 *p
++ = tohex ((myaddr
[i
+ done
] >> 4) & 0xf);
1111 *p
++ = tohex (myaddr
[i
+ done
] & 0xf);
1120 /* There is no correspondance between what the remote protocol uses
1121 for errors and errno codes. We would like a cleaner way of
1122 representing errors (big enough to include errno codes, bfd_error
1123 codes, and others). But for now just return EIO. */
1132 /* Read memory data directly from the remote machine.
1133 This does not use the data cache; the data cache uses this.
1134 MEMADDR is the address in the remote memory space.
1135 MYADDR is the address of the buffer in our space.
1136 LEN is the number of bytes.
1138 Returns number of bytes transferred, or 0 for error. */
1141 remote_read_bytes (memaddr
, myaddr
, len
)
1150 /* Chop transfer down if neccessary */
1153 /* FIXME: This is wrong for larger packets */
1154 if (len
> PBUFSIZ
/ 2 - 1)
1160 int todo
= len
- done
;
1161 int cando
= PBUFSIZ
/ 2 - 32; /* number of bytes that will fit. */
1165 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1166 result in a buffer like sprintf. */
1167 sprintf (buf
, "m%lx,%x", (unsigned long) memaddr
+ done
, todo
);
1173 /* There is no correspondance between what the remote protocol uses
1174 for errors and errno codes. We would like a cleaner way of
1175 representing errors (big enough to include errno codes, bfd_error
1176 codes, and others). But for now just return EIO. */
1181 /* Reply describes memory byte by byte,
1182 each byte encoded as two hex characters. */
1185 for (i
= 0; i
< todo
; i
++)
1187 if (p
[0] == 0 || p
[1] == 0)
1188 /* Reply is short. This means that we were able to read only part
1189 of what we wanted to. */
1191 myaddr
[i
+ done
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1199 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1200 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1201 nonzero. Returns length of data written or read; 0 for error. */
1205 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
1210 struct target_ops
*target
; /* ignored */
1212 return dcache_xfer_memory (remote_dcache
, memaddr
, myaddr
, len
, should_write
);
1217 /* Enable after 4.12. */
1220 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
1221 addr_found
, data_found
)
1225 CORE_ADDR startaddr
;
1229 CORE_ADDR
*addr_found
;
1232 if (increment
== -4 && len
== 4)
1234 long mask_long
, data_long
;
1235 long data_found_long
;
1236 CORE_ADDR addr_we_found
;
1238 long returned_long
[2];
1241 mask_long
= extract_unsigned_integer (mask
, len
);
1242 data_long
= extract_unsigned_integer (data
, len
);
1243 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
1248 /* The stub doesn't support the 't' request. We might want to
1249 remember this fact, but on the other hand the stub could be
1250 switched on us. Maybe we should remember it only until
1251 the next "target remote". */
1252 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1253 hirange
, addr_found
, data_found
);
1258 /* There is no correspondance between what the remote protocol uses
1259 for errors and errno codes. We would like a cleaner way of
1260 representing errors (big enough to include errno codes, bfd_error
1261 codes, and others). But for now just use EIO. */
1262 memory_error (EIO
, startaddr
);
1265 while (*p
!= '\0' && *p
!= ',')
1266 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
1268 error ("Protocol error: short return for search");
1270 data_found_long
= 0;
1271 while (*p
!= '\0' && *p
!= ',')
1272 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
1273 /* Ignore anything after this comma, for future extensions. */
1275 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
1281 *addr_found
= addr_we_found
;
1282 *data_found
= store_unsigned_integer (data_we_found
, len
);
1285 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1286 hirange
, addr_found
, data_found
);
1291 remote_files_info (ignore
)
1292 struct target_ops
*ignore
;
1294 puts_filtered ("Debugging a target over a serial line.\n");
1297 /* Stuff for dealing with the packets which are part of this protocol.
1298 See comment at top of file for details. */
1300 /* Read a single character from the remote end, masking it down to 7 bits. */
1308 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
1313 error ("Remote connection closed");
1315 perror_with_name ("Remote communication error");
1316 case SERIAL_TIMEOUT
:
1323 /* Send the command in BUF to the remote machine,
1324 and read the reply into BUF.
1325 Report an error if we get an error reply. */
1335 error ("Remote failure reply: %s", buf
);
1338 /* Send a packet to the remote machine, with error checking.
1339 The data of the packet is in BUF. */
1346 unsigned char csum
= 0;
1348 int cnt
= strlen (buf
);
1353 /* Copy the packet into buffer BUF2, encapsulating it
1354 and giving it a checksum. */
1356 if (cnt
> (int) sizeof (buf2
) - 5) /* Prosanity check */
1362 for (i
= 0; i
< cnt
; i
++)
1368 *p
++ = tohex ((csum
>> 4) & 0xf);
1369 *p
++ = tohex (csum
& 0xf);
1371 /* Send it over and over until we get a positive ack. */
1375 int started_error_output
= 0;
1380 printf_unfiltered ("Sending packet: %s...", buf2
);
1381 gdb_flush(gdb_stdout
);
1383 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
1384 perror_with_name ("putpkt: write failed");
1386 /* read until either a timeout occurs (-2) or '+' is read */
1389 ch
= readchar (remote_timeout
);
1396 case SERIAL_TIMEOUT
:
1398 if (started_error_output
)
1400 putchar_unfiltered ('\n');
1401 started_error_output
= 0;
1410 printf_unfiltered("Ack\n");
1412 case SERIAL_TIMEOUT
:
1416 break; /* Retransmit buffer */
1419 char junkbuf
[PBUFSIZ
];
1421 /* It's probably an old response, and we're out of sync. Just
1422 gobble up the packet and ignore it. */
1423 getpkt (junkbuf
, 0);
1424 continue; /* Now, go look for + */
1429 if (!started_error_output
)
1431 started_error_output
= 1;
1432 printf_unfiltered ("putpkt: Junk: ");
1434 putchar_unfiltered (ch
& 0177);
1438 break; /* Here to retransmit */
1442 /* This is wrong. If doing a long backtrace, the user should be
1443 able to get out next time we call QUIT, without anything as violent
1444 as interrupt_query. If we want to provide a way out of here
1445 without getting to the next QUIT, it should be based on hitting
1446 ^C twice as in remote_wait. */
1456 /* Come here after finding the start of the frame. Collect the rest into BUF,
1457 verifying the checksum, length, and handling run-length compression.
1458 Returns 0 on any error, 1 on success. */
1473 c
= readchar (remote_timeout
);
1477 case SERIAL_TIMEOUT
:
1479 puts_filtered ("Timeout in mid-packet, retrying\n");
1483 puts_filtered ("Saw new packet start in middle of old one\n");
1484 return 0; /* Start a new packet, count retries */
1487 unsigned char pktcsum
;
1491 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
1492 pktcsum
|= fromhex (readchar (remote_timeout
));
1494 if (csum
== pktcsum
)
1499 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1501 puts_filtered (buf
);
1502 puts_filtered ("\n");
1506 case '*': /* Run length encoding */
1508 c
= readchar (remote_timeout
);
1510 c
= c
- ' ' + 3; /* Compute repeat count */
1513 if (c
> 0 && c
< 255 && bp
+ c
- 1 < buf
+ PBUFSIZ
- 1)
1515 memset (bp
, *(bp
- 1), c
);
1521 printf_filtered ("Repeat count %d too large for buffer: ", c
);
1522 puts_filtered (buf
);
1523 puts_filtered ("\n");
1527 if (bp
< buf
+ PBUFSIZ
- 1)
1535 puts_filtered ("Remote packet too long: ");
1536 puts_filtered (buf
);
1537 puts_filtered ("\n");
1544 /* Read a packet from the remote machine, with error checking,
1545 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1546 If FOREVER, wait forever rather than timing out; this is used
1547 while the target is executing user code. */
1550 getpkt (buf
, forever
)
1559 strcpy (buf
,"timeout");
1563 #ifdef MAINTENANCE_CMDS
1564 timeout
= watchdog
> 0 ? watchdog
: -1;
1571 timeout
= remote_timeout
;
1575 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
1577 /* This can loop forever if the remote side sends us characters
1578 continuously, but if it pauses, we'll get a zero from readchar
1579 because of timeout. Then we'll count that as a retry. */
1581 /* Note that we will only wait forever prior to the start of a packet.
1582 After that, we expect characters to arrive at a brisk pace. They
1583 should show up within remote_timeout intervals. */
1587 c
= readchar (timeout
);
1589 if (c
== SERIAL_TIMEOUT
)
1591 #ifdef MAINTENANCE_CMDS
1592 if (forever
) /* Watchdog went off. Kill the target. */
1594 target_mourn_inferior ();
1595 error ("Watchdog has expired. Target detached.\n");
1599 puts_filtered ("Timed out.\n");
1605 /* We've found the start of a packet, now collect the data. */
1607 val
= read_frame (buf
);
1612 fprintf_unfiltered (gdb_stderr
, "Packet received: %s\n", buf
);
1613 SERIAL_WRITE (remote_desc
, "+", 1);
1617 /* Try the whole thing again. */
1619 SERIAL_WRITE (remote_desc
, "-", 1);
1622 /* We have tried hard enough, and just can't receive the packet. Give up. */
1624 printf_unfiltered ("Ignoring packet error, continuing...\n");
1625 SERIAL_WRITE (remote_desc
, "+", 1);
1631 /* For some mysterious reason, wait_for_inferior calls kill instead of
1632 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1636 target_mourn_inferior ();
1640 /* Use catch_errors so the user can quit from gdb even when we aren't on
1641 speaking terms with the remote system. */
1642 catch_errors (putpkt
, "k", "", RETURN_MASK_ERROR
);
1644 /* Don't wait for it to die. I'm not really sure it matters whether
1645 we do or not. For the existing stubs, kill is a noop. */
1646 target_mourn_inferior ();
1652 remote_mourn_1 (&remote_ops
);
1656 extended_remote_mourn ()
1658 /* We do _not_ want to mourn the target like this; this will
1659 remove the extended remote target from the target stack,
1660 and the next time the user says "run" it'll fail.
1662 FIXME: What is the right thing to do here? */
1664 remote_mourn_1 (&extended_remote_ops
);
1668 /* Worker function for remote_mourn. */
1670 remote_mourn_1 (target
)
1671 struct target_ops
*target
;
1673 unpush_target (target
);
1674 generic_mourn_inferior ();
1677 /* In the extended protocol we want to be able to do things like
1678 "run" and have them basically work as expected. So we need
1679 a special create_inferior function.
1681 FIXME: One day add support for changing the exec file
1682 we're debugging, arguments and an environment. */
1685 extended_remote_create_inferior (exec_file
, args
, env
)
1690 /* Rip out the breakpoints; we'll reinsert them after restarting
1691 the remote server. */
1692 remove_breakpoints ();
1694 /* Now restart the remote server. */
1695 extended_remote_restart ();
1697 /* Now put the breakpoints back in. This way we're safe if the
1698 restart function works via a unix fork on the remote side. */
1699 insert_breakpoints ();
1701 /* Clean up from the last time we were running. */
1702 clear_proceed_status ();
1704 /* Let the remote process run. */
1705 proceed (-1, TARGET_SIGNAL_0
, 0);
1709 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1710 than other targets; in those use REMOTE_BREAKPOINT instead of just
1711 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1712 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1713 the standard routines that are in mem-break.c. */
1715 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1716 the choice of breakpoint instruction affects target program design and
1717 vice versa, and by making it user-tweakable, the special code here
1718 goes away and we need fewer special GDB configurations. */
1720 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1721 #define REMOTE_BREAKPOINT
1724 #ifdef REMOTE_BREAKPOINT
1726 /* If the target isn't bi-endian, just pretend it is. */
1727 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1728 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1729 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1732 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
1733 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
1735 #endif /* REMOTE_BREAKPOINT */
1737 /* Insert a breakpoint on targets that don't have any better breakpoint
1738 support. We read the contents of the target location and stash it,
1739 then overwrite it with a breakpoint instruction. ADDR is the target
1740 location in the target machine. CONTENTS_CACHE is a pointer to
1741 memory allocated for saving the target contents. It is guaranteed
1742 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1743 is accomplished via BREAKPOINT_MAX). */
1746 remote_insert_breakpoint (addr
, contents_cache
)
1748 char *contents_cache
;
1750 #ifdef REMOTE_BREAKPOINT
1753 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
1757 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1758 val
= target_write_memory (addr
, (char *) big_break_insn
,
1759 sizeof big_break_insn
);
1761 val
= target_write_memory (addr
, (char *) little_break_insn
,
1762 sizeof little_break_insn
);
1767 return memory_insert_breakpoint (addr
, contents_cache
);
1768 #endif /* REMOTE_BREAKPOINT */
1772 remote_remove_breakpoint (addr
, contents_cache
)
1774 char *contents_cache
;
1776 #ifdef REMOTE_BREAKPOINT
1777 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
1779 return memory_remove_breakpoint (addr
, contents_cache
);
1780 #endif /* REMOTE_BREAKPOINT */
1783 /* Define the target subroutine names */
1785 struct target_ops remote_ops
= {
1786 "remote", /* to_shortname */
1787 "Remote serial target in gdb-specific protocol", /* to_longname */
1788 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1789 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1790 remote_open
, /* to_open */
1791 remote_close
, /* to_close */
1792 NULL
, /* to_attach */
1793 remote_detach
, /* to_detach */
1794 remote_resume
, /* to_resume */
1795 remote_wait
, /* to_wait */
1796 remote_fetch_registers
, /* to_fetch_registers */
1797 remote_store_registers
, /* to_store_registers */
1798 remote_prepare_to_store
, /* to_prepare_to_store */
1799 remote_xfer_memory
, /* to_xfer_memory */
1800 remote_files_info
, /* to_files_info */
1801 remote_insert_breakpoint
, /* to_insert_breakpoint */
1802 remote_remove_breakpoint
, /* to_remove_breakpoint */
1803 NULL
, /* to_terminal_init */
1804 NULL
, /* to_terminal_inferior */
1805 NULL
, /* to_terminal_ours_for_output */
1806 NULL
, /* to_terminal_ours */
1807 NULL
, /* to_terminal_info */
1808 remote_kill
, /* to_kill */
1809 generic_load
, /* to_load */
1810 NULL
, /* to_lookup_symbol */
1811 NULL
, /* to_create_inferior */
1812 remote_mourn
, /* to_mourn_inferior */
1814 0, /* to_notice_signals */
1815 remote_thread_alive
, /* to_thread_alive */
1817 process_stratum
, /* to_stratum */
1819 1, /* to_has_all_memory */
1820 1, /* to_has_memory */
1821 1, /* to_has_stack */
1822 1, /* to_has_registers */
1823 1, /* to_has_execution */
1824 NULL
, /* sections */
1825 NULL
, /* sections_end */
1826 OPS_MAGIC
/* to_magic */
1829 struct target_ops extended_remote_ops
= {
1830 "extended-remote", /* to_shortname */
1831 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1832 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1833 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1834 extended_remote_open
, /* to_open */
1835 remote_close
, /* to_close */
1836 NULL
, /* to_attach */
1837 remote_detach
, /* to_detach */
1838 remote_resume
, /* to_resume */
1839 remote_wait
, /* to_wait */
1840 remote_fetch_registers
, /* to_fetch_registers */
1841 remote_store_registers
, /* to_store_registers */
1842 remote_prepare_to_store
, /* to_prepare_to_store */
1843 remote_xfer_memory
, /* to_xfer_memory */
1844 remote_files_info
, /* to_files_info */
1846 remote_insert_breakpoint
, /* to_insert_breakpoint */
1847 remote_remove_breakpoint
, /* to_remove_breakpoint */
1849 NULL
, /* to_terminal_init */
1850 NULL
, /* to_terminal_inferior */
1851 NULL
, /* to_terminal_ours_for_output */
1852 NULL
, /* to_terminal_ours */
1853 NULL
, /* to_terminal_info */
1854 remote_kill
, /* to_kill */
1855 generic_load
, /* to_load */
1856 NULL
, /* to_lookup_symbol */
1857 extended_remote_create_inferior
,/* to_create_inferior */
1858 extended_remote_mourn
, /* to_mourn_inferior */
1860 0, /* to_notice_signals */
1861 remote_thread_alive
, /* to_thread_alive */
1863 process_stratum
, /* to_stratum */
1865 1, /* to_has_all_memory */
1866 1, /* to_has_memory */
1867 1, /* to_has_stack */
1868 1, /* to_has_registers */
1869 1, /* to_has_execution */
1870 NULL
, /* sections */
1871 NULL
, /* sections_end */
1872 OPS_MAGIC
/* to_magic */
1876 _initialize_remote ()
1878 add_target (&remote_ops
);
1879 add_target (&extended_remote_ops
);
1881 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1882 var_integer
, (char *)&remote_timeout
,
1883 "Set timeout value for remote read.\n", &setlist
),
1886 add_show_from_set (add_set_cmd ("remotebreak", no_class
,
1887 var_integer
, (char *)&remote_break
,
1888 "Set whether to send break if interrupted.\n", &setlist
),
1891 add_show_from_set (add_set_cmd ("remotewritesize", no_class
,
1892 var_integer
, (char *)&remote_write_size
,
1893 "Set the maximum number of bytes in each memory write packet.\n", &setlist
),