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