OSDN Git Service

2005-02-11 Andrew Cagney <cagney@gnu.org>
[pf3gnuchains/pf3gnuchains3x.git] / gdb / remote-sds.c
1 /* Remote target communications for serial-line targets using SDS' protocol.
2
3    Copyright 1997, 1998, 1999, 2000, 2001, 2002, 2004 Free Software
4    Foundation, Inc.
5
6    This file is part of GDB.
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22
23 /* This interface was written by studying the behavior of the SDS
24    monitor on an ADS 821/860 board, and by consulting the
25    documentation of the monitor that is available on Motorola's web
26    site.  -sts 8/13/97 */
27
28 #include "defs.h"
29 #include "gdb_string.h"
30 #include <fcntl.h>
31 #include "frame.h"
32 #include "inferior.h"
33 #include "exceptions.h"
34 #include "bfd.h"
35 #include "symfile.h"
36 #include "target.h"
37 #include "gdbcmd.h"
38 #include "objfiles.h"
39 #include "gdb-stabs.h"
40 #include "gdbthread.h"
41 #include "gdbcore.h"
42 #include "regcache.h"
43
44 #include <signal.h>
45 #include "serial.h"
46
47 extern void _initialize_remote_sds (void);
48
49 /* Declarations of local functions. */
50
51 static int sds_write_bytes (CORE_ADDR, char *, int);
52
53 static int sds_read_bytes (CORE_ADDR, char *, int);
54
55 static void sds_files_info (struct target_ops *ignore);
56
57 static int sds_xfer_memory (CORE_ADDR, char *, int, int, 
58                             struct mem_attrib *, struct target_ops *);
59
60 static void sds_prepare_to_store (void);
61
62 static void sds_fetch_registers (int);
63
64 static void sds_resume (ptid_t, int, enum target_signal);
65
66 static int sds_start_remote (void *);
67
68 static void sds_open (char *, int);
69
70 static void sds_close (int);
71
72 static void sds_store_registers (int);
73
74 static void sds_mourn (void);
75
76 static void sds_load (char *, int);
77
78 static int getmessage (unsigned char *, int);
79
80 static int putmessage (unsigned char *, int);
81
82 static int sds_send (unsigned char *, int);
83
84 static int readchar (int);
85
86 static ptid_t sds_wait (ptid_t, struct target_waitstatus *);
87
88 static void sds_kill (void);
89
90 static int fromhex (int);
91
92 static void sds_detach (char *, int);
93
94 static void sds_interrupt (int);
95
96 static void sds_interrupt_twice (int);
97
98 static void interrupt_query (void);
99
100 static int read_frame (char *);
101
102 static int sds_insert_breakpoint (CORE_ADDR, char *);
103
104 static int sds_remove_breakpoint (CORE_ADDR, char *);
105
106 static void init_sds_ops (void);
107
108 static void sds_command (char *args, int from_tty);
109
110 /* Define the target operations vector. */
111
112 static struct target_ops sds_ops;
113
114 /* This was 5 seconds, which is a long time to sit and wait.
115    Unless this is going though some terminal server or multiplexer or
116    other form of hairy serial connection, I would think 2 seconds would
117    be plenty.  */
118
119 static int sds_timeout = 2;
120
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.  */
124
125 static struct serial *sds_desc = NULL;
126
127 /* This limit comes from the monitor.  */
128
129 #define PBUFSIZ 250
130
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)
134
135 static int next_msg_id;
136
137 static int just_started;
138
139 static int message_pending;
140 \f
141
142 /* Clean up connection to a remote debugger.  */
143
144 static void
145 sds_close (int quitting)
146 {
147   if (sds_desc)
148     serial_close (sds_desc);
149   sds_desc = NULL;
150 }
151
152 /* Stub for catch_errors.  */
153
154 static int
155 sds_start_remote (void *dummy)
156 {
157   int c;
158   unsigned char buf[200];
159
160   immediate_quit++;             /* Allow user to interrupt it */
161
162   /* Ack any packet which the remote side has already sent.  */
163   serial_write (sds_desc, "{#*\r\n", 5);
164   serial_write (sds_desc, "{#}\r\n", 5);
165
166   while ((c = readchar (1)) >= 0)
167     printf_unfiltered ("%c", c);
168   printf_unfiltered ("\n");
169
170   next_msg_id = 251;
171
172   buf[0] = 26;
173   sds_send (buf, 1);
174
175   buf[0] = 0;
176   sds_send (buf, 1);
177
178   immediate_quit--;
179
180   start_remote ();              /* Initialize gdb process mechanisms */
181   return 1;
182 }
183
184 /* Open a connection to a remote debugger.
185    NAME is the filename used for communication.  */
186
187 static void
188 sds_open (char *name, int from_tty)
189 {
190   if (name == 0)
191     error ("To open a remote debug connection, you need to specify what serial\n\
192 device is attached to the remote system (e.g. /dev/ttya).");
193
194   target_preopen (from_tty);
195
196   unpush_target (&sds_ops);
197
198   sds_desc = serial_open (name);
199   if (!sds_desc)
200     perror_with_name (name);
201
202   if (baud_rate != -1)
203     {
204       if (serial_setbaudrate (sds_desc, baud_rate))
205         {
206           serial_close (sds_desc);
207           perror_with_name (name);
208         }
209     }
210
211
212   serial_raw (sds_desc);
213
214   /* If there is something sitting in the buffer we might take it as a
215      response to a command, which would be bad.  */
216   serial_flush_input (sds_desc);
217
218   if (from_tty)
219     {
220       puts_filtered ("Remote debugging using ");
221       puts_filtered (name);
222       puts_filtered ("\n");
223     }
224   push_target (&sds_ops);       /* Switch to using remote target now */
225
226   just_started = 1;
227
228   /* Start the remote connection; if error (0), discard this target.
229      In particular, if the user quits, be sure to discard it (we'd be
230      in an inconsistent state otherwise).  */
231   if (!catch_errors (sds_start_remote, NULL,
232                      "Couldn't establish connection to remote target\n",
233                      RETURN_MASK_ALL))
234     pop_target ();
235 }
236
237 /* This takes a program previously attached to and detaches it.  After
238    this is done, GDB can be used to debug some other program.  We
239    better not have left any breakpoints in the target program or it'll
240    die when it hits one.  */
241
242 static void
243 sds_detach (char *args, int from_tty)
244 {
245   char buf[PBUFSIZ];
246
247   if (args)
248     error ("Argument given to \"detach\" when remotely debugging.");
249
250 #if 0
251   /* Tell the remote target to detach.  */
252   strcpy (buf, "D");
253   sds_send (buf, 1);
254 #endif
255
256   pop_target ();
257   if (from_tty)
258     puts_filtered ("Ending remote debugging.\n");
259 }
260
261 /* Convert hex digit A to a number.  */
262
263 static int
264 fromhex (int a)
265 {
266   if (a >= '0' && a <= '9')
267     return a - '0';
268   else if (a >= 'a' && a <= 'f')
269     return a - 'a' + 10;
270   else
271     error ("Reply contains invalid hex digit %d", a);
272 }
273
274 static int
275 tob64 (unsigned char *inbuf, char *outbuf, int len)
276 {
277   int i, sum;
278   char *p;
279
280   if (len % 3 != 0)
281     error ("bad length");
282
283   p = outbuf;
284   for (i = 0; i < len; i += 3)
285     {
286       /* Collect the next three bytes into a number.  */
287       sum = ((long) *inbuf++) << 16;
288       sum |= ((long) *inbuf++) << 8;
289       sum |= ((long) *inbuf++);
290
291       /* Spit out 4 6-bit encodings.  */
292       *p++ = ((sum >> 18) & 0x3f) + '0';
293       *p++ = ((sum >> 12) & 0x3f) + '0';
294       *p++ = ((sum >> 6) & 0x3f) + '0';
295       *p++ = (sum & 0x3f) + '0';
296     }
297   return (p - outbuf);
298 }
299
300 static int
301 fromb64 (char *inbuf, char *outbuf, int len)
302 {
303   int i, sum;
304
305   if (len % 4 != 0)
306     error ("bad length");
307
308   for (i = 0; i < len; i += 4)
309     {
310       /* Collect 4 6-bit digits.  */
311       sum = (*inbuf++ - '0') << 18;
312       sum |= (*inbuf++ - '0') << 12;
313       sum |= (*inbuf++ - '0') << 6;
314       sum |= (*inbuf++ - '0');
315
316       /* Now take the resulting 24-bit number and get three bytes out
317          of it.  */
318       *outbuf++ = (sum >> 16) & 0xff;
319       *outbuf++ = (sum >> 8) & 0xff;
320       *outbuf++ = sum & 0xff;
321     }
322
323   return (len / 4) * 3;
324 }
325 \f
326
327 /* Tell the remote machine to resume.  */
328
329 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
330 int last_sent_step;
331
332 static void
333 sds_resume (ptid_t ptid, int step, enum target_signal siggnal)
334 {
335   unsigned char buf[PBUFSIZ];
336
337   last_sent_signal = siggnal;
338   last_sent_step = step;
339
340   buf[0] = (step ? 21 : 20);
341   buf[1] = 0;                   /* (should be signal?) */
342
343   sds_send (buf, 2);
344 }
345 \f
346 /* Send a message to target to halt it.  Target will respond, and send
347    us a message pending notice.  */
348
349 static void
350 sds_interrupt (int signo)
351 {
352   unsigned char buf[PBUFSIZ];
353
354   /* If this doesn't work, try more severe steps.  */
355   signal (signo, sds_interrupt_twice);
356
357   if (remote_debug)
358     fprintf_unfiltered (gdb_stdlog, "sds_interrupt called\n");
359
360   buf[0] = 25;
361   sds_send (buf, 1);
362 }
363
364 static void (*ofunc) ();
365
366 /* The user typed ^C twice.  */
367
368 static void
369 sds_interrupt_twice (int signo)
370 {
371   signal (signo, ofunc);
372
373   interrupt_query ();
374
375   signal (signo, sds_interrupt);
376 }
377
378 /* Ask the user what to do when an interrupt is received.  */
379
380 static void
381 interrupt_query (void)
382 {
383   target_terminal_ours ();
384
385   if (query ("Interrupted while waiting for the program.\n\
386 Give up (and stop debugging it)? "))
387     {
388       target_mourn_inferior ();
389       deprecated_throw_reason (RETURN_QUIT);
390     }
391
392   target_terminal_inferior ();
393 }
394
395 /* If nonzero, ignore the next kill.  */
396 int kill_kludge;
397
398 /* Wait until the remote machine stops, then return, storing status in
399    STATUS just as `wait' would.  Returns "pid" (though it's not clear
400    what, if anything, that means in the case of this target).  */
401
402 static ptid_t
403 sds_wait (ptid_t ptid, struct target_waitstatus *status)
404 {
405   unsigned char buf[PBUFSIZ];
406   int retlen;
407
408   status->kind = TARGET_WAITKIND_EXITED;
409   status->value.integer = 0;
410
411   ofunc = (void (*)()) signal (SIGINT, sds_interrupt);
412
413   signal (SIGINT, ofunc);
414
415   if (just_started)
416     {
417       just_started = 0;
418       status->kind = TARGET_WAITKIND_STOPPED;
419       return inferior_ptid;
420     }
421
422   while (1)
423     {
424       getmessage (buf, 1);
425
426       if (message_pending)
427         {
428           buf[0] = 26;
429           retlen = sds_send (buf, 1);
430           if (remote_debug)
431             {
432               fprintf_unfiltered (gdb_stdlog, "Signals: %02x%02x %02x %02x\n",
433                                   buf[0], buf[1],
434                                   buf[2], buf[3]);
435             }
436           message_pending = 0;
437           status->kind = TARGET_WAITKIND_STOPPED;
438           status->value.sig = TARGET_SIGNAL_TRAP;
439           goto got_status;
440         }
441     }
442 got_status:
443   return inferior_ptid;
444 }
445
446 static unsigned char sprs[16];
447
448 /* Read the remote registers into the block REGS.  */
449 /* Currently we just read all the registers, so we don't use regno.  */
450
451 static void
452 sds_fetch_registers (int regno)
453 {
454   unsigned char buf[PBUFSIZ];
455   int i, retlen;
456   char *regs = alloca (deprecated_register_bytes ());
457
458   /* Unimplemented registers read as all bits zero.  */
459   memset (regs, 0, deprecated_register_bytes ());
460
461   buf[0] = 18;
462   buf[1] = 1;
463   buf[2] = 0;
464   retlen = sds_send (buf, 3);
465
466   for (i = 0; i < 4 * 6; ++i)
467     regs[i + 4 * 32 + 8 * 32] = buf[i];
468   for (i = 0; i < 4 * 4; ++i)
469     sprs[i] = buf[i + 4 * 7];
470
471   buf[0] = 18;
472   buf[1] = 2;
473   buf[2] = 0;
474   retlen = sds_send (buf, 3);
475
476   for (i = 0; i < retlen; i++)
477     regs[i] = buf[i];
478
479   /* (should warn about reply too short) */
480
481   for (i = 0; i < NUM_REGS; i++)
482     regcache_raw_supply (current_regcache, i,
483                          &regs[DEPRECATED_REGISTER_BYTE (i)]);
484 }
485
486 /* Prepare to store registers.  Since we may send them all, we have to
487    read out the ones we don't want to change first.  */
488
489 static void
490 sds_prepare_to_store (void)
491 {
492   /* Make sure the entire registers array is valid.  */
493   deprecated_read_register_bytes (0, (char *) NULL, deprecated_register_bytes ());
494 }
495
496 /* Store register REGNO, or all registers if REGNO == -1, from the contents
497    of REGISTERS.  FIXME: ignores errors.  */
498
499 static void
500 sds_store_registers (int regno)
501 {
502   unsigned char *p, buf[PBUFSIZ];
503   int i;
504
505   /* Store all the special-purpose registers.  */
506   p = buf;
507   *p++ = 19;
508   *p++ = 1;
509   *p++ = 0;
510   *p++ = 0;
511   for (i = 0; i < 4 * 6; i++)
512     *p++ = deprecated_registers[i + 4 * 32 + 8 * 32];
513   for (i = 0; i < 4 * 1; i++)
514     *p++ = 0;
515   for (i = 0; i < 4 * 4; i++)
516     *p++ = sprs[i];
517
518   sds_send (buf, p - buf);
519
520   /* Store all the general-purpose registers.  */
521   p = buf;
522   *p++ = 19;
523   *p++ = 2;
524   *p++ = 0;
525   *p++ = 0;
526   for (i = 0; i < 4 * 32; i++)
527     *p++ = deprecated_registers[i];
528
529   sds_send (buf, p - buf);
530
531 }
532 \f
533 /* Write memory data directly to the remote machine.  This does not
534    inform the data cache; the data cache uses this.  MEMADDR is the
535    address in the remote memory space.  MYADDR is the address of the
536    buffer in our space.  LEN is the number of bytes.
537
538    Returns number of bytes transferred, or 0 for error.  */
539
540 static int
541 sds_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
542 {
543   int max_buf_size;             /* Max size of packet output buffer */
544   int origlen;
545   unsigned char buf[PBUFSIZ];
546   int todo;
547   int i;
548
549   /* Chop the transfer down if necessary */
550
551   max_buf_size = 150;
552
553   origlen = len;
554   while (len > 0)
555     {
556       todo = min (len, max_buf_size);
557
558       buf[0] = 13;
559       buf[1] = 0;
560       buf[2] = (int) (memaddr >> 24) & 0xff;
561       buf[3] = (int) (memaddr >> 16) & 0xff;
562       buf[4] = (int) (memaddr >> 8) & 0xff;
563       buf[5] = (int) (memaddr) & 0xff;
564       buf[6] = 1;
565       buf[7] = 0;
566
567       for (i = 0; i < todo; i++)
568         buf[i + 8] = myaddr[i];
569
570       sds_send (buf, 8 + todo);
571
572       /* (should look at result) */
573
574       myaddr += todo;
575       memaddr += todo;
576       len -= todo;
577     }
578   return origlen;
579 }
580
581 /* Read memory data directly from the remote machine.  This does not
582    use the data cache; the data cache uses this.  MEMADDR is the
583    address in the remote memory space.  MYADDR is the address of the
584    buffer in our space.  LEN is the number of bytes.
585
586    Returns number of bytes transferred, or 0 for error.  */
587
588 static int
589 sds_read_bytes (CORE_ADDR memaddr, char *myaddr, int len)
590 {
591   int max_buf_size;             /* Max size of packet output buffer */
592   int origlen, retlen;
593   unsigned char buf[PBUFSIZ];
594   int todo;
595   int i;
596
597   /* Chop the transfer down if necessary */
598
599   max_buf_size = 150;
600
601   origlen = len;
602   while (len > 0)
603     {
604       todo = min (len, max_buf_size);
605
606       buf[0] = 12;
607       buf[1] = 0;
608       buf[2] = (int) (memaddr >> 24) & 0xff;
609       buf[3] = (int) (memaddr >> 16) & 0xff;
610       buf[4] = (int) (memaddr >> 8) & 0xff;
611       buf[5] = (int) (memaddr) & 0xff;
612       buf[6] = (int) (todo >> 8) & 0xff;
613       buf[7] = (int) (todo) & 0xff;
614       buf[8] = 1;
615
616       retlen = sds_send (buf, 9);
617
618       if (retlen - 2 != todo)
619         {
620           return 0;
621         }
622
623       /* Reply describes memory byte by byte. */
624
625       for (i = 0; i < todo; i++)
626         myaddr[i] = buf[i + 2];
627
628       myaddr += todo;
629       memaddr += todo;
630       len -= todo;
631     }
632
633   return origlen;
634 }
635 \f
636 /* Read or write LEN bytes from inferior memory at MEMADDR,
637    transferring to or from debugger address MYADDR.  Write to inferior
638    if SHOULD_WRITE is nonzero.  Returns length of data written or
639    read; 0 for error.  TARGET is unused.  */
640
641 static int
642 sds_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int should_write,
643                  struct mem_attrib *attrib, struct target_ops *target)
644 {
645   int res;
646
647   if (should_write)
648     res = sds_write_bytes (memaddr, myaddr, len);
649   else
650     res = sds_read_bytes (memaddr, myaddr, len);
651   
652   return res;
653 }
654 \f
655
656 static void
657 sds_files_info (struct target_ops *ignore)
658 {
659   puts_filtered ("Debugging over a serial connection, using SDS protocol.\n");
660 }
661 \f
662 /* Stuff for dealing with the packets which are part of this protocol.
663    See comment at top of file for details.  */
664
665 /* Read a single character from the remote end, masking it down to 7 bits. */
666
667 static int
668 readchar (int timeout)
669 {
670   int ch;
671
672   ch = serial_readchar (sds_desc, timeout);
673
674   if (remote_debug > 1 && ch >= 0)
675     fprintf_unfiltered (gdb_stdlog, "%c(%x)", ch, ch);
676
677   switch (ch)
678     {
679     case SERIAL_EOF:
680       error ("Remote connection closed");
681     case SERIAL_ERROR:
682       perror_with_name ("Remote communication error");
683     case SERIAL_TIMEOUT:
684       return ch;
685     default:
686       return ch & 0x7f;
687     }
688 }
689
690 /* An SDS-style checksum is a sum of the bytes modulo 253.  (Presumably
691    because 253, 254, and 255 are special flags in the protocol.)  */
692
693 static int
694 compute_checksum (int csum, char *buf, int len)
695 {
696   int i;
697
698   for (i = 0; i < len; ++i)
699     csum += (unsigned char) buf[i];
700
701   csum %= 253;
702   return csum;
703 }
704
705 /* Send the command in BUF to the remote machine, and read the reply
706    into BUF also.  */
707
708 static int
709 sds_send (unsigned char *buf, int len)
710 {
711   putmessage (buf, len);
712
713   return getmessage (buf, 0);
714 }
715
716 /* Send a message to the remote machine.  */
717
718 static int
719 putmessage (unsigned char *buf, int len)
720 {
721   int i, enclen;
722   unsigned char csum = 0;
723   char buf2[PBUFSIZ], buf3[PBUFSIZ];
724   unsigned char header[3];
725   char *p;
726
727   /* Copy the packet into buffer BUF2, encapsulating it
728      and giving it a checksum.  */
729
730   if (len > 170)                /* Prosanity check */
731     internal_error (__FILE__, __LINE__, _("failed internal consistency check"));
732
733   if (remote_debug)
734     {
735       fprintf_unfiltered (gdb_stdlog, "Message to send: \"");
736       for (i = 0; i < len; ++i)
737         fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
738       fprintf_unfiltered (gdb_stdlog, "\"\n");
739     }
740
741   p = buf2;
742   *p++ = '$';
743
744   if (len % 3 != 0)
745     {
746       buf[len] = '\0';
747       buf[len + 1] = '\0';
748     }
749
750   header[1] = next_msg_id;
751
752   header[2] = len;
753
754   csum = compute_checksum (csum, buf, len);
755   csum = compute_checksum (csum, header + 1, 2);
756
757   header[0] = csum;
758
759   tob64 (header, p, 3);
760   p += 4;
761   enclen = tob64 (buf, buf3, ((len + 2) / 3) * 3);
762
763   for (i = 0; i < enclen; ++i)
764     *p++ = buf3[i];
765   *p++ = '\r';
766   *p++ = '\n';
767
768   next_msg_id = (next_msg_id + 3) % 245;
769
770   /* Send it over and over until we get a positive ack.  */
771
772   while (1)
773     {
774       if (remote_debug)
775         {
776           *p = '\0';
777           fprintf_unfiltered (gdb_stdlog, "Sending encoded: \"%s\"", buf2);
778           fprintf_unfiltered (gdb_stdlog,
779                               "  (Checksum %d, id %d, length %d)\n",
780                               header[0], header[1], header[2]);
781           gdb_flush (gdb_stdlog);
782         }
783       if (serial_write (sds_desc, buf2, p - buf2))
784         perror_with_name ("putmessage: write failed");
785
786       return 1;
787     }
788 }
789
790 /* Come here after finding the start of the frame.  Collect the rest
791    into BUF.  Returns 0 on any error, 1 on success.  */
792
793 static int
794 read_frame (char *buf)
795 {
796   char *bp;
797   int c;
798
799   bp = buf;
800
801   while (1)
802     {
803       c = readchar (sds_timeout);
804
805       switch (c)
806         {
807         case SERIAL_TIMEOUT:
808           if (remote_debug)
809             fputs_filtered ("Timeout in mid-message, retrying\n", gdb_stdlog);
810           return 0;
811         case '$':
812           if (remote_debug)
813             fputs_filtered ("Saw new packet start in middle of old one\n",
814                             gdb_stdlog);
815           return 0;             /* Start a new packet, count retries */
816         case '\r':
817           break;
818
819         case '\n':
820           {
821             *bp = '\000';
822             if (remote_debug)
823               fprintf_unfiltered (gdb_stdlog, "Received encoded: \"%s\"\n",
824                                   buf);
825             return 1;
826           }
827
828         default:
829           if (bp < buf + PBUFSIZ - 1)
830             {
831               *bp++ = c;
832               continue;
833             }
834
835           *bp = '\0';
836           puts_filtered ("Message too long: ");
837           puts_filtered (buf);
838           puts_filtered ("\n");
839
840           return 0;
841         }
842     }
843 }
844
845 /* Read a packet from the remote machine, with error checking,
846    and store it in BUF.  BUF is expected to be of size PBUFSIZ.
847    If FOREVER, wait forever rather than timing out; this is used
848    while the target is executing user code.  */
849
850 static int
851 getmessage (unsigned char *buf, int forever)
852 {
853   int c, c2, c3;
854   int tries;
855   int timeout;
856   int val, i, len, csum;
857   unsigned char header[3];
858   unsigned char inbuf[500];
859
860   strcpy (buf, "timeout");
861
862   if (forever)
863     {
864       timeout = watchdog > 0 ? watchdog : -1;
865     }
866
867   else
868     timeout = sds_timeout;
869
870 #define MAX_TRIES 3
871
872   for (tries = 1; tries <= MAX_TRIES; tries++)
873     {
874       /* This can loop forever if the remote side sends us characters
875          continuously, but if it pauses, we'll get a zero from readchar
876          because of timeout.  Then we'll count that as a retry.  */
877
878       /* Note that we will only wait forever prior to the start of a packet.
879          After that, we expect characters to arrive at a brisk pace.  They
880          should show up within sds_timeout intervals.  */
881
882       do
883         {
884           c = readchar (timeout);
885
886           if (c == SERIAL_TIMEOUT)
887             {
888               if (forever)      /* Watchdog went off.  Kill the target. */
889                 {
890                   target_mourn_inferior ();
891                   error ("Watchdog has expired.  Target detached.\n");
892                 }
893               if (remote_debug)
894                 fputs_filtered ("Timed out.\n", gdb_stdlog);
895               goto retry;
896             }
897         }
898       while (c != '$' && c != '{');
899
900       /* We might have seen a "trigraph", a sequence of three characters
901          that indicate various sorts of communication state.  */
902
903       if (c == '{')
904         {
905           /* Read the other two chars of the trigraph. */
906           c2 = readchar (timeout);
907           c3 = readchar (timeout);
908           if (remote_debug)
909             fprintf_unfiltered (gdb_stdlog, "Trigraph %c%c%c received\n",
910                                 c, c2, c3);
911           if (c3 == '+')
912             {
913               message_pending = 1;
914               return 0;         /*???? */
915             }
916           continue;
917         }
918
919       val = read_frame (inbuf);
920
921       if (val == 1)
922         {
923           fromb64 (inbuf, header, 4);
924           /* (should check out other bits) */
925           fromb64 (inbuf + 4, buf, strlen (inbuf) - 4);
926
927           len = header[2];
928
929           csum = 0;
930           csum = compute_checksum (csum, buf, len);
931           csum = compute_checksum (csum, header + 1, 2);
932
933           if (csum != header[0])
934             fprintf_unfiltered (gdb_stderr,
935                             "Checksum mismatch: computed %d, received %d\n",
936                                 csum, header[0]);
937
938           if (header[2] == 0xff)
939             fprintf_unfiltered (gdb_stderr, "Requesting resend...\n");
940
941           if (remote_debug)
942             {
943               fprintf_unfiltered (gdb_stdlog,
944                                 "... (Got checksum %d, id %d, length %d)\n",
945                                   header[0], header[1], header[2]);
946               fprintf_unfiltered (gdb_stdlog, "Message received: \"");
947               for (i = 0; i < len; ++i)
948                 {
949                   fprintf_unfiltered (gdb_stdlog, "%02x", (unsigned char) buf[i]);
950                 }
951               fprintf_unfiltered (gdb_stdlog, "\"\n");
952             }
953
954           /* no ack required? */
955           return len;
956         }
957
958       /* Try the whole thing again.  */
959     retry:
960       /* need to do something here */
961       ;
962     }
963
964   /* We have tried hard enough, and just can't receive the packet.  Give up. */
965
966   printf_unfiltered ("Ignoring packet error, continuing...\n");
967   return 0;
968 }
969 \f
970 static void
971 sds_kill (void)
972 {
973   /* Don't try to do anything to the target.  */
974 }
975
976 static void
977 sds_mourn (void)
978 {
979   unpush_target (&sds_ops);
980   generic_mourn_inferior ();
981 }
982
983 static void
984 sds_create_inferior (char *exec_file, char *args, char **env, int from_tty)
985 {
986   inferior_ptid = pid_to_ptid (42000);
987
988   /* Clean up from the last time we were running.  */
989   clear_proceed_status ();
990
991   /* Let the remote process run.  */
992   proceed (bfd_get_start_address (exec_bfd), TARGET_SIGNAL_0, 0);
993 }
994
995 static void
996 sds_load (char *filename, int from_tty)
997 {
998   generic_load (filename, from_tty);
999
1000   inferior_ptid = null_ptid;
1001 }
1002 \f
1003 /* The SDS monitor has commands for breakpoint insertion, although it
1004    it doesn't actually manage the breakpoints, it just returns the
1005    replaced instruction back to the debugger.  */
1006
1007 static int
1008 sds_insert_breakpoint (CORE_ADDR addr, char *contents_cache)
1009 {
1010   int i, retlen;
1011   unsigned char *p, buf[PBUFSIZ];
1012
1013   p = buf;
1014   *p++ = 16;
1015   *p++ = 0;
1016   *p++ = (int) (addr >> 24) & 0xff;
1017   *p++ = (int) (addr >> 16) & 0xff;
1018   *p++ = (int) (addr >> 8) & 0xff;
1019   *p++ = (int) (addr) & 0xff;
1020
1021   retlen = sds_send (buf, p - buf);
1022
1023   for (i = 0; i < 4; ++i)
1024     contents_cache[i] = buf[i + 2];
1025
1026   return 0;
1027 }
1028
1029 static int
1030 sds_remove_breakpoint (CORE_ADDR addr, char *contents_cache)
1031 {
1032   int i, retlen;
1033   unsigned char *p, buf[PBUFSIZ];
1034
1035   p = buf;
1036   *p++ = 17;
1037   *p++ = 0;
1038   *p++ = (int) (addr >> 24) & 0xff;
1039   *p++ = (int) (addr >> 16) & 0xff;
1040   *p++ = (int) (addr >> 8) & 0xff;
1041   *p++ = (int) (addr) & 0xff;
1042   for (i = 0; i < 4; ++i)
1043     *p++ = contents_cache[i];
1044
1045   retlen = sds_send (buf, p - buf);
1046
1047   return 0;
1048 }
1049 \f
1050 static void
1051 init_sds_ops (void)
1052 {
1053   sds_ops.to_shortname = "sds";
1054   sds_ops.to_longname = "Remote serial target with SDS protocol";
1055   sds_ops.to_doc = "Use a remote computer via a serial line; using the SDS protocol.\n\
1056 Specify the serial device it is connected to (e.g. /dev/ttya).";
1057   sds_ops.to_open = sds_open;
1058   sds_ops.to_close = sds_close;
1059   sds_ops.to_detach = sds_detach;
1060   sds_ops.to_resume = sds_resume;
1061   sds_ops.to_wait = sds_wait;
1062   sds_ops.to_fetch_registers = sds_fetch_registers;
1063   sds_ops.to_store_registers = sds_store_registers;
1064   sds_ops.to_prepare_to_store = sds_prepare_to_store;
1065   sds_ops.deprecated_xfer_memory = sds_xfer_memory;
1066   sds_ops.to_files_info = sds_files_info;
1067   sds_ops.to_insert_breakpoint = sds_insert_breakpoint;
1068   sds_ops.to_remove_breakpoint = sds_remove_breakpoint;
1069   sds_ops.to_kill = sds_kill;
1070   sds_ops.to_load = sds_load;
1071   sds_ops.to_create_inferior = sds_create_inferior;
1072   sds_ops.to_mourn_inferior = sds_mourn;
1073   sds_ops.to_stratum = process_stratum;
1074   sds_ops.to_has_all_memory = 1;
1075   sds_ops.to_has_memory = 1;
1076   sds_ops.to_has_stack = 1;
1077   sds_ops.to_has_registers = 1;
1078   sds_ops.to_has_execution = 1;
1079   sds_ops.to_magic = OPS_MAGIC;
1080 }
1081
1082 /* Put a command string, in args, out to the monitor and display the
1083    reply message.  */
1084
1085 static void
1086 sds_command (char *args, int from_tty)
1087 {
1088   char *p;
1089   int i, len, retlen;
1090   unsigned char buf[1000];
1091
1092   /* Convert hexadecimal chars into a byte buffer.  */
1093   p = args;
1094   len = 0;
1095   while (*p != '\0')
1096     {
1097       buf[len++] = fromhex (p[0]) * 16 + fromhex (p[1]);
1098       if (p[1] == '\0')
1099         break;
1100       p += 2;
1101     }
1102
1103   retlen = sds_send (buf, len);
1104
1105   printf_filtered ("Reply is ");
1106   for (i = 0; i < retlen; ++i)
1107     {
1108       printf_filtered ("%02x", buf[i]);
1109     }
1110   printf_filtered ("\n");
1111 }
1112
1113 void
1114 _initialize_remote_sds (void)
1115 {
1116   init_sds_ops ();
1117   add_target (&sds_ops);
1118
1119   deprecated_add_show_from_set
1120     (add_set_cmd ("sdstimeout", no_class,
1121                   var_integer, (char *) &sds_timeout,
1122                   "Set timeout value for sds read.\n", &setlist),
1123      &showlist);
1124
1125   add_com ("sds", class_obscure, sds_command,
1126            "Send a command to the SDS monitor.");
1127 }