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