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