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