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