import gdb-1999-10-18 snapshot
[deliverable/binutils-gdb.git] / gdb / remote.c
index 5c8ca61f9d4273e1caa53e112d2f3eefa95b18d8..24d382e86eb4f669edc2b1995fd1f04d65d43e6e 100644 (file)
@@ -36,6 +36,7 @@
 #include "objfiles.h"
 #include "gdb-stabs.h"
 #include "gdbthread.h"
+#include "remote.h"
 
 #include "dcache.h"
 
 #endif
 
 #include "event-loop.h"
+#include "event-top.h"
+#include "inf-loop.h"
 
 #include <signal.h>
 #include "serial.h"
 
 /* Prototypes for local functions */
-static void initialize_sigint_signal_handler PARAMS ((void));
+static void cleanup_sigint_signal_handler (void *dummy);
+static void initialize_sigint_signal_handler (void);
+
 static void handle_remote_sigint PARAMS ((int));
 static void handle_remote_sigint_twice PARAMS ((int));
 static void async_remote_interrupt PARAMS ((gdb_client_data));
-static void async_remote_interrupt_twice PARAMS ((gdb_client_data));
-
-static void set_extended_protocol PARAMS ((struct continuation_arg *));
+void async_remote_interrupt_twice PARAMS ((gdb_client_data));
 
 static void build_remote_gdbarch_data PARAMS ((void));
 
-static int remote_write_bytes PARAMS ((CORE_ADDR memaddr,
-                                      char *myaddr, int len));
+static int remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len);
 
 static int remote_read_bytes PARAMS ((CORE_ADDR memaddr,
                                      char *myaddr, int len));
@@ -81,7 +83,6 @@ static void remote_resume PARAMS ((int pid, int step,
                                   enum target_signal siggnal));
 static void remote_async_resume PARAMS ((int pid, int step,
                                         enum target_signal siggnal));
-
 static int remote_start_remote PARAMS ((PTR));
 
 static void remote_open PARAMS ((char *name, int from_tty));
@@ -164,6 +165,8 @@ static int remote_query PARAMS ((int /*char */ , char *, char *, int *));
 
 static int hexnumstr PARAMS ((char *, ULONGEST));
 
+static int hexnumnstr PARAMS ((char *, ULONGEST, int));
+
 static CORE_ADDR remote_address_masked PARAMS ((CORE_ADDR));
 
 static void print_packet PARAMS ((char *));
@@ -186,19 +189,11 @@ static void record_currthread PARAMS ((int currthread));
 
 extern int fromhex PARAMS ((int a));
 
-extern void getpkt PARAMS ((char *buf, int forever));
-
-extern int putpkt PARAMS ((char *buf));
-
 static int putpkt_binary PARAMS ((char *buf, int cnt));
 
-void remote_console_output PARAMS ((char *));
-
 static void check_binary_download PARAMS ((CORE_ADDR addr));
 
-#if __STDC__
 struct packet_config;
-#endif
 
 static void show_packet_config_cmd PARAMS ((struct packet_config * config));
 
@@ -259,6 +254,16 @@ static struct target_ops extended_async_remote_ops;
    was static int remote_timeout = 2; */
 extern int remote_timeout;
 
+/* FIXME: cagney/1999-09-23: Even though getpkt was called with
+   ``forever'' still use the normal timeout mechanism.  This is
+   currently used by the ASYNC code to guarentee that target reads
+   during the initial connect always time-out.  Once getpkt has been
+   modified to return a timeout indication and, in turn
+   remote_wait()/wait_for_inferior() have gained a timeout parameter
+   this can go away. */
+static int wait_forever_enabled_p = 1;
+
+
 /* This variable chooses whether to send a ^C or a break when the user
    requests program interruption.  Although ^C is usually what remote
    systems expect, and that is the default here, sometimes a break is
@@ -317,6 +322,11 @@ static int remote_address_size;
 
 static int remote_register_buf_size = 0;
 
+/* Tempoary to track who currently owns the terminal.  See
+   target_async_terminal_* for more details.  */
+
+static int remote_async_terminal_ours_p;
+
 /* Generic configuration support for packets the stub optionally
    supports. Allows the user to specify the use of the packet as well
    as allowing GDB to auto-detect support in the remote stub. */
@@ -1501,13 +1511,8 @@ get_offsets ()
   if (symfile_objfile == NULL)
     return;
 
-  offs = alloca (sizeof (struct section_offsets)
-                + symfile_objfile->num_sections
-                * sizeof (offs->offsets));
-  memcpy (offs, symfile_objfile->section_offsets,
-         sizeof (struct section_offsets)
-         + symfile_objfile->num_sections
-         * sizeof (offs->offsets));
+  offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
+  memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
 
   ANOFFSET (offs, SECT_OFF_TEXT) = text_addr;
 
