Thu May 21 13:14:25 1998 John Metzler <jmetzler@cygnus.com>
[deliverable/binutils-gdb.git] / gdb / remote.c
index 1e23db1a18df03e3c452ad9715088e53df530ef5..8f94e161d1df05261882e6d05d2bb34926a09f3a 100644 (file)
@@ -227,7 +227,7 @@ static void remote_open PARAMS ((char *name, int from_tty));
 
 static void extended_remote_open PARAMS ((char *name, int from_tty));
 
-static void remote_open_1 PARAMS ((char *, int, struct target_ops *));
+static void remote_open_1 PARAMS ((char *, int, struct target_ops *, int extended_p));
 
 static void remote_close PARAMS ((int quitting));
 
@@ -243,10 +243,6 @@ static void extended_remote_create_inferior PARAMS ((char *, char *, char **));
 
 static void remote_mourn_1 PARAMS ((struct target_ops *));
 
-static void getpkt PARAMS ((char *buf, int forever));
-
-static int putpkt PARAMS ((char *buf));
-
 static void remote_send PARAMS ((char *buf));
 
 static int readchar PARAMS ((int timeout));
@@ -257,8 +253,6 @@ static void remote_kill PARAMS ((void));
 
 static int tohex PARAMS ((int nib));
 
-static int fromhex PARAMS ((int a));
-
 static void remote_detach PARAMS ((char *args, int from_tty));
 
 static void remote_interrupt PARAMS ((int signo));
@@ -279,8 +273,110 @@ static int remote_insert_breakpoint PARAMS ((CORE_ADDR, char *));
 
 static int remote_remove_breakpoint PARAMS ((CORE_ADDR, char *));
 
