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