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