-static struct target_ops remote_ops;   /* Forward decl */
-static struct target_ops extended_remote_ops;  /* Forward decl */
+static int hexnumlen PARAMS ((ULONGEST num));
+
+/* exported functions */
+
+extern int fromhex PARAMS ((int a));
+extern void getpkt PARAMS ((char *buf, int forever));
+extern int putpkt PARAMS ((char *buf));
+
+/* Define the target subroutine names */
+
+static struct target_ops remote_ops ;
+
+static void init_remote_ops(void)
+{
+  remote_ops.to_shortname =   "remote";                
+  remote_ops.to_longname =   "Remote serial target in gdb-specific protocol";
+  remote_ops.to_doc =   "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
+Specify the serial device it is connected to (e.g. /dev/ttya)." ;  
+  remote_ops.to_open =   remote_open;          
+  remote_ops.to_close =   remote_close;                
+  remote_ops.to_attach =   NULL;               
+  remote_ops.to_detach =   remote_detach;      
+  remote_ops.to_resume =   remote_resume;      
+  remote_ops.to_wait  =   remote_wait;         
+  remote_ops.to_fetch_registers  =   remote_fetch_registers;
+  remote_ops.to_store_registers  =   remote_store_registers;
+  remote_ops.to_prepare_to_store =   remote_prepare_to_store;
+  remote_ops.to_xfer_memory  =   remote_xfer_memory;   
+  remote_ops.to_files_info  =   remote_files_info;     
+  remote_ops.to_insert_breakpoint =   remote_insert_breakpoint;
+  remote_ops.to_remove_breakpoint =   remote_remove_breakpoint;
+  remote_ops.to_terminal_init  =   NULL;               
+  remote_ops.to_terminal_inferior =   NULL;            
+  remote_ops.to_terminal_ours_for_output =   NULL;
+  remote_ops.to_terminal_ours  =   NULL;       
+  remote_ops.to_terminal_info  =   NULL;       
+  remote_ops.to_kill  =   remote_kill;         
+  remote_ops.to_load  =   generic_load;                
+  remote_ops.to_lookup_symbol =   NULL;                
+  remote_ops.to_create_inferior =   NULL;      
+  remote_ops.to_mourn_inferior =   remote_mourn;
+  remote_ops.to_can_run  =   0;                        
+  remote_ops.to_notice_signals =   0;          
+  remote_ops.to_thread_alive  =   remote_thread_alive;
+  remote_ops.to_stop  =   0;           
+  remote_ops.to_stratum =   process_stratum;
+  remote_ops.DONT_USE =   NULL;                
+  remote_ops.to_has_all_memory =   1;  
+  remote_ops.to_has_memory =   1;      
+  remote_ops.to_has_stack =   1;       
+  remote_ops.to_has_registers =   1;   
+  remote_ops.to_has_execution =   1;   
+  remote_ops.to_sections =   NULL;     
+  remote_ops.to_sections_end =   NULL; 
+  remote_ops.to_magic =   OPS_MAGIC ;  
+} /* init_remote_ops */
+
+static struct target_ops extended_remote_ops ;
+
+static void init_extended_remote_ops(void) 
+{
+  extended_remote_ops.to_shortname =   "extended-remote";      
+  extended_remote_ops.to_longname =   "Extended remote serial target in gdb-specific protocol";
+  extended_remote_ops.to_doc =   "Use a remote computer via a serial line; using a gdb-specific protocol.\n\
+Specify the serial device it is connected to (e.g. /dev/ttya).",
+    extended_remote_ops.to_open =   extended_remote_open;      
+  extended_remote_ops.to_close =   remote_close;       
+  extended_remote_ops.to_attach =   NULL;              
+  extended_remote_ops.to_detach =   remote_detach;     
+  extended_remote_ops.to_resume =   remote_resume;     
+  extended_remote_ops.to_wait  =   remote_wait;                
+  extended_remote_ops.to_fetch_registers  =   remote_fetch_registers;
+  extended_remote_ops.to_store_registers  =   remote_store_registers;  
+  extended_remote_ops.to_prepare_to_store =   remote_prepare_to_store;
+  extended_remote_ops.to_xfer_memory  =   remote_xfer_memory;  
+  extended_remote_ops.to_files_info  =   remote_files_info;    
+  extended_remote_ops.to_insert_breakpoint =   remote_insert_breakpoint;
+  extended_remote_ops.to_remove_breakpoint =   remote_remove_breakpoint;
+  extended_remote_ops.to_terminal_init  =   NULL;              
+  extended_remote_ops.to_terminal_inferior =   NULL;           
+  extended_remote_ops.to_terminal_ours_for_output =   NULL;
+  extended_remote_ops.to_terminal_ours  =   NULL;      
+  extended_remote_ops.to_terminal_info  =   NULL;      
+  extended_remote_ops.to_kill  =   remote_kill;                
+  extended_remote_ops.to_load  =   generic_load;       
+  extended_remote_ops.to_lookup_symbol =   NULL;       
+  extended_remote_ops.to_create_inferior =   extended_remote_create_inferior;
+  extended_remote_ops.to_mourn_inferior =   extended_remote_mourn;
+  extended_remote_ops.to_can_run  =   0;                       
+  extended_remote_ops.to_notice_signals =   0;                 
+  extended_remote_ops.to_thread_alive  =   remote_thread_alive;
+  extended_remote_ops.to_stop  =   0;                  
+  extended_remote_ops.to_stratum =   process_stratum;
+  extended_remote_ops.DONT_USE =   NULL;       
+  extended_remote_ops.to_has_all_memory =   1; 
+  extended_remote_ops.to_has_memory =   1;     
+  extended_remote_ops.to_has_stack =   1;      
+  extended_remote_ops.to_has_registers =   1;  
+  extended_remote_ops.to_has_execution =   1;  
+  extended_remote_ops.to_sections =   NULL;    
+  extended_remote_ops.to_sections_end =   NULL;
+  extended_remote_ops.to_magic =   OPS_MAGIC ;
+} 
+
 
 /* This was 5 seconds, which is a long time to sit and wait.
    Unless this is going though some terminal server or multiplexer or
@@ -332,7 +428,8 @@ static int remote_write_size = PBUFSIZ;
 
 /* This is the size (in chars) of the first response to the `g' command.  This
    is used to limit the size of the memory read and write commands to prevent
-   stub buffers from overflowing.  */
+   stub buffers from overflowing.  The size does not include headers and
+   trailers, it is only the payload size. */
 
 static int remote_register_buf_size = 0;
 
@@ -434,8 +531,8 @@ remote_close (quitting)
 static void
 get_offsets ()
 {
-  char buf[PBUFSIZ];
-  int nvals;
+  char buf[PBUFSIZ], *ptr;
+  int lose;
   CORE_ADDR text_addr, data_addr, bss_addr;
   struct section_offsets *offs;
 
@@ -452,9 +549,43 @@ get_offsets ()
       return;
     }
 
