1 /* Remote target communications for serial-line targets using SDS' protocol.
3 Copyright 1997, 1998, 1999, 2000, 2001, 2002 Free Software
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 /* This interface was written by studying the behavior of the SDS
24 monitor on an ADS 821/860 board, and by consulting the
25 documentation of the monitor that is available on Motorola's web
29 #include "gdb_string.h"
38 #include "gdb-stabs.h"
39 #include "gdbthread.h"
44 #include <sys/types.h>
50 extern void _initialize_remote_sds (void);
52 /* Declarations of local functions. */
54 static int sds_write_bytes (CORE_ADDR, char *, int);
56 static int sds_read_bytes (CORE_ADDR, char *, int);
58 static void sds_files_info (struct target_ops *ignore);
60 static int sds_xfer_memory (CORE_ADDR, char *, int, int,
61 struct mem_attrib *, struct target_ops *);
63 static void sds_prepare_to_store (void);
65 static void sds_fetch_registers (int);
67 static void sds_resume (ptid_t, int, enum target_signal);
69 static int sds_start_remote (void *);
71 static void sds_open (char *, int);
73 static void sds_close (int);
75 static void sds_store_registers (int);
77 static void sds_mourn (void);
79 static void sds_create_inferior (char *, char *, char **);
81 static void sds_load (char *, int);
83 static int getmessage (unsigned char *, int);
85 static int putmessage (unsigned char *, int);
87 static int sds_send (unsigned char *, int);
89 static int readchar (int);
91 static ptid_t sds_wait (ptid_t, struct target_waitstatus *);
93 static void sds_kill (void);
95 static int tohex (int);
97 static int fromhex (int);
99 static void sds_detach (char *, int);
101 static void sds_interrupt (int);
103 static void sds_interrupt_twice (int);
105 static void interrupt_query (void);
107 static int read_frame (char *);
109 static int sds_insert_breakpoint (CORE_ADDR, char *);
111 static int sds_remove_breakpoint (CORE_ADDR, char *);
113 static void init_sds_ops (void);
115 static void sds_command (char *args, int from_tty);
117 /* Define the target operations vector. */
119 static struct target_ops sds_ops;
121 /* This was 5 seconds, which is a long time to sit and wait.
122 Unless this is going though some terminal server or multiplexer or
123 other form of hairy serial connection, I would think 2 seconds would
126 static int sds_timeout = 2;
128 /* Descriptor for I/O to remote machine. Initialize it to NULL so
129 that sds_open knows that we don't have a file open when the program
132 static struct serial *sds_desc = NULL;
134 /* This limit comes from the monitor. */
138 /* Maximum number of bytes to read/write at once. The value here
139 is chosen to fill up a packet (the headers account for the 32). */
140 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
142 static int next_msg_id;
144 static int just_started;
146 static int message_pending;
149 /* Clean up connection to a remote debugger. */
153 sds_close (int quitting)
156 serial_close (sds_desc);
160 /* Stub for catch_errors. */
163 sds_start_remote (void *dummy)
166 unsigned char buf[200];
168 immediate_quit++; /* 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. */
196 sds_open (char *name, int from_tty)
199 error ("To open a remote debug connection, you need to specify what serial\n\
200 device is attached to the remote system (e.g. /dev/ttya).");
202 target_preopen (from_tty);
204 unpush_target (&sds_ops);
206 sds_desc = serial_open (name);
208 perror_with_name (name);
212 if (serial_setbaudrate (sds_desc, baud_rate))
214 serial_close (sds_desc);
215 perror_with_name (name);
220 serial_raw (sds_desc);
222 /* If there is something sitting in the buffer we might take it as a
223 response to a command, which would be bad. */
224 serial_flush_input (sds_desc);
228 puts_filtered ("Remote debugging using ");
229 puts_filtered (name);
230 puts_filtered ("\n");
232 push_target (&sds_ops); /* Switch to using remote target now */
236 /* Start the remote connection; if error (0), discard this target.
237 In particular, if the user quits, be sure to discard it (we'd be
238 in an inconsistent state otherwise). */
239 if (!catch_errors (sds_start_remote, NULL,
240 "Couldn't establish connection to remote target\n",
245 /* This takes a program previously attached to and detaches it. After
246 this is done, GDB can be used to debug some other program. We
247 better not have left any breakpoints in the target program or it'll
248 die when it hits one. */
251 sds_detach (char *args, int from_tty)
256 error ("Argument given to \"detach\" when remotely debugging.");
259 /* Tell the remote target to detach. */
266 puts_filtered ("Ending remote debugging.\n");
269 /* Convert hex digit A to a number. */
274 if (a >= '0' && a <= '9')
276 else if (a >= 'a' && a <= 'f')
279 error ("Reply contains invalid hex digit %d", a);
282 /* Convert number NIB to a hex digit. */
290 return 'a' + nib - 10;
294 tob64 (unsigned char *inbuf, char *outbuf, int len)
300 error ("bad length");
303 for (i = 0; i < len; i += 3)
305 /* Collect the next three bytes into a number. */
306 sum = ((long) *inbuf++) << 16;
307 sum |= ((long) *inbuf++) << 8;
308 sum |= ((long) *inbuf++);
310 /* Spit out 4 6-bit encodings. */
311 *p++ = ((sum >> 18) & 0x3f) + '0';
312 *p++ = ((sum >> 12) & 0x3f) + '0';
313 *p++ = ((sum >> 6) & 0x3f) + '0';
314 *p++ = (sum & 0x3f) + '0';
320 fromb64 (char *inbuf, char *outbuf, int len)
325 error ("bad length");
327 for (i = 0; i < len; i += 4)
329 /* Collect 4 6-bit digits. */
330 sum = (*inbuf++ - '0') << 18;
331 sum |= (*inbuf++ - '0') << 12;
332 sum |= (*inbuf++ - '0') << 6;
333 sum |= (*inbuf++ - '0');
335 /* Now take the resulting 24-bit number and get three bytes out
337 *outbuf++ = (sum >> 16) & 0xff;
338 *outbuf++ = (sum >> 8) & 0xff;
339 *outbuf++ = sum & 0xff;
342 return (len / 4) * 3;
346 /* Tell the remote machine to resume. */
348 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
352 sds_resume (ptid_t ptid, int step, enum target_signal siggnal)
354 unsigned char buf[PBUFSIZ];
356 last_sent_signal = siggnal;
357 last_sent_step = step;
359 buf[0] = (step ? 21 : 20);
360 buf[1] = 0; /* (should be signal?) */
365 /* Send a message to target to halt it. Target will respond, and send
366 us a message pending notice. */
369 sds_interrupt (int signo)
371 unsigned char buf[PBUFSIZ];
373 /* If this doesn't work, try more severe steps. */
374 signal (signo, sds_interrupt_twice);
377 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
383 static void (*ofunc) ();
385 /* The user typed ^C twice. */
388 sds_interrupt_twice (int signo)
390 signal (signo, ofunc);
394 signal (signo, sds_interrupt);
397 /* Ask the user what to do when an interrupt is received. */
400 interrupt_query (void)
402 target_terminal_ours ();
404 if (query ("Interrupted while waiting for the program.\n\
405 Give up (and stop debugging it)? "))
407 target_mourn_inferior ();
408 throw_exception (RETURN_QUIT);
411 target_terminal_inferior ();
414 /* If nonzero, ignore the next kill. */
417 /* Wait until the remote machine stops, then return, storing status in
418 STATUS just as `wait' would. Returns "pid" (though it's not clear
419 what, if anything, that means in the case of this target). */
422 sds_wait (ptid_t ptid, struct target_waitstatus *status)
424 unsigned char buf[PBUFSIZ];
427 status->kind = TARGET_WAITKIND_EXITED;
428 status->value.integer = 0;
430 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
432 signal (SIGINT, ofunc);
437 status->kind = TARGET_WAITKIND_STOPPED;
438 return inferior_ptid;
448 retlen = sds_send (buf, 1);
451 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
456 status->kind = TARGET_WAITKIND_STOPPED;
457 status->value.sig = TARGET_SIGNAL_TRAP;
462 return inferior_ptid;
465 static unsigned char sprs[16];
467 /* Read the remote registers into the block REGS. */
468 /* Currently we just read all the registers, so we don't use regno. */
472 sds_fetch_registers (int regno)
474 unsigned char buf[PBUFSIZ];
476 char regs[REGISTER_BYTES];
478 /* Unimplemented registers read as all bits zero. */
479 memset (regs, 0, REGISTER_BYTES);
484 retlen = sds_send (buf, 3);
486 for (i = 0; i < 4 * 6; ++i)
487 regs[i + 4 * 32 + 8 * 32] = buf[i];
488 for (i = 0; i < 4 * 4; ++i)
489 sprs[i] = buf[i + 4 * 7];
494 retlen = sds_send (buf, 3);
496 for (i = 0; i < retlen; i++)
499 /* (should warn about reply too short) */
501 for (i = 0; i < NUM_REGS; i++)
502 supply_register (i, ®s[REGISTER_BYTE (i)]);
505 /* Prepare to store registers. Since we may send them all, we have to
506 read out the ones we don't want to change first. */
509 sds_prepare_to_store (void)
511 /* Make sure the entire registers array is valid. */
512 deprecated_read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
515 /* Store register REGNO, or all registers if REGNO == -1, from the contents
516 of REGISTERS. FIXME: ignores errors. */
519 sds_store_registers (int regno)
521 unsigned char *p, buf[PBUFSIZ];
524 /* Store all the special-purpose registers. */
530 for (i = 0; i < 4 * 6; i++)
531 *p++ = deprecated_registers[i + 4 * 32 + 8 * 32];
532 for (i = 0; i < 4 * 1; i++)
534 for (i = 0; i < 4 * 4; i++)
537 sds_send (buf, p - buf);
539 /* Store all the general-purpose registers. */
545 for (i = 0; i < 4 * 32; i++)
546 *p++ = deprecated_registers[i];
548 sds_send (buf, p - buf);
552 /* Write memory data directly to the remote machine. This does not
553 inform the data cache; the data cache uses this. MEMADDR is the
554 address in the remote memory space. MYADDR is the address of the
555 buffer in our space. LEN is the number of bytes.
557 Returns number of bytes transferred, or 0 for error. */
560 sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
562 int max_buf_size; /* Max size of packet output buffer */
564 unsigned char buf[PBUFSIZ];
568 /* Chop the transfer down if necessary */
575 todo = min (len, max_buf_size);
579 buf[2] = (int) (memaddr >> 24) & 0xff;
580 buf[3] = (int) (memaddr >> 16) & 0xff;
581 buf[4] = (int) (memaddr >> 8) & 0xff;
582 buf[5] = (int) (memaddr) & 0xff;
586 for (i = 0; i < todo; i++)
587 buf[i + 8] = myaddr[i];
589 sds_send (buf, 8 + todo);
591 /* (should look at result) */
600 /* Read memory data directly from the remote machine. This does not
601 use the data cache; the data cache uses this. MEMADDR is the
602 address in the remote memory space. MYADDR is the address of the
603 buffer in our space. LEN is the number of bytes.
605 Returns number of bytes transferred, or 0 for error. */
608 sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
610 int max_buf_size; /* Max size of packet output buffer */
612 unsigned char buf[PBUFSIZ];
616 /* Chop the transfer down if necessary */
623 todo = min (len, max_buf_size);
627 buf[2] = (int) (memaddr >> 24) & 0xff;
628 buf[3] = (int) (memaddr >> 16) & 0xff;
629 buf[4] = (int) (memaddr >> 8) & 0xff;
630 buf[5] = (int) (memaddr) & 0xff;
631 buf[6] = (int) (todo >> 8) & 0xff;
632 buf[7] = (int) (todo) & 0xff;
635 retlen = sds_send (buf, 9);
637 if (retlen - 2 != todo)
642 /* Reply describes memory byte by byte. */
644 for (i = 0; i < todo; i++)
645 myaddr[i] = buf[i + 2];
655 /* Read or write LEN bytes from inferior memory at MEMADDR,
656 transferring to or from debugger address MYADDR. Write to inferior
657 if SHOULD_WRITE is nonzero. Returns length of data written or
658 read; 0 for error. TARGET is unused. */
662 sds_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int should_write,
663 struct mem_attrib *attrib, struct target_ops *target)
668 res = sds_write_bytes (memaddr, myaddr, len);
670 res = sds_read_bytes (memaddr, myaddr, len);
677 sds_files_info (struct target_ops *ignore)
679 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
682 /* Stuff for dealing with the packets which are part of this protocol.
683 See comment at top of file for details. */
685 /* Read a single character from the remote end, masking it down to 7 bits. */
688 readchar (int timeout)
692 ch = serial_readchar (sds_desc, timeout);
694 if (remote_debug > 1 && ch >= 0)
695 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
700 error ("Remote connection closed");
702 perror_with_name ("Remote communication error");
710 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
711 because 253, 254, and 255 are special flags in the protocol.) */
714 compute_checksum (int csum, char *buf, int len)
718 for (i = 0; i < len; ++i)
719 csum += (unsigned char) buf[i];
725 /* Send the command in BUF to the remote machine, and read the reply
729 sds_send (unsigned char *buf, int len)
731 putmessage (buf, len);
733 return getmessage (buf, 0);
736 /* Send a message to the remote machine. */
739 putmessage (unsigned char *buf, int len)
742 unsigned char csum = 0;
743 char buf2[PBUFSIZ], buf3[PBUFSIZ];
744 unsigned char header[3];
747 /* Copy the packet into buffer BUF2, encapsulating it
748 and giving it a checksum. */
750 if (len > 170) /* Prosanity check */
751 internal_error (__FILE__, __LINE__, "failed internal consistency check");
755 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
756 for (i = 0; i < len; ++i)
757 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
758 fprintf_unfiltered (gdb_stdlog, "\"\n");
770 header[1] = next_msg_id;
774 csum = compute_checksum (csum, buf, len);
775 csum = compute_checksum (csum, header + 1, 2);
779 tob64 (header, p, 3);
781 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
783 for (i = 0; i < enclen; ++i)
788 next_msg_id = (next_msg_id + 3) % 245;
790 /* Send it over and over until we get a positive ack. */
797 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
798 fprintf_unfiltered (gdb_stdlog,
799 " (Checksum %d, id %d, length %d)\n",
800 header[0], header[1], header[2]);
801 gdb_flush (gdb_stdlog);
803 if (serial_write (sds_desc, buf2, p - buf2))
804 perror_with_name ("putmessage: write failed");
810 /* Come here after finding the start of the frame. Collect the rest
811 into BUF. Returns 0 on any error, 1 on success. */
814 read_frame (char *buf)
823 c = readchar (sds_timeout);
829 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
833 fputs_filtered ("Saw new packet start in middle of old one\n",
835 return 0; /* Start a new packet, count retries */
843 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
849 if (bp < buf + PBUFSIZ - 1)
856 puts_filtered ("Message too long: ");
858 puts_filtered ("\n");
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 getmessage (unsigned char *buf, int forever)
876 int val, i, len, csum;
877 unsigned char header[3];
878 unsigned char inbuf[500];
880 strcpy (buf, "timeout");
884 timeout = watchdog > 0 ? watchdog : -1;
888 timeout = sds_timeout;
892 for (tries = 1; tries <= MAX_TRIES; tries++)
894 /* This can loop forever if the remote side sends us characters
895 continuously, but if it pauses, we'll get a zero from readchar
896 because of timeout. Then we'll count that as a retry. */
898 /* Note that we will only wait forever prior to the start of a packet.
899 After that, we expect characters to arrive at a brisk pace. They
900 should show up within sds_timeout intervals. */
904 c = readchar (timeout);
906 if (c == SERIAL_TIMEOUT)
908 if (forever) /* Watchdog went off. Kill the target. */
910 target_mourn_inferior ();
911 error ("Watchdog has expired. Target detached.\n");
914 fputs_filtered ("Timed out.\n", gdb_stdlog);
918 while (c != '$' && c != '{');
920 /* We might have seen a "trigraph", a sequence of three characters
921 that indicate various sorts of communication state. */
925 /* Read the other two chars of the trigraph. */
926 c2 = readchar (timeout);
927 c3 = readchar (timeout);
929 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
939 val = read_frame (inbuf);
943 fromb64 (inbuf, header, 4);
944 /* (should check out other bits) */
945 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
950 csum = compute_checksum (csum, buf, len);
951 csum = compute_checksum (csum, header + 1, 2);
953 if (csum != header[0])
954 fprintf_unfiltered (gdb_stderr,
955 "Checksum mismatch: computed %d, received %d\n",
958 if (header[2] == 0xff)
959 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
963 fprintf_unfiltered (gdb_stdlog,
964 "... (Got checksum %d, id %d, length %d)\n",
965 header[0], header[1], header[2]);
966 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
967 for (i = 0; i < len; ++i)
969 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
971 fprintf_unfiltered (gdb_stdlog, "\"\n");
974 /* no ack required? */
978 /* Try the whole thing again. */
980 /* need to do something here */
984 /* We have tried hard enough, and just can't receive the packet. Give up. */
986 printf_unfiltered ("Ignoring packet error, continuing...\n");
993 /* Don't try to do anything to the target. */
999 unpush_target (&sds_ops);
1000 generic_mourn_inferior ();
1004 sds_create_inferior (char *exec_file, char *args, char **env)
1006 inferior_ptid = pid_to_ptid (42000);
1008 /* Clean up from the last time we were running. */
1009 clear_proceed_status ();
1011 /* Let the remote process run. */
1012 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1016 sds_load (char *filename, int from_tty)
1018 generic_load (filename, from_tty);
1020 inferior_ptid = null_ptid;
1023 /* The SDS monitor has commands for breakpoint insertion, although it
1024 it doesn't actually manage the breakpoints, it just returns the
1025 replaced instruction back to the debugger. */
1028 sds_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
1031 unsigned char *p, buf[PBUFSIZ];
1036 *p++ = (int) (addr >> 24) & 0xff;
1037 *p++ = (int) (addr >> 16) & 0xff;
1038 *p++ = (int) (addr >> 8) & 0xff;
1039 *p++ = (int) (addr) & 0xff;
1041 retlen = sds_send (buf, p - buf);
1043 for (i = 0; i < 4; ++i)
1044 contents_cache[i] = buf[i + 2];
1050 sds_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
1053 unsigned char *p, buf[PBUFSIZ];
1058 *p++ = (int) (addr >> 24) & 0xff;
1059 *p++ = (int) (addr >> 16) & 0xff;
1060 *p++ = (int) (addr >> 8) & 0xff;
1061 *p++ = (int) (addr) & 0xff;
1062 for (i = 0; i < 4; ++i)
1063 *p++ = contents_cache[i];
1065 retlen = sds_send (buf, p - buf);
1073 sds_ops.to_shortname = "sds";
1074 sds_ops.to_longname = "Remote serial target with SDS protocol";
1075 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1076 Specify the serial device it is connected to (e.g. /dev/ttya).";
1077 sds_ops.to_open = sds_open;
1078 sds_ops.to_close = sds_close;
1079 sds_ops.to_detach = sds_detach;
1080 sds_ops.to_resume = sds_resume;
1081 sds_ops.to_wait = sds_wait;
1082 sds_ops.to_fetch_registers = sds_fetch_registers;
1083 sds_ops.to_store_registers = sds_store_registers;
1084 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1085 sds_ops.to_xfer_memory = sds_xfer_memory;
1086 sds_ops.to_files_info = sds_files_info;
1087 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1088 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1089 sds_ops.to_kill = sds_kill;
1090 sds_ops.to_load = sds_load;
1091 sds_ops.to_create_inferior = sds_create_inferior;
1092 sds_ops.to_mourn_inferior = sds_mourn;
1093 sds_ops.to_stratum = process_stratum;
1094 sds_ops.to_has_all_memory = 1;
1095 sds_ops.to_has_memory = 1;
1096 sds_ops.to_has_stack = 1;
1097 sds_ops.to_has_registers = 1;
1098 sds_ops.to_has_execution = 1;
1099 sds_ops.to_magic = OPS_MAGIC;
1102 /* Put a command string, in args, out to the monitor and display the
1106 sds_command (char *args, int from_tty)
1110 unsigned char buf[1000];
1112 /* Convert hexadecimal chars into a byte buffer. */
1117 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1123 retlen = sds_send (buf, len);
1125 printf_filtered ("Reply is ");
1126 for (i = 0; i < retlen; ++i)
1128 printf_filtered ("%02x", buf[i]);
1130 printf_filtered ("\n");
1134 _initialize_remote_sds (void)
1137 add_target (&sds_ops);
1139 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1140 var_integer, (char *) &sds_timeout,
1141 "Set timeout value for sds read.\n", &setlist),
1144 add_com ("sds", class_obscure, sds_command,
1145 "Send a command to the SDS monitor.");