1 /* Remote target communications for serial-line targets using SDS' protocol.
2 Copyright 1997, 2001 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"
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
42 #include <sys/types.h>
48 extern void _initialize_remote_sds (void);
50 /* Declarations of local functions. */
52 static int sds_write_bytes (CORE_ADDR, char *, int);
54 static int sds_read_bytes (CORE_ADDR, char *, int);
56 static void sds_files_info (struct target_ops *ignore);
58 static int sds_xfer_memory (CORE_ADDR, char *, int, int,
59 struct mem_attrib *, struct target_ops *);
61 static void sds_prepare_to_store (void);
63 static void sds_fetch_registers (int);
65 static void sds_resume (int, int, enum target_signal);
67 static int sds_start_remote (PTR);
69 static void sds_open (char *, int);
71 static void sds_close (int);
73 static void sds_store_registers (int);
75 static void sds_mourn (void);
77 static void sds_create_inferior (char *, char *, char **);
79 static void sds_load (char *, int);
81 static int getmessage (unsigned char *, int);
83 static int putmessage (unsigned char *, int);
85 static int sds_send (unsigned char *, int);
87 static int readchar (int);
89 static int sds_wait (int, struct target_waitstatus *);
91 static void sds_kill (void);
93 static int tohex (int);
95 static int fromhex (int);
97 static void sds_detach (char *, int);
99 static void sds_interrupt (int);
101 static void sds_interrupt_twice (int);
103 static void interrupt_query (void);
105 static int read_frame (char *);
107 static int sds_insert_breakpoint (CORE_ADDR, char *);
109 static int sds_remove_breakpoint (CORE_ADDR, char *);
111 static void init_sds_ops (void);
113 static void sds_command (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. */
151 sds_close (int quitting)
154 SERIAL_CLOSE (sds_desc);
158 /* Stub for catch_errors. */
161 sds_start_remote (PTR dummy)
164 unsigned char buf[200];
166 immediate_quit++; /* Allow user to interrupt it */
168 /* Ack any packet which the remote side has already sent. */
169 SERIAL_WRITE (sds_desc, "{#*\r\n", 5);
170 SERIAL_WRITE (sds_desc, "{#}\r\n", 5);
172 while ((c = readchar (1)) >= 0)
173 printf_unfiltered ("%c", c);
174 printf_unfiltered ("\n");
186 start_remote (); /* Initialize gdb process mechanisms */
190 /* Open a connection to a remote debugger.
191 NAME is the filename used for communication. */
194 sds_open (char *name, int from_tty)
197 error ("To open a remote debug connection, you need to specify what serial\n\
198 device is attached to the remote system (e.g. /dev/ttya).");
200 target_preopen (from_tty);
202 unpush_target (&sds_ops);
204 sds_desc = SERIAL_OPEN (name);
206 perror_with_name (name);
210 if (SERIAL_SETBAUDRATE (sds_desc, baud_rate))
212 SERIAL_CLOSE (sds_desc);
213 perror_with_name (name);
218 SERIAL_RAW (sds_desc);
220 /* If there is something sitting in the buffer we might take it as a
221 response to a command, which would be bad. */
222 SERIAL_FLUSH_INPUT (sds_desc);
226 puts_filtered ("Remote debugging using ");
227 puts_filtered (name);
228 puts_filtered ("\n");
230 push_target (&sds_ops); /* Switch to using remote target now */
234 /* Start the remote connection; if error (0), discard this target.
235 In particular, if the user quits, be sure to discard it (we'd be
236 in an inconsistent state otherwise). */
237 if (!catch_errors (sds_start_remote, NULL,
238 "Couldn't establish connection to remote target\n",
243 /* This takes a program previously attached to and detaches it. After
244 this is done, GDB can be used to debug some other program. We
245 better not have left any breakpoints in the target program or it'll
246 die when it hits one. */
249 sds_detach (char *args, int from_tty)
254 error ("Argument given to \"detach\" when remotely debugging.");
257 /* Tell the remote target to detach. */
264 puts_filtered ("Ending remote debugging.\n");
267 /* Convert hex digit A to a number. */
272 if (a >= '0' && a <= '9')
274 else if (a >= 'a' && a <= 'f')
277 error ("Reply contains invalid hex digit %d", a);
280 /* Convert number NIB to a hex digit. */
288 return 'a' + nib - 10;
292 tob64 (unsigned char *inbuf, char *outbuf, int len)
298 error ("bad length");
301 for (i = 0; i < len; i += 3)
303 /* Collect the next three bytes into a number. */
304 sum = ((long) *inbuf++) << 16;
305 sum |= ((long) *inbuf++) << 8;
306 sum |= ((long) *inbuf++);
308 /* Spit out 4 6-bit encodings. */
309 *p++ = ((sum >> 18) & 0x3f) + '0';
310 *p++ = ((sum >> 12) & 0x3f) + '0';
311 *p++ = ((sum >> 6) & 0x3f) + '0';
312 *p++ = (sum & 0x3f) + '0';
318 fromb64 (char *inbuf, char *outbuf, int len)
323 error ("bad length");
325 for (i = 0; i < len; i += 4)
327 /* Collect 4 6-bit digits. */
328 sum = (*inbuf++ - '0') << 18;
329 sum |= (*inbuf++ - '0') << 12;
330 sum |= (*inbuf++ - '0') << 6;
331 sum |= (*inbuf++ - '0');
333 /* Now take the resulting 24-bit number and get three bytes out
335 *outbuf++ = (sum >> 16) & 0xff;
336 *outbuf++ = (sum >> 8) & 0xff;
337 *outbuf++ = sum & 0xff;
340 return (len / 4) * 3;
344 /* Tell the remote machine to resume. */
346 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
350 sds_resume (int pid, int step, enum target_signal siggnal)
352 unsigned char buf[PBUFSIZ];
354 last_sent_signal = siggnal;
355 last_sent_step = step;
357 buf[0] = (step ? 21 : 20);
358 buf[1] = 0; /* (should be signal?) */
363 /* Send a message to target to halt it. Target will respond, and send
364 us a message pending notice. */
367 sds_interrupt (int signo)
369 unsigned char buf[PBUFSIZ];
371 /* If this doesn't work, try more severe steps. */
372 signal (signo, sds_interrupt_twice);
375 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
381 static void (*ofunc) ();
383 /* The user typed ^C twice. */
386 sds_interrupt_twice (int signo)
388 signal (signo, ofunc);
392 signal (signo, sds_interrupt);
395 /* Ask the user what to do when an interrupt is received. */
398 interrupt_query (void)
400 target_terminal_ours ();
402 if (query ("Interrupted while waiting for the program.\n\
403 Give up (and stop debugging it)? "))
405 target_mourn_inferior ();
406 return_to_top_level (RETURN_QUIT);
409 target_terminal_inferior ();
412 /* If nonzero, ignore the next kill. */
415 /* Wait until the remote machine stops, then return, storing status in
416 STATUS just as `wait' would. Returns "pid" (though it's not clear
417 what, if anything, that means in the case of this target). */
420 sds_wait (int pid, struct target_waitstatus *status)
422 unsigned char buf[PBUFSIZ];
425 status->kind = TARGET_WAITKIND_EXITED;
426 status->value.integer = 0;
428 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
430 signal (SIGINT, ofunc);
435 status->kind = TARGET_WAITKIND_STOPPED;
446 retlen = sds_send (buf, 1);
449 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
454 status->kind = TARGET_WAITKIND_STOPPED;
455 status->value.sig = TARGET_SIGNAL_TRAP;
463 static unsigned char sprs[16];
465 /* Read the remote registers into the block REGS. */
466 /* Currently we just read all the registers, so we don't use regno. */
470 sds_fetch_registers (int regno)
472 unsigned char buf[PBUFSIZ];
474 char regs[REGISTER_BYTES];
476 /* Unimplemented registers read as all bits zero. */
477 memset (regs, 0, REGISTER_BYTES);
482 retlen = sds_send (buf, 3);
484 for (i = 0; i < 4 * 6; ++i)
485 regs[i + 4 * 32 + 8 * 32] = buf[i];
486 for (i = 0; i < 4 * 4; ++i)
487 sprs[i] = buf[i + 4 * 7];
492 retlen = sds_send (buf, 3);
494 for (i = 0; i < retlen; i++)
497 /* (should warn about reply too short) */
499 for (i = 0; i < NUM_REGS; i++)
500 supply_register (i, ®s[REGISTER_BYTE (i)]);
503 /* Prepare to store registers. Since we may send them all, we have to
504 read out the ones we don't want to change first. */
507 sds_prepare_to_store (void)
509 /* Make sure the entire registers array is valid. */
510 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
513 /* Store register REGNO, or all registers if REGNO == -1, from the contents
514 of REGISTERS. FIXME: ignores errors. */
517 sds_store_registers (int regno)
519 unsigned char *p, buf[PBUFSIZ];
522 /* Store all the special-purpose registers. */
528 for (i = 0; i < 4 * 6; i++)
529 *p++ = registers[i + 4 * 32 + 8 * 32];
530 for (i = 0; i < 4 * 1; i++)
532 for (i = 0; i < 4 * 4; i++)
535 sds_send (buf, p - buf);
537 /* Store all the general-purpose registers. */
543 for (i = 0; i < 4 * 32; i++)
546 sds_send (buf, p - buf);
550 /* Write memory data directly to the remote machine. This does not
551 inform the data cache; the data cache uses this. MEMADDR is the
552 address in the remote memory space. MYADDR is the address of the
553 buffer in our space. LEN is the number of bytes.
555 Returns number of bytes transferred, or 0 for error. */
558 sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
560 int max_buf_size; /* Max size of packet output buffer */
562 unsigned char buf[PBUFSIZ];
566 /* Chop the transfer down if necessary */
573 todo = min (len, max_buf_size);
577 buf[2] = (int) (memaddr >> 24) & 0xff;
578 buf[3] = (int) (memaddr >> 16) & 0xff;
579 buf[4] = (int) (memaddr >> 8) & 0xff;
580 buf[5] = (int) (memaddr) & 0xff;
584 for (i = 0; i < todo; i++)
585 buf[i + 8] = myaddr[i];
587 sds_send (buf, 8 + todo);
589 /* (should look at result) */
598 /* Read memory data directly from the remote machine. This does not
599 use the data cache; the data cache uses this. MEMADDR is the
600 address in the remote memory space. MYADDR is the address of the
601 buffer in our space. LEN is the number of bytes.
603 Returns number of bytes transferred, or 0 for error. */
606 sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
608 int max_buf_size; /* Max size of packet output buffer */
610 unsigned char buf[PBUFSIZ];
614 /* Chop the transfer down if necessary */
621 todo = min (len, max_buf_size);
625 buf[2] = (int) (memaddr >> 24) & 0xff;
626 buf[3] = (int) (memaddr >> 16) & 0xff;
627 buf[4] = (int) (memaddr >> 8) & 0xff;
628 buf[5] = (int) (memaddr) & 0xff;
629 buf[6] = (int) (todo >> 8) & 0xff;
630 buf[7] = (int) (todo) & 0xff;
633 retlen = sds_send (buf, 9);
635 if (retlen - 2 != todo)
640 /* Reply describes memory byte by byte. */
642 for (i = 0; i < todo; i++)
643 myaddr[i] = buf[i + 2];
653 /* Read or write LEN bytes from inferior memory at MEMADDR,
654 transferring to or from debugger address MYADDR. Write to inferior
655 if SHOULD_WRITE is nonzero. Returns length of data written or
656 read; 0 for error. TARGET is unused. */
660 sds_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int should_write,
661 struct mem_attrib *attrib ATTRIBUTE_UNUSED,
662 struct target_ops *target ATTRIBUTE_UNUSED)
667 res = sds_write_bytes (memaddr, myaddr, len);
669 res = sds_read_bytes (memaddr, myaddr, len);
676 sds_files_info (struct target_ops *ignore)
678 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
681 /* Stuff for dealing with the packets which are part of this protocol.
682 See comment at top of file for details. */
684 /* Read a single character from the remote end, masking it down to 7 bits. */
687 readchar (int timeout)
691 ch = SERIAL_READCHAR (sds_desc, timeout);
693 if (remote_debug > 1 && ch >= 0)
694 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
699 error ("Remote connection closed");
701 perror_with_name ("Remote communication error");
709 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
710 because 253, 254, and 255 are special flags in the protocol.) */
713 compute_checksum (int csum, char *buf, int len)
717 for (i = 0; i < len; ++i)
718 csum += (unsigned char) buf[i];
724 /* Send the command in BUF to the remote machine, and read the reply
728 sds_send (unsigned char *buf, int len)
730 putmessage (buf, len);
732 return getmessage (buf, 0);
735 /* Send a message to the remote machine. */
738 putmessage (unsigned char *buf, int len)
741 unsigned char csum = 0;
742 char buf2[PBUFSIZ], buf3[PBUFSIZ];
743 unsigned char header[3];
746 /* Copy the packet into buffer BUF2, encapsulating it
747 and giving it a checksum. */
749 if (len > 170) /* Prosanity check */
750 internal_error (__FILE__, __LINE__, "failed internal consistency check");
754 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
755 for (i = 0; i < len; ++i)
756 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
757 fprintf_unfiltered (gdb_stdlog, "\"\n");
769 header[1] = next_msg_id;
773 csum = compute_checksum (csum, buf, len);
774 csum = compute_checksum (csum, header + 1, 2);
778 tob64 (header, p, 3);
780 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
782 for (i = 0; i < enclen; ++i)
787 next_msg_id = (next_msg_id + 3) % 245;
789 /* Send it over and over until we get a positive ack. */
796 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
797 fprintf_unfiltered (gdb_stdlog,
798 " (Checksum %d, id %d, length %d)\n",
799 header[0], header[1], header[2]);
800 gdb_flush (gdb_stdlog);
802 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
803 perror_with_name ("putmessage: write failed");
809 /* Come here after finding the start of the frame. Collect the rest
810 into BUF. Returns 0 on any error, 1 on success. */
813 read_frame (char *buf)
822 c = readchar (sds_timeout);
828 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
832 fputs_filtered ("Saw new packet start in middle of old one\n",
834 return 0; /* Start a new packet, count retries */
842 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
848 if (bp < buf + PBUFSIZ - 1)
855 puts_filtered ("Message too long: ");
857 puts_filtered ("\n");
864 /* Read a packet from the remote machine, with error checking,
865 and store it in BUF. BUF is expected to be of size PBUFSIZ.
866 If FOREVER, wait forever rather than timing out; this is used
867 while the target is executing user code. */
870 getmessage (unsigned char *buf, int forever)
875 int val, i, len, csum;
876 unsigned char header[3];
877 unsigned char inbuf[500];
879 strcpy (buf, "timeout");
883 timeout = watchdog > 0 ? watchdog : -1;
887 timeout = sds_timeout;
891 for (tries = 1; tries <= MAX_TRIES; tries++)
893 /* This can loop forever if the remote side sends us characters
894 continuously, but if it pauses, we'll get a zero from readchar
895 because of timeout. Then we'll count that as a retry. */
897 /* Note that we will only wait forever prior to the start of a packet.
898 After that, we expect characters to arrive at a brisk pace. They
899 should show up within sds_timeout intervals. */
903 c = readchar (timeout);
905 if (c == SERIAL_TIMEOUT)
907 if (forever) /* Watchdog went off. Kill the target. */
909 target_mourn_inferior ();
910 error ("Watchdog has expired. Target detached.\n");
913 fputs_filtered ("Timed out.\n", gdb_stdlog);
917 while (c != '$' && c != '{');
919 /* We might have seen a "trigraph", a sequence of three characters
920 that indicate various sorts of communication state. */
924 /* Read the other two chars of the trigraph. */
925 c2 = readchar (timeout);
926 c3 = readchar (timeout);
928 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
938 val = read_frame (inbuf);
942 fromb64 (inbuf, header, 4);
943 /* (should check out other bits) */
944 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
949 csum = compute_checksum (csum, buf, len);
950 csum = compute_checksum (csum, header + 1, 2);
952 if (csum != header[0])
953 fprintf_unfiltered (gdb_stderr,
954 "Checksum mismatch: computed %d, received %d\n",
957 if (header[2] == 0xff)
958 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
962 fprintf_unfiltered (gdb_stdlog,
963 "... (Got checksum %d, id %d, length %d)\n",
964 header[0], header[1], header[2]);
965 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
966 for (i = 0; i < len; ++i)
968 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
970 fprintf_unfiltered (gdb_stdlog, "\"\n");
973 /* no ack required? */
977 /* Try the whole thing again. */
979 /* need to do something here */
982 /* We have tried hard enough, and just can't receive the packet. Give up. */
984 printf_unfiltered ("Ignoring packet error, continuing...\n");
991 /* Don't try to do anything to the target. */
997 unpush_target (&sds_ops);
998 generic_mourn_inferior ();
1002 sds_create_inferior (char *exec_file, char *args, char **env)
1004 inferior_pid = 42000;
1006 /* Clean up from the last time we were running. */
1007 clear_proceed_status ();
1009 /* Let the remote process run. */
1010 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1014 sds_load (char *filename, int from_tty)
1016 generic_load (filename, from_tty);
1021 /* The SDS monitor has commands for breakpoint insertion, although it
1022 it doesn't actually manage the breakpoints, it just returns the
1023 replaced instruction back to the debugger. */
1026 sds_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
1029 unsigned char *p, buf[PBUFSIZ];
1034 *p++ = (int) (addr >> 24) & 0xff;
1035 *p++ = (int) (addr >> 16) & 0xff;
1036 *p++ = (int) (addr >> 8) & 0xff;
1037 *p++ = (int) (addr) & 0xff;
1039 retlen = sds_send (buf, p - buf);
1041 for (i = 0; i < 4; ++i)
1042 contents_cache[i] = buf[i + 2];
1048 sds_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
1051 unsigned char *p, buf[PBUFSIZ];
1056 *p++ = (int) (addr >> 24) & 0xff;
1057 *p++ = (int) (addr >> 16) & 0xff;
1058 *p++ = (int) (addr >> 8) & 0xff;
1059 *p++ = (int) (addr) & 0xff;
1060 for (i = 0; i < 4; ++i)
1061 *p++ = contents_cache[i];
1063 retlen = sds_send (buf, p - buf);
1071 sds_ops.to_shortname = "sds";
1072 sds_ops.to_longname = "Remote serial target with SDS protocol";
1073 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1074 Specify the serial device it is connected to (e.g. /dev/ttya).";
1075 sds_ops.to_open = sds_open;
1076 sds_ops.to_close = sds_close;
1077 sds_ops.to_detach = sds_detach;
1078 sds_ops.to_resume = sds_resume;
1079 sds_ops.to_wait = sds_wait;
1080 sds_ops.to_fetch_registers = sds_fetch_registers;
1081 sds_ops.to_store_registers = sds_store_registers;
1082 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1083 sds_ops.to_xfer_memory = sds_xfer_memory;
1084 sds_ops.to_files_info = sds_files_info;
1085 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1086 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1087 sds_ops.to_kill = sds_kill;
1088 sds_ops.to_load = sds_load;
1089 sds_ops.to_create_inferior = sds_create_inferior;
1090 sds_ops.to_mourn_inferior = sds_mourn;
1091 sds_ops.to_stratum = process_stratum;
1092 sds_ops.to_has_all_memory = 1;
1093 sds_ops.to_has_memory = 1;
1094 sds_ops.to_has_stack = 1;
1095 sds_ops.to_has_registers = 1;
1096 sds_ops.to_has_execution = 1;
1097 sds_ops.to_magic = OPS_MAGIC;
1100 /* Put a command string, in args, out to the monitor and display the
1104 sds_command (char *args, int from_tty)
1108 unsigned char buf[1000];
1110 /* Convert hexadecimal chars into a byte buffer. */
1115 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1121 retlen = sds_send (buf, len);
1123 printf_filtered ("Reply is ");
1124 for (i = 0; i < retlen; ++i)
1126 printf_filtered ("%02x", buf[i]);
1128 printf_filtered ("\n");
1132 _initialize_remote_sds (void)
1135 add_target (&sds_ops);
1137 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1138 var_integer, (char *) &sds_timeout,
1139 "Set timeout value for sds read.\n", &setlist),
1142 add_com ("sds", class_obscure, sds_command,
1143 "Send a command to the SDS monitor.");