-  nvals = sscanf (buf, "Text=%lx;Data=%lx;Bss=%lx", &text_addr, &data_addr,
-                 &bss_addr);
-  if (nvals != 3)
+  /* Pick up each field in turn.  This used to be done with scanf, but
+     scanf will make trouble if CORE_ADDR size doesn't match
+     conversion directives correctly.  The following code will work
+     with any size of CORE_ADDR.  */
+  text_addr = data_addr = bss_addr = 0;
+  ptr = buf;
+  lose = 0;
+
+  if (strncmp (ptr, "Text=", 5) == 0)
+    {
+      ptr += 5;
+      /* Don't use strtol, could lose on big values.  */
+      while (*ptr && *ptr != ';')
+       text_addr = (text_addr << 4) + fromhex (*ptr++);
+    }
+  else
+    lose = 1;
+
+  if (!lose && strncmp (ptr, ";Data=", 6) == 0)
+    {
+      ptr += 6;
+      while (*ptr && *ptr != ';')
+       data_addr = (data_addr << 4) + fromhex (*ptr++);
+    }
+  else
+    lose = 1;
+
+  if (!lose && strncmp (ptr, ";Bss=", 5) == 0)
+    {
+      ptr += 5;
+      while (*ptr && *ptr != ';')
+       bss_addr = (bss_addr << 4) + fromhex (*ptr++);
+    }
+  else
+    lose = 1;
+
+  if (lose)
     error ("Malformed response to offset query, %s", buf);
 
   if (symfile_objfile == NULL)
@@ -511,7 +642,7 @@ remote_open (name, from_tty)
      char *name;
      int from_tty;
 {
-  remote_open_1 (name, from_tty, &remote_ops);
+  remote_open_1 (name, from_tty, &remote_ops, 0);
 }
 
 /* Open a connection to a remote debugger using the extended
@@ -522,25 +653,18 @@ extended_remote_open (name, from_tty)
      char *name;
      int from_tty;
 {
-  char buf[PBUFSIZ];
-
-  /* Do the basic remote open stuff.  */
-  remote_open_1 (name, from_tty, &extended_remote_ops);
-
-  /* Now tell the remote that we're using the extended protocol.  */
-  putpkt ("!");
-  getpkt (buf, 0);
-
+  remote_open_1 (name, from_tty, &extended_remote_ops, 1/*extended_p*/);
 }
 
 /* Generic code for opening a connection to a remote target.  */
 static DCACHE *remote_dcache;
 
 static void
-remote_open_1 (name, from_tty, target)
+remote_open_1 (name, from_tty, target, extended_p)
      char *name;
      int from_tty;
      struct target_ops *target;
+     int extended_p;
 {
   if (name == 0)
     error ("To open a remote debug connection, you need to specify what serial\n\
@@ -602,6 +726,15 @@ device is attached to the remote system (e.g. /dev/ttya).");
   if (!catch_errors (remote_start_remote, (char *)0, 
                     "Couldn't establish connection to remote target\n", RETURN_MASK_ALL))
     pop_target();
+
+
+  if (extended_p)
+    {
+      /* tell the remote that we're using the extended protocol.  */
+      char buf[PBUFSIZ];
+      putpkt ("!");
+      getpkt (buf, 0);
+    }
 }
 
 /* This takes a program previously attached to and detaches it.  After
@@ -630,7 +763,7 @@ remote_detach (args, from_tty)
 
 /* Convert hex digit A to a number.  */
 
-static int
+int
 fromhex (a)
      int a;
 {
@@ -638,6 +771,8 @@ fromhex (a)
     return a - '0';
   else if (a >= 'a' && a <= 'f')
     return a - 'a' + 10;
+  else if (a >= 'A' && a <= 'F')
+    return a - 'A' + 10;
   else 
     error ("Reply contains invalid hex digit %d", a);
 }
@@ -748,6 +883,25 @@ Give up (and stop debugging it)? "))
 /* If nonzero, ignore the next kill.  */
 int kill_kludge;
 
+void
+remote_console_output (msg)
+     char *msg;
+{
+  char *p;
+
+  for (p = msg; *p; p +=2) 
+    {
+      char tb[2];
+      char c = fromhex (p[0]) * 16 + fromhex (p[1]);
+      tb[0] = c;
+      tb[1] = 0;
+      if (target_output_hook)
+       target_output_hook (tb);
+      else 
+       fputs_filtered (tb, gdb_stdout);
+    }
+}
+
 /* Wait until the remote machine stops, then return,
    storing status in STATUS just as `wait' would.
    Returns "pid" (though it's not clear what, if anything, that
@@ -794,7 +948,6 @@ remote_wait (pid, status)
                n... = register number
                r... = register contents
                */
-
            p = &buf[3];        /* after Txx */
 
            while (*p)
@@ -868,17 +1021,7 @@ Packet: '%s'\n",
 
          goto got_status;
        case 'O':               /* Console output */
