66f3b2c084f571e517163cef55d45b7aa4af6711
1 /* Memory-access and commands for inferior process, for GDB.
2 Copyright (C) 1988, 1989 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"
77 #include <sys/types.h>
83 #include <sys/ioctl.h>
89 #define TIOCGETP TCGETA
91 #define TIOCSETN TCSETA
93 #define TIOCSETP TCSETAF
94 #define TERMINAL struct termio
97 #define TERMINAL struct sgttyb
101 static int timeout
= 5;
107 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
108 remote_open knows that we don't have a file open when the program
110 int remote_desc
= -1;
114 /* Maximum number of bytes to read/write at once. The value here
115 is chosen to fill up a packet (the headers account for the 32). */
116 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
118 static void remote_send ();
119 static void putpkt ();
120 static void getpkt ();
122 static void dcache_flush ();
126 /* Called when SIGALRM signal sent due to alarm() timeout. */
132 printf ("remote_timer called\n");
138 /* Open a connection to a remote debugger.
139 NAME is the filename used for communication. */
142 remote_open (name
, from_tty
)
148 if (remote_desc
>= 0)
151 remote_debugging
= 0;
156 remote_desc
= open (name
, O_RDWR
);
158 perror_with_name (name
);
160 ioctl (remote_desc
, TIOCGETP
, &sg
);
162 sg
.c_cc
[VMIN
] = 0; /* read with timeout. */
163 sg
.c_cc
[VTIME
] = timeout
* 10;
164 sg
.c_lflag
&= ~(ICANON
| ECHO
);
168 ioctl (remote_desc
, TIOCSETP
, &sg
);
171 printf ("Remote debugging using %s\n", name
);
172 remote_debugging
= 1;
175 #ifndef NO_SIGINTERRUPT
176 /* Cause SIGALRM's to make reads fail. */
177 if (siginterrupt (SIGALRM
, 1) != 0)
178 perror ("remote_open: error in siginterrupt");
181 /* Set up read timeout timer. */
182 if ((void (*)) signal (SIGALRM
, remote_timer
) == (void (*)) -1)
183 perror ("remote_open: error in signal");
186 putpkt ("?"); /* initiate a query from remote machine */
189 /* Close the open connection to the remote debugger.
190 Use this when you want to detach and do something else
193 remote_close (from_tty
)
196 if (!remote_debugging
)
197 error ("Can't close remote connection: not debugging remotely.");
199 close (remote_desc
); /* This should never be called if
200 there isn't something valid in
203 /* Do not try to close remote_desc again, later in the program. */
207 printf ("Ending remote debugging\n");
209 remote_debugging
= 0;
212 /* Convert hex digit A to a number. */
218 if (a
>= '0' && a
<= '9')
220 else if (a
>= 'a' && a
<= 'f')
223 error ("Reply contains invalid hex digit");
226 /* Convert number NIB to a hex digit. */
238 /* Tell the remote machine to resume. */
241 remote_resume (step
, signal
)
250 strcpy (buf
, step
? "s": "c");
255 /* Wait until the remote machine stops, then return,
256 storing status in STATUS just as `wait' would. */
262 unsigned char buf
[PBUFSIZ
];
264 WSETEXIT ((*status
), 0);
267 error ("Remote failure reply: %s", buf
);
269 error ("Invalid remote reply: %s", buf
);
270 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
273 /* Read the remote registers into the block REGS. */
276 remote_fetch_registers (regs
)
286 /* Reply describes registers byte by byte,
287 each byte encoded as two hex characters. */
290 for (i
= 0; i
< REGISTER_BYTES
; i
++)
292 if (p
[0] == 0 || p
[1] == 0)
293 error ("Remote reply is too short: %s", buf
);
294 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
299 /* Store the remote registers from the contents of the block REGS. */
302 remote_store_registers (regs
)
311 /* Command describes registers byte by byte,
312 each byte encoded as two hex characters. */
315 for (i
= 0; i
< REGISTER_BYTES
; i
++)
317 *p
++ = tohex ((regs
[i
] >> 4) & 0xf);
318 *p
++ = tohex (regs
[i
] & 0xf);
326 /* Read a word from remote address ADDR and return it.
327 This goes through the data cache. */
330 remote_fetch_word (addr
)
335 extern CORE_ADDR text_start
, text_end
;
337 if (addr
>= text_start
&& addr
< text_end
)
340 xfer_core_file (addr
, &buffer
, sizeof (int));
344 return dcache_fetch (addr
);
347 /* Write a word WORD into remote address ADDR.
348 This goes through the data cache. */
351 remote_store_word (addr
, word
)
355 dcache_poke (addr
, word
);
358 void remote_fetch_word (addr
)
361 error ("Internal error: remote_fetch_word is obsolete.\n");
363 void remote_store_word (addr
)
366 error ("Internal error: remote_store_word is obsolete.\n");
370 /* Write memory data directly to the remote machine.
371 This does not inform the data cache; the data cache uses this.
372 MEMADDR is the address in the remote memory space.
373 MYADDR is the address of the buffer in our space.
374 LEN is the number of bytes. */
377 remote_write_bytes (memaddr
, myaddr
, len
)
386 if (len
> PBUFSIZ
/ 2 - 20)
389 sprintf (buf
, "M%x,%x:", memaddr
, len
);
391 /* Command describes registers byte by byte,
392 each byte encoded as two hex characters. */
394 p
= buf
+ strlen (buf
);
395 for (i
= 0; i
< len
; i
++)
397 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
398 *p
++ = tohex (myaddr
[i
] & 0xf);
405 /* Read memory data directly from the remote machine.
406 This does not use the data cache; the data cache uses this.
407 MEMADDR is the address in the remote memory space.
408 MYADDR is the address of the buffer in our space.
409 LEN is the number of bytes. */
412 remote_read_bytes (memaddr
, myaddr
, len
)
421 if (len
> PBUFSIZ
/ 2 - 1)
424 sprintf (buf
, "m%x,%x", memaddr
, len
);
427 /* Reply describes registers byte by byte,
428 each byte encoded as two hex characters. */
431 for (i
= 0; i
< len
; i
++)
433 if (p
[0] == 0 || p
[1] == 0)
434 error ("Remote reply is too short: %s", buf
);
435 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
440 /* Read LEN bytes from inferior memory at MEMADDR. Put the result
441 at debugger address MYADDR. Returns errno value. */
443 remote_read_inferior_memory(memaddr
, myaddr
, len
)
451 if (len
> MAXBUFBYTES
)
452 xfersize
= MAXBUFBYTES
;
456 remote_read_bytes (memaddr
, myaddr
, xfersize
);
461 return 0; /* no error */
464 /* Copy LEN bytes of data from debugger memory at MYADDR
465 to inferior's memory at MEMADDR. Returns errno value. */
467 remote_write_inferior_memory (memaddr
, myaddr
, len
)
475 if (len
> MAXBUFBYTES
)
476 xfersize
= MAXBUFBYTES
;
480 remote_write_bytes(memaddr
, myaddr
, xfersize
);
486 return 0; /* no error */
491 A debug packet whose contents are <data>
492 is encapsulated for transmission in the form:
494 $ <data> # CSUM1 CSUM2
496 <data> must be ASCII alphanumeric and cannot include characters
499 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
500 checksum of <data>, the most significant nibble is sent first.
501 the hex digits 0-9,a-f are used.
503 Receiver responds with:
505 + - if CSUM is correct and ready for next packet
506 - - if CSUM is incorrect
517 /* termio does the timeout for us. */
518 read (remote_desc
, &buf
, 1);
521 read (remote_desc
, &buf
, 1);
528 /* Send the command in BUF to the remote machine,
529 and read the reply into BUF.
530 Report an error if we get an error reply. */
541 error ("Remote failure reply: %s", buf
);
544 /* Send a packet to the remote machine, with error checking.
545 The data of the packet is in BUF. */
552 unsigned char csum
= 0;
555 int cnt
= strlen (buf
);
559 /* Copy the packet into buffer BUF2, encapsulating it
560 and giving it a checksum. */
565 for (i
= 0; i
< cnt
; i
++)
571 *p
++ = tohex ((csum
>> 4) & 0xf);
572 *p
++ = tohex (csum
& 0xf);
574 /* Send it over and over until we get a positive ack. */
580 printf ("Sending packet: %s (%s)\n", buf2
, buf
);
582 write (remote_desc
, buf2
, p
- buf2
);
584 /* read until either a timeout occurs (\0) or '+' is read */
587 } while ((ch
!= '+') && (ch
!= '\0'));
591 /* Read a packet from the remote machine, with error checking,
592 and store it in BUF. */
601 unsigned char c1
, c2
;
604 /* allow immediate quit while reading from device, it could be hung */
609 /* Force csum to be zero here because of possible error retry. */
612 while ((c
= readchar()) != '$');
625 c1
= fromhex (readchar ());
626 c2
= fromhex (readchar ());
627 if ((csum
& 0xff) == (c1
<< 4) + c2
)
629 printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
630 (c1
<< 4) + c2
, csum
& 0xff, buf
);
631 write (remote_desc
, "-", 1);
636 write (remote_desc
, "+", 1);
639 fprintf (stderr
,"Packet received :%s\n", buf
);
642 /* The data cache leads to incorrect results because it doesn't know about
643 volatile variables, thus making it impossible to debug functions which
644 use hardware registers. Therefore it is #if 0'd out. Effect on
645 performance is some, for backtraces of functions with a few
646 arguments each. For functions with many arguments, the stack
647 frames don't fit in the cache blocks, which makes the cache less
648 helpful. Disabling the cache is a big performance win for fetching
649 large structures, because the cache code fetched data in 16-byte
652 /* The data cache records all the data read from the remote machine
653 since the last time it stopped.
655 Each cache block holds 16 bytes of data
656 starting at a multiple-of-16 address. */
658 #define DCACHE_SIZE 64 /* Number of cache blocks */
660 struct dcache_block
{
661 struct dcache_block
*next
, *last
;
662 unsigned int addr
; /* Address for which data is recorded. */
666 struct dcache_block dcache_free
, dcache_valid
;
668 /* Free all the data cache blocks, thus discarding all cached data. */
673 register struct dcache_block
*db
;
675 while ((db
= dcache_valid
.next
) != &dcache_valid
)
678 insque (db
, &dcache_free
);
683 * If addr is present in the dcache, return the address of the block
687 struct dcache_block
*
690 register struct dcache_block
*db
;
695 /* Search all cache blocks for one that is at this address. */
696 db
= dcache_valid
.next
;
697 while (db
!= &dcache_valid
)
699 if ((addr
& 0xfffffff0) == db
->addr
)
706 /* Return the int data at address ADDR in dcache block DC. */
709 dcache_value (db
, addr
)
710 struct dcache_block
*db
;
715 return (db
->data
[(addr
>>2)&3]);
718 /* Get a free cache block, put it on the valid list,
719 and return its address. The caller should store into the block
720 the address and data that it describes. */
722 struct dcache_block
*
725 register struct dcache_block
*db
;
727 if ((db
= dcache_free
.next
) == &dcache_free
)
728 /* If we can't get one from the free list, take last valid */
729 db
= dcache_valid
.last
;
732 insque (db
, &dcache_valid
);
736 /* Return the contents of the word at address ADDR in the remote machine,
737 using the data cache. */
743 register struct dcache_block
*db
;
745 db
= dcache_hit (addr
);
748 db
= dcache_alloc ();
749 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
750 db
->addr
= addr
& ~0xf;
752 return (dcache_value (db
, addr
));
755 /* Write the word at ADDR both in the data cache and in the remote machine. */
757 dcache_poke (addr
, data
)
761 register struct dcache_block
*db
;
763 /* First make sure the word is IN the cache. DB is its cache block. */
764 db
= dcache_hit (addr
);
767 db
= dcache_alloc ();
768 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
769 db
->addr
= addr
& ~0xf;
772 /* Modify the word in the cache. */
773 db
->data
[(addr
>>2)&3] = data
;
775 /* Send the changed word. */
776 remote_write_bytes (addr
, &data
, 4);
779 /* Initialize the data cache. */
784 register struct dcache_block
*db
;
786 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
788 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
789 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
790 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
791 insque (db
, &dcache_free
);
This page took 0.045453 seconds and 4 git commands to generate.