xref: /dflybsd-src/contrib/gdb-7/gdb/regcache.c (revision 5796c8dc12c637f18a1740c26afd8d40ffa9b719)
1*5796c8dcSSimon Schubert /* Cache and manage the values of registers for GDB, the GNU debugger.
2*5796c8dcSSimon Schubert 
3*5796c8dcSSimon Schubert    Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4*5796c8dcSSimon Schubert    2002, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
5*5796c8dcSSimon Schubert 
6*5796c8dcSSimon Schubert    This file is part of GDB.
7*5796c8dcSSimon Schubert 
8*5796c8dcSSimon Schubert    This program is free software; you can redistribute it and/or modify
9*5796c8dcSSimon Schubert    it under the terms of the GNU General Public License as published by
10*5796c8dcSSimon Schubert    the Free Software Foundation; either version 3 of the License, or
11*5796c8dcSSimon Schubert    (at your option) any later version.
12*5796c8dcSSimon Schubert 
13*5796c8dcSSimon Schubert    This program is distributed in the hope that it will be useful,
14*5796c8dcSSimon Schubert    but WITHOUT ANY WARRANTY; without even the implied warranty of
15*5796c8dcSSimon Schubert    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16*5796c8dcSSimon Schubert    GNU General Public License for more details.
17*5796c8dcSSimon Schubert 
18*5796c8dcSSimon Schubert    You should have received a copy of the GNU General Public License
19*5796c8dcSSimon Schubert    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
20*5796c8dcSSimon Schubert 
21*5796c8dcSSimon Schubert #include "defs.h"
22*5796c8dcSSimon Schubert #include "inferior.h"
23*5796c8dcSSimon Schubert #include "target.h"
24*5796c8dcSSimon Schubert #include "gdbarch.h"
25*5796c8dcSSimon Schubert #include "gdbcmd.h"
26*5796c8dcSSimon Schubert #include "regcache.h"
27*5796c8dcSSimon Schubert #include "reggroups.h"
28*5796c8dcSSimon Schubert #include "gdb_assert.h"
29*5796c8dcSSimon Schubert #include "gdb_string.h"
30*5796c8dcSSimon Schubert #include "gdbcmd.h"		/* For maintenanceprintlist.  */
31*5796c8dcSSimon Schubert #include "observer.h"
32*5796c8dcSSimon Schubert 
33*5796c8dcSSimon Schubert /*
34*5796c8dcSSimon Schubert  * DATA STRUCTURE
35*5796c8dcSSimon Schubert  *
36*5796c8dcSSimon Schubert  * Here is the actual register cache.
37*5796c8dcSSimon Schubert  */
38*5796c8dcSSimon Schubert 
39*5796c8dcSSimon Schubert /* Per-architecture object describing the layout of a register cache.
40*5796c8dcSSimon Schubert    Computed once when the architecture is created */
41*5796c8dcSSimon Schubert 
42*5796c8dcSSimon Schubert struct gdbarch_data *regcache_descr_handle;
43*5796c8dcSSimon Schubert 
44*5796c8dcSSimon Schubert struct regcache_descr
45*5796c8dcSSimon Schubert {
46*5796c8dcSSimon Schubert   /* The architecture this descriptor belongs to.  */
47*5796c8dcSSimon Schubert   struct gdbarch *gdbarch;
48*5796c8dcSSimon Schubert 
49*5796c8dcSSimon Schubert   /* The raw register cache.  Each raw (or hard) register is supplied
50*5796c8dcSSimon Schubert      by the target interface.  The raw cache should not contain
51*5796c8dcSSimon Schubert      redundant information - if the PC is constructed from two
52*5796c8dcSSimon Schubert      registers then those registers and not the PC lives in the raw
53*5796c8dcSSimon Schubert      cache.  */
54*5796c8dcSSimon Schubert   int nr_raw_registers;
55*5796c8dcSSimon Schubert   long sizeof_raw_registers;
56*5796c8dcSSimon Schubert   long sizeof_raw_register_valid_p;
57*5796c8dcSSimon Schubert 
58*5796c8dcSSimon Schubert   /* The cooked register space.  Each cooked register in the range
59*5796c8dcSSimon Schubert      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
60*5796c8dcSSimon Schubert      register.  The remaining [NR_RAW_REGISTERS
61*5796c8dcSSimon Schubert      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
62*5796c8dcSSimon Schubert      both raw registers and memory by the architecture methods
63*5796c8dcSSimon Schubert      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
64*5796c8dcSSimon Schubert   int nr_cooked_registers;
65*5796c8dcSSimon Schubert   long sizeof_cooked_registers;
66*5796c8dcSSimon Schubert   long sizeof_cooked_register_valid_p;
67*5796c8dcSSimon Schubert 
68*5796c8dcSSimon Schubert   /* Offset and size (in 8 bit bytes), of reach register in the
69*5796c8dcSSimon Schubert      register cache.  All registers (including those in the range
70*5796c8dcSSimon Schubert      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
71*5796c8dcSSimon Schubert      Assigning all registers an offset makes it possible to keep
72*5796c8dcSSimon Schubert      legacy code, such as that found in read_register_bytes() and
73*5796c8dcSSimon Schubert      write_register_bytes() working.  */
74*5796c8dcSSimon Schubert   long *register_offset;
75*5796c8dcSSimon Schubert   long *sizeof_register;
76*5796c8dcSSimon Schubert 
77*5796c8dcSSimon Schubert   /* Cached table containing the type of each register.  */
78*5796c8dcSSimon Schubert   struct type **register_type;
79*5796c8dcSSimon Schubert };
80*5796c8dcSSimon Schubert 
81*5796c8dcSSimon Schubert static void *
82*5796c8dcSSimon Schubert init_regcache_descr (struct gdbarch *gdbarch)
83*5796c8dcSSimon Schubert {
84*5796c8dcSSimon Schubert   int i;
85*5796c8dcSSimon Schubert   struct regcache_descr *descr;
86*5796c8dcSSimon Schubert   gdb_assert (gdbarch != NULL);
87*5796c8dcSSimon Schubert 
88*5796c8dcSSimon Schubert   /* Create an initial, zero filled, table.  */
89*5796c8dcSSimon Schubert   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
90*5796c8dcSSimon Schubert   descr->gdbarch = gdbarch;
91*5796c8dcSSimon Schubert 
92*5796c8dcSSimon Schubert   /* Total size of the register space.  The raw registers are mapped
93*5796c8dcSSimon Schubert      directly onto the raw register cache while the pseudo's are
94*5796c8dcSSimon Schubert      either mapped onto raw-registers or memory.  */
95*5796c8dcSSimon Schubert   descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
96*5796c8dcSSimon Schubert 			       + gdbarch_num_pseudo_regs (gdbarch);
97*5796c8dcSSimon Schubert   descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (gdbarch)
98*5796c8dcSSimon Schubert 					  + gdbarch_num_pseudo_regs
99*5796c8dcSSimon Schubert 					      (gdbarch);
100*5796c8dcSSimon Schubert 
101*5796c8dcSSimon Schubert   /* Fill in a table of register types.  */
102*5796c8dcSSimon Schubert   descr->register_type
103*5796c8dcSSimon Schubert     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
104*5796c8dcSSimon Schubert   for (i = 0; i < descr->nr_cooked_registers; i++)
105*5796c8dcSSimon Schubert     descr->register_type[i] = gdbarch_register_type (gdbarch, i);
106*5796c8dcSSimon Schubert 
107*5796c8dcSSimon Schubert   /* Construct a strictly RAW register cache.  Don't allow pseudo's
108*5796c8dcSSimon Schubert      into the register cache.  */
109*5796c8dcSSimon Schubert   descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
110*5796c8dcSSimon Schubert 
111*5796c8dcSSimon Schubert   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
112*5796c8dcSSimon Schubert      array.  This pretects GDB from erant code that accesses elements
113*5796c8dcSSimon Schubert      of the global register_valid_p[] array in the range
114*5796c8dcSSimon Schubert      [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs).  */
115*5796c8dcSSimon Schubert   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
116*5796c8dcSSimon Schubert 
117*5796c8dcSSimon Schubert   /* Lay out the register cache.
118*5796c8dcSSimon Schubert 
119*5796c8dcSSimon Schubert      NOTE: cagney/2002-05-22: Only register_type() is used when
120*5796c8dcSSimon Schubert      constructing the register cache.  It is assumed that the
121*5796c8dcSSimon Schubert      register's raw size, virtual size and type length are all the
122*5796c8dcSSimon Schubert      same.  */
123*5796c8dcSSimon Schubert 
124*5796c8dcSSimon Schubert   {
125*5796c8dcSSimon Schubert     long offset = 0;
126*5796c8dcSSimon Schubert     descr->sizeof_register
127*5796c8dcSSimon Schubert       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
128*5796c8dcSSimon Schubert     descr->register_offset
129*5796c8dcSSimon Schubert       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
130*5796c8dcSSimon Schubert     for (i = 0; i < descr->nr_cooked_registers; i++)
131*5796c8dcSSimon Schubert       {
132*5796c8dcSSimon Schubert 	descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
133*5796c8dcSSimon Schubert 	descr->register_offset[i] = offset;
134*5796c8dcSSimon Schubert 	offset += descr->sizeof_register[i];
135*5796c8dcSSimon Schubert 	gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
136*5796c8dcSSimon Schubert       }
137*5796c8dcSSimon Schubert     /* Set the real size of the register cache buffer.  */
138*5796c8dcSSimon Schubert     descr->sizeof_cooked_registers = offset;
139*5796c8dcSSimon Schubert   }
140*5796c8dcSSimon Schubert 
141*5796c8dcSSimon Schubert   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
142*5796c8dcSSimon Schubert      the raw registers.  Unfortunately some code still accesses the
143*5796c8dcSSimon Schubert      register array directly using the global registers[].  Until that
144*5796c8dcSSimon Schubert      code has been purged, play safe and over allocating the register
145*5796c8dcSSimon Schubert      buffer.  Ulgh!  */
146*5796c8dcSSimon Schubert   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
147*5796c8dcSSimon Schubert 
148*5796c8dcSSimon Schubert   return descr;
149*5796c8dcSSimon Schubert }
150*5796c8dcSSimon Schubert 
151*5796c8dcSSimon Schubert static struct regcache_descr *
152*5796c8dcSSimon Schubert regcache_descr (struct gdbarch *gdbarch)
153*5796c8dcSSimon Schubert {
154*5796c8dcSSimon Schubert   return gdbarch_data (gdbarch, regcache_descr_handle);
155*5796c8dcSSimon Schubert }
156*5796c8dcSSimon Schubert 
157*5796c8dcSSimon Schubert /* Utility functions returning useful register attributes stored in
158*5796c8dcSSimon Schubert    the regcache descr.  */
159*5796c8dcSSimon Schubert 
160*5796c8dcSSimon Schubert struct type *
161*5796c8dcSSimon Schubert register_type (struct gdbarch *gdbarch, int regnum)
162*5796c8dcSSimon Schubert {
163*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache_descr (gdbarch);
164*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
165*5796c8dcSSimon Schubert   return descr->register_type[regnum];
166*5796c8dcSSimon Schubert }
167*5796c8dcSSimon Schubert 
168*5796c8dcSSimon Schubert /* Utility functions returning useful register attributes stored in
169*5796c8dcSSimon Schubert    the regcache descr.  */
170*5796c8dcSSimon Schubert 
171*5796c8dcSSimon Schubert int
172*5796c8dcSSimon Schubert register_size (struct gdbarch *gdbarch, int regnum)
173*5796c8dcSSimon Schubert {
174*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache_descr (gdbarch);
175*5796c8dcSSimon Schubert   int size;
176*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0
177*5796c8dcSSimon Schubert 	      && regnum < (gdbarch_num_regs (gdbarch)
178*5796c8dcSSimon Schubert 			   + gdbarch_num_pseudo_regs (gdbarch)));
179*5796c8dcSSimon Schubert   size = descr->sizeof_register[regnum];
180*5796c8dcSSimon Schubert   return size;
181*5796c8dcSSimon Schubert }
182*5796c8dcSSimon Schubert 
183*5796c8dcSSimon Schubert /* The register cache for storing raw register values.  */
184*5796c8dcSSimon Schubert 
185*5796c8dcSSimon Schubert struct regcache
186*5796c8dcSSimon Schubert {
187*5796c8dcSSimon Schubert   struct regcache_descr *descr;
188*5796c8dcSSimon Schubert   /* The register buffers.  A read-only register cache can hold the
189*5796c8dcSSimon Schubert      full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
190*5796c8dcSSimon Schubert      register cache can only hold [0 .. gdbarch_num_regs).  */
191*5796c8dcSSimon Schubert   gdb_byte *registers;
192*5796c8dcSSimon Schubert   /* Register cache status:
193*5796c8dcSSimon Schubert      register_valid_p[REG] == 0 if REG value is not in the cache
194*5796c8dcSSimon Schubert                             > 0 if REG value is in the cache
195*5796c8dcSSimon Schubert                             < 0 if REG value is permanently unavailable */
196*5796c8dcSSimon Schubert   signed char *register_valid_p;
197*5796c8dcSSimon Schubert   /* Is this a read-only cache?  A read-only cache is used for saving
198*5796c8dcSSimon Schubert      the target's register state (e.g, across an inferior function
199*5796c8dcSSimon Schubert      call or just before forcing a function return).  A read-only
200*5796c8dcSSimon Schubert      cache can only be updated via the methods regcache_dup() and
201*5796c8dcSSimon Schubert      regcache_cpy().  The actual contents are determined by the
202*5796c8dcSSimon Schubert      reggroup_save and reggroup_restore methods.  */
203*5796c8dcSSimon Schubert   int readonly_p;
204*5796c8dcSSimon Schubert   /* If this is a read-write cache, which thread's registers is
205*5796c8dcSSimon Schubert      it connected to?  */
206*5796c8dcSSimon Schubert   ptid_t ptid;
207*5796c8dcSSimon Schubert };
208*5796c8dcSSimon Schubert 
209*5796c8dcSSimon Schubert struct regcache *
210*5796c8dcSSimon Schubert regcache_xmalloc (struct gdbarch *gdbarch)
211*5796c8dcSSimon Schubert {
212*5796c8dcSSimon Schubert   struct regcache_descr *descr;
213*5796c8dcSSimon Schubert   struct regcache *regcache;
214*5796c8dcSSimon Schubert   gdb_assert (gdbarch != NULL);
215*5796c8dcSSimon Schubert   descr = regcache_descr (gdbarch);
216*5796c8dcSSimon Schubert   regcache = XMALLOC (struct regcache);
217*5796c8dcSSimon Schubert   regcache->descr = descr;
218*5796c8dcSSimon Schubert   regcache->registers
219*5796c8dcSSimon Schubert     = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
220*5796c8dcSSimon Schubert   regcache->register_valid_p
221*5796c8dcSSimon Schubert     = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
222*5796c8dcSSimon Schubert   regcache->readonly_p = 1;
223*5796c8dcSSimon Schubert   regcache->ptid = minus_one_ptid;
224*5796c8dcSSimon Schubert   return regcache;
225*5796c8dcSSimon Schubert }
226*5796c8dcSSimon Schubert 
227*5796c8dcSSimon Schubert void
228*5796c8dcSSimon Schubert regcache_xfree (struct regcache *regcache)
229*5796c8dcSSimon Schubert {
230*5796c8dcSSimon Schubert   if (regcache == NULL)
231*5796c8dcSSimon Schubert     return;
232*5796c8dcSSimon Schubert   xfree (regcache->registers);
233*5796c8dcSSimon Schubert   xfree (regcache->register_valid_p);
234*5796c8dcSSimon Schubert   xfree (regcache);
235*5796c8dcSSimon Schubert }
236*5796c8dcSSimon Schubert 
237*5796c8dcSSimon Schubert static void
238*5796c8dcSSimon Schubert do_regcache_xfree (void *data)
239*5796c8dcSSimon Schubert {
240*5796c8dcSSimon Schubert   regcache_xfree (data);
241*5796c8dcSSimon Schubert }
242*5796c8dcSSimon Schubert 
243*5796c8dcSSimon Schubert struct cleanup *
244*5796c8dcSSimon Schubert make_cleanup_regcache_xfree (struct regcache *regcache)
245*5796c8dcSSimon Schubert {
246*5796c8dcSSimon Schubert   return make_cleanup (do_regcache_xfree, regcache);
247*5796c8dcSSimon Schubert }
248*5796c8dcSSimon Schubert 
249*5796c8dcSSimon Schubert /* Return REGCACHE's architecture.  */
250*5796c8dcSSimon Schubert 
251*5796c8dcSSimon Schubert struct gdbarch *
252*5796c8dcSSimon Schubert get_regcache_arch (const struct regcache *regcache)
253*5796c8dcSSimon Schubert {
254*5796c8dcSSimon Schubert   return regcache->descr->gdbarch;
255*5796c8dcSSimon Schubert }
256*5796c8dcSSimon Schubert 
257*5796c8dcSSimon Schubert /* Return  a pointer to register REGNUM's buffer cache.  */
258*5796c8dcSSimon Schubert 
259*5796c8dcSSimon Schubert static gdb_byte *
260*5796c8dcSSimon Schubert register_buffer (const struct regcache *regcache, int regnum)
261*5796c8dcSSimon Schubert {
262*5796c8dcSSimon Schubert   return regcache->registers + regcache->descr->register_offset[regnum];
263*5796c8dcSSimon Schubert }
264*5796c8dcSSimon Schubert 
265*5796c8dcSSimon Schubert void
266*5796c8dcSSimon Schubert regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
267*5796c8dcSSimon Schubert 	       void *src)
268*5796c8dcSSimon Schubert {
269*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = dst->descr->gdbarch;
270*5796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
271*5796c8dcSSimon Schubert   int regnum;
272*5796c8dcSSimon Schubert   /* The DST should be `read-only', if it wasn't then the save would
273*5796c8dcSSimon Schubert      end up trying to write the register values back out to the
274*5796c8dcSSimon Schubert      target.  */
275*5796c8dcSSimon Schubert   gdb_assert (dst->readonly_p);
276*5796c8dcSSimon Schubert   /* Clear the dest.  */
277*5796c8dcSSimon Schubert   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
278*5796c8dcSSimon Schubert   memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
279*5796c8dcSSimon Schubert   /* Copy over any registers (identified by their membership in the
280*5796c8dcSSimon Schubert      save_reggroup) and mark them as valid.  The full [0 .. gdbarch_num_regs +
281*5796c8dcSSimon Schubert      gdbarch_num_pseudo_regs) range is checked since some architectures need
282*5796c8dcSSimon Schubert      to save/restore `cooked' registers that live in memory.  */
283*5796c8dcSSimon Schubert   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
284*5796c8dcSSimon Schubert     {
285*5796c8dcSSimon Schubert       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
286*5796c8dcSSimon Schubert 	{
287*5796c8dcSSimon Schubert 	  int valid = cooked_read (src, regnum, buf);
288*5796c8dcSSimon Schubert 	  if (valid)
289*5796c8dcSSimon Schubert 	    {
290*5796c8dcSSimon Schubert 	      memcpy (register_buffer (dst, regnum), buf,
291*5796c8dcSSimon Schubert 		      register_size (gdbarch, regnum));
292*5796c8dcSSimon Schubert 	      dst->register_valid_p[regnum] = 1;
293*5796c8dcSSimon Schubert 	    }
294*5796c8dcSSimon Schubert 	}
295*5796c8dcSSimon Schubert     }
296*5796c8dcSSimon Schubert }
297*5796c8dcSSimon Schubert 
298*5796c8dcSSimon Schubert void
299*5796c8dcSSimon Schubert regcache_restore (struct regcache *dst,
300*5796c8dcSSimon Schubert 		  regcache_cooked_read_ftype *cooked_read,
301*5796c8dcSSimon Schubert 		  void *cooked_read_context)
302*5796c8dcSSimon Schubert {
303*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = dst->descr->gdbarch;
304*5796c8dcSSimon Schubert   gdb_byte buf[MAX_REGISTER_SIZE];
305*5796c8dcSSimon Schubert   int regnum;
306*5796c8dcSSimon Schubert   /* The dst had better not be read-only.  If it is, the `restore'
307*5796c8dcSSimon Schubert      doesn't make much sense.  */
308*5796c8dcSSimon Schubert   gdb_assert (!dst->readonly_p);
309*5796c8dcSSimon Schubert   /* Copy over any registers, being careful to only restore those that
310*5796c8dcSSimon Schubert      were both saved and need to be restored.  The full [0 .. gdbarch_num_regs
311*5796c8dcSSimon Schubert      + gdbarch_num_pseudo_regs) range is checked since some architectures need
312*5796c8dcSSimon Schubert      to save/restore `cooked' registers that live in memory.  */
313*5796c8dcSSimon Schubert   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
314*5796c8dcSSimon Schubert     {
315*5796c8dcSSimon Schubert       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
316*5796c8dcSSimon Schubert 	{
317*5796c8dcSSimon Schubert 	  int valid = cooked_read (cooked_read_context, regnum, buf);
318*5796c8dcSSimon Schubert 	  if (valid)
319*5796c8dcSSimon Schubert 	    regcache_cooked_write (dst, regnum, buf);
320*5796c8dcSSimon Schubert 	}
321*5796c8dcSSimon Schubert     }
322*5796c8dcSSimon Schubert }
323*5796c8dcSSimon Schubert 
324*5796c8dcSSimon Schubert static int
325*5796c8dcSSimon Schubert do_cooked_read (void *src, int regnum, gdb_byte *buf)
326*5796c8dcSSimon Schubert {
327*5796c8dcSSimon Schubert   struct regcache *regcache = src;
328*5796c8dcSSimon Schubert   if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
329*5796c8dcSSimon Schubert     /* Don't even think about fetching a register from a read-only
330*5796c8dcSSimon Schubert        cache when the register isn't yet valid.  There isn't a target
331*5796c8dcSSimon Schubert        from which the register value can be fetched.  */
332*5796c8dcSSimon Schubert     return 0;
333*5796c8dcSSimon Schubert   regcache_cooked_read (regcache, regnum, buf);
334*5796c8dcSSimon Schubert   return 1;
335*5796c8dcSSimon Schubert }
336*5796c8dcSSimon Schubert 
337*5796c8dcSSimon Schubert 
338*5796c8dcSSimon Schubert void
339*5796c8dcSSimon Schubert regcache_cpy (struct regcache *dst, struct regcache *src)
340*5796c8dcSSimon Schubert {
341*5796c8dcSSimon Schubert   int i;
342*5796c8dcSSimon Schubert   gdb_byte *buf;
343*5796c8dcSSimon Schubert   gdb_assert (src != NULL && dst != NULL);
344*5796c8dcSSimon Schubert   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
345*5796c8dcSSimon Schubert   gdb_assert (src != dst);
346*5796c8dcSSimon Schubert   gdb_assert (src->readonly_p || dst->readonly_p);
347*5796c8dcSSimon Schubert   if (!src->readonly_p)
348*5796c8dcSSimon Schubert     regcache_save (dst, do_cooked_read, src);
349*5796c8dcSSimon Schubert   else if (!dst->readonly_p)
350*5796c8dcSSimon Schubert     regcache_restore (dst, do_cooked_read, src);
351*5796c8dcSSimon Schubert   else
352*5796c8dcSSimon Schubert     regcache_cpy_no_passthrough (dst, src);
353*5796c8dcSSimon Schubert }
354*5796c8dcSSimon Schubert 
355*5796c8dcSSimon Schubert void
356*5796c8dcSSimon Schubert regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
357*5796c8dcSSimon Schubert {
358*5796c8dcSSimon Schubert   int i;
359*5796c8dcSSimon Schubert   gdb_assert (src != NULL && dst != NULL);
360*5796c8dcSSimon Schubert   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
361*5796c8dcSSimon Schubert   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
362*5796c8dcSSimon Schubert      move of data into the current regcache.  Doing this would be
363*5796c8dcSSimon Schubert      silly - it would mean that valid_p would be completely invalid.  */
364*5796c8dcSSimon Schubert   gdb_assert (dst->readonly_p);
365*5796c8dcSSimon Schubert   memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
366*5796c8dcSSimon Schubert   memcpy (dst->register_valid_p, src->register_valid_p,
367*5796c8dcSSimon Schubert 	  dst->descr->sizeof_raw_register_valid_p);
368*5796c8dcSSimon Schubert }
369*5796c8dcSSimon Schubert 
370*5796c8dcSSimon Schubert struct regcache *
371*5796c8dcSSimon Schubert regcache_dup (struct regcache *src)
372*5796c8dcSSimon Schubert {
373*5796c8dcSSimon Schubert   struct regcache *newbuf;
374*5796c8dcSSimon Schubert   newbuf = regcache_xmalloc (src->descr->gdbarch);
375*5796c8dcSSimon Schubert   regcache_cpy (newbuf, src);
376*5796c8dcSSimon Schubert   return newbuf;
377*5796c8dcSSimon Schubert }
378*5796c8dcSSimon Schubert 
379*5796c8dcSSimon Schubert struct regcache *
380*5796c8dcSSimon Schubert regcache_dup_no_passthrough (struct regcache *src)
381*5796c8dcSSimon Schubert {
382*5796c8dcSSimon Schubert   struct regcache *newbuf;
383*5796c8dcSSimon Schubert   newbuf = regcache_xmalloc (src->descr->gdbarch);
384*5796c8dcSSimon Schubert   regcache_cpy_no_passthrough (newbuf, src);
385*5796c8dcSSimon Schubert   return newbuf;
386*5796c8dcSSimon Schubert }
387*5796c8dcSSimon Schubert 
388*5796c8dcSSimon Schubert int
389*5796c8dcSSimon Schubert regcache_valid_p (const struct regcache *regcache, int regnum)
390*5796c8dcSSimon Schubert {
391*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
392*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
393*5796c8dcSSimon Schubert   if (regcache->readonly_p)
394*5796c8dcSSimon Schubert     gdb_assert (regnum < regcache->descr->nr_cooked_registers);
395*5796c8dcSSimon Schubert   else
396*5796c8dcSSimon Schubert     gdb_assert (regnum < regcache->descr->nr_raw_registers);
397*5796c8dcSSimon Schubert 
398*5796c8dcSSimon Schubert   return regcache->register_valid_p[regnum];
399*5796c8dcSSimon Schubert }
400*5796c8dcSSimon Schubert 
401*5796c8dcSSimon Schubert void
402*5796c8dcSSimon Schubert regcache_invalidate (struct regcache *regcache, int regnum)
403*5796c8dcSSimon Schubert {
404*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
405*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
406*5796c8dcSSimon Schubert   gdb_assert (!regcache->readonly_p);
407*5796c8dcSSimon Schubert   gdb_assert (regnum < regcache->descr->nr_raw_registers);
408*5796c8dcSSimon Schubert   regcache->register_valid_p[regnum] = 0;
409*5796c8dcSSimon Schubert }
410*5796c8dcSSimon Schubert 
411*5796c8dcSSimon Schubert 
412*5796c8dcSSimon Schubert /* Global structure containing the current regcache.  */
413*5796c8dcSSimon Schubert 
414*5796c8dcSSimon Schubert /* NOTE: this is a write-through cache.  There is no "dirty" bit for
415*5796c8dcSSimon Schubert    recording if the register values have been changed (eg. by the
416*5796c8dcSSimon Schubert    user).  Therefore all registers must be written back to the
417*5796c8dcSSimon Schubert    target when appropriate.  */
418*5796c8dcSSimon Schubert 
419*5796c8dcSSimon Schubert struct regcache_list
420*5796c8dcSSimon Schubert {
421*5796c8dcSSimon Schubert   struct regcache *regcache;
422*5796c8dcSSimon Schubert   struct regcache_list *next;
423*5796c8dcSSimon Schubert };
424*5796c8dcSSimon Schubert 
425*5796c8dcSSimon Schubert static struct regcache_list *current_regcache;
426*5796c8dcSSimon Schubert 
427*5796c8dcSSimon Schubert struct regcache *
428*5796c8dcSSimon Schubert get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
429*5796c8dcSSimon Schubert {
430*5796c8dcSSimon Schubert   struct regcache_list *list;
431*5796c8dcSSimon Schubert   struct regcache *new_regcache;
432*5796c8dcSSimon Schubert 
433*5796c8dcSSimon Schubert   for (list = current_regcache; list; list = list->next)
434*5796c8dcSSimon Schubert     if (ptid_equal (list->regcache->ptid, ptid)
435*5796c8dcSSimon Schubert 	&& get_regcache_arch (list->regcache) == gdbarch)
436*5796c8dcSSimon Schubert       return list->regcache;
437*5796c8dcSSimon Schubert 
438*5796c8dcSSimon Schubert   new_regcache = regcache_xmalloc (gdbarch);
439*5796c8dcSSimon Schubert   new_regcache->readonly_p = 0;
440*5796c8dcSSimon Schubert   new_regcache->ptid = ptid;
441*5796c8dcSSimon Schubert 
442*5796c8dcSSimon Schubert   list = xmalloc (sizeof (struct regcache_list));
443*5796c8dcSSimon Schubert   list->regcache = new_regcache;
444*5796c8dcSSimon Schubert   list->next = current_regcache;
445*5796c8dcSSimon Schubert   current_regcache = list;
446*5796c8dcSSimon Schubert 
447*5796c8dcSSimon Schubert   return new_regcache;
448*5796c8dcSSimon Schubert }
449*5796c8dcSSimon Schubert 
450*5796c8dcSSimon Schubert static ptid_t current_thread_ptid;
451*5796c8dcSSimon Schubert static struct gdbarch *current_thread_arch;
452*5796c8dcSSimon Schubert 
453*5796c8dcSSimon Schubert struct regcache *
454*5796c8dcSSimon Schubert get_thread_regcache (ptid_t ptid)
455*5796c8dcSSimon Schubert {
456*5796c8dcSSimon Schubert   if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
457*5796c8dcSSimon Schubert     {
458*5796c8dcSSimon Schubert       current_thread_ptid = ptid;
459*5796c8dcSSimon Schubert       current_thread_arch = target_thread_architecture (ptid);
460*5796c8dcSSimon Schubert     }
461*5796c8dcSSimon Schubert 
462*5796c8dcSSimon Schubert   return get_thread_arch_regcache (ptid, current_thread_arch);
463*5796c8dcSSimon Schubert }
464*5796c8dcSSimon Schubert 
465*5796c8dcSSimon Schubert struct regcache *
466*5796c8dcSSimon Schubert get_current_regcache (void)
467*5796c8dcSSimon Schubert {
468*5796c8dcSSimon Schubert   return get_thread_regcache (inferior_ptid);
469*5796c8dcSSimon Schubert }
470*5796c8dcSSimon Schubert 
471*5796c8dcSSimon Schubert 
472*5796c8dcSSimon Schubert /* Observer for the target_changed event.  */
473*5796c8dcSSimon Schubert 
474*5796c8dcSSimon Schubert static void
475*5796c8dcSSimon Schubert regcache_observer_target_changed (struct target_ops *target)
476*5796c8dcSSimon Schubert {
477*5796c8dcSSimon Schubert   registers_changed ();
478*5796c8dcSSimon Schubert }
479*5796c8dcSSimon Schubert 
480*5796c8dcSSimon Schubert /* Update global variables old ptids to hold NEW_PTID if they were
481*5796c8dcSSimon Schubert    holding OLD_PTID.  */
482*5796c8dcSSimon Schubert static void
483*5796c8dcSSimon Schubert regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
484*5796c8dcSSimon Schubert {
485*5796c8dcSSimon Schubert   struct regcache_list *list;
486*5796c8dcSSimon Schubert 
487*5796c8dcSSimon Schubert   for (list = current_regcache; list; list = list->next)
488*5796c8dcSSimon Schubert     if (ptid_equal (list->regcache->ptid, old_ptid))
489*5796c8dcSSimon Schubert       list->regcache->ptid = new_ptid;
490*5796c8dcSSimon Schubert }
491*5796c8dcSSimon Schubert 
492*5796c8dcSSimon Schubert /* Low level examining and depositing of registers.
493*5796c8dcSSimon Schubert 
494*5796c8dcSSimon Schubert    The caller is responsible for making sure that the inferior is
495*5796c8dcSSimon Schubert    stopped before calling the fetching routines, or it will get
496*5796c8dcSSimon Schubert    garbage.  (a change from GDB version 3, in which the caller got the
497*5796c8dcSSimon Schubert    value from the last stop).  */
498*5796c8dcSSimon Schubert 
499*5796c8dcSSimon Schubert /* REGISTERS_CHANGED ()
500*5796c8dcSSimon Schubert 
501*5796c8dcSSimon Schubert    Indicate that registers may have changed, so invalidate the cache.  */
502*5796c8dcSSimon Schubert 
503*5796c8dcSSimon Schubert void
504*5796c8dcSSimon Schubert registers_changed (void)
505*5796c8dcSSimon Schubert {
506*5796c8dcSSimon Schubert   struct regcache_list *list, *next;
507*5796c8dcSSimon Schubert 
508*5796c8dcSSimon Schubert   for (list = current_regcache; list; list = next)
509*5796c8dcSSimon Schubert     {
510*5796c8dcSSimon Schubert       next = list->next;
511*5796c8dcSSimon Schubert       regcache_xfree (list->regcache);
512*5796c8dcSSimon Schubert       xfree (list);
513*5796c8dcSSimon Schubert     }
514*5796c8dcSSimon Schubert 
515*5796c8dcSSimon Schubert   current_regcache = NULL;
516*5796c8dcSSimon Schubert 
517*5796c8dcSSimon Schubert   current_thread_ptid = null_ptid;
518*5796c8dcSSimon Schubert   current_thread_arch = NULL;
519*5796c8dcSSimon Schubert 
520*5796c8dcSSimon Schubert   /* Need to forget about any frames we have cached, too. */
521*5796c8dcSSimon Schubert   reinit_frame_cache ();
522*5796c8dcSSimon Schubert 
523*5796c8dcSSimon Schubert   /* Force cleanup of any alloca areas if using C alloca instead of
524*5796c8dcSSimon Schubert      a builtin alloca.  This particular call is used to clean up
525*5796c8dcSSimon Schubert      areas allocated by low level target code which may build up
526*5796c8dcSSimon Schubert      during lengthy interactions between gdb and the target before
527*5796c8dcSSimon Schubert      gdb gives control to the user (ie watchpoints).  */
528*5796c8dcSSimon Schubert   alloca (0);
529*5796c8dcSSimon Schubert }
530*5796c8dcSSimon Schubert 
531*5796c8dcSSimon Schubert 
532*5796c8dcSSimon Schubert void
533*5796c8dcSSimon Schubert regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
534*5796c8dcSSimon Schubert {
535*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL && buf != NULL);
536*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
537*5796c8dcSSimon Schubert   /* Make certain that the register cache is up-to-date with respect
538*5796c8dcSSimon Schubert      to the current thread.  This switching shouldn't be necessary
539*5796c8dcSSimon Schubert      only there is still only one target side register cache.  Sigh!
540*5796c8dcSSimon Schubert      On the bright side, at least there is a regcache object.  */
541*5796c8dcSSimon Schubert   if (!regcache->readonly_p)
542*5796c8dcSSimon Schubert     {
543*5796c8dcSSimon Schubert       if (!regcache_valid_p (regcache, regnum))
544*5796c8dcSSimon Schubert 	{
545*5796c8dcSSimon Schubert 	  struct cleanup *old_chain = save_inferior_ptid ();
546*5796c8dcSSimon Schubert 	  inferior_ptid = regcache->ptid;
547*5796c8dcSSimon Schubert 	  target_fetch_registers (regcache, regnum);
548*5796c8dcSSimon Schubert 	  do_cleanups (old_chain);
549*5796c8dcSSimon Schubert 	}
550*5796c8dcSSimon Schubert #if 0
551*5796c8dcSSimon Schubert       /* FIXME: cagney/2004-08-07: At present a number of targets
552*5796c8dcSSimon Schubert 	 forget (or didn't know that they needed) to set this leading to
553*5796c8dcSSimon Schubert 	 panics.  Also is the problem that targets need to indicate
554*5796c8dcSSimon Schubert 	 that a register is in one of the possible states: valid,
555*5796c8dcSSimon Schubert 	 undefined, unknown.  The last of which isn't yet
556*5796c8dcSSimon Schubert 	 possible.  */
557*5796c8dcSSimon Schubert       gdb_assert (regcache_valid_p (regcache, regnum));
558*5796c8dcSSimon Schubert #endif
559*5796c8dcSSimon Schubert     }
560*5796c8dcSSimon Schubert   /* Copy the value directly into the register cache.  */
561*5796c8dcSSimon Schubert   memcpy (buf, register_buffer (regcache, regnum),
562*5796c8dcSSimon Schubert 	  regcache->descr->sizeof_register[regnum]);
563*5796c8dcSSimon Schubert }
564*5796c8dcSSimon Schubert 
565*5796c8dcSSimon Schubert void
566*5796c8dcSSimon Schubert regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
567*5796c8dcSSimon Schubert {
568*5796c8dcSSimon Schubert   gdb_byte *buf;
569*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
570*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
571*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
572*5796c8dcSSimon Schubert   regcache_raw_read (regcache, regnum, buf);
573*5796c8dcSSimon Schubert   (*val) = extract_signed_integer
574*5796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
575*5796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
576*5796c8dcSSimon Schubert }
577*5796c8dcSSimon Schubert 
578*5796c8dcSSimon Schubert void
579*5796c8dcSSimon Schubert regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
580*5796c8dcSSimon Schubert 			    ULONGEST *val)
581*5796c8dcSSimon Schubert {
582*5796c8dcSSimon Schubert   gdb_byte *buf;
583*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
584*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
585*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
586*5796c8dcSSimon Schubert   regcache_raw_read (regcache, regnum, buf);
587*5796c8dcSSimon Schubert   (*val) = extract_unsigned_integer
588*5796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
589*5796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
590*5796c8dcSSimon Schubert }
591*5796c8dcSSimon Schubert 
592*5796c8dcSSimon Schubert void
593*5796c8dcSSimon Schubert regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
594*5796c8dcSSimon Schubert {
595*5796c8dcSSimon Schubert   void *buf;
596*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
597*5796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
598*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
599*5796c8dcSSimon Schubert   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
600*5796c8dcSSimon Schubert 			gdbarch_byte_order (regcache->descr->gdbarch), val);
601*5796c8dcSSimon Schubert   regcache_raw_write (regcache, regnum, buf);
602*5796c8dcSSimon Schubert }
603*5796c8dcSSimon Schubert 
604*5796c8dcSSimon Schubert void
605*5796c8dcSSimon Schubert regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
606*5796c8dcSSimon Schubert 			     ULONGEST val)
607*5796c8dcSSimon Schubert {
608*5796c8dcSSimon Schubert   void *buf;
609*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
610*5796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
611*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
612*5796c8dcSSimon Schubert   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
613*5796c8dcSSimon Schubert 			  gdbarch_byte_order (regcache->descr->gdbarch), val);
614*5796c8dcSSimon Schubert   regcache_raw_write (regcache, regnum, buf);
615*5796c8dcSSimon Schubert }
616*5796c8dcSSimon Schubert 
617*5796c8dcSSimon Schubert void
618*5796c8dcSSimon Schubert regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
619*5796c8dcSSimon Schubert {
620*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
621*5796c8dcSSimon Schubert   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
622*5796c8dcSSimon Schubert   if (regnum < regcache->descr->nr_raw_registers)
623*5796c8dcSSimon Schubert     regcache_raw_read (regcache, regnum, buf);
624*5796c8dcSSimon Schubert   else if (regcache->readonly_p
625*5796c8dcSSimon Schubert 	   && regnum < regcache->descr->nr_cooked_registers
626*5796c8dcSSimon Schubert 	   && regcache->register_valid_p[regnum])
627*5796c8dcSSimon Schubert     /* Read-only register cache, perhaps the cooked value was cached?  */
628*5796c8dcSSimon Schubert     memcpy (buf, register_buffer (regcache, regnum),
629*5796c8dcSSimon Schubert 	    regcache->descr->sizeof_register[regnum]);
630*5796c8dcSSimon Schubert   else
631*5796c8dcSSimon Schubert     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
632*5796c8dcSSimon Schubert 				  regnum, buf);
633*5796c8dcSSimon Schubert }
634*5796c8dcSSimon Schubert 
635*5796c8dcSSimon Schubert void
636*5796c8dcSSimon Schubert regcache_cooked_read_signed (struct regcache *regcache, int regnum,
637*5796c8dcSSimon Schubert 			     LONGEST *val)
638*5796c8dcSSimon Schubert {
639*5796c8dcSSimon Schubert   gdb_byte *buf;
640*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
641*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
642*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
643*5796c8dcSSimon Schubert   regcache_cooked_read (regcache, regnum, buf);
644*5796c8dcSSimon Schubert   (*val) = extract_signed_integer
645*5796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
646*5796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
647*5796c8dcSSimon Schubert }
648*5796c8dcSSimon Schubert 
649*5796c8dcSSimon Schubert void
650*5796c8dcSSimon Schubert regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
651*5796c8dcSSimon Schubert 			       ULONGEST *val)
652*5796c8dcSSimon Schubert {
653*5796c8dcSSimon Schubert   gdb_byte *buf;
654*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
655*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
656*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
657*5796c8dcSSimon Schubert   regcache_cooked_read (regcache, regnum, buf);
658*5796c8dcSSimon Schubert   (*val) = extract_unsigned_integer
659*5796c8dcSSimon Schubert 	     (buf, regcache->descr->sizeof_register[regnum],
660*5796c8dcSSimon Schubert 	      gdbarch_byte_order (regcache->descr->gdbarch));
661*5796c8dcSSimon Schubert }
662*5796c8dcSSimon Schubert 
663*5796c8dcSSimon Schubert void
664*5796c8dcSSimon Schubert regcache_cooked_write_signed (struct regcache *regcache, int regnum,
665*5796c8dcSSimon Schubert 			      LONGEST val)
666*5796c8dcSSimon Schubert {
667*5796c8dcSSimon Schubert   void *buf;
668*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
669*5796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
670*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
671*5796c8dcSSimon Schubert   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
672*5796c8dcSSimon Schubert 			gdbarch_byte_order (regcache->descr->gdbarch), val);
673*5796c8dcSSimon Schubert   regcache_cooked_write (regcache, regnum, buf);
674*5796c8dcSSimon Schubert }
675*5796c8dcSSimon Schubert 
676*5796c8dcSSimon Schubert void
677*5796c8dcSSimon Schubert regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
678*5796c8dcSSimon Schubert 				ULONGEST val)
679*5796c8dcSSimon Schubert {
680*5796c8dcSSimon Schubert   void *buf;
681*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
682*5796c8dcSSimon Schubert   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
683*5796c8dcSSimon Schubert   buf = alloca (regcache->descr->sizeof_register[regnum]);
684*5796c8dcSSimon Schubert   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
685*5796c8dcSSimon Schubert 			  gdbarch_byte_order (regcache->descr->gdbarch), val);
686*5796c8dcSSimon Schubert   regcache_cooked_write (regcache, regnum, buf);
687*5796c8dcSSimon Schubert }
688*5796c8dcSSimon Schubert 
689*5796c8dcSSimon Schubert void
690*5796c8dcSSimon Schubert regcache_raw_write (struct regcache *regcache, int regnum,
691*5796c8dcSSimon Schubert 		    const gdb_byte *buf)
692*5796c8dcSSimon Schubert {
693*5796c8dcSSimon Schubert   struct cleanup *old_chain;
694*5796c8dcSSimon Schubert 
695*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL && buf != NULL);
696*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
697*5796c8dcSSimon Schubert   gdb_assert (!regcache->readonly_p);
698*5796c8dcSSimon Schubert 
699*5796c8dcSSimon Schubert   /* On the sparc, writing %g0 is a no-op, so we don't even want to
700*5796c8dcSSimon Schubert      change the registers array if something writes to this register.  */
701*5796c8dcSSimon Schubert   if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
702*5796c8dcSSimon Schubert     return;
703*5796c8dcSSimon Schubert 
704*5796c8dcSSimon Schubert   /* If we have a valid copy of the register, and new value == old
705*5796c8dcSSimon Schubert      value, then don't bother doing the actual store. */
706*5796c8dcSSimon Schubert   if (regcache_valid_p (regcache, regnum)
707*5796c8dcSSimon Schubert       && (memcmp (register_buffer (regcache, regnum), buf,
708*5796c8dcSSimon Schubert 		  regcache->descr->sizeof_register[regnum]) == 0))
709*5796c8dcSSimon Schubert     return;
710*5796c8dcSSimon Schubert 
711*5796c8dcSSimon Schubert   old_chain = save_inferior_ptid ();
712*5796c8dcSSimon Schubert   inferior_ptid = regcache->ptid;
713*5796c8dcSSimon Schubert 
714*5796c8dcSSimon Schubert   target_prepare_to_store (regcache);
715*5796c8dcSSimon Schubert   memcpy (register_buffer (regcache, regnum), buf,
716*5796c8dcSSimon Schubert 	  regcache->descr->sizeof_register[regnum]);
717*5796c8dcSSimon Schubert   regcache->register_valid_p[regnum] = 1;
718*5796c8dcSSimon Schubert   target_store_registers (regcache, regnum);
719*5796c8dcSSimon Schubert 
720*5796c8dcSSimon Schubert   do_cleanups (old_chain);
721*5796c8dcSSimon Schubert }
722*5796c8dcSSimon Schubert 
723*5796c8dcSSimon Schubert void
724*5796c8dcSSimon Schubert regcache_cooked_write (struct regcache *regcache, int regnum,
725*5796c8dcSSimon Schubert 		       const gdb_byte *buf)
726*5796c8dcSSimon Schubert {
727*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0);
728*5796c8dcSSimon Schubert   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
729*5796c8dcSSimon Schubert   if (regnum < regcache->descr->nr_raw_registers)
730*5796c8dcSSimon Schubert     regcache_raw_write (regcache, regnum, buf);
731*5796c8dcSSimon Schubert   else
732*5796c8dcSSimon Schubert     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
733*5796c8dcSSimon Schubert 				   regnum, buf);
734*5796c8dcSSimon Schubert }
735*5796c8dcSSimon Schubert 
736*5796c8dcSSimon Schubert /* Perform a partial register transfer using a read, modify, write
737*5796c8dcSSimon Schubert    operation.  */
738*5796c8dcSSimon Schubert 
739*5796c8dcSSimon Schubert typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
740*5796c8dcSSimon Schubert 				    void *buf);
741*5796c8dcSSimon Schubert typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
742*5796c8dcSSimon Schubert 				     const void *buf);
743*5796c8dcSSimon Schubert 
744*5796c8dcSSimon Schubert static void
745*5796c8dcSSimon Schubert regcache_xfer_part (struct regcache *regcache, int regnum,
746*5796c8dcSSimon Schubert 		    int offset, int len, void *in, const void *out,
747*5796c8dcSSimon Schubert 		    void (*read) (struct regcache *regcache, int regnum,
748*5796c8dcSSimon Schubert 				  gdb_byte *buf),
749*5796c8dcSSimon Schubert 		    void (*write) (struct regcache *regcache, int regnum,
750*5796c8dcSSimon Schubert 				   const gdb_byte *buf))
751*5796c8dcSSimon Schubert {
752*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
753*5796c8dcSSimon Schubert   gdb_byte reg[MAX_REGISTER_SIZE];
754*5796c8dcSSimon Schubert   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
755*5796c8dcSSimon Schubert   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
756*5796c8dcSSimon Schubert   /* Something to do?  */
757*5796c8dcSSimon Schubert   if (offset + len == 0)
758*5796c8dcSSimon Schubert     return;
759*5796c8dcSSimon Schubert   /* Read (when needed) ... */
760*5796c8dcSSimon Schubert   if (in != NULL
761*5796c8dcSSimon Schubert       || offset > 0
762*5796c8dcSSimon Schubert       || offset + len < descr->sizeof_register[regnum])
763*5796c8dcSSimon Schubert     {
764*5796c8dcSSimon Schubert       gdb_assert (read != NULL);
765*5796c8dcSSimon Schubert       read (regcache, regnum, reg);
766*5796c8dcSSimon Schubert     }
767*5796c8dcSSimon Schubert   /* ... modify ... */
768*5796c8dcSSimon Schubert   if (in != NULL)
769*5796c8dcSSimon Schubert     memcpy (in, reg + offset, len);
770*5796c8dcSSimon Schubert   if (out != NULL)
771*5796c8dcSSimon Schubert     memcpy (reg + offset, out, len);
772*5796c8dcSSimon Schubert   /* ... write (when needed).  */
773*5796c8dcSSimon Schubert   if (out != NULL)
774*5796c8dcSSimon Schubert     {
775*5796c8dcSSimon Schubert       gdb_assert (write != NULL);
776*5796c8dcSSimon Schubert       write (regcache, regnum, reg);
777*5796c8dcSSimon Schubert     }
778*5796c8dcSSimon Schubert }
779*5796c8dcSSimon Schubert 
780*5796c8dcSSimon Schubert void
781*5796c8dcSSimon Schubert regcache_raw_read_part (struct regcache *regcache, int regnum,
782*5796c8dcSSimon Schubert 			int offset, int len, gdb_byte *buf)
783*5796c8dcSSimon Schubert {
784*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
785*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
786*5796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
787*5796c8dcSSimon Schubert 		      regcache_raw_read, regcache_raw_write);
788*5796c8dcSSimon Schubert }
789*5796c8dcSSimon Schubert 
790*5796c8dcSSimon Schubert void
791*5796c8dcSSimon Schubert regcache_raw_write_part (struct regcache *regcache, int regnum,
792*5796c8dcSSimon Schubert 			 int offset, int len, const gdb_byte *buf)
793*5796c8dcSSimon Schubert {
794*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
795*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
796*5796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
797*5796c8dcSSimon Schubert 		      regcache_raw_read, regcache_raw_write);
798*5796c8dcSSimon Schubert }
799*5796c8dcSSimon Schubert 
800*5796c8dcSSimon Schubert void
801*5796c8dcSSimon Schubert regcache_cooked_read_part (struct regcache *regcache, int regnum,
802*5796c8dcSSimon Schubert 			   int offset, int len, gdb_byte *buf)
803*5796c8dcSSimon Schubert {
804*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
805*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
806*5796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
807*5796c8dcSSimon Schubert 		      regcache_cooked_read, regcache_cooked_write);
808*5796c8dcSSimon Schubert }
809*5796c8dcSSimon Schubert 
810*5796c8dcSSimon Schubert void
811*5796c8dcSSimon Schubert regcache_cooked_write_part (struct regcache *regcache, int regnum,
812*5796c8dcSSimon Schubert 			    int offset, int len, const gdb_byte *buf)
813*5796c8dcSSimon Schubert {
814*5796c8dcSSimon Schubert   struct regcache_descr *descr = regcache->descr;
815*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
816*5796c8dcSSimon Schubert   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
817*5796c8dcSSimon Schubert 		      regcache_cooked_read, regcache_cooked_write);
818*5796c8dcSSimon Schubert }
819*5796c8dcSSimon Schubert 
820*5796c8dcSSimon Schubert /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
821*5796c8dcSSimon Schubert 
822*5796c8dcSSimon Schubert void
823*5796c8dcSSimon Schubert regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
824*5796c8dcSSimon Schubert {
825*5796c8dcSSimon Schubert   void *regbuf;
826*5796c8dcSSimon Schubert   size_t size;
827*5796c8dcSSimon Schubert 
828*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL);
829*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
830*5796c8dcSSimon Schubert   gdb_assert (!regcache->readonly_p);
831*5796c8dcSSimon Schubert 
832*5796c8dcSSimon Schubert   regbuf = register_buffer (regcache, regnum);
833*5796c8dcSSimon Schubert   size = regcache->descr->sizeof_register[regnum];
834*5796c8dcSSimon Schubert 
835*5796c8dcSSimon Schubert   if (buf)
836*5796c8dcSSimon Schubert     memcpy (regbuf, buf, size);
837*5796c8dcSSimon Schubert   else
838*5796c8dcSSimon Schubert     memset (regbuf, 0, size);
839*5796c8dcSSimon Schubert 
840*5796c8dcSSimon Schubert   /* Mark the register as cached.  */
841*5796c8dcSSimon Schubert   regcache->register_valid_p[regnum] = 1;
842*5796c8dcSSimon Schubert }
843*5796c8dcSSimon Schubert 
844*5796c8dcSSimon Schubert /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
845*5796c8dcSSimon Schubert 
846*5796c8dcSSimon Schubert void
847*5796c8dcSSimon Schubert regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
848*5796c8dcSSimon Schubert {
849*5796c8dcSSimon Schubert   const void *regbuf;
850*5796c8dcSSimon Schubert   size_t size;
851*5796c8dcSSimon Schubert 
852*5796c8dcSSimon Schubert   gdb_assert (regcache != NULL && buf != NULL);
853*5796c8dcSSimon Schubert   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
854*5796c8dcSSimon Schubert 
855*5796c8dcSSimon Schubert   regbuf = register_buffer (regcache, regnum);
856*5796c8dcSSimon Schubert   size = regcache->descr->sizeof_register[regnum];
857*5796c8dcSSimon Schubert   memcpy (buf, regbuf, size);
858*5796c8dcSSimon Schubert }
859*5796c8dcSSimon Schubert 
860*5796c8dcSSimon Schubert 
861*5796c8dcSSimon Schubert /* Special handling for register PC.  */
862*5796c8dcSSimon Schubert 
863*5796c8dcSSimon Schubert CORE_ADDR
864*5796c8dcSSimon Schubert regcache_read_pc (struct regcache *regcache)
865*5796c8dcSSimon Schubert {
866*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
867*5796c8dcSSimon Schubert 
868*5796c8dcSSimon Schubert   CORE_ADDR pc_val;
869*5796c8dcSSimon Schubert 
870*5796c8dcSSimon Schubert   if (gdbarch_read_pc_p (gdbarch))
871*5796c8dcSSimon Schubert     pc_val = gdbarch_read_pc (gdbarch, regcache);
872*5796c8dcSSimon Schubert   /* Else use per-frame method on get_current_frame.  */
873*5796c8dcSSimon Schubert   else if (gdbarch_pc_regnum (gdbarch) >= 0)
874*5796c8dcSSimon Schubert     {
875*5796c8dcSSimon Schubert       ULONGEST raw_val;
876*5796c8dcSSimon Schubert       regcache_cooked_read_unsigned (regcache,
877*5796c8dcSSimon Schubert 				     gdbarch_pc_regnum (gdbarch),
878*5796c8dcSSimon Schubert 				     &raw_val);
879*5796c8dcSSimon Schubert       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
880*5796c8dcSSimon Schubert     }
881*5796c8dcSSimon Schubert   else
882*5796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
883*5796c8dcSSimon Schubert 		    _("regcache_read_pc: Unable to find PC"));
884*5796c8dcSSimon Schubert   return pc_val;
885*5796c8dcSSimon Schubert }
886*5796c8dcSSimon Schubert 
887*5796c8dcSSimon Schubert void
888*5796c8dcSSimon Schubert regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
889*5796c8dcSSimon Schubert {
890*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = get_regcache_arch (regcache);
891*5796c8dcSSimon Schubert 
892*5796c8dcSSimon Schubert   if (gdbarch_write_pc_p (gdbarch))
893*5796c8dcSSimon Schubert     gdbarch_write_pc (gdbarch, regcache, pc);
894*5796c8dcSSimon Schubert   else if (gdbarch_pc_regnum (gdbarch) >= 0)
895*5796c8dcSSimon Schubert     regcache_cooked_write_unsigned (regcache,
896*5796c8dcSSimon Schubert 				    gdbarch_pc_regnum (gdbarch), pc);
897*5796c8dcSSimon Schubert   else
898*5796c8dcSSimon Schubert     internal_error (__FILE__, __LINE__,
899*5796c8dcSSimon Schubert 		    _("regcache_write_pc: Unable to update PC"));
900*5796c8dcSSimon Schubert 
901*5796c8dcSSimon Schubert   /* Writing the PC (for instance, from "load") invalidates the
902*5796c8dcSSimon Schubert      current frame.  */
903*5796c8dcSSimon Schubert   reinit_frame_cache ();
904*5796c8dcSSimon Schubert }
905*5796c8dcSSimon Schubert 
906*5796c8dcSSimon Schubert 
907*5796c8dcSSimon Schubert static void
908*5796c8dcSSimon Schubert reg_flush_command (char *command, int from_tty)
909*5796c8dcSSimon Schubert {
910*5796c8dcSSimon Schubert   /* Force-flush the register cache.  */
911*5796c8dcSSimon Schubert   registers_changed ();
912*5796c8dcSSimon Schubert   if (from_tty)
913*5796c8dcSSimon Schubert     printf_filtered (_("Register cache flushed.\n"));
914*5796c8dcSSimon Schubert }
915*5796c8dcSSimon Schubert 
916*5796c8dcSSimon Schubert static void
917*5796c8dcSSimon Schubert dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
918*5796c8dcSSimon Schubert 		   const unsigned char *buf, long len)
919*5796c8dcSSimon Schubert {
920*5796c8dcSSimon Schubert   int i;
921*5796c8dcSSimon Schubert   switch (endian)
922*5796c8dcSSimon Schubert     {
923*5796c8dcSSimon Schubert     case BFD_ENDIAN_BIG:
924*5796c8dcSSimon Schubert       for (i = 0; i < len; i++)
925*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, "%02x", buf[i]);
926*5796c8dcSSimon Schubert       break;
927*5796c8dcSSimon Schubert     case BFD_ENDIAN_LITTLE:
928*5796c8dcSSimon Schubert       for (i = len - 1; i >= 0; i--)
929*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, "%02x", buf[i]);
930*5796c8dcSSimon Schubert       break;
931*5796c8dcSSimon Schubert     default:
932*5796c8dcSSimon Schubert       internal_error (__FILE__, __LINE__, _("Bad switch"));
933*5796c8dcSSimon Schubert     }
934*5796c8dcSSimon Schubert }
935*5796c8dcSSimon Schubert 
936*5796c8dcSSimon Schubert enum regcache_dump_what
937*5796c8dcSSimon Schubert {
938*5796c8dcSSimon Schubert   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
939*5796c8dcSSimon Schubert };
940*5796c8dcSSimon Schubert 
941*5796c8dcSSimon Schubert static void
942*5796c8dcSSimon Schubert regcache_dump (struct regcache *regcache, struct ui_file *file,
943*5796c8dcSSimon Schubert 	       enum regcache_dump_what what_to_dump)
944*5796c8dcSSimon Schubert {
945*5796c8dcSSimon Schubert   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
946*5796c8dcSSimon Schubert   struct gdbarch *gdbarch = regcache->descr->gdbarch;
947*5796c8dcSSimon Schubert   int regnum;
948*5796c8dcSSimon Schubert   int footnote_nr = 0;
949*5796c8dcSSimon Schubert   int footnote_register_size = 0;
950*5796c8dcSSimon Schubert   int footnote_register_offset = 0;
951*5796c8dcSSimon Schubert   int footnote_register_type_name_null = 0;
952*5796c8dcSSimon Schubert   long register_offset = 0;
953*5796c8dcSSimon Schubert   unsigned char buf[MAX_REGISTER_SIZE];
954*5796c8dcSSimon Schubert 
955*5796c8dcSSimon Schubert #if 0
956*5796c8dcSSimon Schubert   fprintf_unfiltered (file, "nr_raw_registers %d\n",
957*5796c8dcSSimon Schubert 		      regcache->descr->nr_raw_registers);
958*5796c8dcSSimon Schubert   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
959*5796c8dcSSimon Schubert 		      regcache->descr->nr_cooked_registers);
960*5796c8dcSSimon Schubert   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
961*5796c8dcSSimon Schubert 		      regcache->descr->sizeof_raw_registers);
962*5796c8dcSSimon Schubert   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
963*5796c8dcSSimon Schubert 		      regcache->descr->sizeof_raw_register_valid_p);
964*5796c8dcSSimon Schubert   fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
965*5796c8dcSSimon Schubert 		      gdbarch_num_regs (gdbarch));
966*5796c8dcSSimon Schubert   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
967*5796c8dcSSimon Schubert 		      gdbarch_num_pseudo_regs (gdbarch));
968*5796c8dcSSimon Schubert #endif
969*5796c8dcSSimon Schubert 
970*5796c8dcSSimon Schubert   gdb_assert (regcache->descr->nr_cooked_registers
971*5796c8dcSSimon Schubert 	      == (gdbarch_num_regs (gdbarch)
972*5796c8dcSSimon Schubert 		  + gdbarch_num_pseudo_regs (gdbarch)));
973*5796c8dcSSimon Schubert 
974*5796c8dcSSimon Schubert   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
975*5796c8dcSSimon Schubert     {
976*5796c8dcSSimon Schubert       /* Name.  */
977*5796c8dcSSimon Schubert       if (regnum < 0)
978*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %-10s", "Name");
979*5796c8dcSSimon Schubert       else
980*5796c8dcSSimon Schubert 	{
981*5796c8dcSSimon Schubert 	  const char *p = gdbarch_register_name (gdbarch, regnum);
982*5796c8dcSSimon Schubert 	  if (p == NULL)
983*5796c8dcSSimon Schubert 	    p = "";
984*5796c8dcSSimon Schubert 	  else if (p[0] == '\0')
985*5796c8dcSSimon Schubert 	    p = "''";
986*5796c8dcSSimon Schubert 	  fprintf_unfiltered (file, " %-10s", p);
987*5796c8dcSSimon Schubert 	}
988*5796c8dcSSimon Schubert 
989*5796c8dcSSimon Schubert       /* Number.  */
990*5796c8dcSSimon Schubert       if (regnum < 0)
991*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4s", "Nr");
992*5796c8dcSSimon Schubert       else
993*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4d", regnum);
994*5796c8dcSSimon Schubert 
995*5796c8dcSSimon Schubert       /* Relative number.  */
996*5796c8dcSSimon Schubert       if (regnum < 0)
997*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4s", "Rel");
998*5796c8dcSSimon Schubert       else if (regnum < gdbarch_num_regs (gdbarch))
999*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4d", regnum);
1000*5796c8dcSSimon Schubert       else
1001*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %4d",
1002*5796c8dcSSimon Schubert 			    (regnum - gdbarch_num_regs (gdbarch)));
1003*5796c8dcSSimon Schubert 
1004*5796c8dcSSimon Schubert       /* Offset.  */
1005*5796c8dcSSimon Schubert       if (regnum < 0)
1006*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %6s  ", "Offset");
1007*5796c8dcSSimon Schubert       else
1008*5796c8dcSSimon Schubert 	{
1009*5796c8dcSSimon Schubert 	  fprintf_unfiltered (file, " %6ld",
1010*5796c8dcSSimon Schubert 			      regcache->descr->register_offset[regnum]);
1011*5796c8dcSSimon Schubert 	  if (register_offset != regcache->descr->register_offset[regnum]
1012*5796c8dcSSimon Schubert 	      || (regnum > 0
1013*5796c8dcSSimon Schubert 		  && (regcache->descr->register_offset[regnum]
1014*5796c8dcSSimon Schubert 		      != (regcache->descr->register_offset[regnum - 1]
1015*5796c8dcSSimon Schubert 			  + regcache->descr->sizeof_register[regnum - 1])))
1016*5796c8dcSSimon Schubert 	      )
1017*5796c8dcSSimon Schubert 	    {
1018*5796c8dcSSimon Schubert 	      if (!footnote_register_offset)
1019*5796c8dcSSimon Schubert 		footnote_register_offset = ++footnote_nr;
1020*5796c8dcSSimon Schubert 	      fprintf_unfiltered (file, "*%d", footnote_register_offset);
1021*5796c8dcSSimon Schubert 	    }
1022*5796c8dcSSimon Schubert 	  else
1023*5796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "  ");
1024*5796c8dcSSimon Schubert 	  register_offset = (regcache->descr->register_offset[regnum]
1025*5796c8dcSSimon Schubert 			     + regcache->descr->sizeof_register[regnum]);
1026*5796c8dcSSimon Schubert 	}
1027*5796c8dcSSimon Schubert 
1028*5796c8dcSSimon Schubert       /* Size.  */
1029*5796c8dcSSimon Schubert       if (regnum < 0)
1030*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %5s ", "Size");
1031*5796c8dcSSimon Schubert       else
1032*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %5ld",
1033*5796c8dcSSimon Schubert 			    regcache->descr->sizeof_register[regnum]);
1034*5796c8dcSSimon Schubert 
1035*5796c8dcSSimon Schubert       /* Type.  */
1036*5796c8dcSSimon Schubert       {
1037*5796c8dcSSimon Schubert 	const char *t;
1038*5796c8dcSSimon Schubert 	if (regnum < 0)
1039*5796c8dcSSimon Schubert 	  t = "Type";
1040*5796c8dcSSimon Schubert 	else
1041*5796c8dcSSimon Schubert 	  {
1042*5796c8dcSSimon Schubert 	    static const char blt[] = "builtin_type";
1043*5796c8dcSSimon Schubert 	    t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1044*5796c8dcSSimon Schubert 	    if (t == NULL)
1045*5796c8dcSSimon Schubert 	      {
1046*5796c8dcSSimon Schubert 		char *n;
1047*5796c8dcSSimon Schubert 		if (!footnote_register_type_name_null)
1048*5796c8dcSSimon Schubert 		  footnote_register_type_name_null = ++footnote_nr;
1049*5796c8dcSSimon Schubert 		n = xstrprintf ("*%d", footnote_register_type_name_null);
1050*5796c8dcSSimon Schubert 		make_cleanup (xfree, n);
1051*5796c8dcSSimon Schubert 		t = n;
1052*5796c8dcSSimon Schubert 	      }
1053*5796c8dcSSimon Schubert 	    /* Chop a leading builtin_type.  */
1054*5796c8dcSSimon Schubert 	    if (strncmp (t, blt, strlen (blt)) == 0)
1055*5796c8dcSSimon Schubert 	      t += strlen (blt);
1056*5796c8dcSSimon Schubert 	  }
1057*5796c8dcSSimon Schubert 	fprintf_unfiltered (file, " %-15s", t);
1058*5796c8dcSSimon Schubert       }
1059*5796c8dcSSimon Schubert 
1060*5796c8dcSSimon Schubert       /* Leading space always present.  */
1061*5796c8dcSSimon Schubert       fprintf_unfiltered (file, " ");
1062*5796c8dcSSimon Schubert 
1063*5796c8dcSSimon Schubert       /* Value, raw.  */
1064*5796c8dcSSimon Schubert       if (what_to_dump == regcache_dump_raw)
1065*5796c8dcSSimon Schubert 	{
1066*5796c8dcSSimon Schubert 	  if (regnum < 0)
1067*5796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "Raw value");
1068*5796c8dcSSimon Schubert 	  else if (regnum >= regcache->descr->nr_raw_registers)
1069*5796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "<cooked>");
1070*5796c8dcSSimon Schubert 	  else if (!regcache_valid_p (regcache, regnum))
1071*5796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "<invalid>");
1072*5796c8dcSSimon Schubert 	  else
1073*5796c8dcSSimon Schubert 	    {
1074*5796c8dcSSimon Schubert 	      regcache_raw_read (regcache, regnum, buf);
1075*5796c8dcSSimon Schubert 	      fprintf_unfiltered (file, "0x");
1076*5796c8dcSSimon Schubert 	      dump_endian_bytes (file,
1077*5796c8dcSSimon Schubert 				 gdbarch_byte_order (gdbarch), buf,
1078*5796c8dcSSimon Schubert 				 regcache->descr->sizeof_register[regnum]);
1079*5796c8dcSSimon Schubert 	    }
1080*5796c8dcSSimon Schubert 	}
1081*5796c8dcSSimon Schubert 
1082*5796c8dcSSimon Schubert       /* Value, cooked.  */
1083*5796c8dcSSimon Schubert       if (what_to_dump == regcache_dump_cooked)
1084*5796c8dcSSimon Schubert 	{
1085*5796c8dcSSimon Schubert 	  if (regnum < 0)
1086*5796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "Cooked value");
1087*5796c8dcSSimon Schubert 	  else
1088*5796c8dcSSimon Schubert 	    {
1089*5796c8dcSSimon Schubert 	      regcache_cooked_read (regcache, regnum, buf);
1090*5796c8dcSSimon Schubert 	      fprintf_unfiltered (file, "0x");
1091*5796c8dcSSimon Schubert 	      dump_endian_bytes (file,
1092*5796c8dcSSimon Schubert 				 gdbarch_byte_order (gdbarch), buf,
1093*5796c8dcSSimon Schubert 				 regcache->descr->sizeof_register[regnum]);
1094*5796c8dcSSimon Schubert 	    }
1095*5796c8dcSSimon Schubert 	}
1096*5796c8dcSSimon Schubert 
1097*5796c8dcSSimon Schubert       /* Group members.  */
1098*5796c8dcSSimon Schubert       if (what_to_dump == regcache_dump_groups)
1099*5796c8dcSSimon Schubert 	{
1100*5796c8dcSSimon Schubert 	  if (regnum < 0)
1101*5796c8dcSSimon Schubert 	    fprintf_unfiltered (file, "Groups");
1102*5796c8dcSSimon Schubert 	  else
1103*5796c8dcSSimon Schubert 	    {
1104*5796c8dcSSimon Schubert 	      const char *sep = "";
1105*5796c8dcSSimon Schubert 	      struct reggroup *group;
1106*5796c8dcSSimon Schubert 	      for (group = reggroup_next (gdbarch, NULL);
1107*5796c8dcSSimon Schubert 		   group != NULL;
1108*5796c8dcSSimon Schubert 		   group = reggroup_next (gdbarch, group))
1109*5796c8dcSSimon Schubert 		{
1110*5796c8dcSSimon Schubert 		  if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1111*5796c8dcSSimon Schubert 		    {
1112*5796c8dcSSimon Schubert 		      fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1113*5796c8dcSSimon Schubert 		      sep = ",";
1114*5796c8dcSSimon Schubert 		    }
1115*5796c8dcSSimon Schubert 		}
1116*5796c8dcSSimon Schubert 	    }
1117*5796c8dcSSimon Schubert 	}
1118*5796c8dcSSimon Schubert 
1119*5796c8dcSSimon Schubert       fprintf_unfiltered (file, "\n");
1120*5796c8dcSSimon Schubert     }
1121*5796c8dcSSimon Schubert 
1122*5796c8dcSSimon Schubert   if (footnote_register_size)
1123*5796c8dcSSimon Schubert     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1124*5796c8dcSSimon Schubert 			footnote_register_size);
1125*5796c8dcSSimon Schubert   if (footnote_register_offset)
1126*5796c8dcSSimon Schubert     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1127*5796c8dcSSimon Schubert 			footnote_register_offset);
1128*5796c8dcSSimon Schubert   if (footnote_register_type_name_null)
1129*5796c8dcSSimon Schubert     fprintf_unfiltered (file,
1130*5796c8dcSSimon Schubert 			"*%d: Register type's name NULL.\n",
1131*5796c8dcSSimon Schubert 			footnote_register_type_name_null);
1132*5796c8dcSSimon Schubert   do_cleanups (cleanups);
1133*5796c8dcSSimon Schubert }
1134*5796c8dcSSimon Schubert 
1135*5796c8dcSSimon Schubert static void
1136*5796c8dcSSimon Schubert regcache_print (char *args, enum regcache_dump_what what_to_dump)
1137*5796c8dcSSimon Schubert {
1138*5796c8dcSSimon Schubert   if (args == NULL)
1139*5796c8dcSSimon Schubert     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1140*5796c8dcSSimon Schubert   else
1141*5796c8dcSSimon Schubert     {
1142*5796c8dcSSimon Schubert       struct cleanup *cleanups;
1143*5796c8dcSSimon Schubert       struct ui_file *file = gdb_fopen (args, "w");
1144*5796c8dcSSimon Schubert       if (file == NULL)
1145*5796c8dcSSimon Schubert 	perror_with_name (_("maintenance print architecture"));
1146*5796c8dcSSimon Schubert       cleanups = make_cleanup_ui_file_delete (file);
1147*5796c8dcSSimon Schubert       regcache_dump (get_current_regcache (), file, what_to_dump);
1148*5796c8dcSSimon Schubert       do_cleanups (cleanups);
1149*5796c8dcSSimon Schubert     }
1150*5796c8dcSSimon Schubert }
1151*5796c8dcSSimon Schubert 
1152*5796c8dcSSimon Schubert static void
1153*5796c8dcSSimon Schubert maintenance_print_registers (char *args, int from_tty)
1154*5796c8dcSSimon Schubert {
1155*5796c8dcSSimon Schubert   regcache_print (args, regcache_dump_none);
1156*5796c8dcSSimon Schubert }
1157*5796c8dcSSimon Schubert 
1158*5796c8dcSSimon Schubert static void
1159*5796c8dcSSimon Schubert maintenance_print_raw_registers (char *args, int from_tty)
1160*5796c8dcSSimon Schubert {
1161*5796c8dcSSimon Schubert   regcache_print (args, regcache_dump_raw);
1162*5796c8dcSSimon Schubert }
1163*5796c8dcSSimon Schubert 
1164*5796c8dcSSimon Schubert static void
1165*5796c8dcSSimon Schubert maintenance_print_cooked_registers (char *args, int from_tty)
1166*5796c8dcSSimon Schubert {
1167*5796c8dcSSimon Schubert   regcache_print (args, regcache_dump_cooked);
1168*5796c8dcSSimon Schubert }
1169*5796c8dcSSimon Schubert 
1170*5796c8dcSSimon Schubert static void
1171*5796c8dcSSimon Schubert maintenance_print_register_groups (char *args, int from_tty)
1172*5796c8dcSSimon Schubert {
1173*5796c8dcSSimon Schubert   regcache_print (args, regcache_dump_groups);
1174*5796c8dcSSimon Schubert }
1175*5796c8dcSSimon Schubert 
1176*5796c8dcSSimon Schubert extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1177*5796c8dcSSimon Schubert 
1178*5796c8dcSSimon Schubert void
1179*5796c8dcSSimon Schubert _initialize_regcache (void)
1180*5796c8dcSSimon Schubert {
1181*5796c8dcSSimon Schubert   regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1182*5796c8dcSSimon Schubert 
1183*5796c8dcSSimon Schubert   observer_attach_target_changed (regcache_observer_target_changed);
1184*5796c8dcSSimon Schubert   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1185*5796c8dcSSimon Schubert 
1186*5796c8dcSSimon Schubert   add_com ("flushregs", class_maintenance, reg_flush_command,
1187*5796c8dcSSimon Schubert 	   _("Force gdb to flush its register cache (maintainer command)"));
1188*5796c8dcSSimon Schubert 
1189*5796c8dcSSimon Schubert   add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1190*5796c8dcSSimon Schubert Print the internal register configuration.\n\
1191*5796c8dcSSimon Schubert Takes an optional file parameter."), &maintenanceprintlist);
1192*5796c8dcSSimon Schubert   add_cmd ("raw-registers", class_maintenance,
1193*5796c8dcSSimon Schubert 	   maintenance_print_raw_registers, _("\
1194*5796c8dcSSimon Schubert Print the internal register configuration including raw values.\n\
1195*5796c8dcSSimon Schubert Takes an optional file parameter."), &maintenanceprintlist);
1196*5796c8dcSSimon Schubert   add_cmd ("cooked-registers", class_maintenance,
1197*5796c8dcSSimon Schubert 	   maintenance_print_cooked_registers, _("\
1198*5796c8dcSSimon Schubert Print the internal register configuration including cooked values.\n\
1199*5796c8dcSSimon Schubert Takes an optional file parameter."), &maintenanceprintlist);
1200*5796c8dcSSimon Schubert   add_cmd ("register-groups", class_maintenance,
1201*5796c8dcSSimon Schubert 	   maintenance_print_register_groups, _("\
1202*5796c8dcSSimon Schubert Print the internal register configuration including each register's group.\n\
1203*5796c8dcSSimon Schubert Takes an optional file parameter."),
1204*5796c8dcSSimon Schubert 	   &maintenanceprintlist);
1205*5796c8dcSSimon Schubert 
1206*5796c8dcSSimon Schubert }
1207