-         for (p = buf + 1; *p; p +=2) 
-           {
-             char tb[2];
-             char c = fromhex (p[0]) * 16 + fromhex (p[1]);
-             tb[0] = c;
-             tb[1] = 0;
-             if (target_output_hook)
-               target_output_hook (tb);
-             else 
-               fputs_filtered (tb, gdb_stdout);
-           }
+         remote_console_output (buf + 1);
          continue;
        case '\0':
          if (last_sent_signal != TARGET_SIGNAL_0)
@@ -1106,7 +1249,7 @@ hexnumlen (num)
   for (i = 0; num != 0; i++)
     num >>= 4;
 
-  return min (i, 1);
+  return max (i, 1);
 }
 
 /* Write memory data directly to the remote machine.
@@ -1129,12 +1272,11 @@ remote_write_bytes (memaddr, myaddr, len)
   /* Chop the transfer down if necessary */
 
   max_buf_size = min (remote_write_size, PBUFSIZ);
-  max_buf_size = min (max_buf_size, remote_register_buf_size);
+  if (remote_register_buf_size != 0)
+    max_buf_size = min (max_buf_size, remote_register_buf_size);
 
-#define PACKET_OVERHEAD (1 + 1 + 1 + 2)        /* $x#xx  - Overhead for all types of packets */
-
-  /* packet overhead + <memaddr>,<len>:  */
-  max_buf_size -= PACKET_OVERHEAD + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 1;
+  /* Subtract header overhead from max payload size -  $M<memaddr>,<len>:#nn */
+  max_buf_size -= 2 + hexnumlen (memaddr + len - 1) + 1 + hexnumlen (len) + 4;
 
   origlen = len;
   while (len > 0)
@@ -1200,10 +1342,8 @@ remote_read_bytes (memaddr, myaddr, len)
   /* Chop the transfer down if necessary */
 
   max_buf_size = min (remote_write_size, PBUFSIZ);
-  max_buf_size = min (max_buf_size, remote_register_buf_size);
-
-  /* packet overhead */
-  max_buf_size -= PACKET_OVERHEAD;
+  if (remote_register_buf_size != 0)
+    max_buf_size = min (max_buf_size, remote_register_buf_size);
 
   origlen = len;
   while (len > 0)
@@ -1264,6 +1404,16 @@ remote_xfer_memory(memaddr, myaddr, len, should_write, target)
      int should_write;
      struct target_ops *target;                        /* ignored */
 {
+#ifdef REMOTE_TRANSLATE_XFER_ADDRESS
+  CORE_ADDR targaddr;
+  int targlen;
+  REMOTE_TRANSLATE_XFER_ADDRESS (memaddr, len, targaddr, targlen);
+  if (targlen == 0)
+    return 0;
+  memaddr = targaddr;
+  len = targlen;
+#endif
+
   return dcache_xfer_memory (remote_dcache, memaddr, myaddr, len, should_write);
 }
 
@@ -1393,7 +1543,7 @@ remote_send (buf)
 /* Send a packet to the remote machine, with error checking.
    The data of the packet is in BUF.  */
 
-static int
+int
 putpkt (buf)
      char *buf;
 {
@@ -1601,7 +1751,7 @@ read_frame (buf)
    If FOREVER, wait forever rather than timing out; this is used
    while the target is executing user code.  */
 
-static void
+void
 getpkt (buf, forever)
      char *buf;
      int forever;
@@ -1664,7 +1814,7 @@ getpkt (buf, forever)
       if (val == 1)
        {
          if (remote_debug)
-           fprintf_unfiltered (gdb_stderr, "Packet received: %s\n", buf);
+           fprintf_unfiltered (gdb_stdout, "Packet received: %s\n", buf);
          SERIAL_WRITE (remote_desc, "+", 1);
          return;
        }
@@ -1834,100 +1984,6 @@ remote_remove_breakpoint (addr, contents_cache)
   return memory_remove_breakpoint (addr, contents_cache);
 #endif /* REMOTE_BREAKPOINT */
 }
