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.
21 All values are encoded in ascii hex digits.
26 reply XX....X Each byte of register data
27 is described by two hex digits.
28 Registers are in the internal order
29 for GDB, and the bytes in a register
30 are in the same order the machine uses.
33 write regs GXX..XX Each byte of register data
34 is described by two hex digits.
38 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
39 reply XX..XX XX..XX is mem contents
42 write mem MAA..AA,LLLL:XX..XX
44 LLLL is number of bytes,
49 cont cAA..AA AA..AA is address to resume
51 resume at same address.
53 step sAA..AA AA..AA is address to resume
55 resume at same address.
57 last signal ? Reply the current reason for stopping.
58 This is the same reply as is generated
59 for step or cont : SAA where AA is the
62 There is no immediate reply to step or cont.
63 The reply comes when the machine stops.
64 It is SAA AA is the "signal number"
66 or... TAAPPPPPPPPFFFFFFFF
67 where AA is the signal number,
68 PPPPPPPP is the PC (PC_REGNUM), and
69 FFFFFFFF is the frame ptr (FP_REGNUM).
84 #if !defined(DONT_USE_REMOTE)
86 #include <sys/types.h>
91 /* Prototypes for local functions */
94 remote_write_bytes PARAMS ((CORE_ADDR, char *, int));
97 remote_read_bytes PARAMS ((CORE_ADDR, char *, int));
100 remote_files_info PARAMS ((struct target_ops *));
103 remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
106 remote_prepare_to_store PARAMS ((void));
109 remote_fetch_registers PARAMS ((int));
112 remote_resume PARAMS ((int, int));
115 remote_start_remote PARAMS ((char *));
118 remote_open PARAMS ((char *, int));
121 remote_close PARAMS ((int));
124 remote_store_registers PARAMS ((int));
127 getpkt PARAMS ((char *, int));
130 putpkt PARAMS ((char *));
133 remote_send PARAMS ((char *));
136 readchar PARAMS ((void));
139 remote_wait PARAMS ((WAITTYPE *));
142 tohex PARAMS ((int));
145 fromhex PARAMS ((int));
148 remote_detach PARAMS ((char *, int));
151 extern struct target_ops remote_ops; /* Forward decl */
153 static int kiodebug = 0;
154 static int timeout = 5;
160 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
161 remote_open knows that we don't have a file open when the program
163 int remote_desc = -1;
167 /* Maximum number of bytes to read/write at once. The value here
168 is chosen to fill up a packet (the headers account for the 32). */
169 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
171 /* Round up PBUFSIZ to hold all the registers, at least. */
172 #if REGISTER_BYTES > MAXBUFBYTES
174 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
177 /* Called when SIGALRM signal sent due to alarm() timeout. */
184 printf ("remote_timer called\n");
190 /* Clean up connection to a remote debugger. */
194 remote_close (quitting)
197 if (remote_desc >= 0)
202 /* Translate baud rates from integers to damn B_codes. Unix should
203 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
214 static struct {int rate, damn_b;} baudtab[] = {
240 for (i = 0; baudtab[i].rate != -1; i++)
241 if (rate == baudtab[i].rate) return baudtab[i].damn_b;
242 return B38400; /* Random */
245 /* Stub for catch_errors. */
248 remote_start_remote (dummy)
251 /* Ack any packet which the remote side has already sent. */
252 write (remote_desc, "+\r", 2);
253 putpkt ("?"); /* initiate a query from remote machine */
255 start_remote (); /* Initialize gdb process mechanisms */
259 /* Open a connection to a remote debugger.
260 NAME is the filename used for communication. */
263 remote_open (name, from_tty)
268 int a_rate, b_rate = 0;
269 int baudrate_set = 0;
273 "To open a remote debug connection, you need to specify what serial\n\
274 device is attached to the remote system (e.g. /dev/ttya).");
276 target_preopen (from_tty);
284 remote_desc = open (name, O_RDWR);
286 perror_with_name (name);
290 if (sscanf (baud_rate, "%d", &a_rate) == 1)
292 b_rate = damn_b (a_rate);
297 ioctl (remote_desc, TIOCGETP, &sg);
299 sg.c_cc[VMIN] = 0; /* read with timeout. */
300 sg.c_cc[VTIME] = timeout * 10;
301 sg.c_lflag &= ~(ICANON | ECHO);
302 sg.c_cflag &= ~PARENB; /* No parity */
303 sg.c_cflag |= CS8; /* 8-bit path */
305 sg.c_cflag = (sg.c_cflag & ~CBAUD) | b_rate;
307 sg.sg_flags |= RAW | ANYP;
308 sg.sg_flags &= ~ECHO;
311 sg.sg_ispeed = b_rate;
312 sg.sg_ospeed = b_rate;
315 ioctl (remote_desc, TIOCSETP, &sg);
319 puts_filtered ("Remote debugging using ");
320 puts_filtered (name);
321 puts_filtered ("\n");
323 push_target (&remote_ops); /* Switch to using remote target now */
326 #ifndef NO_SIGINTERRUPT
327 /* Cause SIGALRM's to make reads fail. */
328 if (siginterrupt (SIGALRM, 1) != 0)
329 perror ("remote_open: error in siginterrupt");
332 /* Set up read timeout timer. */
333 if ((void (*)()) signal (SIGALRM, remote_timer) == (void (*)()) -1)
334 perror ("remote_open: error in signal");
337 /* Start the remote connection; if error (0), discard this target. */
338 immediate_quit++; /* Allow user to interrupt it */
339 if (!catch_errors (remote_start_remote, (char *)0,
340 "Couldn't establish connection to remote target\n"))
345 takes a program previously attached to and detaches it.
346 We better not have left any breakpoints
347 in the program or it'll die when it hits one.
348 Close the open connection to the remote debugger.
349 Use this when you want to detach and do something else
353 remote_detach (args, from_tty)
358 error ("Argument given to \"detach\" when remotely debugging.");
362 puts_filtered ("Ending remote debugging.\n");
365 /* Convert hex digit A to a number. */
371 if (a >= '0' && a <= '9')
373 else if (a >= 'a' && a <= 'f')
376 error ("Reply contains invalid hex digit");
380 /* Convert number NIB to a hex digit. */
392 /* Tell the remote machine to resume. */
395 remote_resume (step, siggnal)
401 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
408 strcpy (buf, step ? "s": "c");
413 /* Send ^C to target to halt it. Target will respond, and send us a
416 void remote_interrupt(signo)
421 printf ("remote_interrupt called\n");
423 write (remote_desc, "\003", 1); /* Send a ^C */
427 /* Wait until the remote machine stops, then return,
428 storing status in STATUS just as `wait' would.
429 Returns "pid" (though it's not clear what, if anything, that
430 means in the case of this target). */
436 unsigned char buf[PBUFSIZ];
441 char regs[MAX_REGISTER_RAW_SIZE];
443 WSETEXIT ((*status), 0);
445 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
446 getpkt ((char *) buf, 1);
447 signal (SIGINT, ofunc);
450 error ("Remote failure reply: %s", buf);
453 /* Expedited reply, containing Signal, {regno, reg} repeat */
454 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
456 n... = register number
457 r... = register contents
460 p = &buf[3]; /* after Txx */
464 regno = strtol (p, &p, 16); /* Read the register number */
467 || regno >= NUM_REGS)
468 error ("Remote sent bad register number %s", buf);
470 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
472 if (p[0] == 0 || p[1] == 0)
473 error ("Remote reply is too short: %s", buf);
474 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
479 error("Remote register badly formatted: %s", buf);
481 supply_register (regno, regs);
484 else if (buf[0] != 'S')
485 error ("Invalid remote reply: %s", buf);
487 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
492 /* Read the remote registers into the block REGS. */
493 /* Currently we just read all the registers, so we don't use regno. */
496 remote_fetch_registers (regno)
502 char regs[REGISTER_BYTES];
507 /* Reply describes registers byte by byte, each byte encoded as two
508 hex characters. Suck them all up, then supply them to the
509 register cacheing/storage mechanism. */
512 for (i = 0; i < REGISTER_BYTES; i++)
514 if (p[0] == 0 || p[1] == 0)
515 error ("Remote reply is too short: %s", buf);
516 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
519 for (i = 0; i < NUM_REGS; i++)
520 supply_register (i, ®s[REGISTER_BYTE(i)]);
523 /* Prepare to store registers. Since we send them all, we have to
524 read out the ones we don't want to change first. */
527 remote_prepare_to_store ()
529 /* Make sure the entire registers array is valid. */
530 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
533 /* Store the remote registers from the contents of the block REGISTERS.
534 FIXME, eventually just store one register if that's all that is needed. */
538 remote_store_registers (regno)
547 /* Command describes registers byte by byte,
548 each byte encoded as two hex characters. */
551 for (i = 0; i < REGISTER_BYTES; i++)
553 *p++ = tohex ((registers[i] >> 4) & 0xf);
554 *p++ = tohex (registers[i] & 0xf);
562 /* Read a word from remote address ADDR and return it.
563 This goes through the data cache. */
566 remote_fetch_word (addr)
571 extern CORE_ADDR text_start, text_end;
573 if (addr >= text_start && addr < text_end)
576 xfer_core_file (addr, &buffer, sizeof (int));
580 return dcache_fetch (addr);
583 /* Write a word WORD into remote address ADDR.
584 This goes through the data cache. */
587 remote_store_word (addr, word)
591 dcache_poke (addr, word);
595 /* Write memory data directly to the remote machine.
596 This does not inform the data cache; the data cache uses this.
597 MEMADDR is the address in the remote memory space.
598 MYADDR is the address of the buffer in our space.
599 LEN is the number of bytes. */
602 remote_write_bytes (memaddr, myaddr, len)
611 if (len > PBUFSIZ / 2 - 20)
614 sprintf (buf, "M%x,%x:", memaddr, len);
616 /* We send target system values byte by byte, in increasing byte addresses,
617 each byte encoded as two hex characters. */
619 p = buf + strlen (buf);
620 for (i = 0; i < len; i++)
622 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
623 *p++ = tohex (myaddr[i] & 0xf);
630 /* Read memory data directly from the remote machine.
631 This does not use the data cache; the data cache uses this.
632 MEMADDR is the address in the remote memory space.
633 MYADDR is the address of the buffer in our space.
634 LEN is the number of bytes. */
637 remote_read_bytes (memaddr, myaddr, len)
646 if (len > PBUFSIZ / 2 - 1)
649 sprintf (buf, "m%x,%x", memaddr, len);
652 /* Reply describes memory byte by byte,
653 each byte encoded as two hex characters. */
656 for (i = 0; i < len; i++)
658 if (p[0] == 0 || p[1] == 0)
659 error ("Remote reply is too short: %s", buf);
660 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
665 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
666 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
667 nonzero. Returns length of data written or read; 0 for error. */
671 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
676 struct target_ops *target; /* ignored */
682 if (len > MAXBUFBYTES)
683 xfersize = MAXBUFBYTES;
688 remote_write_bytes(memaddr, myaddr, xfersize);
690 remote_read_bytes (memaddr, myaddr, xfersize);
695 return origlen; /* no error possible */
699 remote_files_info (ignore)
700 struct target_ops *ignore;
702 puts_filtered ("Debugging a target over a serial line.\n");
707 A debug packet whose contents are <data>
708 is encapsulated for transmission in the form:
710 $ <data> # CSUM1 CSUM2
712 <data> must be ASCII alphanumeric and cannot include characters
715 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
716 checksum of <data>, the most significant nibble is sent first.
717 the hex digits 0-9,a-f are used.
719 Receiver responds with:
721 + - if CSUM is correct and ready for next packet
722 - - if CSUM is incorrect
726 /* Read a single character from the remote end.
727 (If supported, we actually read many characters and buffer them up.)
728 Timeouts cause a zero (nul) to be returned. */
733 static int inbuf_index, inbuf_count;
734 #define INBUFSIZE PBUFSIZ
735 static char inbuf[INBUFSIZE];
736 struct cleanup *old_chain;
738 if (inbuf_index >= inbuf_count)
744 /* Time to do another read... */
747 inbuf[0] = 0; /* Just in case */
749 /* termio does the timeout for us. */
750 inbuf_count = read (remote_desc, inbuf, INBUFSIZE);
752 /* Cancel alarm on error. */
753 old_chain = make_cleanup (alarm, (char *)0);
755 inbuf_count = read (remote_desc, inbuf, INBUFSIZE);
756 do_cleanups (old_chain); /* Cancel the alarm now. */
760 /* Just return the next character from the buffer (or a zero if we
761 got an error and no chars were stored in inbuf). */
762 return inbuf[inbuf_index++] & 0x7f;
765 /* Send the command in BUF to the remote machine,
766 and read the reply into BUF.
767 Report an error if we get an error reply. */
778 error ("Remote failure reply: %s", buf);
781 /* Send a packet to the remote machine, with error checking.
782 The data of the packet is in BUF. */
789 unsigned char csum = 0;
791 int cnt = strlen (buf);
795 /* Copy the packet into buffer BUF2, encapsulating it
796 and giving it a checksum. */
798 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
804 for (i = 0; i < cnt; i++)
810 *p++ = tohex ((csum >> 4) & 0xf);
811 *p++ = tohex (csum & 0xf);
813 /* Send it over and over until we get a positive ack. */
819 printf ("Sending packet: %s...", buf2); fflush(stdout);
821 write (remote_desc, buf2, p - buf2);
823 /* read until either a timeout occurs (\0) or '+' is read */
830 printf ("%02X%c ", ch&0xFF, ch);
832 } while ((ch != '+') && (ch != '\0'));
836 /* Read a packet from the remote machine, with error checking,
837 and store it in BUF. BUF is expected to be of size PBUFSIZ.
838 If FOREVER, wait forever rather than timing out; this is used
839 while the target is executing user code. */
842 getpkt (buf, forever)
848 unsigned char c1, c2;
850 #define MAX_RETRIES 10
853 /* Sorry, this will cause all hell to break loose, i.e. we'll end
854 up in the command loop with an inferior, but (at least if this
855 happens in remote_wait or some such place) without a current_frame,
856 having set up prev_* in wait_for_inferior, etc.
858 If it is necessary to have such an "emergency exit", seems like
859 the only plausible thing to do is to say the inferior died, and
860 make the user reattach if they want to. Perhaps with a prompt
861 asking for confirmation. */
863 /* allow immediate quit while reading from device, it could be hung */
869 /* This can loop forever if the remote side sends us characters
870 continuously, but if it pauses, we'll get a zero from readchar
871 because of timeout. Then we'll count that as a retry. */
873 if (0 == (c = readchar()))
876 if (++retries >= MAX_RETRIES)
877 if (kiodebug) puts_filtered ("Timed out.\n");
881 /* Force csum to be zero here because of possible error retry. */
891 puts_filtered ("Timeout in mid-packet, retrying\n");
892 goto whole; /* Start a new packet, count retries */
897 puts_filtered ("Saw new packet start in middle of old one\n");
898 goto whole; /* Start a new packet, count retries */
902 if (bp >= buf+PBUFSIZ-1)
905 puts_filtered ("Remote packet too long: ");
907 puts_filtered ("\n");
915 c1 = fromhex (readchar ());
916 c2 = fromhex (readchar ());
917 if ((csum & 0xff) == (c1 << 4) + c2)
919 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
920 (c1 << 4) + c2, csum & 0xff);
922 puts_filtered ("\n");
924 /* Try the whole thing again. */
926 if (++retries < MAX_RETRIES)
928 write (remote_desc, "-", 1);
932 printf ("Ignoring packet error, continuing...\n");
943 write (remote_desc, "+", 1);
946 fprintf (stderr,"Packet received: %s\n", buf);
949 /* The data cache leads to incorrect results because it doesn't know about
950 volatile variables, thus making it impossible to debug functions which
951 use hardware registers. Therefore it is #if 0'd out. Effect on
952 performance is some, for backtraces of functions with a few
953 arguments each. For functions with many arguments, the stack
954 frames don't fit in the cache blocks, which makes the cache less
955 helpful. Disabling the cache is a big performance win for fetching
956 large structures, because the cache code fetched data in 16-byte
959 /* The data cache records all the data read from the remote machine
960 since the last time it stopped.
962 Each cache block holds 16 bytes of data
963 starting at a multiple-of-16 address. */
965 #define DCACHE_SIZE 64 /* Number of cache blocks */
967 struct dcache_block {
968 struct dcache_block *next, *last;
969 unsigned int addr; /* Address for which data is recorded. */
973 struct dcache_block dcache_free, dcache_valid;
975 /* Free all the data cache blocks, thus discarding all cached data. */
980 register struct dcache_block *db;
982 while ((db = dcache_valid.next) != &dcache_valid)
985 insque (db, &dcache_free);
990 * If addr is present in the dcache, return the address of the block
994 struct dcache_block *
997 register struct dcache_block *db;
1002 /* Search all cache blocks for one that is at this address. */
1003 db = dcache_valid.next;
1004 while (db != &dcache_valid)
1006 if ((addr & 0xfffffff0) == db->addr)
1013 /* Return the int data at address ADDR in dcache block DC. */
1016 dcache_value (db, addr)
1017 struct dcache_block *db;
1022 return (db->data[(addr>>2)&3]);
1025 /* Get a free cache block, put it on the valid list,
1026 and return its address. The caller should store into the block
1027 the address and data that it describes. */
1029 struct dcache_block *
1032 register struct dcache_block *db;
1034 if ((db = dcache_free.next) == &dcache_free)
1035 /* If we can't get one from the free list, take last valid */
1036 db = dcache_valid.last;
1039 insque (db, &dcache_valid);
1043 /* Return the contents of the word at address ADDR in the remote machine,
1044 using the data cache. */
1050 register struct dcache_block *db;
1052 db = dcache_hit (addr);
1055 db = dcache_alloc ();
1056 remote_read_bytes (addr & ~0xf, db->data, 16);
1057 db->addr = addr & ~0xf;
1059 return (dcache_value (db, addr));
1062 /* Write the word at ADDR both in the data cache and in the remote machine. */
1064 dcache_poke (addr, data)
1068 register struct dcache_block *db;
1070 /* First make sure the word is IN the cache. DB is its cache block. */
1071 db = dcache_hit (addr);
1074 db = dcache_alloc ();
1075 remote_read_bytes (addr & ~0xf, db->data, 16);
1076 db->addr = addr & ~0xf;
1079 /* Modify the word in the cache. */
1080 db->data[(addr>>2)&3] = data;
1082 /* Send the changed word. */
1083 remote_write_bytes (addr, &data, 4);
1086 /* Initialize the data cache. */
1091 register struct dcache_block *db;
1093 db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) *
1095 dcache_free.next = dcache_free.last = &dcache_free;
1096 dcache_valid.next = dcache_valid.last = &dcache_valid;
1097 for (i=0;i<DCACHE_SIZE;i++,db++)
1098 insque (db, &dcache_free);
1102 /* Define the target subroutine names */
1104 struct target_ops remote_ops = {
1105 "remote", /* to_shortname */
1106 "Remote serial target in gdb-specific protocol", /* to_longname */
1107 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1108 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1109 remote_open, /* to_open */
1110 remote_close, /* to_close */
1111 NULL, /* to_attach */
1112 remote_detach, /* to_detach */
1113 remote_resume, /* to_resume */
1114 remote_wait, /* to_wait */
1115 remote_fetch_registers, /* to_fetch_registers */
1116 remote_store_registers, /* to_store_registers */
1117 remote_prepare_to_store, /* to_prepare_to_store */
1118 remote_xfer_memory, /* to_xfer_memory */
1119 remote_files_info, /* to_files_info */
1120 NULL, /* to_insert_breakpoint */
1121 NULL, /* to_remove_breakpoint */
1122 NULL, /* to_terminal_init */
1123 NULL, /* to_terminal_inferior */
1124 NULL, /* to_terminal_ours_for_output */
1125 NULL, /* to_terminal_ours */
1126 NULL, /* to_terminal_info */
1129 NULL, /* to_lookup_symbol */
1130 NULL, /* to_create_inferior */
1131 NULL, /* to_mourn_inferior */
1133 0, /* to_notice_signals */
1134 process_stratum, /* to_stratum */
1136 1, /* to_has_all_memory */
1137 1, /* to_has_memory */
1138 1, /* to_has_stack */
1139 1, /* to_has_registers */
1140 1, /* to_has_execution */
1141 NULL, /* sections */
1142 NULL, /* sections_end */
1143 OPS_MAGIC /* to_magic */
1147 _initialize_remote ()
1149 add_target (&remote_ops);
1152 add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug,
1153 "Set debugging of remote serial I/O.\n\
1154 When enabled, each packet sent or received with the remote target\n\
1155 is displayed.", &setlist),