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 extern struct target_ops remote_ops
; /* Forward decl */
270 extern struct target_ops extended_remote_ops
; /* Forward decl */
272 /* This was 5 seconds, which is a long time to sit and wait.
273 Unless this is going though some terminal server or multiplexer or
274 other form of hairy serial connection, I would think 2 seconds would
277 /* Changed to allow option to set timeout value.
278 was static int remote_timeout = 2; */
279 extern int remote_timeout
;
281 /* This variable chooses whether to send a ^C or a break when the user
282 requests program interruption. Although ^C is usually what remote
283 systems expect, and that is the default here, sometimes a break is
284 preferable instead. */
286 static int remote_break
;
288 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
289 remote_open knows that we don't have a file open when the program
291 serial_t remote_desc
= NULL
;
293 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
294 and i386-stub.c. Normally, no one would notice because it only matters
295 for writing large chunks of memory (e.g. in downloads). Also, this needs
296 to be more than 400 if required to hold the registers (see below, where
297 we round it up based on REGISTER_BYTES). */
300 /* Maximum number of bytes to read/write at once. The value here
301 is chosen to fill up a packet (the headers account for the 32). */
302 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
304 /* Round up PBUFSIZ to hold all the registers, at least. */
305 /* The blank line after the #if seems to be required to work around a
306 bug in HP's PA compiler. */
307 #if REGISTER_BYTES > MAXBUFBYTES
310 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
313 /* This variable sets the number of bytes to be written to the target
314 in a single packet. Normally PBUFSIZ is satisfactory, but some
315 targets need smaller values (perhaps because the receiving end
318 static int remote_write_size
= PBUFSIZ
;
320 /* Should we try the 'P' request? If this is set to one when the stub
321 doesn't support 'P', the only consequence is some unnecessary traffic. */
322 static int stub_supports_P
= 1;
325 /* These are the threads which we last sent to the remote system. -1 for all
326 or -2 for not sent yet. */
336 int state
= gen
? general_thread
: cont_thread
;
340 buf
[1] = gen
? 'g' : 'c';
347 sprintf (&buf
[2], "-%x", -th
);
349 sprintf (&buf
[2], "%x", th
);
358 /* Return nonzero if the thread TH is still alive on the remote system. */
361 remote_thread_alive (th
)
368 sprintf (&buf
[1], "-%x", -th
);
370 sprintf (&buf
[1], "%x", th
);
373 return (buf
[0] == 'O' && buf
[1] == 'K');
376 /* Restart the remote side; this is an extended protocol operation. */
379 extended_remote_restart ()
383 /* Send the restart command; for reasons I don't understand the
384 remote side really expects a number after the "R". */
386 sprintf (&buf
[1], "%x", 0);
389 /* Now query for status so this looks just like we restarted
390 gdbserver from scratch. */
395 /* Clean up connection to a remote debugger. */
399 remote_close (quitting
)
403 SERIAL_CLOSE (remote_desc
);
407 /* Query the remote side for the text, data and bss offsets. */
414 CORE_ADDR text_addr
, data_addr
, bss_addr
;
415 struct section_offsets
*offs
;
421 if (buf
[0] == '\000')
422 return; /* Return silently. Stub doesn't support this
426 warning ("Remote failure reply: %s", buf
);
430 nvals
= sscanf (buf
, "Text=%lx;Data=%lx;Bss=%lx", &text_addr
, &data_addr
,
433 error ("Malformed response to offset query, %s", buf
);
435 if (symfile_objfile
== NULL
)
438 offs
= (struct section_offsets
*) alloca (sizeof (struct section_offsets
)
439 + symfile_objfile
->num_sections
440 * sizeof (offs
->offsets
));
441 memcpy (offs
, symfile_objfile
->section_offsets
,
442 sizeof (struct section_offsets
)
443 + symfile_objfile
->num_sections
444 * sizeof (offs
->offsets
));
446 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
448 /* This is a temporary kludge to force data and bss to use the same offsets
449 because that's what nlmconv does now. The real solution requires changes
450 to the stub and remote.c that I don't have time to do right now. */
452 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
453 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
455 objfile_relocate (symfile_objfile
, offs
);
458 /* Stub for catch_errors. */
461 remote_start_remote (dummy
)
464 immediate_quit
= 1; /* Allow user to interrupt it */
466 /* Ack any packet which the remote side has already sent. */
467 SERIAL_WRITE (remote_desc
, "+", 1);
469 /* Let the stub know that we want it to return the thread. */
472 get_offsets (); /* Get text, data & bss offsets */
474 putpkt ("?"); /* initiate a query from remote machine */
477 start_remote (); /* Initialize gdb process mechanisms */
481 /* Open a connection to a remote debugger.
482 NAME is the filename used for communication. */
485 remote_open (name
, from_tty
)
489 remote_open_1 (name
, from_tty
, &remote_ops
);
492 /* Open a connection to a remote debugger using the extended
493 remote gdb protocol. NAME is the filename used for communication. */
496 extended_remote_open (name
, from_tty
)
502 /* Do the basic remote open stuff. */
503 remote_open_1 (name
, from_tty
, &extended_remote_ops
);
505 /* Now tell the remote that we're using the extended protocol. */
511 /* Generic code for opening a connection to a remote target. */
512 static DCACHE
*remote_dcache
;
515 remote_open_1 (name
, from_tty
, target
)
518 struct target_ops
*target
;
521 error ("To open a remote debug connection, you need to specify what serial\n\
522 device is attached to the remote system (e.g. /dev/ttya).");
524 target_preopen (from_tty
);
526 unpush_target (target
);
528 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
530 remote_desc
= SERIAL_OPEN (name
);
532 perror_with_name (name
);
536 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
538 SERIAL_CLOSE (remote_desc
);
539 perror_with_name (name
);
544 SERIAL_RAW (remote_desc
);
546 /* If there is something sitting in the buffer we might take it as a
547 response to a command, which would be bad. */
548 SERIAL_FLUSH_INPUT (remote_desc
);
552 puts_filtered ("Remote debugging using ");
553 puts_filtered (name
);
554 puts_filtered ("\n");
556 push_target (target
); /* Switch to using remote target now */
558 /* Start out by trying the 'P' request to set registers. We set this each
559 time that we open a new target so that if the user switches from one
560 stub to another, we can (if the target is closed and reopened) cope. */
566 /* Without this, some commands which require an active target (such as kill)
567 won't work. This variable serves (at least) double duty as both the pid
568 of the target process (if it has such), and as a flag indicating that a
569 target is active. These functions should be split out into seperate
570 variables, especially since GDB will someday have a notion of debugging
571 several processes. */
573 inferior_pid
= 42000;
574 /* Start the remote connection; if error (0), discard this target.
575 In particular, if the user quits, be sure to discard it
576 (we'd be in an inconsistent state otherwise). */
577 if (!catch_errors (remote_start_remote
, (char *)0,
578 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL
))
582 /* This takes a program previously attached to and detaches it. After
583 this is done, GDB can be used to debug some other program. We
584 better not have left any breakpoints in the target program or it'll
585 die when it hits one. */
588 remote_detach (args
, from_tty
)
595 error ("Argument given to \"detach\" when remotely debugging.");
597 /* Tell the remote target to detach. */
603 puts_filtered ("Ending remote debugging.\n");
606 /* Convert hex digit A to a number. */
612 if (a
>= '0' && a
<= '9')
614 else if (a
>= 'a' && a
<= 'f')
617 error ("Reply contains invalid hex digit %d", a
);
620 /* Convert number NIB to a hex digit. */
632 /* Tell the remote machine to resume. */
634 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
638 remote_resume (pid
, step
, siggnal
)
640 enum target_signal siggnal
;
645 set_thread (inferior_pid
, 0);
649 dcache_flush (remote_dcache
);
651 last_sent_signal
= siggnal
;
652 last_sent_step
= step
;
654 if (siggnal
!= TARGET_SIGNAL_0
)
656 buf
[0] = step
? 'S' : 'C';
657 buf
[1] = tohex (((int)siggnal
>> 4) & 0xf);
658 buf
[2] = tohex ((int)siggnal
& 0xf);
662 strcpy (buf
, step
? "s": "c");
667 /* Send ^C to target to halt it. Target will respond, and send us a
671 remote_interrupt (signo
)
674 /* If this doesn't work, try more severe steps. */
675 signal (signo
, remote_interrupt_twice
);
678 printf_unfiltered ("remote_interrupt called\n");
680 /* Send a break or a ^C, depending on user preference. */
682 SERIAL_SEND_BREAK (remote_desc
);
684 SERIAL_WRITE (remote_desc
, "\003", 1);
687 static void (*ofunc
)();
689 /* The user typed ^C twice. */
691 remote_interrupt_twice (signo
)
694 signal (signo
, ofunc
);
698 signal (signo
, remote_interrupt
);
701 /* Ask the user what to do when an interrupt is received. */
706 target_terminal_ours ();
708 if (query ("Interrupted while waiting for the program.\n\
709 Give up (and stop debugging it)? "))
711 target_mourn_inferior ();
712 return_to_top_level (RETURN_QUIT
);
715 target_terminal_inferior ();
718 /* If nonzero, ignore the next kill. */
721 /* Wait until the remote machine stops, then return,
722 storing status in STATUS just as `wait' would.
723 Returns "pid" (though it's not clear what, if anything, that
724 means in the case of this target). */
727 remote_wait (pid
, status
)
729 struct target_waitstatus
*status
;
731 unsigned char buf
[PBUFSIZ
];
734 status
->kind
= TARGET_WAITKIND_EXITED
;
735 status
->value
.integer
= 0;
741 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
742 getpkt ((char *) buf
, 1);
743 signal (SIGINT
, ofunc
);
747 case 'E': /* Error of some sort */
748 warning ("Remote failure reply: %s", buf
);
750 case 'T': /* Status with PC, SP, FP, ... */
754 char regs
[MAX_REGISTER_RAW_SIZE
];
756 /* Expedited reply, containing Signal, {regno, reg} repeat */
757 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
759 n... = register number
760 r... = register contents
763 p
= &buf
[3]; /* after Txx */
770 regno
= strtol ((const char *) p
, &p_temp
, 16); /* Read the register number */
771 p1
= (unsigned char *)p_temp
;
775 p1
= (unsigned char *) strchr ((const char *) p
, ':');
777 warning ("Malformed packet (missing colon): %s\n\
780 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
782 thread_num
= strtol ((const char *) ++p1
, &p_temp
, 16);
783 p
= (unsigned char *)p_temp
;
791 warning ("Malformed packet (missing colon): %s\n\
795 if (regno
>= NUM_REGS
)
796 warning ("Remote sent bad register number %ld: %s\n\
800 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
802 if (p
[0] == 0 || p
[1] == 0)
803 warning ("Remote reply is too short: %s", buf
);
804 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
807 supply_register (regno
, regs
);
811 warning ("Remote register badly formatted: %s", buf
);
815 case 'S': /* Old style status, just signal only */
816 status
->kind
= TARGET_WAITKIND_STOPPED
;
817 status
->value
.sig
= (enum target_signal
)
818 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
821 case 'W': /* Target exited */
823 /* The remote process exited. */
824 status
->kind
= TARGET_WAITKIND_EXITED
;
825 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
829 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
830 status
->value
.sig
= (enum target_signal
)
831 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
835 case 'O': /* Console output */
836 for (p
= buf
+ 1; *p
; p
+=2)
839 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
842 if (target_output_hook
)
843 target_output_hook (tb
);
845 fputs_filtered (tb
, gdb_stdout
);
849 if (last_sent_signal
!= TARGET_SIGNAL_0
)
851 /* Zero length reply means that we tried 'S' or 'C' and
852 the remote system doesn't support it. */
853 target_terminal_ours_for_output ();
855 ("Can't send signals to this remote system. %s not sent.\n",
856 target_signal_to_name (last_sent_signal
));
857 last_sent_signal
= TARGET_SIGNAL_0
;
858 target_terminal_inferior ();
860 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
861 putpkt ((char *) buf
);
864 /* else fallthrough */
866 warning ("Invalid remote reply: %s", buf
);
871 if (thread_num
!= -1)
873 /* Initial thread value can only be acquired via wait, so deal with
874 this marker which is used before the first thread value is
876 if (inferior_pid
== 42000)
878 inferior_pid
= thread_num
;
879 add_thread (inferior_pid
);
886 /* Number of bytes of registers this stub implements. */
887 static int register_bytes_found
;
889 /* Read the remote registers into the block REGS. */
890 /* Currently we just read all the registers, so we don't use regno. */
893 remote_fetch_registers (regno
)
899 char regs
[REGISTER_BYTES
];
901 set_thread (inferior_pid
, 1);
906 /* Unimplemented registers read as all bits zero. */
907 memset (regs
, 0, REGISTER_BYTES
);
909 /* We can get out of synch in various cases. If the first character
910 in the buffer is not a hex character, assume that has happened
911 and try to fetch another packet to read. */
912 while ((buf
[0] < '0' || buf
[0] > '9')
913 && (buf
[0] < 'a' || buf
[0] > 'f'))
916 printf_unfiltered ("Bad register packet; fetching a new packet\n");
920 /* Reply describes registers byte by byte, each byte encoded as two
921 hex characters. Suck them all up, then supply them to the
922 register cacheing/storage mechanism. */
925 for (i
= 0; i
< REGISTER_BYTES
; i
++)
931 warning ("Remote reply is of odd length: %s", buf
);
932 /* Don't change register_bytes_found in this case, and don't
933 print a second warning. */
936 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
940 if (i
!= register_bytes_found
)
942 register_bytes_found
= i
;
943 #ifdef REGISTER_BYTES_OK
944 if (!REGISTER_BYTES_OK (i
))
945 warning ("Remote reply is too short: %s", buf
);
950 for (i
= 0; i
< NUM_REGS
; i
++)
951 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
954 /* Prepare to store registers. Since we may send them all (using a
955 'G' request), we have to read out the ones we don't want to change
959 remote_prepare_to_store ()
961 /* Make sure the entire registers array is valid. */
962 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
965 /* Store register REGNO, or all registers if REGNO == -1, from the contents
966 of REGISTERS. FIXME: ignores errors. */
969 remote_store_registers (regno
)
976 set_thread (inferior_pid
, 1);
978 if (regno
>= 0 && stub_supports_P
)
980 /* Try storing a single register. */
983 sprintf (buf
, "P%x=", regno
);
984 p
= buf
+ strlen (buf
);
985 regp
= ®isters
[REGISTER_BYTE (regno
)];
986 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
988 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
989 *p
++ = tohex (regp
[i
] & 0xf);
995 /* The stub understands the 'P' request. We are done. */
999 /* The stub does not support the 'P' request. Use 'G' instead,
1000 and don't try using 'P' in the future (it will just waste our
1002 stub_supports_P
= 0;
1007 /* Command describes registers byte by byte,
1008 each byte encoded as two hex characters. */
1011 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1012 for (i
= 0; i
< register_bytes_found
; i
++)
1014 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
1015 *p
++ = tohex (registers
[i
] & 0xf);
1023 Use of the data cache *used* to be disabled because it loses for looking at
1024 and changing hardware I/O ports and the like. Accepting `volatile'
1025 would perhaps be one way to fix it. Another idea would be to use the
1026 executable file for the text segment (for all SEC_CODE sections?
1027 For all SEC_READONLY sections?). This has problems if you want to
1028 actually see what the memory contains (e.g. self-modifying code,
1029 clobbered memory, user downloaded the wrong thing).
1031 Because it speeds so much up, it's now enabled, if you're playing
1032 with registers you turn it of (set remotecache 0)
1035 /* Read a word from remote address ADDR and return it.
1036 This goes through the data cache. */
1040 remote_fetch_word (addr
)
1043 return dcache_fetch (remote_dcache
, addr
);
1046 /* Write a word WORD into remote address ADDR.
1047 This goes through the data cache. */
1050 remote_store_word (addr
, word
)
1054 dcache_poke (remote_dcache
, addr
, word
);
1056 #endif /* 0 (unused?) */
1059 /* Write memory data directly to the remote machine.
1060 This does not inform the data cache; the data cache uses this.
1061 MEMADDR is the address in the remote memory space.
1062 MYADDR is the address of the buffer in our space.
1063 LEN is the number of bytes.
1065 Returns number of bytes transferred, or 0 for error. */
1068 remote_write_bytes (memaddr
, myaddr
, len
)
1077 /* Chop the transfer down if necessary */
1082 int todo
= len
- done
;
1083 int cando
= min(remote_write_size
, PBUFSIZ
) / 2 - 32; /* num bytes that will fit */
1088 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1089 result in a buffer like sprintf. */
1090 sprintf (buf
, "M%lx,%x:", (unsigned long) memaddr
+ done
, todo
);
1092 /* We send target system values byte by byte, in increasing byte addresses,
1093 each byte encoded as two hex characters. */
1095 p
= buf
+ strlen (buf
);
1096 for (i
= 0; i
< todo
; i
++)
1098 *p
++ = tohex ((myaddr
[i
+ done
] >> 4) & 0xf);
1099 *p
++ = tohex (myaddr
[i
+ done
] & 0xf);
1108 /* There is no correspondance between what the remote protocol uses
1109 for errors and errno codes. We would like a cleaner way of
1110 representing errors (big enough to include errno codes, bfd_error
1111 codes, and others). But for now just return EIO. */
1120 /* Read memory data directly from the remote machine.
1121 This does not use the data cache; the data cache uses this.
1122 MEMADDR is the address in the remote memory space.
1123 MYADDR is the address of the buffer in our space.
1124 LEN is the number of bytes.
1126 Returns number of bytes transferred, or 0 for error. */
1129 remote_read_bytes (memaddr
, myaddr
, len
)
1138 /* Chop transfer down if neccessary */
1141 /* FIXME: This is wrong for larger packets */
1142 if (len
> PBUFSIZ
/ 2 - 1)
1148 int todo
= len
- done
;
1149 int cando
= PBUFSIZ
/ 2 - 32; /* number of bytes that will fit. */
1153 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1154 result in a buffer like sprintf. */
1155 sprintf (buf
, "m%lx,%x", (unsigned long) memaddr
+ done
, todo
);
1161 /* There is no correspondance between what the remote protocol uses
1162 for errors and errno codes. We would like a cleaner way of
1163 representing errors (big enough to include errno codes, bfd_error
1164 codes, and others). But for now just return EIO. */
1169 /* Reply describes memory byte by byte,
1170 each byte encoded as two hex characters. */
1173 for (i
= 0; i
< todo
; i
++)
1175 if (p
[0] == 0 || p
[1] == 0)
1176 /* Reply is short. This means that we were able to read only part
1177 of what we wanted to. */
1179 myaddr
[i
+ done
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1187 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1188 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1189 nonzero. Returns length of data written or read; 0 for error. */
1193 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
1198 struct target_ops
*target
; /* ignored */
1200 return dcache_xfer_memory (remote_dcache
, memaddr
, myaddr
, len
, should_write
);
1205 /* Enable after 4.12. */
1208 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
1209 addr_found
, data_found
)
1213 CORE_ADDR startaddr
;
1217 CORE_ADDR
*addr_found
;
1220 if (increment
== -4 && len
== 4)
1222 long mask_long
, data_long
;
1223 long data_found_long
;
1224 CORE_ADDR addr_we_found
;
1226 long returned_long
[2];
1229 mask_long
= extract_unsigned_integer (mask
, len
);
1230 data_long
= extract_unsigned_integer (data
, len
);
1231 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
1236 /* The stub doesn't support the 't' request. We might want to
1237 remember this fact, but on the other hand the stub could be
1238 switched on us. Maybe we should remember it only until
1239 the next "target remote". */
1240 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1241 hirange
, addr_found
, data_found
);
1246 /* There is no correspondance between what the remote protocol uses
1247 for errors and errno codes. We would like a cleaner way of
1248 representing errors (big enough to include errno codes, bfd_error
1249 codes, and others). But for now just use EIO. */
1250 memory_error (EIO
, startaddr
);
1253 while (*p
!= '\0' && *p
!= ',')
1254 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
1256 error ("Protocol error: short return for search");
1258 data_found_long
= 0;
1259 while (*p
!= '\0' && *p
!= ',')
1260 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
1261 /* Ignore anything after this comma, for future extensions. */
1263 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
1269 *addr_found
= addr_we_found
;
1270 *data_found
= store_unsigned_integer (data_we_found
, len
);
1273 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1274 hirange
, addr_found
, data_found
);
1279 remote_files_info (ignore
)
1280 struct target_ops
*ignore
;
1282 puts_filtered ("Debugging a target over a serial line.\n");
1285 /* Stuff for dealing with the packets which are part of this protocol.
1286 See comment at top of file for details. */
1288 /* Read a single character from the remote end, masking it down to 7 bits. */
1296 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
1301 error ("Remote connection closed");
1303 perror_with_name ("Remote communication error");
1304 case SERIAL_TIMEOUT
:
1311 /* Send the command in BUF to the remote machine,
1312 and read the reply into BUF.
1313 Report an error if we get an error reply. */
1323 error ("Remote failure reply: %s", buf
);
1326 /* Send a packet to the remote machine, with error checking.
1327 The data of the packet is in BUF. */
1334 unsigned char csum
= 0;
1336 int cnt
= strlen (buf
);
1341 /* Copy the packet into buffer BUF2, encapsulating it
1342 and giving it a checksum. */
1344 if (cnt
> (int) sizeof (buf2
) - 5) /* Prosanity check */
1350 for (i
= 0; i
< cnt
; i
++)
1356 *p
++ = tohex ((csum
>> 4) & 0xf);
1357 *p
++ = tohex (csum
& 0xf);
1359 /* Send it over and over until we get a positive ack. */
1363 int started_error_output
= 0;
1368 printf_unfiltered ("Sending packet: %s...", buf2
);
1369 gdb_flush(gdb_stdout
);
1371 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
1372 perror_with_name ("putpkt: write failed");
1374 /* read until either a timeout occurs (-2) or '+' is read */
1377 ch
= readchar (remote_timeout
);
1384 case SERIAL_TIMEOUT
:
1386 if (started_error_output
)
1388 putchar_unfiltered ('\n');
1389 started_error_output
= 0;
1398 printf_unfiltered("Ack\n");
1400 case SERIAL_TIMEOUT
:
1404 break; /* Retransmit buffer */
1407 char junkbuf
[PBUFSIZ
];
1409 /* It's probably an old response, and we're out of sync. Just
1410 gobble up the packet and ignore it. */
1411 getpkt (junkbuf
, 0);
1412 continue; /* Now, go look for + */
1417 if (!started_error_output
)
1419 started_error_output
= 1;
1420 printf_unfiltered ("putpkt: Junk: ");
1422 putchar_unfiltered (ch
& 0177);
1426 break; /* Here to retransmit */
1430 /* This is wrong. If doing a long backtrace, the user should be
1431 able to get out next time we call QUIT, without anything as violent
1432 as interrupt_query. If we want to provide a way out of here
1433 without getting to the next QUIT, it should be based on hitting
1434 ^C twice as in remote_wait. */
1444 /* Come here after finding the start of the frame. Collect the rest into BUF,
1445 verifying the checksum, length, and handling run-length compression.
1446 Returns 0 on any error, 1 on success. */
1461 c
= readchar (remote_timeout
);
1465 case SERIAL_TIMEOUT
:
1467 puts_filtered ("Timeout in mid-packet, retrying\n");
1471 puts_filtered ("Saw new packet start in middle of old one\n");
1472 return 0; /* Start a new packet, count retries */
1475 unsigned char pktcsum
;
1479 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
1480 pktcsum
|= fromhex (readchar (remote_timeout
));
1482 if (csum
== pktcsum
)
1487 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1489 puts_filtered (buf
);
1490 puts_filtered ("\n");
1494 case '*': /* Run length encoding */
1496 c
= readchar (remote_timeout
);
1498 c
= c
- ' ' + 3; /* Compute repeat count */
1501 if (c
> 0 && c
< 255 && bp
+ c
- 1 < buf
+ PBUFSIZ
- 1)
1503 memset (bp
, *(bp
- 1), c
);
1509 printf_filtered ("Repeat count %d too large for buffer: ", c
);
1510 puts_filtered (buf
);
1511 puts_filtered ("\n");
1515 if (bp
< buf
+ PBUFSIZ
- 1)
1523 puts_filtered ("Remote packet too long: ");
1524 puts_filtered (buf
);
1525 puts_filtered ("\n");
1532 /* Read a packet from the remote machine, with error checking,
1533 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1534 If FOREVER, wait forever rather than timing out; this is used
1535 while the target is executing user code. */
1538 getpkt (buf
, forever
)
1547 strcpy (buf
,"timeout");
1551 #ifdef MAINTENANCE_CMDS
1552 timeout
= watchdog
> 0 ? watchdog
: -1;
1559 timeout
= remote_timeout
;
1563 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
1565 /* This can loop forever if the remote side sends us characters
1566 continuously, but if it pauses, we'll get a zero from readchar
1567 because of timeout. Then we'll count that as a retry. */
1569 /* Note that we will only wait forever prior to the start of a packet.
1570 After that, we expect characters to arrive at a brisk pace. They
1571 should show up within remote_timeout intervals. */
1575 c
= readchar (timeout
);
1577 if (c
== SERIAL_TIMEOUT
)
1579 #ifdef MAINTENANCE_CMDS
1580 if (forever
) /* Watchdog went off. Kill the target. */
1582 target_mourn_inferior ();
1583 error ("Watchdog has expired. Target detached.\n");
1587 puts_filtered ("Timed out.\n");
1593 /* We've found the start of a packet, now collect the data. */
1595 val
= read_frame (buf
);
1600 fprintf_unfiltered (gdb_stderr
, "Packet received: %s\n", buf
);
1601 SERIAL_WRITE (remote_desc
, "+", 1);
1605 /* Try the whole thing again. */
1607 SERIAL_WRITE (remote_desc
, "-", 1);
1610 /* We have tried hard enough, and just can't receive the packet. Give up. */
1612 printf_unfiltered ("Ignoring packet error, continuing...\n");
1613 SERIAL_WRITE (remote_desc
, "+", 1);
1619 /* For some mysterious reason, wait_for_inferior calls kill instead of
1620 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1624 target_mourn_inferior ();
1628 /* Use catch_errors so the user can quit from gdb even when we aren't on
1629 speaking terms with the remote system. */
1630 catch_errors (putpkt
, "k", "", RETURN_MASK_ERROR
);
1632 /* Don't wait for it to die. I'm not really sure it matters whether
1633 we do or not. For the existing stubs, kill is a noop. */
1634 target_mourn_inferior ();
1640 remote_mourn_1 (&remote_ops
);
1644 extended_remote_mourn ()
1646 /* We do _not_ want to mourn the target like this; this will
1647 remove the extended remote target from the target stack,
1648 and the next time the user says "run" it'll fail.
1650 FIXME: What is the right thing to do here? */
1652 remote_mourn_1 (&extended_remote_ops
);
1656 /* Worker function for remote_mourn. */
1658 remote_mourn_1 (target
)
1659 struct target_ops
*target
;
1661 unpush_target (target
);
1662 generic_mourn_inferior ();
1665 /* In the extended protocol we want to be able to do things like
1666 "run" and have them basically work as expected. So we need
1667 a special create_inferior function.
1669 FIXME: One day add support for changing the exec file
1670 we're debugging, arguments and an environment. */
1673 extended_remote_create_inferior (exec_file
, args
, env
)
1678 /* Rip out the breakpoints; we'll reinsert them after restarting
1679 the remote server. */
1680 remove_breakpoints ();
1682 /* Now restart the remote server. */
1683 extended_remote_restart ();
1685 /* Now put the breakpoints back in. This way we're safe if the
1686 restart function works via a unix fork on the remote side. */
1687 insert_breakpoints ();
1689 /* Clean up from the last time we were running. */
1690 clear_proceed_status ();
1692 /* Let the remote process run. */
1693 proceed (-1, TARGET_SIGNAL_0
, 0);
1697 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1698 than other targets; in those use REMOTE_BREAKPOINT instead of just
1699 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1700 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1701 the standard routines that are in mem-break.c. */
1703 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1704 the choice of breakpoint instruction affects target program design and
1705 vice versa, and by making it user-tweakable, the special code here
1706 goes away and we need fewer special GDB configurations. */
1708 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1709 #define REMOTE_BREAKPOINT
1712 #ifdef REMOTE_BREAKPOINT
1714 /* If the target isn't bi-endian, just pretend it is. */
1715 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1716 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1717 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1720 static unsigned char big_break_insn
[] = BIG_REMOTE_BREAKPOINT
;
1721 static unsigned char little_break_insn
[] = LITTLE_REMOTE_BREAKPOINT
;
1723 #endif /* REMOTE_BREAKPOINT */
1725 /* Insert a breakpoint on targets that don't have any better breakpoint
1726 support. We read the contents of the target location and stash it,
1727 then overwrite it with a breakpoint instruction. ADDR is the target
1728 location in the target machine. CONTENTS_CACHE is a pointer to
1729 memory allocated for saving the target contents. It is guaranteed
1730 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1731 is accomplished via BREAKPOINT_MAX). */
1734 remote_insert_breakpoint (addr
, contents_cache
)
1736 char *contents_cache
;
1738 #ifdef REMOTE_BREAKPOINT
1741 val
= target_read_memory (addr
, contents_cache
, sizeof big_break_insn
);
1745 if (TARGET_BYTE_ORDER
== BIG_ENDIAN
)
1746 val
= target_write_memory (addr
, (char *) big_break_insn
,
1747 sizeof big_break_insn
);
1749 val
= target_write_memory (addr
, (char *) little_break_insn
,
1750 sizeof little_break_insn
);
1755 return memory_insert_breakpoint (addr
, contents_cache
);
1756 #endif /* REMOTE_BREAKPOINT */
1760 remote_remove_breakpoint (addr
, contents_cache
)
1762 char *contents_cache
;
1764 #ifdef REMOTE_BREAKPOINT
1765 return target_write_memory (addr
, contents_cache
, sizeof big_break_insn
);
1767 return memory_remove_breakpoint (addr
, contents_cache
);
1768 #endif /* REMOTE_BREAKPOINT */
1771 /* Define the target subroutine names */
1773 struct target_ops remote_ops
= {
1774 "remote", /* to_shortname */
1775 "Remote serial target in gdb-specific protocol", /* to_longname */
1776 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1777 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1778 remote_open
, /* to_open */
1779 remote_close
, /* to_close */
1780 NULL
, /* to_attach */
1781 remote_detach
, /* to_detach */
1782 remote_resume
, /* to_resume */
1783 remote_wait
, /* to_wait */
1784 remote_fetch_registers
, /* to_fetch_registers */
1785 remote_store_registers
, /* to_store_registers */
1786 remote_prepare_to_store
, /* to_prepare_to_store */
1787 remote_xfer_memory
, /* to_xfer_memory */
1788 remote_files_info
, /* to_files_info */
1789 remote_insert_breakpoint
, /* to_insert_breakpoint */
1790 remote_remove_breakpoint
, /* to_remove_breakpoint */
1791 NULL
, /* to_terminal_init */
1792 NULL
, /* to_terminal_inferior */
1793 NULL
, /* to_terminal_ours_for_output */
1794 NULL
, /* to_terminal_ours */
1795 NULL
, /* to_terminal_info */
1796 remote_kill
, /* to_kill */
1797 generic_load
, /* to_load */
1798 NULL
, /* to_lookup_symbol */
1799 NULL
, /* to_create_inferior */
1800 remote_mourn
, /* to_mourn_inferior */
1802 0, /* to_notice_signals */
1803 remote_thread_alive
, /* to_thread_alive */
1805 process_stratum
, /* to_stratum */
1807 1, /* to_has_all_memory */
1808 1, /* to_has_memory */
1809 1, /* to_has_stack */
1810 1, /* to_has_registers */
1811 1, /* to_has_execution */
1812 NULL
, /* sections */
1813 NULL
, /* sections_end */
1814 OPS_MAGIC
/* to_magic */
1817 struct target_ops extended_remote_ops
= {
1818 "extended-remote", /* to_shortname */
1819 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1820 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1821 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1822 extended_remote_open
, /* to_open */
1823 remote_close
, /* to_close */
1824 NULL
, /* to_attach */
1825 remote_detach
, /* to_detach */
1826 remote_resume
, /* to_resume */
1827 remote_wait
, /* to_wait */
1828 remote_fetch_registers
, /* to_fetch_registers */
1829 remote_store_registers
, /* to_store_registers */
1830 remote_prepare_to_store
, /* to_prepare_to_store */
1831 remote_xfer_memory
, /* to_xfer_memory */
1832 remote_files_info
, /* to_files_info */
1834 remote_insert_breakpoint
, /* to_insert_breakpoint */
1835 remote_remove_breakpoint
, /* to_remove_breakpoint */
1837 NULL
, /* to_terminal_init */
1838 NULL
, /* to_terminal_inferior */
1839 NULL
, /* to_terminal_ours_for_output */
1840 NULL
, /* to_terminal_ours */
1841 NULL
, /* to_terminal_info */
1842 remote_kill
, /* to_kill */
1843 generic_load
, /* to_load */
1844 NULL
, /* to_lookup_symbol */
1845 extended_remote_create_inferior
,/* to_create_inferior */
1846 extended_remote_mourn
, /* to_mourn_inferior */
1848 0, /* to_notice_signals */
1849 remote_thread_alive
, /* to_thread_alive */
1851 process_stratum
, /* to_stratum */
1853 1, /* to_has_all_memory */
1854 1, /* to_has_memory */
1855 1, /* to_has_stack */
1856 1, /* to_has_registers */
1857 1, /* to_has_execution */
1858 NULL
, /* sections */
1859 NULL
, /* sections_end */
1860 OPS_MAGIC
/* to_magic */
1864 _initialize_remote ()
1866 add_target (&remote_ops
);
1867 add_target (&extended_remote_ops
);
1869 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1870 var_integer
, (char *)&remote_timeout
,
1871 "Set timeout value for remote read.\n", &setlist
),
1874 add_show_from_set (add_set_cmd ("remotebreak", no_class
,
1875 var_integer
, (char *)&remote_break
,
1876 "Set whether to send break if interrupted.\n", &setlist
),
1879 add_show_from_set (add_set_cmd ("remotewritesize", no_class
,
1880 var_integer
, (char *)&remote_write_size
,
1881 "Set the maximum number of bytes in each memory write packet.\n", &setlist
),