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, Boston, MA 02111-1307, USA. */
20 /* This interface was written by studying the behavior of the SDS
21 monitor on an ADS 821/860 board, and by consulting the
22 documentation of the monitor that is available on Motorola's web
26 #include "gdb_string.h"
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
42 #include <sys/types.h>
48 extern void _initialize_remote_sds PARAMS ((void));
50 /* Declarations of local functions. */
52 static int sds_write_bytes PARAMS ((CORE_ADDR, char *, int));
54 static int sds_read_bytes PARAMS ((CORE_ADDR, char *, int));
56 static void sds_files_info PARAMS ((struct target_ops *ignore));
58 static int sds_xfer_memory PARAMS ((CORE_ADDR, char *,
59 int, int, struct target_ops *));
61 static void sds_prepare_to_store PARAMS ((void));
63 static void sds_fetch_registers PARAMS ((int));
65 static void sds_resume PARAMS ((int, int, enum target_signal));
67 static int sds_start_remote PARAMS ((PTR));
69 static void sds_open PARAMS ((char *, int));
71 static void sds_close PARAMS ((int));
73 static void sds_store_registers PARAMS ((int));
75 static void sds_mourn PARAMS ((void));
77 static void sds_create_inferior PARAMS ((char *, char *, char **));
79 static void sds_load PARAMS ((char *, int));
81 static int getmessage PARAMS ((unsigned char *, int));
83 static int putmessage PARAMS ((unsigned char *, int));
85 static int sds_send PARAMS ((unsigned char *, int));
87 static int readchar PARAMS ((int));
89 static int sds_wait PARAMS ((int, struct target_waitstatus *));
91 static void sds_kill PARAMS ((void));
93 static int tohex PARAMS ((int));
95 static int fromhex PARAMS ((int));
97 static void sds_detach PARAMS ((char *, int));
99 static void sds_interrupt PARAMS ((int));
101 static void sds_interrupt_twice PARAMS ((int));
103 static void interrupt_query PARAMS ((void));
105 static int read_frame PARAMS ((char *));
107 static int sds_insert_breakpoint PARAMS ((CORE_ADDR, char *));
109 static int sds_remove_breakpoint PARAMS ((CORE_ADDR, char *));
111 static void init_sds_ops PARAMS ((void));
113 static void sds_command PARAMS ((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. */
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 printf_unfiltered ("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_stderr, "Signals: %04x %02x %02x\n",
475 ((int) buf[0]) << 8 + buf[1],
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];
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 printf_unfiltered("%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];
786 /* Copy the packet into buffer BUF2, encapsulating it
787 and giving it a checksum. */
789 if (len > 170) /* Prosanity check */
794 fprintf_unfiltered (gdb_stderr, "Message to send: \"");
795 for (i = 0; i < len; ++i)
796 fprintf_unfiltered (gdb_stderr, "%02x", buf[i]);
797 fprintf_unfiltered (gdb_stderr, "\"\n");
809 header[1] = next_msg_id;
813 csum = compute_checksum (csum, buf, len);
814 csum = compute_checksum (csum, header + 1, 2);
818 tob64 (header, p, 3);
820 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
822 for (i = 0; i < enclen; ++i)
827 next_msg_id = (next_msg_id + 3) % 245;
829 /* Send it over and over until we get a positive ack. */
833 int started_error_output = 0;
838 printf_unfiltered ("Sending encoded: \"%s\"", buf2);
839 printf_unfiltered (" (Checksum %d, id %d, length %d)\n",
840 header[0], header[1], header[2]);
841 gdb_flush (gdb_stdout);
843 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
844 perror_with_name ("putmessage: write failed");
852 /* Come here after finding the start of the frame. Collect the rest
853 into BUF. Returns 0 on any error, 1 on success. */
866 c = readchar (sds_timeout);
872 puts_filtered ("Timeout in mid-message, retrying\n");
876 puts_filtered ("Saw new packet start in middle of old one\n");
877 return 0; /* Start a new packet, count retries */
885 fprintf_unfiltered (gdb_stderr, "Received encoded: \"%s\"\n",
891 if (bp < buf + PBUFSIZ - 1)
898 puts_filtered ("Message too long: ");
900 puts_filtered ("\n");
907 /* Read a packet from the remote machine, with error checking,
908 and store it in BUF. BUF is expected to be of size PBUFSIZ.
909 If FOREVER, wait forever rather than timing out; this is used
910 while the target is executing user code. */
913 getmessage (buf, forever)
920 int val, i, len, csum;
921 unsigned char header[3];
922 unsigned char inbuf[500];
924 strcpy (buf, "timeout");
928 #ifdef MAINTENANCE_CMDS
929 timeout = watchdog > 0 ? watchdog : -1;
936 timeout = sds_timeout;
940 for (tries = 1; tries <= MAX_TRIES; tries++)
942 /* This can loop forever if the remote side sends us characters
943 continuously, but if it pauses, we'll get a zero from readchar
944 because of timeout. Then we'll count that as a retry. */
946 /* Note that we will only wait forever prior to the start of a packet.
947 After that, we expect characters to arrive at a brisk pace. They
948 should show up within sds_timeout intervals. */
952 c = readchar (timeout);
954 if (c == SERIAL_TIMEOUT)
956 #ifdef MAINTENANCE_CMDS
957 if (forever) /* Watchdog went off. Kill the target. */
959 target_mourn_inferior ();
960 error ("Watchdog has expired. Target detached.\n");
964 puts_filtered ("Timed out.\n");
968 while (c != '$' && c != '{');
970 /* We might have seen a "trigraph", a sequence of three characters
971 that indicate various sorts of communication state. */
975 /* Read the other two chars of the trigraph. */
976 c2 = readchar (timeout);
977 c3 = readchar (timeout);
979 fprintf_unfiltered (gdb_stderr, "Trigraph %c%c%c received\n",
989 val = read_frame (inbuf);
993 fromb64 (inbuf, header, 4);
994 /* (should check out other bits) */
995 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
1000 csum = compute_checksum (csum, buf, len);
1001 csum = compute_checksum (csum, header + 1, 2);
1003 if (csum != header[0])
1004 fprintf_unfiltered (gdb_stderr,
1005 "Checksum mismatch: computed %d, received %d\n",
1008 if (header[2] == 0xff)
1009 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
1013 fprintf_unfiltered (gdb_stderr,
1014 "... (Got checksum %d, id %d, length %d)\n",
1015 header[0], header[1], header[2]);
1016 fprintf_unfiltered (gdb_stderr, "Message received: \"");
1017 for (i = 0; i < len; ++i)
1019 fprintf_unfiltered (gdb_stderr, "%02x", (unsigned char) buf[i]);
1021 fprintf_unfiltered (gdb_stderr, "\"\n");
1024 /* no ack required? */
1028 /* Try the whole thing again. */
1030 /* need to do something here */
1033 /* We have tried hard enough, and just can't receive the packet. Give up. */
1035 printf_unfiltered ("Ignoring packet error, continuing...\n");
1042 /* Don't try to do anything to the target. */
1048 unpush_target (&sds_ops);
1049 generic_mourn_inferior ();
1053 sds_create_inferior (exec_file, args, env)
1058 inferior_pid = 42000;
1060 /* Clean up from the last time we were running. */
1061 clear_proceed_status ();
1063 /* Let the remote process run. */
1064 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1068 sds_load (filename, from_tty)
1072 generic_load (filename, from_tty);
1077 /* The SDS monitor has commands for breakpoint insertion, although it
1078 it doesn't actually manage the breakpoints, it just returns the
1079 replaced instruction back to the debugger. */
1082 sds_insert_breakpoint (addr, contents_cache)
1084 char *contents_cache;
1087 unsigned char *p, buf[PBUFSIZ];
1092 *p++ = (int) (addr >> 24) & 0xff;
1093 *p++ = (int) (addr >> 16) & 0xff;
1094 *p++ = (int) (addr >> 8) & 0xff;
1095 *p++ = (int) (addr ) & 0xff;
1097 retlen = sds_send (buf, p - buf);
1099 for (i = 0; i < 4; ++i)
1100 contents_cache[i] = buf[i + 2];
1106 sds_remove_breakpoint (addr, contents_cache)
1108 char *contents_cache;
1111 unsigned char *p, buf[PBUFSIZ];
1116 *p++ = (int) (addr >> 24) & 0xff;
1117 *p++ = (int) (addr >> 16) & 0xff;
1118 *p++ = (int) (addr >> 8) & 0xff;
1119 *p++ = (int) (addr ) & 0xff;
1120 for (i = 0; i < 4; ++i)
1121 *p++ = contents_cache[i];
1123 retlen = sds_send (buf, p - buf);
1131 sds_ops.to_shortname = "sds";
1132 sds_ops.to_longname = "Remote serial target with SDS protocol";
1133 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1134 Specify the serial device it is connected to (e.g. /dev/ttya).";
1135 sds_ops.to_open = sds_open;
1136 sds_ops.to_close = sds_close;
1137 sds_ops.to_detach = sds_detach;
1138 sds_ops.to_resume = sds_resume;
1139 sds_ops.to_wait = sds_wait;
1140 sds_ops.to_fetch_registers = sds_fetch_registers;
1141 sds_ops.to_store_registers = sds_store_registers;
1142 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1143 sds_ops.to_xfer_memory = sds_xfer_memory;
1144 sds_ops.to_files_info = sds_files_info;
1145 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1146 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1147 sds_ops.to_kill = sds_kill;
1148 sds_ops.to_load = sds_load;
1149 sds_ops.to_create_inferior = sds_create_inferior;
1150 sds_ops.to_mourn_inferior = sds_mourn;
1151 sds_ops.to_stratum = process_stratum;
1152 sds_ops.to_has_all_memory = 1;
1153 sds_ops.to_has_memory = 1;
1154 sds_ops.to_has_stack = 1;
1155 sds_ops.to_has_registers = 1;
1156 sds_ops.to_has_execution = 1;
1157 sds_ops.to_magic = OPS_MAGIC;
1160 /* Put a command string, in args, out to the monitor and display the
1164 sds_command (args, from_tty)
1170 unsigned char buf[1000];
1172 /* Convert hexadecimal chars into a byte buffer. */
1177 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1183 retlen = sds_send (buf, len);
1185 printf_filtered ("Reply is ");
1186 for (i = 0; i < retlen; ++i)
1188 printf_filtered ("%02x", buf[i]);
1190 printf_filtered ("\n");
1194 _initialize_remote_sds ()
1197 add_target (&sds_ops);
1199 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1200 var_integer, (char *)&sds_timeout,
1201 "Set timeout value for sds read.\n", &setlist),
1204 add_com ("sds", class_obscure, sds_command,
1205 "Send a command to the SDS monitor.");