xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/regcache.c (revision 345cf9fb81bd0411c53e25d62cd93bdcaa865312)
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2020 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 "gdbthread.h"
23 #include "target.h"
24 #include "test-target.h"
25 #include "scoped-mock-context.h"
26 #include "gdbarch.h"
27 #include "gdbcmd.h"
28 #include "regcache.h"
29 #include "reggroups.h"
30 #include "observable.h"
31 #include "regset.h"
32 #include <unordered_map>
33 
34 /*
35  * DATA STRUCTURE
36  *
37  * Here is the actual register cache.
38  */
39 
40 /* Per-architecture object describing the layout of a register cache.
41    Computed once when the architecture is created.  */
42 
43 struct gdbarch_data *regcache_descr_handle;
44 
45 struct regcache_descr
46 {
47   /* The architecture this descriptor belongs to.  */
48   struct gdbarch *gdbarch;
49 
50   /* The raw register cache.  Each raw (or hard) register is supplied
51      by the target interface.  The raw cache should not contain
52      redundant information - if the PC is constructed from two
53      registers then those registers and not the PC lives in the raw
54      cache.  */
55   long sizeof_raw_registers;
56 
57   /* The cooked register space.  Each cooked register in the range
58      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
59      register.  The remaining [NR_RAW_REGISTERS
60      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
61      both raw registers and memory by the architecture methods
62      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
63   int nr_cooked_registers;
64   long sizeof_cooked_registers;
65 
66   /* Offset and size (in 8 bit bytes), of each register in the
67      register cache.  All registers (including those in the range
68      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
69      offset.  */
70   long *register_offset;
71   long *sizeof_register;
72 
73   /* Cached table containing the type of each register.  */
74   struct type **register_type;
75 };
76 
77 static void *
78 init_regcache_descr (struct gdbarch *gdbarch)
79 {
80   int i;
81   struct regcache_descr *descr;
82   gdb_assert (gdbarch != NULL);
83 
84   /* Create an initial, zero filled, table.  */
85   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
86   descr->gdbarch = gdbarch;
87 
88   /* Total size of the register space.  The raw registers are mapped
89      directly onto the raw register cache while the pseudo's are
90      either mapped onto raw-registers or memory.  */
91   descr->nr_cooked_registers = gdbarch_num_cooked_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       }
123     /* Set the real size of the raw register cache buffer.  */
124     descr->sizeof_raw_registers = offset;
125 
126     for (; i < descr->nr_cooked_registers; i++)
127       {
128 	descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
129 	descr->register_offset[i] = offset;
130 	offset += descr->sizeof_register[i];
131       }
132     /* Set the real size of the readonly register cache buffer.  */
133     descr->sizeof_cooked_registers = offset;
134   }
135 
136   return descr;
137 }
138 
139 static struct regcache_descr *
140 regcache_descr (struct gdbarch *gdbarch)
141 {
142   return (struct regcache_descr *) gdbarch_data (gdbarch,
143 						 regcache_descr_handle);
144 }
145 
146 /* Utility functions returning useful register attributes stored in
147    the regcache descr.  */
148 
149 struct type *
150 register_type (struct gdbarch *gdbarch, int regnum)
151 {
152   struct regcache_descr *descr = regcache_descr (gdbarch);
153 
154   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
155   return descr->register_type[regnum];
156 }
157 
158 /* Utility functions returning useful register attributes stored in
159    the regcache descr.  */
160 
161 int
162 register_size (struct gdbarch *gdbarch, int regnum)
163 {
164   struct regcache_descr *descr = regcache_descr (gdbarch);
165   int size;
166 
167   gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
168   size = descr->sizeof_register[regnum];
169   return size;
170 }
171 
172 /* See gdbsupport/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 (process_stratum_target *target, gdbarch *gdbarch,
201 		    const address_space *aspace_)
202 /* The register buffers.  A read/write register cache can only hold
203    [0 .. gdbarch_num_regs).  */
204   : detached_regcache (gdbarch, false), m_aspace (aspace_), m_target (target)
205 {
206   m_ptid = minus_one_ptid;
207 }
208 
209 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
210   : readonly_detached_regcache (src.arch (),
211 				[&src] (int regnum, gdb_byte *buf)
212 				  {
213 				    return src.cooked_read (regnum, buf);
214 				  })
215 {
216 }
217 
218 gdbarch *
219 reg_buffer::arch () const
220 {
221   return m_descr->gdbarch;
222 }
223 
224 /* Return  a pointer to register REGNUM's buffer cache.  */
225 
226 gdb_byte *
227 reg_buffer::register_buffer (int regnum) const
228 {
229   return m_registers.get () + m_descr->register_offset[regnum];
230 }
231 
232 void
233 reg_buffer::save (register_read_ftype cooked_read)
234 {
235   struct gdbarch *gdbarch = m_descr->gdbarch;
236   int regnum;
237 
238   /* It should have pseudo registers.  */
239   gdb_assert (m_has_pseudo);
240   /* Clear the dest.  */
241   memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
242   memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
243   /* Copy over any registers (identified by their membership in the
244      save_reggroup) and mark them as valid.  The full [0 .. gdbarch_num_regs +
245      gdbarch_num_pseudo_regs) range is checked since some architectures need
246      to save/restore `cooked' registers that live in memory.  */
247   for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
248     {
249       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
250 	{
251 	  gdb_byte *dst_buf = register_buffer (regnum);
252 	  enum register_status status = cooked_read (regnum, dst_buf);
253 
254 	  gdb_assert (status != REG_UNKNOWN);
255 
256 	  if (status != REG_VALID)
257 	    memset (dst_buf, 0, register_size (gdbarch, regnum));
258 
259 	  m_register_status[regnum] = status;
260 	}
261     }
262 }
263 
264 void
265 regcache::restore (readonly_detached_regcache *src)
266 {
267   struct gdbarch *gdbarch = m_descr->gdbarch;
268   int regnum;
269 
270   gdb_assert (src != NULL);
271   gdb_assert (src->m_has_pseudo);
272 
273   gdb_assert (gdbarch == src->arch ());
274 
275   /* Copy over any registers, being careful to only restore those that
276      were both saved and need to be restored.  The full [0 .. gdbarch_num_regs
277      + gdbarch_num_pseudo_regs) range is checked since some architectures need
278      to save/restore `cooked' registers that live in memory.  */
279   for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
280     {
281       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
282 	{
283 	  if (src->m_register_status[regnum] == REG_VALID)
284 	    cooked_write (regnum, src->register_buffer (regnum));
285 	}
286     }
287 }
288 
289 /* See gdbsupport/common-regcache.h.  */
290 
291 enum register_status
292 reg_buffer::get_register_status (int regnum) const
293 {
294   assert_regnum (regnum);
295 
296   return m_register_status[regnum];
297 }
298 
299 void
300 reg_buffer::invalidate (int regnum)
301 {
302   assert_regnum (regnum);
303   m_register_status[regnum] = REG_UNKNOWN;
304 }
305 
306 void
307 reg_buffer::assert_regnum (int regnum) const
308 {
309   gdb_assert (regnum >= 0);
310   if (m_has_pseudo)
311     gdb_assert (regnum < m_descr->nr_cooked_registers);
312   else
313     gdb_assert (regnum < gdbarch_num_regs (arch ()));
314 }
315 
316 /* Type to map a ptid to a list of regcaches (one thread may have multiple
317    regcaches, associated to different gdbarches).  */
318 
319 using ptid_regcache_map
320   = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
321 
322 /* Type holding regcaches for a given pid.  */
323 
324 using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>;
325 
326 /* Type holding regcaches for a given target.  */
327 
328 using target_pid_ptid_regcache_map
329   = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>;
330 
331 /* Global structure containing the existing regcaches.  */
332 
333 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
334    recording if the register values have been changed (eg. by the
335    user).  Therefore all registers must be written back to the
336    target when appropriate.  */
337 static target_pid_ptid_regcache_map regcaches;
338 
339 struct regcache *
340 get_thread_arch_aspace_regcache (process_stratum_target *target,
341 				 ptid_t ptid, gdbarch *arch,
342 				 struct address_space *aspace)
343 {
344   gdb_assert (target != nullptr);
345 
346   /* Find the map for this target.  */
347   pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[target];
348 
349   /* Find the map for this pid.  */
350   ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()];
351 
352   /* Check first if a regcache for this arch already exists.  */
353   auto range = ptid_regc_map.equal_range (ptid);
354   for (auto it = range.first; it != range.second; ++it)
355     {
356       if (it->second->arch () == arch)
357 	return it->second.get ();
358     }
359 
360   /* It does not exist, create it.  */
361   regcache *new_regcache = new regcache (target, arch, aspace);
362   new_regcache->set_ptid (ptid);
363   /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
364      constructor explictly instead of implicitly.  */
365   ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
366 
367   return new_regcache;
368 }
369 
370 struct regcache *
371 get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
372 			  struct gdbarch *gdbarch)
373 {
374   scoped_restore_current_inferior restore_current_inferior;
375   set_current_inferior (find_inferior_ptid (target, ptid));
376   address_space *aspace = target_thread_address_space (ptid);
377 
378   return get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
379 }
380 
381 static process_stratum_target *current_thread_target;
382 static ptid_t current_thread_ptid;
383 static struct gdbarch *current_thread_arch;
384 
385 struct regcache *
386 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
387 {
388   if (!current_thread_arch
389       || target != current_thread_target
390       || current_thread_ptid != ptid)
391     {
392       gdb_assert (ptid != null_ptid);
393 
394       current_thread_ptid = ptid;
395       current_thread_target = target;
396 
397       scoped_restore_current_inferior restore_current_inferior;
398       set_current_inferior (find_inferior_ptid (target, ptid));
399       current_thread_arch = target_thread_architecture (ptid);
400     }
401 
402   return get_thread_arch_regcache (target, ptid, current_thread_arch);
403 }
404 
405 /* See regcache.h.  */
406 
407 struct regcache *
408 get_thread_regcache (thread_info *thread)
409 {
410   return get_thread_regcache (thread->inf->process_target (),
411 			      thread->ptid);
412 }
413 
414 struct regcache *
415 get_current_regcache (void)
416 {
417   return get_thread_regcache (inferior_thread ());
418 }
419 
420 /* See gdbsupport/common-regcache.h.  */
421 
422 struct regcache *
423 get_thread_regcache_for_ptid (ptid_t ptid)
424 {
425   /* This function doesn't take a process_stratum_target parameter
426      because it's a gdbsupport/ routine implemented by both gdb and
427      gdbserver.  It always refers to a ptid of the current target.  */
428   process_stratum_target *proc_target = current_inferior ()->process_target ();
429   return get_thread_regcache (proc_target, ptid);
430 }
431 
432 /* Observer for the target_changed event.  */
433 
434 static void
435 regcache_observer_target_changed (struct target_ops *target)
436 {
437   registers_changed ();
438 }
439 
440 /* Update regcaches related to OLD_PTID to now use NEW_PTID.  */
441 static void
442 regcache_thread_ptid_changed (process_stratum_target *target,
443 			      ptid_t old_ptid, ptid_t new_ptid)
444 {
445   /* Look up map for target.  */
446   auto pid_ptid_regc_map_it = regcaches.find (target);
447   if (pid_ptid_regc_map_it == regcaches.end ())
448     return;
449 
450  /* Look up map for pid.  */
451   pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
452   auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ());
453   if (ptid_regc_map_it == pid_ptid_regc_map.end ())
454     return;
455 
456   /* Update all regcaches belonging to old_ptid.  */
457   ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
458   auto range = ptid_regc_map.equal_range (old_ptid);
459   for (auto it = range.first; it != range.second;)
460     {
461       regcache_up rc = std::move (it->second);
462       rc->set_ptid (new_ptid);
463 
464       /* Remove old before inserting new, to avoid rehashing,
465 	 which would invalidate iterators.  */
466       it = ptid_regc_map.erase (it);
467       ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
468     }
469 }
470 
471 /* Low level examining and depositing of registers.
472 
473    The caller is responsible for making sure that the inferior is
474    stopped before calling the fetching routines, or it will get
475    garbage.  (a change from GDB version 3, in which the caller got the
476    value from the last stop).  */
477 
478 /* REGISTERS_CHANGED ()
479 
480    Indicate that registers may have changed, so invalidate the cache.  */
481 
482 void
483 registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
484 {
485   if (target == nullptr)
486     {
487       /* Since there can be ptid clashes between targets, it's not valid to
488 	 pass a ptid without saying to which target it belongs.  */
489       gdb_assert (ptid == minus_one_ptid);
490 
491       /* Delete all the regcaches of all targets.  */
492       regcaches.clear ();
493     }
494   else if (ptid.is_pid ())
495     {
496       /* Non-NULL target and pid ptid, delete all regcaches belonging
497 	 to this (TARGET, PID).  */
498 
499       /* Look up map for target.  */
500       auto pid_ptid_regc_map_it = regcaches.find (target);
501       if (pid_ptid_regc_map_it != regcaches.end ())
502 	{
503 	  pid_ptid_regcache_map &pid_ptid_regc_map
504 	    = pid_ptid_regc_map_it->second;
505 
506 	  pid_ptid_regc_map.erase (ptid.pid ());
507 	}
508     }
509   else if (ptid != minus_one_ptid)
510     {
511       /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
512 	 to this (TARGET, PTID).  */
513 
514       /* Look up map for target.  */
515       auto pid_ptid_regc_map_it = regcaches.find (target);
516       if (pid_ptid_regc_map_it != regcaches.end ())
517 	{
518 	  pid_ptid_regcache_map &pid_ptid_regc_map
519 	    = pid_ptid_regc_map_it->second;
520 
521 	  /* Look up map for pid.  */
522 	  auto ptid_regc_map_it
523 	    = pid_ptid_regc_map.find (ptid.pid ());
524 	  if (ptid_regc_map_it != pid_ptid_regc_map.end ())
525 	    {
526 	      ptid_regcache_map &ptid_regc_map
527 		= ptid_regc_map_it->second;
528 
529 	      ptid_regc_map.erase (ptid);
530 	    }
531 	}
532     }
533   else
534     {
535        /* Non-NULL target and minus_one_ptid, delete all regcaches
536 	  associated to this target.  */
537       regcaches.erase (target);
538     }
539 
540   if ((target == nullptr || current_thread_target == target)
541       && current_thread_ptid.matches (ptid))
542     {
543       current_thread_target = NULL;
544       current_thread_ptid = null_ptid;
545       current_thread_arch = NULL;
546     }
547 
548   if ((target == nullptr || current_inferior ()->process_target () == target)
549       && inferior_ptid.matches (ptid))
550     {
551       /* We just deleted the regcache of the current thread.  Need to
552 	 forget about any frames we have cached, too.  */
553       reinit_frame_cache ();
554     }
555 }
556 
557 /* See regcache.h.  */
558 
559 void
560 registers_changed_thread (thread_info *thread)
561 {
562   registers_changed_ptid (thread->inf->process_target (), thread->ptid);
563 }
564 
565 void
566 registers_changed (void)
567 {
568   registers_changed_ptid (nullptr, minus_one_ptid);
569 }
570 
571 void
572 regcache::raw_update (int regnum)
573 {
574   assert_regnum (regnum);
575 
576   /* Make certain that the register cache is up-to-date with respect
577      to the current thread.  This switching shouldn't be necessary
578      only there is still only one target side register cache.  Sigh!
579      On the bright side, at least there is a regcache object.  */
580 
581   if (get_register_status (regnum) == REG_UNKNOWN)
582     {
583       target_fetch_registers (this, regnum);
584 
585       /* A number of targets can't access the whole set of raw
586 	 registers (because the debug API provides no means to get at
587 	 them).  */
588       if (m_register_status[regnum] == REG_UNKNOWN)
589 	m_register_status[regnum] = REG_UNAVAILABLE;
590     }
591 }
592 
593 enum register_status
594 readable_regcache::raw_read (int regnum, gdb_byte *buf)
595 {
596   gdb_assert (buf != NULL);
597   raw_update (regnum);
598 
599   if (m_register_status[regnum] != REG_VALID)
600     memset (buf, 0, m_descr->sizeof_register[regnum]);
601   else
602     memcpy (buf, register_buffer (regnum),
603 	    m_descr->sizeof_register[regnum]);
604 
605   return m_register_status[regnum];
606 }
607 
608 enum register_status
609 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
610 {
611   gdb_assert (regcache != NULL);
612   return regcache->raw_read (regnum, val);
613 }
614 
615 template<typename T, typename>
616 enum register_status
617 readable_regcache::raw_read (int regnum, T *val)
618 {
619   gdb_byte *buf;
620   enum register_status status;
621 
622   assert_regnum (regnum);
623   buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
624   status = raw_read (regnum, buf);
625   if (status == REG_VALID)
626     *val = extract_integer<T> (buf,
627 			       m_descr->sizeof_register[regnum],
628 			       gdbarch_byte_order (m_descr->gdbarch));
629   else
630     *val = 0;
631   return status;
632 }
633 
634 enum register_status
635 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
636 			    ULONGEST *val)
637 {
638   gdb_assert (regcache != NULL);
639   return regcache->raw_read (regnum, val);
640 }
641 
642 void
643 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
644 {
645   gdb_assert (regcache != NULL);
646   regcache->raw_write (regnum, val);
647 }
648 
649 template<typename T, typename>
650 void
651 regcache::raw_write (int regnum, T val)
652 {
653   gdb_byte *buf;
654 
655   assert_regnum (regnum);
656   buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
657   store_integer (buf, m_descr->sizeof_register[regnum],
658 		 gdbarch_byte_order (m_descr->gdbarch), val);
659   raw_write (regnum, buf);
660 }
661 
662 void
663 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
664 			     ULONGEST val)
665 {
666   gdb_assert (regcache != NULL);
667   regcache->raw_write (regnum, val);
668 }
669 
670 LONGEST
671 regcache_raw_get_signed (struct regcache *regcache, int regnum)
672 {
673   LONGEST value;
674   enum register_status status;
675 
676   status = regcache_raw_read_signed (regcache, regnum, &value);
677   if (status == REG_UNAVAILABLE)
678     throw_error (NOT_AVAILABLE_ERROR,
679 		 _("Register %d is not available"), regnum);
680   return value;
681 }
682 
683 enum register_status
684 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
685 {
686   gdb_assert (regnum >= 0);
687   gdb_assert (regnum < m_descr->nr_cooked_registers);
688   if (regnum < num_raw_registers ())
689     return raw_read (regnum, buf);
690   else if (m_has_pseudo
691 	   && m_register_status[regnum] != REG_UNKNOWN)
692     {
693       if (m_register_status[regnum] == REG_VALID)
694 	memcpy (buf, register_buffer (regnum),
695 		m_descr->sizeof_register[regnum]);
696       else
697 	memset (buf, 0, m_descr->sizeof_register[regnum]);
698 
699       return m_register_status[regnum];
700     }
701   else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
702     {
703       struct value *mark, *computed;
704       enum register_status result = REG_VALID;
705 
706       mark = value_mark ();
707 
708       computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
709 						     this, regnum);
710       if (value_entirely_available (computed))
711 	memcpy (buf, value_contents_raw (computed),
712 		m_descr->sizeof_register[regnum]);
713       else
714 	{
715 	  memset (buf, 0, m_descr->sizeof_register[regnum]);
716 	  result = REG_UNAVAILABLE;
717 	}
718 
719       value_free_to_mark (mark);
720 
721       return result;
722     }
723   else
724     return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
725 					 regnum, buf);
726 }
727 
728 struct value *
729 readable_regcache::cooked_read_value (int regnum)
730 {
731   gdb_assert (regnum >= 0);
732   gdb_assert (regnum < m_descr->nr_cooked_registers);
733 
734   if (regnum < num_raw_registers ()
735       || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
736       || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
737     {
738       struct value *result;
739 
740       result = allocate_value (register_type (m_descr->gdbarch, regnum));
741       VALUE_LVAL (result) = lval_register;
742       VALUE_REGNUM (result) = regnum;
743 
744       /* It is more efficient in general to do this delegation in this
745 	 direction than in the other one, even though the value-based
746 	 API is preferred.  */
747       if (cooked_read (regnum,
748 		       value_contents_raw (result)) == REG_UNAVAILABLE)
749 	mark_value_bytes_unavailable (result, 0,
750 				      TYPE_LENGTH (value_type (result)));
751 
752       return result;
753     }
754   else
755     return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
756 					       this, regnum);
757 }
758 
759 enum register_status
760 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
761 			     LONGEST *val)
762 {
763   gdb_assert (regcache != NULL);
764   return regcache->cooked_read (regnum, val);
765 }
766 
767 template<typename T, typename>
768 enum register_status
769 readable_regcache::cooked_read (int regnum, T *val)
770 {
771   enum register_status status;
772   gdb_byte *buf;
773 
774   gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
775   buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
776   status = cooked_read (regnum, buf);
777   if (status == REG_VALID)
778     *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
779 			       gdbarch_byte_order (m_descr->gdbarch));
780   else
781     *val = 0;
782   return status;
783 }
784 
785 enum register_status
786 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
787 			       ULONGEST *val)
788 {
789   gdb_assert (regcache != NULL);
790   return regcache->cooked_read (regnum, val);
791 }
792 
793 void
794 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
795 			      LONGEST val)
796 {
797   gdb_assert (regcache != NULL);
798   regcache->cooked_write (regnum, val);
799 }
800 
801 template<typename T, typename>
802 void
803 regcache::cooked_write (int regnum, T val)
804 {
805   gdb_byte *buf;
806 
807   gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
808   buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
809   store_integer (buf, m_descr->sizeof_register[regnum],
810 		 gdbarch_byte_order (m_descr->gdbarch), val);
811   cooked_write (regnum, buf);
812 }
813 
814 void
815 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
816 				ULONGEST val)
817 {
818   gdb_assert (regcache != NULL);
819   regcache->cooked_write (regnum, val);
820 }
821 
822 void
823 regcache::raw_write (int regnum, const gdb_byte *buf)
824 {
825 
826   gdb_assert (buf != NULL);
827   assert_regnum (regnum);
828 
829   /* On the sparc, writing %g0 is a no-op, so we don't even want to
830      change the registers array if something writes to this register.  */
831   if (gdbarch_cannot_store_register (arch (), regnum))
832     return;
833 
834   /* If we have a valid copy of the register, and new value == old
835      value, then don't bother doing the actual store.  */
836   if (get_register_status (regnum) == REG_VALID
837       && (memcmp (register_buffer (regnum), buf,
838 		  m_descr->sizeof_register[regnum]) == 0))
839     return;
840 
841   target_prepare_to_store (this);
842   raw_supply (regnum, buf);
843 
844   /* Invalidate the register after it is written, in case of a
845      failure.  */
846   auto invalidator
847     = make_scope_exit ([&] { this->invalidate (regnum); });
848 
849   target_store_registers (this, regnum);
850 
851   /* The target did not throw an error so we can discard invalidating
852      the register.  */
853   invalidator.release ();
854 }
855 
856 void
857 regcache::cooked_write (int regnum, const gdb_byte *buf)
858 {
859   gdb_assert (regnum >= 0);
860   gdb_assert (regnum < m_descr->nr_cooked_registers);
861   if (regnum < num_raw_registers ())
862     raw_write (regnum, buf);
863   else
864     gdbarch_pseudo_register_write (m_descr->gdbarch, this,
865 				   regnum, buf);
866 }
867 
868 /* See regcache.h.  */
869 
870 enum register_status
871 readable_regcache::read_part (int regnum, int offset, int len,
872 			      gdb_byte *out, bool is_raw)
873 {
874   int reg_size = register_size (arch (), regnum);
875 
876   gdb_assert (out != NULL);
877   gdb_assert (offset >= 0 && offset <= reg_size);
878   gdb_assert (len >= 0 && offset + len <= reg_size);
879 
880   if (offset == 0 && len == 0)
881     {
882       /* Nothing to do.  */
883       return REG_VALID;
884     }
885 
886   if (offset == 0 && len == reg_size)
887     {
888       /* Read the full register.  */
889       return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
890     }
891 
892   enum register_status status;
893   gdb_byte *reg = (gdb_byte *) alloca (reg_size);
894 
895   /* Read full register to buffer.  */
896   status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
897   if (status != REG_VALID)
898     return status;
899 
900   /* Copy out.  */
901   memcpy (out, reg + offset, len);
902   return REG_VALID;
903 }
904 
905 /* See regcache.h.  */
906 
907 void
908 reg_buffer::raw_collect_part (int regnum, int offset, int len,
909 			      gdb_byte *out) const
910 {
911   int reg_size = register_size (arch (), regnum);
912 
913   gdb_assert (out != nullptr);
914   gdb_assert (offset >= 0 && offset <= reg_size);
915   gdb_assert (len >= 0 && offset + len <= reg_size);
916 
917   if (offset == 0 && len == 0)
918     {
919       /* Nothing to do.  */
920       return;
921     }
922 
923   if (offset == 0 && len == reg_size)
924     {
925       /* Collect the full register.  */
926       return raw_collect (regnum, out);
927     }
928 
929   /* Read to buffer, then write out.  */
930   gdb_byte *reg = (gdb_byte *) alloca (reg_size);
931   raw_collect (regnum, reg);
932   memcpy (out, reg + offset, len);
933 }
934 
935 /* See regcache.h.  */
936 
937 enum register_status
938 regcache::write_part (int regnum, int offset, int len,
939 		      const gdb_byte *in, bool is_raw)
940 {
941   int reg_size = register_size (arch (), regnum);
942 
943   gdb_assert (in != NULL);
944   gdb_assert (offset >= 0 && offset <= reg_size);
945   gdb_assert (len >= 0 && offset + len <= reg_size);
946 
947   if (offset == 0 && len == 0)
948     {
949       /* Nothing to do.  */
950       return REG_VALID;
951     }
952 
953   if (offset == 0 && len == reg_size)
954     {
955       /* Write the full register.  */
956       (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
957       return REG_VALID;
958     }
959 
960   enum register_status status;
961   gdb_byte *reg = (gdb_byte *) alloca (reg_size);
962 
963   /* Read existing register to buffer.  */
964   status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
965   if (status != REG_VALID)
966     return status;
967 
968   /* Update buffer, then write back to regcache.  */
969   memcpy (reg + offset, in, len);
970   is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
971   return REG_VALID;
972 }
973 
974 /* See regcache.h.  */
975 
976 void
977 reg_buffer::raw_supply_part (int regnum, int offset, int len,
978 			     const gdb_byte *in)
979 {
980   int reg_size = register_size (arch (), regnum);
981 
982   gdb_assert (in != nullptr);
983   gdb_assert (offset >= 0 && offset <= reg_size);
984   gdb_assert (len >= 0 && offset + len <= reg_size);
985 
986   if (offset == 0 && len == 0)
987     {
988       /* Nothing to do.  */
989       return;
990     }
991 
992   if (offset == 0 && len == reg_size)
993     {
994       /* Supply the full register.  */
995       return raw_supply (regnum, in);
996     }
997 
998   gdb_byte *reg = (gdb_byte *) alloca (reg_size);
999 
1000   /* Read existing value to buffer.  */
1001   raw_collect (regnum, reg);
1002 
1003   /* Write to buffer, then write out.  */
1004   memcpy (reg + offset, in, len);
1005   raw_supply (regnum, reg);
1006 }
1007 
1008 enum register_status
1009 readable_regcache::raw_read_part (int regnum, int offset, int len,
1010 				  gdb_byte *buf)
1011 {
1012   assert_regnum (regnum);
1013   return read_part (regnum, offset, len, buf, true);
1014 }
1015 
1016 /* See regcache.h.  */
1017 
1018 void
1019 regcache::raw_write_part (int regnum, int offset, int len,
1020 			  const gdb_byte *buf)
1021 {
1022   assert_regnum (regnum);
1023   write_part (regnum, offset, len, buf, true);
1024 }
1025 
1026 /* See regcache.h.  */
1027 
1028 enum register_status
1029 readable_regcache::cooked_read_part (int regnum, int offset, int len,
1030 				     gdb_byte *buf)
1031 {
1032   gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1033   return read_part (regnum, offset, len, buf, false);
1034 }
1035 
1036 /* See regcache.h.  */
1037 
1038 void
1039 regcache::cooked_write_part (int regnum, int offset, int len,
1040 			     const gdb_byte *buf)
1041 {
1042   gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1043   write_part (regnum, offset, len, buf, false);
1044 }
1045 
1046 /* See gdbsupport/common-regcache.h.  */
1047 
1048 void
1049 reg_buffer::raw_supply (int regnum, const void *buf)
1050 {
1051   void *regbuf;
1052   size_t size;
1053 
1054   assert_regnum (regnum);
1055 
1056   regbuf = register_buffer (regnum);
1057   size = m_descr->sizeof_register[regnum];
1058 
1059   if (buf)
1060     {
1061       memcpy (regbuf, buf, size);
1062       m_register_status[regnum] = REG_VALID;
1063     }
1064   else
1065     {
1066       /* This memset not strictly necessary, but better than garbage
1067 	 in case the register value manages to escape somewhere (due
1068 	 to a bug, no less).  */
1069       memset (regbuf, 0, size);
1070       m_register_status[regnum] = REG_UNAVAILABLE;
1071     }
1072 }
1073 
1074 /* See regcache.h.  */
1075 
1076 void
1077 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1078 				int addr_len, bool is_signed)
1079 {
1080   enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1081   gdb_byte *regbuf;
1082   size_t regsize;
1083 
1084   assert_regnum (regnum);
1085 
1086   regbuf = register_buffer (regnum);
1087   regsize = m_descr->sizeof_register[regnum];
1088 
1089   copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1090 			byte_order);
1091   m_register_status[regnum] = REG_VALID;
1092 }
1093 
1094 /* See regcache.h.  */
1095 
1096 void
1097 reg_buffer::raw_supply_zeroed (int regnum)
1098 {
1099   void *regbuf;
1100   size_t size;
1101 
1102   assert_regnum (regnum);
1103 
1104   regbuf = register_buffer (regnum);
1105   size = m_descr->sizeof_register[regnum];
1106 
1107   memset (regbuf, 0, size);
1108   m_register_status[regnum] = REG_VALID;
1109 }
1110 
1111 /* See gdbsupport/common-regcache.h.  */
1112 
1113 void
1114 reg_buffer::raw_collect (int regnum, void *buf) const
1115 {
1116   const void *regbuf;
1117   size_t size;
1118 
1119   gdb_assert (buf != NULL);
1120   assert_regnum (regnum);
1121 
1122   regbuf = register_buffer (regnum);
1123   size = m_descr->sizeof_register[regnum];
1124   memcpy (buf, regbuf, size);
1125 }
1126 
1127 /* See regcache.h.  */
1128 
1129 void
1130 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1131 				 bool is_signed) const
1132 {
1133   enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1134   const gdb_byte *regbuf;
1135   size_t regsize;
1136 
1137   assert_regnum (regnum);
1138 
1139   regbuf = register_buffer (regnum);
1140   regsize = m_descr->sizeof_register[regnum];
1141 
1142   copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1143 			byte_order);
1144 }
1145 
1146 /* See regcache.h.  */
1147 
1148 void
1149 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1150 				    const gdb_byte *in_buf, gdb_byte *out_buf,
1151 				    int slot_size, int offs) const
1152 {
1153   struct gdbarch *gdbarch = arch ();
1154   int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1155 
1156   /* Use part versions and reg_size to prevent possible buffer overflows when
1157      accessing the regcache.  */
1158 
1159   if (out_buf != nullptr)
1160     {
1161       raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1162 
1163       /* Ensure any additional space is cleared.  */
1164       if (slot_size > reg_size)
1165 	memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1166     }
1167   else if (in_buf != nullptr)
1168     out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1169   else
1170     {
1171       /* Invalidate the register.  */
1172       out_regcache->raw_supply (regnum, nullptr);
1173     }
1174 }
1175 
1176 /* See regcache.h.  */
1177 
1178 void
1179 regcache::transfer_regset (const struct regset *regset,
1180 			   struct regcache *out_regcache,
1181 			   int regnum, const gdb_byte *in_buf,
1182 			   gdb_byte *out_buf, size_t size) const
1183 {
1184   const struct regcache_map_entry *map;
1185   int offs = 0, count;
1186 
1187   for (map = (const struct regcache_map_entry *) regset->regmap;
1188        (count = map->count) != 0;
1189        map++)
1190     {
1191       int regno = map->regno;
1192       int slot_size = map->size;
1193 
1194       if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1195 	slot_size = m_descr->sizeof_register[regno];
1196 
1197       if (regno == REGCACHE_MAP_SKIP
1198 	  || (regnum != -1
1199 	      && (regnum < regno || regnum >= regno + count)))
1200 	  offs += count * slot_size;
1201 
1202       else if (regnum == -1)
1203 	for (; count--; regno++, offs += slot_size)
1204 	  {
1205 	    if (offs + slot_size > size)
1206 	      break;
1207 
1208 	    transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1209 				      slot_size, offs);
1210 	  }
1211       else
1212 	{
1213 	  /* Transfer a single register and return.  */
1214 	  offs += (regnum - regno) * slot_size;
1215 	  if (offs + slot_size > size)
1216 	    return;
1217 
1218 	  transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1219 				    slot_size, offs);
1220 	  return;
1221 	}
1222     }
1223 }
1224 
1225 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1226    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1227    If BUF is NULL, set the register(s) to "unavailable" status. */
1228 
1229 void
1230 regcache_supply_regset (const struct regset *regset,
1231 			struct regcache *regcache,
1232 			int regnum, const void *buf, size_t size)
1233 {
1234   regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1235 }
1236 
1237 void
1238 regcache::supply_regset (const struct regset *regset,
1239 			 int regnum, const void *buf, size_t size)
1240 {
1241   transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
1242 }
1243 
1244 /* Collect register REGNUM from REGCACHE to BUF, using the register
1245    map in REGSET.  If REGNUM is -1, do this for all registers in
1246    REGSET.  */
1247 
1248 void
1249 regcache_collect_regset (const struct regset *regset,
1250 			 const struct regcache *regcache,
1251 			 int regnum, void *buf, size_t size)
1252 {
1253   regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1254 }
1255 
1256 void
1257 regcache::collect_regset (const struct regset *regset,
1258 			 int regnum, void *buf, size_t size) const
1259 {
1260   transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1261 }
1262 
1263 /* See gdbsupport/common-regcache.h.  */
1264 
1265 bool
1266 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1267 {
1268   gdb_assert (buf != NULL);
1269   assert_regnum (regnum);
1270 
1271   const char *regbuf = (const char *) register_buffer (regnum);
1272   size_t size = m_descr->sizeof_register[regnum];
1273   gdb_assert (size >= offset);
1274 
1275   return (memcmp (buf, regbuf + offset, size - offset) == 0);
1276 }
1277 
1278 /* Special handling for register PC.  */
1279 
1280 CORE_ADDR
1281 regcache_read_pc (struct regcache *regcache)
1282 {
1283   struct gdbarch *gdbarch = regcache->arch ();
1284 
1285   CORE_ADDR pc_val;
1286 
1287   if (gdbarch_read_pc_p (gdbarch))
1288     pc_val = gdbarch_read_pc (gdbarch, regcache);
1289   /* Else use per-frame method on get_current_frame.  */
1290   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1291     {
1292       ULONGEST raw_val;
1293 
1294       if (regcache_cooked_read_unsigned (regcache,
1295 					 gdbarch_pc_regnum (gdbarch),
1296 					 &raw_val) == REG_UNAVAILABLE)
1297 	throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1298 
1299       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1300     }
1301   else
1302     internal_error (__FILE__, __LINE__,
1303 		    _("regcache_read_pc: Unable to find PC"));
1304   return pc_val;
1305 }
1306 
1307 /* See gdbsupport/common-regcache.h.  */
1308 
1309 CORE_ADDR
1310 regcache_read_pc_protected (regcache *regcache)
1311 {
1312   CORE_ADDR pc;
1313   try
1314     {
1315       pc = regcache_read_pc (regcache);
1316     }
1317   catch (const gdb_exception_error &ex)
1318     {
1319       pc = 0;
1320     }
1321 
1322   return pc;
1323 }
1324 
1325 void
1326 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1327 {
1328   struct gdbarch *gdbarch = regcache->arch ();
1329 
1330   if (gdbarch_write_pc_p (gdbarch))
1331     gdbarch_write_pc (gdbarch, regcache, pc);
1332   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1333     regcache_cooked_write_unsigned (regcache,
1334 				    gdbarch_pc_regnum (gdbarch), pc);
1335   else
1336     internal_error (__FILE__, __LINE__,
1337 		    _("regcache_write_pc: Unable to update PC"));
1338 
1339   /* Writing the PC (for instance, from "load") invalidates the
1340      current frame.  */
1341   reinit_frame_cache ();
1342 }
1343 
1344 int
1345 reg_buffer::num_raw_registers () const
1346 {
1347   return gdbarch_num_regs (arch ());
1348 }
1349 
1350 void
1351 regcache::debug_print_register (const char *func,  int regno)
1352 {
1353   struct gdbarch *gdbarch = arch ();
1354 
1355   fprintf_unfiltered (gdb_stdlog, "%s ", func);
1356   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1357       && gdbarch_register_name (gdbarch, regno) != NULL
1358       && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1359     fprintf_unfiltered (gdb_stdlog, "(%s)",
1360 			gdbarch_register_name (gdbarch, regno));
1361   else
1362     fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1363   if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1364     {
1365       enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1366       int size = register_size (gdbarch, regno);
1367       gdb_byte *buf = register_buffer (regno);
1368 
1369       fprintf_unfiltered (gdb_stdlog, " = ");
1370       for (int i = 0; i < size; i++)
1371 	{
1372 	  fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1373 	}
1374       if (size <= sizeof (LONGEST))
1375 	{
1376 	  ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1377 
1378 	  fprintf_unfiltered (gdb_stdlog, " %s %s",
1379 			      core_addr_to_string_nz (val), plongest (val));
1380 	}
1381     }
1382   fprintf_unfiltered (gdb_stdlog, "\n");
1383 }
1384 
1385 static void
1386 reg_flush_command (const char *command, int from_tty)
1387 {
1388   /* Force-flush the register cache.  */
1389   registers_changed ();
1390   if (from_tty)
1391     printf_filtered (_("Register cache flushed.\n"));
1392 }
1393 
1394 void
1395 register_dump::dump (ui_file *file)
1396 {
1397   auto descr = regcache_descr (m_gdbarch);
1398   int regnum;
1399   int footnote_nr = 0;
1400   int footnote_register_offset = 0;
1401   int footnote_register_type_name_null = 0;
1402   long register_offset = 0;
1403 
1404   gdb_assert (descr->nr_cooked_registers
1405 	      == gdbarch_num_cooked_regs (m_gdbarch));
1406 
1407   for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1408     {
1409       /* Name.  */
1410       if (regnum < 0)
1411 	fprintf_unfiltered (file, " %-10s", "Name");
1412       else
1413 	{
1414 	  const char *p = gdbarch_register_name (m_gdbarch, regnum);
1415 
1416 	  if (p == NULL)
1417 	    p = "";
1418 	  else if (p[0] == '\0')
1419 	    p = "''";
1420 	  fprintf_unfiltered (file, " %-10s", p);
1421 	}
1422 
1423       /* Number.  */
1424       if (regnum < 0)
1425 	fprintf_unfiltered (file, " %4s", "Nr");
1426       else
1427 	fprintf_unfiltered (file, " %4d", regnum);
1428 
1429       /* Relative number.  */
1430       if (regnum < 0)
1431 	fprintf_unfiltered (file, " %4s", "Rel");
1432       else if (regnum < gdbarch_num_regs (m_gdbarch))
1433 	fprintf_unfiltered (file, " %4d", regnum);
1434       else
1435 	fprintf_unfiltered (file, " %4d",
1436 			    (regnum - gdbarch_num_regs (m_gdbarch)));
1437 
1438       /* Offset.  */
1439       if (regnum < 0)
1440 	fprintf_unfiltered (file, " %6s  ", "Offset");
1441       else
1442 	{
1443 	  fprintf_unfiltered (file, " %6ld",
1444 			      descr->register_offset[regnum]);
1445 	  if (register_offset != descr->register_offset[regnum]
1446 	      || (regnum > 0
1447 		  && (descr->register_offset[regnum]
1448 		      != (descr->register_offset[regnum - 1]
1449 			  + descr->sizeof_register[regnum - 1])))
1450 	      )
1451 	    {
1452 	      if (!footnote_register_offset)
1453 		footnote_register_offset = ++footnote_nr;
1454 	      fprintf_unfiltered (file, "*%d", footnote_register_offset);
1455 	    }
1456 	  else
1457 	    fprintf_unfiltered (file, "  ");
1458 	  register_offset = (descr->register_offset[regnum]
1459 			     + descr->sizeof_register[regnum]);
1460 	}
1461 
1462       /* Size.  */
1463       if (regnum < 0)
1464 	fprintf_unfiltered (file, " %5s ", "Size");
1465       else
1466 	fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1467 
1468       /* Type.  */
1469       {
1470 	const char *t;
1471 	std::string name_holder;
1472 
1473 	if (regnum < 0)
1474 	  t = "Type";
1475 	else
1476 	  {
1477 	    static const char blt[] = "builtin_type";
1478 
1479 	    t = register_type (m_gdbarch, regnum)->name ();
1480 	    if (t == NULL)
1481 	      {
1482 		if (!footnote_register_type_name_null)
1483 		  footnote_register_type_name_null = ++footnote_nr;
1484 		name_holder = string_printf ("*%d",
1485 					     footnote_register_type_name_null);
1486 		t = name_holder.c_str ();
1487 	      }
1488 	    /* Chop a leading builtin_type.  */
1489 	    if (startswith (t, blt))
1490 	      t += strlen (blt);
1491 	  }
1492 	fprintf_unfiltered (file, " %-15s", t);
1493       }
1494 
1495       /* Leading space always present.  */
1496       fprintf_unfiltered (file, " ");
1497 
1498       dump_reg (file, regnum);
1499 
1500       fprintf_unfiltered (file, "\n");
1501     }
1502 
1503   if (footnote_register_offset)
1504     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1505 			footnote_register_offset);
1506   if (footnote_register_type_name_null)
1507     fprintf_unfiltered (file,
1508 			"*%d: Register type's name NULL.\n",
1509 			footnote_register_type_name_null);
1510 }
1511 
1512 #if GDB_SELF_TEST
1513 #include "gdbsupport/selftest.h"
1514 #include "selftest-arch.h"
1515 #include "target-float.h"
1516 
1517 namespace selftests {
1518 
1519 static size_t
1520 regcaches_size ()
1521 {
1522   size_t size = 0;
1523 
1524   for (auto pid_ptid_regc_map_it = regcaches.cbegin ();
1525        pid_ptid_regc_map_it != regcaches.cend ();
1526        ++pid_ptid_regc_map_it)
1527     {
1528       const pid_ptid_regcache_map &pid_ptid_regc_map
1529 	= pid_ptid_regc_map_it->second;
1530 
1531       for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin ();
1532 	   ptid_regc_map_it != pid_ptid_regc_map.cend ();
1533 	   ++ptid_regc_map_it)
1534 	{
1535 	  const ptid_regcache_map &ptid_regc_map
1536 	    = ptid_regc_map_it->second;
1537 
1538 	  size += ptid_regc_map.size ();
1539 	}
1540     }
1541 
1542   return size;
1543 }
1544 
1545 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES.  */
1546 
1547 static int
1548 regcache_count (process_stratum_target *target, ptid_t ptid)
1549 {
1550   /* Look up map for target.  */
1551   auto pid_ptid_regc_map_it = regcaches.find (target);
1552   if (pid_ptid_regc_map_it != regcaches.end ())
1553     {
1554       pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
1555 
1556       /* Look map for pid.  */
1557       auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ());
1558       if (ptid_regc_map_it != pid_ptid_regc_map.end ())
1559 	{
1560 	  ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
1561 	  auto range = ptid_regc_map.equal_range (ptid);
1562 
1563 	  return std::distance (range.first, range.second);
1564 	}
1565     }
1566 
1567   return 0;
1568 };
1569 
1570 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks.  */
1571 
1572 static void
1573 get_thread_arch_aspace_regcache_and_check (process_stratum_target *target,
1574 					   ptid_t ptid)
1575 {
1576   /* We currently only test with a single gdbarch.  Any gdbarch will do, so use
1577      the current inferior's gdbarch.  Also use the current inferior's address
1578      space.  */
1579   gdbarch *arch = current_inferior ()->gdbarch;
1580   address_space *aspace = current_inferior ()->aspace;
1581   regcache *regcache
1582     = get_thread_arch_aspace_regcache (target, ptid, arch, aspace);
1583 
1584   SELF_CHECK (regcache != NULL);
1585   SELF_CHECK (regcache->target () == target);
1586   SELF_CHECK (regcache->ptid () == ptid);
1587   SELF_CHECK (regcache->arch () == arch);
1588   SELF_CHECK (regcache->aspace () == aspace);
1589 }
1590 
1591 /* The data that the regcaches selftests must hold onto for the duration of the
1592    test.  */
1593 
1594 struct regcache_test_data
1595 {
1596   regcache_test_data ()
1597   {
1598     /* Ensure the regcaches container is empty at the start.  */
1599     registers_changed ();
1600   }
1601 
1602   ~regcache_test_data ()
1603   {
1604     /* Make sure to leave the global regcaches container empty.  */
1605     registers_changed ();
1606   }
1607 
1608   test_target_ops test_target1;
1609   test_target_ops test_target2;
1610 };
1611 
1612 using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
1613 
1614 /* Set up a few regcaches from two different targets, for use in
1615    regcache-management tests.
1616 
1617    Return a pointer, because the `regcache_test_data` type is not moveable.  */
1618 
1619 static regcache_test_data_up
1620 populate_regcaches_for_test ()
1621 {
1622   regcache_test_data_up data (new regcache_test_data);
1623   size_t expected_regcache_size = 0;
1624 
1625   SELF_CHECK (regcaches_size () == 0);
1626 
1627   /* Populate the regcache container with a few regcaches for the two test
1628      targets. */
1629   for (int pid : { 1, 2 })
1630     {
1631       for (long lwp : { 1, 2, 3 })
1632 	{
1633 	  get_thread_arch_aspace_regcache_and_check
1634 	    (&data->test_target1, ptid_t (pid, lwp));
1635 	  expected_regcache_size++;
1636 	  SELF_CHECK (regcaches_size () == expected_regcache_size);
1637 
1638 	  get_thread_arch_aspace_regcache_and_check
1639 	    (&data->test_target2, ptid_t (pid, lwp));
1640 	  expected_regcache_size++;
1641 	  SELF_CHECK (regcaches_size () == expected_regcache_size);
1642 	}
1643     }
1644 
1645   return data;
1646 }
1647 
1648 static void
1649 get_thread_arch_aspace_regcache_test ()
1650 {
1651   /* populate_regcaches_for_test already tests most of the
1652      get_thread_arch_aspace_regcache functionality.  */
1653   regcache_test_data_up data = populate_regcaches_for_test ();
1654   size_t regcaches_size_before = regcaches_size ();
1655 
1656   /* Test that getting an existing regcache doesn't create a new one.  */
1657   get_thread_arch_aspace_regcache_and_check (&data->test_target1, ptid_t (2, 2));
1658   SELF_CHECK (regcaches_size () == regcaches_size_before);
1659 }
1660 
1661   /* Test marking all regcaches of all targets as changed.  */
1662 
1663 static void
1664 registers_changed_ptid_all_test ()
1665 {
1666   regcache_test_data_up data = populate_regcaches_for_test ();
1667 
1668   registers_changed_ptid (nullptr, minus_one_ptid);
1669   SELF_CHECK (regcaches_size () == 0);
1670 }
1671 
1672 /* Test marking regcaches of a specific target as changed.  */
1673 
1674 static void
1675 registers_changed_ptid_target_test ()
1676 {
1677   regcache_test_data_up data = populate_regcaches_for_test ();
1678 
1679   registers_changed_ptid (&data->test_target1, minus_one_ptid);
1680   SELF_CHECK (regcaches_size () == 6);
1681 
1682   /* Check that we deleted the regcache for the right target.  */
1683   SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1684   SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1685 }
1686 
1687 /* Test marking regcaches of a specific (target, pid) as changed.  */
1688 
1689 static void
1690 registers_changed_ptid_target_pid_test ()
1691 {
1692   regcache_test_data_up data = populate_regcaches_for_test ();
1693 
1694   registers_changed_ptid (&data->test_target1, ptid_t (2));
1695   SELF_CHECK (regcaches_size () == 9);
1696 
1697   /* Regcaches from target1 should not exist, while regcaches from target2
1698      should exist.  */
1699   SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1700   SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1701 }
1702 
1703 /* Test marking regcaches of a specific (target, ptid) as changed.  */
1704 
1705 static void
1706 registers_changed_ptid_target_ptid_test ()
1707 {
1708   regcache_test_data_up data = populate_regcaches_for_test ();
1709 
1710   registers_changed_ptid (&data->test_target1, ptid_t (2, 2));
1711   SELF_CHECK (regcaches_size () == 11);
1712 
1713   /* Check that we deleted the regcache for the right target.  */
1714   SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1715   SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1716 }
1717 
1718 class target_ops_no_register : public test_target_ops
1719 {
1720 public:
1721   target_ops_no_register ()
1722     : test_target_ops {}
1723   {}
1724 
1725   void reset ()
1726   {
1727     fetch_registers_called = 0;
1728     store_registers_called = 0;
1729     xfer_partial_called = 0;
1730   }
1731 
1732   void fetch_registers (regcache *regs, int regno) override;
1733   void store_registers (regcache *regs, int regno) override;
1734 
1735   enum target_xfer_status xfer_partial (enum target_object object,
1736 					const char *annex, gdb_byte *readbuf,
1737 					const gdb_byte *writebuf,
1738 					ULONGEST offset, ULONGEST len,
1739 					ULONGEST *xfered_len) override;
1740 
1741   unsigned int fetch_registers_called = 0;
1742   unsigned int store_registers_called = 0;
1743   unsigned int xfer_partial_called = 0;
1744 };
1745 
1746 void
1747 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1748 {
1749   /* Mark register available.  */
1750   regs->raw_supply_zeroed (regno);
1751   this->fetch_registers_called++;
1752 }
1753 
1754 void
1755 target_ops_no_register::store_registers (regcache *regs, int regno)
1756 {
1757   this->store_registers_called++;
1758 }
1759 
1760 enum target_xfer_status
1761 target_ops_no_register::xfer_partial (enum target_object object,
1762 				      const char *annex, gdb_byte *readbuf,
1763 				      const gdb_byte *writebuf,
1764 				      ULONGEST offset, ULONGEST len,
1765 				      ULONGEST *xfered_len)
1766 {
1767   this->xfer_partial_called++;
1768 
1769   *xfered_len = len;
1770   return TARGET_XFER_OK;
1771 }
1772 
1773 class readwrite_regcache : public regcache
1774 {
1775 public:
1776   readwrite_regcache (process_stratum_target *target,
1777 		      struct gdbarch *gdbarch)
1778     : regcache (target, gdbarch, nullptr)
1779   {}
1780 };
1781 
1782 /* Test regcache::cooked_read gets registers from raw registers and
1783    memory instead of target to_{fetch,store}_registers.  */
1784 
1785 static void
1786 cooked_read_test (struct gdbarch *gdbarch)
1787 {
1788   scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1789 
1790   /* Test that read one raw register from regcache_no_target will go
1791      to the target layer.  */
1792 
1793   /* Find a raw register which size isn't zero.  */
1794   int nonzero_regnum;
1795   for (nonzero_regnum = 0;
1796        nonzero_regnum < gdbarch_num_regs (gdbarch);
1797        nonzero_regnum++)
1798     {
1799       if (register_size (gdbarch, nonzero_regnum) != 0)
1800 	break;
1801     }
1802 
1803   readwrite_regcache readwrite (&mockctx.mock_target, gdbarch);
1804   gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1805 
1806   readwrite.raw_read (nonzero_regnum, buf.data ());
1807 
1808   /* raw_read calls target_fetch_registers.  */
1809   SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1810   mockctx.mock_target.reset ();
1811 
1812   /* Mark all raw registers valid, so the following raw registers
1813      accesses won't go to target.  */
1814   for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1815     readwrite.raw_update (i);
1816 
1817   mockctx.mock_target.reset ();
1818   /* Then, read all raw and pseudo registers, and don't expect calling
1819      to_{fetch,store}_registers.  */
1820   for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1821     {
1822       if (register_size (gdbarch, regnum) == 0)
1823 	continue;
1824 
1825       gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1826 
1827       SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1828 						      inner_buf.data ()));
1829 
1830       SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1831       SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1832       SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1833 
1834       mockctx.mock_target.reset ();
1835     }
1836 
1837   readonly_detached_regcache readonly (readwrite);
1838 
1839   /* GDB may go to target layer to fetch all registers and memory for
1840      readonly regcache.  */
1841   mockctx.mock_target.reset ();
1842 
1843   for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1844     {
1845       if (register_size (gdbarch, regnum) == 0)
1846 	continue;
1847 
1848       gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1849       enum register_status status = readonly.cooked_read (regnum,
1850 							  inner_buf.data ());
1851 
1852       if (regnum < gdbarch_num_regs (gdbarch))
1853 	{
1854 	  auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1855 
1856 	  if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1857 	      || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1858 	      || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1859 	      || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1860 	      || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1861 	      || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1862 	      || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1863 	      || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
1864 	    {
1865 	      /* Raw registers.  If raw registers are not in save_reggroup,
1866 		 their status are unknown.  */
1867 	      if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1868 		SELF_CHECK (status == REG_VALID);
1869 	      else
1870 		SELF_CHECK (status == REG_UNKNOWN);
1871 	    }
1872 	  else
1873 	    SELF_CHECK (status == REG_VALID);
1874 	}
1875       else
1876 	{
1877 	  if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1878 	    SELF_CHECK (status == REG_VALID);
1879 	  else
1880 	    {
1881 	      /* If pseudo registers are not in save_reggroup, some of
1882 		 them can be computed from saved raw registers, but some
1883 		 of them are unknown.  */
1884 	      auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1885 
1886 	      if (bfd_arch == bfd_arch_frv
1887 		  || bfd_arch == bfd_arch_m32c
1888 		  || bfd_arch == bfd_arch_mep
1889 		  || bfd_arch == bfd_arch_sh)
1890 		SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1891 	      else if (bfd_arch == bfd_arch_mips
1892 		       || bfd_arch == bfd_arch_h8300)
1893 		SELF_CHECK (status == REG_UNKNOWN);
1894 	      else
1895 		SELF_CHECK (status == REG_VALID);
1896 	    }
1897 	}
1898 
1899       SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1900       SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1901       SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1902 
1903       mockctx.mock_target.reset ();
1904     }
1905 }
1906 
1907 /* Test regcache::cooked_write by writing some expected contents to
1908    registers, and checking that contents read from registers and the
1909    expected contents are the same.  */
1910 
1911 static void
1912 cooked_write_test (struct gdbarch *gdbarch)
1913 {
1914   /* Error out if debugging something, because we're going to push the
1915      test target, which would pop any existing target.  */
1916   if (current_top_target ()->stratum () >= process_stratum)
1917     error (_("target already pushed"));
1918 
1919   /* Create a mock environment.  A process_stratum target pushed.  */
1920 
1921   target_ops_no_register mock_target;
1922 
1923   /* Push the process_stratum target so we can mock accessing
1924      registers.  */
1925   push_target (&mock_target);
1926 
1927   /* Pop it again on exit (return/exception).  */
1928   struct on_exit
1929   {
1930     ~on_exit ()
1931     {
1932       pop_all_targets_at_and_above (process_stratum);
1933     }
1934   } pop_targets;
1935 
1936   readwrite_regcache readwrite (&mock_target, gdbarch);
1937 
1938   const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1939 
1940   for (auto regnum = 0; regnum < num_regs; regnum++)
1941     {
1942       if (register_size (gdbarch, regnum) == 0
1943 	  || gdbarch_cannot_store_register (gdbarch, regnum))
1944 	continue;
1945 
1946       auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1947 
1948       if (bfd_arch == bfd_arch_sparc
1949 	  /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1950 	     SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test.  */
1951 	  && gdbarch_ptr_bit (gdbarch) == 64
1952 	  && (regnum >= gdbarch_num_regs (gdbarch)
1953 	      && regnum <= gdbarch_num_regs (gdbarch) + 4))
1954 	continue;
1955 
1956       std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1957       std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1958       const auto type = register_type (gdbarch, regnum);
1959 
1960       if (type->code () == TYPE_CODE_FLT
1961 	  || type->code () == TYPE_CODE_DECFLOAT)
1962 	{
1963 	  /* Generate valid float format.  */
1964 	  target_float_from_string (expected.data (), type, "1.25");
1965 	}
1966       else if (type->code () == TYPE_CODE_INT
1967 	       || type->code () == TYPE_CODE_ARRAY
1968 	       || type->code () == TYPE_CODE_PTR
1969 	       || type->code () == TYPE_CODE_UNION
1970 	       || type->code () == TYPE_CODE_STRUCT)
1971 	{
1972 	  if (bfd_arch == bfd_arch_ia64
1973 	      || (regnum >= gdbarch_num_regs (gdbarch)
1974 		  && (bfd_arch == bfd_arch_xtensa
1975 		      || bfd_arch == bfd_arch_bfin
1976 		      || bfd_arch == bfd_arch_m32c
1977 		      /* m68hc11 pseudo registers are in memory.  */
1978 		      || bfd_arch == bfd_arch_m68hc11
1979 		      || bfd_arch == bfd_arch_m68hc12
1980 		      || bfd_arch == bfd_arch_s390))
1981 	      || (bfd_arch == bfd_arch_frv
1982 		  /* FRV pseudo registers except iacc0.  */
1983 		  && regnum > gdbarch_num_regs (gdbarch)))
1984 	    {
1985 	      /* Skip setting the expected values for some architecture
1986 		 registers.  */
1987 	    }
1988 	  else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1989 	    {
1990 	      /* RL78_PC_REGNUM */
1991 	      for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1992 		expected[j] = j;
1993 	    }
1994 	  else
1995 	    {
1996 	      for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1997 		expected[j] = j;
1998 	    }
1999 	}
2000       else if (type->code () == TYPE_CODE_FLAGS)
2001 	{
2002 	  /* No idea how to test flags.  */
2003 	  continue;
2004 	}
2005       else
2006 	{
2007 	  /* If we don't know how to create the expected value for the
2008 	     this type, make it fail.  */
2009 	  SELF_CHECK (0);
2010 	}
2011 
2012       readwrite.cooked_write (regnum, expected.data ());
2013 
2014       SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2015       SELF_CHECK (expected == buf);
2016     }
2017 }
2018 
2019 /* Verify that when two threads with the same ptid exist (from two different
2020    targets) and one of them changes ptid, we only update the appropriate
2021    regcaches.  */
2022 
2023 static void
2024 regcache_thread_ptid_changed ()
2025 {
2026   /* This test relies on the global regcache list to initially be empty.  */
2027   registers_changed ();
2028 
2029   /* Any arch will do.  */
2030   gdbarch *arch = current_inferior ()->gdbarch;
2031 
2032   /* Prepare two targets with one thread each, with the same ptid.  */
2033   scoped_mock_context<test_target_ops> target1 (arch);
2034   scoped_mock_context<test_target_ops> target2 (arch);
2035   target2.mock_inferior.next = &target1.mock_inferior;
2036 
2037   ptid_t old_ptid (111, 222);
2038   ptid_t new_ptid (111, 333);
2039 
2040   target1.mock_inferior.pid = old_ptid.pid ();
2041   target1.mock_thread.ptid = old_ptid;
2042   target2.mock_inferior.pid = old_ptid.pid ();
2043   target2.mock_thread.ptid = old_ptid;
2044 
2045   gdb_assert (regcaches.empty ());
2046 
2047   /* Populate the regcaches container.  */
2048   get_thread_arch_aspace_regcache (&target1.mock_target, old_ptid, arch,
2049 				   nullptr);
2050   get_thread_arch_aspace_regcache (&target2.mock_target, old_ptid, arch,
2051 				   nullptr);
2052 
2053   gdb_assert (regcaches.size () == 2);
2054   gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
2055   gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
2056   gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2057   gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2058 
2059   thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
2060 
2061   gdb_assert (regcaches.size () == 2);
2062   gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
2063   gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
2064   gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2065   gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2066 
2067   /* Leave the regcache list empty.  */
2068   registers_changed ();
2069   gdb_assert (regcaches.empty ());
2070 }
2071 
2072 } // namespace selftests
2073 #endif /* GDB_SELF_TEST */
2074 
2075 void _initialize_regcache ();
2076 void
2077 _initialize_regcache ()
2078 {
2079   regcache_descr_handle
2080     = gdbarch_data_register_post_init (init_regcache_descr);
2081 
2082   gdb::observers::target_changed.attach (regcache_observer_target_changed);
2083   gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed);
2084 
2085   add_com ("flushregs", class_maintenance, reg_flush_command,
2086 	   _("Force gdb to flush its register cache (maintainer command)."));
2087 
2088 #if GDB_SELF_TEST
2089   selftests::register_test ("get_thread_arch_aspace_regcache",
2090   			    selftests::get_thread_arch_aspace_regcache_test);
2091   selftests::register_test ("registers_changed_ptid_all",
2092 			    selftests::registers_changed_ptid_all_test);
2093   selftests::register_test ("registers_changed_ptid_target",
2094   			    selftests::registers_changed_ptid_target_test);
2095   selftests::register_test ("registers_changed_ptid_target_pid",
2096   			    selftests::registers_changed_ptid_target_pid_test);
2097   selftests::register_test ("registers_changed_ptid_target_ptid",
2098 			    selftests::registers_changed_ptid_target_ptid_test);
2099 
2100   selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2101 					 selftests::cooked_read_test);
2102   selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2103 					 selftests::cooked_write_test);
2104   selftests::register_test ("regcache_thread_ptid_changed",
2105 			    selftests::regcache_thread_ptid_changed);
2106 #endif
2107 }
2108