2005-04-26 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / remote.c
index 3fde452b2b8fb2d13bc29fffed08cac5d742a675..7ad9ec26052d90022702593d7b6121314f0a7d72 100644 (file)
@@ -1,7 +1,7 @@
 /* Remote target communications for serial-line targets in custom GDB protocol
 
    Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
-   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
+   1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
    Free Software Foundation, Inc.
 
    This file is part of GDB.
@@ -41,6 +41,7 @@
 #include "regcache.h"
 #include "value.h"
 #include "gdb_assert.h"
+#include "observer.h"
 
 #include <ctype.h>
 #include <sys/time.h>
@@ -412,9 +413,9 @@ get_memory_packet_size (struct memory_packet_config *config)
 #ifndef MAX_REMOTE_PACKET_SIZE
 #define MAX_REMOTE_PACKET_SIZE 16384
 #endif
-  /* NOTE: 16 is just chosen at random.  */
+  /* NOTE: 20 ensures we can write at least one byte.  */
 #ifndef MIN_REMOTE_PACKET_SIZE
-#define MIN_REMOTE_PACKET_SIZE 16
+#define MIN_REMOTE_PACKET_SIZE 20
 #endif
   long what_they_get;
   if (config->fixed_p)
@@ -639,13 +640,10 @@ add_packet_config_cmd (struct packet_config *config,
                       struct cmd_list_element **show_remote_list,
                       int legacy)
 {
-  struct cmd_list_element *set_cmd;
-  struct cmd_list_element *show_cmd;
   char *set_doc;
   char *show_doc;
-  char *help_doc;
-  char *print;
   char *cmd_name;
+
   config->name = name;
   config->title = title;
   config->detect = AUTO_BOOLEAN_AUTO;
@@ -654,8 +652,6 @@ add_packet_config_cmd (struct packet_config *config,
                        name, title);
   show_doc = xstrprintf ("Show current use of remote protocol `%s' (%s) packet",
                         name, title);
-  print = xstrprintf ("Current use of remote protocol `%s' (%s) is %%s",
-                     name, title);
   /* set/show TITLE-packet {auto,on,off} */
   cmd_name = xstrprintf ("%s-packet", title);
   add_setshow_auto_boolean_cmd (cmd_name, class_obscure,
@@ -753,7 +749,6 @@ show_remote_protocol_vcont_packet_cmd (struct ui_file *file, int from_tty,
                                       struct cmd_list_element *c,
                                       const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_vcont);
 }
 
@@ -772,7 +767,6 @@ show_remote_protocol_qSymbol_packet_cmd (struct ui_file *file, int from_tty,
                                         struct cmd_list_element *c,
                                         const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_qSymbol);
 }
 
@@ -792,7 +786,6 @@ show_remote_protocol_P_packet_cmd (struct ui_file *file, int from_tty,
                                   struct cmd_list_element *c,
                                   const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_P);
 }
 
@@ -825,7 +818,6 @@ show_remote_protocol_Z_software_bp_packet_cmd (struct ui_file *file, int from_tt
                                               struct cmd_list_element *c,
                                               const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_SOFTWARE_BP]);
 }
 
@@ -841,7 +833,6 @@ show_remote_protocol_Z_hardware_bp_packet_cmd (struct ui_file *file, int from_tt
                                               struct cmd_list_element *c,
                                               const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_HARDWARE_BP]);
 }
 
@@ -857,7 +848,6 @@ show_remote_protocol_Z_write_wp_packet_cmd (struct ui_file *file, int from_tty,
                                            struct cmd_list_element *c,
                                            const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_WRITE_WP]);
 }
 
@@ -873,7 +863,6 @@ show_remote_protocol_Z_read_wp_packet_cmd (struct ui_file *file, int from_tty,
                                           struct cmd_list_element *c,
                                           const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_READ_WP]);
 }
 
@@ -889,7 +878,6 @@ show_remote_protocol_Z_access_wp_packet_cmd (struct ui_file *file, int from_tty,
                                             struct cmd_list_element *c,
                                             const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_Z[Z_PACKET_ACCESS_WP]);
 }
 
@@ -958,7 +946,6 @@ show_remote_protocol_binary_download_cmd (struct ui_file *file, int from_tty,
                                          struct cmd_list_element *c,
                                          const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_binary_download);
 }
 
@@ -977,10 +964,27 @@ show_remote_protocol_qPart_auxv_packet_cmd (struct ui_file *file, int from_tty,
                                            struct cmd_list_element *c,
                                            const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_qPart_auxv);
 }
 
