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 int memory_insert_breakpoint ();
87 extern int memory_remove_breakpoint ();
88 extern void add_syms_addr_command ();
89 extern struct value *call_function_by_hand();
90 extern void start_remote ();
92 extern struct target_ops remote_ops; /* Forward decl */
95 static int timeout = 5;
101 /* Descriptor for I/O to remote machine. Initialize it to -1 so that
102 remote_open knows that we don't have a file open when the program
104 int remote_desc = -1;
108 /* Maximum number of bytes to read/write at once. The value here
109 is chosen to fill up a packet (the headers account for the 32). */
110 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
112 static void remote_send ();
113 static void putpkt ();
114 static void getpkt ();
116 static void dcache_flush ();
120 /* Called when SIGALRM signal sent due to alarm() timeout. */
126 printf ("remote_timer called\n");
132 /* Initialize remote connection */
139 /* Clean up connection to a remote debugger. */
142 remote_close (quitting)
145 if (remote_desc >= 0)
150 /* Open a connection to a remote debugger.
151 NAME is the filename used for communication. */
154 remote_open (name, from_tty)
162 "To open a remote debug connection, you need to specify what serial\n\
163 device is attached to the remote system (e.g. /dev/ttya).");
165 target_preopen (from_tty);
173 remote_desc = open (name, O_RDWR);
175 perror_with_name (name);
177 ioctl (remote_desc, TIOCGETP, &sg);
179 sg.c_cc[VMIN] = 0; /* read with timeout. */
180 sg.c_cc[VTIME] = timeout * 10;
181 sg.c_lflag &= ~(ICANON | ECHO);
185 ioctl (remote_desc, TIOCSETP, &sg);
188 printf ("Remote debugging using %s\n", name);
189 push_target (&remote_ops); /* Switch to using remote target now */
192 #ifndef NO_SIGINTERRUPT
193 /* Cause SIGALRM's to make reads fail. */
194 if (siginterrupt (SIGALRM, 1) != 0)
195 perror ("remote_open: error in siginterrupt");
198 /* Set up read timeout timer. */
199 if ((void (*)) signal (SIGALRM, remote_timer) == (void (*)) -1)
200 perror ("remote_open: error in signal");
203 /* Ack any packet which the remote side has already sent. */
204 write (remote_desc, "+", 1);
205 putpkt ("?"); /* initiate a query from remote machine */
207 start_remote (); /* Initialize gdb process mechanisms */
211 takes a program previously attached to and detaches it.
212 We better not have left any breakpoints
213 in the program or it'll die when it hits one.
214 Close the open connection to the remote debugger.
215 Use this when you want to detach and do something else
219 remote_detach (args, from_tty)
224 error ("Argument given to \"detach\" when remotely debugging.");
228 printf ("Ending remote debugging.\n");
231 /* Convert hex digit A to a number. */
237 if (a >= '0' && a <= '9')
239 else if (a >= 'a' && a <= 'f')
242 error ("Reply contains invalid hex digit");
246 /* Convert number NIB to a hex digit. */
258 /* Tell the remote machine to resume. */
261 remote_resume (step, siggnal)
267 error ("Can't send signals to a remote system.");
273 strcpy (buf, step ? "s": "c");
278 /* Wait until the remote machine stops, then return,
279 storing status in STATUS just as `wait' would. */
285 unsigned char buf[PBUFSIZ];
287 WSETEXIT ((*status), 0);
290 error ("Remote failure reply: %s", buf);
292 error ("Invalid remote reply: %s", buf);
293 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
296 /* Read the remote registers into the block REGS. */
299 remote_fetch_registers (regno)
305 char regs[REGISTER_BYTES];
310 /* Reply describes registers byte by byte, each byte encoded as two
311 hex characters. Suck them all up, then supply them to the
312 register cacheing/storage mechanism. */
315 for (i = 0; i < REGISTER_BYTES; i++)
317 if (p[0] == 0 || p[1] == 0)
318 error ("Remote reply is too short: %s", buf);
319 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
322 for (i = 0; i < NUM_REGS; i++)
323 supply_register (i, ®s[REGISTER_BYTE(i)]);
327 /* Prepare to store registers. Since we send them all, we have to
328 read out the ones we don't want to change first. */
331 remote_prepare_to_store ()
333 remote_fetch_registers (-1);
336 /* Store the remote registers from the contents of the block REGISTERS.
337 FIXME, eventually just store one register if that's all that is needed. */
340 remote_store_registers (regno)
349 /* Command describes registers byte by byte,
350 each byte encoded as two hex characters. */
353 for (i = 0; i < REGISTER_BYTES; i++)
355 *p++ = tohex ((registers[i] >> 4) & 0xf);
356 *p++ = tohex (registers[i] & 0xf);
365 /* Read a word from remote address ADDR and return it.
366 This goes through the data cache. */
369 remote_fetch_word (addr)
374 extern CORE_ADDR text_start, text_end;
376 if (addr >= text_start && addr < text_end)
379 xfer_core_file (addr, &buffer, sizeof (int));
383 return dcache_fetch (addr);
386 /* Write a word WORD into remote address ADDR.
387 This goes through the data cache. */
390 remote_store_word (addr, word)
394 dcache_poke (addr, word);
398 /* Write memory data directly to the remote machine.
399 This does not inform the data cache; the data cache uses this.
400 MEMADDR is the address in the remote memory space.
401 MYADDR is the address of the buffer in our space.
402 LEN is the number of bytes. */
405 remote_write_bytes (memaddr, myaddr, len)
414 if (len > PBUFSIZ / 2 - 20)
417 sprintf (buf, "M%x,%x:", memaddr, len);
419 /* Command describes registers byte by byte,
420 each byte encoded as two hex characters. */
422 p = buf + strlen (buf);
423 for (i = 0; i < len; i++)
425 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
426 *p++ = tohex (myaddr[i] & 0xf);
433 /* Read memory data directly from the remote machine.
434 This does not use the data cache; the data cache uses this.
435 MEMADDR is the address in the remote memory space.
436 MYADDR is the address of the buffer in our space.
437 LEN is the number of bytes. */
440 remote_read_bytes (memaddr, myaddr, len)
449 if (len > PBUFSIZ / 2 - 1)
452 sprintf (buf, "m%x,%x", memaddr, len);
455 /* Reply describes registers byte by byte,
456 each byte encoded as two hex characters. */
459 for (i = 0; i < len; i++)
461 if (p[0] == 0 || p[1] == 0)
462 error ("Remote reply is too short: %s", buf);
463 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
468 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
469 to or from debugger address MYADDR. Write to inferior if WRITE is
470 nonzero. Returns length of data written or read; 0 for error. */
473 remote_xfer_inferior_memory(memaddr, myaddr, len, write)
483 if (len > MAXBUFBYTES)
484 xfersize = MAXBUFBYTES;
489 remote_write_bytes(memaddr, myaddr, xfersize);
491 remote_read_bytes (memaddr, myaddr, xfersize);
496 return origlen; /* no error possible */
502 printf ("remote files info missing here. FIXME.\n");
507 A debug packet whose contents are <data>
508 is encapsulated for transmission in the form:
510 $ <data> # CSUM1 CSUM2
512 <data> must be ASCII alphanumeric and cannot include characters
515 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
516 checksum of <data>, the most significant nibble is sent first.
517 the hex digits 0-9,a-f are used.
519 Receiver responds with:
521 + - if CSUM is correct and ready for next packet
522 - - if CSUM is incorrect
533 /* termio does the timeout for us. */
534 read (remote_desc, &buf, 1);
537 read (remote_desc, &buf, 1);
544 /* Send the command in BUF to the remote machine,
545 and read the reply into BUF.
546 Report an error if we get an error reply. */
557 error ("Remote failure reply: %s", buf);
560 /* Send a packet to the remote machine, with error checking.
561 The data of the packet is in BUF. */
568 unsigned char csum = 0;
570 int cnt = strlen (buf);
574 /* Copy the packet into buffer BUF2, encapsulating it
575 and giving it a checksum. */
580 for (i = 0; i < cnt; i++)
586 *p++ = tohex ((csum >> 4) & 0xf);
587 *p++ = tohex (csum & 0xf);
589 /* Send it over and over until we get a positive ack. */
595 printf ("Sending packet: %s (%s)\n", buf2, buf);
597 write (remote_desc, buf2, p - buf2);
599 /* read until either a timeout occurs (\0) or '+' is read */
602 } while ((ch != '+') && (ch != '\0'));
606 /* Read a packet from the remote machine, with error checking,
607 and store it in BUF. */
616 unsigned char c1, c2;
618 /* allow immediate quit while reading from device, it could be hung */
623 /* Force csum to be zero here because of possible error retry. */
626 while ((c = readchar()) != '$');
639 c1 = fromhex (readchar ());
640 c2 = fromhex (readchar ());
641 if ((csum & 0xff) == (c1 << 4) + c2)
643 printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
644 (c1 << 4) + c2, csum & 0xff, buf);
645 write (remote_desc, "-", 1);
650 write (remote_desc, "+", 1);
653 fprintf (stderr,"Packet received :%s\n", buf);
656 /* The data cache leads to incorrect results because it doesn't know about
657 volatile variables, thus making it impossible to debug functions which
658 use hardware registers. Therefore it is #if 0'd out. Effect on
659 performance is some, for backtraces of functions with a few
660 arguments each. For functions with many arguments, the stack
661 frames don't fit in the cache blocks, which makes the cache less
662 helpful. Disabling the cache is a big performance win for fetching
663 large structures, because the cache code fetched data in 16-byte
666 /* The data cache records all the data read from the remote machine
667 since the last time it stopped.
669 Each cache block holds 16 bytes of data
670 starting at a multiple-of-16 address. */
672 #define DCACHE_SIZE 64 /* Number of cache blocks */
674 struct dcache_block {
675 struct dcache_block *next, *last;
676 unsigned int addr; /* Address for which data is recorded. */
680 struct dcache_block dcache_free, dcache_valid;
682 /* Free all the data cache blocks, thus discarding all cached data. */
687 register struct dcache_block *db;
689 while ((db = dcache_valid.next) != &dcache_valid)
692 insque (db, &dcache_free);
697 * If addr is present in the dcache, return the address of the block
701 struct dcache_block *
704 register struct dcache_block *db;
709 /* Search all cache blocks for one that is at this address. */
710 db = dcache_valid.next;
711 while (db != &dcache_valid)
713 if ((addr & 0xfffffff0) == db->addr)
720 /* Return the int data at address ADDR in dcache block DC. */
723 dcache_value (db, addr)
724 struct dcache_block *db;
729 return (db->data[(addr>>2)&3]);
732 /* Get a free cache block, put it on the valid list,
733 and return its address. The caller should store into the block
734 the address and data that it describes. */
736 struct dcache_block *
739 register struct dcache_block *db;
741 if ((db = dcache_free.next) == &dcache_free)
742 /* If we can't get one from the free list, take last valid */
743 db = dcache_valid.last;
746 insque (db, &dcache_valid);
750 /* Return the contents of the word at address ADDR in the remote machine,
751 using the data cache. */
757 register struct dcache_block *db;
759 db = dcache_hit (addr);
762 db = dcache_alloc ();
763 remote_read_bytes (addr & ~0xf, db->data, 16);
764 db->addr = addr & ~0xf;
766 return (dcache_value (db, addr));
769 /* Write the word at ADDR both in the data cache and in the remote machine. */
771 dcache_poke (addr, data)
775 register struct dcache_block *db;
777 /* First make sure the word is IN the cache. DB is its cache block. */
778 db = dcache_hit (addr);
781 db = dcache_alloc ();
782 remote_read_bytes (addr & ~0xf, db->data, 16);
783 db->addr = addr & ~0xf;
786 /* Modify the word in the cache. */
787 db->data[(addr>>2)&3] = data;
789 /* Send the changed word. */
790 remote_write_bytes (addr, &data, 4);
793 /* Initialize the data cache. */
798 register struct dcache_block *db;
800 db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) *
802 dcache_free.next = dcache_free.last = &dcache_free;
803 dcache_valid.next = dcache_valid.last = &dcache_valid;
804 for (i=0;i<DCACHE_SIZE;i++,db++)
805 insque (db, &dcache_free);
809 /* Define the target subroutine names */
811 struct target_ops remote_ops = {
812 "remote", "Remote serial target in gdb-specific protocol",
813 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
814 Specify the serial device it is connected to (e.g. /dev/ttya).",
815 remote_open, remote_close,
816 0, remote_detach, remote_resume, remote_wait, /* attach */
817 remote_fetch_registers, remote_store_registers,
818 remote_prepare_to_store, 0, 0, /* conv_from, conv_to */
819 remote_xfer_inferior_memory, remote_files_info,
820 0, 0, /* insert_breakpoint, remove_breakpoint, */
821 0, 0, 0, 0, 0, /* Terminal crud */
823 0, add_syms_addr_command, /* load */
824 call_function_by_hand,
825 0, /* lookup_symbol */
826 0, 0, /* create_inferior FIXME, mourn_inferior FIXME */
827 process_stratum, 0, /* next */
828 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
829 OPS_MAGIC, /* Always the last thing */
833 _initialize_remote ()
835 add_target (&remote_ops);