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