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