@@ -1594,8 +1599,10 @@ remote_cisco_section_offsets (text_addr, data_addr, bss_addr,
       sprintf_vma (tmp + strlen (tmp), bss_addr);
       fprintf_filtered (gdb_stdlog, tmp);
       fprintf_filtered (gdb_stdlog,
-                       "Reloc offset: text = 0x%x data = 0x%x bss = 0x%x\n",
-                   (long) *text_offs, (long) *data_offs, (long) *bss_offs);
+                       "Reloc offset: text = 0x%s data = 0x%s bss = 0x%s\n",
+                       paddr_nz (*text_offs),
+                       paddr_nz (*data_offs),
+                       paddr_nz (*bss_offs));
     }
 
   return 0;
@@ -1621,15 +1628,8 @@ remote_cisco_objfile_relocate (text_off, data_off, bss_off)
          broken for xcoff, dwarf, sdb-coff, etc.  But there is no
          simple canonical representation for this stuff.  */
 
-      offs = ((struct section_offsets *)
-             alloca (sizeof (struct section_offsets)
-                     + (symfile_objfile->num_sections
-                        * sizeof (offs->offsets))));
-
-      memcpy (offs, symfile_objfile->section_offsets,
-             (sizeof (struct section_offsets)
-              + (symfile_objfile->num_sections
-                 * sizeof (offs->offsets))));
+      offs = (struct section_offsets *) alloca (SIZEOF_SECTION_OFFSETS);
+      memcpy (offs, symfile_objfile->section_offsets, SIZEOF_SECTION_OFFSETS);
 
       ANOFFSET (offs, SECT_OFF_TEXT) = text_off;
       ANOFFSET (offs, SECT_OFF_DATA) = data_off;
@@ -1733,6 +1733,9 @@ remote_open_1 (name, from_tty, target, extended_p)
     error ("To open a remote debug connection, you need to specify what\n\
 serial device is attached to the remote system (e.g. /dev/ttya).");
 
+  /* See FIXME above */
+  wait_forever_enabled_p = 1;
+
   target_preopen (from_tty);
 
   unpush_target (target);
@@ -1848,13 +1851,6 @@ serial device is attached to the remote system (e.g. /dev/ttya).");
       puts_filtered ("\n");
     }
 
-  /* If running in asynchronous mode, register the target with the
-     event loop.  Set things up so that when there is an event on the
-     file descriptor, the event loop will call fetch_inferior_event,
-     which will do the proper analysis to determine what happened. */
-  if (async_p)
-    add_file_handler (remote_desc->fd, fetch_inferior_event, 0);
-
   push_target (target);                /* Switch to using remote target now */
 
   init_packet_config (&remote_protocol_P);
@@ -1867,20 +1863,26 @@ serial device is attached to the remote system (e.g. /dev/ttya).");
      binary downloading. */
   init_packet_config (&remote_protocol_binary_download);
 
-  /* If running asynchronously, set things up for telling the target
-     to use the extended protocol. This will happen only after the
-     target has been connected to, in fetch_inferior_event. */
-  if (extended_p && async_p)
-    add_continuation (set_extended_protocol, NULL);
-
   /* Without this, some commands which require an active target (such
      as kill) won't work.  This variable serves (at least) double duty
      as both the pid of the target process (if it has such), and as a
      flag indicating that a target is active.  These functions should
      be split out into seperate variables, especially since GDB will
      someday have a notion of debugging several processes.  */
-
   inferior_pid = MAGIC_NULL_PID;
+
+  /* With this target we start out by owning the terminal. */
+  remote_async_terminal_ours_p = 1;
+
+  /* FIXME: cagney/1999-09-23: During the initial connection it is
+     assumed that the target is already ready and able to respond to
+     requests. Unfortunatly remote_start_remote() eventually calls
+     wait_for_inferior() with no timeout.  wait_forever_enabled_p gets
+     around this. Eventually a mechanism that allows
+     wait_for_inferior() to expect/get timeouts will be
+     implemented. */
+  wait_forever_enabled_p = 0;
+
   /* Start the remote connection; if error (0), discard this target.
      In particular, if the user quits, be sure to discard it
      (we'd be in an inconsistent state otherwise).  */
@@ -1888,37 +1890,22 @@ serial device is attached to the remote system (e.g. /dev/ttya).");
                     "Couldn't establish connection to remote target\n",
                     RETURN_MASK_ALL))
     {
-      /* Unregister the file descriptor from the event loop. */
-      if (async_p)
-       delete_file_handler (remote_desc->fd);
       pop_target ();
+      wait_forever_enabled_p = 1;
       return;
     }
 
-  if (!async_p)
+  wait_forever_enabled_p = 1;
+
+  if (extended_p)
     {
-      if (extended_p)
-       {
-         /* tell the remote that we're using the extended protocol.  */
-         char *buf = alloca (PBUFSIZ);
-         putpkt ("!");
-         getpkt (buf, 0);
-       }
+      /* tell the remote that we're using the extended protocol.  */
+      char *buf = alloca (PBUFSIZ);
+      putpkt ("!");
+      getpkt (buf, 0);
     }
 }
 
