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