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