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_regs (gdbarch)
90 + gdbarch_num_pseudo_regs (gdbarch);
92 /* Fill in a table of register types. */
94 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
96 for (i = 0; i < descr->nr_cooked_registers; i++)
97 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
99 /* Construct a strictly RAW register cache. Don't allow pseudo's
100 into the register cache. */
102 /* Lay out the register cache.
104 NOTE: cagney/2002-05-22: Only register_type() is used when
105 constructing the register cache. It is assumed that the
106 register's raw size, virtual size and type length are all the
112 descr->sizeof_register
113 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
114 descr->register_offset
115 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
116 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
118 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
119 descr->register_offset[i] = offset;
120 offset += descr->sizeof_register[i];
122 /* Set the real size of the raw register cache buffer. */
123 descr->sizeof_raw_registers = offset;
125 for (; i < descr->nr_cooked_registers; i++)
127 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
128 descr->register_offset[i] = offset;
129 offset += descr->sizeof_register[i];
131 /* Set the real size of the readonly register cache buffer. */
132 descr->sizeof_cooked_registers = offset;
138 static struct regcache_descr *
139 regcache_descr (struct gdbarch *gdbarch)
141 return (struct regcache_descr *) gdbarch_data (gdbarch,
142 regcache_descr_handle);
145 /* Utility functions returning useful register attributes stored in
146 the regcache descr. */
149 register_type (struct gdbarch *gdbarch, int regnum)
151 struct regcache_descr *descr = regcache_descr (gdbarch);
153 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
154 return descr->register_type[regnum];
157 /* Utility functions returning useful register attributes stored in
158 the regcache descr. */
161 register_size (struct gdbarch *gdbarch, int regnum)
163 struct regcache_descr *descr = regcache_descr (gdbarch);
166 gdb_assert (regnum >= 0
167 && regnum < (gdbarch_num_regs (gdbarch)
168 + gdbarch_num_pseudo_regs (gdbarch)));
169 size = descr->sizeof_register[regnum];
173 /* See common/common-regcache.h. */
176 regcache_register_size (const struct regcache *regcache, int n)
178 return register_size (regcache->arch (), n);
181 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
182 : m_has_pseudo (has_pseudo)
184 gdb_assert (gdbarch != NULL);
185 m_descr = regcache_descr (gdbarch);
189 m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers] ());
190 m_register_status.reset
191 (new register_status[m_descr->nr_cooked_registers] ());
195 m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers] ());
196 m_register_status.reset
197 (new register_status[gdbarch_num_regs (gdbarch)] ());
201 regcache::regcache (gdbarch *gdbarch, const address_space *aspace_)
202 /* The register buffers. A read/write register cache can only hold
203 [0 .. gdbarch_num_regs). */
204 : detached_regcache (gdbarch, false), m_aspace (aspace_)
206 m_ptid = minus_one_ptid;
209 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
210 : readonly_detached_regcache (src.arch (),
211 [&src] (int regnum, gdb_byte *buf)
213 return src.cooked_read (regnum, buf);
219 reg_buffer::arch () const
221 return m_descr->gdbarch;
224 /* Cleanup class for invalidating a register. */
226 class regcache_invalidator
230 regcache_invalidator (struct regcache *regcache, int regnum)
231 : m_regcache (regcache),
236 ~regcache_invalidator ()
238 if (m_regcache != nullptr)
239 m_regcache->invalidate (m_regnum);
242 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
246 m_regcache = nullptr;
251 struct regcache *m_regcache;
255 /* Return a pointer to register REGNUM's buffer cache. */
258 reg_buffer::register_buffer (int regnum) const
260 return m_registers.get () + m_descr->register_offset[regnum];
264 reg_buffer::save (register_read_ftype cooked_read)
266 struct gdbarch *gdbarch = m_descr->gdbarch;
269 /* It should have pseudo registers. */
270 gdb_assert (m_has_pseudo);
271 /* Clear the dest. */
272 memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
273 memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
274 /* Copy over any registers (identified by their membership in the
275 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
276 gdbarch_num_pseudo_regs) range is checked since some architectures need
277 to save/restore `cooked' registers that live in memory. */
278 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
280 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
282 gdb_byte *dst_buf = register_buffer (regnum);
283 enum register_status status = cooked_read (regnum, dst_buf);
285 gdb_assert (status != REG_UNKNOWN);
287 if (status != REG_VALID)
288 memset (dst_buf, 0, register_size (gdbarch, regnum));
290 m_register_status[regnum] = status;
296 regcache::restore (readonly_detached_regcache *src)
298 struct gdbarch *gdbarch = m_descr->gdbarch;
301 gdb_assert (src != NULL);
302 gdb_assert (src->m_has_pseudo);
304 gdb_assert (gdbarch == src->arch ());
306 /* Copy over any registers, being careful to only restore those that
307 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
308 + gdbarch_num_pseudo_regs) range is checked since some architectures need
309 to save/restore `cooked' registers that live in memory. */
310 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
312 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
314 if (src->m_register_status[regnum] == REG_VALID)
315 cooked_write (regnum, src->register_buffer (regnum));
320 /* See common/common-regcache.h. */
323 reg_buffer::get_register_status (int regnum) const
325 assert_regnum (regnum);
327 return m_register_status[regnum];
331 reg_buffer::invalidate (int regnum)
333 assert_regnum (regnum);
334 m_register_status[regnum] = REG_UNKNOWN;
338 reg_buffer::assert_regnum (int regnum) const
340 gdb_assert (regnum >= 0);
342 gdb_assert (regnum < m_descr->nr_cooked_registers);
344 gdb_assert (regnum < gdbarch_num_regs (arch ()));
347 /* Global structure containing the current regcache. */
349 /* NOTE: this is a write-through cache. There is no "dirty" bit for
350 recording if the register values have been changed (eg. by the
351 user). Therefore all registers must be written back to the
352 target when appropriate. */
353 std::forward_list<regcache *> regcache::current_regcache;
356 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
357 struct address_space *aspace)
359 for (const auto ®cache : regcache::current_regcache)
360 if (regcache->ptid () == ptid && regcache->arch () == gdbarch)
363 regcache *new_regcache = new regcache (gdbarch, aspace);
365 regcache::current_regcache.push_front (new_regcache);
366 new_regcache->set_ptid (ptid);
372 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
374 address_space *aspace = target_thread_address_space (ptid);
376 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
379 static ptid_t current_thread_ptid;
380 static struct gdbarch *current_thread_arch;
383 get_thread_regcache (ptid_t ptid)
385 if (!current_thread_arch || current_thread_ptid != ptid)
387 current_thread_ptid = ptid;
388 current_thread_arch = target_thread_architecture (ptid);
391 return get_thread_arch_regcache (ptid, current_thread_arch);
394 /* See regcache.h. */
397 get_thread_regcache (thread_info *thread)
399 return get_thread_regcache (thread->ptid);
403 get_current_regcache (void)
405 return get_thread_regcache (inferior_thread ());
408 /* See common/common-regcache.h. */
411 get_thread_regcache_for_ptid (ptid_t ptid)
413 return get_thread_regcache (ptid);
416 /* Observer for the target_changed event. */
419 regcache_observer_target_changed (struct target_ops *target)
421 registers_changed ();
424 /* Update global variables old ptids to hold NEW_PTID if they were
427 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
429 for (auto ®cache : regcache::current_regcache)
431 if (regcache->ptid () == old_ptid)
432 regcache->set_ptid (new_ptid);
436 /* Low level examining and depositing of registers.
438 The caller is responsible for making sure that the inferior is
439 stopped before calling the fetching routines, or it will get
440 garbage. (a change from GDB version 3, in which the caller got the
441 value from the last stop). */
443 /* REGISTERS_CHANGED ()
445 Indicate that registers may have changed, so invalidate the cache. */
448 registers_changed_ptid (ptid_t ptid)
450 for (auto oit = regcache::current_regcache.before_begin (),
451 it = std::next (oit);
452 it != regcache::current_regcache.end ();
455 if ((*it)->ptid ().matches (ptid))
458 it = regcache::current_regcache.erase_after (oit);
464 if (current_thread_ptid.matches (ptid))
466 current_thread_ptid = null_ptid;
467 current_thread_arch = NULL;
470 if (inferior_ptid.matches (ptid))
472 /* We just deleted the regcache of the current thread. Need to
473 forget about any frames we have cached, too. */
474 reinit_frame_cache ();
478 /* See regcache.h. */
481 registers_changed_thread (thread_info *thread)
483 registers_changed_ptid (thread->ptid);
487 registers_changed (void)
489 registers_changed_ptid (minus_one_ptid);
491 /* Force cleanup of any alloca areas if using C alloca instead of
492 a builtin alloca. This particular call is used to clean up
493 areas allocated by low level target code which may build up
494 during lengthy interactions between gdb and the target before
495 gdb gives control to the user (ie watchpoints). */
500 regcache::raw_update (int regnum)
502 assert_regnum (regnum);
504 /* Make certain that the register cache is up-to-date with respect
505 to the current thread. This switching shouldn't be necessary
506 only there is still only one target side register cache. Sigh!
507 On the bright side, at least there is a regcache object. */
509 if (get_register_status (regnum) == REG_UNKNOWN)
511 target_fetch_registers (this, regnum);
513 /* A number of targets can't access the whole set of raw
514 registers (because the debug API provides no means to get at
516 if (m_register_status[regnum] == REG_UNKNOWN)
517 m_register_status[regnum] = REG_UNAVAILABLE;
522 readable_regcache::raw_read (int regnum, gdb_byte *buf)
524 gdb_assert (buf != NULL);
527 if (m_register_status[regnum] != REG_VALID)
528 memset (buf, 0, m_descr->sizeof_register[regnum]);
530 memcpy (buf, register_buffer (regnum),
531 m_descr->sizeof_register[regnum]);
533 return m_register_status[regnum];
537 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
539 gdb_assert (regcache != NULL);
540 return regcache->raw_read (regnum, val);
543 template<typename T, typename>
545 readable_regcache::raw_read (int regnum, T *val)
548 enum register_status status;
550 assert_regnum (regnum);
551 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
552 status = raw_read (regnum, buf);
553 if (status == REG_VALID)
554 *val = extract_integer<T> (buf,
555 m_descr->sizeof_register[regnum],
556 gdbarch_byte_order (m_descr->gdbarch));
563 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
566 gdb_assert (regcache != NULL);
567 return regcache->raw_read (regnum, val);
571 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
573 gdb_assert (regcache != NULL);
574 regcache->raw_write (regnum, val);
577 template<typename T, typename>
579 regcache::raw_write (int regnum, T val)
583 assert_regnum (regnum);
584 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
585 store_integer (buf, m_descr->sizeof_register[regnum],
586 gdbarch_byte_order (m_descr->gdbarch), val);
587 raw_write (regnum, buf);
591 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
594 gdb_assert (regcache != NULL);
595 regcache->raw_write (regnum, val);
599 regcache_raw_get_signed (struct regcache *regcache, int regnum)
602 enum register_status status;
604 status = regcache_raw_read_signed (regcache, regnum, &value);
605 if (status == REG_UNAVAILABLE)
606 throw_error (NOT_AVAILABLE_ERROR,
607 _("Register %d is not available"), regnum);
612 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
614 gdb_assert (regnum >= 0);
615 gdb_assert (regnum < m_descr->nr_cooked_registers);
616 if (regnum < num_raw_registers ())
617 return raw_read (regnum, buf);
618 else if (m_has_pseudo
619 && m_register_status[regnum] != REG_UNKNOWN)
621 if (m_register_status[regnum] == REG_VALID)
622 memcpy (buf, register_buffer (regnum),
623 m_descr->sizeof_register[regnum]);
625 memset (buf, 0, m_descr->sizeof_register[regnum]);
627 return m_register_status[regnum];
629 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
631 struct value *mark, *computed;
632 enum register_status result = REG_VALID;
634 mark = value_mark ();
636 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
638 if (value_entirely_available (computed))
639 memcpy (buf, value_contents_raw (computed),
640 m_descr->sizeof_register[regnum]);
643 memset (buf, 0, m_descr->sizeof_register[regnum]);
644 result = REG_UNAVAILABLE;
647 value_free_to_mark (mark);
652 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
657 readable_regcache::cooked_read_value (int regnum)
659 gdb_assert (regnum >= 0);
660 gdb_assert (regnum < m_descr->nr_cooked_registers);
662 if (regnum < num_raw_registers ()
663 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
664 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
666 struct value *result;
668 result = allocate_value (register_type (m_descr->gdbarch, regnum));
669 VALUE_LVAL (result) = lval_register;
670 VALUE_REGNUM (result) = regnum;
672 /* It is more efficient in general to do this delegation in this
673 direction than in the other one, even though the value-based
675 if (cooked_read (regnum,
676 value_contents_raw (result)) == REG_UNAVAILABLE)
677 mark_value_bytes_unavailable (result, 0,
678 TYPE_LENGTH (value_type (result)));
683 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
688 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
691 gdb_assert (regcache != NULL);
692 return regcache->cooked_read (regnum, val);
695 template<typename T, typename>
697 readable_regcache::cooked_read (int regnum, T *val)
699 enum register_status status;
702 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
703 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
704 status = cooked_read (regnum, buf);
705 if (status == REG_VALID)
706 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
707 gdbarch_byte_order (m_descr->gdbarch));
714 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
717 gdb_assert (regcache != NULL);
718 return regcache->cooked_read (regnum, val);
722 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
725 gdb_assert (regcache != NULL);
726 regcache->cooked_write (regnum, val);
729 template<typename T, typename>
731 regcache::cooked_write (int regnum, T val)
735 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
736 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
737 store_integer (buf, m_descr->sizeof_register[regnum],
738 gdbarch_byte_order (m_descr->gdbarch), val);
739 cooked_write (regnum, buf);
743 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
746 gdb_assert (regcache != NULL);
747 regcache->cooked_write (regnum, val);
751 regcache::raw_write (int regnum, const gdb_byte *buf)
754 gdb_assert (buf != NULL);
755 assert_regnum (regnum);
757 /* On the sparc, writing %g0 is a no-op, so we don't even want to
758 change the registers array if something writes to this register. */
759 if (gdbarch_cannot_store_register (arch (), regnum))
762 /* If we have a valid copy of the register, and new value == old
763 value, then don't bother doing the actual store. */
764 if (get_register_status (regnum) == REG_VALID
765 && (memcmp (register_buffer (regnum), buf,
766 m_descr->sizeof_register[regnum]) == 0))
769 target_prepare_to_store (this);
770 raw_supply (regnum, buf);
772 /* Invalidate the register after it is written, in case of a
774 regcache_invalidator invalidator (this, regnum);
776 target_store_registers (this, regnum);
778 /* The target did not throw an error so we can discard invalidating
780 invalidator.release ();
784 regcache::cooked_write (int regnum, const gdb_byte *buf)
786 gdb_assert (regnum >= 0);
787 gdb_assert (regnum < m_descr->nr_cooked_registers);
788 if (regnum < num_raw_registers ())
789 raw_write (regnum, buf);
791 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
795 /* See regcache.h. */
798 readable_regcache::read_part (int regnum, int offset, int len,
799 gdb_byte *out, bool is_raw)
801 int reg_size = register_size (arch (), regnum);
803 gdb_assert (out != NULL);
804 gdb_assert (offset >= 0 && offset <= reg_size);
805 gdb_assert (len >= 0 && offset + len <= reg_size);
807 if (offset == 0 && len == 0)
813 if (offset == 0 && len == reg_size)
815 /* Read the full register. */
816 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
819 enum register_status status;
820 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
822 /* Read full register to buffer. */
823 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
824 if (status != REG_VALID)
828 memcpy (out, reg + offset, len);
832 /* See regcache.h. */
835 reg_buffer::raw_collect_part (int regnum, int offset, int len,
838 int reg_size = register_size (arch (), regnum);
840 gdb_assert (out != nullptr);
841 gdb_assert (offset >= 0 && offset <= reg_size);
842 gdb_assert (len >= 0 && offset + len <= reg_size);
844 if (offset == 0 && len == 0)
850 if (offset == 0 && len == reg_size)
852 /* Collect the full register. */
853 return raw_collect (regnum, out);
856 /* Read to buffer, then write out. */
857 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
858 raw_collect (regnum, reg);
859 memcpy (out, reg + offset, len);
862 /* See regcache.h. */
865 regcache::write_part (int regnum, int offset, int len,
866 const gdb_byte *in, bool is_raw)
868 int reg_size = register_size (arch (), regnum);
870 gdb_assert (in != NULL);
871 gdb_assert (offset >= 0 && offset <= reg_size);
872 gdb_assert (len >= 0 && offset + len <= reg_size);
874 if (offset == 0 && len == 0)
880 if (offset == 0 && len == reg_size)
882 /* Write the full register. */
883 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
887 enum register_status status;
888 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
890 /* Read existing register to buffer. */
891 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
892 if (status != REG_VALID)
895 /* Update buffer, then write back to regcache. */
896 memcpy (reg + offset, in, len);
897 is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
901 /* See regcache.h. */
904 reg_buffer::raw_supply_part (int regnum, int offset, int len,
907 int reg_size = register_size (arch (), regnum);
909 gdb_assert (in != nullptr);
910 gdb_assert (offset >= 0 && offset <= reg_size);
911 gdb_assert (len >= 0 && offset + len <= reg_size);
913 if (offset == 0 && len == 0)
919 if (offset == 0 && len == reg_size)
921 /* Supply the full register. */
922 return raw_supply (regnum, in);
925 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
927 /* Read existing value to buffer. */
928 raw_collect (regnum, reg);
930 /* Write to buffer, then write out. */
931 memcpy (reg + offset, in, len);
932 raw_supply (regnum, reg);
936 readable_regcache::raw_read_part (int regnum, int offset, int len,
939 assert_regnum (regnum);
940 return read_part (regnum, offset, len, buf, true);
943 /* See regcache.h. */
946 regcache::raw_write_part (int regnum, int offset, int len,
949 assert_regnum (regnum);
950 write_part (regnum, offset, len, buf, true);
953 /* See regcache.h. */
956 readable_regcache::cooked_read_part (int regnum, int offset, int len,
959 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
960 return read_part (regnum, offset, len, buf, false);
963 /* See regcache.h. */
966 regcache::cooked_write_part (int regnum, int offset, int len,
969 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
970 write_part (regnum, offset, len, buf, false);
973 /* See common/common-regcache.h. */
976 reg_buffer::raw_supply (int regnum, const void *buf)
981 assert_regnum (regnum);
983 regbuf = register_buffer (regnum);
984 size = m_descr->sizeof_register[regnum];
988 memcpy (regbuf, buf, size);
989 m_register_status[regnum] = REG_VALID;
993 /* This memset not strictly necessary, but better than garbage
994 in case the register value manages to escape somewhere (due
995 to a bug, no less). */
996 memset (regbuf, 0, size);
997 m_register_status[regnum] = REG_UNAVAILABLE;
1001 /* See regcache.h. */
1004 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1005 int addr_len, bool is_signed)
1007 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1011 assert_regnum (regnum);
1013 regbuf = register_buffer (regnum);
1014 regsize = m_descr->sizeof_register[regnum];
1016 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1018 m_register_status[regnum] = REG_VALID;
1021 /* See regcache.h. */
1024 reg_buffer::raw_supply_zeroed (int regnum)
1029 assert_regnum (regnum);
1031 regbuf = register_buffer (regnum);
1032 size = m_descr->sizeof_register[regnum];
1034 memset (regbuf, 0, size);
1035 m_register_status[regnum] = REG_VALID;
1038 /* See common/common-regcache.h. */
1041 reg_buffer::raw_collect (int regnum, void *buf) const
1046 gdb_assert (buf != NULL);
1047 assert_regnum (regnum);
1049 regbuf = register_buffer (regnum);
1050 size = m_descr->sizeof_register[regnum];
1051 memcpy (buf, regbuf, size);
1054 /* See regcache.h. */
1057 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1058 bool is_signed) const
1060 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1061 const gdb_byte *regbuf;
1064 assert_regnum (regnum);
1066 regbuf = register_buffer (regnum);
1067 regsize = m_descr->sizeof_register[regnum];
1069 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1073 /* See regcache.h. */
1076 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1077 const gdb_byte *in_buf, gdb_byte *out_buf,
1078 int slot_size, int offs) const
1080 struct gdbarch *gdbarch = arch ();
1081 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1083 /* Use part versions and reg_size to prevent possible buffer overflows when
1084 accessing the regcache. */
1086 if (out_buf != nullptr)
1088 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1090 /* Ensure any additional space is cleared. */
1091 if (slot_size > reg_size)
1092 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1094 else if (in_buf != nullptr)
1095 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1098 /* Invalidate the register. */
1099 out_regcache->raw_supply (regnum, nullptr);
1103 /* See regcache.h. */
1106 regcache::transfer_regset (const struct regset *regset,
1107 struct regcache *out_regcache,
1108 int regnum, const gdb_byte *in_buf,
1109 gdb_byte *out_buf, size_t size) const
1111 const struct regcache_map_entry *map;
1112 int offs = 0, count;
1114 for (map = (const struct regcache_map_entry *) regset->regmap;
1115 (count = map->count) != 0;
1118 int regno = map->regno;
1119 int slot_size = map->size;
1121 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1122 slot_size = m_descr->sizeof_register[regno];
1124 if (regno == REGCACHE_MAP_SKIP
1126 && (regnum < regno || regnum >= regno + count)))
1127 offs += count * slot_size;
1129 else if (regnum == -1)
1130 for (; count--; regno++, offs += slot_size)
1132 if (offs + slot_size > size)
1135 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1140 /* Transfer a single register and return. */
1141 offs += (regnum - regno) * slot_size;
1142 if (offs + slot_size > size)
1145 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1152 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1153 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1154 If BUF is NULL, set the register(s) to "unavailable" status. */
1157 regcache_supply_regset (const struct regset *regset,
1158 struct regcache *regcache,
1159 int regnum, const void *buf, size_t size)
1161 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1165 regcache::supply_regset (const struct regset *regset,
1166 int regnum, const void *buf, size_t size)
1168 transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
1171 /* Collect register REGNUM from REGCACHE to BUF, using the register
1172 map in REGSET. If REGNUM is -1, do this for all registers in
1176 regcache_collect_regset (const struct regset *regset,
1177 const struct regcache *regcache,
1178 int regnum, void *buf, size_t size)
1180 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1184 regcache::collect_regset (const struct regset *regset,
1185 int regnum, void *buf, size_t size) const
1187 transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1190 /* See common/common-regcache.h. */
1193 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1195 gdb_assert (buf != NULL);
1196 assert_regnum (regnum);
1198 const char *regbuf = (const char *) register_buffer (regnum);
1199 size_t size = m_descr->sizeof_register[regnum];
1200 gdb_assert (size >= offset);
1202 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1205 /* Special handling for register PC. */
1208 regcache_read_pc (struct regcache *regcache)
1210 struct gdbarch *gdbarch = regcache->arch ();
1214 if (gdbarch_read_pc_p (gdbarch))
1215 pc_val = gdbarch_read_pc (gdbarch, regcache);
1216 /* Else use per-frame method on get_current_frame. */
1217 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1221 if (regcache_cooked_read_unsigned (regcache,
1222 gdbarch_pc_regnum (gdbarch),
1223 &raw_val) == REG_UNAVAILABLE)
1224 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1226 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1229 internal_error (__FILE__, __LINE__,
1230 _("regcache_read_pc: Unable to find PC"));
1235 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1237 struct gdbarch *gdbarch = regcache->arch ();
1239 if (gdbarch_write_pc_p (gdbarch))
1240 gdbarch_write_pc (gdbarch, regcache, pc);
1241 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1242 regcache_cooked_write_unsigned (regcache,
1243 gdbarch_pc_regnum (gdbarch), pc);
1245 internal_error (__FILE__, __LINE__,
1246 _("regcache_write_pc: Unable to update PC"));
1248 /* Writing the PC (for instance, from "load") invalidates the
1250 reinit_frame_cache ();
1254 reg_buffer::num_raw_registers () const
1256 return gdbarch_num_regs (arch ());
1260 regcache::debug_print_register (const char *func, int regno)
1262 struct gdbarch *gdbarch = arch ();
1264 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1265 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1266 && gdbarch_register_name (gdbarch, regno) != NULL
1267 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1268 fprintf_unfiltered (gdb_stdlog, "(%s)",
1269 gdbarch_register_name (gdbarch, regno));
1271 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1272 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1274 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1275 int size = register_size (gdbarch, regno);
1276 gdb_byte *buf = register_buffer (regno);
1278 fprintf_unfiltered (gdb_stdlog, " = ");
1279 for (int i = 0; i < size; i++)
1281 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1283 if (size <= sizeof (LONGEST))
1285 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1287 fprintf_unfiltered (gdb_stdlog, " %s %s",
1288 core_addr_to_string_nz (val), plongest (val));
1291 fprintf_unfiltered (gdb_stdlog, "\n");
1295 reg_flush_command (const char *command, int from_tty)
1297 /* Force-flush the register cache. */
1298 registers_changed ();
1300 printf_filtered (_("Register cache flushed.\n"));
1304 register_dump::dump (ui_file *file)
1306 auto descr = regcache_descr (m_gdbarch);
1308 int footnote_nr = 0;
1309 int footnote_register_offset = 0;
1310 int footnote_register_type_name_null = 0;
1311 long register_offset = 0;
1313 gdb_assert (descr->nr_cooked_registers
1314 == (gdbarch_num_regs (m_gdbarch)
1315 + gdbarch_num_pseudo_regs (m_gdbarch)));
1317 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1321 fprintf_unfiltered (file, " %-10s", "Name");
1324 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1328 else if (p[0] == '\0')
1330 fprintf_unfiltered (file, " %-10s", p);
1335 fprintf_unfiltered (file, " %4s", "Nr");
1337 fprintf_unfiltered (file, " %4d", regnum);
1339 /* Relative number. */
1341 fprintf_unfiltered (file, " %4s", "Rel");
1342 else if (regnum < gdbarch_num_regs (m_gdbarch))
1343 fprintf_unfiltered (file, " %4d", regnum);
1345 fprintf_unfiltered (file, " %4d",
1346 (regnum - gdbarch_num_regs (m_gdbarch)));
1350 fprintf_unfiltered (file, " %6s ", "Offset");
1353 fprintf_unfiltered (file, " %6ld",
1354 descr->register_offset[regnum]);
1355 if (register_offset != descr->register_offset[regnum]
1357 && (descr->register_offset[regnum]
1358 != (descr->register_offset[regnum - 1]
1359 + descr->sizeof_register[regnum - 1])))
1362 if (!footnote_register_offset)
1363 footnote_register_offset = ++footnote_nr;
1364 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1367 fprintf_unfiltered (file, " ");
1368 register_offset = (descr->register_offset[regnum]
1369 + descr->sizeof_register[regnum]);
1374 fprintf_unfiltered (file, " %5s ", "Size");
1376 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1381 std::string name_holder;
1387 static const char blt[] = "builtin_type";
1389 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1392 if (!footnote_register_type_name_null)
1393 footnote_register_type_name_null = ++footnote_nr;
1394 name_holder = string_printf ("*%d",
1395 footnote_register_type_name_null);
1396 t = name_holder.c_str ();
1398 /* Chop a leading builtin_type. */
1399 if (startswith (t, blt))
1402 fprintf_unfiltered (file, " %-15s", t);
1405 /* Leading space always present. */
1406 fprintf_unfiltered (file, " ");
1408 dump_reg (file, regnum);
1410 fprintf_unfiltered (file, "\n");
1413 if (footnote_register_offset)
1414 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1415 footnote_register_offset);
1416 if (footnote_register_type_name_null)
1417 fprintf_unfiltered (file,
1418 "*%d: Register type's name NULL.\n",
1419 footnote_register_type_name_null);
1423 #include "selftest.h"
1424 #include "selftest-arch.h"
1425 #include "gdbthread.h"
1426 #include "target-float.h"
1428 namespace selftests {
1430 class regcache_access : public regcache
1434 /* Return the number of elements in current_regcache. */
1437 current_regcache_size ()
1439 return std::distance (regcache::current_regcache.begin (),
1440 regcache::current_regcache.end ());
1445 current_regcache_test (void)
1447 /* It is empty at the start. */
1448 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1450 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1452 /* Get regcache from ptid1, a new regcache is added to
1453 current_regcache. */
1454 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1458 SELF_CHECK (regcache != NULL);
1459 SELF_CHECK (regcache->ptid () == ptid1);
1460 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1462 /* Get regcache from ptid2, a new regcache is added to
1463 current_regcache. */
1464 regcache = get_thread_arch_aspace_regcache (ptid2,
1467 SELF_CHECK (regcache != NULL);
1468 SELF_CHECK (regcache->ptid () == ptid2);
1469 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1471 /* Get regcache from ptid3, a new regcache is added to
1472 current_regcache. */
1473 regcache = get_thread_arch_aspace_regcache (ptid3,
1476 SELF_CHECK (regcache != NULL);
1477 SELF_CHECK (regcache->ptid () == ptid3);
1478 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1480 /* Get regcache from ptid2 again, nothing is added to
1481 current_regcache. */
1482 regcache = get_thread_arch_aspace_regcache (ptid2,
1485 SELF_CHECK (regcache != NULL);
1486 SELF_CHECK (regcache->ptid () == ptid2);
1487 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1489 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1490 current_regcache. */
1491 registers_changed_ptid (ptid2);
1492 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1495 class target_ops_no_register : public test_target_ops
1498 target_ops_no_register ()
1499 : test_target_ops {}
1504 fetch_registers_called = 0;
1505 store_registers_called = 0;
1506 xfer_partial_called = 0;
1509 void fetch_registers (regcache *regs, int regno) override;
1510 void store_registers (regcache *regs, int regno) override;
1512 enum target_xfer_status xfer_partial (enum target_object object,
1513 const char *annex, gdb_byte *readbuf,
1514 const gdb_byte *writebuf,
1515 ULONGEST offset, ULONGEST len,
1516 ULONGEST *xfered_len) override;
1518 unsigned int fetch_registers_called = 0;
1519 unsigned int store_registers_called = 0;
1520 unsigned int xfer_partial_called = 0;
1524 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1526 /* Mark register available. */
1527 regs->raw_supply_zeroed (regno);
1528 this->fetch_registers_called++;
1532 target_ops_no_register::store_registers (regcache *regs, int regno)
1534 this->store_registers_called++;
1537 enum target_xfer_status
1538 target_ops_no_register::xfer_partial (enum target_object object,
1539 const char *annex, gdb_byte *readbuf,
1540 const gdb_byte *writebuf,
1541 ULONGEST offset, ULONGEST len,
1542 ULONGEST *xfered_len)
1544 this->xfer_partial_called++;
1547 return TARGET_XFER_OK;
1550 class readwrite_regcache : public regcache
1553 readwrite_regcache (struct gdbarch *gdbarch)
1554 : regcache (gdbarch, nullptr)
1558 /* Test regcache::cooked_read gets registers from raw registers and
1559 memory instead of target to_{fetch,store}_registers. */
1562 cooked_read_test (struct gdbarch *gdbarch)
1564 /* Error out if debugging something, because we're going to push the
1565 test target, which would pop any existing target. */
1566 if (current_top_target ()->to_stratum >= process_stratum)
1567 error (_("target already pushed"));
1569 /* Create a mock environment. An inferior with a thread, with a
1570 process_stratum target pushed. */
1572 target_ops_no_register mock_target;
1573 ptid_t mock_ptid (1, 1);
1574 inferior mock_inferior (mock_ptid.pid ());
1575 address_space mock_aspace {};
1576 mock_inferior.gdbarch = gdbarch;
1577 mock_inferior.aspace = &mock_aspace;
1578 thread_info mock_thread (&mock_inferior, mock_ptid);
1580 scoped_restore restore_thread_list
1581 = make_scoped_restore (&thread_list, &mock_thread);
1583 /* Add the mock inferior to the inferior list so that look ups by
1584 target+ptid can find it. */
1585 scoped_restore restore_inferior_list
1586 = make_scoped_restore (&inferior_list);
1587 inferior_list = &mock_inferior;
1589 /* Switch to the mock inferior. */
1590 scoped_restore_current_inferior restore_current_inferior;
1591 set_current_inferior (&mock_inferior);
1593 /* Push the process_stratum target so we can mock accessing
1595 push_target (&mock_target);
1597 /* Pop it again on exit (return/exception). */
1602 pop_all_targets_at_and_above (process_stratum);
1606 /* Switch to the mock thread. */
1607 scoped_restore restore_inferior_ptid
1608 = make_scoped_restore (&inferior_ptid, mock_ptid);
1610 /* Test that read one raw register from regcache_no_target will go
1611 to the target layer. */
1613 /* Find a raw register which size isn't zero. */
1615 for (nonzero_regnum = 0;
1616 nonzero_regnum < gdbarch_num_regs (gdbarch);
1619 if (register_size (gdbarch, nonzero_regnum) != 0)
1623 readwrite_regcache readwrite (gdbarch);
1624 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1626 readwrite.raw_read (nonzero_regnum, buf.data ());
1628 /* raw_read calls target_fetch_registers. */
1629 SELF_CHECK (mock_target.fetch_registers_called > 0);
1630 mock_target.reset ();
1632 /* Mark all raw registers valid, so the following raw registers
1633 accesses won't go to target. */
1634 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1635 readwrite.raw_update (i);
1637 mock_target.reset ();
1638 /* Then, read all raw and pseudo registers, and don't expect calling
1639 to_{fetch,store}_registers. */
1640 for (int regnum = 0;
1641 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1644 if (register_size (gdbarch, regnum) == 0)
1647 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1649 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1650 inner_buf.data ()));
1652 SELF_CHECK (mock_target.fetch_registers_called == 0);
1653 SELF_CHECK (mock_target.store_registers_called == 0);
1655 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1656 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1657 SELF_CHECK (mock_target.xfer_partial_called == 0);
1659 mock_target.reset ();
1662 readonly_detached_regcache readonly (readwrite);
1664 /* GDB may go to target layer to fetch all registers and memory for
1665 readonly regcache. */
1666 mock_target.reset ();
1668 for (int regnum = 0;
1669 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1672 if (register_size (gdbarch, regnum) == 0)
1675 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1676 enum register_status status = readonly.cooked_read (regnum,
1679 if (regnum < gdbarch_num_regs (gdbarch))
1681 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1683 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1684 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1685 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1686 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1687 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1688 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1689 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1690 || bfd_arch == bfd_arch_riscv)
1692 /* Raw registers. If raw registers are not in save_reggroup,
1693 their status are unknown. */
1694 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1695 SELF_CHECK (status == REG_VALID);
1697 SELF_CHECK (status == REG_UNKNOWN);
1700 SELF_CHECK (status == REG_VALID);
1704 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1705 SELF_CHECK (status == REG_VALID);
1708 /* If pseudo registers are not in save_reggroup, some of
1709 them can be computed from saved raw registers, but some
1710 of them are unknown. */
1711 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1713 if (bfd_arch == bfd_arch_frv
1714 || bfd_arch == bfd_arch_m32c
1715 || bfd_arch == bfd_arch_mep
1716 || bfd_arch == bfd_arch_sh)
1717 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1718 else if (bfd_arch == bfd_arch_mips
1719 || bfd_arch == bfd_arch_h8300)
1720 SELF_CHECK (status == REG_UNKNOWN);
1722 SELF_CHECK (status == REG_VALID);
1726 SELF_CHECK (mock_target.fetch_registers_called == 0);
1727 SELF_CHECK (mock_target.store_registers_called == 0);
1728 SELF_CHECK (mock_target.xfer_partial_called == 0);
1730 mock_target.reset ();
1734 /* Test regcache::cooked_write by writing some expected contents to
1735 registers, and checking that contents read from registers and the
1736 expected contents are the same. */
1739 cooked_write_test (struct gdbarch *gdbarch)
1741 /* Error out if debugging something, because we're going to push the
1742 test target, which would pop any existing target. */
1743 if (current_top_target ()->to_stratum >= process_stratum)
1744 error (_("target already pushed"));
1746 /* Create a mock environment. A process_stratum target pushed. */
1748 target_ops_no_register mock_target;
1750 /* Push the process_stratum target so we can mock accessing
1752 push_target (&mock_target);
1754 /* Pop it again on exit (return/exception). */
1759 pop_all_targets_at_and_above (process_stratum);
1763 readwrite_regcache readwrite (gdbarch);
1765 const int num_regs = (gdbarch_num_regs (gdbarch)
1766 + gdbarch_num_pseudo_regs (gdbarch));
1768 for (auto regnum = 0; regnum < num_regs; regnum++)
1770 if (register_size (gdbarch, regnum) == 0
1771 || gdbarch_cannot_store_register (gdbarch, regnum))
1774 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1776 if ((bfd_arch == bfd_arch_sparc
1777 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1778 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1779 && gdbarch_ptr_bit (gdbarch) == 64
1780 && (regnum >= gdbarch_num_regs (gdbarch)
1781 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1782 || (bfd_arch == bfd_arch_spu
1783 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1784 TARGET_OBJECT_SPU. */
1785 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
1788 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1789 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1790 const auto type = register_type (gdbarch, regnum);
1792 if (TYPE_CODE (type) == TYPE_CODE_FLT
1793 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1795 /* Generate valid float format. */
1796 target_float_from_string (expected.data (), type, "1.25");
1798 else if (TYPE_CODE (type) == TYPE_CODE_INT
1799 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1800 || TYPE_CODE (type) == TYPE_CODE_PTR
1801 || TYPE_CODE (type) == TYPE_CODE_UNION
1802 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
1804 if (bfd_arch == bfd_arch_ia64
1805 || (regnum >= gdbarch_num_regs (gdbarch)
1806 && (bfd_arch == bfd_arch_xtensa
1807 || bfd_arch == bfd_arch_bfin
1808 || bfd_arch == bfd_arch_m32c
1809 /* m68hc11 pseudo registers are in memory. */
1810 || bfd_arch == bfd_arch_m68hc11
1811 || bfd_arch == bfd_arch_m68hc12
1812 || bfd_arch == bfd_arch_s390))
1813 || (bfd_arch == bfd_arch_frv
1814 /* FRV pseudo registers except iacc0. */
1815 && regnum > gdbarch_num_regs (gdbarch)))
1817 /* Skip setting the expected values for some architecture
1820 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1822 /* RL78_PC_REGNUM */
1823 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1828 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1832 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
1834 /* No idea how to test flags. */
1839 /* If we don't know how to create the expected value for the
1840 this type, make it fail. */
1844 readwrite.cooked_write (regnum, expected.data ());
1846 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1847 SELF_CHECK (expected == buf);
1851 } // namespace selftests
1852 #endif /* GDB_SELF_TEST */
1855 _initialize_regcache (void)
1857 regcache_descr_handle
1858 = gdbarch_data_register_post_init (init_regcache_descr);
1860 gdb::observers::target_changed.attach (regcache_observer_target_changed);
1861 gdb::observers::thread_ptid_changed.attach
1862 (regcache::regcache_thread_ptid_changed);
1864 add_com ("flushregs", class_maintenance, reg_flush_command,
1865 _("Force gdb to flush its register cache (maintainer command)"));
1868 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1870 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1871 selftests::cooked_read_test);
1872 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1873 selftests::cooked_write_test);