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