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