collect some remote things into remote-utils
[binutils-gdb.git] / gdb / remote.c
1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993 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., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20 /* Remote communication protocol.
21
22 A debug packet whose contents are <data>
23 is encapsulated for transmission in the form:
24
25 $ <data> # CSUM1 CSUM2
26
27 <data> must be ASCII alphanumeric and cannot include characters
28 '$' or '#'
29
30 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
31 checksum of <data>, the most significant nibble is sent first.
32 the hex digits 0-9,a-f are used.
33
34 Receiver responds with:
35
36 + - if CSUM is correct and ready for next packet
37 - - if CSUM is incorrect
38
39 <data> is as follows:
40 All values are encoded in ascii hex digits.
41
42 Request Packet
43
44 read registers g
45 reply XX....X Each byte of register data
46 is described by two hex digits.
47 Registers are in the internal order
48 for GDB, and the bytes in a register
49 are in the same order the machine uses.
50 or ENN for an error.
51
52 write regs GXX..XX Each byte of register data
53 is described by two hex digits.
54 reply OK for success
55 ENN for an error
56
57 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
58 reply XX..XX XX..XX is mem contents
59 Can be fewer bytes than requested
60 if able to read only part of the data.
61 or ENN NN is errno
62
63 write mem MAA..AA,LLLL:XX..XX
64 AA..AA is address,
65 LLLL is number of bytes,
66 XX..XX is data
67 reply OK for success
68 ENN for an error (this includes the case
69 where only part of the data was
70 written).
71
72 cont cAA..AA AA..AA is address to resume
73 If AA..AA is omitted,
74 resume at same address.
75
76 step sAA..AA AA..AA is address to resume
77 If AA..AA is omitted,
78 resume at same address.
79
80 last signal ? Reply the current reason for stopping.
81 This is the same reply as is generated
82 for step or cont : SAA where AA is the
83 signal number.
84
85 There is no immediate reply to step or cont.
86 The reply comes when the machine stops.
87 It is SAA AA is the "signal number"
88
89 or... TAAn...:r...;n:r...;n...:r...;
90 AA = signal number
91 n... = register number
92 r... = register contents
93 or... WAA The process extited, and AA is
94 the exit status. This is only
95 applicable for certains sorts of
96 targets.
97 or... NAATT;DD;BB Relocate the object file.
98 AA = signal number
99 TT = text address
100 DD = data address
101 BB = bss address
102 This is used by the NLM stub,
103 which is why it only has three
104 addresses rather than one per
105 section: the NLM stub always
106 sees only three sections, even
107 though gdb may see more.
108
109 kill request k
110
111 toggle debug d toggle debug flag (see 386 & 68k stubs)
112 reset r reset -- see sparc stub.
113 reserved <other> On other requests, the stub should
114 ignore the request and send an empty
115 response ($#<checksum>). This way
116 we can extend the protocol and GDB
117 can tell whether the stub it is
118 talking to uses the old or the new.
119 */
120
121 #include "defs.h"
122 #include <string.h>
123 #include <fcntl.h>
124 #include "frame.h"
125 #include "inferior.h"
126 #include "bfd.h"
127 #include "symfile.h"
128 #include "target.h"
129 #include "wait.h"
130 #include "terminal.h"
131 #include "gdbcmd.h"
132 #include "objfiles.h"
133 #include "gdb-stabs.h"
134
135 #include "dcache.h"
136
137 #if !defined(DONT_USE_REMOTE)
138 #ifdef USG
139 #include <sys/types.h>
140 #endif
141
142 #include <signal.h>
143 #include "serial.h"
144
145 /* Prototypes for local functions */
146
147 static int
148 remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
149
150 static int
151 remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
152
153 static void
154 remote_files_info PARAMS ((struct target_ops *ignore));
155
156 static int
157 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
158 int should_write, struct target_ops *target));
159
160 static void
161 remote_prepare_to_store PARAMS ((void));
162
163 static void
164 remote_fetch_registers PARAMS ((int regno));
165
166 static void
167 remote_resume PARAMS ((int pid, int step, int siggnal));
168
169 static int
170 remote_start_remote PARAMS ((char *dummy));
171
172 static void
173 remote_open PARAMS ((char *name, int from_tty));
174
175 static void
176 remote_close PARAMS ((int quitting));
177
178 static void
179 remote_store_registers PARAMS ((int regno));
180
181 static void
182 getpkt PARAMS ((char *buf, int forever));
183
184 static void
185 putpkt PARAMS ((char *buf));
186
187 static void
188 remote_send PARAMS ((char *buf));
189
190 static int
191 readchar PARAMS ((void));
192
193 static int
194 remote_wait PARAMS ((WAITTYPE *status));
195
196 static int
197 tohex PARAMS ((int nib));
198
199 static int
200 fromhex PARAMS ((int a));
201
202 static void
203 remote_detach PARAMS ((char *args, int from_tty));
204
205 static void
206 remote_interrupt PARAMS ((int signo));
207
208 static void
209 remote_interrupt_twice PARAMS ((int signo));
210
211 extern struct target_ops remote_ops; /* Forward decl */
212
213 /* This was 5 seconds, which is a long time to sit and wait.
214 Unless this is going though some terminal server or multiplexer or
215 other form of hairy serial connection, I would think 2 seconds would
216 be plenty. */
217 static int timeout = 2;
218
219 #if 0
220 int icache;
221 #endif
222
223 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
224 remote_open knows that we don't have a file open when the program
225 starts. */
226 serial_t remote_desc = NULL;
227
228 #define PBUFSIZ 1024
229
230 /* Maximum number of bytes to read/write at once. The value here
231 is chosen to fill up a packet (the headers account for the 32). */
232 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
233
234 /* Round up PBUFSIZ to hold all the registers, at least. */
235 #if REGISTER_BYTES > MAXBUFBYTES
236 #undef PBUFSIZ
237 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
238 #endif
239 \f
240 /* Clean up connection to a remote debugger. */
241
242 /* ARGSUSED */
243 static void
244 remote_close (quitting)
245 int quitting;
246 {
247 if (remote_desc)
248 SERIAL_CLOSE (remote_desc);
249 remote_desc = NULL;
250 }
251
252 /* Stub for catch_errors. */
253
254 static int
255 remote_start_remote (dummy)
256 char *dummy;
257 {
258 immediate_quit = 1; /* Allow user to interrupt it */
259
260 /* Ack any packet which the remote side has already sent. */
261 /* I'm not sure this \r is needed; we don't use it any other time we
262 send an ack. */
263 SERIAL_WRITE (remote_desc, "+\r", 2);
264 putpkt ("?"); /* initiate a query from remote machine */
265 immediate_quit = 0;
266
267 start_remote (); /* Initialize gdb process mechanisms */
268 return 1;
269 }
270
271 /* Open a connection to a remote debugger.
272 NAME is the filename used for communication. */
273
274 static DCACHE *remote_dcache;
275
276 static void
277 remote_open (name, from_tty)
278 char *name;
279 int from_tty;
280 {
281 if (name == 0)
282 error (
283 "To open a remote debug connection, you need to specify what serial\n\
284 device is attached to the remote system (e.g. /dev/ttya).");
285
286 target_preopen (from_tty);
287
288 unpush_target (&remote_ops);
289
290 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
291
292 remote_desc = SERIAL_OPEN (name);
293 if (!remote_desc)
294 perror_with_name (name);
295
296 if (baud_rate)
297 {
298 int rate;
299
300 if (sscanf (baud_rate, "%d", &rate) == 1)
301 if (SERIAL_SETBAUDRATE (remote_desc, rate))
302 {
303 SERIAL_CLOSE (remote_desc);
304 perror_with_name (name);
305 }
306 }
307
308 SERIAL_RAW (remote_desc);
309
310 if (from_tty)
311 {
312 puts_filtered ("Remote debugging using ");
313 puts_filtered (name);
314 puts_filtered ("\n");
315 }
316 push_target (&remote_ops); /* Switch to using remote target now */
317
318 /* Start the remote connection; if error (0), discard this target.
319 In particular, if the user quits, be sure to discard it
320 (we'd be in an inconsistent state otherwise). */
321 if (!catch_errors (remote_start_remote, (char *)0,
322 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
323 pop_target();
324 }
325
326 /* remote_detach()
327 takes a program previously attached to and detaches it.
328 We better not have left any breakpoints
329 in the program or it'll die when it hits one.
330 Close the open connection to the remote debugger.
331 Use this when you want to detach and do something else
332 with your gdb. */
333
334 static void
335 remote_detach (args, from_tty)
336 char *args;
337 int from_tty;
338 {
339 if (args)
340 error ("Argument given to \"detach\" when remotely debugging.");
341
342 pop_target ();
343 if (from_tty)
344 puts_filtered ("Ending remote debugging.\n");
345 }
346
347 /* Convert hex digit A to a number. */
348
349 static int
350 fromhex (a)
351 int a;
352 {
353 if (a >= '0' && a <= '9')
354 return a - '0';
355 else if (a >= 'a' && a <= 'f')
356 return a - 'a' + 10;
357 else
358 error ("Reply contains invalid hex digit");
359 return -1;
360 }
361
362 /* Convert number NIB to a hex digit. */
363
364 static int
365 tohex (nib)
366 int nib;
367 {
368 if (nib < 10)
369 return '0'+nib;
370 else
371 return 'a'+nib-10;
372 }
373 \f
374 /* Tell the remote machine to resume. */
375
376 static void
377 remote_resume (pid, step, siggnal)
378 int pid, step, siggnal;
379 {
380 char buf[PBUFSIZ];
381
382 if (siggnal)
383 {
384 char *name;
385 target_terminal_ours_for_output ();
386 printf_filtered ("Can't send signals to a remote system. ");
387 name = strsigno (siggnal);
388 if (name)
389 printf_filtered (name);
390 else
391 printf_filtered ("Signal %d", siggnal);
392 printf_filtered (" not sent.\n");
393 target_terminal_inferior ();
394 }
395
396 dcache_flush (remote_dcache);
397
398 strcpy (buf, step ? "s": "c");
399
400 putpkt (buf);
401 }
402 \f
403 /* Send ^C to target to halt it. Target will respond, and send us a
404 packet. */
405
406 static void
407 remote_interrupt (signo)
408 int signo;
409 {
410 /* If this doesn't work, try more severe steps. */
411 signal (signo, remote_interrupt_twice);
412
413 if (remote_debug)
414 printf ("remote_interrupt called\n");
415
416 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
417 }
418
419 static void (*ofunc)();
420
421 /* The user typed ^C twice. */
422 static void
423 remote_interrupt_twice (signo)
424 int signo;
425 {
426 signal (signo, ofunc);
427
428 target_terminal_ours ();
429 if (query ("Interrupted while waiting for the program.\n\
430 Give up (and stop debugging it)? "))
431 {
432 target_mourn_inferior ();
433 return_to_top_level (RETURN_QUIT);
434 }
435 else
436 {
437 signal (signo, remote_interrupt);
438 target_terminal_inferior ();
439 }
440 }
441
442 /* Wait until the remote machine stops, then return,
443 storing status in STATUS just as `wait' would.
444 Returns "pid" (though it's not clear what, if anything, that
445 means in the case of this target). */
446
447 static int
448 remote_wait (status)
449 WAITTYPE *status;
450 {
451 unsigned char buf[PBUFSIZ];
452
453 WSETEXIT ((*status), 0);
454
455 while (1)
456 {
457 unsigned char *p;
458
459 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
460 getpkt ((char *) buf, 1);
461 signal (SIGINT, ofunc);
462
463 if (buf[0] == 'E')
464 warning ("Remote failure reply: %s", buf);
465 else if (buf[0] == 'T')
466 {
467 int i;
468 long regno;
469 char regs[MAX_REGISTER_RAW_SIZE];
470
471 /* Expedited reply, containing Signal, {regno, reg} repeat */
472 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
473 ss = signal number
474 n... = register number
475 r... = register contents
476 */
477
478 p = &buf[3]; /* after Txx */
479
480 while (*p)
481 {
482 unsigned char *p1;
483
484 regno = strtol (p, &p1, 16); /* Read the register number */
485
486 if (p1 == p)
487 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
488 p1, buf);
489
490 p = p1;
491
492 if (*p++ != ':')
493 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
494 p, buf);
495
496 if (regno >= NUM_REGS)
497 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
498 regno, p, buf);
499
500 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
501 {
502 if (p[0] == 0 || p[1] == 0)
503 warning ("Remote reply is too short: %s", buf);
504 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
505 p += 2;
506 }
507
508 if (*p++ != ';')
509 warning ("Remote register badly formatted: %s", buf);
510
511 supply_register (regno, regs);
512 }
513 break;
514 }
515 else if (buf[0] == 'N')
516 {
517 unsigned char *p1;
518 bfd_vma text_addr, data_addr, bss_addr;
519
520 /* Relocate object file. Format is NAATT;DD;BB where AA is
521 the signal number, TT is the new text address, DD is the
522 new data address, and BB is the new bss address. This is
523 used by the NLM stub; gdb may see more sections. */
524 p = &buf[3];
525 text_addr = strtol (p, &p1, 16);
526 if (p1 == p || *p1 != ';')
527 warning ("Malformed relocation packet: Packet '%s'", buf);
528 p = p1 + 1;
529 data_addr = strtol (p, &p1, 16);
530 if (p1 == p || *p1 != ';')
531 warning ("Malformed relocation packet: Packet '%s'", buf);
532 p = p1 + 1;
533 bss_addr = strtol (p, &p1, 16);
534 if (p1 == p)
535 warning ("Malformed relocation packet: Packet '%s'", buf);
536
537 if (symfile_objfile != NULL)
538 {
539 struct section_offsets *offs;
540
541 /* FIXME: Why don't the various symfile_offsets routines
542 in the sym_fns vectors set this? */
543 if (symfile_objfile->num_sections == 0)
544 symfile_objfile->num_sections = SECT_OFF_MAX;
545
546 offs = ((struct section_offsets *)
547 alloca (sizeof (struct section_offsets)
548 + (symfile_objfile->num_sections
549 * sizeof (offs->offsets))));
550 memcpy (offs, symfile_objfile->section_offsets,
551 (sizeof (struct section_offsets)
552 + (symfile_objfile->num_sections
553 * sizeof (offs->offsets))));
554 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
555 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
556 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
557
558 objfile_relocate (symfile_objfile, offs);
559 }
560 break;
561 }
562 else if (buf[0] == 'W')
563 {
564 /* The remote process exited. */
565 WSETEXIT (*status, (fromhex (buf[1]) << 4) + fromhex (buf[2]));
566 return 0;
567 }
568 else if (buf[0] == 'S')
569 break;
570 else
571 warning ("Invalid remote reply: %s", buf);
572 }
573
574 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
575
576 return 0;
577 }
578
579 /* Read the remote registers into the block REGS. */
580 /* Currently we just read all the registers, so we don't use regno. */
581 /* ARGSUSED */
582 static void
583 remote_fetch_registers (regno)
584 int regno;
585 {
586 char buf[PBUFSIZ];
587 int i;
588 char *p;
589 char regs[REGISTER_BYTES];
590
591 sprintf (buf, "g");
592 remote_send (buf);
593
594 /* Reply describes registers byte by byte, each byte encoded as two
595 hex characters. Suck them all up, then supply them to the
596 register cacheing/storage mechanism. */
597
598 p = buf;
599 for (i = 0; i < REGISTER_BYTES; i++)
600 {
601 if (p[0] == 0 || p[1] == 0)
602 error ("Remote reply is too short: %s", buf);
603 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
604 p += 2;
605 }
606 for (i = 0; i < NUM_REGS; i++)
607 supply_register (i, &regs[REGISTER_BYTE(i)]);
608 }
609
610 /* Prepare to store registers. Since we send them all, we have to
611 read out the ones we don't want to change first. */
612
613 static void
614 remote_prepare_to_store ()
615 {
616 /* Make sure the entire registers array is valid. */
617 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
618 }
619
620 /* Store the remote registers from the contents of the block REGISTERS.
621 FIXME, eventually just store one register if that's all that is needed. */
622
623 /* ARGSUSED */
624 static void
625 remote_store_registers (regno)
626 int regno;
627 {
628 char buf[PBUFSIZ];
629 int i;
630 char *p;
631
632 buf[0] = 'G';
633
634 /* Command describes registers byte by byte,
635 each byte encoded as two hex characters. */
636
637 p = buf + 1;
638 for (i = 0; i < REGISTER_BYTES; i++)
639 {
640 *p++ = tohex ((registers[i] >> 4) & 0xf);
641 *p++ = tohex (registers[i] & 0xf);
642 }
643 *p = '\0';
644
645 remote_send (buf);
646 }
647
648 #if 0
649
650 /* Use of the data cache is disabled because it loses for looking at
651 and changing hardware I/O ports and the like. Accepting `volatile'
652 would perhaps be one way to fix it, but a better way which would
653 win for more cases would be to use the executable file for the text
654 segment, like the `icache' code below but done cleanly (in some
655 target-independent place, perhaps in target_xfer_memory, perhaps
656 based on assigning each target a speed or perhaps by some simpler
657 mechanism). */
658
659 /* Read a word from remote address ADDR and return it.
660 This goes through the data cache. */
661
662 static int
663 remote_fetch_word (addr)
664 CORE_ADDR addr;
665 {
666 #if 0
667 if (icache)
668 {
669 extern CORE_ADDR text_start, text_end;
670
671 if (addr >= text_start && addr < text_end)
672 {
673 int buffer;
674 xfer_core_file (addr, &buffer, sizeof (int));
675 return buffer;
676 }
677 }
678 #endif
679 return dcache_fetch (remote_dcache, addr);
680 }
681
682 /* Write a word WORD into remote address ADDR.
683 This goes through the data cache. */
684
685 static void
686 remote_store_word (addr, word)
687 CORE_ADDR addr;
688 int word;
689 {
690 dcache_poke (remote_dcache, addr, word);
691 }
692 #endif /* 0 */
693 \f
694 /* Write memory data directly to the remote machine.
695 This does not inform the data cache; the data cache uses this.
696 MEMADDR is the address in the remote memory space.
697 MYADDR is the address of the buffer in our space.
698 LEN is the number of bytes.
699
700 Returns number of bytes transferred, or 0 for error. */
701
702 static int
703 remote_write_bytes (memaddr, myaddr, len)
704 CORE_ADDR memaddr;
705 unsigned char *myaddr;
706 int len;
707 {
708 char buf[PBUFSIZ];
709 int i;
710 char *p;
711
712 if (len > PBUFSIZ / 2 - 20)
713 abort ();
714
715 sprintf (buf, "M%x,%x:", memaddr, len);
716
717 /* We send target system values byte by byte, in increasing byte addresses,
718 each byte encoded as two hex characters. */
719
720 p = buf + strlen (buf);
721 for (i = 0; i < len; i++)
722 {
723 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
724 *p++ = tohex (myaddr[i] & 0xf);
725 }
726 *p = '\0';
727
728 putpkt (buf);
729 getpkt (buf, 0);
730
731 if (buf[0] == 'E')
732 {
733 /* There is no correspondance between what the remote protocol uses
734 for errors and errno codes. We would like a cleaner way of
735 representing errors (big enough to include errno codes, bfd_error
736 codes, and others). But for now just return EIO. */
737 errno = EIO;
738 return 0;
739 }
740 return len;
741 }
742
743 /* Read memory data directly from the remote machine.
744 This does not use the data cache; the data cache uses this.
745 MEMADDR is the address in the remote memory space.
746 MYADDR is the address of the buffer in our space.
747 LEN is the number of bytes.
748
749 Returns number of bytes transferred, or 0 for error. */
750
751 static int
752 remote_read_bytes (memaddr, myaddr, len)
753 CORE_ADDR memaddr;
754 unsigned char *myaddr;
755 int len;
756 {
757 char buf[PBUFSIZ];
758 int i;
759 char *p;
760
761 if (len > PBUFSIZ / 2 - 1)
762 abort ();
763
764 sprintf (buf, "m%x,%x", memaddr, len);
765 putpkt (buf);
766 getpkt (buf, 0);
767
768 if (buf[0] == 'E')
769 {
770 /* There is no correspondance between what the remote protocol uses
771 for errors and errno codes. We would like a cleaner way of
772 representing errors (big enough to include errno codes, bfd_error
773 codes, and others). But for now just return EIO. */
774 errno = EIO;
775 return 0;
776 }
777
778 /* Reply describes memory byte by byte,
779 each byte encoded as two hex characters. */
780
781 p = buf;
782 for (i = 0; i < len; i++)
783 {
784 if (p[0] == 0 || p[1] == 0)
785 /* Reply is short. This means that we were able to read only part
786 of what we wanted to. */
787 break;
788 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
789 p += 2;
790 }
791 return i;
792 }
793 \f
794 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
795 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
796 nonzero. Returns length of data written or read; 0 for error. */
797
798 /* ARGSUSED */
799 static int
800 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
801 CORE_ADDR memaddr;
802 char *myaddr;
803 int len;
804 int should_write;
805 struct target_ops *target; /* ignored */
806 {
807 int xfersize;
808 int bytes_xferred;
809 int total_xferred = 0;
810
811 while (len > 0)
812 {
813 if (len > MAXBUFBYTES)
814 xfersize = MAXBUFBYTES;
815 else
816 xfersize = len;
817
818 if (should_write)
819 bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
820 else
821 bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
822
823 /* If we get an error, we are done xferring. */
824 if (bytes_xferred == 0)
825 break;
826
827 memaddr += bytes_xferred;
828 myaddr += bytes_xferred;
829 len -= bytes_xferred;
830 total_xferred += bytes_xferred;
831 }
832 return total_xferred;
833 }
834
835 static void
836 remote_files_info (ignore)
837 struct target_ops *ignore;
838 {
839 puts_filtered ("Debugging a target over a serial line.\n");
840 }
841 \f
842 /* Stuff for dealing with the packets which are part of this protocol.
843 See comment at top of file for details. */
844
845 /* Read a single character from the remote end, masking it down to 7 bits. */
846
847 static int
848 readchar ()
849 {
850 int ch;
851
852 ch = SERIAL_READCHAR (remote_desc, timeout);
853
854 if (ch < 0)
855 return ch;
856
857 return ch & 0x7f;
858 }
859
860 /* Send the command in BUF to the remote machine,
861 and read the reply into BUF.
862 Report an error if we get an error reply. */
863
864 static void
865 remote_send (buf)
866 char *buf;
867 {
868
869 putpkt (buf);
870 getpkt (buf, 0);
871
872 if (buf[0] == 'E')
873 error ("Remote failure reply: %s", buf);
874 }
875
876 /* Send a packet to the remote machine, with error checking.
877 The data of the packet is in BUF. */
878
879 static void
880 putpkt (buf)
881 char *buf;
882 {
883 int i;
884 unsigned char csum = 0;
885 char buf2[PBUFSIZ];
886 int cnt = strlen (buf);
887 int ch;
888 char *p;
889
890 /* Copy the packet into buffer BUF2, encapsulating it
891 and giving it a checksum. */
892
893 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
894 abort();
895
896 p = buf2;
897 *p++ = '$';
898
899 for (i = 0; i < cnt; i++)
900 {
901 csum += buf[i];
902 *p++ = buf[i];
903 }
904 *p++ = '#';
905 *p++ = tohex ((csum >> 4) & 0xf);
906 *p++ = tohex (csum & 0xf);
907
908 /* Send it over and over until we get a positive ack. */
909
910 while (1)
911 {
912 if (remote_debug)
913 {
914 *p = '\0';
915 printf ("Sending packet: %s...", buf2); fflush(stdout);
916 }
917 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
918 perror_with_name ("putpkt: write failed");
919
920 /* read until either a timeout occurs (-2) or '+' is read */
921 while (1)
922 {
923 ch = readchar ();
924
925 switch (ch)
926 {
927 case '+':
928 if (remote_debug)
929 printf("Ack\n");
930 return;
931 case SERIAL_TIMEOUT:
932 break; /* Retransmit buffer */
933 case SERIAL_ERROR:
934 perror_with_name ("putpkt: couldn't read ACK");
935 case SERIAL_EOF:
936 error ("putpkt: EOF while trying to read ACK");
937 default:
938 if (remote_debug)
939 printf ("%02X %c ", ch&0xFF, ch);
940 continue;
941 }
942 break; /* Here to retransmit */
943 }
944 }
945 }
946
947 /* Read a packet from the remote machine, with error checking,
948 and store it in BUF. BUF is expected to be of size PBUFSIZ.
949 If FOREVER, wait forever rather than timing out; this is used
950 while the target is executing user code. */
951
952 static void
953 getpkt (buf, forever)
954 char *buf;
955 int forever;
956 {
957 char *bp;
958 unsigned char csum;
959 int c = 0;
960 unsigned char c1, c2;
961 int retries = 0;
962 #define MAX_RETRIES 10
963
964 while (1)
965 {
966 /* This can loop forever if the remote side sends us characters
967 continuously, but if it pauses, we'll get a zero from readchar
968 because of timeout. Then we'll count that as a retry. */
969
970 c = readchar();
971 if (c > 0 && c != '$')
972 continue;
973
974 if (c == SERIAL_TIMEOUT)
975 {
976 if (forever)
977 continue;
978 if (++retries >= MAX_RETRIES)
979 if (remote_debug) puts_filtered ("Timed out.\n");
980 goto out;
981 }
982
983 if (c == SERIAL_EOF)
984 error ("Remote connection closed");
985 if (c == SERIAL_ERROR)
986 perror_with_name ("Remote communication error");
987
988 /* Force csum to be zero here because of possible error retry. */
989 csum = 0;
990 bp = buf;
991
992 while (1)
993 {
994 c = readchar ();
995 if (c == SERIAL_TIMEOUT)
996 {
997 if (remote_debug)
998 puts_filtered ("Timeout in mid-packet, retrying\n");
999 goto whole; /* Start a new packet, count retries */
1000 }
1001 if (c == '$')
1002 {
1003 if (remote_debug)
1004 puts_filtered ("Saw new packet start in middle of old one\n");
1005 goto whole; /* Start a new packet, count retries */
1006 }
1007 if (c == '#')
1008 break;
1009 if (bp >= buf+PBUFSIZ-1)
1010 {
1011 *bp = '\0';
1012 puts_filtered ("Remote packet too long: ");
1013 puts_filtered (buf);
1014 puts_filtered ("\n");
1015 goto whole;
1016 }
1017 *bp++ = c;
1018 csum += c;
1019 }
1020 *bp = 0;
1021
1022 c1 = fromhex (readchar ());
1023 c2 = fromhex (readchar ());
1024 if ((csum & 0xff) == (c1 << 4) + c2)
1025 break;
1026 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1027 (c1 << 4) + c2, csum & 0xff);
1028 puts_filtered (buf);
1029 puts_filtered ("\n");
1030
1031 /* Try the whole thing again. */
1032 whole:
1033 if (++retries < MAX_RETRIES)
1034 {
1035 SERIAL_WRITE (remote_desc, "-", 1);
1036 }
1037 else
1038 {
1039 printf ("Ignoring packet error, continuing...\n");
1040 break;
1041 }
1042 }
1043
1044 out:
1045
1046 SERIAL_WRITE (remote_desc, "+", 1);
1047
1048 if (remote_debug)
1049 fprintf (stderr,"Packet received: %s\n", buf);
1050 }
1051 \f
1052 static void
1053 remote_kill ()
1054 {
1055 putpkt ("k");
1056 /* Don't wait for it to die. I'm not really sure it matters whether
1057 we do or not. For the existing stubs, kill is a noop. */
1058 target_mourn_inferior ();
1059 }
1060
1061 static void
1062 remote_mourn ()
1063 {
1064 unpush_target (&remote_ops);
1065 generic_mourn_inferior ();
1066 }
1067 \f
1068 #ifdef REMOTE_BREAKPOINT
1069
1070 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1071 than other targets. */
1072 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1073
1074 /* Check that it fits in BREAKPOINT_MAX bytes. */
1075 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1076
1077 #else /* No REMOTE_BREAKPOINT. */
1078
1079 /* Same old breakpoint instruction. This code does nothing different
1080 than mem-break.c. */
1081 static unsigned char break_insn[] = BREAKPOINT;
1082
1083 #endif /* No REMOTE_BREAKPOINT. */
1084
1085 /* Insert a breakpoint on targets that don't have any better breakpoint
1086 support. We read the contents of the target location and stash it,
1087 then overwrite it with a breakpoint instruction. ADDR is the target
1088 location in the target machine. CONTENTS_CACHE is a pointer to
1089 memory allocated for saving the target contents. It is guaranteed
1090 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1091 is accomplished via BREAKPOINT_MAX). */
1092
1093 static int
1094 remote_insert_breakpoint (addr, contents_cache)
1095 CORE_ADDR addr;
1096 char *contents_cache;
1097 {
1098 int val;
1099
1100 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1101
1102 if (val == 0)
1103 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1104
1105 return val;
1106 }
1107
1108 static int
1109 remote_remove_breakpoint (addr, contents_cache)
1110 CORE_ADDR addr;
1111 char *contents_cache;
1112 {
1113 return target_write_memory (addr, contents_cache, sizeof break_insn);
1114 }
1115 \f
1116 /* Define the target subroutine names */
1117
1118 struct target_ops remote_ops = {
1119 "remote", /* to_shortname */
1120 "Remote serial target in gdb-specific protocol", /* to_longname */
1121 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1122 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1123 remote_open, /* to_open */
1124 remote_close, /* to_close */
1125 NULL, /* to_attach */
1126 remote_detach, /* to_detach */
1127 remote_resume, /* to_resume */
1128 remote_wait, /* to_wait */
1129 remote_fetch_registers, /* to_fetch_registers */
1130 remote_store_registers, /* to_store_registers */
1131 remote_prepare_to_store, /* to_prepare_to_store */
1132 remote_xfer_memory, /* to_xfer_memory */
1133 remote_files_info, /* to_files_info */
1134
1135 remote_insert_breakpoint, /* to_insert_breakpoint */
1136 remote_remove_breakpoint, /* to_remove_breakpoint */
1137
1138 NULL, /* to_terminal_init */
1139 NULL, /* to_terminal_inferior */
1140 NULL, /* to_terminal_ours_for_output */
1141 NULL, /* to_terminal_ours */
1142 NULL, /* to_terminal_info */
1143 remote_kill, /* to_kill */
1144 generic_load, /* to_load */
1145 NULL, /* to_lookup_symbol */
1146 NULL, /* to_create_inferior */
1147 remote_mourn, /* to_mourn_inferior */
1148 0, /* to_can_run */
1149 0, /* to_notice_signals */
1150 process_stratum, /* to_stratum */
1151 NULL, /* to_next */
1152 1, /* to_has_all_memory */
1153 1, /* to_has_memory */
1154 1, /* to_has_stack */
1155 1, /* to_has_registers */
1156 1, /* to_has_execution */
1157 NULL, /* sections */
1158 NULL, /* sections_end */
1159 OPS_MAGIC /* to_magic */
1160 };
1161
1162 void
1163 _initialize_remote ()
1164 {
1165 add_target (&remote_ops);
1166 }
1167 #endif