1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 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 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
185 : m_has_pseudo (has_pseudo)
187 gdb_assert (gdbarch != NULL);
188 m_descr = regcache_descr (gdbarch);
192 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
193 m_register_status = XCNEWVEC (signed char,
194 m_descr->nr_cooked_registers);
198 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
199 m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch));
203 regcache::regcache (gdbarch *gdbarch, const address_space *aspace_,
205 /* The register buffers. A read-only register cache can hold the
206 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a
207 read/write register cache can only hold [0 .. gdbarch_num_regs). */
208 : readable_regcache (gdbarch, readonly_p_),
209 m_aspace (aspace_), m_readonly_p (readonly_p_)
211 m_ptid = minus_one_ptid;
214 static enum register_status
215 do_cooked_read (void *src, int regnum, gdb_byte *buf)
217 struct regcache *regcache = (struct regcache *) src;
219 return regcache_cooked_read (regcache, regnum, buf);
222 regcache::regcache (readonly_t, const regcache &src)
223 : regcache (src.arch (), nullptr, true)
225 gdb_assert (!src.m_readonly_p);
226 save (do_cooked_read, (void *) &src);
229 readonly_detached_regcache::readonly_detached_regcache (const regcache &src)
230 : readonly_detached_regcache (src.arch (), do_cooked_read, (void *) &src)
235 reg_buffer::arch () const
237 return m_descr->gdbarch;
240 /* See regcache.h. */
243 regcache_get_ptid (const struct regcache *regcache)
245 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
247 return regcache->ptid ();
250 /* Cleanup class for invalidating a register. */
252 class regcache_invalidator
256 regcache_invalidator (struct regcache *regcache, int regnum)
257 : m_regcache (regcache),
262 ~regcache_invalidator ()
264 if (m_regcache != nullptr)
265 regcache_invalidate (m_regcache, m_regnum);
268 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
272 m_regcache = nullptr;
277 struct regcache *m_regcache;
281 /* Return a pointer to register REGNUM's buffer cache. */
284 reg_buffer::register_buffer (int regnum) const
286 return m_registers + m_descr->register_offset[regnum];
290 reg_buffer::save (regcache_cooked_read_ftype *cooked_read,
293 struct gdbarch *gdbarch = m_descr->gdbarch;
296 /* It should have pseudo registers. */
297 gdb_assert (m_has_pseudo);
298 /* Clear the dest. */
299 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
300 memset (m_register_status, 0, m_descr->nr_cooked_registers);
301 /* Copy over any registers (identified by their membership in the
302 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
303 gdbarch_num_pseudo_regs) range is checked since some architectures need
304 to save/restore `cooked' registers that live in memory. */
305 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
307 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
309 gdb_byte *dst_buf = register_buffer (regnum);
310 enum register_status status = cooked_read (src, regnum, dst_buf);
312 gdb_assert (status != REG_UNKNOWN);
314 if (status != REG_VALID)
315 memset (dst_buf, 0, register_size (gdbarch, regnum));
317 m_register_status[regnum] = status;
323 regcache::restore (readonly_detached_regcache *src)
325 struct gdbarch *gdbarch = m_descr->gdbarch;
328 gdb_assert (src != NULL);
329 gdb_assert (!m_readonly_p);
330 gdb_assert (src->m_has_pseudo);
332 gdb_assert (gdbarch == src->arch ());
334 /* Copy over any registers, being careful to only restore those that
335 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
336 + gdbarch_num_pseudo_regs) range is checked since some architectures need
337 to save/restore `cooked' registers that live in memory. */
338 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
340 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
342 if (src->m_register_status[regnum] == REG_VALID)
343 cooked_write (regnum, src->register_buffer (regnum));
349 regcache_register_status (const struct regcache *regcache, int regnum)
351 gdb_assert (regcache != NULL);
352 return regcache->get_register_status (regnum);
356 regcache::get_register_status (int regnum) const
358 gdb_assert (regnum >= 0);
360 gdb_assert (regnum < m_descr->nr_cooked_registers);
362 gdb_assert (regnum < num_raw_registers ());
364 return (enum register_status) m_register_status[regnum];
368 regcache_invalidate (struct regcache *regcache, int regnum)
370 gdb_assert (regcache != NULL);
371 regcache->invalidate (regnum);
375 regcache::invalidate (int regnum)
377 gdb_assert (!m_readonly_p);
378 assert_regnum (regnum);
379 m_register_status[regnum] = REG_UNKNOWN;
383 reg_buffer::assert_regnum (int regnum) const
385 gdb_assert (regnum >= 0);
387 gdb_assert (regnum < m_descr->nr_cooked_registers);
389 gdb_assert (regnum < gdbarch_num_regs (arch ()));
392 /* Global structure containing the current regcache. */
394 /* NOTE: this is a write-through cache. There is no "dirty" bit for
395 recording if the register values have been changed (eg. by the
396 user). Therefore all registers must be written back to the
397 target when appropriate. */
398 std::forward_list<regcache *> regcache::current_regcache;
401 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
402 struct address_space *aspace)
404 for (const auto ®cache : regcache::current_regcache)
405 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
408 regcache *new_regcache = new regcache (gdbarch, aspace, false);
410 regcache::current_regcache.push_front (new_regcache);
411 new_regcache->set_ptid (ptid);
417 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
419 address_space *aspace = target_thread_address_space (ptid);
421 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
424 static ptid_t current_thread_ptid;
425 static struct gdbarch *current_thread_arch;
428 get_thread_regcache (ptid_t ptid)
430 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
432 current_thread_ptid = ptid;
433 current_thread_arch = target_thread_architecture (ptid);
436 return get_thread_arch_regcache (ptid, current_thread_arch);
440 get_current_regcache (void)
442 return get_thread_regcache (inferior_ptid);
445 /* See common/common-regcache.h. */
448 get_thread_regcache_for_ptid (ptid_t ptid)
450 return get_thread_regcache (ptid);
453 /* Observer for the target_changed event. */
456 regcache_observer_target_changed (struct target_ops *target)
458 registers_changed ();
461 /* Update global variables old ptids to hold NEW_PTID if they were
464 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
466 for (auto ®cache : regcache::current_regcache)
468 if (ptid_equal (regcache->ptid (), old_ptid))
469 regcache->set_ptid (new_ptid);
473 /* Low level examining and depositing of registers.
475 The caller is responsible for making sure that the inferior is
476 stopped before calling the fetching routines, or it will get
477 garbage. (a change from GDB version 3, in which the caller got the
478 value from the last stop). */
480 /* REGISTERS_CHANGED ()
482 Indicate that registers may have changed, so invalidate the cache. */
485 registers_changed_ptid (ptid_t ptid)
487 for (auto oit = regcache::current_regcache.before_begin (),
488 it = std::next (oit);
489 it != regcache::current_regcache.end ();
492 if (ptid_match ((*it)->ptid (), ptid))
495 it = regcache::current_regcache.erase_after (oit);
501 if (ptid_match (current_thread_ptid, ptid))
503 current_thread_ptid = null_ptid;
504 current_thread_arch = NULL;
507 if (ptid_match (inferior_ptid, ptid))
509 /* We just deleted the regcache of the current thread. Need to
510 forget about any frames we have cached, too. */
511 reinit_frame_cache ();
516 registers_changed (void)
518 registers_changed_ptid (minus_one_ptid);
520 /* Force cleanup of any alloca areas if using C alloca instead of
521 a builtin alloca. This particular call is used to clean up
522 areas allocated by low level target code which may build up
523 during lengthy interactions between gdb and the target before
524 gdb gives control to the user (ie watchpoints). */
529 regcache_raw_update (struct regcache *regcache, int regnum)
531 gdb_assert (regcache != NULL);
533 regcache->raw_update (regnum);
537 regcache::raw_update (int regnum)
539 assert_regnum (regnum);
541 /* Make certain that the register cache is up-to-date with respect
542 to the current thread. This switching shouldn't be necessary
543 only there is still only one target side register cache. Sigh!
544 On the bright side, at least there is a regcache object. */
546 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
548 target_fetch_registers (this, regnum);
550 /* A number of targets can't access the whole set of raw
551 registers (because the debug API provides no means to get at
553 if (m_register_status[regnum] == REG_UNKNOWN)
554 m_register_status[regnum] = REG_UNAVAILABLE;
559 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
561 return regcache->raw_read (regnum, buf);
565 readable_regcache::raw_read (int regnum, gdb_byte *buf)
567 gdb_assert (buf != NULL);
570 if (m_register_status[regnum] != REG_VALID)
571 memset (buf, 0, m_descr->sizeof_register[regnum]);
573 memcpy (buf, register_buffer (regnum),
574 m_descr->sizeof_register[regnum]);
576 return (enum register_status) m_register_status[regnum];
580 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
582 gdb_assert (regcache != NULL);
583 return regcache->raw_read (regnum, val);
586 template<typename T, typename>
588 readable_regcache::raw_read (int regnum, T *val)
591 enum register_status status;
593 assert_regnum (regnum);
594 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
595 status = raw_read (regnum, buf);
596 if (status == REG_VALID)
597 *val = extract_integer<T> (buf,
598 m_descr->sizeof_register[regnum],
599 gdbarch_byte_order (m_descr->gdbarch));
606 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
609 gdb_assert (regcache != NULL);
610 return regcache->raw_read (regnum, val);
614 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
616 gdb_assert (regcache != NULL);
617 regcache->raw_write (regnum, val);
620 template<typename T, typename>
622 regcache::raw_write (int regnum, T val)
626 assert_regnum (regnum);
627 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
628 store_integer (buf, m_descr->sizeof_register[regnum],
629 gdbarch_byte_order (m_descr->gdbarch), val);
630 raw_write (regnum, buf);
634 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
637 gdb_assert (regcache != NULL);
638 regcache->raw_write (regnum, val);
642 regcache_raw_get_signed (struct regcache *regcache, int regnum)
645 enum register_status status;
647 status = regcache_raw_read_signed (regcache, regnum, &value);
648 if (status == REG_UNAVAILABLE)
649 throw_error (NOT_AVAILABLE_ERROR,
650 _("Register %d is not available"), regnum);
655 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
657 return regcache->cooked_read (regnum, buf);
661 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
663 gdb_assert (regnum >= 0);
664 gdb_assert (regnum < m_descr->nr_cooked_registers);
665 if (regnum < num_raw_registers ())
666 return raw_read (regnum, buf);
667 else if (m_has_pseudo
668 && m_register_status[regnum] != REG_UNKNOWN)
670 if (m_register_status[regnum] == REG_VALID)
671 memcpy (buf, register_buffer (regnum),
672 m_descr->sizeof_register[regnum]);
674 memset (buf, 0, m_descr->sizeof_register[regnum]);
676 return (enum register_status) m_register_status[regnum];
678 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
680 struct value *mark, *computed;
681 enum register_status result = REG_VALID;
683 mark = value_mark ();
685 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
687 if (value_entirely_available (computed))
688 memcpy (buf, value_contents_raw (computed),
689 m_descr->sizeof_register[regnum]);
692 memset (buf, 0, m_descr->sizeof_register[regnum]);
693 result = REG_UNAVAILABLE;
696 value_free_to_mark (mark);
701 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
706 regcache_cooked_read_value (struct regcache *regcache, int regnum)
708 return regcache->cooked_read_value (regnum);
712 readable_regcache::cooked_read_value (int regnum)
714 gdb_assert (regnum >= 0);
715 gdb_assert (regnum < m_descr->nr_cooked_registers);
717 if (regnum < num_raw_registers ()
718 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
719 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
721 struct value *result;
723 result = allocate_value (register_type (m_descr->gdbarch, regnum));
724 VALUE_LVAL (result) = lval_register;
725 VALUE_REGNUM (result) = regnum;
727 /* It is more efficient in general to do this delegation in this
728 direction than in the other one, even though the value-based
730 if (cooked_read (regnum,
731 value_contents_raw (result)) == REG_UNAVAILABLE)
732 mark_value_bytes_unavailable (result, 0,
733 TYPE_LENGTH (value_type (result)));
738 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
743 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
746 gdb_assert (regcache != NULL);
747 return regcache->cooked_read (regnum, val);
750 template<typename T, typename>
752 readable_regcache::cooked_read (int regnum, T *val)
754 enum register_status status;
757 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
758 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
759 status = cooked_read (regnum, buf);
760 if (status == REG_VALID)
761 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
762 gdbarch_byte_order (m_descr->gdbarch));
769 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
772 gdb_assert (regcache != NULL);
773 return regcache->cooked_read (regnum, val);
777 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
780 gdb_assert (regcache != NULL);
781 regcache->cooked_write (regnum, val);
784 template<typename T, typename>
786 regcache::cooked_write (int regnum, T val)
790 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
791 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
792 store_integer (buf, m_descr->sizeof_register[regnum],
793 gdbarch_byte_order (m_descr->gdbarch), val);
794 cooked_write (regnum, buf);
798 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
801 gdb_assert (regcache != NULL);
802 regcache->cooked_write (regnum, val);
805 /* See regcache.h. */
808 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
811 regcache->raw_set_cached_value (regnum, buf);
815 regcache::raw_set_cached_value (int regnum, const gdb_byte *buf)
817 memcpy (register_buffer (regnum), buf,
818 m_descr->sizeof_register[regnum]);
819 m_register_status[regnum] = REG_VALID;
823 regcache_raw_write (struct regcache *regcache, int regnum,
826 gdb_assert (regcache != NULL && buf != NULL);
827 regcache->raw_write (regnum, buf);
831 regcache::raw_write (int regnum, const gdb_byte *buf)
834 gdb_assert (buf != NULL);
835 assert_regnum (regnum);
836 gdb_assert (!m_readonly_p);
838 /* On the sparc, writing %g0 is a no-op, so we don't even want to
839 change the registers array if something writes to this register. */
840 if (gdbarch_cannot_store_register (arch (), regnum))
843 /* If we have a valid copy of the register, and new value == old
844 value, then don't bother doing the actual store. */
845 if (get_register_status (regnum) == REG_VALID
846 && (memcmp (register_buffer (regnum), buf,
847 m_descr->sizeof_register[regnum]) == 0))
850 target_prepare_to_store (this);
851 raw_set_cached_value (regnum, buf);
853 /* Invalidate the register after it is written, in case of a
855 regcache_invalidator invalidator (this, regnum);
857 target_store_registers (this, regnum);
859 /* The target did not throw an error so we can discard invalidating
861 invalidator.release ();
865 regcache_cooked_write (struct regcache *regcache, int regnum,
868 regcache->cooked_write (regnum, buf);
872 regcache::cooked_write (int regnum, const gdb_byte *buf)
874 gdb_assert (regnum >= 0);
875 gdb_assert (regnum < m_descr->nr_cooked_registers);
876 if (regnum < num_raw_registers ())
877 raw_write (regnum, buf);
879 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
883 /* Perform a partial register transfer using a read, modify, write
886 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
888 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
892 readable_regcache::read_part (int regnum, int offset, int len, void *in,
895 struct gdbarch *gdbarch = arch ();
896 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
898 gdb_assert (in != NULL);
899 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
900 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
901 /* Something to do? */
902 if (offset + len == 0)
904 /* Read (when needed) ... */
905 enum register_status status;
908 status = raw_read (regnum, reg);
910 status = cooked_read (regnum, reg);
911 if (status != REG_VALID)
915 memcpy (in, reg + offset, len);
921 regcache::write_part (int regnum, int offset, int len,
922 const void *out, bool is_raw)
924 struct gdbarch *gdbarch = arch ();
925 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
927 gdb_assert (out != NULL);
928 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
929 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
930 /* Something to do? */
931 if (offset + len == 0)
933 /* Read (when needed) ... */
935 || offset + len < m_descr->sizeof_register[regnum])
937 enum register_status status;
940 status = raw_read (regnum, reg);
942 status = cooked_read (regnum, reg);
943 if (status != REG_VALID)
947 memcpy (reg + offset, out, len);
948 /* ... write (when needed). */
950 raw_write (regnum, reg);
952 cooked_write (regnum, reg);
958 regcache_raw_read_part (struct regcache *regcache, int regnum,
959 int offset, int len, gdb_byte *buf)
961 return regcache->raw_read_part (regnum, offset, len, buf);
965 readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
967 assert_regnum (regnum);
968 return read_part (regnum, offset, len, buf, true);
972 regcache_raw_write_part (struct regcache *regcache, int regnum,
973 int offset, int len, const gdb_byte *buf)
975 regcache->raw_write_part (regnum, offset, len, buf);
979 regcache::raw_write_part (int regnum, int offset, int len,
982 assert_regnum (regnum);
983 write_part (regnum, offset, len, buf, true);
987 regcache_cooked_read_part (struct regcache *regcache, int regnum,
988 int offset, int len, gdb_byte *buf)
990 return regcache->cooked_read_part (regnum, offset, len, buf);
995 readable_regcache::cooked_read_part (int regnum, int offset, int len,
998 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
999 return read_part (regnum, offset, len, buf, false);
1003 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1004 int offset, int len, const gdb_byte *buf)
1006 regcache->cooked_write_part (regnum, offset, len, buf);
1010 regcache::cooked_write_part (int regnum, int offset, int len,
1011 const gdb_byte *buf)
1013 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1014 write_part (regnum, offset, len, buf, false);
1017 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1020 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1022 gdb_assert (regcache != NULL);
1023 regcache->raw_supply (regnum, buf);
1027 regcache::raw_supply (int regnum, const void *buf)
1032 assert_regnum (regnum);
1033 gdb_assert (!m_readonly_p);
1035 regbuf = register_buffer (regnum);
1036 size = m_descr->sizeof_register[regnum];
1040 memcpy (regbuf, buf, size);
1041 m_register_status[regnum] = REG_VALID;
1045 /* This memset not strictly necessary, but better than garbage
1046 in case the register value manages to escape somewhere (due
1047 to a bug, no less). */
1048 memset (regbuf, 0, size);
1049 m_register_status[regnum] = REG_UNAVAILABLE;
1053 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1054 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1055 the register size is greater than ADDR_LEN, then the integer will be sign or
1056 zero extended. If the register size is smaller than the integer, then the
1057 most significant bytes of the integer will be truncated. */
1060 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1063 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1067 assert_regnum (regnum);
1068 gdb_assert (!m_readonly_p);
1070 regbuf = register_buffer (regnum);
1071 regsize = m_descr->sizeof_register[regnum];
1073 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1075 m_register_status[regnum] = REG_VALID;
1078 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1079 as calling raw_supply with NULL (which will set the state to
1083 regcache::raw_supply_zeroed (int regnum)
1088 assert_regnum (regnum);
1089 gdb_assert (!m_readonly_p);
1091 regbuf = register_buffer (regnum);
1092 size = m_descr->sizeof_register[regnum];
1094 memset (regbuf, 0, size);
1095 m_register_status[regnum] = REG_VALID;
1098 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1101 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1103 gdb_assert (regcache != NULL && buf != NULL);
1104 regcache->raw_collect (regnum, buf);
1108 regcache::raw_collect (int regnum, void *buf) const
1113 gdb_assert (buf != NULL);
1114 assert_regnum (regnum);
1116 regbuf = register_buffer (regnum);
1117 size = m_descr->sizeof_register[regnum];
1118 memcpy (buf, regbuf, size);
1121 /* Transfer a single or all registers belonging to a certain register
1122 set to or from a buffer. This is the main worker function for
1123 regcache_supply_regset and regcache_collect_regset. */
1125 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1126 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1127 If ADDR_LEN is greater than the register size, then the integer will be sign
1128 or zero extended. If ADDR_LEN is smaller than the register size, then the
1129 most significant bytes of the integer will be truncated. */
1132 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1133 bool is_signed) const
1135 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1136 const gdb_byte *regbuf;
1139 assert_regnum (regnum);
1141 regbuf = register_buffer (regnum);
1142 regsize = m_descr->sizeof_register[regnum];
1144 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1149 regcache::transfer_regset (const struct regset *regset,
1150 struct regcache *out_regcache,
1151 int regnum, const void *in_buf,
1152 void *out_buf, size_t size) const
1154 const struct regcache_map_entry *map;
1155 int offs = 0, count;
1157 for (map = (const struct regcache_map_entry *) regset->regmap;
1158 (count = map->count) != 0;
1161 int regno = map->regno;
1162 int slot_size = map->size;
1164 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1165 slot_size = m_descr->sizeof_register[regno];
1167 if (regno == REGCACHE_MAP_SKIP
1169 && (regnum < regno || regnum >= regno + count)))
1170 offs += count * slot_size;
1172 else if (regnum == -1)
1173 for (; count--; regno++, offs += slot_size)
1175 if (offs + slot_size > size)
1179 raw_collect (regno, (gdb_byte *) out_buf + offs);
1181 out_regcache->raw_supply (regno, in_buf
1182 ? (const gdb_byte *) in_buf + offs
1187 /* Transfer a single register and return. */
1188 offs += (regnum - regno) * slot_size;
1189 if (offs + slot_size > size)
1193 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1195 out_regcache->raw_supply (regnum, in_buf
1196 ? (const gdb_byte *) in_buf + offs
1203 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1204 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1205 If BUF is NULL, set the register(s) to "unavailable" status. */
1208 regcache_supply_regset (const struct regset *regset,
1209 struct regcache *regcache,
1210 int regnum, const void *buf, size_t size)
1212 regcache->supply_regset (regset, regnum, buf, size);
1216 regcache::supply_regset (const struct regset *regset,
1217 int regnum, const void *buf, size_t size)
1219 transfer_regset (regset, this, regnum, buf, NULL, size);
1222 /* Collect register REGNUM from REGCACHE to BUF, using the register
1223 map in REGSET. If REGNUM is -1, do this for all registers in
1227 regcache_collect_regset (const struct regset *regset,
1228 const struct regcache *regcache,
1229 int regnum, void *buf, size_t size)
1231 regcache->collect_regset (regset, regnum, buf, size);
1235 regcache::collect_regset (const struct regset *regset,
1236 int regnum, void *buf, size_t size) const
1238 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1242 /* Special handling for register PC. */
1245 regcache_read_pc (struct regcache *regcache)
1247 struct gdbarch *gdbarch = regcache->arch ();
1251 if (gdbarch_read_pc_p (gdbarch))
1252 pc_val = gdbarch_read_pc (gdbarch, regcache);
1253 /* Else use per-frame method on get_current_frame. */
1254 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1258 if (regcache_cooked_read_unsigned (regcache,
1259 gdbarch_pc_regnum (gdbarch),
1260 &raw_val) == REG_UNAVAILABLE)
1261 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1263 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1266 internal_error (__FILE__, __LINE__,
1267 _("regcache_read_pc: Unable to find PC"));
1272 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1274 struct gdbarch *gdbarch = regcache->arch ();
1276 if (gdbarch_write_pc_p (gdbarch))
1277 gdbarch_write_pc (gdbarch, regcache, pc);
1278 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1279 regcache_cooked_write_unsigned (regcache,
1280 gdbarch_pc_regnum (gdbarch), pc);
1282 internal_error (__FILE__, __LINE__,
1283 _("regcache_write_pc: Unable to update PC"));
1285 /* Writing the PC (for instance, from "load") invalidates the
1287 reinit_frame_cache ();
1291 reg_buffer::num_raw_registers () const
1293 return gdbarch_num_regs (arch ());
1297 regcache::debug_print_register (const char *func, int regno)
1299 struct gdbarch *gdbarch = arch ();
1301 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1302 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1303 && gdbarch_register_name (gdbarch, regno) != NULL
1304 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1305 fprintf_unfiltered (gdb_stdlog, "(%s)",
1306 gdbarch_register_name (gdbarch, regno));
1308 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1309 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1311 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1312 int size = register_size (gdbarch, regno);
1313 gdb_byte *buf = register_buffer (regno);
1315 fprintf_unfiltered (gdb_stdlog, " = ");
1316 for (int i = 0; i < size; i++)
1318 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1320 if (size <= sizeof (LONGEST))
1322 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1324 fprintf_unfiltered (gdb_stdlog, " %s %s",
1325 core_addr_to_string_nz (val), plongest (val));
1328 fprintf_unfiltered (gdb_stdlog, "\n");
1332 reg_flush_command (const char *command, int from_tty)
1334 /* Force-flush the register cache. */
1335 registers_changed ();
1337 printf_filtered (_("Register cache flushed.\n"));
1341 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1343 struct gdbarch *gdbarch = m_descr->gdbarch;
1345 int footnote_nr = 0;
1346 int footnote_register_offset = 0;
1347 int footnote_register_type_name_null = 0;
1348 long register_offset = 0;
1350 gdb_assert (m_descr->nr_cooked_registers
1351 == (gdbarch_num_regs (gdbarch)
1352 + gdbarch_num_pseudo_regs (gdbarch)));
1354 for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1358 fprintf_unfiltered (file, " %-10s", "Name");
1361 const char *p = gdbarch_register_name (gdbarch, regnum);
1365 else if (p[0] == '\0')
1367 fprintf_unfiltered (file, " %-10s", p);
1372 fprintf_unfiltered (file, " %4s", "Nr");
1374 fprintf_unfiltered (file, " %4d", regnum);
1376 /* Relative number. */
1378 fprintf_unfiltered (file, " %4s", "Rel");
1379 else if (regnum < gdbarch_num_regs (gdbarch))
1380 fprintf_unfiltered (file, " %4d", regnum);
1382 fprintf_unfiltered (file, " %4d",
1383 (regnum - gdbarch_num_regs (gdbarch)));
1387 fprintf_unfiltered (file, " %6s ", "Offset");
1390 fprintf_unfiltered (file, " %6ld",
1391 m_descr->register_offset[regnum]);
1392 if (register_offset != m_descr->register_offset[regnum]
1394 && (m_descr->register_offset[regnum]
1395 != (m_descr->register_offset[regnum - 1]
1396 + m_descr->sizeof_register[regnum - 1])))
1399 if (!footnote_register_offset)
1400 footnote_register_offset = ++footnote_nr;
1401 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1404 fprintf_unfiltered (file, " ");
1405 register_offset = (m_descr->register_offset[regnum]
1406 + m_descr->sizeof_register[regnum]);
1411 fprintf_unfiltered (file, " %5s ", "Size");
1413 fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1418 std::string name_holder;
1424 static const char blt[] = "builtin_type";
1426 t = TYPE_NAME (register_type (arch (), regnum));
1429 if (!footnote_register_type_name_null)
1430 footnote_register_type_name_null = ++footnote_nr;
1431 name_holder = string_printf ("*%d",
1432 footnote_register_type_name_null);
1433 t = name_holder.c_str ();
1435 /* Chop a leading builtin_type. */
1436 if (startswith (t, blt))
1439 fprintf_unfiltered (file, " %-15s", t);
1442 /* Leading space always present. */
1443 fprintf_unfiltered (file, " ");
1446 if (what_to_dump == regcache_dump_raw)
1449 fprintf_unfiltered (file, "Raw value");
1450 else if (regnum >= num_raw_registers ())
1451 fprintf_unfiltered (file, "<cooked>");
1452 else if (get_register_status (regnum) == REG_UNKNOWN)
1453 fprintf_unfiltered (file, "<invalid>");
1454 else if (get_register_status (regnum) == REG_UNAVAILABLE)
1455 fprintf_unfiltered (file, "<unavailable>");
1458 raw_update (regnum);
1459 print_hex_chars (file, register_buffer (regnum),
1460 m_descr->sizeof_register[regnum],
1461 gdbarch_byte_order (gdbarch), true);
1465 /* Value, cooked. */
1466 if (what_to_dump == regcache_dump_cooked)
1469 fprintf_unfiltered (file, "Cooked value");
1472 const gdb_byte *buf = NULL;
1473 enum register_status status;
1474 struct value *value = NULL;
1476 if (regnum < num_raw_registers ())
1478 raw_update (regnum);
1479 status = get_register_status (regnum);
1480 buf = register_buffer (regnum);
1484 value = cooked_read_value (regnum);
1486 if (!value_optimized_out (value)
1487 && value_entirely_available (value))
1490 buf = value_contents_all (value);
1493 status = REG_UNAVAILABLE;
1496 if (status == REG_UNKNOWN)
1497 fprintf_unfiltered (file, "<invalid>");
1498 else if (status == REG_UNAVAILABLE)
1499 fprintf_unfiltered (file, "<unavailable>");
1501 print_hex_chars (file, buf,
1502 m_descr->sizeof_register[regnum],
1503 gdbarch_byte_order (gdbarch), true);
1507 release_value (value);
1513 /* Group members. */
1514 if (what_to_dump == regcache_dump_groups)
1517 fprintf_unfiltered (file, "Groups");
1520 const char *sep = "";
1521 struct reggroup *group;
1523 for (group = reggroup_next (gdbarch, NULL);
1525 group = reggroup_next (gdbarch, group))
1527 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1529 fprintf_unfiltered (file,
1530 "%s%s", sep, reggroup_name (group));
1537 /* Remote packet configuration. */
1538 if (what_to_dump == regcache_dump_remote)
1542 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1544 else if (regnum < num_raw_registers ())
1548 if (remote_register_number_and_offset (arch (), regnum,
1550 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1554 fprintf_unfiltered (file, "\n");
1557 if (footnote_register_offset)
1558 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1559 footnote_register_offset);
1560 if (footnote_register_type_name_null)
1561 fprintf_unfiltered (file,
1562 "*%d: Register type's name NULL.\n",
1563 footnote_register_type_name_null);
1567 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
1569 /* Where to send output. */
1577 if (!file.open (args, "w"))
1578 perror_with_name (_("maintenance print architecture"));
1582 if (target_has_registers)
1583 get_current_regcache ()->dump (out, what_to_dump);
1586 /* For the benefit of "maint print registers" & co when
1587 debugging an executable, allow dumping a regcache even when
1588 there is no thread selected / no registers. */
1589 regcache dummy_regs (target_gdbarch ());
1590 dummy_regs.dump (out, what_to_dump);
1595 maintenance_print_registers (const char *args, int from_tty)
1597 regcache_print (args, regcache_dump_none);
1601 maintenance_print_raw_registers (const char *args, int from_tty)
1603 regcache_print (args, regcache_dump_raw);
1607 maintenance_print_cooked_registers (const char *args, int from_tty)
1609 regcache_print (args, regcache_dump_cooked);
1613 maintenance_print_register_groups (const char *args, int from_tty)
1615 regcache_print (args, regcache_dump_groups);
1619 maintenance_print_remote_registers (const char *args, int from_tty)
1621 regcache_print (args, regcache_dump_remote);
1625 #include "selftest.h"
1626 #include "selftest-arch.h"
1627 #include "gdbthread.h"
1628 #include "target-float.h"
1630 namespace selftests {
1632 class regcache_access : public regcache
1636 /* Return the number of elements in current_regcache. */
1639 current_regcache_size ()
1641 return std::distance (regcache::current_regcache.begin (),
1642 regcache::current_regcache.end ());
1647 current_regcache_test (void)
1649 /* It is empty at the start. */
1650 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1652 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1654 /* Get regcache from ptid1, a new regcache is added to
1655 current_regcache. */
1656 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1660 SELF_CHECK (regcache != NULL);
1661 SELF_CHECK (regcache->ptid () == ptid1);
1662 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1664 /* Get regcache from ptid2, a new regcache is added to
1665 current_regcache. */
1666 regcache = get_thread_arch_aspace_regcache (ptid2,
1669 SELF_CHECK (regcache != NULL);
1670 SELF_CHECK (regcache->ptid () == ptid2);
1671 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1673 /* Get regcache from ptid3, a new regcache is added to
1674 current_regcache. */
1675 regcache = get_thread_arch_aspace_regcache (ptid3,
1678 SELF_CHECK (regcache != NULL);
1679 SELF_CHECK (regcache->ptid () == ptid3);
1680 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1682 /* Get regcache from ptid2 again, nothing is added to
1683 current_regcache. */
1684 regcache = get_thread_arch_aspace_regcache (ptid2,
1687 SELF_CHECK (regcache != NULL);
1688 SELF_CHECK (regcache->ptid () == ptid2);
1689 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1691 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1692 current_regcache. */
1693 registers_changed_ptid (ptid2);
1694 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1697 static void test_target_fetch_registers (target_ops *self, regcache *regs,
1699 static void test_target_store_registers (target_ops *self, regcache *regs,
1701 static enum target_xfer_status
1702 test_target_xfer_partial (struct target_ops *ops,
1703 enum target_object object,
1704 const char *annex, gdb_byte *readbuf,
1705 const gdb_byte *writebuf,
1706 ULONGEST offset, ULONGEST len,
1707 ULONGEST *xfered_len);
1709 class target_ops_no_register : public test_target_ops
1712 target_ops_no_register ()
1713 : test_target_ops {}
1715 to_fetch_registers = test_target_fetch_registers;
1716 to_store_registers = test_target_store_registers;
1717 to_xfer_partial = test_target_xfer_partial;
1724 fetch_registers_called = 0;
1725 store_registers_called = 0;
1726 xfer_partial_called = 0;
1729 unsigned int fetch_registers_called = 0;
1730 unsigned int store_registers_called = 0;
1731 unsigned int xfer_partial_called = 0;
1735 test_target_fetch_registers (target_ops *self, regcache *regs, int regno)
1737 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1739 /* Mark register available. */
1740 regs->raw_supply_zeroed (regno);
1741 ops->fetch_registers_called++;
1745 test_target_store_registers (target_ops *self, regcache *regs, int regno)
1747 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1749 ops->store_registers_called++;
1752 static enum target_xfer_status
1753 test_target_xfer_partial (struct target_ops *self, enum target_object object,
1754 const char *annex, gdb_byte *readbuf,
1755 const gdb_byte *writebuf,
1756 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1758 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1760 ops->xfer_partial_called++;
1763 return TARGET_XFER_OK;
1766 class readwrite_regcache : public regcache
1769 readwrite_regcache (struct gdbarch *gdbarch)
1770 : regcache (gdbarch, nullptr, false)
1774 /* Test regcache::cooked_read gets registers from raw registers and
1775 memory instead of target to_{fetch,store}_registers. */
1778 cooked_read_test (struct gdbarch *gdbarch)
1780 /* Error out if debugging something, because we're going to push the
1781 test target, which would pop any existing target. */
1782 if (current_target.to_stratum >= process_stratum)
1783 error (_("target already pushed"));
1785 /* Create a mock environment. An inferior with a thread, with a
1786 process_stratum target pushed. */
1788 target_ops_no_register mock_target;
1789 ptid_t mock_ptid (1, 1);
1790 inferior mock_inferior (mock_ptid.pid ());
1791 address_space mock_aspace {};
1792 mock_inferior.gdbarch = gdbarch;
1793 mock_inferior.aspace = &mock_aspace;
1794 thread_info mock_thread (&mock_inferior, mock_ptid);
1796 scoped_restore restore_thread_list
1797 = make_scoped_restore (&thread_list, &mock_thread);
1799 /* Add the mock inferior to the inferior list so that look ups by
1800 target+ptid can find it. */
1801 scoped_restore restore_inferior_list
1802 = make_scoped_restore (&inferior_list);
1803 inferior_list = &mock_inferior;
1805 /* Switch to the mock inferior. */
1806 scoped_restore_current_inferior restore_current_inferior;
1807 set_current_inferior (&mock_inferior);
1809 /* Push the process_stratum target so we can mock accessing
1811 push_target (&mock_target);
1813 /* Pop it again on exit (return/exception). */
1818 pop_all_targets_at_and_above (process_stratum);
1822 /* Switch to the mock thread. */
1823 scoped_restore restore_inferior_ptid
1824 = make_scoped_restore (&inferior_ptid, mock_ptid);
1826 /* Test that read one raw register from regcache_no_target will go
1827 to the target layer. */
1830 /* Find a raw register which size isn't zero. */
1831 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1833 if (register_size (gdbarch, regnum) != 0)
1837 readwrite_regcache readwrite (gdbarch);
1838 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1840 readwrite.raw_read (regnum, buf.data ());
1842 /* raw_read calls target_fetch_registers. */
1843 SELF_CHECK (mock_target.fetch_registers_called > 0);
1844 mock_target.reset ();
1846 /* Mark all raw registers valid, so the following raw registers
1847 accesses won't go to target. */
1848 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1849 readwrite.raw_update (i);
1851 mock_target.reset ();
1852 /* Then, read all raw and pseudo registers, and don't expect calling
1853 to_{fetch,store}_registers. */
1854 for (int regnum = 0;
1855 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1858 if (register_size (gdbarch, regnum) == 0)
1861 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1863 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1865 SELF_CHECK (mock_target.fetch_registers_called == 0);
1866 SELF_CHECK (mock_target.store_registers_called == 0);
1868 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1869 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1870 SELF_CHECK (mock_target.xfer_partial_called == 0);
1872 mock_target.reset ();
1875 regcache readonly (regcache::readonly, readwrite);
1877 /* GDB may go to target layer to fetch all registers and memory for
1878 readonly regcache. */
1879 mock_target.reset ();
1881 for (int regnum = 0;
1882 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
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 /* Test regcache::cooked_write by writing some expected contents to
1947 registers, and checking that contents read from registers and the
1948 expected contents are the same. */
1951 cooked_write_test (struct gdbarch *gdbarch)
1953 /* Error out if debugging something, because we're going to push the
1954 test target, which would pop any existing target. */
1955 if (current_target.to_stratum >= process_stratum)
1956 error (_("target already pushed"));
1958 /* Create a mock environment. A process_stratum target pushed. */
1960 target_ops_no_register mock_target;
1962 /* Push the process_stratum target so we can mock accessing
1964 push_target (&mock_target);
1966 /* Pop it again on exit (return/exception). */
1971 pop_all_targets_at_and_above (process_stratum);
1975 readwrite_regcache readwrite (gdbarch);
1977 const int num_regs = (gdbarch_num_regs (gdbarch)
1978 + gdbarch_num_pseudo_regs (gdbarch));
1980 for (auto regnum = 0; regnum < num_regs; regnum++)
1982 if (register_size (gdbarch, regnum) == 0
1983 || gdbarch_cannot_store_register (gdbarch, regnum))
1986 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1988 if ((bfd_arch == bfd_arch_sparc
1989 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1990 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1991 && gdbarch_ptr_bit (gdbarch) == 64
1992 && (regnum >= gdbarch_num_regs (gdbarch)
1993 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1994 || (bfd_arch == bfd_arch_sh
1995 /* FPSCR_C_REGNUM in sh64 is hard to test. */
1996 && gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh5
1998 || (bfd_arch == bfd_arch_spu
1999 /* SPU pseudo registers except SPU_SP_REGNUM are got by
2000 TARGET_OBJECT_SPU. */
2001 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
2004 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
2005 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
2006 const auto type = register_type (gdbarch, regnum);
2008 if (TYPE_CODE (type) == TYPE_CODE_FLT
2009 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2011 /* Generate valid float format. */
2012 target_float_from_string (expected.data (), type, "1.25");
2014 else if (TYPE_CODE (type) == TYPE_CODE_INT
2015 || TYPE_CODE (type) == TYPE_CODE_ARRAY
2016 || TYPE_CODE (type) == TYPE_CODE_PTR
2017 || TYPE_CODE (type) == TYPE_CODE_UNION
2018 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
2020 if (bfd_arch == bfd_arch_ia64
2021 || (regnum >= gdbarch_num_regs (gdbarch)
2022 && (bfd_arch == bfd_arch_xtensa
2023 || bfd_arch == bfd_arch_bfin
2024 || bfd_arch == bfd_arch_m32c
2025 /* m68hc11 pseudo registers are in memory. */
2026 || bfd_arch == bfd_arch_m68hc11
2027 || bfd_arch == bfd_arch_m68hc12
2028 || bfd_arch == bfd_arch_s390))
2029 || (bfd_arch == bfd_arch_frv
2030 /* FRV pseudo registers except iacc0. */
2031 && regnum > gdbarch_num_regs (gdbarch)))
2033 /* Skip setting the expected values for some architecture
2036 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
2038 /* RL78_PC_REGNUM */
2039 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
2044 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
2048 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
2050 /* No idea how to test flags. */
2055 /* If we don't know how to create the expected value for the
2056 this type, make it fail. */
2060 readwrite.cooked_write (regnum, expected.data ());
2062 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2063 SELF_CHECK (expected == buf);
2067 } // namespace selftests
2068 #endif /* GDB_SELF_TEST */
2071 _initialize_regcache (void)
2073 regcache_descr_handle
2074 = gdbarch_data_register_post_init (init_regcache_descr);
2076 observer_attach_target_changed (regcache_observer_target_changed);
2077 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
2079 add_com ("flushregs", class_maintenance, reg_flush_command,
2080 _("Force gdb to flush its register cache (maintainer command)"));
2082 add_cmd ("registers", class_maintenance, maintenance_print_registers,
2083 _("Print the internal register configuration.\n"
2084 "Takes an optional file parameter."), &maintenanceprintlist);
2085 add_cmd ("raw-registers", class_maintenance,
2086 maintenance_print_raw_registers,
2087 _("Print the internal register configuration "
2088 "including raw values.\n"
2089 "Takes an optional file parameter."), &maintenanceprintlist);
2090 add_cmd ("cooked-registers", class_maintenance,
2091 maintenance_print_cooked_registers,
2092 _("Print the internal register configuration "
2093 "including cooked values.\n"
2094 "Takes an optional file parameter."), &maintenanceprintlist);
2095 add_cmd ("register-groups", class_maintenance,
2096 maintenance_print_register_groups,
2097 _("Print the internal register configuration "
2098 "including each register's group.\n"
2099 "Takes an optional file parameter."),
2100 &maintenanceprintlist);
2101 add_cmd ("remote-registers", class_maintenance,
2102 maintenance_print_remote_registers, _("\
2103 Print the internal register configuration including each register's\n\
2104 remote register number and buffer offset in the g/G packets.\n\
2105 Takes an optional file parameter."),
2106 &maintenanceprintlist);
2109 selftests::register_test ("current_regcache", selftests::current_regcache_test);
2111 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2112 selftests::cooked_read_test);
2113 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2114 selftests::cooked_write_test);