2010-02-03 H.J. Lu <hongjiu.lu@intel.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / regcache.c
index c8ea2adeb32acb7be4dba9ce9254438b4087f658..d6cdc46d42a5a0152b79fd80b50c336829a4c2d6 100644 (file)
@@ -1,12 +1,12 @@
 /* Register support routines for the remote server for GDB.
 /* Register support routines for the remote server for GDB.
-   Copyright (C) 2001, 2002, 2004, 2005
+   Copyright (C) 2001, 2002, 2004, 2005, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -15,9 +15,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "server.h"
 #include "regdef.h"
 
 #include "server.h"
 #include "regdef.h"
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdlib.h>
 #include <string.h>
 
-/* The private data for the register cache.  Note that we have one
-   per inferior; this is primarily for simplicity, as the performance
-   benefit is minimal.  */
-
-struct inferior_regcache_data
-{
-  int registers_valid;
-  unsigned char *registers;
-};
-
 static int register_bytes;
 
 static struct reg *reg_defs;
 static int register_bytes;
 
 static struct reg *reg_defs;
@@ -42,20 +30,23 @@ static int num_registers;
 
 const char **gdbserver_expedite_regs;
 
 
 const char **gdbserver_expedite_regs;
 
-static struct inferior_regcache_data *
-get_regcache (struct thread_info *inf, int fetch)
+struct regcache *
+get_thread_regcache (struct thread_info *thread, int fetch)
 {
 {
-  struct inferior_regcache_data *regcache;
+  struct regcache *regcache;
 
 
-  regcache = (struct inferior_regcache_data *) inferior_regcache_data (inf);
+  regcache = (struct regcache *) inferior_regcache_data (thread);
 
   if (regcache == NULL)
     fatal ("no register cache");
 
 
   if (regcache == NULL)
     fatal ("no register cache");
 
-  /* FIXME - fetch registers for INF */
   if (fetch && regcache->registers_valid == 0)
     {
   if (fetch && regcache->registers_valid == 0)
     {
-      fetch_inferior_registers (0);
+      struct thread_info *saved_inferior = current_inferior;
+
+      current_inferior = thread;
+      fetch_inferior_registers (regcache, -1);
+      current_inferior = saved_inferior;
       regcache->registers_valid = 1;
     }
 
       regcache->registers_valid = 1;
     }
 
@@ -66,16 +57,16 @@ void
 regcache_invalidate_one (struct inferior_list_entry *entry)
 {
   struct thread_info *thread = (struct thread_info *) entry;
 regcache_invalidate_one (struct inferior_list_entry *entry)
 {
   struct thread_info *thread = (struct thread_info *) entry;
-  struct inferior_regcache_data *regcache;
+  struct regcache *regcache;
 
 
-  regcache = (struct inferior_regcache_data *) inferior_regcache_data (thread);
+  regcache = (struct regcache *) inferior_regcache_data (thread);
 
   if (regcache->registers_valid)
     {
       struct thread_info *saved_inferior = current_inferior;
 
       current_inferior = thread;
 
   if (regcache->registers_valid)
     {
       struct thread_info *saved_inferior = current_inferior;
 
       current_inferior = thread;
-      store_inferior_registers (-1);
+      store_inferior_registers (regcache, -1);
       current_inferior = saved_inferior;
     }
 
       current_inferior = saved_inferior;
     }
 
@@ -83,30 +74,26 @@ regcache_invalidate_one (struct inferior_list_entry *entry)
 }
 
 void
 }
 
 void
-regcache_invalidate ()
+regcache_invalidate (void)
 {
   for_each_inferior (&all_threads, regcache_invalidate_one);
 }
 
 {
   for_each_inferior (&all_threads, regcache_invalidate_one);
 }
 
-int
-registers_length (void)
-{
-  return 2 * register_bytes;
-}
-
-void *
+struct regcache *
 new_register_cache (void)
 {
 new_register_cache (void)
 {
-  struct inferior_regcache_data *regcache;
+  struct regcache *regcache;
 
 
-  regcache = malloc (sizeof (*regcache));
+  if (register_bytes == 0)
+    return NULL; /* The architecture hasn't been initialized yet.  */
 
 
-  /* Make sure to zero-initialize the register cache when it is created,
-     in case there are registers the target never fetches.  This way they'll
-     read as zero instead of garbage.  */
-  regcache->registers = calloc (1, register_bytes);
-  if (regcache->registers == NULL)
-    fatal ("Could not allocate register cache.");
+  regcache = xmalloc (sizeof (*regcache));
+
+  /* Make sure to zero-initialize the register cache when it is
+     created, in case there are registers the target never
+     fetches.  This way they'll read as zero instead of
+     garbage.  */
+  regcache->registers = xcalloc (1, register_bytes);
 
   regcache->registers_valid = 0;
 
 
   regcache->registers_valid = 0;
 
@@ -114,20 +101,31 @@ new_register_cache (void)
 }
 
 void
 }
 
 void
-free_register_cache (void *regcache_p)
+free_register_cache (struct regcache *regcache)
+{
+  if (regcache)
+    {
+      free (regcache->registers);
+      free (regcache);
+    }
+}
+
+static void
+realloc_register_cache (struct inferior_list_entry *thread_p)
 {
 {
-  struct inferior_regcache_data *regcache
-    = (struct inferior_regcache_data *) regcache_p;
+  struct thread_info *thread = (struct thread_info *) thread_p;
+  struct regcache *regcache
+    = (struct regcache *) inferior_regcache_data (thread);
 
 
-  free (regcache->registers);
-  free (regcache);
+  free_register_cache (regcache);
+  set_inferior_regcache_data (thread, new_register_cache ());
 }
 
 void
 set_register_cache (struct reg *regs, int n)
 {
   int offset, i;
 }
 
 void
 set_register_cache (struct reg *regs, int n)
 {
   int offset, i;
-  
+
   reg_defs = regs;
   num_registers = n;
 
   reg_defs = regs;
   num_registers = n;
 
@@ -139,25 +137,33 @@ set_register_cache (struct reg *regs, int n)
     }
 
   register_bytes = offset / 8;
     }
 
   register_bytes = offset / 8;
+
+  /* Make sure PBUFSIZ is large enough to hold a full register packet.  */
+  if (2 * register_bytes + 32 > PBUFSIZ)
+    fatal ("Register packet size exceeds PBUFSIZ.");
+
+  /* Re-allocate all pre-existing register caches.  */
+  for_each_inferior (&all_threads, realloc_register_cache);
 }
 
 void
 }
 
 void
