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 PARAMS ((void));
51 /* Declarations of local functions. */
53 static int sds_write_bytes PARAMS ((CORE_ADDR, char *, int));
55 static int sds_read_bytes PARAMS ((CORE_ADDR, char *, int));
57 static void sds_files_info PARAMS ((struct target_ops * ignore));
59 static int sds_xfer_memory PARAMS ((CORE_ADDR, char *,
60 int, int, struct target_ops *));
62 static void sds_prepare_to_store PARAMS ((void));
64 static void sds_fetch_registers PARAMS ((int));
66 static void sds_resume PARAMS ((int, int, enum target_signal));
68 static int sds_start_remote PARAMS ((PTR));
70 static void sds_open PARAMS ((char *, int));
72 static void sds_close PARAMS ((int));
74 static void sds_store_registers PARAMS ((int));
76 static void sds_mourn PARAMS ((void));
78 static void sds_create_inferior PARAMS ((char *, char *, char **));
80 static void sds_load PARAMS ((char *, int));
82 static int getmessage PARAMS ((unsigned char *, int));
84 static int putmessage PARAMS ((unsigned char *, int));
86 static int sds_send PARAMS ((unsigned char *, int));
88 static int readchar PARAMS ((int));
90 static int sds_wait PARAMS ((int, struct target_waitstatus *));
92 static void sds_kill PARAMS ((void));
94 static int tohex PARAMS ((int));
96 static int fromhex PARAMS ((int));
98 static void sds_detach PARAMS ((char *, int));
100 static void sds_interrupt PARAMS ((int));
102 static void sds_interrupt_twice PARAMS ((int));
104 static void interrupt_query PARAMS ((void));
106 static int read_frame PARAMS ((char *));
108 static int sds_insert_breakpoint PARAMS ((CORE_ADDR, char *));
110 static int sds_remove_breakpoint PARAMS ((CORE_ADDR, char *));
112 static void init_sds_ops PARAMS ((void));
114 static void sds_command PARAMS ((char *args, int from_tty));
116 /* Define the target operations vector. */
118 static struct target_ops sds_ops;
120 /* This was 5 seconds, which is a long time to sit and wait.
121 Unless this is going though some terminal server or multiplexer or
122 other form of hairy serial connection, I would think 2 seconds would
125 static int sds_timeout = 2;
127 /* Descriptor for I/O to remote machine. Initialize it to NULL so
128 that sds_open knows that we don't have a file open when the program
131 static serial_t sds_desc = NULL;
133 /* This limit comes from the monitor. */
137 /* Maximum number of bytes to read/write at once. The value here
138 is chosen to fill up a packet (the headers account for the 32). */
139 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
141 static int next_msg_id;
143 static int just_started;
145 static int message_pending;
148 /* Clean up connection to a remote debugger. */
156 SERIAL_CLOSE (sds_desc);
160 /* Stub for catch_errors. */
163 sds_start_remote (dummy)
167 unsigned char buf[200];
169 immediate_quit = 1; /* Allow user to interrupt it */
171 /* Ack any packet which the remote side has already sent. */
172 SERIAL_WRITE (sds_desc, "{#*\r\n", 5);
173 SERIAL_WRITE (sds_desc, "{#}\r\n", 5);
175 while ((c = readchar (1)) >= 0)
176 printf_unfiltered ("%c", c);
177 printf_unfiltered ("\n");
189 start_remote (); /* Initialize gdb process mechanisms */
193 /* Open a connection to a remote debugger.
194 NAME is the filename used for communication. */
196 static DCACHE *sds_dcache;
199 sds_open (name, from_tty)
204 error ("To open a remote debug connection, you need to specify what serial\n\
205 device is attached to the remote system (e.g. /dev/ttya).");
207 target_preopen (from_tty);
209 unpush_target (&sds_ops);
211 sds_dcache = dcache_init (sds_read_bytes, sds_write_bytes);
213 sds_desc = SERIAL_OPEN (name);
215 perror_with_name (name);
219 if (SERIAL_SETBAUDRATE (sds_desc, baud_rate))
221 SERIAL_CLOSE (sds_desc);
222 perror_with_name (name);
227 SERIAL_RAW (sds_desc);
229 /* If there is something sitting in the buffer we might take it as a
230 response to a command, which would be bad. */
231 SERIAL_FLUSH_INPUT (sds_desc);
235 puts_filtered ("Remote debugging using ");
236 puts_filtered (name);
237 puts_filtered ("\n");
239 push_target (&sds_ops); /* Switch to using remote target now */
243 /* Start the remote connection; if error (0), discard this target.
244 In particular, if the user quits, be sure to discard it (we'd be
245 in an inconsistent state otherwise). */
246 if (!catch_errors (sds_start_remote, NULL,
247 "Couldn't establish connection to remote target\n",
252 /* This takes a program previously attached to and detaches it. After
253 this is done, GDB can be used to debug some other program. We
254 better not have left any breakpoints in the target program or it'll
255 die when it hits one. */
258 sds_detach (args, from_tty)
265 error ("Argument given to \"detach\" when remotely debugging.");
268 /* Tell the remote target to detach. */
275 puts_filtered ("Ending remote debugging.\n");
278 /* Convert hex digit A to a number. */
284 if (a >= '0' && a <= '9')
286 else if (a >= 'a' && a <= 'f')
289 error ("Reply contains invalid hex digit %d", a);
292 /* Convert number NIB to a hex digit. */
301 return 'a' + nib - 10;
305 tob64 (inbuf, outbuf, len)
306 unsigned char *inbuf;
314 error ("bad length");
317 for (i = 0; i < len; i += 3)
319 /* Collect the next three bytes into a number. */
320 sum = ((long) *inbuf++) << 16;
321 sum |= ((long) *inbuf++) << 8;
322 sum |= ((long) *inbuf++);
324 /* Spit out 4 6-bit encodings. */
325 *p++ = ((sum >> 18) & 0x3f) + '0';
326 *p++ = ((sum >> 12) & 0x3f) + '0';
327 *p++ = ((sum >> 6) & 0x3f) + '0';
328 *p++ = (sum & 0x3f) + '0';
334 fromb64 (inbuf, outbuf, len)
335 char *inbuf, *outbuf;
341 error ("bad length");
343 for (i = 0; i < len; i += 4)
345 /* Collect 4 6-bit digits. */
346 sum = (*inbuf++ - '0') << 18;
347 sum |= (*inbuf++ - '0') << 12;
348 sum |= (*inbuf++ - '0') << 6;
349 sum |= (*inbuf++ - '0');
351 /* Now take the resulting 24-bit number and get three bytes out
353 *outbuf++ = (sum >> 16) & 0xff;
354 *outbuf++ = (sum >> 8) & 0xff;
355 *outbuf++ = sum & 0xff;
358 return (len / 4) * 3;
362 /* Tell the remote machine to resume. */
364 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
368 sds_resume (pid, step, siggnal)
370 enum target_signal siggnal;
372 unsigned char buf[PBUFSIZ];
374 dcache_flush (sds_dcache);
376 last_sent_signal = siggnal;
377 last_sent_step = step;
379 buf[0] = (step ? 21 : 20);
380 buf[1] = 0; /* (should be signal?) */
385 /* Send a message to target to halt it. Target will respond, and send
386 us a message pending notice. */
389 sds_interrupt (signo)
392 unsigned char buf[PBUFSIZ];
394 /* If this doesn't work, try more severe steps. */
395 signal (signo, sds_interrupt_twice);
398 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
404 static void (*ofunc) ();
406 /* The user typed ^C twice. */
409 sds_interrupt_twice (signo)
412 signal (signo, ofunc);
416 signal (signo, sds_interrupt);
419 /* Ask the user what to do when an interrupt is received. */
424 target_terminal_ours ();
426 if (query ("Interrupted while waiting for the program.\n\
427 Give up (and stop debugging it)? "))
429 target_mourn_inferior ();
430 return_to_top_level (RETURN_QUIT);
433 target_terminal_inferior ();
436 /* If nonzero, ignore the next kill. */
439 /* Wait until the remote machine stops, then return, storing status in
440 STATUS just as `wait' would. Returns "pid" (though it's not clear
441 what, if anything, that means in the case of this target). */
444 sds_wait (pid, status)
446 struct target_waitstatus *status;
448 unsigned char buf[PBUFSIZ];
451 status->kind = TARGET_WAITKIND_EXITED;
452 status->value.integer = 0;
454 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
456 signal (SIGINT, ofunc);
461 status->kind = TARGET_WAITKIND_STOPPED;
472 retlen = sds_send (buf, 1);
475 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
480 status->kind = TARGET_WAITKIND_STOPPED;
481 status->value.sig = TARGET_SIGNAL_TRAP;
489 static unsigned char sprs[16];
491 /* Read the remote registers into the block REGS. */
492 /* Currently we just read all the registers, so we don't use regno. */
496 sds_fetch_registers (regno)
499 unsigned char buf[PBUFSIZ];
501 char regs[REGISTER_BYTES];
503 /* Unimplemented registers read as all bits zero. */
504 memset (regs, 0, REGISTER_BYTES);
509 retlen = sds_send (buf, 3);
511 for (i = 0; i < 4 * 6; ++i)
512 regs[i + 4 * 32 + 8 * 32] = buf[i];
513 for (i = 0; i < 4 * 4; ++i)
514 sprs[i] = buf[i + 4 * 7];
519 retlen = sds_send (buf, 3);
521 for (i = 0; i < retlen; i++)
524 /* (should warn about reply too short) */
526 for (i = 0; i < NUM_REGS; i++)
527 supply_register (i, ®s[REGISTER_BYTE (i)]);
530 /* Prepare to store registers. Since we may send them all, we have to
531 read out the ones we don't want to change first. */
534 sds_prepare_to_store ()
536 /* Make sure the entire registers array is valid. */
537 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
540 /* Store register REGNO, or all registers if REGNO == -1, from the contents
541 of REGISTERS. FIXME: ignores errors. */
544 sds_store_registers (regno)
547 unsigned char *p, buf[PBUFSIZ];
550 /* Store all the special-purpose registers. */
556 for (i = 0; i < 4 * 6; i++)
557 *p++ = registers[i + 4 * 32 + 8 * 32];
558 for (i = 0; i < 4 * 1; i++)
560 for (i = 0; i < 4 * 4; i++)
563 sds_send (buf, p - buf);
565 /* Store all the general-purpose registers. */
571 for (i = 0; i < 4 * 32; i++)
574 sds_send (buf, p - buf);
578 /* Write memory data directly to the remote machine. This does not
579 inform the data cache; the data cache uses this. MEMADDR is the
580 address in the remote memory space. MYADDR is the address of the
581 buffer in our space. LEN is the number of bytes.
583 Returns number of bytes transferred, or 0 for error. */
586 sds_write_bytes (memaddr, myaddr, len)
591 int max_buf_size; /* Max size of packet output buffer */
593 unsigned char buf[PBUFSIZ];
597 /* Chop the transfer down if necessary */
604 todo = min (len, max_buf_size);
608 buf[2] = (int) (memaddr >> 24) & 0xff;
609 buf[3] = (int) (memaddr >> 16) & 0xff;
610 buf[4] = (int) (memaddr >> 8) & 0xff;
611 buf[5] = (int) (memaddr) & 0xff;
615 for (i = 0; i < todo; i++)
616 buf[i + 8] = myaddr[i];
618 sds_send (buf, 8 + todo);
620 /* (should look at result) */
629 /* Read memory data directly from the remote machine. This does not
630 use the data cache; the data cache uses this. MEMADDR is the
631 address in the remote memory space. MYADDR is the address of the
632 buffer in our space. LEN is the number of bytes.
634 Returns number of bytes transferred, or 0 for error. */
637 sds_read_bytes (memaddr, myaddr, len)
642 int max_buf_size; /* Max size of packet output buffer */
644 unsigned char buf[PBUFSIZ];
648 /* Chop the transfer down if necessary */
655 todo = min (len, max_buf_size);
659 buf[2] = (int) (memaddr >> 24) & 0xff;
660 buf[3] = (int) (memaddr >> 16) & 0xff;
661 buf[4] = (int) (memaddr >> 8) & 0xff;
662 buf[5] = (int) (memaddr) & 0xff;
663 buf[6] = (int) (todo >> 8) & 0xff;
664 buf[7] = (int) (todo) & 0xff;
667 retlen = sds_send (buf, 9);
669 if (retlen - 2 != todo)
674 /* Reply describes memory byte by byte. */
676 for (i = 0; i < todo; i++)
677 myaddr[i] = buf[i + 2];
687 /* Read or write LEN bytes from inferior memory at MEMADDR,
688 transferring to or from debugger address MYADDR. Write to inferior
689 if SHOULD_WRITE is nonzero. Returns length of data written or
690 read; 0 for error. */
694 sds_xfer_memory (memaddr, myaddr, len, should_write, target)
699 struct target_ops *target; /* ignored */
701 return dcache_xfer_memory (sds_dcache, memaddr, myaddr, len, should_write);
706 sds_files_info (ignore)
707 struct target_ops *ignore;
709 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
712 /* Stuff for dealing with the packets which are part of this protocol.
713 See comment at top of file for details. */
715 /* Read a single character from the remote end, masking it down to 7 bits. */
723 ch = SERIAL_READCHAR (sds_desc, timeout);
725 if (remote_debug > 1 && ch >= 0)
726 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
731 error ("Remote connection closed");
733 perror_with_name ("Remote communication error");
741 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
742 because 253, 254, and 255 are special flags in the protocol.) */
745 compute_checksum (csum, buf, len)
751 for (i = 0; i < len; ++i)
752 csum += (unsigned char) buf[i];
758 /* Send the command in BUF to the remote machine, and read the reply
766 putmessage (buf, len);
768 return getmessage (buf, 0);
771 /* Send a message to the remote machine. */
774 putmessage (buf, len)
779 unsigned char csum = 0;
780 char buf2[PBUFSIZ], buf3[PBUFSIZ];
781 unsigned char header[3];
784 /* Copy the packet into buffer BUF2, encapsulating it
785 and giving it a checksum. */
787 if (len > 170) /* Prosanity check */
792 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
793 for (i = 0; i < len; ++i)
794 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
795 fprintf_unfiltered (gdb_stdlog, "\"\n");
807 header[1] = next_msg_id;
811 csum = compute_checksum (csum, buf, len);
812 csum = compute_checksum (csum, header + 1, 2);
816 tob64 (header, p, 3);
818 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
820 for (i = 0; i < enclen; ++i)
825 next_msg_id = (next_msg_id + 3) % 245;
827 /* Send it over and over until we get a positive ack. */
834 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
835 fprintf_unfiltered (gdb_stdlog,
836 " (Checksum %d, id %d, length %d)\n",
837 header[0], header[1], header[2]);
838 gdb_flush (gdb_stdlog);
840 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
841 perror_with_name ("putmessage: write failed");
847 /* Come here after finding the start of the frame. Collect the rest
848 into BUF. Returns 0 on any error, 1 on success. */
861 c = readchar (sds_timeout);
867 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
871 fputs_filtered ("Saw new packet start in middle of old one\n",
873 return 0; /* Start a new packet, count retries */
881 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
887 if (bp < buf + PBUFSIZ - 1)
894 puts_filtered ("Message too long: ");
896 puts_filtered ("\n");
903 /* Read a packet from the remote machine, with error checking,
904 and store it in BUF. BUF is expected to be of size PBUFSIZ.
905 If FOREVER, wait forever rather than timing out; this is used
906 while the target is executing user code. */
909 getmessage (buf, forever)
916 int val, i, len, csum;
917 unsigned char header[3];
918 unsigned char inbuf[500];
920 strcpy (buf, "timeout");
924 timeout = watchdog > 0 ? watchdog : -1;
928 timeout = sds_timeout;
932 for (tries = 1; tries <= MAX_TRIES; tries++)
934 /* This can loop forever if the remote side sends us characters
935 continuously, but if it pauses, we'll get a zero from readchar
936 because of timeout. Then we'll count that as a retry. */
938 /* Note that we will only wait forever prior to the start of a packet.
939 After that, we expect characters to arrive at a brisk pace. They
940 should show up within sds_timeout intervals. */
944 c = readchar (timeout);
946 if (c == SERIAL_TIMEOUT)
948 if (forever) /* Watchdog went off. Kill the target. */
950 target_mourn_inferior ();
951 error ("Watchdog has expired. Target detached.\n");
954 fputs_filtered ("Timed out.\n", gdb_stdlog);
958 while (c != '$' && c != '{');
960 /* We might have seen a "trigraph", a sequence of three characters
961 that indicate various sorts of communication state. */
965 /* Read the other two chars of the trigraph. */
966 c2 = readchar (timeout);
967 c3 = readchar (timeout);
969 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
979 val = read_frame (inbuf);
983 fromb64 (inbuf, header, 4);
984 /* (should check out other bits) */
985 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
990 csum = compute_checksum (csum, buf, len);
991 csum = compute_checksum (csum, header + 1, 2);
993 if (csum != header[0])
994 fprintf_unfiltered (gdb_stderr,
995 "Checksum mismatch: computed %d, received %d\n",
998 if (header[2] == 0xff)
999 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
1003 fprintf_unfiltered (gdb_stdlog,
1004 "... (Got checksum %d, id %d, length %d)\n",
1005 header[0], header[1], header[2]);
1006 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
1007 for (i = 0; i < len; ++i)
1009 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
1011 fprintf_unfiltered (gdb_stdlog, "\"\n");
1014 /* no ack required? */
1018 /* Try the whole thing again. */
1020 /* need to do something here */
1023 /* We have tried hard enough, and just can't receive the packet. Give up. */
1025 printf_unfiltered ("Ignoring packet error, continuing...\n");
1032 /* Don't try to do anything to the target. */
1038 unpush_target (&sds_ops);
1039 generic_mourn_inferior ();
1043 sds_create_inferior (exec_file, args, env)
1048 inferior_pid = 42000;
1050 /* Clean up from the last time we were running. */
1051 clear_proceed_status ();
1053 /* Let the remote process run. */
1054 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1058 sds_load (filename, from_tty)
1062 generic_load (filename, from_tty);
1067 /* The SDS monitor has commands for breakpoint insertion, although it
1068 it doesn't actually manage the breakpoints, it just returns the
1069 replaced instruction back to the debugger. */
1072 sds_insert_breakpoint (addr, contents_cache)
1074 char *contents_cache;
1077 unsigned char *p, buf[PBUFSIZ];
1082 *p++ = (int) (addr >> 24) & 0xff;
1083 *p++ = (int) (addr >> 16) & 0xff;
1084 *p++ = (int) (addr >> 8) & 0xff;
1085 *p++ = (int) (addr) & 0xff;
1087 retlen = sds_send (buf, p - buf);
1089 for (i = 0; i < 4; ++i)
1090 contents_cache[i] = buf[i + 2];
1096 sds_remove_breakpoint (addr, contents_cache)
1098 char *contents_cache;
1101 unsigned char *p, buf[PBUFSIZ];
1106 *p++ = (int) (addr >> 24) & 0xff;
1107 *p++ = (int) (addr >> 16) & 0xff;
1108 *p++ = (int) (addr >> 8) & 0xff;
1109 *p++ = (int) (addr) & 0xff;
1110 for (i = 0; i < 4; ++i)
1111 *p++ = contents_cache[i];
1113 retlen = sds_send (buf, p - buf);
1121 sds_ops.to_shortname = "sds";
1122 sds_ops.to_longname = "Remote serial target with SDS protocol";
1123 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1124 Specify the serial device it is connected to (e.g. /dev/ttya).";
1125 sds_ops.to_open = sds_open;
1126 sds_ops.to_close = sds_close;
1127 sds_ops.to_detach = sds_detach;
1128 sds_ops.to_resume = sds_resume;
1129 sds_ops.to_wait = sds_wait;
1130 sds_ops.to_fetch_registers = sds_fetch_registers;
1131 sds_ops.to_store_registers = sds_store_registers;
1132 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1133 sds_ops.to_xfer_memory = sds_xfer_memory;
1134 sds_ops.to_files_info = sds_files_info;
1135 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1136 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1137 sds_ops.to_kill = sds_kill;
1138 sds_ops.to_load = sds_load;
1139 sds_ops.to_create_inferior = sds_create_inferior;
1140 sds_ops.to_mourn_inferior = sds_mourn;
1141 sds_ops.to_stratum = process_stratum;
1142 sds_ops.to_has_all_memory = 1;
1143 sds_ops.to_has_memory = 1;
1144 sds_ops.to_has_stack = 1;
1145 sds_ops.to_has_registers = 1;
1146 sds_ops.to_has_execution = 1;
1147 sds_ops.to_magic = OPS_MAGIC;
1150 /* Put a command string, in args, out to the monitor and display the
1154 sds_command (args, from_tty)
1160 unsigned char buf[1000];
1162 /* Convert hexadecimal chars into a byte buffer. */
1167 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1173 retlen = sds_send (buf, len);
1175 printf_filtered ("Reply is ");
1176 for (i = 0; i < retlen; ++i)
1178 printf_filtered ("%02x", buf[i]);
1180 printf_filtered ("\n");
1184 _initialize_remote_sds ()
1187 add_target (&sds_ops);
1189 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1190 var_integer, (char *) &sds_timeout,
1191 "Set timeout value for sds read.\n", &setlist),
1194 add_com ("sds", class_obscure, sds_command,
1195 "Send a command to the SDS monitor.");