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 /* Observer for the target_changed event.  */
466
467 void
468 regcache_observer_target_changed (struct target_ops *target)
469 {
470   registers_changed ();
471 }
472
473 /* Low level examining and depositing of registers.
474
475    The caller is responsible for making sure that the inferior is
476    stopped before calling the fetching routines, or it will get
477    garbage.  (a change from GDB version 3, in which the caller got the
478    value from the last stop).  */
479
480 /* REGISTERS_CHANGED ()
481
482    Indicate that registers may have changed, so invalidate the cache.  */
483
484 void
485 registers_changed (void)
486 {
487   int i;
488
489   registers_ptid = pid_to_ptid (-1);
490
491   /* Force cleanup of any alloca areas if using C alloca instead of
492      a builtin alloca.  This particular call is used to clean up
493      areas allocated by low level target code which may build up
494      during lengthy interactions between gdb and the target before
495      gdb gives control to the user (ie watchpoints).  */
496   alloca (0);
497
498   for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
499     set_register_cached (i, 0);
500
501   if (deprecated_registers_changed_hook)
502     deprecated_registers_changed_hook ();
503 }
504
505 /* DEPRECATED_REGISTERS_FETCHED ()
506
507    Indicate that all registers have been fetched, so mark them all valid.  */
508
509 /* FIXME: cagney/2001-12-04: This function is DEPRECATED.  The target
510    code was blatting the registers[] array and then calling this.
511    Since targets should only be using regcache_raw_supply() the need for
512    this function/hack is eliminated.  */
513
514 void
515 deprecated_registers_fetched (void)
516 {
517   int i;
518
519   for (i = 0; i < NUM_REGS; i++)
520     set_register_cached (i, 1);
521   /* Do not assume that the pseudo-regs have also been fetched.
522      Fetching all real regs NEVER accounts for pseudo-regs.  */
523 }
524
525 /* deprecated_read_register_bytes and deprecated_write_register_bytes
526    are generally a *BAD* idea.  They are inefficient because they need
527    to check for partial updates, which can only be done by scanning
528    through all of the registers and seeing if the bytes that are being
529    read/written fall inside of an invalid register.  [The main reason
530    this is necessary is that register sizes can vary, so a simple
531    index won't suffice.]  It is far better to call read_register_gen
532    and write_register_gen if you want to get at the raw register
533    contents, as it only takes a regnum as an argument, and therefore
534    can't do a partial register update.
535
536    Prior to the recent fixes to check for partial updates, both read
537    and deprecated_write_register_bytes always checked to see if any
538    registers were stale, and then called target_fetch_registers (-1)
539    to update the whole set.  This caused really slowed things down for
540    remote targets.  */
541
542 /* Copy INLEN bytes of consecutive data from registers
543    starting with the INREGBYTE'th byte of register data
544    into memory at MYADDR.  */
545
546 void
547 deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
548 {
549   int in_end = in_start + in_len;
550   int regnum;
551   char reg_buf[MAX_REGISTER_SIZE];
552
553   /* See if we are trying to read bytes from out-of-date registers.  If so,
554      update just those registers.  */
555
556   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
557     {
558       int reg_start;
559       int reg_end;
560       int reg_len;
561       int start;
562       int end;
563       int byte;
564
565       reg_start = DEPRECATED_REGISTER_BYTE (regnum);
566       reg_len = DEPRECATED_REGISTER_RAW_SIZE (regnum);
567       reg_end = reg_start + reg_len;
568
569       if (reg_end <= in_start || in_end <= reg_start)
570         /* The range the user wants to read doesn't overlap with regnum.  */
571         continue;
572
573       if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
574         /* Force the cache to fetch the entire register.  */
575         deprecated_read_register_gen (regnum, reg_buf);
576       else
577         /* Legacy note: even though this register is ``invalid'' we
578            still need to return something.  It would appear that some
579            code relies on apparent gaps in the register array also
580            being returned.  */
581         /* FIXME: cagney/2001-08-18: This is just silly.  It defeats
582            the entire register read/write flow of control.  Must
583            resist temptation to return 0xdeadbeef.  */
584         memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
585
586       /* Legacy note: This function, for some reason, allows a NULL
587          input buffer.  If the buffer is NULL, the registers are still
588          fetched, just the final transfer is skipped. */
589       if (in_buf == NULL)
590         continue;
591
592       /* start = max (reg_start, in_start) */
593       if (reg_start > in_start)
594         start = reg_start;
595       else
596         start = in_start;
597
598       /* end = min (reg_end, in_end) */
599       if (reg_end < in_end)
600         end = reg_end;
601       else
602         end = in_end;
603
604       /* Transfer just the bytes common to both IN_BUF and REG_BUF */
605       for (byte = start; byte < end; byte++)
606         {
607           in_buf[byte - in_start] = reg_buf[byte - reg_start];
608         }
609     }
610 }
611
612 void
613 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
614 {
615   gdb_assert (regcache != NULL && buf != NULL);
616   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
617   /* Make certain that the register cache is up-to-date with respect
618      to the current thread.  This switching shouldn't be necessary
619      only there is still only one target side register cache.  Sigh!
620      On the bright side, at least there is a regcache object.  */
621   if (!regcache->readonly_p)
622     {
623       gdb_assert (regcache == current_regcache);
624       if (! ptid_equal (registers_ptid, inferior_ptid))
625         {
626           registers_changed ();
627           registers_ptid = inferior_ptid;
628         }
629       if (!register_cached (regnum))
630         target_fetch_registers (regnum);
631     }
632   /* Copy the value directly into the register cache.  */
633   memcpy (buf, register_buffer (regcache, regnum),
634           regcache->descr->sizeof_register[regnum]);
635 }
636
637 void
638 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
639 {
640   char *buf;
641   gdb_assert (regcache != NULL);
642   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
643   buf = alloca (regcache->descr->sizeof_register[regnum]);
644   regcache_raw_read (regcache, regnum, buf);
645   (*val) = extract_signed_integer (buf,
646                                    regcache->descr->sizeof_register[regnum]);
647 }
648
649 void
650 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
651                             ULONGEST *val)
652 {
653   char *buf;
654   gdb_assert (regcache != NULL);
655   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
656   buf = alloca (regcache->descr->sizeof_register[regnum]);
657   regcache_raw_read (regcache, regnum, buf);
658   (*val) = extract_unsigned_integer (buf,
659                                      regcache->descr->sizeof_register[regnum]);
660 }
661
662 void
663 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
664 {
665   void *buf;
666   gdb_assert (regcache != NULL);
667   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
668   buf = alloca (regcache->descr->sizeof_register[regnum]);
669   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
670   regcache_raw_write (regcache, regnum, buf);
671 }
672
673 void
674 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
675                              ULONGEST val)
676 {
677   void *buf;
678   gdb_assert (regcache != NULL);
679   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
680   buf = alloca (regcache->descr->sizeof_register[regnum]);
681   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
682   regcache_raw_write (regcache, regnum, buf);
683 }
684
685 void
686 deprecated_read_register_gen (int regnum, char *buf)
687 {
688   gdb_assert (current_regcache != NULL);
689   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
690   regcache_cooked_read (current_regcache, regnum, buf);
691 }
692
693 void
694 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
695 {
696   gdb_assert (regnum >= 0);
697   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
698   if (regnum < regcache->descr->nr_raw_registers)
699     regcache_raw_read (regcache, regnum, buf);
700   else if (regcache->readonly_p
701            && regnum < regcache->descr->nr_cooked_registers
702            && regcache->register_valid_p[regnum])
703     /* Read-only register cache, perhaps the cooked value was cached?  */
704     memcpy (buf, register_buffer (regcache, regnum),
705             regcache->descr->sizeof_register[regnum]);
706   else
707     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
708                                   regnum, buf);
709 }
710
711 void
712 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
713                              LONGEST *val)
714 {
715   char *buf;
716   gdb_assert (regcache != NULL);
717   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
718   buf = alloca (regcache->descr->sizeof_register[regnum]);
719   regcache_cooked_read (regcache, regnum, buf);
720   (*val) = extract_signed_integer (buf,
721                                    regcache->descr->sizeof_register[regnum]);
722 }
723
724 void
725 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
726                                ULONGEST *val)
727 {
728   char *buf;
729   gdb_assert (regcache != NULL);
730   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
731   buf = alloca (regcache->descr->sizeof_register[regnum]);
732   regcache_cooked_read (regcache, regnum, buf);
733   (*val) = extract_unsigned_integer (buf,
734                                      regcache->descr->sizeof_register[regnum]);
735 }
736
737 void
738 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
739                               LONGEST val)
740 {
741   void *buf;
742   gdb_assert (regcache != NULL);
743   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
744   buf = alloca (regcache->descr->sizeof_register[regnum]);
745   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
746   regcache_cooked_write (regcache, regnum, buf);
747 }
748
749 void
750 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
751                                 ULONGEST val)
752 {
753   void *buf;
754   gdb_assert (regcache != NULL);
755   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
756   buf = alloca (regcache->descr->sizeof_register[regnum]);
757   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
758   regcache_cooked_write (regcache, regnum, buf);
759 }
760
761 void
762 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
763 {
764   gdb_assert (regcache != NULL && buf != NULL);
765   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
766   gdb_assert (!regcache->readonly_p);
767
768   /* On the sparc, writing %g0 is a no-op, so we don't even want to
769      change the registers array if something writes to this register.  */
770   if (CANNOT_STORE_REGISTER (regnum))
771     return;
772
773   /* Make certain that the correct cache is selected.  */
774   gdb_assert (regcache == current_regcache);
775   if (! ptid_equal (registers_ptid, inferior_ptid))
776     {
777       registers_changed ();
778       registers_ptid = inferior_ptid;
779     }
780
781   /* If we have a valid copy of the register, and new value == old
782      value, then don't bother doing the actual store. */
783   if (regcache_valid_p (regcache, regnum)
784       && (memcmp (register_buffer (regcache, regnum), buf,
785                   regcache->descr->sizeof_register[regnum]) == 0))
786     return;
787
788   target_prepare_to_store ();
789   memcpy (register_buffer (regcache, regnum), buf,
790           regcache->descr->sizeof_register[regnum]);
791   regcache->register_valid_p[regnum] = 1;
792   target_store_registers (regnum);
793 }
794
795 void
796 deprecated_write_register_gen (int regnum, char *buf)
797 {
798   gdb_assert (current_regcache != NULL);
799   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
800   regcache_cooked_write (current_regcache, regnum, buf);
801 }
802
803 void
804 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
805 {
806   gdb_assert (regnum >= 0);
807   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
808   if (regnum < regcache->descr->nr_raw_registers)
809     regcache_raw_write (regcache, regnum, buf);
810   else
811     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
812                                    regnum, buf);
813 }
814
815 /* Copy INLEN bytes of consecutive data from memory at MYADDR
816    into registers starting with the MYREGSTART'th byte of register data.  */
817
818 void
819 deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
820 {
821   int myregend = myregstart + inlen;
822   int regnum;
823
824   target_prepare_to_store ();
825
826   /* Scan through the registers updating any that are covered by the
827      range myregstart<=>myregend using write_register_gen, which does
828      nice things like handling threads, and avoiding updates when the
829      new and old contents are the same.  */
830
831   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
832     {
833       int regstart, regend;
834
835       regstart = DEPRECATED_REGISTER_BYTE (regnum);
836       regend = regstart + DEPRECATED_REGISTER_RAW_SIZE (regnum);
837
838       /* Is this register completely outside the range the user is writing?  */
839       if (myregend <= regstart || regend <= myregstart)
840         /* do nothing */ ;              
841
842       /* Is this register completely within the range the user is writing?  */
843       else if (myregstart <= regstart && regend <= myregend)
844         deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
845
846       /* The register partially overlaps the range being written.  */
847       else
848         {
849           char regbuf[MAX_REGISTER_SIZE];
850           /* What's the overlap between this register's bytes and
851              those the caller wants to write?  */
852           int overlapstart = max (regstart, myregstart);
853           int overlapend   = min (regend,   myregend);
854
855           /* We may be doing a partial update of an invalid register.
856              Update it from the target before scribbling on it.  */
857           deprecated_read_register_gen (regnum, regbuf);
858
859           memcpy (&deprecated_registers[overlapstart],
860                   myaddr + (overlapstart - myregstart),
861                   overlapend - overlapstart);
862
863           target_store_registers (regnum);
864         }
865     }
866 }
867
868 /* Perform a partial register transfer using a read, modify, write
869    operation.  */
870
871 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
872                                     void *buf);
873 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
874                                      const void *buf);
875
876 static void
877 regcache_xfer_part (struct regcache *regcache, int regnum,
878                     int offset, int len, void *in, const void *out,
879                     regcache_read_ftype *read, regcache_write_ftype *write)
880 {
881   struct regcache_descr *descr = regcache->descr;
882   bfd_byte reg[MAX_REGISTER_SIZE];
883   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
884   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
885   /* Something to do?  */
886   if (offset + len == 0)
887     return;
888   /* Read (when needed) ... */
889   if (in != NULL
890       || offset > 0
891       || offset + len < descr->sizeof_register[regnum])
892     {
893       gdb_assert (read != NULL);
894       read (regcache, regnum, reg);
895     }
896   /* ... modify ... */
897   if (in != NULL)
898     memcpy (in, reg + offset, len);
899   if (out != NULL)
900     memcpy (reg + offset, out, len);
901   /* ... write (when needed).  */
902   if (out != NULL)
903     {
904       gdb_assert (write != NULL);
905       write (regcache, regnum, reg);
906     }
907 }
908
909 void
910 regcache_raw_read_part (struct regcache *regcache, int regnum,
911                         int offset, int len, void *buf)
912 {
913   struct regcache_descr *descr = regcache->descr;
914   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
915   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
916                       regcache_raw_read, regcache_raw_write);
917 }
918
919 void
920 regcache_raw_write_part (struct regcache *regcache, int regnum,
921                          int offset, int len, const void *buf)
922 {
923   struct regcache_descr *descr = regcache->descr;
924   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
925   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
926                       regcache_raw_read, regcache_raw_write);
927 }
928
929 void
930 regcache_cooked_read_part (struct regcache *regcache, int regnum,
931                            int offset, int len, void *buf)
932 {
933   struct regcache_descr *descr = regcache->descr;
934   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
935   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
936                       regcache_cooked_read, regcache_cooked_write);
937 }
938
939 void
940 regcache_cooked_write_part (struct regcache *regcache, int regnum,
941                             int offset, int len, const void *buf)
942 {
943   struct regcache_descr *descr = regcache->descr;
944   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
945   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
946                       regcache_cooked_read, regcache_cooked_write);
947 }
948
949 /* Hack to keep code that view the register buffer as raw bytes
950    working.  */
951
952 int
953 register_offset_hack (struct gdbarch *gdbarch, int regnum)
954 {
955   struct regcache_descr *descr = regcache_descr (gdbarch);
956   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
957   return descr->register_offset[regnum];
958 }
959
960 /* Return the contents of register REGNUM as an unsigned integer.  */
961
962 ULONGEST
963 read_register (int regnum)
964 {
965   char *buf = alloca (DEPRECATED_REGISTER_RAW_SIZE (regnum));
966   deprecated_read_register_gen (regnum, buf);
967   return (extract_unsigned_integer (buf, DEPRECATED_REGISTER_RAW_SIZE (regnum)));
968 }
969
970 ULONGEST
971 read_register_pid (int regnum, ptid_t ptid)
972 {
973   ptid_t save_ptid;
974   int save_pid;
975   CORE_ADDR retval;
976
977   if (ptid_equal (ptid, inferior_ptid))
978     return read_register (regnum);
979
980   save_ptid = inferior_ptid;
981
982   inferior_ptid = ptid;
983
984   retval = read_register (regnum);
985
986   inferior_ptid = save_ptid;
987
988   return retval;
989 }
990
991 /* Store VALUE into the raw contents of register number REGNUM.  */
992
993 void
994 write_register (int regnum, LONGEST val)
995 {
996   void *buf;
997   int size;
998   size = DEPRECATED_REGISTER_RAW_SIZE (regnum);
999   buf = alloca (size);
1000   store_signed_integer (buf, size, (LONGEST) val);
1001   deprecated_write_register_gen (regnum, buf);
1002 }
1003
1004 void
1005 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1006 {
1007   ptid_t save_ptid;
1008
1009   if (ptid_equal (ptid, inferior_ptid))
1010     {
1011       write_register (regnum, val);
1012       return;
1013     }
1014
1015   save_ptid = inferior_ptid;
1016
1017   inferior_ptid = ptid;
1018
1019   write_register (regnum, val);
1020
1021   inferior_ptid = save_ptid;
1022 }
1023
1024 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
1025
1026 void
1027 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1028 {
1029   void *regbuf;
1030   size_t size;
1031
1032   gdb_assert (regcache != NULL);
1033   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1034   gdb_assert (!regcache->readonly_p);
1035
1036   /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1037      CURRENT_REGCACHE specially here.  */
1038   if (regcache == current_regcache
1039       && !ptid_equal (registers_ptid, inferior_ptid))
1040     {
1041       registers_changed ();
1042       registers_ptid = inferior_ptid;
1043     }
1044
1045   regbuf = register_buffer (regcache, regnum);
1046   size = regcache->descr->sizeof_register[regnum];
1047
1048   if (buf)
1049     memcpy (regbuf, buf, size);
1050   else
1051     memset (regbuf, 0, size);
1052
1053   /* Mark the register as cached.  */
1054   regcache->register_valid_p[regnum] = 1;
1055 }
1056
1057 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
1058
1059 void
1060 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1061 {
1062   const void *regbuf;
1063   size_t size;
1064
1065   gdb_assert (regcache != NULL && buf != NULL);
1066   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1067
1068   regbuf = register_buffer (regcache, regnum);
1069   size = regcache->descr->sizeof_register[regnum];
1070   memcpy (buf, regbuf, size);
1071 }
1072
1073
1074 /* read_pc, write_pc, read_sp, deprecated_read_fp, etc.  Special
1075    handling for registers PC, SP, and FP.  */
1076
1077 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(),
1078    read_sp(), and deprecated_read_fp(), will eventually be replaced by
1079    per-frame methods.  Instead of relying on the global INFERIOR_PTID,
1080    they will use the contextual information provided by the FRAME.
1081    These functions do not belong in the register cache.  */
1082
1083 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1084    write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to
1085    be replaced by something that does not rely on global state.  But
1086    what?  */
1087
1088 CORE_ADDR
1089 read_pc_pid (ptid_t ptid)
1090 {
1091   ptid_t saved_inferior_ptid;
1092   CORE_ADDR pc_val;
1093
1094   /* In case ptid != inferior_ptid. */
1095   saved_inferior_ptid = inferior_ptid;
1096   inferior_ptid = ptid;
1097
1098   if (TARGET_READ_PC_P ())
1099     pc_val = TARGET_READ_PC (ptid);
1100   /* Else use per-frame method on get_current_frame.  */
1101   else if (PC_REGNUM >= 0)
1102     {
1103       CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1104       pc_val = ADDR_BITS_REMOVE (raw_val);
1105     }
1106   else
1107     internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
1108
1109   inferior_ptid = saved_inferior_ptid;
1110   return pc_val;
1111 }
1112
1113 CORE_ADDR
1114 read_pc (void)
1115 {
1116   return read_pc_pid (inferior_ptid);
1117 }
1118
1119 void
1120 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1121 {
1122   if (PC_REGNUM >= 0)
1123     write_register_pid (PC_REGNUM, pc, ptid);
1124   else
1125     internal_error (__FILE__, __LINE__,
1126                     "generic_target_write_pc");
1127 }
1128
1129 void
1130 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1131 {
1132   ptid_t saved_inferior_ptid;
1133
1134   /* In case ptid != inferior_ptid. */
1135   saved_inferior_ptid = inferior_ptid;
1136   inferior_ptid = ptid;
1137
1138   TARGET_WRITE_PC (pc, ptid);
1139
1140   inferior_ptid = saved_inferior_ptid;
1141 }
1142
1143 void
1144 write_pc (CORE_ADDR pc)
1145 {
1146   write_pc_pid (pc, inferior_ptid);
1147 }
1148
1149 /* Cope with strage ways of getting to the stack and frame pointers */
1150
1151 CORE_ADDR
1152 read_sp (void)
1153 {
1154   if (TARGET_READ_SP_P ())
1155     return TARGET_READ_SP ();
1156   else if (gdbarch_unwind_sp_p (current_gdbarch))
1157     return get_frame_sp (get_current_frame ());
1158   else if (SP_REGNUM >= 0)
1159     /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1160        about the architecture so put it at the end.  */
1161     return read_register (SP_REGNUM);
1162   internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
1163 }
1164
1165 void
1166 deprecated_write_sp (CORE_ADDR val)
1167 {
1168   gdb_assert (SP_REGNUM >= 0);
1169   write_register (SP_REGNUM, val);
1170 }
1171
1172 CORE_ADDR
1173 deprecated_read_fp (void)
1174 {
1175   if (DEPRECATED_TARGET_READ_FP_P ())
1176     return DEPRECATED_TARGET_READ_FP ();
1177   else if (DEPRECATED_FP_REGNUM >= 0)
1178     return read_register (DEPRECATED_FP_REGNUM);
1179   else
1180     internal_error (__FILE__, __LINE__, "deprecated_read_fp");
1181 }
1182
1183 static void
1184 reg_flush_command (char *command, int from_tty)
1185 {
1186   /* Force-flush the register cache.  */
1187   registers_changed ();
1188   if (from_tty)
1189     printf_filtered ("Register cache flushed.\n");
1190 }
1191
1192 static void
1193 build_regcache (void)
1194 {
1195   current_regcache = regcache_xmalloc (current_gdbarch);
1196   current_regcache->readonly_p = 0;
1197   deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
1198   deprecated_register_valid = current_regcache->register_valid_p;
1199 }
1200
1201 static void
1202 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1203                    const unsigned char *buf, long len)
1204 {
1205   int i;
1206   switch (endian)
1207     {
1208     case BFD_ENDIAN_BIG:
1209       for (i = 0; i < len; i++)
1210         fprintf_unfiltered (file, "%02x", buf[i]);
1211       break;
1212     case BFD_ENDIAN_LITTLE:
1213       for (i = len - 1; i >= 0; i--)
1214         fprintf_unfiltered (file, "%02x", buf[i]);
1215       break;
1216     default:
1217       internal_error (__FILE__, __LINE__, "Bad switch");
1218     }
1219 }
1220
1221 enum regcache_dump_what
1222 {
1223   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1224 };
1225
1226 static void
1227 regcache_dump (struct regcache *regcache, struct ui_file *file,
1228                enum regcache_dump_what what_to_dump)
1229 {
1230   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1231   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1232   int regnum;
1233   int footnote_nr = 0;
1234   int footnote_register_size = 0;
1235   int footnote_register_offset = 0;
1236   int footnote_register_type_name_null = 0;
1237   long register_offset = 0;
1238   unsigned char buf[MAX_REGISTER_SIZE];
1239
1240 #if 0
1241   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1242                       regcache->descr->nr_raw_registers);
1243   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1244                       regcache->descr->nr_cooked_registers);
1245   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1246                       regcache->descr->sizeof_raw_registers);
1247   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1248                       regcache->descr->sizeof_raw_register_valid_p);
1249   fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1250   fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1251 #endif
1252
1253   gdb_assert (regcache->descr->nr_cooked_registers
1254               == (NUM_REGS + NUM_PSEUDO_REGS));
1255
1256   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1257     {
1258       /* Name.  */
1259       if (regnum < 0)
1260         fprintf_unfiltered (file, " %-10s", "Name");
1261       else
1262         {
1263           const char *p = REGISTER_NAME (regnum);
1264           if (p == NULL)
1265             p = "";
1266           else if (p[0] == '\0')
1267             p = "''";
1268           fprintf_unfiltered (file, " %-10s", p);
1269         }
1270
1271       /* Number.  */
1272       if (regnum < 0)
1273         fprintf_unfiltered (file, " %4s", "Nr");
1274       else
1275         fprintf_unfiltered (file, " %4d", regnum);
1276
1277       /* Relative number.  */
1278       if (regnum < 0)
1279         fprintf_unfiltered (file, " %4s", "Rel");
1280       else if (regnum < NUM_REGS)
1281         fprintf_unfiltered (file, " %4d", regnum);
1282       else
1283         fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1284
1285       /* Offset.  */
1286       if (regnum < 0)
1287         fprintf_unfiltered (file, " %6s  ", "Offset");
1288       else
1289         {
1290           fprintf_unfiltered (file, " %6ld",
1291                               regcache->descr->register_offset[regnum]);
1292           if (register_offset != regcache->descr->register_offset[regnum]
1293               || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
1294               || (regnum > 0
1295                   && (regcache->descr->register_offset[regnum]
1296                       != (regcache->descr->register_offset[regnum - 1]
1297                           + regcache->descr->sizeof_register[regnum - 1])))
1298               )
1299             {
1300               if (!footnote_register_offset)
1301                 footnote_register_offset = ++footnote_nr;
1302               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1303             }
1304           else
1305             fprintf_unfiltered (file, "  ");
1306           register_offset = (regcache->descr->register_offset[regnum]
1307                              + regcache->descr->sizeof_register[regnum]);
1308         }
1309
1310       /* Size.  */
1311       if (regnum < 0)
1312         fprintf_unfiltered (file, " %5s ", "Size");
1313       else
1314         {
1315           fprintf_unfiltered (file, " %5ld",
1316                               regcache->descr->sizeof_register[regnum]);
1317           if ((regcache->descr->sizeof_register[regnum]
1318                != DEPRECATED_REGISTER_RAW_SIZE (regnum))
1319               || (regcache->descr->sizeof_register[regnum]
1320                   != DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum))
1321               || (regcache->descr->sizeof_register[regnum]
1322                   != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1323                                                  regnum)))
1324               )
1325             {
1326               if (!footnote_register_size)
1327                 footnote_register_size = ++footnote_nr;
1328               fprintf_unfiltered (file, "*%d", footnote_register_size);
1329             }
1330           else
1331             fprintf_unfiltered (file, " ");
1332         }
1333
1334       /* Type.  */
1335       {
1336         const char *t;
1337         if (regnum < 0)
1338           t = "Type";
1339         else
1340           {
1341             static const char blt[] = "builtin_type";
1342             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1343             if (t == NULL)
1344               {
1345                 char *n;
1346                 if (!footnote_register_type_name_null)
1347                   footnote_register_type_name_null = ++footnote_nr;
1348                 n = xstrprintf ("*%d", footnote_register_type_name_null);
1349                 make_cleanup (xfree, n);
1350                 t = n;
1351               }
1352             /* Chop a leading builtin_type.  */
1353             if (strncmp (t, blt, strlen (blt)) == 0)
1354               t += strlen (blt);
1355           }
1356         fprintf_unfiltered (file, " %-15s", t);
1357       }
1358
1359       /* Leading space always present.  */
1360       fprintf_unfiltered (file, " ");
1361
1362       /* Value, raw.  */
1363       if (what_to_dump == regcache_dump_raw)
1364         {
1365           if (regnum < 0)
1366             fprintf_unfiltered (file, "Raw value");
1367           else if (regnum >= regcache->descr->nr_raw_registers)
1368             fprintf_unfiltered (file, "<cooked>");
1369           else if (!regcache_valid_p (regcache, regnum))
1370             fprintf_unfiltered (file, "<invalid>");
1371           else
1372             {
1373               regcache_raw_read (regcache, regnum, buf);
1374               fprintf_unfiltered (file, "0x");
1375               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1376                                  DEPRECATED_REGISTER_RAW_SIZE (regnum));
1377             }
1378         }
1379
1380       /* Value, cooked.  */
1381       if (what_to_dump == regcache_dump_cooked)
1382         {
1383           if (regnum < 0)
1384             fprintf_unfiltered (file, "Cooked value");
1385           else
1386             {
1387               regcache_cooked_read (regcache, regnum, buf);
1388               fprintf_unfiltered (file, "0x");
1389               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1390                                  DEPRECATED_REGISTER_VIRTUAL_SIZE (regnum));
1391             }
1392         }
1393
1394       /* Group members.  */
1395       if (what_to_dump == regcache_dump_groups)
1396         {
1397           if (regnum < 0)
1398             fprintf_unfiltered (file, "Groups");
1399           else
1400             {
1401               const char *sep = "";
1402               struct reggroup *group;
1403               for (group = reggroup_next (gdbarch, NULL);
1404                    group != NULL;
1405                    group = reggroup_next (gdbarch, group))
1406                 {
1407                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1408                     {
1409                       fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1410                       sep = ",";
1411                     }
1412                 }
1413             }
1414         }
1415
1416       fprintf_unfiltered (file, "\n");
1417     }
1418
1419   if (footnote_register_size)
1420     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1421                         footnote_register_size);
1422   if (footnote_register_offset)
1423     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1424                         footnote_register_offset);
1425   if (footnote_register_type_name_null)
1426     fprintf_unfiltered (file, 
1427                         "*%d: Register type's name NULL.\n",
1428                         footnote_register_type_name_null);
1429   do_cleanups (cleanups);
1430 }
1431
1432 static void
1433 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1434 {
1435   if (args == NULL)
1436     regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1437   else
1438     {
1439       struct ui_file *file = gdb_fopen (args, "w");
1440       if (file == NULL)
1441         perror_with_name ("maintenance print architecture");
1442       regcache_dump (current_regcache, file, what_to_dump);    
1443       ui_file_delete (file);
1444     }
1445 }
1446
1447 static void
1448 maintenance_print_registers (char *args, int from_tty)
1449 {
1450   regcache_print (args, regcache_dump_none);
1451 }
1452
1453 static void
1454 maintenance_print_raw_registers (char *args, int from_tty)
1455 {
1456   regcache_print (args, regcache_dump_raw);
1457 }
1458
1459 static void
1460 maintenance_print_cooked_registers (char *args, int from_tty)
1461 {
1462   regcache_print (args, regcache_dump_cooked);
1463 }
1464
1465 static void
1466 maintenance_print_register_groups (char *args, int from_tty)
1467 {
1468   regcache_print (args, regcache_dump_groups);
1469 }
1470
1471 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1472
1473 void
1474 _initialize_regcache (void)
1475 {
1476   regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1477   DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1478   DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_registers);
1479   DEPRECATED_REGISTER_GDBARCH_SWAP (deprecated_register_valid);
1480   deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1481
1482   observer_attach_target_changed (regcache_observer_target_changed);
1483
1484   add_com ("flushregs", class_maintenance, reg_flush_command,
1485            "Force gdb to flush its register cache (maintainer command)");
1486
1487    /* Initialize the thread/process associated with the current set of
1488       registers.  For now, -1 is special, and means `no current process'.  */
1489   registers_ptid = pid_to_ptid (-1);
1490
1491   add_cmd ("registers", class_maintenance,
1492            maintenance_print_registers,
1493            "Print the internal register configuration.\
1494 Takes an optional file parameter.",
1495            &maintenanceprintlist);
1496   add_cmd ("raw-registers", class_maintenance,
1497            maintenance_print_raw_registers,
1498            "Print the internal register configuration including raw values.\
1499 Takes an optional file parameter.",
1500            &maintenanceprintlist);
1501   add_cmd ("cooked-registers", class_maintenance,
1502            maintenance_print_cooked_registers,
1503            "Print the internal register configuration including cooked values.\
1504 Takes an optional file parameter.",
1505            &maintenanceprintlist);
1506   add_cmd ("register-groups", class_maintenance,
1507            maintenance_print_register_groups,
1508            "Print the internal register configuration including each register's group.\
1509 Takes an optional file parameter.",
1510            &maintenanceprintlist);
1511
1512 }