7240f0b52774128111424b3187d444ded0fe7418
[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, 1994 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 search tAA:PP,MM Search backward starting at address
120 AA for a match with pattern PP and
121 mask MM. PP and MM are 4 bytes.
122 Not supported by all stubs.
123
124 Responses can be run-length encoded to save space. A '*' means that
125 the next two characters are hex digits giving a repeat count which
126 stands for that many repititions of the character preceding the '*'.
127 Note that this means that responses cannot contain '*'. Example:
128 "0*03" means the same as "0000". */
129
130 #include "defs.h"
131 #include <string.h>
132 #include <fcntl.h>
133 #include "frame.h"
134 #include "inferior.h"
135 #include "bfd.h"
136 #include "symfile.h"
137 #include "target.h"
138 #include "wait.h"
139 #include "terminal.h"
140 #include "gdbcmd.h"
141 #include "objfiles.h"
142 #include "gdb-stabs.h"
143
144 #include "dcache.h"
145
146 #if !defined(DONT_USE_REMOTE)
147 #ifdef USG
148 #include <sys/types.h>
149 #endif
150
151 #include <signal.h>
152 #include "serial.h"
153
154 /* Prototypes for local functions */
155
156 static int
157 remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
158
159 static int
160 remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
161
162 static void
163 remote_files_info PARAMS ((struct target_ops *ignore));
164
165 static int
166 remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
167 int should_write, struct target_ops *target));
168
169 static void
170 remote_prepare_to_store PARAMS ((void));
171
172 static void
173 remote_fetch_registers PARAMS ((int regno));
174
175 static void
176 remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
177
178 static int
179 remote_start_remote PARAMS ((char *dummy));
180
181 static void
182 remote_open PARAMS ((char *name, int from_tty));
183
184 static void
185 remote_close PARAMS ((int quitting));
186
187 static void
188 remote_store_registers PARAMS ((int regno));
189
190 static void
191 getpkt PARAMS ((char *buf, int forever));
192
193 static void
194 putpkt PARAMS ((char *buf));
195
196 static void
197 remote_send PARAMS ((char *buf));
198
199 static int
200 readchar PARAMS ((void));
201
202 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
203
204 static int
205 tohex PARAMS ((int nib));
206
207 static int
208 fromhex PARAMS ((int a));
209
210 static void
211 remote_detach PARAMS ((char *args, int from_tty));
212
213 static void
214 remote_interrupt PARAMS ((int signo));
215
216 static void
217 remote_interrupt_twice PARAMS ((int signo));
218
219 static void
220 interrupt_query PARAMS ((void));
221
222 extern struct target_ops remote_ops; /* Forward decl */
223
224 /* This was 5 seconds, which is a long time to sit and wait.
225 Unless this is going though some terminal server or multiplexer or
226 other form of hairy serial connection, I would think 2 seconds would
227 be plenty. */
228 static int timeout = 2;
229
230 #if 0
231 int icache;
232 #endif
233
234 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
235 remote_open knows that we don't have a file open when the program
236 starts. */
237 serial_t remote_desc = NULL;
238
239 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
240 and i386-stub.c. Normally, no one would notice because it only matters
241 for writing large chunks of memory (e.g. in downloads). Also, this needs
242 to be more than 400 if required to hold the registers (see below, where
243 we round it up based on REGISTER_BYTES). */
244 #define PBUFSIZ 400
245
246 /* Maximum number of bytes to read/write at once. The value here
247 is chosen to fill up a packet (the headers account for the 32). */
248 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
249
250 /* Round up PBUFSIZ to hold all the registers, at least. */
251 #if REGISTER_BYTES > MAXBUFBYTES
252 #undef PBUFSIZ
253 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
254 #endif
255 \f
256 /* Clean up connection to a remote debugger. */
257
258 /* ARGSUSED */
259 static void
260 remote_close (quitting)
261 int quitting;
262 {
263 if (remote_desc)
264 SERIAL_CLOSE (remote_desc);
265 remote_desc = NULL;
266 }
267
268 /* Stub for catch_errors. */
269
270 static int
271 remote_start_remote (dummy)
272 char *dummy;
273 {
274 immediate_quit = 1; /* Allow user to interrupt it */
275
276 /* Ack any packet which the remote side has already sent. */
277 /* I'm not sure this \r is needed; we don't use it any other time we
278 send an ack. */
279 SERIAL_WRITE (remote_desc, "+\r", 2);
280 putpkt ("?"); /* initiate a query from remote machine */
281 immediate_quit = 0;
282
283 start_remote (); /* Initialize gdb process mechanisms */
284 return 1;
285 }
286
287 /* Open a connection to a remote debugger.
288 NAME is the filename used for communication. */
289
290 static DCACHE *remote_dcache;
291
292 static void
293 remote_open (name, from_tty)
294 char *name;
295 int from_tty;
296 {
297 if (name == 0)
298 error (
299 "To open a remote debug connection, you need to specify what serial\n\
300 device is attached to the remote system (e.g. /dev/ttya).");
301
302 target_preopen (from_tty);
303
304 unpush_target (&remote_ops);
305
306 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
307
308 remote_desc = SERIAL_OPEN (name);
309 if (!remote_desc)
310 perror_with_name (name);
311
312 if (baud_rate != -1)
313 {
314 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
315 {
316 SERIAL_CLOSE (remote_desc);
317 perror_with_name (name);
318 }
319 }
320
321 SERIAL_RAW (remote_desc);
322
323 /* If there is something sitting in the buffer we might take it as a
324 response to a command, which would be bad. */
325 SERIAL_FLUSH_INPUT (remote_desc);
326
327 if (from_tty)
328 {
329 puts_filtered ("Remote debugging using ");
330 puts_filtered (name);
331 puts_filtered ("\n");
332 }
333 push_target (&remote_ops); /* Switch to using remote target now */
334
335 /* Start the remote connection; if error (0), discard this target.
336 In particular, if the user quits, be sure to discard it
337 (we'd be in an inconsistent state otherwise). */
338 if (!catch_errors (remote_start_remote, (char *)0,
339 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
340 pop_target();
341 }
342
343 /* remote_detach()
344 takes a program previously attached to and detaches it.
345 We better not have left any breakpoints
346 in the program or it'll die when it hits one.
347 Close the open connection to the remote debugger.
348 Use this when you want to detach and do something else
349 with your gdb. */
350
351 static void
352 remote_detach (args, from_tty)
353 char *args;
354 int from_tty;
355 {
356 if (args)
357 error ("Argument given to \"detach\" when remotely debugging.");
358
359 pop_target ();
360 if (from_tty)
361 puts_filtered ("Ending remote debugging.\n");
362 }
363
364 /* Convert hex digit A to a number. */
365
366 static int
367 fromhex (a)
368 int a;
369 {
370 if (a >= '0' && a <= '9')
371 return a - '0';
372 else if (a >= 'a' && a <= 'f')
373 return a - 'a' + 10;
374 else
375 error ("Reply contains invalid hex digit");
376 return -1;
377 }
378
379 /* Convert number NIB to a hex digit. */
380
381 static int
382 tohex (nib)
383 int nib;
384 {
385 if (nib < 10)
386 return '0'+nib;
387 else
388 return 'a'+nib-10;
389 }
390 \f
391 /* Tell the remote machine to resume. */
392
393 static void
394 remote_resume (pid, step, siggnal)
395 int pid, step;
396 enum target_signal siggnal;
397 {
398 char buf[PBUFSIZ];
399
400 if (siggnal)
401 {
402 char *name;
403 target_terminal_ours_for_output ();
404 printf_filtered
405 ("Can't send signals to a remote system. %s not sent.\n",
406 target_signal_to_name (siggnal));
407 target_terminal_inferior ();
408 }
409
410 dcache_flush (remote_dcache);
411
412 strcpy (buf, step ? "s": "c");
413
414 putpkt (buf);
415 }
416 \f
417 /* Send ^C to target to halt it. Target will respond, and send us a
418 packet. */
419
420 static void
421 remote_interrupt (signo)
422 int signo;
423 {
424 /* If this doesn't work, try more severe steps. */
425 signal (signo, remote_interrupt_twice);
426
427 if (remote_debug)
428 printf_unfiltered ("remote_interrupt called\n");
429
430 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
431 }
432
433 static void (*ofunc)();
434
435 /* The user typed ^C twice. */
436 static void
437 remote_interrupt_twice (signo)
438 int signo;
439 {
440 signal (signo, ofunc);
441
442 interrupt_query ();
443
444 signal (signo, remote_interrupt);
445 }
446
447 /* Ask the user what to do when an interrupt is received. */
448
449 static void
450 interrupt_query ()
451 {
452 target_terminal_ours ();
453
454 if (query ("Interrupted while waiting for the program.\n\
455 Give up (and stop debugging it)? "))
456 {
457 target_mourn_inferior ();
458 return_to_top_level (RETURN_QUIT);
459 }
460
461 target_terminal_inferior ();
462 }
463
464 /* Wait until the remote machine stops, then return,
465 storing status in STATUS just as `wait' would.
466 Returns "pid" (though it's not clear what, if anything, that
467 means in the case of this target). */
468
469 static int
470 remote_wait (pid, status)
471 int pid;
472 struct target_waitstatus *status;
473 {
474 unsigned char buf[PBUFSIZ];
475
476 status->kind = TARGET_WAITKIND_EXITED;
477 status->value.integer = 0;
478
479 while (1)
480 {
481 unsigned char *p;
482
483 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
484 getpkt ((char *) buf, 1);
485 signal (SIGINT, ofunc);
486
487 if (buf[0] == 'E')
488 warning ("Remote failure reply: %s", buf);
489 else if (buf[0] == 'T')
490 {
491 int i;
492 long regno;
493 char regs[MAX_REGISTER_RAW_SIZE];
494
495 /* Expedited reply, containing Signal, {regno, reg} repeat */
496 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
497 ss = signal number
498 n... = register number
499 r... = register contents
500 */
501
502 p = &buf[3]; /* after Txx */
503
504 while (*p)
505 {
506 unsigned char *p1;
507
508 regno = strtol (p, &p1, 16); /* Read the register number */
509
510 if (p1 == p)
511 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
512 p1, buf);
513
514 p = p1;
515
516 if (*p++ != ':')
517 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
518 p, buf);
519
520 if (regno >= NUM_REGS)
521 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
522 regno, p, buf);
523
524 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
525 {
526 if (p[0] == 0 || p[1] == 0)
527 warning ("Remote reply is too short: %s", buf);
528 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
529 p += 2;
530 }
531
532 if (*p++ != ';')
533 warning ("Remote register badly formatted: %s", buf);
534
535 supply_register (regno, regs);
536 }
537 break;
538 }
539 else if (buf[0] == 'N')
540 {
541 unsigned char *p1;
542 bfd_vma text_addr, data_addr, bss_addr;
543
544 /* Relocate object file. Format is NAATT;DD;BB where AA is
545 the signal number, TT is the new text address, DD is the
546 new data address, and BB is the new bss address. This is
547 used by the NLM stub; gdb may see more sections. */
548 p = &buf[3];
549 text_addr = strtoul (p, &p1, 16);
550 if (p1 == p || *p1 != ';')
551 warning ("Malformed relocation packet: Packet '%s'", buf);
552 p = p1 + 1;
553 data_addr = strtoul (p, &p1, 16);
554 if (p1 == p || *p1 != ';')
555 warning ("Malformed relocation packet: Packet '%s'", buf);
556 p = p1 + 1;
557 bss_addr = strtoul (p, &p1, 16);
558 if (p1 == p)
559 warning ("Malformed relocation packet: Packet '%s'", buf);
560
561 if (symfile_objfile != NULL
562 && (ANOFFSET (symfile_objfile->section_offsets,
563 SECT_OFF_TEXT) != text_addr
564 || ANOFFSET (symfile_objfile->section_offsets,
565 SECT_OFF_DATA) != data_addr
566 || ANOFFSET (symfile_objfile->section_offsets,
567 SECT_OFF_BSS) != bss_addr))
568 {
569 struct section_offsets *offs;
570
571 /* FIXME: This code assumes gdb-stabs.h is being used;
572 it's broken for xcoff, dwarf, sdb-coff, etc. But
573 there is no simple canonical representation for this
574 stuff. (Just what does "text" as seen by the stub
575 mean, anyway?). */
576
577 offs = ((struct section_offsets *)
578 alloca (sizeof (struct section_offsets)
579 + (symfile_objfile->num_sections
580 * sizeof (offs->offsets))));
581 memcpy (offs, symfile_objfile->section_offsets,
582 (sizeof (struct section_offsets)
583 + (symfile_objfile->num_sections
584 * sizeof (offs->offsets))));
585 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
586 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
587 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
588
589 objfile_relocate (symfile_objfile, offs);
590 {
591 struct obj_section *s;
592 bfd *abfd;
593
594 abfd = symfile_objfile->obfd;
595
596 for (s = symfile_objfile->sections;
597 s < symfile_objfile->sections_end; ++s)
598 {
599 flagword flags;
600
601 flags = bfd_get_section_flags (abfd, s->the_bfd_section);
602
603 if (flags & SEC_CODE)
604 {
605 s->addr += text_addr;
606 s->endaddr += text_addr;
607 }
608 else if (flags & (SEC_DATA | SEC_LOAD))
609 {
610 s->addr += data_addr;
611 s->endaddr += data_addr;
612 }
613 else if (flags & SEC_ALLOC)
614 {
615 s->addr += bss_addr;
616 s->endaddr += bss_addr;
617 }
618 }
619 }
620 }
621 break;
622 }
623 else if (buf[0] == 'W')
624 {
625 /* The remote process exited. */
626 status->kind = TARGET_WAITKIND_EXITED;
627 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
628 return 0;
629 }
630 else if (buf[0] == 'S')
631 break;
632 else
633 warning ("Invalid remote reply: %s", buf);
634 }
635
636 status->kind = TARGET_WAITKIND_STOPPED;
637 status->value.sig = (enum target_signal)
638 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
639
640 return 0;
641 }
642
643 /* Number of bytes of registers this stub implements. */
644 static int register_bytes_found;
645
646 /* Read the remote registers into the block REGS. */
647 /* Currently we just read all the registers, so we don't use regno. */
648 /* ARGSUSED */
649 static void
650 remote_fetch_registers (regno)
651 int regno;
652 {
653 char buf[PBUFSIZ];
654 int i;
655 char *p;
656 char regs[REGISTER_BYTES];
657
658 sprintf (buf, "g");
659 remote_send (buf);
660
661 /* Unimplemented registers read as all bits zero. */
662 memset (regs, 0, REGISTER_BYTES);
663
664 /* We can get out of synch in various cases. If the first character
665 in the buffer is not a hex character, assume that has happened
666 and try to fetch another packet to read. */
667 while ((buf[0] < '0' || buf[0] > '9')
668 && (buf[0] < 'a' || buf[0] > 'f'))
669 {
670 if (remote_debug)
671 printf_unfiltered ("Bad register packet; fetching a new packet\n");
672 getpkt (buf, 0);
673 }
674
675 /* Reply describes registers byte by byte, each byte encoded as two
676 hex characters. Suck them all up, then supply them to the
677 register cacheing/storage mechanism. */
678
679 p = buf;
680 for (i = 0; i < REGISTER_BYTES; i++)
681 {
682 if (p[0] == 0)
683 break;
684 if (p[1] == 0)
685 {
686 warning ("Remote reply is of odd length: %s", buf);
687 /* Don't change register_bytes_found in this case, and don't
688 print a second warning. */
689 goto supply_them;
690 }
691 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
692 p += 2;
693 }
694
695 if (i != register_bytes_found)
696 {
697 register_bytes_found = i;
698 #ifdef REGISTER_BYTES_OK
699 if (!REGISTER_BYTES_OK (i))
700 warning ("Remote reply is too short: %s", buf);
701 #endif
702 }
703
704 supply_them:
705 for (i = 0; i < NUM_REGS; i++)
706 supply_register (i, &regs[REGISTER_BYTE(i)]);
707 }
708
709 /* Prepare to store registers. Since we send them all, we have to
710 read out the ones we don't want to change first. */
711
712 static void
713 remote_prepare_to_store ()
714 {
715 /* Make sure the entire registers array is valid. */
716 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
717 }
718
719 /* Store the remote registers from the contents of the block REGISTERS.
720 FIXME, eventually just store one register if that's all that is needed. */
721
722 /* ARGSUSED */
723 static void
724 remote_store_registers (regno)
725 int regno;
726 {
727 char buf[PBUFSIZ];
728 int i;
729 char *p;
730
731 buf[0] = 'G';
732
733 /* Command describes registers byte by byte,
734 each byte encoded as two hex characters. */
735
736 p = buf + 1;
737 /* remote_prepare_to_store insures that register_bytes_found gets set. */
738 for (i = 0; i < register_bytes_found; i++)
739 {
740 *p++ = tohex ((registers[i] >> 4) & 0xf);
741 *p++ = tohex (registers[i] & 0xf);
742 }
743 *p = '\0';
744
745 remote_send (buf);
746 }
747
748 #if 0
749
750 /* Use of the data cache is disabled because it loses for looking at
751 and changing hardware I/O ports and the like. Accepting `volatile'
752 would perhaps be one way to fix it, but a better way which would
753 win for more cases would be to use the executable file for the text
754 segment, like the `icache' code below but done cleanly (in some
755 target-independent place, perhaps in target_xfer_memory, perhaps
756 based on assigning each target a speed or perhaps by some simpler
757 mechanism). */
758
759 /* Read a word from remote address ADDR and return it.
760 This goes through the data cache. */
761
762 static int
763 remote_fetch_word (addr)
764 CORE_ADDR addr;
765 {
766 #if 0
767 if (icache)
768 {
769 extern CORE_ADDR text_start, text_end;
770
771 if (addr >= text_start && addr < text_end)
772 {
773 int buffer;
774 xfer_core_file (addr, &buffer, sizeof (int));
775 return buffer;
776 }
777 }
778 #endif
779 return dcache_fetch (remote_dcache, addr);
780 }
781
782 /* Write a word WORD into remote address ADDR.
783 This goes through the data cache. */
784
785 static void
786 remote_store_word (addr, word)
787 CORE_ADDR addr;
788 int word;
789 {
790 dcache_poke (remote_dcache, addr, word);
791 }
792 #endif /* 0 */
793 \f
794 /* Write memory data directly to the remote machine.
795 This does not inform the data cache; the data cache uses this.
796 MEMADDR is the address in the remote memory space.
797 MYADDR is the address of the buffer in our space.
798 LEN is the number of bytes.
799
800 Returns number of bytes transferred, or 0 for error. */
801
802 static int
803 remote_write_bytes (memaddr, myaddr, len)
804 CORE_ADDR memaddr;
805 unsigned char *myaddr;
806 int len;
807 {
808 char buf[PBUFSIZ];
809 int i;
810 char *p;
811
812 sprintf (buf, "M%x,%x:", memaddr, len);
813
814 /* We send target system values byte by byte, in increasing byte addresses,
815 each byte encoded as two hex characters. */
816
817 p = buf + strlen (buf);
818 for (i = 0; i < len; i++)
819 {
820 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
821 *p++ = tohex (myaddr[i] & 0xf);
822 }
823 *p = '\0';
824
825 putpkt (buf);
826 getpkt (buf, 0);
827
828 if (buf[0] == 'E')
829 {
830 /* There is no correspondance between what the remote protocol uses
831 for errors and errno codes. We would like a cleaner way of
832 representing errors (big enough to include errno codes, bfd_error
833 codes, and others). But for now just return EIO. */
834 errno = EIO;
835 return 0;
836 }
837 return len;
838 }
839
840 /* Read memory data directly from the remote machine.
841 This does not use the data cache; the data cache uses this.
842 MEMADDR is the address in the remote memory space.
843 MYADDR is the address of the buffer in our space.
844 LEN is the number of bytes.
845
846 Returns number of bytes transferred, or 0 for error. */
847
848 static int
849 remote_read_bytes (memaddr, myaddr, len)
850 CORE_ADDR memaddr;
851 unsigned char *myaddr;
852 int len;
853 {
854 char buf[PBUFSIZ];
855 int i;
856 char *p;
857
858 if (len > PBUFSIZ / 2 - 1)
859 abort ();
860
861 sprintf (buf, "m%x,%x", memaddr, len);
862 putpkt (buf);
863 getpkt (buf, 0);
864
865 if (buf[0] == 'E')
866 {
867 /* There is no correspondance between what the remote protocol uses
868 for errors and errno codes. We would like a cleaner way of
869 representing errors (big enough to include errno codes, bfd_error
870 codes, and others). But for now just return EIO. */
871 errno = EIO;
872 return 0;
873 }
874
875 /* Reply describes memory byte by byte,
876 each byte encoded as two hex characters. */
877
878 p = buf;
879 for (i = 0; i < len; i++)
880 {
881 if (p[0] == 0 || p[1] == 0)
882 /* Reply is short. This means that we were able to read only part
883 of what we wanted to. */
884 break;
885 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
886 p += 2;
887 }
888 return i;
889 }
890 \f
891 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
892 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
893 nonzero. Returns length of data written or read; 0 for error. */
894
895 /* ARGSUSED */
896 static int
897 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
898 CORE_ADDR memaddr;
899 char *myaddr;
900 int len;
901 int should_write;
902 struct target_ops *target; /* ignored */
903 {
904 int xfersize;
905 int bytes_xferred;
906 int total_xferred = 0;
907
908 while (len > 0)
909 {
910 if (len > MAXBUFBYTES)
911 xfersize = MAXBUFBYTES;
912 else
913 xfersize = len;
914
915 if (should_write)
916 bytes_xferred = remote_write_bytes (memaddr,
917 (unsigned char *)myaddr, xfersize);
918 else
919 bytes_xferred = remote_read_bytes (memaddr,
920 (unsigned char *)myaddr, xfersize);
921
922 /* If we get an error, we are done xferring. */
923 if (bytes_xferred == 0)
924 break;
925
926 memaddr += bytes_xferred;
927 myaddr += bytes_xferred;
928 len -= bytes_xferred;
929 total_xferred += bytes_xferred;
930 }
931 return total_xferred;
932 }
933
934 #if 0
935 /* Enable after 4.12. */
936
937 void
938 remote_search (len, data, mask, startaddr, increment, lorange, hirange
939 addr_found, data_found)
940 int len;
941 char *data;
942 char *mask;
943 CORE_ADDR startaddr;
944 int increment;
945 CORE_ADDR lorange;
946 CORE_ADDR hirange;
947 CORE_ADDR *addr_found;
948 char *data_found;
949 {
950 if (increment == -4 && len == 4)
951 {
952 long mask_long, data_long;
953 long data_found_long;
954 CORE_ADDR addr_we_found;
955 char buf[PBUFSIZ];
956 long returned_long[2];
957 char *p;
958
959 mask_long = extract_unsigned_integer (mask, len);
960 data_long = extract_unsigned_integer (data, len);
961 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
962 putpkt (buf);
963 getpkt (buf, 0);
964 if (buf[0] == '\0')
965 {
966 /* The stub doesn't support the 't' request. We might want to
967 remember this fact, but on the other hand the stub could be
968 switched on us. Maybe we should remember it only until
969 the next "target remote". */
970 generic_search (len, data, mask, startaddr, increment, lorange,
971 hirange, addr_found, data_found);
972 return;
973 }
974
975 if (buf[0] == 'E')
976 /* There is no correspondance between what the remote protocol uses
977 for errors and errno codes. We would like a cleaner way of
978 representing errors (big enough to include errno codes, bfd_error
979 codes, and others). But for now just use EIO. */
980 memory_error (EIO, startaddr);
981 p = buf;
982 addr_we_found = 0;
983 while (*p != '\0' && *p != ',')
984 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
985 if (*p == '\0')
986 error ("Protocol error: short return for search");
987
988 data_found_long = 0;
989 while (*p != '\0' && *p != ',')
990 data_found_long = (data_found_long << 4) + fromhex (*p++);
991 /* Ignore anything after this comma, for future extensions. */
992
993 if (addr_we_found < lorange || addr_we_found >= hirange)
994 {
995 *addr_found = 0;
996 return;
997 }
998
999 *addr_found = addr_we_found;
1000 *data_found = store_unsigned_integer (data_we_found, len);
1001 return;
1002 }
1003 generic_search (len, data, mask, startaddr, increment, lorange,
1004 hirange, addr_found, data_found);
1005 }
1006 #endif /* 0 */
1007 \f
1008 static void
1009 remote_files_info (ignore)
1010 struct target_ops *ignore;
1011 {
1012 puts_filtered ("Debugging a target over a serial line.\n");
1013 }
1014 \f
1015 /* Stuff for dealing with the packets which are part of this protocol.
1016 See comment at top of file for details. */
1017
1018 /* Read a single character from the remote end, masking it down to 7 bits. */
1019
1020 static int
1021 readchar ()
1022 {
1023 int ch;
1024
1025 ch = SERIAL_READCHAR (remote_desc, timeout);
1026
1027 if (ch < 0)
1028 return ch;
1029
1030 return ch & 0x7f;
1031 }
1032
1033 /* Send the command in BUF to the remote machine,
1034 and read the reply into BUF.
1035 Report an error if we get an error reply. */
1036
1037 static void
1038 remote_send (buf)
1039 char *buf;
1040 {
1041
1042 putpkt (buf);
1043 getpkt (buf, 0);
1044
1045 if (buf[0] == 'E')
1046 error ("Remote failure reply: %s", buf);
1047 }
1048
1049 /* Send a packet to the remote machine, with error checking.
1050 The data of the packet is in BUF. */
1051
1052 static void
1053 putpkt (buf)
1054 char *buf;
1055 {
1056 int i;
1057 unsigned char csum = 0;
1058 char buf2[PBUFSIZ];
1059 int cnt = strlen (buf);
1060 int ch;
1061 char *p;
1062
1063 /* Copy the packet into buffer BUF2, encapsulating it
1064 and giving it a checksum. */
1065
1066 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
1067 abort();
1068
1069 p = buf2;
1070 *p++ = '$';
1071
1072 for (i = 0; i < cnt; i++)
1073 {
1074 csum += buf[i];
1075 *p++ = buf[i];
1076 }
1077 *p++ = '#';
1078 *p++ = tohex ((csum >> 4) & 0xf);
1079 *p++ = tohex (csum & 0xf);
1080
1081 /* Send it over and over until we get a positive ack. */
1082
1083 while (1)
1084 {
1085 if (remote_debug)
1086 {
1087 *p = '\0';
1088 printf_unfiltered ("Sending packet: %s...", buf2); gdb_flush(gdb_stdout);
1089 }
1090 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1091 perror_with_name ("putpkt: write failed");
1092
1093 /* read until either a timeout occurs (-2) or '+' is read */
1094 while (1)
1095 {
1096 ch = readchar ();
1097
1098 switch (ch)
1099 {
1100 case '+':
1101 if (remote_debug)
1102 printf_unfiltered("Ack\n");
1103 return;
1104 case SERIAL_TIMEOUT:
1105 break; /* Retransmit buffer */
1106 case SERIAL_ERROR:
1107 perror_with_name ("putpkt: couldn't read ACK");
1108 case SERIAL_EOF:
1109 error ("putpkt: EOF while trying to read ACK");
1110 default:
1111 if (remote_debug)
1112 printf_unfiltered ("%02X %c ", ch&0xFF, ch);
1113 continue;
1114 }
1115 break; /* Here to retransmit */
1116 }
1117
1118 #if 0
1119 /* This is wrong. If doing a long backtrace, the user should be
1120 able to get out next time we call QUIT, without anything as violent
1121 as interrupt_query. If we want to provide a way out of here
1122 without getting to the next QUIT, it should be based on hitting
1123 ^C twice as in remote_wait. */
1124 if (quit_flag)
1125 {
1126 quit_flag = 0;
1127 interrupt_query ();
1128 }
1129 #endif
1130 }
1131 }
1132
1133 /* Read a packet from the remote machine, with error checking,
1134 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1135 If FOREVER, wait forever rather than timing out; this is used
1136 while the target is executing user code. */
1137
1138 static void
1139 getpkt (retbuf, forever)
1140 char *retbuf;
1141 int forever;
1142 {
1143 char *bp;
1144 unsigned char csum;
1145 int c = 0;
1146 unsigned char c1, c2;
1147 int retries = 0;
1148 char buf[PBUFSIZ];
1149
1150 #define MAX_RETRIES 10
1151
1152 while (1)
1153 {
1154 #if 0
1155 /* This is wrong. If doing a long backtrace, the user should be
1156 able to get out time next we call QUIT, without anything as violent
1157 as interrupt_query. If we want to provide a way out of here
1158 without getting to the next QUIT, it should be based on hitting
1159 ^C twice as in remote_wait. */
1160 if (quit_flag)
1161 {
1162 quit_flag = 0;
1163 interrupt_query ();
1164 }
1165 #endif
1166
1167 /* This can loop forever if the remote side sends us characters
1168 continuously, but if it pauses, we'll get a zero from readchar
1169 because of timeout. Then we'll count that as a retry. */
1170
1171 c = readchar();
1172 if (c > 0 && c != '$')
1173 continue;
1174
1175 if (c == SERIAL_TIMEOUT)
1176 {
1177 if (forever)
1178 continue;
1179 if (++retries >= MAX_RETRIES)
1180 if (remote_debug) puts_filtered ("Timed out.\n");
1181 goto out;
1182 }
1183
1184 if (c == SERIAL_EOF)
1185 error ("Remote connection closed");
1186 if (c == SERIAL_ERROR)
1187 perror_with_name ("Remote communication error");
1188
1189 /* Force csum to be zero here because of possible error retry. */
1190 csum = 0;
1191 bp = buf;
1192
1193 while (1)
1194 {
1195 c = readchar ();
1196 if (c == SERIAL_TIMEOUT)
1197 {
1198 if (remote_debug)
1199 puts_filtered ("Timeout in mid-packet, retrying\n");
1200 goto whole; /* Start a new packet, count retries */
1201 }
1202 if (c == '$')
1203 {
1204 if (remote_debug)
1205 puts_filtered ("Saw new packet start in middle of old one\n");
1206 goto whole; /* Start a new packet, count retries */
1207 }
1208 if (c == '#')
1209 break;
1210 if (bp >= buf+PBUFSIZ-1)
1211 {
1212 *bp = '\0';
1213 puts_filtered ("Remote packet too long: ");
1214 puts_filtered (buf);
1215 puts_filtered ("\n");
1216 goto whole;
1217 }
1218 *bp++ = c;
1219 csum += c;
1220 }
1221 *bp = 0;
1222
1223 c1 = fromhex (readchar ());
1224 c2 = fromhex (readchar ());
1225 if ((csum & 0xff) == (c1 << 4) + c2)
1226 break;
1227 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1228 (c1 << 4) + c2, csum & 0xff);
1229 puts_filtered (buf);
1230 puts_filtered ("\n");
1231
1232 /* Try the whole thing again. */
1233 whole:
1234 if (++retries < MAX_RETRIES)
1235 {
1236 SERIAL_WRITE (remote_desc, "-", 1);
1237 }
1238 else
1239 {
1240 printf_unfiltered ("Ignoring packet error, continuing...\n");
1241 break;
1242 }
1243 }
1244
1245 /* Deal with run-length encoding. */
1246 {
1247 char *src = buf;
1248 char *dest = retbuf;
1249 int i;
1250 int repeat;
1251 do {
1252 if (*src == '*')
1253 {
1254 if (src[1] == '\0' || src[2] == '\0')
1255 {
1256 if (remote_debug)
1257 puts_filtered ("Packet too short, retrying\n");
1258 goto whole;
1259 }
1260 repeat = (fromhex (src[1]) << 4) + fromhex (src[2]);
1261 for (i = 0; i < repeat; ++i)
1262 {
1263 *dest++ = src[-1];
1264 }
1265 src += 2;
1266 }
1267 else
1268 {
1269 *dest++ = *src;
1270 }
1271 } while (*src++ != '\0');
1272 }
1273
1274 out:
1275 SERIAL_WRITE (remote_desc, "+", 1);
1276
1277 if (remote_debug)
1278 fprintf_unfiltered (gdb_stderr,"Packet received: %s\n", buf);
1279 }
1280 \f
1281 static void
1282 remote_kill ()
1283 {
1284 putpkt ("k");
1285 /* Don't wait for it to die. I'm not really sure it matters whether
1286 we do or not. For the existing stubs, kill is a noop. */
1287 target_mourn_inferior ();
1288 }
1289
1290 static void
1291 remote_mourn ()
1292 {
1293 unpush_target (&remote_ops);
1294 generic_mourn_inferior ();
1295 }
1296 \f
1297 #ifdef REMOTE_BREAKPOINT
1298
1299 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1300 than other targets. */
1301 static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1302
1303 /* Check that it fits in BREAKPOINT_MAX bytes. */
1304 static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1305
1306 #else /* No REMOTE_BREAKPOINT. */
1307
1308 /* Same old breakpoint instruction. This code does nothing different
1309 than mem-break.c. */
1310 static unsigned char break_insn[] = BREAKPOINT;
1311
1312 #endif /* No REMOTE_BREAKPOINT. */
1313
1314 /* Insert a breakpoint on targets that don't have any better breakpoint
1315 support. We read the contents of the target location and stash it,
1316 then overwrite it with a breakpoint instruction. ADDR is the target
1317 location in the target machine. CONTENTS_CACHE is a pointer to
1318 memory allocated for saving the target contents. It is guaranteed
1319 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1320 is accomplished via BREAKPOINT_MAX). */
1321
1322 static int
1323 remote_insert_breakpoint (addr, contents_cache)
1324 CORE_ADDR addr;
1325 char *contents_cache;
1326 {
1327 int val;
1328
1329 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1330
1331 if (val == 0)
1332 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1333
1334 return val;
1335 }
1336
1337 static int
1338 remote_remove_breakpoint (addr, contents_cache)
1339 CORE_ADDR addr;
1340 char *contents_cache;
1341 {
1342 return target_write_memory (addr, contents_cache, sizeof break_insn);
1343 }
1344 \f
1345 /* Define the target subroutine names */
1346
1347 struct target_ops remote_ops = {
1348 "remote", /* to_shortname */
1349 "Remote serial target in gdb-specific protocol", /* to_longname */
1350 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1351 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1352 remote_open, /* to_open */
1353 remote_close, /* to_close */
1354 NULL, /* to_attach */
1355 remote_detach, /* to_detach */
1356 remote_resume, /* to_resume */
1357 remote_wait, /* to_wait */
1358 remote_fetch_registers, /* to_fetch_registers */
1359 remote_store_registers, /* to_store_registers */
1360 remote_prepare_to_store, /* to_prepare_to_store */
1361 remote_xfer_memory, /* to_xfer_memory */
1362 remote_files_info, /* to_files_info */
1363
1364 remote_insert_breakpoint, /* to_insert_breakpoint */
1365 remote_remove_breakpoint, /* to_remove_breakpoint */
1366
1367 NULL, /* to_terminal_init */
1368 NULL, /* to_terminal_inferior */
1369 NULL, /* to_terminal_ours_for_output */
1370 NULL, /* to_terminal_ours */
1371 NULL, /* to_terminal_info */
1372 remote_kill, /* to_kill */
1373 generic_load, /* to_load */
1374 NULL, /* to_lookup_symbol */
1375 NULL, /* to_create_inferior */
1376 remote_mourn, /* to_mourn_inferior */
1377 0, /* to_can_run */
1378 0, /* to_notice_signals */
1379 process_stratum, /* to_stratum */
1380 NULL, /* to_next */
1381 1, /* to_has_all_memory */
1382 1, /* to_has_memory */
1383 1, /* to_has_stack */
1384 1, /* to_has_registers */
1385 1, /* to_has_execution */
1386 NULL, /* sections */
1387 NULL, /* sections_end */
1388 OPS_MAGIC /* to_magic */
1389 };
1390 #endif /* Use remote. */
1391
1392 void
1393 _initialize_remote ()
1394 {
1395 #if !defined(DONT_USE_REMOTE)
1396 add_target (&remote_ops);
1397 #endif
1398 }
This page took 0.058379 seconds and 3 git commands to generate.