-/* This will be called by fetch_inferior_event, via the
-   cmd_continuation pointer, only after the target has stopped. */
-static void
-set_extended_protocol (arg)
-     struct continuation_arg *arg;
-{
-  /* tell the remote that we're using the extended protocol.  */
-  char *buf = alloca (PBUFSIZ);
-  putpkt ("!");
-  getpkt (buf, 0);
-}
-
 /* This takes a program previously attached to and detaches it.  After
    this is done, GDB can be used to debug some other program.  We
    better not have left any breakpoints in the target program or it'll
@@ -1960,8 +1947,8 @@ remote_async_detach (args, from_tty)
   remote_send (buf);
 
   /* Unregister the file descriptor from the event loop. */
-  if (async_p)
-    delete_file_handler (remote_desc->fd);
+  if (SERIAL_IS_ASYNC_P (remote_desc))
+    SERIAL_ASYNC (remote_desc, NULL, 0);
 
   pop_target ();
   if (from_tty)
@@ -2060,26 +2047,6 @@ remote_async_resume (pid, step, siggnal)
   if (target_resume_hook)
     (*target_resume_hook) ();
 
-  /* Set things up before execution starts for async commands. */
-  /* This function can be entered more than once for the same execution
-     command, because it is also called by handle_inferior_event. So
-     we make sure that we don't do the initialization for sync
-     execution more than once. */
-  if (async_p && !target_executing)
-    {
-      target_executing = 1;
-
-      /* If the command must look synchronous, fake it, by making gdb
-         display an empty prompt after the command has completed. Also
-         disable input. */
-      if (sync_execution)
-       {
-         push_prompt ("", "", "");
-         delete_file_handler (input_fd);
-         initialize_sigint_signal_handler ();
-       }
-    }
-
   if (siggnal != TARGET_SIGNAL_0)
     {
       buf[0] = step ? 'S' : 'C';
@@ -2090,6 +2057,22 @@ remote_async_resume (pid, step, siggnal)
   else
     strcpy (buf, step ? "s" : "c");
 
+  /* We are about to start executing the inferior, let's register it
+     with the event loop. NOTE: this is the one place where all the
+     execution commands end up. We could alternatively do this in each
+     of the execution commands in infcmd.c.*/
+  /* FIXME: ezannoni 1999-09-28: We may need to move this out of here
+     into infcmd.c in order to allow inferior function calls to work
+     NOT asynchronously. */
+  if (event_loop_p && SERIAL_CAN_ASYNC_P (remote_desc))
+    target_async (inferior_event_handler, 0);
+  /* Tell the world that the target is now executing. */
+  /* FIXME: cagney/1999-09-23: Is it the targets responsibility to set
+     this?  Instead, should the client of target just assume (for
+     async targets) that the target is going to start executing?  Is
+     this information already found in the continuation block?  */
+  if (SERIAL_IS_ASYNC_P (remote_desc))
+    target_executing = 1;
   putpkt (buf);
 }
 \f
@@ -2124,11 +2107,11 @@ handle_remote_sigint_twice (sig)
 {
   signal (sig, handle_sigint);
   sigint_remote_twice_token =
-    create_async_signal_handler (async_remote_interrupt, NULL);
+    create_async_signal_handler (inferior_event_handler_wrapper, NULL);
   mark_async_signal_handler_wrapper (sigint_remote_twice_token);
 }
 
-/* Perform the real interruption of hte target execution, in response
+/* Perform the real interruption of the target execution, in response
    to a ^C. */
 static void
 async_remote_interrupt (arg)
@@ -2142,24 +2125,31 @@ async_remote_interrupt (arg)
 
 /* Perform interrupt, if the first attempt did not succeed. Just give
    up on the target alltogether. */
-static void
+void
 async_remote_interrupt_twice (arg)
      gdb_client_data arg;
 {
-  interrupt_query ();
-  signal (SIGINT, handle_remote_sigint);
+  if (remote_debug)
+    fprintf_unfiltered (gdb_stdlog, "remote_interrupt_twice called\n");
+  /* Do something only if the target was not killed by the previous
+     cntl-C. */
+  if (target_executing)
+    {
+      interrupt_query ();
+      signal (SIGINT, handle_remote_sigint);
+    }
 }
 
 /* Reinstall the usual SIGINT handlers, after the target has
    stopped. */
-void
-cleanup_sigint_signal_handler ()
+static void
+cleanup_sigint_signal_handler (void *dummy)
 {
   signal (SIGINT, handle_sigint);
   if (sigint_remote_twice_token)
-    delete_async_signal_handler ((async_signal_handler **) & sigint_remote_twice_token);
+    delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_twice_token);
   if (sigint_remote_token)
