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];
502 char regs[REGISTER_BYTES];
504 /* Unimplemented registers read as all bits zero. */
505 memset (regs, 0, REGISTER_BYTES);
510 retlen = sds_send (buf, 3);
512 for (i = 0; i < 4 * 6; ++i)
513 regs[i + 4 * 32 + 8 * 32] = buf[i];
514 for (i = 0; i < 4 * 4; ++i)
515 sprs[i] = buf[i + 4 * 7];
520 retlen = sds_send (buf, 3);
522 for (i = 0; i < retlen; i++)
525 /* (should warn about reply too short) */
527 for (i = 0; i < NUM_REGS; i++)
528 supply_register (i, ®s[REGISTER_BYTE (i)]);
531 /* Prepare to store registers. Since we may send them all, we have to
532 read out the ones we don't want to change first. */
535 sds_prepare_to_store ()
537 /* Make sure the entire registers array is valid. */
538 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
541 /* Store register REGNO, or all registers if REGNO == -1, from the contents
542 of REGISTERS. FIXME: ignores errors. */
545 sds_store_registers (regno)
548 unsigned char *p, buf[PBUFSIZ];
551 /* Store all the special-purpose registers. */
557 for (i = 0; i < 4 * 6; i++)
558 *p++ = registers[i + 4 * 32 + 8 * 32];
559 for (i = 0; i < 4 * 1; i++)
561 for (i = 0; i < 4 * 4; i++)
564 sds_send (buf, p - buf);
566 /* Store all the general-purpose registers. */
572 for (i = 0; i < 4 * 32; i++)
575 sds_send (buf, p - buf);
579 /* Write memory data directly to the remote machine. This does not
580 inform the data cache; the data cache uses this. MEMADDR is the
581 address in the remote memory space. MYADDR is the address of the
582 buffer in our space. LEN is the number of bytes.
584 Returns number of bytes transferred, or 0 for error. */
587 sds_write_bytes (memaddr, myaddr, len)
592 int max_buf_size; /* Max size of packet output buffer */
594 unsigned char buf[PBUFSIZ];
598 /* Chop the transfer down if necessary */
605 todo = min (len, max_buf_size);
609 buf[2] = (int) (memaddr >> 24) & 0xff;
610 buf[3] = (int) (memaddr >> 16) & 0xff;
611 buf[4] = (int) (memaddr >> 8) & 0xff;
612 buf[5] = (int) (memaddr) & 0xff;
616 for (i = 0; i < todo; i++)
617 buf[i + 8] = myaddr[i];
619 sds_send (buf, 8 + todo);
621 /* (should look at result) */
630 /* Read memory data directly from the remote machine. This does not
631 use the data cache; the data cache uses this. MEMADDR is the
632 address in the remote memory space. MYADDR is the address of the
633 buffer in our space. LEN is the number of bytes.
635 Returns number of bytes transferred, or 0 for error. */
638 sds_read_bytes (memaddr, myaddr, len)
643 int max_buf_size; /* Max size of packet output buffer */
645 unsigned char buf[PBUFSIZ];
649 /* Chop the transfer down if necessary */
656 todo = min (len, max_buf_size);
660 buf[2] = (int) (memaddr >> 24) & 0xff;
661 buf[3] = (int) (memaddr >> 16) & 0xff;
662 buf[4] = (int) (memaddr >> 8) & 0xff;
663 buf[5] = (int) (memaddr) & 0xff;
664 buf[6] = (int) (todo >> 8) & 0xff;
665 buf[7] = (int) (todo) & 0xff;
668 retlen = sds_send (buf, 9);
670 if (retlen - 2 != todo)
675 /* Reply describes memory byte by byte. */
677 for (i = 0; i < todo; i++)
678 myaddr[i] = buf[i + 2];
688 /* Read or write LEN bytes from inferior memory at MEMADDR,
689 transferring to or from debugger address MYADDR. Write to inferior
690 if SHOULD_WRITE is nonzero. Returns length of data written or
691 read; 0 for error. */
695 sds_xfer_memory (memaddr, myaddr, len, should_write, target)
700 struct target_ops *target; /* ignored */
702 return dcache_xfer_memory (sds_dcache, memaddr, myaddr, len, should_write);
707 sds_files_info (ignore)
708 struct target_ops *ignore;
710 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
713 /* Stuff for dealing with the packets which are part of this protocol.
714 See comment at top of file for details. */
716 /* Read a single character from the remote end, masking it down to 7 bits. */
724 ch = SERIAL_READCHAR (sds_desc, timeout);
726 if (remote_debug > 1 && ch >= 0)
727 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
732 error ("Remote connection closed");
734 perror_with_name ("Remote communication error");
742 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
743 because 253, 254, and 255 are special flags in the protocol.) */
746 compute_checksum (csum, buf, len)
752 for (i = 0; i < len; ++i)
753 csum += (unsigned char) buf[i];
759 /* Send the command in BUF to the remote machine, and read the reply
767 putmessage (buf, len);
769 return getmessage (buf, 0);
772 /* Send a message to the remote machine. */
775 putmessage (buf, len)
780 unsigned char csum = 0;
781 char buf2[PBUFSIZ], buf3[PBUFSIZ];
782 unsigned char header[3];
787 /* Copy the packet into buffer BUF2, encapsulating it
788 and giving it a checksum. */
790 if (len > 170) /* Prosanity check */
795 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
796 for (i = 0; i < len; ++i)
797 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
798 fprintf_unfiltered (gdb_stdlog, "\"\n");
810 header[1] = next_msg_id;
814 csum = compute_checksum (csum, buf, len);
815 csum = compute_checksum (csum, header + 1, 2);
819 tob64 (header, p, 3);
821 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
823 for (i = 0; i < enclen; ++i)
828 next_msg_id = (next_msg_id + 3) % 245;
830 /* Send it over and over until we get a positive ack. */
834 int started_error_output = 0;
839 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
840 fprintf_unfiltered (gdb_stdlog,
841 " (Checksum %d, id %d, length %d)\n",
842 header[0], header[1], header[2]);
843 gdb_flush (gdb_stdlog);
845 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
846 perror_with_name ("putmessage: write failed");
854 /* Come here after finding the start of the frame. Collect the rest
855 into BUF. Returns 0 on any error, 1 on success. */
868 c = readchar (sds_timeout);
874 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
878 fputs_filtered ("Saw new packet start in middle of old one\n",
880 return 0; /* Start a new packet, count retries */
888 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
894 if (bp < buf + PBUFSIZ - 1)
901 puts_filtered ("Message too long: ");
903 puts_filtered ("\n");
910 /* Read a packet from the remote machine, with error checking,
911 and store it in BUF. BUF is expected to be of size PBUFSIZ.
912 If FOREVER, wait forever rather than timing out; this is used
913 while the target is executing user code. */
916 getmessage (buf, forever)
923 int val, i, len, csum;
924 unsigned char header[3];
925 unsigned char inbuf[500];
927 strcpy (buf, "timeout");
931 timeout = watchdog > 0 ? watchdog : -1;
935 timeout = sds_timeout;
939 for (tries = 1; tries <= MAX_TRIES; tries++)
941 /* This can loop forever if the remote side sends us characters
942 continuously, but if it pauses, we'll get a zero from readchar
943 because of timeout. Then we'll count that as a retry. */
945 /* Note that we will only wait forever prior to the start of a packet.
946 After that, we expect characters to arrive at a brisk pace. They
947 should show up within sds_timeout intervals. */
951 c = readchar (timeout);
953 if (c == SERIAL_TIMEOUT)
955 if (forever) /* Watchdog went off. Kill the target. */
957 target_mourn_inferior ();
958 error ("Watchdog has expired. Target detached.\n");
961 fputs_filtered ("Timed out.\n", gdb_stdlog);
965 while (c != '$' && c != '{');
967 /* We might have seen a "trigraph", a sequence of three characters
968 that indicate various sorts of communication state. */
972 /* Read the other two chars of the trigraph. */
973 c2 = readchar (timeout);
974 c3 = readchar (timeout);
976 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
986 val = read_frame (inbuf);
990 fromb64 (inbuf, header, 4);
991 /* (should check out other bits) */
992 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
997 csum = compute_checksum (csum, buf, len);
998 csum = compute_checksum (csum, header + 1, 2);
1000 if (csum != header[0])
1001 fprintf_unfiltered (gdb_stderr,
1002 "Checksum mismatch: computed %d, received %d\n",
1005 if (header[2] == 0xff)
1006 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
1010 fprintf_unfiltered (gdb_stdlog,
1011 "... (Got checksum %d, id %d, length %d)\n",
1012 header[0], header[1], header[2]);
1013 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
1014 for (i = 0; i < len; ++i)
1016 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
1018 fprintf_unfiltered (gdb_stdlog, "\"\n");
1021 /* no ack required? */
1025 /* Try the whole thing again. */
1027 /* need to do something here */
1030 /* We have tried hard enough, and just can't receive the packet. Give up. */
1032 printf_unfiltered ("Ignoring packet error, continuing...\n");
1039 /* Don't try to do anything to the target. */
1045 unpush_target (&sds_ops);
1046 generic_mourn_inferior ();
1050 sds_create_inferior (exec_file, args, env)
1055 inferior_pid = 42000;
1057 /* Clean up from the last time we were running. */
1058 clear_proceed_status ();
1060 /* Let the remote process run. */
1061 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1065 sds_load (filename, from_tty)
1069 generic_load (filename, from_tty);
1074 /* The SDS monitor has commands for breakpoint insertion, although it
1075 it doesn't actually manage the breakpoints, it just returns the
1076 replaced instruction back to the debugger. */
1079 sds_insert_breakpoint (addr, contents_cache)
1081 char *contents_cache;
1084 unsigned char *p, buf[PBUFSIZ];
1089 *p++ = (int) (addr >> 24) & 0xff;
1090 *p++ = (int) (addr >> 16) & 0xff;
1091 *p++ = (int) (addr >> 8) & 0xff;
1092 *p++ = (int) (addr) & 0xff;
1094 retlen = sds_send (buf, p - buf);
1096 for (i = 0; i < 4; ++i)
1097 contents_cache[i] = buf[i + 2];
1103 sds_remove_breakpoint (addr, contents_cache)
1105 char *contents_cache;
1108 unsigned char *p, buf[PBUFSIZ];
1113 *p++ = (int) (addr >> 24) & 0xff;
1114 *p++ = (int) (addr >> 16) & 0xff;
1115 *p++ = (int) (addr >> 8) & 0xff;
1116 *p++ = (int) (addr) & 0xff;
1117 for (i = 0; i < 4; ++i)
1118 *p++ = contents_cache[i];
1120 retlen = sds_send (buf, p - buf);
1128 sds_ops.to_shortname = "sds";
1129 sds_ops.to_longname = "Remote serial target with SDS protocol";
1130 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1131 Specify the serial device it is connected to (e.g. /dev/ttya).";
1132 sds_ops.to_open = sds_open;
1133 sds_ops.to_close = sds_close;
1134 sds_ops.to_detach = sds_detach;
1135 sds_ops.to_resume = sds_resume;
1136 sds_ops.to_wait = sds_wait;
1137 sds_ops.to_fetch_registers = sds_fetch_registers;
1138 sds_ops.to_store_registers = sds_store_registers;
1139 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1140 sds_ops.to_xfer_memory = sds_xfer_memory;
1141 sds_ops.to_files_info = sds_files_info;
1142 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1143 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1144 sds_ops.to_kill = sds_kill;
1145 sds_ops.to_load = sds_load;
1146 sds_ops.to_create_inferior = sds_create_inferior;
1147 sds_ops.to_mourn_inferior = sds_mourn;
1148 sds_ops.to_stratum = process_stratum;
1149 sds_ops.to_has_all_memory = 1;
1150 sds_ops.to_has_memory = 1;
1151 sds_ops.to_has_stack = 1;
1152 sds_ops.to_has_registers = 1;
1153 sds_ops.to_has_execution = 1;
1154 sds_ops.to_magic = OPS_MAGIC;
1157 /* Put a command string, in args, out to the monitor and display the
1161 sds_command (args, from_tty)
1167 unsigned char buf[1000];
1169 /* Convert hexadecimal chars into a byte buffer. */
1174 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1180 retlen = sds_send (buf, len);
1182 printf_filtered ("Reply is ");
1183 for (i = 0; i < retlen; ++i)
1185 printf_filtered ("%02x", buf[i]);
1187 printf_filtered ("\n");
1191 _initialize_remote_sds ()
1194 add_target (&sds_ops);
1196 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1197 var_integer, (char *) &sds_timeout,
1198 "Set timeout value for sds read.\n", &setlist),
1201 add_com ("sds", class_obscure, sds_command,
1202 "Send a command to the SDS monitor.");