Introduce regcache_get_ptid
[external/binutils.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2017 Free Software Foundation, Inc.
4
5    This file is part of GDB.
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "target.h"
23 #include "gdbarch.h"
24 #include "gdbcmd.h"
25 #include "regcache.h"
26 #include "reggroups.h"
27 #include "observer.h"
28 #include "remote.h"
29 #include "valprint.h"
30 #include "regset.h"
31
32 /*
33  * DATA STRUCTURE
34  *
35  * Here is the actual register cache.
36  */
37
38 /* Per-architecture object describing the layout of a register cache.
39    Computed once when the architecture is created.  */
40
41 struct gdbarch_data *regcache_descr_handle;
42
43 struct regcache_descr
44 {
45   /* The architecture this descriptor belongs to.  */
46   struct gdbarch *gdbarch;
47
48   /* The raw register cache.  Each raw (or hard) register is supplied
49      by the target interface.  The raw cache should not contain
50      redundant information - if the PC is constructed from two
51      registers then those registers and not the PC lives in the raw
52      cache.  */
53   int nr_raw_registers;
54   long sizeof_raw_registers;
55   long sizeof_raw_register_status;
56
57   /* The cooked register space.  Each cooked register in the range
58      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
59      register.  The remaining [NR_RAW_REGISTERS
60      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
61      both raw registers and memory by the architecture methods
62      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
63   int nr_cooked_registers;
64   long sizeof_cooked_registers;
65   long sizeof_cooked_register_status;
66
67   /* Offset and size (in 8 bit bytes), of each register in the
68      register cache.  All registers (including those in the range
69      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
70      offset.  */
71   long *register_offset;
72   long *sizeof_register;
73
74   /* Cached table containing the type of each register.  */
75   struct type **register_type;
76 };
77
78 static void *
79 init_regcache_descr (struct gdbarch *gdbarch)
80 {
81   int i;
82   struct regcache_descr *descr;
83   gdb_assert (gdbarch != NULL);
84
85   /* Create an initial, zero filled, table.  */
86   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
87   descr->gdbarch = gdbarch;
88
89   /* Total size of the register space.  The raw registers are mapped
90      directly onto the raw register cache while the pseudo's are
91      either mapped onto raw-registers or memory.  */
92   descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
93                                + gdbarch_num_pseudo_regs (gdbarch);
94   descr->sizeof_cooked_register_status
95     = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
96
97   /* Fill in a table of register types.  */
98   descr->register_type
99     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
100                               struct type *);
101   for (i = 0; i < descr->nr_cooked_registers; i++)
102     descr->register_type[i] = gdbarch_register_type (gdbarch, i);
103
104   /* Construct a strictly RAW register cache.  Don't allow pseudo's
105      into the register cache.  */
106   descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
107   descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
108
109   /* Lay out the register cache.
110
111      NOTE: cagney/2002-05-22: Only register_type() is used when
112      constructing the register cache.  It is assumed that the
113      register's raw size, virtual size and type length are all the
114      same.  */
115
116   {
117     long offset = 0;
118
119     descr->sizeof_register
120       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
121     descr->register_offset
122       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
123     for (i = 0; i < descr->nr_raw_registers; i++)
124       {
125         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
126         descr->register_offset[i] = offset;
127         offset += descr->sizeof_register[i];
128         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
129       }
130     /* Set the real size of the raw register cache buffer.  */
131     descr->sizeof_raw_registers = offset;
132
133     for (; i < descr->nr_cooked_registers; i++)
134       {
135         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
136         descr->register_offset[i] = offset;
137         offset += descr->sizeof_register[i];
138         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
139       }
140     /* Set the real size of the readonly register cache buffer.  */
141     descr->sizeof_cooked_registers = offset;
142   }
143
144   return descr;
145 }
146
147 static struct regcache_descr *
148 regcache_descr (struct gdbarch *gdbarch)
149 {
150   return (struct regcache_descr *) gdbarch_data (gdbarch,
151                                                  regcache_descr_handle);
152 }
153
154 /* Utility functions returning useful register attributes stored in
155    the regcache descr.  */
156
157 struct type *
158 register_type (struct gdbarch *gdbarch, int regnum)
159 {
160   struct regcache_descr *descr = regcache_descr (gdbarch);
161
162   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
163   return descr->register_type[regnum];
164 }
165
166 /* Utility functions returning useful register attributes stored in
167    the regcache descr.  */
168
169 int
170 register_size (struct gdbarch *gdbarch, int regnum)
171 {
172   struct regcache_descr *descr = regcache_descr (gdbarch);
173   int size;
174
175   gdb_assert (regnum >= 0
176               && regnum < (gdbarch_num_regs (gdbarch)
177                            + gdbarch_num_pseudo_regs (gdbarch)));
178   size = descr->sizeof_register[regnum];
179   return size;
180 }
181
182 /* See common/common-regcache.h.  */
183
184 int
185 regcache_register_size (const struct regcache *regcache, int n)
186 {
187   return register_size (get_regcache_arch (regcache), n);
188 }
189
190 /* The register cache for storing raw register values.  */
191
192 struct regcache
193 {
194   struct regcache_descr *descr;
195
196   /* The address space of this register cache (for registers where it
197      makes sense, like PC or SP).  */
198   struct address_space *aspace;
199
200   /* The register buffers.  A read-only register cache can hold the
201      full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
202      register cache can only hold [0 .. gdbarch_num_regs).  */
203   gdb_byte *registers;
204   /* Register cache status.  */
205   signed char *register_status;
206   /* Is this a read-only cache?  A read-only cache is used for saving
207      the target's register state (e.g, across an inferior function
208      call or just before forcing a function return).  A read-only
209      cache can only be updated via the methods regcache_dup() and
210      regcache_cpy().  The actual contents are determined by the
211      reggroup_save and reggroup_restore methods.  */
212   int readonly_p;
213   /* If this is a read-write cache, which thread's registers is
214      it connected to?  */
215   ptid_t ptid;
216 };
217
218 /* See regcache.h.  */
219
220 ptid_t
221 regcache_get_ptid (const struct regcache *regcache)
222 {
223   gdb_assert (!ptid_equal (regcache->ptid, minus_one_ptid));
224
225   return regcache->ptid;
226 }
227
228 static struct regcache *
229 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
230                     int readonly_p)
231 {
232   struct regcache_descr *descr;
233   struct regcache *regcache;
234
235   gdb_assert (gdbarch != NULL);
236   descr = regcache_descr (gdbarch);
237   regcache = XNEW (struct regcache);
238   regcache->descr = descr;
239   regcache->readonly_p = readonly_p;
240   if (readonly_p)
241     {
242       regcache->registers
243         = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
244       regcache->register_status
245         = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
246     }
247   else
248     {
249       regcache->registers
250         = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
251       regcache->register_status
252         = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
253     }
254   regcache->aspace = aspace;
255   regcache->ptid = minus_one_ptid;
256   return regcache;
257 }
258
259 struct regcache *
260 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
261 {
262   return regcache_xmalloc_1 (gdbarch, aspace, 1);
263 }
264
265 void
266 regcache_xfree (struct regcache *regcache)
267 {
268   if (regcache == NULL)
269     return;
270   xfree (regcache->registers);
271   xfree (regcache->register_status);
272   xfree (regcache);
273 }
274
275 static void
276 do_regcache_xfree (void *data)
277 {
278   regcache_xfree ((struct regcache *) data);
279 }
280
281 struct cleanup *
282 make_cleanup_regcache_xfree (struct regcache *regcache)
283 {
284   return make_cleanup (do_regcache_xfree, regcache);
285 }
286
287 /* Cleanup routines for invalidating a register.  */
288
289 struct register_to_invalidate
290 {
291   struct regcache *regcache;
292   int regnum;
293 };
294
295 static void
296 do_regcache_invalidate (void *data)
297 {
298   struct register_to_invalidate *reg = (struct register_to_invalidate *) data;
299
300   regcache_invalidate (reg->regcache, reg->regnum);
301 }
302
303 static struct cleanup *
304 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
305 {
306   struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
307
308   reg->regcache = regcache;
309   reg->regnum = regnum;
310   return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
311 }
312
313 /* Return REGCACHE's architecture.  */
314
315 struct gdbarch *
316 get_regcache_arch (const struct regcache *regcache)
317 {
318   return regcache->descr->gdbarch;
319 }
320
321 struct address_space *
322 get_regcache_aspace (const struct regcache *regcache)
323 {
324   return regcache->aspace;
325 }
326
327 /* Return  a pointer to register REGNUM's buffer cache.  */
328
329 static gdb_byte *
330 register_buffer (const struct regcache *regcache, int regnum)
331 {
332   return regcache->registers + regcache->descr->register_offset[regnum];
333 }
334
335 void
336 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
337                void *src)
338 {
339   struct gdbarch *gdbarch = dst->descr->gdbarch;
340   gdb_byte buf[MAX_REGISTER_SIZE];
341   int regnum;
342
343   /* The DST should be `read-only', if it wasn't then the save would
344      end up trying to write the register values back out to the
345      target.  */
346   gdb_assert (dst->readonly_p);
347   /* Clear the dest.  */
348   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
349   memset (dst->register_status, 0,
350           dst->descr->sizeof_cooked_register_status);
351   /* Copy over any registers (identified by their membership in the
352      save_reggroup) and mark them as valid.  The full [0 .. gdbarch_num_regs +
353      gdbarch_num_pseudo_regs) range is checked since some architectures need
354      to save/restore `cooked' registers that live in memory.  */
355   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
356     {
357       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
358         {
359           enum register_status status = cooked_read (src, regnum, buf);
360
361           if (status == REG_VALID)
362             memcpy (register_buffer (dst, regnum), buf,
363                     register_size (gdbarch, regnum));
364           else
365             {
366               gdb_assert (status != REG_UNKNOWN);
367
368               memset (register_buffer (dst, regnum), 0,
369                       register_size (gdbarch, regnum));
370             }
371           dst->register_status[regnum] = status;
372         }
373     }
374 }
375
376 static void
377 regcache_restore (struct regcache *dst,
378                   regcache_cooked_read_ftype *cooked_read,
379                   void *cooked_read_context)
380 {
381   struct gdbarch *gdbarch = dst->descr->gdbarch;
382   gdb_byte buf[MAX_REGISTER_SIZE];
383   int regnum;
384
385   /* The dst had better not be read-only.  If it is, the `restore'
386      doesn't make much sense.  */
387   gdb_assert (!dst->readonly_p);
388   /* Copy over any registers, being careful to only restore those that
389      were both saved and need to be restored.  The full [0 .. gdbarch_num_regs
390      + gdbarch_num_pseudo_regs) range is checked since some architectures need
391      to save/restore `cooked' registers that live in memory.  */
392   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
393     {
394       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
395         {
396           enum register_status status;
397
398           status = cooked_read (cooked_read_context, regnum, buf);
399           if (status == REG_VALID)
400             regcache_cooked_write (dst, regnum, buf);
401         }
402     }
403 }
404
405 static enum register_status
406 do_cooked_read (void *src, int regnum, gdb_byte *buf)
407 {
408   struct regcache *regcache = (struct regcache *) src;
409
410   return regcache_cooked_read (regcache, regnum, buf);
411 }
412
413 static void regcache_cpy_no_passthrough (struct regcache *dst,
414                                          struct regcache *src);
415
416 void
417 regcache_cpy (struct regcache *dst, struct regcache *src)
418 {
419   gdb_assert (src != NULL && dst != NULL);
420   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
421   gdb_assert (src != dst);
422   gdb_assert (src->readonly_p || dst->readonly_p);
423
424   if (!src->readonly_p)
425     regcache_save (dst, do_cooked_read, src);
426   else if (!dst->readonly_p)
427     regcache_restore (dst, do_cooked_read, src);
428   else
429     regcache_cpy_no_passthrough (dst, src);
430 }
431
432 /* Copy/duplicate the contents of a register cache.  Unlike regcache_cpy,
433    which is pass-through, this does not go through to the target.
434    Only values values already in the cache are transferred.  The SRC and DST
435    buffers must not overlap.  */
436
437 static void
438 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
439 {
440   gdb_assert (src != NULL && dst != NULL);
441   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
442   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
443      move of data into a thread's regcache.  Doing this would be silly
444      - it would mean that regcache->register_status would be
445      completely invalid.  */
446   gdb_assert (dst->readonly_p && src->readonly_p);
447
448   memcpy (dst->registers, src->registers,
449           dst->descr->sizeof_cooked_registers);
450   memcpy (dst->register_status, src->register_status,
451           dst->descr->sizeof_cooked_register_status);
452 }
453
454 struct regcache *
455 regcache_dup (struct regcache *src)
456 {
457   struct regcache *newbuf;
458
459   newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
460   regcache_cpy (newbuf, src);
461   return newbuf;
462 }
463
464 enum register_status
465 regcache_register_status (const struct regcache *regcache, int regnum)
466 {
467   gdb_assert (regcache != NULL);
468   gdb_assert (regnum >= 0);
469   if (regcache->readonly_p)
470     gdb_assert (regnum < regcache->descr->nr_cooked_registers);
471   else
472     gdb_assert (regnum < regcache->descr->nr_raw_registers);
473
474   return (enum register_status) regcache->register_status[regnum];
475 }
476
477 void
478 regcache_invalidate (struct regcache *regcache, int regnum)
479 {
480   gdb_assert (regcache != NULL);
481   gdb_assert (regnum >= 0);
482   gdb_assert (!regcache->readonly_p);
483   gdb_assert (regnum < regcache->descr->nr_raw_registers);
484   regcache->register_status[regnum] = REG_UNKNOWN;
485 }
486
487
488 /* Global structure containing the current regcache.  */
489
490 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
491    recording if the register values have been changed (eg. by the
492    user).  Therefore all registers must be written back to the
493    target when appropriate.  */
494
495 struct regcache_list
496 {
497   struct regcache *regcache;
498   struct regcache_list *next;
499 };
500
501 static struct regcache_list *current_regcache;
502
503 struct regcache *
504 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
505                                  struct address_space *aspace)
506 {
507   struct regcache_list *list;
508   struct regcache *new_regcache;
509
510   for (list = current_regcache; list; list = list->next)
511     if (ptid_equal (list->regcache->ptid, ptid)
512         && get_regcache_arch (list->regcache) == gdbarch)
513       return list->regcache;
514
515   new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
516   new_regcache->ptid = ptid;
517
518   list = XNEW (struct regcache_list);
519   list->regcache = new_regcache;
520   list->next = current_regcache;
521   current_regcache = list;
522
523   return new_regcache;
524 }
525
526 struct regcache *
527 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
528 {
529   struct address_space *aspace;
530
531   /* For the benefit of "maint print registers" & co when debugging an
532      executable, allow dumping the regcache even when there is no
533      thread selected (target_thread_address_space internal-errors if
534      no address space is found).  Note that normal user commands will
535      fail higher up on the call stack due to no
536      target_has_registers.  */
537   aspace = (ptid_equal (null_ptid, ptid)
538             ? NULL
539             : target_thread_address_space (ptid));
540
541   return get_thread_arch_aspace_regcache  (ptid, gdbarch, aspace);
542 }
543
544 static ptid_t current_thread_ptid;
545 static struct gdbarch *current_thread_arch;
546
547 struct regcache *
548 get_thread_regcache (ptid_t ptid)
549 {
550   if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
551     {
552       current_thread_ptid = ptid;
553       current_thread_arch = target_thread_architecture (ptid);
554     }
555
556   return get_thread_arch_regcache (ptid, current_thread_arch);
557 }
558
559 struct regcache *
560 get_current_regcache (void)
561 {
562   return get_thread_regcache (inferior_ptid);
563 }
564
565 /* See common/common-regcache.h.  */
566
567 struct regcache *
568 get_thread_regcache_for_ptid (ptid_t ptid)
569 {
570   return get_thread_regcache (ptid);
571 }
572
573 /* Observer for the target_changed event.  */
574
575 static void
576 regcache_observer_target_changed (struct target_ops *target)
577 {
578   registers_changed ();
579 }
580
581 /* Update global variables old ptids to hold NEW_PTID if they were
582    holding OLD_PTID.  */
583 static void
584 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
585 {
586   struct regcache_list *list;
587
588   for (list = current_regcache; list; list = list->next)
589     if (ptid_equal (list->regcache->ptid, old_ptid))
590       list->regcache->ptid = new_ptid;
591 }
592
593 /* Low level examining and depositing of registers.
594
595    The caller is responsible for making sure that the inferior is
596    stopped before calling the fetching routines, or it will get
597    garbage.  (a change from GDB version 3, in which the caller got the
598    value from the last stop).  */
599
600 /* REGISTERS_CHANGED ()
601
602    Indicate that registers may have changed, so invalidate the cache.  */
603
604 void
605 registers_changed_ptid (ptid_t ptid)
606 {
607   struct regcache_list *list, **list_link;
608
609   list = current_regcache;
610   list_link = &current_regcache;
611   while (list)
612     {
613       if (ptid_match (list->regcache->ptid, ptid))
614         {
615           struct regcache_list *dead = list;
616
617           *list_link = list->next;
618           regcache_xfree (list->regcache);
619           list = *list_link;
620           xfree (dead);
621           continue;
622         }
623
624       list_link = &list->next;
625       list = *list_link;
626     }
627
628   if (ptid_match (current_thread_ptid, ptid))
629     {
630       current_thread_ptid = null_ptid;
631       current_thread_arch = NULL;
632     }
633
634   if (ptid_match (inferior_ptid, ptid))
635     {
636       /* We just deleted the regcache of the current thread.  Need to
637          forget about any frames we have cached, too.  */
638       reinit_frame_cache ();
639     }
640 }
641
642 void
643 registers_changed (void)
644 {
645   registers_changed_ptid (minus_one_ptid);
646
647   /* Force cleanup of any alloca areas if using C alloca instead of
648      a builtin alloca.  This particular call is used to clean up
649      areas allocated by low level target code which may build up
650      during lengthy interactions between gdb and the target before
651      gdb gives control to the user (ie watchpoints).  */
652   alloca (0);
653 }
654
655 void
656 regcache_raw_update (struct regcache *regcache, int regnum)
657 {
658   gdb_assert (regcache != NULL);
659   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
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
666   if (!regcache->readonly_p
667       && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
668     {
669       struct cleanup *old_chain = save_inferior_ptid ();
670
671       inferior_ptid = regcache->ptid;
672       target_fetch_registers (regcache, regnum);
673       do_cleanups (old_chain);
674
675       /* A number of targets can't access the whole set of raw
676          registers (because the debug API provides no means to get at
677          them).  */
678       if (regcache->register_status[regnum] == REG_UNKNOWN)
679         regcache->register_status[regnum] = REG_UNAVAILABLE;
680     }
681 }
682
683 enum register_status
684 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
685 {
686   gdb_assert (buf != NULL);
687   regcache_raw_update (regcache, regnum);
688
689   if (regcache->register_status[regnum] != REG_VALID)
690     memset (buf, 0, regcache->descr->sizeof_register[regnum]);
691   else
692     memcpy (buf, register_buffer (regcache, regnum),
693             regcache->descr->sizeof_register[regnum]);
694
695   return (enum register_status) regcache->register_status[regnum];
696 }
697
698 enum register_status
699 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
700 {
701   gdb_byte *buf;
702   enum register_status status;
703
704   gdb_assert (regcache != NULL);
705   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
706   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
707   status = regcache_raw_read (regcache, regnum, buf);
708   if (status == REG_VALID)
709     *val = extract_signed_integer
710       (buf, regcache->descr->sizeof_register[regnum],
711        gdbarch_byte_order (regcache->descr->gdbarch));
712   else
713     *val = 0;
714   return status;
715 }
716
717 enum register_status
718 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
719                             ULONGEST *val)
720 {
721   gdb_byte *buf;
722   enum register_status status;
723
724   gdb_assert (regcache != NULL);
725   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
726   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
727   status = regcache_raw_read (regcache, regnum, buf);
728   if (status == REG_VALID)
729     *val = extract_unsigned_integer
730       (buf, regcache->descr->sizeof_register[regnum],
731        gdbarch_byte_order (regcache->descr->gdbarch));
732   else
733     *val = 0;
734   return status;
735 }
736
737 void
738 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
739 {
740   gdb_byte *buf;
741
742   gdb_assert (regcache != NULL);
743   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
744   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
745   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
746                         gdbarch_byte_order (regcache->descr->gdbarch), val);
747   regcache_raw_write (regcache, regnum, buf);
748 }
749
750 void
751 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
752                              ULONGEST val)
753 {
754   gdb_byte *buf;
755
756   gdb_assert (regcache != NULL);
757   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
758   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
759   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
760                           gdbarch_byte_order (regcache->descr->gdbarch), val);
761   regcache_raw_write (regcache, regnum, buf);
762 }
763
764 LONGEST
765 regcache_raw_get_signed (struct regcache *regcache, int regnum)
766 {
767   LONGEST value;
768   enum register_status status;
769
770   status = regcache_raw_read_signed (regcache, regnum, &value);
771   if (status == REG_UNAVAILABLE)
772     throw_error (NOT_AVAILABLE_ERROR,
773                  _("Register %d is not available"), regnum);
774   return value;
775 }
776
777 enum register_status
778 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
779 {
780   gdb_assert (regnum >= 0);
781   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
782   if (regnum < regcache->descr->nr_raw_registers)
783     return regcache_raw_read (regcache, regnum, buf);
784   else if (regcache->readonly_p
785            && regcache->register_status[regnum] != REG_UNKNOWN)
786     {
787       /* Read-only register cache, perhaps the cooked value was
788          cached?  */
789       if (regcache->register_status[regnum] == REG_VALID)
790         memcpy (buf, register_buffer (regcache, regnum),
791                 regcache->descr->sizeof_register[regnum]);
792       else
793         memset (buf, 0, regcache->descr->sizeof_register[regnum]);
794
795       return (enum register_status) regcache->register_status[regnum];
796     }
797   else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
798     {
799       struct value *mark, *computed;
800       enum register_status result = REG_VALID;
801
802       mark = value_mark ();
803
804       computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
805                                                      regcache, regnum);
806       if (value_entirely_available (computed))
807         memcpy (buf, value_contents_raw (computed),
808                 regcache->descr->sizeof_register[regnum]);
809       else
810         {
811           memset (buf, 0, regcache->descr->sizeof_register[regnum]);
812           result = REG_UNAVAILABLE;
813         }
814
815       value_free_to_mark (mark);
816
817       return result;
818     }
819   else
820     return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
821                                          regnum, buf);
822 }
823
824 struct value *
825 regcache_cooked_read_value (struct regcache *regcache, int regnum)
826 {
827   gdb_assert (regnum >= 0);
828   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
829
830   if (regnum < regcache->descr->nr_raw_registers
831       || (regcache->readonly_p
832           && regcache->register_status[regnum] != REG_UNKNOWN)
833       || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
834     {
835       struct value *result;
836
837       result = allocate_value (register_type (regcache->descr->gdbarch,
838                                               regnum));
839       VALUE_LVAL (result) = lval_register;
840       VALUE_REGNUM (result) = regnum;
841
842       /* It is more efficient in general to do this delegation in this
843          direction than in the other one, even though the value-based
844          API is preferred.  */
845       if (regcache_cooked_read (regcache, regnum,
846                                 value_contents_raw (result)) == REG_UNAVAILABLE)
847         mark_value_bytes_unavailable (result, 0,
848                                       TYPE_LENGTH (value_type (result)));
849
850       return result;
851     }
852   else
853     return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
854                                                regcache, regnum);
855 }
856
857 enum register_status
858 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
859                              LONGEST *val)
860 {
861   enum register_status status;
862   gdb_byte *buf;
863
864   gdb_assert (regcache != NULL);
865   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
866   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
867   status = regcache_cooked_read (regcache, regnum, buf);
868   if (status == REG_VALID)
869     *val = extract_signed_integer
870       (buf, regcache->descr->sizeof_register[regnum],
871        gdbarch_byte_order (regcache->descr->gdbarch));
872   else
873     *val = 0;
874   return status;
875 }
876
877 enum register_status
878 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
879                                ULONGEST *val)
880 {
881   enum register_status status;
882   gdb_byte *buf;
883
884   gdb_assert (regcache != NULL);
885   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
886   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
887   status = regcache_cooked_read (regcache, regnum, buf);
888   if (status == REG_VALID)
889     *val = extract_unsigned_integer
890       (buf, regcache->descr->sizeof_register[regnum],
891        gdbarch_byte_order (regcache->descr->gdbarch));
892   else
893     *val = 0;
894   return status;
895 }
896
897 void
898 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
899                               LONGEST val)
900 {
901   gdb_byte *buf;
902
903   gdb_assert (regcache != NULL);
904   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
905   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
906   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
907                         gdbarch_byte_order (regcache->descr->gdbarch), val);
908   regcache_cooked_write (regcache, regnum, buf);
909 }
910
911 void
912 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
913                                 ULONGEST val)
914 {
915   gdb_byte *buf;
916
917   gdb_assert (regcache != NULL);
918   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
919   buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
920   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
921                           gdbarch_byte_order (regcache->descr->gdbarch), val);
922   regcache_cooked_write (regcache, regnum, buf);
923 }
924
925 /* See regcache.h.  */
926
927 void
928 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
929                                const gdb_byte *buf)
930 {
931   memcpy (register_buffer (regcache, regnum), buf,
932           regcache->descr->sizeof_register[regnum]);
933   regcache->register_status[regnum] = REG_VALID;
934 }
935
936 void
937 regcache_raw_write (struct regcache *regcache, int regnum,
938                     const gdb_byte *buf)
939 {
940   struct cleanup *chain_before_save_inferior;
941   struct cleanup *chain_before_invalidate_register;
942
943   gdb_assert (regcache != NULL && buf != NULL);
944   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
945   gdb_assert (!regcache->readonly_p);
946
947   /* On the sparc, writing %g0 is a no-op, so we don't even want to
948      change the registers array if something writes to this register.  */
949   if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
950     return;
951
952   /* If we have a valid copy of the register, and new value == old
953      value, then don't bother doing the actual store.  */
954   if (regcache_register_status (regcache, regnum) == REG_VALID
955       && (memcmp (register_buffer (regcache, regnum), buf,
956                   regcache->descr->sizeof_register[regnum]) == 0))
957     return;
958
959   chain_before_save_inferior = save_inferior_ptid ();
960   inferior_ptid = regcache->ptid;
961
962   target_prepare_to_store (regcache);
963   regcache_raw_set_cached_value (regcache, regnum, buf);
964
965   /* Register a cleanup function for invalidating the register after it is
966      written, in case of a failure.  */
967   chain_before_invalidate_register
968     = make_cleanup_regcache_invalidate (regcache, regnum);
969
970   target_store_registers (regcache, regnum);
971
972   /* The target did not throw an error so we can discard invalidating the
973      register and restore the cleanup chain to what it was.  */
974   discard_cleanups (chain_before_invalidate_register);
975
976   do_cleanups (chain_before_save_inferior);
977 }
978
979 void
980 regcache_cooked_write (struct regcache *regcache, int regnum,
981                        const gdb_byte *buf)
982 {
983   gdb_assert (regnum >= 0);
984   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
985   if (regnum < regcache->descr->nr_raw_registers)
986     regcache_raw_write (regcache, regnum, buf);
987   else
988     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
989                                    regnum, buf);
990 }
991
992 /* Perform a partial register transfer using a read, modify, write
993    operation.  */
994
995 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
996                                     void *buf);
997 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
998                                      const void *buf);
999
1000 static enum register_status
1001 regcache_xfer_part (struct regcache *regcache, int regnum,
1002                     int offset, int len, void *in, const void *out,
1003                     enum register_status (*read) (struct regcache *regcache,
1004                                                   int regnum,
1005                                                   gdb_byte *buf),
1006                     void (*write) (struct regcache *regcache, int regnum,
1007                                    const gdb_byte *buf))
1008 {
1009   struct regcache_descr *descr = regcache->descr;
1010   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1011   gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
1012
1013   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
1014   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
1015   /* Something to do?  */
1016   if (offset + len == 0)
1017     return REG_VALID;
1018   /* Read (when needed) ...  */
1019   if (in != NULL
1020       || offset > 0
1021       || offset + len < descr->sizeof_register[regnum])
1022     {
1023       enum register_status status;
1024
1025       gdb_assert (read != NULL);
1026       status = read (regcache, regnum, reg);
1027       if (status != REG_VALID)
1028         return status;
1029     }
1030   /* ... modify ...  */
1031   if (in != NULL)
1032     memcpy (in, reg + offset, len);
1033   if (out != NULL)
1034     memcpy (reg + offset, out, len);
1035   /* ... write (when needed).  */
1036   if (out != NULL)
1037     {
1038       gdb_assert (write != NULL);
1039       write (regcache, regnum, reg);
1040     }
1041
1042   return REG_VALID;
1043 }
1044
1045 enum register_status
1046 regcache_raw_read_part (struct regcache *regcache, int regnum,
1047                         int offset, int len, gdb_byte *buf)
1048 {
1049   struct regcache_descr *descr = regcache->descr;
1050
1051   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1052   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1053                              regcache_raw_read, regcache_raw_write);
1054 }
1055
1056 void
1057 regcache_raw_write_part (struct regcache *regcache, int regnum,
1058                          int offset, int len, const gdb_byte *buf)
1059 {
1060   struct regcache_descr *descr = regcache->descr;
1061
1062   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1063   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1064                       regcache_raw_read, regcache_raw_write);
1065 }
1066
1067 enum register_status
1068 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1069                            int offset, int len, gdb_byte *buf)
1070 {
1071   struct regcache_descr *descr = regcache->descr;
1072
1073   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1074   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1075                              regcache_cooked_read, regcache_cooked_write);
1076 }
1077
1078 void
1079 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1080                             int offset, int len, const gdb_byte *buf)
1081 {
1082   struct regcache_descr *descr = regcache->descr;
1083
1084   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1085   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1086                       regcache_cooked_read, regcache_cooked_write);
1087 }
1088
1089 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
1090
1091 void
1092 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1093 {
1094   void *regbuf;
1095   size_t size;
1096
1097   gdb_assert (regcache != NULL);
1098   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1099   gdb_assert (!regcache->readonly_p);
1100
1101   regbuf = register_buffer (regcache, regnum);
1102   size = regcache->descr->sizeof_register[regnum];
1103
1104   if (buf)
1105     {
1106       memcpy (regbuf, buf, size);
1107       regcache->register_status[regnum] = REG_VALID;
1108     }
1109   else
1110     {
1111       /* This memset not strictly necessary, but better than garbage
1112          in case the register value manages to escape somewhere (due
1113          to a bug, no less).  */
1114       memset (regbuf, 0, size);
1115       regcache->register_status[regnum] = REG_UNAVAILABLE;
1116     }
1117 }
1118
1119 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
1120
1121 void
1122 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1123 {
1124   const void *regbuf;
1125   size_t size;
1126
1127   gdb_assert (regcache != NULL && buf != NULL);
1128   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1129
1130   regbuf = register_buffer (regcache, regnum);
1131   size = regcache->descr->sizeof_register[regnum];
1132   memcpy (buf, regbuf, size);
1133 }
1134
1135 /* Transfer a single or all registers belonging to a certain register
1136    set to or from a buffer.  This is the main worker function for
1137    regcache_supply_regset and regcache_collect_regset.  */
1138
1139 static void
1140 regcache_transfer_regset (const struct regset *regset,
1141                           const struct regcache *regcache,
1142                           struct regcache *out_regcache,
1143                           int regnum, const void *in_buf,
1144                           void *out_buf, size_t size)
1145 {
1146   const struct regcache_map_entry *map;
1147   int offs = 0, count;
1148
1149   for (map = (const struct regcache_map_entry *) regset->regmap;
1150        (count = map->count) != 0;
1151        map++)
1152     {
1153       int regno = map->regno;
1154       int slot_size = map->size;
1155
1156       if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1157         slot_size = regcache->descr->sizeof_register[regno];
1158
1159       if (regno == REGCACHE_MAP_SKIP
1160           || (regnum != -1
1161               && (regnum < regno || regnum >= regno + count)))
1162           offs += count * slot_size;
1163
1164       else if (regnum == -1)
1165         for (; count--; regno++, offs += slot_size)
1166           {
1167             if (offs + slot_size > size)
1168               break;
1169
1170             if (out_buf)
1171               regcache_raw_collect (regcache, regno,
1172                                     (gdb_byte *) out_buf + offs);
1173             else
1174               regcache_raw_supply (out_regcache, regno, in_buf
1175                                    ? (const gdb_byte *) in_buf + offs
1176                                    : NULL);
1177           }
1178       else
1179         {
1180           /* Transfer a single register and return.  */
1181           offs += (regnum - regno) * slot_size;
1182           if (offs + slot_size > size)
1183             return;
1184
1185           if (out_buf)
1186             regcache_raw_collect (regcache, regnum,
1187                                   (gdb_byte *) out_buf + offs);
1188           else
1189             regcache_raw_supply (out_regcache, regnum, in_buf
1190                                  ? (const gdb_byte *) in_buf + offs
1191                                  : NULL);
1192           return;
1193         }
1194     }
1195 }
1196
1197 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1198    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1199    If BUF is NULL, set the register(s) to "unavailable" status. */
1200
1201 void
1202 regcache_supply_regset (const struct regset *regset,
1203                         struct regcache *regcache,
1204                         int regnum, const void *buf, size_t size)
1205 {
1206   regcache_transfer_regset (regset, regcache, regcache, regnum,
1207                             buf, NULL, size);
1208 }
1209
1210 /* Collect register REGNUM from REGCACHE to BUF, using the register
1211    map in REGSET.  If REGNUM is -1, do this for all registers in
1212    REGSET.  */
1213
1214 void
1215 regcache_collect_regset (const struct regset *regset,
1216                          const struct regcache *regcache,
1217                          int regnum, void *buf, size_t size)
1218 {
1219   regcache_transfer_regset (regset, regcache, NULL, regnum,
1220                             NULL, buf, size);
1221 }
1222
1223
1224 /* Special handling for register PC.  */
1225
1226 CORE_ADDR
1227 regcache_read_pc (struct regcache *regcache)
1228 {
1229   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1230
1231   CORE_ADDR pc_val;
1232
1233   if (gdbarch_read_pc_p (gdbarch))
1234     pc_val = gdbarch_read_pc (gdbarch, regcache);
1235   /* Else use per-frame method on get_current_frame.  */
1236   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1237     {
1238       ULONGEST raw_val;
1239
1240       if (regcache_cooked_read_unsigned (regcache,
1241                                          gdbarch_pc_regnum (gdbarch),
1242                                          &raw_val) == REG_UNAVAILABLE)
1243         throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1244
1245       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1246     }
1247   else
1248     internal_error (__FILE__, __LINE__,
1249                     _("regcache_read_pc: Unable to find PC"));
1250   return pc_val;
1251 }
1252
1253 void
1254 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1255 {
1256   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1257
1258   if (gdbarch_write_pc_p (gdbarch))
1259     gdbarch_write_pc (gdbarch, regcache, pc);
1260   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1261     regcache_cooked_write_unsigned (regcache,
1262                                     gdbarch_pc_regnum (gdbarch), pc);
1263   else
1264     internal_error (__FILE__, __LINE__,
1265                     _("regcache_write_pc: Unable to update PC"));
1266
1267   /* Writing the PC (for instance, from "load") invalidates the
1268      current frame.  */
1269   reinit_frame_cache ();
1270 }
1271
1272
1273 static void
1274 reg_flush_command (char *command, int from_tty)
1275 {
1276   /* Force-flush the register cache.  */
1277   registers_changed ();
1278   if (from_tty)
1279     printf_filtered (_("Register cache flushed.\n"));
1280 }
1281
1282 enum regcache_dump_what
1283 {
1284   regcache_dump_none, regcache_dump_raw,
1285   regcache_dump_cooked, regcache_dump_groups,
1286   regcache_dump_remote
1287 };
1288
1289 static void
1290 regcache_dump (struct regcache *regcache, struct ui_file *file,
1291                enum regcache_dump_what what_to_dump)
1292 {
1293   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1294   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1295   int regnum;
1296   int footnote_nr = 0;
1297   int footnote_register_size = 0;
1298   int footnote_register_offset = 0;
1299   int footnote_register_type_name_null = 0;
1300   long register_offset = 0;
1301   gdb_byte buf[MAX_REGISTER_SIZE];
1302
1303 #if 0
1304   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1305                       regcache->descr->nr_raw_registers);
1306   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1307                       regcache->descr->nr_cooked_registers);
1308   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1309                       regcache->descr->sizeof_raw_registers);
1310   fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1311                       regcache->descr->sizeof_raw_register_status);
1312   fprintf_unfiltered (file, "gdbarch_num_regs %d\n", 
1313                       gdbarch_num_regs (gdbarch));
1314   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1315                       gdbarch_num_pseudo_regs (gdbarch));
1316 #endif
1317
1318   gdb_assert (regcache->descr->nr_cooked_registers
1319               == (gdbarch_num_regs (gdbarch)
1320                   + gdbarch_num_pseudo_regs (gdbarch)));
1321
1322   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1323     {
1324       /* Name.  */
1325       if (regnum < 0)
1326         fprintf_unfiltered (file, " %-10s", "Name");
1327       else
1328         {
1329           const char *p = gdbarch_register_name (gdbarch, regnum);
1330
1331           if (p == NULL)
1332             p = "";
1333           else if (p[0] == '\0')
1334             p = "''";
1335           fprintf_unfiltered (file, " %-10s", p);
1336         }
1337
1338       /* Number.  */
1339       if (regnum < 0)
1340         fprintf_unfiltered (file, " %4s", "Nr");
1341       else
1342         fprintf_unfiltered (file, " %4d", regnum);
1343
1344       /* Relative number.  */
1345       if (regnum < 0)
1346         fprintf_unfiltered (file, " %4s", "Rel");
1347       else if (regnum < gdbarch_num_regs (gdbarch))
1348         fprintf_unfiltered (file, " %4d", regnum);
1349       else
1350         fprintf_unfiltered (file, " %4d",
1351                             (regnum - gdbarch_num_regs (gdbarch)));
1352
1353       /* Offset.  */
1354       if (regnum < 0)
1355         fprintf_unfiltered (file, " %6s  ", "Offset");
1356       else
1357         {
1358           fprintf_unfiltered (file, " %6ld",
1359                               regcache->descr->register_offset[regnum]);
1360           if (register_offset != regcache->descr->register_offset[regnum]
1361               || (regnum > 0
1362                   && (regcache->descr->register_offset[regnum]
1363                       != (regcache->descr->register_offset[regnum - 1]
1364                           + regcache->descr->sizeof_register[regnum - 1])))
1365               )
1366             {
1367               if (!footnote_register_offset)
1368                 footnote_register_offset = ++footnote_nr;
1369               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1370             }
1371           else
1372             fprintf_unfiltered (file, "  ");
1373           register_offset = (regcache->descr->register_offset[regnum]
1374                              + regcache->descr->sizeof_register[regnum]);
1375         }
1376
1377       /* Size.  */
1378       if (regnum < 0)
1379         fprintf_unfiltered (file, " %5s ", "Size");
1380       else
1381         fprintf_unfiltered (file, " %5ld",
1382                             regcache->descr->sizeof_register[regnum]);
1383
1384       /* Type.  */
1385       {
1386         const char *t;
1387
1388         if (regnum < 0)
1389           t = "Type";
1390         else
1391           {
1392             static const char blt[] = "builtin_type";
1393
1394             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1395             if (t == NULL)
1396               {
1397                 char *n;
1398
1399                 if (!footnote_register_type_name_null)
1400                   footnote_register_type_name_null = ++footnote_nr;
1401                 n = xstrprintf ("*%d", footnote_register_type_name_null);
1402                 make_cleanup (xfree, n);
1403                 t = n;
1404               }
1405             /* Chop a leading builtin_type.  */
1406             if (startswith (t, blt))
1407               t += strlen (blt);
1408           }
1409         fprintf_unfiltered (file, " %-15s", t);
1410       }
1411
1412       /* Leading space always present.  */
1413       fprintf_unfiltered (file, " ");
1414
1415       /* Value, raw.  */
1416       if (what_to_dump == regcache_dump_raw)
1417         {
1418           if (regnum < 0)
1419             fprintf_unfiltered (file, "Raw value");
1420           else if (regnum >= regcache->descr->nr_raw_registers)
1421             fprintf_unfiltered (file, "<cooked>");
1422           else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1423             fprintf_unfiltered (file, "<invalid>");
1424           else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1425             fprintf_unfiltered (file, "<unavailable>");
1426           else
1427             {
1428               regcache_raw_read (regcache, regnum, buf);
1429               print_hex_chars (file, buf,
1430                                regcache->descr->sizeof_register[regnum],
1431                                gdbarch_byte_order (gdbarch));
1432             }
1433         }
1434
1435       /* Value, cooked.  */
1436       if (what_to_dump == regcache_dump_cooked)
1437         {
1438           if (regnum < 0)
1439             fprintf_unfiltered (file, "Cooked value");
1440           else
1441             {
1442               enum register_status status;
1443
1444               status = regcache_cooked_read (regcache, regnum, buf);
1445               if (status == REG_UNKNOWN)
1446                 fprintf_unfiltered (file, "<invalid>");
1447               else if (status == REG_UNAVAILABLE)
1448                 fprintf_unfiltered (file, "<unavailable>");
1449               else
1450                 print_hex_chars (file, buf,
1451                                  regcache->descr->sizeof_register[regnum],
1452                                  gdbarch_byte_order (gdbarch));
1453             }
1454         }
1455
1456       /* Group members.  */
1457       if (what_to_dump == regcache_dump_groups)
1458         {
1459           if (regnum < 0)
1460             fprintf_unfiltered (file, "Groups");
1461           else
1462             {
1463               const char *sep = "";
1464               struct reggroup *group;
1465
1466               for (group = reggroup_next (gdbarch, NULL);
1467                    group != NULL;
1468                    group = reggroup_next (gdbarch, group))
1469                 {
1470                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1471                     {
1472                       fprintf_unfiltered (file,
1473                                           "%s%s", sep, reggroup_name (group));
1474                       sep = ",";
1475                     }
1476                 }
1477             }
1478         }
1479
1480       /* Remote packet configuration.  */
1481       if (what_to_dump == regcache_dump_remote)
1482         {
1483           if (regnum < 0)
1484             {
1485               fprintf_unfiltered (file, "Rmt Nr  g/G Offset");
1486             }
1487           else if (regnum < regcache->descr->nr_raw_registers)
1488             {
1489               int pnum, poffset;
1490
1491               if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1492                                                      &pnum, &poffset))
1493                 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1494             }
1495         }
1496
1497       fprintf_unfiltered (file, "\n");
1498     }
1499
1500   if (footnote_register_size)
1501     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1502                         footnote_register_size);
1503   if (footnote_register_offset)
1504     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1505                         footnote_register_offset);
1506   if (footnote_register_type_name_null)
1507     fprintf_unfiltered (file, 
1508                         "*%d: Register type's name NULL.\n",
1509                         footnote_register_type_name_null);
1510   do_cleanups (cleanups);
1511 }
1512
1513 static void
1514 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1515 {
1516   if (args == NULL)
1517     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1518   else
1519     {
1520       stdio_file file;
1521
1522       if (!file.open (args, "w"))
1523         perror_with_name (_("maintenance print architecture"));
1524       regcache_dump (get_current_regcache (), &file, what_to_dump);
1525     }
1526 }
1527
1528 static void
1529 maintenance_print_registers (char *args, int from_tty)
1530 {
1531   regcache_print (args, regcache_dump_none);
1532 }
1533
1534 static void
1535 maintenance_print_raw_registers (char *args, int from_tty)
1536 {
1537   regcache_print (args, regcache_dump_raw);
1538 }
1539
1540 static void
1541 maintenance_print_cooked_registers (char *args, int from_tty)
1542 {
1543   regcache_print (args, regcache_dump_cooked);
1544 }
1545
1546 static void
1547 maintenance_print_register_groups (char *args, int from_tty)
1548 {
1549   regcache_print (args, regcache_dump_groups);
1550 }
1551
1552 static void
1553 maintenance_print_remote_registers (char *args, int from_tty)
1554 {
1555   regcache_print (args, regcache_dump_remote);
1556 }
1557
1558 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1559
1560 void
1561 _initialize_regcache (void)
1562 {
1563   regcache_descr_handle
1564     = gdbarch_data_register_post_init (init_regcache_descr);
1565
1566   observer_attach_target_changed (regcache_observer_target_changed);
1567   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1568
1569   add_com ("flushregs", class_maintenance, reg_flush_command,
1570            _("Force gdb to flush its register cache (maintainer command)"));
1571
1572   add_cmd ("registers", class_maintenance, maintenance_print_registers,
1573            _("Print the internal register configuration.\n"
1574              "Takes an optional file parameter."), &maintenanceprintlist);
1575   add_cmd ("raw-registers", class_maintenance,
1576            maintenance_print_raw_registers,
1577            _("Print the internal register configuration "
1578              "including raw values.\n"
1579              "Takes an optional file parameter."), &maintenanceprintlist);
1580   add_cmd ("cooked-registers", class_maintenance,
1581            maintenance_print_cooked_registers,
1582            _("Print the internal register configuration "
1583              "including cooked values.\n"
1584              "Takes an optional file parameter."), &maintenanceprintlist);
1585   add_cmd ("register-groups", class_maintenance,
1586            maintenance_print_register_groups,
1587            _("Print the internal register configuration "
1588              "including each register's group.\n"
1589              "Takes an optional file parameter."),
1590            &maintenanceprintlist);
1591   add_cmd ("remote-registers", class_maintenance,
1592            maintenance_print_remote_registers, _("\
1593 Print the internal register configuration including each register's\n\
1594 remote register number and buffer offset in the g/G packets.\n\
1595 Takes an optional file parameter."),
1596            &maintenanceprintlist);
1597
1598 }