* symtab.c (find_pc_symtab): some object file formats, notably mips,
[deliverable/binutils-gdb.git] / gdb / remote.c
index 85621249df4429b0db4eca669030d885d440a5d4..a17b0052e45354da37b3712a4ea1a4c923135671 100644 (file)
@@ -1,5 +1,5 @@
 /* Remote target communications for serial-line targets in custom GDB protocol
-   Copyright 1988, 1991, 1992 Free Software Foundation, Inc.
+   Copyright 1988, 1991, 1992, 1993 Free Software Foundation, Inc.
 
 This file is part of GDB.
 
@@ -63,19 +63,25 @@ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
        The reply comes when the machine stops.
        It is           SAA             AA is the "signal number"
 
+       or...           TAAPPPPPPPPFFFFFFFF
+                                       where AA is the signal number,
+                                       PPPPPPPP is the PC (PC_REGNUM), and
+                                       FFFFFFFF is the frame ptr (FP_REGNUM).
+
        kill req        k
 */
 
-#include <stdio.h>
+#include "defs.h"
 #include <string.h>
 #include <fcntl.h>
-#include "defs.h"
 #include "frame.h"
 #include "inferior.h"
 #include "target.h"
 #include "wait.h"
 #include "terminal.h"
+#include "gdbcmd.h"
 
+#if !defined(DONT_USE_REMOTE)
 #ifdef USG
 #include <sys/types.h>
 #endif
@@ -105,6 +111,9 @@ remote_fetch_registers PARAMS ((int));
 static void
 remote_resume PARAMS ((int, int));
 
+static int
+remote_start_remote PARAMS ((char *));
+
 static void
 remote_open PARAMS ((char *, int));
 
@@ -115,7 +124,7 @@ static void
 remote_store_registers PARAMS ((int));
 
 static void
-getpkt PARAMS ((char *));
+getpkt PARAMS ((char *, int));
 
 static void
 putpkt PARAMS ((char *));
@@ -141,7 +150,7 @@ remote_detach PARAMS ((char *, int));
 
 extern struct target_ops remote_ops;   /* Forward decl */
 
-static int kiodebug;
+static int kiodebug = 0;
 static int timeout = 5;
 
 #if 0
@@ -168,7 +177,8 @@ int remote_desc = -1;
 /* Called when SIGALRM signal sent due to alarm() timeout.  */
 #ifndef HAVE_TERMIO
 void
