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