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