* Sanitization fixes to retain new files.
[binutils-gdb.git] / gdb / remote-sds.c
blobd3c6b70e7bfdd956a3e44408a8d49dc22a5406d3
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
23 site. -sts 8/13/97 */
25 #include "defs.h"
26 #include "gdb_string.h"
27 #include <fcntl.h>
28 #include "frame.h"
29 #include "inferior.h"
30 #include "bfd.h"
31 #include "symfile.h"
32 #include "target.h"
33 #include "wait.h"
34 #include "gdbcmd.h"
35 #include "objfiles.h"
36 #include "gdb-stabs.h"
37 #include "gdbthread.h"
38 #include "gdbcore.h"
39 #include "dcache.h"
41 #ifdef USG
42 #include <sys/types.h>
43 #endif
45 #include <signal.h>
46 #include "serial.h"
48 /* Declarations of local functions. */
50 static int sds_write_bytes PARAMS ((CORE_ADDR, char *, int));
52 static int sds_read_bytes PARAMS ((CORE_ADDR, char *, int));
54 static void sds_files_info PARAMS ((struct target_ops *ignore));
56 static int sds_xfer_memory PARAMS ((CORE_ADDR, char *,
57 int, int, struct target_ops *));
59 static void sds_prepare_to_store PARAMS ((void));
61 static void sds_fetch_registers PARAMS ((int));
63 static void sds_resume PARAMS ((int, int, enum target_signal));
65 static int sds_start_remote PARAMS ((char *));
67 static void sds_open PARAMS ((char *, int));
69 static void sds_close PARAMS ((int));
71 static void sds_store_registers PARAMS ((int));
73 static void sds_mourn PARAMS ((void));
75 static void sds_create_inferior PARAMS ((char *, char *, char **));
77 static void sds_load PARAMS ((char *, int));
79 static int getmessage PARAMS ((unsigned char *, int));
81 static int putmessage PARAMS ((unsigned char *, int));
83 static int sds_send PARAMS ((unsigned char *, int));
85 static int readchar PARAMS ((int));
87 static int sds_wait PARAMS ((int, struct target_waitstatus *));
89 static void sds_kill PARAMS ((void));
91 static int tohex PARAMS ((int));
93 static int fromhex PARAMS ((int));
95 static void sds_detach PARAMS ((char *, int));
97 static void sds_interrupt PARAMS ((int));
99 static void sds_interrupt_twice PARAMS ((int));
101 static void interrupt_query PARAMS ((void));
103 static int read_frame PARAMS ((char *));
105 static int sds_insert_breakpoint PARAMS ((CORE_ADDR, char *));
107 static int sds_remove_breakpoint PARAMS ((CORE_ADDR, char *));
110 static struct target_ops sds_ops; /* Forward decl */
112 /* This was 5 seconds, which is a long time to sit and wait.
113 Unless this is going though some terminal server or multiplexer or
114 other form of hairy serial connection, I would think 2 seconds would
115 be plenty. */
117 /* Changed to allow option to set timeout value.
118 was static int sds_timeout = 2; */
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
123 starts. */
125 static serial_t sds_desc = NULL;
127 /* This limit comes from the monitor. */
129 #define PBUFSIZ 250
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. */
144 /* ARGSUSED */
145 static void
146 sds_close (quitting)
147 int quitting;
149 if (sds_desc)
150 SERIAL_CLOSE (sds_desc);
151 sds_desc = NULL;
154 /* Stub for catch_errors. */
156 static int
157 sds_start_remote (dummy)
158 char *dummy;
160 char c;
161 unsigned char buf[200];
163 immediate_quit = 1; /* Allow user to interrupt it */
165 /* Ack any packet which the remote side has already sent. */
166 SERIAL_WRITE (sds_desc, "{#*\r\n", 5);
167 SERIAL_WRITE (sds_desc, "{#}\r\n", 5);
169 while ((c = readchar (1)) >= 0)
170 printf_unfiltered ("%c", c);
171 printf_unfiltered ("\n");
173 next_msg_id = 251;
175 buf[0] = 26;
176 sds_send (buf, 1);
178 buf[0] = 0;
179 sds_send (buf, 1);
181 immediate_quit = 0;
183 start_remote (); /* Initialize gdb process mechanisms */
184 return 1;
187 /* Open a connection to a remote debugger.
188 NAME is the filename used for communication. */
190 static DCACHE *sds_dcache;
192 static void
193 sds_open (name, from_tty)
194 char *name;
195 int from_tty;
197 if (name == 0)
198 error ("To open a remote debug connection, you need to specify what serial\n\
199 device is attached to the remote system (e.g. /dev/ttya).");
201 target_preopen (from_tty);
203 unpush_target (&sds_ops);
205 sds_dcache = dcache_init (sds_read_bytes, sds_write_bytes);
207 sds_desc = SERIAL_OPEN (name);
208 if (!sds_desc)
209 perror_with_name (name);
211 if (baud_rate != -1)
213 if (SERIAL_SETBAUDRATE (sds_desc, baud_rate))
215 SERIAL_CLOSE (sds_desc);
216 perror_with_name (name);
221 SERIAL_RAW (sds_desc);
223 /* If there is something sitting in the buffer we might take it as a
224 response to a command, which would be bad. */
225 SERIAL_FLUSH_INPUT (sds_desc);
227 if (from_tty)
229 puts_filtered ("Remote debugging using ");
230 puts_filtered (name);
231 puts_filtered ("\n");
233 push_target (&sds_ops); /* Switch to using remote target now */
235 just_started = 1;
237 /* Start the remote connection; if error (0), discard this target.
238 In particular, if the user quits, be sure to discard it (we'd be
239 in an inconsistent state otherwise). */
240 if (!catch_errors (sds_start_remote, (char *)0,
241 "Couldn't establish connection to remote target\n",
242 RETURN_MASK_ALL))
243 pop_target ();
246 /* This takes a program previously attached to and detaches it. After
247 this is done, GDB can be used to debug some other program. We
248 better not have left any breakpoints in the target program or it'll
249 die when it hits one. */
251 static void
252 sds_detach (args, from_tty)
253 char *args;
254 int from_tty;
256 char buf[PBUFSIZ];
258 if (args)
259 error ("Argument given to \"detach\" when remotely debugging.");
261 #if 0
262 /* Tell the remote target to detach. */
263 strcpy (buf, "D");
264 sds_send (buf, 1);
265 #endif
267 pop_target ();
268 if (from_tty)
269 puts_filtered ("Ending remote debugging.\n");
272 /* Convert hex digit A to a number. */
274 static int
275 fromhex (a)
276 int a;
278 if (a >= '0' && a <= '9')
279 return a - '0';
280 else if (a >= 'a' && a <= 'f')
281 return a - 'a' + 10;
282 else
283 error ("Reply contains invalid hex digit %d", a);
286 /* Convert number NIB to a hex digit. */
288 static int
289 tohex (nib)
290 int nib;
292 if (nib < 10)
293 return '0'+nib;
294 else
295 return 'a'+nib-10;
298 static int
299 tob64 (inbuf, outbuf, len)
300 unsigned char *inbuf;
301 char *outbuf;
302 int len;
304 int i, sum;
305 char *p;
307 if (len % 3 != 0)
308 error ("bad length");
310 p = outbuf;
311 for (i = 0; i < len; i += 3)
313 /* Collect the next three bytes into a number. */
314 sum = ((long) *inbuf++) << 16;
315 sum |= ((long) *inbuf++) << 8;
316 sum |= ((long) *inbuf++);
318 /* Spit out 4 6-bit encodings. */
319 *p++ = ((sum >> 18) & 0x3f) + '0';
320 *p++ = ((sum >> 12) & 0x3f) + '0';
321 *p++ = ((sum >> 6) & 0x3f) + '0';
322 *p++ = (sum & 0x3f) + '0';
324 return (p - outbuf);
327 static int
328 fromb64 (inbuf, outbuf, len)
329 char *inbuf, *outbuf;
330 int len;
332 int i, sum;
334 if (len % 4 != 0)
335 error ("bad length");
337 for (i = 0; i < len; i += 4)
339 /* Collect 4 6-bit digits. */
340 sum = (*inbuf++ - '0') << 18;
341 sum |= (*inbuf++ - '0') << 12;
342 sum |= (*inbuf++ - '0') << 6;
343 sum |= (*inbuf++ - '0');
345 /* Now take the resulting 24-bit number and get three bytes out
346 of it. */
347 *outbuf++ = (sum >> 16) & 0xff;
348 *outbuf++ = (sum >> 8) & 0xff;
349 *outbuf++ = sum & 0xff;
352 return (len / 4) * 3;
356 /* Tell the remote machine to resume. */
358 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
359 int last_sent_step;
361 static void
362 sds_resume (pid, step, siggnal)
363 int pid, step;
364 enum target_signal siggnal;
366 unsigned char buf[PBUFSIZ];
368 dcache_flush (sds_dcache);
370 last_sent_signal = siggnal;
371 last_sent_step = step;
373 buf[0] = (step ? 21 : 20);
374 buf[1] = 0; /* (should be signal?) */
376 sds_send (buf, 2);
379 /* Send a message to target to halt it. Target will respond, and send
380 us a message pending notice. */
382 static void
383 sds_interrupt (signo)
384 int signo;
386 unsigned char buf[PBUFSIZ];
388 /* If this doesn't work, try more severe steps. */
389 signal (signo, sds_interrupt_twice);
391 if (remote_debug)
392 printf_unfiltered ("sds_interrupt called\n");
394 buf[0] = 25;
395 sds_send (buf, 1);
398 static void (*ofunc)();
400 /* The user typed ^C twice. */
402 static void
403 sds_interrupt_twice (signo)
404 int signo;
406 signal (signo, ofunc);
408 interrupt_query ();
410 signal (signo, sds_interrupt);
413 /* Ask the user what to do when an interrupt is received. */
415 static void
416 interrupt_query ()
418 target_terminal_ours ();
420 if (query ("Interrupted while waiting for the program.\n\
421 Give up (and stop debugging it)? "))
423 target_mourn_inferior ();
424 return_to_top_level (RETURN_QUIT);
427 target_terminal_inferior ();
430 /* If nonzero, ignore the next kill. */
431 int kill_kludge;
433 /* Wait until the remote machine stops, then return, storing status in
434 STATUS just as `wait' would. Returns "pid" (though it's not clear
435 what, if anything, that means in the case of this target). */
437 static int
438 sds_wait (pid, status)
439 int pid;
440 struct target_waitstatus *status;
442 unsigned char buf[PBUFSIZ];
443 int retlen;
445 status->kind = TARGET_WAITKIND_EXITED;
446 status->value.integer = 0;
448 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
450 signal (SIGINT, ofunc);
452 if (just_started)
454 just_started = 0;
455 status->kind = TARGET_WAITKIND_STOPPED;
456 return inferior_pid;
459 while (1)
461 getmessage (buf, 1);
463 if (message_pending)
465 buf[0] = 26;
466 retlen = sds_send (buf, 1);
467 if (remote_debug)
469 fprintf_unfiltered (gdb_stderr, "Signals: %04x %02x %02x\n",
470 ((int) buf[0]) << 8 + buf[1],
471 buf[2], buf[3]);
473 message_pending = 0;
474 status->kind = TARGET_WAITKIND_STOPPED;
475 status->value.sig = TARGET_SIGNAL_TRAP;
476 goto got_status;
479 got_status:
480 return inferior_pid;
483 static unsigned char sprs[16];
485 /* Read the remote registers into the block REGS. */
486 /* Currently we just read all the registers, so we don't use regno. */
488 /* ARGSUSED */
489 static void
490 sds_fetch_registers (regno)
491 int regno;
493 unsigned char buf[PBUFSIZ];
494 int i, retlen;
495 char *p;
496 char regs[REGISTER_BYTES];
498 /* Unimplemented registers read as all bits zero. */
499 memset (regs, 0, REGISTER_BYTES);
501 buf[0] = 18;
502 buf[1] = 1;
503 buf[2] = 0;
504 retlen = sds_send (buf, 3);
506 for (i = 0; i < 4 * 6; ++i)
507 regs[i + 4 * 32 + 8 * 32] = buf[i];
508 for (i = 0; i < 4 * 4; ++i)
509 sprs[i] = buf[i + 4 * 7];
511 buf[0] = 18;
512 buf[1] = 2;
513 buf[2] = 0;
514 retlen = sds_send (buf, 3);
516 for (i = 0; i < retlen; i++)
517 regs[i] = buf[i];
519 /* (should warn about reply too short) */
521 for (i = 0; i < NUM_REGS; i++)
522 supply_register (i, &regs[REGISTER_BYTE(i)]);
525 /* Prepare to store registers. Since we may send them all, we have to
526 read out the ones we don't want to change first. */
528 static void
529 sds_prepare_to_store ()
531 /* Make sure the entire registers array is valid. */
532 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
535 /* Store register REGNO, or all registers if REGNO == -1, from the contents
536 of REGISTERS. FIXME: ignores errors. */
538 static void
539 sds_store_registers (regno)
540 int regno;
542 unsigned char *p, buf[PBUFSIZ];
543 int i;
545 /* Store all the special-purpose registers. */
546 p = buf;
547 *p++ = 19;
548 *p++ = 1;
549 *p++ = 0;
550 *p++ = 0;
551 for (i = 0; i < 4 * 6; i++)
552 *p++ = registers[i + 4 * 32 + 8 * 32];
553 for (i = 0; i < 4 * 1; i++)
554 *p++ = 0;
555 for (i = 0; i < 4 * 4; i++)
556 *p++ = sprs[i];
558 sds_send (buf, p - buf);
560 /* Store all the general-purpose registers. */
561 p = buf;
562 *p++ = 19;
563 *p++ = 2;
564 *p++ = 0;
565 *p++ = 0;
566 for (i = 0; i < 4 * 32; i++)
567 *p++ = registers[i];
569 sds_send (buf, p - buf);
573 /* Write memory data directly to the remote machine. This does not
574 inform the data cache; the data cache uses this. MEMADDR is the
575 address in the remote memory space. MYADDR is the address of the
576 buffer in our space. LEN is the number of bytes.
578 Returns number of bytes transferred, or 0 for error. */
580 static int
581 sds_write_bytes (memaddr, myaddr, len)
582 CORE_ADDR memaddr;
583 char *myaddr;
584 int len;
586 int max_buf_size; /* Max size of packet output buffer */
587 int origlen;
588 unsigned char buf[PBUFSIZ];
589 int todo;
590 int i;
592 /* Chop the transfer down if necessary */
594 max_buf_size = 150;
596 origlen = len;
597 while (len > 0)
599 todo = min (len, max_buf_size);
601 buf[0] = 13;
602 buf[1] = 0;
603 buf[2] = (int) (memaddr >> 24) & 0xff;
604 buf[3] = (int) (memaddr >> 16) & 0xff;
605 buf[4] = (int) (memaddr >> 8) & 0xff;
606 buf[5] = (int) (memaddr ) & 0xff;
607 buf[6] = 1;
608 buf[7] = 0;
610 for (i = 0; i < todo; i++)
611 buf[i + 8] = myaddr[i];
613 sds_send (buf, 8 + todo);
615 /* (should look at result) */
617 myaddr += todo;
618 memaddr += todo;
619 len -= todo;
621 return origlen;
624 /* Read memory data directly from the remote machine. This does not
625 use the data cache; the data cache uses this. MEMADDR is the
626 address in the remote memory space. MYADDR is the address of the
627 buffer in our space. LEN is the number of bytes.
629 Returns number of bytes transferred, or 0 for error. */
631 static int
632 sds_read_bytes (memaddr, myaddr, len)
633 CORE_ADDR memaddr;
634 char *myaddr;
635 int len;
637 int max_buf_size; /* Max size of packet output buffer */
638 int origlen, retlen;
639 unsigned char buf[PBUFSIZ];
640 int todo;
641 int i;
643 /* Chop the transfer down if necessary */
645 max_buf_size = 150;
647 origlen = len;
648 while (len > 0)
650 todo = min (len, max_buf_size);
652 buf[0] = 12;
653 buf[1] = 0;
654 buf[2] = (int) (memaddr >> 24) & 0xff;
655 buf[3] = (int) (memaddr >> 16) & 0xff;
656 buf[4] = (int) (memaddr >> 8) & 0xff;
657 buf[5] = (int) (memaddr ) & 0xff;
658 buf[6] = (int) (todo >> 8) & 0xff;
659 buf[7] = (int) (todo ) & 0xff;
660 buf[8] = 1;
662 retlen = sds_send (buf, 9);
664 if (retlen - 2 != todo)
666 return 0;
669 /* Reply describes memory byte by byte. */
671 for (i = 0; i < todo; i++)
672 myaddr[i] = buf[i + 2];
674 myaddr += todo;
675 memaddr += todo;
676 len -= todo;
679 return origlen;
682 /* Read or write LEN bytes from inferior memory at MEMADDR,
683 transferring to or from debugger address MYADDR. Write to inferior
684 if SHOULD_WRITE is nonzero. Returns length of data written or
685 read; 0 for error. */
687 /* ARGSUSED */
688 static int
689 sds_xfer_memory(memaddr, myaddr, len, should_write, target)
690 CORE_ADDR memaddr;
691 char *myaddr;
692 int len;
693 int should_write;
694 struct target_ops *target; /* ignored */
696 return dcache_xfer_memory (sds_dcache, memaddr, myaddr, len, should_write);
700 static void
701 sds_files_info (ignore)
702 struct target_ops *ignore;
704 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
707 /* Stuff for dealing with the packets which are part of this protocol.
708 See comment at top of file for details. */
710 /* Read a single character from the remote end, masking it down to 7 bits. */
712 static int
713 readchar (timeout)
714 int timeout;
716 int ch;
718 ch = SERIAL_READCHAR (sds_desc, timeout);
720 if (remote_debug > 1 && ch >= 0)
721 printf_unfiltered("%c(%x)", ch, ch);
723 switch (ch)
725 case SERIAL_EOF:
726 error ("Remote connection closed");
727 case SERIAL_ERROR:
728 perror_with_name ("Remote communication error");
729 case SERIAL_TIMEOUT:
730 return ch;
731 default:
732 return ch & 0x7f;
736 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
737 because 253, 254, and 255 are special flags in the protocol.) */
739 static int
740 compute_checksum (csum, buf, len)
741 int csum, len;
742 char *buf;
744 int i;
746 for (i = 0; i < len; ++i)
747 csum += (unsigned char) buf[i];
749 csum %= 253;
750 return csum;
753 /* Send the command in BUF to the remote machine, and read the reply
754 into BUF also. */
756 static int
757 sds_send (buf, len)
758 unsigned char *buf;
759 int len;
761 putmessage (buf, len);
763 return getmessage (buf, 0);
766 /* Send a message to the remote machine. */
768 static int
769 putmessage (buf, len)
770 unsigned char *buf;
771 int len;
773 int i, enclen;
774 unsigned char csum = 0;
775 char buf2[PBUFSIZ], buf3[PBUFSIZ];
776 unsigned char header[3];
777 int ch;
778 int tcount = 0;
779 char *p;
781 /* Copy the packet into buffer BUF2, encapsulating it
782 and giving it a checksum. */
784 if (len > 170) /* Prosanity check */
785 abort();
787 if (remote_debug)
789 fprintf_unfiltered (gdb_stderr, "Message to send: \"");
790 for (i = 0; i < len; ++i)
791 fprintf_unfiltered (gdb_stderr, "%02x", buf[i]);
792 fprintf_unfiltered (gdb_stderr, "\"\n");
795 p = buf2;
796 *p++ = '$';
798 if (len % 3 != 0)
800 buf[len] = '\0';
801 buf[len+1] = '\0';
804 header[1] = next_msg_id;
806 header[2] = len;
808 csum = compute_checksum (csum, buf, len);
809 csum = compute_checksum (csum, header + 1, 2);
811 header[0] = csum;
813 tob64 (header, p, 3);
814 p += 4;
815 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
817 for (i = 0; i < enclen; ++i)
818 *p++ = buf3[i];
819 *p++ = '\r';
820 *p++ = '\n';
822 next_msg_id = (next_msg_id + 3) % 245;
824 /* Send it over and over until we get a positive ack. */
826 while (1)
828 int started_error_output = 0;
830 if (remote_debug)
832 *p = '\0';
833 printf_unfiltered ("Sending encoded: \"%s\"", buf2);
834 printf_unfiltered (" (Checksum %d, id %d, length %d)\n",
835 header[0], header[1], header[2]);
836 gdb_flush (gdb_stdout);
838 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
839 perror_with_name ("putmessage: write failed");
841 return 1;
847 /* Come here after finding the start of the frame. Collect the rest
848 into BUF. Returns 0 on any error, 1 on success. */
850 static int
851 read_frame (buf)
852 char *buf;
854 char *bp;
855 int c;
857 bp = buf;
859 while (1)
861 c = readchar (sds_timeout);
863 switch (c)
865 case SERIAL_TIMEOUT:
866 if (remote_debug)
867 puts_filtered ("Timeout in mid-message, retrying\n");
868 return 0;
869 case '$':
870 if (remote_debug)
871 puts_filtered ("Saw new packet start in middle of old one\n");
872 return 0; /* Start a new packet, count retries */
873 case '\r':
874 break;
876 case '\n':
878 *bp = '\000';
879 if (remote_debug)
880 fprintf_unfiltered (gdb_stderr, "Received encoded: \"%s\"\n",
881 buf);
882 return 1;
885 default:
886 if (bp < buf + PBUFSIZ - 1)
888 *bp++ = c;
889 continue;
892 *bp = '\0';
893 puts_filtered ("Message too long: ");
894 puts_filtered (buf);
895 puts_filtered ("\n");
897 return 0;
902 /* Read a packet from the remote machine, with error checking,
903 and store it in BUF. BUF is expected to be of size PBUFSIZ.
904 If FOREVER, wait forever rather than timing out; this is used
905 while the target is executing user code. */
907 static int
908 getmessage (buf, forever)
909 unsigned char *buf;
910 int forever;
912 int c, c2, c3;
913 int tries;
914 int timeout;
915 int val, i, len, csum;
916 unsigned char header[3];
917 unsigned char inbuf[500];
919 strcpy (buf, "timeout");
921 if (forever)
923 #ifdef MAINTENANCE_CMDS
924 timeout = watchdog > 0 ? watchdog : -1;
925 #else
926 timeout = -1;
927 #endif
930 else
931 timeout = sds_timeout;
933 #define MAX_TRIES 3
935 for (tries = 1; tries <= MAX_TRIES; tries++)
937 /* This can loop forever if the remote side sends us characters
938 continuously, but if it pauses, we'll get a zero from readchar
939 because of timeout. Then we'll count that as a retry. */
941 /* Note that we will only wait forever prior to the start of a packet.
942 After that, we expect characters to arrive at a brisk pace. They
943 should show up within sds_timeout intervals. */
947 c = readchar (timeout);
949 if (c == SERIAL_TIMEOUT)
951 #ifdef MAINTENANCE_CMDS
952 if (forever) /* Watchdog went off. Kill the target. */
954 target_mourn_inferior ();
955 error ("Watchdog has expired. Target detached.\n");
957 #endif
958 if (remote_debug)
959 puts_filtered ("Timed out.\n");
960 goto retry;
963 while (c != '$' && c != '{');
965 /* We might have seen a "trigraph", a sequence of three characters
966 that indicate various sorts of communication state. */
968 if (c == '{')
970 /* Read the other two chars of the trigraph. */
971 c2 = readchar (timeout);
972 c3 = readchar (timeout);
973 if (remote_debug)
974 fprintf_unfiltered (gdb_stderr, "Trigraph %c%c%c received\n",
975 c, c2, c3);
976 if (c3 == '+')
978 message_pending = 1;
979 return 0; /*????*/
981 continue;
984 val = read_frame (inbuf);
986 if (val == 1)
988 fromb64 (inbuf, header, 4);
989 /* (should check out other bits) */
990 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
992 len = header[2];
994 csum = 0;
995 csum = compute_checksum (csum, buf, len);
996 csum = compute_checksum (csum, header + 1, 2);
998 if (csum != header[0])
999 fprintf_unfiltered (gdb_stderr,
1000 "Checksum mismatch: computed %d, received %d\n",
1001 csum, header[0]);
1003 if (header[2] == 0xff)
1004 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
1006 if (remote_debug)
1008 fprintf_unfiltered (gdb_stderr,
1009 "... (Got checksum %d, id %d, length %d)\n",
1010 header[0], header[1], header[2]);
1011 fprintf_unfiltered (gdb_stderr, "Message received: \"");
1012 for (i = 0; i < len; ++i)
1014 fprintf_unfiltered (gdb_stderr, "%02x", (unsigned char) buf[i]);
1016 fprintf_unfiltered (gdb_stderr, "\"\n");
1019 /* no ack required? */
1020 return len;
1023 /* Try the whole thing again. */
1024 retry:
1025 /* need to do something here */
1028 /* We have tried hard enough, and just can't receive the packet. Give up. */
1030 printf_unfiltered ("Ignoring packet error, continuing...\n");
1031 return 0;
1034 static void
1035 sds_kill ()
1037 /* Don't try to do anything to the target. */
1040 static void
1041 sds_mourn ()
1043 unpush_target (&sds_ops);
1044 generic_mourn_inferior ();
1047 static void
1048 sds_create_inferior (exec_file, args, env)
1049 char *exec_file;
1050 char *args;
1051 char **env;
1053 inferior_pid = 42000;
1055 /* Clean up from the last time we were running. */
1056 clear_proceed_status ();
1058 /* Let the remote process run. */
1059 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1062 static void
1063 sds_load (filename, from_tty)
1064 char *filename;
1065 int from_tty;
1067 generic_load (filename, from_tty);
1069 inferior_pid = 0;
1072 /* The SDS monitor has commands for breakpoint insertion, although it
1073 it doesn't actually manage the breakpoints, it just returns the
1074 replaced instruction back to the debugger. */
1076 static int
1077 sds_insert_breakpoint (addr, contents_cache)
1078 CORE_ADDR addr;
1079 char *contents_cache;
1081 int i, retlen;
1082 unsigned char *p, buf[PBUFSIZ];
1084 p = buf;
1085 *p++ = 16;
1086 *p++ = 0;
1087 *p++ = (int) (addr >> 24) & 0xff;
1088 *p++ = (int) (addr >> 16) & 0xff;
1089 *p++ = (int) (addr >> 8) & 0xff;
1090 *p++ = (int) (addr ) & 0xff;
1092 retlen = sds_send (buf, p - buf);
1094 for (i = 0; i < 4; ++i)
1095 contents_cache[i] = buf[i + 2];
1097 return 0;
1100 static int
1101 sds_remove_breakpoint (addr, contents_cache)
1102 CORE_ADDR addr;
1103 char *contents_cache;
1105 int i, retlen;
1106 unsigned char *p, buf[PBUFSIZ];
1108 p = buf;
1109 *p++ = 17;
1110 *p++ = 0;
1111 *p++ = (int) (addr >> 24) & 0xff;
1112 *p++ = (int) (addr >> 16) & 0xff;
1113 *p++ = (int) (addr >> 8) & 0xff;
1114 *p++ = (int) (addr ) & 0xff;
1115 for (i = 0; i < 4; ++i)
1116 *p++ = contents_cache[i];
1118 retlen = sds_send (buf, p - buf);
1120 return 0;
1123 /* Define the target operations vector. */
1125 static struct target_ops sds_ops ;
1127 static void init_sds_ops(void)
1129 sds_ops.to_shortname = "sds";
1130 sds_ops.to_longname = "Remote serial target with SDS protocol";
1131 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1132 Specify the serial device it is connected to (e.g. /dev/ttya).",
1133 sds_ops.to_open = sds_open;
1134 sds_ops.to_close = sds_close;
1135 sds_ops.to_attach = NULL;
1136 sds_ops.to_detach = sds_detach;
1137 sds_ops.to_resume = sds_resume;
1138 sds_ops.to_wait = sds_wait;
1139 sds_ops.to_fetch_registers = sds_fetch_registers;
1140 sds_ops.to_store_registers = sds_store_registers;
1141 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1142 sds_ops.to_xfer_memory = sds_xfer_memory;
1143 sds_ops.to_files_info = sds_files_info;
1144 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1145 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1146 sds_ops.to_terminal_init = NULL;
1147 sds_ops.to_terminal_inferior = NULL;
1148 sds_ops.to_terminal_ours_for_output = NULL;
1149 sds_ops.to_terminal_ours = NULL;
1150 sds_ops.to_terminal_info = NULL;
1151 sds_ops.to_kill = sds_kill;
1152 sds_ops.to_load = sds_load;
1153 sds_ops.to_lookup_symbol = NULL;
1154 sds_ops.to_create_inferior = sds_create_inferior;
1155 sds_ops.to_mourn_inferior = sds_mourn;
1156 sds_ops.to_can_run = 0;
1157 sds_ops.to_notice_signals = 0;
1158 sds_ops.to_thread_alive = 0;
1159 sds_ops.to_stop = 0;
1160 sds_ops.to_stratum = process_stratum;
1161 sds_ops.DONT_USE = NULL;
1162 sds_ops.to_has_all_memory = 1;
1163 sds_ops.to_has_memory = 1;
1164 sds_ops.to_has_stack = 1; /* to_has_stack */
1165 sds_ops.to_has_registers = 1; sds_ops.to_has_execution = 1;
1166 sds_ops.to_sections = NULL;
1167 sds_ops.to_sections_end = NULL;
1168 sds_ops.to_magic = OPS_MAGIC ;
1171 /* Put a command string, in args, out to the monitor and display the
1172 reply message. */
1174 static void
1175 sds_command (args, from_tty)
1176 char *args;
1177 int from_tty;
1179 char *p;
1180 int i, len, retlen;
1181 unsigned char buf[1000];
1183 /* Convert hexadecimal chars into a byte buffer. */
1184 p = args;
1185 len = 0;
1186 while (*p != '\0')
1188 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1189 if (p[1] == '\0')
1190 break;
1191 p += 2;
1194 retlen = sds_send (buf, len);
1196 printf_filtered ("Reply is ");
1197 for (i = 0; i < retlen; ++i)
1199 printf_filtered ("%02x", buf[i]);
1201 printf_filtered ("\n");
1204 void
1205 _initialize_remote_sds ()
1207 init_sds_ops() ;
1208 add_target (&sds_ops);
1210 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1211 var_integer, (char *)&sds_timeout,
1212 "Set timeout value for sds read.\n", &setlist),
1213 &showlist);
1215 add_com ("sds", class_obscure, sds_command,
1216 "Send a command to the SDS monitor.");