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