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.
22 A debug packet whose contents are <data>
23 is encapsulated for transmission in the form:
25 $ <data> # CSUM1 CSUM2
27 <data> must be ASCII alphanumeric and cannot include characters
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.
34 Receiver responds with:
36 + - if CSUM is correct and ready for next packet
37 - - if CSUM is incorrect
40 All values are encoded in ascii hex digits.
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.
52 write regs GXX..XX Each byte of register data
53 is described by two hex digits.
57 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
58 reply XX..XX XX..XX is mem contents
61 write mem MAA..AA,LLLL:XX..XX
63 LLLL is number of bytes,
68 cont cAA..AA AA..AA is address to resume
70 resume at same address.
72 step sAA..AA AA..AA is address to resume
74 resume at same address.
76 last signal ? Reply the current reason for stopping.
77 This is the same reply as is generated
78 for step or cont : SAA where AA is the
81 There is no immediate reply to step or cont.
82 The reply comes when the machine stops.
83 It is SAA AA is the "signal number"
85 or... TAAn...:r...;n:r...;n...:r...;
87 n... = register number
88 r... = register contents
102 #include "terminal.h"
105 #if !defined(DONT_USE_REMOTE)
107 #include <sys/types.h>
113 /* Prototypes for local functions */
116 remote_write_bytes
PARAMS ((CORE_ADDR
, char *, int));
119 remote_read_bytes
PARAMS ((CORE_ADDR
, char *, int));
122 remote_files_info
PARAMS ((struct target_ops
*));
125 remote_xfer_memory
PARAMS ((CORE_ADDR
, char *, int, int, struct target_ops
*));
128 remote_prepare_to_store
PARAMS ((void));
131 remote_fetch_registers
PARAMS ((int));
134 remote_resume
PARAMS ((int, int));
137 remote_start_remote
PARAMS ((char *));
140 remote_open
PARAMS ((char *, int));
143 remote_close
PARAMS ((int));
146 remote_store_registers
PARAMS ((int));
149 getpkt
PARAMS ((char *, int));
152 putpkt
PARAMS ((char *));
155 remote_send
PARAMS ((char *));
158 readchar
PARAMS ((void));
161 remote_wait
PARAMS ((WAITTYPE
*));
164 tohex
PARAMS ((int));
167 fromhex
PARAMS ((int));
170 remote_detach
PARAMS ((char *, int));
172 extern struct target_ops remote_ops
; /* Forward decl */
174 static int kiodebug
= 0;
175 /* This was 5 seconds, which is a long time to sit and wait.
176 Unless this is going though some terminal server or multiplexer or
177 other form of hairy serial connection, I would think 2 seconds would
179 static int timeout
= 2;
185 /* Descriptor for I/O to remote machine. Initialize it to NULL so that
186 remote_open knows that we don't have a file open when the program
188 serial_t remote_desc
= NULL
;
192 /* Maximum number of bytes to read/write at once. The value here
193 is chosen to fill up a packet (the headers account for the 32). */
194 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
196 /* Round up PBUFSIZ to hold all the registers, at least. */
197 #if REGISTER_BYTES > MAXBUFBYTES
199 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
202 /* Clean up connection to a remote debugger. */
206 remote_close (quitting
)
210 SERIAL_CLOSE (remote_desc
);
214 /* Stub for catch_errors. */
217 remote_start_remote (dummy
)
220 /* Ack any packet which the remote side has already sent. */
221 /* I'm not sure this \r is needed; we don't use it any other time we
223 SERIAL_WRITE (remote_desc
, "+\r", 2);
224 putpkt ("?"); /* initiate a query from remote machine */
226 start_remote (); /* Initialize gdb process mechanisms */
230 /* Open a connection to a remote debugger.
231 NAME is the filename used for communication. */
234 remote_open (name
, from_tty
)
240 "To open a remote debug connection, you need to specify what serial\n\
241 device is attached to the remote system (e.g. /dev/ttya).");
243 target_preopen (from_tty
);
245 unpush_target (&remote_ops
);
251 remote_desc
= SERIAL_OPEN (name
);
253 perror_with_name (name
);
259 if (sscanf (baud_rate
, "%d", &rate
) == 1)
260 if (SERIAL_SETBAUDRATE (remote_desc
, rate
))
262 SERIAL_CLOSE (remote_desc
);
263 perror_with_name (name
);
267 SERIAL_RAW (remote_desc
);
271 puts_filtered ("Remote debugging using ");
272 puts_filtered (name
);
273 puts_filtered ("\n");
275 push_target (&remote_ops
); /* Switch to using remote target now */
277 /* Start the remote connection; if error (0), discard this target. */
278 immediate_quit
++; /* Allow user to interrupt it */
279 if (!catch_errors (remote_start_remote
, (char *)0,
280 "Couldn't establish connection to remote target\n", RETURN_MASK_ALL
))
285 takes a program previously attached to and detaches it.
286 We better not have left any breakpoints
287 in the program or it'll die when it hits one.
288 Close the open connection to the remote debugger.
289 Use this when you want to detach and do something else
293 remote_detach (args
, from_tty
)
298 error ("Argument given to \"detach\" when remotely debugging.");
302 puts_filtered ("Ending remote debugging.\n");
305 /* Convert hex digit A to a number. */
311 if (a
>= '0' && a
<= '9')
313 else if (a
>= 'a' && a
<= 'f')
316 error ("Reply contains invalid hex digit");
320 /* Convert number NIB to a hex digit. */
332 /* Tell the remote machine to resume. */
335 remote_resume (step
, siggnal
)
343 target_terminal_ours_for_output ();
344 printf_filtered ("Can't send signals to a remote system. ");
345 name
= strsigno (siggnal
);
347 printf_filtered (name
);
349 printf_filtered ("Signal %d", siggnal
);
350 printf_filtered (" not sent.\n");
351 target_terminal_inferior ();
358 strcpy (buf
, step
? "s": "c");
363 static void remote_interrupt_twice
PARAMS ((int));
364 static void (*ofunc
)();
366 /* Send ^C to target to halt it. Target will respond, and send us a
369 void remote_interrupt(signo
)
372 /* If this doesn't work, try more severe steps. */
373 signal (signo
, remote_interrupt_twice
);
376 printf ("remote_interrupt called\n");
378 SERIAL_WRITE (remote_desc
, "\003", 1); /* Send a ^C */
381 /* The user typed ^C twice. */
383 remote_interrupt_twice (signo
)
386 signal (signo
, ofunc
);
388 target_terminal_ours ();
389 if (query ("Interrupted while waiting for the program.\n\
390 Give up (and stop debugging it)? "))
392 target_mourn_inferior ();
393 return_to_top_level (RETURN_QUIT
);
397 signal (signo
, remote_interrupt
);
398 target_terminal_inferior ();
402 /* Wait until the remote machine stops, then return,
403 storing status in STATUS just as `wait' would.
404 Returns "pid" (though it's not clear what, if anything, that
405 means in the case of this target). */
411 unsigned char buf
[PBUFSIZ
];
415 char regs
[MAX_REGISTER_RAW_SIZE
];
417 WSETEXIT ((*status
), 0);
419 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
420 getpkt ((char *) buf
, 1);
421 signal (SIGINT
, ofunc
);
424 error ("Remote failure reply: %s", buf
);
427 /* Expedited reply, containing Signal, {regno, reg} repeat */
428 /* format is: 'Tssn...:r...;n...:r...;n...:r...;#cc', where
430 n... = register number
431 r... = register contents
434 p
= &buf
[3]; /* after Txx */
438 regno
= strtol (p
, &p
, 16); /* Read the register number */
441 || regno
>= NUM_REGS
)
442 error ("Remote sent bad register number %s", buf
);
444 for (i
= 0; i
< REGISTER_RAW_SIZE (regno
); i
++)
446 if (p
[0] == 0 || p
[1] == 0)
447 error ("Remote reply is too short: %s", buf
);
448 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
453 error("Remote register badly formatted: %s", buf
);
455 supply_register (regno
, regs
);
458 else if (buf
[0] != 'S')
459 error ("Invalid remote reply: %s", buf
);
461 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
466 /* Read the remote registers into the block REGS. */
467 /* Currently we just read all the registers, so we don't use regno. */
470 remote_fetch_registers (regno
)
476 char regs
[REGISTER_BYTES
];
481 /* Reply describes registers byte by byte, each byte encoded as two
482 hex characters. Suck them all up, then supply them to the
483 register cacheing/storage mechanism. */
486 for (i
= 0; i
< REGISTER_BYTES
; i
++)
488 if (p
[0] == 0 || p
[1] == 0)
489 error ("Remote reply is too short: %s", buf
);
490 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
493 for (i
= 0; i
< NUM_REGS
; i
++)
494 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
497 /* Prepare to store registers. Since we send them all, we have to
498 read out the ones we don't want to change first. */
501 remote_prepare_to_store ()
503 /* Make sure the entire registers array is valid. */
504 read_register_bytes (0, (char *)NULL
, REGISTER_BYTES
);
507 /* Store the remote registers from the contents of the block REGISTERS.
508 FIXME, eventually just store one register if that's all that is needed. */
512 remote_store_registers (regno
)
521 /* Command describes registers byte by byte,
522 each byte encoded as two hex characters. */
525 for (i
= 0; i
< REGISTER_BYTES
; i
++)
527 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
528 *p
++ = tohex (registers
[i
] & 0xf);
536 /* Read a word from remote address ADDR and return it.
537 This goes through the data cache. */
540 remote_fetch_word (addr
)
545 extern CORE_ADDR text_start
, text_end
;
547 if (addr
>= text_start
&& addr
< text_end
)
550 xfer_core_file (addr
, &buffer
, sizeof (int));
554 return dcache_fetch (addr
);
557 /* Write a word WORD into remote address ADDR.
558 This goes through the data cache. */
561 remote_store_word (addr
, word
)
565 dcache_poke (addr
, word
);
569 /* Write memory data directly to the remote machine.
570 This does not inform the data cache; the data cache uses this.
571 MEMADDR is the address in the remote memory space.
572 MYADDR is the address of the buffer in our space.
573 LEN is the number of bytes. */
576 remote_write_bytes (memaddr
, myaddr
, len
)
585 if (len
> PBUFSIZ
/ 2 - 20)
588 sprintf (buf
, "M%x,%x:", memaddr
, len
);
590 /* We send target system values byte by byte, in increasing byte addresses,
591 each byte encoded as two hex characters. */
593 p
= buf
+ strlen (buf
);
594 for (i
= 0; i
< len
; i
++)
596 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
597 *p
++ = tohex (myaddr
[i
] & 0xf);
604 /* Read memory data directly from the remote machine.
605 This does not use the data cache; the data cache uses this.
606 MEMADDR is the address in the remote memory space.
607 MYADDR is the address of the buffer in our space.
608 LEN is the number of bytes. */
611 remote_read_bytes (memaddr
, myaddr
, len
)
620 if (len
> PBUFSIZ
/ 2 - 1)
623 sprintf (buf
, "m%x,%x", memaddr
, len
);
626 /* Reply describes memory byte by byte,
627 each byte encoded as two hex characters. */
630 for (i
= 0; i
< len
; i
++)
632 if (p
[0] == 0 || p
[1] == 0)
633 error ("Remote reply is too short: %s", buf
);
634 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
639 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
640 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
641 nonzero. Returns length of data written or read; 0 for error. */
645 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
650 struct target_ops
*target
; /* ignored */
656 if (len
> MAXBUFBYTES
)
657 xfersize
= MAXBUFBYTES
;
662 remote_write_bytes(memaddr
, myaddr
, xfersize
);
664 remote_read_bytes (memaddr
, myaddr
, xfersize
);
669 return origlen
; /* no error possible */
673 remote_files_info (ignore
)
674 struct target_ops
*ignore
;
676 puts_filtered ("Debugging a target over a serial line.\n");
679 /* Stuff for dealing with the packets which are part of this protocol.
680 See comment at top of file for details. */
682 /* Read a single character from the remote end, masking it down to 7 bits. */
689 ch
= SERIAL_READCHAR (remote_desc
, timeout
);
697 /* Send the command in BUF to the remote machine,
698 and read the reply into BUF.
699 Report an error if we get an error reply. */
710 error ("Remote failure reply: %s", buf
);
713 /* Send a packet to the remote machine, with error checking.
714 The data of the packet is in BUF. */
721 unsigned char csum
= 0;
723 int cnt
= strlen (buf
);
727 /* Copy the packet into buffer BUF2, encapsulating it
728 and giving it a checksum. */
730 if (cnt
> sizeof(buf2
) - 5) /* Prosanity check */
736 for (i
= 0; i
< cnt
; i
++)
742 *p
++ = tohex ((csum
>> 4) & 0xf);
743 *p
++ = tohex (csum
& 0xf);
745 /* Send it over and over until we get a positive ack. */
752 printf ("Sending packet: %s...", buf2
); fflush(stdout
);
754 if (SERIAL_WRITE (remote_desc
, buf2
, p
- buf2
))
755 perror_with_name ("putpkt: write failed");
757 /* read until either a timeout occurs (-2) or '+' is read */
769 break; /* Retransmit buffer */
771 perror_with_name ("putpkt: couldn't read ACK");
773 error ("putpkt: EOF while trying to read ACK");
776 printf ("%02X %c ", ch
&0xFF, ch
);
779 break; /* Here to retransmit */
784 /* Read a packet from the remote machine, with error checking,
785 and store it in BUF. BUF is expected to be of size PBUFSIZ.
786 If FOREVER, wait forever rather than timing out; this is used
787 while the target is executing user code. */
790 getpkt (buf
, forever
)
797 unsigned char c1
, c2
;
799 #define MAX_RETRIES 10
803 /* This can loop forever if the remote side sends us characters
804 continuously, but if it pauses, we'll get a zero from readchar
805 because of timeout. Then we'll count that as a retry. */
808 if (c
> 0 && c
!= '$')
811 if (c
== SERIAL_TIMEOUT
)
815 if (++retries
>= MAX_RETRIES
)
816 if (kiodebug
) puts_filtered ("Timed out.\n");
821 error ("Remote connection closed");
822 if (c
== SERIAL_ERROR
)
823 perror_with_name ("Remote communication error");
825 /* Force csum to be zero here because of possible error retry. */
832 if (c
== SERIAL_TIMEOUT
)
835 puts_filtered ("Timeout in mid-packet, retrying\n");
836 goto whole
; /* Start a new packet, count retries */
841 puts_filtered ("Saw new packet start in middle of old one\n");
842 goto whole
; /* Start a new packet, count retries */
846 if (bp
>= buf
+PBUFSIZ
-1)
849 puts_filtered ("Remote packet too long: ");
851 puts_filtered ("\n");
859 c1
= fromhex (readchar ());
860 c2
= fromhex (readchar ());
861 if ((csum
& 0xff) == (c1
<< 4) + c2
)
863 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
864 (c1
<< 4) + c2
, csum
& 0xff);
866 puts_filtered ("\n");
868 /* Try the whole thing again. */
870 if (++retries
< MAX_RETRIES
)
872 SERIAL_WRITE (remote_desc
, "-", 1);
876 printf ("Ignoring packet error, continuing...\n");
883 SERIAL_WRITE (remote_desc
, "+", 1);
886 fprintf (stderr
,"Packet received: %s\n", buf
);
889 /* The data cache leads to incorrect results because it doesn't know about
890 volatile variables, thus making it impossible to debug functions which
891 use hardware registers. Therefore it is #if 0'd out. Effect on
892 performance is some, for backtraces of functions with a few
893 arguments each. For functions with many arguments, the stack
894 frames don't fit in the cache blocks, which makes the cache less
895 helpful. Disabling the cache is a big performance win for fetching
896 large structures, because the cache code fetched data in 16-byte
899 /* The data cache records all the data read from the remote machine
900 since the last time it stopped.
902 Each cache block holds 16 bytes of data
903 starting at a multiple-of-16 address. */
905 #define DCACHE_SIZE 64 /* Number of cache blocks */
907 struct dcache_block
{
908 struct dcache_block
*next
, *last
;
909 unsigned int addr
; /* Address for which data is recorded. */
913 struct dcache_block dcache_free
, dcache_valid
;
915 /* Free all the data cache blocks, thus discarding all cached data. */
920 register struct dcache_block
*db
;
922 while ((db
= dcache_valid
.next
) != &dcache_valid
)
925 insque (db
, &dcache_free
);
930 * If addr is present in the dcache, return the address of the block
934 struct dcache_block
*
937 register struct dcache_block
*db
;
942 /* Search all cache blocks for one that is at this address. */
943 db
= dcache_valid
.next
;
944 while (db
!= &dcache_valid
)
946 if ((addr
& 0xfffffff0) == db
->addr
)
953 /* Return the int data at address ADDR in dcache block DC. */
956 dcache_value (db
, addr
)
957 struct dcache_block
*db
;
962 return (db
->data
[(addr
>>2)&3]);
965 /* Get a free cache block, put it on the valid list,
966 and return its address. The caller should store into the block
967 the address and data that it describes. */
969 struct dcache_block
*
972 register struct dcache_block
*db
;
974 if ((db
= dcache_free
.next
) == &dcache_free
)
975 /* If we can't get one from the free list, take last valid */
976 db
= dcache_valid
.last
;
979 insque (db
, &dcache_valid
);
983 /* Return the contents of the word at address ADDR in the remote machine,
984 using the data cache. */
990 register struct dcache_block
*db
;
992 db
= dcache_hit (addr
);
995 db
= dcache_alloc ();
996 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
997 db
->addr
= addr
& ~0xf;
999 return (dcache_value (db
, addr
));
1002 /* Write the word at ADDR both in the data cache and in the remote machine. */
1004 dcache_poke (addr
, data
)
1008 register struct dcache_block
*db
;
1010 /* First make sure the word is IN the cache. DB is its cache block. */
1011 db
= dcache_hit (addr
);
1014 db
= dcache_alloc ();
1015 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
1016 db
->addr
= addr
& ~0xf;
1019 /* Modify the word in the cache. */
1020 db
->data
[(addr
>>2)&3] = data
;
1022 /* Send the changed word. */
1023 remote_write_bytes (addr
, &data
, 4);
1026 /* Initialize the data cache. */
1031 register struct dcache_block
*db
;
1033 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
1035 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
1036 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
1037 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
1038 insque (db
, &dcache_free
);
1046 /* Don't wait for it to die. I'm not really sure it matters whether
1047 we do or not. For the existing stubs, kill is a noop. */
1048 target_mourn_inferior ();
1054 unpush_target (&remote_ops
);
1055 generic_mourn_inferior ();
1058 /* Define the target subroutine names */
1060 struct target_ops remote_ops
= {
1061 "remote", /* to_shortname */
1062 "Remote serial target in gdb-specific protocol", /* to_longname */
1063 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1064 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
1065 remote_open
, /* to_open */
1066 remote_close
, /* to_close */
1067 NULL
, /* to_attach */
1068 remote_detach
, /* to_detach */
1069 remote_resume
, /* to_resume */
1070 remote_wait
, /* to_wait */
1071 remote_fetch_registers
, /* to_fetch_registers */
1072 remote_store_registers
, /* to_store_registers */
1073 remote_prepare_to_store
, /* to_prepare_to_store */
1074 remote_xfer_memory
, /* to_xfer_memory */
1075 remote_files_info
, /* to_files_info */
1076 NULL
, /* to_insert_breakpoint */
1077 NULL
, /* to_remove_breakpoint */
1078 NULL
, /* to_terminal_init */
1079 NULL
, /* to_terminal_inferior */
1080 NULL
, /* to_terminal_ours_for_output */
1081 NULL
, /* to_terminal_ours */
1082 NULL
, /* to_terminal_info */
1083 remote_kill
, /* to_kill */
1084 generic_load
, /* to_load */
1085 NULL
, /* to_lookup_symbol */
1086 NULL
, /* to_create_inferior */
1087 remote_mourn
, /* to_mourn_inferior */
1089 0, /* to_notice_signals */
1090 process_stratum
, /* to_stratum */
1092 1, /* to_has_all_memory */
1093 1, /* to_has_memory */
1094 1, /* to_has_stack */
1095 1, /* to_has_registers */
1096 1, /* to_has_execution */
1097 NULL
, /* sections */
1098 NULL
, /* sections_end */
1099 OPS_MAGIC
/* to_magic */
1103 _initialize_remote ()
1105 add_target (&remote_ops
);
1108 add_set_cmd ("remotedebug", no_class
, var_boolean
, (char *)&kiodebug
,
1109 "Set debugging of remote serial I/O.\n\
1110 When enabled, each packet sent or received with the remote target\n\
1111 is displayed.", &setlist
),
This page took 0.054524 seconds and 5 git commands to generate.