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