Update NEWS post GDB 10 branch creation.
[binutils-gdb.git] / gdb / regcache.c
blob91d3202b94baa87573c1b903bd6759dd83f99981
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
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>
35 * DATA STRUCTURE
37 * Here is the actual register cache.
40 /* Per-architecture object describing the layout of a register cache.
41 Computed once when the architecture is created. */
43 struct gdbarch_data *regcache_descr_handle;
45 struct regcache_descr
47 /* The architecture this descriptor belongs to. */
48 struct gdbarch *gdbarch;
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;
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;
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;
73 /* Cached table containing the type of each register. */
74 struct type **register_type;
77 static void *
78 init_regcache_descr (struct gdbarch *gdbarch)
80 int i;
81 struct regcache_descr *descr;
82 gdb_assert (gdbarch != NULL);
84 /* Create an initial, zero filled, table. */
85 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
86 descr->gdbarch = gdbarch;
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);
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);
100 /* Construct a strictly RAW register cache. Don't allow pseudo's
101 into the register cache. */
103 /* Lay out the register cache.
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. */
111 long offset = 0;
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++)
119 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
120 descr->register_offset[i] = offset;
121 offset += descr->sizeof_register[i];
123 /* Set the real size of the raw register cache buffer. */
124 descr->sizeof_raw_registers = offset;
126 for (; i < descr->nr_cooked_registers; i++)
128 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
129 descr->register_offset[i] = offset;
130 offset += descr->sizeof_register[i];
132 /* Set the real size of the readonly register cache buffer. */
133 descr->sizeof_cooked_registers = offset;
136 return descr;
139 static struct regcache_descr *
140 regcache_descr (struct gdbarch *gdbarch)
142 return (struct regcache_descr *) gdbarch_data (gdbarch,
143 regcache_descr_handle);
146 /* Utility functions returning useful register attributes stored in
147 the regcache descr. */
149 struct type *
150 register_type (struct gdbarch *gdbarch, int regnum)
152 struct regcache_descr *descr = regcache_descr (gdbarch);
154 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
155 return descr->register_type[regnum];
158 /* Utility functions returning useful register attributes stored in
159 the regcache descr. */
162 register_size (struct gdbarch *gdbarch, int regnum)
164 struct regcache_descr *descr = regcache_descr (gdbarch);
165 int size;
167 gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
168 size = descr->sizeof_register[regnum];
169 return size;
172 /* See gdbsupport/common-regcache.h. */
175 regcache_register_size (const struct regcache *regcache, int n)
177 return register_size (regcache->arch (), n);
180 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
181 : m_has_pseudo (has_pseudo)
183 gdb_assert (gdbarch != NULL);
184 m_descr = regcache_descr (gdbarch);
186 if (has_pseudo)
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] ());
192 else
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)] ());
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)
206 m_ptid = minus_one_ptid;
209 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
210 : readonly_detached_regcache (src.arch (),
211 [&src] (int regnum, gdb_byte *buf)
213 return src.cooked_read (regnum, buf);
218 gdbarch *
219 reg_buffer::arch () const
221 return m_descr->gdbarch;
224 /* Return a pointer to register REGNUM's buffer cache. */
226 gdb_byte *
227 reg_buffer::register_buffer (int regnum) const
229 return m_registers.get () + m_descr->register_offset[regnum];
232 void
233 reg_buffer::save (register_read_ftype cooked_read)
235 struct gdbarch *gdbarch = m_descr->gdbarch;
236 int regnum;
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++)
249 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
251 gdb_byte *dst_buf = register_buffer (regnum);
252 enum register_status status = cooked_read (regnum, dst_buf);
254 gdb_assert (status != REG_UNKNOWN);
256 if (status != REG_VALID)
257 memset (dst_buf, 0, register_size (gdbarch, regnum));
259 m_register_status[regnum] = status;
264 void
265 regcache::restore (readonly_detached_regcache *src)
267 struct gdbarch *gdbarch = m_descr->gdbarch;
268 int regnum;
270 gdb_assert (src != NULL);
271 gdb_assert (src->m_has_pseudo);
273 gdb_assert (gdbarch == src->arch ());
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++)
281 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
283 if (src->m_register_status[regnum] == REG_VALID)
284 cooked_write (regnum, src->register_buffer (regnum));
289 /* See gdbsupport/common-regcache.h. */
291 enum register_status
292 reg_buffer::get_register_status (int regnum) const
294 assert_regnum (regnum);
296 return m_register_status[regnum];
299 void
300 reg_buffer::invalidate (int regnum)
302 assert_regnum (regnum);
303 m_register_status[regnum] = REG_UNKNOWN;
306 void
307 reg_buffer::assert_regnum (int regnum) const
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 ()));
316 /* Type to map a ptid to a list of regcaches (one thread may have multiple
317 regcaches, associated to different gdbarches). */
319 using ptid_regcache_map
320 = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
322 /* Type holding regcaches for a given pid. */
324 using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>;
326 /* Type holding regcaches for a given target. */
328 using target_pid_ptid_regcache_map
329 = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>;
331 /* Global structure containing the existing regcaches. */
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;
339 struct regcache *
340 get_thread_arch_aspace_regcache (process_stratum_target *target,
341 ptid_t ptid, gdbarch *arch,
342 struct address_space *aspace)
344 gdb_assert (target != nullptr);
346 /* Find the map for this target. */
347 pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[target];
349 /* Find the map for this pid. */
350 ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()];
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)
356 if (it->second->arch () == arch)
357 return it->second.get ();
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)));
367 return new_regcache;
370 struct regcache *
371 get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
372 struct gdbarch *gdbarch)
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);
378 return get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
381 static process_stratum_target *current_thread_target;
382 static ptid_t current_thread_ptid;
383 static struct gdbarch *current_thread_arch;
385 struct regcache *
386 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
388 if (!current_thread_arch
389 || target != current_thread_target
390 || current_thread_ptid != ptid)
392 gdb_assert (ptid != null_ptid);
394 current_thread_ptid = ptid;
395 current_thread_target = target;
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);
402 return get_thread_arch_regcache (target, ptid, current_thread_arch);
405 /* See regcache.h. */
407 struct regcache *
408 get_thread_regcache (thread_info *thread)
410 return get_thread_regcache (thread->inf->process_target (),
411 thread->ptid);
414 struct regcache *
415 get_current_regcache (void)
417 return get_thread_regcache (inferior_thread ());
420 /* See gdbsupport/common-regcache.h. */
422 struct regcache *
423 get_thread_regcache_for_ptid (ptid_t ptid)
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);
432 /* Observer for the target_changed event. */
434 static void
435 regcache_observer_target_changed (struct target_ops *target)
437 registers_changed ();
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)
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;
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;
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;)
461 regcache_up rc = std::move (it->second);
462 rc->set_ptid (new_ptid);
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)));
471 /* Low level examining and depositing of registers.
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). */
478 /* REGISTERS_CHANGED ()
480 Indicate that registers may have changed, so invalidate the cache. */
482 void
483 registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
485 if (target == nullptr)
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);
491 /* Delete all the regcaches of all targets. */
492 regcaches.clear ();
494 else if (ptid.is_pid ())
496 /* Non-NULL target and pid ptid, delete all regcaches belonging
497 to this (TARGET, PID). */
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 ())
503 pid_ptid_regcache_map &pid_ptid_regc_map
504 = pid_ptid_regc_map_it->second;
506 pid_ptid_regc_map.erase (ptid.pid ());
509 else if (ptid != minus_one_ptid)
511 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
512 to this (TARGET, PTID). */
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 ())
518 pid_ptid_regcache_map &pid_ptid_regc_map
519 = pid_ptid_regc_map_it->second;
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 ())
526 ptid_regcache_map &ptid_regc_map
527 = ptid_regc_map_it->second;
529 ptid_regc_map.erase (ptid);
533 else
535 /* Non-NULL target and minus_one_ptid, delete all regcaches
536 associated to this target. */
537 regcaches.erase (target);
540 if ((target == nullptr || current_thread_target == target)
541 && current_thread_ptid.matches (ptid))
543 current_thread_target = NULL;
544 current_thread_ptid = null_ptid;
545 current_thread_arch = NULL;
548 if ((target == nullptr || current_inferior ()->process_target () == target)
549 && inferior_ptid.matches (ptid))
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 ();
557 /* See regcache.h. */
559 void
560 registers_changed_thread (thread_info *thread)
562 registers_changed_ptid (thread->inf->process_target (), thread->ptid);
565 void
566 registers_changed (void)
568 registers_changed_ptid (nullptr, minus_one_ptid);
571 void
572 regcache::raw_update (int regnum)
574 assert_regnum (regnum);
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. */
581 if (get_register_status (regnum) == REG_UNKNOWN)
583 target_fetch_registers (this, regnum);
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;
593 enum register_status
594 readable_regcache::raw_read (int regnum, gdb_byte *buf)
596 gdb_assert (buf != NULL);
597 raw_update (regnum);
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]);
605 return m_register_status[regnum];
608 enum register_status
609 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
611 gdb_assert (regcache != NULL);
612 return regcache->raw_read (regnum, val);
615 template<typename T, typename>
616 enum register_status
617 readable_regcache::raw_read (int regnum, T *val)
619 gdb_byte *buf;
620 enum register_status status;
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;
634 enum register_status
635 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
636 ULONGEST *val)
638 gdb_assert (regcache != NULL);
639 return regcache->raw_read (regnum, val);
642 void
643 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
645 gdb_assert (regcache != NULL);
646 regcache->raw_write (regnum, val);
649 template<typename T, typename>
650 void
651 regcache::raw_write (int regnum, T val)
653 gdb_byte *buf;
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);
662 void
663 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
664 ULONGEST val)
666 gdb_assert (regcache != NULL);
667 regcache->raw_write (regnum, val);
670 LONGEST
671 regcache_raw_get_signed (struct regcache *regcache, int regnum)
673 LONGEST value;
674 enum register_status status;
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;
683 enum register_status
684 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
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)
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]);
699 return m_register_status[regnum];
701 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
703 struct value *mark, *computed;
704 enum register_status result = REG_VALID;
706 mark = value_mark ();
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
715 memset (buf, 0, m_descr->sizeof_register[regnum]);
716 result = REG_UNAVAILABLE;
719 value_free_to_mark (mark);
721 return result;
723 else
724 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
725 regnum, buf);
728 struct value *
729 readable_regcache::cooked_read_value (int regnum)
731 gdb_assert (regnum >= 0);
732 gdb_assert (regnum < m_descr->nr_cooked_registers);
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))
738 struct value *result;
740 result = allocate_value (register_type (m_descr->gdbarch, regnum));
741 VALUE_LVAL (result) = lval_register;
742 VALUE_REGNUM (result) = regnum;
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)));
752 return result;
754 else
755 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
756 this, regnum);
759 enum register_status
760 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
761 LONGEST *val)
763 gdb_assert (regcache != NULL);
764 return regcache->cooked_read (regnum, val);
767 template<typename T, typename>
768 enum register_status
769 readable_regcache::cooked_read (int regnum, T *val)
771 enum register_status status;
772 gdb_byte *buf;
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;
785 enum register_status
786 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
787 ULONGEST *val)
789 gdb_assert (regcache != NULL);
790 return regcache->cooked_read (regnum, val);
793 void
794 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
795 LONGEST val)
797 gdb_assert (regcache != NULL);
798 regcache->cooked_write (regnum, val);
801 template<typename T, typename>
802 void
803 regcache::cooked_write (int regnum, T val)
805 gdb_byte *buf;
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);
814 void
815 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
816 ULONGEST val)
818 gdb_assert (regcache != NULL);
819 regcache->cooked_write (regnum, val);
822 void
823 regcache::raw_write (int regnum, const gdb_byte *buf)
826 gdb_assert (buf != NULL);
827 assert_regnum (regnum);
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;
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;
841 target_prepare_to_store (this);
842 raw_supply (regnum, buf);
844 /* Invalidate the register after it is written, in case of a
845 failure. */
846 auto invalidator
847 = make_scope_exit ([&] { this->invalidate (regnum); });
849 target_store_registers (this, regnum);
851 /* The target did not throw an error so we can discard invalidating
852 the register. */
853 invalidator.release ();
856 void
857 regcache::cooked_write (int regnum, const gdb_byte *buf)
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);
868 /* See regcache.h. */
870 enum register_status
871 readable_regcache::read_part (int regnum, int offset, int len,
872 gdb_byte *out, bool is_raw)
874 int reg_size = register_size (arch (), regnum);
876 gdb_assert (out != NULL);
877 gdb_assert (offset >= 0 && offset <= reg_size);
878 gdb_assert (len >= 0 && offset + len <= reg_size);
880 if (offset == 0 && len == 0)
882 /* Nothing to do. */
883 return REG_VALID;
886 if (offset == 0 && len == reg_size)
888 /* Read the full register. */
889 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
892 enum register_status status;
893 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
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;
900 /* Copy out. */
901 memcpy (out, reg + offset, len);
902 return REG_VALID;
905 /* See regcache.h. */
907 void
908 reg_buffer::raw_collect_part (int regnum, int offset, int len,
909 gdb_byte *out) const
911 int reg_size = register_size (arch (), regnum);
913 gdb_assert (out != nullptr);
914 gdb_assert (offset >= 0 && offset <= reg_size);
915 gdb_assert (len >= 0 && offset + len <= reg_size);
917 if (offset == 0 && len == 0)
919 /* Nothing to do. */
920 return;
923 if (offset == 0 && len == reg_size)
925 /* Collect the full register. */
926 return raw_collect (regnum, out);
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);
935 /* See regcache.h. */
937 enum register_status
938 regcache::write_part (int regnum, int offset, int len,
939 const gdb_byte *in, bool is_raw)
941 int reg_size = register_size (arch (), regnum);
943 gdb_assert (in != NULL);
944 gdb_assert (offset >= 0 && offset <= reg_size);
945 gdb_assert (len >= 0 && offset + len <= reg_size);
947 if (offset == 0 && len == 0)
949 /* Nothing to do. */
950 return REG_VALID;
953 if (offset == 0 && len == reg_size)
955 /* Write the full register. */
956 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
957 return REG_VALID;
960 enum register_status status;
961 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
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;
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;
974 /* See regcache.h. */
976 void
977 reg_buffer::raw_supply_part (int regnum, int offset, int len,
978 const gdb_byte *in)
980 int reg_size = register_size (arch (), regnum);
982 gdb_assert (in != nullptr);
983 gdb_assert (offset >= 0 && offset <= reg_size);
984 gdb_assert (len >= 0 && offset + len <= reg_size);
986 if (offset == 0 && len == 0)
988 /* Nothing to do. */
989 return;
992 if (offset == 0 && len == reg_size)
994 /* Supply the full register. */
995 return raw_supply (regnum, in);
998 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
1000 /* Read existing value to buffer. */
1001 raw_collect (regnum, reg);
1003 /* Write to buffer, then write out. */
1004 memcpy (reg + offset, in, len);
1005 raw_supply (regnum, reg);
1008 enum register_status
1009 readable_regcache::raw_read_part (int regnum, int offset, int len,
1010 gdb_byte *buf)
1012 assert_regnum (regnum);
1013 return read_part (regnum, offset, len, buf, true);
1016 /* See regcache.h. */
1018 void
1019 regcache::raw_write_part (int regnum, int offset, int len,
1020 const gdb_byte *buf)
1022 assert_regnum (regnum);
1023 write_part (regnum, offset, len, buf, true);
1026 /* See regcache.h. */
1028 enum register_status
1029 readable_regcache::cooked_read_part (int regnum, int offset, int len,
1030 gdb_byte *buf)
1032 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1033 return read_part (regnum, offset, len, buf, false);
1036 /* See regcache.h. */
1038 void
1039 regcache::cooked_write_part (int regnum, int offset, int len,
1040 const gdb_byte *buf)
1042 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1043 write_part (regnum, offset, len, buf, false);
1046 /* See gdbsupport/common-regcache.h. */
1048 void
1049 reg_buffer::raw_supply (int regnum, const void *buf)
1051 void *regbuf;
1052 size_t size;
1054 assert_regnum (regnum);
1056 regbuf = register_buffer (regnum);
1057 size = m_descr->sizeof_register[regnum];
1059 if (buf)
1061 memcpy (regbuf, buf, size);
1062 m_register_status[regnum] = REG_VALID;
1064 else
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;
1074 /* See regcache.h. */
1076 void
1077 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1078 int addr_len, bool is_signed)
1080 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1081 gdb_byte *regbuf;
1082 size_t regsize;
1084 assert_regnum (regnum);
1086 regbuf = register_buffer (regnum);
1087 regsize = m_descr->sizeof_register[regnum];
1089 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1090 byte_order);
1091 m_register_status[regnum] = REG_VALID;
1094 /* See regcache.h. */
1096 void
1097 reg_buffer::raw_supply_zeroed (int regnum)
1099 void *regbuf;
1100 size_t size;
1102 assert_regnum (regnum);
1104 regbuf = register_buffer (regnum);
1105 size = m_descr->sizeof_register[regnum];
1107 memset (regbuf, 0, size);
1108 m_register_status[regnum] = REG_VALID;
1111 /* See gdbsupport/common-regcache.h. */
1113 void
1114 reg_buffer::raw_collect (int regnum, void *buf) const
1116 const void *regbuf;
1117 size_t size;
1119 gdb_assert (buf != NULL);
1120 assert_regnum (regnum);
1122 regbuf = register_buffer (regnum);
1123 size = m_descr->sizeof_register[regnum];
1124 memcpy (buf, regbuf, size);
1127 /* See regcache.h. */
1129 void
1130 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1131 bool is_signed) const
1133 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1134 const gdb_byte *regbuf;
1135 size_t regsize;
1137 assert_regnum (regnum);
1139 regbuf = register_buffer (regnum);
1140 regsize = m_descr->sizeof_register[regnum];
1142 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1143 byte_order);
1146 /* See regcache.h. */
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
1153 struct gdbarch *gdbarch = arch ();
1154 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1156 /* Use part versions and reg_size to prevent possible buffer overflows when
1157 accessing the regcache. */
1159 if (out_buf != nullptr)
1161 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
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);
1167 else if (in_buf != nullptr)
1168 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1169 else
1171 /* Invalidate the register. */
1172 out_regcache->raw_supply (regnum, nullptr);
1176 /* See regcache.h. */
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
1184 const struct regcache_map_entry *map;
1185 int offs = 0, count;
1187 for (map = (const struct regcache_map_entry *) regset->regmap;
1188 (count = map->count) != 0;
1189 map++)
1191 int regno = map->regno;
1192 int slot_size = map->size;
1194 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1195 slot_size = m_descr->sizeof_register[regno];
1197 if (regno == REGCACHE_MAP_SKIP
1198 || (regnum != -1
1199 && (regnum < regno || regnum >= regno + count)))
1200 offs += count * slot_size;
1202 else if (regnum == -1)
1203 for (; count--; regno++, offs += slot_size)
1205 if (offs + slot_size > size)
1206 break;
1208 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1209 slot_size, offs);
1211 else
1213 /* Transfer a single register and return. */
1214 offs += (regnum - regno) * slot_size;
1215 if (offs + slot_size > size)
1216 return;
1218 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1219 slot_size, offs);
1220 return;
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. */
1229 void
1230 regcache_supply_regset (const struct regset *regset,
1231 struct regcache *regcache,
1232 int regnum, const void *buf, size_t size)
1234 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1237 void
1238 regcache::supply_regset (const struct regset *regset,
1239 int regnum, const void *buf, size_t size)
1241 transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
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. */
1248 void
1249 regcache_collect_regset (const struct regset *regset,
1250 const struct regcache *regcache,
1251 int regnum, void *buf, size_t size)
1253 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1256 void
1257 regcache::collect_regset (const struct regset *regset,
1258 int regnum, void *buf, size_t size) const
1260 transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1263 /* See gdbsupport/common-regcache.h. */
1265 bool
1266 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1268 gdb_assert (buf != NULL);
1269 assert_regnum (regnum);
1271 const char *regbuf = (const char *) register_buffer (regnum);
1272 size_t size = m_descr->sizeof_register[regnum];
1273 gdb_assert (size >= offset);
1275 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1278 /* Special handling for register PC. */
1280 CORE_ADDR
1281 regcache_read_pc (struct regcache *regcache)
1283 struct gdbarch *gdbarch = regcache->arch ();
1285 CORE_ADDR pc_val;
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)
1292 ULONGEST raw_val;
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"));
1299 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1301 else
1302 internal_error (__FILE__, __LINE__,
1303 _("regcache_read_pc: Unable to find PC"));
1304 return pc_val;
1307 /* See gdbsupport/common-regcache.h. */
1309 CORE_ADDR
1310 regcache_read_pc_protected (regcache *regcache)
1312 CORE_ADDR pc;
1315 pc = regcache_read_pc (regcache);
1317 catch (const gdb_exception_error &ex)
1319 pc = 0;
1322 return pc;
1325 void
1326 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1328 struct gdbarch *gdbarch = regcache->arch ();
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"));
1339 /* Writing the PC (for instance, from "load") invalidates the
1340 current frame. */
1341 reinit_frame_cache ();
1345 reg_buffer::num_raw_registers () const
1347 return gdbarch_num_regs (arch ());
1350 void
1351 regcache::debug_print_register (const char *func, int regno)
1353 struct gdbarch *gdbarch = arch ();
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))
1365 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1366 int size = register_size (gdbarch, regno);
1367 gdb_byte *buf = register_buffer (regno);
1369 fprintf_unfiltered (gdb_stdlog, " = ");
1370 for (int i = 0; i < size; i++)
1372 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1374 if (size <= sizeof (LONGEST))
1376 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1378 fprintf_unfiltered (gdb_stdlog, " %s %s",
1379 core_addr_to_string_nz (val), plongest (val));
1382 fprintf_unfiltered (gdb_stdlog, "\n");
1385 static void
1386 reg_flush_command (const char *command, int from_tty)
1388 /* Force-flush the register cache. */
1389 registers_changed ();
1390 if (from_tty)
1391 printf_filtered (_("Register cache flushed.\n"));
1394 void
1395 register_dump::dump (ui_file *file)
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;
1404 gdb_assert (descr->nr_cooked_registers
1405 == gdbarch_num_cooked_regs (m_gdbarch));
1407 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1409 /* Name. */
1410 if (regnum < 0)
1411 fprintf_unfiltered (file, " %-10s", "Name");
1412 else
1414 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1416 if (p == NULL)
1417 p = "";
1418 else if (p[0] == '\0')
1419 p = "''";
1420 fprintf_unfiltered (file, " %-10s", p);
1423 /* Number. */
1424 if (regnum < 0)
1425 fprintf_unfiltered (file, " %4s", "Nr");
1426 else
1427 fprintf_unfiltered (file, " %4d", regnum);
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)));
1438 /* Offset. */
1439 if (regnum < 0)
1440 fprintf_unfiltered (file, " %6s ", "Offset");
1441 else
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])))
1452 if (!footnote_register_offset)
1453 footnote_register_offset = ++footnote_nr;
1454 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1456 else
1457 fprintf_unfiltered (file, " ");
1458 register_offset = (descr->register_offset[regnum]
1459 + descr->sizeof_register[regnum]);
1462 /* Size. */
1463 if (regnum < 0)
1464 fprintf_unfiltered (file, " %5s ", "Size");
1465 else
1466 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1468 /* Type. */
1470 const char *t;
1471 std::string name_holder;
1473 if (regnum < 0)
1474 t = "Type";
1475 else
1477 static const char blt[] = "builtin_type";
1479 t = register_type (m_gdbarch, regnum)->name ();
1480 if (t == NULL)
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 ();
1488 /* Chop a leading builtin_type. */
1489 if (startswith (t, blt))
1490 t += strlen (blt);
1492 fprintf_unfiltered (file, " %-15s", t);
1495 /* Leading space always present. */
1496 fprintf_unfiltered (file, " ");
1498 dump_reg (file, regnum);
1500 fprintf_unfiltered (file, "\n");
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);
1512 #if GDB_SELF_TEST
1513 #include "gdbsupport/selftest.h"
1514 #include "selftest-arch.h"
1515 #include "target-float.h"
1517 namespace selftests {
1519 static size_t
1520 regcaches_size ()
1522 size_t size = 0;
1524 for (auto pid_ptid_regc_map_it = regcaches.cbegin ();
1525 pid_ptid_regc_map_it != regcaches.cend ();
1526 ++pid_ptid_regc_map_it)
1528 const pid_ptid_regcache_map &pid_ptid_regc_map
1529 = pid_ptid_regc_map_it->second;
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)
1535 const ptid_regcache_map &ptid_regc_map
1536 = ptid_regc_map_it->second;
1538 size += ptid_regc_map.size ();
1542 return size;
1545 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1547 static int
1548 regcache_count (process_stratum_target *target, ptid_t ptid)
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 ())
1554 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
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 ())
1560 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
1561 auto range = ptid_regc_map.equal_range (ptid);
1563 return std::distance (range.first, range.second);
1567 return 0;
1570 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1572 static void
1573 get_thread_arch_aspace_regcache_and_check (process_stratum_target *target,
1574 ptid_t ptid)
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);
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);
1591 /* The data that the regcaches selftests must hold onto for the duration of the
1592 test. */
1594 struct regcache_test_data
1596 regcache_test_data ()
1598 /* Ensure the regcaches container is empty at the start. */
1599 registers_changed ();
1602 ~regcache_test_data ()
1604 /* Make sure to leave the global regcaches container empty. */
1605 registers_changed ();
1608 test_target_ops test_target1;
1609 test_target_ops test_target2;
1612 using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
1614 /* Set up a few regcaches from two different targets, for use in
1615 regcache-management tests.
1617 Return a pointer, because the `regcache_test_data` type is not moveable. */
1619 static regcache_test_data_up
1620 populate_regcaches_for_test ()
1622 regcache_test_data_up data (new regcache_test_data);
1623 size_t expected_regcache_size = 0;
1625 SELF_CHECK (regcaches_size () == 0);
1627 /* Populate the regcache container with a few regcaches for the two test
1628 targets. */
1629 for (int pid : { 1, 2 })
1631 for (long lwp : { 1, 2, 3 })
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);
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);
1645 return data;
1648 static void
1649 get_thread_arch_aspace_regcache_test ()
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 ();
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);
1661 /* Test marking all regcaches of all targets as changed. */
1663 static void
1664 registers_changed_ptid_all_test ()
1666 regcache_test_data_up data = populate_regcaches_for_test ();
1668 registers_changed_ptid (nullptr, minus_one_ptid);
1669 SELF_CHECK (regcaches_size () == 0);
1672 /* Test marking regcaches of a specific target as changed. */
1674 static void
1675 registers_changed_ptid_target_test ()
1677 regcache_test_data_up data = populate_regcaches_for_test ();
1679 registers_changed_ptid (&data->test_target1, minus_one_ptid);
1680 SELF_CHECK (regcaches_size () == 6);
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);
1687 /* Test marking regcaches of a specific (target, pid) as changed. */
1689 static void
1690 registers_changed_ptid_target_pid_test ()
1692 regcache_test_data_up data = populate_regcaches_for_test ();
1694 registers_changed_ptid (&data->test_target1, ptid_t (2));
1695 SELF_CHECK (regcaches_size () == 9);
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);
1703 /* Test marking regcaches of a specific (target, ptid) as changed. */
1705 static void
1706 registers_changed_ptid_target_ptid_test ()
1708 regcache_test_data_up data = populate_regcaches_for_test ();
1710 registers_changed_ptid (&data->test_target1, ptid_t (2, 2));
1711 SELF_CHECK (regcaches_size () == 11);
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);
1718 class target_ops_no_register : public test_target_ops
1720 public:
1721 target_ops_no_register ()
1722 : test_target_ops {}
1725 void reset ()
1727 fetch_registers_called = 0;
1728 store_registers_called = 0;
1729 xfer_partial_called = 0;
1732 void fetch_registers (regcache *regs, int regno) override;
1733 void store_registers (regcache *regs, int regno) override;
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;
1741 unsigned int fetch_registers_called = 0;
1742 unsigned int store_registers_called = 0;
1743 unsigned int xfer_partial_called = 0;
1746 void
1747 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1749 /* Mark register available. */
1750 regs->raw_supply_zeroed (regno);
1751 this->fetch_registers_called++;
1754 void
1755 target_ops_no_register::store_registers (regcache *regs, int regno)
1757 this->store_registers_called++;
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)
1767 this->xfer_partial_called++;
1769 *xfered_len = len;
1770 return TARGET_XFER_OK;
1773 class readwrite_regcache : public regcache
1775 public:
1776 readwrite_regcache (process_stratum_target *target,
1777 struct gdbarch *gdbarch)
1778 : regcache (target, gdbarch, nullptr)
1782 /* Test regcache::cooked_read gets registers from raw registers and
1783 memory instead of target to_{fetch,store}_registers. */
1785 static void
1786 cooked_read_test (struct gdbarch *gdbarch)
1788 scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1790 /* Test that read one raw register from regcache_no_target will go
1791 to the target layer. */
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++)
1799 if (register_size (gdbarch, nonzero_regnum) != 0)
1800 break;
1803 readwrite_regcache readwrite (&mockctx.mock_target, gdbarch);
1804 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1806 readwrite.raw_read (nonzero_regnum, buf.data ());
1808 /* raw_read calls target_fetch_registers. */
1809 SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1810 mockctx.mock_target.reset ();
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);
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++)
1822 if (register_size (gdbarch, regnum) == 0)
1823 continue;
1825 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1827 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1828 inner_buf.data ()));
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);
1834 mockctx.mock_target.reset ();
1837 readonly_detached_regcache readonly (readwrite);
1839 /* GDB may go to target layer to fetch all registers and memory for
1840 readonly regcache. */
1841 mockctx.mock_target.reset ();
1843 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1845 if (register_size (gdbarch, regnum) == 0)
1846 continue;
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 ());
1852 if (regnum < gdbarch_num_regs (gdbarch))
1854 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
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)
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);
1872 else
1873 SELF_CHECK (status == REG_VALID);
1875 else
1877 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1878 SELF_CHECK (status == REG_VALID);
1879 else
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;
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);
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);
1903 mockctx.mock_target.reset ();
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. */
1911 static void
1912 cooked_write_test (struct gdbarch *gdbarch)
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"));
1919 /* Create a mock environment. A process_stratum target pushed. */
1921 target_ops_no_register mock_target;
1923 /* Push the process_stratum target so we can mock accessing
1924 registers. */
1925 push_target (&mock_target);
1927 /* Pop it again on exit (return/exception). */
1928 struct on_exit
1930 ~on_exit ()
1932 pop_all_targets_at_and_above (process_stratum);
1934 } pop_targets;
1936 readwrite_regcache readwrite (&mock_target, gdbarch);
1938 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1940 for (auto regnum = 0; regnum < num_regs; regnum++)
1942 if (register_size (gdbarch, regnum) == 0
1943 || gdbarch_cannot_store_register (gdbarch, regnum))
1944 continue;
1946 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
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;
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);
1960 if (type->code () == TYPE_CODE_FLT
1961 || type->code () == TYPE_CODE_DECFLOAT)
1963 /* Generate valid float format. */
1964 target_float_from_string (expected.data (), type, "1.25");
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)
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)))
1985 /* Skip setting the expected values for some architecture
1986 registers. */
1988 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1990 /* RL78_PC_REGNUM */
1991 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1992 expected[j] = j;
1994 else
1996 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1997 expected[j] = j;
2000 else if (type->code () == TYPE_CODE_FLAGS)
2002 /* No idea how to test flags. */
2003 continue;
2005 else
2007 /* If we don't know how to create the expected value for the
2008 this type, make it fail. */
2009 SELF_CHECK (0);
2012 readwrite.cooked_write (regnum, expected.data ());
2014 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2015 SELF_CHECK (expected == buf);
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. */
2023 static void
2024 regcache_thread_ptid_changed ()
2026 /* This test relies on the global regcache list to initially be empty. */
2027 registers_changed ();
2029 /* Any arch will do. */
2030 gdbarch *arch = current_inferior ()->gdbarch;
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;
2037 ptid_t old_ptid (111, 222);
2038 ptid_t new_ptid (111, 333);
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;
2045 gdb_assert (regcaches.empty ());
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);
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);
2059 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
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);
2067 /* Leave the regcache list empty. */
2068 registers_changed ();
2069 gdb_assert (regcaches.empty ());
2072 } // namespace selftests
2073 #endif /* GDB_SELF_TEST */
2075 void _initialize_regcache ();
2076 void
2077 _initialize_regcache ()
2079 regcache_descr_handle
2080 = gdbarch_data_register_post_init (init_regcache_descr);
2082 gdb::observers::target_changed.attach (regcache_observer_target_changed);
2083 gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed);
2085 add_com ("flushregs", class_maintenance, reg_flush_command,
2086 _("Force gdb to flush its register cache (maintainer command)."));
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);
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