-registers_to_string (char *buf)
+registers_to_string (struct regcache *regcache, char *buf)
 {
 {
-  unsigned char *registers = get_regcache (current_inferior, 1)->registers;
+  unsigned char *registers = regcache->registers;
 
   convert_int_to_ascii (registers, buf, register_bytes);
 }
 
 void
 
   convert_int_to_ascii (registers, buf, register_bytes);
 }
 
 void
-registers_from_string (char *buf)
+registers_from_string (struct regcache *regcache, char *buf)
 {
   int len = strlen (buf);
 {
   int len = strlen (buf);
-  unsigned char *registers = get_regcache (current_inferior, 1)->registers;
+  unsigned char *registers = regcache->registers;
 
   if (len != register_bytes * 2)
     {
 
   if (len != register_bytes * 2)
     {
-      warning ("Wrong sized register packet (expected %d bytes, got %d)", 2*register_bytes, len);
+      warning ("Wrong sized register packet (expected %d bytes, got %d)",
+              2*register_bytes, len);
       if (len > register_bytes * 2)
        len = register_bytes * 2;
     }
       if (len > register_bytes * 2)
        len = register_bytes * 2;
     }
@@ -201,40 +207,43 @@ register_size (int n)
 }
 
 static unsigned char *
 }
 
 static unsigned char *
-register_data (int n, int fetch)
+register_data (struct regcache *regcache, int n, int fetch)
 {
 {
-  unsigned char *registers
-    = get_regcache (current_inferior, fetch)->registers;
-
-  return registers + (reg_defs[n].offset / 8);
+  return regcache->registers + (reg_defs[n].offset / 8);
 }
 
 void
 }
 
 void
-supply_register (int n, const void *buf)
+supply_register (struct regcache *regcache, int n, const void *buf)
 {
 {
-  memcpy (register_data (n, 0), buf, register_size (n));
+  if (buf)
+    memcpy (register_data (regcache, n, 0), buf, register_size (n));
+  else
+    memset (register_data (regcache, n, 0), 0, register_size (n));
 }
 
 void
 }
 
 void
-supply_register_by_name (const char *name, const void *buf)
+supply_register_by_name (struct regcache *regcache,
+                        const char *name, const void *buf)
 {
 {
-  supply_register (find_regno (name), buf);
+  supply_register (regcache, find_regno (name), buf);
 }
 
 void
 }
 
 void
-collect_register (int n, void *buf)
+collect_register (struct regcache *regcache, int n, void *buf)
 {
 {
-  memcpy (buf, register_data (n, 1), register_size (n));
+  memcpy (buf, register_data (regcache, n, 1), register_size (n));
 }
 
 void
 }
 
 void
-collect_register_as_string (int n, char *buf)
+collect_register_as_string (struct regcache *regcache, int n, char *buf)
 {
 {
-  convert_int_to_ascii (register_data (n, 1), buf, register_size (n));
+  convert_int_to_ascii (register_data (regcache, n, 1),
+                       buf, register_size (n));
 }
 
 void
 }
 
 void
-collect_register_by_name (const char *name, void *buf)
+collect_register_by_name (struct regcache *regcache,
+                         const char *name, void *buf)
 {
 {
-  collect_register (find_regno (name), buf);
+  collect_register (regcache, find_regno (name), buf);
 }
 }
This page took 0.04652 seconds and 4 git commands to generate.