86df56a8e48947d00106a8513d9bad9eeafd0697
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"
66 or... TAAPPPPPPPPFFFFFFFF
67 where AA is the signal number,
68 PPPPPPPP is the PC (PC_REGNUM), and
69 FFFFFFFF is the frame ptr (FP_REGNUM).
84 #if !defined(DONT_USE_REMOTE)
86 #include <sys/types.h>
91 /* Prototypes for local functions */
94 remote_write_bytes
PARAMS ((CORE_ADDR
, char *, int));
97 remote_read_bytes
PARAMS ((CORE_ADDR
, char *, int));
100 remote_files_info
PARAMS ((struct target_ops
*));
103 remote_xfer_memory
PARAMS ((CORE_ADDR
, char *, int, int, struct target_ops
*));
106 remote_prepare_to_store
PARAMS ((void));
109 remote_fetch_registers
PARAMS ((int));
112 remote_resume
PARAMS ((int, int));
115 remote_open
PARAMS ((char *, int));
118 remote_close
PARAMS ((int));
121 remote_store_registers
PARAMS ((int));
124 getpkt
PARAMS ((char *));
127 putpkt
PARAMS ((char *));
130 remote_send
PARAMS ((char *));
133 readchar
PARAMS ((void));
136 remote_wait
PARAMS ((WAITTYPE
*));
139 tohex
PARAMS ((int));
142 fromhex
PARAMS ((int));
145 remote_detach
PARAMS ((char *, int));
148 extern struct target_ops remote_ops
; /* Forward decl */
150 static int kiodebug
= 0;
151 static int timeout
= 5;
157 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
158 remote_open knows that we don't have a file open when the program
160 int remote_desc
= -1;
164 /* Maximum number of bytes to read/write at once. The value here
165 is chosen to fill up a packet (the headers account for the 32). */
166 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
168 /* Round up PBUFSIZ to hold all the registers, at least. */
169 #if REGISTER_BYTES > MAXBUFBYTES
171 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
174 /* Called when SIGALRM signal sent due to alarm() timeout. */
181 printf ("remote_timer called\n");
187 /* Clean up connection to a remote debugger. */
191 remote_close (quitting
)
194 if (remote_desc
>= 0)
199 /* Translate baud rates from integers to damn B_codes. Unix should
200 have outgrown this crap years ago, but even POSIX wouldn't buck it. */
211 static struct {int rate
, damn_b
;} baudtab
[] = {
237 for (i
= 0; baudtab
[i
].rate
!= -1; i
++)
238 if (rate
== baudtab
[i
].rate
) return baudtab
[i
].damn_b
;
239 return B38400
; /* Random */
242 /* Open a connection to a remote debugger.
243 NAME is the filename used for communication. */
246 remote_open (name
, from_tty
)
251 int a_rate
, b_rate
= 0;
252 int baudrate_set
= 0;
256 "To open a remote debug connection, you need to specify what serial\n\
257 device is attached to the remote system (e.g. /dev/ttya).");
259 target_preopen (from_tty
);
267 remote_desc
= open (name
, O_RDWR
);
269 perror_with_name (name
);
273 if (sscanf (baud_rate
, "%d", &a_rate
) == 1)
275 b_rate
= damn_b (a_rate
);
280 ioctl (remote_desc
, TIOCGETP
, &sg
);
282 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
283 sg
.c_cc
[VTIME
] = timeout
* 10;
284 sg
.c_lflag
&= ~(ICANON
| ECHO
);
285 sg
.c_cflag
&= ~PARENB
; /* No parity */
286 sg
.c_cflag
|= CS8
; /* 8-bit path */
288 sg
.c_cflag
= (sg
.c_cflag
& ~CBAUD
) | b_rate
;
290 sg
.sg_flags
|= RAW
| ANYP
;
291 sg
.sg_flags
&= ~ECHO
;
294 sg
.sg_ispeed
= b_rate
;
295 sg
.sg_ospeed
= b_rate
;
298 ioctl (remote_desc
, TIOCSETP
, &sg
);
301 printf ("Remote debugging using %s\n", name
);
302 push_target (&remote_ops
); /* Switch to using remote target now */
305 #ifndef NO_SIGINTERRUPT
306 /* Cause SIGALRM's to make reads fail. */
307 if (siginterrupt (SIGALRM
, 1) != 0)
308 perror ("remote_open: error in siginterrupt");
311 /* Set up read timeout timer. */
312 if ((void (*)()) signal (SIGALRM
, remote_timer
) == (void (*)()) -1)
313 perror ("remote_open: error in signal");
316 /* Ack any packet which the remote side has already sent. */
317 write (remote_desc
, "+\r", 2);
318 putpkt ("?"); /* initiate a query from remote machine */
320 start_remote (); /* Initialize gdb process mechanisms */
324 takes a program previously attached to and detaches it.
325 We better not have left any breakpoints
326 in the program or it'll die when it hits one.
327 Close the open connection to the remote debugger.
328 Use this when you want to detach and do something else
332 remote_detach (args
, from_tty
)
337 error ("Argument given to \"detach\" when remotely debugging.");
341 printf ("Ending remote debugging.\n");
344 /* Convert hex digit A to a number. */
350 if (a
>= '0' && a
<= '9')
352 else if (a
>= 'a' && a
<= 'f')
355 error ("Reply contains invalid hex digit");
359 /* Convert number NIB to a hex digit. */
371 /* Tell the remote machine to resume. */
374 remote_resume (step
, siggnal
)
380 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
387 strcpy (buf
, step
? "s": "c");
392 /* Send ^C to target to halt it. Target will respond, and send us a
395 void remote_interrupt(signo
)
400 printf ("remote_interrupt called\n");
402 write (remote_desc
, "\003", 1); /* Send a ^C */
406 /* Wait until the remote machine stops, then return,
407 storing status in STATUS just as `wait' would.
408 Returns "pid" (though it's not clear what, if anything, that
409 means in the case of this target). */
415 unsigned char buf
[PBUFSIZ
];
420 unsigned char regs
[MAX_REGISTER_RAW_SIZE
];
422 WSETEXIT ((*status
), 0);
424 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
425 getpkt ((char *) buf
);
426 signal (SIGINT
, ofunc
);
429 error ("Remote failure reply: %s", buf
);
432 /* Expedited reply, containing Signal, {regno, reg} repeat */
433 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
435 n... = register number
436 r... = register contents
439 p
= &buf
[3]; /* after Txx */
443 regno
= strtol (p
, &p
, 16); /* Read the register number */
446 || regno
>= NUM_REGS
)
447 error ("Remote sent bad register number %s", buf
);
449 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
451 if (p
[0] == 0 || p
[1] == 0)
452 error ("Remote reply is too short: %s", buf
);
453 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
458 error("Remote register badly formatted: %s", buf
);
460 supply_register (regno
, regs
);
463 else if (buf
[0] != 'S')
464 error ("Invalid remote reply: %s", buf
);
466 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
471 /* Read the remote registers into the block REGS. */
472 /* Currently we just read all the registers, so we don't use regno. */
475 remote_fetch_registers (regno
)
481 char regs
[REGISTER_BYTES
];
486 /* Reply describes registers byte by byte, each byte encoded as two
487 hex characters. Suck them all up, then supply them to the
488 register cacheing/storage mechanism. */
491 for (i
= 0; i
< REGISTER_BYTES
; i
++)
493 if (p
[0] == 0 || p
[1] == 0)
494 error ("Remote reply is too short: %s", buf
);
495 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
498 for (i
= 0; i
< NUM_REGS
; i
++)
499 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
502 /* Prepare to store registers. Since we send them all, we have to
503 read out the ones we don't want to change first. */
506 remote_prepare_to_store ()
508 remote_fetch_registers (-1);
511 /* Store the remote registers from the contents of the block REGISTERS.
512 FIXME, eventually just store one register if that's all that is needed. */
516 remote_store_registers (regno
)
525 /* Command describes registers byte by byte,
526 each byte encoded as two hex characters. */
529 for (i
= 0; i
< REGISTER_BYTES
; i
++)
531 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
532 *p
++ = tohex (registers
[i
] & 0xf);
540 /* Read a word from remote address ADDR and return it.
541 This goes through the data cache. */
544 remote_fetch_word (addr
)
549 extern CORE_ADDR text_start
, text_end
;
551 if (addr
>= text_start
&& addr
< text_end
)
554 xfer_core_file (addr
, &buffer
, sizeof (int));
558 return dcache_fetch (addr
);
561 /* Write a word WORD into remote address ADDR.
562 This goes through the data cache. */
565 remote_store_word (addr
, word
)
569 dcache_poke (addr
, word
);
573 /* Write memory data directly to the remote machine.
574 This does not inform the data cache; the data cache uses this.
575 MEMADDR is the address in the remote memory space.
576 MYADDR is the address of the buffer in our space.
577 LEN is the number of bytes. */
580 remote_write_bytes (memaddr
, myaddr
, len
)
589 if (len
> PBUFSIZ
/ 2 - 20)
592 sprintf (buf
, "M%x,%x:", memaddr
, len
);
594 /* We send target system values byte by byte, in increasing byte addresses,
595 each byte encoded as two hex characters. */
597 p
= buf
+ strlen (buf
);
598 for (i
= 0; i
< len
; i
++)
600 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
601 *p
++ = tohex (myaddr
[i
] & 0xf);
608 /* Read memory data directly from the remote machine.
609 This does not use the data cache; the data cache uses this.
610 MEMADDR is the address in the remote memory space.
611 MYADDR is the address of the buffer in our space.
612 LEN is the number of bytes. */
615 remote_read_bytes (memaddr
, myaddr
, len
)
624 if (len
> PBUFSIZ
/ 2 - 1)
627 sprintf (buf
, "m%x,%x", memaddr
, len
);
630 /* Reply describes memory byte by byte,
631 each byte encoded as two hex characters. */
634 for (i
= 0; i
< len
; i
++)
636 if (p
[0] == 0 || p
[1] == 0)
637 error ("Remote reply is too short: %s", buf
);
638 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
643 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
644 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
645 nonzero. Returns length of data written or read; 0 for error. */
649 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
654 struct target_ops
*target
; /* ignored */
660 if (len
> MAXBUFBYTES
)
661 xfersize
= MAXBUFBYTES
;
666 remote_write_bytes(memaddr
, myaddr
, xfersize
);
668 remote_read_bytes (memaddr
, myaddr
, xfersize
);
673 return origlen
; /* no error possible */
677 remote_files_info (ignore
)
678 struct target_ops
*ignore
;
680 printf ("Debugging a target over a serial line.\n");
685 A debug packet whose contents are <data>
686 is encapsulated for transmission in the form:
688 $ <data> # CSUM1 CSUM2
690 <data> must be ASCII alphanumeric and cannot include characters
693 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
694 checksum of <data>, the most significant nibble is sent first.
695 the hex digits 0-9,a-f are used.
697 Receiver responds with:
699 + - if CSUM is correct and ready for next packet
700 - - if CSUM is incorrect
704 /* Read a single character from the remote end.
705 (If supported, we actually read many characters and buffer them up.) */
710 static int inbuf_index
, inbuf_count
;
711 #define INBUFSIZE PBUFSIZ
712 static char inbuf
[INBUFSIZE
];
714 if (inbuf_index
>= inbuf_count
)
716 /* Time to do another read... */
719 inbuf
[0] = 0; /* Just in case */
721 /* termio does the timeout for us. */
722 inbuf_count
= read (remote_desc
, inbuf
, INBUFSIZE
);
725 inbuf_count
= read (remote_desc
, inbuf
, INBUFSIZE
);
730 /* Just return the next character from the buffer. */
731 return inbuf
[inbuf_index
++] & 0x7f;
734 /* Send the command in BUF to the remote machine,
735 and read the reply into BUF.
736 Report an error if we get an error reply. */
747 error ("Remote failure reply: %s", buf
);
750 /* Send a packet to the remote machine, with error checking.
751 The data of the packet is in BUF. */
758 unsigned char csum
= 0;
760 int cnt
= strlen (buf
);
764 /* Copy the packet into buffer BUF2, encapsulating it
765 and giving it a checksum. */
767 if (cnt
> sizeof(buf2
) - 5) /* Prosanity check */
773 for (i
= 0; i
< cnt
; i
++)
779 *p
++ = tohex ((csum
>> 4) & 0xf);
780 *p
++ = tohex (csum
& 0xf);
782 /* Send it over and over until we get a positive ack. */
788 printf ("Sending packet: %s...", buf2
); fflush(stdout
);
790 write (remote_desc
, buf2
, p
- buf2
);
792 /* read until either a timeout occurs (\0) or '+' is read */
799 printf ("%02X%c ", ch
&0xFF, ch
);
801 } while ((ch
!= '+') && (ch
!= '\0'));
805 /* Read a packet from the remote machine, with error checking,
806 and store it in BUF. BUF is expected to be of size PBUFSIZ. */
815 unsigned char c1
, c2
;
817 #define MAX_RETRIES 10
820 /* Sorry, this will cause all hell to break loose, i.e. we'll end
821 up in the command loop with an inferior, but (at least if this
822 happens in remote_wait or some such place) without a current_frame,
823 having set up prev_* in wait_for_inferior, etc.
825 If it is necessary to have such an "emergency exit", seems like
826 the only plausible thing to do is to say the inferior died, and
827 make the user reattach if they want to. Perhaps with a prompt
828 asking for confirmation. */
830 /* allow immediate quit while reading from device, it could be hung */
836 /* Force csum to be zero here because of possible error retry. */
839 while ((c
= readchar()) != '$');
847 if (bp
>= buf
+PBUFSIZ
-1)
850 printf_filtered ("Remote packet too long: %s\n", buf
);
858 c1
= fromhex (readchar ());
859 c2
= fromhex (readchar ());
860 if ((csum
& 0xff) == (c1
<< 4) + c2
)
862 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
863 (c1
<< 4) + c2
, csum
& 0xff, buf
);
865 /* Try the whole thing again. */
867 if (++retries
< MAX_RETRIES
)
869 write (remote_desc
, "-", 1);
873 printf ("Ignoring packet error, continuing...\n");
882 write (remote_desc
, "+", 1);
885 fprintf (stderr
,"Packet received: %s\n", buf
);
888 /* The data cache leads to incorrect results because it doesn't know about
889 volatile variables, thus making it impossible to debug functions which
890 use hardware registers. Therefore it is #if 0'd out. Effect on
891 performance is some, for backtraces of functions with a few
892 arguments each. For functions with many arguments, the stack
893 frames don't fit in the cache blocks, which makes the cache less
894 helpful. Disabling the cache is a big performance win for fetching
895 large structures, because the cache code fetched data in 16-byte
898 /* The data cache records all the data read from the remote machine
899 since the last time it stopped.
901 Each cache block holds 16 bytes of data
902 starting at a multiple-of-16 address. */
904 #define DCACHE_SIZE 64 /* Number of cache blocks */
906 struct dcache_block
{
907 struct dcache_block
*next
, *last
;
908 unsigned int addr
; /* Address for which data is recorded. */
912 struct dcache_block dcache_free
, dcache_valid
;
914 /* Free all the data cache blocks, thus discarding all cached data. */
919 register struct dcache_block
*db
;
921 while ((db
= dcache_valid
.next
) != &dcache_valid
)
924 insque (db
, &dcache_free
);
929 * If addr is present in the dcache, return the address of the block
933 struct dcache_block
*
936 register struct dcache_block
*db
;
941 /* Search all cache blocks for one that is at this address. */
942 db
= dcache_valid
.next
;
943 while (db
!= &dcache_valid
)
945 if ((addr
& 0xfffffff0) == db
->addr
)
952 /* Return the int data at address ADDR in dcache block DC. */
955 dcache_value (db
, addr
)
956 struct dcache_block
*db
;
961 return (db
->data
[(addr
>>2)&3]);
964 /* Get a free cache block, put it on the valid list,
965 and return its address. The caller should store into the block
966 the address and data that it describes. */
968 struct dcache_block
*
971 register struct dcache_block
*db
;
973 if ((db
= dcache_free
.next
) == &dcache_free
)
974 /* If we can't get one from the free list, take last valid */
975 db
= dcache_valid
.last
;
978 insque (db
, &dcache_valid
);
982 /* Return the contents of the word at address ADDR in the remote machine,
983 using the data cache. */
989 register struct dcache_block
*db
;
991 db
= dcache_hit (addr
);
994 db
= dcache_alloc ();
995 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
996 db
->addr
= addr
& ~0xf;
998 return (dcache_value (db
, addr
));
1001 /* Write the word at ADDR both in the data cache and in the remote machine. */
1003 dcache_poke (addr
, data
)
1007 register struct dcache_block
*db
;
1009 /* First make sure the word is IN the cache. DB is its cache block. */
1010 db
= dcache_hit (addr
);
1013 db
= dcache_alloc ();
1014 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
1015 db
->addr
= addr
& ~0xf;
1018 /* Modify the word in the cache. */
1019 db
->data
[(addr
>>2)&3] = data
;
1021 /* Send the changed word. */
1022 remote_write_bytes (addr
, &data
, 4);
1025 /* Initialize the data cache. */
1030 register struct dcache_block
*db
;
1032 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
1034 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
1035 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
1036 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
1037 insque (db
, &dcache_free
);
1041 /* Define the target subroutine names */
1043 struct target_ops remote_ops
= {
1044 "remote", /* to_shortname */
1045 "Remote serial target in gdb-specific protocol", /* to_longname */
1046 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1047 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1048 remote_open
, /* to_open */
1049 remote_close
, /* to_close */
1050 NULL
, /* to_attach */
1051 remote_detach
, /* to_detach */
1052 remote_resume
, /* to_resume */
1053 remote_wait
, /* to_wait */
1054 remote_fetch_registers
, /* to_fetch_registers */
1055 remote_store_registers
, /* to_store_registers */
1056 remote_prepare_to_store
, /* to_prepare_to_store */
1057 remote_xfer_memory
, /* to_xfer_memory */
1058 remote_files_info
, /* to_files_info */
1059 NULL
, /* to_insert_breakpoint */
1060 NULL
, /* to_remove_breakpoint */
1061 NULL
, /* to_terminal_init */
1062 NULL
, /* to_terminal_inferior */
1063 NULL
, /* to_terminal_ours_for_output */
1064 NULL
, /* to_terminal_ours */
1065 NULL
, /* to_terminal_info */
1068 NULL
, /* to_lookup_symbol */
1069 NULL
, /* to_create_inferior */
1070 NULL
, /* to_mourn_inferior */
1071 process_stratum
, /* to_stratum */
1073 1, /* to_has_all_memory */
1074 1, /* to_has_memory */
1075 1, /* to_has_stack */
1076 1, /* to_has_registers */
1077 1, /* to_has_execution */
1078 NULL
, /* sections */
1079 NULL
, /* sections_end */
1080 OPS_MAGIC
/* to_magic */
1084 _initialize_remote ()
1086 add_target (&remote_ops
);
1089 add_set_cmd ("remotedebug", no_class
, var_boolean
, (char *)&kiodebug
,
1090 "Set debugging of remote serial I/O.\n\
1091 When enabled, each packet sent or received with the remote target\n\
1092 is displayed.", &setlist
),