-    delete_async_signal_handler ((async_signal_handler **) & sigint_remote_token);
+    delete_async_signal_handler ((struct async_signal_handler **) & sigint_remote_token);
 }
 
 /* Send ^C to target to halt it.  Target will respond, and send us a
@@ -2228,13 +2218,57 @@ Give up (and stop debugging it)? "))
   target_terminal_inferior ();
 }
 
+/* Enable/disable target terminal ownership.  Most targets can use
+   terminal groups to control terminal ownership.  Remote targets are
+   different in that explicit transfer of ownership to/from GDB/target
+   is required. */
+
+static void
+remote_async_terminal_inferior (void)
+{
+  /* FIXME: cagney/1999-09-27: Shouldn't need to test for
+     sync_execution here.  This function should only be called when
+     GDB is resuming the inferior in the forground.  A background
+     resume (``run&'') should leave GDB in control of the terminal and
+     consequently should not call this code. */
+  if (!sync_execution)
+    return;
+  /* FIXME: cagney/1999-09-27: Closely related to the above.  Make
+     calls target_terminal_*() idenpotent. The event-loop GDB talking
+     to an asynchronous target with a synchronous command calls this
+     function from both event-top.c and infrun.c/infcmd.c.  Once GDB
+     stops trying to transfer the terminal to the target when it
+     shouldn't this guard can go away.  */
+  if (!remote_async_terminal_ours_p)
+    return;
+  delete_file_handler (input_fd);
+  remote_async_terminal_ours_p = 0;
+  initialize_sigint_signal_handler ();
+  /* NOTE: At this point we could also register our selves as the
+     recipient of all input.  Any characters typed could then be
+     passed on down to the target. */
+}
+
+static void
+remote_async_terminal_ours (void)
+{
+  /* See FIXME in remote_async_terminal_inferior. */
+  if (!sync_execution)
+    return;
+  /* See FIXME in remote_async_terminal_inferior. */
+  if (remote_async_terminal_ours_p)
+    return;
+  cleanup_sigint_signal_handler (NULL);
+  add_file_handler (input_fd, stdin_event_handler, 0);
+  remote_async_terminal_ours_p = 1;
+}
+
 /* If nonzero, ignore the next kill.  */
 
 int kill_kludge;
 
 void
-remote_console_output (msg)
-     char *msg;
+remote_console_output (char *msg)
 {
   char *p;
 
@@ -2246,6 +2280,7 @@ remote_console_output (msg)
       tb[1] = 0;
       fputs_unfiltered (tb, gdb_stdtarg);
     }
+  gdb_flush (gdb_stdtarg);
 }
 
 /* Wait until the remote machine stops, then return,
@@ -2391,7 +2426,7 @@ Packet: '%s'\n",
 
            if (symfile_objfile == NULL)
              {
-               warning ("Relocation packet recieved with no symbol file.  \
+               warning ("Relocation packet received with no symbol file.  \
 Packet Dropped");
                goto got_status;
              }
@@ -2485,10 +2520,14 @@ remote_async_wait (pid, status)
     {
       unsigned char *p;
 
-      if (!async_p)
+      if (!SERIAL_IS_ASYNC_P (remote_desc))
        ofunc = signal (SIGINT, remote_interrupt);
-      getpkt ((char *) buf, 1);
-      if (!async_p)
+      /* FIXME: cagney/1999-09-27: If we're in async mode we should
+         _never_ wait for ever -> test on target_is_async_p().
+         However, before we do that we need to ensure that the caller
+         knows how to take the target into/out of async mode. */
+      getpkt ((char *) buf, wait_forever_enabled_p);
+      if (!SERIAL_IS_ASYNC_P (remote_desc))
        signal (SIGINT, ofunc);
 
       /* This is a hook for when we need to do something (perhaps the
@@ -2926,25 +2965,37 @@ hexnumlen (num)
   return max (i, 1);
 }
 
-/* Set BUF to the hex digits representing NUM.  */
+/* Set BUF to the minimum number of hex digits representing NUM.  */
 
 static int
 hexnumstr (buf, num)
      char *buf;
      ULONGEST num;
 {
-  int i;
   int len = hexnumlen (num);
+  return hexnumnstr (buf, num, len);
+}
+
 
-  buf[len] = '\0';
+/* Set BUF to the hex digits representing NUM, padded to WIDTH characters.  */
 
-  for (i = len - 1; i >= 0; i--)
+static int
+hexnumnstr (buf, num, width)
+     char *buf;
+     ULONGEST num;
+     int width;
+{
+  int i;
+
+  buf[width] = '\0';
+
+  for (i = width - 1; i >= 0; i--)
     {
       buf[i] = "0123456789abcdef"[(num & 0xf)];
       num >>= 4;
     }
 
-  return len;
+  return width;
 }
 
 /* Mask all but the least significant REMOTE_ADDRESS_SIZE bits. */
@@ -3026,139 +3077,134 @@ check_binary_download (addr)
    MYADDR is the address of the buffer in our space.
    LEN is the number of bytes.
 
-   Returns number of bytes transferred, or 0 for error.  */
+   Returns number of bytes transferred, or 0 (setting errno) for
+   error.  Only transfer a single packet. */
 
 static int
