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