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