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