1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992, 1993 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>
92 /* Prototypes for local functions */
95 remote_write_bytes
PARAMS ((CORE_ADDR
, char *, int));
98 remote_read_bytes
PARAMS ((CORE_ADDR
, char *, int));
101 remote_files_info
PARAMS ((struct target_ops
*));
104 remote_xfer_memory
PARAMS ((CORE_ADDR
, char *, int, int, struct target_ops
*));
107 remote_prepare_to_store
PARAMS ((void));
110 remote_fetch_registers
PARAMS ((int));
113 remote_resume
PARAMS ((int, int));
116 remote_start_remote
PARAMS ((char *));
119 remote_open
PARAMS ((char *, int));
122 remote_close
PARAMS ((int));
125 remote_store_registers
PARAMS ((int));
128 getpkt
PARAMS ((char *, int));
131 putpkt
PARAMS ((char *));
134 remote_send
PARAMS ((char *));
137 readchar
PARAMS ((void));
140 remote_wait
PARAMS ((WAITTYPE
*));
143 tohex
PARAMS ((int));
146 fromhex
PARAMS ((int));
149 remote_detach
PARAMS ((char *, int));
151 extern struct target_ops remote_ops
; /* Forward decl */
153 static int kiodebug
= 0;
154 /* This was 5 seconds, which is a long time to sit and wait.
155 Unless this is going though some terminal server or multiplexer or
156 other form of hairy serial connection, I would think 2 seconds would
158 static int timeout
= 2;
164 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
165 remote_open knows that we don't have a file open when the program
167 serial_t remote_desc
= NULL
;
171 /* Maximum number of bytes to read/write at once. The value here
172 is chosen to fill up a packet (the headers account for the 32). */
173 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
175 /* Round up PBUFSIZ to hold all the registers, at least. */
176 #if REGISTER_BYTES > MAXBUFBYTES
178 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
181 /* Clean up connection to a remote debugger. */
185 remote_close (quitting
)
189 SERIAL_CLOSE (remote_desc
);
193 /* Stub for catch_errors. */
196 remote_start_remote (dummy
)
199 /* Ack any packet which the remote side has already sent. */
200 SERIAL_WRITE (remote_desc
, "+\r", 2);
201 putpkt ("?"); /* initiate a query from remote machine */
203 start_remote (); /* Initialize gdb process mechanisms */
207 /* Open a connection to a remote debugger.
208 NAME is the filename used for communication. */
211 remote_open (name
, from_tty
)
217 "To open a remote debug connection, you need to specify what serial\n\
218 device is attached to the remote system (e.g. /dev/ttya).");
220 target_preopen (from_tty
);
222 unpush_target (&remote_ops
);
228 remote_desc
= SERIAL_OPEN (name
);
230 perror_with_name (name
);
236 if (sscanf (baud_rate
, "%d", &rate
) == 1)
237 if (SERIAL_SETBAUDRATE (remote_desc
, rate
))
239 SERIAL_CLOSE (remote_desc
);
240 perror_with_name (name
);
244 SERIAL_RAW (remote_desc
);
248 puts_filtered ("Remote debugging using ");
249 puts_filtered (name
);
250 puts_filtered ("\n");
252 push_target (&remote_ops
); /* Switch to using remote target now */
254 /* Start the remote connection; if error (0), discard this target. */
255 immediate_quit
++; /* Allow user to interrupt it */
256 if (!catch_errors (remote_start_remote
, (char *)0,
257 "Couldn't establish connection to remote target\n"))
262 takes a program previously attached to and detaches it.
263 We better not have left any breakpoints
264 in the program or it'll die when it hits one.
265 Close the open connection to the remote debugger.
266 Use this when you want to detach and do something else
270 remote_detach (args
, from_tty
)
275 error ("Argument given to \"detach\" when remotely debugging.");
279 puts_filtered ("Ending remote debugging.\n");
282 /* Convert hex digit A to a number. */
288 if (a
>= '0' && a
<= '9')
290 else if (a
>= 'a' && a
<= 'f')
293 error ("Reply contains invalid hex digit");
297 /* Convert number NIB to a hex digit. */
309 /* Tell the remote machine to resume. */
312 remote_resume (step
, siggnal
)
320 target_terminal_ours_for_output ();
321 printf_filtered ("Can't send signals to a remote system. ");
322 name
= strsigno (siggnal
);
324 printf_filtered (name
);
326 printf_filtered ("Signal %d", siggnal
);
327 printf_filtered (" not sent.\n");
328 target_terminal_inferior ();
335 strcpy (buf
, step
? "s": "c");
340 static void remote_interrupt_twice
PARAMS ((int));
341 static void (*ofunc
)();
343 /* Send ^C to target to halt it. Target will respond, and send us a
346 void remote_interrupt(signo
)
349 /* If this doesn't work, try more severe steps. */
350 signal (signo
, remote_interrupt_twice
);
353 printf ("remote_interrupt called\n");
355 SERIAL_WRITE (remote_desc
, "\003", 1); /* Send a ^C */
358 /* The user typed ^C twice. */
360 remote_interrupt_twice (signo
)
363 signal (signo
, ofunc
);
365 target_terminal_ours ();
366 if (query ("Interrupted while waiting for the inferior.\n\
367 Give up (and stop debugging it)? "))
369 target_mourn_inferior ();
370 return_to_top_level ();
374 signal (signo
, remote_interrupt
);
375 target_terminal_inferior ();
379 /* Wait until the remote machine stops, then return,
380 storing status in STATUS just as `wait' would.
381 Returns "pid" (though it's not clear what, if anything, that
382 means in the case of this target). */
388 unsigned char buf
[PBUFSIZ
];
392 char regs
[MAX_REGISTER_RAW_SIZE
];
394 WSETEXIT ((*status
), 0);
396 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
397 getpkt ((char *) buf
, 1);
398 signal (SIGINT
, ofunc
);
401 error ("Remote failure reply: %s", buf
);
404 /* Expedited reply, containing Signal, {regno, reg} repeat */
405 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
407 n... = register number
408 r... = register contents
411 p
= &buf
[3]; /* after Txx */
415 regno
= strtol (p
, &p
, 16); /* Read the register number */
418 || regno
>= NUM_REGS
)
419 error ("Remote sent bad register number %s", buf
);
421 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
423 if (p
[0] == 0 || p
[1] == 0)
424 error ("Remote reply is too short: %s", buf
);
425 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
430 error("Remote register badly formatted: %s", buf
);
432 supply_register (regno
, regs
);
435 else if (buf
[0] != 'S')
436 error ("Invalid remote reply: %s", buf
);
438 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
443 /* Read the remote registers into the block REGS. */
444 /* Currently we just read all the registers, so we don't use regno. */
447 remote_fetch_registers (regno
)
453 char regs
[REGISTER_BYTES
];
458 /* Reply describes registers byte by byte, each byte encoded as two
459 hex characters. Suck them all up, then supply them to the
460 register cacheing/storage mechanism. */
463 for (i
= 0; i
< REGISTER_BYTES
; i
++)
465 if (p
[0] == 0 || p
[1] == 0)
466 error ("Remote reply is too short: %s", buf
);
467 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
470 for (i
= 0; i
< NUM_REGS
; i
++)
471 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
474 /* Prepare to store registers. Since we send them all, we have to
475 read out the ones we don't want to change first. */
478 remote_prepare_to_store ()
480 /* Make sure the entire registers array is valid. */
481 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
484 /* Store the remote registers from the contents of the block REGISTERS.
485 FIXME, eventually just store one register if that's all that is needed. */
489 remote_store_registers (regno
)
498 /* Command describes registers byte by byte,
499 each byte encoded as two hex characters. */
502 for (i
= 0; i
< REGISTER_BYTES
; i
++)
504 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
505 *p
++ = tohex (registers
[i
] & 0xf);
513 /* Read a word from remote address ADDR and return it.
514 This goes through the data cache. */
517 remote_fetch_word (addr
)
522 extern CORE_ADDR text_start
, text_end
;
524 if (addr
>= text_start
&& addr
< text_end
)
527 xfer_core_file (addr
, &buffer
, sizeof (int));
531 return dcache_fetch (addr
);
534 /* Write a word WORD into remote address ADDR.
535 This goes through the data cache. */
538 remote_store_word (addr
, word
)
542 dcache_poke (addr
, word
);
546 /* Write memory data directly to the remote machine.
547 This does not inform the data cache; the data cache uses this.
548 MEMADDR is the address in the remote memory space.
549 MYADDR is the address of the buffer in our space.
550 LEN is the number of bytes. */
553 remote_write_bytes (memaddr
, myaddr
, len
)
562 if (len
> PBUFSIZ
/ 2 - 20)
565 sprintf (buf
, "M%x,%x:", memaddr
, len
);
567 /* We send target system values byte by byte, in increasing byte addresses,
568 each byte encoded as two hex characters. */
570 p
= buf
+ strlen (buf
);
571 for (i
= 0; i
< len
; i
++)
573 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
574 *p
++ = tohex (myaddr
[i
] & 0xf);
581 /* Read memory data directly from the remote machine.
582 This does not use the data cache; the data cache uses this.
583 MEMADDR is the address in the remote memory space.
584 MYADDR is the address of the buffer in our space.
585 LEN is the number of bytes. */
588 remote_read_bytes (memaddr
, myaddr
, len
)
597 if (len
> PBUFSIZ
/ 2 - 1)
600 sprintf (buf
, "m%x,%x", memaddr
, len
);
603 /* Reply describes memory byte by byte,
604 each byte encoded as two hex characters. */
607 for (i
= 0; i
< len
; i
++)
609 if (p
[0] == 0 || p
[1] == 0)
610 error ("Remote reply is too short: %s", buf
);
611 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
616 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
617 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
618 nonzero. Returns length of data written or read; 0 for error. */
622 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
627 struct target_ops
*target
; /* ignored */
633 if (len
> MAXBUFBYTES
)
634 xfersize
= MAXBUFBYTES
;
639 remote_write_bytes(memaddr
, myaddr
, xfersize
);
641 remote_read_bytes (memaddr
, myaddr
, xfersize
);
646 return origlen
; /* no error possible */
650 remote_files_info (ignore
)
651 struct target_ops
*ignore
;
653 puts_filtered ("Debugging a target over a serial line.\n");
658 A debug packet whose contents are <data>
659 is encapsulated for transmission in the form:
661 $ <data> # CSUM1 CSUM2
663 <data> must be ASCII alphanumeric and cannot include characters
666 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
667 checksum of <data>, the most significant nibble is sent first.
668 the hex digits 0-9,a-f are used.
670 Receiver responds with:
672 + - if CSUM is correct and ready for next packet
673 - - if CSUM is incorrect
677 /* Read a single character from the remote end, masking it down to 7 bits. */
684 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
692 /* Send the command in BUF to the remote machine,
693 and read the reply into BUF.
694 Report an error if we get an error reply. */
705 error ("Remote failure reply: %s", buf
);
708 /* Send a packet to the remote machine, with error checking.
709 The data of the packet is in BUF. */
716 unsigned char csum
= 0;
718 int cnt
= strlen (buf
);
722 /* Copy the packet into buffer BUF2, encapsulating it
723 and giving it a checksum. */
725 if (cnt
> sizeof(buf2
) - 5) /* Prosanity check */
731 for (i
= 0; i
< cnt
; i
++)
737 *p
++ = tohex ((csum
>> 4) & 0xf);
738 *p
++ = tohex (csum
& 0xf);
740 /* Send it over and over until we get a positive ack. */
746 printf ("Sending packet: %s...", buf2
); fflush(stdout
);
748 SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
);
750 /* read until either a timeout occurs (-2) or '+' is read */
757 printf ("%02X%c ", ch
&0xFF, ch
);
759 } while ((ch
!= '+') && (ch
!= SERIAL_TIMEOUT
));
763 /* Read a packet from the remote machine, with error checking,
764 and store it in BUF. BUF is expected to be of size PBUFSIZ.
765 If FOREVER, wait forever rather than timing out; this is used
766 while the target is executing user code. */
769 getpkt (buf
, forever
)
776 unsigned char c1
, c2
;
778 #define MAX_RETRIES 10
782 /* This can loop forever if the remote side sends us characters
783 continuously, but if it pauses, we'll get a zero from readchar
784 because of timeout. Then we'll count that as a retry. */
786 if ((c
= readchar()) == SERIAL_TIMEOUT
)
789 if (++retries
>= MAX_RETRIES
)
790 if (kiodebug
) puts_filtered ("Timed out.\n");
794 /* Force csum to be zero here because of possible error retry. */
801 if (c
== SERIAL_TIMEOUT
)
804 puts_filtered ("Timeout in mid-packet, retrying\n");
805 goto whole
; /* Start a new packet, count retries */
810 puts_filtered ("Saw new packet start in middle of old one\n");
811 goto whole
; /* Start a new packet, count retries */
815 if (bp
>= buf
+PBUFSIZ
-1)
818 puts_filtered ("Remote packet too long: ");
820 puts_filtered ("\n");
828 c1
= fromhex (readchar ());
829 c2
= fromhex (readchar ());
830 if ((csum
& 0xff) == (c1
<< 4) + c2
)
832 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
833 (c1
<< 4) + c2
, csum
& 0xff);
835 puts_filtered ("\n");
837 /* Try the whole thing again. */
839 if (++retries
< MAX_RETRIES
)
841 SERIAL_WRITE (remote_desc
, "-", 1);
845 printf ("Ignoring packet error, continuing...\n");
852 SERIAL_WRITE (remote_desc
, "+", 1);
855 fprintf (stderr
,"Packet received: %s\n", buf
);
858 /* The data cache leads to incorrect results because it doesn't know about
859 volatile variables, thus making it impossible to debug functions which
860 use hardware registers. Therefore it is #if 0'd out. Effect on
861 performance is some, for backtraces of functions with a few
862 arguments each. For functions with many arguments, the stack
863 frames don't fit in the cache blocks, which makes the cache less
864 helpful. Disabling the cache is a big performance win for fetching
865 large structures, because the cache code fetched data in 16-byte
868 /* The data cache records all the data read from the remote machine
869 since the last time it stopped.
871 Each cache block holds 16 bytes of data
872 starting at a multiple-of-16 address. */
874 #define DCACHE_SIZE 64 /* Number of cache blocks */
876 struct dcache_block
{
877 struct dcache_block
*next
, *last
;
878 unsigned int addr
; /* Address for which data is recorded. */
882 struct dcache_block dcache_free
, dcache_valid
;
884 /* Free all the data cache blocks, thus discarding all cached data. */
889 register struct dcache_block
*db
;
891 while ((db
= dcache_valid
.next
) != &dcache_valid
)
894 insque (db
, &dcache_free
);
899 * If addr is present in the dcache, return the address of the block
903 struct dcache_block
*
906 register struct dcache_block
*db
;
911 /* Search all cache blocks for one that is at this address. */
912 db
= dcache_valid
.next
;
913 while (db
!= &dcache_valid
)
915 if ((addr
& 0xfffffff0) == db
->addr
)
922 /* Return the int data at address ADDR in dcache block DC. */
925 dcache_value (db
, addr
)
926 struct dcache_block
*db
;
931 return (db
->data
[(addr
>>2)&3]);
934 /* Get a free cache block, put it on the valid list,
935 and return its address. The caller should store into the block
936 the address and data that it describes. */
938 struct dcache_block
*
941 register struct dcache_block
*db
;
943 if ((db
= dcache_free
.next
) == &dcache_free
)
944 /* If we can't get one from the free list, take last valid */
945 db
= dcache_valid
.last
;
948 insque (db
, &dcache_valid
);
952 /* Return the contents of the word at address ADDR in the remote machine,
953 using the data cache. */
959 register struct dcache_block
*db
;
961 db
= dcache_hit (addr
);
964 db
= dcache_alloc ();
965 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
966 db
->addr
= addr
& ~0xf;
968 return (dcache_value (db
, addr
));
971 /* Write the word at ADDR both in the data cache and in the remote machine. */
973 dcache_poke (addr
, data
)
977 register struct dcache_block
*db
;
979 /* First make sure the word is IN the cache. DB is its cache block. */
980 db
= dcache_hit (addr
);
983 db
= dcache_alloc ();
984 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
985 db
->addr
= addr
& ~0xf;
988 /* Modify the word in the cache. */
989 db
->data
[(addr
>>2)&3] = data
;
991 /* Send the changed word. */
992 remote_write_bytes (addr
, &data
, 4);
995 /* Initialize the data cache. */
1000 register struct dcache_block
*db
;
1002 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
1004 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
1005 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
1006 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
1007 insque (db
, &dcache_free
);
1015 /* Don't wait for it to die. I'm not really sure it matters whether
1016 we do or not. For the existing stubs, kill is a noop. */
1017 target_mourn_inferior ();
1023 unpush_target (&remote_ops
);
1024 generic_mourn_inferior ();
1027 /* Define the target subroutine names */
1029 struct target_ops remote_ops
= {
1030 "remote", /* to_shortname */
1031 "Remote serial target in gdb-specific protocol", /* to_longname */
1032 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1033 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1034 remote_open
, /* to_open */
1035 remote_close
, /* to_close */
1036 NULL
, /* to_attach */
1037 remote_detach
, /* to_detach */
1038 remote_resume
, /* to_resume */
1039 remote_wait
, /* to_wait */
1040 remote_fetch_registers
, /* to_fetch_registers */
1041 remote_store_registers
, /* to_store_registers */
1042 remote_prepare_to_store
, /* to_prepare_to_store */
1043 remote_xfer_memory
, /* to_xfer_memory */
1044 remote_files_info
, /* to_files_info */
1045 NULL
, /* to_insert_breakpoint */
1046 NULL
, /* to_remove_breakpoint */
1047 NULL
, /* to_terminal_init */
1048 NULL
, /* to_terminal_inferior */
1049 NULL
, /* to_terminal_ours_for_output */
1050 NULL
, /* to_terminal_ours */
1051 NULL
, /* to_terminal_info */
1052 remote_kill
, /* to_kill */
1054 NULL
, /* to_lookup_symbol */
1055 NULL
, /* to_create_inferior */
1056 remote_mourn
, /* to_mourn_inferior */
1058 0, /* to_notice_signals */
1059 process_stratum
, /* to_stratum */
1061 1, /* to_has_all_memory */
1062 1, /* to_has_memory */
1063 1, /* to_has_stack */
1064 1, /* to_has_registers */
1065 1, /* to_has_execution */
1066 NULL
, /* sections */
1067 NULL
, /* sections_end */
1068 OPS_MAGIC
/* to_magic */
1072 _initialize_remote ()
1074 add_target (&remote_ops
);
1077 add_set_cmd ("remotedebug", no_class
, var_boolean
, (char *)&kiodebug
,
1078 "Set debugging of remote serial I/O.\n\
1079 When enabled, each packet sent or received with the remote target\n\
1080 is displayed.", &setlist
),
This page took 0.050918 seconds and 4 git commands to generate.