1 /* Remote target communications for serial-line targets using SDS' protocol.
3 Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2004 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"
33 #include "exceptions.h"
39 #include "gdb-stabs.h"
40 #include "gdbthread.h"
47 extern void _initialize_remote_sds (void);
49 /* Declarations of local functions. */
51 static int sds_write_bytes (CORE_ADDR, char *, int);
53 static int sds_read_bytes (CORE_ADDR, char *, int);
55 static void sds_files_info (struct target_ops *ignore);
57 static int sds_xfer_memory (CORE_ADDR, char *, int, int,
58 struct mem_attrib *, struct target_ops *);
60 static void sds_prepare_to_store (void);
62 static void sds_fetch_registers (int);
64 static void sds_resume (ptid_t, int, enum target_signal);
66 static int sds_start_remote (void *);
68 static void sds_open (char *, int);
70 static void sds_close (int);
72 static void sds_store_registers (int);
74 static void sds_mourn (void);
76 static void sds_load (char *, int);
78 static int getmessage (unsigned char *, int);
80 static int putmessage (unsigned char *, int);
82 static int sds_send (unsigned char *, int);
84 static int readchar (int);
86 static ptid_t sds_wait (ptid_t, struct target_waitstatus *);
88 static void sds_kill (void);
90 static int fromhex (int);
92 static void sds_detach (char *, int);
94 static void sds_interrupt (int);
96 static void sds_interrupt_twice (int);
98 static void interrupt_query (void);
100 static int read_frame (char *);
102 static int sds_insert_breakpoint (CORE_ADDR, char *);
104 static int sds_remove_breakpoint (CORE_ADDR, char *);
106 static void init_sds_ops (void);
108 static void sds_command (char *args, int from_tty);
110 /* Define the target operations vector. */
112 static struct target_ops sds_ops;
114 /* This was 5 seconds, which is a long time to sit and wait.
115 Unless this is going though some terminal server or multiplexer or
116 other form of hairy serial connection, I would think 2 seconds would
119 static int sds_timeout = 2;
121 /* Descriptor for I/O to remote machine. Initialize it to NULL so
122 that sds_open knows that we don't have a file open when the program
125 static struct serial *sds_desc = NULL;
127 /* This limit comes from the monitor. */
131 /* Maximum number of bytes to read/write at once. The value here
132 is chosen to fill up a packet (the headers account for the 32). */
133 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
135 static int next_msg_id;
137 static int just_started;
139 static int message_pending;
142 /* Clean up connection to a remote debugger. */
145 sds_close (int quitting)
148 serial_close (sds_desc);
152 /* Stub for catch_errors. */
155 sds_start_remote (void *dummy)
158 unsigned char buf[200];
160 immediate_quit++; /* Allow user to interrupt it */
162 /* Ack any packet which the remote side has already sent. */
163 serial_write (sds_desc, "{#*\r\n", 5);
164 serial_write (sds_desc, "{#}\r\n", 5);
166 while ((c = readchar (1)) >= 0)
167 printf_unfiltered ("%c", c);
168 printf_unfiltered ("\n");
180 start_remote (); /* Initialize gdb process mechanisms */
184 /* Open a connection to a remote debugger.
185 NAME is the filename used for communication. */
188 sds_open (char *name, int from_tty)
191 error ("To open a remote debug connection, you need to specify what serial\n\
192 device is attached to the remote system (e.g. /dev/ttya).");
194 target_preopen (from_tty);
196 unpush_target (&sds_ops);
198 sds_desc = serial_open (name);
200 perror_with_name (name);
204 if (serial_setbaudrate (sds_desc, baud_rate))
206 serial_close (sds_desc);
207 perror_with_name (name);
212 serial_raw (sds_desc);
214 /* If there is something sitting in the buffer we might take it as a
215 response to a command, which would be bad. */
216 serial_flush_input (sds_desc);
220 puts_filtered ("Remote debugging using ");
221 puts_filtered (name);
222 puts_filtered ("\n");
224 push_target (&sds_ops); /* Switch to using remote target now */
228 /* Start the remote connection; if error (0), discard this target.
229 In particular, if the user quits, be sure to discard it (we'd be
230 in an inconsistent state otherwise). */
231 if (!catch_errors (sds_start_remote, NULL,
232 "Couldn't establish connection to remote target\n",
237 /* This takes a program previously attached to and detaches it. After
238 this is done, GDB can be used to debug some other program. We
239 better not have left any breakpoints in the target program or it'll
240 die when it hits one. */
243 sds_detach (char *args, int from_tty)
248 error ("Argument given to \"detach\" when remotely debugging.");
251 /* Tell the remote target to detach. */
258 puts_filtered ("Ending remote debugging.\n");
261 /* Convert hex digit A to a number. */
266 if (a >= '0' && a <= '9')
268 else if (a >= 'a' && a <= 'f')
271 error ("Reply contains invalid hex digit %d", a);
275 tob64 (unsigned char *inbuf, char *outbuf, int len)
281 error ("bad length");
284 for (i = 0; i < len; i += 3)
286 /* Collect the next three bytes into a number. */
287 sum = ((long) *inbuf++) << 16;
288 sum |= ((long) *inbuf++) << 8;
289 sum |= ((long) *inbuf++);
291 /* Spit out 4 6-bit encodings. */
292 *p++ = ((sum >> 18) & 0x3f) + '0';
293 *p++ = ((sum >> 12) & 0x3f) + '0';
294 *p++ = ((sum >> 6) & 0x3f) + '0';
295 *p++ = (sum & 0x3f) + '0';
301 fromb64 (char *inbuf, char *outbuf, int len)
306 error ("bad length");
308 for (i = 0; i < len; i += 4)
310 /* Collect 4 6-bit digits. */
311 sum = (*inbuf++ - '0') << 18;
312 sum |= (*inbuf++ - '0') << 12;
313 sum |= (*inbuf++ - '0') << 6;
314 sum |= (*inbuf++ - '0');
316 /* Now take the resulting 24-bit number and get three bytes out
318 *outbuf++ = (sum >> 16) & 0xff;
319 *outbuf++ = (sum >> 8) & 0xff;
320 *outbuf++ = sum & 0xff;
323 return (len / 4) * 3;
327 /* Tell the remote machine to resume. */
329 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
333 sds_resume (ptid_t ptid, int step, enum target_signal siggnal)
335 unsigned char buf[PBUFSIZ];
337 last_sent_signal = siggnal;
338 last_sent_step = step;
340 buf[0] = (step ? 21 : 20);
341 buf[1] = 0; /* (should be signal?) */
346 /* Send a message to target to halt it. Target will respond, and send
347 us a message pending notice. */
350 sds_interrupt (int signo)
352 unsigned char buf[PBUFSIZ];
354 /* If this doesn't work, try more severe steps. */
355 signal (signo, sds_interrupt_twice);
358 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
364 static void (*ofunc) ();
366 /* The user typed ^C twice. */
369 sds_interrupt_twice (int signo)
371 signal (signo, ofunc);
375 signal (signo, sds_interrupt);
378 /* Ask the user what to do when an interrupt is received. */
381 interrupt_query (void)
383 target_terminal_ours ();
385 if (query ("Interrupted while waiting for the program.\n\
386 Give up (and stop debugging it)? "))
388 target_mourn_inferior ();
389 deprecated_throw_reason (RETURN_QUIT);
392 target_terminal_inferior ();
395 /* If nonzero, ignore the next kill. */
398 /* Wait until the remote machine stops, then return, storing status in
399 STATUS just as `wait' would. Returns "pid" (though it's not clear
400 what, if anything, that means in the case of this target). */
403 sds_wait (ptid_t ptid, struct target_waitstatus *status)
405 unsigned char buf[PBUFSIZ];
408 status->kind = TARGET_WAITKIND_EXITED;
409 status->value.integer = 0;
411 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
413 signal (SIGINT, ofunc);
418 status->kind = TARGET_WAITKIND_STOPPED;
419 return inferior_ptid;
429 retlen = sds_send (buf, 1);
432 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
437 status->kind = TARGET_WAITKIND_STOPPED;
438 status->value.sig = TARGET_SIGNAL_TRAP;
443 return inferior_ptid;
446 static unsigned char sprs[16];
448 /* Read the remote registers into the block REGS. */
449 /* Currently we just read all the registers, so we don't use regno. */
452 sds_fetch_registers (int regno)
454 unsigned char buf[PBUFSIZ];
456 char *regs = alloca (deprecated_register_bytes ());
458 /* Unimplemented registers read as all bits zero. */
459 memset (regs, 0, deprecated_register_bytes ());
464 retlen = sds_send (buf, 3);
466 for (i = 0; i < 4 * 6; ++i)
467 regs[i + 4 * 32 + 8 * 32] = buf[i];
468 for (i = 0; i < 4 * 4; ++i)
469 sprs[i] = buf[i + 4 * 7];
474 retlen = sds_send (buf, 3);
476 for (i = 0; i < retlen; i++)
479 /* (should warn about reply too short) */
481 for (i = 0; i < NUM_REGS; i++)
482 regcache_raw_supply (current_regcache, i,
483 ®s[DEPRECATED_REGISTER_BYTE (i)]);
486 /* Prepare to store registers. Since we may send them all, we have to
487 read out the ones we don't want to change first. */
490 sds_prepare_to_store (void)
492 /* Make sure the entire registers array is valid. */
493 deprecated_read_register_bytes (0, (char *) NULL, deprecated_register_bytes ());
496 /* Store register REGNO, or all registers if REGNO == -1, from the contents
497 of REGISTERS. FIXME: ignores errors. */
500 sds_store_registers (int regno)
502 unsigned char *p, buf[PBUFSIZ];
505 /* Store all the special-purpose registers. */
511 for (i = 0; i < 4 * 6; i++)
512 *p++ = deprecated_registers[i + 4 * 32 + 8 * 32];
513 for (i = 0; i < 4 * 1; i++)
515 for (i = 0; i < 4 * 4; i++)
518 sds_send (buf, p - buf);
520 /* Store all the general-purpose registers. */
526 for (i = 0; i < 4 * 32; i++)
527 *p++ = deprecated_registers[i];
529 sds_send (buf, p - buf);
533 /* Write memory data directly to the remote machine. This does not
534 inform the data cache; the data cache uses this. MEMADDR is the
535 address in the remote memory space. MYADDR is the address of the
536 buffer in our space. LEN is the number of bytes.
538 Returns number of bytes transferred, or 0 for error. */
541 sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
543 int max_buf_size; /* Max size of packet output buffer */
545 unsigned char buf[PBUFSIZ];
549 /* Chop the transfer down if necessary */
556 todo = min (len, max_buf_size);
560 buf[2] = (int) (memaddr >> 24) & 0xff;
561 buf[3] = (int) (memaddr >> 16) & 0xff;
562 buf[4] = (int) (memaddr >> 8) & 0xff;
563 buf[5] = (int) (memaddr) & 0xff;
567 for (i = 0; i < todo; i++)
568 buf[i + 8] = myaddr[i];
570 sds_send (buf, 8 + todo);
572 /* (should look at result) */
581 /* Read memory data directly from the remote machine. This does not
582 use the data cache; the data cache uses this. MEMADDR is the
583 address in the remote memory space. MYADDR is the address of the
584 buffer in our space. LEN is the number of bytes.
586 Returns number of bytes transferred, or 0 for error. */
589 sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int 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;
612 buf[6] = (int) (todo >> 8) & 0xff;
613 buf[7] = (int) (todo) & 0xff;
616 retlen = sds_send (buf, 9);
618 if (retlen - 2 != todo)
623 /* Reply describes memory byte by byte. */
625 for (i = 0; i < todo; i++)
626 myaddr[i] = buf[i + 2];
636 /* Read or write LEN bytes from inferior memory at MEMADDR,
637 transferring to or from debugger address MYADDR. Write to inferior
638 if SHOULD_WRITE is nonzero. Returns length of data written or
639 read; 0 for error. TARGET is unused. */
642 sds_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int should_write,
643 struct mem_attrib *attrib, struct target_ops *target)
648 res = sds_write_bytes (memaddr, myaddr, len);
650 res = sds_read_bytes (memaddr, myaddr, len);
657 sds_files_info (struct target_ops *ignore)
659 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
662 /* Stuff for dealing with the packets which are part of this protocol.
663 See comment at top of file for details. */
665 /* Read a single character from the remote end, masking it down to 7 bits. */
668 readchar (int timeout)
672 ch = serial_readchar (sds_desc, timeout);
674 if (remote_debug > 1 && ch >= 0)
675 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
680 error ("Remote connection closed");
682 perror_with_name ("Remote communication error");
690 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
691 because 253, 254, and 255 are special flags in the protocol.) */
694 compute_checksum (int csum, char *buf, int len)
698 for (i = 0; i < len; ++i)
699 csum += (unsigned char) buf[i];
705 /* Send the command in BUF to the remote machine, and read the reply
709 sds_send (unsigned char *buf, int len)
711 putmessage (buf, len);
713 return getmessage (buf, 0);
716 /* Send a message to the remote machine. */
719 putmessage (unsigned char *buf, int len)
722 unsigned char csum = 0;
723 char buf2[PBUFSIZ], buf3[PBUFSIZ];
724 unsigned char header[3];
727 /* Copy the packet into buffer BUF2, encapsulating it
728 and giving it a checksum. */
730 if (len > 170) /* Prosanity check */
731 internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
735 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
736 for (i = 0; i < len; ++i)
737 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
738 fprintf_unfiltered (gdb_stdlog, "\"\n");
750 header[1] = next_msg_id;
754 csum = compute_checksum (csum, buf, len);
755 csum = compute_checksum (csum, header + 1, 2);
759 tob64 (header, p, 3);
761 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
763 for (i = 0; i < enclen; ++i)
768 next_msg_id = (next_msg_id + 3) % 245;
770 /* Send it over and over until we get a positive ack. */
777 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
778 fprintf_unfiltered (gdb_stdlog,
779 " (Checksum %d, id %d, length %d)\n",
780 header[0], header[1], header[2]);
781 gdb_flush (gdb_stdlog);
783 if (serial_write (sds_desc, buf2, p - buf2))
784 perror_with_name ("putmessage: write failed");
790 /* Come here after finding the start of the frame. Collect the rest
791 into BUF. Returns 0 on any error, 1 on success. */
794 read_frame (char *buf)
803 c = readchar (sds_timeout);
809 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
813 fputs_filtered ("Saw new packet start in middle of old one\n",
815 return 0; /* Start a new packet, count retries */
823 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
829 if (bp < buf + PBUFSIZ - 1)
836 puts_filtered ("Message too long: ");
838 puts_filtered ("\n");
845 /* Read a packet from the remote machine, with error checking,
846 and store it in BUF. BUF is expected to be of size PBUFSIZ.
847 If FOREVER, wait forever rather than timing out; this is used
848 while the target is executing user code. */
851 getmessage (unsigned char *buf, int forever)
856 int val, i, len, csum;
857 unsigned char header[3];
858 unsigned char inbuf[500];
860 strcpy (buf, "timeout");
864 timeout = watchdog > 0 ? watchdog : -1;
868 timeout = sds_timeout;
872 for (tries = 1; tries <= MAX_TRIES; tries++)
874 /* This can loop forever if the remote side sends us characters
875 continuously, but if it pauses, we'll get a zero from readchar
876 because of timeout. Then we'll count that as a retry. */
878 /* Note that we will only wait forever prior to the start of a packet.
879 After that, we expect characters to arrive at a brisk pace. They
880 should show up within sds_timeout intervals. */
884 c = readchar (timeout);
886 if (c == SERIAL_TIMEOUT)
888 if (forever) /* Watchdog went off. Kill the target. */
890 target_mourn_inferior ();
891 error ("Watchdog has expired. Target detached.\n");
894 fputs_filtered ("Timed out.\n", gdb_stdlog);
898 while (c != '$' && c != '{');
900 /* We might have seen a "trigraph", a sequence of three characters
901 that indicate various sorts of communication state. */
905 /* Read the other two chars of the trigraph. */
906 c2 = readchar (timeout);
907 c3 = readchar (timeout);
909 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
919 val = read_frame (inbuf);
923 fromb64 (inbuf, header, 4);
924 /* (should check out other bits) */
925 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
930 csum = compute_checksum (csum, buf, len);
931 csum = compute_checksum (csum, header + 1, 2);
933 if (csum != header[0])
934 fprintf_unfiltered (gdb_stderr,
935 "Checksum mismatch: computed %d, received %d\n",
938 if (header[2] == 0xff)
939 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
943 fprintf_unfiltered (gdb_stdlog,
944 "... (Got checksum %d, id %d, length %d)\n",
945 header[0], header[1], header[2]);
946 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
947 for (i = 0; i < len; ++i)
949 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
951 fprintf_unfiltered (gdb_stdlog, "\"\n");
954 /* no ack required? */
958 /* Try the whole thing again. */
960 /* need to do something here */
964 /* We have tried hard enough, and just can't receive the packet. Give up. */
966 printf_unfiltered ("Ignoring packet error, continuing...\n");
973 /* Don't try to do anything to the target. */
979 unpush_target (&sds_ops);
980 generic_mourn_inferior ();
984 sds_create_inferior (char *exec_file, char *args, char **env, int from_tty)
986 inferior_ptid = pid_to_ptid (42000);
988 /* Clean up from the last time we were running. */
989 clear_proceed_status ();
991 /* Let the remote process run. */
992 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
996 sds_load (char *filename, int from_tty)
998 generic_load (filename, from_tty);
1000 inferior_ptid = null_ptid;
1003 /* The SDS monitor has commands for breakpoint insertion, although it
1004 it doesn't actually manage the breakpoints, it just returns the
1005 replaced instruction back to the debugger. */
1008 sds_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
1011 unsigned char *p, buf[PBUFSIZ];
1016 *p++ = (int) (addr >> 24) & 0xff;
1017 *p++ = (int) (addr >> 16) & 0xff;
1018 *p++ = (int) (addr >> 8) & 0xff;
1019 *p++ = (int) (addr) & 0xff;
1021 retlen = sds_send (buf, p - buf);
1023 for (i = 0; i < 4; ++i)
1024 contents_cache[i] = buf[i + 2];
1030 sds_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
1033 unsigned char *p, buf[PBUFSIZ];
1038 *p++ = (int) (addr >> 24) & 0xff;
1039 *p++ = (int) (addr >> 16) & 0xff;
1040 *p++ = (int) (addr >> 8) & 0xff;
1041 *p++ = (int) (addr) & 0xff;
1042 for (i = 0; i < 4; ++i)
1043 *p++ = contents_cache[i];
1045 retlen = sds_send (buf, p - buf);
1053 sds_ops.to_shortname = "sds";
1054 sds_ops.to_longname = "Remote serial target with SDS protocol";
1055 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1056 Specify the serial device it is connected to (e.g. /dev/ttya).";
1057 sds_ops.to_open = sds_open;
1058 sds_ops.to_close = sds_close;
1059 sds_ops.to_detach = sds_detach;
1060 sds_ops.to_resume = sds_resume;
1061 sds_ops.to_wait = sds_wait;
1062 sds_ops.to_fetch_registers = sds_fetch_registers;
1063 sds_ops.to_store_registers = sds_store_registers;
1064 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1065 sds_ops.deprecated_xfer_memory = sds_xfer_memory;
1066 sds_ops.to_files_info = sds_files_info;
1067 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1068 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1069 sds_ops.to_kill = sds_kill;
1070 sds_ops.to_load = sds_load;
1071 sds_ops.to_create_inferior = sds_create_inferior;
1072 sds_ops.to_mourn_inferior = sds_mourn;
1073 sds_ops.to_stratum = process_stratum;
1074 sds_ops.to_has_all_memory = 1;
1075 sds_ops.to_has_memory = 1;
1076 sds_ops.to_has_stack = 1;
1077 sds_ops.to_has_registers = 1;
1078 sds_ops.to_has_execution = 1;
1079 sds_ops.to_magic = OPS_MAGIC;
1082 /* Put a command string, in args, out to the monitor and display the
1086 sds_command (char *args, int from_tty)
1090 unsigned char buf[1000];
1092 /* Convert hexadecimal chars into a byte buffer. */
1097 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1103 retlen = sds_send (buf, len);
1105 printf_filtered ("Reply is ");
1106 for (i = 0; i < retlen; ++i)
1108 printf_filtered ("%02x", buf[i]);
1110 printf_filtered ("\n");
1114 _initialize_remote_sds (void)
1117 add_target (&sds_ops);
1119 deprecated_add_show_from_set
1120 (add_set_cmd ("sdstimeout", no_class,
1121 var_integer, (char *) &sds_timeout,
1122 "Set timeout value for sds read.\n", &setlist),
1125 add_com ("sds", class_obscure, sds_command,
1126 "Send a command to the SDS monitor.");