* remote.c (remote_wait): Added 'W' and 'N' responses.
[deliverable/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 unsigned char *p;
453 int i;
454 long regno;
455 char regs[MAX_REGISTER_RAW_SIZE];
456
457 WSETEXIT ((*status), 0);
458
459 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
460 getpkt ((char *) buf, 1);
461 signal (SIGINT, ofunc);
462
463 if (buf[0] == 'E')
464 error ("Remote failure reply: %s", buf);
465 if (buf[0] == 'T')
466 {
467 /* Expedited reply, containing Signal, {regno, reg} repeat */
468 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
469 ss = signal number
470 n... = register number
471 r... = register contents
472 */
473
474 p = &buf[3]; /* after Txx */
475
476 while (*p)
477 {
478 unsigned char *p1;
479
480 regno = strtol (p, &p1, 16); /* Read the register number */
481
482 if (p1 == p)
483 error ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
484 p1, buf);
485
486 p = p1;
487
488 if (*p++ != ':')
489 error ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
490 p, buf);
491
492 if (regno >= NUM_REGS)
493 error ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
494 regno, p, buf);
495
496 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
497 {
498 if (p[0] == 0 || p[1] == 0)
499 error ("Remote reply is too short: %s", buf);
500 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
501 p += 2;
502 }
503
504 if (*p++ != ';')
505 error("Remote register badly formatted: %s", buf);
506
507 supply_register (regno, regs);
508 }
509 }
510 else if (buf[0] == 'N')
511 {
512 unsigned char *p1;
513 bfd_vma text_addr, data_addr, bss_addr;
514
515 /* Relocate object file. Format is NAATT;DD;BB where AA is the
516 signal number, TT is the new text address, DD is the new data
517 address, and BB is the new bss address. This is used by the
518 NLM stub; gdb may see more sections. */
519 p = &buf[3];
520 text_addr = strtol (p, &p1, 16);
521 if (p1 == p || *p1 != ';')
522 error ("Malformed relocation packet: Packet '%s'", buf);
523 p = p1 + 1;
524 data_addr = strtol (p, &p1, 16);
525 if (p1 == p || *p1 != ';')
526 error ("Malformed relocation packet: Packet '%s'", buf);
527 p = p1 + 1;
528 bss_addr = strtol (p, &p1, 16);
529 if (p1 == p)
530 error ("Malformed relocation packet: Packet '%s'", buf);
531
532 if (symfile_objfile != NULL)
533 {
534 struct section_offsets *offs;
535
536 /* FIXME: Why don't the various symfile_offsets routines in
537 the sym_fns vectors set this? */
538 if (symfile_objfile->num_sections == 0)
539 symfile_objfile->num_sections = SECT_OFF_MAX;
540
541 offs = ((struct section_offsets *)
542 alloca (sizeof (struct section_offsets)
543 + (symfile_objfile->num_sections
544 * sizeof (offs->offsets))));
545 memcpy (offs, symfile_objfile->section_offsets,
546 (sizeof (struct section_offsets)
547 + (symfile_objfile->num_sections
548 * sizeof (offs->offsets))));
549 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
550 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
551 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
552
553 objfile_relocate (symfile_objfile, offs);
554 }
555 }
556 else if (buf[0] == 'W')
557 {
558 /* The remote process exited. */
559 WSETEXIT (*status, (fromhex (buf[1]) << 4) + fromhex (buf[2]));
560 return 0;
561 }
562 else if (buf[0] != 'S')
563 error ("Invalid remote reply: %s", buf);
564
565 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
566
567 return 0;
568 }
569
570 /* Read the remote registers into the block REGS. */
571 /* Currently we just read all the registers, so we don't use regno. */
572 /* ARGSUSED */
573 static void
574 remote_fetch_registers (regno)
575 int regno;
576 {
577 char buf[PBUFSIZ];
578 int i;
579 char *p;
580 char regs[REGISTER_BYTES];
581
582 sprintf (buf, "g");
583 remote_send (buf);
584
585 /* Reply describes registers byte by byte, each byte encoded as two
586 hex characters. Suck them all up, then supply them to the
587 register cacheing/storage mechanism. */
588
589 p = buf;
590 for (i = 0; i < REGISTER_BYTES; i++)
591 {
592 if (p[0] == 0 || p[1] == 0)
593 error ("Remote reply is too short: %s", buf);
594 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
595 p += 2;
596 }
597 for (i = 0; i < NUM_REGS; i++)
598 supply_register (i, &regs[REGISTER_BYTE(i)]);
599 }
600
601 /* Prepare to store registers. Since we send them all, we have to
602 read out the ones we don't want to change first. */
603
604 static void
605 remote_prepare_to_store ()
606 {
607 /* Make sure the entire registers array is valid. */
608 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
609 }
610
611 /* Store the remote registers from the contents of the block REGISTERS.
612 FIXME, eventually just store one register if that's all that is needed. */
613
614 /* ARGSUSED */
615 static void
616 remote_store_registers (regno)
617 int regno;
618 {
619 char buf[PBUFSIZ];
620 int i;
621 char *p;
622
623 buf[0] = 'G';
624
625 /* Command describes registers byte by byte,
626 each byte encoded as two hex characters. */
627
628 p = buf + 1;
629 for (i = 0; i < REGISTER_BYTES; i++)
630 {
631 *p++ = tohex ((registers[i] >> 4) & 0xf);
632 *p++ = tohex (registers[i] & 0xf);
633 }
634 *p = '\0';
635
636 remote_send (buf);
637 }
638
639 #if 0
640
641 /* Use of the data cache is disabled because it loses for looking at
642 and changing hardware I/O ports and the like. Accepting `volatile'
643 would perhaps be one way to fix it, but a better way which would
644 win for more cases would be to use the executable file for the text
645 segment, like the `icache' code below but done cleanly (in some
646 target-independent place, perhaps in target_xfer_memory, perhaps
647 based on assigning each target a speed or perhaps by some simpler
648 mechanism). */
649
650 /* Read a word from remote address ADDR and return it.
651 This goes through the data cache. */
652
653 static int
654 remote_fetch_word (addr)
655 CORE_ADDR addr;
656 {
657 #if 0
658 if (icache)
659 {
660 extern CORE_ADDR text_start, text_end;
661
662 if (addr >= text_start && addr < text_end)
663 {
664 int buffer;
665 xfer_core_file (addr, &buffer, sizeof (int));
666 return buffer;
667 }
668 }
669 #endif
670 return dcache_fetch (remote_dcache, addr);
671 }
672
673 /* Write a word WORD into remote address ADDR.
674 This goes through the data cache. */
675
676 static void
677 remote_store_word (addr, word)
678 CORE_ADDR addr;
679 int word;
680 {
681 dcache_poke (remote_dcache, addr, word);
682 }
683 #endif /* 0 */
684 \f
685 /* Write memory data directly to the remote machine.
686 This does not inform the data cache; the data cache uses this.
687 MEMADDR is the address in the remote memory space.
688 MYADDR is the address of the buffer in our space.
689 LEN is the number of bytes.
690
691 Returns number of bytes transferred, or 0 for error. */
692
693 static int
694 remote_write_bytes (memaddr, myaddr, len)
695 CORE_ADDR memaddr;
696 unsigned char *myaddr;
697 int len;
698 {
699 char buf[PBUFSIZ];
700 int i;
701 char *p;
702
703 if (len > PBUFSIZ / 2 - 20)
704 abort ();
705
706 sprintf (buf, "M%x,%x:", memaddr, len);
707
708 /* We send target system values byte by byte, in increasing byte addresses,
709 each byte encoded as two hex characters. */
710
711 p = buf + strlen (buf);
712 for (i = 0; i < len; i++)
713 {
714 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
715 *p++ = tohex (myaddr[i] & 0xf);
716 }
717 *p = '\0';
718
719 putpkt (buf);
720 getpkt (buf, 0);
721
722 if (buf[0] == 'E')
723 {
724 /* There is no correspondance between what the remote protocol uses
725 for errors and errno codes. We would like a cleaner way of
726 representing errors (big enough to include errno codes, bfd_error
727 codes, and others). But for now just return EIO. */
728 errno = EIO;
729 return 0;
730 }
731 return len;
732 }
733
734 /* Read memory data directly from the remote machine.
735 This does not use the data cache; the data cache uses this.
736 MEMADDR is the address in the remote memory space.
737 MYADDR is the address of the buffer in our space.
738 LEN is the number of bytes.
739
740 Returns number of bytes transferred, or 0 for error. */
741
742 static int
743 remote_read_bytes (memaddr, myaddr, len)
744 CORE_ADDR memaddr;
745 unsigned char *myaddr;
746 int len;
747 {
748 char buf[PBUFSIZ];
749 int i;
750 char *p;
751
752 if (len > PBUFSIZ / 2 - 1)
753 abort ();
754
755 sprintf (buf, "m%x,%x", memaddr, len);
756 putpkt (buf);
757 getpkt (buf, 0);
758
759 if (buf[0] == 'E')
760 {
761 /* There is no correspondance between what the remote protocol uses
762 for errors and errno codes. We would like a cleaner way of
763 representing errors (big enough to include errno codes, bfd_error
764 codes, and others). But for now just return EIO. */
765 errno = EIO;
766 return 0;
767 }
768
769 /* Reply describes memory byte by byte,
770 each byte encoded as two hex characters. */
771
772 p = buf;
773 for (i = 0; i < len; i++)
774 {
775 if (p[0] == 0 || p[1] == 0)
776 /* Reply is short. This means that we were able to read only part
777 of what we wanted to. */
778 break;
779 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
780 p += 2;
781 }
782 return i;
783 }
784 \f
785 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
786 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
787 nonzero. Returns length of data written or read; 0 for error. */
788
789 /* ARGSUSED */
790 static int
791 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
792 CORE_ADDR memaddr;
793 char *myaddr;
794 int len;
795 int should_write;
796 struct target_ops *target; /* ignored */
797 {
798 int xfersize;
799 int bytes_xferred;
800 int total_xferred = 0;
801
802 while (len > 0)
803 {
804 if (len > MAXBUFBYTES)
805 xfersize = MAXBUFBYTES;
806 else
807 xfersize = len;
808
809 if (should_write)
810 bytes_xferred = remote_write_bytes (memaddr, myaddr, xfersize);
811 else
812 bytes_xferred = remote_read_bytes (memaddr, myaddr, xfersize);
813
814 /* If we get an error, we are done xferring. */
815 if (bytes_xferred == 0)
816 break;
817
818 memaddr += bytes_xferred;
819 myaddr += bytes_xferred;
820 len -= bytes_xferred;
821 total_xferred += bytes_xferred;
822 }
823 return total_xferred;
824 }
825
826 static void
827 remote_files_info (ignore)
828 struct target_ops *ignore;
829 {
830 puts_filtered ("Debugging a target over a serial line.\n");
831 }
832 \f
833 /* Stuff for dealing with the packets which are part of this protocol.
834 See comment at top of file for details. */
835
836 /* Read a single character from the remote end, masking it down to 7 bits. */
837
838 static int
839 readchar ()
840 {
841 int ch;
842
843 ch = SERIAL_READCHAR (remote_desc, timeout);
844
845 if (ch < 0)
846 return ch;
847
848 return ch & 0x7f;
849 }
850
851 /* Send the command in BUF to the remote machine,
852 and read the reply into BUF.
853 Report an error if we get an error reply. */
854
855 static void
856 remote_send (buf)
857 char *buf;
858 {
859
860 putpkt (buf);
861 getpkt (buf, 0);
862
863 if (buf[0] == 'E')
864 error ("Remote failure reply: %s", buf);
865 }
866
867 /* Send a packet to the remote machine, with error checking.
868 The data of the packet is in BUF. */
869
870 static void
871 putpkt (buf)
872 char *buf;
873 {
874 int i;
875 unsigned char csum = 0;
876 char buf2[PBUFSIZ];
877 int cnt = strlen (buf);
878 int ch;
879 char *p;
880
881 /* Copy the packet into buffer BUF2, encapsulating it
882 and giving it a checksum. */
883
884 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
885 abort();
886
887 p = buf2;
888 *p++ = '$';
889
890 for (i = 0; i < cnt; i++)
891 {
892 csum += buf[i];
893 *p++ = buf[i];
894 }
895 *p++ = '#';
896 *p++ = tohex ((csum >> 4) & 0xf);
897 *p++ = tohex (csum & 0xf);
898
899 /* Send it over and over until we get a positive ack. */
900
901 while (1)
902 {
903 if (remote_debug)
904 {
905 *p = '\0';
906 printf ("Sending packet: %s...", buf2); fflush(stdout);
907 }
908 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
909 perror_with_name ("putpkt: write failed");
910
911 /* read until either a timeout occurs (-2) or '+' is read */
912 while (1)
913 {
914 ch = readchar ();
915
916 switch (ch)
917 {
918 case '+':
919 if (remote_debug)
920 printf("Ack\n");
921 return;
922 case SERIAL_TIMEOUT:
923 break; /* Retransmit buffer */
924 case SERIAL_ERROR:
925 perror_with_name ("putpkt: couldn't read ACK");
926 case SERIAL_EOF:
927 error ("putpkt: EOF while trying to read ACK");
928 default:
929 if (remote_debug)
930 printf ("%02X %c ", ch&0xFF, ch);
931 continue;
932 }
933 break; /* Here to retransmit */
934 }
935 }
936 }
937
938 /* Read a packet from the remote machine, with error checking,
939 and store it in BUF. BUF is expected to be of size PBUFSIZ.
940 If FOREVER, wait forever rather than timing out; this is used
941 while the target is executing user code. */
942
943 static void
944 getpkt (buf, forever)
945 char *buf;
946 int forever;
947 {
948 char *bp;
949 unsigned char csum;
950 int c = 0;
951 unsigned char c1, c2;
952 int retries = 0;
953 #define MAX_RETRIES 10
954
955 while (1)
956 {
957 /* This can loop forever if the remote side sends us characters
958 continuously, but if it pauses, we'll get a zero from readchar
959 because of timeout. Then we'll count that as a retry. */
960
961 c = readchar();
962 if (c > 0 && c != '$')
963 continue;
964
965 if (c == SERIAL_TIMEOUT)
966 {
967 if (forever)
968 continue;
969 if (++retries >= MAX_RETRIES)
970 if (remote_debug) puts_filtered ("Timed out.\n");
971 goto out;
972 }
973
974 if (c == SERIAL_EOF)
975 error ("Remote connection closed");
976 if (c == SERIAL_ERROR)
977 perror_with_name ("Remote communication error");
978
979 /* Force csum to be zero here because of possible error retry. */
980 csum = 0;
981 bp = buf;
982
983 while (1)
984 {
985 c = readchar ();
986 if (c == SERIAL_TIMEOUT)
987 {
988 if (remote_debug)
989 puts_filtered ("Timeout in mid-packet, retrying\n");
990 goto whole; /* Start a new packet, count retries */
991 }
992 if (c == '$')
993 {
994 if (remote_debug)
995 puts_filtered ("Saw new packet start in middle of old one\n");
996 goto whole; /* Start a new packet, count retries */
997 }
998 if (c == '#')
999 break;
1000 if (bp >= buf+PBUFSIZ-1)
1001 {
1002 *bp = '\0';
1003 puts_filtered ("Remote packet too long: ");
1004 puts_filtered (buf);
1005 puts_filtered ("\n");
1006 goto whole;
1007 }
1008 *bp++ = c;
1009 csum += c;
1010 }
1011 *bp = 0;
1012
1013 c1 = fromhex (readchar ());
1014 c2 = fromhex (readchar ());
1015 if ((csum & 0xff) == (c1 << 4) + c2)
1016 break;
1017 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1018 (c1 << 4) + c2, csum & 0xff);
1019 puts_filtered (buf);
1020 puts_filtered ("\n");
1021
1022 /* Try the whole thing again. */
1023 whole:
1024 if (++retries < MAX_RETRIES)
1025 {
1026 SERIAL_WRITE (remote_desc, "-", 1);
1027 }
1028 else
1029 {
1030 printf ("Ignoring packet error, continuing...\n");
1031 break;
1032 }
1033 }
1034
1035 out:
1036
1037 SERIAL_WRITE (remote_desc, "+", 1);
1038
1039 if (remote_debug)
1040 fprintf (stderr,"Packet received: %s\n", buf);
1041 }
1042 \f
1043 static void
1044 remote_kill ()
1045 {
1046 putpkt ("k");
1047 /* Don't wait for it to die. I'm not really sure it matters whether
1048 we do or not. For the existing stubs, kill is a noop. */
1049 target_mourn_inferior ();
1050 }
1051
1052 static void
1053 remote_mourn ()
1054 {
1055 unpush_target (&remote_ops);
1056 generic_mourn_inferior ();
1057 }
1058 \f
1059 #ifdef REMOTE_BREAKPOINT
1060
1061 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1062 than other targets. */
1063 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1064
1065 /* Check that it fits in BREAKPOINT_MAX bytes. */
1066 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1067
1068 #else /* No REMOTE_BREAKPOINT. */
1069
1070 /* Same old breakpoint instruction. This code does nothing different
1071 than mem-break.c. */
1072 static unsigned char break_insn[] = BREAKPOINT;
1073
1074 #endif /* No REMOTE_BREAKPOINT. */
1075
1076 /* Insert a breakpoint on targets that don't have any better breakpoint
1077 support. We read the contents of the target location and stash it,
1078 then overwrite it with a breakpoint instruction. ADDR is the target
1079 location in the target machine. CONTENTS_CACHE is a pointer to
1080 memory allocated for saving the target contents. It is guaranteed
1081 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1082 is accomplished via BREAKPOINT_MAX). */
1083
1084 static int
1085 remote_insert_breakpoint (addr, contents_cache)
1086 CORE_ADDR addr;
1087 char *contents_cache;
1088 {
1089 int val;
1090
1091 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1092
1093 if (val == 0)
1094 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1095
1096 return val;
1097 }
1098
1099 static int
1100 remote_remove_breakpoint (addr, contents_cache)
1101 CORE_ADDR addr;
1102 char *contents_cache;
1103 {
1104 return target_write_memory (addr, contents_cache, sizeof break_insn);
1105 }
1106 \f
1107 /* Define the target subroutine names */
1108
1109 struct target_ops remote_ops = {
1110 "remote", /* to_shortname */
1111 "Remote serial target in gdb-specific protocol", /* to_longname */
1112 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1113 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1114 remote_open, /* to_open */
1115 remote_close, /* to_close */
1116 NULL, /* to_attach */
1117 remote_detach, /* to_detach */
1118 remote_resume, /* to_resume */
1119 remote_wait, /* to_wait */
1120 remote_fetch_registers, /* to_fetch_registers */
1121 remote_store_registers, /* to_store_registers */
1122 remote_prepare_to_store, /* to_prepare_to_store */
1123 remote_xfer_memory, /* to_xfer_memory */
1124 remote_files_info, /* to_files_info */
1125
1126 remote_insert_breakpoint, /* to_insert_breakpoint */
1127 remote_remove_breakpoint, /* to_remove_breakpoint */
1128
1129 NULL, /* to_terminal_init */
1130 NULL, /* to_terminal_inferior */
1131 NULL, /* to_terminal_ours_for_output */
1132 NULL, /* to_terminal_ours */
1133 NULL, /* to_terminal_info */
1134 remote_kill, /* to_kill */
1135 generic_load, /* to_load */
1136 NULL, /* to_lookup_symbol */
1137 NULL, /* to_create_inferior */
1138 remote_mourn, /* to_mourn_inferior */
1139 0, /* to_can_run */
1140 0, /* to_notice_signals */
1141 process_stratum, /* to_stratum */
1142 NULL, /* to_next */
1143 1, /* to_has_all_memory */
1144 1, /* to_has_memory */
1145 1, /* to_has_stack */
1146 1, /* to_has_registers */
1147 1, /* to_has_execution */
1148 NULL, /* sections */
1149 NULL, /* sections_end */
1150 OPS_MAGIC /* to_magic */
1151 };
1152
1153 void
1154 _initialize_remote ()
1155 {
1156 add_target (&remote_ops);
1157 }
1158 #endif
This page took 0.052614 seconds and 5 git commands to generate.