1 /* Memory-access and commands for inferior process, for GDB.
2 Copyright (C) 1988 Free Software Foundation, Inc.
4 GDB is distributed in the hope that it will be useful, but WITHOUT ANY
5 WARRANTY. No author or distributor accepts responsibility to anyone
6 for the consequences of using it or for whether it serves any
7 particular purpose or works at all, unless he says so in writing.
8 Refer to the GDB General Public License for full details.
10 Everyone is granted permission to copy, modify and redistribute GDB,
11 but only under the conditions described in the GDB General Public
12 License. A copy of this license is supposed to have been given to you
13 along with GDB so you can know your rights and responsibilities. It
14 should be in a file named COPYING. Among other things, the copyright
15 notice and this notice must be preserved on all copies.
17 In other words, go ahead and share GDB, but don't try to stop
18 anyone else from sharing it farther. Help stamp out software hoarding!
21 /* Remote communication protocol.
22 All values are encoded in ascii hex digits.
27 reply XX....X Each byte of register data
28 is described by two hex digits.
29 Registers are in the internal order
30 for GDB, and the bytes in a register
31 are in the same order the machine uses.
34 write regs GXX..XX Each byte of register data
35 is described by two hex digits.
39 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
40 reply XX..XX XX..XX is mem contents
43 write mem MAA..AA,LLLL:XX..XX
45 LLLL is number of bytes,
50 cont cAA..AA AA..AA is address to resume
52 resume at same address.
54 step sAA..AA AA..AA is address to resume
56 resume at same address.
58 There is no immediate reply to step or cont.
59 The reply comes when the machine stops.
60 It is SAA AA is the "signal number"
69 #include "initialize.h"
75 #include <sys/ioctl.h>
84 /* Descriptor for I/O to remote machine. */
89 static void remote_send ();
90 static void putpkt ();
91 static void getpkt ();
92 static void dcache_flush ();
96 /* Open a connection to a remote debugger.
97 NAME is the filename used for communication. */
100 remote_open (name
, from_tty
)
106 remote_debugging
= 0;
109 remote_desc
= open (name
, O_RDWR
);
111 perror_with_name (name
);
113 ioctl (remote_desc
, TIOCGETP
, &sg
);
115 ioctl (remote_desc
, TIOCSETP
, &sg
);
118 printf ("Remote debugging using %s\n", name
);
119 remote_debugging
= 1;
122 /* Convert hex digit A to a number. */
128 if (a
>= '0' && a
<= '9')
130 else if (a
>= 'a' && a
<= 'f')
133 error ("Reply contains invalid hex digit");
136 /* Convert number NIB to a hex digit. */
148 /* Tell the remote machine to resume. */
151 remote_resume (step
, signal
)
158 strcpy (buf
, step
? "s": "c");
163 /* Wait until the remote machine stops, then return,
164 storing status in STATUS just as `wait' would. */
172 status
->w_status
= 0;
175 error ("Remote failure reply: %s", buf
);
177 error ("Invalid remote reply: %s", buf
);
178 status
->w_stopval
= WSTOPPED
;
179 status
->w_stopsig
= (fromhex (buf
[1]) << 4) + fromhex (buf
[2]);
182 /* Read the remote registers into the block REGS. */
185 remote_fetch_registers (regs
)
195 /* Reply describes registers byte by byte,
196 each byte encoded as two hex characters. */
199 for (i
= 0; i
< REGISTER_BYTES
; i
++)
201 if (p
[0] == 0 || p
[1] == 0)
202 error ("Remote reply is too short: %s", buf
);
203 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
208 /* Store the remote registers from the contents of the block REGS. */
211 remote_store_registers (regs
)
220 /* Command describes registers byte by byte,
221 each byte encoded as two hex characters. */
224 for (i
= 0; i
< REGISTER_BYTES
; i
++)
226 *p
++ = (regs
[i
] > 4) & 0xf;
227 *p
++ = regs
[i
] & 0xf;
233 /* Read a word from remote address ADDR and return it.
234 This goes through the data cache. */
237 remote_fetch_word (addr
)
242 extern CORE_ADDR text_start
, text_end
;
244 if (addr
>= text_start
&& addr
< text_end
)
247 xfer_core_file (addr
, &buffer
, sizeof (int));
251 return dcache_fetch (addr
);
254 /* Write a word WORD into remote address ADDR.
255 This goes through the data cache. */
258 remote_store_word (addr
, word
)
262 dcache_poke (addr
, word
);
265 /* Write memory data directly to the remote machine.
266 This does not inform the data cache; the data cache uses this.
267 MEMADDR is the address in the remote memory space.
268 MYADDR is the address of the buffer in our space.
269 LEN is the number of bytes. */
272 remote_write_bytes (memaddr
, myaddr
, len
)
281 if (len
> PBUFSIZ
/ 2 - 20)
284 sprintf (buf
, "M%x,%x:", memaddr
, len
);
286 /* Command describes registers byte by byte,
287 each byte encoded as two hex characters. */
289 p
= buf
+ strlen (buf
);
290 for (i
= 0; i
< len
; i
++)
292 *p
++ = (myaddr
[i
] > 4) & 0xf;
293 *p
++ = myaddr
[i
] & 0xf;
299 /* Read memory data directly from the remote machine.
300 This does not use the data cache; the data cache uses this.
301 MEMADDR is the address in the remote memory space.
302 MYADDR is the address of the buffer in our space.
303 LEN is the number of bytes. */
306 remote_read_bytes (memaddr
, myaddr
, len
)
315 if (len
> PBUFSIZ
/ 2 - 1)
318 sprintf (buf
, "m%x,%x", memaddr
, len
);
321 /* Reply describes registers byte by byte,
322 each byte encoded as two hex characters. */
325 for (i
= 0; i
< REGISTER_BYTES
; i
++)
327 if (p
[0] == 0 || p
[1] == 0)
328 error ("Remote reply is too short: %s", buf
);
329 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
336 A debug packet whose contents are <data>
337 is encapsulated for transmission in the form:
339 $ <data> # CSUM1 CSUM2
341 <data> must be ASCII alphanumeric and cannot include characters
344 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
345 checksum of <data>, the most significant nibble is sent first.
346 the hex digits 0-9,a-f are used.
348 Receiver responds with:
350 + - if CSUM is correct and ready for next packet
351 - - if CSUM is incorrect
355 /* Send the command in BUF to the remote machine,
356 and read the reply into BUF.
357 Report an error if we get an error reply. */
368 error ("Remote failure reply: %s", buf
);
371 /* Send a packet to the remote machine, with error checking.
372 The data of the packet is in BUF. */
382 int cnt
= strlen (buf
);
386 fprintf (stderr
, "Sending packet: %s\n", buf
);
388 /* Copy the packet into buffer BUF2, encapsulating it
389 and giving it a checksum. */
394 for (i
= 0; i
< cnt
; i
++)
400 *p
++ = tohex ((csum
>> 4) & 0xf);
401 *p
++ = tohex (csum
& 0xf);
403 /* Send it over and over until we get a positive ack. */
406 write (remote_desc
, buf2
, p
- buf2
);
407 read (remote_desc
, buf3
, 1);
408 } while (buf3
[0] != '+');
415 while (read (remote_desc
, buf
, 1) != 1) ;
416 return buf
[0] & 0x7f;
419 /* Read a packet from the remote machine, with error checking,
420 and store it in BUF. */
433 while ((c
= readchar()) != '$');
446 c1
= fromhex (readchar ());
447 c2
= fromhex (readchar ());
448 if (csum
== (c1
<< 4) + c2
)
450 printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
451 (c1
<< 4) + c2
, csum
, buf
);
452 write (remote_desc
, "-", 1);
455 write (remote_desc
, "+", 1);
458 fprintf (stderr
,"Packet received :%s\n", buf
);
461 /* The data cache records all the data read from the remote machine
462 since the last time it stopped.
464 Each cache block holds 16 bytes of data
465 starting at a multiple-of-16 address. */
467 #define DCACHE_SIZE 64 /* Number of cache blocks */
469 struct dcache_block
{
470 struct dcache_block
*next
, *last
;
471 unsigned int addr
; /* Address for which data is recorded. */
475 struct dcache_block dcache_free
, dcache_valid
;
477 /* Free all the data cache blocks, thus discarding all cached data. */
482 register struct dcache_block
*db
;
484 while ((db
= dcache_valid
.next
) != &dcache_valid
)
487 insque (db
, &dcache_free
);
492 * If addr is present in the dcache, return the address of the block
496 struct dcache_block
*
499 register struct dcache_block
*db
;
504 /* Search all cache blocks for one that is at this address. */
505 db
= dcache_valid
.next
;
506 while (db
!= &dcache_valid
)
508 if ((addr
& 0xfffffff0) == db
->addr
)
515 /* Return the int data at address ADDR in dcache block DC. */
518 dcache_value (db
, addr
)
519 struct dcache_block
*db
;
524 return (db
->data
[(addr
>>2)&3]);
527 /* Get a free cache block, put it on the valid list,
528 and return its address. The caller should store into the block
529 the address and data that it describes. */
531 struct dcache_block
*
534 register struct dcache_block
*db
;
536 if ((db
= dcache_free
.next
) == &dcache_free
)
537 /* If we can't get one from the free list, take last valid */
538 db
= dcache_valid
.last
;
541 insque (db
, &dcache_valid
);
545 /* Return the contents of the word at address ADDR in the remote machine,
546 using the data cache. */
552 register struct dcache_block
*db
;
554 db
= dcache_hit (addr
);
557 db
= dcache_alloc ();
558 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
559 db
->addr
= addr
& ~0xf;
561 return (dcache_value (db
, addr
));
564 /* Write the word at ADDR both in the data cache and in the remote machine. */
566 dcache_poke (addr
, data
)
570 register struct dcache_block
*db
;
572 /* First make sure the word is IN the cache. DB is its cache block. */
573 db
= dcache_hit (addr
);
576 db
= dcache_alloc ();
577 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
578 db
->addr
= addr
& ~0xf;
581 /* Modify the word in the cache. */
582 db
->data
[(addr
>>2)&3] = data
;
584 /* Send the changed word. */
585 remote_write_bytes (addr
, &data
, 4);
588 /* Initialize the data cache. */
593 register struct dcache_block
*db
;
595 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
597 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
598 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
599 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
600 insque (db
, &dcache_free
);
This page took 0.059879 seconds and 5 git commands to generate.