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