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/>. */
22 #include "gdbthread.h"
27 #include "reggroups.h"
28 #include "observable.h"
30 #include <forward_list>
35 * Here is the actual register cache.
38 /* Per-architecture object describing the layout of a register cache.
39 Computed once when the architecture is created. */
41 struct gdbarch_data *regcache_descr_handle;
45 /* The architecture this descriptor belongs to. */
46 struct gdbarch *gdbarch;
48 /* The raw register cache. Each raw (or hard) register is supplied
49 by the target interface. The raw cache should not contain
50 redundant information - if the PC is constructed from two
51 registers then those registers and not the PC lives in the raw
53 long sizeof_raw_registers;
55 /* The cooked register space. Each cooked register in the range
56 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
57 register. The remaining [NR_RAW_REGISTERS
58 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
59 both raw registers and memory by the architecture methods
60 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
61 int nr_cooked_registers;
62 long sizeof_cooked_registers;
64 /* Offset and size (in 8 bit bytes), of each register in the
65 register cache. All registers (including those in the range
66 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
68 long *register_offset;
69 long *sizeof_register;
71 /* Cached table containing the type of each register. */
72 struct type **register_type;
76 init_regcache_descr (struct gdbarch *gdbarch)
79 struct regcache_descr *descr;
80 gdb_assert (gdbarch != NULL);
82 /* Create an initial, zero filled, table. */
83 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
84 descr->gdbarch = gdbarch;
86 /* Total size of the register space. The raw registers are mapped
87 directly onto the raw register cache while the pseudo's are
88 either mapped onto raw-registers or memory. */
89 descr->nr_cooked_registers = gdbarch_num_cooked_regs (gdbarch);
91 /* Fill in a table of register types. */
93 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
95 for (i = 0; i < descr->nr_cooked_registers; i++)
96 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
98 /* Construct a strictly RAW register cache. Don't allow pseudo's
99 into the register cache. */
101 /* Lay out the register cache.
103 NOTE: cagney/2002-05-22: Only register_type() is used when
104 constructing the register cache. It is assumed that the
105 register's raw size, virtual size and type length are all the
111 descr->sizeof_register
112 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
113 descr->register_offset
114 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
115 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
117 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
118 descr->register_offset[i] = offset;
119 offset += descr->sizeof_register[i];
121 /* Set the real size of the raw register cache buffer. */
122 descr->sizeof_raw_registers = offset;
124 for (; i < descr->nr_cooked_registers; i++)
126 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
127 descr->register_offset[i] = offset;
128 offset += descr->sizeof_register[i];
130 /* Set the real size of the readonly register cache buffer. */
131 descr->sizeof_cooked_registers = offset;
137 static struct regcache_descr *
138 regcache_descr (struct gdbarch *gdbarch)
140 return (struct regcache_descr *) gdbarch_data (gdbarch,
141 regcache_descr_handle);
144 /* Utility functions returning useful register attributes stored in
145 the regcache descr. */
148 register_type (struct gdbarch *gdbarch, int regnum)
150 struct regcache_descr *descr = regcache_descr (gdbarch);
152 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
153 return descr->register_type[regnum];
156 /* Utility functions returning useful register attributes stored in
157 the regcache descr. */
160 register_size (struct gdbarch *gdbarch, int regnum)
162 struct regcache_descr *descr = regcache_descr (gdbarch);
165 gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
166 size = descr->sizeof_register[regnum];
170 /* See common/common-regcache.h. */
173 regcache_register_size (const struct regcache *regcache, int n)
175 return register_size (regcache->arch (), n);
178 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
179 : m_has_pseudo (has_pseudo)
181 gdb_assert (gdbarch != NULL);
182 m_descr = regcache_descr (gdbarch);
186 m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers] ());
187 m_register_status.reset
188 (new register_status[m_descr->nr_cooked_registers] ());
192 m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers] ());
193 m_register_status.reset
194 (new register_status[gdbarch_num_regs (gdbarch)] ());
198 regcache::regcache (gdbarch *gdbarch, const address_space *aspace_)
199 /* The register buffers. A read/write register cache can only hold
200 [0 .. gdbarch_num_regs). */
201 : detached_regcache (gdbarch, false), m_aspace (aspace_)
203 m_ptid = minus_one_ptid;
206 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
207 : readonly_detached_regcache (src.arch (),
208 [&src] (int regnum, gdb_byte *buf)
210 return src.cooked_read (regnum, buf);
216 reg_buffer::arch () const
218 return m_descr->gdbarch;
221 /* Cleanup class for invalidating a register. */
223 class regcache_invalidator
227 regcache_invalidator (struct regcache *regcache, int regnum)
228 : m_regcache (regcache),
233 ~regcache_invalidator ()
235 if (m_regcache != nullptr)
236 m_regcache->invalidate (m_regnum);
239 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
243 m_regcache = nullptr;
248 struct regcache *m_regcache;
252 /* Return a pointer to register REGNUM's buffer cache. */
255 reg_buffer::register_buffer (int regnum) const
257 return m_registers.get () + m_descr->register_offset[regnum];
261 reg_buffer::save (register_read_ftype cooked_read)
263 struct gdbarch *gdbarch = m_descr->gdbarch;
266 /* It should have pseudo registers. */
267 gdb_assert (m_has_pseudo);
268 /* Clear the dest. */
269 memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
270 memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
271 /* Copy over any registers (identified by their membership in the
272 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
273 gdbarch_num_pseudo_regs) range is checked since some architectures need
274 to save/restore `cooked' registers that live in memory. */
275 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
277 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
279 gdb_byte *dst_buf = register_buffer (regnum);
280 enum register_status status = cooked_read (regnum, dst_buf);
282 gdb_assert (status != REG_UNKNOWN);
284 if (status != REG_VALID)
285 memset (dst_buf, 0, register_size (gdbarch, regnum));
287 m_register_status[regnum] = status;
293 regcache::restore (readonly_detached_regcache *src)
295 struct gdbarch *gdbarch = m_descr->gdbarch;
298 gdb_assert (src != NULL);
299 gdb_assert (src->m_has_pseudo);
301 gdb_assert (gdbarch == src->arch ());
303 /* Copy over any registers, being careful to only restore those that
304 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
305 + gdbarch_num_pseudo_regs) range is checked since some architectures need
306 to save/restore `cooked' registers that live in memory. */
307 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
309 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
311 if (src->m_register_status[regnum] == REG_VALID)
312 cooked_write (regnum, src->register_buffer (regnum));
317 /* See common/common-regcache.h. */
320 reg_buffer::get_register_status (int regnum) const
322 assert_regnum (regnum);
324 return m_register_status[regnum];
328 reg_buffer::invalidate (int regnum)
330 assert_regnum (regnum);
331 m_register_status[regnum] = REG_UNKNOWN;
335 reg_buffer::assert_regnum (int regnum) const
337 gdb_assert (regnum >= 0);
339 gdb_assert (regnum < m_descr->nr_cooked_registers);
341 gdb_assert (regnum < gdbarch_num_regs (arch ()));
344 /* Global structure containing the current regcache. */
346 /* NOTE: this is a write-through cache. There is no "dirty" bit for
347 recording if the register values have been changed (eg. by the
348 user). Therefore all registers must be written back to the
349 target when appropriate. */
350 std::forward_list<regcache *> regcache::current_regcache;
353 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
354 struct address_space *aspace)
356 for (const auto ®cache : regcache::current_regcache)
357 if (regcache->ptid () == ptid && regcache->arch () == gdbarch)
360 regcache *new_regcache = new regcache (gdbarch, aspace);
362 regcache::current_regcache.push_front (new_regcache);
363 new_regcache->set_ptid (ptid);
369 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
371 address_space *aspace = target_thread_address_space (ptid);
373 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
376 static ptid_t current_thread_ptid;
377 static struct gdbarch *current_thread_arch;
380 get_thread_regcache (ptid_t ptid)
382 if (!current_thread_arch || current_thread_ptid != ptid)
384 current_thread_ptid = ptid;
385 current_thread_arch = target_thread_architecture (ptid);
388 return get_thread_arch_regcache (ptid, current_thread_arch);
391 /* See regcache.h. */
394 get_thread_regcache (thread_info *thread)
396 return get_thread_regcache (thread->ptid);
400 get_current_regcache (void)
402 return get_thread_regcache (inferior_thread ());
405 /* See common/common-regcache.h. */
408 get_thread_regcache_for_ptid (ptid_t ptid)
410 return get_thread_regcache (ptid);
413 /* Observer for the target_changed event. */
416 regcache_observer_target_changed (struct target_ops *target)
418 registers_changed ();
421 /* Update global variables old ptids to hold NEW_PTID if they were
424 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
426 for (auto ®cache : regcache::current_regcache)
428 if (regcache->ptid () == old_ptid)
429 regcache->set_ptid (new_ptid);
433 /* Low level examining and depositing of registers.
435 The caller is responsible for making sure that the inferior is
436 stopped before calling the fetching routines, or it will get
437 garbage. (a change from GDB version 3, in which the caller got the
438 value from the last stop). */
440 /* REGISTERS_CHANGED ()
442 Indicate that registers may have changed, so invalidate the cache. */
445 registers_changed_ptid (ptid_t ptid)
447 for (auto oit = regcache::current_regcache.before_begin (),
448 it = std::next (oit);
449 it != regcache::current_regcache.end ();
452 if ((*it)->ptid ().matches (ptid))
455 it = regcache::current_regcache.erase_after (oit);
461 if (current_thread_ptid.matches (ptid))
463 current_thread_ptid = null_ptid;
464 current_thread_arch = NULL;
467 if (inferior_ptid.matches (ptid))
469 /* We just deleted the regcache of the current thread. Need to
470 forget about any frames we have cached, too. */
471 reinit_frame_cache ();
475 /* See regcache.h. */
478 registers_changed_thread (thread_info *thread)
480 registers_changed_ptid (thread->ptid);
484 registers_changed (void)
486 registers_changed_ptid (minus_one_ptid);
488 /* Force cleanup of any alloca areas if using C alloca instead of
489 a builtin alloca. This particular call is used to clean up
490 areas allocated by low level target code which may build up
491 during lengthy interactions between gdb and the target before
492 gdb gives control to the user (ie watchpoints). */
497 regcache::raw_update (int regnum)
499 assert_regnum (regnum);
501 /* Make certain that the register cache is up-to-date with respect
502 to the current thread. This switching shouldn't be necessary
503 only there is still only one target side register cache. Sigh!
504 On the bright side, at least there is a regcache object. */
506 if (get_register_status (regnum) == REG_UNKNOWN)
508 target_fetch_registers (this, regnum);
510 /* A number of targets can't access the whole set of raw
511 registers (because the debug API provides no means to get at
513 if (m_register_status[regnum] == REG_UNKNOWN)
514 m_register_status[regnum] = REG_UNAVAILABLE;
519 readable_regcache::raw_read (int regnum, gdb_byte *buf)
521 gdb_assert (buf != NULL);
524 if (m_register_status[regnum] != REG_VALID)
525 memset (buf, 0, m_descr->sizeof_register[regnum]);
527 memcpy (buf, register_buffer (regnum),
528 m_descr->sizeof_register[regnum]);
530 return m_register_status[regnum];
534 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
536 gdb_assert (regcache != NULL);
537 return regcache->raw_read (regnum, val);
540 template<typename T, typename>
542 readable_regcache::raw_read (int regnum, T *val)
545 enum register_status status;
547 assert_regnum (regnum);
548 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
549 status = raw_read (regnum, buf);
550 if (status == REG_VALID)
551 *val = extract_integer<T> (buf,
552 m_descr->sizeof_register[regnum],
553 gdbarch_byte_order (m_descr->gdbarch));
560 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
563 gdb_assert (regcache != NULL);
564 return regcache->raw_read (regnum, val);
568 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
570 gdb_assert (regcache != NULL);
571 regcache->raw_write (regnum, val);
574 template<typename T, typename>
576 regcache::raw_write (int regnum, T val)
580 assert_regnum (regnum);
581 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
582 store_integer (buf, m_descr->sizeof_register[regnum],
583 gdbarch_byte_order (m_descr->gdbarch), val);
584 raw_write (regnum, buf);
588 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
591 gdb_assert (regcache != NULL);
592 regcache->raw_write (regnum, val);
596 regcache_raw_get_signed (struct regcache *regcache, int regnum)
599 enum register_status status;
601 status = regcache_raw_read_signed (regcache, regnum, &value);
602 if (status == REG_UNAVAILABLE)
603 throw_error (NOT_AVAILABLE_ERROR,
604 _("Register %d is not available"), regnum);
609 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
611 gdb_assert (regnum >= 0);
612 gdb_assert (regnum < m_descr->nr_cooked_registers);
613 if (regnum < num_raw_registers ())
614 return raw_read (regnum, buf);
615 else if (m_has_pseudo
616 && m_register_status[regnum] != REG_UNKNOWN)
618 if (m_register_status[regnum] == REG_VALID)
619 memcpy (buf, register_buffer (regnum),
620 m_descr->sizeof_register[regnum]);
622 memset (buf, 0, m_descr->sizeof_register[regnum]);
624 return m_register_status[regnum];
626 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
628 struct value *mark, *computed;
629 enum register_status result = REG_VALID;
631 mark = value_mark ();
633 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
635 if (value_entirely_available (computed))
636 memcpy (buf, value_contents_raw (computed),
637 m_descr->sizeof_register[regnum]);
640 memset (buf, 0, m_descr->sizeof_register[regnum]);
641 result = REG_UNAVAILABLE;
644 value_free_to_mark (mark);
649 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
654 readable_regcache::cooked_read_value (int regnum)
656 gdb_assert (regnum >= 0);
657 gdb_assert (regnum < m_descr->nr_cooked_registers);
659 if (regnum < num_raw_registers ()
660 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
661 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
663 struct value *result;
665 result = allocate_value (register_type (m_descr->gdbarch, regnum));
666 VALUE_LVAL (result) = lval_register;
667 VALUE_REGNUM (result) = regnum;
669 /* It is more efficient in general to do this delegation in this
670 direction than in the other one, even though the value-based
672 if (cooked_read (regnum,
673 value_contents_raw (result)) == REG_UNAVAILABLE)
674 mark_value_bytes_unavailable (result, 0,
675 TYPE_LENGTH (value_type (result)));
680 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
685 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
688 gdb_assert (regcache != NULL);
689 return regcache->cooked_read (regnum, val);
692 template<typename T, typename>
694 readable_regcache::cooked_read (int regnum, T *val)
696 enum register_status status;
699 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
700 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
701 status = cooked_read (regnum, buf);
702 if (status == REG_VALID)
703 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
704 gdbarch_byte_order (m_descr->gdbarch));
711 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
714 gdb_assert (regcache != NULL);
715 return regcache->cooked_read (regnum, val);
719 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
722 gdb_assert (regcache != NULL);
723 regcache->cooked_write (regnum, val);
726 template<typename T, typename>
728 regcache::cooked_write (int regnum, T val)
732 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
733 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
734 store_integer (buf, m_descr->sizeof_register[regnum],
735 gdbarch_byte_order (m_descr->gdbarch), val);
736 cooked_write (regnum, buf);
740 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
743 gdb_assert (regcache != NULL);
744 regcache->cooked_write (regnum, val);
748 regcache::raw_write (int regnum, const gdb_byte *buf)
751 gdb_assert (buf != NULL);
752 assert_regnum (regnum);
754 /* On the sparc, writing %g0 is a no-op, so we don't even want to
755 change the registers array if something writes to this register. */
756 if (gdbarch_cannot_store_register (arch (), regnum))
759 /* If we have a valid copy of the register, and new value == old
760 value, then don't bother doing the actual store. */
761 if (get_register_status (regnum) == REG_VALID
762 && (memcmp (register_buffer (regnum), buf,
763 m_descr->sizeof_register[regnum]) == 0))
766 target_prepare_to_store (this);
767 raw_supply (regnum, buf);
769 /* Invalidate the register after it is written, in case of a
771 regcache_invalidator invalidator (this, regnum);
773 target_store_registers (this, regnum);
775 /* The target did not throw an error so we can discard invalidating
777 invalidator.release ();
781 regcache::cooked_write (int regnum, const gdb_byte *buf)
783 gdb_assert (regnum >= 0);
784 gdb_assert (regnum < m_descr->nr_cooked_registers);
785 if (regnum < num_raw_registers ())
786 raw_write (regnum, buf);
788 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
792 /* See regcache.h. */
795 readable_regcache::read_part (int regnum, int offset, int len,
796 gdb_byte *out, bool is_raw)
798 int reg_size = register_size (arch (), regnum);
800 gdb_assert (out != NULL);
801 gdb_assert (offset >= 0 && offset <= reg_size);
802 gdb_assert (len >= 0 && offset + len <= reg_size);
804 if (offset == 0 && len == 0)
810 if (offset == 0 && len == reg_size)
812 /* Read the full register. */
813 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
816 enum register_status status;
817 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
819 /* Read full register to buffer. */
820 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
821 if (status != REG_VALID)
825 memcpy (out, reg + offset, len);
829 /* See regcache.h. */
832 reg_buffer::raw_collect_part (int regnum, int offset, int len,
835 int reg_size = register_size (arch (), regnum);
837 gdb_assert (out != nullptr);
838 gdb_assert (offset >= 0 && offset <= reg_size);
839 gdb_assert (len >= 0 && offset + len <= reg_size);
841 if (offset == 0 && len == 0)
847 if (offset == 0 && len == reg_size)
849 /* Collect the full register. */
850 return raw_collect (regnum, out);
853 /* Read to buffer, then write out. */
854 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
855 raw_collect (regnum, reg);
856 memcpy (out, reg + offset, len);
859 /* See regcache.h. */
862 regcache::write_part (int regnum, int offset, int len,
863 const gdb_byte *in, bool is_raw)
865 int reg_size = register_size (arch (), regnum);
867 gdb_assert (in != NULL);
868 gdb_assert (offset >= 0 && offset <= reg_size);
869 gdb_assert (len >= 0 && offset + len <= reg_size);
871 if (offset == 0 && len == 0)
877 if (offset == 0 && len == reg_size)
879 /* Write the full register. */
880 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
884 enum register_status status;
885 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
887 /* Read existing register to buffer. */
888 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
889 if (status != REG_VALID)
892 /* Update buffer, then write back to regcache. */
893 memcpy (reg + offset, in, len);
894 is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
898 /* See regcache.h. */
901 reg_buffer::raw_supply_part (int regnum, int offset, int len,
904 int reg_size = register_size (arch (), regnum);
906 gdb_assert (in != nullptr);
907 gdb_assert (offset >= 0 && offset <= reg_size);
908 gdb_assert (len >= 0 && offset + len <= reg_size);
910 if (offset == 0 && len == 0)
916 if (offset == 0 && len == reg_size)
918 /* Supply the full register. */
919 return raw_supply (regnum, in);
922 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
924 /* Read existing value to buffer. */
925 raw_collect (regnum, reg);
927 /* Write to buffer, then write out. */
928 memcpy (reg + offset, in, len);
929 raw_supply (regnum, reg);
933 readable_regcache::raw_read_part (int regnum, int offset, int len,
936 assert_regnum (regnum);
937 return read_part (regnum, offset, len, buf, true);
940 /* See regcache.h. */
943 regcache::raw_write_part (int regnum, int offset, int len,
946 assert_regnum (regnum);
947 write_part (regnum, offset, len, buf, true);
950 /* See regcache.h. */
953 readable_regcache::cooked_read_part (int regnum, int offset, int len,
956 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
957 return read_part (regnum, offset, len, buf, false);
960 /* See regcache.h. */
963 regcache::cooked_write_part (int regnum, int offset, int len,
966 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
967 write_part (regnum, offset, len, buf, false);
970 /* See common/common-regcache.h. */
973 reg_buffer::raw_supply (int regnum, const void *buf)
978 assert_regnum (regnum);
980 regbuf = register_buffer (regnum);
981 size = m_descr->sizeof_register[regnum];
985 memcpy (regbuf, buf, size);
986 m_register_status[regnum] = REG_VALID;
990 /* This memset not strictly necessary, but better than garbage
991 in case the register value manages to escape somewhere (due
992 to a bug, no less). */
993 memset (regbuf, 0, size);
994 m_register_status[regnum] = REG_UNAVAILABLE;
998 /* See regcache.h. */
1001 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1002 int addr_len, bool is_signed)
1004 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1008 assert_regnum (regnum);
1010 regbuf = register_buffer (regnum);
1011 regsize = m_descr->sizeof_register[regnum];
1013 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1015 m_register_status[regnum] = REG_VALID;
1018 /* See regcache.h. */
1021 reg_buffer::raw_supply_zeroed (int regnum)
1026 assert_regnum (regnum);
1028 regbuf = register_buffer (regnum);
1029 size = m_descr->sizeof_register[regnum];
1031 memset (regbuf, 0, size);
1032 m_register_status[regnum] = REG_VALID;
1035 /* See common/common-regcache.h. */
1038 reg_buffer::raw_collect (int regnum, void *buf) const
1043 gdb_assert (buf != NULL);
1044 assert_regnum (regnum);
1046 regbuf = register_buffer (regnum);
1047 size = m_descr->sizeof_register[regnum];
1048 memcpy (buf, regbuf, size);
1051 /* See regcache.h. */
1054 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1055 bool is_signed) const
1057 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1058 const gdb_byte *regbuf;
1061 assert_regnum (regnum);
1063 regbuf = register_buffer (regnum);
1064 regsize = m_descr->sizeof_register[regnum];
1066 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1070 /* See regcache.h. */
1073 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1074 const gdb_byte *in_buf, gdb_byte *out_buf,
1075 int slot_size, int offs) const
1077 struct gdbarch *gdbarch = arch ();
1078 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1080 /* Use part versions and reg_size to prevent possible buffer overflows when
1081 accessing the regcache. */
1083 if (out_buf != nullptr)
1085 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1087 /* Ensure any additional space is cleared. */
1088 if (slot_size > reg_size)
1089 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1091 else if (in_buf != nullptr)
1092 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1095 /* Invalidate the register. */
1096 out_regcache->raw_supply (regnum, nullptr);
1100 /* See regcache.h. */
1103 regcache::transfer_regset (const struct regset *regset,
1104 struct regcache *out_regcache,
1105 int regnum, const gdb_byte *in_buf,
1106 gdb_byte *out_buf, size_t size) const
1108 const struct regcache_map_entry *map;
1109 int offs = 0, count;
1111 for (map = (const struct regcache_map_entry *) regset->regmap;
1112 (count = map->count) != 0;
1115 int regno = map->regno;
1116 int slot_size = map->size;
1118 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1119 slot_size = m_descr->sizeof_register[regno];
1121 if (regno == REGCACHE_MAP_SKIP
1123 && (regnum < regno || regnum >= regno + count)))
1124 offs += count * slot_size;
1126 else if (regnum == -1)
1127 for (; count--; regno++, offs += slot_size)
1129 if (offs + slot_size > size)
1132 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1137 /* Transfer a single register and return. */
1138 offs += (regnum - regno) * slot_size;
1139 if (offs + slot_size > size)
1142 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1149 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1150 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1151 If BUF is NULL, set the register(s) to "unavailable" status. */
1154 regcache_supply_regset (const struct regset *regset,
1155 struct regcache *regcache,
1156 int regnum, const void *buf, size_t size)
1158 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1162 regcache::supply_regset (const struct regset *regset,
1163 int regnum, const void *buf, size_t size)
1165 transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
1168 /* Collect register REGNUM from REGCACHE to BUF, using the register
1169 map in REGSET. If REGNUM is -1, do this for all registers in
1173 regcache_collect_regset (const struct regset *regset,
1174 const struct regcache *regcache,
1175 int regnum, void *buf, size_t size)
1177 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1181 regcache::collect_regset (const struct regset *regset,
1182 int regnum, void *buf, size_t size) const
1184 transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1187 /* See common/common-regcache.h. */
1190 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1192 gdb_assert (buf != NULL);
1193 assert_regnum (regnum);
1195 const char *regbuf = (const char *) register_buffer (regnum);
1196 size_t size = m_descr->sizeof_register[regnum];
1197 gdb_assert (size >= offset);
1199 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1202 /* Special handling for register PC. */
1205 regcache_read_pc (struct regcache *regcache)
1207 struct gdbarch *gdbarch = regcache->arch ();
1211 if (gdbarch_read_pc_p (gdbarch))
1212 pc_val = gdbarch_read_pc (gdbarch, regcache);
1213 /* Else use per-frame method on get_current_frame. */
1214 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1218 if (regcache_cooked_read_unsigned (regcache,
1219 gdbarch_pc_regnum (gdbarch),
1220 &raw_val) == REG_UNAVAILABLE)
1221 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1223 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1226 internal_error (__FILE__, __LINE__,
1227 _("regcache_read_pc: Unable to find PC"));
1232 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1234 struct gdbarch *gdbarch = regcache->arch ();
1236 if (gdbarch_write_pc_p (gdbarch))
1237 gdbarch_write_pc (gdbarch, regcache, pc);
1238 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1239 regcache_cooked_write_unsigned (regcache,
1240 gdbarch_pc_regnum (gdbarch), pc);
1242 internal_error (__FILE__, __LINE__,
1243 _("regcache_write_pc: Unable to update PC"));
1245 /* Writing the PC (for instance, from "load") invalidates the
1247 reinit_frame_cache ();
1251 reg_buffer::num_raw_registers () const
1253 return gdbarch_num_regs (arch ());
1257 regcache::debug_print_register (const char *func, int regno)
1259 struct gdbarch *gdbarch = arch ();
1261 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1262 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1263 && gdbarch_register_name (gdbarch, regno) != NULL
1264 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1265 fprintf_unfiltered (gdb_stdlog, "(%s)",
1266 gdbarch_register_name (gdbarch, regno));
1268 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1269 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1271 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1272 int size = register_size (gdbarch, regno);
1273 gdb_byte *buf = register_buffer (regno);
1275 fprintf_unfiltered (gdb_stdlog, " = ");
1276 for (int i = 0; i < size; i++)
1278 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1280 if (size <= sizeof (LONGEST))
1282 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1284 fprintf_unfiltered (gdb_stdlog, " %s %s",
1285 core_addr_to_string_nz (val), plongest (val));
1288 fprintf_unfiltered (gdb_stdlog, "\n");
1292 reg_flush_command (const char *command, int from_tty)
1294 /* Force-flush the register cache. */
1295 registers_changed ();
1297 printf_filtered (_("Register cache flushed.\n"));
1301 register_dump::dump (ui_file *file)
1303 auto descr = regcache_descr (m_gdbarch);
1305 int footnote_nr = 0;
1306 int footnote_register_offset = 0;
1307 int footnote_register_type_name_null = 0;
1308 long register_offset = 0;
1310 gdb_assert (descr->nr_cooked_registers
1311 == gdbarch_num_cooked_regs (m_gdbarch));
1313 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1317 fprintf_unfiltered (file, " %-10s", "Name");
1320 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1324 else if (p[0] == '\0')
1326 fprintf_unfiltered (file, " %-10s", p);
1331 fprintf_unfiltered (file, " %4s", "Nr");
1333 fprintf_unfiltered (file, " %4d", regnum);
1335 /* Relative number. */
1337 fprintf_unfiltered (file, " %4s", "Rel");
1338 else if (regnum < gdbarch_num_regs (m_gdbarch))
1339 fprintf_unfiltered (file, " %4d", regnum);
1341 fprintf_unfiltered (file, " %4d",
1342 (regnum - gdbarch_num_regs (m_gdbarch)));
1346 fprintf_unfiltered (file, " %6s ", "Offset");
1349 fprintf_unfiltered (file, " %6ld",
1350 descr->register_offset[regnum]);
1351 if (register_offset != descr->register_offset[regnum]
1353 && (descr->register_offset[regnum]
1354 != (descr->register_offset[regnum - 1]
1355 + descr->sizeof_register[regnum - 1])))
1358 if (!footnote_register_offset)
1359 footnote_register_offset = ++footnote_nr;
1360 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1363 fprintf_unfiltered (file, " ");
1364 register_offset = (descr->register_offset[regnum]
1365 + descr->sizeof_register[regnum]);
1370 fprintf_unfiltered (file, " %5s ", "Size");
1372 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1377 std::string name_holder;
1383 static const char blt[] = "builtin_type";
1385 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1388 if (!footnote_register_type_name_null)
1389 footnote_register_type_name_null = ++footnote_nr;
1390 name_holder = string_printf ("*%d",
1391 footnote_register_type_name_null);
1392 t = name_holder.c_str ();
1394 /* Chop a leading builtin_type. */
1395 if (startswith (t, blt))
1398 fprintf_unfiltered (file, " %-15s", t);
1401 /* Leading space always present. */
1402 fprintf_unfiltered (file, " ");
1404 dump_reg (file, regnum);
1406 fprintf_unfiltered (file, "\n");
1409 if (footnote_register_offset)
1410 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1411 footnote_register_offset);
1412 if (footnote_register_type_name_null)
1413 fprintf_unfiltered (file,
1414 "*%d: Register type's name NULL.\n",
1415 footnote_register_type_name_null);
1419 #include "selftest.h"
1420 #include "selftest-arch.h"
1421 #include "gdbthread.h"
1422 #include "target-float.h"
1424 namespace selftests {
1426 class regcache_access : public regcache
1430 /* Return the number of elements in current_regcache. */
1433 current_regcache_size ()
1435 return std::distance (regcache::current_regcache.begin (),
1436 regcache::current_regcache.end ());
1441 current_regcache_test (void)
1443 /* It is empty at the start. */
1444 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1446 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1448 /* Get regcache from ptid1, a new regcache is added to
1449 current_regcache. */
1450 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1454 SELF_CHECK (regcache != NULL);
1455 SELF_CHECK (regcache->ptid () == ptid1);
1456 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1458 /* Get regcache from ptid2, a new regcache is added to
1459 current_regcache. */
1460 regcache = get_thread_arch_aspace_regcache (ptid2,
1463 SELF_CHECK (regcache != NULL);
1464 SELF_CHECK (regcache->ptid () == ptid2);
1465 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1467 /* Get regcache from ptid3, a new regcache is added to
1468 current_regcache. */
1469 regcache = get_thread_arch_aspace_regcache (ptid3,
1472 SELF_CHECK (regcache != NULL);
1473 SELF_CHECK (regcache->ptid () == ptid3);
1474 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1476 /* Get regcache from ptid2 again, nothing is added to
1477 current_regcache. */
1478 regcache = get_thread_arch_aspace_regcache (ptid2,
1481 SELF_CHECK (regcache != NULL);
1482 SELF_CHECK (regcache->ptid () == ptid2);
1483 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1485 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1486 current_regcache. */
1487 registers_changed_ptid (ptid2);
1488 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1491 class target_ops_no_register : public test_target_ops
1494 target_ops_no_register ()
1495 : test_target_ops {}
1500 fetch_registers_called = 0;
1501 store_registers_called = 0;
1502 xfer_partial_called = 0;
1505 void fetch_registers (regcache *regs, int regno) override;
1506 void store_registers (regcache *regs, int regno) override;
1508 enum target_xfer_status xfer_partial (enum target_object object,
1509 const char *annex, gdb_byte *readbuf,
1510 const gdb_byte *writebuf,
1511 ULONGEST offset, ULONGEST len,
1512 ULONGEST *xfered_len) override;
1514 unsigned int fetch_registers_called = 0;
1515 unsigned int store_registers_called = 0;
1516 unsigned int xfer_partial_called = 0;
1520 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1522 /* Mark register available. */
1523 regs->raw_supply_zeroed (regno);
1524 this->fetch_registers_called++;
1528 target_ops_no_register::store_registers (regcache *regs, int regno)
1530 this->store_registers_called++;
1533 enum target_xfer_status
1534 target_ops_no_register::xfer_partial (enum target_object object,
1535 const char *annex, gdb_byte *readbuf,
1536 const gdb_byte *writebuf,
1537 ULONGEST offset, ULONGEST len,
1538 ULONGEST *xfered_len)
1540 this->xfer_partial_called++;
1543 return TARGET_XFER_OK;
1546 class readwrite_regcache : public regcache
1549 readwrite_regcache (struct gdbarch *gdbarch)
1550 : regcache (gdbarch, nullptr)
1554 /* Test regcache::cooked_read gets registers from raw registers and
1555 memory instead of target to_{fetch,store}_registers. */
1558 cooked_read_test (struct gdbarch *gdbarch)
1560 /* Error out if debugging something, because we're going to push the
1561 test target, which would pop any existing target. */
1562 if (current_top_target ()->to_stratum >= process_stratum)
1563 error (_("target already pushed"));
1565 /* Create a mock environment. An inferior with a thread, with a
1566 process_stratum target pushed. */
1568 target_ops_no_register mock_target;
1569 ptid_t mock_ptid (1, 1);
1570 inferior mock_inferior (mock_ptid.pid ());
1571 address_space mock_aspace {};
1572 mock_inferior.gdbarch = gdbarch;
1573 mock_inferior.aspace = &mock_aspace;
1574 thread_info mock_thread (&mock_inferior, mock_ptid);
1576 scoped_restore restore_thread_list
1577 = make_scoped_restore (&thread_list, &mock_thread);
1579 /* Add the mock inferior to the inferior list so that look ups by
1580 target+ptid can find it. */
1581 scoped_restore restore_inferior_list
1582 = make_scoped_restore (&inferior_list);
1583 inferior_list = &mock_inferior;
1585 /* Switch to the mock inferior. */
1586 scoped_restore_current_inferior restore_current_inferior;
1587 set_current_inferior (&mock_inferior);
1589 /* Push the process_stratum target so we can mock accessing
1591 push_target (&mock_target);
1593 /* Pop it again on exit (return/exception). */
1598 pop_all_targets_at_and_above (process_stratum);
1602 /* Switch to the mock thread. */
1603 scoped_restore restore_inferior_ptid
1604 = make_scoped_restore (&inferior_ptid, mock_ptid);
1606 /* Test that read one raw register from regcache_no_target will go
1607 to the target layer. */
1609 /* Find a raw register which size isn't zero. */
1611 for (nonzero_regnum = 0;
1612 nonzero_regnum < gdbarch_num_regs (gdbarch);
1615 if (register_size (gdbarch, nonzero_regnum) != 0)
1619 readwrite_regcache readwrite (gdbarch);
1620 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1622 readwrite.raw_read (nonzero_regnum, buf.data ());
1624 /* raw_read calls target_fetch_registers. */
1625 SELF_CHECK (mock_target.fetch_registers_called > 0);
1626 mock_target.reset ();
1628 /* Mark all raw registers valid, so the following raw registers
1629 accesses won't go to target. */
1630 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1631 readwrite.raw_update (i);
1633 mock_target.reset ();
1634 /* Then, read all raw and pseudo registers, and don't expect calling
1635 to_{fetch,store}_registers. */
1636 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1638 if (register_size (gdbarch, regnum) == 0)
1641 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1643 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1644 inner_buf.data ()));
1646 SELF_CHECK (mock_target.fetch_registers_called == 0);
1647 SELF_CHECK (mock_target.store_registers_called == 0);
1649 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1650 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1651 SELF_CHECK (mock_target.xfer_partial_called == 0);
1653 mock_target.reset ();
1656 readonly_detached_regcache readonly (readwrite);
1658 /* GDB may go to target layer to fetch all registers and memory for
1659 readonly regcache. */
1660 mock_target.reset ();
1662 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1664 if (register_size (gdbarch, regnum) == 0)
1667 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1668 enum register_status status = readonly.cooked_read (regnum,
1671 if (regnum < gdbarch_num_regs (gdbarch))
1673 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1675 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1676 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1677 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1678 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1679 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1680 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1681 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1682 || bfd_arch == bfd_arch_riscv)
1684 /* Raw registers. If raw registers are not in save_reggroup,
1685 their status are unknown. */
1686 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1687 SELF_CHECK (status == REG_VALID);
1689 SELF_CHECK (status == REG_UNKNOWN);
1692 SELF_CHECK (status == REG_VALID);
1696 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1697 SELF_CHECK (status == REG_VALID);
1700 /* If pseudo registers are not in save_reggroup, some of
1701 them can be computed from saved raw registers, but some
1702 of them are unknown. */
1703 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1705 if (bfd_arch == bfd_arch_frv
1706 || bfd_arch == bfd_arch_m32c
1707 || bfd_arch == bfd_arch_mep
1708 || bfd_arch == bfd_arch_sh)
1709 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1710 else if (bfd_arch == bfd_arch_mips
1711 || bfd_arch == bfd_arch_h8300)
1712 SELF_CHECK (status == REG_UNKNOWN);
1714 SELF_CHECK (status == REG_VALID);
1718 SELF_CHECK (mock_target.fetch_registers_called == 0);
1719 SELF_CHECK (mock_target.store_registers_called == 0);
1720 SELF_CHECK (mock_target.xfer_partial_called == 0);
1722 mock_target.reset ();
1726 /* Test regcache::cooked_write by writing some expected contents to
1727 registers, and checking that contents read from registers and the
1728 expected contents are the same. */
1731 cooked_write_test (struct gdbarch *gdbarch)
1733 /* Error out if debugging something, because we're going to push the
1734 test target, which would pop any existing target. */
1735 if (current_top_target ()->to_stratum >= process_stratum)
1736 error (_("target already pushed"));
1738 /* Create a mock environment. A process_stratum target pushed. */
1740 target_ops_no_register mock_target;
1742 /* Push the process_stratum target so we can mock accessing
1744 push_target (&mock_target);
1746 /* Pop it again on exit (return/exception). */
1751 pop_all_targets_at_and_above (process_stratum);
1755 readwrite_regcache readwrite (gdbarch);
1757 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1759 for (auto regnum = 0; regnum < num_regs; regnum++)
1761 if (register_size (gdbarch, regnum) == 0
1762 || gdbarch_cannot_store_register (gdbarch, regnum))
1765 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1767 if ((bfd_arch == bfd_arch_sparc
1768 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1769 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1770 && gdbarch_ptr_bit (gdbarch) == 64
1771 && (regnum >= gdbarch_num_regs (gdbarch)
1772 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1773 || (bfd_arch == bfd_arch_spu
1774 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1775 TARGET_OBJECT_SPU. */
1776 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
1779 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1780 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1781 const auto type = register_type (gdbarch, regnum);
1783 if (TYPE_CODE (type) == TYPE_CODE_FLT
1784 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1786 /* Generate valid float format. */
1787 target_float_from_string (expected.data (), type, "1.25");
1789 else if (TYPE_CODE (type) == TYPE_CODE_INT
1790 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1791 || TYPE_CODE (type) == TYPE_CODE_PTR
1792 || TYPE_CODE (type) == TYPE_CODE_UNION
1793 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
1795 if (bfd_arch == bfd_arch_ia64
1796 || (regnum >= gdbarch_num_regs (gdbarch)
1797 && (bfd_arch == bfd_arch_xtensa
1798 || bfd_arch == bfd_arch_bfin
1799 || bfd_arch == bfd_arch_m32c
1800 /* m68hc11 pseudo registers are in memory. */
1801 || bfd_arch == bfd_arch_m68hc11
1802 || bfd_arch == bfd_arch_m68hc12
1803 || bfd_arch == bfd_arch_s390))
1804 || (bfd_arch == bfd_arch_frv
1805 /* FRV pseudo registers except iacc0. */
1806 && regnum > gdbarch_num_regs (gdbarch)))
1808 /* Skip setting the expected values for some architecture
1811 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1813 /* RL78_PC_REGNUM */
1814 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1819 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1823 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
1825 /* No idea how to test flags. */
1830 /* If we don't know how to create the expected value for the
1831 this type, make it fail. */
1835 readwrite.cooked_write (regnum, expected.data ());
1837 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1838 SELF_CHECK (expected == buf);
1842 } // namespace selftests
1843 #endif /* GDB_SELF_TEST */
1846 _initialize_regcache (void)
1848 regcache_descr_handle
1849 = gdbarch_data_register_post_init (init_regcache_descr);
1851 gdb::observers::target_changed.attach (regcache_observer_target_changed);
1852 gdb::observers::thread_ptid_changed.attach
1853 (regcache::regcache_thread_ptid_changed);
1855 add_com ("flushregs", class_maintenance, reg_flush_command,
1856 _("Force gdb to flush its register cache (maintainer command)"));
1859 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1861 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1862 selftests::cooked_read_test);
1863 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1864 selftests::cooked_write_test);