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