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