]> Git Repo - binutils.git/blob - gdb/remote.c
ansi name abuse changes
[binutils.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 void
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 }
329
330 /* Prepare to store registers.  Since we send them all, we have to
331    read out the ones we don't want to change first.  */
332
333 void 
334 remote_prepare_to_store ()
335 {
336   remote_fetch_registers (-1);
337 }
338
339 /* Store the remote registers from the contents of the block REGISTERS. 
340    FIXME, eventually just store one register if that's all that is needed.  */
341
342 /* ARGSUSED */
343 int
344 remote_store_registers (regno)
345      int regno;
346 {
347   char buf[PBUFSIZ];
348   int i;
349   char *p;
350
351   buf[0] = 'G';
352   
353   /* Command describes registers byte by byte,
354      each byte encoded as two hex characters.  */
355
356   p = buf + 1;
357   for (i = 0; i < REGISTER_BYTES; i++)
358     {
359       *p++ = tohex ((registers[i] >> 4) & 0xf);
360       *p++ = tohex (registers[i] & 0xf);
361     }
362   *p = '\0';
363
364   remote_send (buf);
365   return 0;
366 }
367
368 #if 0
369 /* Read a word from remote address ADDR and return it.
370    This goes through the data cache.  */
371
372 int
373 remote_fetch_word (addr)
374      CORE_ADDR addr;
375 {
376   if (icache)
377     {
378       extern CORE_ADDR text_start, text_end;
379
380       if (addr >= text_start && addr < text_end)
381         {
382           int buffer;
383           xfer_core_file (addr, &buffer, sizeof (int));
384           return buffer;
385         }
386     }
387   return dcache_fetch (addr);
388 }
389
390 /* Write a word WORD into remote address ADDR.
391    This goes through the data cache.  */
392
393 void
394 remote_store_word (addr, word)
395      CORE_ADDR addr;
396      int word;
397 {
398   dcache_poke (addr, word);
399 }
400 #endif /* 0 */
401 \f
402 /* Write memory data directly to the remote machine.
403    This does not inform the data cache; the data cache uses this.
404    MEMADDR is the address in the remote memory space.
405    MYADDR is the address of the buffer in our space.
406    LEN is the number of bytes.  */
407
408 void
409 remote_write_bytes (memaddr, myaddr, len)
410      CORE_ADDR memaddr;
411      char *myaddr;
412      int len;
413 {
414   char buf[PBUFSIZ];
415   int i;
416   char *p;
417
418   if (len > PBUFSIZ / 2 - 20)
419     abort ();
420
421   sprintf (buf, "M%x,%x:", memaddr, len);
422
423   /* Command describes registers byte by byte,
424      each byte encoded as two hex characters.  */
425
426   p = buf + strlen (buf);
427   for (i = 0; i < len; i++)
428     {
429       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
430       *p++ = tohex (myaddr[i] & 0xf);
431     }
432   *p = '\0';
433
434   remote_send (buf);
435 }
436
437 /* Read memory data directly from the remote machine.
438    This does not use the data cache; the data cache uses this.
439    MEMADDR is the address in the remote memory space.
440    MYADDR is the address of the buffer in our space.
441    LEN is the number of bytes.  */
442
443 void
444 remote_read_bytes (memaddr, myaddr, len)
445      CORE_ADDR memaddr;
446      char *myaddr;
447      int len;
448 {
449   char buf[PBUFSIZ];
450   int i;
451   char *p;
452
453   if (len > PBUFSIZ / 2 - 1)
454     abort ();
455
456   sprintf (buf, "m%x,%x", memaddr, len);
457   remote_send (buf);
458
459   /* Reply describes registers byte by byte,
460      each byte encoded as two hex characters.  */
461
462   p = buf;
463   for (i = 0; i < len; i++)
464     {
465       if (p[0] == 0 || p[1] == 0)
466         error ("Remote reply is too short: %s", buf);
467       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
468       p += 2;
469     }
470 }
471 \f
472 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
473    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
474    nonzero.  Returns length of data written or read; 0 for error.  */
475
476 int
477 remote_xfer_inferior_memory(memaddr, myaddr, len, should_write)
478      CORE_ADDR memaddr;
479      char *myaddr;
480      int len;
481      int should_write;
482 {
483   int origlen = len;
484   int xfersize;
485   while (len > 0)
486     {
487       if (len > MAXBUFBYTES)
488         xfersize = MAXBUFBYTES;
489       else
490         xfersize = len;
491
492       if (should_write)
493         remote_write_bytes(memaddr, myaddr, xfersize);
494       else
495         remote_read_bytes (memaddr, myaddr, xfersize);
496       memaddr += xfersize;
497       myaddr  += xfersize;
498       len     -= xfersize;
499     }
500   return origlen; /* no error possible */
501 }
502
503 void
504 remote_files_info ()
505 {
506   printf ("remote files info missing here.  FIXME.\n");
507 }
508 \f
509 /*
510
511 A debug packet whose contents are <data>
512 is encapsulated for transmission in the form:
513
514         $ <data> # CSUM1 CSUM2
515
516         <data> must be ASCII alphanumeric and cannot include characters
517         '$' or '#'
518
519         CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
520         checksum of <data>, the most significant nibble is sent first.
521         the hex digits 0-9,a-f are used.
522
523 Receiver responds with:
524
525         +       - if CSUM is correct and ready for next packet
526         -       - if CSUM is incorrect
527
528 */
529
530 static int
531 readchar ()
532 {
533   char buf;
534
535   buf = '\0';
536 #ifdef HAVE_TERMIO
537   /* termio does the timeout for us.  */
538   read (remote_desc, &buf, 1);
539 #else
540   alarm (timeout);
541   read (remote_desc, &buf, 1);
542   alarm (0);
543 #endif
544
545   return buf & 0x7f;
546 }
547
548 /* Send the command in BUF to the remote machine,
549    and read the reply into BUF.
550    Report an error if we get an error reply.  */
551
552 static void
553 remote_send (buf)
554      char *buf;
555 {
556
557   putpkt (buf);
558   getpkt (buf);
559
560   if (buf[0] == 'E')
561     error ("Remote failure reply: %s", buf);
562 }
563
564 /* Send a packet to the remote machine, with error checking.
565    The data of the packet is in BUF.  */
566
567 static void
568 putpkt (buf)
569      char *buf;
570 {
571   int i;
572   unsigned char csum = 0;
573   char buf2[500];
574   int cnt = strlen (buf);
575   char ch;
576   char *p;
577
578   /* Copy the packet into buffer BUF2, encapsulating it
579      and giving it a checksum.  */
580
581   p = buf2;
582   *p++ = '$';
583
584   for (i = 0; i < cnt; i++)
585     {
586       csum += buf[i];
587       *p++ = buf[i];
588     }
589   *p++ = '#';
590   *p++ = tohex ((csum >> 4) & 0xf);
591   *p++ = tohex (csum & 0xf);
592
593   /* Send it over and over until we get a positive ack.  */
594
595   do {
596     if (kiodebug)
597       {
598         *p = '\0';
599         printf ("Sending packet: %s (%s)\n", buf2, buf);
600       }
601     write (remote_desc, buf2, p - buf2);
602
603     /* read until either a timeout occurs (\0) or '+' is read */
604     do {
605       ch = readchar ();
606     } while ((ch != '+') && (ch != '\0'));
607   } while (ch != '+');
608 }
609
610 /* Read a packet from the remote machine, with error checking,
611    and store it in BUF.  */
612
613 static void
614 getpkt (buf)
615      char *buf;
616 {
617   char *bp;
618   unsigned char csum;
619   int c;
620   unsigned char c1, c2;
621
622 #if 0
623   /* Sorry, this will cause all hell to break loose, i.e. we'll end
624      up in the command loop with an inferior, but (at least if this
625      happens in remote_wait or some such place) without a current_frame,
626      having set up prev_* in wait_for_inferior, etc.
627
628      If it is necessary to have such an "emergency exit", seems like
629      the only plausible thing to do is to say the inferior died, and
630      make the user reattach if they want to.  Perhaps with a prompt
631      asking for confirmation.  */
632
633   /* allow immediate quit while reading from device, it could be hung */
634   immediate_quit++;
635 #endif /* 0 */
636
637   while (1)
638     {
639       /* Force csum to be zero here because of possible error retry.  */
640       csum = 0;
641       
642       while ((c = readchar()) != '$');
643
644       bp = buf;
645       while (1)
646         {
647           c = readchar ();
648           if (c == '#')
649             break;
650           *bp++ = c;
651           csum += c;
652         }
653       *bp = 0;
654
655       c1 = fromhex (readchar ());
656       c2 = fromhex (readchar ());
657       if ((csum & 0xff) == (c1 << 4) + c2)
658         break;
659       printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
660               (c1 << 4) + c2, csum & 0xff, buf);
661       write (remote_desc, "-", 1);
662     }
663
664 #if 0
665   immediate_quit--;
666 #endif
667
668   write (remote_desc, "+", 1);
669
670   if (kiodebug)
671     fprintf (stderr,"Packet received :%s\n", buf);
672 }
673 \f
674 /* The data cache leads to incorrect results because it doesn't know about
675    volatile variables, thus making it impossible to debug functions which
676    use hardware registers.  Therefore it is #if 0'd out.  Effect on
677    performance is some, for backtraces of functions with a few
678    arguments each.  For functions with many arguments, the stack
679    frames don't fit in the cache blocks, which makes the cache less
680    helpful.  Disabling the cache is a big performance win for fetching
681    large structures, because the cache code fetched data in 16-byte
682    chunks.  */
683 #if 0
684 /* The data cache records all the data read from the remote machine
685    since the last time it stopped.
686
687    Each cache block holds 16 bytes of data
688    starting at a multiple-of-16 address.  */
689
690 #define DCACHE_SIZE 64          /* Number of cache blocks */
691
692 struct dcache_block {
693         struct dcache_block *next, *last;
694         unsigned int addr;      /* Address for which data is recorded.  */
695         int data[4];
696 };
697
698 struct dcache_block dcache_free, dcache_valid;
699
700 /* Free all the data cache blocks, thus discarding all cached data.  */ 
701
702 static void
703 dcache_flush ()
704 {
705   register struct dcache_block *db;
706
707   while ((db = dcache_valid.next) != &dcache_valid)
708     {
709       remque (db);
710       insque (db, &dcache_free);
711     }
712 }
713
714 /*
715  * If addr is present in the dcache, return the address of the block 
716  * containing it.
717  */
718
719 struct dcache_block *
720 dcache_hit (addr)
721 {
722   register struct dcache_block *db;
723
724   if (addr & 3)
725     abort ();
726
727   /* Search all cache blocks for one that is at this address.  */
728   db = dcache_valid.next;
729   while (db != &dcache_valid)
730     {
731       if ((addr & 0xfffffff0) == db->addr)
732         return db;
733       db = db->next;
734     }
735   return NULL;
736 }
737
738 /*  Return the int data at address ADDR in dcache block DC.  */
739
740 int
741 dcache_value (db, addr)
742      struct dcache_block *db;
743      unsigned int addr;
744 {
745   if (addr & 3)
746     abort ();
747   return (db->data[(addr>>2)&3]);
748 }
749
750 /* Get a free cache block, put it on the valid list,
751    and return its address.  The caller should store into the block
752    the address and data that it describes.  */
753
754 struct dcache_block *
755 dcache_alloc ()
756 {
757   register struct dcache_block *db;
758
759   if ((db = dcache_free.next) == &dcache_free)
760     /* If we can't get one from the free list, take last valid */
761     db = dcache_valid.last;
762
763   remque (db);
764   insque (db, &dcache_valid);
765   return (db);
766 }
767
768 /* Return the contents of the word at address ADDR in the remote machine,
769    using the data cache.  */
770
771 int
772 dcache_fetch (addr)
773      CORE_ADDR addr;
774 {
775   register struct dcache_block *db;
776
777   db = dcache_hit (addr);
778   if (db == 0)
779     {
780       db = dcache_alloc ();
781       remote_read_bytes (addr & ~0xf, db->data, 16);
782       db->addr = addr & ~0xf;
783     }
784   return (dcache_value (db, addr));
785 }
786
787 /* Write the word at ADDR both in the data cache and in the remote machine.  */
788
789 dcache_poke (addr, data)
790      CORE_ADDR addr;
791      int data;
792 {
793   register struct dcache_block *db;
794
795   /* First make sure the word is IN the cache.  DB is its cache block.  */
796   db = dcache_hit (addr);
797   if (db == 0)
798     {
799       db = dcache_alloc ();
800       remote_read_bytes (addr & ~0xf, db->data, 16);
801       db->addr = addr & ~0xf;
802     }
803
804   /* Modify the word in the cache.  */
805   db->data[(addr>>2)&3] = data;
806
807   /* Send the changed word.  */
808   remote_write_bytes (addr, &data, 4);
809 }
810
811 /* Initialize the data cache.  */
812
813 dcache_init ()
814 {
815   register i;
816   register struct dcache_block *db;
817
818   db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * 
819                                         DCACHE_SIZE);
820   dcache_free.next = dcache_free.last = &dcache_free;
821   dcache_valid.next = dcache_valid.last = &dcache_valid;
822   for (i=0;i<DCACHE_SIZE;i++,db++)
823     insque (db, &dcache_free);
824 }
825 #endif /* 0 */
826
827 /* Define the target subroutine names */
828
829 struct target_ops remote_ops = {
830         "remote", "Remote serial target in gdb-specific protocol",
831         "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
832 Specify the serial device it is connected to (e.g. /dev/ttya).",
833         remote_open, remote_close,
834         0, remote_detach, remote_resume, remote_wait,  /* attach */
835         remote_fetch_registers, remote_store_registers,
836         remote_prepare_to_store, 0, 0, /* conv_from, conv_to */
837         remote_xfer_inferior_memory, remote_files_info,
838         0, 0, /* insert_breakpoint, remove_breakpoint, */
839         0, 0, 0, 0, 0,  /* Terminal crud */
840         0, /* kill */
841         0, add_syms_addr_command,  /* load */
842         call_function_by_hand,
843         0, /* lookup_symbol */
844         0, 0, /* create_inferior FIXME, mourn_inferior FIXME */
845         process_stratum, 0, /* next */
846         1, 1, 1, 1, 1,  /* all mem, mem, stack, regs, exec */
847         OPS_MAGIC,              /* Always the last thing */
848 };
849
850 void
851 _initialize_remote ()
852 {
853   add_target (&remote_ops);
854 }
This page took 0.071295 seconds and 4 git commands to generate.