-remote_write_bytes (memaddr, myaddr, len)
-     CORE_ADDR memaddr;
-     char *myaddr;
-     int len;
+remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
 {
-  unsigned char *buf = alloca (PBUFSIZ);
+  unsigned char *buf;
   int max_buf_size;            /* Max size of packet output buffer */
-  int origlen;
+  unsigned char *p;
+  unsigned char *plen;
+  int plenlen;
+  int todo;
+  int nr_bytes;
 
   /* Verify that the target can support a binary download */
   check_binary_download (memaddr);
 
-  /* Chop the transfer down if necessary */
-
+  /* Determine the max packet size. */
   max_buf_size = min (remote_write_size, PBUFSIZ);
   if (remote_register_buf_size != 0)
     max_buf_size = min (max_buf_size, remote_register_buf_size);
+  buf = alloca (max_buf_size + 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)
-    {
-      unsigned char *p, *plen;
-      int todo;
-      int i;
+  /* construct "M"<memaddr>","<len>":" */
+  /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
+  p = buf;
 
-      /* construct "M"<memaddr>","<len>":" */
-      /* sprintf (buf, "M%lx,%x:", (unsigned long) memaddr, todo); */
-      memaddr = remote_address_masked (memaddr);
-      p = buf;
-      switch (remote_protocol_binary_download.support)
+  /* Append [XM].  Compute a best guess of the number of bytes
+     actually transfered. */
+  switch (remote_protocol_binary_download.support)
+    {
+    case PACKET_ENABLE:
+      *p++ = 'X';
+      /* Best guess at number of bytes that will fit. */
+      todo = min (len, max_buf_size);
+      break;
+    case PACKET_DISABLE:
+      *p++ = 'M';
+      /* num bytes that will fit */
+      todo = min (len, max_buf_size / 2);
+      break;
+    case PACKET_SUPPORT_UNKNOWN:
+      internal_error ("remote_write_bytes: bad switch");
+    }
+  
+  /* Append <memaddr> */
+  memaddr = remote_address_masked (memaddr);
+  p += hexnumstr (p, (ULONGEST) memaddr);
+  *p++ = ',';
+  
+  /* Append <len>.  Retain the location/size of <len>.  It may
+     need to be adjusted once the packet body has been created. */
+  plen = p;
+  plenlen = hexnumstr (p, (ULONGEST) todo);
+  p += plenlen;
+  *p++ = ':';
+  *p = '\0';
+  
+  /* Append the packet body. */
+  switch (remote_protocol_binary_download.support)
+    {
+    case PACKET_ENABLE:
+      /* Binary mode.  Send target system values byte by byte, in
+        increasing byte addresses.  Only escape certain critical
+        characters.  */
+      for (nr_bytes = 0;
+          (nr_bytes < todo) && (p - buf) < (max_buf_size - 2);
+          nr_bytes++)
        {
-       case PACKET_ENABLE:
-         *p++ = 'X';
-         todo = min (len, max_buf_size);
-         break;
-       case PACKET_DISABLE:
-         *p++ = 'M';
-         todo = min (len, max_buf_size / 2);   /* num bytes that will fit */
-         break;
-       case PACKET_SUPPORT_UNKNOWN:
-         internal_error ("remote_write_bytes: bad switch");
+         switch (myaddr[nr_bytes] & 0xff)
+           {
+           case '$':
+           case '#':
+           case 0x7d:
+             /* These must be escaped */
+             *p++ = 0x7d;
+             *p++ = (myaddr[nr_bytes] & 0xff) ^ 0x20;
+             break;
+           default:
+             *p++ = myaddr[nr_bytes] & 0xff;
+             break;
+           }
        }
-
-      p += hexnumstr (p, (ULONGEST) memaddr);
-      *p++ = ',';
-
-      plen = p;                        /* remember where len field goes */
-      p += hexnumstr (p, (ULONGEST) todo);
-      *p++ = ':';
-      *p = '\0';
-
-      /* We send target system values byte by byte, in increasing byte
-         addresses, each byte encoded as two hex characters (or one
-         binary character).  */
-      switch (remote_protocol_binary_download.support)
+      if (nr_bytes < todo)
        {
-       case PACKET_ENABLE:
-         {
-           int escaped = 0;
-           for (i = 0;
-                (i < todo) && (i + escaped) < (max_buf_size - 2);
-                i++)
-             {
-               switch (myaddr[i] & 0xff)
-                 {
-                 case '$':
-                 case '#':
-                 case 0x7d:
-                   /* These must be escaped */
-                   escaped++;
-                   *p++ = 0x7d;
-                   *p++ = (myaddr[i] & 0xff) ^ 0x20;
-                   break;
-                 default:
-                   *p++ = myaddr[i] & 0xff;
-                   break;
-                 }
-             }
-           
-           if (i < todo)
-             {
-               /* Escape chars have filled up the buffer prematurely, 
-                  and we have actually sent fewer bytes than planned.
-                  Fix-up the length field of the packet.  */
-               
-               /* FIXME: will fail if new len is a shorter string than 
-                  old len.  */
-               
-               plen += hexnumstr (plen, (ULONGEST) i);
-               *plen++ = ':';
-             }
-           break;
-         }
-       case PACKET_DISABLE:
-         {
-           for (i = 0; i < todo; i++)
-             {
-               *p++ = tohex ((myaddr[i] >> 4) & 0xf);
-               *p++ = tohex (myaddr[i] & 0xf);
-             }
-           *p = '\0';
-           break;
-         }
-       case PACKET_SUPPORT_UNKNOWN:
-         internal_error ("remote_write_bytes: bad switch");
+         /* Escape chars have filled up the buffer prematurely, 
+            and we have actually sent fewer bytes than planned.
+            Fix-up the length field of the packet.  Use the same
+            number of characters as before.  */
+         
+         plen += hexnumnstr (plen, (ULONGEST) nr_bytes, plenlen);
+         *plen = ':';  /* overwrite \0 from hexnumnstr() */
        }
-
-      putpkt_binary (buf, (int) (p - buf));
-      getpkt (buf, 0);
-
-      if (buf[0] == 'E')
+      break;
+    case PACKET_DISABLE:
+      /* Normal mode: Send target system values byte by byte, in
+        increasing byte addresses.  Each byte is encoded as a two hex
+        value.  */
+      for (nr_bytes = 0; nr_bytes < todo; nr_bytes++)
        {
-         /* There is no correspondance between what the remote protocol uses
-            for errors and errno codes.  We would like a cleaner way of
-            representing errors (big enough to include errno codes, bfd_error
-            codes, and others).  But for now just return EIO.  */
-         errno = EIO;
-         return 0;
+         *p++ = tohex ((myaddr[nr_bytes] >> 4) & 0xf);
+         *p++ = tohex (myaddr[nr_bytes] & 0xf);
        }
-
-      /* Increment by i, not by todo, in case escape chars 
-         caused us to send fewer bytes than we'd planned.  */
-      myaddr += i;
-      memaddr += i;
-      len -= i;
+      *p = '\0';
+      break;
+    case PACKET_SUPPORT_UNKNOWN:
+      internal_error ("remote_write_bytes: bad switch");
     }
-  return origlen;
+  
+  putpkt_binary (buf, (int) (p - buf));
+  getpkt (buf, 0);
+  
+  if (buf[0] == 'E')
+    {
+      /* There is no correspondance between what the remote protocol
+        uses for errors and errno codes.  We would like a cleaner way
+        of representing errors (big enough to include errno codes,
+        bfd_error codes, and others).  But for now just return EIO.  */
+      errno = EIO;
+      return 0;
+    }
+  
+  /* Return NR_BYTES, not TODO, in case escape chars caused us to send fewer
+     bytes than we'd planned.  */
+  return nr_bytes;
 }
 
 /* Read memory data directly from the remote machine.
@@ -3169,6 +3215,13 @@ remote_write_bytes (memaddr, myaddr, len)
 
    Returns number of bytes transferred, or 0 for error.  */
 
+/* NOTE: cagney/1999-10-18: This function (and its siblings in other
+   remote targets) shouldn't attempt to read the entire buffer.
+   Instead it should read a single packet worth of data and then
+   return the byte size of that packet to the caller.  The caller (its
+   caller and its callers caller ;-) already contains code for
+   handling partial reads. */
+
 static int
 remote_read_bytes (memaddr, myaddr, len)
      CORE_ADDR memaddr;
@@ -3361,17 +3414,22 @@ readchar (timeout)
 
   ch = SERIAL_READCHAR (remote_desc, timeout);
 
-  switch (ch)
+  if (ch >= 0)
+    return (ch & 0x7f);
+
+  switch ((enum serial_rc) ch)
     {
     case SERIAL_EOF:
+      target_mourn_inferior ();
       error ("Remote connection closed");
+      /* no return */
     case SERIAL_ERROR:
       perror_with_name ("Remote communication error");
+      /* no return */
     case SERIAL_TIMEOUT:
-      return ch;
-    default:
-      return ch & 0x7f;
+      break;
     }
+  return ch;
 }
 
 /* Send the command in BUF to the remote machine, and read the reply
@@ -3455,7 +3513,7 @@ putpkt_binary (buf, cnt)
          *p = '\0';
          fprintf_unfiltered (gdb_stdlog, "Sending packet: ");
          fputstrn_unfiltered (buf2, p - buf2, 0, gdb_stdlog);
-         fprintf_unfiltered (gdb_stdlog, "...", buf2);
+         fprintf_unfiltered (gdb_stdlog, "...");
          gdb_flush (gdb_stdlog);
        }
       if (SERIAL_WRITE (remote_desc, buf2, p - buf2))
@@ -3531,33 +3589,6 @@ putpkt_binary (buf, cnt)
 
 static int remote_cisco_mode;
 
-static void
-remote_cisco_expand (src, dest)
-     char *src;
-     char *dest;
-{
-  int i;
-  int repeat;
-
-  do
-    {
-      if (*src == '*')
-       {
-         repeat = (fromhex (src[1]) << 4) + fromhex (src[2]);
-         for (i = 0; i < repeat; i++)
-           {
-             *dest++ = *(src - 1);
-           }
-         src += 2;
-       }
-      else
-       {
-         *dest++ = *src;
-       }
-    }
-  while (*src++);
-}
-
 /* Come here after finding the start of the frame.  Collect the rest
    into BUF, verifying the checksum, length, and handling run-length
    compression.  Returns 0 on any error, 1 on success.  */
