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