1 /* Remote target communications for serial-line targets in custom GDB protocol
2 Copyright 1988, 1991, 1992 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).
86 static int kiodebug
= 0;
87 static int timeout
= 5;
91 /* Maximum number of bytes to read/write at once. The value here
92 is chosen to fill up a packet (the headers account for the 32). */
93 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
95 /* Round up PBUFSIZ to hold all the registers, at least. */
96 #if REGISTER_BYTES > MAXBUFBYTES
98 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
102 takes a program previously attached to and detaches it.
103 We better not have left any breakpoints
104 in the program or it'll die when it hits one.
105 Close the open connection to the remote debugger.
106 Use this when you want to detach and do something else
110 remote_detach (args
, from_tty
)
115 error ("Argument given to \"detach\" when remotely debugging.");
119 printf ("Ending remote debugging.\n");
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");
137 /* Convert number NIB to a hex digit. */
149 /* Tell the remote machine to resume. */
151 /* Send a packet to the remote machine, with error checking.
152 The data of the packet is in BUF. */
159 unsigned char csum
= 0;
161 int cnt
= strlen (buf
);
165 /* Copy the packet into buffer BUF2, encapsulating it
166 and giving it a checksum. */
168 if (cnt
> sizeof(buf2
) - 5) /* Prosanity check */
174 for (i
= 0; i
< cnt
; i
++)
180 *p
++ = tohex ((csum
>> 4) & 0xf);
181 *p
++ = tohex (csum
& 0xf);
183 /* Send it over and over until we get a positive ack. */
189 printf ("Sending packet: %s...", buf2
); fflush(stdout
);
191 serial_write (buf2
, p
- buf2
);
193 /* read until either a timeout occurs (\0) or '+' is read */
200 printf ("%02X%c ", ch
&0xFF, ch
);
202 } while ((ch
!= '+') && (ch
!= '\0'));
206 /* Read a packet from the remote machine, with error checking,
207 and store it in BUF. BUF is expected to be of size PBUFSIZ. */
216 unsigned char c1
, c2
;
220 /* Force csum to be zero here because of possible error retry. */
223 while ((c
= readchar()) != '$');
231 if (bp
>= buf
+PBUFSIZ
-1)
234 printf_filtered ("Remote packet too long: %s\n", buf
);
242 c1
= fromhex (readchar ());
243 c2
= fromhex (readchar ());
244 if ((csum
& 0xff) == (c1
<< 4) + c2
)
246 printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
247 (c1
<< 4) + c2
, csum
& 0xff, buf
);
248 /* Try the whole thing again. */
250 serial_write ("-", 1);
257 serial_write ("+", 1);
260 fprintf (stderr
,"Packet received: %s\n", buf
);
264 remote_resume (step
, siggnal
)
270 error ("Can't send signals to a remote system. Try `handle %d ignore'.",
277 strcpy (buf
, step
? "s": "c");
282 /* Send ^C to target to halt it. Target will respond, and send us a
285 void remote_interrupt(signo
)
290 printf ("remote_interrupt called\n");
292 serial_write ("\003", 1); /* Send a ^C */
296 /* Wait until the remote machine stops, then return,
297 storing status in STATUS just as `wait' would.
298 Returns "pid" (though it's not clear what, if anything, that
299 means in the case of this target). */
305 unsigned char buf
[PBUFSIZ
];
309 char regs
[REGISTER_RAW_SIZE (PC_REGNUM
) + REGISTER_RAW_SIZE (FP_REGNUM
)];
311 WSETEXIT ((*status
), 0);
313 ofunc
= (void (*)()) signal (SIGINT
, remote_interrupt
);
314 getpkt ((char *) buf
);
315 signal (SIGINT
, ofunc
);
318 error ("Remote failure reply: %s", buf
);
321 /* Expedited reply, containing Signal, PC, and FP. */
322 p
= &buf
[3]; /* after Txx */
323 for (i
= 0; i
< sizeof (regs
); i
++)
325 if (p
[0] == 0 || p
[1] == 0)
326 error ("Remote reply is too short: %s", buf
);
327 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
330 supply_register (PC_REGNUM
, ®s
[0]);
331 supply_register (FP_REGNUM
, ®s
[REGISTER_RAW_SIZE (PC_REGNUM
)]);
333 else if (buf
[0] != 'S')
334 error ("Invalid remote reply: %s", buf
);
336 WSETSTOP ((*status
), (((fromhex (buf
[1])) << 4) + (fromhex (buf
[2]))));
341 /* Send the command in BUF to the remote machine,
342 and read the reply into BUF.
343 Report an error if we get an error reply. */
354 error ("Remote failure reply: %s", buf
);
357 /* Read the remote registers into the block REGS. */
358 /* Currently we just read all the registers, so we don't use regno. */
361 remote_fetch_registers (regno
)
367 char regs
[REGISTER_BYTES
];
372 /* Reply describes registers byte by byte, each byte encoded as two
373 hex characters. Suck them all up, then supply them to the
374 register cacheing/storage mechanism. */
377 for (i
= 0; i
< REGISTER_BYTES
; i
++)
379 if (p
[0] == 0 || p
[1] == 0)
380 error ("Remote reply is too short: %s", buf
);
381 regs
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
384 for (i
= 0; i
< NUM_REGS
; i
++)
385 supply_register (i
, ®s
[REGISTER_BYTE(i
)]);
388 /* Prepare to store registers. Since we send them all, we have to
389 read out the ones we don't want to change first. */
392 remote_prepare_to_store ()
394 remote_fetch_registers (-1);
397 /* Store the remote registers from the contents of the block REGISTERS.
398 FIXME, eventually just store one register if that's all that is needed. */
402 remote_store_registers (regno
)
411 /* Command describes registers byte by byte,
412 each byte encoded as two hex characters. */
415 for (i
= 0; i
< REGISTER_BYTES
; i
++)
417 *p
++ = tohex ((registers
[i
] >> 4) & 0xf);
418 *p
++ = tohex (registers
[i
] & 0xf);
426 /* Write memory data directly to the remote machine.
427 This does not inform the data cache; the data cache uses this.
428 MEMADDR is the address in the remote memory space.
429 MYADDR is the address of the buffer in our space.
430 LEN is the number of bytes. */
433 remote_write_bytes (memaddr
, myaddr
, len
)
442 if (len
> PBUFSIZ
/ 2 - 20)
445 sprintf (buf
, "M%x,%x:", memaddr
, len
);
447 /* We send target system values byte by byte, in increasing byte addresses,
448 each byte encoded as two hex characters. */
450 p
= buf
+ strlen (buf
);
451 for (i
= 0; i
< len
; i
++)
453 *p
++ = tohex ((myaddr
[i
] >> 4) & 0xf);
454 *p
++ = tohex (myaddr
[i
] & 0xf);
461 /* Read memory data directly from the remote machine.
462 This does not use the data cache; the data cache uses this.
463 MEMADDR is the address in the remote memory space.
464 MYADDR is the address of the buffer in our space.
465 LEN is the number of bytes. */
468 remote_read_bytes (memaddr
, myaddr
, len
)
477 if (len
> PBUFSIZ
/ 2 - 1)
480 sprintf (buf
, "m%x,%x", memaddr
, len
);
483 /* Reply describes memory byte by byte,
484 each byte encoded as two hex characters. */
487 for (i
= 0; i
< len
; i
++)
489 if (p
[0] == 0 || p
[1] == 0)
490 error ("Remote reply is too short: %s", buf
);
491 myaddr
[i
] = fromhex (p
[0]) * 16 + fromhex (p
[1]);
496 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
497 to or from debugger address MYADDR. Write to inferior if SHOULD_WRITE is
498 nonzero. Returns length of data written or read; 0 for error. */
502 remote_xfer_memory(memaddr
, myaddr
, len
, should_write
, target
)
507 struct target_ops
*target
; /* ignored */
513 if (len
> MAXBUFBYTES
)
514 xfersize
= MAXBUFBYTES
;
519 remote_write_bytes(memaddr
, myaddr
, xfersize
);
521 remote_read_bytes (memaddr
, myaddr
, xfersize
);
526 return origlen
; /* no error possible */
530 remote_files_info (ignore
)
531 struct target_ops
*ignore
;
533 printf ("Debugging a target over a serial line.\n");
538 A debug packet whose contents are <data>
539 is encapsulated for transmission in the form:
541 $ <data> # CSUM1 CSUM2
543 <data> must be ASCII alphanumeric and cannot include characters
546 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
547 checksum of <data>, the most significant nibble is sent first.
548 the hex digits 0-9,a-f are used.
550 Receiver responds with:
552 + - if CSUM is correct and ready for next packet
553 - - if CSUM is incorrect
557 /* Read a single character from the remote end.
558 (If supported, we actually read many characters and buffer them up.) */
563 static int inbuf_index
, inbuf_count
;
564 #define INBUFSIZE PBUFSIZ
565 static char inbuf
[INBUFSIZE
];
567 if (inbuf_index
>= inbuf_count
)
569 /* Time to do another read... */
572 inbuf
[0] = 0; /* Just in case */
573 if ((inbuf
[inbuf_index
] = serial_readchar (timeout
)) < 0)
579 /* Just return the next character from the buffer. */
580 return inbuf
[inbuf_index
++] & 0x7f;
585 /* The data cache leads to incorrect results because it doesn't know about
586 volatile variables, thus making it impossible to debug functions which
587 use hardware registers. Therefore it is #if 0'd out. Effect on
588 performance is some, for backtraces of functions with a few
589 arguments each. For functions with many arguments, the stack
590 frames don't fit in the cache blocks, which makes the cache less
591 helpful. Disabling the cache is a big performance win for fetching
592 large structures, because the cache code fetched data in 16-byte
595 /* The data cache records all the data read from the remote machine
596 since the last time it stopped.
598 Each cache block holds 16 bytes of data
599 starting at a multiple-of-16 address. */
601 #define DCACHE_SIZE 64 /* Number of cache blocks */
603 struct dcache_block
{
604 struct dcache_block
*next
, *last
;
605 unsigned int addr
; /* Address for which data is recorded. */
609 struct dcache_block dcache_free
, dcache_valid
;
611 /* Free all the data cache blocks, thus discarding all cached data. */
616 register struct dcache_block
*db
;
618 while ((db
= dcache_valid
.next
) != &dcache_valid
)
621 insque (db
, &dcache_free
);
626 * If addr is present in the dcache, return the address of the block
630 struct dcache_block
*
633 register struct dcache_block
*db
;
638 /* Search all cache blocks for one that is at this address. */
639 db
= dcache_valid
.next
;
640 while (db
!= &dcache_valid
)
642 if ((addr
& 0xfffffff0) == db
->addr
)
649 /* Return the int data at address ADDR in dcache block DC. */
652 dcache_value (db
, addr
)
653 struct dcache_block
*db
;
658 return (db
->data
[(addr
>>2)&3]);
661 /* Get a free cache block, put it on the valid list,
662 and return its address. The caller should store into the block
663 the address and data that it describes. */
665 struct dcache_block
*
668 register struct dcache_block
*db
;
670 if ((db
= dcache_free
.next
) == &dcache_free
)
671 /* If we can't get one from the free list, take last valid */
672 db
= dcache_valid
.last
;
675 insque (db
, &dcache_valid
);
679 /* Return the contents of the word at address ADDR in the remote machine,
680 using the data cache. */
686 register struct dcache_block
*db
;
688 db
= dcache_hit (addr
);
691 db
= dcache_alloc ();
692 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
693 db
->addr
= addr
& ~0xf;
695 return (dcache_value (db
, addr
));
698 /* Write the word at ADDR both in the data cache and in the remote machine. */
700 dcache_poke (addr
, data
)
704 register struct dcache_block
*db
;
706 /* First make sure the word is IN the cache. DB is its cache block. */
707 db
= dcache_hit (addr
);
710 db
= dcache_alloc ();
711 remote_read_bytes (addr
& ~0xf, db
->data
, 16);
712 db
->addr
= addr
& ~0xf;
715 /* Modify the word in the cache. */
716 db
->data
[(addr
>>2)&3] = data
;
718 /* Send the changed word. */
719 remote_write_bytes (addr
, &data
, 4);
722 /* Initialize the data cache. */
727 register struct dcache_block
*db
;
729 db
= (struct dcache_block
*) xmalloc (sizeof (struct dcache_block
) *
731 dcache_free
.next
= dcache_free
.last
= &dcache_free
;
732 dcache_valid
.next
= dcache_valid
.last
= &dcache_valid
;
733 for (i
=0;i
<DCACHE_SIZE
;i
++,db
++)
734 insque (db
, &dcache_free
);
738 /* Define the target subroutine names */
740 struct target_ops remote_ops
= {
741 "remote-ser", /* to_shortname */
742 "Remote serial target in gdb-specific protocol with serial-packaging routines", /* to_longname */
743 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
744 Specify the serial device it is connected to (e.g. /dev/ttya).", /* to_doc */
745 serial_open
, /* to_open */
746 serial_close
, /* to_close */
747 NULL
, /* to_attach */
748 remote_detach
, /* to_detach */
749 remote_resume
, /* to_resume */
750 remote_wait
, /* to_wait */
751 remote_fetch_registers
, /* to_fetch_registers */
752 remote_store_registers
, /* to_store_registers */
753 remote_prepare_to_store
, /* to_prepare_to_store */
754 NULL
, /* to_convert_to_virtual */
755 NULL
, /* to_convert_from_virtual */
756 remote_xfer_memory
, /* to_xfer_memory */
757 remote_files_info
, /* to_files_info */
758 NULL
, /* to_insert_breakpoint */
759 NULL
, /* to_remove_breakpoint */
760 NULL
, /* to_terminal_init */
761 NULL
, /* to_terminal_inferior */
762 NULL
, /* to_terminal_ours_for_output */
763 NULL
, /* to_terminal_ours */
764 NULL
, /* to_terminal_info */
767 NULL
, /* to_lookup_symbol */
768 NULL
, /* to_create_inferior */
769 NULL
, /* to_mourn_inferior */
770 process_stratum
, /* to_stratum */
772 1, /* to_has_all_memory */
773 1, /* to_has_memory */
774 1, /* to_has_stack */
775 1, /* to_has_registers */
776 1, /* to_has_execution */
778 NULL
, /* sections_end */
779 OPS_MAGIC
/* to_magic */
783 _initialize_remote ()
785 add_target (&remote_ops
);
788 add_set_cmd ("remotedebug", no_class
, var_boolean
, (char *)&kiodebug
,
789 "Set debugging of remote serial I/O.\n\
790 When enabled, each packet sent or received with the remote target\n\
791 is displayed.", &setlist
),