Detect the absence of a symbol hash table.
[deliverable/binutils-gdb.git] / gdb / remote.c
1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 91, 92, 93, 94, 95, 96, 97, 1998
3 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* Remote communication protocol.
22
23 A debug packet whose contents are <data>
24 is encapsulated for transmission in the form:
25
26 $ <data> # CSUM1 CSUM2
27
28 <data> must be ASCII alphanumeric and cannot include characters
29 '$' or '#'. If <data> starts with two characters followed by
30 ':', then the existing stubs interpret this as a sequence number.
31
32 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
33 checksum of <data>, the most significant nibble is sent first.
34 the hex digits 0-9,a-f are used.
35
36 Receiver responds with:
37
38 + - if CSUM is correct and ready for next packet
39 - - if CSUM is incorrect
40
41 <data> is as follows:
42 Most values are encoded in ascii hex digits. Signal numbers are according
43 to the numbering in target.h.
44
45 Request Packet
46
47 set thread Hct... Set thread for subsequent operations.
48 c = 'c' for thread used in step and
49 continue; t... can be -1 for all
50 threads.
51 c = 'g' for thread used in other
52 operations. If zero, pick a thread,
53 any thread.
54 reply OK for success
55 ENN for an error.
56
57 read registers g
58 reply XX....X Each byte of register data
59 is described by two hex digits.
60 Registers are in the internal order
61 for GDB, and the bytes in a register
62 are in the same order the machine uses.
63 or ENN for an error.
64
65 write regs GXX..XX Each byte of register data
66 is described by two hex digits.
67 reply OK for success
68 ENN for an error
69
70 write reg Pn...=r... Write register n... with value r...,
71 which contains two hex digits for each
72 byte in the register (target byte
73 order).
74 reply OK for success
75 ENN for an error
76 (not supported by all stubs).
77
78 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
79 reply XX..XX XX..XX is mem contents
80 Can be fewer bytes than requested
81 if able to read only part of the data.
82 or ENN NN is errno
83
84 write mem MAA..AA,LLLL:XX..XX
85 AA..AA is address,
86 LLLL is number of bytes,
87 XX..XX is data
88 reply OK for success
89 ENN for an error (this includes the case
90 where only part of the data was
91 written).
92
93 continue cAA..AA AA..AA is address to resume
94 If AA..AA is omitted,
95 resume at same address.
96
97 step sAA..AA AA..AA is address to resume
98 If AA..AA is omitted,
99 resume at same address.
100
101 continue with Csig;AA..AA Continue with signal sig (hex signal
102 signal number). If ;AA..AA is omitted, resume
103 at same address.
104
105 step with Ssig;AA..AA Like 'C' but step not continue.
106 signal
107
108 last signal ? Reply the current reason for stopping.
109 This is the same reply as is generated
110 for step or cont : SAA where AA is the
111 signal number.
112
113 detach D Reply OK.
114
115 There is no immediate reply to step or cont.
116 The reply comes when the machine stops.
117 It is SAA AA is the signal number.
118
119 or... TAAn...:r...;n...:r...;n...:r...;
120 AA = signal number
121 n... = register number (hex)
122 r... = register contents
123 n... = `thread'
124 r... = thread process ID. This is
125 a hex integer.
126 n... = other string not starting
127 with valid hex digit.
128 gdb should ignore this n,r pair
129 and go on to the next. This way
130 we can extend the protocol.
131 or... WAA The process exited, and AA is
132 the exit status. This is only
133 applicable for certains sorts of
134 targets.
135 or... XAA The process terminated with signal
136 AA.
137 or... OXX..XX XX..XX is hex encoding of ASCII data. This
138 can happen at any time while the program is
139 running and the debugger should
140 continue to wait for 'W', 'T', etc.
141
142 thread alive TXX Find out if the thread XX is alive.
143 reply OK thread is still alive
144 ENN thread is dead
145
146 remote restart RXX Restart the remote server
147
148 extended ops ! Use the extended remote protocol.
149 Sticky -- only needs to be set once.
150
151 kill request k
152
153 toggle debug d toggle debug flag (see 386 & 68k stubs)
154 reset r reset -- see sparc stub.
155 reserved <other> On other requests, the stub should
156 ignore the request and send an empty
157 response ($#<checksum>). This way
158 we can extend the protocol and GDB
159 can tell whether the stub it is
160 talking to uses the old or the new.
161 search tAA:PP,MM Search backwards starting at address
162 AA for a match with pattern PP and
163 mask MM. PP and MM are 4 bytes.
164 Not supported by all stubs.
165
166 general query qXXXX Request info about XXXX.
167 general set QXXXX=yyyy Set value of XXXX to yyyy.
168 query sect offs qOffsets Get section offsets. Reply is
169 Text=xxx;Data=yyy;Bss=zzz
170
171 Responses can be run-length encoded to save space. A '*' means that
172 the next character is an ASCII encoding giving a repeat count which
173 stands for that many repititions of the character preceding the '*'.
174 The encoding is n+29, yielding a printable character where n >=3
175 (which is where rle starts to win). Don't use an n > 126.
176
177 So
178 "0* " means the same as "0000". */
179
180 #include "defs.h"
181 #include "gdb_string.h"
182 #include <fcntl.h>
183 #include "frame.h"
184 #include "inferior.h"
185 #include "bfd.h"
186 #include "symfile.h"
187 #include "target.h"
188 #include "wait.h"
189 /*#include "terminal.h"*/
190 #include "gdbcmd.h"
191 #include "objfiles.h"
192 #include "gdb-stabs.h"
193 #include "gdbthread.h"
194
195 #include "dcache.h"
196
197 #ifdef USG
198 #include <sys/types.h>
199 #endif
200
201 #include <signal.h>
202 #include "serial.h"
203
204 /* Prototypes for local functions */
205
206 static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
207 char *myaddr, int len));
208
209 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
210 char *myaddr, int len));
211
212 static void remote_files_info PARAMS ((struct target_ops *ignore));
213
214 static int remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr,
215 int len, int should_write,
216 struct target_ops *target));
217
218 static void remote_prepare_to_store PARAMS ((void));
219
220 static void remote_fetch_registers PARAMS ((int regno));
221
222 static void remote_resume PARAMS ((int pid, int step,
223 enum target_signal siggnal));
224
225 static int remote_start_remote PARAMS ((char *dummy));
226
227 static void remote_open PARAMS ((char *name, int from_tty));
228
229 static void extended_remote_open PARAMS ((char *name, int from_tty));
230
231 static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p));
232
233 static void remote_close PARAMS ((int quitting));
234
235 static void remote_store_registers PARAMS ((int regno));
236
237 static void remote_mourn PARAMS ((void));
238
239 static void extended_remote_restart PARAMS ((void));
240
241 static void extended_remote_mourn PARAMS ((void));
242
243 static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
244
245 static void remote_mourn_1 PARAMS ((struct target_ops *));
246
247 static void remote_send PARAMS ((char *buf));
248
249 static int readchar PARAMS ((int timeout));
250
251 static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
252
253 static void remote_kill PARAMS ((void));
254
255 static int tohex PARAMS ((int nib));
256
257 static void remote_detach PARAMS ((char *args, int from_tty));
258
259 static void remote_interrupt PARAMS ((int signo));
260
261 static void interrupt_query PARAMS ((void));
262
263 static void set_thread PARAMS ((int, int));
264
265 static int remote_thread_alive PARAMS ((int));
266
267 static void get_offsets PARAMS ((void));
268
269 static int read_frame PARAMS ((char *));
270
271 static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
272
273 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
274
275 static int hexnumlen PARAMS ((ULONGEST num));
276
277 static void init_remote_ops PARAMS ((void));
278
279 static void init_extended_remote_ops PARAMS ((void));
280
281 static void remote_stop PARAMS ((void));
282
283 static int hexnumstr PARAMS ((char *, ULONGEST));
284
285 static CORE_ADDR remote_address_masked PARAMS ((CORE_ADDR));
286
287 static void print_packet PARAMS ((char *));
288
289 static unsigned long crc32 PARAMS ((unsigned char *, int, unsigned int));
290
291 static void compare_sections_command PARAMS ((char *, int));
292
293 static void packet_command PARAMS ((char *, int));
294
295 /* exported functions */
296
297 extern int fromhex PARAMS ((int a));
298
299 extern void getpkt PARAMS ((char *buf, int forever));
300
301 extern int putpkt PARAMS ((char *buf));
302
303 void remote_console_output PARAMS ((char *));
304
305 void open_remote_target PARAMS ((char *, int, struct target_ops *, int));
306
307 void _initialize_remote PARAMS ((void));
308
309 /* */
310
311 static struct target_ops remote_ops;
312
313 static struct target_ops extended_remote_ops;
314
315 /* This was 5 seconds, which is a long time to sit and wait.
316 Unless this is going though some terminal server or multiplexer or
317 other form of hairy serial connection, I would think 2 seconds would
318 be plenty. */
319
320 /* Changed to allow option to set timeout value.
321 was static int remote_timeout = 2; */
322 extern int remote_timeout;
323
324 /* This variable chooses whether to send a ^C or a break when the user
325 requests program interruption. Although ^C is usually what remote
326 systems expect, and that is the default here, sometimes a break is
327 preferable instead. */
328
329 static int remote_break;
330
331 /* Has the user attempted to interrupt the target? If so, then offer
332 the user the opportunity to bail out completely if he interrupts
333 again. */
334 static int interrupted_already = 0;
335
336 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
337 remote_open knows that we don't have a file open when the program
338 starts. */
339 static serial_t remote_desc = NULL;
340
341 /* Having this larger than 400 causes us to be incompatible with m68k-stub.c
342 and i386-stub.c. Normally, no one would notice because it only matters
343 for writing large chunks of memory (e.g. in downloads). Also, this needs
344 to be more than 400 if required to hold the registers (see below, where
345 we round it up based on REGISTER_BYTES). */
346 #define PBUFSIZ 400
347
348 /* Maximum number of bytes to read/write at once. The value here
349 is chosen to fill up a packet (the headers account for the 32). */
350 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
351
352 /* Round up PBUFSIZ to hold all the registers, at least. */
353 /* The blank line after the #if seems to be required to work around a
354 bug in HP's PA compiler. */
355 #if REGISTER_BYTES > MAXBUFBYTES
356
357 #undef PBUFSIZ
358 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
359 #endif
360
361 /* This variable sets the number of bytes to be written to the target
362 in a single packet. Normally PBUFSIZ is satisfactory, but some
363 targets need smaller values (perhaps because the receiving end
364 is slow). */
365
366 static int remote_write_size = PBUFSIZ;
367
368 /* This variable sets the number of bits in an address that are to be
369 sent in a memory ("M" or "m") packet. Normally, after stripping
370 leading zeros, the entire address would be sent. This variable
371 restricts the address to REMOTE_ADDRESS_SIZE bits. HISTORY: The
372 initial implementation of remote.c restricted the address sent in
373 memory packets to ``host::sizeof long'' bytes - (typically 32
374 bits). Consequently, for 64 bit targets, the upper 32 bits of an
375 address was never sent. Since fixing this bug may cause a break in
376 some remote targets this variable is principly provided to
377 facilitate backward compatibility. */
378
379 static int remote_address_size;
380
381 /* This is the size (in chars) of the first response to the `g' command. This
382 is used to limit the size of the memory read and write commands to prevent
383 stub buffers from overflowing. The size does not include headers and
384 trailers, it is only the payload size. */
385
386 static int remote_register_buf_size = 0;
387
388 /* Should we try the 'P' request? If this is set to one when the stub
389 doesn't support 'P', the only consequence is some unnecessary traffic. */
390 static int stub_supports_P = 1;
391
392 /* These are pointers to hook functions that may be set in order to
393 modify resume/wait behavior for a particular architecture. */
394
395 void (*target_resume_hook) PARAMS ((void));
396 void (*target_wait_loop_hook) PARAMS ((void));
397
398 \f
399 /* These are the threads which we last sent to the remote system. -1 for all
400 or -2 for not sent yet. */
401 int general_thread;
402 int cont_thread;
403
404 static void
405 set_thread (th, gen)
406 int th;
407 int gen;
408 {
409 char buf[PBUFSIZ];
410 int state = gen ? general_thread : cont_thread;
411 if (state == th)
412 return;
413 buf[0] = 'H';
414 buf[1] = gen ? 'g' : 'c';
415 if (th == 42000)
416 {
417 buf[2] = '0';
418 buf[3] = '\0';
419 }
420 else if (th < 0)
421 sprintf (&buf[2], "-%x", -th);
422 else
423 sprintf (&buf[2], "%x", th);
424 putpkt (buf);
425 getpkt (buf, 0);
426 if (gen)
427 general_thread = th;
428 else
429 cont_thread = th;
430 }
431 \f
432 /* Return nonzero if the thread TH is still alive on the remote system. */
433
434 static int
435 remote_thread_alive (th)
436 int th;
437 {
438 char buf[PBUFSIZ];
439
440 buf[0] = 'T';
441 if (th < 0)
442 sprintf (&buf[1], "-%x", -th);
443 else
444 sprintf (&buf[1], "%x", th);
445 putpkt (buf);
446 getpkt (buf, 0);
447 return (buf[0] == 'O' && buf[1] == 'K');
448 }
449
450 /* Restart the remote side; this is an extended protocol operation. */
451
452 static void
453 extended_remote_restart ()
454 {
455 char buf[PBUFSIZ];
456
457 /* Send the restart command; for reasons I don't understand the
458 remote side really expects a number after the "R". */
459 buf[0] = 'R';
460 sprintf (&buf[1], "%x", 0);
461 putpkt (buf);
462
463 /* Now query for status so this looks just like we restarted
464 gdbserver from scratch. */
465 putpkt ("?");
466 getpkt (buf, 0);
467 }
468 \f
469 /* Clean up connection to a remote debugger. */
470
471 /* ARGSUSED */
472 static void
473 remote_close (quitting)
474 int quitting;
475 {
476 if (remote_desc)
477 SERIAL_CLOSE (remote_desc);
478 remote_desc = NULL;
479 }
480
481 /* Query the remote side for the text, data and bss offsets. */
482
483 static void
484 get_offsets ()
485 {
486 char buf[PBUFSIZ], *ptr;
487 int lose;
488 CORE_ADDR text_addr, data_addr, bss_addr;
489 struct section_offsets *offs;
490
491 putpkt ("qOffsets");
492
493 getpkt (buf, 0);
494
495 if (buf[0] == '\000')
496 return; /* Return silently. Stub doesn't support this
497 command. */
498 if (buf[0] == 'E')
499 {
500 warning ("Remote failure reply: %s", buf);
501 return;
502 }
503
504 /* Pick up each field in turn. This used to be done with scanf, but
505 scanf will make trouble if CORE_ADDR size doesn't match
506 conversion directives correctly. The following code will work
507 with any size of CORE_ADDR. */
508 text_addr = data_addr = bss_addr = 0;
509 ptr = buf;
510 lose = 0;
511
512 if (strncmp (ptr, "Text=", 5) == 0)
513 {
514 ptr += 5;
515 /* Don't use strtol, could lose on big values. */
516 while (*ptr && *ptr != ';')
517 text_addr = (text_addr << 4) + fromhex (*ptr++);
518 }
519 else
520 lose = 1;
521
522 if (!lose && strncmp (ptr, ";Data=", 6) == 0)
523 {
524 ptr += 6;
525 while (*ptr && *ptr != ';')
526 data_addr = (data_addr << 4) + fromhex (*ptr++);
527 }
528 else
529 lose = 1;
530
531 if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
532 {
533 ptr += 5;
534 while (*ptr && *ptr != ';')
535 bss_addr = (bss_addr << 4) + fromhex (*ptr++);
536 }
537 else
538 lose = 1;
539
540 if (lose)
541 error ("Malformed response to offset query, %s", buf);
542
543 if (symfile_objfile == NULL)
544 return;
545
546 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
547 + symfile_objfile->num_sections
548 * sizeof (offs->offsets));
549 memcpy (offs, symfile_objfile->section_offsets,
550 sizeof (struct section_offsets)
551 + symfile_objfile->num_sections
552 * sizeof (offs->offsets));
553
554 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
555
556 /* This is a temporary kludge to force data and bss to use the same offsets
557 because that's what nlmconv does now. The real solution requires changes
558 to the stub and remote.c that I don't have time to do right now. */
559
560 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
561 ANOFFSET (offs, SECT_OFF_BSS) = data_addr;
562
563 objfile_relocate (symfile_objfile, offs);
564 }
565
566 /* Stub for catch_errors. */
567
568 static int
569 remote_start_remote (dummy)
570 char *dummy;
571 {
572 immediate_quit = 1; /* Allow user to interrupt it */
573
574 /* Ack any packet which the remote side has already sent. */
575 SERIAL_WRITE (remote_desc, "+", 1);
576
577 /* Let the stub know that we want it to return the thread. */
578 set_thread (-1, 0);
579
580 get_offsets (); /* Get text, data & bss offsets */
581
582 putpkt ("?"); /* initiate a query from remote machine */
583 immediate_quit = 0;
584
585 start_remote (); /* Initialize gdb process mechanisms */
586 return 1;
587 }
588
589 /* Open a connection to a remote debugger.
590 NAME is the filename used for communication. */
591
592 static void
593 remote_open (name, from_tty)
594 char *name;
595 int from_tty;
596 {
597 remote_open_1 (name, from_tty, &remote_ops, 0);
598 }
599
600 /* Open a connection to a remote debugger using the extended
601 remote gdb protocol. NAME is the filename used for communication. */
602
603 static void
604 extended_remote_open (name, from_tty)
605 char *name;
606 int from_tty;
607 {
608 remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/);
609 }
610
611 /* Generic code for opening a connection to a remote target. */
612 static DCACHE *remote_dcache;
613
614 static void
615 remote_open_1 (name, from_tty, target, extended_p)
616 char *name;
617 int from_tty;
618 struct target_ops *target;
619 int extended_p;
620 {
621 if (name == 0)
622 error ("To open a remote debug connection, you need to specify what serial\n\
623 device is attached to the remote system (e.g. /dev/ttya).");
624
625 target_preopen (from_tty);
626
627 unpush_target (target);
628
629 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
630
631 remote_desc = SERIAL_OPEN (name);
632 if (!remote_desc)
633 perror_with_name (name);
634
635 if (baud_rate != -1)
636 {
637 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
638 {
639 SERIAL_CLOSE (remote_desc);
640 perror_with_name (name);
641 }
642 }
643
644
645 SERIAL_RAW (remote_desc);
646
647 /* If there is something sitting in the buffer we might take it as a
648 response to a command, which would be bad. */
649 SERIAL_FLUSH_INPUT (remote_desc);
650
651 if (from_tty)
652 {
653 puts_filtered ("Remote debugging using ");
654 puts_filtered (name);
655 puts_filtered ("\n");
656 }
657 push_target (target); /* Switch to using remote target now */
658
659 /* Start out by trying the 'P' request to set registers. We set this each
660 time that we open a new target so that if the user switches from one
661 stub to another, we can (if the target is closed and reopened) cope. */
662 stub_supports_P = 1;
663
664 general_thread = -2;
665 cont_thread = -2;
666
667 /* Without this, some commands which require an active target (such as kill)
668 won't work. This variable serves (at least) double duty as both the pid
669 of the target process (if it has such), and as a flag indicating that a
670 target is active. These functions should be split out into seperate
671 variables, especially since GDB will someday have a notion of debugging
672 several processes. */
673
674 inferior_pid = 42000;
675 /* Start the remote connection; if error (0), discard this target.
676 In particular, if the user quits, be sure to discard it
677 (we'd be in an inconsistent state otherwise). */
678 if (!catch_errors (remote_start_remote, (char *)0,
679 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
680 {
681 pop_target();
682 return;
683 }
684
685 if (extended_p)
686 {
687 /* tell the remote that we're using the extended protocol. */
688 char buf[PBUFSIZ];
689 putpkt ("!");
690 getpkt (buf, 0);
691 }
692 }
693
694 /* This takes a program previously attached to and detaches it. After
695 this is done, GDB can be used to debug some other program. We
696 better not have left any breakpoints in the target program or it'll
697 die when it hits one. */
698
699 static void
700 remote_detach (args, from_tty)
701 char *args;
702 int from_tty;
703 {
704 char buf[PBUFSIZ];
705
706 if (args)
707 error ("Argument given to \"detach\" when remotely debugging.");
708
709 /* Tell the remote target to detach. */
710 strcpy (buf, "D");
711 remote_send (buf);
712
713 pop_target ();
714 if (from_tty)
715 puts_filtered ("Ending remote debugging.\n");
716 }
717
718 /* Convert hex digit A to a number. */
719
720 int
721 fromhex (a)
722 int a;
723 {
724 if (a >= '0' && a <= '9')
725 return a - '0';
726 else if (a >= 'a' && a <= 'f')
727 return a - 'a' + 10;
728 else if (a >= 'A' && a <= 'F')
729 return a - 'A' + 10;
730 else
731 error ("Reply contains invalid hex digit %d", a);
732 }
733
734 /* Convert number NIB to a hex digit. */
735
736 static int
737 tohex (nib)
738 int nib;
739 {
740 if (nib < 10)
741 return '0'+nib;
742 else
743 return 'a'+nib-10;
744 }
745 \f
746 /* Tell the remote machine to resume. */
747
748 static enum target_signal last_sent_signal = TARGET_SIGNAL_0;
749 int last_sent_step;
750
751 static void
752 remote_resume (pid, step, siggnal)
753 int pid, step;
754 enum target_signal siggnal;
755 {
756 char buf[PBUFSIZ];
757
758 if (pid == -1)
759 set_thread (inferior_pid, 0);
760 else
761 set_thread (pid, 0);
762
763 dcache_flush (remote_dcache);
764
765 last_sent_signal = siggnal;
766 last_sent_step = step;
767
768 /* A hook for when we need to do something at the last moment before
769 resumption. */
770 if (target_resume_hook)
771 (*target_resume_hook) ();
772
773 if (siggnal != TARGET_SIGNAL_0)
774 {
775 buf[0] = step ? 'S' : 'C';
776 buf[1] = tohex (((int)siggnal >> 4) & 0xf);
777 buf[2] = tohex ((int)siggnal & 0xf);
778 buf[3] = '\0';
779 }
780 else
781 strcpy (buf, step ? "s": "c");
782
783 putpkt (buf);
784 }
785 \f
786 /* Send ^C to target to halt it. Target will respond, and send us a
787 packet. */
788 static void (*ofunc) PARAMS ((int));
789
790 static void
791 remote_interrupt (signo)
792 int signo;
793 {
794 remote_stop ();
795 signal (signo, remote_interrupt);
796 }
797
798 static void
799 remote_stop ()
800 {
801 if (!interrupted_already)
802 {
803 /* Send a break or a ^C, depending on user preference. */
804 interrupted_already = 1;
805
806 if (remote_debug)
807 printf_unfiltered ("remote_stop called\n");
808
809 if (remote_break)
810 SERIAL_SEND_BREAK (remote_desc);
811 else
812 SERIAL_WRITE (remote_desc, "\003", 1);
813 }
814 else
815 {
816 signal (SIGINT, ofunc);
817 interrupt_query ();
818 signal (SIGINT, remote_interrupt);
819 interrupted_already = 0;
820 }
821 }
822
823 /* Ask the user what to do when an interrupt is received. */
824
825 static void
826 interrupt_query ()
827 {
828 target_terminal_ours ();
829
830 if (query ("Interrupted while waiting for the program.\n\
831 Give up (and stop debugging it)? "))
832 {
833 target_mourn_inferior ();
834 return_to_top_level (RETURN_QUIT);
835 }
836
837 target_terminal_inferior ();
838 }
839
840 /* If nonzero, ignore the next kill. */
841 int kill_kludge;
842
843 void
844 remote_console_output (msg)
845 char *msg;
846 {
847 char *p;
848
849 for (p = msg; *p; p +=2)
850 {
851 char tb[2];
852 char c = fromhex (p[0]) * 16 + fromhex (p[1]);
853 tb[0] = c;
854 tb[1] = 0;
855 if (target_output_hook)
856 target_output_hook (tb);
857 else
858 fputs_filtered (tb, gdb_stdout);
859 }
860 }
861
862 /* Wait until the remote machine stops, then return,
863 storing status in STATUS just as `wait' would.
864 Returns "pid" (though it's not clear what, if anything, that
865 means in the case of this target). */
866
867 static int
868 remote_wait (pid, status)
869 int pid;
870 struct target_waitstatus *status;
871 {
872 unsigned char buf[PBUFSIZ];
873 int thread_num = -1;
874
875 status->kind = TARGET_WAITKIND_EXITED;
876 status->value.integer = 0;
877
878 while (1)
879 {
880 unsigned char *p;
881
882 interrupted_already = 0;
883 ofunc = signal (SIGINT, remote_interrupt);
884 getpkt ((char *) buf, 1);
885 signal (SIGINT, ofunc);
886
887 /* This is a hook for when we need to do something (perhaps the
888 collection of trace data) every time the target stops. */
889 if (target_wait_loop_hook)
890 (*target_wait_loop_hook) ();
891
892 switch (buf[0])
893 {
894 case 'E': /* Error of some sort */
895 warning ("Remote failure reply: %s", buf);
896 continue;
897 case 'T': /* Status with PC, SP, FP, ... */
898 {
899 int i;
900 long regno;
901 char regs[MAX_REGISTER_RAW_SIZE];
902
903 /* Expedited reply, containing Signal, {regno, reg} repeat */
904 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
905 ss = signal number
906 n... = register number
907 r... = register contents
908 */
909 p = &buf[3]; /* after Txx */
910
911 while (*p)
912 {
913 unsigned char *p1;
914 char *p_temp;
915
916 regno = strtol ((const char *) p, &p_temp, 16); /* Read the register number */
917 p1 = (unsigned char *)p_temp;
918
919 if (p1 == p)
920 {
921 p1 = (unsigned char *) strchr ((const char *) p, ':');
922 if (p1 == NULL)
923 warning ("Malformed packet (missing colon): %s\n\
924 Packet: '%s'\n",
925 p, buf);
926 if (strncmp ((const char *) p, "thread", p1 - p) == 0)
927 {
928 thread_num = strtol ((const char *) ++p1, &p_temp, 16);
929 p = (unsigned char *)p_temp;
930 }
931 }
932 else
933 {
934 p = p1;
935
936 if (*p++ != ':')
937 warning ("Malformed packet (missing colon): %s\n\
938 Packet: '%s'\n",
939 p, buf);
940
941 if (regno >= NUM_REGS)
942 warning ("Remote sent bad register number %ld: %s\n\
943 Packet: '%s'\n",
944 regno, p, buf);
945
946 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
947 {
948 if (p[0] == 0 || p[1] == 0)
949 warning ("Remote reply is too short: %s", buf);
950 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
951 p += 2;
952 }
953 supply_register (regno, regs);
954 }
955
956 if (*p++ != ';')
957 warning ("Remote register badly formatted: %s", buf);
958 }
959 }
960 /* fall through */
961 case 'S': /* Old style status, just signal only */
962 status->kind = TARGET_WAITKIND_STOPPED;
963 status->value.sig = (enum target_signal)
964 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
965
966 goto got_status;
967 case 'W': /* Target exited */
968 {
969 /* The remote process exited. */
970 status->kind = TARGET_WAITKIND_EXITED;
971 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
972 goto got_status;
973 }
974 case 'X':
975 status->kind = TARGET_WAITKIND_SIGNALLED;
976 status->value.sig = (enum target_signal)
977 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
978 kill_kludge = 1;
979
980 goto got_status;
981 case 'O': /* Console output */
982 remote_console_output (buf + 1);
983 continue;
984 case '\0':
985 if (last_sent_signal != TARGET_SIGNAL_0)
986 {
987 /* Zero length reply means that we tried 'S' or 'C' and
988 the remote system doesn't support it. */
989 target_terminal_ours_for_output ();
990 printf_filtered
991 ("Can't send signals to this remote system. %s not sent.\n",
992 target_signal_to_name (last_sent_signal));
993 last_sent_signal = TARGET_SIGNAL_0;
994 target_terminal_inferior ();
995
996 strcpy ((char *) buf, last_sent_step ? "s" : "c");
997 putpkt ((char *) buf);
998 continue;
999 }
1000 /* else fallthrough */
1001 default:
1002 warning ("Invalid remote reply: %s", buf);
1003 continue;
1004 }
1005 }
1006 got_status:
1007 if (thread_num != -1)
1008 {
1009 /* Initial thread value can only be acquired via wait, so deal with
1010 this marker which is used before the first thread value is
1011 acquired. */
1012 if (inferior_pid == 42000)
1013 {
1014 inferior_pid = thread_num;
1015 add_thread (inferior_pid);
1016 }
1017 return thread_num;
1018 }
1019 return inferior_pid;
1020 }
1021
1022 /* Number of bytes of registers this stub implements. */
1023 static int register_bytes_found;
1024
1025 /* Read the remote registers into the block REGS. */
1026 /* Currently we just read all the registers, so we don't use regno. */
1027 /* ARGSUSED */
1028 static void
1029 remote_fetch_registers (regno)
1030 int regno;
1031 {
1032 char buf[PBUFSIZ];
1033 int i;
1034 char *p;
1035 char regs[REGISTER_BYTES];
1036
1037 set_thread (inferior_pid, 1);
1038
1039 sprintf (buf, "g");
1040 remote_send (buf);
1041
1042 if (remote_register_buf_size == 0)
1043 remote_register_buf_size = strlen (buf);
1044
1045 /* Unimplemented registers read as all bits zero. */
1046 memset (regs, 0, REGISTER_BYTES);
1047
1048 /* We can get out of synch in various cases. If the first character
1049 in the buffer is not a hex character, assume that has happened
1050 and try to fetch another packet to read. */
1051 while ((buf[0] < '0' || buf[0] > '9')
1052 && (buf[0] < 'a' || buf[0] > 'f')
1053 && buf[0] != 'x') /* New: unavailable register value */
1054 {
1055 if (remote_debug)
1056 printf_unfiltered ("Bad register packet; fetching a new packet\n");
1057 getpkt (buf, 0);
1058 }
1059
1060 /* Reply describes registers byte by byte, each byte encoded as two
1061 hex characters. Suck them all up, then supply them to the
1062 register cacheing/storage mechanism. */
1063
1064 p = buf;
1065 for (i = 0; i < REGISTER_BYTES; i++)
1066 {
1067 if (p[0] == 0)
1068 break;
1069 if (p[1] == 0)
1070 {
1071 warning ("Remote reply is of odd length: %s", buf);
1072 /* Don't change register_bytes_found in this case, and don't
1073 print a second warning. */
1074 goto supply_them;
1075 }
1076 if (p[0] == 'x' && p[1] == 'x')
1077 regs[i] = 0; /* 'x' */
1078 else
1079 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1080 p += 2;
1081 }
1082
1083 if (i != register_bytes_found)
1084 {
1085 register_bytes_found = i;
1086 #ifdef REGISTER_BYTES_OK
1087 if (!REGISTER_BYTES_OK (i))
1088 warning ("Remote reply is too short: %s", buf);
1089 #endif
1090 }
1091
1092 supply_them:
1093 for (i = 0; i < NUM_REGS; i++)
1094 {
1095 supply_register (i, &regs[REGISTER_BYTE(i)]);
1096 if (buf[REGISTER_BYTE(i) * 2] == 'x')
1097 register_valid[i] = -1; /* register value not available */
1098 }
1099 }
1100
1101 /* Prepare to store registers. Since we may send them all (using a
1102 'G' request), we have to read out the ones we don't want to change
1103 first. */
1104
1105 static void
1106 remote_prepare_to_store ()
1107 {
1108 /* Make sure the entire registers array is valid. */
1109 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
1110 }
1111
1112 /* Store register REGNO, or all registers if REGNO == -1, from the contents
1113 of REGISTERS. FIXME: ignores errors. */
1114
1115 static void
1116 remote_store_registers (regno)
1117 int regno;
1118 {
1119 char buf[PBUFSIZ];
1120 int i;
1121 char *p;
1122
1123 set_thread (inferior_pid, 1);
1124
1125 if (regno >= 0 && stub_supports_P)
1126 {
1127 /* Try storing a single register. */
1128 char *regp;
1129
1130 sprintf (buf, "P%x=", regno);
1131 p = buf + strlen (buf);
1132 regp = &registers[REGISTER_BYTE (regno)];
1133 for (i = 0; i < REGISTER_RAW_SIZE (regno); ++i)
1134 {
1135 *p++ = tohex ((regp[i] >> 4) & 0xf);
1136 *p++ = tohex (regp[i] & 0xf);
1137 }
1138 *p = '\0';
1139 remote_send (buf);
1140 if (buf[0] != '\0')
1141 {
1142 /* The stub understands the 'P' request. We are done. */
1143 return;
1144 }
1145
1146 /* The stub does not support the 'P' request. Use 'G' instead,
1147 and don't try using 'P' in the future (it will just waste our
1148 time). */
1149 stub_supports_P = 0;
1150 }
1151
1152 buf[0] = 'G';
1153
1154 /* Command describes registers byte by byte,
1155 each byte encoded as two hex characters. */
1156
1157 p = buf + 1;
1158 /* remote_prepare_to_store insures that register_bytes_found gets set. */
1159 for (i = 0; i < register_bytes_found; i++)
1160 {
1161 *p++ = tohex ((registers[i] >> 4) & 0xf);
1162 *p++ = tohex (registers[i] & 0xf);
1163 }
1164 *p = '\0';
1165
1166 remote_send (buf);
1167 }
1168
1169 /*
1170 Use of the data cache *used* to be disabled because it loses for looking at
1171 and changing hardware I/O ports and the like. Accepting `volatile'
1172 would perhaps be one way to fix it. Another idea would be to use the
1173 executable file for the text segment (for all SEC_CODE sections?
1174 For all SEC_READONLY sections?). This has problems if you want to
1175 actually see what the memory contains (e.g. self-modifying code,
1176 clobbered memory, user downloaded the wrong thing).
1177
1178 Because it speeds so much up, it's now enabled, if you're playing
1179 with registers you turn it of (set remotecache 0)
1180 */
1181
1182 /* Read a word from remote address ADDR and return it.
1183 This goes through the data cache. */
1184
1185 #if 0 /* unused? */
1186 static int
1187 remote_fetch_word (addr)
1188 CORE_ADDR addr;
1189 {
1190 return dcache_fetch (remote_dcache, addr);
1191 }
1192
1193 /* Write a word WORD into remote address ADDR.
1194 This goes through the data cache. */
1195
1196 static void
1197 remote_store_word (addr, word)
1198 CORE_ADDR addr;
1199 int word;
1200 {
1201 dcache_poke (remote_dcache, addr, word);
1202 }
1203 #endif /* 0 (unused?) */
1204
1205 \f
1206
1207 /* Return the number of hex digits in num. */
1208
1209 static int
1210 hexnumlen (num)
1211 ULONGEST num;
1212 {
1213 int i;
1214
1215 for (i = 0; num != 0; i++)
1216 num >>= 4;
1217
1218 return max (i, 1);
1219 }
1220
1221 /* Set BUF to the hex digits representing NUM */
1222
1223 static int
1224 hexnumstr (buf, num)
1225 char *buf;
1226 ULONGEST num;
1227 {
1228 int i;
1229 int len = hexnumlen (num);
1230
1231 buf[len] = '\0';
1232
1233 for (i = len - 1; i >= 0; i--)
1234 {
1235 buf[i] = "0123456789abcdef" [(num & 0xf)];
1236 num >>= 4;
1237 }
1238
1239 return len;
1240 }
1241
1242 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits */
1243
1244 static CORE_ADDR
1245 remote_address_masked (addr)
1246 CORE_ADDR addr;
1247 {
1248 if (remote_address_size > 0
1249 && remote_address_size < (sizeof (ULONGEST) * 8))
1250 {
1251 /* Only create a mask when that mask can safely be constructed
1252 in a ULONGEST variable. */
1253 ULONGEST mask = 1;
1254 mask = (mask << remote_address_size) - 1;
1255 addr &= mask;
1256 }
1257 return addr;
1258 }
1259
1260 /* Write memory data directly to the remote machine.
1261 This does not inform the data cache; the data cache uses this.
1262 MEMADDR is the address in the remote memory space.
1263 MYADDR is the address of the buffer in our space.
1264 LEN is the number of bytes.
1265
1266 Returns number of bytes transferred, or 0 for error. */
1267
1268 static int
1269 remote_write_bytes (memaddr, myaddr, len)
1270 CORE_ADDR memaddr;
1271 char *myaddr;
1272 int len;
1273 {
1274 int max_buf_size; /* Max size of packet output buffer */
1275 int origlen;
1276
1277 /* Chop the transfer down if necessary */
1278
1279 max_buf_size = min (remote_write_size, PBUFSIZ);
1280 if (remote_register_buf_size != 0)
1281 max_buf_size = min (max_buf_size, remote_register_buf_size);
1282
1283 /* Subtract header overhead from max payload size - $M<memaddr>,<len>:#nn */
1284 max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
1285
1286 origlen = len;
1287 while (len > 0)
1288 {
1289 char buf[PBUFSIZ];
1290 char *p;
1291 int todo;
1292 int i;
1293
1294 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
1295
1296 /* construct "M"<memaddr>","<len>":" */
1297 /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
1298 memaddr = remote_address_masked (memaddr);
1299 p = buf;
1300 *p++ = 'M';
1301 p += hexnumstr (p, (ULONGEST) memaddr);
1302 *p++ = ',';
1303 p += hexnumstr (p, (ULONGEST) todo);
1304 *p++ = ':';
1305 *p = '\0';
1306
1307 /* We send target system values byte by byte, in increasing byte addresses,
1308 each byte encoded as two hex characters. */
1309
1310 for (i = 0; i < todo; i++)
1311 {
1312 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
1313 *p++ = tohex (myaddr[i] & 0xf);
1314 }
1315 *p = '\0';
1316
1317 putpkt (buf);
1318 getpkt (buf, 0);
1319
1320 if (buf[0] == 'E')
1321 {
1322 /* There is no correspondance between what the remote protocol uses
1323 for errors and errno codes. We would like a cleaner way of
1324 representing errors (big enough to include errno codes, bfd_error
1325 codes, and others). But for now just return EIO. */
1326 errno = EIO;
1327 return 0;
1328 }
1329 myaddr += todo;
1330 memaddr += todo;
1331 len -= todo;
1332 }
1333 return origlen;
1334 }
1335
1336 /* Read memory data directly from the remote machine.
1337 This does not use the data cache; the data cache uses this.
1338 MEMADDR is the address in the remote memory space.
1339 MYADDR is the address of the buffer in our space.
1340 LEN is the number of bytes.
1341
1342 Returns number of bytes transferred, or 0 for error. */
1343
1344 static int
1345 remote_read_bytes (memaddr, myaddr, len)
1346 CORE_ADDR memaddr;
1347 char *myaddr;
1348 int len;
1349 {
1350 int max_buf_size; /* Max size of packet output buffer */
1351 int origlen;
1352
1353 /* Chop the transfer down if necessary */
1354
1355 max_buf_size = min (remote_write_size, PBUFSIZ);
1356 if (remote_register_buf_size != 0)
1357 max_buf_size = min (max_buf_size, remote_register_buf_size);
1358
1359 origlen = len;
1360 while (len > 0)
1361 {
1362 char buf[PBUFSIZ];
1363 char *p;
1364 int todo;
1365 int i;
1366
1367 todo = min (len, max_buf_size / 2); /* num bytes that will fit */
1368
1369 /* construct "m"<memaddr>","<len>" */
1370 /* sprintf (buf, "m%lx,%x", (unsigned long) memaddr, todo); */
1371 memaddr = remote_address_masked (memaddr);
1372 p = buf;
1373 *p++ = 'm';
1374 p += hexnumstr (p, (ULONGEST) memaddr);
1375 *p++ = ',';
1376 p += hexnumstr (p, (ULONGEST) todo);
1377 *p = '\0';
1378
1379 putpkt (buf);
1380 getpkt (buf, 0);
1381
1382 if (buf[0] == 'E')
1383 {
1384 /* There is no correspondance between what the remote protocol uses
1385 for errors and errno codes. We would like a cleaner way of
1386 representing errors (big enough to include errno codes, bfd_error
1387 codes, and others). But for now just return EIO. */
1388 errno = EIO;
1389 return 0;
1390 }
1391
1392 /* Reply describes memory byte by byte,
1393 each byte encoded as two hex characters. */
1394
1395 p = buf;
1396 for (i = 0; i < todo; i++)
1397 {
1398 if (p[0] == 0 || p[1] == 0)
1399 /* Reply is short. This means that we were able to read only part
1400 of what we wanted to. */
1401 return i + (origlen - len);
1402 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
1403 p += 2;
1404 }
1405 myaddr += todo;
1406 memaddr += todo;
1407 len -= todo;
1408 }
1409 return origlen;
1410 }
1411 \f
1412 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
1413 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
1414 nonzero. Returns length of data written or read; 0 for error. */
1415
1416 /* ARGSUSED */
1417 static int
1418 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
1419 CORE_ADDR memaddr;
1420 char *myaddr;
1421 int len;
1422 int should_write;
1423 struct target_ops *target; /* ignored */
1424 {
1425 #ifdef REMOTE_TRANSLATE_XFER_ADDRESS
1426 CORE_ADDR targaddr;
1427 int targlen;
1428 REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen);
1429 if (targlen == 0)
1430 return 0;
1431 memaddr = targaddr;
1432 len = targlen;
1433 #endif
1434
1435 return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
1436 }
1437
1438
1439 #if 0
1440 /* Enable after 4.12. */
1441
1442 void
1443 remote_search (len, data, mask, startaddr, increment, lorange, hirange
1444 addr_found, data_found)
1445 int len;
1446 char *data;
1447 char *mask;
1448 CORE_ADDR startaddr;
1449 int increment;
1450 CORE_ADDR lorange;
1451 CORE_ADDR hirange;
1452 CORE_ADDR *addr_found;
1453 char *data_found;
1454 {
1455 if (increment == -4 && len == 4)
1456 {
1457 long mask_long, data_long;
1458 long data_found_long;
1459 CORE_ADDR addr_we_found;
1460 char buf[PBUFSIZ];
1461 long returned_long[2];
1462 char *p;
1463
1464 mask_long = extract_unsigned_integer (mask, len);
1465 data_long = extract_unsigned_integer (data, len);
1466 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
1467 putpkt (buf);
1468 getpkt (buf, 0);
1469 if (buf[0] == '\0')
1470 {
1471 /* The stub doesn't support the 't' request. We might want to
1472 remember this fact, but on the other hand the stub could be
1473 switched on us. Maybe we should remember it only until
1474 the next "target remote". */
1475 generic_search (len, data, mask, startaddr, increment, lorange,
1476 hirange, addr_found, data_found);
1477 return;
1478 }
1479
1480 if (buf[0] == 'E')
1481 /* There is no correspondance between what the remote protocol uses
1482 for errors and errno codes. We would like a cleaner way of
1483 representing errors (big enough to include errno codes, bfd_error
1484 codes, and others). But for now just use EIO. */
1485 memory_error (EIO, startaddr);
1486 p = buf;
1487 addr_we_found = 0;
1488 while (*p != '\0' && *p != ',')
1489 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
1490 if (*p == '\0')
1491 error ("Protocol error: short return for search");
1492
1493 data_found_long = 0;
1494 while (*p != '\0' && *p != ',')
1495 data_found_long = (data_found_long << 4) + fromhex (*p++);
1496 /* Ignore anything after this comma, for future extensions. */
1497
1498 if (addr_we_found < lorange || addr_we_found >= hirange)
1499 {
1500 *addr_found = 0;
1501 return;
1502 }
1503
1504 *addr_found = addr_we_found;
1505 *data_found = store_unsigned_integer (data_we_found, len);
1506 return;
1507 }
1508 generic_search (len, data, mask, startaddr, increment, lorange,
1509 hirange, addr_found, data_found);
1510 }
1511 #endif /* 0 */
1512 \f
1513 static void
1514 remote_files_info (ignore)
1515 struct target_ops *ignore;
1516 {
1517 puts_filtered ("Debugging a target over a serial line.\n");
1518 }
1519 \f
1520 /* Stuff for dealing with the packets which are part of this protocol.
1521 See comment at top of file for details. */
1522
1523 /* Read a single character from the remote end, masking it down to 7 bits. */
1524
1525 static int
1526 readchar (timeout)
1527 int timeout;
1528 {
1529 int ch;
1530
1531 ch = SERIAL_READCHAR (remote_desc, timeout);
1532
1533 switch (ch)
1534 {
1535 case SERIAL_EOF:
1536 error ("Remote connection closed");
1537 case SERIAL_ERROR:
1538 perror_with_name ("Remote communication error");
1539 case SERIAL_TIMEOUT:
1540 return ch;
1541 default:
1542 return ch & 0x7f;
1543 }
1544 }
1545
1546 /* Send the command in BUF to the remote machine,
1547 and read the reply into BUF.
1548 Report an error if we get an error reply. */
1549
1550 static void
1551 remote_send (buf)
1552 char *buf;
1553 {
1554 putpkt (buf);
1555 getpkt (buf, 0);
1556
1557 if (buf[0] == 'E')
1558 error ("Remote failure reply: %s", buf);
1559 }
1560
1561 /* Display a null-terminated packet on stdout, for debugging, using C
1562 string notation. */
1563
1564 static void
1565 print_packet (buf)
1566 char *buf;
1567 {
1568 puts_filtered ("\"");
1569 while (*buf)
1570 gdb_printchar (*buf++, gdb_stdout, '"');
1571 puts_filtered ("\"");
1572 }
1573
1574
1575 /* Send a packet to the remote machine, with error checking.
1576 The data of the packet is in BUF. */
1577
1578 int
1579 putpkt (buf)
1580 char *buf;
1581 {
1582 int i;
1583 unsigned char csum = 0;
1584 char buf2[PBUFSIZ];
1585 int cnt = strlen (buf);
1586 int ch;
1587 int tcount = 0;
1588 char *p;
1589
1590 /* Copy the packet into buffer BUF2, encapsulating it
1591 and giving it a checksum. */
1592
1593 if (cnt > (int) sizeof (buf2) - 5) /* Prosanity check */
1594 abort();
1595
1596 p = buf2;
1597 *p++ = '$';
1598
1599 for (i = 0; i < cnt; i++)
1600 {
1601 csum += buf[i];
1602 *p++ = buf[i];
1603 }
1604 *p++ = '#';
1605 *p++ = tohex ((csum >> 4) & 0xf);
1606 *p++ = tohex (csum & 0xf);
1607
1608 /* Send it over and over until we get a positive ack. */
1609
1610 while (1)
1611 {
1612 int started_error_output = 0;
1613
1614 if (remote_debug)
1615 {
1616 *p = '\0';
1617 printf_unfiltered ("Sending packet: %s...", buf2);
1618 gdb_flush(gdb_stdout);
1619 }
1620 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1621 perror_with_name ("putpkt: write failed");
1622
1623 /* read until either a timeout occurs (-2) or '+' is read */
1624 while (1)
1625 {
1626 ch = readchar (remote_timeout);
1627
1628 if (remote_debug)
1629 {
1630 switch (ch)
1631 {
1632 case '+':
1633 case SERIAL_TIMEOUT:
1634 case '$':
1635 if (started_error_output)
1636 {
1637 putchar_unfiltered ('\n');
1638 started_error_output = 0;
1639 }
1640 }
1641 }
1642
1643 switch (ch)
1644 {
1645 case '+':
1646 if (remote_debug)
1647 printf_unfiltered("Ack\n");
1648 return 1;
1649 case SERIAL_TIMEOUT:
1650 tcount ++;
1651 if (tcount > 3)
1652 return 0;
1653 break; /* Retransmit buffer */
1654 case '$':
1655 {
1656 char junkbuf[PBUFSIZ];
1657
1658 /* It's probably an old response, and we're out of sync. Just
1659 gobble up the packet and ignore it. */
1660 getpkt (junkbuf, 0);
1661 continue; /* Now, go look for + */
1662 }
1663 default:
1664 if (remote_debug)
1665 {
1666 if (!started_error_output)
1667 {
1668 started_error_output = 1;
1669 printf_unfiltered ("putpkt: Junk: ");
1670 }
1671 putchar_unfiltered (ch & 0177);
1672 }
1673 continue;
1674 }
1675 break; /* Here to retransmit */
1676 }
1677
1678 #if 0
1679 /* This is wrong. If doing a long backtrace, the user should be
1680 able to get out next time we call QUIT, without anything as violent
1681 as interrupt_query. If we want to provide a way out of here
1682 without getting to the next QUIT, it should be based on hitting
1683 ^C twice as in remote_wait. */
1684 if (quit_flag)
1685 {
1686 quit_flag = 0;
1687 interrupt_query ();
1688 }
1689 #endif
1690 }
1691 }
1692
1693 /* Come here after finding the start of the frame. Collect the rest into BUF,
1694 verifying the checksum, length, and handling run-length compression.
1695 Returns 0 on any error, 1 on success. */
1696
1697 static int
1698 read_frame (buf)
1699 char *buf;
1700 {
1701 unsigned char csum;
1702 char *bp;
1703 int c;
1704
1705 csum = 0;
1706 bp = buf;
1707
1708 while (1)
1709 {
1710 c = readchar (remote_timeout);
1711
1712 switch (c)
1713 {
1714 case SERIAL_TIMEOUT:
1715 if (remote_debug)
1716 puts_filtered ("Timeout in mid-packet, retrying\n");
1717 return 0;
1718 case '$':
1719 if (remote_debug)
1720 puts_filtered ("Saw new packet start in middle of old one\n");
1721 return 0; /* Start a new packet, count retries */
1722 case '#':
1723 {
1724 unsigned char pktcsum;
1725
1726 *bp = '\000';
1727
1728 pktcsum = fromhex (readchar (remote_timeout)) << 4;
1729 pktcsum |= fromhex (readchar (remote_timeout));
1730
1731 if (csum == pktcsum)
1732 return 1;
1733
1734 if (remote_debug)
1735 {
1736 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1737 pktcsum, csum);
1738 puts_filtered (buf);
1739 puts_filtered ("\n");
1740 }
1741 return 0;
1742 }
1743 case '*': /* Run length encoding */
1744 csum += c;
1745 c = readchar (remote_timeout);
1746 csum += c;
1747 c = c - ' ' + 3; /* Compute repeat count */
1748
1749
1750 if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
1751 {
1752 memset (bp, *(bp - 1), c);
1753 bp += c;
1754 continue;
1755 }
1756
1757 *bp = '\0';
1758 printf_filtered ("Repeat count %d too large for buffer: ", c);
1759 puts_filtered (buf);
1760 puts_filtered ("\n");
1761 return 0;
1762
1763 default:
1764 if (bp < buf + PBUFSIZ - 1)
1765 {
1766 *bp++ = c;
1767 csum += c;
1768 continue;
1769 }
1770
1771 *bp = '\0';
1772 puts_filtered ("Remote packet too long: ");
1773 puts_filtered (buf);
1774 puts_filtered ("\n");
1775
1776 return 0;
1777 }
1778 }
1779 }
1780
1781 /* Read a packet from the remote machine, with error checking,
1782 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1783 If FOREVER, wait forever rather than timing out; this is used
1784 while the target is executing user code. */
1785
1786 void
1787 getpkt (buf, forever)
1788 char *buf;
1789 int forever;
1790 {
1791 int c;
1792 int tries;
1793 int timeout;
1794 int val;
1795
1796 strcpy (buf,"timeout");
1797
1798 if (forever)
1799 {
1800 #ifdef MAINTENANCE_CMDS
1801 timeout = watchdog > 0 ? watchdog : -1;
1802 #else
1803 timeout = -1;
1804 #endif
1805 }
1806
1807 else
1808 timeout = remote_timeout;
1809
1810 #define MAX_TRIES 3
1811
1812 for (tries = 1; tries <= MAX_TRIES; tries++)
1813 {
1814 /* This can loop forever if the remote side sends us characters
1815 continuously, but if it pauses, we'll get a zero from readchar
1816 because of timeout. Then we'll count that as a retry. */
1817
1818 /* Note that we will only wait forever prior to the start of a packet.
1819 After that, we expect characters to arrive at a brisk pace. They
1820 should show up within remote_timeout intervals. */
1821
1822 do
1823 {
1824 c = readchar (timeout);
1825
1826 if (c == SERIAL_TIMEOUT)
1827 {
1828 #ifdef MAINTENANCE_CMDS
1829 if (forever) /* Watchdog went off. Kill the target. */
1830 {
1831 target_mourn_inferior ();
1832 error ("Watchdog has expired. Target detached.\n");
1833 }
1834 #endif
1835 if (remote_debug)
1836 puts_filtered ("Timed out.\n");
1837 goto retry;
1838 }
1839 }
1840 while (c != '$');
1841
1842 /* We've found the start of a packet, now collect the data. */
1843
1844 val = read_frame (buf);
1845
1846 if (val == 1)
1847 {
1848 if (remote_debug)
1849 fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
1850 SERIAL_WRITE (remote_desc, "+", 1);
1851 return;
1852 }
1853
1854 /* Try the whole thing again. */
1855 retry:
1856 SERIAL_WRITE (remote_desc, "-", 1);
1857 }
1858
1859 /* We have tried hard enough, and just can't receive the packet. Give up. */
1860
1861 printf_unfiltered ("Ignoring packet error, continuing...\n");
1862 SERIAL_WRITE (remote_desc, "+", 1);
1863 }
1864 \f
1865 static void
1866 remote_kill ()
1867 {
1868 /* For some mysterious reason, wait_for_inferior calls kill instead of
1869 mourn after it gets TARGET_WAITKIND_SIGNALLED. Work around it. */
1870 if (kill_kludge)
1871 {
1872 kill_kludge = 0;
1873 target_mourn_inferior ();
1874 return;
1875 }
1876
1877 /* Use catch_errors so the user can quit from gdb even when we aren't on
1878 speaking terms with the remote system. */
1879 catch_errors (putpkt, "k", "", RETURN_MASK_ERROR);
1880
1881 /* Don't wait for it to die. I'm not really sure it matters whether
1882 we do or not. For the existing stubs, kill is a noop. */
1883 target_mourn_inferior ();
1884 }
1885
1886 static void
1887 remote_mourn ()
1888 {
1889 remote_mourn_1 (&remote_ops);
1890 }
1891
1892 static void
1893 extended_remote_mourn ()
1894 {
1895 /* We do _not_ want to mourn the target like this; this will
1896 remove the extended remote target from the target stack,
1897 and the next time the user says "run" it'll fail.
1898
1899 FIXME: What is the right thing to do here? */
1900 #if 0
1901 remote_mourn_1 (&extended_remote_ops);
1902 #endif
1903 }
1904
1905 /* Worker function for remote_mourn. */
1906 static void
1907 remote_mourn_1 (target)
1908 struct target_ops *target;
1909 {
1910 unpush_target (target);
1911 generic_mourn_inferior ();
1912 }
1913
1914 /* In the extended protocol we want to be able to do things like
1915 "run" and have them basically work as expected. So we need
1916 a special create_inferior function.
1917
1918 FIXME: One day add support for changing the exec file
1919 we're debugging, arguments and an environment. */
1920
1921 static void
1922 extended_remote_create_inferior (exec_file, args, env)
1923 char *exec_file;
1924 char *args;
1925 char **env;
1926 {
1927 /* Rip out the breakpoints; we'll reinsert them after restarting
1928 the remote server. */
1929 remove_breakpoints ();
1930
1931 /* Now restart the remote server. */
1932 extended_remote_restart ();
1933
1934 /* Now put the breakpoints back in. This way we're safe if the
1935 restart function works via a unix fork on the remote side. */
1936 insert_breakpoints ();
1937
1938 /* Clean up from the last time we were running. */
1939 clear_proceed_status ();
1940
1941 /* Let the remote process run. */
1942 proceed (-1, TARGET_SIGNAL_0, 0);
1943 }
1944
1945 \f
1946 /* On some machines, e.g. 68k, we may use a different breakpoint instruction
1947 than other targets; in those use REMOTE_BREAKPOINT instead of just
1948 BREAKPOINT. Also, bi-endian targets may define LITTLE_REMOTE_BREAKPOINT
1949 and BIG_REMOTE_BREAKPOINT. If none of these are defined, we just call
1950 the standard routines that are in mem-break.c. */
1951
1952 /* FIXME, these ought to be done in a more dynamic fashion. For instance,
1953 the choice of breakpoint instruction affects target program design and
1954 vice versa, and by making it user-tweakable, the special code here
1955 goes away and we need fewer special GDB configurations. */
1956
1957 #if defined (LITTLE_REMOTE_BREAKPOINT) && defined (BIG_REMOTE_BREAKPOINT) && !defined(REMOTE_BREAKPOINT)
1958 #define REMOTE_BREAKPOINT
1959 #endif
1960
1961 #ifdef REMOTE_BREAKPOINT
1962
1963 /* If the target isn't bi-endian, just pretend it is. */
1964 #if !defined (LITTLE_REMOTE_BREAKPOINT) && !defined (BIG_REMOTE_BREAKPOINT)
1965 #define LITTLE_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1966 #define BIG_REMOTE_BREAKPOINT REMOTE_BREAKPOINT
1967 #endif
1968
1969 static unsigned char big_break_insn[] = BIG_REMOTE_BREAKPOINT;
1970 static unsigned char little_break_insn[] = LITTLE_REMOTE_BREAKPOINT;
1971
1972 #endif /* REMOTE_BREAKPOINT */
1973
1974 /* Insert a breakpoint on targets that don't have any better breakpoint
1975 support. We read the contents of the target location and stash it,
1976 then overwrite it with a breakpoint instruction. ADDR is the target
1977 location in the target machine. CONTENTS_CACHE is a pointer to
1978 memory allocated for saving the target contents. It is guaranteed
1979 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1980 is accomplished via BREAKPOINT_MAX). */
1981
1982 static int
1983 remote_insert_breakpoint (addr, contents_cache)
1984 CORE_ADDR addr;
1985 char *contents_cache;
1986 {
1987 #ifdef REMOTE_BREAKPOINT
1988 int val;
1989
1990 val = target_read_memory (addr, contents_cache, sizeof big_break_insn);
1991
1992 if (val == 0)
1993 {
1994 if (TARGET_BYTE_ORDER == BIG_ENDIAN)
1995 val = target_write_memory (addr, (char *) big_break_insn,
1996 sizeof big_break_insn);
1997 else
1998 val = target_write_memory (addr, (char *) little_break_insn,
1999 sizeof little_break_insn);
2000 }
2001
2002 return val;
2003 #else
2004 return memory_insert_breakpoint (addr, contents_cache);
2005 #endif /* REMOTE_BREAKPOINT */
2006 }
2007
2008 static int
2009 remote_remove_breakpoint (addr, contents_cache)
2010 CORE_ADDR addr;
2011 char *contents_cache;
2012 {
2013 #ifdef REMOTE_BREAKPOINT
2014 return target_write_memory (addr, contents_cache, sizeof big_break_insn);
2015 #else
2016 return memory_remove_breakpoint (addr, contents_cache);
2017 #endif /* REMOTE_BREAKPOINT */
2018 }
2019
2020 /* Some targets are only capable of doing downloads, and afterwards they switch
2021 to the remote serial protocol. This function provides a clean way to get
2022 from the download target to the remote target. It's basically just a
2023 wrapper so that we don't have to expose any of the internal workings of
2024 remote.c.
2025
2026 Prior to calling this routine, you should shutdown the current target code,
2027 else you will get the "A program is being debugged already..." message.
2028 Usually a call to pop_target() suffices.
2029 */
2030
2031 void
2032 push_remote_target (name, from_tty)
2033 char *name;
2034 int from_tty;
2035 {
2036 printf_filtered ("Switching to remote protocol\n");
2037 remote_open (name, from_tty);
2038 }
2039
2040 /* Other targets want to use the entire remote serial module but with
2041 certain remote_ops overridden. */
2042
2043 void
2044 open_remote_target (name, from_tty, target, extended_p)
2045 char *name;
2046 int from_tty;
2047 struct target_ops *target;
2048 int extended_p;
2049 {
2050 printf_filtered ("Selecting the %sremote protocol\n",
2051 (extended_p ? "extended-" : ""));
2052 remote_open_1 (name, from_tty, target, extended_p);
2053 }
2054
2055 /* Table used by the crc32 function to calcuate the checksum. */
2056 static unsigned long crc32_table[256] = {0, 0};
2057
2058 static unsigned long
2059 crc32 (buf, len, crc)
2060 unsigned char *buf;
2061 int len;
2062 unsigned int crc;
2063 {
2064 if (! crc32_table[1])
2065 {
2066 /* Initialize the CRC table and the decoding table. */
2067 int i, j;
2068 unsigned int c;
2069
2070 for (i = 0; i < 256; i++)
2071 {
2072 for (c = i << 24, j = 8; j > 0; --j)
2073 c = c & 0x80000000 ? (c << 1) ^ 0x04c11db7 : (c << 1);
2074 crc32_table[i] = c;
2075 }
2076 }
2077
2078 while (len--)
2079 {
2080 crc = (crc << 8) ^ crc32_table[((crc >> 24) ^ *buf) & 255];
2081 buf++;
2082 }
2083 return crc;
2084 }
2085
2086 /* compare-sections command
2087
2088 With no arguments, compares each loadable section in the exec bfd
2089 with the same memory range on the target, and reports mismatches.
2090 Useful for verifying the image on the target against the exec file.
2091 Depends on the target understanding the new "qCRC:" request. */
2092
2093 static void
2094 compare_sections_command (args, from_tty)
2095 char *args;
2096 int from_tty;
2097 {
2098 asection *s;
2099 unsigned long host_crc, target_crc;
2100 extern bfd *exec_bfd;
2101 struct cleanup *old_chain;
2102 char *tmp, *sectdata, *sectname, buf[PBUFSIZ];
2103 bfd_size_type size;
2104 bfd_vma lma;
2105 int matched = 0;
2106 int mismatched = 0;
2107
2108 if (!exec_bfd)
2109 error ("command cannot be used without an exec file");
2110 if (!current_target.to_shortname ||
2111 strcmp (current_target.to_shortname, "remote") != 0)
2112 error ("command can only be used with remote target");
2113
2114 for (s = exec_bfd->sections; s; s = s->next)
2115 {
2116 if (!(s->flags & SEC_LOAD))
2117 continue; /* skip non-loadable section */
2118
2119 size = bfd_get_section_size_before_reloc (s);
2120 if (size == 0)
2121 continue; /* skip zero-length section */
2122
2123 sectname = (char *) bfd_get_section_name (exec_bfd, s);
2124 if (args && strcmp (args, sectname) != 0)
2125 continue; /* not the section selected by user */
2126
2127 matched = 1; /* do this section */
2128 lma = s->lma;
2129 /* FIXME: assumes lma can fit into long */
2130 sprintf (buf, "qCRC:%lx,%lx", (long) lma, (long) size);
2131 putpkt (buf);
2132
2133 /* be clever; compute the host_crc before waiting for target reply */
2134 sectdata = xmalloc (size);
2135 old_chain = make_cleanup (free, sectdata);
2136 bfd_get_section_contents (exec_bfd, s, sectdata, 0, size);
2137 host_crc = crc32 ((unsigned char *) sectdata, size, 0xffffffff);
2138
2139 getpkt (buf, 0);
2140 if (buf[0] == 'E')
2141 error ("target memory fault, section %s, range 0x%08x -- 0x%08x",
2142 sectname, lma, lma + size);
2143 if (buf[0] != 'C')
2144 error ("remote target does not support this operation");
2145
2146 for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
2147 target_crc = target_crc * 16 + fromhex (*tmp);
2148
2149 printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
2150 sectname, lma, lma + size);
2151 if (host_crc == target_crc)
2152 printf_filtered ("matched.\n");
2153 else
2154 {
2155 printf_filtered ("MIS-MATCHED!\n");
2156 mismatched++;
2157 }
2158
2159 do_cleanups (old_chain);
2160 }
2161 if (mismatched > 0)
2162 warning ("One or more sections of the remote executable does not match\nthe loaded file\n");
2163 if (args && !matched)
2164 printf_filtered ("No loaded section named '%s'.\n", args);
2165 }
2166
2167 static void
2168 packet_command (args, from_tty)
2169 char *args;
2170 int from_tty;
2171 {
2172 char buf[PBUFSIZ];
2173
2174 if (! remote_desc)
2175 error ("command can only be used with remote target");
2176
2177 if (! args)
2178 error ("remote-packet command requires packet text as argument");
2179
2180 puts_filtered ("sending: ");
2181 print_packet (args);
2182 puts_filtered ("\n");
2183 putpkt (args);
2184
2185 getpkt (buf, 0);
2186 puts_filtered ("received: ");
2187 print_packet (buf);
2188 puts_filtered ("\n");
2189 }
2190
2191 static void
2192 init_remote_ops ()
2193 {
2194 remote_ops.to_shortname = "remote";
2195 remote_ops.to_longname = "Remote serial target in gdb-specific protocol";
2196 remote_ops.to_doc = "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
2197 Specify the serial device it is connected to (e.g. /dev/ttya).";
2198 remote_ops.to_open = remote_open;
2199 remote_ops.to_close = remote_close;
2200 remote_ops.to_detach = remote_detach;
2201 remote_ops.to_resume = remote_resume;
2202 remote_ops.to_wait = remote_wait;
2203 remote_ops.to_fetch_registers = remote_fetch_registers;
2204 remote_ops.to_store_registers = remote_store_registers;
2205 remote_ops.to_prepare_to_store = remote_prepare_to_store;
2206 remote_ops.to_xfer_memory = remote_xfer_memory;
2207 remote_ops.to_files_info = remote_files_info;
2208 remote_ops.to_insert_breakpoint = remote_insert_breakpoint;
2209 remote_ops.to_remove_breakpoint = remote_remove_breakpoint;
2210 remote_ops.to_kill = remote_kill;
2211 remote_ops.to_load = generic_load;
2212 remote_ops.to_mourn_inferior = remote_mourn;
2213 remote_ops.to_thread_alive = remote_thread_alive;
2214 remote_ops.to_stop = remote_stop;
2215 remote_ops.to_stratum = process_stratum;
2216 remote_ops.to_has_all_memory = 1;
2217 remote_ops.to_has_memory = 1;
2218 remote_ops.to_has_stack = 1;
2219 remote_ops.to_has_registers = 1;
2220 remote_ops.to_has_execution = 1;
2221 remote_ops.to_magic = OPS_MAGIC;
2222 }
2223
2224 static void
2225 init_extended_remote_ops ()
2226 {
2227 extended_remote_ops = remote_ops;
2228
2229 extended_remote_ops.to_shortname = "extended-remote";
2230 extended_remote_ops.to_longname = "Extended remote serial target in gdb-specific protocol";
2231 extended_remote_ops.to_doc = "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
2232 Specify the serial device it is connected to (e.g. /dev/ttya).",
2233 extended_remote_ops.to_open = extended_remote_open;
2234 extended_remote_ops.to_create_inferior = extended_remote_create_inferior;
2235 extended_remote_ops.to_mourn_inferior = extended_remote_mourn;
2236 }
2237
2238 void
2239 _initialize_remote ()
2240 {
2241 init_remote_ops ();
2242 add_target (&remote_ops);
2243
2244 init_extended_remote_ops ();
2245 add_target (&extended_remote_ops);
2246
2247 add_cmd ("compare-sections", class_obscure, compare_sections_command,
2248 "Compare section data on target to the exec file.\n\
2249 Argument is a single section name (default: all loaded sections).",
2250 &cmdlist);
2251
2252 add_cmd ("packet", class_maintenance, packet_command,
2253 "Send an arbitrary packet to a remote target.\n\
2254 maintenance packet TEXT\n\
2255 If GDB is talking to an inferior via the GDB serial protocol, then\n\
2256 this command sends the string TEXT to the inferior, and displays the\n\
2257 response packet. GDB supplies the initial `$' character, and the\n\
2258 terminating `#' character and checksum.",
2259 &maintenancelist);
2260
2261 add_show_from_set (add_set_cmd ("remotetimeout", no_class,
2262 var_integer, (char *)&remote_timeout,
2263 "Set timeout value for remote read.\n", &setlist),
2264 &showlist);
2265
2266 add_show_from_set (add_set_cmd ("remotebreak", no_class,
2267 var_integer, (char *)&remote_break,
2268 "Set whether to send break if interrupted.\n", &setlist),
2269 &showlist);
2270
2271 add_show_from_set (add_set_cmd ("remotewritesize", no_class,
2272 var_integer, (char *)&remote_write_size,
2273 "Set the maximum number of bytes in each memory write packet.\n", &setlist),
2274 &showlist);
2275
2276 remote_address_size = TARGET_PTR_BIT;
2277 add_show_from_set (add_set_cmd ("remoteaddresssize", class_obscure,
2278 var_integer, (char *)&remote_address_size,
2279 "Set the maximum size of the address (in bits) in a memory packet.\n", &setlist),
2280 &showlist);
2281 }
This page took 0.079031 seconds and 4 git commands to generate.