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