Fix last patch.
[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
28 '$' or '#'
29
30 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
31 checksum of <data>, the most significant nibble is sent first.
32 the hex digits 0-9,a-f are used.
33
34 Receiver responds with:
35
36 + - if CSUM is correct and ready for next packet
37 - - if CSUM is incorrect
38
39 <data> is as follows:
bd5635a1
RP
40 All values are encoded in ascii hex digits.
41
42 Request Packet
43
44 read registers g
45 reply XX....X Each byte of register data
46 is described by two hex digits.
47 Registers are in the internal order
48 for GDB, and the bytes in a register
49 are in the same order the machine uses.
50 or ENN for an error.
51
52 write regs GXX..XX Each byte of register data
53 is described by two hex digits.
54 reply OK for success
55 ENN for an error
56
57 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
58 reply XX..XX XX..XX is mem contents
d538b510
RP
59 Can be fewer bytes than requested
60 if able to read only part of the data.
bd5635a1
RP
61 or ENN NN is errno
62
63 write mem MAA..AA,LLLL:XX..XX
64 AA..AA is address,
65 LLLL is number of bytes,
66 XX..XX is data
67 reply OK for success
d538b510
RP
68 ENN for an error (this includes the case
69 where only part of the data was
70 written).
bd5635a1
RP
71
72 cont cAA..AA AA..AA is address to resume
73 If AA..AA is omitted,
74 resume at same address.
75
76 step sAA..AA AA..AA is address to resume
77 If AA..AA is omitted,
78 resume at same address.
79
80 last signal ? Reply the current reason for stopping.
81 This is the same reply as is generated
82 for step or cont : SAA where AA is the
83 signal number.
84
85 There is no immediate reply to step or cont.
86 The reply comes when the machine stops.
87 It is SAA AA is the "signal number"
88
e50ebec8
JK
89 or... TAAn...:r...;n:r...;n...:r...;
90 AA = signal number
91 n... = register number
92 r... = register contents
72bba93b 93 or... WAA The process exited, and AA is
758aeb93
ILT
94 the exit status. This is only
95 applicable for certains sorts of
96 targets.
d538b510
RP
97 kill request k
98
99 toggle debug d toggle debug flag (see 386 & 68k stubs)
100 reset r reset -- see sparc stub.
101 reserved <other> On other requests, the stub should
102 ignore the request and send an empty
103 response ($#<checksum>). This way
104 we can extend the protocol and GDB
105 can tell whether the stub it is
106 talking to uses the old or the new.
72bba93b
SG
107<<<<<<< remote.c
108 search tAA:PP,MM Search backwards starting at address
109||||||| 1.81
110 search tAA:PP,MM Search backword starting at address
111=======
94d4b713 112 search tAA:PP,MM Search backward starting at address
72bba93b 113>>>>>>> 1.82
94d4b713
JK
114 AA for a match with pattern PP and
115 mask MM. PP and MM are 4 bytes.
116 Not supported by all stubs.
117
72bba93b
SG
118<<<<<<< remote.c
119 general query qXXXX Request info about XXXX.
120 general set QXXXX=yyyy Set value of XXXX to yyyy.
121 query sect offs qOffsets Get section offsets. Reply is
122 Text=xxx;Data=yyy;Bss=zzz
123*/
124
125||||||| 1.81
126=======
94d4b713
JK
127 Responses can be run-length encoded to save space. A '*' means that
128 the next two characters are hex digits giving a repeat count which
129 stands for that many repititions of the character preceding the '*'.
130 Note that this means that responses cannot contain '*'. Example:
131 "0*03" means the same as "0000". */
bd5635a1 132
72bba93b 133>>>>>>> 1.82
d747e0af 134#include "defs.h"
bd5635a1
RP
135#include <string.h>
136#include <fcntl.h>
bd5635a1
RP
137#include "frame.h"
138#include "inferior.h"
e50ebec8 139#include "bfd.h"
6b27ebe8 140#include "symfile.h"
bd5635a1
RP
141#include "target.h"
142#include "wait.h"
143#include "terminal.h"
8f86a4e4 144#include "gdbcmd.h"
758aeb93
ILT
145#include "objfiles.h"
146#include "gdb-stabs.h"
bd5635a1 147
d538b510
RP
148#include "dcache.h"
149
8f86a4e4 150#if !defined(DONT_USE_REMOTE)
bd5635a1
RP
151#ifdef USG
152#include <sys/types.h>
153#endif
154
155#include <signal.h>
ebdb9ade 156#include "serial.h"
bd5635a1 157
b543979c
JG
158/* Prototypes for local functions */
159
d538b510
RP
160static int
161remote_write_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
b543979c 162
d538b510
RP
163static int
164remote_read_bytes PARAMS ((CORE_ADDR memaddr, unsigned char *myaddr, int len));
b543979c
JG
165
166static void
5af4f5f6 167remote_files_info PARAMS ((struct target_ops *ignore));
b543979c
JG
168
169static int
5af4f5f6
JK
170remote_xfer_memory PARAMS ((CORE_ADDR memaddr, char *myaddr, int len,
171 int should_write, struct target_ops *target));
b543979c
JG
172
173static void
174remote_prepare_to_store PARAMS ((void));
175
176static void
5af4f5f6 177remote_fetch_registers PARAMS ((int regno));
b543979c
JG
178
179static void
94d4b713 180remote_resume PARAMS ((int pid, int step, enum target_signal siggnal));
b543979c 181
7c622b41 182static int
5af4f5f6 183remote_start_remote PARAMS ((char *dummy));
7c622b41 184
b543979c 185static void
5af4f5f6 186remote_open PARAMS ((char *name, int from_tty));
b543979c
JG
187
188static void
5af4f5f6 189remote_close PARAMS ((int quitting));
b543979c
JG
190
191static void
5af4f5f6 192remote_store_registers PARAMS ((int regno));
b543979c
JG
193
194static void
5af4f5f6 195getpkt PARAMS ((char *buf, int forever));
b543979c
JG
196
197static void
5af4f5f6 198putpkt PARAMS ((char *buf));
b543979c
JG
199
200static void
5af4f5f6 201remote_send PARAMS ((char *buf));
b543979c
JG
202
203static int
204readchar PARAMS ((void));
205
94d4b713 206static int remote_wait PARAMS ((int pid, struct target_waitstatus *status));
b543979c
JG
207
208static int
5af4f5f6 209tohex PARAMS ((int nib));
b543979c
JG
210
211static int
5af4f5f6 212fromhex PARAMS ((int a));
b543979c
JG
213
214static void
5af4f5f6
JK
215remote_detach PARAMS ((char *args, int from_tty));
216
217static void
218remote_interrupt PARAMS ((int signo));
219
220static void
221remote_interrupt_twice PARAMS ((int signo));
b543979c 222
981a3309
SG
223static void
224interrupt_query PARAMS ((void));
225
bd5635a1
RP
226extern struct target_ops remote_ops; /* Forward decl */
227
ebdb9ade
JK
228/* This was 5 seconds, which is a long time to sit and wait.
229 Unless this is going though some terminal server or multiplexer or
230 other form of hairy serial connection, I would think 2 seconds would
231 be plenty. */
232static int timeout = 2;
bd5635a1
RP
233
234#if 0
235int icache;
236#endif
237
16e1d1d3 238/* Descriptor for I/O to remote machine. Initialize it to NULL so that
bd5635a1
RP
239 remote_open knows that we don't have a file open when the program
240 starts. */
ebdb9ade 241serial_t remote_desc = NULL;
bd5635a1 242
4d57c599
JK
243/* Having this larger than 400 causes us to be incompatible with m68k-stub.c
244 and i386-stub.c. Normally, no one would notice because it only matters
245 for writing large chunks of memory (e.g. in downloads). Also, this needs
246 to be more than 400 if required to hold the registers (see below, where
247 we round it up based on REGISTER_BYTES). */
248#define PBUFSIZ 400
bd5635a1
RP
249
250/* Maximum number of bytes to read/write at once. The value here
251 is chosen to fill up a packet (the headers account for the 32). */
252#define MAXBUFBYTES ((PBUFSIZ-32)/2)
253
b543979c
JG
254/* Round up PBUFSIZ to hold all the registers, at least. */
255#if REGISTER_BYTES > MAXBUFBYTES
256#undef PBUFSIZ
257#define PBUFSIZ (REGISTER_BYTES * 2 + 32)
bd5635a1 258#endif
bd5635a1 259\f
bd5635a1
RP
260/* Clean up connection to a remote debugger. */
261
e1ce8aa5 262/* ARGSUSED */
b543979c 263static void
bd5635a1
RP
264remote_close (quitting)
265 int quitting;
266{
ebdb9ade
JK
267 if (remote_desc)
268 SERIAL_CLOSE (remote_desc);
269 remote_desc = NULL;
b543979c
JG
270}
271
72bba93b
SG
272/* Query the remote side for the text, data and bss offsets. */
273
274static void
275get_offsets ()
276{
277 unsigned char buf [PBUFSIZ];
278 int nvals;
279 CORE_ADDR text_addr, data_addr, bss_addr;
280 struct section_offsets *offs;
281
282 putpkt ("qOffsets");
283
284 getpkt (buf, 1);
285
286 if (buf[0] == 'E')
287 {
288 warning ("Remote failure reply: %s", buf);
289 return;
290 }
291
292 nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
293 &bss_addr);
294 if (nvals != 3)
295 error ("Malformed response to offset query, %s", buf);
296
297 if (symfile_objfile == NULL)
298 return;
299
300 offs = (struct section_offsets *) alloca (sizeof (struct section_offsets)
301 + symfile_objfile->num_sections
302 * sizeof (offs->offsets));
303 memcpy (offs, symfile_objfile->section_offsets,
304 sizeof (struct section_offsets)
305 + symfile_objfile->num_sections
306 * sizeof (offs->offsets));
307
308 ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
309 ANOFFSET (offs, SECT_OFF_DATA) = data_addr;
310 ANOFFSET (offs, SECT_OFF_BSS) = bss_addr;
311
312 objfile_relocate (symfile_objfile, offs);
313}
314
7c622b41
JG
315/* Stub for catch_errors. */
316
317static int
318remote_start_remote (dummy)
319 char *dummy;
320{
ac7a377f
JK
321 immediate_quit = 1; /* Allow user to interrupt it */
322
7c622b41 323 /* Ack any packet which the remote side has already sent. */
72bba93b
SG
324
325 SERIAL_WRITE (remote_desc, "+", 1);
326
327 get_offsets (); /* Get text, data & bss offsets */
328
7c622b41 329 putpkt ("?"); /* initiate a query from remote machine */
ac7a377f 330 immediate_quit = 0;
7c622b41
JG
331
332 start_remote (); /* Initialize gdb process mechanisms */
72bba93b 333
7c622b41
JG
334 return 1;
335}
336
bd5635a1
RP
337/* Open a connection to a remote debugger.
338 NAME is the filename used for communication. */
339
d538b510
RP
340static DCACHE *remote_dcache;
341
b543979c 342static void
bd5635a1
RP
343remote_open (name, from_tty)
344 char *name;
345 int from_tty;
346{
bd5635a1
RP
347 if (name == 0)
348 error (
349"To open a remote debug connection, you need to specify what serial\n\
350device is attached to the remote system (e.g. /dev/ttya).");
351
f2fc6e7a
JK
352 target_preopen (from_tty);
353
ebdb9ade 354 unpush_target (&remote_ops);
bd5635a1 355
d538b510 356 remote_dcache = dcache_init (remote_read_bytes, remote_write_bytes);
bd5635a1 357
ebdb9ade
JK
358 remote_desc = SERIAL_OPEN (name);
359 if (!remote_desc)
bd5635a1
RP
360 perror_with_name (name);
361
94d4b713 362 if (baud_rate != -1)
b543979c 363 {
94d4b713
JK
364 if (SERIAL_SETBAUDRATE (remote_desc, baud_rate))
365 {
366 SERIAL_CLOSE (remote_desc);
367 perror_with_name (name);
368 }
b543979c 369 }
ebdb9ade
JK
370
371 SERIAL_RAW (remote_desc);
bd5635a1 372
e15f2a54
JK
373 /* If there is something sitting in the buffer we might take it as a
374 response to a command, which would be bad. */
375 SERIAL_FLUSH_INPUT (remote_desc);
376
bd5635a1 377 if (from_tty)
7c622b41
JG
378 {
379 puts_filtered ("Remote debugging using ");
380 puts_filtered (name);
381 puts_filtered ("\n");
382 }
bd5635a1 383 push_target (&remote_ops); /* Switch to using remote target now */
bd5635a1 384
ac7a377f
JK
385 /* Start the remote connection; if error (0), discard this target.
386 In particular, if the user quits, be sure to discard it
387 (we'd be in an inconsistent state otherwise). */
7c622b41 388 if (!catch_errors (remote_start_remote, (char *)0,
e50ebec8 389 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
7c622b41 390 pop_target();
bd5635a1
RP
391}
392
393/* remote_detach()
394 takes a program previously attached to and detaches it.
395 We better not have left any breakpoints
396 in the program or it'll die when it hits one.
397 Close the open connection to the remote debugger.
398 Use this when you want to detach and do something else
399 with your gdb. */
400
401static void
402remote_detach (args, from_tty)
403 char *args;
404 int from_tty;
405{
406 if (args)
407 error ("Argument given to \"detach\" when remotely debugging.");
408
409 pop_target ();
410 if (from_tty)
7c622b41 411 puts_filtered ("Ending remote debugging.\n");
bd5635a1
RP
412}
413
414/* Convert hex digit A to a number. */
415
416static int
417fromhex (a)
418 int a;
419{
420 if (a >= '0' && a <= '9')
421 return a - '0';
422 else if (a >= 'a' && a <= 'f')
423 return a - 'a' + 10;
424 else
425 error ("Reply contains invalid hex digit");
426 return -1;
427}
428
429/* Convert number NIB to a hex digit. */
430
431static int
432tohex (nib)
433 int nib;
434{
435 if (nib < 10)
436 return '0'+nib;
437 else
438 return 'a'+nib-10;
439}
440\f
441/* Tell the remote machine to resume. */
442
b543979c 443static void
d538b510 444remote_resume (pid, step, siggnal)
94d4b713
JK
445 int pid, step;
446 enum target_signal siggnal;
bd5635a1
RP
447{
448 char buf[PBUFSIZ];
449
450 if (siggnal)
ebdb9ade
JK
451 {
452 char *name;
453 target_terminal_ours_for_output ();
94d4b713
JK
454 printf_filtered
455 ("Can't send signals to a remote system. %s not sent.\n",
456 target_signal_to_name (siggnal));
ebdb9ade
JK
457 target_terminal_inferior ();
458 }
bd5635a1 459
d538b510 460 dcache_flush (remote_dcache);
bd5635a1
RP
461
462 strcpy (buf, step ? "s": "c");
463
464 putpkt (buf);
465}
ebdb9ade 466\f
b543979c
JG
467/* Send ^C to target to halt it. Target will respond, and send us a
468 packet. */
469
5af4f5f6
JK
470static void
471remote_interrupt (signo)
e676a15f 472 int signo;
b543979c 473{
ebdb9ade
JK
474 /* If this doesn't work, try more severe steps. */
475 signal (signo, remote_interrupt_twice);
8f86a4e4 476
d0d8484a 477 if (remote_debug)
199b2450 478 printf_unfiltered ("remote_interrupt called\n");
8f86a4e4 479
ebdb9ade 480 SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
b543979c
JG
481}
482
5af4f5f6
JK
483static void (*ofunc)();
484
ebdb9ade
JK
485/* The user typed ^C twice. */
486static void
487remote_interrupt_twice (signo)
488 int signo;
489{
490 signal (signo, ofunc);
491
981a3309
SG
492 interrupt_query ();
493
494 signal (signo, remote_interrupt);
495}
496
497/* Ask the user what to do when an interrupt is received. */
498
499static void
500interrupt_query ()
501{
ebdb9ade 502 target_terminal_ours ();
981a3309 503
6b27ebe8 504 if (query ("Interrupted while waiting for the program.\n\
ebdb9ade
JK
505Give up (and stop debugging it)? "))
506 {
507 target_mourn_inferior ();
e50ebec8 508 return_to_top_level (RETURN_QUIT);
ebdb9ade 509 }
981a3309
SG
510
511 target_terminal_inferior ();
ebdb9ade 512}
b543979c 513
bd5635a1 514/* Wait until the remote machine stops, then return,
e1ce8aa5
JK
515 storing status in STATUS just as `wait' would.
516 Returns "pid" (though it's not clear what, if anything, that
517 means in the case of this target). */
bd5635a1 518
b543979c 519static int
d0d8484a
SG
520remote_wait (pid, status)
521 int pid;
94d4b713 522 struct target_waitstatus *status;
bd5635a1
RP
523{
524 unsigned char buf[PBUFSIZ];
8f86a4e4 525
94d4b713
JK
526 status->kind = TARGET_WAITKIND_EXITED;
527 status->value.integer = 0;
b543979c 528
4f8a48e5 529 while (1)
8f86a4e4 530 {
4f8a48e5 531 unsigned char *p;
a03d4f8e 532
4f8a48e5
ILT
533 ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
534 getpkt ((char *) buf, 1);
535 signal (SIGINT, ofunc);
4ecee2f9 536
4f8a48e5
ILT
537 if (buf[0] == 'E')
538 warning ("Remote failure reply: %s", buf);
539 else if (buf[0] == 'T')
8f86a4e4 540 {
4f8a48e5
ILT
541 int i;
542 long regno;
543 char regs[MAX_REGISTER_RAW_SIZE];
a03d4f8e 544
4f8a48e5
ILT
545 /* Expedited reply, containing Signal, {regno, reg} repeat */
546 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
547 ss = signal number
548 n... = register number
549 r... = register contents
550 */
5af4f5f6 551
4f8a48e5 552 p = &buf[3]; /* after Txx */
5af4f5f6 553
4f8a48e5
ILT
554 while (*p)
555 {
556 unsigned char *p1;
5af4f5f6 557
4f8a48e5 558 regno = strtol (p, &p1, 16); /* Read the register number */
5af4f5f6 559
4f8a48e5
ILT
560 if (p1 == p)
561 warning ("Remote sent badly formed register number: %s\nPacket: '%s'\n",
562 p1, buf);
4ecee2f9 563
4f8a48e5 564 p = p1;
4ecee2f9 565
4f8a48e5
ILT
566 if (*p++ != ':')
567 warning ("Malformed packet (missing colon): %s\nPacket: '%s'\n",
568 p, buf);
a03d4f8e 569
4f8a48e5
ILT
570 if (regno >= NUM_REGS)
571 warning ("Remote sent bad register number %d: %s\nPacket: '%s'\n",
572 regno, p, buf);
573
574 for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
575 {
576 if (p[0] == 0 || p[1] == 0)
577 warning ("Remote reply is too short: %s", buf);
578 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
579 p += 2;
580 }
581
582 if (*p++ != ';')
583 warning ("Remote register badly formatted: %s", buf);
584
585 supply_register (regno, regs);
586 }
587 break;
8f86a4e4 588 }
4f8a48e5
ILT
589 else if (buf[0] == 'W')
590 {
591 /* The remote process exited. */
94d4b713
JK
592 status->kind = TARGET_WAITKIND_EXITED;
593 status->value.integer = (fromhex (buf[1]) << 4) + fromhex (buf[2]);
4f8a48e5
ILT
594 return 0;
595 }
596 else if (buf[0] == 'S')
597 break;
598 else
599 warning ("Invalid remote reply: %s", buf);
758aeb93 600 }
8f86a4e4 601
94d4b713
JK
602 status->kind = TARGET_WAITKIND_STOPPED;
603 status->value.sig = (enum target_signal)
604 (((fromhex (buf[1])) << 4) + (fromhex (buf[2])));
8f86a4e4 605
e1ce8aa5 606 return 0;
bd5635a1
RP
607}
608
55fea07b
JK
609/* Number of bytes of registers this stub implements. */
610static int register_bytes_found;
611
bd5635a1 612/* Read the remote registers into the block REGS. */
e1ce8aa5
JK
613/* Currently we just read all the registers, so we don't use regno. */
614/* ARGSUSED */
b543979c 615static void
bd5635a1
RP
616remote_fetch_registers (regno)
617 int regno;
618{
619 char buf[PBUFSIZ];
620 int i;
621 char *p;
622 char regs[REGISTER_BYTES];
623
624 sprintf (buf, "g");
625 remote_send (buf);
626
55fea07b
JK
627 /* Unimplemented registers read as all bits zero. */
628 memset (regs, 0, REGISTER_BYTES);
629
981a3309
SG
630 /* We can get out of synch in various cases. If the first character
631 in the buffer is not a hex character, assume that has happened
632 and try to fetch another packet to read. */
633 while ((buf[0] < '0' || buf[0] > '9')
634 && (buf[0] < 'a' || buf[0] > 'f'))
635 {
d0d8484a 636 if (remote_debug)
199b2450 637 printf_unfiltered ("Bad register packet; fetching a new packet\n");
981a3309
SG
638 getpkt (buf, 0);
639 }
640
bd5635a1
RP
641 /* Reply describes registers byte by byte, each byte encoded as two
642 hex characters. Suck them all up, then supply them to the
643 register cacheing/storage mechanism. */
644
645 p = buf;
646 for (i = 0; i < REGISTER_BYTES; i++)
647 {
55fea07b
JK
648 if (p[0] == 0)
649 break;
650 if (p[1] == 0)
651 {
652 warning ("Remote reply is of odd length: %s", buf);
653 /* Don't change register_bytes_found in this case, and don't
654 print a second warning. */
655 goto supply_them;
656 }
bd5635a1
RP
657 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
658 p += 2;
659 }
55fea07b
JK
660
661 if (i != register_bytes_found)
662 {
663 register_bytes_found = i;
664#ifdef REGISTER_BYTES_OK
665 if (!REGISTER_BYTES_OK (i))
666 warning ("Remote reply is too short: %s", buf);
667#endif
668 }
669
670 supply_them:
bd5635a1
RP
671 for (i = 0; i < NUM_REGS; i++)
672 supply_register (i, &regs[REGISTER_BYTE(i)]);
bd5635a1
RP
673}
674
675/* Prepare to store registers. Since we send them all, we have to
676 read out the ones we don't want to change first. */
677
b543979c 678static void
bd5635a1
RP
679remote_prepare_to_store ()
680{
34517ebc
JG
681 /* Make sure the entire registers array is valid. */
682 read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
bd5635a1
RP
683}
684
685/* Store the remote registers from the contents of the block REGISTERS.
686 FIXME, eventually just store one register if that's all that is needed. */
687
e1ce8aa5 688/* ARGSUSED */
b543979c 689static void
bd5635a1
RP
690remote_store_registers (regno)
691 int regno;
692{
693 char buf[PBUFSIZ];
694 int i;
695 char *p;
696
697 buf[0] = 'G';
698
699 /* Command describes registers byte by byte,
700 each byte encoded as two hex characters. */
701
702 p = buf + 1;
55fea07b
JK
703 /* remote_prepare_to_store insures that register_bytes_found gets set. */
704 for (i = 0; i < register_bytes_found; i++)
bd5635a1
RP
705 {
706 *p++ = tohex ((registers[i] >> 4) & 0xf);
707 *p++ = tohex (registers[i] & 0xf);
708 }
709 *p = '\0';
710
711 remote_send (buf);
bd5635a1
RP
712}
713
b43e0347
JK
714#if 0
715
716/* Use of the data cache is disabled because it loses for looking at
717 and changing hardware I/O ports and the like. Accepting `volatile'
718 would perhaps be one way to fix it, but a better way which would
719 win for more cases would be to use the executable file for the text
720 segment, like the `icache' code below but done cleanly (in some
721 target-independent place, perhaps in target_xfer_memory, perhaps
722 based on assigning each target a speed or perhaps by some simpler
723 mechanism). */
724
bd5635a1
RP
725/* Read a word from remote address ADDR and return it.
726 This goes through the data cache. */
727
b43e0347 728static int
bd5635a1
RP
729remote_fetch_word (addr)
730 CORE_ADDR addr;
731{
d538b510 732#if 0
bd5635a1
RP
733 if (icache)
734 {
735 extern CORE_ADDR text_start, text_end;
736
737 if (addr >= text_start && addr < text_end)
738 {
739 int buffer;
740 xfer_core_file (addr, &buffer, sizeof (int));
741 return buffer;
742 }
743 }
d538b510
RP
744#endif
745 return dcache_fetch (remote_dcache, addr);
bd5635a1
RP
746}
747
748/* Write a word WORD into remote address ADDR.
749 This goes through the data cache. */
750
b43e0347 751static void
bd5635a1
RP
752remote_store_word (addr, word)
753 CORE_ADDR addr;
754 int word;
755{
d538b510 756 dcache_poke (remote_dcache, addr, word);
bd5635a1 757}
b43e0347 758#endif /* 0 */
bd5635a1
RP
759\f
760/* Write memory data directly to the remote machine.
761 This does not inform the data cache; the data cache uses this.
762 MEMADDR is the address in the remote memory space.
763 MYADDR is the address of the buffer in our space.
d538b510 764 LEN is the number of bytes.
bd5635a1 765
d538b510
RP
766 Returns number of bytes transferred, or 0 for error. */
767
768static int
bd5635a1
RP
769remote_write_bytes (memaddr, myaddr, len)
770 CORE_ADDR memaddr;
d538b510 771 unsigned char *myaddr;
bd5635a1
RP
772 int len;
773{
774 char buf[PBUFSIZ];
775 int i;
776 char *p;
777
bd5635a1
RP
778 sprintf (buf, "M%x,%x:", memaddr, len);
779
b543979c 780 /* We send target system values byte by byte, in increasing byte addresses,
bd5635a1
RP
781 each byte encoded as two hex characters. */
782
783 p = buf + strlen (buf);
784 for (i = 0; i < len; i++)
785 {
786 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
787 *p++ = tohex (myaddr[i] & 0xf);
788 }
789 *p = '\0';
790
d538b510
RP
791 putpkt (buf);
792 getpkt (buf, 0);
793
794 if (buf[0] == 'E')
795 {
796 /* There is no correspondance between what the remote protocol uses
797 for errors and errno codes. We would like a cleaner way of
798 representing errors (big enough to include errno codes, bfd_error
799 codes, and others). But for now just return EIO. */
800 errno = EIO;
801 return 0;
802 }
803 return len;
bd5635a1
RP
804}
805
806/* Read memory data directly from the remote machine.
807 This does not use the data cache; the data cache uses this.
808 MEMADDR is the address in the remote memory space.
809 MYADDR is the address of the buffer in our space.
d538b510 810 LEN is the number of bytes.
bd5635a1 811
d538b510
RP
812 Returns number of bytes transferred, or 0 for error. */
813
814static int
bd5635a1
RP
815remote_read_bytes (memaddr, myaddr, len)
816 CORE_ADDR memaddr;
d538b510 817 unsigned char *myaddr;
bd5635a1
RP
818 int len;
819{
820 char buf[PBUFSIZ];
821 int i;
822 char *p;
823
824 if (len > PBUFSIZ / 2 - 1)
825 abort ();
826
827 sprintf (buf, "m%x,%x", memaddr, len);
d538b510
RP
828 putpkt (buf);
829 getpkt (buf, 0);
830
831 if (buf[0] == 'E')
832 {
833 /* There is no correspondance between what the remote protocol uses
834 for errors and errno codes. We would like a cleaner way of
835 representing errors (big enough to include errno codes, bfd_error
836 codes, and others). But for now just return EIO. */
837 errno = EIO;
838 return 0;
839 }
bd5635a1 840
b543979c 841 /* Reply describes memory byte by byte,
bd5635a1
RP
842 each byte encoded as two hex characters. */
843
844 p = buf;
845 for (i = 0; i < len; i++)
846 {
847 if (p[0] == 0 || p[1] == 0)
d538b510
RP
848 /* Reply is short. This means that we were able to read only part
849 of what we wanted to. */
850 break;
bd5635a1
RP
851 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
852 p += 2;
853 }
d538b510 854 return i;
bd5635a1
RP
855}
856\f
857/* Read or write LEN bytes from inferior memory at MEMADDR, transferring
e1ce8aa5 858 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
bd5635a1
RP
859 nonzero. Returns length of data written or read; 0 for error. */
860
b543979c
JG
861/* ARGSUSED */
862static int
863remote_xfer_memory(memaddr, myaddr, len, should_write, target)
bd5635a1
RP
864 CORE_ADDR memaddr;
865 char *myaddr;
866 int len;
e1ce8aa5 867 int should_write;
b543979c 868 struct target_ops *target; /* ignored */
bd5635a1 869{
bd5635a1 870 int xfersize;
d538b510
RP
871 int bytes_xferred;
872 int total_xferred = 0;
873
bd5635a1
RP
874 while (len > 0)
875 {
876 if (len > MAXBUFBYTES)
877 xfersize = MAXBUFBYTES;
878 else
879 xfersize = len;
880
e1ce8aa5 881 if (should_write)
94d4b713
JK
882 bytes_xferred = remote_write_bytes (memaddr,
883 (unsigned char *)myaddr, xfersize);
bd5635a1 884 else
94d4b713
JK
885 bytes_xferred = remote_read_bytes (memaddr,
886 (unsigned char *)myaddr, xfersize);
d538b510
RP
887
888 /* If we get an error, we are done xferring. */
889 if (bytes_xferred == 0)
890 break;
891
892 memaddr += bytes_xferred;
893 myaddr += bytes_xferred;
894 len -= bytes_xferred;
895 total_xferred += bytes_xferred;
bd5635a1 896 }
d538b510 897 return total_xferred;
bd5635a1
RP
898}
899
94d4b713
JK
900#if 0
901/* Enable after 4.12. */
902
903void
904remote_search (len, data, mask, startaddr, increment, lorange, hirange
905 addr_found, data_found)
906 int len;
907 char *data;
908 char *mask;
909 CORE_ADDR startaddr;
910 int increment;
911 CORE_ADDR lorange;
912 CORE_ADDR hirange;
913 CORE_ADDR *addr_found;
914 char *data_found;
915{
916 if (increment == -4 && len == 4)
917 {
918 long mask_long, data_long;
919 long data_found_long;
920 CORE_ADDR addr_we_found;
921 char buf[PBUFSIZ];
922 long returned_long[2];
923 char *p;
924
925 mask_long = extract_unsigned_integer (mask, len);
926 data_long = extract_unsigned_integer (data, len);
927 sprintf (buf, "t%x:%x,%x", startaddr, data_long, mask_long);
928 putpkt (buf);
929 getpkt (buf, 0);
930 if (buf[0] == '\0')
931 {
932 /* The stub doesn't support the 't' request. We might want to
933 remember this fact, but on the other hand the stub could be
934 switched on us. Maybe we should remember it only until
935 the next "target remote". */
936 generic_search (len, data, mask, startaddr, increment, lorange,
937 hirange, addr_found, data_found);
938 return;
939 }
940
941 if (buf[0] == 'E')
942 /* There is no correspondance between what the remote protocol uses
943 for errors and errno codes. We would like a cleaner way of
944 representing errors (big enough to include errno codes, bfd_error
945 codes, and others). But for now just use EIO. */
946 memory_error (EIO, startaddr);
947 p = buf;
948 addr_we_found = 0;
949 while (*p != '\0' && *p != ',')
950 addr_we_found = (addr_we_found << 4) + fromhex (*p++);
951 if (*p == '\0')
952 error ("Protocol error: short return for search");
953
954 data_found_long = 0;
955 while (*p != '\0' && *p != ',')
956 data_found_long = (data_found_long << 4) + fromhex (*p++);
957 /* Ignore anything after this comma, for future extensions. */
958
959 if (addr_we_found < lorange || addr_we_found >= hirange)
960 {
961 *addr_found = 0;
962 return;
963 }
964
965 *addr_found = addr_we_found;
966 *data_found = store_unsigned_integer (data_we_found, len);
967 return;
968 }
969 generic_search (len, data, mask, startaddr, increment, lorange,
970 hirange, addr_found, data_found);
971}
972#endif /* 0 */
973\f
b543979c 974static void
8f86a4e4 975remote_files_info (ignore)
5af4f5f6 976 struct target_ops *ignore;
bd5635a1 977{
7c622b41 978 puts_filtered ("Debugging a target over a serial line.\n");
bd5635a1
RP
979}
980\f
e50ebec8
JK
981/* Stuff for dealing with the packets which are part of this protocol.
982 See comment at top of file for details. */
bd5635a1 983
ebdb9ade 984/* Read a single character from the remote end, masking it down to 7 bits. */
b543979c 985
bd5635a1
RP
986static int
987readchar ()
988{
ebdb9ade 989 int ch;
bd5635a1 990
ebdb9ade 991 ch = SERIAL_READCHAR (remote_desc, timeout);
fce7f2d9 992
ebdb9ade
JK
993 if (ch < 0)
994 return ch;
bd5635a1 995
ebdb9ade 996 return ch & 0x7f;
bd5635a1
RP
997}
998
999/* Send the command in BUF to the remote machine,
1000 and read the reply into BUF.
1001 Report an error if we get an error reply. */
1002
1003static void
1004remote_send (buf)
1005 char *buf;
1006{
1007
1008 putpkt (buf);
7c622b41 1009 getpkt (buf, 0);
bd5635a1
RP
1010
1011 if (buf[0] == 'E')
1012 error ("Remote failure reply: %s", buf);
1013}
1014
1015/* Send a packet to the remote machine, with error checking.
1016 The data of the packet is in BUF. */
1017
1018static void
1019putpkt (buf)
1020 char *buf;
1021{
1022 int i;
1023 unsigned char csum = 0;
b543979c 1024 char buf2[PBUFSIZ];
bd5635a1 1025 int cnt = strlen (buf);
ebdb9ade 1026 int ch;
bd5635a1
RP
1027 char *p;
1028
1029 /* Copy the packet into buffer BUF2, encapsulating it
1030 and giving it a checksum. */
1031
b543979c
JG
1032 if (cnt > sizeof(buf2) - 5) /* Prosanity check */
1033 abort();
1034
bd5635a1
RP
1035 p = buf2;
1036 *p++ = '$';
1037
1038 for (i = 0; i < cnt; i++)
1039 {
1040 csum += buf[i];
1041 *p++ = buf[i];
1042 }
1043 *p++ = '#';
1044 *p++ = tohex ((csum >> 4) & 0xf);
1045 *p++ = tohex (csum & 0xf);
1046
1047 /* Send it over and over until we get a positive ack. */
1048
6b27ebe8
JK
1049 while (1)
1050 {
d0d8484a 1051 if (remote_debug)
6b27ebe8
JK
1052 {
1053 *p = '\0';
199b2450 1054 printf_unfiltered ("Sending packet: %s...", buf2); gdb_flush(gdb_stdout);
6b27ebe8
JK
1055 }
1056 if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
1057 perror_with_name ("putpkt: write failed");
1058
1059 /* read until either a timeout occurs (-2) or '+' is read */
1060 while (1)
1061 {
1062 ch = readchar ();
1063
1064 switch (ch)
1065 {
1066 case '+':
d0d8484a 1067 if (remote_debug)
199b2450 1068 printf_unfiltered("Ack\n");
6b27ebe8
JK
1069 return;
1070 case SERIAL_TIMEOUT:
1071 break; /* Retransmit buffer */
1072 case SERIAL_ERROR:
1073 perror_with_name ("putpkt: couldn't read ACK");
1074 case SERIAL_EOF:
1075 error ("putpkt: EOF while trying to read ACK");
1076 default:
d0d8484a 1077 if (remote_debug)
199b2450 1078 printf_unfiltered ("%02X %c ", ch&0xFF, ch);
6b27ebe8
JK
1079 continue;
1080 }
1081 break; /* Here to retransmit */
1082 }
981a3309 1083
94d4b713
JK
1084#if 0
1085 /* This is wrong. If doing a long backtrace, the user should be
1086 able to get out next time we call QUIT, without anything as violent
1087 as interrupt_query. If we want to provide a way out of here
1088 without getting to the next QUIT, it should be based on hitting
1089 ^C twice as in remote_wait. */
981a3309
SG
1090 if (quit_flag)
1091 {
1092 quit_flag = 0;
1093 interrupt_query ();
1094 }
94d4b713 1095#endif
6b27ebe8 1096 }
bd5635a1
RP
1097}
1098
1099/* Read a packet from the remote machine, with error checking,
7c622b41
JG
1100 and store it in BUF. BUF is expected to be of size PBUFSIZ.
1101 If FOREVER, wait forever rather than timing out; this is used
1102 while the target is executing user code. */
bd5635a1
RP
1103
1104static void
94d4b713
JK
1105getpkt (retbuf, forever)
1106 char *retbuf;
ebdb9ade 1107 int forever;
bd5635a1
RP
1108{
1109 char *bp;
1110 unsigned char csum;
7c622b41 1111 int c = 0;
bd5635a1 1112 unsigned char c1, c2;
38094c60 1113 int retries = 0;
94d4b713
JK
1114 char buf[PBUFSIZ];
1115
38094c60 1116#define MAX_RETRIES 10
bd5635a1 1117
bd5635a1
RP
1118 while (1)
1119 {
94d4b713
JK
1120#if 0
1121 /* This is wrong. If doing a long backtrace, the user should be
1122 able to get out time next we call QUIT, without anything as violent
1123 as interrupt_query. If we want to provide a way out of here
1124 without getting to the next QUIT, it should be based on hitting
1125 ^C twice as in remote_wait. */
981a3309
SG
1126 if (quit_flag)
1127 {
1128 quit_flag = 0;
1129 interrupt_query ();
1130 }
94d4b713 1131#endif
981a3309 1132
7c622b41
JG
1133 /* This can loop forever if the remote side sends us characters
1134 continuously, but if it pauses, we'll get a zero from readchar
1135 because of timeout. Then we'll count that as a retry. */
6b27ebe8
JK
1136
1137 c = readchar();
1138 if (c > 0 && c != '$')
1139 continue;
1140
1141 if (c == SERIAL_TIMEOUT)
1142 {
1143 if (forever)
1144 continue;
1145 if (++retries >= MAX_RETRIES)
d0d8484a 1146 if (remote_debug) puts_filtered ("Timed out.\n");
6b27ebe8
JK
1147 goto out;
1148 }
1149
1150 if (c == SERIAL_EOF)
1151 error ("Remote connection closed");
1152 if (c == SERIAL_ERROR)
1153 perror_with_name ("Remote communication error");
7c622b41 1154
bd5635a1
RP
1155 /* Force csum to be zero here because of possible error retry. */
1156 csum = 0;
bd5635a1 1157 bp = buf;
7c622b41 1158
bd5635a1
RP
1159 while (1)
1160 {
1161 c = readchar ();
ebdb9ade 1162 if (c == SERIAL_TIMEOUT)
7c622b41 1163 {
d0d8484a 1164 if (remote_debug)
7c622b41
JG
1165 puts_filtered ("Timeout in mid-packet, retrying\n");
1166 goto whole; /* Start a new packet, count retries */
1167 }
1168 if (c == '$')
1169 {
d0d8484a 1170 if (remote_debug)
7c622b41
JG
1171 puts_filtered ("Saw new packet start in middle of old one\n");
1172 goto whole; /* Start a new packet, count retries */
1173 }
bd5635a1
RP
1174 if (c == '#')
1175 break;
8f86a4e4
JG
1176 if (bp >= buf+PBUFSIZ-1)
1177 {
1178 *bp = '\0';
7c622b41
JG
1179 puts_filtered ("Remote packet too long: ");
1180 puts_filtered (buf);
1181 puts_filtered ("\n");
8f86a4e4
JG
1182 goto whole;
1183 }
bd5635a1
RP
1184 *bp++ = c;
1185 csum += c;
1186 }
1187 *bp = 0;
1188
1189 c1 = fromhex (readchar ());
1190 c2 = fromhex (readchar ());
1191 if ((csum & 0xff) == (c1 << 4) + c2)
1192 break;
7c622b41
JG
1193 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
1194 (c1 << 4) + c2, csum & 0xff);
1195 puts_filtered (buf);
1196 puts_filtered ("\n");
38094c60 1197
8f86a4e4
JG
1198 /* Try the whole thing again. */
1199whole:
38094c60
JG
1200 if (++retries < MAX_RETRIES)
1201 {
ebdb9ade 1202 SERIAL_WRITE (remote_desc, "-", 1);
38094c60
JG
1203 }
1204 else
1205 {
199b2450 1206 printf_unfiltered ("Ignoring packet error, continuing...\n");
38094c60
JG
1207 break;
1208 }
bd5635a1
RP
1209 }
1210
94d4b713
JK
1211 /* Deal with run-length encoding. */
1212 {
1213 char *src = buf;
1214 char *dest = retbuf;
1215 int i;
1216 int repeat;
1217 do {
1218 if (*src == '*')
1219 {
1220 if (src[1] == '\0' || src[2] == '\0')
1221 {
1222 if (remote_debug)
1223 puts_filtered ("Packet too short, retrying\n");
1224 goto whole;
1225 }
1226 repeat = (fromhex (src[1]) << 4) + fromhex (src[2]);
1227 for (i = 0; i < repeat; ++i)
1228 {
1229 *dest++ = src[-1];
1230 }
1231 src += 2;
1232 }
1233 else
1234 {
1235 *dest++ = *src;
1236 }
1237 } while (*src++ != '\0');
1238 }
7c622b41 1239
94d4b713 1240out:
ebdb9ade 1241 SERIAL_WRITE (remote_desc, "+", 1);
bd5635a1 1242
d0d8484a 1243 if (remote_debug)
199b2450 1244 fprintf_unfiltered (gdb_stderr,"Packet received: %s\n", buf);
bd5635a1
RP
1245}
1246\f
ebdb9ade
JK
1247static void
1248remote_kill ()
1249{
1250 putpkt ("k");
1251 /* Don't wait for it to die. I'm not really sure it matters whether
1252 we do or not. For the existing stubs, kill is a noop. */
1253 target_mourn_inferior ();
1254}
bd5635a1 1255
ebdb9ade
JK
1256static void
1257remote_mourn ()
1258{
1259 unpush_target (&remote_ops);
1260 generic_mourn_inferior ();
1261}
1262\f
5af4f5f6
JK
1263#ifdef REMOTE_BREAKPOINT
1264
1265/* On some machines, e.g. 68k, we may use a different breakpoint instruction
1266 than other targets. */
1267static unsigned char break_insn[] = REMOTE_BREAKPOINT;
1268
1269/* Check that it fits in BREAKPOINT_MAX bytes. */
1270static unsigned char check_break_insn_size[BREAKPOINT_MAX] = REMOTE_BREAKPOINT;
1271
1272#else /* No REMOTE_BREAKPOINT. */
1273
1274/* Same old breakpoint instruction. This code does nothing different
1275 than mem-break.c. */
1276static unsigned char break_insn[] = BREAKPOINT;
1277
1278#endif /* No REMOTE_BREAKPOINT. */
1279
1280/* Insert a breakpoint on targets that don't have any better breakpoint
1281 support. We read the contents of the target location and stash it,
1282 then overwrite it with a breakpoint instruction. ADDR is the target
1283 location in the target machine. CONTENTS_CACHE is a pointer to
1284 memory allocated for saving the target contents. It is guaranteed
1285 by the caller to be long enough to save sizeof BREAKPOINT bytes (this
1286 is accomplished via BREAKPOINT_MAX). */
1287
d538b510 1288static int
5af4f5f6
JK
1289remote_insert_breakpoint (addr, contents_cache)
1290 CORE_ADDR addr;
1291 char *contents_cache;
1292{
1293 int val;
1294
1295 val = target_read_memory (addr, contents_cache, sizeof break_insn);
1296
1297 if (val == 0)
1298 val = target_write_memory (addr, (char *)break_insn, sizeof break_insn);
1299
1300 return val;
1301}
1302
d538b510 1303static int
5af4f5f6
JK
1304remote_remove_breakpoint (addr, contents_cache)
1305 CORE_ADDR addr;
1306 char *contents_cache;
1307{
1308 return target_write_memory (addr, contents_cache, sizeof break_insn);
1309}
1310\f
bd5635a1
RP
1311/* Define the target subroutine names */
1312
1313struct target_ops remote_ops = {
b543979c
JG
1314 "remote", /* to_shortname */
1315 "Remote serial target in gdb-specific protocol", /* to_longname */
1316 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1317Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1318 remote_open, /* to_open */
1319 remote_close, /* to_close */
1320 NULL, /* to_attach */
1321 remote_detach, /* to_detach */
1322 remote_resume, /* to_resume */
1323 remote_wait, /* to_wait */
1324 remote_fetch_registers, /* to_fetch_registers */
1325 remote_store_registers, /* to_store_registers */
1326 remote_prepare_to_store, /* to_prepare_to_store */
b543979c
JG
1327 remote_xfer_memory, /* to_xfer_memory */
1328 remote_files_info, /* to_files_info */
5af4f5f6
JK
1329
1330 remote_insert_breakpoint, /* to_insert_breakpoint */
1331 remote_remove_breakpoint, /* to_remove_breakpoint */
1332
b543979c
JG
1333 NULL, /* to_terminal_init */
1334 NULL, /* to_terminal_inferior */
1335 NULL, /* to_terminal_ours_for_output */
1336 NULL, /* to_terminal_ours */
1337 NULL, /* to_terminal_info */
ebdb9ade 1338 remote_kill, /* to_kill */
6b27ebe8 1339 generic_load, /* to_load */
b543979c
JG
1340 NULL, /* to_lookup_symbol */
1341 NULL, /* to_create_inferior */
ebdb9ade 1342 remote_mourn, /* to_mourn_inferior */
34517ebc 1343 0, /* to_can_run */
7c622b41 1344 0, /* to_notice_signals */
b543979c
JG
1345 process_stratum, /* to_stratum */
1346 NULL, /* to_next */
1347 1, /* to_has_all_memory */
1348 1, /* to_has_memory */
1349 1, /* to_has_stack */
1350 1, /* to_has_registers */
1351 1, /* to_has_execution */
1352 NULL, /* sections */
1353 NULL, /* sections_end */
1354 OPS_MAGIC /* to_magic */
bd5635a1 1355};
976bb0be 1356#endif /* Use remote. */
bd5635a1
RP
1357
1358void
1359_initialize_remote ()
1360{
976bb0be 1361#if !defined(DONT_USE_REMOTE)
bd5635a1 1362 add_target (&remote_ops);
8f86a4e4 1363#endif
976bb0be 1364}
This page took 0.229566 seconds and 4 git commands to generate.