1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, 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
30 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
31 checksum of <data>, the most significant nibble is sent first.
32 the hex digits 0-9,a-f are used.
34 Receiver responds with:
36 + - if CSUM is correct and ready for next packet
37 - - if CSUM is incorrect
40 All values are encoded in ascii hex digits.
45 reply XX....X Each byte of register data
46 is described by two hex digits.
47 Registers are in the internal order
48 for GDB, and the bytes in a register
49 are in the same order the machine uses.
52 write regs GXX..XX Each byte of register data
53 is described by two hex digits.
57 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
58 reply XX..XX XX..XX is mem contents
59 Can be fewer bytes than requested
60 if able to read only part of the data.
63 write mem MAA..AA,LLLL:XX..XX
65 LLLL is number of bytes,
68 ENN for an error (this includes the case
69 where only part of the data was
72 cont cAA..AA AA..AA is address to resume
74 resume at same address.
76 step sAA..AA AA..AA is address to resume
78 resume at same address.
80 last signal ? Reply the current reason for stopping.
81 This is the same reply as is generated
82 for step or cont : SAA where AA is the
85 There is no immediate reply to step or cont.
86 The reply comes when the machine stops.
87 It is SAA AA is the "signal number"
89 or... TAAn...:r...;n:r...;n...:r...;
91 n... = register number
92 r... = register contents
96 toggle debug d toggle debug flag (see 386 & 68k stubs)
97 reset r reset -- see sparc stub.
98 reserved <other> On other requests, the stub should
99 ignore the request and send an empty
100 response ($#<checksum>). This way
101 we can extend the protocol and GDB
102 can tell whether the stub it is
103 talking to uses the old or the new.
110 #include "inferior.h"
115 #include "terminal.h"
120 #if !defined(DONT_USE_REMOTE)
122 #include <sys/types.h>
128 /* Prototypes for local functions */
131 remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
134 remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
137 remote_files_info PARAMS ((struct target_ops *ignore));
140 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
141 int should_write, struct target_ops *target));
144 remote_prepare_to_store PARAMS ((void));
147 remote_fetch_registers PARAMS ((int regno));
150 remote_resume PARAMS ((int pid, int step, int siggnal));
153 remote_start_remote PARAMS ((char *dummy));
156 remote_open PARAMS ((char *name, int from_tty));
159 remote_close PARAMS ((int quitting));
162 remote_store_registers PARAMS ((int regno));
165 getpkt PARAMS ((char *buf, int forever));
168 putpkt PARAMS ((char *buf));
171 remote_send PARAMS ((char *buf));
174 readchar PARAMS ((void));
177 remote_wait PARAMS ((WAITTYPE *status));
180 tohex PARAMS ((int nib));
183 fromhex PARAMS ((int a));
186 remote_detach PARAMS ((char *args, int from_tty));
189 remote_interrupt PARAMS ((int signo));
192 remote_interrupt_twice PARAMS ((int signo));
194 extern struct target_ops remote_ops; /* Forward decl */
196 /* This was 5 seconds, which is a long time to sit and wait.
197 Unless this is going though some terminal server or multiplexer or
198 other form of hairy serial connection, I would think 2 seconds would
200 static int timeout = 2;
206 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
207 remote_open knows that we don't have a file open when the program
209 serial_t remote_desc = NULL;
213 /* Maximum number of bytes to read/write at once. The value here
214 is chosen to fill up a packet (the headers account for the 32). */
215 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
217 /* Round up PBUFSIZ to hold all the registers, at least. */
218 #if REGISTER_BYTES > MAXBUFBYTES
220 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
223 /* Clean up connection to a remote debugger. */
227 remote_close (quitting)
231 SERIAL_CLOSE (remote_desc);
235 /* Stub for catch_errors. */
238 remote_start_remote (dummy)
241 /* Ack any packet which the remote side has already sent. */
242 /* I'm not sure this \r is needed; we don't use it any other time we
244 SERIAL_WRITE (remote_desc, "+\r", 2);
245 putpkt ("?"); /* initiate a query from remote machine */
247 start_remote (); /* Initialize gdb process mechanisms */
251 /* Open a connection to a remote debugger.
252 NAME is the filename used for communication. */
254 static DCACHE *remote_dcache;
257 remote_open (name, from_tty)
263 "To open a remote debug connection, you need to specify what serial\n\
264 device is attached to the remote system (e.g. /dev/ttya).");
266 target_preopen (from_tty);
268 unpush_target (&remote_ops);
270 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
272 remote_desc = SERIAL_OPEN (name);
274 perror_with_name (name);
280 if (sscanf (baud_rate, "%d", &rate) == 1)
281 if (SERIAL_SETBAUDRATE (remote_desc, rate))
283 SERIAL_CLOSE (remote_desc);
284 perror_with_name (name);
288 SERIAL_RAW (remote_desc);
292 puts_filtered ("Remote debugging using ");
293 puts_filtered (name);
294 puts_filtered ("\n");
296 push_target (&remote_ops); /* Switch to using remote target now */
298 /* Start the remote connection; if error (0), discard this target. */
299 immediate_quit++; /* Allow user to interrupt it */
300 if (!catch_errors (remote_start_remote, (char *)0,
301 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
306 takes a program previously attached to and detaches it.
307 We better not have left any breakpoints
308 in the program or it'll die when it hits one.
309 Close the open connection to the remote debugger.
310 Use this when you want to detach and do something else
314 remote_detach (args, from_tty)
319 error ("Argument given to \"detach\" when remotely debugging.");
323 puts_filtered ("Ending remote debugging.\n");
326 /* Convert hex digit A to a number. */
332 if (a >= '0' && a <= '9')
334 else if (a >= 'a' && a <= 'f')
337 error ("Reply contains invalid hex digit");
341 /* Convert number NIB to a hex digit. */
353 /* Tell the remote machine to resume. */
356 remote_resume (pid, step, siggnal)
357 int pid, step, siggnal;
364 target_terminal_ours_for_output ();
365 printf_filtered ("Can't send signals to a remote system. ");
366 name = strsigno (siggnal);
368 printf_filtered (name);
370 printf_filtered ("Signal %d", siggnal);
371 printf_filtered (" not sent.\n");
372 target_terminal_inferior ();
375 dcache_flush (remote_dcache);
377 strcpy (buf, step ? "s": "c");
382 /* Send ^C to target to halt it. Target will respond, and send us a
386 remote_interrupt (signo)
389 /* If this doesn't work, try more severe steps. */
390 signal (signo, remote_interrupt_twice);
393 printf ("remote_interrupt called\n");
395 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
398 static void (*ofunc)();
400 /* The user typed ^C twice. */
402 remote_interrupt_twice (signo)
405 signal (signo, ofunc);
407 target_terminal_ours ();
408 if (query ("Interrupted while waiting for the program.\n\
409 Give up (and stop debugging it)? "))
411 target_mourn_inferior ();
412 return_to_top_level (RETURN_QUIT);
416 signal (signo, remote_interrupt);
417 target_terminal_inferior ();
421 /* Wait until the remote machine stops, then return,
422 storing status in STATUS just as `wait' would.
423 Returns "pid" (though it's not clear what, if anything, that
424 means in the case of this target). */
430 unsigned char buf[PBUFSIZ];
434 char regs[MAX_REGISTER_RAW_SIZE];
436 WSETEXIT ((*status), 0);
438 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
439 getpkt ((char *) buf, 1);
440 signal (SIGINT, ofunc);
443 error ("Remote failure reply: %s", buf);
446 /* Expedited reply, containing Signal, {regno, reg} repeat */
447 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
449 n... = register number
450 r... = register contents
453 p = &buf[3]; /* after Txx */
459 regno = strtol (p, &p1, 16); /* Read the register number */
462 error ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
468 error ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
471 if (regno >= NUM_REGS)
472 error ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
475 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
477 if (p[0] == 0 || p[1] == 0)
478 error ("Remote reply is too short: %s", buf);
479 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
484 error("Remote register badly formatted: %s", buf);
486 supply_register (regno, regs);
489 else if (buf[0] != 'S')
490 error ("Invalid remote reply: %s", buf);
492 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
497 /* Read the remote registers into the block REGS. */
498 /* Currently we just read all the registers, so we don't use regno. */
501 remote_fetch_registers (regno)
507 char regs[REGISTER_BYTES];
512 /* Reply describes registers byte by byte, each byte encoded as two
513 hex characters. Suck them all up, then supply them to the
514 register cacheing/storage mechanism. */
517 for (i = 0; i < REGISTER_BYTES; i++)
519 if (p[0] == 0 || p[1] == 0)
520 error ("Remote reply is too short: %s", buf);
521 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
524 for (i = 0; i < NUM_REGS; i++)
525 supply_register (i, ®s[REGISTER_BYTE(i)]);
528 /* Prepare to store registers. Since we send them all, we have to
529 read out the ones we don't want to change first. */
532 remote_prepare_to_store ()
534 /* Make sure the entire registers array is valid. */
535 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
538 /* Store the remote registers from the contents of the block REGISTERS.
539 FIXME, eventually just store one register if that's all that is needed. */
543 remote_store_registers (regno)
552 /* Command describes registers byte by byte,
553 each byte encoded as two hex characters. */
556 for (i = 0; i < REGISTER_BYTES; i++)
558 *p++ = tohex ((registers[i] >> 4) & 0xf);
559 *p++ = tohex (registers[i] & 0xf);
568 /* Use of the data cache is disabled because it loses for looking at
569 and changing hardware I/O ports and the like. Accepting `volatile'
570 would perhaps be one way to fix it, but a better way which would
571 win for more cases would be to use the executable file for the text
572 segment, like the `icache' code below but done cleanly (in some
573 target-independent place, perhaps in target_xfer_memory, perhaps
574 based on assigning each target a speed or perhaps by some simpler
577 /* Read a word from remote address ADDR and return it.
578 This goes through the data cache. */
581 remote_fetch_word (addr)
587 extern CORE_ADDR text_start, text_end;
589 if (addr >= text_start && addr < text_end)
592 xfer_core_file (addr, &buffer, sizeof (int));
597 return dcache_fetch (remote_dcache, addr);
600 /* Write a word WORD into remote address ADDR.
601 This goes through the data cache. */
604 remote_store_word (addr, word)
608 dcache_poke (remote_dcache, addr, word);
612 /* Write memory data directly to the remote machine.
613 This does not inform the data cache; the data cache uses this.
614 MEMADDR is the address in the remote memory space.
615 MYADDR is the address of the buffer in our space.
616 LEN is the number of bytes.
618 Returns number of bytes transferred, or 0 for error. */
621 remote_write_bytes (memaddr, myaddr, len)
623 unsigned char *myaddr;
630 if (len > PBUFSIZ / 2 - 20)
633 sprintf (buf, "M%x,%x:", memaddr, len);
635 /* We send target system values byte by byte, in increasing byte addresses,
636 each byte encoded as two hex characters. */
638 p = buf + strlen (buf);
639 for (i = 0; i < len; i++)
641 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
642 *p++ = tohex (myaddr[i] & 0xf);
651 /* There is no correspondance between what the remote protocol uses
652 for errors and errno codes. We would like a cleaner way of
653 representing errors (big enough to include errno codes, bfd_error
654 codes, and others). But for now just return EIO. */
661 /* Read memory data directly from the remote machine.
662 This does not use the data cache; the data cache uses this.
663 MEMADDR is the address in the remote memory space.
664 MYADDR is the address of the buffer in our space.
665 LEN is the number of bytes.
667 Returns number of bytes transferred, or 0 for error. */
670 remote_read_bytes (memaddr, myaddr, len)
672 unsigned char *myaddr;
679 if (len > PBUFSIZ / 2 - 1)
682 sprintf (buf, "m%x,%x", memaddr, len);
688 /* There is no correspondance between what the remote protocol uses
689 for errors and errno codes. We would like a cleaner way of
690 representing errors (big enough to include errno codes, bfd_error
691 codes, and others). But for now just return EIO. */
696 /* Reply describes memory byte by byte,
697 each byte encoded as two hex characters. */
700 for (i = 0; i < len; i++)
702 if (p[0] == 0 || p[1] == 0)
703 /* Reply is short. This means that we were able to read only part
704 of what we wanted to. */
706 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
712 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
713 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
714 nonzero. Returns length of data written or read; 0 for error. */
718 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
723 struct target_ops *target; /* ignored */
727 int total_xferred = 0;
731 if (len > MAXBUFBYTES)
732 xfersize = MAXBUFBYTES;
737 bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
739 bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
741 /* If we get an error, we are done xferring. */
742 if (bytes_xferred == 0)
745 memaddr += bytes_xferred;
746 myaddr += bytes_xferred;
747 len -= bytes_xferred;
748 total_xferred += bytes_xferred;
750 return total_xferred;
754 remote_files_info (ignore)
755 struct target_ops *ignore;
757 puts_filtered ("Debugging a target over a serial line.\n");
760 /* Stuff for dealing with the packets which are part of this protocol.
761 See comment at top of file for details. */
763 /* Read a single character from the remote end, masking it down to 7 bits. */
770 ch = SERIAL_READCHAR (remote_desc, timeout);
778 /* Send the command in BUF to the remote machine,
779 and read the reply into BUF.
780 Report an error if we get an error reply. */
791 error ("Remote failure reply: %s", buf);
794 /* Send a packet to the remote machine, with error checking.
795 The data of the packet is in BUF. */
802 unsigned char csum = 0;
804 int cnt = strlen (buf);
808 /* Copy the packet into buffer BUF2, encapsulating it
809 and giving it a checksum. */
811 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
817 for (i = 0; i < cnt; i++)
823 *p++ = tohex ((csum >> 4) & 0xf);
824 *p++ = tohex (csum & 0xf);
826 /* Send it over and over until we get a positive ack. */
833 printf ("Sending packet: %s...", buf2); fflush(stdout);
835 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
836 perror_with_name ("putpkt: write failed");
838 /* read until either a timeout occurs (-2) or '+' is read */
850 break; /* Retransmit buffer */
852 perror_with_name ("putpkt: couldn't read ACK");
854 error ("putpkt: EOF while trying to read ACK");
857 printf ("%02X %c ", ch&0xFF, ch);
860 break; /* Here to retransmit */
865 /* Read a packet from the remote machine, with error checking,
866 and store it in BUF. BUF is expected to be of size PBUFSIZ.
867 If FOREVER, wait forever rather than timing out; this is used
868 while the target is executing user code. */
871 getpkt (buf, forever)
878 unsigned char c1, c2;
880 #define MAX_RETRIES 10
884 /* This can loop forever if the remote side sends us characters
885 continuously, but if it pauses, we'll get a zero from readchar
886 because of timeout. Then we'll count that as a retry. */
889 if (c > 0 && c != '$')
892 if (c == SERIAL_TIMEOUT)
896 if (++retries >= MAX_RETRIES)
897 if (remote_debug) puts_filtered ("Timed out.\n");
902 error ("Remote connection closed");
903 if (c == SERIAL_ERROR)
904 perror_with_name ("Remote communication error");
906 /* Force csum to be zero here because of possible error retry. */
913 if (c == SERIAL_TIMEOUT)
916 puts_filtered ("Timeout in mid-packet, retrying\n");
917 goto whole; /* Start a new packet, count retries */
922 puts_filtered ("Saw new packet start in middle of old one\n");
923 goto whole; /* Start a new packet, count retries */
927 if (bp >= buf+PBUFSIZ-1)
930 puts_filtered ("Remote packet too long: ");
932 puts_filtered ("\n");
940 c1 = fromhex (readchar ());
941 c2 = fromhex (readchar ());
942 if ((csum & 0xff) == (c1 << 4) + c2)
944 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
945 (c1 << 4) + c2, csum & 0xff);
947 puts_filtered ("\n");
949 /* Try the whole thing again. */
951 if (++retries < MAX_RETRIES)
953 SERIAL_WRITE (remote_desc, "-", 1);
957 printf ("Ignoring packet error, continuing...\n");
964 SERIAL_WRITE (remote_desc, "+", 1);
967 fprintf (stderr,"Packet received: %s\n", buf);
974 /* Don't wait for it to die. I'm not really sure it matters whether
975 we do or not. For the existing stubs, kill is a noop. */
976 target_mourn_inferior ();
982 unpush_target (&remote_ops);
983 generic_mourn_inferior ();
986 #ifdef REMOTE_BREAKPOINT
988 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
989 than other targets. */
990 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
992 /* Check that it fits in BREAKPOINT_MAX bytes. */
993 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
995 #else /* No REMOTE_BREAKPOINT. */
997 /* Same old breakpoint instruction. This code does nothing different
999 static unsigned char break_insn[] = BREAKPOINT;
1001 #endif /* No REMOTE_BREAKPOINT. */
1003 /* Insert a breakpoint on targets that don't have any better breakpoint
1004 support. We read the contents of the target location and stash it,
1005 then overwrite it with a breakpoint instruction. ADDR is the target
1006 location in the target machine. CONTENTS_CACHE is a pointer to
1007 memory allocated for saving the target contents. It is guaranteed
1008 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1009 is accomplished via BREAKPOINT_MAX). */
1012 remote_insert_breakpoint (addr, contents_cache)
1014 char *contents_cache;
1018 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1021 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1027 remote_remove_breakpoint (addr, contents_cache)
1029 char *contents_cache;
1031 return target_write_memory (addr, contents_cache, sizeof break_insn);
1034 /* Define the target subroutine names */
1036 struct target_ops remote_ops = {
1037 "remote", /* to_shortname */
1038 "Remote serial target in gdb-specific protocol", /* to_longname */
1039 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1040 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1041 remote_open, /* to_open */
1042 remote_close, /* to_close */
1043 NULL, /* to_attach */
1044 remote_detach, /* to_detach */
1045 remote_resume, /* to_resume */
1046 remote_wait, /* to_wait */
1047 remote_fetch_registers, /* to_fetch_registers */
1048 remote_store_registers, /* to_store_registers */
1049 remote_prepare_to_store, /* to_prepare_to_store */
1050 remote_xfer_memory, /* to_xfer_memory */
1051 remote_files_info, /* to_files_info */
1053 remote_insert_breakpoint, /* to_insert_breakpoint */
1054 remote_remove_breakpoint, /* to_remove_breakpoint */
1056 NULL, /* to_terminal_init */
1057 NULL, /* to_terminal_inferior */
1058 NULL, /* to_terminal_ours_for_output */
1059 NULL, /* to_terminal_ours */
1060 NULL, /* to_terminal_info */
1061 remote_kill, /* to_kill */
1062 generic_load, /* to_load */
1063 NULL, /* to_lookup_symbol */
1064 NULL, /* to_create_inferior */
1065 remote_mourn, /* to_mourn_inferior */
1067 0, /* to_notice_signals */
1068 process_stratum, /* to_stratum */
1070 1, /* to_has_all_memory */
1071 1, /* to_has_memory */
1072 1, /* to_has_stack */
1073 1, /* to_has_registers */
1074 1, /* to_has_execution */
1075 NULL, /* sections */
1076 NULL, /* sections_end */
1077 OPS_MAGIC /* to_magic */
1081 _initialize_remote ()
1083 add_target (&remote_ops);