@@ -3598,16 +3629,7 @@ read_frame (buf)
            pktcsum |= fromhex (readchar (remote_timeout));
 
            if (csum == pktcsum)
-             {
-               if (remote_cisco_mode)  /* variant run-length-encoding */
-                 {
-                   char *tmp_buf = alloca (PBUFSIZ);
-
-                   remote_cisco_expand (buf, tmp_buf);
-                   strcpy (buf, tmp_buf);
-                 }
-               return 1;
-             }
+              return 1;
 
            if (remote_debug)
              {
@@ -3620,27 +3642,43 @@ read_frame (buf)
            return 0;
          }
        case '*':               /* Run length encoding */
-         if (remote_cisco_mode == 0)   /* variant run-length-encoding */
-           {
-             csum += c;
-             c = readchar (remote_timeout);
-             csum += c;
-             c = c - ' ' + 3;  /* Compute repeat count */
+          {
+           int repeat;
+           csum += c;
 
-             if (c > 0 && c < 255 && bp + c - 1 < buf + PBUFSIZ - 1)
-               {
-                 memset (bp, *(bp - 1), c);
-                 bp += c;
-                 continue;
-               }
+           if (remote_cisco_mode == 0)
+             {
+               c = readchar (remote_timeout);
+               csum += c;
+               repeat = c - ' ' + 3;   /* Compute repeat count */
+             }
+           else 
+             { 
+               /* Cisco's run-length encoding variant uses two 
+                  hex chars to represent the repeat count. */
+
+               c = readchar (remote_timeout);
+               csum += c;
+               repeat  = fromhex (c) << 4;
+               c = readchar (remote_timeout);
+               csum += c;
+               repeat += fromhex (c);
+             }
 
-             *bp = '\0';
-             printf_filtered ("Repeat count %d too large for buffer: ", c);
-             puts_filtered (buf);
-             puts_filtered ("\n");
-             return 0;
-           }
-         /* else fall thru to treat like default */
+           if (repeat > 0 && repeat <= 255 
+                && bp + repeat - 1 < buf + PBUFSIZ - 1)
+             {
+               memset (bp, *(bp - 1), repeat);
+               bp += c;
+               continue;
+             }
+
+           *bp = '\0';
+           printf_filtered ("Repeat count %d too large for buffer: ", repeat);
+           puts_filtered (buf);
+           puts_filtered ("\n");
+           return 0;
+         }
        default:
          if (bp < buf + PBUFSIZ - 1)
            {
@@ -3702,8 +3740,9 @@ getpkt (buf, forever)
 
          if (c == SERIAL_TIMEOUT)
            {
-             if (forever)      /* Watchdog went off.  Kill the target. */
+             if (forever)      /* Watchdog went off?  Kill the target. */
                {
+                 QUIT;
                  target_mourn_inferior ();
                  error ("Watchdog has expired.  Target detached.\n");
                }
@@ -3767,8 +3806,8 @@ static void
 remote_async_kill ()
 {
   /* Unregister the file descriptor from the event loop. */
-  if (async_p)
-    delete_file_handler (remote_desc->fd);
+  if (SERIAL_IS_ASYNC_P (remote_desc))
+    SERIAL_ASYNC (remote_desc, NULL, 0);
 
   /* For some mysterious reason, wait_for_inferior calls kill instead of
      mourn after it gets TARGET_WAITKIND_SIGNALLED.  Work around it.  */
@@ -3866,8 +3905,8 @@ extended_remote_async_create_inferior (exec_file, args, env)
 
   /* If running asynchronously, register the target file descriptor
      with the event loop. */
-  if (async_p)
-    add_file_handler (remote_desc->fd, fetch_inferior_event, 0);
+  if (event_loop_p && target_can_async_p ())
+    target_async (inferior_event_handler, 0);
 
   /* Now restart the remote server.  */
   extended_remote_restart ();
@@ -3996,7 +4035,6 @@ remote_remove_breakpoint (addr, contents_cache)
      CORE_ADDR addr;
      char *contents_cache;
 {
-  char buf[PBUFSIZ];
   int bp_size;
 
   if ((remote_protocol_Z.support == PACKET_ENABLE)
@@ -4042,7 +4080,7 @@ remote_insert_watchpoint (addr, len, type)
   p = strchr (buf, '\0');
   addr = remote_address_masked (addr);
   p += hexnumstr (p, (ULONGEST) addr);
-  sprintf (p, ",%lx", len);
+  sprintf (p, ",%x", len);
   
   putpkt (buf);
   getpkt (buf, 0);
@@ -4065,7 +4103,7 @@ remote_remove_watchpoint (addr, len, type)
   p = strchr (buf, '\0');
   addr = remote_address_masked (addr);
   p += hexnumstr (p, (ULONGEST) addr);
-  sprintf (p, ",%lx", len);
+  sprintf (p, ",%x", len);
   putpkt (buf);
   getpkt (buf, 0);
 
@@ -4260,8 +4298,8 @@ compare_sections_command (args, from_tty)
       for (target_crc = 0, tmp = &buf[1]; *tmp; tmp++)
        target_crc = target_crc * 16 + fromhex (*tmp);
 
-      printf_filtered ("Section %s, range 0x%08x -- 0x%08x: ",
-                      sectname, lma, lma + size);
+      printf_filtered ("Section %s, range 0x%s -- 0x%s: ",
+                      sectname, paddr (lma), paddr (lma + size));
       if (host_crc == target_crc)
        printf_filtered ("matched.\n");
       else
@@ -4289,7 +4327,6 @@ remote_query (query_type, buf, outbuf, bufsiz)
   int i;
   char *buf2 = alloca (PBUFSIZ);
   char *p2 = &buf2[0];
-  char *p = buf;
 
   if (!bufsiz)
     error ("null pointer to remote bufer size specified");
@@ -4709,6 +4746,9 @@ remote_cisco_open (name, from_tty)
            "To open a remote debug connection, you need to specify what \n\
 device is attached to the remote system (e.g. host:port).");
 
+  /* See FIXME above */
+  wait_forever_enabled_p = 1;
+
   target_preopen (from_tty);
 
   unpush_target (&remote_cisco_ops);
@@ -4879,7 +4919,6 @@ readsocket ()
 static int
 readtty ()
 {
-  int status;
   int tty_bytecount;
 
   /* First, read a buffer full from the terminal */
@@ -4954,7 +4993,7 @@ minitelnet ()
 
       FD_ZERO (&input);
       FD_SET (fileno (stdin), &input);
-      FD_SET (remote_desc->fd, &input);
+      FD_SET (DEPRECATED_SERIAL_FD (remote_desc), &input);
 
       status = select (tablesize, &input, 0, 0, 0);
       if ((status == -1) && (errno != EINTR))
@@ -5047,6 +5086,49 @@ Specify the serial device it is connected to (e.g. host:2020).";
   remote_cisco_ops.to_magic = OPS_MAGIC;
 }
 
+static int
+remote_can_async_p (void)
+{
+  /* We're async whenever the serial device is. */
+  return SERIAL_CAN_ASYNC_P (remote_desc);
+}
+
+static int
+remote_is_async_p (void)
+{
+  /* We're async whenever the serial device is. */
+  return SERIAL_IS_ASYNC_P (remote_desc);
+}
+
+/* Pass the SERIAL event on and up to the client.  One day this code
+   will be able to delay notifying the client of an event until the
+   point where an entire packet has been received. */
+
+static void (*async_client_callback) (enum inferior_event_type event_type, void *context);
+static void *async_client_context;
+static serial_event_ftype remote_async_serial_handler;
+
+static void
+remote_async_serial_handler (serial_t scb, void *context)
+{
+  /* Don't propogate error information up to the client.  Instead let
+     the client find out about the error by querying the target.  */
+  async_client_callback (INF_REG_EVENT, async_client_context);
+}
+
+static void
+remote_async (void (*callback) (enum inferior_event_type event_type, void *context), void *context)
+{
+  if (callback != NULL)
+    {
+      SERIAL_ASYNC (remote_desc, remote_async_serial_handler, NULL);
+      async_client_callback = callback;
+      async_client_context = context;
+    }
+  else
+    SERIAL_ASYNC (remote_desc, NULL, NULL);
+}
+
 /* Target async and target extended-async.
 
    This are temporary targets, until it is all tested.  Eventually
@@ -5073,6 +5155,8 @@ Specify the serial device it is connected to (e.g. /dev/ttya).";
   remote_async_ops.to_files_info = remote_files_info;
   remote_async_ops.to_insert_breakpoint = remote_insert_breakpoint;
   remote_async_ops.to_remove_breakpoint = remote_remove_breakpoint;
+  remote_async_ops.to_terminal_inferior = remote_async_terminal_inferior;
+  remote_async_ops.to_terminal_ours = remote_async_terminal_ours;
   remote_async_ops.to_kill = remote_async_kill;
   remote_async_ops.to_load = generic_load;
   remote_async_ops.to_mourn_inferior = remote_async_mourn;
@@ -5088,7 +5172,9 @@ Specify the serial device it is connected to (e.g. /dev/ttya).";
   remote_async_ops.to_has_registers = 1;
   remote_async_ops.to_has_execution = 1;
   remote_async_ops.to_has_thread_control = tc_schedlock;       /* can lock scheduler */
-  remote_async_ops.to_has_async_exec = 1;
+  remote_async_ops.to_can_async_p = remote_can_async_p;
+  remote_async_ops.to_is_async_p = remote_is_async_p;
+  remote_async_ops.to_async = remote_async;
   remote_async_ops.to_magic = OPS_MAGIC;
 }
 
@@ -5164,13 +5250,13 @@ _initialize_remote ()
 Remote protocol specific variables\n\
 Configure various remote-protocol specific variables such as\n\
 the packets being used",
-                 &remote_set_cmdlist, "remote ",
+                 &remote_set_cmdlist, "set remote ",
                  0/*allow-unknown*/, &setlist);
   add_prefix_cmd ("remote", class_maintenance, set_remote_cmd, "\
 Remote protocol specific variables\n\
 Configure various remote-protocol specific variables such as\n\
 the packets being used",
-                 &remote_show_cmdlist, "remote ",
+                 &remote_show_cmdlist, "show remote ",
                  0/*allow-unknown*/, &showlist);
 
   add_cmd ("compare-sections", class_obscure, compare_sections_command,
This page took 0.037057 seconds and 4 git commands to generate.