* target.h: Add to_doc and target_preopen.
[deliverable/binutils-gdb.git] / gdb / remote.c
1 /* Memory-access and commands for inferior process, for GDB.
2 Copyright (C) 1988-1991 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
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)
9 any later version.
10
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.
15
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. */
19
20 /* Remote communication protocol.
21 All values are encoded in ascii hex digits.
22
23 Request Packet
24
25 read registers g
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.
31 or ENN for an error.
32
33 write regs GXX..XX Each byte of register data
34 is described by two hex digits.
35 reply OK for success
36 ENN for an error
37
38 read mem mAA..AA,LLLL AA..AA is address, LLLL is length.
39 reply XX..XX XX..XX is mem contents
40 or ENN NN is errno
41
42 write mem MAA..AA,LLLL:XX..XX
43 AA..AA is address,
44 LLLL is number of bytes,
45 XX..XX is data
46 reply OK for success
47 ENN for an error
48
49 cont cAA..AA AA..AA is address to resume
50 If AA..AA is omitted,
51 resume at same address.
52
53 step sAA..AA AA..AA is address to resume
54 If AA..AA is omitted,
55 resume at same address.
56
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
60 signal number.
61
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"
65
66 kill req k
67 */
68
69 #include <stdio.h>
70 #include <string.h>
71 #include <fcntl.h>
72 #include "defs.h"
73 #include "param.h"
74 #include "frame.h"
75 #include "inferior.h"
76 #include "target.h"
77 #include "wait.h"
78 #include "terminal.h"
79
80 #ifdef USG
81 #include <sys/types.h>
82 #endif
83
84 #include <signal.h>
85
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 ();
91
92 extern struct target_ops remote_ops; /* Forward decl */
93
94 static int kiodebug;
95 static int timeout = 5;
96
97 #if 0
98 int icache;
99 #endif
100
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
103 starts. */
104 int remote_desc = -1;
105
106 #define PBUFSIZ 400
107
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)
111
112 static void remote_send ();
113 static void putpkt ();
114 static void getpkt ();
115 #if 0
116 static void dcache_flush ();
117 #endif
118
119 \f
120 /* Called when SIGALRM signal sent due to alarm() timeout. */
121 #ifndef HAVE_TERMIO
122 void
123 remote_timer ()
124 {
125 if (kiodebug)
126 printf ("remote_timer called\n");
127
128 alarm (timeout);
129 }
130 #endif
131
132 /* Initialize remote connection */
133
134 void
135 remote_start()
136 {
137 }
138
139 /* Clean up connection to a remote debugger. */
140
141 void
142 remote_close (quitting)
143 int quitting;
144 {
145 if (remote_desc >= 0)
146 close (remote_desc);
147 remote_desc = -1;
148 }
149
150 /* Open a connection to a remote debugger.
151 NAME is the filename used for communication. */
152
153 void
154 remote_open (name, from_tty)
155 char *name;
156 int from_tty;
157 {
158 TERMINAL sg;
159
160 if (name == 0)
161 error (
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).");
164
165 target_preopen (from_tty);
166
167 remote_close (0);
168
169 #if 0
170 dcache_init ();
171 #endif
172
173 remote_desc = open (name, O_RDWR);
174 if (remote_desc < 0)
175 perror_with_name (name);
176
177 ioctl (remote_desc, TIOCGETP, &sg);
178 #ifdef HAVE_TERMIO
179 sg.c_cc[VMIN] = 0; /* read with timeout. */
180 sg.c_cc[VTIME] = timeout * 10;
181 sg.c_lflag &= ~(ICANON | ECHO);
182 #else
183 sg.sg_flags = RAW;
184 #endif
185 ioctl (remote_desc, TIOCSETP, &sg);
186
187 if (from_tty)
188 printf ("Remote debugging using %s\n", name);
189 push_target (&remote_ops); /* Switch to using remote target now */
190 start_remote (); /* Initialize gdb process mechanisms */
191
192 #ifndef HAVE_TERMIO
193 #ifndef NO_SIGINTERRUPT
194 /* Cause SIGALRM's to make reads fail. */
195 if (siginterrupt (SIGALRM, 1) != 0)
196 perror ("remote_open: error in siginterrupt");
197 #endif
198
199 /* Set up read timeout timer. */
200 if ((void (*)) signal (SIGALRM, remote_timer) == (void (*)) -1)
201 perror ("remote_open: error in signal");
202 #endif
203
204 putpkt ("?"); /* initiate a query from remote machine */
205 }
206
207 /* remote_detach()
208 takes a program previously attached to and detaches it.
209 We better not have left any breakpoints
210 in the program or it'll die when it hits one.
211 Close the open connection to the remote debugger.
212 Use this when you want to detach and do something else
213 with your gdb. */
214
215 static void
216 remote_detach (args, from_tty)
217 char *args;
218 int from_tty;
219 {
220 if (args)
221 error ("Argument given to \"detach\" when remotely debugging.");
222
223 pop_target ();
224 if (from_tty)
225 printf ("Ending remote debugging.\n");
226 }
227
228 /* Convert hex digit A to a number. */
229
230 static int
231 fromhex (a)
232 int a;
233 {
234 if (a >= '0' && a <= '9')
235 return a - '0';
236 else if (a >= 'a' && a <= 'f')
237 return a - 'a' + 10;
238 else
239 error ("Reply contains invalid hex digit");
240 return -1;
241 }
242
243 /* Convert number NIB to a hex digit. */
244
245 static int
246 tohex (nib)
247 int nib;
248 {
249 if (nib < 10)
250 return '0'+nib;
251 else
252 return 'a'+nib-10;
253 }
254 \f
255 /* Tell the remote machine to resume. */
256
257 void
258 remote_resume (step, siggnal)
259 int step, siggnal;
260 {
261 char buf[PBUFSIZ];
262
263 if (siggnal)
264 error ("Can't send signals to a remote system.");
265
266 #if 0
267 dcache_flush ();
268 #endif
269
270 strcpy (buf, step ? "s": "c");
271
272 putpkt (buf);
273 }
274
275 /* Wait until the remote machine stops, then return,
276 storing status in STATUS just as `wait' would. */
277
278 int
279 remote_wait (status)
280 WAITTYPE *status;
281 {
282 unsigned char buf[PBUFSIZ];
283
284 WSETEXIT ((*status), 0);
285 getpkt (buf);
286 if (buf[0] == 'E')
287 error ("Remote failure reply: %s", buf);
288 if (buf[0] != 'S')
289 error ("Invalid remote reply: %s", buf);
290 WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
291 }
292
293 /* Read the remote registers into the block REGS. */
294
295 int
296 remote_fetch_registers (regno)
297 int regno;
298 {
299 char buf[PBUFSIZ];
300 int i;
301 char *p;
302 char regs[REGISTER_BYTES];
303
304 sprintf (buf, "g");
305 remote_send (buf);
306
307 /* Reply describes registers byte by byte, each byte encoded as two
308 hex characters. Suck them all up, then supply them to the
309 register cacheing/storage mechanism. */
310
311 p = buf;
312 for (i = 0; i < REGISTER_BYTES; i++)
313 {
314 if (p[0] == 0 || p[1] == 0)
315 error ("Remote reply is too short: %s", buf);
316 regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
317 p += 2;
318 }
319 for (i = 0; i < NUM_REGS; i++)
320 supply_register (i, &regs[REGISTER_BYTE(i)]);
321 return 0;
322 }
323
324 /* Prepare to store registers. Since we send them all, we have to
325 read out the ones we don't want to change first. */
326
327 void
328 remote_prepare_to_store ()
329 {
330 remote_fetch_registers (-1);
331 }
332
333 /* Store the remote registers from the contents of the block REGISTERS.
334 FIXME, eventually just store one register if that's all that is needed. */
335
336 int
337 remote_store_registers (regno)
338 int regno;
339 {
340 char buf[PBUFSIZ];
341 int i;
342 char *p;
343
344 buf[0] = 'G';
345
346 /* Command describes registers byte by byte,
347 each byte encoded as two hex characters. */
348
349 p = buf + 1;
350 for (i = 0; i < REGISTER_BYTES; i++)
351 {
352 *p++ = tohex ((registers[i] >> 4) & 0xf);
353 *p++ = tohex (registers[i] & 0xf);
354 }
355 *p = '\0';
356
357 remote_send (buf);
358 return 0;
359 }
360
361 #if 0
362 /* Read a word from remote address ADDR and return it.
363 This goes through the data cache. */
364
365 int
366 remote_fetch_word (addr)
367 CORE_ADDR addr;
368 {
369 if (icache)
370 {
371 extern CORE_ADDR text_start, text_end;
372
373 if (addr >= text_start && addr < text_end)
374 {
375 int buffer;
376 xfer_core_file (addr, &buffer, sizeof (int));
377 return buffer;
378 }
379 }
380 return dcache_fetch (addr);
381 }
382
383 /* Write a word WORD into remote address ADDR.
384 This goes through the data cache. */
385
386 void
387 remote_store_word (addr, word)
388 CORE_ADDR addr;
389 int word;
390 {
391 dcache_poke (addr, word);
392 }
393 #endif /* 0 */
394 \f
395 /* Write memory data directly to the remote machine.
396 This does not inform the data cache; the data cache uses this.
397 MEMADDR is the address in the remote memory space.
398 MYADDR is the address of the buffer in our space.
399 LEN is the number of bytes. */
400
401 void
402 remote_write_bytes (memaddr, myaddr, len)
403 CORE_ADDR memaddr;
404 char *myaddr;
405 int len;
406 {
407 char buf[PBUFSIZ];
408 int i;
409 char *p;
410
411 if (len > PBUFSIZ / 2 - 20)
412 abort ();
413
414 sprintf (buf, "M%x,%x:", memaddr, len);
415
416 /* Command describes registers byte by byte,
417 each byte encoded as two hex characters. */
418
419 p = buf + strlen (buf);
420 for (i = 0; i < len; i++)
421 {
422 *p++ = tohex ((myaddr[i] >> 4) & 0xf);
423 *p++ = tohex (myaddr[i] & 0xf);
424 }
425 *p = '\0';
426
427 remote_send (buf);
428 }
429
430 /* Read memory data directly from the remote machine.
431 This does not use the data cache; the data cache uses this.
432 MEMADDR is the address in the remote memory space.
433 MYADDR is the address of the buffer in our space.
434 LEN is the number of bytes. */
435
436 void
437 remote_read_bytes (memaddr, myaddr, len)
438 CORE_ADDR memaddr;
439 char *myaddr;
440 int len;
441 {
442 char buf[PBUFSIZ];
443 int i;
444 char *p;
445
446 if (len > PBUFSIZ / 2 - 1)
447 abort ();
448
449 sprintf (buf, "m%x,%x", memaddr, len);
450 remote_send (buf);
451
452 /* Reply describes registers byte by byte,
453 each byte encoded as two hex characters. */
454
455 p = buf;
456 for (i = 0; i < len; i++)
457 {
458 if (p[0] == 0 || p[1] == 0)
459 error ("Remote reply is too short: %s", buf);
460 myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
461 p += 2;
462 }
463 }
464 \f
465 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
466 to or from debugger address MYADDR. Write to inferior if WRITE is
467 nonzero. Returns length of data written or read; 0 for error. */
468
469 int
470 remote_xfer_inferior_memory(memaddr, myaddr, len, write)
471 CORE_ADDR memaddr;
472 char *myaddr;
473 int len;
474 int write;
475 {
476 int origlen = len;
477 int xfersize;
478 while (len > 0)
479 {
480 if (len > MAXBUFBYTES)
481 xfersize = MAXBUFBYTES;
482 else
483 xfersize = len;
484
485 if (write)
486 remote_write_bytes(memaddr, myaddr, xfersize);
487 else
488 remote_read_bytes (memaddr, myaddr, xfersize);
489 memaddr += xfersize;
490 myaddr += xfersize;
491 len -= xfersize;
492 }
493 return origlen; /* no error possible */
494 }
495
496 void
497 remote_files_info ()
498 {
499 printf ("remote files info missing here. FIXME.\n");
500 }
501 \f
502 /*
503
504 A debug packet whose contents are <data>
505 is encapsulated for transmission in the form:
506
507 $ <data> # CSUM1 CSUM2
508
509 <data> must be ASCII alphanumeric and cannot include characters
510 '$' or '#'
511
512 CSUM1 and CSUM2 are ascii hex representation of an 8-bit
513 checksum of <data>, the most significant nibble is sent first.
514 the hex digits 0-9,a-f are used.
515
516 Receiver responds with:
517
518 + - if CSUM is correct and ready for next packet
519 - - if CSUM is incorrect
520
521 */
522
523 static int
524 readchar ()
525 {
526 char buf;
527
528 buf = '\0';
529 #ifdef HAVE_TERMIO
530 /* termio does the timeout for us. */
531 read (remote_desc, &buf, 1);
532 #else
533 alarm (timeout);
534 read (remote_desc, &buf, 1);
535 alarm (0);
536 #endif
537
538 return buf & 0x7f;
539 }
540
541 /* Send the command in BUF to the remote machine,
542 and read the reply into BUF.
543 Report an error if we get an error reply. */
544
545 static void
546 remote_send (buf)
547 char *buf;
548 {
549
550 putpkt (buf);
551 getpkt (buf);
552
553 if (buf[0] == 'E')
554 error ("Remote failure reply: %s", buf);
555 }
556
557 /* Send a packet to the remote machine, with error checking.
558 The data of the packet is in BUF. */
559
560 static void
561 putpkt (buf)
562 char *buf;
563 {
564 int i;
565 unsigned char csum = 0;
566 char buf2[500];
567 int cnt = strlen (buf);
568 char ch;
569 char *p;
570
571 /* Copy the packet into buffer BUF2, encapsulating it
572 and giving it a checksum. */
573
574 p = buf2;
575 *p++ = '$';
576
577 for (i = 0; i < cnt; i++)
578 {
579 csum += buf[i];
580 *p++ = buf[i];
581 }
582 *p++ = '#';
583 *p++ = tohex ((csum >> 4) & 0xf);
584 *p++ = tohex (csum & 0xf);
585
586 /* Send it over and over until we get a positive ack. */
587
588 do {
589 if (kiodebug)
590 {
591 *p = '\0';
592 printf ("Sending packet: %s (%s)\n", buf2, buf);
593 }
594 write (remote_desc, buf2, p - buf2);
595
596 /* read until either a timeout occurs (\0) or '+' is read */
597 do {
598 ch = readchar ();
599 } while ((ch != '+') && (ch != '\0'));
600 } while (ch != '+');
601 }
602
603 /* Read a packet from the remote machine, with error checking,
604 and store it in BUF. */
605
606 static void
607 getpkt (buf)
608 char *buf;
609 {
610 char *bp;
611 unsigned char csum;
612 int c;
613 unsigned char c1, c2;
614
615 /* allow immediate quit while reading from device, it could be hung */
616 immediate_quit++;
617
618 while (1)
619 {
620 /* Force csum to be zero here because of possible error retry. */
621 csum = 0;
622
623 while ((c = readchar()) != '$');
624
625 bp = buf;
626 while (1)
627 {
628 c = readchar ();
629 if (c == '#')
630 break;
631 *bp++ = c;
632 csum += c;
633 }
634 *bp = 0;
635
636 c1 = fromhex (readchar ());
637 c2 = fromhex (readchar ());
638 if ((csum & 0xff) == (c1 << 4) + c2)
639 break;
640 printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
641 (c1 << 4) + c2, csum & 0xff, buf);
642 write (remote_desc, "-", 1);
643 }
644
645 immediate_quit--;
646
647 write (remote_desc, "+", 1);
648
649 if (kiodebug)
650 fprintf (stderr,"Packet received :%s\n", buf);
651 }
652 \f
653 /* The data cache leads to incorrect results because it doesn't know about
654 volatile variables, thus making it impossible to debug functions which
655 use hardware registers. Therefore it is #if 0'd out. Effect on
656 performance is some, for backtraces of functions with a few
657 arguments each. For functions with many arguments, the stack
658 frames don't fit in the cache blocks, which makes the cache less
659 helpful. Disabling the cache is a big performance win for fetching
660 large structures, because the cache code fetched data in 16-byte
661 chunks. */
662 #if 0
663 /* The data cache records all the data read from the remote machine
664 since the last time it stopped.
665
666 Each cache block holds 16 bytes of data
667 starting at a multiple-of-16 address. */
668
669 #define DCACHE_SIZE 64 /* Number of cache blocks */
670
671 struct dcache_block {
672 struct dcache_block *next, *last;
673 unsigned int addr; /* Address for which data is recorded. */
674 int data[4];
675 };
676
677 struct dcache_block dcache_free, dcache_valid;
678
679 /* Free all the data cache blocks, thus discarding all cached data. */
680
681 static void
682 dcache_flush ()
683 {
684 register struct dcache_block *db;
685
686 while ((db = dcache_valid.next) != &dcache_valid)
687 {
688 remque (db);
689 insque (db, &dcache_free);
690 }
691 }
692
693 /*
694 * If addr is present in the dcache, return the address of the block
695 * containing it.
696 */
697
698 struct dcache_block *
699 dcache_hit (addr)
700 {
701 register struct dcache_block *db;
702
703 if (addr & 3)
704 abort ();
705
706 /* Search all cache blocks for one that is at this address. */
707 db = dcache_valid.next;
708 while (db != &dcache_valid)
709 {
710 if ((addr & 0xfffffff0) == db->addr)
711 return db;
712 db = db->next;
713 }
714 return NULL;
715 }
716
717 /* Return the int data at address ADDR in dcache block DC. */
718
719 int
720 dcache_value (db, addr)
721 struct dcache_block *db;
722 unsigned int addr;
723 {
724 if (addr & 3)
725 abort ();
726 return (db->data[(addr>>2)&3]);
727 }
728
729 /* Get a free cache block, put it on the valid list,
730 and return its address. The caller should store into the block
731 the address and data that it describes. */
732
733 struct dcache_block *
734 dcache_alloc ()
735 {
736 register struct dcache_block *db;
737
738 if ((db = dcache_free.next) == &dcache_free)
739 /* If we can't get one from the free list, take last valid */
740 db = dcache_valid.last;
741
742 remque (db);
743 insque (db, &dcache_valid);
744 return (db);
745 }
746
747 /* Return the contents of the word at address ADDR in the remote machine,
748 using the data cache. */
749
750 int
751 dcache_fetch (addr)
752 CORE_ADDR addr;
753 {
754 register struct dcache_block *db;
755
756 db = dcache_hit (addr);
757 if (db == 0)
758 {
759 db = dcache_alloc ();
760 remote_read_bytes (addr & ~0xf, db->data, 16);
761 db->addr = addr & ~0xf;
762 }
763 return (dcache_value (db, addr));
764 }
765
766 /* Write the word at ADDR both in the data cache and in the remote machine. */
767
768 dcache_poke (addr, data)
769 CORE_ADDR addr;
770 int data;
771 {
772 register struct dcache_block *db;
773
774 /* First make sure the word is IN the cache. DB is its cache block. */
775 db = dcache_hit (addr);
776 if (db == 0)
777 {
778 db = dcache_alloc ();
779 remote_read_bytes (addr & ~0xf, db->data, 16);
780 db->addr = addr & ~0xf;
781 }
782
783 /* Modify the word in the cache. */
784 db->data[(addr>>2)&3] = data;
785
786 /* Send the changed word. */
787 remote_write_bytes (addr, &data, 4);
788 }
789
790 /* Initialize the data cache. */
791
792 dcache_init ()
793 {
794 register i;
795 register struct dcache_block *db;
796
797 db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) *
798 DCACHE_SIZE);
799 dcache_free.next = dcache_free.last = &dcache_free;
800 dcache_valid.next = dcache_valid.last = &dcache_valid;
801 for (i=0;i<DCACHE_SIZE;i++,db++)
802 insque (db, &dcache_free);
803 }
804 #endif /* 0 */
805
806 /* Define the target subroutine names */
807
808 struct target_ops remote_ops = {
809 "remote", "Remote serial target in gdb-specific protocol",
810 "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
811 Specify the serial device it is connected to (e.g. /dev/ttya).",
812 remote_open, remote_close,
813 0, remote_detach, remote_resume, remote_wait, /* attach */
814 remote_fetch_registers, remote_store_registers,
815 remote_prepare_to_store, 0, 0, /* conv_from, conv_to */
816 remote_xfer_inferior_memory, remote_files_info,
817 0, 0, /* insert_breakpoint, remove_breakpoint, */
818 0, 0, 0, 0, 0, /* Terminal crud */
819 0, /* kill */
820 0, add_syms_addr_command, /* load */
821 call_function_by_hand,
822 0, /* lookup_symbol */
823 0, 0, /* create_inferior FIXME, mourn_inferior FIXME */
824 process_stratum, 0, /* next */
825 1, 1, 1, 1, 1, /* all mem, mem, stack, regs, exec */
826 OPS_MAGIC, /* Always the last thing */
827 };
828
829 void
830 _initialize_remote ()
831 {
832 add_target (&remote_ops);
833 }
This page took 0.047182 seconds and 5 git commands to generate.