1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* Remote communication protocol.
21 All values are encoded in ascii hex digits.
26 reply XX....X Each byte of register data
27 is described by two hex digits.
28 Registers are in the internal order
29 for GDB, and the bytes in a register
30 are in the same order the machine uses.
33 write regs GXX..XX Each byte of register data
34 is described by two hex digits.
38 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
39 reply XX..XX XX..XX is mem contents
42 write mem MAA..AA,LLLL:XX..XX
44 LLLL is number of bytes,
49 cont cAA..AA AA..AA is address to resume
51 resume at same address.
53 step sAA..AA AA..AA is address to resume
55 resume at same address.
57 last signal ? Reply the current reason for stopping.
58 This is the same reply as is generated
59 for step or cont : SAA where AA is the
62 There is no immediate reply to step or cont.
63 The reply comes when the machine stops.
64 It is SAA AA is the "signal number"
79 #include <sys/types.h>
84 /* Prototypes for local functions */
87 remote_write_bytes
PARAMS ((CORE_ADDR
, char *, int));
90 remote_read_bytes
PARAMS ((CORE_ADDR
, char *, int));
93 remote_files_info
PARAMS ((struct target_ops
*));
96 remote_xfer_memory
PARAMS ((CORE_ADDR
, char *, int, int, struct target_ops
*));
99 remote_prepare_to_store
PARAMS ((void));
102 remote_fetch_registers
PARAMS ((int));
105 remote_resume
PARAMS ((int, int));
108 remote_open
PARAMS ((char *, int));
111 remote_close
PARAMS ((int));
114 remote_store_registers
PARAMS ((int));
117 getpkt
PARAMS ((char *));
120 putpkt
PARAMS ((char *));
123 remote_send
PARAMS ((char *));
126 readchar
PARAMS ((void));
129 remote_wait
PARAMS ((WAITTYPE
*));
132 tohex
PARAMS ((int));
135 fromhex
PARAMS ((int));
138 remote_detach
PARAMS ((char *, int));
141 extern struct target_ops remote_ops
; /* Forward decl */
144 static int timeout
= 5;
150 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
151 remote_open knows that we don't have a file open when the program
153 int remote_desc
= -1;
157 /* Maximum number of bytes to read/write at once. The value here
158 is chosen to fill up a packet (the headers account for the 32). */
159 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
161 /* Round up PBUFSIZ to hold all the registers, at least. */
162 #if REGISTER_BYTES > MAXBUFBYTES
164 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
167 /* Called when SIGALRM signal sent due to alarm() timeout. */
173 printf ("remote_timer called\n");
179 /* Clean up connection to a remote debugger. */
183 remote_close (quitting
)
186 if (remote_desc
>= 0)
191 /* Translate baud rates from integers to damn B_codes. Unix should
192 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
201 static struct {int rate
, damn_b
;} baudtab
[] = {
227 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
228 if (rate
== baudtab
[i
].rate
) return baudtab
[i
].damn_b
;
229 return B38400
; /* Random */
232 /* Open a connection to a remote debugger.
233 NAME is the filename used for communication. */
236 remote_open (name
, from_tty
)
242 int baudrate_set
= 0;
246 "To open a remote debug connection, you need to specify what serial\n\
247 device is attached to the remote system (e.g. /dev/ttya).");
249 target_preopen (from_tty
);
257 remote_desc
= open (name
, O_RDWR
);
259 perror_with_name (name
);
263 if (1 != sscanf (baud_rate
, "%d ", &a_rate
))
265 b_rate
= damn_b (a_rate
);
270 ioctl (remote_desc
, TIOCGETP
, &sg
);
272 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
273 sg
.c_cc
[VTIME
] = timeout
* 10;
274 sg
.c_lflag
&= ~(ICANON
| ECHO
);
275 sg
.c_cflag
&= ~PARENB
; /* No parity */
276 sg
.c_cflag
|= CS8
; /* 8-bit path */
278 sg
.c_cflag
= (sg
.c_cflag
& ~CBAUD
) | b_rate
;
280 sg
.sg_flags
|= RAW
| ANYP
;
281 sg
.sg_flags
&= ~ECHO
;
284 sg
.sg_ispeed
= b_rate
;
285 sg
.sg_ospeed
= b_rate
;
288 ioctl (remote_desc
, TIOCSETP
, &sg
);
291 printf ("Remote debugging using %s\n", name
);
292 push_target (&remote_ops
); /* Switch to using remote target now */
295 #ifndef NO_SIGINTERRUPT
296 /* Cause SIGALRM's to make reads fail. */
297 if (siginterrupt (SIGALRM
, 1) != 0)
298 perror ("remote_open: error in siginterrupt");
301 /* Set up read timeout timer. */
302 if ((void (*)()) signal (SIGALRM
, remote_timer
) == (void (*)()) -1)
303 perror ("remote_open: error in signal");
306 /* Ack any packet which the remote side has already sent. */
307 write (remote_desc
, "+", 1);
308 putpkt ("?"); /* initiate a query from remote machine */
310 start_remote (); /* Initialize gdb process mechanisms */
314 takes a program previously attached to and detaches it.
315 We better not have left any breakpoints
316 in the program or it'll die when it hits one.
317 Close the open connection to the remote debugger.
318 Use this when you want to detach and do something else
322 remote_detach (args
, from_tty
)
327 error ("Argument given to \"detach\" when remotely debugging.");
331 printf ("Ending remote debugging.\n");
334 /* Convert hex digit A to a number. */
340 if (a
>= '0' && a
<= '9')
342 else if (a
>= 'a' && a
<= 'f')
345 error ("Reply contains invalid hex digit");
349 /* Convert number NIB to a hex digit. */
361 /* Tell the remote machine to resume. */
364 remote_resume (step
, siggnal
)
370 error ("Can't send signals to a remote system.");
376 strcpy (buf
, step
? "s": "c");
381 /* Send ^C to target to halt it. Target will respond, and send us a
384 void remote_interrupt()
386 write (remote_desc
, "\003", 1); /* Send a ^C */
390 /* Wait until the remote machine stops, then return,
391 storing status in STATUS just as `wait' would.
392 Returns "pid" (though it's not clear what, if anything, that
393 means in the case of this target). */
399 unsigned char buf
[PBUFSIZ
];
402 WSETEXIT ((*status
), 0);
404 ofunc
= signal (SIGINT
, remote_interrupt
);
405 getpkt ((char *) buf
);
406 signal (SIGINT
, ofunc
);
409 error ("Remote failure reply: %s", buf
);
411 error ("Invalid remote reply: %s", buf
);
412 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
416 /* Read the remote registers into the block REGS. */
418 /* Currently we just read all the registers, so we don't use regno. */
421 remote_fetch_registers (regno
)
427 char regs
[REGISTER_BYTES
];
432 /* Reply describes registers byte by byte, each byte encoded as two
433 hex characters. Suck them all up, then supply them to the
434 register cacheing/storage mechanism. */
437 for (i
= 0; i
< REGISTER_BYTES
; i
++)
439 if (p
[0] == 0 || p
[1] == 0)
440 error ("Remote reply is too short: %s", buf
);
441 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
444 for (i
= 0; i
< NUM_REGS
; i
++)
445 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
448 /* Prepare to store registers. Since we send them all, we have to
449 read out the ones we don't want to change first. */
452 remote_prepare_to_store ()
454 remote_fetch_registers (-1);
457 /* Store the remote registers from the contents of the block REGISTERS.
458 FIXME, eventually just store one register if that's all that is needed. */
462 remote_store_registers (regno
)
471 /* Command describes registers byte by byte,
472 each byte encoded as two hex characters. */
475 for (i
= 0; i
< REGISTER_BYTES
; i
++)
477 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
478 *p
++ = tohex (registers
[i
] & 0xf);
486 /* Read a word from remote address ADDR and return it.
487 This goes through the data cache. */
490 remote_fetch_word (addr
)
495 extern CORE_ADDR text_start
, text_end
;
497 if (addr
>= text_start
&& addr
< text_end
)
500 xfer_core_file (addr
, &buffer
, sizeof (int));
504 return dcache_fetch (addr
);
507 /* Write a word WORD into remote address ADDR.
508 This goes through the data cache. */
511 remote_store_word (addr
, word
)
515 dcache_poke (addr
, word
);
519 /* Write memory data directly to the remote machine.
520 This does not inform the data cache; the data cache uses this.
521 MEMADDR is the address in the remote memory space.
522 MYADDR is the address of the buffer in our space.
523 LEN is the number of bytes. */
526 remote_write_bytes (memaddr
, myaddr
, len
)
535 if (len
> PBUFSIZ
/ 2 - 20)
538 sprintf (buf
, "M%x,%x:", memaddr
, len
);
540 /* We send target system values byte by byte, in increasing byte addresses,
541 each byte encoded as two hex characters. */
543 p
= buf
+ strlen (buf
);
544 for (i
= 0; i
< len
; i
++)
546 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
547 *p
++ = tohex (myaddr
[i
] & 0xf);
554 /* Read memory data directly from the remote machine.
555 This does not use the data cache; the data cache uses this.
556 MEMADDR is the address in the remote memory space.
557 MYADDR is the address of the buffer in our space.
558 LEN is the number of bytes. */
561 remote_read_bytes (memaddr
, myaddr
, len
)
570 if (len
> PBUFSIZ
/ 2 - 1)
573 sprintf (buf
, "m%x,%x", memaddr
, len
);
576 /* Reply describes memory byte by byte,
577 each byte encoded as two hex characters. */
580 for (i
= 0; i
< len
; i
++)
582 if (p
[0] == 0 || p
[1] == 0)
583 error ("Remote reply is too short: %s", buf
);
584 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
589 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
590 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
591 nonzero. Returns length of data written or read; 0 for error. */
595 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
600 struct target_ops
*target
; /* ignored */
606 if (len
> MAXBUFBYTES
)
607 xfersize
= MAXBUFBYTES
;
612 remote_write_bytes(memaddr
, myaddr
, xfersize
);
614 remote_read_bytes (memaddr
, myaddr
, xfersize
);
619 return origlen
; /* no error possible */
623 remote_files_info (target
)
624 struct target_ops
*target
;
626 printf ("remote files info missing here. FIXME.\n");
631 A debug packet whose contents are <data>
632 is encapsulated for transmission in the form:
634 $ <data> # CSUM1 CSUM2
636 <data> must be ASCII alphanumeric and cannot include characters
639 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
640 checksum of <data>, the most significant nibble is sent first.
641 the hex digits 0-9,a-f are used.
643 Receiver responds with:
645 + - if CSUM is correct and ready for next packet
646 - - if CSUM is incorrect
650 /* Read a single character from the remote end.
651 (If supported, we actually read many characters and buffer them up.) */
657 static int inbuf_index
, inbuf_count
;
658 #define INBUFSIZE PBUFSIZ
659 static char inbuf
[INBUFSIZE
];
661 if (inbuf_index
>= inbuf_count
)
663 /* Time to do another read... */
666 inbuf
[0] = 0; /* Just in case */
668 /* termio does the timeout for us. */
669 inbuf_count
= read (remote_desc
, inbuf
, INBUFSIZE
);
672 inbuf_count
= read (remote_desc
, inbuf
, INBUFSIZE
);
677 /* Just return the next character from the buffer. */
678 return inbuf
[inbuf_index
++] & 0x7f;
681 /* Send the command in BUF to the remote machine,
682 and read the reply into BUF.
683 Report an error if we get an error reply. */
694 error ("Remote failure reply: %s", buf
);
697 /* Send a packet to the remote machine, with error checking.
698 The data of the packet is in BUF. */
705 unsigned char csum
= 0;
707 int cnt
= strlen (buf
);
711 /* Copy the packet into buffer BUF2, encapsulating it
712 and giving it a checksum. */
714 if (cnt
> sizeof(buf2
) - 5) /* Prosanity check */
720 for (i
= 0; i
< cnt
; i
++)
726 *p
++ = tohex ((csum
>> 4) & 0xf);
727 *p
++ = tohex (csum
& 0xf);
729 /* Send it over and over until we get a positive ack. */
735 printf ("Sending packet: %s (%s)\n", buf2
, buf
);
737 write (remote_desc
, buf2
, p
- buf2
);
739 /* read until either a timeout occurs (\0) or '+' is read */
742 } while ((ch
!= '+') && (ch
!= '\0'));
746 /* Read a packet from the remote machine, with error checking,
747 and store it in BUF. */
756 unsigned char c1
, c2
;
759 /* Sorry, this will cause all hell to break loose, i.e. we'll end
760 up in the command loop with an inferior, but (at least if this
761 happens in remote_wait or some such place) without a current_frame,
762 having set up prev_* in wait_for_inferior, etc.
764 If it is necessary to have such an "emergency exit", seems like
765 the only plausible thing to do is to say the inferior died, and
766 make the user reattach if they want to. Perhaps with a prompt
767 asking for confirmation. */
769 /* allow immediate quit while reading from device, it could be hung */
775 /* Force csum to be zero here because of possible error retry. */
778 while ((c
= readchar()) != '$');
791 c1
= fromhex (readchar ());
792 c2
= fromhex (readchar ());
793 if ((csum
& 0xff) == (c1
<< 4) + c2
)
795 printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
796 (c1
<< 4) + c2
, csum
& 0xff, buf
);
797 write (remote_desc
, "-", 1);
804 write (remote_desc
, "+", 1);
807 fprintf (stderr
,"Packet received :%s\n", buf
);
810 /* The data cache leads to incorrect results because it doesn't know about
811 volatile variables, thus making it impossible to debug functions which
812 use hardware registers. Therefore it is #if 0'd out. Effect on
813 performance is some, for backtraces of functions with a few
814 arguments each. For functions with many arguments, the stack
815 frames don't fit in the cache blocks, which makes the cache less
816 helpful. Disabling the cache is a big performance win for fetching
817 large structures, because the cache code fetched data in 16-byte
820 /* The data cache records all the data read from the remote machine
821 since the last time it stopped.
823 Each cache block holds 16 bytes of data
824 starting at a multiple-of-16 address. */
826 #define DCACHE_SIZE 64 /* Number of cache blocks */
828 struct dcache_block
{
829 struct dcache_block
*next
, *last
;
830 unsigned int addr
; /* Address for which data is recorded. */
834 struct dcache_block dcache_free
, dcache_valid
;
836 /* Free all the data cache blocks, thus discarding all cached data. */
841 register struct dcache_block
*db
;
843 while ((db
= dcache_valid
.next
) != &dcache_valid
)
846 insque (db
, &dcache_free
);
851 * If addr is present in the dcache, return the address of the block
855 struct dcache_block
*
858 register struct dcache_block
*db
;
863 /* Search all cache blocks for one that is at this address. */
864 db
= dcache_valid
.next
;
865 while (db
!= &dcache_valid
)
867 if ((addr
& 0xfffffff0) == db
->addr
)
874 /* Return the int data at address ADDR in dcache block DC. */
877 dcache_value (db
, addr
)
878 struct dcache_block
*db
;
883 return (db
->data
[(addr
>>2)&3]);
886 /* Get a free cache block, put it on the valid list,
887 and return its address. The caller should store into the block
888 the address and data that it describes. */
890 struct dcache_block
*
893 register struct dcache_block
*db
;
895 if ((db
= dcache_free
.next
) == &dcache_free
)
896 /* If we can't get one from the free list, take last valid */
897 db
= dcache_valid
.last
;
900 insque (db
, &dcache_valid
);
904 /* Return the contents of the word at address ADDR in the remote machine,
905 using the data cache. */
911 register struct dcache_block
*db
;
913 db
= dcache_hit (addr
);
916 db
= dcache_alloc ();
917 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
918 db
->addr
= addr
& ~0xf;
920 return (dcache_value (db
, addr
));
923 /* Write the word at ADDR both in the data cache and in the remote machine. */
925 dcache_poke (addr
, data
)
929 register struct dcache_block
*db
;
931 /* First make sure the word is IN the cache. DB is its cache block. */
932 db
= dcache_hit (addr
);
935 db
= dcache_alloc ();
936 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
937 db
->addr
= addr
& ~0xf;
940 /* Modify the word in the cache. */
941 db
->data
[(addr
>>2)&3] = data
;
943 /* Send the changed word. */
944 remote_write_bytes (addr
, &data
, 4);
947 /* Initialize the data cache. */
952 register struct dcache_block
*db
;
954 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
956 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
957 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
958 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
959 insque (db
, &dcache_free
);
963 /* Define the target subroutine names */
965 struct target_ops remote_ops
= {
966 "remote", /* to_shortname */
967 "Remote serial target in gdb-specific protocol", /* to_longname */
968 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
969 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
970 remote_open
, /* to_open */
971 remote_close
, /* to_close */
972 NULL
, /* to_attach */
973 remote_detach
, /* to_detach */
974 remote_resume
, /* to_resume */
975 remote_wait
, /* to_wait */
976 remote_fetch_registers
, /* to_fetch_registers */
977 remote_store_registers
, /* to_store_registers */
978 remote_prepare_to_store
, /* to_prepare_to_store */
979 NULL
, /* to_convert_to_virtual */
980 NULL
, /* to_convert_from_virtual */
981 remote_xfer_memory
, /* to_xfer_memory */
982 remote_files_info
, /* to_files_info */
983 NULL
, /* to_insert_breakpoint */
984 NULL
, /* to_remove_breakpoint */
985 NULL
, /* to_terminal_init */
986 NULL
, /* to_terminal_inferior */
987 NULL
, /* to_terminal_ours_for_output */
988 NULL
, /* to_terminal_ours */
989 NULL
, /* to_terminal_info */
992 NULL
, /* to_lookup_symbol */
993 NULL
, /* to_create_inferior */
994 NULL
, /* to_mourn_inferior */
995 process_stratum
, /* to_stratum */
997 1, /* to_has_all_memory */
998 1, /* to_has_memory */
999 1, /* to_has_stack */
1000 1, /* to_has_registers */
1001 1, /* to_has_execution */
1002 NULL
, /* sections */
1003 NULL
, /* sections_end */
1004 OPS_MAGIC
/* to_magic */
1008 _initialize_remote ()
1010 add_target (&remote_ops
);