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