1 /* Remote target communications for serial-line targets using SDS' protocol.
2 Copyright 1997 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,
19 Boston, MA 02111-1307, USA. */
21 /* This interface was written by studying the behavior of the SDS
22 monitor on an ADS 821/860 board, and by consulting the
23 documentation of the monitor that is available on Motorola's web
27 #include "gdb_string.h"
37 #include "gdb-stabs.h"
38 #include "gdbthread.h"
43 #include <sys/types.h>
49 extern void _initialize_remote_sds (void);
51 /* Declarations of local functions. */
53 static int sds_write_bytes (CORE_ADDR, char *, int);
55 static int sds_read_bytes (CORE_ADDR, char *, int);
57 static void sds_files_info (struct target_ops *ignore);
59 static int sds_xfer_memory (CORE_ADDR, char *, int, int, struct target_ops *);
61 static void sds_prepare_to_store (void);
63 static void sds_fetch_registers (int);
65 static void sds_resume (int, int, enum target_signal);
67 static int sds_start_remote (PTR);
69 static void sds_open (char *, int);
71 static void sds_close (int);
73 static void sds_store_registers (int);
75 static void sds_mourn (void);
77 static void sds_create_inferior (char *, char *, char **);
79 static void sds_load (char *, int);
81 static int getmessage (unsigned char *, int);
83 static int putmessage (unsigned char *, int);
85 static int sds_send (unsigned char *, int);
87 static int readchar (int);
89 static int sds_wait (int, struct target_waitstatus *);
91 static void sds_kill (void);
93 static int tohex (int);
95 static int fromhex (int);
97 static void sds_detach (char *, int);
99 static void sds_interrupt (int);
101 static void sds_interrupt_twice (int);
103 static void interrupt_query (void);
105 static int read_frame (char *);
107 static int sds_insert_breakpoint (CORE_ADDR, char *);
109 static int sds_remove_breakpoint (CORE_ADDR, char *);
111 static void init_sds_ops (void);
113 static void sds_command (char *args, int from_tty);
115 /* Define the target operations vector. */
117 static struct target_ops sds_ops;
119 /* This was 5 seconds, which is a long time to sit and wait.
120 Unless this is going though some terminal server or multiplexer or
121 other form of hairy serial connection, I would think 2 seconds would
124 static int sds_timeout = 2;
126 /* Descriptor for I/O to remote machine. Initialize it to NULL so
127 that sds_open knows that we don't have a file open when the program
130 static serial_t sds_desc = NULL;
132 /* This limit comes from the monitor. */
136 /* Maximum number of bytes to read/write at once. The value here
137 is chosen to fill up a packet (the headers account for the 32). */
138 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
140 static int next_msg_id;
142 static int just_started;
144 static int message_pending;
147 /* Clean up connection to a remote debugger. */
155 SERIAL_CLOSE (sds_desc);
159 /* Stub for catch_errors. */
162 sds_start_remote (dummy)
166 unsigned char buf[200];
168 immediate_quit = 1; /* Allow user to interrupt it */
170 /* Ack any packet which the remote side has already sent. */
171 SERIAL_WRITE (sds_desc, "{#*\r\n", 5);
172 SERIAL_WRITE (sds_desc, "{#}\r\n", 5);
174 while ((c = readchar (1)) >= 0)
175 printf_unfiltered ("%c", c);
176 printf_unfiltered ("\n");
188 start_remote (); /* Initialize gdb process mechanisms */
192 /* Open a connection to a remote debugger.
193 NAME is the filename used for communication. */
195 static DCACHE *sds_dcache;
198 sds_open (name, from_tty)
203 error ("To open a remote debug connection, you need to specify what serial\n\
204 device is attached to the remote system (e.g. /dev/ttya).");
206 target_preopen (from_tty);
208 unpush_target (&sds_ops);
210 sds_dcache = dcache_init (sds_read_bytes, sds_write_bytes);
212 sds_desc = SERIAL_OPEN (name);
214 perror_with_name (name);
218 if (SERIAL_SETBAUDRATE (sds_desc, baud_rate))
220 SERIAL_CLOSE (sds_desc);
221 perror_with_name (name);
226 SERIAL_RAW (sds_desc);
228 /* If there is something sitting in the buffer we might take it as a
229 response to a command, which would be bad. */
230 SERIAL_FLUSH_INPUT (sds_desc);
234 puts_filtered ("Remote debugging using ");
235 puts_filtered (name);
236 puts_filtered ("\n");
238 push_target (&sds_ops); /* Switch to using remote target now */
242 /* Start the remote connection; if error (0), discard this target.
243 In particular, if the user quits, be sure to discard it (we'd be
244 in an inconsistent state otherwise). */
245 if (!catch_errors (sds_start_remote, NULL,
246 "Couldn't establish connection to remote target\n",
251 /* This takes a program previously attached to and detaches it. After
252 this is done, GDB can be used to debug some other program. We
253 better not have left any breakpoints in the target program or it'll
254 die when it hits one. */
257 sds_detach (args, from_tty)
264 error ("Argument given to \"detach\" when remotely debugging.");
267 /* Tell the remote target to detach. */
274 puts_filtered ("Ending remote debugging.\n");
277 /* Convert hex digit A to a number. */
283 if (a >= '0' && a <= '9')
285 else if (a >= 'a' && a <= 'f')
288 error ("Reply contains invalid hex digit %d", a);
291 /* Convert number NIB to a hex digit. */
300 return 'a' + nib - 10;
304 tob64 (inbuf, outbuf, len)
305 unsigned char *inbuf;
313 error ("bad length");
316 for (i = 0; i < len; i += 3)
318 /* Collect the next three bytes into a number. */
319 sum = ((long) *inbuf++) << 16;
320 sum |= ((long) *inbuf++) << 8;
321 sum |= ((long) *inbuf++);
323 /* Spit out 4 6-bit encodings. */
324 *p++ = ((sum >> 18) & 0x3f) + '0';
325 *p++ = ((sum >> 12) & 0x3f) + '0';
326 *p++ = ((sum >> 6) & 0x3f) + '0';
327 *p++ = (sum & 0x3f) + '0';
333 fromb64 (inbuf, outbuf, len)
334 char *inbuf, *outbuf;
340 error ("bad length");
342 for (i = 0; i < len; i += 4)
344 /* Collect 4 6-bit digits. */
345 sum = (*inbuf++ - '0') << 18;
346 sum |= (*inbuf++ - '0') << 12;
347 sum |= (*inbuf++ - '0') << 6;
348 sum |= (*inbuf++ - '0');
350 /* Now take the resulting 24-bit number and get three bytes out
352 *outbuf++ = (sum >> 16) & 0xff;
353 *outbuf++ = (sum >> 8) & 0xff;
354 *outbuf++ = sum & 0xff;
357 return (len / 4) * 3;
361 /* Tell the remote machine to resume. */
363 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
367 sds_resume (pid, step, siggnal)
369 enum target_signal siggnal;
371 unsigned char buf[PBUFSIZ];
373 dcache_flush (sds_dcache);
375 last_sent_signal = siggnal;
376 last_sent_step = step;
378 buf[0] = (step ? 21 : 20);
379 buf[1] = 0; /* (should be signal?) */
384 /* Send a message to target to halt it. Target will respond, and send
385 us a message pending notice. */
388 sds_interrupt (signo)
391 unsigned char buf[PBUFSIZ];
393 /* If this doesn't work, try more severe steps. */
394 signal (signo, sds_interrupt_twice);
397 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
403 static void (*ofunc) ();
405 /* The user typed ^C twice. */
408 sds_interrupt_twice (signo)
411 signal (signo, ofunc);
415 signal (signo, sds_interrupt);
418 /* Ask the user what to do when an interrupt is received. */
423 target_terminal_ours ();
425 if (query ("Interrupted while waiting for the program.\n\
426 Give up (and stop debugging it)? "))
428 target_mourn_inferior ();
429 return_to_top_level (RETURN_QUIT);
432 target_terminal_inferior ();
435 /* If nonzero, ignore the next kill. */
438 /* Wait until the remote machine stops, then return, storing status in
439 STATUS just as `wait' would. Returns "pid" (though it's not clear
440 what, if anything, that means in the case of this target). */
443 sds_wait (pid, status)
445 struct target_waitstatus *status;
447 unsigned char buf[PBUFSIZ];
450 status->kind = TARGET_WAITKIND_EXITED;
451 status->value.integer = 0;
453 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
455 signal (SIGINT, ofunc);
460 status->kind = TARGET_WAITKIND_STOPPED;
471 retlen = sds_send (buf, 1);
474 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
479 status->kind = TARGET_WAITKIND_STOPPED;
480 status->value.sig = TARGET_SIGNAL_TRAP;
488 static unsigned char sprs[16];
490 /* Read the remote registers into the block REGS. */
491 /* Currently we just read all the registers, so we don't use regno. */
495 sds_fetch_registers (regno)
498 unsigned char buf[PBUFSIZ];
500 char regs[REGISTER_BYTES];
502 /* Unimplemented registers read as all bits zero. */
503 memset (regs, 0, REGISTER_BYTES);
508 retlen = sds_send (buf, 3);
510 for (i = 0; i < 4 * 6; ++i)
511 regs[i + 4 * 32 + 8 * 32] = buf[i];
512 for (i = 0; i < 4 * 4; ++i)
513 sprs[i] = buf[i + 4 * 7];
518 retlen = sds_send (buf, 3);
520 for (i = 0; i < retlen; i++)
523 /* (should warn about reply too short) */
525 for (i = 0; i < NUM_REGS; i++)
526 supply_register (i, ®s[REGISTER_BYTE (i)]);
529 /* Prepare to store registers. Since we may send them all, we have to
530 read out the ones we don't want to change first. */
533 sds_prepare_to_store ()
535 /* Make sure the entire registers array is valid. */
536 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
539 /* Store register REGNO, or all registers if REGNO == -1, from the contents
540 of REGISTERS. FIXME: ignores errors. */
543 sds_store_registers (regno)
546 unsigned char *p, buf[PBUFSIZ];
549 /* Store all the special-purpose registers. */
555 for (i = 0; i < 4 * 6; i++)
556 *p++ = registers[i + 4 * 32 + 8 * 32];
557 for (i = 0; i < 4 * 1; i++)
559 for (i = 0; i < 4 * 4; i++)
562 sds_send (buf, p - buf);
564 /* Store all the general-purpose registers. */
570 for (i = 0; i < 4 * 32; i++)
573 sds_send (buf, p - buf);
577 /* Write memory data directly to the remote machine. This does not
578 inform the data cache; the data cache uses this. MEMADDR is the
579 address in the remote memory space. MYADDR is the address of the
580 buffer in our space. LEN is the number of bytes.
582 Returns number of bytes transferred, or 0 for error. */
585 sds_write_bytes (memaddr, myaddr, len)
590 int max_buf_size; /* Max size of packet output buffer */
592 unsigned char buf[PBUFSIZ];
596 /* Chop the transfer down if necessary */
603 todo = min (len, max_buf_size);
607 buf[2] = (int) (memaddr >> 24) & 0xff;
608 buf[3] = (int) (memaddr >> 16) & 0xff;
609 buf[4] = (int) (memaddr >> 8) & 0xff;
610 buf[5] = (int) (memaddr) & 0xff;
614 for (i = 0; i < todo; i++)
615 buf[i + 8] = myaddr[i];
617 sds_send (buf, 8 + todo);
619 /* (should look at result) */
628 /* Read memory data directly from the remote machine. This does not
629 use the data cache; the data cache uses this. MEMADDR is the
630 address in the remote memory space. MYADDR is the address of the
631 buffer in our space. LEN is the number of bytes.
633 Returns number of bytes transferred, or 0 for error. */
636 sds_read_bytes (memaddr, myaddr, len)
641 int max_buf_size; /* Max size of packet output buffer */
643 unsigned char buf[PBUFSIZ];
647 /* Chop the transfer down if necessary */
654 todo = min (len, max_buf_size);
658 buf[2] = (int) (memaddr >> 24) & 0xff;
659 buf[3] = (int) (memaddr >> 16) & 0xff;
660 buf[4] = (int) (memaddr >> 8) & 0xff;
661 buf[5] = (int) (memaddr) & 0xff;
662 buf[6] = (int) (todo >> 8) & 0xff;
663 buf[7] = (int) (todo) & 0xff;
666 retlen = sds_send (buf, 9);
668 if (retlen - 2 != todo)
673 /* Reply describes memory byte by byte. */
675 for (i = 0; i < todo; i++)
676 myaddr[i] = buf[i + 2];
686 /* Read or write LEN bytes from inferior memory at MEMADDR,
687 transferring to or from debugger address MYADDR. Write to inferior
688 if SHOULD_WRITE is nonzero. Returns length of data written or
689 read; 0 for error. */
693 sds_xfer_memory (memaddr, myaddr, len, should_write, target)
698 struct target_ops *target; /* ignored */
700 return dcache_xfer_memory (sds_dcache, memaddr, myaddr, len, should_write);
705 sds_files_info (ignore)
706 struct target_ops *ignore;
708 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
711 /* Stuff for dealing with the packets which are part of this protocol.
712 See comment at top of file for details. */
714 /* Read a single character from the remote end, masking it down to 7 bits. */
722 ch = SERIAL_READCHAR (sds_desc, timeout);
724 if (remote_debug > 1 && ch >= 0)
725 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
730 error ("Remote connection closed");
732 perror_with_name ("Remote communication error");
740 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
741 because 253, 254, and 255 are special flags in the protocol.) */
744 compute_checksum (csum, buf, len)
750 for (i = 0; i < len; ++i)
751 csum += (unsigned char) buf[i];
757 /* Send the command in BUF to the remote machine, and read the reply
765 putmessage (buf, len);
767 return getmessage (buf, 0);
770 /* Send a message to the remote machine. */
773 putmessage (buf, len)
778 unsigned char csum = 0;
779 char buf2[PBUFSIZ], buf3[PBUFSIZ];
780 unsigned char header[3];
783 /* Copy the packet into buffer BUF2, encapsulating it
784 and giving it a checksum. */
786 if (len > 170) /* Prosanity check */
791 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
792 for (i = 0; i < len; ++i)
793 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
794 fprintf_unfiltered (gdb_stdlog, "\"\n");
806 header[1] = next_msg_id;
810 csum = compute_checksum (csum, buf, len);
811 csum = compute_checksum (csum, header + 1, 2);
815 tob64 (header, p, 3);
817 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
819 for (i = 0; i < enclen; ++i)
824 next_msg_id = (next_msg_id + 3) % 245;
826 /* Send it over and over until we get a positive ack. */
833 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
834 fprintf_unfiltered (gdb_stdlog,
835 " (Checksum %d, id %d, length %d)\n",
836 header[0], header[1], header[2]);
837 gdb_flush (gdb_stdlog);
839 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
840 perror_with_name ("putmessage: write failed");
846 /* Come here after finding the start of the frame. Collect the rest
847 into BUF. Returns 0 on any error, 1 on success. */
860 c = readchar (sds_timeout);
866 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
870 fputs_filtered ("Saw new packet start in middle of old one\n",
872 return 0; /* Start a new packet, count retries */
880 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
886 if (bp < buf + PBUFSIZ - 1)
893 puts_filtered ("Message too long: ");
895 puts_filtered ("\n");
902 /* Read a packet from the remote machine, with error checking,
903 and store it in BUF. BUF is expected to be of size PBUFSIZ.
904 If FOREVER, wait forever rather than timing out; this is used
905 while the target is executing user code. */
908 getmessage (buf, forever)
915 int val, i, len, csum;
916 unsigned char header[3];
917 unsigned char inbuf[500];
919 strcpy (buf, "timeout");
923 timeout = watchdog > 0 ? watchdog : -1;
927 timeout = sds_timeout;
931 for (tries = 1; tries <= MAX_TRIES; tries++)
933 /* This can loop forever if the remote side sends us characters
934 continuously, but if it pauses, we'll get a zero from readchar
935 because of timeout. Then we'll count that as a retry. */
937 /* Note that we will only wait forever prior to the start of a packet.
938 After that, we expect characters to arrive at a brisk pace. They
939 should show up within sds_timeout intervals. */
943 c = readchar (timeout);
945 if (c == SERIAL_TIMEOUT)
947 if (forever) /* Watchdog went off. Kill the target. */
949 target_mourn_inferior ();
950 error ("Watchdog has expired. Target detached.\n");
953 fputs_filtered ("Timed out.\n", gdb_stdlog);
957 while (c != '$' && c != '{');
959 /* We might have seen a "trigraph", a sequence of three characters
960 that indicate various sorts of communication state. */
964 /* Read the other two chars of the trigraph. */
965 c2 = readchar (timeout);
966 c3 = readchar (timeout);
968 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
978 val = read_frame (inbuf);
982 fromb64 (inbuf, header, 4);
983 /* (should check out other bits) */
984 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
989 csum = compute_checksum (csum, buf, len);
990 csum = compute_checksum (csum, header + 1, 2);
992 if (csum != header[0])
993 fprintf_unfiltered (gdb_stderr,
994 "Checksum mismatch: computed %d, received %d\n",
997 if (header[2] == 0xff)
998 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
1002 fprintf_unfiltered (gdb_stdlog,
1003 "... (Got checksum %d, id %d, length %d)\n",
1004 header[0], header[1], header[2]);
1005 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
1006 for (i = 0; i < len; ++i)
1008 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
1010 fprintf_unfiltered (gdb_stdlog, "\"\n");
1013 /* no ack required? */
1017 /* Try the whole thing again. */
1019 /* need to do something here */
1022 /* We have tried hard enough, and just can't receive the packet. Give up. */
1024 printf_unfiltered ("Ignoring packet error, continuing...\n");
1031 /* Don't try to do anything to the target. */
1037 unpush_target (&sds_ops);
1038 generic_mourn_inferior ();
1042 sds_create_inferior (exec_file, args, env)
1047 inferior_pid = 42000;
1049 /* Clean up from the last time we were running. */
1050 clear_proceed_status ();
1052 /* Let the remote process run. */
1053 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1057 sds_load (filename, from_tty)
1061 generic_load (filename, from_tty);
1066 /* The SDS monitor has commands for breakpoint insertion, although it
1067 it doesn't actually manage the breakpoints, it just returns the
1068 replaced instruction back to the debugger. */
1071 sds_insert_breakpoint (addr, contents_cache)
1073 char *contents_cache;
1076 unsigned char *p, buf[PBUFSIZ];
1081 *p++ = (int) (addr >> 24) & 0xff;
1082 *p++ = (int) (addr >> 16) & 0xff;
1083 *p++ = (int) (addr >> 8) & 0xff;
1084 *p++ = (int) (addr) & 0xff;
1086 retlen = sds_send (buf, p - buf);
1088 for (i = 0; i < 4; ++i)
1089 contents_cache[i] = buf[i + 2];
1095 sds_remove_breakpoint (addr, contents_cache)
1097 char *contents_cache;
1100 unsigned char *p, buf[PBUFSIZ];
1105 *p++ = (int) (addr >> 24) & 0xff;
1106 *p++ = (int) (addr >> 16) & 0xff;
1107 *p++ = (int) (addr >> 8) & 0xff;
1108 *p++ = (int) (addr) & 0xff;
1109 for (i = 0; i < 4; ++i)
1110 *p++ = contents_cache[i];
1112 retlen = sds_send (buf, p - buf);
1120 sds_ops.to_shortname = "sds";
1121 sds_ops.to_longname = "Remote serial target with SDS protocol";
1122 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1123 Specify the serial device it is connected to (e.g. /dev/ttya).";
1124 sds_ops.to_open = sds_open;
1125 sds_ops.to_close = sds_close;
1126 sds_ops.to_detach = sds_detach;
1127 sds_ops.to_resume = sds_resume;
1128 sds_ops.to_wait = sds_wait;
1129 sds_ops.to_fetch_registers = sds_fetch_registers;
1130 sds_ops.to_store_registers = sds_store_registers;
1131 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1132 sds_ops.to_xfer_memory = sds_xfer_memory;
1133 sds_ops.to_files_info = sds_files_info;
1134 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1135 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1136 sds_ops.to_kill = sds_kill;
1137 sds_ops.to_load = sds_load;
1138 sds_ops.to_create_inferior = sds_create_inferior;
1139 sds_ops.to_mourn_inferior = sds_mourn;
1140 sds_ops.to_stratum = process_stratum;
1141 sds_ops.to_has_all_memory = 1;
1142 sds_ops.to_has_memory = 1;
1143 sds_ops.to_has_stack = 1;
1144 sds_ops.to_has_registers = 1;
1145 sds_ops.to_has_execution = 1;
1146 sds_ops.to_magic = OPS_MAGIC;
1149 /* Put a command string, in args, out to the monitor and display the
1153 sds_command (args, from_tty)
1159 unsigned char buf[1000];
1161 /* Convert hexadecimal chars into a byte buffer. */
1166 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1172 retlen = sds_send (buf, len);
1174 printf_filtered ("Reply is ");
1175 for (i = 0; i < retlen; ++i)
1177 printf_filtered ("%02x", buf[i]);
1179 printf_filtered ("\n");
1183 _initialize_remote_sds ()
1186 add_target (&sds_ops);
1188 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1189 var_integer, (char *) &sds_timeout,
1190 "Set timeout value for sds read.\n", &setlist),
1193 add_com ("sds", class_obscure, sds_command,
1194 "Send a command to the SDS monitor.");