xref: /dflybsd-src/contrib/gdb-7/gdb/regcache.c (revision cf7f2e2d389e8012d562650bd94d7e433f449d6e)
15796c8dcSSimon Schubert /* Cache and manage the values of registers for GDB, the GNU debugger.
25796c8dcSSimon Schubert 
35796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4*cf7f2e2dSJohn Marino    2002, 2004, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
55796c8dcSSimon Schubert 
65796c8dcSSimon Schubert    This file is part of GDB.
75796c8dcSSimon Schubert 
85796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
95796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
105796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
115796c8dcSSimon Schubert    (at your option) any later version.
125796c8dcSSimon Schubert 
135796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
145796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
155796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
165796c8dcSSimon Schubert    GNU General Public License for more details.
175796c8dcSSimon Schubert 
185796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
195796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
205796c8dcSSimon Schubert 
215796c8dcSSimon Schubert #include "defs.h"
225796c8dcSSimon Schubert #include "inferior.h"
235796c8dcSSimon Schubert #include "target.h"
245796c8dcSSimon Schubert #include "gdbarch.h"
255796c8dcSSimon Schubert #include "gdbcmd.h"
265796c8dcSSimon Schubert #include "regcache.h"
275796c8dcSSimon Schubert #include "reggroups.h"
285796c8dcSSimon Schubert #include "gdb_assert.h"
295796c8dcSSimon Schubert #include "gdb_string.h"
305796c8dcSSimon Schubert #include "gdbcmd.h"		/* For maintenanceprintlist.  */
315796c8dcSSimon Schubert #include "observer.h"
325796c8dcSSimon Schubert 
335796c8dcSSimon Schubert /*
345796c8dcSSimon Schubert  * DATA STRUCTURE
355796c8dcSSimon Schubert  *
365796c8dcSSimon Schubert  * Here is the actual register cache.
375796c8dcSSimon Schubert  */
385796c8dcSSimon Schubert 
395796c8dcSSimon Schubert /* Per-architecture object describing the layout of a register cache.
405796c8dcSSimon Schubert    Computed once when the architecture is created */
415796c8dcSSimon Schubert 
425796c8dcSSimon Schubert struct gdbarch_data *regcache_descr_handle;
435796c8dcSSimon Schubert 
445796c8dcSSimon Schubert struct regcache_descr
455796c8dcSSimon Schubert {
465796c8dcSSimon Schubert   /* The architecture this descriptor belongs to.  */
475796c8dcSSimon Schubert   struct gdbarch *gdbarch;
485796c8dcSSimon Schubert 
495796c8dcSSimon Schubert   /* The raw register cache.  Each raw (or hard) register is supplied
505796c8dcSSimon Schubert      by the target interface.  The raw cache should not contain
515796c8dcSSimon Schubert      redundant information - if the PC is constructed from two
525796c8dcSSimon Schubert      registers then those registers and not the PC lives in the raw
535796c8dcSSimon Schubert      cache.  */
545796c8dcSSimon Schubert   int nr_raw_registers;
555796c8dcSSimon Schubert   long sizeof_raw_registers;
565796c8dcSSimon Schubert   long sizeof_raw_register_valid_p;
575796c8dcSSimon Schubert 
585796c8dcSSimon Schubert   /* The cooked register space.  Each cooked register in the range
595796c8dcSSimon Schubert      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
605796c8dcSSimon Schubert      register.  The remaining [NR_RAW_REGISTERS
615796c8dcSSimon Schubert      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
625796c8dcSSimon Schubert      both raw registers and memory by the architecture methods
635796c8dcSSimon Schubert      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
645796c8dcSSimon Schubert   int nr_cooked_registers;
655796c8dcSSimon Schubert   long sizeof_cooked_registers;
665796c8dcSSimon Schubert   long sizeof_cooked_register_valid_p;
675796c8dcSSimon Schubert 
685796c8dcSSimon Schubert   /* Offset and size (in 8 bit bytes), of reach register in the
695796c8dcSSimon Schubert      register cache.  All registers (including those in the range
705796c8dcSSimon Schubert      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
715796c8dcSSimon Schubert      Assigning all registers an offset makes it possible to keep
725796c8dcSSimon Schubert      legacy code, such as that found in read_register_bytes() and
735796c8dcSSimon Schubert      write_register_bytes() working.  */
745796c8dcSSimon Schubert   long *register_offset;
755796c8dcSSimon Schubert   long *sizeof_register;
765796c8dcSSimon Schubert 
775796c8dcSSimon Schubert   /* Cached table containing the type of each register.  */
785796c8dcSSimon Schubert   struct type **register_type;
795796c8dcSSimon Schubert };
805796c8dcSSimon Schubert 
815796c8dcSSimon Schubert static void *
825796c8dcSSimon Schubert init_regcache_descr (struct gdbarch *gdbarch)
835796c8dcSSimon Schubert {
845796c8dcSSimon Schubert   int i;
855796c8dcSSimon Schubert   struct regcache_descr *descr;
865796c8dcSSimon Schubert   gdb_assert (gdbarch != NULL);
875796c8dcSSimon Schubert 
885796c8dcSSimon Schubert   /* Create an initial, zero filled, table.  */
895796c8dcSSimon Schubert   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
905796c8dcSSimon Schubert   descr->gdbarch = gdbarch;
915796c8dcSSimon Schubert 
925796c8dcSSimon Schubert   /* Total size of the register space.  The raw registers are mapped
935796c8dcSSimon Schubert      directly onto the raw register cache while the pseudo's are
945796c8dcSSimon Schubert      either mapped onto raw-registers or memory.  */
955796c8dcSSimon Schubert   descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
965796c8dcSSimon Schubert 			       + gdbarch_num_pseudo_regs (gdbarch);
975796c8dcSSimon Schubert   descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (gdbarch)
985796c8dcSSimon Schubert 					  + gdbarch_num_pseudo_regs
995796c8dcSSimon Schubert 					      (gdbarch);
1005796c8dcSSimon Schubert 
1015796c8dcSSimon Schubert   /* Fill in a table of register types.  */
1025796c8dcSSimon Schubert   descr->register_type
1035796c8dcSSimon Schubert     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
1045796c8dcSSimon Schubert   for (i = 0; i < descr->nr_cooked_registers; i++)
1055796c8dcSSimon Schubert     descr->register_type[i] = gdbarch_register_type (gdbarch, i);
1065796c8dcSSimon Schubert 
1075796c8dcSSimon Schubert   /* Construct a strictly RAW register cache.  Don't allow pseudo's
1085796c8dcSSimon Schubert      into the register cache.  */
1095796c8dcSSimon Schubert   descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
1105796c8dcSSimon Schubert 
1115796c8dcSSimon Schubert   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
1125796c8dcSSimon Schubert      array.  This pretects GDB from erant code that accesses elements
1135796c8dcSSimon Schubert      of the global register_valid_p[] array in the range
1145796c8dcSSimon Schubert      [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs).  */
1155796c8dcSSimon Schubert   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
1165796c8dcSSimon Schubert 
1175796c8dcSSimon Schubert   /* Lay out the register cache.
1185796c8dcSSimon Schubert 
1195796c8dcSSimon Schubert      NOTE: cagney/2002-05-22: Only register_type() is used when
1205796c8dcSSimon Schubert      constructing the register cache.  It is assumed that the
1215796c8dcSSimon Schubert      register's raw size, virtual size and type length are all the
1225796c8dcSSimon Schubert      same.  */
1235796c8dcSSimon Schubert 
1245796c8dcSSimon Schubert   {
1255796c8dcSSimon Schubert     long offset = 0;
126*cf7f2e2dSJohn Marino 
1275796c8dcSSimon Schubert     descr->sizeof_register
1285796c8dcSSimon Schubert       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
1295796c8dcSSimon Schubert     descr->register_offset
1305796c8dcSSimon Schubert       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
1315796c8dcSSimon Schubert     for (i = 0; i < descr->nr_cooked_registers; i++)
1325796c8dcSSimon Schubert       {
1335796c8dcSSimon Schubert 	descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
1345796c8dcSSimon Schubert 	descr->register_offset[i] = offset;
1355796c8dcSSimon Schubert 	offset += descr->sizeof_register[i];
1365796c8dcSSimon Schubert 	gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
1375796c8dcSSimon Schubert       }
1385796c8dcSSimon Schubert     /* Set the real size of the register cache buffer.  */
1395796c8dcSSimon Schubert     descr->sizeof_cooked_registers = offset;
1405796c8dcSSimon Schubert   }
1415796c8dcSSimon Schubert 
1425796c8dcSSimon Schubert   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
1435796c8dcSSimon Schubert      the raw registers.  Unfortunately some code still accesses the
1445796c8dcSSimon Schubert      register array directly using the global registers[].  Until that
1455796c8dcSSimon Schubert      code has been purged, play safe and over allocating the register
1465796c8dcSSimon Schubert      buffer.  Ulgh!  */
1475796c8dcSSimon Schubert   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
1485796c8dcSSimon Schubert 
1495796c8dcSSimon Schubert   return descr;
1505796c8dcSSimon Schubert }
1515796c8dcSSimon Schubert 
1525796c8dcSSimon Schubert static struct regcache_descr *
1535796c8dcSSimon Schubert regcache_descr (struct gdbarch *gdbarch)
1545796c8dcSSimon Schubert {
1555796c8dcSSimon Schubert   return gdbarch_data (gdbarch, regcache_descr_handle);
1565796c8dcSSimon Schubert }
1575796c8dcSSimon Schubert 
1585796c8dcSSimon Schubert /* Utility functions returning useful register attributes stored in
1595796c8dcSSimon Schubert    the regcache descr.  */
1605796c8dcSSimon Schubert 
1615796c8dcSSimon Schubert struct type *
1625796c8dcSSimon Schubert register_type (struct gdbarch *gdbarch, int regnum)
1635796c8dcSSimon Schubert {
1645796c8dcSSimon Schubert   struct regcache_descr *descr = regcache_descr (gdbarch);
165*cf7f2e2dSJohn Marino 
1665796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1675796c8dcSSimon Schubert   return descr->register_type[regnum];
1685796c8dcSSimon Schubert }
1695796c8dcSSimon Schubert 
1705796c8dcSSimon Schubert /* Utility functions returning useful register attributes stored in
1715796c8dcSSimon Schubert    the regcache descr.  */
1725796c8dcSSimon Schubert 
1735796c8dcSSimon Schubert int
1745796c8dcSSimon Schubert register_size (struct gdbarch *gdbarch, int regnum)
1755796c8dcSSimon Schubert {
1765796c8dcSSimon Schubert   struct regcache_descr *descr = regcache_descr (gdbarch);
1775796c8dcSSimon Schubert   int size;
178*cf7f2e2dSJohn Marino 
1795796c8dcSSimon Schubert   gdb_assert (regnum >= 0
1805796c8dcSSimon Schubert 	      && regnum < (gdbarch_num_regs (gdbarch)
1815796c8dcSSimon Schubert 			   + gdbarch_num_pseudo_regs (gdbarch)));
1825796c8dcSSimon Schubert   size = descr->sizeof_register[regnum];
1835796c8dcSSimon Schubert   return size;
1845796c8dcSSimon Schubert }
1855796c8dcSSimon Schubert 
1865796c8dcSSimon Schubert /* The register cache for storing raw register values.  */
1875796c8dcSSimon Schubert 
1885796c8dcSSimon Schubert struct regcache
1895796c8dcSSimon Schubert {
1905796c8dcSSimon Schubert   struct regcache_descr *descr;
191*cf7f2e2dSJohn Marino 
192*cf7f2e2dSJohn Marino   /* The address space of this register cache (for registers where it
193*cf7f2e2dSJohn Marino      makes sense, like PC or SP).  */
194*cf7f2e2dSJohn Marino   struct address_space *aspace;
195*cf7f2e2dSJohn Marino 
1965796c8dcSSimon Schubert   /* The register buffers.  A read-only register cache can hold the
1975796c8dcSSimon Schubert      full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
1985796c8dcSSimon Schubert      register cache can only hold [0 .. gdbarch_num_regs).  */
1995796c8dcSSimon Schubert   gdb_byte *registers;
2005796c8dcSSimon Schubert   /* Register cache status:
2015796c8dcSSimon Schubert      register_valid_p[REG] == 0 if REG value is not in the cache
2025796c8dcSSimon Schubert                             > 0 if REG value is in the cache
2035796c8dcSSimon Schubert                             < 0 if REG value is permanently unavailable */
2045796c8dcSSimon Schubert   signed char *register_valid_p;
2055796c8dcSSimon Schubert   /* Is this a read-only cache?  A read-only cache is used for saving
2065796c8dcSSimon Schubert      the target's register state (e.g, across an inferior function
2075796c8dcSSimon Schubert      call or just before forcing a function return).  A read-only
2085796c8dcSSimon Schubert      cache can only be updated via the methods regcache_dup() and
2095796c8dcSSimon Schubert      regcache_cpy().  The actual contents are determined by the
2105796c8dcSSimon Schubert      reggroup_save and reggroup_restore methods.  */
2115796c8dcSSimon Schubert   int readonly_p;
2125796c8dcSSimon Schubert   /* If this is a read-write cache, which thread's registers is
2135796c8dcSSimon Schubert      it connected to?  */
2145796c8dcSSimon Schubert   ptid_t ptid;
2155796c8dcSSimon Schubert };
2165796c8dcSSimon Schubert 
2175796c8dcSSimon Schubert struct regcache *
218*cf7f2e2dSJohn Marino regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
2195796c8dcSSimon Schubert {
2205796c8dcSSimon Schubert   struct regcache_descr *descr;
2215796c8dcSSimon Schubert   struct regcache *regcache;
222*cf7f2e2dSJohn Marino 
2235796c8dcSSimon Schubert   gdb_assert (gdbarch != NULL);
2245796c8dcSSimon Schubert   descr = regcache_descr (gdbarch);
2255796c8dcSSimon Schubert   regcache = XMALLOC (struct regcache);
2265796c8dcSSimon Schubert   regcache->descr = descr;
2275796c8dcSSimon Schubert   regcache->registers
2285796c8dcSSimon Schubert     = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
2295796c8dcSSimon Schubert   regcache->register_valid_p
2305796c8dcSSimon Schubert     = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
231*cf7f2e2dSJohn Marino   regcache->aspace = aspace;
2325796c8dcSSimon Schubert   regcache->readonly_p = 1;
2335796c8dcSSimon Schubert   regcache->ptid = minus_one_ptid;
2345796c8dcSSimon Schubert   return regcache;
2355796c8dcSSimon Schubert }
2365796c8dcSSimon Schubert 
2375796c8dcSSimon Schubert void
2385796c8dcSSimon Schubert regcache_xfree (struct regcache *regcache)
2395796c8dcSSimon Schubert {
2405796c8dcSSimon Schubert   if (regcache == NULL)
2415796c8dcSSimon Schubert     return;
2425796c8dcSSimon Schubert   xfree (regcache->registers);
2435796c8dcSSimon Schubert   xfree (regcache->register_valid_p);
2445796c8dcSSimon Schubert   xfree (regcache);
2455796c8dcSSimon Schubert }
2465796c8dcSSimon Schubert 
2475796c8dcSSimon Schubert static void
2485796c8dcSSimon Schubert do_regcache_xfree (void *data)
2495796c8dcSSimon Schubert {
2505796c8dcSSimon Schubert   regcache_xfree (data);
2515796c8dcSSimon Schubert }
2525796c8dcSSimon Schubert 
2535796c8dcSSimon Schubert struct cleanup *
2545796c8dcSSimon Schubert make_cleanup_regcache_xfree (struct regcache *regcache)
2555796c8dcSSimon Schubert {
2565796c8dcSSimon Schubert   return make_cleanup (do_regcache_xfree, regcache);
2575796c8dcSSimon Schubert }
2585796c8dcSSimon Schubert 
2595796c8dcSSimon Schubert /* Return REGCACHE's architecture.  */
2605796c8dcSSimon Schubert 
2615796c8dcSSimon Schubert struct gdbarch *
2625796c8dcSSimon Schubert get_regcache_arch (const struct regcache *regcache)
2635796c8dcSSimon Schubert {
2645796c8dcSSimon Schubert   return regcache->descr->gdbarch;
2655796c8dcSSimon Schubert }
2665796c8dcSSimon Schubert 
267*cf7f2e2dSJohn Marino struct address_space *
268*cf7f2e2dSJohn Marino get_regcache_aspace (const struct regcache *regcache)
269*cf7f2e2dSJohn Marino {
270*cf7f2e2dSJohn Marino   return regcache->aspace;
271*cf7f2e2dSJohn Marino }
272*cf7f2e2dSJohn Marino 
2735796c8dcSSimon Schubert /* Return  a pointer to register REGNUM's buffer cache.  */
2745796c8dcSSimon Schubert 
2755796c8dcSSimon Schubert static gdb_byte *
2765796c8dcSSimon Schubert register_buffer (const struct regcache *regcache, int regnum)
2775796c8dcSSimon Schubert {
2785796c8dcSSimon Schubert   return regcache->registers + regcache->descr->register_offset[regnum];
2795796c8dcSSimon Schubert }
2805796c8dcSSimon Schubert 
2815796c8dcSSimon Schubert void
2825796c8dcSSimon Schubert regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
2835796c8dcSSimon Schubert 	       void *src)
2845796c8dcSSimon Schubert {
2855796c8dcSSimon Schubert   struct gdbarch *gdbarch = dst->descr->gdbarch;
2865796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
2875796c8dcSSimon Schubert   int regnum;
288*cf7f2e2dSJohn Marino 
2895796c8dcSSimon Schubert   /* The DST should be `read-only', if it wasn't then the save would
2905796c8dcSSimon Schubert      end up trying to write the register values back out to the
2915796c8dcSSimon Schubert      target.  */
2925796c8dcSSimon Schubert   gdb_assert (dst->readonly_p);
2935796c8dcSSimon Schubert   /* Clear the dest.  */
2945796c8dcSSimon Schubert   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
2955796c8dcSSimon Schubert   memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
2965796c8dcSSimon Schubert   /* Copy over any registers (identified by their membership in the
2975796c8dcSSimon Schubert      save_reggroup) and mark them as valid.  The full [0 .. gdbarch_num_regs +
2985796c8dcSSimon Schubert      gdbarch_num_pseudo_regs) range is checked since some architectures need
2995796c8dcSSimon Schubert      to save/restore `cooked' registers that live in memory.  */
3005796c8dcSSimon Schubert   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
3015796c8dcSSimon Schubert     {
3025796c8dcSSimon Schubert       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
3035796c8dcSSimon Schubert 	{
3045796c8dcSSimon Schubert 	  int valid = cooked_read (src, regnum, buf);
305*cf7f2e2dSJohn Marino 
3065796c8dcSSimon Schubert 	  if (valid)
3075796c8dcSSimon Schubert 	    {
3085796c8dcSSimon Schubert 	      memcpy (register_buffer (dst, regnum), buf,
3095796c8dcSSimon Schubert 		      register_size (gdbarch, regnum));
3105796c8dcSSimon Schubert 	      dst->register_valid_p[regnum] = 1;
3115796c8dcSSimon Schubert 	    }
3125796c8dcSSimon Schubert 	}
3135796c8dcSSimon Schubert     }
3145796c8dcSSimon Schubert }
3155796c8dcSSimon Schubert 
3165796c8dcSSimon Schubert void
3175796c8dcSSimon Schubert regcache_restore (struct regcache *dst,
3185796c8dcSSimon Schubert 		  regcache_cooked_read_ftype *cooked_read,
3195796c8dcSSimon Schubert 		  void *cooked_read_context)
3205796c8dcSSimon Schubert {
3215796c8dcSSimon Schubert   struct gdbarch *gdbarch = dst->descr->gdbarch;
3225796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
3235796c8dcSSimon Schubert   int regnum;
324*cf7f2e2dSJohn Marino 
3255796c8dcSSimon Schubert   /* The dst had better not be read-only.  If it is, the `restore'
3265796c8dcSSimon Schubert      doesn't make much sense.  */
3275796c8dcSSimon Schubert   gdb_assert (!dst->readonly_p);
3285796c8dcSSimon Schubert   /* Copy over any registers, being careful to only restore those that
3295796c8dcSSimon Schubert      were both saved and need to be restored.  The full [0 .. gdbarch_num_regs
3305796c8dcSSimon Schubert      + gdbarch_num_pseudo_regs) range is checked since some architectures need
3315796c8dcSSimon Schubert      to save/restore `cooked' registers that live in memory.  */
3325796c8dcSSimon Schubert   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
3335796c8dcSSimon Schubert     {
3345796c8dcSSimon Schubert       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
3355796c8dcSSimon Schubert 	{
3365796c8dcSSimon Schubert 	  int valid = cooked_read (cooked_read_context, regnum, buf);
337*cf7f2e2dSJohn Marino 
3385796c8dcSSimon Schubert 	  if (valid)
3395796c8dcSSimon Schubert 	    regcache_cooked_write (dst, regnum, buf);
3405796c8dcSSimon Schubert 	}
3415796c8dcSSimon Schubert     }
3425796c8dcSSimon Schubert }
3435796c8dcSSimon Schubert 
3445796c8dcSSimon Schubert static int
3455796c8dcSSimon Schubert do_cooked_read (void *src, int regnum, gdb_byte *buf)
3465796c8dcSSimon Schubert {
3475796c8dcSSimon Schubert   struct regcache *regcache = src;
348*cf7f2e2dSJohn Marino 
3495796c8dcSSimon Schubert   if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
3505796c8dcSSimon Schubert     /* Don't even think about fetching a register from a read-only
3515796c8dcSSimon Schubert        cache when the register isn't yet valid.  There isn't a target
3525796c8dcSSimon Schubert        from which the register value can be fetched.  */
3535796c8dcSSimon Schubert     return 0;
3545796c8dcSSimon Schubert   regcache_cooked_read (regcache, regnum, buf);
3555796c8dcSSimon Schubert   return 1;
3565796c8dcSSimon Schubert }
3575796c8dcSSimon Schubert 
3585796c8dcSSimon Schubert 
3595796c8dcSSimon Schubert void
3605796c8dcSSimon Schubert regcache_cpy (struct regcache *dst, struct regcache *src)
3615796c8dcSSimon Schubert {
3625796c8dcSSimon Schubert   gdb_assert (src != NULL && dst != NULL);
3635796c8dcSSimon Schubert   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
3645796c8dcSSimon Schubert   gdb_assert (src != dst);
3655796c8dcSSimon Schubert   gdb_assert (src->readonly_p || dst->readonly_p);
366*cf7f2e2dSJohn Marino 
3675796c8dcSSimon Schubert   if (!src->readonly_p)
3685796c8dcSSimon Schubert     regcache_save (dst, do_cooked_read, src);
3695796c8dcSSimon Schubert   else if (!dst->readonly_p)
3705796c8dcSSimon Schubert     regcache_restore (dst, do_cooked_read, src);
3715796c8dcSSimon Schubert   else
3725796c8dcSSimon Schubert     regcache_cpy_no_passthrough (dst, src);
3735796c8dcSSimon Schubert }
3745796c8dcSSimon Schubert 
3755796c8dcSSimon Schubert void
3765796c8dcSSimon Schubert regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
3775796c8dcSSimon Schubert {
3785796c8dcSSimon Schubert   gdb_assert (src != NULL && dst != NULL);
3795796c8dcSSimon Schubert   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
3805796c8dcSSimon Schubert   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
3815796c8dcSSimon Schubert      move of data into the current regcache.  Doing this would be
3825796c8dcSSimon Schubert      silly - it would mean that valid_p would be completely invalid.  */
3835796c8dcSSimon Schubert   gdb_assert (dst->readonly_p);
384*cf7f2e2dSJohn Marino 
3855796c8dcSSimon Schubert   memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
3865796c8dcSSimon Schubert   memcpy (dst->register_valid_p, src->register_valid_p,
3875796c8dcSSimon Schubert 	  dst->descr->sizeof_raw_register_valid_p);
3885796c8dcSSimon Schubert }
3895796c8dcSSimon Schubert 
3905796c8dcSSimon Schubert struct regcache *
3915796c8dcSSimon Schubert regcache_dup (struct regcache *src)
3925796c8dcSSimon Schubert {
3935796c8dcSSimon Schubert   struct regcache *newbuf;
394*cf7f2e2dSJohn Marino 
395*cf7f2e2dSJohn Marino   newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
3965796c8dcSSimon Schubert   regcache_cpy (newbuf, src);
3975796c8dcSSimon Schubert   return newbuf;
3985796c8dcSSimon Schubert }
3995796c8dcSSimon Schubert 
4005796c8dcSSimon Schubert struct regcache *
4015796c8dcSSimon Schubert regcache_dup_no_passthrough (struct regcache *src)
4025796c8dcSSimon Schubert {
4035796c8dcSSimon Schubert   struct regcache *newbuf;
404*cf7f2e2dSJohn Marino 
405*cf7f2e2dSJohn Marino   newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
4065796c8dcSSimon Schubert   regcache_cpy_no_passthrough (newbuf, src);
4075796c8dcSSimon Schubert   return newbuf;
4085796c8dcSSimon Schubert }
4095796c8dcSSimon Schubert 
4105796c8dcSSimon Schubert int
4115796c8dcSSimon Schubert regcache_valid_p (const struct regcache *regcache, int regnum)
4125796c8dcSSimon Schubert {
4135796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
4145796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
4155796c8dcSSimon Schubert   if (regcache->readonly_p)
4165796c8dcSSimon Schubert     gdb_assert (regnum < regcache->descr->nr_cooked_registers);
4175796c8dcSSimon Schubert   else
4185796c8dcSSimon Schubert     gdb_assert (regnum < regcache->descr->nr_raw_registers);
4195796c8dcSSimon Schubert 
4205796c8dcSSimon Schubert   return regcache->register_valid_p[regnum];
4215796c8dcSSimon Schubert }
4225796c8dcSSimon Schubert 
4235796c8dcSSimon Schubert void
4245796c8dcSSimon Schubert regcache_invalidate (struct regcache *regcache, int regnum)
4255796c8dcSSimon Schubert {
4265796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
4275796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
4285796c8dcSSimon Schubert   gdb_assert (!regcache->readonly_p);
4295796c8dcSSimon Schubert   gdb_assert (regnum < regcache->descr->nr_raw_registers);
4305796c8dcSSimon Schubert   regcache->register_valid_p[regnum] = 0;
4315796c8dcSSimon Schubert }
4325796c8dcSSimon Schubert 
4335796c8dcSSimon Schubert 
4345796c8dcSSimon Schubert /* Global structure containing the current regcache.  */
4355796c8dcSSimon Schubert 
4365796c8dcSSimon Schubert /* NOTE: this is a write-through cache.  There is no "dirty" bit for
4375796c8dcSSimon Schubert    recording if the register values have been changed (eg. by the
4385796c8dcSSimon Schubert    user).  Therefore all registers must be written back to the
4395796c8dcSSimon Schubert    target when appropriate.  */
4405796c8dcSSimon Schubert 
4415796c8dcSSimon Schubert struct regcache_list
4425796c8dcSSimon Schubert {
4435796c8dcSSimon Schubert   struct regcache *regcache;
4445796c8dcSSimon Schubert   struct regcache_list *next;
4455796c8dcSSimon Schubert };
4465796c8dcSSimon Schubert 
4475796c8dcSSimon Schubert static struct regcache_list *current_regcache;
4485796c8dcSSimon Schubert 
4495796c8dcSSimon Schubert struct regcache *
4505796c8dcSSimon Schubert get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
4515796c8dcSSimon Schubert {
4525796c8dcSSimon Schubert   struct regcache_list *list;
4535796c8dcSSimon Schubert   struct regcache *new_regcache;
4545796c8dcSSimon Schubert 
4555796c8dcSSimon Schubert   for (list = current_regcache; list; list = list->next)
4565796c8dcSSimon Schubert     if (ptid_equal (list->regcache->ptid, ptid)
4575796c8dcSSimon Schubert 	&& get_regcache_arch (list->regcache) == gdbarch)
4585796c8dcSSimon Schubert       return list->regcache;
4595796c8dcSSimon Schubert 
460*cf7f2e2dSJohn Marino   new_regcache = regcache_xmalloc (gdbarch,
461*cf7f2e2dSJohn Marino 				   target_thread_address_space (ptid));
4625796c8dcSSimon Schubert   new_regcache->readonly_p = 0;
4635796c8dcSSimon Schubert   new_regcache->ptid = ptid;
464*cf7f2e2dSJohn Marino   gdb_assert (new_regcache->aspace != NULL);
4655796c8dcSSimon Schubert 
4665796c8dcSSimon Schubert   list = xmalloc (sizeof (struct regcache_list));
4675796c8dcSSimon Schubert   list->regcache = new_regcache;
4685796c8dcSSimon Schubert   list->next = current_regcache;
4695796c8dcSSimon Schubert   current_regcache = list;
4705796c8dcSSimon Schubert 
4715796c8dcSSimon Schubert   return new_regcache;
4725796c8dcSSimon Schubert }
4735796c8dcSSimon Schubert 
4745796c8dcSSimon Schubert static ptid_t current_thread_ptid;
4755796c8dcSSimon Schubert static struct gdbarch *current_thread_arch;
4765796c8dcSSimon Schubert 
4775796c8dcSSimon Schubert struct regcache *
4785796c8dcSSimon Schubert get_thread_regcache (ptid_t ptid)
4795796c8dcSSimon Schubert {
4805796c8dcSSimon Schubert   if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
4815796c8dcSSimon Schubert     {
4825796c8dcSSimon Schubert       current_thread_ptid = ptid;
4835796c8dcSSimon Schubert       current_thread_arch = target_thread_architecture (ptid);
4845796c8dcSSimon Schubert     }
4855796c8dcSSimon Schubert 
4865796c8dcSSimon Schubert   return get_thread_arch_regcache (ptid, current_thread_arch);
4875796c8dcSSimon Schubert }
4885796c8dcSSimon Schubert 
4895796c8dcSSimon Schubert struct regcache *
4905796c8dcSSimon Schubert get_current_regcache (void)
4915796c8dcSSimon Schubert {
4925796c8dcSSimon Schubert   return get_thread_regcache (inferior_ptid);
4935796c8dcSSimon Schubert }
4945796c8dcSSimon Schubert 
4955796c8dcSSimon Schubert 
4965796c8dcSSimon Schubert /* Observer for the target_changed event.  */
4975796c8dcSSimon Schubert 
4985796c8dcSSimon Schubert static void
4995796c8dcSSimon Schubert regcache_observer_target_changed (struct target_ops *target)
5005796c8dcSSimon Schubert {
5015796c8dcSSimon Schubert   registers_changed ();
5025796c8dcSSimon Schubert }
5035796c8dcSSimon Schubert 
5045796c8dcSSimon Schubert /* Update global variables old ptids to hold NEW_PTID if they were
5055796c8dcSSimon Schubert    holding OLD_PTID.  */
5065796c8dcSSimon Schubert static void
5075796c8dcSSimon Schubert regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
5085796c8dcSSimon Schubert {
5095796c8dcSSimon Schubert   struct regcache_list *list;
5105796c8dcSSimon Schubert 
5115796c8dcSSimon Schubert   for (list = current_regcache; list; list = list->next)
5125796c8dcSSimon Schubert     if (ptid_equal (list->regcache->ptid, old_ptid))
5135796c8dcSSimon Schubert       list->regcache->ptid = new_ptid;
5145796c8dcSSimon Schubert }
5155796c8dcSSimon Schubert 
5165796c8dcSSimon Schubert /* Low level examining and depositing of registers.
5175796c8dcSSimon Schubert 
5185796c8dcSSimon Schubert    The caller is responsible for making sure that the inferior is
5195796c8dcSSimon Schubert    stopped before calling the fetching routines, or it will get
5205796c8dcSSimon Schubert    garbage.  (a change from GDB version 3, in which the caller got the
5215796c8dcSSimon Schubert    value from the last stop).  */
5225796c8dcSSimon Schubert 
5235796c8dcSSimon Schubert /* REGISTERS_CHANGED ()
5245796c8dcSSimon Schubert 
5255796c8dcSSimon Schubert    Indicate that registers may have changed, so invalidate the cache.  */
5265796c8dcSSimon Schubert 
5275796c8dcSSimon Schubert void
528*cf7f2e2dSJohn Marino registers_changed_ptid (ptid_t ptid)
5295796c8dcSSimon Schubert {
530*cf7f2e2dSJohn Marino   struct regcache_list *list, **list_link;
5315796c8dcSSimon Schubert 
532*cf7f2e2dSJohn Marino   list = current_regcache;
533*cf7f2e2dSJohn Marino   list_link = &current_regcache;
534*cf7f2e2dSJohn Marino   while (list)
5355796c8dcSSimon Schubert     {
536*cf7f2e2dSJohn Marino       if (ptid_match (list->regcache->ptid, ptid))
537*cf7f2e2dSJohn Marino 	{
538*cf7f2e2dSJohn Marino 	  struct regcache_list *dead = list;
539*cf7f2e2dSJohn Marino 
540*cf7f2e2dSJohn Marino 	  *list_link = list->next;
5415796c8dcSSimon Schubert 	  regcache_xfree (list->regcache);
542*cf7f2e2dSJohn Marino 	  list = *list_link;
543*cf7f2e2dSJohn Marino 	  xfree (dead);
544*cf7f2e2dSJohn Marino 	  continue;
545*cf7f2e2dSJohn Marino 	}
546*cf7f2e2dSJohn Marino 
547*cf7f2e2dSJohn Marino       list_link = &list->next;
548*cf7f2e2dSJohn Marino       list = *list_link;
5495796c8dcSSimon Schubert     }
5505796c8dcSSimon Schubert 
5515796c8dcSSimon Schubert   current_regcache = NULL;
5525796c8dcSSimon Schubert 
5535796c8dcSSimon Schubert   current_thread_ptid = null_ptid;
5545796c8dcSSimon Schubert   current_thread_arch = NULL;
5555796c8dcSSimon Schubert 
5565796c8dcSSimon Schubert   /* Need to forget about any frames we have cached, too. */
5575796c8dcSSimon Schubert   reinit_frame_cache ();
5585796c8dcSSimon Schubert 
5595796c8dcSSimon Schubert   /* Force cleanup of any alloca areas if using C alloca instead of
5605796c8dcSSimon Schubert      a builtin alloca.  This particular call is used to clean up
5615796c8dcSSimon Schubert      areas allocated by low level target code which may build up
5625796c8dcSSimon Schubert      during lengthy interactions between gdb and the target before
5635796c8dcSSimon Schubert      gdb gives control to the user (ie watchpoints).  */
5645796c8dcSSimon Schubert   alloca (0);
5655796c8dcSSimon Schubert }
5665796c8dcSSimon Schubert 
567*cf7f2e2dSJohn Marino void
568*cf7f2e2dSJohn Marino registers_changed (void)
569*cf7f2e2dSJohn Marino {
570*cf7f2e2dSJohn Marino   registers_changed_ptid (minus_one_ptid);
571*cf7f2e2dSJohn Marino }
5725796c8dcSSimon Schubert 
5735796c8dcSSimon Schubert void
5745796c8dcSSimon Schubert regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
5755796c8dcSSimon Schubert {
5765796c8dcSSimon Schubert   gdb_assert (regcache != NULL && buf != NULL);
5775796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
5785796c8dcSSimon Schubert   /* Make certain that the register cache is up-to-date with respect
5795796c8dcSSimon Schubert      to the current thread.  This switching shouldn't be necessary
5805796c8dcSSimon Schubert      only there is still only one target side register cache.  Sigh!
5815796c8dcSSimon Schubert      On the bright side, at least there is a regcache object.  */
5825796c8dcSSimon Schubert   if (!regcache->readonly_p)
5835796c8dcSSimon Schubert     {
5845796c8dcSSimon Schubert       if (!regcache_valid_p (regcache, regnum))
5855796c8dcSSimon Schubert 	{
5865796c8dcSSimon Schubert 	  struct cleanup *old_chain = save_inferior_ptid ();
587*cf7f2e2dSJohn Marino 
5885796c8dcSSimon Schubert 	  inferior_ptid = regcache->ptid;
5895796c8dcSSimon Schubert 	  target_fetch_registers (regcache, regnum);
5905796c8dcSSimon Schubert 	  do_cleanups (old_chain);
5915796c8dcSSimon Schubert 	}
5925796c8dcSSimon Schubert #if 0
5935796c8dcSSimon Schubert       /* FIXME: cagney/2004-08-07: At present a number of targets
5945796c8dcSSimon Schubert 	 forget (or didn't know that they needed) to set this leading to
5955796c8dcSSimon Schubert 	 panics.  Also is the problem that targets need to indicate
5965796c8dcSSimon Schubert 	 that a register is in one of the possible states: valid,
5975796c8dcSSimon Schubert 	 undefined, unknown.  The last of which isn't yet
5985796c8dcSSimon Schubert 	 possible.  */
5995796c8dcSSimon Schubert       gdb_assert (regcache_valid_p (regcache, regnum));
6005796c8dcSSimon Schubert #endif
6015796c8dcSSimon Schubert     }
6025796c8dcSSimon Schubert   /* Copy the value directly into the register cache.  */
6035796c8dcSSimon Schubert   memcpy (buf, register_buffer (regcache, regnum),
6045796c8dcSSimon Schubert 	  regcache->descr->sizeof_register[regnum]);
6055796c8dcSSimon Schubert }
6065796c8dcSSimon Schubert 
6075796c8dcSSimon Schubert void
6085796c8dcSSimon Schubert regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
6095796c8dcSSimon Schubert {
6105796c8dcSSimon Schubert   gdb_byte *buf;
611*cf7f2e2dSJohn Marino 
6125796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
6135796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
6145796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
6155796c8dcSSimon Schubert   regcache_raw_read (regcache, regnum, buf);
6165796c8dcSSimon Schubert   (*val) = extract_signed_integer
6175796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
6185796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
6195796c8dcSSimon Schubert }
6205796c8dcSSimon Schubert 
6215796c8dcSSimon Schubert void
6225796c8dcSSimon Schubert regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
6235796c8dcSSimon Schubert 			    ULONGEST *val)
6245796c8dcSSimon Schubert {
6255796c8dcSSimon Schubert   gdb_byte *buf;
626*cf7f2e2dSJohn Marino 
6275796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
6285796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
6295796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
6305796c8dcSSimon Schubert   regcache_raw_read (regcache, regnum, buf);
6315796c8dcSSimon Schubert   (*val) = extract_unsigned_integer
6325796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
6335796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
6345796c8dcSSimon Schubert }
6355796c8dcSSimon Schubert 
6365796c8dcSSimon Schubert void
6375796c8dcSSimon Schubert regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
6385796c8dcSSimon Schubert {
6395796c8dcSSimon Schubert   void *buf;
640*cf7f2e2dSJohn Marino 
6415796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
6425796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
6435796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
6445796c8dcSSimon Schubert   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
6455796c8dcSSimon Schubert 			gdbarch_byte_order (regcache->descr->gdbarch), val);
6465796c8dcSSimon Schubert   regcache_raw_write (regcache, regnum, buf);
6475796c8dcSSimon Schubert }
6485796c8dcSSimon Schubert 
6495796c8dcSSimon Schubert void
6505796c8dcSSimon Schubert regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
6515796c8dcSSimon Schubert 			     ULONGEST val)
6525796c8dcSSimon Schubert {
6535796c8dcSSimon Schubert   void *buf;
654*cf7f2e2dSJohn Marino 
6555796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
6565796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
6575796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
6585796c8dcSSimon Schubert   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
6595796c8dcSSimon Schubert 			  gdbarch_byte_order (regcache->descr->gdbarch), val);
6605796c8dcSSimon Schubert   regcache_raw_write (regcache, regnum, buf);
6615796c8dcSSimon Schubert }
6625796c8dcSSimon Schubert 
6635796c8dcSSimon Schubert void
6645796c8dcSSimon Schubert regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
6655796c8dcSSimon Schubert {
6665796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
6675796c8dcSSimon Schubert   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
6685796c8dcSSimon Schubert   if (regnum < regcache->descr->nr_raw_registers)
6695796c8dcSSimon Schubert     regcache_raw_read (regcache, regnum, buf);
6705796c8dcSSimon Schubert   else if (regcache->readonly_p
6715796c8dcSSimon Schubert 	   && regnum < regcache->descr->nr_cooked_registers
6725796c8dcSSimon Schubert 	   && regcache->register_valid_p[regnum])
6735796c8dcSSimon Schubert     /* Read-only register cache, perhaps the cooked value was cached?  */
6745796c8dcSSimon Schubert     memcpy (buf, register_buffer (regcache, regnum),
6755796c8dcSSimon Schubert 	    regcache->descr->sizeof_register[regnum]);
6765796c8dcSSimon Schubert   else
6775796c8dcSSimon Schubert     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
6785796c8dcSSimon Schubert 				  regnum, buf);
6795796c8dcSSimon Schubert }
6805796c8dcSSimon Schubert 
6815796c8dcSSimon Schubert void
6825796c8dcSSimon Schubert regcache_cooked_read_signed (struct regcache *regcache, int regnum,
6835796c8dcSSimon Schubert 			     LONGEST *val)
6845796c8dcSSimon Schubert {
6855796c8dcSSimon Schubert   gdb_byte *buf;
686*cf7f2e2dSJohn Marino 
6875796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
6885796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
6895796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
6905796c8dcSSimon Schubert   regcache_cooked_read (regcache, regnum, buf);
6915796c8dcSSimon Schubert   (*val) = extract_signed_integer
6925796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
6935796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
6945796c8dcSSimon Schubert }
6955796c8dcSSimon Schubert 
6965796c8dcSSimon Schubert void
6975796c8dcSSimon Schubert regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
6985796c8dcSSimon Schubert 			       ULONGEST *val)
6995796c8dcSSimon Schubert {
7005796c8dcSSimon Schubert   gdb_byte *buf;
701*cf7f2e2dSJohn Marino 
7025796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
7035796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
7045796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
7055796c8dcSSimon Schubert   regcache_cooked_read (regcache, regnum, buf);
7065796c8dcSSimon Schubert   (*val) = extract_unsigned_integer
7075796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
7085796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
7095796c8dcSSimon Schubert }
7105796c8dcSSimon Schubert 
7115796c8dcSSimon Schubert void
7125796c8dcSSimon Schubert regcache_cooked_write_signed (struct regcache *regcache, int regnum,
7135796c8dcSSimon Schubert 			      LONGEST val)
7145796c8dcSSimon Schubert {
7155796c8dcSSimon Schubert   void *buf;
716*cf7f2e2dSJohn Marino 
7175796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
7185796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
7195796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
7205796c8dcSSimon Schubert   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
7215796c8dcSSimon Schubert 			gdbarch_byte_order (regcache->descr->gdbarch), val);
7225796c8dcSSimon Schubert   regcache_cooked_write (regcache, regnum, buf);
7235796c8dcSSimon Schubert }
7245796c8dcSSimon Schubert 
7255796c8dcSSimon Schubert void
7265796c8dcSSimon Schubert regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
7275796c8dcSSimon Schubert 				ULONGEST val)
7285796c8dcSSimon Schubert {
7295796c8dcSSimon Schubert   void *buf;
730*cf7f2e2dSJohn Marino 
7315796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
7325796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
7335796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
7345796c8dcSSimon Schubert   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
7355796c8dcSSimon Schubert 			  gdbarch_byte_order (regcache->descr->gdbarch), val);
7365796c8dcSSimon Schubert   regcache_cooked_write (regcache, regnum, buf);
7375796c8dcSSimon Schubert }
7385796c8dcSSimon Schubert 
7395796c8dcSSimon Schubert void
7405796c8dcSSimon Schubert regcache_raw_write (struct regcache *regcache, int regnum,
7415796c8dcSSimon Schubert 		    const gdb_byte *buf)
7425796c8dcSSimon Schubert {
7435796c8dcSSimon Schubert   struct cleanup *old_chain;
7445796c8dcSSimon Schubert 
7455796c8dcSSimon Schubert   gdb_assert (regcache != NULL && buf != NULL);
7465796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
7475796c8dcSSimon Schubert   gdb_assert (!regcache->readonly_p);
7485796c8dcSSimon Schubert 
7495796c8dcSSimon Schubert   /* On the sparc, writing %g0 is a no-op, so we don't even want to
7505796c8dcSSimon Schubert      change the registers array if something writes to this register.  */
7515796c8dcSSimon Schubert   if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
7525796c8dcSSimon Schubert     return;
7535796c8dcSSimon Schubert 
7545796c8dcSSimon Schubert   /* If we have a valid copy of the register, and new value == old
7555796c8dcSSimon Schubert      value, then don't bother doing the actual store. */
7565796c8dcSSimon Schubert   if (regcache_valid_p (regcache, regnum)
7575796c8dcSSimon Schubert       && (memcmp (register_buffer (regcache, regnum), buf,
7585796c8dcSSimon Schubert 		  regcache->descr->sizeof_register[regnum]) == 0))
7595796c8dcSSimon Schubert     return;
7605796c8dcSSimon Schubert 
7615796c8dcSSimon Schubert   old_chain = save_inferior_ptid ();
7625796c8dcSSimon Schubert   inferior_ptid = regcache->ptid;
7635796c8dcSSimon Schubert 
7645796c8dcSSimon Schubert   target_prepare_to_store (regcache);
7655796c8dcSSimon Schubert   memcpy (register_buffer (regcache, regnum), buf,
7665796c8dcSSimon Schubert 	  regcache->descr->sizeof_register[regnum]);
7675796c8dcSSimon Schubert   regcache->register_valid_p[regnum] = 1;
7685796c8dcSSimon Schubert   target_store_registers (regcache, regnum);
7695796c8dcSSimon Schubert 
7705796c8dcSSimon Schubert   do_cleanups (old_chain);
7715796c8dcSSimon Schubert }
7725796c8dcSSimon Schubert 
7735796c8dcSSimon Schubert void
7745796c8dcSSimon Schubert regcache_cooked_write (struct regcache *regcache, int regnum,
7755796c8dcSSimon Schubert 		       const gdb_byte *buf)
7765796c8dcSSimon Schubert {
7775796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
7785796c8dcSSimon Schubert   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
7795796c8dcSSimon Schubert   if (regnum < regcache->descr->nr_raw_registers)
7805796c8dcSSimon Schubert     regcache_raw_write (regcache, regnum, buf);
7815796c8dcSSimon Schubert   else
7825796c8dcSSimon Schubert     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
7835796c8dcSSimon Schubert 				   regnum, buf);
7845796c8dcSSimon Schubert }
7855796c8dcSSimon Schubert 
7865796c8dcSSimon Schubert /* Perform a partial register transfer using a read, modify, write
7875796c8dcSSimon Schubert    operation.  */
7885796c8dcSSimon Schubert 
7895796c8dcSSimon Schubert typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
7905796c8dcSSimon Schubert 				    void *buf);
7915796c8dcSSimon Schubert typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
7925796c8dcSSimon Schubert 				     const void *buf);
7935796c8dcSSimon Schubert 
7945796c8dcSSimon Schubert static void
7955796c8dcSSimon Schubert regcache_xfer_part (struct regcache *regcache, int regnum,
7965796c8dcSSimon Schubert 		    int offset, int len, void *in, const void *out,
7975796c8dcSSimon Schubert 		    void (*read) (struct regcache *regcache, int regnum,
7985796c8dcSSimon Schubert 				  gdb_byte *buf),
7995796c8dcSSimon Schubert 		    void (*write) (struct regcache *regcache, int regnum,
8005796c8dcSSimon Schubert 				   const gdb_byte *buf))
8015796c8dcSSimon Schubert {
8025796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
8035796c8dcSSimon Schubert   gdb_byte reg[MAX_REGISTER_SIZE];
804*cf7f2e2dSJohn Marino 
8055796c8dcSSimon Schubert   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
8065796c8dcSSimon Schubert   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
8075796c8dcSSimon Schubert   /* Something to do?  */
8085796c8dcSSimon Schubert   if (offset + len == 0)
8095796c8dcSSimon Schubert     return;
8105796c8dcSSimon Schubert   /* Read (when needed) ... */
8115796c8dcSSimon Schubert   if (in != NULL
8125796c8dcSSimon Schubert       || offset > 0
8135796c8dcSSimon Schubert       || offset + len < descr->sizeof_register[regnum])
8145796c8dcSSimon Schubert     {
8155796c8dcSSimon Schubert       gdb_assert (read != NULL);
8165796c8dcSSimon Schubert       read (regcache, regnum, reg);
8175796c8dcSSimon Schubert     }
8185796c8dcSSimon Schubert   /* ... modify ... */
8195796c8dcSSimon Schubert   if (in != NULL)
8205796c8dcSSimon Schubert     memcpy (in, reg + offset, len);
8215796c8dcSSimon Schubert   if (out != NULL)
8225796c8dcSSimon Schubert     memcpy (reg + offset, out, len);
8235796c8dcSSimon Schubert   /* ... write (when needed).  */
8245796c8dcSSimon Schubert   if (out != NULL)
8255796c8dcSSimon Schubert     {
8265796c8dcSSimon Schubert       gdb_assert (write != NULL);
8275796c8dcSSimon Schubert       write (regcache, regnum, reg);
8285796c8dcSSimon Schubert     }
8295796c8dcSSimon Schubert }
8305796c8dcSSimon Schubert 
8315796c8dcSSimon Schubert void
8325796c8dcSSimon Schubert regcache_raw_read_part (struct regcache *regcache, int regnum,
8335796c8dcSSimon Schubert 			int offset, int len, gdb_byte *buf)
8345796c8dcSSimon Schubert {
8355796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
836*cf7f2e2dSJohn Marino 
8375796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
8385796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
8395796c8dcSSimon Schubert 		      regcache_raw_read, regcache_raw_write);
8405796c8dcSSimon Schubert }
8415796c8dcSSimon Schubert 
8425796c8dcSSimon Schubert void
8435796c8dcSSimon Schubert regcache_raw_write_part (struct regcache *regcache, int regnum,
8445796c8dcSSimon Schubert 			 int offset, int len, const gdb_byte *buf)
8455796c8dcSSimon Schubert {
8465796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
847*cf7f2e2dSJohn Marino 
8485796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
8495796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
8505796c8dcSSimon Schubert 		      regcache_raw_read, regcache_raw_write);
8515796c8dcSSimon Schubert }
8525796c8dcSSimon Schubert 
8535796c8dcSSimon Schubert void
8545796c8dcSSimon Schubert regcache_cooked_read_part (struct regcache *regcache, int regnum,
8555796c8dcSSimon Schubert 			   int offset, int len, gdb_byte *buf)
8565796c8dcSSimon Schubert {
8575796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
858*cf7f2e2dSJohn Marino 
8595796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
8605796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
8615796c8dcSSimon Schubert 		      regcache_cooked_read, regcache_cooked_write);
8625796c8dcSSimon Schubert }
8635796c8dcSSimon Schubert 
8645796c8dcSSimon Schubert void
8655796c8dcSSimon Schubert regcache_cooked_write_part (struct regcache *regcache, int regnum,
8665796c8dcSSimon Schubert 			    int offset, int len, const gdb_byte *buf)
8675796c8dcSSimon Schubert {
8685796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
869*cf7f2e2dSJohn Marino 
8705796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
8715796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
8725796c8dcSSimon Schubert 		      regcache_cooked_read, regcache_cooked_write);
8735796c8dcSSimon Schubert }
8745796c8dcSSimon Schubert 
8755796c8dcSSimon Schubert /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
8765796c8dcSSimon Schubert 
8775796c8dcSSimon Schubert void
8785796c8dcSSimon Schubert regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
8795796c8dcSSimon Schubert {
8805796c8dcSSimon Schubert   void *regbuf;
8815796c8dcSSimon Schubert   size_t size;
8825796c8dcSSimon Schubert 
8835796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
8845796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
8855796c8dcSSimon Schubert   gdb_assert (!regcache->readonly_p);
8865796c8dcSSimon Schubert 
8875796c8dcSSimon Schubert   regbuf = register_buffer (regcache, regnum);
8885796c8dcSSimon Schubert   size = regcache->descr->sizeof_register[regnum];
8895796c8dcSSimon Schubert 
8905796c8dcSSimon Schubert   if (buf)
8915796c8dcSSimon Schubert     memcpy (regbuf, buf, size);
8925796c8dcSSimon Schubert   else
8935796c8dcSSimon Schubert     memset (regbuf, 0, size);
8945796c8dcSSimon Schubert 
8955796c8dcSSimon Schubert   /* Mark the register as cached.  */
8965796c8dcSSimon Schubert   regcache->register_valid_p[regnum] = 1;
8975796c8dcSSimon Schubert }
8985796c8dcSSimon Schubert 
8995796c8dcSSimon Schubert /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
9005796c8dcSSimon Schubert 
9015796c8dcSSimon Schubert void
9025796c8dcSSimon Schubert regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
9035796c8dcSSimon Schubert {
9045796c8dcSSimon Schubert   const void *regbuf;
9055796c8dcSSimon Schubert   size_t size;
9065796c8dcSSimon Schubert 
9075796c8dcSSimon Schubert   gdb_assert (regcache != NULL && buf != NULL);
9085796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
9095796c8dcSSimon Schubert 
9105796c8dcSSimon Schubert   regbuf = register_buffer (regcache, regnum);
9115796c8dcSSimon Schubert   size = regcache->descr->sizeof_register[regnum];
9125796c8dcSSimon Schubert   memcpy (buf, regbuf, size);
9135796c8dcSSimon Schubert }
9145796c8dcSSimon Schubert 
9155796c8dcSSimon Schubert 
9165796c8dcSSimon Schubert /* Special handling for register PC.  */
9175796c8dcSSimon Schubert 
9185796c8dcSSimon Schubert CORE_ADDR
9195796c8dcSSimon Schubert regcache_read_pc (struct regcache *regcache)
9205796c8dcSSimon Schubert {
9215796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
9225796c8dcSSimon Schubert 
9235796c8dcSSimon Schubert   CORE_ADDR pc_val;
9245796c8dcSSimon Schubert 
9255796c8dcSSimon Schubert   if (gdbarch_read_pc_p (gdbarch))
9265796c8dcSSimon Schubert     pc_val = gdbarch_read_pc (gdbarch, regcache);
9275796c8dcSSimon Schubert   /* Else use per-frame method on get_current_frame.  */
9285796c8dcSSimon Schubert   else if (gdbarch_pc_regnum (gdbarch) >= 0)
9295796c8dcSSimon Schubert     {
9305796c8dcSSimon Schubert       ULONGEST raw_val;
931*cf7f2e2dSJohn Marino 
9325796c8dcSSimon Schubert       regcache_cooked_read_unsigned (regcache,
9335796c8dcSSimon Schubert 				     gdbarch_pc_regnum (gdbarch),
9345796c8dcSSimon Schubert 				     &raw_val);
9355796c8dcSSimon Schubert       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
9365796c8dcSSimon Schubert     }
9375796c8dcSSimon Schubert   else
9385796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
9395796c8dcSSimon Schubert 		    _("regcache_read_pc: Unable to find PC"));
9405796c8dcSSimon Schubert   return pc_val;
9415796c8dcSSimon Schubert }
9425796c8dcSSimon Schubert 
9435796c8dcSSimon Schubert void
9445796c8dcSSimon Schubert regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
9455796c8dcSSimon Schubert {
9465796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
9475796c8dcSSimon Schubert 
9485796c8dcSSimon Schubert   if (gdbarch_write_pc_p (gdbarch))
9495796c8dcSSimon Schubert     gdbarch_write_pc (gdbarch, regcache, pc);
9505796c8dcSSimon Schubert   else if (gdbarch_pc_regnum (gdbarch) >= 0)
9515796c8dcSSimon Schubert     regcache_cooked_write_unsigned (regcache,
9525796c8dcSSimon Schubert 				    gdbarch_pc_regnum (gdbarch), pc);
9535796c8dcSSimon Schubert   else
9545796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
9555796c8dcSSimon Schubert 		    _("regcache_write_pc: Unable to update PC"));
9565796c8dcSSimon Schubert 
9575796c8dcSSimon Schubert   /* Writing the PC (for instance, from "load") invalidates the
9585796c8dcSSimon Schubert      current frame.  */
9595796c8dcSSimon Schubert   reinit_frame_cache ();
9605796c8dcSSimon Schubert }
9615796c8dcSSimon Schubert 
9625796c8dcSSimon Schubert 
9635796c8dcSSimon Schubert static void
9645796c8dcSSimon Schubert reg_flush_command (char *command, int from_tty)
9655796c8dcSSimon Schubert {
9665796c8dcSSimon Schubert   /* Force-flush the register cache.  */
9675796c8dcSSimon Schubert   registers_changed ();
9685796c8dcSSimon Schubert   if (from_tty)
9695796c8dcSSimon Schubert     printf_filtered (_("Register cache flushed.\n"));
9705796c8dcSSimon Schubert }
9715796c8dcSSimon Schubert 
9725796c8dcSSimon Schubert static void
9735796c8dcSSimon Schubert dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
9745796c8dcSSimon Schubert 		   const unsigned char *buf, long len)
9755796c8dcSSimon Schubert {
9765796c8dcSSimon Schubert   int i;
977*cf7f2e2dSJohn Marino 
9785796c8dcSSimon Schubert   switch (endian)
9795796c8dcSSimon Schubert     {
9805796c8dcSSimon Schubert     case BFD_ENDIAN_BIG:
9815796c8dcSSimon Schubert       for (i = 0; i < len; i++)
9825796c8dcSSimon Schubert 	fprintf_unfiltered (file, "%02x", buf[i]);
9835796c8dcSSimon Schubert       break;
9845796c8dcSSimon Schubert     case BFD_ENDIAN_LITTLE:
9855796c8dcSSimon Schubert       for (i = len - 1; i >= 0; i--)
9865796c8dcSSimon Schubert 	fprintf_unfiltered (file, "%02x", buf[i]);
9875796c8dcSSimon Schubert       break;
9885796c8dcSSimon Schubert     default:
9895796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("Bad switch"));
9905796c8dcSSimon Schubert     }
9915796c8dcSSimon Schubert }
9925796c8dcSSimon Schubert 
9935796c8dcSSimon Schubert enum regcache_dump_what
9945796c8dcSSimon Schubert {
9955796c8dcSSimon Schubert   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
9965796c8dcSSimon Schubert };
9975796c8dcSSimon Schubert 
9985796c8dcSSimon Schubert static void
9995796c8dcSSimon Schubert regcache_dump (struct regcache *regcache, struct ui_file *file,
10005796c8dcSSimon Schubert 	       enum regcache_dump_what what_to_dump)
10015796c8dcSSimon Schubert {
10025796c8dcSSimon Schubert   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
10035796c8dcSSimon Schubert   struct gdbarch *gdbarch = regcache->descr->gdbarch;
10045796c8dcSSimon Schubert   int regnum;
10055796c8dcSSimon Schubert   int footnote_nr = 0;
10065796c8dcSSimon Schubert   int footnote_register_size = 0;
10075796c8dcSSimon Schubert   int footnote_register_offset = 0;
10085796c8dcSSimon Schubert   int footnote_register_type_name_null = 0;
10095796c8dcSSimon Schubert   long register_offset = 0;
10105796c8dcSSimon Schubert   unsigned char buf[MAX_REGISTER_SIZE];
10115796c8dcSSimon Schubert 
10125796c8dcSSimon Schubert #if 0
10135796c8dcSSimon Schubert   fprintf_unfiltered (file, "nr_raw_registers %d\n",
10145796c8dcSSimon Schubert 		      regcache->descr->nr_raw_registers);
10155796c8dcSSimon Schubert   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
10165796c8dcSSimon Schubert 		      regcache->descr->nr_cooked_registers);
10175796c8dcSSimon Schubert   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
10185796c8dcSSimon Schubert 		      regcache->descr->sizeof_raw_registers);
10195796c8dcSSimon Schubert   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
10205796c8dcSSimon Schubert 		      regcache->descr->sizeof_raw_register_valid_p);
10215796c8dcSSimon Schubert   fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
10225796c8dcSSimon Schubert 		      gdbarch_num_regs (gdbarch));
10235796c8dcSSimon Schubert   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
10245796c8dcSSimon Schubert 		      gdbarch_num_pseudo_regs (gdbarch));
10255796c8dcSSimon Schubert #endif
10265796c8dcSSimon Schubert 
10275796c8dcSSimon Schubert   gdb_assert (regcache->descr->nr_cooked_registers
10285796c8dcSSimon Schubert 	      == (gdbarch_num_regs (gdbarch)
10295796c8dcSSimon Schubert 		  + gdbarch_num_pseudo_regs (gdbarch)));
10305796c8dcSSimon Schubert 
10315796c8dcSSimon Schubert   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
10325796c8dcSSimon Schubert     {
10335796c8dcSSimon Schubert       /* Name.  */
10345796c8dcSSimon Schubert       if (regnum < 0)
10355796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %-10s", "Name");
10365796c8dcSSimon Schubert       else
10375796c8dcSSimon Schubert 	{
10385796c8dcSSimon Schubert 	  const char *p = gdbarch_register_name (gdbarch, regnum);
1039*cf7f2e2dSJohn Marino 
10405796c8dcSSimon Schubert 	  if (p == NULL)
10415796c8dcSSimon Schubert 	    p = "";
10425796c8dcSSimon Schubert 	  else if (p[0] == '\0')
10435796c8dcSSimon Schubert 	    p = "''";
10445796c8dcSSimon Schubert 	  fprintf_unfiltered (file, " %-10s", p);
10455796c8dcSSimon Schubert 	}
10465796c8dcSSimon Schubert 
10475796c8dcSSimon Schubert       /* Number.  */
10485796c8dcSSimon Schubert       if (regnum < 0)
10495796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4s", "Nr");
10505796c8dcSSimon Schubert       else
10515796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4d", regnum);
10525796c8dcSSimon Schubert 
10535796c8dcSSimon Schubert       /* Relative number.  */
10545796c8dcSSimon Schubert       if (regnum < 0)
10555796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4s", "Rel");
10565796c8dcSSimon Schubert       else if (regnum < gdbarch_num_regs (gdbarch))
10575796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4d", regnum);
10585796c8dcSSimon Schubert       else
10595796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4d",
10605796c8dcSSimon Schubert 			    (regnum - gdbarch_num_regs (gdbarch)));
10615796c8dcSSimon Schubert 
10625796c8dcSSimon Schubert       /* Offset.  */
10635796c8dcSSimon Schubert       if (regnum < 0)
10645796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %6s  ", "Offset");
10655796c8dcSSimon Schubert       else
10665796c8dcSSimon Schubert 	{
10675796c8dcSSimon Schubert 	  fprintf_unfiltered (file, " %6ld",
10685796c8dcSSimon Schubert 			      regcache->descr->register_offset[regnum]);
10695796c8dcSSimon Schubert 	  if (register_offset != regcache->descr->register_offset[regnum]
10705796c8dcSSimon Schubert 	      || (regnum > 0
10715796c8dcSSimon Schubert 		  && (regcache->descr->register_offset[regnum]
10725796c8dcSSimon Schubert 		      != (regcache->descr->register_offset[regnum - 1]
10735796c8dcSSimon Schubert 			  + regcache->descr->sizeof_register[regnum - 1])))
10745796c8dcSSimon Schubert 	      )
10755796c8dcSSimon Schubert 	    {
10765796c8dcSSimon Schubert 	      if (!footnote_register_offset)
10775796c8dcSSimon Schubert 		footnote_register_offset = ++footnote_nr;
10785796c8dcSSimon Schubert 	      fprintf_unfiltered (file, "*%d", footnote_register_offset);
10795796c8dcSSimon Schubert 	    }
10805796c8dcSSimon Schubert 	  else
10815796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "  ");
10825796c8dcSSimon Schubert 	  register_offset = (regcache->descr->register_offset[regnum]
10835796c8dcSSimon Schubert 			     + regcache->descr->sizeof_register[regnum]);
10845796c8dcSSimon Schubert 	}
10855796c8dcSSimon Schubert 
10865796c8dcSSimon Schubert       /* Size.  */
10875796c8dcSSimon Schubert       if (regnum < 0)
10885796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %5s ", "Size");
10895796c8dcSSimon Schubert       else
10905796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %5ld",
10915796c8dcSSimon Schubert 			    regcache->descr->sizeof_register[regnum]);
10925796c8dcSSimon Schubert 
10935796c8dcSSimon Schubert       /* Type.  */
10945796c8dcSSimon Schubert       {
10955796c8dcSSimon Schubert 	const char *t;
1096*cf7f2e2dSJohn Marino 
10975796c8dcSSimon Schubert 	if (regnum < 0)
10985796c8dcSSimon Schubert 	  t = "Type";
10995796c8dcSSimon Schubert 	else
11005796c8dcSSimon Schubert 	  {
11015796c8dcSSimon Schubert 	    static const char blt[] = "builtin_type";
1102*cf7f2e2dSJohn Marino 
11035796c8dcSSimon Schubert 	    t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
11045796c8dcSSimon Schubert 	    if (t == NULL)
11055796c8dcSSimon Schubert 	      {
11065796c8dcSSimon Schubert 		char *n;
1107*cf7f2e2dSJohn Marino 
11085796c8dcSSimon Schubert 		if (!footnote_register_type_name_null)
11095796c8dcSSimon Schubert 		  footnote_register_type_name_null = ++footnote_nr;
11105796c8dcSSimon Schubert 		n = xstrprintf ("*%d", footnote_register_type_name_null);
11115796c8dcSSimon Schubert 		make_cleanup (xfree, n);
11125796c8dcSSimon Schubert 		t = n;
11135796c8dcSSimon Schubert 	      }
11145796c8dcSSimon Schubert 	    /* Chop a leading builtin_type.  */
11155796c8dcSSimon Schubert 	    if (strncmp (t, blt, strlen (blt)) == 0)
11165796c8dcSSimon Schubert 	      t += strlen (blt);
11175796c8dcSSimon Schubert 	  }
11185796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %-15s", t);
11195796c8dcSSimon Schubert       }
11205796c8dcSSimon Schubert 
11215796c8dcSSimon Schubert       /* Leading space always present.  */
11225796c8dcSSimon Schubert       fprintf_unfiltered (file, " ");
11235796c8dcSSimon Schubert 
11245796c8dcSSimon Schubert       /* Value, raw.  */
11255796c8dcSSimon Schubert       if (what_to_dump == regcache_dump_raw)
11265796c8dcSSimon Schubert 	{
11275796c8dcSSimon Schubert 	  if (regnum < 0)
11285796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "Raw value");
11295796c8dcSSimon Schubert 	  else if (regnum >= regcache->descr->nr_raw_registers)
11305796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "<cooked>");
11315796c8dcSSimon Schubert 	  else if (!regcache_valid_p (regcache, regnum))
11325796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "<invalid>");
11335796c8dcSSimon Schubert 	  else
11345796c8dcSSimon Schubert 	    {
11355796c8dcSSimon Schubert 	      regcache_raw_read (regcache, regnum, buf);
11365796c8dcSSimon Schubert 	      fprintf_unfiltered (file, "0x");
11375796c8dcSSimon Schubert 	      dump_endian_bytes (file,
11385796c8dcSSimon Schubert 				 gdbarch_byte_order (gdbarch), buf,
11395796c8dcSSimon Schubert 				 regcache->descr->sizeof_register[regnum]);
11405796c8dcSSimon Schubert 	    }
11415796c8dcSSimon Schubert 	}
11425796c8dcSSimon Schubert 
11435796c8dcSSimon Schubert       /* Value, cooked.  */
11445796c8dcSSimon Schubert       if (what_to_dump == regcache_dump_cooked)
11455796c8dcSSimon Schubert 	{
11465796c8dcSSimon Schubert 	  if (regnum < 0)
11475796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "Cooked value");
11485796c8dcSSimon Schubert 	  else
11495796c8dcSSimon Schubert 	    {
11505796c8dcSSimon Schubert 	      regcache_cooked_read (regcache, regnum, buf);
11515796c8dcSSimon Schubert 	      fprintf_unfiltered (file, "0x");
11525796c8dcSSimon Schubert 	      dump_endian_bytes (file,
11535796c8dcSSimon Schubert 				 gdbarch_byte_order (gdbarch), buf,
11545796c8dcSSimon Schubert 				 regcache->descr->sizeof_register[regnum]);
11555796c8dcSSimon Schubert 	    }
11565796c8dcSSimon Schubert 	}
11575796c8dcSSimon Schubert 
11585796c8dcSSimon Schubert       /* Group members.  */
11595796c8dcSSimon Schubert       if (what_to_dump == regcache_dump_groups)
11605796c8dcSSimon Schubert 	{
11615796c8dcSSimon Schubert 	  if (regnum < 0)
11625796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "Groups");
11635796c8dcSSimon Schubert 	  else
11645796c8dcSSimon Schubert 	    {
11655796c8dcSSimon Schubert 	      const char *sep = "";
11665796c8dcSSimon Schubert 	      struct reggroup *group;
1167*cf7f2e2dSJohn Marino 
11685796c8dcSSimon Schubert 	      for (group = reggroup_next (gdbarch, NULL);
11695796c8dcSSimon Schubert 		   group != NULL;
11705796c8dcSSimon Schubert 		   group = reggroup_next (gdbarch, group))
11715796c8dcSSimon Schubert 		{
11725796c8dcSSimon Schubert 		  if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
11735796c8dcSSimon Schubert 		    {
11745796c8dcSSimon Schubert 		      fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
11755796c8dcSSimon Schubert 		      sep = ",";
11765796c8dcSSimon Schubert 		    }
11775796c8dcSSimon Schubert 		}
11785796c8dcSSimon Schubert 	    }
11795796c8dcSSimon Schubert 	}
11805796c8dcSSimon Schubert 
11815796c8dcSSimon Schubert       fprintf_unfiltered (file, "\n");
11825796c8dcSSimon Schubert     }
11835796c8dcSSimon Schubert 
11845796c8dcSSimon Schubert   if (footnote_register_size)
11855796c8dcSSimon Schubert     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
11865796c8dcSSimon Schubert 			footnote_register_size);
11875796c8dcSSimon Schubert   if (footnote_register_offset)
11885796c8dcSSimon Schubert     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
11895796c8dcSSimon Schubert 			footnote_register_offset);
11905796c8dcSSimon Schubert   if (footnote_register_type_name_null)
11915796c8dcSSimon Schubert     fprintf_unfiltered (file,
11925796c8dcSSimon Schubert 			"*%d: Register type's name NULL.\n",
11935796c8dcSSimon Schubert 			footnote_register_type_name_null);
11945796c8dcSSimon Schubert   do_cleanups (cleanups);
11955796c8dcSSimon Schubert }
11965796c8dcSSimon Schubert 
11975796c8dcSSimon Schubert static void
11985796c8dcSSimon Schubert regcache_print (char *args, enum regcache_dump_what what_to_dump)
11995796c8dcSSimon Schubert {
12005796c8dcSSimon Schubert   if (args == NULL)
12015796c8dcSSimon Schubert     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
12025796c8dcSSimon Schubert   else
12035796c8dcSSimon Schubert     {
12045796c8dcSSimon Schubert       struct cleanup *cleanups;
12055796c8dcSSimon Schubert       struct ui_file *file = gdb_fopen (args, "w");
1206*cf7f2e2dSJohn Marino 
12075796c8dcSSimon Schubert       if (file == NULL)
12085796c8dcSSimon Schubert 	perror_with_name (_("maintenance print architecture"));
12095796c8dcSSimon Schubert       cleanups = make_cleanup_ui_file_delete (file);
12105796c8dcSSimon Schubert       regcache_dump (get_current_regcache (), file, what_to_dump);
12115796c8dcSSimon Schubert       do_cleanups (cleanups);
12125796c8dcSSimon Schubert     }
12135796c8dcSSimon Schubert }
12145796c8dcSSimon Schubert 
12155796c8dcSSimon Schubert static void
12165796c8dcSSimon Schubert maintenance_print_registers (char *args, int from_tty)
12175796c8dcSSimon Schubert {
12185796c8dcSSimon Schubert   regcache_print (args, regcache_dump_none);
12195796c8dcSSimon Schubert }
12205796c8dcSSimon Schubert 
12215796c8dcSSimon Schubert static void
12225796c8dcSSimon Schubert maintenance_print_raw_registers (char *args, int from_tty)
12235796c8dcSSimon Schubert {
12245796c8dcSSimon Schubert   regcache_print (args, regcache_dump_raw);
12255796c8dcSSimon Schubert }
12265796c8dcSSimon Schubert 
12275796c8dcSSimon Schubert static void
12285796c8dcSSimon Schubert maintenance_print_cooked_registers (char *args, int from_tty)
12295796c8dcSSimon Schubert {
12305796c8dcSSimon Schubert   regcache_print (args, regcache_dump_cooked);
12315796c8dcSSimon Schubert }
12325796c8dcSSimon Schubert 
12335796c8dcSSimon Schubert static void
12345796c8dcSSimon Schubert maintenance_print_register_groups (char *args, int from_tty)
12355796c8dcSSimon Schubert {
12365796c8dcSSimon Schubert   regcache_print (args, regcache_dump_groups);
12375796c8dcSSimon Schubert }
12385796c8dcSSimon Schubert 
12395796c8dcSSimon Schubert extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
12405796c8dcSSimon Schubert 
12415796c8dcSSimon Schubert void
12425796c8dcSSimon Schubert _initialize_regcache (void)
12435796c8dcSSimon Schubert {
12445796c8dcSSimon Schubert   regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
12455796c8dcSSimon Schubert 
12465796c8dcSSimon Schubert   observer_attach_target_changed (regcache_observer_target_changed);
12475796c8dcSSimon Schubert   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
12485796c8dcSSimon Schubert 
12495796c8dcSSimon Schubert   add_com ("flushregs", class_maintenance, reg_flush_command,
12505796c8dcSSimon Schubert 	   _("Force gdb to flush its register cache (maintainer command)"));
12515796c8dcSSimon Schubert 
12525796c8dcSSimon Schubert   add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
12535796c8dcSSimon Schubert Print the internal register configuration.\n\
12545796c8dcSSimon Schubert Takes an optional file parameter."), &maintenanceprintlist);
12555796c8dcSSimon Schubert   add_cmd ("raw-registers", class_maintenance,
12565796c8dcSSimon Schubert 	   maintenance_print_raw_registers, _("\
12575796c8dcSSimon Schubert Print the internal register configuration including raw values.\n\
12585796c8dcSSimon Schubert Takes an optional file parameter."), &maintenanceprintlist);
12595796c8dcSSimon Schubert   add_cmd ("cooked-registers", class_maintenance,
12605796c8dcSSimon Schubert 	   maintenance_print_cooked_registers, _("\
12615796c8dcSSimon Schubert Print the internal register configuration including cooked values.\n\
12625796c8dcSSimon Schubert Takes an optional file parameter."), &maintenanceprintlist);
12635796c8dcSSimon Schubert   add_cmd ("register-groups", class_maintenance,
12645796c8dcSSimon Schubert 	   maintenance_print_register_groups, _("\
12655796c8dcSSimon Schubert Print the internal register configuration including each register's group.\n\
12665796c8dcSSimon Schubert Takes an optional file parameter."),
12675796c8dcSSimon Schubert 	   &maintenanceprintlist);
12685796c8dcSSimon Schubert 
12695796c8dcSSimon Schubert }
1270