-remote_timer ()
+remote_timer (signo)
+     int signo;
 {
   if (kiodebug)
     printf ("remote_timer called\n");
@@ -199,6 +209,8 @@ remote_close (quitting)
 #define B38400 EXTB
 #endif
 
+
+
 static struct {int rate, damn_b;} baudtab[] = {
        {0, B0},
        {50, B50},
@@ -230,6 +242,20 @@ damn_b (rate)
   return B38400;       /* Random */
 }
 
+/* Stub for catch_errors.  */
+
+static int
+remote_start_remote (dummy)
+     char *dummy;
+{
+  /* Ack any packet which the remote side has already sent.  */
+  write (remote_desc, "+\r", 2);
+  putpkt ("?");                        /* initiate a query from remote machine */
+
+  start_remote ();             /* Initialize gdb process mechanisms */
+  return 1;
+}
+
 /* Open a connection to a remote debugger.
    NAME is the filename used for communication.  */
 
@@ -239,7 +265,7 @@ remote_open (name, from_tty)
      int from_tty;
 {
   TERMINAL sg;
-  int a_rate, b_rate;
+  int a_rate, b_rate = 0;
   int baudrate_set = 0;
 
   if (name == 0)
@@ -261,7 +287,7 @@ device is attached to the remote system (e.g. /dev/ttya).");
 
   if (baud_rate)
     {
-      if (1 != sscanf (baud_rate, "%d ", &a_rate))
+      if (sscanf (baud_rate, "%d", &a_rate) == 1)
        {
          b_rate = damn_b (a_rate);
          baudrate_set = 1;
@@ -276,7 +302,7 @@ device is attached to the remote system (e.g. /dev/ttya).");
   sg.c_cflag &= ~PARENB;       /* No parity */
   sg.c_cflag |= CS8;           /* 8-bit path */
   if (baudrate_set)
-    sg.c_cflag = (sb.c_cflag & ~CBAUD) | b_rate;
+    sg.c_cflag = (sg.c_cflag & ~CBAUD) | b_rate;
 #else
   sg.sg_flags |= RAW | ANYP;
   sg.sg_flags &= ~ECHO;
@@ -289,7 +315,11 @@ device is attached to the remote system (e.g. /dev/ttya).");
   ioctl (remote_desc, TIOCSETP, &sg);
 
   if (from_tty)
-    printf ("Remote debugging using %s\n", name);
+    {
+      puts_filtered ("Remote debugging using ");
+      puts_filtered (name);
+      puts_filtered ("\n");
+    }
   push_target (&remote_ops);   /* Switch to using remote target now */
 
 #ifndef HAVE_TERMIO
@@ -304,11 +334,11 @@ device is attached to the remote system (e.g. /dev/ttya).");
     perror ("remote_open: error in signal");
 #endif
 
-  /* Ack any packet which the remote side has already sent.  */
-  write (remote_desc, "+", 1);
-  putpkt ("?");                        /* initiate a query from remote machine */
-
-  start_remote ();             /* Initialize gdb process mechanisms */
+  /* Start the remote connection; if error (0), discard this target. */
+  immediate_quit++;            /* Allow user to interrupt it */
+  if (!catch_errors (remote_start_remote, (char *)0, 
+       "Couldn't establish connection to remote target\n"))
+    pop_target();
 }
 
 /* remote_detach()
@@ -329,7 +359,7 @@ remote_detach (args, from_tty)
   
   pop_target ();
   if (from_tty)
-    printf ("Ending remote debugging.\n");
+    puts_filtered ("Ending remote debugging.\n");
 }
 
 /* Convert hex digit A to a number.  */
@@ -368,7 +398,8 @@ remote_resume (step, siggnal)
   char buf[PBUFSIZ];
 
   if (siggnal)
-    error ("Can't send signals to a remote system.");
+    error ("Can't send signals to a remote system.  Try `handle %d ignore'.",
+          siggnal);
 
 #if 0
   dcache_flush ();
@@ -382,8 +413,13 @@ remote_resume (step, siggnal)
 /* Send ^C to target to halt it.  Target will respond, and send us a
    packet.  */
 
-void remote_interrupt()
+void remote_interrupt(signo)
+     int signo;
 {
+  
+  if (kiodebug)
+    printf ("remote_interrupt called\n");
+
   write (remote_desc, "\003", 1);      /* Send a ^C */
 }
 
@@ -399,23 +435,61 @@ remote_wait (status)
 {
   unsigned char buf[PBUFSIZ];
   void (*ofunc)();
-  
+  unsigned char *p;
+  int i;
+  long regno;
+  char regs[MAX_REGISTER_RAW_SIZE];
+
   WSETEXIT ((*status), 0);
 
-  ofunc = signal (SIGINT, remote_interrupt);
-  getpkt ((char *) buf);
+  ofunc = (void (*)()) signal (SIGINT, remote_interrupt);
+  getpkt ((char *) buf, 1);
   signal (SIGINT, ofunc);
 
   if (buf[0] == 'E')
     error ("Remote failure reply: %s", buf);
-  if (buf[0] != 'S')
+  if (buf[0] == 'T')
+    {
+      /* Expedited reply, containing Signal, {regno, reg} repeat */
+      /*  format is:  'Tssn...:r...;n...:r...;n...:r...;#cc', where
+         ss = signal number
+         n... = register number
+         r... = register contents
+         */
+
+      p = &buf[3];             /* after Txx */
+
+      while (*p)
+       {
+         regno = strtol (p, &p, 16); /* Read the register number */
+
+         if (*p++ != ':'
+             || regno >= NUM_REGS)
+           error ("Remote sent bad register number %s", buf);
+
+         for (i = 0; i < REGISTER_RAW_SIZE (regno); i++)
+           {
+             if (p[0] == 0 || p[1] == 0)
+               error ("Remote reply is too short: %s", buf);
+             regs[i] = fromhex (p[0]) * 16 + fromhex (p[1]);
+             p += 2;
+           }
+
+         if (*p++ != ';')
+           error("Remote register badly formatted: %s", buf);
+
+         supply_register (regno, regs);
+       }
+    }
+  else if (buf[0] != 'S')
     error ("Invalid remote reply: %s", buf);
+
   WSETSTOP ((*status), (((fromhex (buf[1])) << 4) + (fromhex (buf[2]))));
+
   return 0;
 }
 
 /* Read the remote registers into the block REGS.  */
-
 /* Currently we just read all the registers, so we don't use regno.  */
 /* ARGSUSED */
 static void
@@ -452,7 +526,8 @@ remote_fetch_registers (regno)
 static void 
 remote_prepare_to_store ()
 {
-  remote_fetch_registers (-1);
+  /* Make sure the entire registers array is valid.  */
+  read_register_bytes (0, (char *)NULL, REGISTER_BYTES);
 }
 
 /* Store the remote registers from the contents of the block REGISTERS. 
@@ -621,10 +696,10 @@ remote_xfer_memory(memaddr, myaddr, len, should_write, target)
 }
 
 static void
-remote_files_info (target)
-struct target_ops *target;
+remote_files_info (ignore)
+struct target_ops *ignore;
 {
-  printf ("remote files info missing here.  FIXME.\n");
+  puts_filtered ("Debugging a target over a serial line.\n");
 }
 \f
 /*
@@ -649,18 +724,23 @@ Receiver responds with:
 */
 
 /* Read a single character from the remote end.
-   (If supported, we actually read many characters and buffer them up.)  */
+   (If supported, we actually read many characters and buffer them up.)
+   Timeouts cause a zero (nul) to be returned.  */
 
 static int
 readchar ()
 {
-  char buf;
   static int inbuf_index, inbuf_count;
 #define        INBUFSIZE       PBUFSIZ
   static char inbuf[INBUFSIZE];
+  struct cleanup *old_chain;
 
   if (inbuf_index >= inbuf_count)
     {
+#ifndef HAVE_TERMIO
+      extern int alarm ();
+#endif
+
       /* Time to do another read... */
       inbuf_index = 0;
       inbuf_count = 0;
@@ -669,13 +749,16 @@ readchar ()
       /* termio does the timeout for us.  */
       inbuf_count = read (remote_desc, inbuf, INBUFSIZE);
 #else
+      /* Cancel alarm on error.  */
+      old_chain = make_cleanup (alarm, (char *)0);
       alarm (timeout);
       inbuf_count = read (remote_desc, inbuf, INBUFSIZE);
-      alarm (0);
+      do_cleanups (old_chain);         /* Cancel the alarm now.  */
 #endif
     }
 
-  /* Just return the next character from the buffer.  */
+  /* Just return the next character from the buffer (or a zero if we
+     got an error and no chars were stored in inbuf).  */
   return inbuf[inbuf_index++] & 0x7f;
 }
 
@@ -689,7 +772,7 @@ remote_send (buf)
 {
 
   putpkt (buf);
-  getpkt (buf);
+  getpkt (buf, 0);
 
   if (buf[0] == 'E')
     error ("Remote failure reply: %s", buf);
@@ -733,28 +816,38 @@ putpkt (buf)
     if (kiodebug)
       {
        *p = '\0';
-       printf ("Sending packet: %s (%s)\n", buf2, buf);
+       printf ("Sending packet: %s...", buf2);  fflush(stdout);
       }
     write (remote_desc, buf2, p - buf2);
 
     /* read until either a timeout occurs (\0) or '+' is read */
     do {
       ch = readchar ();
+      if (kiodebug) {
+       if (ch == '+')
+         printf("Ack\n");
+       else
+         printf ("%02X%c ", ch&0xFF, ch);
+      }
     } while ((ch != '+') && (ch != '\0'));
   } while (ch != '+');
 }
 
 /* Read a packet from the remote machine, with error checking,
-   and store it in BUF.  */
+   and store it in BUF.  BUF is expected to be of size PBUFSIZ.
+   If FOREVER, wait forever rather than timing out; this is used
+   while the target is executing user code.  */
 
 static void
-getpkt (buf)
+getpkt (buf, forever)
      char *buf;
 {
   char *bp;
   unsigned char csum;
-  int c;
+  int c = 0;
   unsigned char c1, c2;
+  int retries = 0;
+#define MAX_RETRIES    10
 
 #if 0
   /* Sorry, this will cause all hell to break loose, i.e. we'll end
@@ -773,17 +866,47 @@ getpkt (buf)
 
   while (1)
     {
+      /* This can loop forever if the remote side sends us characters
+        continuously, but if it pauses, we'll get a zero from readchar
+        because of timeout.  Then we'll count that as a retry.  */
+      while (c != '$')
+        if (0 == (c  = readchar()))
+         if (!forever) 
+           {
+             if (++retries >= MAX_RETRIES)
+               if (kiodebug) puts_filtered ("Timed out.\n");
+               goto out;
+           }
+
       /* Force csum to be zero here because of possible error retry.  */
       csum = 0;
-      
-      while ((c = readchar()) != '$');
-
       bp = buf;
+
       while (1)
        {
          c = readchar ();
+         if (c == '\0')
+           {
+             if (kiodebug)
+               puts_filtered ("Timeout in mid-packet, retrying\n");
+             goto whole;               /* Start a new packet, count retries */
+           } 
+         if (c == '$')
+           {
+             if (kiodebug)
+               puts_filtered ("Saw new packet start in middle of old one\n");
+             goto whole;               /* Start a new packet, count retries */
+           }
          if (c == '#')
            break;
+         if (bp >= buf+PBUFSIZ-1)
+         {
+           *bp = '\0';
+           puts_filtered ("Remote packet too long: ");
+           puts_filtered (buf);
+           puts_filtered ("\n");
+           goto whole;
+         }
          *bp++ = c;
          csum += c;
        }
@@ -793,11 +916,26 @@ getpkt (buf)
       c2 = fromhex (readchar ());
       if ((csum & 0xff) == (c1 << 4) + c2)
        break;
-      printf ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=%s\n",
-             (c1 << 4) + c2, csum & 0xff, buf);
-      write (remote_desc, "-", 1);
+      printf_filtered ("Bad checksum, sentsum=0x%x, csum=0x%x, buf=",
+             (c1 << 4) + c2, csum & 0xff);
+      puts_filtered (buf);
+      puts_filtered ("\n");
+
+      /* Try the whole thing again.  */
+whole:
+      if (++retries < MAX_RETRIES)
+       {
+         write (remote_desc, "-", 1);
+       }
+      else
+       {
+         printf ("Ignoring packet error, continuing...\n");
+         break;
+       }
     }
 
+out:
+
 #if 0
   immediate_quit--;
 #endif
@@ -805,7 +943,7 @@ getpkt (buf)
   write (remote_desc, "+", 1);
 
   if (kiodebug)
-    fprintf (stderr,"Packet received :%s\n", buf);
+    fprintf (stderr,"Packet received%s\n", buf);
 }
 \f
 /* The data cache leads to incorrect results because it doesn't know about
@@ -977,8 +1115,6 @@ Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
   remote_fetch_registers,      /* to_fetch_registers */
   remote_store_registers,      /* to_store_registers */
   remote_prepare_to_store,     /* to_prepare_to_store */
-  NULL,                                /* to_convert_to_virtual */
-  NULL,                                /* to_convert_from_virtual */
   remote_xfer_memory,          /* to_xfer_memory */
   remote_files_info,           /* to_files_info */
   NULL,                                /* to_insert_breakpoint */
@@ -993,6 +1129,8 @@ Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
   NULL,                                /* to_lookup_symbol */
   NULL,                                /* to_create_inferior */
   NULL,                                /* to_mourn_inferior */
+  0,                           /* to_can_run */
+  0,                           /* to_notice_signals */
   process_stratum,             /* to_stratum */
   NULL,                                /* to_next */
   1,                           /* to_has_all_memory */
@@ -1009,4 +1147,13 @@ void
 _initialize_remote ()
 {
   add_target (&remote_ops);
+
+  add_show_from_set (
+    add_set_cmd ("remotedebug", no_class, var_boolean, (char *)&kiodebug,
+                  "Set debugging of remote serial I/O.\n\
+When enabled, each packet sent or received with the remote target\n\
+is displayed.", &setlist),
+       &showlist);
 }
+
+#endif
This page took 0.029944 seconds and 4 git commands to generate.