* remote.c: Move comments regarding packets to top of file with the
[external/binutils.git] / gdb / remote.c
1 /* Remote target communications for serial-line targets in custom GDB protocol
2    Copyright 1988, 1991, 1992, 1993 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program 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 2 of the License, or
9 (at your option) any later version.
10
11 This program 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 this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* Remote communication protocol.
21
22    A debug packet whose contents are <data>
23    is encapsulated for transmission in the form:
24
25         $ <data> # CSUM1 CSUM2
26
27         <data> must be ASCII alphanumeric and cannot include characters
28         '$' or '#'
29
30         CSUM1 and CSUM2 are ascii hex representation of an 8-bit 
31         checksum of <data>, the most significant nibble is sent first.
32         the hex digits 0-9,a-f are used.
33
34    Receiver responds with:
35
36         +       - if CSUM is correct and ready for next packet
37         -       - if CSUM is incorrect
38
39    <data> is as follows:
40    All values are encoded in ascii hex digits.
41
42         Request         Packet
43
44         read registers  g
45         reply           XX....X         Each byte of register data
46                                         is described by two hex digits.
47                                         Registers are in the internal order
48                                         for GDB, and the bytes in a register
49                                         are in the same order the machine uses.
50                         or ENN          for an error.
51
52         write regs      GXX..XX         Each byte of register data
53                                         is described by two hex digits.
54         reply           OK              for success
55                         ENN             for an error
56
57         read mem        mAA..AA,LLLL    AA..AA is address, LLLL is length.
58         reply           XX..XX          XX..XX is mem contents
59                         or ENN          NN is errno
60
61         write mem       MAA..AA,LLLL:XX..XX
62                                         AA..AA is address,
63                                         LLLL is number of bytes,
64                                         XX..XX is data
65         reply           OK              for success
66                         ENN             for an error
67
68         cont            cAA..AA         AA..AA is address to resume
69                                         If AA..AA is omitted,
70                                         resume at same address.
71
72         step            sAA..AA         AA..AA is address to resume
73                                         If AA..AA is omitted,
74                                         resume at same address.
75
76         last signal     ?               Reply the current reason for stopping.
77                                         This is the same reply as is generated
78                                         for step or cont : SAA where AA is the
79                                         signal number.
80
81         There is no immediate reply to step or cont.
82         The reply comes when the machine stops.
83         It is           SAA             AA is the "signal number"
84
85         or...           TAAn...:r...;n:r...;n...:r...;
86                                         AA = signal number
87                                         n... = register number
88                                         r... = register contents
89
90         kill req        k
91 */
92
93 #include "defs.h"
94 #include <string.h>
95 #include <fcntl.h>
96 #include "frame.h"
97 #include "inferior.h"
98 #include "bfd.h"
99 #include "symfile.h"
100 #include "target.h"
101 #include "wait.h"
102 #include "terminal.h"
103 #include "gdbcmd.h"
104
105 #if !defined(DONT_USE_REMOTE)
106 #ifdef USG
107 #include <sys/types.h>
108 #endif
109
110 #include <signal.h>
111 #include "serial.h"
112
113 /* Prototypes for local functions */
114
115 static void
116 remote_write_bytes PARAMS ((CORE_ADDR, char *, int));
117
118 static void
119 remote_read_bytes PARAMS ((CORE_ADDR, char *, int));
120
121 static void
122 remote_files_info PARAMS ((struct target_ops *));
123
124 static int
125 remote_xfer_memory PARAMS ((CORE_ADDR, char *, int, int, struct target_ops *));
126
127 static void 
128 remote_prepare_to_store PARAMS ((void));
129
130 static void
131 remote_fetch_registers PARAMS ((int));
132
133 static void
134 remote_resume PARAMS ((int, int));
135
136 static int
137 remote_start_remote PARAMS ((char *));
138
139 static void
140 remote_open PARAMS ((char *, int));
141
142 static void
143 remote_close PARAMS ((int));
144
145 static void
146 remote_store_registers PARAMS ((int));
147
148 static void
149 getpkt PARAMS ((char *, int));
150
151 static void
152 putpkt PARAMS ((char *));
153
154 static void
155 remote_send PARAMS ((char *));
156
157 static int
158 readchar PARAMS ((void));
159
160 static int
161 remote_wait PARAMS ((WAITTYPE *));
162
163 static int
164 tohex PARAMS ((int));
165
166 static int
167 fromhex PARAMS ((int));
168
169 static void
170 remote_detach PARAMS ((char *, int));
171
172 extern struct target_ops remote_ops;    /* Forward decl */
173
174 static int kiodebug = 0;
175 /* This was 5 seconds, which is a long time to sit and wait.
176    Unless this is going though some terminal server or multiplexer or
177    other form of hairy serial connection, I would think 2 seconds would
178    be plenty.  */
179 static int timeout = 2;
180
181 #if 0
182 int icache;
183 #endif
184
185 /* Descriptor for I/O to remote machine.  Initialize it to -1 so that
186    remote_open knows that we don't have a file open when the program
187    starts.  */
188 serial_t remote_desc = NULL;
189
190 #define PBUFSIZ 1024
191
192 /* Maximum number of bytes to read/write at once.  The value here
193    is chosen to fill up a packet (the headers account for the 32).  */
194 #define MAXBUFBYTES ((PBUFSIZ-32)/2)
195
196 /* Round up PBUFSIZ to hold all the registers, at least.  */
197 #if REGISTER_BYTES > MAXBUFBYTES
198 #undef  PBUFSIZ
199 #define PBUFSIZ (REGISTER_BYTES * 2 + 32)
200 #endif
201 \f
202 /* Clean up connection to a remote debugger.  */
203
204 /* ARGSUSED */
205 static void
206 remote_close (quitting)
207      int quitting;
208 {
209   if (remote_desc)
210     SERIAL_CLOSE (remote_desc);
211   remote_desc = NULL;
212 }
213
214 /* Stub for catch_errors.  */
215
216 static int
217 remote_start_remote (dummy)
218      char *dummy;
219 {
220   /* Ack any packet which the remote side has already sent.  */
221   SERIAL_WRITE (remote_desc, "+\r", 2);
222   putpkt ("?");                 /* initiate a query from remote machine */
223
224   start_remote ();              /* Initialize gdb process mechanisms */
225   return 1;
226 }
227
228 /* Open a connection to a remote debugger.
229    NAME is the filename used for communication.  */
230
231 static void
232 remote_open (name, from_tty)
233      char *name;
234      int from_tty;
235 {
236   if (name == 0)
237     error (
238 "To open a remote debug connection, you need to specify what serial\n\
239 device is attached to the remote system (e.g. /dev/ttya).");
240
241   target_preopen (from_tty);
242
243   unpush_target (&remote_ops);
244
245 #if 0
246   dcache_init ();
247 #endif
248
249   remote_desc = SERIAL_OPEN (name);
250   if (!remote_desc)
251     perror_with_name (name);
252
253   if (baud_rate)
254     {
255       int rate;
256
257       if (sscanf (baud_rate, "%d", &rate) == 1)
258         if (SERIAL_SETBAUDRATE (remote_desc, rate))
259           {
260             SERIAL_CLOSE (remote_desc);
261             perror_with_name (name);
262           }
263     }
264
265   SERIAL_RAW (remote_desc);
266
267   if (from_tty)
268     {
269       puts_filtered ("Remote debugging using ");
270       puts_filtered (name);
271       puts_filtered ("\n");
272     }
273   push_target (&remote_ops);    /* Switch to using remote target now */
274
275   /* Start the remote connection; if error (0), discard this target. */
276   immediate_quit++;             /* Allow user to interrupt it */
277   if (!catch_errors (remote_start_remote, (char *)0, 
278         "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
279     pop_target();
280 }
281
282 /* remote_detach()
283    takes a program previously attached to and detaches it.
284    We better not have left any breakpoints
285    in the program or it'll die when it hits one.
286    Close the open connection to the remote debugger.
287    Use this when you want to detach and do something else
288    with your gdb.  */
289
290 static void
291 remote_detach (args, from_tty)
292      char *args;
293      int from_tty;
294 {
295   if (args)
296     error ("Argument given to \"detach\" when remotely debugging.");
297   
298   pop_target ();
299   if (from_tty)
300     puts_filtered ("Ending remote debugging.\n");
301 }
302
303 /* Convert hex digit A to a number.  */
304
305 static int
306 fromhex (a)
307      int a;
308 {
309   if (a >= '0' && a <= '9')
310     return a - '0';
311   else if (a >= 'a' && a <= 'f')
312     return a - 'a' + 10;
313   else
314     error ("Reply contains invalid hex digit");
315   return -1;
316 }
317
318 /* Convert number NIB to a hex digit.  */
319
320 static int
321 tohex (nib)
322      int nib;
323 {
324   if (nib < 10)
325     return '0'+nib;
326   else
327     return 'a'+nib-10;
328 }
329 \f
330 /* Tell the remote machine to resume.  */
331
332 static void
333 remote_resume (step, siggnal)
334      int step, siggnal;
335 {
336   char buf[PBUFSIZ];
337
338   if (siggnal)
339     {
340       char *name;
341       target_terminal_ours_for_output ();
342       printf_filtered ("Can't send signals to a remote system.  ");
343       name = strsigno (siggnal);
344       if (name)
345         printf_filtered (name);
346       else
347         printf_filtered ("Signal %d", siggnal);
348       printf_filtered (" not sent.\n");
349       target_terminal_inferior ();
350     }
351
352 #if 0
353   dcache_flush ();
354 #endif
355
356   strcpy (buf, step ? "s": "c");
357
358   putpkt (buf);
359 }
360 \f
361 static void remote_interrupt_twice PARAMS ((int));
362 static void (*ofunc)();
363
364 /* Send ^C to target to halt it.  Target will respond, and send us a
365    packet.  */
366
367 void remote_interrupt(signo)
368      int signo;
369 {
370   /* If this doesn't work, try more severe steps.  */
371   signal (signo, remote_interrupt_twice);
372   
373   if (kiodebug)
374     printf ("remote_interrupt called\n");
375
376   SERIAL_WRITE (remote_desc, "\003", 1); /* Send a ^C */
377 }
378
379 /* The user typed ^C twice.  */
380 static void
381 remote_interrupt_twice (signo)
382      int signo;
383 {
384   signal (signo, ofunc);
385   
386   target_terminal_ours ();
387   if (query ("Interrupted while waiting for the program.\n\
388 Give up (and stop debugging it)? "))
389     {
390       target_mourn_inferior ();
391       return_to_top_level (RETURN_QUIT);
392     }
393   else
394     {
395       signal (signo, remote_interrupt);
396       target_terminal_inferior ();
397     }
398 }
399
400 /* Wait until the remote machine stops, then return,
401    storing status in STATUS just as `wait' would.
402    Returns "pid" (though it's not clear what, if anything, that
403    means in the case of this target).  */
404
405 static int
406 remote_wait (status)
407      WAITTYPE *status;
408 {
409   unsigned char buf[PBUFSIZ];
410   unsigned char *p;
411   int i;
412   long regno;
413   char regs[MAX_REGISTER_RAW_SIZE];
414
415   WSETEXIT ((*status), 0);
416
417   ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
418   getpkt ((char *) buf, 1);
419   signal (SIGINT, ofunc);
420
421   if (buf[0] == 'E')
422     error ("Remote failure reply: %s", buf);
423   if (buf[0] == 'T')
424     {
425       /* Expedited reply, containing Signal, {regno, reg} repeat */
426       /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
427           ss = signal number
428           n... = register number
429           r... = register contents
430           */
431
432       p = &buf[3];              /* after Txx */
433
434       while (*p)
435         {
436           regno = strtol (p, &p, 16); /* Read the register number */
437
438           if (*p++ != ':'
439               || regno >= NUM_REGS)
440             error ("Remote sent bad register number %s", buf);
441
442           for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
443             {
444               if (p[0] == 0 || p[1] == 0)
445                 error ("Remote reply is too short: %s", buf);
446               regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
447               p += 2;
448             }
449
450           if (*p++ != ';')
451             error("Remote register badly formatted: %s", buf);
452
453           supply_register (regno, regs);
454         }
455     }
456   else if (buf[0] != 'S')
457     error ("Invalid remote reply: %s", buf);
458
459   WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
460
461   return 0;
462 }
463
464 /* Read the remote registers into the block REGS.  */
465 /* Currently we just read all the registers, so we don't use regno.  */
466 /* ARGSUSED */
467 static void
468 remote_fetch_registers (regno)
469      int regno;
470 {
471   char buf[PBUFSIZ];
472   int i;
473   char *p;
474   char regs[REGISTER_BYTES];
475
476   sprintf (buf, "g");
477   remote_send (buf);
478
479   /* Reply describes registers byte by byte, each byte encoded as two
480      hex characters.  Suck them all up, then supply them to the
481      register cacheing/storage mechanism.  */
482
483   p = buf;
484   for (i = 0; i < REGISTER_BYTES; i++)
485     {
486       if (p[0] == 0 || p[1] == 0)
487         error ("Remote reply is too short: %s", buf);
488       regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
489       p += 2;
490     }
491   for (i = 0; i < NUM_REGS; i++)
492     supply_register (i, &regs[REGISTER_BYTE(i)]);
493 }
494
495 /* Prepare to store registers.  Since we send them all, we have to
496    read out the ones we don't want to change first.  */
497
498 static void 
499 remote_prepare_to_store ()
500 {
501   /* Make sure the entire registers array is valid.  */
502   read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
503 }
504
505 /* Store the remote registers from the contents of the block REGISTERS. 
506    FIXME, eventually just store one register if that's all that is needed.  */
507
508 /* ARGSUSED */
509 static void
510 remote_store_registers (regno)
511      int regno;
512 {
513   char buf[PBUFSIZ];
514   int i;
515   char *p;
516
517   buf[0] = 'G';
518   
519   /* Command describes registers byte by byte,
520      each byte encoded as two hex characters.  */
521
522   p = buf + 1;
523   for (i = 0; i < REGISTER_BYTES; i++)
524     {
525       *p++ = tohex ((registers[i] >> 4) & 0xf);
526       *p++ = tohex (registers[i] & 0xf);
527     }
528   *p = '\0';
529
530   remote_send (buf);
531 }
532
533 #if 0
534 /* Read a word from remote address ADDR and return it.
535    This goes through the data cache.  */
536
537 int
538 remote_fetch_word (addr)
539      CORE_ADDR addr;
540 {
541   if (icache)
542     {
543       extern CORE_ADDR text_start, text_end;
544
545       if (addr >= text_start && addr < text_end)
546         {
547           int buffer;
548           xfer_core_file (addr, &buffer, sizeof (int));
549           return buffer;
550         }
551     }
552   return dcache_fetch (addr);
553 }
554
555 /* Write a word WORD into remote address ADDR.
556    This goes through the data cache.  */
557
558 void
559 remote_store_word (addr, word)
560      CORE_ADDR addr;
561      int word;
562 {
563   dcache_poke (addr, word);
564 }
565 #endif /* 0 */
566 \f
567 /* Write memory data directly to the remote machine.
568    This does not inform the data cache; the data cache uses this.
569    MEMADDR is the address in the remote memory space.
570    MYADDR is the address of the buffer in our space.
571    LEN is the number of bytes.  */
572
573 static void
574 remote_write_bytes (memaddr, myaddr, len)
575      CORE_ADDR memaddr;
576      char *myaddr;
577      int len;
578 {
579   char buf[PBUFSIZ];
580   int i;
581   char *p;
582
583   if (len > PBUFSIZ / 2 - 20)
584     abort ();
585
586   sprintf (buf, "M%x,%x:", memaddr, len);
587
588   /* We send target system values byte by byte, in increasing byte addresses,
589      each byte encoded as two hex characters.  */
590
591   p = buf + strlen (buf);
592   for (i = 0; i < len; i++)
593     {
594       *p++ = tohex ((myaddr[i] >> 4) & 0xf);
595       *p++ = tohex (myaddr[i] & 0xf);
596     }
597   *p = '\0';
598
599   remote_send (buf);
600 }
601
602 /* Read memory data directly from the remote machine.
603    This does not use the data cache; the data cache uses this.
604    MEMADDR is the address in the remote memory space.
605    MYADDR is the address of the buffer in our space.
606    LEN is the number of bytes.  */
607
608 static void
609 remote_read_bytes (memaddr, myaddr, len)
610      CORE_ADDR memaddr;
611      char *myaddr;
612      int len;
613 {
614   char buf[PBUFSIZ];
615   int i;
616   char *p;
617
618   if (len > PBUFSIZ / 2 - 1)
619     abort ();
620
621   sprintf (buf, "m%x,%x", memaddr, len);
622   remote_send (buf);
623
624   /* Reply describes memory byte by byte,
625      each byte encoded as two hex characters.  */
626
627   p = buf;
628   for (i = 0; i < len; i++)
629     {
630       if (p[0] == 0 || p[1] == 0)
631         error ("Remote reply is too short: %s", buf);
632       myaddr[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
633       p += 2;
634     }
635 }
636 \f
637 /* Read or write LEN bytes from inferior memory at MEMADDR, transferring
638    to or from debugger address MYADDR.  Write to inferior if SHOULD_WRITE is
639    nonzero.  Returns length of data written or read; 0 for error.  */
640
641 /* ARGSUSED */
642 static int
643 remote_xfer_memory(memaddr, myaddr, len, should_write, target)
644      CORE_ADDR memaddr;
645      char *myaddr;
646      int len;
647      int should_write;
648      struct target_ops *target;                 /* ignored */
649 {
650   int origlen = len;
651   int xfersize;
652   while (len > 0)
653     {
654       if (len > MAXBUFBYTES)
655         xfersize = MAXBUFBYTES;
656       else
657         xfersize = len;
658
659       if (should_write)
660         remote_write_bytes(memaddr, myaddr, xfersize);
661       else
662         remote_read_bytes (memaddr, myaddr, xfersize);
663       memaddr += xfersize;
664       myaddr  += xfersize;
665       len     -= xfersize;
666     }
667   return origlen; /* no error possible */
668 }
669
670 static void
671 remote_files_info (ignore)
672 struct target_ops *ignore;
673 {
674   puts_filtered ("Debugging a target over a serial line.\n");
675 }
676 \f
677 /* Stuff for dealing with the packets which are part of this protocol.
678    See comment at top of file for details.  */
679
680 /* Read a single character from the remote end, masking it down to 7 bits. */
681
682 static int
683 readchar ()
684 {
685   int ch;
686
687   ch = SERIAL_READCHAR (remote_desc, timeout);
688
689   if (ch < 0)
690     return ch;
691
692   return ch & 0x7f;
693 }
694
695 /* Send the command in BUF to the remote machine,
696    and read the reply into BUF.
697    Report an error if we get an error reply.  */
698
699 static void
700 remote_send (buf)
701      char *buf;
702 {
703
704   putpkt (buf);
705   getpkt (buf, 0);
706
707   if (buf[0] == 'E')
708     error ("Remote failure reply: %s", buf);
709 }
710
711 /* Send a packet to the remote machine, with error checking.
712    The data of the packet is in BUF.  */
713
714 static void
715 putpkt (buf)
716      char *buf;
717 {
718   int i;
719   unsigned char csum = 0;
720   char buf2[PBUFSIZ];
721   int cnt = strlen (buf);
722   int ch;
723   char *p;
724
725   /* Copy the packet into buffer BUF2, encapsulating it
726      and giving it a checksum.  */
727
728   if (cnt > sizeof(buf2) - 5)           /* Prosanity check */
729     abort();
730
731   p = buf2;
732   *p++ = '$';
733
734   for (i = 0; i < cnt; i++)
735     {
736       csum += buf[i];
737       *p++ = buf[i];
738     }
739   *p++ = '#';
740   *p++ = tohex ((csum >> 4) & 0xf);
741   *p++ = tohex (csum & 0xf);
742
743   /* Send it over and over until we get a positive ack.  */
744
745   while (1)
746     {
747       if (kiodebug)
748         {
749           *p = '\0';
750           printf ("Sending packet: %s...", buf2);  fflush(stdout);
751         }
752       if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
753         perror_with_name ("putpkt: write failed");
754
755       /* read until either a timeout occurs (-2) or '+' is read */
756       while (1)
757         {
758           ch = readchar ();
759
760           switch (ch)
761             {
762             case '+':
763               if (kiodebug)
764                 printf("Ack\n");
765               return;
766             case SERIAL_TIMEOUT:
767               break;            /* Retransmit buffer */
768             case SERIAL_ERROR:
769               perror_with_name ("putpkt: couldn't read ACK");
770             case SERIAL_EOF:
771               error ("putpkt: EOF while trying to read ACK");
772             default:
773               if (kiodebug)
774                 printf ("%02X %c ", ch&0xFF, ch);
775               continue;
776             }
777           break;                /* Here to retransmit */
778         }
779     }
780 }
781
782 /* Read a packet from the remote machine, with error checking,
783    and store it in BUF.  BUF is expected to be of size PBUFSIZ.
784    If FOREVER, wait forever rather than timing out; this is used
785    while the target is executing user code.  */
786
787 static void
788 getpkt (buf, forever)
789      char *buf;
790      int forever;
791 {
792   char *bp;
793   unsigned char csum;
794   int c = 0;
795   unsigned char c1, c2;
796   int retries = 0;
797 #define MAX_RETRIES     10
798
799   while (1)
800     {
801       /* This can loop forever if the remote side sends us characters
802          continuously, but if it pauses, we'll get a zero from readchar
803          because of timeout.  Then we'll count that as a retry.  */
804
805       c = readchar();
806       if (c > 0 && c != '$')
807         continue;
808
809       if (c == SERIAL_TIMEOUT)
810         {
811           if (forever)
812             continue;
813           if (++retries >= MAX_RETRIES)
814             if (kiodebug) puts_filtered ("Timed out.\n");
815           goto out;
816         }
817
818       if (c == SERIAL_EOF)
819         error ("Remote connection closed");
820       if (c == SERIAL_ERROR)
821         perror_with_name ("Remote communication error");
822
823       /* Force csum to be zero here because of possible error retry.  */
824       csum = 0;
825       bp = buf;
826
827       while (1)
828         {
829           c = readchar ();
830           if (c == SERIAL_TIMEOUT)
831             {
832               if (kiodebug)
833                 puts_filtered ("Timeout in mid-packet, retrying\n");
834               goto whole;               /* Start a new packet, count retries */
835             } 
836           if (c == '$')
837             {
838               if (kiodebug)
839                 puts_filtered ("Saw new packet start in middle of old one\n");
840               goto whole;               /* Start a new packet, count retries */
841             }
842           if (c == '#')
843             break;
844           if (bp >= buf+PBUFSIZ-1)
845           {
846             *bp = '\0';
847             puts_filtered ("Remote packet too long: ");
848             puts_filtered (buf);
849             puts_filtered ("\n");
850             goto whole;
851           }
852           *bp++ = c;
853           csum += c;
854         }
855       *bp = 0;
856
857       c1 = fromhex (readchar ());
858       c2 = fromhex (readchar ());
859       if ((csum & 0xff) == (c1 << 4) + c2)
860         break;
861       printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
862               (c1 << 4) + c2, csum & 0xff);
863       puts_filtered (buf);
864       puts_filtered ("\n");
865
866       /* Try the whole thing again.  */
867 whole:
868       if (++retries < MAX_RETRIES)
869         {
870           SERIAL_WRITE (remote_desc, "-", 1);
871         }
872       else
873         {
874           printf ("Ignoring packet error, continuing...\n");
875           break;
876         }
877     }
878
879 out:
880
881   SERIAL_WRITE (remote_desc, "+", 1);
882
883   if (kiodebug)
884     fprintf (stderr,"Packet received: %s\n", buf);
885 }
886 \f
887 /* The data cache leads to incorrect results because it doesn't know about
888    volatile variables, thus making it impossible to debug functions which
889    use hardware registers.  Therefore it is #if 0'd out.  Effect on
890    performance is some, for backtraces of functions with a few
891    arguments each.  For functions with many arguments, the stack
892    frames don't fit in the cache blocks, which makes the cache less
893    helpful.  Disabling the cache is a big performance win for fetching
894    large structures, because the cache code fetched data in 16-byte
895    chunks.  */
896 #if 0
897 /* The data cache records all the data read from the remote machine
898    since the last time it stopped.
899
900    Each cache block holds 16 bytes of data
901    starting at a multiple-of-16 address.  */
902
903 #define DCACHE_SIZE 64          /* Number of cache blocks */
904
905 struct dcache_block {
906         struct dcache_block *next, *last;
907         unsigned int addr;      /* Address for which data is recorded.  */
908         int data[4];
909 };
910
911 struct dcache_block dcache_free, dcache_valid;
912
913 /* Free all the data cache blocks, thus discarding all cached data.  */ 
914
915 static void
916 dcache_flush ()
917 {
918   register struct dcache_block *db;
919
920   while ((db = dcache_valid.next) != &dcache_valid)
921     {
922       remque (db);
923       insque (db, &dcache_free);
924     }
925 }
926
927 /*
928  * If addr is present in the dcache, return the address of the block 
929  * containing it.
930  */
931
932 struct dcache_block *
933 dcache_hit (addr)
934 {
935   register struct dcache_block *db;
936
937   if (addr & 3)
938     abort ();
939
940   /* Search all cache blocks for one that is at this address.  */
941   db = dcache_valid.next;
942   while (db != &dcache_valid)
943     {
944       if ((addr & 0xfffffff0) == db->addr)
945         return db;
946       db = db->next;
947     }
948   return NULL;
949 }
950
951 /*  Return the int data at address ADDR in dcache block DC.  */
952
953 int
954 dcache_value (db, addr)
955      struct dcache_block *db;
956      unsigned int addr;
957 {
958   if (addr & 3)
959     abort ();
960   return (db->data[(addr>>2)&3]);
961 }
962
963 /* Get a free cache block, put it on the valid list,
964    and return its address.  The caller should store into the block
965    the address and data that it describes.  */
966
967 struct dcache_block *
968 dcache_alloc ()
969 {
970   register struct dcache_block *db;
971
972   if ((db = dcache_free.next) == &dcache_free)
973     /* If we can't get one from the free list, take last valid */
974     db = dcache_valid.last;
975
976   remque (db);
977   insque (db, &dcache_valid);
978   return (db);
979 }
980
981 /* Return the contents of the word at address ADDR in the remote machine,
982    using the data cache.  */
983
984 int
985 dcache_fetch (addr)
986      CORE_ADDR addr;
987 {
988   register struct dcache_block *db;
989
990   db = dcache_hit (addr);
991   if (db == 0)
992     {
993       db = dcache_alloc ();
994       remote_read_bytes (addr & ~0xf, db->data, 16);
995       db->addr = addr & ~0xf;
996     }
997   return (dcache_value (db, addr));
998 }
999
1000 /* Write the word at ADDR both in the data cache and in the remote machine.  */
1001
1002 dcache_poke (addr, data)
1003      CORE_ADDR addr;
1004      int data;
1005 {
1006   register struct dcache_block *db;
1007
1008   /* First make sure the word is IN the cache.  DB is its cache block.  */
1009   db = dcache_hit (addr);
1010   if (db == 0)
1011     {
1012       db = dcache_alloc ();
1013       remote_read_bytes (addr & ~0xf, db->data, 16);
1014       db->addr = addr & ~0xf;
1015     }
1016
1017   /* Modify the word in the cache.  */
1018   db->data[(addr>>2)&3] = data;
1019
1020   /* Send the changed word.  */
1021   remote_write_bytes (addr, &data, 4);
1022 }
1023
1024 /* Initialize the data cache.  */
1025
1026 dcache_init ()
1027 {
1028   register i;
1029   register struct dcache_block *db;
1030
1031   db = (struct dcache_block *) xmalloc (sizeof (struct dcache_block) * 
1032                                         DCACHE_SIZE);
1033   dcache_free.next = dcache_free.last = &dcache_free;
1034   dcache_valid.next = dcache_valid.last = &dcache_valid;
1035   for (i=0;i<DCACHE_SIZE;i++,db++)
1036     insque (db, &dcache_free);
1037 }
1038 #endif /* 0 */
1039 \f
1040 static void
1041 remote_kill ()
1042 {
1043   putpkt ("k");
1044   /* Don't wait for it to die.  I'm not really sure it matters whether
1045      we do or not.  For the existing stubs, kill is a noop.  */
1046   target_mourn_inferior ();
1047 }
1048
1049 static void
1050 remote_mourn ()
1051 {
1052   unpush_target (&remote_ops);
1053   generic_mourn_inferior ();
1054 }
1055 \f
1056 /* Define the target subroutine names */
1057
1058 struct target_ops remote_ops = {
1059   "remote",                     /* to_shortname */
1060   "Remote serial target in gdb-specific protocol",      /* to_longname */
1061   "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
1062 Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
1063   remote_open,                  /* to_open */
1064   remote_close,                 /* to_close */
1065   NULL,                         /* to_attach */
1066   remote_detach,                /* to_detach */
1067   remote_resume,                /* to_resume */
1068   remote_wait,                  /* to_wait */
1069   remote_fetch_registers,       /* to_fetch_registers */
1070   remote_store_registers,       /* to_store_registers */
1071   remote_prepare_to_store,      /* to_prepare_to_store */
1072   remote_xfer_memory,           /* to_xfer_memory */
1073   remote_files_info,            /* to_files_info */
1074   NULL,                         /* to_insert_breakpoint */
1075   NULL,                         /* to_remove_breakpoint */
1076   NULL,                         /* to_terminal_init */
1077   NULL,                         /* to_terminal_inferior */
1078   NULL,                         /* to_terminal_ours_for_output */
1079   NULL,                         /* to_terminal_ours */
1080   NULL,                         /* to_terminal_info */
1081   remote_kill,                  /* to_kill */
1082   generic_load,                 /* to_load */
1083   NULL,                         /* to_lookup_symbol */
1084   NULL,                         /* to_create_inferior */
1085   remote_mourn,                 /* to_mourn_inferior */
1086   0,                            /* to_can_run */
1087   0,                            /* to_notice_signals */
1088   process_stratum,              /* to_stratum */
1089   NULL,                         /* to_next */
1090   1,                            /* to_has_all_memory */
1091   1,                            /* to_has_memory */
1092   1,                            /* to_has_stack */
1093   1,                            /* to_has_registers */
1094   1,                            /* to_has_execution */
1095   NULL,                         /* sections */
1096   NULL,                         /* sections_end */
1097   OPS_MAGIC                     /* to_magic */
1098 };
1099
1100 void
1101 _initialize_remote ()
1102 {
1103   add_target (&remote_ops);
1104
1105   add_show_from_set (
1106     add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug,
1107                    "Set debugging of remote serial I/O.\n\
1108 When enabled, each packet sent or received with the remote target\n\
1109 is displayed.", &setlist),
1110         &showlist);
1111 }
1112
1113 #endif