1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2 
3    Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4    2001, 2002, 2004 Free Software Foundation, Inc.
5 
6    This file is part of GDB.
7 
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, USA.  */
22 
23 #include "defs.h"
24 #include "inferior.h"
25 #include "target.h"
26 #include "gdbarch.h"
27 #include "gdbcmd.h"
28 #include "regcache.h"
29 #include "reggroups.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include "gdbcmd.h"		/* For maintenanceprintlist.  */
33 #include "observer.h"
34 
35 /*
36  * DATA STRUCTURE
37  *
38  * Here is the actual register cache.
39  */
40 
41 /* Per-architecture object describing the layout of a register cache.
42    Computed once when the architecture is created */
43 
44 struct gdbarch_data *regcache_descr_handle;
45 
46 struct regcache_descr
47 {
48   /* The architecture this descriptor belongs to.  */
49   struct gdbarch *gdbarch;
50 
51   /* The raw register cache.  Each raw (or hard) register is supplied
52      by the target interface.  The raw cache should not contain
53      redundant information - if the PC is constructed from two
54      registers then those registers and not the PC lives in the raw
55      cache.  */
56   int nr_raw_registers;
57   long sizeof_raw_registers;
58   long sizeof_raw_register_valid_p;
59 
60   /* The cooked register space.  Each cooked register in the range
61      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62      register.  The remaining [NR_RAW_REGISTERS
63      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
64      both raw registers and memory by the architecture methods
65      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
66   int nr_cooked_registers;
67   long sizeof_cooked_registers;
68   long sizeof_cooked_register_valid_p;
69 
70   /* Offset and size (in 8 bit bytes), of reach register in the
71      register cache.  All registers (including those in the range
72      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
73      Assigning all registers an offset makes it possible to keep
74      legacy code, such as that found in read_register_bytes() and
75      write_register_bytes() working.  */
76   long *register_offset;
77   long *sizeof_register;
78 
79   /* Cached table containing the type of each register.  */
80   struct type **register_type;
81 };
82 
83 static void *
init_regcache_descr(struct gdbarch * gdbarch)84 init_regcache_descr (struct gdbarch *gdbarch)
85 {
86   int i;
87   struct regcache_descr *descr;
88   gdb_assert (gdbarch != NULL);
89 
90   /* Create an initial, zero filled, table.  */
91   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
92   descr->gdbarch = gdbarch;
93 
94   /* Total size of the register space.  The raw registers are mapped
95      directly onto the raw register cache while the pseudo's are
96      either mapped onto raw-registers or memory.  */
97   descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
98   descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
99 
100   /* Fill in a table of register types.  */
101   descr->register_type
102     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
103   for (i = 0; i < descr->nr_cooked_registers; i++)
104     descr->register_type[i] = gdbarch_register_type (gdbarch, i);
105 
106   /* Construct a strictly RAW register cache.  Don't allow pseudo's
107      into the register cache.  */
108   descr->nr_raw_registers = NUM_REGS;
109 
110   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111      array.  This pretects GDB from erant code that accesses elements
112      of the global register_valid_p[] array in the range [NUM_REGS
113      .. NUM_REGS + NUM_PSEUDO_REGS).  */
114   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
115 
116   /* Lay out the register cache.
117 
118      NOTE: cagney/2002-05-22: Only register_type() is used when
119      constructing the register cache.  It is assumed that the
120      register's raw size, virtual size and type length are all the
121      same.  */
122 
123   {
124     long offset = 0;
125     descr->sizeof_register
126       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
127     descr->register_offset
128       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
129     for (i = 0; i < descr->nr_cooked_registers; i++)
130       {
131 	descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
132 	descr->register_offset[i] = offset;
133 	offset += descr->sizeof_register[i];
134 	gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
135       }
136     /* Set the real size of the register cache buffer.  */
137     descr->sizeof_cooked_registers = offset;
138   }
139 
140   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
141      the raw registers.  Unfortunately some code still accesses the
142      register array directly using the global registers[].  Until that
143      code has been purged, play safe and over allocating the register
144      buffer.  Ulgh!  */
145   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
146 
147   return descr;
148 }
149 
150 static struct regcache_descr *
regcache_descr(struct gdbarch * gdbarch)151 regcache_descr (struct gdbarch *gdbarch)
152 {
153   return gdbarch_data (gdbarch, regcache_descr_handle);
154 }
155 
156 /* Utility functions returning useful register attributes stored in
157    the regcache descr.  */
158 
159 struct type *
register_type(struct gdbarch * gdbarch,int regnum)160 register_type (struct gdbarch *gdbarch, int regnum)
161 {
162   struct regcache_descr *descr = regcache_descr (gdbarch);
163   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164   return descr->register_type[regnum];
165 }
166 
167 /* Utility functions returning useful register attributes stored in
168    the regcache descr.  */
169 
170 int
register_size(struct gdbarch * gdbarch,int regnum)171 register_size (struct gdbarch *gdbarch, int regnum)
172 {
173   struct regcache_descr *descr = regcache_descr (gdbarch);
174   int size;
175   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
176   size = descr->sizeof_register[regnum];
177   return size;
178 }
179 
180 /* The register cache for storing raw register values.  */
181 
182 struct regcache
183 {
184   struct regcache_descr *descr;
185   /* The register buffers.  A read-only register cache can hold the
186      full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
187      register cache can only hold [0 .. NUM_REGS).  */
188   gdb_byte *registers;
189   gdb_byte *register_valid_p;
190   /* Is this a read-only cache?  A read-only cache is used for saving
191      the target's register state (e.g, across an inferior function
192      call or just before forcing a function return).  A read-only
193      cache can only be updated via the methods regcache_dup() and
194      regcache_cpy().  The actual contents are determined by the
195      reggroup_save and reggroup_restore methods.  */
196   int readonly_p;
197 };
198 
199 struct regcache *
regcache_xmalloc(struct gdbarch * gdbarch)200 regcache_xmalloc (struct gdbarch *gdbarch)
201 {
202   struct regcache_descr *descr;
203   struct regcache *regcache;
204   gdb_assert (gdbarch != NULL);
205   descr = regcache_descr (gdbarch);
206   regcache = XMALLOC (struct regcache);
207   regcache->descr = descr;
208   regcache->registers
209     = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
210   regcache->register_valid_p
211     = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
212   regcache->readonly_p = 1;
213   return regcache;
214 }
215 
216 void
regcache_xfree(struct regcache * regcache)217 regcache_xfree (struct regcache *regcache)
218 {
219   if (regcache == NULL)
220     return;
221   xfree (regcache->registers);
222   xfree (regcache->register_valid_p);
223   xfree (regcache);
224 }
225 
226 static void
do_regcache_xfree(void * data)227 do_regcache_xfree (void *data)
228 {
229   regcache_xfree (data);
230 }
231 
232 struct cleanup *
make_cleanup_regcache_xfree(struct regcache * regcache)233 make_cleanup_regcache_xfree (struct regcache *regcache)
234 {
235   return make_cleanup (do_regcache_xfree, regcache);
236 }
237 
238 /* Return REGCACHE's architecture.  */
239 
240 struct gdbarch *
get_regcache_arch(const struct regcache * regcache)241 get_regcache_arch (const struct regcache *regcache)
242 {
243   return regcache->descr->gdbarch;
244 }
245 
246 /* Return  a pointer to register REGNUM's buffer cache.  */
247 
248 static gdb_byte *
register_buffer(const struct regcache * regcache,int regnum)249 register_buffer (const struct regcache *regcache, int regnum)
250 {
251   return regcache->registers + regcache->descr->register_offset[regnum];
252 }
253 
254 void
regcache_save(struct regcache * dst,regcache_cooked_read_ftype * cooked_read,void * src)255 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
256 	       void *src)
257 {
258   struct gdbarch *gdbarch = dst->descr->gdbarch;
259   gdb_byte buf[MAX_REGISTER_SIZE];
260   int regnum;
261   /* The DST should be `read-only', if it wasn't then the save would
262      end up trying to write the register values back out to the
263      target.  */
264   gdb_assert (dst->readonly_p);
265   /* Clear the dest.  */
266   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
267   memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
268   /* Copy over any registers (identified by their membership in the
269      save_reggroup) and mark them as valid.  The full [0 .. NUM_REGS +
270      NUM_PSEUDO_REGS) range is checked since some architectures need
271      to save/restore `cooked' registers that live in memory.  */
272   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
273     {
274       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
275 	{
276 	  int valid = cooked_read (src, regnum, buf);
277 	  if (valid)
278 	    {
279 	      memcpy (register_buffer (dst, regnum), buf,
280 		      register_size (gdbarch, regnum));
281 	      dst->register_valid_p[regnum] = 1;
282 	    }
283 	}
284     }
285 }
286 
287 void
regcache_restore(struct regcache * dst,regcache_cooked_read_ftype * cooked_read,void * cooked_read_context)288 regcache_restore (struct regcache *dst,
289 		  regcache_cooked_read_ftype *cooked_read,
290 		  void *cooked_read_context)
291 {
292   struct gdbarch *gdbarch = dst->descr->gdbarch;
293   gdb_byte buf[MAX_REGISTER_SIZE];
294   int regnum;
295   /* The dst had better not be read-only.  If it is, the `restore'
296      doesn't make much sense.  */
297   gdb_assert (!dst->readonly_p);
298   /* Copy over any registers, being careful to only restore those that
299      were both saved and need to be restored.  The full [0 .. NUM_REGS
300      + NUM_PSEUDO_REGS) range is checked since some architectures need
301      to save/restore `cooked' registers that live in memory.  */
302   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
303     {
304       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
305 	{
306 	  int valid = cooked_read (cooked_read_context, regnum, buf);
307 	  if (valid)
308 	    regcache_cooked_write (dst, regnum, buf);
309 	}
310     }
311 }
312 
313 static int
do_cooked_read(void * src,int regnum,gdb_byte * buf)314 do_cooked_read (void *src, int regnum, gdb_byte *buf)
315 {
316   struct regcache *regcache = src;
317   if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
318     /* Don't even think about fetching a register from a read-only
319        cache when the register isn't yet valid.  There isn't a target
320        from which the register value can be fetched.  */
321     return 0;
322   regcache_cooked_read (regcache, regnum, buf);
323   return 1;
324 }
325 
326 
327 void
regcache_cpy(struct regcache * dst,struct regcache * src)328 regcache_cpy (struct regcache *dst, struct regcache *src)
329 {
330   int i;
331   gdb_byte *buf;
332   gdb_assert (src != NULL && dst != NULL);
333   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
334   gdb_assert (src != dst);
335   gdb_assert (src->readonly_p || dst->readonly_p);
336   if (!src->readonly_p)
337     regcache_save (dst, do_cooked_read, src);
338   else if (!dst->readonly_p)
339     regcache_restore (dst, do_cooked_read, src);
340   else
341     regcache_cpy_no_passthrough (dst, src);
342 }
343 
344 void
regcache_cpy_no_passthrough(struct regcache * dst,struct regcache * src)345 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
346 {
347   int i;
348   gdb_assert (src != NULL && dst != NULL);
349   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
350   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
351      move of data into the current_regcache().  Doing this would be
352      silly - it would mean that valid_p would be completely invalid.  */
353   gdb_assert (dst != current_regcache);
354   memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
355   memcpy (dst->register_valid_p, src->register_valid_p,
356 	  dst->descr->sizeof_raw_register_valid_p);
357 }
358 
359 struct regcache *
regcache_dup(struct regcache * src)360 regcache_dup (struct regcache *src)
361 {
362   struct regcache *newbuf;
363   gdb_assert (current_regcache != NULL);
364   newbuf = regcache_xmalloc (src->descr->gdbarch);
365   regcache_cpy (newbuf, src);
366   return newbuf;
367 }
368 
369 struct regcache *
regcache_dup_no_passthrough(struct regcache * src)370 regcache_dup_no_passthrough (struct regcache *src)
371 {
372   struct regcache *newbuf;
373   gdb_assert (current_regcache != NULL);
374   newbuf = regcache_xmalloc (src->descr->gdbarch);
375   regcache_cpy_no_passthrough (newbuf, src);
376   return newbuf;
377 }
378 
379 int
regcache_valid_p(struct regcache * regcache,int regnum)380 regcache_valid_p (struct regcache *regcache, int regnum)
381 {
382   gdb_assert (regcache != NULL);
383   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
384   return regcache->register_valid_p[regnum];
385 }
386 
387 gdb_byte *
deprecated_grub_regcache_for_registers(struct regcache * regcache)388 deprecated_grub_regcache_for_registers (struct regcache *regcache)
389 {
390   return regcache->registers;
391 }
392 
393 /* Global structure containing the current regcache.  */
394 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
395    deprecated_register_valid[] currently point into this structure.  */
396 struct regcache *current_regcache;
397 
398 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
399    recording if the register values have been changed (eg. by the
400    user).  Therefore all registers must be written back to the
401    target when appropriate.  */
402 
403 /* The thread/process associated with the current set of registers. */
404 
405 static ptid_t registers_ptid;
406 
407 /*
408  * FUNCTIONS:
409  */
410 
411 /* REGISTER_CACHED()
412 
413    Returns 0 if the value is not in the cache (needs fetch).
414           >0 if the value is in the cache.
415 	  <0 if the value is permanently unavailable (don't ask again).  */
416 
417 int
register_cached(int regnum)418 register_cached (int regnum)
419 {
420   return current_regcache->register_valid_p[regnum];
421 }
422 
423 /* Record that REGNUM's value is cached if STATE is >0, uncached but
424    fetchable if STATE is 0, and uncached and unfetchable if STATE is <0.  */
425 
426 void
set_register_cached(int regnum,int state)427 set_register_cached (int regnum, int state)
428 {
429   gdb_assert (regnum >= 0);
430   gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
431   current_regcache->register_valid_p[regnum] = state;
432 }
433 
434 /* Observer for the target_changed event.  */
435 
436 void
regcache_observer_target_changed(struct target_ops * target)437 regcache_observer_target_changed (struct target_ops *target)
438 {
439   registers_changed ();
440 }
441 
442 /* Low level examining and depositing of registers.
443 
444    The caller is responsible for making sure that the inferior is
445    stopped before calling the fetching routines, or it will get
446    garbage.  (a change from GDB version 3, in which the caller got the
447    value from the last stop).  */
448 
449 /* REGISTERS_CHANGED ()
450 
451    Indicate that registers may have changed, so invalidate the cache.  */
452 
453 void
registers_changed(void)454 registers_changed (void)
455 {
456   int i;
457 
458   registers_ptid = pid_to_ptid (-1);
459 
460   /* Force cleanup of any alloca areas if using C alloca instead of
461      a builtin alloca.  This particular call is used to clean up
462      areas allocated by low level target code which may build up
463      during lengthy interactions between gdb and the target before
464      gdb gives control to the user (ie watchpoints).  */
465   alloca (0);
466 
467   for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
468     set_register_cached (i, 0);
469 
470   if (deprecated_registers_changed_hook)
471     deprecated_registers_changed_hook ();
472 }
473 
474 /* DEPRECATED_REGISTERS_FETCHED ()
475 
476    Indicate that all registers have been fetched, so mark them all valid.  */
477 
478 /* FIXME: cagney/2001-12-04: This function is DEPRECATED.  The target
479    code was blatting the registers[] array and then calling this.
480    Since targets should only be using regcache_raw_supply() the need for
481    this function/hack is eliminated.  */
482 
483 void
deprecated_registers_fetched(void)484 deprecated_registers_fetched (void)
485 {
486   int i;
487 
488   for (i = 0; i < NUM_REGS; i++)
489     set_register_cached (i, 1);
490   /* Do not assume that the pseudo-regs have also been fetched.
491      Fetching all real regs NEVER accounts for pseudo-regs.  */
492 }
493 
494 /* deprecated_read_register_bytes and deprecated_write_register_bytes
495    are generally a *BAD* idea.  They are inefficient because they need
496    to check for partial updates, which can only be done by scanning
497    through all of the registers and seeing if the bytes that are being
498    read/written fall inside of an invalid register.  [The main reason
499    this is necessary is that register sizes can vary, so a simple
500    index won't suffice.]  It is far better to call read_register_gen
501    and write_register_gen if you want to get at the raw register
502    contents, as it only takes a regnum as an argument, and therefore
503    can't do a partial register update.
504 
505    Prior to the recent fixes to check for partial updates, both read
506    and deprecated_write_register_bytes always checked to see if any
507    registers were stale, and then called target_fetch_registers (-1)
508    to update the whole set.  This caused really slowed things down for
509    remote targets.  */
510 
511 /* Copy INLEN bytes of consecutive data from registers
512    starting with the INREGBYTE'th byte of register data
513    into memory at MYADDR.  */
514 
515 void
deprecated_read_register_bytes(int in_start,gdb_byte * in_buf,int in_len)516 deprecated_read_register_bytes (int in_start, gdb_byte *in_buf, int in_len)
517 {
518   int in_end = in_start + in_len;
519   int regnum;
520   gdb_byte reg_buf[MAX_REGISTER_SIZE];
521 
522   /* See if we are trying to read bytes from out-of-date registers.  If so,
523      update just those registers.  */
524 
525   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
526     {
527       int reg_start;
528       int reg_end;
529       int reg_len;
530       int start;
531       int end;
532       int byte;
533 
534       reg_start = DEPRECATED_REGISTER_BYTE (regnum);
535       reg_len = register_size (current_gdbarch, regnum);
536       reg_end = reg_start + reg_len;
537 
538       if (reg_end <= in_start || in_end <= reg_start)
539 	/* The range the user wants to read doesn't overlap with regnum.  */
540 	continue;
541 
542       if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
543 	/* Force the cache to fetch the entire register.  */
544 	deprecated_read_register_gen (regnum, reg_buf);
545 
546       /* Legacy note: This function, for some reason, allows a NULL
547          input buffer.  If the buffer is NULL, the registers are still
548          fetched, just the final transfer is skipped. */
549       if (in_buf == NULL)
550 	continue;
551 
552       /* start = max (reg_start, in_start) */
553       if (reg_start > in_start)
554 	start = reg_start;
555       else
556 	start = in_start;
557 
558       /* end = min (reg_end, in_end) */
559       if (reg_end < in_end)
560 	end = reg_end;
561       else
562 	end = in_end;
563 
564       /* Transfer just the bytes common to both IN_BUF and REG_BUF */
565       for (byte = start; byte < end; byte++)
566 	{
567 	  in_buf[byte - in_start] = reg_buf[byte - reg_start];
568 	}
569     }
570 }
571 
572 void
regcache_raw_read(struct regcache * regcache,int regnum,gdb_byte * buf)573 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
574 {
575   gdb_assert (regcache != NULL && buf != NULL);
576   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
577   /* Make certain that the register cache is up-to-date with respect
578      to the current thread.  This switching shouldn't be necessary
579      only there is still only one target side register cache.  Sigh!
580      On the bright side, at least there is a regcache object.  */
581   if (!regcache->readonly_p)
582     {
583       gdb_assert (regcache == current_regcache);
584       if (! ptid_equal (registers_ptid, inferior_ptid))
585 	{
586 	  registers_changed ();
587 	  registers_ptid = inferior_ptid;
588 	}
589       if (!register_cached (regnum))
590 	target_fetch_registers (regnum);
591 #if 0
592       /* FIXME: cagney/2004-08-07: At present a number of targets
593 	 forget (or didn't know that they needed) to set this leading to
594 	 panics.  Also is the problem that targets need to indicate
595 	 that a register is in one of the possible states: valid,
596 	 undefined, unknown.  The last of which isn't yet
597 	 possible.  */
598       gdb_assert (register_cached (regnum));
599 #endif
600     }
601   /* Copy the value directly into the register cache.  */
602   memcpy (buf, register_buffer (regcache, regnum),
603 	  regcache->descr->sizeof_register[regnum]);
604 }
605 
606 void
regcache_raw_read_signed(struct regcache * regcache,int regnum,LONGEST * val)607 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
608 {
609   gdb_byte *buf;
610   gdb_assert (regcache != NULL);
611   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
612   buf = alloca (regcache->descr->sizeof_register[regnum]);
613   regcache_raw_read (regcache, regnum, buf);
614   (*val) = extract_signed_integer (buf,
615 				   regcache->descr->sizeof_register[regnum]);
616 }
617 
618 void
regcache_raw_read_unsigned(struct regcache * regcache,int regnum,ULONGEST * val)619 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
620 			    ULONGEST *val)
621 {
622   gdb_byte *buf;
623   gdb_assert (regcache != NULL);
624   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
625   buf = alloca (regcache->descr->sizeof_register[regnum]);
626   regcache_raw_read (regcache, regnum, buf);
627   (*val) = extract_unsigned_integer (buf,
628 				     regcache->descr->sizeof_register[regnum]);
629 }
630 
631 void
regcache_raw_write_signed(struct regcache * regcache,int regnum,LONGEST val)632 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
633 {
634   void *buf;
635   gdb_assert (regcache != NULL);
636   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
637   buf = alloca (regcache->descr->sizeof_register[regnum]);
638   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
639   regcache_raw_write (regcache, regnum, buf);
640 }
641 
642 void
regcache_raw_write_unsigned(struct regcache * regcache,int regnum,ULONGEST val)643 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
644 			     ULONGEST val)
645 {
646   void *buf;
647   gdb_assert (regcache != NULL);
648   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
649   buf = alloca (regcache->descr->sizeof_register[regnum]);
650   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
651   regcache_raw_write (regcache, regnum, buf);
652 }
653 
654 void
deprecated_read_register_gen(int regnum,gdb_byte * buf)655 deprecated_read_register_gen (int regnum, gdb_byte *buf)
656 {
657   gdb_assert (current_regcache != NULL);
658   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
659   regcache_cooked_read (current_regcache, regnum, buf);
660 }
661 
662 void
regcache_cooked_read(struct regcache * regcache,int regnum,gdb_byte * buf)663 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
664 {
665   gdb_assert (regnum >= 0);
666   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
667   if (regnum < regcache->descr->nr_raw_registers)
668     regcache_raw_read (regcache, regnum, buf);
669   else if (regcache->readonly_p
670 	   && regnum < regcache->descr->nr_cooked_registers
671 	   && regcache->register_valid_p[regnum])
672     /* Read-only register cache, perhaps the cooked value was cached?  */
673     memcpy (buf, register_buffer (regcache, regnum),
674 	    regcache->descr->sizeof_register[regnum]);
675   else
676     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
677 				  regnum, buf);
678 }
679 
680 void
regcache_cooked_read_signed(struct regcache * regcache,int regnum,LONGEST * val)681 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
682 			     LONGEST *val)
683 {
684   gdb_byte *buf;
685   gdb_assert (regcache != NULL);
686   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
687   buf = alloca (regcache->descr->sizeof_register[regnum]);
688   regcache_cooked_read (regcache, regnum, buf);
689   (*val) = extract_signed_integer (buf,
690 				   regcache->descr->sizeof_register[regnum]);
691 }
692 
693 void
regcache_cooked_read_unsigned(struct regcache * regcache,int regnum,ULONGEST * val)694 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
695 			       ULONGEST *val)
696 {
697   gdb_byte *buf;
698   gdb_assert (regcache != NULL);
699   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
700   buf = alloca (regcache->descr->sizeof_register[regnum]);
701   regcache_cooked_read (regcache, regnum, buf);
702   (*val) = extract_unsigned_integer (buf,
703 				     regcache->descr->sizeof_register[regnum]);
704 }
705 
706 void
regcache_cooked_write_signed(struct regcache * regcache,int regnum,LONGEST val)707 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
708 			      LONGEST val)
709 {
710   void *buf;
711   gdb_assert (regcache != NULL);
712   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
713   buf = alloca (regcache->descr->sizeof_register[regnum]);
714   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
715   regcache_cooked_write (regcache, regnum, buf);
716 }
717 
718 void
regcache_cooked_write_unsigned(struct regcache * regcache,int regnum,ULONGEST val)719 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
720 				ULONGEST val)
721 {
722   void *buf;
723   gdb_assert (regcache != NULL);
724   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
725   buf = alloca (regcache->descr->sizeof_register[regnum]);
726   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
727   regcache_cooked_write (regcache, regnum, buf);
728 }
729 
730 void
regcache_raw_write(struct regcache * regcache,int regnum,const gdb_byte * buf)731 regcache_raw_write (struct regcache *regcache, int regnum,
732 		    const gdb_byte *buf)
733 {
734   gdb_assert (regcache != NULL && buf != NULL);
735   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
736   gdb_assert (!regcache->readonly_p);
737 
738   /* On the sparc, writing %g0 is a no-op, so we don't even want to
739      change the registers array if something writes to this register.  */
740   if (CANNOT_STORE_REGISTER (regnum))
741     return;
742 
743   /* Make certain that the correct cache is selected.  */
744   gdb_assert (regcache == current_regcache);
745   if (! ptid_equal (registers_ptid, inferior_ptid))
746     {
747       registers_changed ();
748       registers_ptid = inferior_ptid;
749     }
750 
751   /* If we have a valid copy of the register, and new value == old
752      value, then don't bother doing the actual store. */
753   if (regcache_valid_p (regcache, regnum)
754       && (memcmp (register_buffer (regcache, regnum), buf,
755 		  regcache->descr->sizeof_register[regnum]) == 0))
756     return;
757 
758   target_prepare_to_store ();
759   memcpy (register_buffer (regcache, regnum), buf,
760 	  regcache->descr->sizeof_register[regnum]);
761   regcache->register_valid_p[regnum] = 1;
762   target_store_registers (regnum);
763 }
764 
765 void
deprecated_write_register_gen(int regnum,gdb_byte * buf)766 deprecated_write_register_gen (int regnum, gdb_byte *buf)
767 {
768   gdb_assert (current_regcache != NULL);
769   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
770   regcache_cooked_write (current_regcache, regnum, buf);
771 }
772 
773 void
regcache_cooked_write(struct regcache * regcache,int regnum,const gdb_byte * buf)774 regcache_cooked_write (struct regcache *regcache, int regnum,
775 		       const gdb_byte *buf)
776 {
777   gdb_assert (regnum >= 0);
778   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
779   if (regnum < regcache->descr->nr_raw_registers)
780     regcache_raw_write (regcache, regnum, buf);
781   else
782     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
783 				   regnum, buf);
784 }
785 
786 /* Copy INLEN bytes of consecutive data from memory at MYADDR
787    into registers starting with the MYREGSTART'th byte of register data.  */
788 
789 void
deprecated_write_register_bytes(int myregstart,gdb_byte * myaddr,int inlen)790 deprecated_write_register_bytes (int myregstart, gdb_byte *myaddr, int inlen)
791 {
792   int myregend = myregstart + inlen;
793   int regnum;
794 
795   target_prepare_to_store ();
796 
797   /* Scan through the registers updating any that are covered by the
798      range myregstart<=>myregend using write_register_gen, which does
799      nice things like handling threads, and avoiding updates when the
800      new and old contents are the same.  */
801 
802   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
803     {
804       int regstart, regend;
805 
806       regstart = DEPRECATED_REGISTER_BYTE (regnum);
807       regend = regstart + register_size (current_gdbarch, regnum);
808 
809       /* Is this register completely outside the range the user is writing?  */
810       if (myregend <= regstart || regend <= myregstart)
811 	/* do nothing */ ;
812 
813       /* Is this register completely within the range the user is writing?  */
814       else if (myregstart <= regstart && regend <= myregend)
815 	deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
816 
817       /* The register partially overlaps the range being written.  */
818       else
819 	{
820 	  gdb_byte regbuf[MAX_REGISTER_SIZE];
821 	  /* What's the overlap between this register's bytes and
822              those the caller wants to write?  */
823 	  int overlapstart = max (regstart, myregstart);
824 	  int overlapend   = min (regend,   myregend);
825 
826 	  /* We may be doing a partial update of an invalid register.
827 	     Update it from the target before scribbling on it.  */
828 	  deprecated_read_register_gen (regnum, regbuf);
829 
830 	  target_store_registers (regnum);
831 	}
832     }
833 }
834 
835 /* Perform a partial register transfer using a read, modify, write
836    operation.  */
837 
838 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
839 				    void *buf);
840 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
841 				     const void *buf);
842 
843 static void
regcache_xfer_part(struct regcache * regcache,int regnum,int offset,int len,void * in,const void * out,void (* read)(struct regcache * regcache,int regnum,gdb_byte * buf),void (* write)(struct regcache * regcache,int regnum,const gdb_byte * buf))844 regcache_xfer_part (struct regcache *regcache, int regnum,
845 		    int offset, int len, void *in, const void *out,
846 		    void (*read) (struct regcache *regcache, int regnum,
847 				  gdb_byte *buf),
848 		    void (*write) (struct regcache *regcache, int regnum,
849 				   const gdb_byte *buf))
850 {
851   struct regcache_descr *descr = regcache->descr;
852   gdb_byte reg[MAX_REGISTER_SIZE];
853   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
854   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
855   /* Something to do?  */
856   if (offset + len == 0)
857     return;
858   /* Read (when needed) ... */
859   if (in != NULL
860       || offset > 0
861       || offset + len < descr->sizeof_register[regnum])
862     {
863       gdb_assert (read != NULL);
864       read (regcache, regnum, reg);
865     }
866   /* ... modify ... */
867   if (in != NULL)
868     memcpy (in, reg + offset, len);
869   if (out != NULL)
870     memcpy (reg + offset, out, len);
871   /* ... write (when needed).  */
872   if (out != NULL)
873     {
874       gdb_assert (write != NULL);
875       write (regcache, regnum, reg);
876     }
877 }
878 
879 void
regcache_raw_read_part(struct regcache * regcache,int regnum,int offset,int len,gdb_byte * buf)880 regcache_raw_read_part (struct regcache *regcache, int regnum,
881 			int offset, int len, gdb_byte *buf)
882 {
883   struct regcache_descr *descr = regcache->descr;
884   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
885   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
886 		      regcache_raw_read, regcache_raw_write);
887 }
888 
889 void
regcache_raw_write_part(struct regcache * regcache,int regnum,int offset,int len,const gdb_byte * buf)890 regcache_raw_write_part (struct regcache *regcache, int regnum,
891 			 int offset, int len, const gdb_byte *buf)
892 {
893   struct regcache_descr *descr = regcache->descr;
894   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
895   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
896 		      regcache_raw_read, regcache_raw_write);
897 }
898 
899 void
regcache_cooked_read_part(struct regcache * regcache,int regnum,int offset,int len,gdb_byte * buf)900 regcache_cooked_read_part (struct regcache *regcache, int regnum,
901 			   int offset, int len, gdb_byte *buf)
902 {
903   struct regcache_descr *descr = regcache->descr;
904   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
905   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
906 		      regcache_cooked_read, regcache_cooked_write);
907 }
908 
909 void
regcache_cooked_write_part(struct regcache * regcache,int regnum,int offset,int len,const gdb_byte * buf)910 regcache_cooked_write_part (struct regcache *regcache, int regnum,
911 			    int offset, int len, const gdb_byte *buf)
912 {
913   struct regcache_descr *descr = regcache->descr;
914   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
915   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
916 		      regcache_cooked_read, regcache_cooked_write);
917 }
918 
919 /* Hack to keep code that view the register buffer as raw bytes
920    working.  */
921 
922 int
register_offset_hack(struct gdbarch * gdbarch,int regnum)923 register_offset_hack (struct gdbarch *gdbarch, int regnum)
924 {
925   struct regcache_descr *descr = regcache_descr (gdbarch);
926   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
927   return descr->register_offset[regnum];
928 }
929 
930 /* Hack to keep code using register_bytes working.  */
931 
932 int
deprecated_register_bytes(void)933 deprecated_register_bytes (void)
934 {
935   return current_regcache->descr->sizeof_raw_registers;
936 }
937 
938 /* Return the contents of register REGNUM as an unsigned integer.  */
939 
940 ULONGEST
read_register(int regnum)941 read_register (int regnum)
942 {
943   gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
944   deprecated_read_register_gen (regnum, buf);
945   return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
946 }
947 
948 ULONGEST
read_register_pid(int regnum,ptid_t ptid)949 read_register_pid (int regnum, ptid_t ptid)
950 {
951   ptid_t save_ptid;
952   int save_pid;
953   CORE_ADDR retval;
954 
955   if (ptid_equal (ptid, inferior_ptid))
956     return read_register (regnum);
957 
958   save_ptid = inferior_ptid;
959 
960   inferior_ptid = ptid;
961 
962   retval = read_register (regnum);
963 
964   inferior_ptid = save_ptid;
965 
966   return retval;
967 }
968 
969 /* Store VALUE into the raw contents of register number REGNUM.  */
970 
971 void
write_register(int regnum,LONGEST val)972 write_register (int regnum, LONGEST val)
973 {
974   void *buf;
975   int size;
976   size = register_size (current_gdbarch, regnum);
977   buf = alloca (size);
978   store_signed_integer (buf, size, (LONGEST) val);
979   deprecated_write_register_gen (regnum, buf);
980 }
981 
982 void
write_register_pid(int regnum,CORE_ADDR val,ptid_t ptid)983 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
984 {
985   ptid_t save_ptid;
986 
987   if (ptid_equal (ptid, inferior_ptid))
988     {
989       write_register (regnum, val);
990       return;
991     }
992 
993   save_ptid = inferior_ptid;
994 
995   inferior_ptid = ptid;
996 
997   write_register (regnum, val);
998 
999   inferior_ptid = save_ptid;
1000 }
1001 
1002 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
1003 
1004 void
regcache_raw_supply(struct regcache * regcache,int regnum,const void * buf)1005 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1006 {
1007   void *regbuf;
1008   size_t size;
1009 
1010   gdb_assert (regcache != NULL);
1011   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1012   gdb_assert (!regcache->readonly_p);
1013 
1014   /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1015      CURRENT_REGCACHE specially here.  */
1016   if (regcache == current_regcache
1017       && !ptid_equal (registers_ptid, inferior_ptid))
1018     {
1019       registers_changed ();
1020       registers_ptid = inferior_ptid;
1021     }
1022 
1023   regbuf = register_buffer (regcache, regnum);
1024   size = regcache->descr->sizeof_register[regnum];
1025 
1026   if (buf)
1027     memcpy (regbuf, buf, size);
1028   else
1029     memset (regbuf, 0, size);
1030 
1031   /* Mark the register as cached.  */
1032   regcache->register_valid_p[regnum] = 1;
1033 }
1034 
1035 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
1036 
1037 void
regcache_raw_collect(const struct regcache * regcache,int regnum,void * buf)1038 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1039 {
1040   const void *regbuf;
1041   size_t size;
1042 
1043   gdb_assert (regcache != NULL && buf != NULL);
1044   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1045 
1046   regbuf = register_buffer (regcache, regnum);
1047   size = regcache->descr->sizeof_register[regnum];
1048   memcpy (buf, regbuf, size);
1049 }
1050 
1051 
1052 /* read_pc, write_pc, read_sp, etc.  Special handling for registers
1053    PC, SP, and FP.  */
1054 
1055 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
1056    read_sp(), will eventually be replaced by per-frame methods.
1057    Instead of relying on the global INFERIOR_PTID, they will use the
1058    contextual information provided by the FRAME.  These functions do
1059    not belong in the register cache.  */
1060 
1061 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1062    write_pc_pid() and write_pc(), all need to be replaced by something
1063    that does not rely on global state.  But what?  */
1064 
1065 CORE_ADDR
read_pc_pid(ptid_t ptid)1066 read_pc_pid (ptid_t ptid)
1067 {
1068   ptid_t saved_inferior_ptid;
1069   CORE_ADDR pc_val;
1070 
1071   /* In case ptid != inferior_ptid. */
1072   saved_inferior_ptid = inferior_ptid;
1073   inferior_ptid = ptid;
1074 
1075   if (TARGET_READ_PC_P ())
1076     pc_val = TARGET_READ_PC (ptid);
1077   /* Else use per-frame method on get_current_frame.  */
1078   else if (PC_REGNUM >= 0)
1079     {
1080       CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1081       pc_val = ADDR_BITS_REMOVE (raw_val);
1082     }
1083   else
1084     internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
1085 
1086   inferior_ptid = saved_inferior_ptid;
1087   return pc_val;
1088 }
1089 
1090 CORE_ADDR
read_pc(void)1091 read_pc (void)
1092 {
1093   return read_pc_pid (inferior_ptid);
1094 }
1095 
1096 void
generic_target_write_pc(CORE_ADDR pc,ptid_t ptid)1097 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1098 {
1099   if (PC_REGNUM >= 0)
1100     write_register_pid (PC_REGNUM, pc, ptid);
1101   else
1102     internal_error (__FILE__, __LINE__,
1103 		    _("generic_target_write_pc"));
1104 }
1105 
1106 void
write_pc_pid(CORE_ADDR pc,ptid_t ptid)1107 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1108 {
1109   ptid_t saved_inferior_ptid;
1110 
1111   /* In case ptid != inferior_ptid. */
1112   saved_inferior_ptid = inferior_ptid;
1113   inferior_ptid = ptid;
1114 
1115   TARGET_WRITE_PC (pc, ptid);
1116 
1117   inferior_ptid = saved_inferior_ptid;
1118 }
1119 
1120 void
write_pc(CORE_ADDR pc)1121 write_pc (CORE_ADDR pc)
1122 {
1123   write_pc_pid (pc, inferior_ptid);
1124 }
1125 
1126 /* Cope with strage ways of getting to the stack and frame pointers */
1127 
1128 CORE_ADDR
read_sp(void)1129 read_sp (void)
1130 {
1131   if (TARGET_READ_SP_P ())
1132     return TARGET_READ_SP ();
1133   else if (gdbarch_unwind_sp_p (current_gdbarch))
1134     return get_frame_sp (get_current_frame ());
1135   else if (SP_REGNUM >= 0)
1136     /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1137        about the architecture so put it at the end.  */
1138     return read_register (SP_REGNUM);
1139   internal_error (__FILE__, __LINE__, _("read_sp: Unable to find SP"));
1140 }
1141 
1142 static void
reg_flush_command(char * command,int from_tty)1143 reg_flush_command (char *command, int from_tty)
1144 {
1145   /* Force-flush the register cache.  */
1146   registers_changed ();
1147   if (from_tty)
1148     printf_filtered (_("Register cache flushed.\n"));
1149 }
1150 
1151 static void
build_regcache(void)1152 build_regcache (void)
1153 {
1154   current_regcache = regcache_xmalloc (current_gdbarch);
1155   current_regcache->readonly_p = 0;
1156 }
1157 
1158 static void
dump_endian_bytes(struct ui_file * file,enum bfd_endian endian,const unsigned char * buf,long len)1159 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1160 		   const unsigned char *buf, long len)
1161 {
1162   int i;
1163   switch (endian)
1164     {
1165     case BFD_ENDIAN_BIG:
1166       for (i = 0; i < len; i++)
1167 	fprintf_unfiltered (file, "%02x", buf[i]);
1168       break;
1169     case BFD_ENDIAN_LITTLE:
1170       for (i = len - 1; i >= 0; i--)
1171 	fprintf_unfiltered (file, "%02x", buf[i]);
1172       break;
1173     default:
1174       internal_error (__FILE__, __LINE__, _("Bad switch"));
1175     }
1176 }
1177 
1178 enum regcache_dump_what
1179 {
1180   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1181 };
1182 
1183 static void
regcache_dump(struct regcache * regcache,struct ui_file * file,enum regcache_dump_what what_to_dump)1184 regcache_dump (struct regcache *regcache, struct ui_file *file,
1185 	       enum regcache_dump_what what_to_dump)
1186 {
1187   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1188   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1189   int regnum;
1190   int footnote_nr = 0;
1191   int footnote_register_size = 0;
1192   int footnote_register_offset = 0;
1193   int footnote_register_type_name_null = 0;
1194   long register_offset = 0;
1195   unsigned char buf[MAX_REGISTER_SIZE];
1196 
1197 #if 0
1198   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1199 		      regcache->descr->nr_raw_registers);
1200   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1201 		      regcache->descr->nr_cooked_registers);
1202   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1203 		      regcache->descr->sizeof_raw_registers);
1204   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1205 		      regcache->descr->sizeof_raw_register_valid_p);
1206   fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1207   fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1208 #endif
1209 
1210   gdb_assert (regcache->descr->nr_cooked_registers
1211 	      == (NUM_REGS + NUM_PSEUDO_REGS));
1212 
1213   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1214     {
1215       /* Name.  */
1216       if (regnum < 0)
1217 	fprintf_unfiltered (file, " %-10s", "Name");
1218       else
1219 	{
1220 	  const char *p = REGISTER_NAME (regnum);
1221 	  if (p == NULL)
1222 	    p = "";
1223 	  else if (p[0] == '\0')
1224 	    p = "''";
1225 	  fprintf_unfiltered (file, " %-10s", p);
1226 	}
1227 
1228       /* Number.  */
1229       if (regnum < 0)
1230 	fprintf_unfiltered (file, " %4s", "Nr");
1231       else
1232 	fprintf_unfiltered (file, " %4d", regnum);
1233 
1234       /* Relative number.  */
1235       if (regnum < 0)
1236 	fprintf_unfiltered (file, " %4s", "Rel");
1237       else if (regnum < NUM_REGS)
1238 	fprintf_unfiltered (file, " %4d", regnum);
1239       else
1240 	fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1241 
1242       /* Offset.  */
1243       if (regnum < 0)
1244 	fprintf_unfiltered (file, " %6s  ", "Offset");
1245       else
1246 	{
1247 	  fprintf_unfiltered (file, " %6ld",
1248 			      regcache->descr->register_offset[regnum]);
1249 	  if (register_offset != regcache->descr->register_offset[regnum]
1250 	      || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
1251 	      || (regnum > 0
1252 		  && (regcache->descr->register_offset[regnum]
1253 		      != (regcache->descr->register_offset[regnum - 1]
1254 			  + regcache->descr->sizeof_register[regnum - 1])))
1255 	      )
1256 	    {
1257 	      if (!footnote_register_offset)
1258 		footnote_register_offset = ++footnote_nr;
1259 	      fprintf_unfiltered (file, "*%d", footnote_register_offset);
1260 	    }
1261 	  else
1262 	    fprintf_unfiltered (file, "  ");
1263 	  register_offset = (regcache->descr->register_offset[regnum]
1264 			     + regcache->descr->sizeof_register[regnum]);
1265 	}
1266 
1267       /* Size.  */
1268       if (regnum < 0)
1269 	fprintf_unfiltered (file, " %5s ", "Size");
1270       else
1271 	fprintf_unfiltered (file, " %5ld",
1272 			    regcache->descr->sizeof_register[regnum]);
1273 
1274       /* Type.  */
1275       {
1276 	const char *t;
1277 	if (regnum < 0)
1278 	  t = "Type";
1279 	else
1280 	  {
1281 	    static const char blt[] = "builtin_type";
1282 	    t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1283 	    if (t == NULL)
1284 	      {
1285 		char *n;
1286 		if (!footnote_register_type_name_null)
1287 		  footnote_register_type_name_null = ++footnote_nr;
1288 		n = xstrprintf ("*%d", footnote_register_type_name_null);
1289 		make_cleanup (xfree, n);
1290 		t = n;
1291 	      }
1292 	    /* Chop a leading builtin_type.  */
1293 	    if (strncmp (t, blt, strlen (blt)) == 0)
1294 	      t += strlen (blt);
1295 	  }
1296 	fprintf_unfiltered (file, " %-15s", t);
1297       }
1298 
1299       /* Leading space always present.  */
1300       fprintf_unfiltered (file, " ");
1301 
1302       /* Value, raw.  */
1303       if (what_to_dump == regcache_dump_raw)
1304 	{
1305 	  if (regnum < 0)
1306 	    fprintf_unfiltered (file, "Raw value");
1307 	  else if (regnum >= regcache->descr->nr_raw_registers)
1308 	    fprintf_unfiltered (file, "<cooked>");
1309 	  else if (!regcache_valid_p (regcache, regnum))
1310 	    fprintf_unfiltered (file, "<invalid>");
1311 	  else
1312 	    {
1313 	      regcache_raw_read (regcache, regnum, buf);
1314 	      fprintf_unfiltered (file, "0x");
1315 	      dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1316 				 regcache->descr->sizeof_register[regnum]);
1317 	    }
1318 	}
1319 
1320       /* Value, cooked.  */
1321       if (what_to_dump == regcache_dump_cooked)
1322 	{
1323 	  if (regnum < 0)
1324 	    fprintf_unfiltered (file, "Cooked value");
1325 	  else
1326 	    {
1327 	      regcache_cooked_read (regcache, regnum, buf);
1328 	      fprintf_unfiltered (file, "0x");
1329 	      dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1330 				 regcache->descr->sizeof_register[regnum]);
1331 	    }
1332 	}
1333 
1334       /* Group members.  */
1335       if (what_to_dump == regcache_dump_groups)
1336 	{
1337 	  if (regnum < 0)
1338 	    fprintf_unfiltered (file, "Groups");
1339 	  else
1340 	    {
1341 	      const char *sep = "";
1342 	      struct reggroup *group;
1343 	      for (group = reggroup_next (gdbarch, NULL);
1344 		   group != NULL;
1345 		   group = reggroup_next (gdbarch, group))
1346 		{
1347 		  if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1348 		    {
1349 		      fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1350 		      sep = ",";
1351 		    }
1352 		}
1353 	    }
1354 	}
1355 
1356       fprintf_unfiltered (file, "\n");
1357     }
1358 
1359   if (footnote_register_size)
1360     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1361 			footnote_register_size);
1362   if (footnote_register_offset)
1363     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1364 			footnote_register_offset);
1365   if (footnote_register_type_name_null)
1366     fprintf_unfiltered (file,
1367 			"*%d: Register type's name NULL.\n",
1368 			footnote_register_type_name_null);
1369   do_cleanups (cleanups);
1370 }
1371 
1372 static void
regcache_print(char * args,enum regcache_dump_what what_to_dump)1373 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1374 {
1375   if (args == NULL)
1376     regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1377   else
1378     {
1379       struct ui_file *file = gdb_fopen (args, "w");
1380       if (file == NULL)
1381 	perror_with_name (_("maintenance print architecture"));
1382       regcache_dump (current_regcache, file, what_to_dump);
1383       ui_file_delete (file);
1384     }
1385 }
1386 
1387 static void
maintenance_print_registers(char * args,int from_tty)1388 maintenance_print_registers (char *args, int from_tty)
1389 {
1390   regcache_print (args, regcache_dump_none);
1391 }
1392 
1393 static void
maintenance_print_raw_registers(char * args,int from_tty)1394 maintenance_print_raw_registers (char *args, int from_tty)
1395 {
1396   regcache_print (args, regcache_dump_raw);
1397 }
1398 
1399 static void
maintenance_print_cooked_registers(char * args,int from_tty)1400 maintenance_print_cooked_registers (char *args, int from_tty)
1401 {
1402   regcache_print (args, regcache_dump_cooked);
1403 }
1404 
1405 static void
maintenance_print_register_groups(char * args,int from_tty)1406 maintenance_print_register_groups (char *args, int from_tty)
1407 {
1408   regcache_print (args, regcache_dump_groups);
1409 }
1410 
1411 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1412 
1413 void
_initialize_regcache(void)1414 _initialize_regcache (void)
1415 {
1416   regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1417   DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1418   deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1419 
1420   observer_attach_target_changed (regcache_observer_target_changed);
1421 
1422   add_com ("flushregs", class_maintenance, reg_flush_command,
1423 	   _("Force gdb to flush its register cache (maintainer command)"));
1424 
1425    /* Initialize the thread/process associated with the current set of
1426       registers.  For now, -1 is special, and means `no current process'.  */
1427   registers_ptid = pid_to_ptid (-1);
1428 
1429   add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1430 Print the internal register configuration.\n\
1431 Takes an optional file parameter."), &maintenanceprintlist);
1432   add_cmd ("raw-registers", class_maintenance,
1433 	   maintenance_print_raw_registers, _("\
1434 Print the internal register configuration including raw values.\n\
1435 Takes an optional file parameter."), &maintenanceprintlist);
1436   add_cmd ("cooked-registers", class_maintenance,
1437 	   maintenance_print_cooked_registers, _("\
1438 Print the internal register configuration including cooked values.\n\
1439 Takes an optional file parameter."), &maintenanceprintlist);
1440   add_cmd ("register-groups", class_maintenance,
1441 	   maintenance_print_register_groups, _("\
1442 Print the internal register configuration including each register's group.\n\
1443 Takes an optional file parameter."),
1444 	   &maintenanceprintlist);
1445 
1446 }
1447