+/* Should we try the 'qGetTLSAddr' (Get Thread Local Storage Address) request? */
+static struct packet_config remote_protocol_qGetTLSAddr;
+
+static void
+set_remote_protocol_qGetTLSAddr_packet_cmd (char *args, int from_tty,
+                                 struct cmd_list_element *c)
+{
+  update_packet_config (&remote_protocol_qGetTLSAddr);
+}
+
+static void
+show_remote_protocol_qGetTLSAddr_packet_cmd (struct ui_file *file, int from_tty,
+                                            struct cmd_list_element *c,
+                                            const char *value)
+{
+  show_packet_config_cmd (&remote_protocol_qGetTLSAddr);
+}
+
 static struct packet_config remote_protocol_p;
 
 static void
@@ -995,7 +999,6 @@ show_remote_protocol_p_packet_cmd (struct ui_file *file, int from_tty,
                                   struct cmd_list_element *c,
                                   const char *value)
 {
-  deprecated_show_value_hack (file, from_tty, c, value);
   show_packet_config_cmd (&remote_protocol_p);
 }
 
@@ -2110,6 +2113,7 @@ init_all_packet_configs (void)
      downloading.  */
   update_packet_config (&remote_protocol_binary_download);
   update_packet_config (&remote_protocol_qPart_auxv);
+  update_packet_config (&remote_protocol_qGetTLSAddr);
 }
 
 /* Symbol look-up.  */