-\f
-/* Define the target subroutine names */
-
-static struct target_ops remote_ops =
-{
-  "remote",                    /* to_shortname */
-  "Remote serial target in gdb-specific protocol",     /* to_longname */
-  "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
-Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
-  remote_open,                 /* to_open */
-  remote_close,                        /* to_close */
-  NULL,                                /* to_attach */
-  remote_detach,               /* to_detach */
-  remote_resume,               /* to_resume */
-  remote_wait,                 /* to_wait */
-  remote_fetch_registers,      /* to_fetch_registers */
-  remote_store_registers,      /* to_store_registers */
-  remote_prepare_to_store,     /* to_prepare_to_store */
-  remote_xfer_memory,          /* to_xfer_memory */
-  remote_files_info,           /* to_files_info */
-  remote_insert_breakpoint,    /* to_insert_breakpoint */
-  remote_remove_breakpoint,    /* to_remove_breakpoint */
-  NULL,                                /* to_terminal_init */
-  NULL,                                /* to_terminal_inferior */
-  NULL,                                /* to_terminal_ours_for_output */
-  NULL,                                /* to_terminal_ours */
-  NULL,                                /* to_terminal_info */
-  remote_kill,                 /* to_kill */
-  generic_load,                        /* to_load */
-  NULL,                                /* to_lookup_symbol */
-  NULL,                                /* to_create_inferior */
-  remote_mourn,                        /* to_mourn_inferior */
-  0,                           /* to_can_run */
-  0,                           /* to_notice_signals */
-  remote_thread_alive,         /* to_thread_alive */
-  0,                           /* to_stop */
-  process_stratum,             /* to_stratum */
-  NULL,                                /* to_next */
-  1,                           /* to_has_all_memory */
-  1,                           /* to_has_memory */
-  1,                           /* to_has_stack */
-  1,                           /* to_has_registers */
-  1,                           /* to_has_execution */
-  NULL,                                /* sections */
-  NULL,                                /* sections_end */
-  OPS_MAGIC                    /* to_magic */
-};
-
-static struct target_ops extended_remote_ops =
-{
-  "extended-remote",                   /* to_shortname */
-  "Extended remote serial target in gdb-specific protocol",/* to_longname */
-  "Use a remote computer via a serial line, using a gdb-specific protocol.\n\
-Specify the serial device it is connected to (e.g. /dev/ttya).",  /* to_doc */
-  extended_remote_open,                        /* to_open */
-  remote_close,                        /* to_close */
-  NULL,                                /* to_attach */
-  remote_detach,               /* to_detach */
-  remote_resume,               /* to_resume */
-  remote_wait,                 /* to_wait */
-  remote_fetch_registers,      /* to_fetch_registers */
-  remote_store_registers,      /* to_store_registers */
-  remote_prepare_to_store,     /* to_prepare_to_store */
-  remote_xfer_memory,          /* to_xfer_memory */
-  remote_files_info,           /* to_files_info */
-
-  remote_insert_breakpoint,    /* to_insert_breakpoint */
-  remote_remove_breakpoint,    /* to_remove_breakpoint */
-
-  NULL,                                /* to_terminal_init */
-  NULL,                                /* to_terminal_inferior */
-  NULL,                                /* to_terminal_ours_for_output */
-  NULL,                                /* to_terminal_ours */
-  NULL,                                /* to_terminal_info */
-  remote_kill,                 /* to_kill */
-  generic_load,                        /* to_load */
-  NULL,                                /* to_lookup_symbol */
-  extended_remote_create_inferior,/* to_create_inferior */
-  extended_remote_mourn,       /* to_mourn_inferior */
-  0,                           /* to_can_run */
-  0,                           /* to_notice_signals */
-  remote_thread_alive,         /* to_thread_alive */
-  0,                           /* to_stop */
-  process_stratum,             /* to_stratum */
-  NULL,                                /* to_next */
-  1,                           /* to_has_all_memory */
-  1,                           /* to_has_memory */
-  1,                           /* to_has_stack */
-  1,                           /* to_has_registers */
-  1,                           /* to_has_execution */
-  NULL,                                /* sections */
-  NULL,                                /* sections_end */
-  OPS_MAGIC                    /* to_magic */
-};
 
 /* Some targets are only capable of doing downloads, and afterwards they switch
    to the remote serial protocol.  This function provides a clean way to get
@@ -1949,9 +2005,27 @@ push_remote_target (name, from_tty)
   remote_open (name, from_tty);
 }
 
+/* Other targets want to use the entire remote serial module but with
+   certain remote_ops overridden. */
+
+void
+open_remote_target (name, from_tty, target, extended_p)
+     char *name;
+     int from_tty;
+     struct target_ops *target;
+     int extended_p;
+{
+  printf_filtered ("Selecting the %sremote protocol\n",
+                  (extended_p ? "extended-" : ""));
+  remote_open_1 (name, from_tty, target, extended_p);
+}
+
+
 void
 _initialize_remote ()
 {
+  init_remote_ops() ;
+  init_extended__remote_ops() ;
   add_target (&remote_ops);
   add_target (&extended_remote_ops);
 
This page took 0.029501 seconds and 4 git commands to generate.