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"
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 static int remote_timeout
= 2;
279 /* This variable chooses whether to send a ^C or a break when the user
280 requests program interruption. Although ^C is usually what remote
281 systems expect, and that is the default here, sometimes a break is
282 preferable instead. */
284 static int remote_break
;
286 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
287 remote_open knows that we don't have a file open when the program
289 serial_t remote_desc
= NULL
;
291 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
292 and i386-stub.c. Normally, no one would notice because it only matters
293 for writing large chunks of memory (e.g. in downloads). Also, this needs
294 to be more than 400 if required to hold the registers (see below, where
295 we round it up based on REGISTER_BYTES). */
298 /* Maximum number of bytes to read/write at once. The value here
299 is chosen to fill up a packet (the headers account for the 32). */
300 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
302 /* Round up PBUFSIZ to hold all the registers, at least. */
303 /* The blank line after the #if seems to be required to work around a
304 bug in HP's PA compiler. */
305 #if REGISTER_BYTES > MAXBUFBYTES
308 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
311 /* Should we try the 'P' request? If this is set to one when the stub
312 doesn't support 'P', the only consequence is some unnecessary traffic. */
313 static int stub_supports_P
= 1;
316 /* These are the threads which we last sent to the remote system. -1 for all
317 or -2 for not sent yet. */
327 int state
= gen
? general_thread
: cont_thread
;
331 buf
[1] = gen
? 'g' : 'c';
338 sprintf (&buf
[2], "-%x", -th
);
340 sprintf (&buf
[2], "%x", th
);
349 /* Return nonzero if the thread TH is still alive on the remote system. */
352 remote_thread_alive (th
)
359 sprintf (&buf
[1], "-%x", -th
);
361 sprintf (&buf
[1], "%x", th
);
364 return (buf
[0] == 'O' && buf
[1] == 'K');
367 /* Restart the remote side; this is an extended protocol operation. */
370 extended_remote_restart ()
374 /* Send the restart command; for reasons I don't understand the
375 remote side really expects a number after the "R". */
377 sprintf (&buf
[1], "%x", 0);
380 /* Now query for status so this looks just like we restarted
381 gdbserver from scratch. */
386 /* Clean up connection to a remote debugger. */
390 remote_close (quitting
)
394 SERIAL_CLOSE (remote_desc
);
398 /* Query the remote side for the text, data and bss offsets. */
405 CORE_ADDR text_addr
, data_addr
, bss_addr
;
406 struct section_offsets
*offs
;
412 if (buf
[0] == '\000')
413 return; /* Return silently. Stub doesn't support this
417 warning ("Remote failure reply: %s", buf
);
421 nvals
= sscanf (buf
, "Text=%lx;Data=%lx;Bss=%lx", &text_addr
, &data_addr
,
424 error ("Malformed response to offset query, %s", buf
);
426 if (symfile_objfile
== NULL
)
429 offs
= (struct section_offsets
*) alloca (sizeof (struct section_offsets
)
430 + symfile_objfile
->num_sections
431 * sizeof (offs
->offsets
));
432 memcpy (offs
, symfile_objfile
->section_offsets
,
433 sizeof (struct section_offsets
)
434 + symfile_objfile
->num_sections
435 * sizeof (offs
->offsets
));
437 ANOFFSET (offs
, SECT_OFF_TEXT
) = text_addr
;
439 /* This is a temporary kludge to force data and bss to use the same offsets
440 because that's what nlmconv does now. The real solution requires changes
441 to the stub and remote.c that I don't have time to do right now. */
443 ANOFFSET (offs
, SECT_OFF_DATA
) = data_addr
;
444 ANOFFSET (offs
, SECT_OFF_BSS
) = data_addr
;
446 objfile_relocate (symfile_objfile
, offs
);
449 /* Stub for catch_errors. */
452 remote_start_remote (dummy
)
455 immediate_quit
= 1; /* Allow user to interrupt it */
457 /* Ack any packet which the remote side has already sent. */
458 SERIAL_WRITE (remote_desc
, "+", 1);
460 /* Let the stub know that we want it to return the thread. */
463 get_offsets (); /* Get text, data & bss offsets */
465 putpkt ("?"); /* initiate a query from remote machine */
468 start_remote (); /* Initialize gdb process mechanisms */
472 /* Open a connection to a remote debugger.
473 NAME is the filename used for communication. */
476 remote_open (name
, from_tty
)
480 remote_open_1 (name
, from_tty
, &remote_ops
);
483 /* Open a connection to a remote debugger using the extended
484 remote gdb protocol. NAME is the filename used for communication. */
487 extended_remote_open (name
, from_tty
)
493 /* Do the basic remote open stuff. */
494 remote_open_1 (name
, from_tty
, &extended_remote_ops
);
496 /* Now tell the remote that we're using the extended protocol. */
502 /* Generic code for opening a connection to a remote target. */
503 static DCACHE
*remote_dcache
;
506 remote_open_1 (name
, from_tty
, target
)
509 struct target_ops
*target
;
512 error ("To open a remote debug connection, you need to specify what serial\n\
513 device is attached to the remote system (e.g. /dev/ttya).");
515 target_preopen (from_tty
);
517 unpush_target (target
);
519 remote_dcache
= dcache_init (remote_read_bytes
, remote_write_bytes
);
521 remote_desc
= SERIAL_OPEN (name
);
523 perror_with_name (name
);
527 if (SERIAL_SETBAUDRATE (remote_desc
, baud_rate
))
529 SERIAL_CLOSE (remote_desc
);
530 perror_with_name (name
);
535 SERIAL_RAW (remote_desc
);
537 /* If there is something sitting in the buffer we might take it as a
538 response to a command, which would be bad. */
539 SERIAL_FLUSH_INPUT (remote_desc
);
543 puts_filtered ("Remote debugging using ");
544 puts_filtered (name
);
545 puts_filtered ("\n");
547 push_target (target
); /* Switch to using remote target now */
549 /* Start out by trying the 'P' request to set registers. We set this each
550 time that we open a new target so that if the user switches from one
551 stub to another, we can (if the target is closed and reopened) cope. */
557 /* Without this, some commands which require an active target (such as kill)
558 won't work. This variable serves (at least) double duty as both the pid
559 of the target process (if it has such), and as a flag indicating that a
560 target is active. These functions should be split out into seperate
561 variables, especially since GDB will someday have a notion of debugging
562 several processes. */
564 inferior_pid
= 42000;
565 /* Start the remote connection; if error (0), discard this target.
566 In particular, if the user quits, be sure to discard it
567 (we'd be in an inconsistent state otherwise). */
568 if (!catch_errors (remote_start_remote
, (char *)0,
569 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL
))
573 /* This takes a program previously attached to and detaches it. After
574 this is done, GDB can be used to debug some other program. We
575 better not have left any breakpoints in the target program or it'll
576 die when it hits one. */
579 remote_detach (args
, from_tty
)
586 error ("Argument given to \"detach\" when remotely debugging.");
588 /* Tell the remote target to detach. */
594 puts_filtered ("Ending remote debugging.\n");
597 /* Convert hex digit A to a number. */
603 if (a
>= '0' && a
<= '9')
605 else if (a
>= 'a' && a
<= 'f')
608 error ("Reply contains invalid hex digit %d", a
);
611 /* Convert number NIB to a hex digit. */
623 /* Tell the remote machine to resume. */
625 static enum target_signal last_sent_signal
= TARGET_SIGNAL_0
;
629 remote_resume (pid
, step
, siggnal
)
631 enum target_signal siggnal
;
636 set_thread (inferior_pid
, 0);
640 dcache_flush (remote_dcache
);
642 last_sent_signal
= siggnal
;
643 last_sent_step
= step
;
645 if (siggnal
!= TARGET_SIGNAL_0
)
647 buf
[0] = step
? 'S' : 'C';
648 buf
[1] = tohex (((int)siggnal
>> 4) & 0xf);
649 buf
[2] = tohex ((int)siggnal
& 0xf);
653 strcpy (buf
, step
? "s": "c");
658 /* Send ^C to target to halt it. Target will respond, and send us a
662 remote_interrupt (signo
)
665 /* If this doesn't work, try more severe steps. */
666 signal (signo
, remote_interrupt_twice
);
669 printf_unfiltered ("remote_interrupt called\n");
671 /* Send a break or a ^C, depending on user preference. */
673 SERIAL_SEND_BREAK (remote_desc
);
675 SERIAL_WRITE (remote_desc
, "\003", 1);
678 static void (*ofunc
)();
680 /* The user typed ^C twice. */
682 remote_interrupt_twice (signo
)
685 signal (signo
, ofunc
);
689 signal (signo
, remote_interrupt
);
692 /* Ask the user what to do when an interrupt is received. */
697 target_terminal_ours ();
699 if (query ("Interrupted while waiting for the program.\n\
700 Give up (and stop debugging it)? "))
702 target_mourn_inferior ();
703 return_to_top_level (RETURN_QUIT
);
706 target_terminal_inferior ();
709 /* If nonzero, ignore the next kill. */
712 /* Wait until the remote machine stops, then return,
713 storing status in STATUS just as `wait' would.
714 Returns "pid" (though it's not clear what, if anything, that
715 means in the case of this target). */
718 remote_wait (pid
, status
)
720 struct target_waitstatus
*status
;
722 unsigned char buf
[PBUFSIZ
];
725 status
->kind
= TARGET_WAITKIND_EXITED
;
726 status
->value
.integer
= 0;
732 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
733 getpkt ((char *) buf
, 1);
734 signal (SIGINT
, ofunc
);
738 case 'E': /* Error of some sort */
739 warning ("Remote failure reply: %s", buf
);
741 case 'T': /* Status with PC, SP, FP, ... */
745 char regs
[MAX_REGISTER_RAW_SIZE
];
747 /* Expedited reply, containing Signal, {regno, reg} repeat */
748 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
750 n... = register number
751 r... = register contents
754 p
= &buf
[3]; /* after Txx */
761 regno
= strtol ((const char *) p
, &p_temp
, 16); /* Read the register number */
762 p1
= (unsigned char *)p_temp
;
766 p1
= (unsigned char *) strchr ((const char *) p
, ':');
768 warning ("Malformed packet (missing colon): %s\n\
771 if (strncmp ((const char *) p
, "thread", p1
- p
) == 0)
773 thread_num
= strtol ((const char *) ++p1
, &p_temp
, 16);
774 p
= (unsigned char *)p_temp
;
782 warning ("Malformed packet (missing colon): %s\n\
786 if (regno
>= NUM_REGS
)
787 warning ("Remote sent bad register number %ld: %s\n\
791 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
793 if (p
[0] == 0 || p
[1] == 0)
794 warning ("Remote reply is too short: %s", buf
);
795 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
798 supply_register (regno
, regs
);
802 warning ("Remote register badly formatted: %s", buf
);
806 case 'S': /* Old style status, just signal only */
807 status
->kind
= TARGET_WAITKIND_STOPPED
;
808 status
->value
.sig
= (enum target_signal
)
809 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
812 case 'W': /* Target exited */
814 /* The remote process exited. */
815 status
->kind
= TARGET_WAITKIND_EXITED
;
816 status
->value
.integer
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
820 status
->kind
= TARGET_WAITKIND_SIGNALLED
;
821 status
->value
.sig
= (enum target_signal
)
822 (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2])));
826 case 'O': /* Console output */
827 for (p
= buf
+ 1; *p
; p
+=2)
830 char c
= fromhex (p
[0]) * 16 + fromhex (p
[1]);
833 if (target_output_hook
)
834 target_output_hook (tb
);
836 fputs_filtered (tb
, gdb_stdout
);
840 if (last_sent_signal
!= TARGET_SIGNAL_0
)
842 /* Zero length reply means that we tried 'S' or 'C' and
843 the remote system doesn't support it. */
844 target_terminal_ours_for_output ();
846 ("Can't send signals to this remote system. %s not sent.\n",
847 target_signal_to_name (last_sent_signal
));
848 last_sent_signal
= TARGET_SIGNAL_0
;
849 target_terminal_inferior ();
851 strcpy ((char *) buf
, last_sent_step
? "s" : "c");
852 putpkt ((char *) buf
);
855 /* else fallthrough */
857 warning ("Invalid remote reply: %s", buf
);
862 if (thread_num
!= -1)
864 /* Initial thread value can only be acquired via wait, so deal with
865 this marker which is used before the first thread value is
867 if (inferior_pid
== 42000)
869 inferior_pid
= thread_num
;
870 add_thread (inferior_pid
);
877 /* Number of bytes of registers this stub implements. */
878 static int register_bytes_found
;
880 /* Read the remote registers into the block REGS. */
881 /* Currently we just read all the registers, so we don't use regno. */
884 remote_fetch_registers (regno
)
890 char regs
[REGISTER_BYTES
];
892 set_thread (inferior_pid
, 1);
897 /* Unimplemented registers read as all bits zero. */
898 memset (regs
, 0, REGISTER_BYTES
);
900 /* We can get out of synch in various cases. If the first character
901 in the buffer is not a hex character, assume that has happened
902 and try to fetch another packet to read. */
903 while ((buf
[0] < '0' || buf
[0] > '9')
904 && (buf
[0] < 'a' || buf
[0] > 'f'))
907 printf_unfiltered ("Bad register packet; fetching a new packet\n");
911 /* Reply describes registers byte by byte, each byte encoded as two
912 hex characters. Suck them all up, then supply them to the
913 register cacheing/storage mechanism. */
916 for (i
= 0; i
< REGISTER_BYTES
; i
++)
922 warning ("Remote reply is of odd length: %s", buf
);
923 /* Don't change register_bytes_found in this case, and don't
924 print a second warning. */
927 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
931 if (i
!= register_bytes_found
)
933 register_bytes_found
= i
;
934 #ifdef REGISTER_BYTES_OK
935 if (!REGISTER_BYTES_OK (i
))
936 warning ("Remote reply is too short: %s", buf
);
941 for (i
= 0; i
< NUM_REGS
; i
++)
942 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
945 /* Prepare to store registers. Since we may send them all (using a
946 'G' request), we have to read out the ones we don't want to change
950 remote_prepare_to_store ()
952 /* Make sure the entire registers array is valid. */
953 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
956 /* Store register REGNO, or all registers if REGNO == -1, from the contents
957 of REGISTERS. FIXME: ignores errors. */
960 remote_store_registers (regno
)
967 set_thread (inferior_pid
, 1);
969 if (regno
>= 0 && stub_supports_P
)
971 /* Try storing a single register. */
974 sprintf (buf
, "P%x=", regno
);
975 p
= buf
+ strlen (buf
);
976 regp
= ®isters
[REGISTER_BYTE (regno
)];
977 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); ++i
)
979 *p
++ = tohex ((regp
[i
] >> 4) & 0xf);
980 *p
++ = tohex (regp
[i
] & 0xf);
986 /* The stub understands the 'P' request. We are done. */
990 /* The stub does not support the 'P' request. Use 'G' instead,
991 and don't try using 'P' in the future (it will just waste our
998 /* Command describes registers byte by byte,
999 each byte encoded as two hex characters. */
1002 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1003 for (i
= 0; i
< register_bytes_found
; i
++)
1005 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
1006 *p
++ = tohex (registers
[i
] & 0xf);
1014 Use of the data cache *used* to be disabled because it loses for looking at
1015 and changing hardware I/O ports and the like. Accepting `volatile'
1016 would perhaps be one way to fix it. Another idea would be to use the
1017 executable file for the text segment (for all SEC_CODE sections?
1018 For all SEC_READONLY sections?). This has problems if you want to
1019 actually see what the memory contains (e.g. self-modifying code,
1020 clobbered memory, user downloaded the wrong thing).
1022 Because it speeds so much up, it's now enabled, if you're playing
1023 with registers you turn it of (set remotecache 0)
1026 /* Read a word from remote address ADDR and return it.
1027 This goes through the data cache. */
1031 remote_fetch_word (addr
)
1034 return dcache_fetch (remote_dcache
, addr
);
1037 /* Write a word WORD into remote address ADDR.
1038 This goes through the data cache. */
1041 remote_store_word (addr
, word
)
1045 dcache_poke (remote_dcache
, addr
, word
);
1047 #endif /* 0 (unused?) */
1050 /* Write memory data directly to the remote machine.
1051 This does not inform the data cache; the data cache uses this.
1052 MEMADDR is the address in the remote memory space.
1053 MYADDR is the address of the buffer in our space.
1054 LEN is the number of bytes.
1056 Returns number of bytes transferred, or 0 for error. */
1059 remote_write_bytes (memaddr
, myaddr
, len
)
1068 /* Chop the transfer down if necessary */
1073 int todo
= len
- done
;
1074 int cando
= PBUFSIZ
/2 - 32; /* number of bytes that will fit. */
1078 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1079 result in a buffer like sprintf. */
1080 sprintf (buf
, "M%lx,%x:", (unsigned long) memaddr
+ done
, todo
);
1082 /* We send target system values byte by byte, in increasing byte addresses,
1083 each byte encoded as two hex characters. */
1085 p
= buf
+ strlen (buf
);
1086 for (i
= 0; i
< todo
; i
++)
1088 *p
++ = tohex ((myaddr
[i
+ done
] >> 4) & 0xf);
1089 *p
++ = tohex (myaddr
[i
+ done
] & 0xf);
1098 /* There is no correspondance between what the remote protocol uses
1099 for errors and errno codes. We would like a cleaner way of
1100 representing errors (big enough to include errno codes, bfd_error
1101 codes, and others). But for now just return EIO. */
1110 /* Read memory data directly from the remote machine.
1111 This does not use the data cache; the data cache uses this.
1112 MEMADDR is the address in the remote memory space.
1113 MYADDR is the address of the buffer in our space.
1114 LEN is the number of bytes.
1116 Returns number of bytes transferred, or 0 for error. */
1119 remote_read_bytes (memaddr
, myaddr
, len
)
1128 /* Chop transfer down if neccessary */
1131 /* FIXME: This is wrong for larger packets */
1132 if (len
> PBUFSIZ
/ 2 - 1)
1138 int todo
= len
- done
;
1139 int cando
= PBUFSIZ
/ 2 - 32; /* number of bytes that will fit. */
1143 /* FIXME-32x64: Need a version of print_address_numeric which puts the
1144 result in a buffer like sprintf. */
1145 sprintf (buf
, "m%lx,%x", (unsigned long) memaddr
+ done
, todo
);
1151 /* There is no correspondance between what the remote protocol uses
1152 for errors and errno codes. We would like a cleaner way of
1153 representing errors (big enough to include errno codes, bfd_error
1154 codes, and others). But for now just return EIO. */
1159 /* Reply describes memory byte by byte,
1160 each byte encoded as two hex characters. */
1163 for (i
= 0; i
< todo
; i
++)
1165 if (p
[0] == 0 || p
[1] == 0)
1166 /* Reply is short. This means that we were able to read only part
1167 of what we wanted to. */
1169 myaddr
[i
+ done
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
1177 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1178 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1179 nonzero. Returns length of data written or read; 0 for error. */
1183 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
1188 struct target_ops
*target
; /* ignored */
1190 return dcache_xfer_memory (remote_dcache
, memaddr
, myaddr
, len
, should_write
);
1195 /* Enable after 4.12. */
1198 remote_search (len
, data
, mask
, startaddr
, increment
, lorange
, hirange
1199 addr_found
, data_found
)
1203 CORE_ADDR startaddr
;
1207 CORE_ADDR
*addr_found
;
1210 if (increment
== -4 && len
== 4)
1212 long mask_long
, data_long
;
1213 long data_found_long
;
1214 CORE_ADDR addr_we_found
;
1216 long returned_long
[2];
1219 mask_long
= extract_unsigned_integer (mask
, len
);
1220 data_long
= extract_unsigned_integer (data
, len
);
1221 sprintf (buf
, "t%x:%x,%x", startaddr
, data_long
, mask_long
);
1226 /* The stub doesn't support the 't' request. We might want to
1227 remember this fact, but on the other hand the stub could be
1228 switched on us. Maybe we should remember it only until
1229 the next "target remote". */
1230 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1231 hirange
, addr_found
, data_found
);
1236 /* There is no correspondance between what the remote protocol uses
1237 for errors and errno codes. We would like a cleaner way of
1238 representing errors (big enough to include errno codes, bfd_error
1239 codes, and others). But for now just use EIO. */
1240 memory_error (EIO
, startaddr
);
1243 while (*p
!= '\0' && *p
!= ',')
1244 addr_we_found
= (addr_we_found
<< 4) + fromhex (*p
++);
1246 error ("Protocol error: short return for search");
1248 data_found_long
= 0;
1249 while (*p
!= '\0' && *p
!= ',')
1250 data_found_long
= (data_found_long
<< 4) + fromhex (*p
++);
1251 /* Ignore anything after this comma, for future extensions. */
1253 if (addr_we_found
< lorange
|| addr_we_found
>= hirange
)
1259 *addr_found
= addr_we_found
;
1260 *data_found
= store_unsigned_integer (data_we_found
, len
);
1263 generic_search (len
, data
, mask
, startaddr
, increment
, lorange
,
1264 hirange
, addr_found
, data_found
);
1269 remote_files_info (ignore
)
1270 struct target_ops
*ignore
;
1272 puts_filtered ("Debugging a target over a serial line.\n");
1275 /* Stuff for dealing with the packets which are part of this protocol.
1276 See comment at top of file for details. */
1278 /* Read a single character from the remote end, masking it down to 7 bits. */
1286 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
1291 error ("Remote connection closed");
1293 perror_with_name ("Remote communication error");
1294 case SERIAL_TIMEOUT
:
1301 /* Send the command in BUF to the remote machine,
1302 and read the reply into BUF.
1303 Report an error if we get an error reply. */
1313 error ("Remote failure reply: %s", buf
);
1316 /* Send a packet to the remote machine, with error checking.
1317 The data of the packet is in BUF. */
1324 unsigned char csum
= 0;
1326 int cnt
= strlen (buf
);
1331 /* Copy the packet into buffer BUF2, encapsulating it
1332 and giving it a checksum. */
1334 if (cnt
> (int) sizeof (buf2
) - 5) /* Prosanity check */
1340 for (i
= 0; i
< cnt
; i
++)
1346 *p
++ = tohex ((csum
>> 4) & 0xf);
1347 *p
++ = tohex (csum
& 0xf);
1349 /* Send it over and over until we get a positive ack. */
1353 int started_error_output
= 0;
1358 printf_unfiltered ("Sending packet: %s...", buf2
);
1359 gdb_flush(gdb_stdout
);
1361 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
1362 perror_with_name ("putpkt: write failed");
1364 /* read until either a timeout occurs (-2) or '+' is read */
1367 ch
= readchar (remote_timeout
);
1374 case SERIAL_TIMEOUT
:
1376 if (started_error_output
)
1378 putchar_unfiltered ('\n');
1379 started_error_output
= 0;
1388 printf_unfiltered("Ack\n");
1390 case SERIAL_TIMEOUT
:
1394 break; /* Retransmit buffer */
1397 char junkbuf
[PBUFSIZ
];
1399 /* It's probably an old response, and we're out of sync. Just
1400 gobble up the packet and ignore it. */
1401 getpkt (junkbuf
, 0);
1402 continue; /* Now, go look for + */
1407 if (!started_error_output
)
1409 started_error_output
= 1;
1410 printf_unfiltered ("putpkt: Junk: ");
1412 putchar_unfiltered (ch
& 0177);
1416 break; /* Here to retransmit */
1420 /* This is wrong. If doing a long backtrace, the user should be
1421 able to get out next time we call QUIT, without anything as violent
1422 as interrupt_query. If we want to provide a way out of here
1423 without getting to the next QUIT, it should be based on hitting
1424 ^C twice as in remote_wait. */
1434 /* Come here after finding the start of the frame. Collect the rest into BUF,
1435 verifying the checksum, length, and handling run-length compression.
1436 Returns 0 on any error, 1 on success. */
1451 c
= readchar (remote_timeout
);
1455 case SERIAL_TIMEOUT
:
1457 puts_filtered ("Timeout in mid-packet, retrying\n");
1461 puts_filtered ("Saw new packet start in middle of old one\n");
1462 return 0; /* Start a new packet, count retries */
1465 unsigned char pktcsum
;
1469 pktcsum
= fromhex (readchar (remote_timeout
)) << 4;
1470 pktcsum
|= fromhex (readchar (remote_timeout
));
1472 if (csum
== pktcsum
)
1477 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1479 puts_filtered (buf
);
1480 puts_filtered ("\n");
1484 case '*': /* Run length encoding */
1486 c
= readchar (remote_timeout
);
1488 c
= c
- ' ' + 3; /* Compute repeat count */
1491 if (c
> 0 && c
< 255 && bp
+ c
- 1 < buf
+ PBUFSIZ
- 1)
1493 memset (bp
, *(bp
- 1), c
);
1499 printf_filtered ("Repeat count %d too large for buffer: ", c
);
1500 puts_filtered (buf
);
1501 puts_filtered ("\n");
1505 if (bp
< buf
+ PBUFSIZ
- 1)
1513 puts_filtered ("Remote packet too long: ");
1514 puts_filtered (buf
);
1515 puts_filtered ("\n");
1522 /* Read a packet from the remote machine, with error checking,
1523 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1524 If FOREVER, wait forever rather than timing out; this is used
1525 while the target is executing user code. */
1528 getpkt (buf
, forever
)
1537 strcpy (buf
,"timeout");
1541 #ifdef MAINTENANCE_CMDS
1542 timeout
= watchdog
> 0 ? watchdog
: -1;
1549 timeout
= remote_timeout
;
1553 for (tries
= 1; tries
<= MAX_TRIES
; tries
++)
1555 /* This can loop forever if the remote side sends us characters
1556 continuously, but if it pauses, we'll get a zero from readchar
1557 because of timeout. Then we'll count that as a retry. */
1559 /* Note that we will only wait forever prior to the start of a packet.
1560 After that, we expect characters to arrive at a brisk pace. They
1561 should show up within remote_timeout intervals. */
1565 c
= readchar (timeout
);
1567 if (c
== SERIAL_TIMEOUT
)
1569 #ifdef MAINTENANCE_CMDS
1570 if (forever
) /* Watchdog went off. Kill the target. */
1572 target_mourn_inferior ();
1573 error ("Watchdog has expired. Target detached.\n");
1577 puts_filtered ("Timed out.\n");
1583 /* We've found the start of a packet, now collect the data. */
1585 val
= read_frame (buf
);
1590 fprintf_unfiltered (gdb_stderr
, "Packet received: %s\n", buf
);
1591 SERIAL_WRITE (remote_desc
, "+", 1);
1595 /* Try the whole thing again. */
1597 SERIAL_WRITE (remote_desc
, "-", 1);
1600 /* We have tried hard enough, and just can't receive the packet. Give up. */
1602 printf_unfiltered ("Ignoring packet error, continuing...\n");
1603 SERIAL_WRITE (remote_desc
, "+", 1);
1609 /* For some mysterious reason, wait_for_inferior calls kill instead of
1610 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1614 target_mourn_inferior ();
1618 /* Use catch_errors so the user can quit from gdb even when we aren't on
1619 speaking terms with the remote system. */
1620 catch_errors (putpkt
, "k", "", RETURN_MASK_ERROR
);
1622 /* Don't wait for it to die. I'm not really sure it matters whether
1623 we do or not. For the existing stubs, kill is a noop. */
1624 target_mourn_inferior ();
1630 remote_mourn_1 (&remote_ops
);
1634 extended_remote_mourn ()
1636 /* We do _not_ want to mourn the target like this; this will
1637 remove the extended remote target from the target stack,
1638 and the next time the user says "run" it'll fail.
1640 FIXME: What is the right thing to do here? */
1642 remote_mourn_1 (&extended_remote_ops
);
1646 /* Worker function for remote_mourn. */
1648 remote_mourn_1 (target
)
1649 struct target_ops
*target
;
1651 unpush_target (target
);
1652 generic_mourn_inferior ();
1655 /* In the extended protocol we want to be able to do things like
1656 "run" and have them basically work as expected. So we need
1657 a special create_inferior function.
1659 FIXME: One day add support for changing the exec file
1660 we're debugging, arguments and an environment. */
1663 extended_remote_create_inferior (exec_file
, args
, env
)
1668 /* Rip out the breakpoints; we'll reinsert them after restarting
1669 the remote server. */
1670 remove_breakpoints ();
1672 /* Now restart the remote server. */
1673 extended_remote_restart ();
1675 /* Now put the breakpoints back in. This way we're safe if the
1676 restart function works via a unix fork on the remote side. */
1677 insert_breakpoints ();
1679 /* Clean up from the last time we were running. */
1680 clear_proceed_status ();
1682 /* Let the remote process run. */
1683 proceed (-1, TARGET_SIGNAL_0
, 0);
1687 #ifdef REMOTE_BREAKPOINT
1689 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1690 than other targets. */
1691 static unsigned char break_insn
[] = REMOTE_BREAKPOINT
;
1693 #else /* No REMOTE_BREAKPOINT. */
1695 /* Same old breakpoint instruction. This code does nothing different
1696 than mem-break.c. */
1697 static unsigned char break_insn
[] = BREAKPOINT
;
1699 #endif /* No REMOTE_BREAKPOINT. */
1701 /* Insert a breakpoint on targets that don't have any better breakpoint
1702 support. We read the contents of the target location and stash it,
1703 then overwrite it with a breakpoint instruction. ADDR is the target
1704 location in the target machine. CONTENTS_CACHE is a pointer to
1705 memory allocated for saving the target contents. It is guaranteed
1706 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1707 is accomplished via BREAKPOINT_MAX). */
1710 remote_insert_breakpoint (addr
, contents_cache
)
1712 char *contents_cache
;
1716 val
= target_read_memory (addr
, contents_cache
, sizeof break_insn
);
1719 val
= target_write_memory (addr
, (char *)break_insn
, sizeof break_insn
);
1725 remote_remove_breakpoint (addr
, contents_cache
)
1727 char *contents_cache
;
1729 return target_write_memory (addr
, contents_cache
, sizeof break_insn
);
1732 /* Define the target subroutine names */
1734 struct target_ops remote_ops
= {
1735 "remote", /* to_shortname */
1736 "Remote serial target in gdb-specific protocol", /* to_longname */
1737 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1738 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1739 remote_open
, /* to_open */
1740 remote_close
, /* to_close */
1741 NULL
, /* to_attach */
1742 remote_detach
, /* to_detach */
1743 remote_resume
, /* to_resume */
1744 remote_wait
, /* to_wait */
1745 remote_fetch_registers
, /* to_fetch_registers */
1746 remote_store_registers
, /* to_store_registers */
1747 remote_prepare_to_store
, /* to_prepare_to_store */
1748 remote_xfer_memory
, /* to_xfer_memory */
1749 remote_files_info
, /* to_files_info */
1750 remote_insert_breakpoint
, /* to_insert_breakpoint */
1751 remote_remove_breakpoint
, /* to_remove_breakpoint */
1752 NULL
, /* to_terminal_init */
1753 NULL
, /* to_terminal_inferior */
1754 NULL
, /* to_terminal_ours_for_output */
1755 NULL
, /* to_terminal_ours */
1756 NULL
, /* to_terminal_info */
1757 remote_kill
, /* to_kill */
1758 generic_load
, /* to_load */
1759 NULL
, /* to_lookup_symbol */
1760 NULL
, /* to_create_inferior */
1761 remote_mourn
, /* to_mourn_inferior */
1763 0, /* to_notice_signals */
1764 remote_thread_alive
, /* to_thread_alive */
1766 process_stratum
, /* to_stratum */
1768 1, /* to_has_all_memory */
1769 1, /* to_has_memory */
1770 1, /* to_has_stack */
1771 1, /* to_has_registers */
1772 1, /* to_has_execution */
1773 NULL
, /* sections */
1774 NULL
, /* sections_end */
1775 OPS_MAGIC
/* to_magic */
1778 struct target_ops extended_remote_ops
= {
1779 "extended-remote", /* to_shortname */
1780 "Extended remote serial target in gdb-specific protocol",/* to_longname */
1781 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1782 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1783 extended_remote_open
, /* to_open */
1784 remote_close
, /* to_close */
1785 NULL
, /* to_attach */
1786 remote_detach
, /* to_detach */
1787 remote_resume
, /* to_resume */
1788 remote_wait
, /* to_wait */
1789 remote_fetch_registers
, /* to_fetch_registers */
1790 remote_store_registers
, /* to_store_registers */
1791 remote_prepare_to_store
, /* to_prepare_to_store */
1792 remote_xfer_memory
, /* to_xfer_memory */
1793 remote_files_info
, /* to_files_info */
1795 remote_insert_breakpoint
, /* to_insert_breakpoint */
1796 remote_remove_breakpoint
, /* to_remove_breakpoint */
1798 NULL
, /* to_terminal_init */
1799 NULL
, /* to_terminal_inferior */
1800 NULL
, /* to_terminal_ours_for_output */
1801 NULL
, /* to_terminal_ours */
1802 NULL
, /* to_terminal_info */
1803 remote_kill
, /* to_kill */
1804 generic_load
, /* to_load */
1805 NULL
, /* to_lookup_symbol */
1806 extended_remote_create_inferior
,/* to_create_inferior */
1807 extended_remote_mourn
, /* to_mourn_inferior */
1809 0, /* to_notice_signals */
1810 remote_thread_alive
, /* to_thread_alive */
1812 process_stratum
, /* to_stratum */
1814 1, /* to_has_all_memory */
1815 1, /* to_has_memory */
1816 1, /* to_has_stack */
1817 1, /* to_has_registers */
1818 1, /* to_has_execution */
1819 NULL
, /* sections */
1820 NULL
, /* sections_end */
1821 OPS_MAGIC
/* to_magic */
1825 _initialize_remote ()
1827 add_target (&remote_ops
);
1828 add_target (&extended_remote_ops
);
1830 add_show_from_set (add_set_cmd ("remotetimeout", no_class
,
1831 var_integer
, (char *)&remote_timeout
,
1832 "Set timeout value for remote read.\n", &setlist
),
1835 add_show_from_set (add_set_cmd ("remotebreak", no_class
,
1836 var_integer
, (char *)&remote_break
,
1837 "Set whether to send break if interrupted.\n", &setlist
),