@@ -2175,7 +2179,7 @@ static void
 remote_open_1 (char *name, int from_tty, struct target_ops *target,
               int extended_p, int async_p)
 {
-  struct exception ex;
+  struct gdb_exception ex;
   struct remote_state *rs = get_remote_state ();
   if (name == 0)
     error (_("To open a remote debug connection, you need to specify what\n"
@@ -2311,6 +2315,8 @@ remote_open_1 (char *name, int from_tty, struct target_ops *target,
       remote_check_symbols (symfile_objfile);
     }
 #endif
+
+  observer_notify_inferior_created (&current_target, from_tty);
 }
 
 /* This takes a program previously attached to and detaches it.  After
@@ -2345,9 +2351,6 @@ remote_detach (char *args, int from_tty)
 static void
 remote_disconnect (char *args, int from_tty)
 {
-  struct remote_state *rs = get_remote_state ();
-  char *buf = alloca (rs->remote_packet_size);
-
   if (args)
     error (_("Argument given to \"detach\" when remotely debugging."));
 
@@ -2563,7 +2566,6 @@ remote_resume (ptid_t ptid, int step, enum target_signal siggnal)
   struct remote_state *rs = get_remote_state ();
   char *buf = alloca (rs->remote_packet_size);
   int pid = PIDGET (ptid);
-  char *p;
 
   last_sent_signal = siggnal;
   last_sent_step = step;
@@ -2864,7 +2866,6 @@ remote_wait (ptid_t ptid, struct target_waitstatus *status)
          continue;
        case 'T':               /* Status with PC, SP, FP, ...  */
          {
-           int i;
            char regs[MAX_REGISTER_SIZE];
 
            /* Expedited reply, containing Signal, {regno, reg} repeat.  */
@@ -3060,7 +3061,6 @@ remote_async_wait (ptid_t ptid, struct target_waitstatus *status)
          continue;
        case 'T':               /* Status with PC, SP, FP, ...  */
          {
-           int i;
            char regs[MAX_REGISTER_SIZE];
 
            /* Expedited reply, containing Signal, {regno, reg} repeat.  */
@@ -3329,6 +3329,7 @@ remote_fetch_registers (int regnum)
      in the buffer is not a hex character, assume that has happened
      and try to fetch another packet to read.  */
   while ((buf[0] < '0' || buf[0] > '9')
+        && (buf[0] < 'A' || buf[0] > 'F')
         && (buf[0] < 'a' || buf[0] > 'f')
         && buf[0] != 'x')      /* New: unavailable register value.  */
     {
@@ -3437,7 +3438,6 @@ store_register_using_P (int regnum)
   char *buf = alloca (rs->remote_packet_size);
   char regp[MAX_REGISTER_SIZE];
   char *p;
-  int i;
 
   sprintf (buf, "P%s=", phex_nz (reg->pnum, 0));
   p = buf + strlen (buf);
@@ -3458,7 +3458,6 @@ remote_store_registers (int regnum)
   struct remote_state *rs = get_remote_state ();
   char *buf;
   char *regs;
-  int i;
   char *p;
 
   set_thread (PIDGET (inferior_ptid), 1);
@@ -3497,7 +3496,7 @@ remote_store_registers (int regnum)
   {
     int i;
     regs = alloca (rs->sizeof_g_packet);
-    memset (regs, rs->sizeof_g_packet, 0);
+    memset (regs, 0, rs->sizeof_g_packet);
     for (i = 0; i < NUM_REGS + NUM_PSEUDO_REGS; i++)
       {
        struct packet_reg *r = &rs->regs[i];
@@ -3656,16 +3655,18 @@ remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
   /* Verify that the target can support a binary download.  */
   check_binary_download (memaddr);
 
+  payload_size = get_memory_write_packet_size ();
+  
   /* Compute the size, and then allocate space for the largest
-     possible packet.  Include space for an extra trailing NULL.  */
-  sizeof_buf = get_memory_write_packet_size () + 1;
+     possible packet.  Include space for an extra trailing NUL.  */
+  sizeof_buf = payload_size + 1;
   buf = alloca (sizeof_buf);
 
   /* Compute the size of the actual payload by subtracting out the
-     packet header and footer overhead: "$M<memaddr>,<len>:...#nn".  */
-  payload_size = (get_memory_write_packet_size () - (strlen ("$M,:#NN")
-                                                    + hexnumlen (memaddr)
-                                                    + hexnumlen (len)));
+     packet header and footer overhead: "$M<memaddr>,<len>:...#nn".
+     */
+  payload_size -= strlen ("$M,:#NN");
+  payload_size -= hexnumlen (memaddr);
 
   /* Construct the packet header: "[MX]<memaddr>,<len>:".   */
 
@@ -3678,11 +3679,15 @@ remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
       *p++ = 'X';
       /* Best guess at number of bytes that will fit.  */
       todo = min (len, payload_size);
+      payload_size -= hexnumlen (todo);
+      todo = min (todo, payload_size);
       break;
     case PACKET_DISABLE:
       *p++ = 'M';
       /* Num bytes that will fit.  */
       todo = min (len, payload_size / 2);
+      payload_size -= hexnumlen (todo);
+      todo = min (todo, payload_size / 2);
       break;
     case PACKET_SUPPORT_UNKNOWN:
       internal_error (__FILE__, __LINE__,
@@ -3690,6 +3695,9 @@ remote_write_bytes (CORE_ADDR memaddr, char *myaddr, int len)
     default:
       internal_error (__FILE__, __LINE__, _("bad switch"));
     }
+  if (todo <= 0)
+    internal_error (__FILE__, __LINE__,
+                   _("minumum packet size too small to write data"));
 
   /* Append "<memaddr>".  */
   memaddr = remote_address_masked (memaddr);
@@ -5082,7 +5090,6 @@ remote_rcmd (char *command,
             struct ui_file *outbuf)
 {
   struct remote_state *rs = get_remote_state ();
-  int i;
   char *buf = alloca (rs->remote_packet_size);
   char *p = buf;
 
@@ -5309,12 +5316,73 @@ Fetch and print the remote list of thread identifiers, one pkt only"));
 static char *
 remote_pid_to_str (ptid_t ptid)
 {
-  static char buf[30];
+  static char buf[32];
+  int size;
 
-  sprintf (buf, "Thread %d", PIDGET (ptid));
+  size = snprintf (buf, sizeof buf, "thread %d", ptid_get_pid (ptid));
+  gdb_assert (size < sizeof buf);
   return buf;
 }
 
+/* Get the address of the thread local variable in OBJFILE which is
+   stored at OFFSET within the thread local storage for thread PTID.  */
+
+static CORE_ADDR
+remote_get_thread_local_address (ptid_t ptid, CORE_ADDR lm, CORE_ADDR offset)
+{
+  if (remote_protocol_qGetTLSAddr.support != PACKET_DISABLE)
+    {
+      struct remote_state *rs = get_remote_state ();
+      char *buf = alloca (rs->remote_packet_size);
+      char *p = buf;
+      enum packet_result result;
+
+      strcpy (p, "qGetTLSAddr:");
+      p += strlen (p);
+      p += hexnumstr (p, PIDGET (ptid));
+      *p++ = ',';
+      p += hexnumstr (p, offset);
+      *p++ = ',';
+      p += hexnumstr (p, lm);
+      *p++ = '\0';
+
+      putpkt (buf);
+      getpkt (buf, rs->remote_packet_size, 0);
+      result = packet_ok (buf, &remote_protocol_qGetTLSAddr);
+      if (result == PACKET_OK)
+       {
+         ULONGEST result;
+
+         unpack_varlen_hex (buf, &result);
+         return result;
+       }
+      else if (result == PACKET_UNKNOWN)
+       {
+         struct gdb_exception e
+           = { RETURN_ERROR, TLS_GENERIC_ERROR,
+               "Remote target doesn't support qGetTLSAddr packet" };
+         throw_exception (e);
+       }
+      else
+       {
+         struct gdb_exception e
+           = { RETURN_ERROR, TLS_GENERIC_ERROR,
+               "Remote target failed to process qGetTLSAddr request" };
+         throw_exception (e);
+
+       }
+    }
+  else
+    {
+      struct gdb_exception e
+       = { RETURN_ERROR, TLS_GENERIC_ERROR,
+           "TLS not supported or disabled on this target" };
+      throw_exception (e);
+    }
+  /* Not reached.  */
+  return 0;
+}
+
 static void
 init_remote_ops (void)
 {
@@ -5354,6 +5422,7 @@ Specify the serial device it is connected to\n\
   remote_ops.to_stop = remote_stop;
   remote_ops.to_xfer_partial = remote_xfer_partial;
   remote_ops.to_rcmd = remote_rcmd;
+  remote_ops.to_get_thread_local_address = remote_get_thread_local_address;
   remote_ops.to_stratum = process_stratum;
   remote_ops.to_has_all_memory = 1;
   remote_ops.to_has_memory = 1;
@@ -5529,6 +5598,7 @@ show_remote_cmd (char *args, int from_tty)
   show_remote_protocol_vcont_packet_cmd (gdb_stdout, from_tty, NULL, NULL);
   show_remote_protocol_binary_download_cmd (gdb_stdout, from_tty, NULL, NULL);
   show_remote_protocol_qPart_auxv_packet_cmd (gdb_stdout, from_tty, NULL, NULL);
+  show_remote_protocol_qGetTLSAddr_packet_cmd (gdb_stdout, from_tty, NULL, NULL);
 }
 
 static void
@@ -5559,7 +5629,6 @@ _initialize_remote (void)
 {
   static struct cmd_list_element *remote_set_cmdlist;
   static struct cmd_list_element *remote_show_cmdlist;
-  struct cmd_list_element *tmpcmd;
 
   /* architecture specific data */
   remote_gdbarch_data_handle = 
@@ -5674,13 +5743,13 @@ Specify a negative limit for unlimited."),
                            NULL, NULL, /* FIXME: i18n: The maximum number of target hardware breakpoints is %s.  */
                            &remote_set_cmdlist, &remote_show_cmdlist);
 
-  deprecated_add_show_from_set
-    (add_set_cmd ("remoteaddresssize", class_obscure,
-                 var_integer, (char *) &remote_address_size,
-                 "Set the maximum size of the address (in bits) \
-in a memory packet.\n",
-                 &setlist),
-     &showlist);
+  add_setshow_integer_cmd ("remoteaddresssize", class_obscure,
+                          &remote_address_size, _("\
+Set the maximum size of the address (in bits) in a memory packet."), _("\
+Show the maximum size of the address (in bits) in a memory packet."), NULL,
+                          NULL,
+                          NULL, /* FIXME: i18n: */
+                          &setlist, &showlist);
 
   add_packet_config_cmd (&remote_protocol_binary_download,
                         "X", "binary-download",
@@ -5759,6 +5828,13 @@ in a memory packet.\n",
                         &remote_set_cmdlist, &remote_show_cmdlist,
                         0);
 
+  add_packet_config_cmd (&remote_protocol_qGetTLSAddr,
+                        "qGetTLSAddr", "get-thread-local-storage-address",
+                        set_remote_protocol_qGetTLSAddr_packet_cmd,
+                        show_remote_protocol_qGetTLSAddr_packet_cmd,
+                        &remote_set_cmdlist, &remote_show_cmdlist,
+                        0);
+
   /* Keep the old ``set remote Z-packet ...'' working.  */
   add_setshow_auto_boolean_cmd ("Z-packet", class_obscure,
                                &remote_Z_packet_detect, _("\
This page took 0.039307 seconds and 4 git commands to generate.