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