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