1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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/>. */
26 #include "reggroups.h"
31 #include <forward_list>
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created. */
42 struct gdbarch_data *regcache_descr_handle;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
54 long sizeof_raw_registers;
56 /* The cooked register space. Each cooked register in the range
57 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
58 register. The remaining [NR_RAW_REGISTERS
59 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
60 both raw registers and memory by the architecture methods
61 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
62 int nr_cooked_registers;
63 long sizeof_cooked_registers;
65 /* Offset and size (in 8 bit bytes), of each register in the
66 register cache. All registers (including those in the range
67 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
69 long *register_offset;
70 long *sizeof_register;
72 /* Cached table containing the type of each register. */
73 struct type **register_type;
77 init_regcache_descr (struct gdbarch *gdbarch)
80 struct regcache_descr *descr;
81 gdb_assert (gdbarch != NULL);
83 /* Create an initial, zero filled, table. */
84 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
85 descr->gdbarch = gdbarch;
87 /* Total size of the register space. The raw registers are mapped
88 directly onto the raw register cache while the pseudo's are
89 either mapped onto raw-registers or memory. */
90 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
91 + gdbarch_num_pseudo_regs (gdbarch);
93 /* Fill in a table of register types. */
95 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
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
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];
122 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
124 /* Set the real size of the raw register cache buffer. */
125 descr->sizeof_raw_registers = offset;
127 for (; i < descr->nr_cooked_registers; i++)
129 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
130 descr->register_offset[i] = offset;
131 offset += descr->sizeof_register[i];
132 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
134 /* Set the real size of the readonly register cache buffer. */
135 descr->sizeof_cooked_registers = offset;
141 static struct regcache_descr *
142 regcache_descr (struct gdbarch *gdbarch)
144 return (struct regcache_descr *) gdbarch_data (gdbarch,
145 regcache_descr_handle);
148 /* Utility functions returning useful register attributes stored in
149 the regcache descr. */
152 register_type (struct gdbarch *gdbarch, int regnum)
154 struct regcache_descr *descr = regcache_descr (gdbarch);
156 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
157 return descr->register_type[regnum];
160 /* Utility functions returning useful register attributes stored in
161 the regcache descr. */
164 register_size (struct gdbarch *gdbarch, int regnum)
166 struct regcache_descr *descr = regcache_descr (gdbarch);
169 gdb_assert (regnum >= 0
170 && regnum < (gdbarch_num_regs (gdbarch)
171 + gdbarch_num_pseudo_regs (gdbarch)));
172 size = descr->sizeof_register[regnum];
176 /* See common/common-regcache.h. */
179 regcache_register_size (const struct regcache *regcache, int n)
181 return register_size (regcache->arch (), n);
184 regcache::regcache (gdbarch *gdbarch, const address_space *aspace_,
186 : m_aspace (aspace_), m_readonly_p (readonly_p_)
188 gdb_assert (gdbarch != NULL);
189 m_descr = regcache_descr (gdbarch);
193 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
194 m_register_status = XCNEWVEC (signed char,
195 m_descr->nr_cooked_registers);
199 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
200 m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch));
202 m_ptid = minus_one_ptid;
205 static enum register_status
206 do_cooked_read (void *src, int regnum, gdb_byte *buf)
208 struct regcache *regcache = (struct regcache *) src;
210 return regcache_cooked_read (regcache, regnum, buf);
213 regcache::regcache (readonly_t, const regcache &src)
214 : regcache (src.arch (), nullptr, true)
216 gdb_assert (!src.m_readonly_p);
217 save (do_cooked_read, (void *) &src);
221 regcache::arch () const
223 return m_descr->gdbarch;
226 /* See regcache.h. */
229 regcache_get_ptid (const struct regcache *regcache)
231 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
233 return regcache->ptid ();
236 /* Cleanup class for invalidating a register. */
238 class regcache_invalidator
242 regcache_invalidator (struct regcache *regcache, int regnum)
243 : m_regcache (regcache),
248 ~regcache_invalidator ()
250 if (m_regcache != nullptr)
251 regcache_invalidate (m_regcache, m_regnum);
254 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
258 m_regcache = nullptr;
263 struct regcache *m_regcache;
267 /* Return a pointer to register REGNUM's buffer cache. */
270 regcache::register_buffer (int regnum) const
272 return m_registers + m_descr->register_offset[regnum];
276 regcache_save (struct regcache *regcache,
277 regcache_cooked_read_ftype *cooked_read, void *src)
279 regcache->save (cooked_read, src);
283 regcache::save (regcache_cooked_read_ftype *cooked_read,
286 struct gdbarch *gdbarch = m_descr->gdbarch;
289 /* The DST should be `read-only', if it wasn't then the save would
290 end up trying to write the register values back out to the
292 gdb_assert (m_readonly_p);
293 /* Clear the dest. */
294 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
295 memset (m_register_status, 0, m_descr->nr_cooked_registers);
296 /* Copy over any registers (identified by their membership in the
297 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
298 gdbarch_num_pseudo_regs) range is checked since some architectures need
299 to save/restore `cooked' registers that live in memory. */
300 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
302 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
304 gdb_byte *dst_buf = register_buffer (regnum);
305 enum register_status status = cooked_read (src, regnum, dst_buf);
307 gdb_assert (status != REG_UNKNOWN);
309 if (status != REG_VALID)
310 memset (dst_buf, 0, register_size (gdbarch, regnum));
312 m_register_status[regnum] = status;
318 regcache::restore (struct regcache *src)
320 struct gdbarch *gdbarch = m_descr->gdbarch;
323 /* The dst had better not be read-only. If it is, the `restore'
324 doesn't make much sense. */
325 gdb_assert (!m_readonly_p);
326 gdb_assert (src->m_readonly_p);
327 /* Copy over any registers, being careful to only restore those that
328 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
329 + gdbarch_num_pseudo_regs) range is checked since some architectures need
330 to save/restore `cooked' registers that live in memory. */
331 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
333 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
335 if (src->m_register_status[regnum] == REG_VALID)
336 cooked_write (regnum, src->register_buffer (regnum));
342 regcache_cpy (struct regcache *dst, struct regcache *src)
344 gdb_assert (src != NULL && dst != NULL);
345 gdb_assert (src->m_descr->gdbarch == dst->m_descr->gdbarch);
346 gdb_assert (src != dst);
347 gdb_assert (src->m_readonly_p && !dst->m_readonly_p);
353 regcache_dup (struct regcache *src)
355 return new regcache (regcache::readonly, *src);
359 regcache_register_status (const struct regcache *regcache, int regnum)
361 gdb_assert (regcache != NULL);
362 return regcache->get_register_status (regnum);
366 regcache::get_register_status (int regnum) const
368 gdb_assert (regnum >= 0);
370 gdb_assert (regnum < m_descr->nr_cooked_registers);
372 gdb_assert (regnum < num_raw_registers ());
374 return (enum register_status) m_register_status[regnum];
378 regcache_invalidate (struct regcache *regcache, int regnum)
380 gdb_assert (regcache != NULL);
381 regcache->invalidate (regnum);
385 regcache::invalidate (int regnum)
387 gdb_assert (!m_readonly_p);
388 assert_regnum (regnum);
389 m_register_status[regnum] = REG_UNKNOWN;
393 regcache::assert_regnum (int regnum) const
395 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (arch ()));
398 /* Global structure containing the current regcache. */
400 /* NOTE: this is a write-through cache. There is no "dirty" bit for
401 recording if the register values have been changed (eg. by the
402 user). Therefore all registers must be written back to the
403 target when appropriate. */
404 std::forward_list<regcache *> regcache::current_regcache;
407 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
408 struct address_space *aspace)
410 for (const auto ®cache : regcache::current_regcache)
411 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
414 regcache *new_regcache = new regcache (gdbarch, aspace, false);
416 regcache::current_regcache.push_front (new_regcache);
417 new_regcache->set_ptid (ptid);
423 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
425 address_space *aspace = target_thread_address_space (ptid);
427 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
430 static ptid_t current_thread_ptid;
431 static struct gdbarch *current_thread_arch;
434 get_thread_regcache (ptid_t ptid)
436 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
438 current_thread_ptid = ptid;
439 current_thread_arch = target_thread_architecture (ptid);
442 return get_thread_arch_regcache (ptid, current_thread_arch);
446 get_current_regcache (void)
448 return get_thread_regcache (inferior_ptid);
451 /* See common/common-regcache.h. */
454 get_thread_regcache_for_ptid (ptid_t ptid)
456 return get_thread_regcache (ptid);
459 /* Observer for the target_changed event. */
462 regcache_observer_target_changed (struct target_ops *target)
464 registers_changed ();
467 /* Update global variables old ptids to hold NEW_PTID if they were
470 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
472 for (auto ®cache : regcache::current_regcache)
474 if (ptid_equal (regcache->ptid (), old_ptid))
475 regcache->set_ptid (new_ptid);
479 /* Low level examining and depositing of registers.
481 The caller is responsible for making sure that the inferior is
482 stopped before calling the fetching routines, or it will get
483 garbage. (a change from GDB version 3, in which the caller got the
484 value from the last stop). */
486 /* REGISTERS_CHANGED ()
488 Indicate that registers may have changed, so invalidate the cache. */
491 registers_changed_ptid (ptid_t ptid)
493 for (auto oit = regcache::current_regcache.before_begin (),
494 it = std::next (oit);
495 it != regcache::current_regcache.end ();
498 if (ptid_match ((*it)->ptid (), ptid))
501 it = regcache::current_regcache.erase_after (oit);
507 if (ptid_match (current_thread_ptid, ptid))
509 current_thread_ptid = null_ptid;
510 current_thread_arch = NULL;
513 if (ptid_match (inferior_ptid, ptid))
515 /* We just deleted the regcache of the current thread. Need to
516 forget about any frames we have cached, too. */
517 reinit_frame_cache ();
522 registers_changed (void)
524 registers_changed_ptid (minus_one_ptid);
526 /* Force cleanup of any alloca areas if using C alloca instead of
527 a builtin alloca. This particular call is used to clean up
528 areas allocated by low level target code which may build up
529 during lengthy interactions between gdb and the target before
530 gdb gives control to the user (ie watchpoints). */
535 regcache_raw_update (struct regcache *regcache, int regnum)
537 gdb_assert (regcache != NULL);
539 regcache->raw_update (regnum);
543 regcache::raw_update (int regnum)
545 assert_regnum (regnum);
547 /* Make certain that the register cache is up-to-date with respect
548 to the current thread. This switching shouldn't be necessary
549 only there is still only one target side register cache. Sigh!
550 On the bright side, at least there is a regcache object. */
552 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
554 target_fetch_registers (this, regnum);
556 /* A number of targets can't access the whole set of raw
557 registers (because the debug API provides no means to get at
559 if (m_register_status[regnum] == REG_UNKNOWN)
560 m_register_status[regnum] = REG_UNAVAILABLE;
565 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
567 return regcache->raw_read (regnum, buf);
571 regcache::raw_read (int regnum, gdb_byte *buf)
573 gdb_assert (buf != NULL);
576 if (m_register_status[regnum] != REG_VALID)
577 memset (buf, 0, m_descr->sizeof_register[regnum]);
579 memcpy (buf, register_buffer (regnum),
580 m_descr->sizeof_register[regnum]);
582 return (enum register_status) m_register_status[regnum];
586 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
588 gdb_assert (regcache != NULL);
589 return regcache->raw_read (regnum, val);
592 template<typename T, typename>
594 regcache::raw_read (int regnum, T *val)
597 enum register_status status;
599 assert_regnum (regnum);
600 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
601 status = raw_read (regnum, buf);
602 if (status == REG_VALID)
603 *val = extract_integer<T> (buf,
604 m_descr->sizeof_register[regnum],
605 gdbarch_byte_order (m_descr->gdbarch));
612 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
615 gdb_assert (regcache != NULL);
616 return regcache->raw_read (regnum, val);
620 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
622 gdb_assert (regcache != NULL);
623 regcache->raw_write (regnum, val);
626 template<typename T, typename>
628 regcache::raw_write (int regnum, T val)
632 assert_regnum (regnum);
633 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
634 store_integer (buf, m_descr->sizeof_register[regnum],
635 gdbarch_byte_order (m_descr->gdbarch), val);
636 raw_write (regnum, buf);
640 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
643 gdb_assert (regcache != NULL);
644 regcache->raw_write (regnum, val);
648 regcache_raw_get_signed (struct regcache *regcache, int regnum)
651 enum register_status status;
653 status = regcache_raw_read_signed (regcache, regnum, &value);
654 if (status == REG_UNAVAILABLE)
655 throw_error (NOT_AVAILABLE_ERROR,
656 _("Register %d is not available"), regnum);
661 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
663 return regcache->cooked_read (regnum, buf);
667 regcache::cooked_read (int regnum, gdb_byte *buf)
669 gdb_assert (regnum >= 0);
670 gdb_assert (regnum < m_descr->nr_cooked_registers);
671 if (regnum < num_raw_registers ())
672 return raw_read (regnum, buf);
673 else if (m_readonly_p
674 && m_register_status[regnum] != REG_UNKNOWN)
676 /* Read-only register cache, perhaps the cooked value was
678 if (m_register_status[regnum] == REG_VALID)
679 memcpy (buf, register_buffer (regnum),
680 m_descr->sizeof_register[regnum]);
682 memset (buf, 0, m_descr->sizeof_register[regnum]);
684 return (enum register_status) m_register_status[regnum];
686 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
688 struct value *mark, *computed;
689 enum register_status result = REG_VALID;
691 mark = value_mark ();
693 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
695 if (value_entirely_available (computed))
696 memcpy (buf, value_contents_raw (computed),
697 m_descr->sizeof_register[regnum]);
700 memset (buf, 0, m_descr->sizeof_register[regnum]);
701 result = REG_UNAVAILABLE;
704 value_free_to_mark (mark);
709 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
714 regcache_cooked_read_value (struct regcache *regcache, int regnum)
716 return regcache->cooked_read_value (regnum);
720 regcache::cooked_read_value (int regnum)
722 gdb_assert (regnum >= 0);
723 gdb_assert (regnum < m_descr->nr_cooked_registers);
725 if (regnum < num_raw_registers ()
726 || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN)
727 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
729 struct value *result;
731 result = allocate_value (register_type (m_descr->gdbarch, regnum));
732 VALUE_LVAL (result) = lval_register;
733 VALUE_REGNUM (result) = regnum;
735 /* It is more efficient in general to do this delegation in this
736 direction than in the other one, even though the value-based
738 if (cooked_read (regnum,
739 value_contents_raw (result)) == REG_UNAVAILABLE)
740 mark_value_bytes_unavailable (result, 0,
741 TYPE_LENGTH (value_type (result)));
746 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
751 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
754 gdb_assert (regcache != NULL);
755 return regcache->cooked_read (regnum, val);
758 template<typename T, typename>
760 regcache::cooked_read (int regnum, T *val)
762 enum register_status status;
765 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
766 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
767 status = cooked_read (regnum, buf);
768 if (status == REG_VALID)
769 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
770 gdbarch_byte_order (m_descr->gdbarch));
777 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
780 gdb_assert (regcache != NULL);
781 return regcache->cooked_read (regnum, val);
785 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
788 gdb_assert (regcache != NULL);
789 regcache->cooked_write (regnum, val);
792 template<typename T, typename>
794 regcache::cooked_write (int regnum, T val)
798 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
799 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
800 store_integer (buf, m_descr->sizeof_register[regnum],
801 gdbarch_byte_order (m_descr->gdbarch), val);
802 cooked_write (regnum, buf);
806 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
809 gdb_assert (regcache != NULL);
810 regcache->cooked_write (regnum, val);
813 /* See regcache.h. */
816 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
819 regcache->raw_set_cached_value (regnum, buf);
823 regcache::raw_set_cached_value (int regnum, const gdb_byte *buf)
825 memcpy (register_buffer (regnum), buf,
826 m_descr->sizeof_register[regnum]);
827 m_register_status[regnum] = REG_VALID;
831 regcache_raw_write (struct regcache *regcache, int regnum,
834 gdb_assert (regcache != NULL && buf != NULL);
835 regcache->raw_write (regnum, buf);
839 regcache::raw_write (int regnum, const gdb_byte *buf)
842 gdb_assert (buf != NULL);
843 assert_regnum (regnum);
844 gdb_assert (!m_readonly_p);
846 /* On the sparc, writing %g0 is a no-op, so we don't even want to
847 change the registers array if something writes to this register. */
848 if (gdbarch_cannot_store_register (arch (), regnum))
851 /* If we have a valid copy of the register, and new value == old
852 value, then don't bother doing the actual store. */
853 if (get_register_status (regnum) == REG_VALID
854 && (memcmp (register_buffer (regnum), buf,
855 m_descr->sizeof_register[regnum]) == 0))
858 target_prepare_to_store (this);
859 raw_set_cached_value (regnum, buf);
861 /* Invalidate the register after it is written, in case of a
863 regcache_invalidator invalidator (this, regnum);
865 target_store_registers (this, regnum);
867 /* The target did not throw an error so we can discard invalidating
869 invalidator.release ();
873 regcache_cooked_write (struct regcache *regcache, int regnum,
876 regcache->cooked_write (regnum, buf);
880 regcache::cooked_write (int regnum, const gdb_byte *buf)
882 gdb_assert (regnum >= 0);
883 gdb_assert (regnum < m_descr->nr_cooked_registers);
884 if (regnum < num_raw_registers ())
885 raw_write (regnum, buf);
887 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
891 /* Perform a partial register transfer using a read, modify, write
894 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
896 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
900 regcache::xfer_part (int regnum, int offset, int len, void *in,
901 const void *out, bool is_raw)
903 struct gdbarch *gdbarch = arch ();
904 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
906 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
907 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
908 /* Something to do? */
909 if (offset + len == 0)
911 /* Read (when needed) ... */
914 || offset + len < m_descr->sizeof_register[regnum])
916 enum register_status status;
919 status = raw_read (regnum, reg);
921 status = cooked_read (regnum, reg);
922 if (status != REG_VALID)
927 memcpy (in, reg + offset, len);
929 memcpy (reg + offset, out, len);
930 /* ... write (when needed). */
934 raw_write (regnum, reg);
936 cooked_write (regnum, reg);
943 regcache_raw_read_part (struct regcache *regcache, int regnum,
944 int offset, int len, gdb_byte *buf)
946 return regcache->raw_read_part (regnum, offset, len, buf);
950 regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
952 assert_regnum (regnum);
953 return xfer_part (regnum, offset, len, buf, NULL, true);
957 regcache_raw_write_part (struct regcache *regcache, int regnum,
958 int offset, int len, const gdb_byte *buf)
960 regcache->raw_write_part (regnum, offset, len, buf);
964 regcache::raw_write_part (int regnum, int offset, int len,
967 assert_regnum (regnum);
968 xfer_part (regnum, offset, len, NULL, buf, true);
972 regcache_cooked_read_part (struct regcache *regcache, int regnum,
973 int offset, int len, gdb_byte *buf)
975 return regcache->cooked_read_part (regnum, offset, len, buf);
980 regcache::cooked_read_part (int regnum, int offset, int len, gdb_byte *buf)
982 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
983 return xfer_part (regnum, offset, len, buf, NULL, false);
987 regcache_cooked_write_part (struct regcache *regcache, int regnum,
988 int offset, int len, const gdb_byte *buf)
990 regcache->cooked_write_part (regnum, offset, len, buf);
994 regcache::cooked_write_part (int regnum, int offset, int len,
997 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
998 xfer_part (regnum, offset, len, NULL, buf, false);
1001 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1004 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1006 gdb_assert (regcache != NULL);
1007 regcache->raw_supply (regnum, buf);
1011 regcache::raw_supply (int regnum, const void *buf)
1016 assert_regnum (regnum);
1017 gdb_assert (!m_readonly_p);
1019 regbuf = register_buffer (regnum);
1020 size = m_descr->sizeof_register[regnum];
1024 memcpy (regbuf, buf, size);
1025 m_register_status[regnum] = REG_VALID;
1029 /* This memset not strictly necessary, but better than garbage
1030 in case the register value manages to escape somewhere (due
1031 to a bug, no less). */
1032 memset (regbuf, 0, size);
1033 m_register_status[regnum] = REG_UNAVAILABLE;
1037 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1038 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1039 the register size is greater than ADDR_LEN, then the integer will be sign or
1040 zero extended. If the register size is smaller than the integer, then the
1041 most significant bytes of the integer will be truncated. */
1044 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1047 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1051 assert_regnum (regnum);
1052 gdb_assert (!m_readonly_p);
1054 regbuf = register_buffer (regnum);
1055 regsize = m_descr->sizeof_register[regnum];
1057 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1059 m_register_status[regnum] = REG_VALID;
1062 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1063 as calling raw_supply with NULL (which will set the state to
1067 regcache::raw_supply_zeroed (int regnum)
1072 assert_regnum (regnum);
1073 gdb_assert (!m_readonly_p);
1075 regbuf = register_buffer (regnum);
1076 size = m_descr->sizeof_register[regnum];
1078 memset (regbuf, 0, size);
1079 m_register_status[regnum] = REG_VALID;
1082 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1085 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1087 gdb_assert (regcache != NULL && buf != NULL);
1088 regcache->raw_collect (regnum, buf);
1092 regcache::raw_collect (int regnum, void *buf) const
1097 gdb_assert (buf != NULL);
1098 assert_regnum (regnum);
1100 regbuf = register_buffer (regnum);
1101 size = m_descr->sizeof_register[regnum];
1102 memcpy (buf, regbuf, size);
1105 /* Transfer a single or all registers belonging to a certain register
1106 set to or from a buffer. This is the main worker function for
1107 regcache_supply_regset and regcache_collect_regset. */
1109 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1110 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1111 If ADDR_LEN is greater than the register size, then the integer will be sign
1112 or zero extended. If ADDR_LEN is smaller than the register size, then the
1113 most significant bytes of the integer will be truncated. */
1116 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1117 bool is_signed) const
1119 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1120 const gdb_byte *regbuf;
1123 assert_regnum (regnum);
1125 regbuf = register_buffer (regnum);
1126 regsize = m_descr->sizeof_register[regnum];
1128 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1133 regcache::transfer_regset (const struct regset *regset,
1134 struct regcache *out_regcache,
1135 int regnum, const void *in_buf,
1136 void *out_buf, size_t size) const
1138 const struct regcache_map_entry *map;
1139 int offs = 0, count;
1141 for (map = (const struct regcache_map_entry *) regset->regmap;
1142 (count = map->count) != 0;
1145 int regno = map->regno;
1146 int slot_size = map->size;
1148 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1149 slot_size = m_descr->sizeof_register[regno];
1151 if (regno == REGCACHE_MAP_SKIP
1153 && (regnum < regno || regnum >= regno + count)))
1154 offs += count * slot_size;
1156 else if (regnum == -1)
1157 for (; count--; regno++, offs += slot_size)
1159 if (offs + slot_size > size)
1163 raw_collect (regno, (gdb_byte *) out_buf + offs);
1165 out_regcache->raw_supply (regno, in_buf
1166 ? (const gdb_byte *) in_buf + offs
1171 /* Transfer a single register and return. */
1172 offs += (regnum - regno) * slot_size;
1173 if (offs + slot_size > size)
1177 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1179 out_regcache->raw_supply (regnum, in_buf
1180 ? (const gdb_byte *) in_buf + offs
1187 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1188 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1189 If BUF is NULL, set the register(s) to "unavailable" status. */
1192 regcache_supply_regset (const struct regset *regset,
1193 struct regcache *regcache,
1194 int regnum, const void *buf, size_t size)
1196 regcache->supply_regset (regset, regnum, buf, size);
1200 regcache::supply_regset (const struct regset *regset,
1201 int regnum, const void *buf, size_t size)
1203 transfer_regset (regset, this, regnum, buf, NULL, size);
1206 /* Collect register REGNUM from REGCACHE to BUF, using the register
1207 map in REGSET. If REGNUM is -1, do this for all registers in
1211 regcache_collect_regset (const struct regset *regset,
1212 const struct regcache *regcache,
1213 int regnum, void *buf, size_t size)
1215 regcache->collect_regset (regset, regnum, buf, size);
1219 regcache::collect_regset (const struct regset *regset,
1220 int regnum, void *buf, size_t size) const
1222 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1226 /* Special handling for register PC. */
1229 regcache_read_pc (struct regcache *regcache)
1231 struct gdbarch *gdbarch = regcache->arch ();
1235 if (gdbarch_read_pc_p (gdbarch))
1236 pc_val = gdbarch_read_pc (gdbarch, regcache);
1237 /* Else use per-frame method on get_current_frame. */
1238 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1242 if (regcache_cooked_read_unsigned (regcache,
1243 gdbarch_pc_regnum (gdbarch),
1244 &raw_val) == REG_UNAVAILABLE)
1245 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1247 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1250 internal_error (__FILE__, __LINE__,
1251 _("regcache_read_pc: Unable to find PC"));
1256 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1258 struct gdbarch *gdbarch = regcache->arch ();
1260 if (gdbarch_write_pc_p (gdbarch))
1261 gdbarch_write_pc (gdbarch, regcache, pc);
1262 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1263 regcache_cooked_write_unsigned (regcache,
1264 gdbarch_pc_regnum (gdbarch), pc);
1266 internal_error (__FILE__, __LINE__,
1267 _("regcache_write_pc: Unable to update PC"));
1269 /* Writing the PC (for instance, from "load") invalidates the
1271 reinit_frame_cache ();
1275 regcache::num_raw_registers () const
1277 return gdbarch_num_regs (arch ());
1281 regcache::debug_print_register (const char *func, int regno)
1283 struct gdbarch *gdbarch = arch ();
1285 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1286 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1287 && gdbarch_register_name (gdbarch, regno) != NULL
1288 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1289 fprintf_unfiltered (gdb_stdlog, "(%s)",
1290 gdbarch_register_name (gdbarch, regno));
1292 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1293 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1295 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1296 int size = register_size (gdbarch, regno);
1297 gdb_byte *buf = register_buffer (regno);
1299 fprintf_unfiltered (gdb_stdlog, " = ");
1300 for (int i = 0; i < size; i++)
1302 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1304 if (size <= sizeof (LONGEST))
1306 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1308 fprintf_unfiltered (gdb_stdlog, " %s %s",
1309 core_addr_to_string_nz (val), plongest (val));
1312 fprintf_unfiltered (gdb_stdlog, "\n");
1316 reg_flush_command (const char *command, int from_tty)
1318 /* Force-flush the register cache. */
1319 registers_changed ();
1321 printf_filtered (_("Register cache flushed.\n"));
1325 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1327 struct gdbarch *gdbarch = m_descr->gdbarch;
1329 int footnote_nr = 0;
1330 int footnote_register_size = 0;
1331 int footnote_register_offset = 0;
1332 int footnote_register_type_name_null = 0;
1333 long register_offset = 0;
1335 gdb_assert (m_descr->nr_cooked_registers
1336 == (gdbarch_num_regs (gdbarch)
1337 + gdbarch_num_pseudo_regs (gdbarch)));
1339 for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1343 fprintf_unfiltered (file, " %-10s", "Name");
1346 const char *p = gdbarch_register_name (gdbarch, regnum);
1350 else if (p[0] == '\0')
1352 fprintf_unfiltered (file, " %-10s", p);
1357 fprintf_unfiltered (file, " %4s", "Nr");
1359 fprintf_unfiltered (file, " %4d", regnum);
1361 /* Relative number. */
1363 fprintf_unfiltered (file, " %4s", "Rel");
1364 else if (regnum < gdbarch_num_regs (gdbarch))
1365 fprintf_unfiltered (file, " %4d", regnum);
1367 fprintf_unfiltered (file, " %4d",
1368 (regnum - gdbarch_num_regs (gdbarch)));
1372 fprintf_unfiltered (file, " %6s ", "Offset");
1375 fprintf_unfiltered (file, " %6ld",
1376 m_descr->register_offset[regnum]);
1377 if (register_offset != m_descr->register_offset[regnum]
1379 && (m_descr->register_offset[regnum]
1380 != (m_descr->register_offset[regnum - 1]
1381 + m_descr->sizeof_register[regnum - 1])))
1384 if (!footnote_register_offset)
1385 footnote_register_offset = ++footnote_nr;
1386 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1389 fprintf_unfiltered (file, " ");
1390 register_offset = (m_descr->register_offset[regnum]
1391 + m_descr->sizeof_register[regnum]);
1396 fprintf_unfiltered (file, " %5s ", "Size");
1398 fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1403 std::string name_holder;
1409 static const char blt[] = "builtin_type";
1411 t = TYPE_NAME (register_type (arch (), regnum));
1414 if (!footnote_register_type_name_null)
1415 footnote_register_type_name_null = ++footnote_nr;
1416 name_holder = string_printf ("*%d",
1417 footnote_register_type_name_null);
1418 t = name_holder.c_str ();
1420 /* Chop a leading builtin_type. */
1421 if (startswith (t, blt))
1424 fprintf_unfiltered (file, " %-15s", t);
1427 /* Leading space always present. */
1428 fprintf_unfiltered (file, " ");
1431 if (what_to_dump == regcache_dump_raw)
1434 fprintf_unfiltered (file, "Raw value");
1435 else if (regnum >= num_raw_registers ())
1436 fprintf_unfiltered (file, "<cooked>");
1437 else if (get_register_status (regnum) == REG_UNKNOWN)
1438 fprintf_unfiltered (file, "<invalid>");
1439 else if (get_register_status (regnum) == REG_UNAVAILABLE)
1440 fprintf_unfiltered (file, "<unavailable>");
1443 raw_update (regnum);
1444 print_hex_chars (file, register_buffer (regnum),
1445 m_descr->sizeof_register[regnum],
1446 gdbarch_byte_order (gdbarch), true);
1450 /* Value, cooked. */
1451 if (what_to_dump == regcache_dump_cooked)
1454 fprintf_unfiltered (file, "Cooked value");
1457 const gdb_byte *buf = NULL;
1458 enum register_status status;
1459 struct value *value = NULL;
1461 if (regnum < num_raw_registers ())
1463 raw_update (regnum);
1464 status = get_register_status (regnum);
1465 buf = register_buffer (regnum);
1469 value = cooked_read_value (regnum);
1471 if (!value_optimized_out (value)
1472 && value_entirely_available (value))
1475 buf = value_contents_all (value);
1478 status = REG_UNAVAILABLE;
1481 if (status == REG_UNKNOWN)
1482 fprintf_unfiltered (file, "<invalid>");
1483 else if (status == REG_UNAVAILABLE)
1484 fprintf_unfiltered (file, "<unavailable>");
1486 print_hex_chars (file, buf,
1487 m_descr->sizeof_register[regnum],
1488 gdbarch_byte_order (gdbarch), true);
1492 release_value (value);
1498 /* Group members. */
1499 if (what_to_dump == regcache_dump_groups)
1502 fprintf_unfiltered (file, "Groups");
1505 const char *sep = "";
1506 struct reggroup *group;
1508 for (group = reggroup_next (gdbarch, NULL);
1510 group = reggroup_next (gdbarch, group))
1512 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1514 fprintf_unfiltered (file,
1515 "%s%s", sep, reggroup_name (group));
1522 /* Remote packet configuration. */
1523 if (what_to_dump == regcache_dump_remote)
1527 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1529 else if (regnum < num_raw_registers ())
1533 if (remote_register_number_and_offset (arch (), regnum,
1535 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1539 fprintf_unfiltered (file, "\n");
1542 if (footnote_register_size)
1543 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1544 footnote_register_size);
1545 if (footnote_register_offset)
1546 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1547 footnote_register_offset);
1548 if (footnote_register_type_name_null)
1549 fprintf_unfiltered (file,
1550 "*%d: Register type's name NULL.\n",
1551 footnote_register_type_name_null);
1555 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
1557 /* Where to send output. */
1565 if (!file.open (args, "w"))
1566 perror_with_name (_("maintenance print architecture"));
1570 if (target_has_registers)
1571 get_current_regcache ()->dump (out, what_to_dump);
1574 /* For the benefit of "maint print registers" & co when
1575 debugging an executable, allow dumping a regcache even when
1576 there is no thread selected / no registers. */
1577 regcache dummy_regs (target_gdbarch ());
1578 dummy_regs.dump (out, what_to_dump);
1583 maintenance_print_registers (const char *args, int from_tty)
1585 regcache_print (args, regcache_dump_none);
1589 maintenance_print_raw_registers (const char *args, int from_tty)
1591 regcache_print (args, regcache_dump_raw);
1595 maintenance_print_cooked_registers (const char *args, int from_tty)
1597 regcache_print (args, regcache_dump_cooked);
1601 maintenance_print_register_groups (const char *args, int from_tty)
1603 regcache_print (args, regcache_dump_groups);
1607 maintenance_print_remote_registers (const char *args, int from_tty)
1609 regcache_print (args, regcache_dump_remote);
1613 #include "selftest.h"
1614 #include "selftest-arch.h"
1615 #include "gdbthread.h"
1617 namespace selftests {
1619 class regcache_access : public regcache
1623 /* Return the number of elements in current_regcache. */
1626 current_regcache_size ()
1628 return std::distance (regcache::current_regcache.begin (),
1629 regcache::current_regcache.end ());
1634 current_regcache_test (void)
1636 /* It is empty at the start. */
1637 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1639 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1641 /* Get regcache from ptid1, a new regcache is added to
1642 current_regcache. */
1643 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1647 SELF_CHECK (regcache != NULL);
1648 SELF_CHECK (regcache->ptid () == ptid1);
1649 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1651 /* Get regcache from ptid2, a new regcache is added to
1652 current_regcache. */
1653 regcache = get_thread_arch_aspace_regcache (ptid2,
1656 SELF_CHECK (regcache != NULL);
1657 SELF_CHECK (regcache->ptid () == ptid2);
1658 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1660 /* Get regcache from ptid3, a new regcache is added to
1661 current_regcache. */
1662 regcache = get_thread_arch_aspace_regcache (ptid3,
1665 SELF_CHECK (regcache != NULL);
1666 SELF_CHECK (regcache->ptid () == ptid3);
1667 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1669 /* Get regcache from ptid2 again, nothing is added to
1670 current_regcache. */
1671 regcache = get_thread_arch_aspace_regcache (ptid2,
1674 SELF_CHECK (regcache != NULL);
1675 SELF_CHECK (regcache->ptid () == ptid2);
1676 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1678 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1679 current_regcache. */
1680 registers_changed_ptid (ptid2);
1681 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1684 static void test_target_fetch_registers (target_ops *self, regcache *regs,
1686 static void test_target_store_registers (target_ops *self, regcache *regs,
1688 static enum target_xfer_status
1689 test_target_xfer_partial (struct target_ops *ops,
1690 enum target_object object,
1691 const char *annex, gdb_byte *readbuf,
1692 const gdb_byte *writebuf,
1693 ULONGEST offset, ULONGEST len,
1694 ULONGEST *xfered_len);
1696 class target_ops_no_register : public test_target_ops
1699 target_ops_no_register ()
1700 : test_target_ops {}
1702 to_fetch_registers = test_target_fetch_registers;
1703 to_store_registers = test_target_store_registers;
1704 to_xfer_partial = test_target_xfer_partial;
1711 fetch_registers_called = 0;
1712 store_registers_called = 0;
1713 xfer_partial_called = 0;
1716 unsigned int fetch_registers_called = 0;
1717 unsigned int store_registers_called = 0;
1718 unsigned int xfer_partial_called = 0;
1722 test_target_fetch_registers (target_ops *self, regcache *regs, int regno)
1724 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1726 /* Mark register available. */
1727 regs->raw_supply_zeroed (regno);
1728 ops->fetch_registers_called++;
1732 test_target_store_registers (target_ops *self, regcache *regs, int regno)
1734 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1736 ops->store_registers_called++;
1739 static enum target_xfer_status
1740 test_target_xfer_partial (struct target_ops *self, enum target_object object,
1741 const char *annex, gdb_byte *readbuf,
1742 const gdb_byte *writebuf,
1743 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1745 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1747 ops->xfer_partial_called++;
1750 return TARGET_XFER_OK;
1753 class readwrite_regcache : public regcache
1756 readwrite_regcache (struct gdbarch *gdbarch)
1757 : regcache (gdbarch, nullptr, false)
1761 /* Test regcache::cooked_read gets registers from raw registers and
1762 memory instead of target to_{fetch,store}_registers. */
1765 cooked_read_test (struct gdbarch *gdbarch)
1767 /* Error out if debugging something, because we're going to push the
1768 test target, which would pop any existing target. */
1769 if (current_target.to_stratum >= process_stratum)
1770 error (_("target already pushed"));
1772 /* Create a mock environment. An inferior with a thread, with a
1773 process_stratum target pushed. */
1775 target_ops_no_register mock_target;
1776 ptid_t mock_ptid (1, 1);
1777 inferior mock_inferior (mock_ptid.pid ());
1778 address_space mock_aspace {};
1779 mock_inferior.gdbarch = gdbarch;
1780 mock_inferior.aspace = &mock_aspace;
1781 thread_info mock_thread (&mock_inferior, mock_ptid);
1783 scoped_restore restore_thread_list
1784 = make_scoped_restore (&thread_list, &mock_thread);
1786 /* Add the mock inferior to the inferior list so that look ups by
1787 target+ptid can find it. */
1788 scoped_restore restore_inferior_list
1789 = make_scoped_restore (&inferior_list);
1790 inferior_list = &mock_inferior;
1792 /* Switch to the mock inferior. */
1793 scoped_restore_current_inferior restore_current_inferior;
1794 set_current_inferior (&mock_inferior);
1796 /* Push the process_stratum target so we can mock accessing
1798 push_target (&mock_target);
1800 /* Pop it again on exit (return/exception). */
1805 pop_all_targets_at_and_above (process_stratum);
1809 /* Switch to the mock thread. */
1810 scoped_restore restore_inferior_ptid
1811 = make_scoped_restore (&inferior_ptid, mock_ptid);
1813 /* Test that read one raw register from regcache_no_target will go
1814 to the target layer. */
1817 /* Find a raw register which size isn't zero. */
1818 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1820 if (register_size (gdbarch, regnum) != 0)
1824 readwrite_regcache readwrite (gdbarch);
1825 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1827 readwrite.raw_read (regnum, buf.data ());
1829 /* raw_read calls target_fetch_registers. */
1830 SELF_CHECK (mock_target.fetch_registers_called > 0);
1831 mock_target.reset ();
1833 /* Mark all raw registers valid, so the following raw registers
1834 accesses won't go to target. */
1835 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1836 readwrite.raw_update (i);
1838 mock_target.reset ();
1839 /* Then, read all raw and pseudo registers, and don't expect calling
1840 to_{fetch,store}_registers. */
1841 for (int regnum = 0;
1842 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1845 if (register_size (gdbarch, regnum) == 0)
1848 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1850 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1852 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_mt)
1854 /* MT pseudo registers are banked, and different banks are
1855 selected by a raw registers, so GDB needs to write to
1856 that raw register to get different banked pseudo registers.
1857 See mt_select_coprocessor. */
1858 SELF_CHECK (mock_target.fetch_registers_called == 0);
1859 SELF_CHECK (mock_target.store_registers_called == 0);
1862 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1863 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1864 SELF_CHECK (mock_target.xfer_partial_called == 0);
1866 mock_target.reset ();
1869 regcache readonly (regcache::readonly, readwrite);
1871 /* GDB may go to target layer to fetch all registers and memory for
1872 readonly regcache. */
1873 mock_target.reset ();
1875 for (int regnum = 0;
1876 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1879 if (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_mt)
1881 /* Trigger an internal error otherwise. */
1885 if (register_size (gdbarch, regnum) == 0)
1888 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1889 enum register_status status = readonly.cooked_read (regnum,
1892 if (regnum < gdbarch_num_regs (gdbarch))
1894 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1896 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1897 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1898 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1899 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1900 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1901 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1902 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score)
1904 /* Raw registers. If raw registers are not in save_reggroup,
1905 their status are unknown. */
1906 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1907 SELF_CHECK (status == REG_VALID);
1909 SELF_CHECK (status == REG_UNKNOWN);
1912 SELF_CHECK (status == REG_VALID);
1916 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1917 SELF_CHECK (status == REG_VALID);
1920 /* If pseudo registers are not in save_reggroup, some of
1921 them can be computed from saved raw registers, but some
1922 of them are unknown. */
1923 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1925 if (bfd_arch == bfd_arch_frv
1926 || bfd_arch == bfd_arch_m32c
1927 || bfd_arch == bfd_arch_mep
1928 || bfd_arch == bfd_arch_sh)
1929 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1930 else if (bfd_arch == bfd_arch_mips
1931 || bfd_arch == bfd_arch_h8300)
1932 SELF_CHECK (status == REG_UNKNOWN);
1934 SELF_CHECK (status == REG_VALID);
1938 SELF_CHECK (mock_target.fetch_registers_called == 0);
1939 SELF_CHECK (mock_target.store_registers_called == 0);
1940 SELF_CHECK (mock_target.xfer_partial_called == 0);
1942 mock_target.reset ();
1946 } // namespace selftests
1947 #endif /* GDB_SELF_TEST */
1950 _initialize_regcache (void)
1952 regcache_descr_handle
1953 = gdbarch_data_register_post_init (init_regcache_descr);
1955 observer_attach_target_changed (regcache_observer_target_changed);
1956 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
1958 add_com ("flushregs", class_maintenance, reg_flush_command,
1959 _("Force gdb to flush its register cache (maintainer command)"));
1961 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1962 _("Print the internal register configuration.\n"
1963 "Takes an optional file parameter."), &maintenanceprintlist);
1964 add_cmd ("raw-registers", class_maintenance,
1965 maintenance_print_raw_registers,
1966 _("Print the internal register configuration "
1967 "including raw values.\n"
1968 "Takes an optional file parameter."), &maintenanceprintlist);
1969 add_cmd ("cooked-registers", class_maintenance,
1970 maintenance_print_cooked_registers,
1971 _("Print the internal register configuration "
1972 "including cooked values.\n"
1973 "Takes an optional file parameter."), &maintenanceprintlist);
1974 add_cmd ("register-groups", class_maintenance,
1975 maintenance_print_register_groups,
1976 _("Print the internal register configuration "
1977 "including each register's group.\n"
1978 "Takes an optional file parameter."),
1979 &maintenanceprintlist);
1980 add_cmd ("remote-registers", class_maintenance,
1981 maintenance_print_remote_registers, _("\
1982 Print the internal register configuration including each register's\n\
1983 remote register number and buffer offset in the g/G packets.\n\
1984 Takes an optional file parameter."),
1985 &maintenanceprintlist);
1988 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1990 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1991 selftests::cooked_read_test);