1 /* Memory-access and commands for inferior process, for GDB.
2 Copyright (C) 1988-1991 Free Software Foundation, Inc.
4 This file is part of GDB.
6 GDB 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 1, or (at your option)
11 GDB 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 GDB; see the file COPYING. If not, write to
18 the Free Software Foundation, 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"
81 #include <sys/types.h>
86 extern void add_syms_addr_command ();
87 extern struct value
*call_function_by_hand();
88 extern void start_remote ();
90 extern struct target_ops remote_ops
; /* Forward decl */
93 static int timeout
= 5;
99 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
100 remote_open knows that we don't have a file open when the program
102 int remote_desc
= -1;
106 /* Maximum number of bytes to read/write at once. The value here
107 is chosen to fill up a packet (the headers account for the 32). */
108 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
110 static void remote_send ();
111 static void putpkt ();
112 static void getpkt ();
114 static void dcache_flush ();
118 /* Called when SIGALRM signal sent due to alarm() timeout. */
124 printf ("remote_timer called\n");
130 /* Initialize remote connection */
137 /* Clean up connection to a remote debugger. */
141 remote_close (quitting
)
144 if (remote_desc
>= 0)
149 /* Open a connection to a remote debugger.
150 NAME is the filename used for communication. */
153 remote_open (name
, from_tty
)
161 "To open a remote debug connection, you need to specify what serial\n\
162 device is attached to the remote system (e.g. /dev/ttya).");
164 target_preopen (from_tty
);
172 remote_desc
= open (name
, O_RDWR
);
174 perror_with_name (name
);
176 ioctl (remote_desc
, TIOCGETP
, &sg
);
178 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
179 sg
.c_cc
[VTIME
] = timeout
* 10;
180 sg
.c_lflag
&= ~(ICANON
| ECHO
);
184 ioctl (remote_desc
, TIOCSETP
, &sg
);
187 printf ("Remote debugging using %s\n", name
);
188 push_target (&remote_ops
); /* Switch to using remote target now */
191 #ifndef NO_SIGINTERRUPT
192 /* Cause SIGALRM's to make reads fail. */
193 if (siginterrupt (SIGALRM
, 1) != 0)
194 perror ("remote_open: error in siginterrupt");
197 /* Set up read timeout timer. */
198 if ((void (*)()) signal (SIGALRM
, remote_timer
) == (void (*)()) -1)
199 perror ("remote_open: error in signal");
202 /* Ack any packet which the remote side has already sent. */
203 write (remote_desc
, "+", 1);
204 putpkt ("?"); /* initiate a query from remote machine */
206 start_remote (); /* Initialize gdb process mechanisms */
210 takes a program previously attached to and detaches it.
211 We better not have left any breakpoints
212 in the program or it'll die when it hits one.
213 Close the open connection to the remote debugger.
214 Use this when you want to detach and do something else
218 remote_detach (args
, from_tty
)
223 error ("Argument given to \"detach\" when remotely debugging.");
227 printf ("Ending remote debugging.\n");
230 /* Convert hex digit A to a number. */
236 if (a
>= '0' && a
<= '9')
238 else if (a
>= 'a' && a
<= 'f')
241 error ("Reply contains invalid hex digit");
245 /* Convert number NIB to a hex digit. */
257 /* Tell the remote machine to resume. */
260 remote_resume (step
, siggnal
)
266 error ("Can't send signals to a remote system.");
272 strcpy (buf
, step
? "s": "c");
277 /* Wait until the remote machine stops, then return,
278 storing status in STATUS just as `wait' would.
279 Returns "pid" (though it's not clear what, if anything, that
280 means in the case of this target). */
286 unsigned char buf
[PBUFSIZ
];
288 WSETEXIT ((*status
), 0);
291 error ("Remote failure reply: %s", buf
);
293 error ("Invalid remote reply: %s", buf
);
294 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
298 /* Read the remote registers into the block REGS. */
300 /* Currently we just read all the registers, so we don't use regno. */
303 remote_fetch_registers (regno
)
309 char regs
[REGISTER_BYTES
];
314 /* Reply describes registers byte by byte, each byte encoded as two
315 hex characters. Suck them all up, then supply them to the
316 register cacheing/storage mechanism. */
319 for (i
= 0; i
< REGISTER_BYTES
; i
++)
321 if (p
[0] == 0 || p
[1] == 0)
322 error ("Remote reply is too short: %s", buf
);
323 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
326 for (i
= 0; i
< NUM_REGS
; i
++)
327 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
331 /* Prepare to store registers. Since we send them all, we have to
332 read out the ones we don't want to change first. */
335 remote_prepare_to_store ()
337 remote_fetch_registers (-1);
340 /* Store the remote registers from the contents of the block REGISTERS.
341 FIXME, eventually just store one register if that's all that is needed. */
345 remote_store_registers (regno
)
354 /* Command describes registers byte by byte,
355 each byte encoded as two hex characters. */
358 for (i
= 0; i
< REGISTER_BYTES
; i
++)
360 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
361 *p
++ = tohex (registers
[i
] & 0xf);
370 /* Read a word from remote address ADDR and return it.
371 This goes through the data cache. */
374 remote_fetch_word (addr
)
379 extern CORE_ADDR text_start
, text_end
;
381 if (addr
>= text_start
&& addr
< text_end
)
384 xfer_core_file (addr
, &buffer
, sizeof (int));
388 return dcache_fetch (addr
);
391 /* Write a word WORD into remote address ADDR.
392 This goes through the data cache. */
395 remote_store_word (addr
, word
)
399 dcache_poke (addr
, word
);
403 /* Write memory data directly to the remote machine.
404 This does not inform the data cache; the data cache uses this.
405 MEMADDR is the address in the remote memory space.
406 MYADDR is the address of the buffer in our space.
407 LEN is the number of bytes. */
410 remote_write_bytes (memaddr
, myaddr
, len
)
419 if (len
> PBUFSIZ
/ 2 - 20)
422 sprintf (buf
, "M%x,%x:", memaddr
, len
);
424 /* Command describes registers byte by byte,
425 each byte encoded as two hex characters. */
427 p
= buf
+ strlen (buf
);
428 for (i
= 0; i
< len
; i
++)
430 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
431 *p
++ = tohex (myaddr
[i
] & 0xf);
438 /* Read memory data directly from the remote machine.
439 This does not use the data cache; the data cache uses this.
440 MEMADDR is the address in the remote memory space.
441 MYADDR is the address of the buffer in our space.
442 LEN is the number of bytes. */
445 remote_read_bytes (memaddr
, myaddr
, len
)
454 if (len
> PBUFSIZ
/ 2 - 1)
457 sprintf (buf
, "m%x,%x", memaddr
, len
);
460 /* Reply describes registers byte by byte,
461 each byte encoded as two hex characters. */
464 for (i
= 0; i
< len
; i
++)
466 if (p
[0] == 0 || p
[1] == 0)
467 error ("Remote reply is too short: %s", buf
);
468 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
473 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
474 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
475 nonzero. Returns length of data written or read; 0 for error. */
478 remote_xfer_inferior_memory(memaddr
, myaddr
, len
, should_write
)
488 if (len
> MAXBUFBYTES
)
489 xfersize
= MAXBUFBYTES
;
494 remote_write_bytes(memaddr
, myaddr
, xfersize
);
496 remote_read_bytes (memaddr
, myaddr
, xfersize
);
501 return origlen
; /* no error possible */
507 printf ("remote files info missing here. FIXME.\n");
512 A debug packet whose contents are <data>
513 is encapsulated for transmission in the form:
515 $ <data> # CSUM1 CSUM2
517 <data> must be ASCII alphanumeric and cannot include characters
520 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
521 checksum of <data>, the most significant nibble is sent first.
522 the hex digits 0-9,a-f are used.
524 Receiver responds with:
526 + - if CSUM is correct and ready for next packet
527 - - if CSUM is incorrect
538 /* termio does the timeout for us. */
539 read (remote_desc
, &buf
, 1);
542 read (remote_desc
, &buf
, 1);
549 /* Send the command in BUF to the remote machine,
550 and read the reply into BUF.
551 Report an error if we get an error reply. */
562 error ("Remote failure reply: %s", buf
);
565 /* Send a packet to the remote machine, with error checking.
566 The data of the packet is in BUF. */
573 unsigned char csum
= 0;
575 int cnt
= strlen (buf
);
579 /* Copy the packet into buffer BUF2, encapsulating it
580 and giving it a checksum. */
585 for (i
= 0; i
< cnt
; i
++)
591 *p
++ = tohex ((csum
>> 4) & 0xf);
592 *p
++ = tohex (csum
& 0xf);
594 /* Send it over and over until we get a positive ack. */
600 printf ("Sending packet: %s (%s)\n", buf2
, buf
);
602 write (remote_desc
, buf2
, p
- buf2
);
604 /* read until either a timeout occurs (\0) or '+' is read */
607 } while ((ch
!= '+') && (ch
!= '\0'));
611 /* Read a packet from the remote machine, with error checking,
612 and store it in BUF. */
621 unsigned char c1
, c2
;
624 /* Sorry, this will cause all hell to break loose, i.e. we'll end
625 up in the command loop with an inferior, but (at least if this
626 happens in remote_wait or some such place) without a current_frame,
627 having set up prev_* in wait_for_inferior, etc.
629 If it is necessary to have such an "emergency exit", seems like
630 the only plausible thing to do is to say the inferior died, and
631 make the user reattach if they want to. Perhaps with a prompt
632 asking for confirmation. */
634 /* allow immediate quit while reading from device, it could be hung */
640 /* Force csum to be zero here because of possible error retry. */
643 while ((c
= readchar()) != '$');
656 c1
= fromhex (readchar ());
657 c2
= fromhex (readchar ());
658 if ((csum
& 0xff) == (c1
<< 4) + c2
)
660 printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
661 (c1
<< 4) + c2
, csum
& 0xff, buf
);
662 write (remote_desc
, "-", 1);
669 write (remote_desc
, "+", 1);
672 fprintf (stderr
,"Packet received :%s\n", buf
);
675 /* The data cache leads to incorrect results because it doesn't know about
676 volatile variables, thus making it impossible to debug functions which
677 use hardware registers. Therefore it is #if 0'd out. Effect on
678 performance is some, for backtraces of functions with a few
679 arguments each. For functions with many arguments, the stack
680 frames don't fit in the cache blocks, which makes the cache less
681 helpful. Disabling the cache is a big performance win for fetching
682 large structures, because the cache code fetched data in 16-byte
685 /* The data cache records all the data read from the remote machine
686 since the last time it stopped.
688 Each cache block holds 16 bytes of data
689 starting at a multiple-of-16 address. */
691 #define DCACHE_SIZE 64 /* Number of cache blocks */
693 struct dcache_block
{
694 struct dcache_block
*next
, *last
;
695 unsigned int addr
; /* Address for which data is recorded. */
699 struct dcache_block dcache_free
, dcache_valid
;
701 /* Free all the data cache blocks, thus discarding all cached data. */
706 register struct dcache_block
*db
;
708 while ((db
= dcache_valid
.next
) != &dcache_valid
)
711 insque (db
, &dcache_free
);
716 * If addr is present in the dcache, return the address of the block
720 struct dcache_block
*
723 register struct dcache_block
*db
;
728 /* Search all cache blocks for one that is at this address. */
729 db
= dcache_valid
.next
;
730 while (db
!= &dcache_valid
)
732 if ((addr
& 0xfffffff0) == db
->addr
)
739 /* Return the int data at address ADDR in dcache block DC. */
742 dcache_value (db
, addr
)
743 struct dcache_block
*db
;
748 return (db
->data
[(addr
>>2)&3]);
751 /* Get a free cache block, put it on the valid list,
752 and return its address. The caller should store into the block
753 the address and data that it describes. */
755 struct dcache_block
*
758 register struct dcache_block
*db
;
760 if ((db
= dcache_free
.next
) == &dcache_free
)
761 /* If we can't get one from the free list, take last valid */
762 db
= dcache_valid
.last
;
765 insque (db
, &dcache_valid
);
769 /* Return the contents of the word at address ADDR in the remote machine,
770 using the data cache. */
776 register struct dcache_block
*db
;
778 db
= dcache_hit (addr
);
781 db
= dcache_alloc ();
782 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
783 db
->addr
= addr
& ~0xf;
785 return (dcache_value (db
, addr
));
788 /* Write the word at ADDR both in the data cache and in the remote machine. */
790 dcache_poke (addr
, data
)
794 register struct dcache_block
*db
;
796 /* First make sure the word is IN the cache. DB is its cache block. */
797 db
= dcache_hit (addr
);
800 db
= dcache_alloc ();
801 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
802 db
->addr
= addr
& ~0xf;
805 /* Modify the word in the cache. */
806 db
->data
[(addr
>>2)&3] = data
;
808 /* Send the changed word. */
809 remote_write_bytes (addr
, &data
, 4);
812 /* Initialize the data cache. */
817 register struct dcache_block
*db
;
819 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
821 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
822 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
823 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
824 insque (db
, &dcache_free
);
828 /* Define the target subroutine names */
830 struct target_ops remote_ops
= {
831 "remote", "Remote serial target in gdb-specific protocol",
832 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
833 Specify the serial device it is connected to (e.g. /dev/ttya).",
834 remote_open
, remote_close
,
835 0, remote_detach
, remote_resume
, remote_wait
, /* attach */
836 remote_fetch_registers
, remote_store_registers
,
837 remote_prepare_to_store
, 0, 0, /* conv_from, conv_to */
838 remote_xfer_inferior_memory
, remote_files_info
,
839 0, 0, /* insert_breakpoint, remove_breakpoint, */
840 0, 0, 0, 0, 0, /* Terminal crud */
842 0, add_syms_addr_command
, /* load */
843 call_function_by_hand
,
844 0, /* lookup_symbol */
845 0, 0, /* create_inferior FIXME, mourn_inferior FIXME */
846 process_stratum
, 0, /* next */
847 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
848 OPS_MAGIC
, /* Always the last thing */
852 _initialize_remote ()
854 add_target (&remote_ops
);