* Contribute CGEN simulator build support code.
[binutils-gdb.git] / gdb / remote-sds.c
blob582d46b31c34822496b90276609122193d321591
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
24 site. -sts 8/13/97 */
26 #include "defs.h"
27 #include "gdb_string.h"
28 #include <fcntl.h>
29 #include "frame.h"
30 #include "inferior.h"
31 #include "bfd.h"
32 #include "symfile.h"
33 #include "target.h"
34 #include "gdb_wait.h"
35 #include "gdbcmd.h"
36 #include "objfiles.h"
37 #include "gdb-stabs.h"
38 #include "gdbthread.h"
39 #include "gdbcore.h"
40 #include "dcache.h"
42 #ifdef USG
43 #include <sys/types.h>
44 #endif
46 #include <signal.h>
47 #include "serial.h"
49 extern void _initialize_remote_sds (void);
51 /* Declarations of local functions. */
53 static int sds_write_bytes (CORE_ADDR, char *, int);
55 static int sds_read_bytes (CORE_ADDR, char *, int);
57 static void sds_files_info (struct target_ops *ignore);
59 static int sds_xfer_memory (CORE_ADDR, char *, int, int, 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
122 be plenty. */
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
128 starts. */
130 static serial_t sds_desc = NULL;
132 /* This limit comes from the monitor. */
134 #define PBUFSIZ 250
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. */
149 /* ARGSUSED */
150 static void
151 sds_close (int quitting)
153 if (sds_desc)
154 SERIAL_CLOSE (sds_desc);
155 sds_desc = NULL;
158 /* Stub for catch_errors. */
160 static int
161 sds_start_remote (PTR dummy)
163 char c;
164 unsigned char buf[200];
166 immediate_quit = 1; /* 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");
176 next_msg_id = 251;
178 buf[0] = 26;
179 sds_send (buf, 1);
181 buf[0] = 0;
182 sds_send (buf, 1);
184 immediate_quit = 0;
186 start_remote (); /* Initialize gdb process mechanisms */
187 return 1;
190 /* Open a connection to a remote debugger.
191 NAME is the filename used for communication. */
193 static DCACHE *sds_dcache;
195 static void
196 sds_open (char *name, int from_tty)
198 if (name == 0)
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 if (!sds_dcache)
207 sds_dcache = dcache_init (sds_read_bytes, sds_write_bytes);
208 else
209 dcache_invd (sds_dcache);
211 sds_desc = SERIAL_OPEN (name);
212 if (!sds_desc)
213 perror_with_name (name);
215 if (baud_rate != -1)
217 if (SERIAL_SETBAUDRATE (sds_desc, baud_rate))
219 SERIAL_CLOSE (sds_desc);
220 perror_with_name (name);
225 SERIAL_RAW (sds_desc);
227 /* If there is something sitting in the buffer we might take it as a
228 response to a command, which would be bad. */
229 SERIAL_FLUSH_INPUT (sds_desc);
231 if (from_tty)
233 puts_filtered ("Remote debugging using ");
234 puts_filtered (name);
235 puts_filtered ("\n");
237 push_target (&sds_ops); /* Switch to using remote target now */
239 just_started = 1;
241 /* Start the remote connection; if error (0), discard this target.
242 In particular, if the user quits, be sure to discard it (we'd be
243 in an inconsistent state otherwise). */
244 if (!catch_errors (sds_start_remote, NULL,
245 "Couldn't establish connection to remote target\n",
246 RETURN_MASK_ALL))
247 pop_target ();
250 /* This takes a program previously attached to and detaches it. After
251 this is done, GDB can be used to debug some other program. We
252 better not have left any breakpoints in the target program or it'll
253 die when it hits one. */
255 static void
256 sds_detach (char *args, int from_tty)
258 char buf[PBUFSIZ];
260 if (args)
261 error ("Argument given to \"detach\" when remotely debugging.");
263 #if 0
264 /* Tell the remote target to detach. */
265 strcpy (buf, "D");
266 sds_send (buf, 1);
267 #endif
269 pop_target ();
270 if (from_tty)
271 puts_filtered ("Ending remote debugging.\n");
274 /* Convert hex digit A to a number. */
276 static int
277 fromhex (int a)
279 if (a >= '0' && a <= '9')
280 return a - '0';
281 else if (a >= 'a' && a <= 'f')
282 return a - 'a' + 10;
283 else
284 error ("Reply contains invalid hex digit %d", a);
287 /* Convert number NIB to a hex digit. */
289 static int
290 tohex (int nib)
292 if (nib < 10)
293 return '0' + nib;
294 else
295 return 'a' + nib - 10;
298 static int
299 tob64 (unsigned char *inbuf, char *outbuf, int len)
301 int i, sum;
302 char *p;
304 if (len % 3 != 0)
305 error ("bad length");
307 p = outbuf;
308 for (i = 0; i < len; i += 3)
310 /* Collect the next three bytes into a number. */
311 sum = ((long) *inbuf++) << 16;
312 sum |= ((long) *inbuf++) << 8;
313 sum |= ((long) *inbuf++);
315 /* Spit out 4 6-bit encodings. */
316 *p++ = ((sum >> 18) & 0x3f) + '0';
317 *p++ = ((sum >> 12) & 0x3f) + '0';
318 *p++ = ((sum >> 6) & 0x3f) + '0';
319 *p++ = (sum & 0x3f) + '0';
321 return (p - outbuf);
324 static int
325 fromb64 (char *inbuf, char *outbuf, int len)
327 int i, sum;
329 if (len % 4 != 0)
330 error ("bad length");
332 for (i = 0; i < len; i += 4)
334 /* Collect 4 6-bit digits. */
335 sum = (*inbuf++ - '0') << 18;
336 sum |= (*inbuf++ - '0') << 12;
337 sum |= (*inbuf++ - '0') << 6;
338 sum |= (*inbuf++ - '0');
340 /* Now take the resulting 24-bit number and get three bytes out
341 of it. */
342 *outbuf++ = (sum >> 16) & 0xff;
343 *outbuf++ = (sum >> 8) & 0xff;
344 *outbuf++ = sum & 0xff;
347 return (len / 4) * 3;
351 /* Tell the remote machine to resume. */
353 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
354 int last_sent_step;
356 static void
357 sds_resume (int pid, int step, enum target_signal siggnal)
359 unsigned char buf[PBUFSIZ];
361 dcache_invd (sds_dcache);
363 last_sent_signal = siggnal;
364 last_sent_step = step;
366 buf[0] = (step ? 21 : 20);
367 buf[1] = 0; /* (should be signal?) */
369 sds_send (buf, 2);
372 /* Send a message to target to halt it. Target will respond, and send
373 us a message pending notice. */
375 static void
376 sds_interrupt (int signo)
378 unsigned char buf[PBUFSIZ];
380 /* If this doesn't work, try more severe steps. */
381 signal (signo, sds_interrupt_twice);
383 if (remote_debug)
384 fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
386 buf[0] = 25;
387 sds_send (buf, 1);
390 static void (*ofunc) ();
392 /* The user typed ^C twice. */
394 static void
395 sds_interrupt_twice (int signo)
397 signal (signo, ofunc);
399 interrupt_query ();
401 signal (signo, sds_interrupt);
404 /* Ask the user what to do when an interrupt is received. */
406 static void
407 interrupt_query (void)
409 target_terminal_ours ();
411 if (query ("Interrupted while waiting for the program.\n\
412 Give up (and stop debugging it)? "))
414 target_mourn_inferior ();
415 return_to_top_level (RETURN_QUIT);
418 target_terminal_inferior ();
421 /* If nonzero, ignore the next kill. */
422 int kill_kludge;
424 /* Wait until the remote machine stops, then return, storing status in
425 STATUS just as `wait' would. Returns "pid" (though it's not clear
426 what, if anything, that means in the case of this target). */
428 static int
429 sds_wait (int pid, struct target_waitstatus *status)
431 unsigned char buf[PBUFSIZ];
432 int retlen;
434 status->kind = TARGET_WAITKIND_EXITED;
435 status->value.integer = 0;
437 ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
439 signal (SIGINT, ofunc);
441 if (just_started)
443 just_started = 0;
444 status->kind = TARGET_WAITKIND_STOPPED;
445 return inferior_pid;
448 while (1)
450 getmessage (buf, 1);
452 if (message_pending)
454 buf[0] = 26;
455 retlen = sds_send (buf, 1);
456 if (remote_debug)
458 fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
459 buf[0], buf[1],
460 buf[2], buf[3]);
462 message_pending = 0;
463 status->kind = TARGET_WAITKIND_STOPPED;
464 status->value.sig = TARGET_SIGNAL_TRAP;
465 goto got_status;
468 got_status:
469 return inferior_pid;
472 static unsigned char sprs[16];
474 /* Read the remote registers into the block REGS. */
475 /* Currently we just read all the registers, so we don't use regno. */
477 /* ARGSUSED */
478 static void
479 sds_fetch_registers (int regno)
481 unsigned char buf[PBUFSIZ];
482 int i, retlen;
483 char regs[REGISTER_BYTES];
485 /* Unimplemented registers read as all bits zero. */
486 memset (regs, 0, REGISTER_BYTES);
488 buf[0] = 18;
489 buf[1] = 1;
490 buf[2] = 0;
491 retlen = sds_send (buf, 3);
493 for (i = 0; i < 4 * 6; ++i)
494 regs[i + 4 * 32 + 8 * 32] = buf[i];
495 for (i = 0; i < 4 * 4; ++i)
496 sprs[i] = buf[i + 4 * 7];
498 buf[0] = 18;
499 buf[1] = 2;
500 buf[2] = 0;
501 retlen = sds_send (buf, 3);
503 for (i = 0; i < retlen; i++)
504 regs[i] = buf[i];
506 /* (should warn about reply too short) */
508 for (i = 0; i < NUM_REGS; i++)
509 supply_register (i, &regs[REGISTER_BYTE (i)]);
512 /* Prepare to store registers. Since we may send them all, we have to
513 read out the ones we don't want to change first. */
515 static void
516 sds_prepare_to_store (void)
518 /* Make sure the entire registers array is valid. */
519 read_register_bytes (0, (char *) NULL, REGISTER_BYTES);
522 /* Store register REGNO, or all registers if REGNO == -1, from the contents
523 of REGISTERS. FIXME: ignores errors. */
525 static void
526 sds_store_registers (int regno)
528 unsigned char *p, buf[PBUFSIZ];
529 int i;
531 /* Store all the special-purpose registers. */
532 p = buf;
533 *p++ = 19;
534 *p++ = 1;
535 *p++ = 0;
536 *p++ = 0;
537 for (i = 0; i < 4 * 6; i++)
538 *p++ = registers[i + 4 * 32 + 8 * 32];
539 for (i = 0; i < 4 * 1; i++)
540 *p++ = 0;
541 for (i = 0; i < 4 * 4; i++)
542 *p++ = sprs[i];
544 sds_send (buf, p - buf);
546 /* Store all the general-purpose registers. */
547 p = buf;
548 *p++ = 19;
549 *p++ = 2;
550 *p++ = 0;
551 *p++ = 0;
552 for (i = 0; i < 4 * 32; i++)
553 *p++ = registers[i];
555 sds_send (buf, p - buf);
559 /* Write memory data directly to the remote machine. This does not
560 inform the data cache; the data cache uses this. MEMADDR is the
561 address in the remote memory space. MYADDR is the address of the
562 buffer in our space. LEN is the number of bytes.
564 Returns number of bytes transferred, or 0 for error. */
566 static int
567 sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
569 int max_buf_size; /* Max size of packet output buffer */
570 int origlen;
571 unsigned char buf[PBUFSIZ];
572 int todo;
573 int i;
575 /* Chop the transfer down if necessary */
577 max_buf_size = 150;
579 origlen = len;
580 while (len > 0)
582 todo = min (len, max_buf_size);
584 buf[0] = 13;
585 buf[1] = 0;
586 buf[2] = (int) (memaddr >> 24) & 0xff;
587 buf[3] = (int) (memaddr >> 16) & 0xff;
588 buf[4] = (int) (memaddr >> 8) & 0xff;
589 buf[5] = (int) (memaddr) & 0xff;
590 buf[6] = 1;
591 buf[7] = 0;
593 for (i = 0; i < todo; i++)
594 buf[i + 8] = myaddr[i];
596 sds_send (buf, 8 + todo);
598 /* (should look at result) */
600 myaddr += todo;
601 memaddr += todo;
602 len -= todo;
604 return origlen;
607 /* Read memory data directly from the remote machine. This does not
608 use the data cache; the data cache uses this. MEMADDR is the
609 address in the remote memory space. MYADDR is the address of the
610 buffer in our space. LEN is the number of bytes.
612 Returns number of bytes transferred, or 0 for error. */
614 static int
615 sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
617 int max_buf_size; /* Max size of packet output buffer */
618 int origlen, retlen;
619 unsigned char buf[PBUFSIZ];
620 int todo;
621 int i;
623 /* Chop the transfer down if necessary */
625 max_buf_size = 150;
627 origlen = len;
628 while (len > 0)
630 todo = min (len, max_buf_size);
632 buf[0] = 12;
633 buf[1] = 0;
634 buf[2] = (int) (memaddr >> 24) & 0xff;
635 buf[3] = (int) (memaddr >> 16) & 0xff;
636 buf[4] = (int) (memaddr >> 8) & 0xff;
637 buf[5] = (int) (memaddr) & 0xff;
638 buf[6] = (int) (todo >> 8) & 0xff;
639 buf[7] = (int) (todo) & 0xff;
640 buf[8] = 1;
642 retlen = sds_send (buf, 9);
644 if (retlen - 2 != todo)
646 return 0;
649 /* Reply describes memory byte by byte. */
651 for (i = 0; i < todo; i++)
652 myaddr[i] = buf[i + 2];
654 myaddr += todo;
655 memaddr += todo;
656 len -= todo;
659 return origlen;
662 /* Read or write LEN bytes from inferior memory at MEMADDR,
663 transferring to or from debugger address MYADDR. Write to inferior
664 if SHOULD_WRITE is nonzero. Returns length of data written or
665 read; 0 for error. */
667 /* ARGSUSED */
668 static int
669 sds_xfer_memory (memaddr, myaddr, len, should_write, target)
670 CORE_ADDR memaddr;
671 char *myaddr;
672 int len;
673 int should_write;
674 struct target_ops *target; /* ignored */
676 return dcache_xfer_memory (sds_dcache, memaddr, myaddr, len, should_write);
680 static void
681 sds_files_info (struct target_ops *ignore)
683 puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
686 /* Stuff for dealing with the packets which are part of this protocol.
687 See comment at top of file for details. */
689 /* Read a single character from the remote end, masking it down to 7 bits. */
691 static int
692 readchar (int timeout)
694 int ch;
696 ch = SERIAL_READCHAR (sds_desc, timeout);
698 if (remote_debug > 1 && ch >= 0)
699 fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
701 switch (ch)
703 case SERIAL_EOF:
704 error ("Remote connection closed");
705 case SERIAL_ERROR:
706 perror_with_name ("Remote communication error");
707 case SERIAL_TIMEOUT:
708 return ch;
709 default:
710 return ch & 0x7f;
714 /* An SDS-style checksum is a sum of the bytes modulo 253. (Presumably
715 because 253, 254, and 255 are special flags in the protocol.) */
717 static int
718 compute_checksum (int csum, char *buf, int len)
720 int i;
722 for (i = 0; i < len; ++i)
723 csum += (unsigned char) buf[i];
725 csum %= 253;
726 return csum;
729 /* Send the command in BUF to the remote machine, and read the reply
730 into BUF also. */
732 static int
733 sds_send (unsigned char *buf, int len)
735 putmessage (buf, len);
737 return getmessage (buf, 0);
740 /* Send a message to the remote machine. */
742 static int
743 putmessage (unsigned char *buf, int len)
745 int i, enclen;
746 unsigned char csum = 0;
747 char buf2[PBUFSIZ], buf3[PBUFSIZ];
748 unsigned char header[3];
749 char *p;
751 /* Copy the packet into buffer BUF2, encapsulating it
752 and giving it a checksum. */
754 if (len > 170) /* Prosanity check */
755 abort ();
757 if (remote_debug)
759 fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
760 for (i = 0; i < len; ++i)
761 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
762 fprintf_unfiltered (gdb_stdlog, "\"\n");
765 p = buf2;
766 *p++ = '$';
768 if (len % 3 != 0)
770 buf[len] = '\0';
771 buf[len + 1] = '\0';
774 header[1] = next_msg_id;
776 header[2] = len;
778 csum = compute_checksum (csum, buf, len);
779 csum = compute_checksum (csum, header + 1, 2);
781 header[0] = csum;
783 tob64 (header, p, 3);
784 p += 4;
785 enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
787 for (i = 0; i < enclen; ++i)
788 *p++ = buf3[i];
789 *p++ = '\r';
790 *p++ = '\n';
792 next_msg_id = (next_msg_id + 3) % 245;
794 /* Send it over and over until we get a positive ack. */
796 while (1)
798 if (remote_debug)
800 *p = '\0';
801 fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
802 fprintf_unfiltered (gdb_stdlog,
803 " (Checksum %d, id %d, length %d)\n",
804 header[0], header[1], header[2]);
805 gdb_flush (gdb_stdlog);
807 if (SERIAL_WRITE (sds_desc, buf2, p - buf2))
808 perror_with_name ("putmessage: write failed");
810 return 1;
814 /* Come here after finding the start of the frame. Collect the rest
815 into BUF. Returns 0 on any error, 1 on success. */
817 static int
818 read_frame (char *buf)
820 char *bp;
821 int c;
823 bp = buf;
825 while (1)
827 c = readchar (sds_timeout);
829 switch (c)
831 case SERIAL_TIMEOUT:
832 if (remote_debug)
833 fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
834 return 0;
835 case '$':
836 if (remote_debug)
837 fputs_filtered ("Saw new packet start in middle of old one\n",
838 gdb_stdlog);
839 return 0; /* Start a new packet, count retries */
840 case '\r':
841 break;
843 case '\n':
845 *bp = '\000';
846 if (remote_debug)
847 fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
848 buf);
849 return 1;
852 default:
853 if (bp < buf + PBUFSIZ - 1)
855 *bp++ = c;
856 continue;
859 *bp = '\0';
860 puts_filtered ("Message too long: ");
861 puts_filtered (buf);
862 puts_filtered ("\n");
864 return 0;
869 /* Read a packet from the remote machine, with error checking,
870 and store it in BUF. BUF is expected to be of size PBUFSIZ.
871 If FOREVER, wait forever rather than timing out; this is used
872 while the target is executing user code. */
874 static int
875 getmessage (unsigned char *buf, int forever)
877 int c, c2, c3;
878 int tries;
879 int timeout;
880 int val, i, len, csum;
881 unsigned char header[3];
882 unsigned char inbuf[500];
884 strcpy (buf, "timeout");
886 if (forever)
888 timeout = watchdog > 0 ? watchdog : -1;
891 else
892 timeout = sds_timeout;
894 #define MAX_TRIES 3
896 for (tries = 1; tries <= MAX_TRIES; tries++)
898 /* This can loop forever if the remote side sends us characters
899 continuously, but if it pauses, we'll get a zero from readchar
900 because of timeout. Then we'll count that as a retry. */
902 /* Note that we will only wait forever prior to the start of a packet.
903 After that, we expect characters to arrive at a brisk pace. They
904 should show up within sds_timeout intervals. */
908 c = readchar (timeout);
910 if (c == SERIAL_TIMEOUT)
912 if (forever) /* Watchdog went off. Kill the target. */
914 target_mourn_inferior ();
915 error ("Watchdog has expired. Target detached.\n");
917 if (remote_debug)
918 fputs_filtered ("Timed out.\n", gdb_stdlog);
919 goto retry;
922 while (c != '$' && c != '{');
924 /* We might have seen a "trigraph", a sequence of three characters
925 that indicate various sorts of communication state. */
927 if (c == '{')
929 /* Read the other two chars of the trigraph. */
930 c2 = readchar (timeout);
931 c3 = readchar (timeout);
932 if (remote_debug)
933 fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
934 c, c2, c3);
935 if (c3 == '+')
937 message_pending = 1;
938 return 0; /*???? */
940 continue;
943 val = read_frame (inbuf);
945 if (val == 1)
947 fromb64 (inbuf, header, 4);
948 /* (should check out other bits) */
949 fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
951 len = header[2];
953 csum = 0;
954 csum = compute_checksum (csum, buf, len);
955 csum = compute_checksum (csum, header + 1, 2);
957 if (csum != header[0])
958 fprintf_unfiltered (gdb_stderr,
959 "Checksum mismatch: computed %d, received %d\n",
960 csum, header[0]);
962 if (header[2] == 0xff)
963 fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
965 if (remote_debug)
967 fprintf_unfiltered (gdb_stdlog,
968 "... (Got checksum %d, id %d, length %d)\n",
969 header[0], header[1], header[2]);
970 fprintf_unfiltered (gdb_stdlog, "Message received: \"");
971 for (i = 0; i < len; ++i)
973 fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
975 fprintf_unfiltered (gdb_stdlog, "\"\n");
978 /* no ack required? */
979 return len;
982 /* Try the whole thing again. */
983 retry:
984 /* need to do something here */
987 /* We have tried hard enough, and just can't receive the packet. Give up. */
989 printf_unfiltered ("Ignoring packet error, continuing...\n");
990 return 0;
993 static void
994 sds_kill (void)
996 /* Don't try to do anything to the target. */
999 static void
1000 sds_mourn (void)
1002 unpush_target (&sds_ops);
1003 generic_mourn_inferior ();
1006 static void
1007 sds_create_inferior (char *exec_file, char *args, char **env)
1009 inferior_pid = 42000;
1011 /* Clean up from the last time we were running. */
1012 clear_proceed_status ();
1014 /* Let the remote process run. */
1015 proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
1018 static void
1019 sds_load (char *filename, int from_tty)
1021 generic_load (filename, from_tty);
1023 inferior_pid = 0;
1026 /* The SDS monitor has commands for breakpoint insertion, although it
1027 it doesn't actually manage the breakpoints, it just returns the
1028 replaced instruction back to the debugger. */
1030 static int
1031 sds_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
1033 int i, retlen;
1034 unsigned char *p, buf[PBUFSIZ];
1036 p = buf;
1037 *p++ = 16;
1038 *p++ = 0;
1039 *p++ = (int) (addr >> 24) & 0xff;
1040 *p++ = (int) (addr >> 16) & 0xff;
1041 *p++ = (int) (addr >> 8) & 0xff;
1042 *p++ = (int) (addr) & 0xff;
1044 retlen = sds_send (buf, p - buf);
1046 for (i = 0; i < 4; ++i)
1047 contents_cache[i] = buf[i + 2];
1049 return 0;
1052 static int
1053 sds_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
1055 int i, retlen;
1056 unsigned char *p, buf[PBUFSIZ];
1058 p = buf;
1059 *p++ = 17;
1060 *p++ = 0;
1061 *p++ = (int) (addr >> 24) & 0xff;
1062 *p++ = (int) (addr >> 16) & 0xff;
1063 *p++ = (int) (addr >> 8) & 0xff;
1064 *p++ = (int) (addr) & 0xff;
1065 for (i = 0; i < 4; ++i)
1066 *p++ = contents_cache[i];
1068 retlen = sds_send (buf, p - buf);
1070 return 0;
1073 static void
1074 init_sds_ops (void)
1076 sds_ops.to_shortname = "sds";
1077 sds_ops.to_longname = "Remote serial target with SDS protocol";
1078 sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1079 Specify the serial device it is connected to (e.g. /dev/ttya).";
1080 sds_ops.to_open = sds_open;
1081 sds_ops.to_close = sds_close;
1082 sds_ops.to_detach = sds_detach;
1083 sds_ops.to_resume = sds_resume;
1084 sds_ops.to_wait = sds_wait;
1085 sds_ops.to_fetch_registers = sds_fetch_registers;
1086 sds_ops.to_store_registers = sds_store_registers;
1087 sds_ops.to_prepare_to_store = sds_prepare_to_store;
1088 sds_ops.to_xfer_memory = sds_xfer_memory;
1089 sds_ops.to_files_info = sds_files_info;
1090 sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1091 sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1092 sds_ops.to_kill = sds_kill;
1093 sds_ops.to_load = sds_load;
1094 sds_ops.to_create_inferior = sds_create_inferior;
1095 sds_ops.to_mourn_inferior = sds_mourn;
1096 sds_ops.to_stratum = process_stratum;
1097 sds_ops.to_has_all_memory = 1;
1098 sds_ops.to_has_memory = 1;
1099 sds_ops.to_has_stack = 1;
1100 sds_ops.to_has_registers = 1;
1101 sds_ops.to_has_execution = 1;
1102 sds_ops.to_magic = OPS_MAGIC;
1105 /* Put a command string, in args, out to the monitor and display the
1106 reply message. */
1108 static void
1109 sds_command (char *args, int from_tty)
1111 char *p;
1112 int i, len, retlen;
1113 unsigned char buf[1000];
1115 /* Convert hexadecimal chars into a byte buffer. */
1116 p = args;
1117 len = 0;
1118 while (*p != '\0')
1120 buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1121 if (p[1] == '\0')
1122 break;
1123 p += 2;
1126 retlen = sds_send (buf, len);
1128 printf_filtered ("Reply is ");
1129 for (i = 0; i < retlen; ++i)
1131 printf_filtered ("%02x", buf[i]);
1133 printf_filtered ("\n");
1136 void
1137 _initialize_remote_sds (void)
1139 init_sds_ops ();
1140 add_target (&sds_ops);
1142 add_show_from_set (add_set_cmd ("sdstimeout", no_class,
1143 var_integer, (char *) &sds_timeout,
1144 "Set timeout value for sds read.\n", &setlist),
1145 &showlist);
1147 add_com ("sds", class_obscure, sds_command,
1148 "Send a command to the SDS monitor.");