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