1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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"
24 #include "test-target.h"
28 #include "reggroups.h"
29 #include "observable.h"
31 #include <forward_list>
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created. */
42 struct gdbarch_data *regcache_descr_handle;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
54 long sizeof_raw_registers;
56 /* The cooked register space. Each cooked register in the range
57 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
58 register. The remaining [NR_RAW_REGISTERS
59 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
60 both raw registers and memory by the architecture methods
61 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
62 int nr_cooked_registers;
63 long sizeof_cooked_registers;
65 /* Offset and size (in 8 bit bytes), of each register in the
66 register cache. All registers (including those in the range
67 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
69 long *register_offset;
70 long *sizeof_register;
72 /* Cached table containing the type of each register. */
73 struct type **register_type;
77 init_regcache_descr (struct gdbarch *gdbarch)
80 struct regcache_descr *descr;
81 gdb_assert (gdbarch != NULL);
83 /* Create an initial, zero filled, table. */
84 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
85 descr->gdbarch = gdbarch;
87 /* Total size of the register space. The raw registers are mapped
88 directly onto the raw register cache while the pseudo's are
89 either mapped onto raw-registers or memory. */
90 descr->nr_cooked_registers = gdbarch_num_cooked_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 && regnum < gdbarch_num_cooked_regs (gdbarch));
167 size = descr->sizeof_register[regnum];
171 /* See gdbsupport/common-regcache.h. */
174 regcache_register_size (const struct regcache *regcache, int n)
176 return register_size (regcache->arch (), n);
179 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
180 : m_has_pseudo (has_pseudo)
182 gdb_assert (gdbarch != NULL);
183 m_descr = regcache_descr (gdbarch);
187 m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers] ());
188 m_register_status.reset
189 (new register_status[m_descr->nr_cooked_registers] ());
193 m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers] ());
194 m_register_status.reset
195 (new register_status[gdbarch_num_regs (gdbarch)] ());
199 regcache::regcache (gdbarch *gdbarch, const address_space *aspace_)
200 /* The register buffers. A read/write register cache can only hold
201 [0 .. gdbarch_num_regs). */
202 : detached_regcache (gdbarch, false), m_aspace (aspace_)
204 m_ptid = minus_one_ptid;
207 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
208 : readonly_detached_regcache (src.arch (),
209 [&src] (int regnum, gdb_byte *buf)
211 return src.cooked_read (regnum, buf);
217 reg_buffer::arch () const
219 return m_descr->gdbarch;
222 /* Return a pointer to register REGNUM's buffer cache. */
225 reg_buffer::register_buffer (int regnum) const
227 return m_registers.get () + m_descr->register_offset[regnum];
231 reg_buffer::save (register_read_ftype cooked_read)
233 struct gdbarch *gdbarch = m_descr->gdbarch;
236 /* It should have pseudo registers. */
237 gdb_assert (m_has_pseudo);
238 /* Clear the dest. */
239 memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
240 memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
241 /* Copy over any registers (identified by their membership in the
242 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
243 gdbarch_num_pseudo_regs) range is checked since some architectures need
244 to save/restore `cooked' registers that live in memory. */
245 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
247 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
249 gdb_byte *dst_buf = register_buffer (regnum);
250 enum register_status status = cooked_read (regnum, dst_buf);
252 gdb_assert (status != REG_UNKNOWN);
254 if (status != REG_VALID)
255 memset (dst_buf, 0, register_size (gdbarch, regnum));
257 m_register_status[regnum] = status;
263 regcache::restore (readonly_detached_regcache *src)
265 struct gdbarch *gdbarch = m_descr->gdbarch;
268 gdb_assert (src != NULL);
269 gdb_assert (src->m_has_pseudo);
271 gdb_assert (gdbarch == src->arch ());
273 /* Copy over any registers, being careful to only restore those that
274 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
275 + gdbarch_num_pseudo_regs) range is checked since some architectures need
276 to save/restore `cooked' registers that live in memory. */
277 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
279 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
281 if (src->m_register_status[regnum] == REG_VALID)
282 cooked_write (regnum, src->register_buffer (regnum));
287 /* See gdbsupport/common-regcache.h. */
290 reg_buffer::get_register_status (int regnum) const
292 assert_regnum (regnum);
294 return m_register_status[regnum];
298 reg_buffer::invalidate (int regnum)
300 assert_regnum (regnum);
301 m_register_status[regnum] = REG_UNKNOWN;
305 reg_buffer::assert_regnum (int regnum) const
307 gdb_assert (regnum >= 0);
309 gdb_assert (regnum < m_descr->nr_cooked_registers);
311 gdb_assert (regnum < gdbarch_num_regs (arch ()));
314 /* Global structure containing the current regcache. */
316 /* NOTE: this is a write-through cache. There is no "dirty" bit for
317 recording if the register values have been changed (eg. by the
318 user). Therefore all registers must be written back to the
319 target when appropriate. */
320 std::forward_list<regcache *> regcache::current_regcache;
323 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
324 struct address_space *aspace)
326 for (const auto ®cache : regcache::current_regcache)
327 if (regcache->ptid () == ptid && regcache->arch () == gdbarch)
330 regcache *new_regcache = new regcache (gdbarch, aspace);
332 regcache::current_regcache.push_front (new_regcache);
333 new_regcache->set_ptid (ptid);
339 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
341 address_space *aspace = target_thread_address_space (ptid);
343 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
346 static ptid_t current_thread_ptid;
347 static struct gdbarch *current_thread_arch;
350 get_thread_regcache (ptid_t ptid)
352 if (!current_thread_arch || current_thread_ptid != ptid)
354 current_thread_ptid = ptid;
355 current_thread_arch = target_thread_architecture (ptid);
358 return get_thread_arch_regcache (ptid, current_thread_arch);
361 /* See regcache.h. */
364 get_thread_regcache (thread_info *thread)
366 return get_thread_regcache (thread->ptid);
370 get_current_regcache (void)
372 return get_thread_regcache (inferior_thread ());
375 /* See gdbsupport/common-regcache.h. */
378 get_thread_regcache_for_ptid (ptid_t ptid)
380 return get_thread_regcache (ptid);
383 /* Observer for the target_changed event. */
386 regcache_observer_target_changed (struct target_ops *target)
388 registers_changed ();
391 /* Update global variables old ptids to hold NEW_PTID if they were
394 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
396 for (auto ®cache : regcache::current_regcache)
398 if (regcache->ptid () == old_ptid)
399 regcache->set_ptid (new_ptid);
403 /* Low level examining and depositing of registers.
405 The caller is responsible for making sure that the inferior is
406 stopped before calling the fetching routines, or it will get
407 garbage. (a change from GDB version 3, in which the caller got the
408 value from the last stop). */
410 /* REGISTERS_CHANGED ()
412 Indicate that registers may have changed, so invalidate the cache. */
415 registers_changed_ptid (ptid_t ptid)
417 for (auto oit = regcache::current_regcache.before_begin (),
418 it = std::next (oit);
419 it != regcache::current_regcache.end ();
422 if ((*it)->ptid ().matches (ptid))
425 it = regcache::current_regcache.erase_after (oit);
431 if (current_thread_ptid.matches (ptid))
433 current_thread_ptid = null_ptid;
434 current_thread_arch = NULL;
437 if (inferior_ptid.matches (ptid))
439 /* We just deleted the regcache of the current thread. Need to
440 forget about any frames we have cached, too. */
441 reinit_frame_cache ();
445 /* See regcache.h. */
448 registers_changed_thread (thread_info *thread)
450 registers_changed_ptid (thread->ptid);
454 registers_changed (void)
456 registers_changed_ptid (minus_one_ptid);
460 regcache::raw_update (int regnum)
462 assert_regnum (regnum);
464 /* Make certain that the register cache is up-to-date with respect
465 to the current thread. This switching shouldn't be necessary
466 only there is still only one target side register cache. Sigh!
467 On the bright side, at least there is a regcache object. */
469 if (get_register_status (regnum) == REG_UNKNOWN)
471 target_fetch_registers (this, regnum);
473 /* A number of targets can't access the whole set of raw
474 registers (because the debug API provides no means to get at
476 if (m_register_status[regnum] == REG_UNKNOWN)
477 m_register_status[regnum] = REG_UNAVAILABLE;
482 readable_regcache::raw_read (int regnum, gdb_byte *buf)
484 gdb_assert (buf != NULL);
487 if (m_register_status[regnum] != REG_VALID)
488 memset (buf, 0, m_descr->sizeof_register[regnum]);
490 memcpy (buf, register_buffer (regnum),
491 m_descr->sizeof_register[regnum]);
493 return m_register_status[regnum];
497 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
499 gdb_assert (regcache != NULL);
500 return regcache->raw_read (regnum, val);
503 template<typename T, typename>
505 readable_regcache::raw_read (int regnum, T *val)
508 enum register_status status;
510 assert_regnum (regnum);
511 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
512 status = raw_read (regnum, buf);
513 if (status == REG_VALID)
514 *val = extract_integer<T> (buf,
515 m_descr->sizeof_register[regnum],
516 gdbarch_byte_order (m_descr->gdbarch));
523 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
526 gdb_assert (regcache != NULL);
527 return regcache->raw_read (regnum, val);
531 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
533 gdb_assert (regcache != NULL);
534 regcache->raw_write (regnum, val);
537 template<typename T, typename>
539 regcache::raw_write (int regnum, T val)
543 assert_regnum (regnum);
544 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
545 store_integer (buf, m_descr->sizeof_register[regnum],
546 gdbarch_byte_order (m_descr->gdbarch), val);
547 raw_write (regnum, buf);
551 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
554 gdb_assert (regcache != NULL);
555 regcache->raw_write (regnum, val);
559 regcache_raw_get_signed (struct regcache *regcache, int regnum)
562 enum register_status status;
564 status = regcache_raw_read_signed (regcache, regnum, &value);
565 if (status == REG_UNAVAILABLE)
566 throw_error (NOT_AVAILABLE_ERROR,
567 _("Register %d is not available"), regnum);
572 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
574 gdb_assert (regnum >= 0);
575 gdb_assert (regnum < m_descr->nr_cooked_registers);
576 if (regnum < num_raw_registers ())
577 return raw_read (regnum, buf);
578 else if (m_has_pseudo
579 && m_register_status[regnum] != REG_UNKNOWN)
581 if (m_register_status[regnum] == REG_VALID)
582 memcpy (buf, register_buffer (regnum),
583 m_descr->sizeof_register[regnum]);
585 memset (buf, 0, m_descr->sizeof_register[regnum]);
587 return m_register_status[regnum];
589 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
591 struct value *mark, *computed;
592 enum register_status result = REG_VALID;
594 mark = value_mark ();
596 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
598 if (value_entirely_available (computed))
599 memcpy (buf, value_contents_raw (computed),
600 m_descr->sizeof_register[regnum]);
603 memset (buf, 0, m_descr->sizeof_register[regnum]);
604 result = REG_UNAVAILABLE;
607 value_free_to_mark (mark);
612 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
617 readable_regcache::cooked_read_value (int regnum)
619 gdb_assert (regnum >= 0);
620 gdb_assert (regnum < m_descr->nr_cooked_registers);
622 if (regnum < num_raw_registers ()
623 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
624 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
626 struct value *result;
628 result = allocate_value (register_type (m_descr->gdbarch, regnum));
629 VALUE_LVAL (result) = lval_register;
630 VALUE_REGNUM (result) = regnum;
632 /* It is more efficient in general to do this delegation in this
633 direction than in the other one, even though the value-based
635 if (cooked_read (regnum,
636 value_contents_raw (result)) == REG_UNAVAILABLE)
637 mark_value_bytes_unavailable (result, 0,
638 TYPE_LENGTH (value_type (result)));
643 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
648 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
651 gdb_assert (regcache != NULL);
652 return regcache->cooked_read (regnum, val);
655 template<typename T, typename>
657 readable_regcache::cooked_read (int regnum, T *val)
659 enum register_status status;
662 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
663 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
664 status = cooked_read (regnum, buf);
665 if (status == REG_VALID)
666 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
667 gdbarch_byte_order (m_descr->gdbarch));
674 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
677 gdb_assert (regcache != NULL);
678 return regcache->cooked_read (regnum, val);
682 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
685 gdb_assert (regcache != NULL);
686 regcache->cooked_write (regnum, val);
689 template<typename T, typename>
691 regcache::cooked_write (int regnum, T val)
695 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
696 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
697 store_integer (buf, m_descr->sizeof_register[regnum],
698 gdbarch_byte_order (m_descr->gdbarch), val);
699 cooked_write (regnum, buf);
703 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
706 gdb_assert (regcache != NULL);
707 regcache->cooked_write (regnum, val);
711 regcache::raw_write (int regnum, const gdb_byte *buf)
714 gdb_assert (buf != NULL);
715 assert_regnum (regnum);
717 /* On the sparc, writing %g0 is a no-op, so we don't even want to
718 change the registers array if something writes to this register. */
719 if (gdbarch_cannot_store_register (arch (), regnum))
722 /* If we have a valid copy of the register, and new value == old
723 value, then don't bother doing the actual store. */
724 if (get_register_status (regnum) == REG_VALID
725 && (memcmp (register_buffer (regnum), buf,
726 m_descr->sizeof_register[regnum]) == 0))
729 target_prepare_to_store (this);
730 raw_supply (regnum, buf);
732 /* Invalidate the register after it is written, in case of a
735 = make_scope_exit ([&] { this->invalidate (regnum); });
737 target_store_registers (this, regnum);
739 /* The target did not throw an error so we can discard invalidating
741 invalidator.release ();
745 regcache::cooked_write (int regnum, const gdb_byte *buf)
747 gdb_assert (regnum >= 0);
748 gdb_assert (regnum < m_descr->nr_cooked_registers);
749 if (regnum < num_raw_registers ())
750 raw_write (regnum, buf);
752 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
756 /* See regcache.h. */
759 readable_regcache::read_part (int regnum, int offset, int len,
760 gdb_byte *out, bool is_raw)
762 int reg_size = register_size (arch (), regnum);
764 gdb_assert (out != NULL);
765 gdb_assert (offset >= 0 && offset <= reg_size);
766 gdb_assert (len >= 0 && offset + len <= reg_size);
768 if (offset == 0 && len == 0)
774 if (offset == 0 && len == reg_size)
776 /* Read the full register. */
777 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
780 enum register_status status;
781 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
783 /* Read full register to buffer. */
784 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
785 if (status != REG_VALID)
789 memcpy (out, reg + offset, len);
793 /* See regcache.h. */
796 reg_buffer::raw_collect_part (int regnum, int offset, int len,
799 int reg_size = register_size (arch (), regnum);
801 gdb_assert (out != nullptr);
802 gdb_assert (offset >= 0 && offset <= reg_size);
803 gdb_assert (len >= 0 && offset + len <= reg_size);
805 if (offset == 0 && len == 0)
811 if (offset == 0 && len == reg_size)
813 /* Collect the full register. */
814 return raw_collect (regnum, out);
817 /* Read to buffer, then write out. */
818 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
819 raw_collect (regnum, reg);
820 memcpy (out, reg + offset, len);
823 /* See regcache.h. */
826 regcache::write_part (int regnum, int offset, int len,
827 const gdb_byte *in, bool is_raw)
829 int reg_size = register_size (arch (), regnum);
831 gdb_assert (in != NULL);
832 gdb_assert (offset >= 0 && offset <= reg_size);
833 gdb_assert (len >= 0 && offset + len <= reg_size);
835 if (offset == 0 && len == 0)
841 if (offset == 0 && len == reg_size)
843 /* Write the full register. */
844 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
848 enum register_status status;
849 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
851 /* Read existing register to buffer. */
852 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
853 if (status != REG_VALID)
856 /* Update buffer, then write back to regcache. */
857 memcpy (reg + offset, in, len);
858 is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
862 /* See regcache.h. */
865 reg_buffer::raw_supply_part (int regnum, int offset, int len,
868 int reg_size = register_size (arch (), regnum);
870 gdb_assert (in != nullptr);
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 /* Supply the full register. */
883 return raw_supply (regnum, in);
886 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
888 /* Read existing value to buffer. */
889 raw_collect (regnum, reg);
891 /* Write to buffer, then write out. */
892 memcpy (reg + offset, in, len);
893 raw_supply (regnum, reg);
897 readable_regcache::raw_read_part (int regnum, int offset, int len,
900 assert_regnum (regnum);
901 return read_part (regnum, offset, len, buf, true);
904 /* See regcache.h. */
907 regcache::raw_write_part (int regnum, int offset, int len,
910 assert_regnum (regnum);
911 write_part (regnum, offset, len, buf, true);
914 /* See regcache.h. */
917 readable_regcache::cooked_read_part (int regnum, int offset, int len,
920 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
921 return read_part (regnum, offset, len, buf, false);
924 /* See regcache.h. */
927 regcache::cooked_write_part (int regnum, int offset, int len,
930 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
931 write_part (regnum, offset, len, buf, false);
934 /* See gdbsupport/common-regcache.h. */
937 reg_buffer::raw_supply (int regnum, const void *buf)
942 assert_regnum (regnum);
944 regbuf = register_buffer (regnum);
945 size = m_descr->sizeof_register[regnum];
949 memcpy (regbuf, buf, size);
950 m_register_status[regnum] = REG_VALID;
954 /* This memset not strictly necessary, but better than garbage
955 in case the register value manages to escape somewhere (due
956 to a bug, no less). */
957 memset (regbuf, 0, size);
958 m_register_status[regnum] = REG_UNAVAILABLE;
962 /* See regcache.h. */
965 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
966 int addr_len, bool is_signed)
968 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
972 assert_regnum (regnum);
974 regbuf = register_buffer (regnum);
975 regsize = m_descr->sizeof_register[regnum];
977 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
979 m_register_status[regnum] = REG_VALID;
982 /* See regcache.h. */
985 reg_buffer::raw_supply_zeroed (int regnum)
990 assert_regnum (regnum);
992 regbuf = register_buffer (regnum);
993 size = m_descr->sizeof_register[regnum];
995 memset (regbuf, 0, size);
996 m_register_status[regnum] = REG_VALID;
999 /* See gdbsupport/common-regcache.h. */
1002 reg_buffer::raw_collect (int regnum, void *buf) const
1007 gdb_assert (buf != NULL);
1008 assert_regnum (regnum);
1010 regbuf = register_buffer (regnum);
1011 size = m_descr->sizeof_register[regnum];
1012 memcpy (buf, regbuf, size);
1015 /* See regcache.h. */
1018 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1019 bool is_signed) const
1021 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1022 const gdb_byte *regbuf;
1025 assert_regnum (regnum);
1027 regbuf = register_buffer (regnum);
1028 regsize = m_descr->sizeof_register[regnum];
1030 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1034 /* See regcache.h. */
1037 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1038 const gdb_byte *in_buf, gdb_byte *out_buf,
1039 int slot_size, int offs) const
1041 struct gdbarch *gdbarch = arch ();
1042 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1044 /* Use part versions and reg_size to prevent possible buffer overflows when
1045 accessing the regcache. */
1047 if (out_buf != nullptr)
1049 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1051 /* Ensure any additional space is cleared. */
1052 if (slot_size > reg_size)
1053 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1055 else if (in_buf != nullptr)
1056 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1059 /* Invalidate the register. */
1060 out_regcache->raw_supply (regnum, nullptr);
1064 /* See regcache.h. */
1067 regcache::transfer_regset (const struct regset *regset,
1068 struct regcache *out_regcache,
1069 int regnum, const gdb_byte *in_buf,
1070 gdb_byte *out_buf, size_t size) const
1072 const struct regcache_map_entry *map;
1073 int offs = 0, count;
1075 for (map = (const struct regcache_map_entry *) regset->regmap;
1076 (count = map->count) != 0;
1079 int regno = map->regno;
1080 int slot_size = map->size;
1082 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1083 slot_size = m_descr->sizeof_register[regno];
1085 if (regno == REGCACHE_MAP_SKIP
1087 && (regnum < regno || regnum >= regno + count)))
1088 offs += count * slot_size;
1090 else if (regnum == -1)
1091 for (; count--; regno++, offs += slot_size)
1093 if (offs + slot_size > size)
1096 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1101 /* Transfer a single register and return. */
1102 offs += (regnum - regno) * slot_size;
1103 if (offs + slot_size > size)
1106 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1113 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1114 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1115 If BUF is NULL, set the register(s) to "unavailable" status. */
1118 regcache_supply_regset (const struct regset *regset,
1119 struct regcache *regcache,
1120 int regnum, const void *buf, size_t size)
1122 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1126 regcache::supply_regset (const struct regset *regset,
1127 int regnum, const void *buf, size_t size)
1129 transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
1132 /* Collect register REGNUM from REGCACHE to BUF, using the register
1133 map in REGSET. If REGNUM is -1, do this for all registers in
1137 regcache_collect_regset (const struct regset *regset,
1138 const struct regcache *regcache,
1139 int regnum, void *buf, size_t size)
1141 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1145 regcache::collect_regset (const struct regset *regset,
1146 int regnum, void *buf, size_t size) const
1148 transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1151 /* See gdbsupport/common-regcache.h. */
1154 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1156 gdb_assert (buf != NULL);
1157 assert_regnum (regnum);
1159 const char *regbuf = (const char *) register_buffer (regnum);
1160 size_t size = m_descr->sizeof_register[regnum];
1161 gdb_assert (size >= offset);
1163 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1166 /* Special handling for register PC. */
1169 regcache_read_pc (struct regcache *regcache)
1171 struct gdbarch *gdbarch = regcache->arch ();
1175 if (gdbarch_read_pc_p (gdbarch))
1176 pc_val = gdbarch_read_pc (gdbarch, regcache);
1177 /* Else use per-frame method on get_current_frame. */
1178 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1182 if (regcache_cooked_read_unsigned (regcache,
1183 gdbarch_pc_regnum (gdbarch),
1184 &raw_val) == REG_UNAVAILABLE)
1185 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1187 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1190 internal_error (__FILE__, __LINE__,
1191 _("regcache_read_pc: Unable to find PC"));
1196 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1198 struct gdbarch *gdbarch = regcache->arch ();
1200 if (gdbarch_write_pc_p (gdbarch))
1201 gdbarch_write_pc (gdbarch, regcache, pc);
1202 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1203 regcache_cooked_write_unsigned (regcache,
1204 gdbarch_pc_regnum (gdbarch), pc);
1206 internal_error (__FILE__, __LINE__,
1207 _("regcache_write_pc: Unable to update PC"));
1209 /* Writing the PC (for instance, from "load") invalidates the
1211 reinit_frame_cache ();
1215 reg_buffer::num_raw_registers () const
1217 return gdbarch_num_regs (arch ());
1221 regcache::debug_print_register (const char *func, int regno)
1223 struct gdbarch *gdbarch = arch ();
1225 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1226 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1227 && gdbarch_register_name (gdbarch, regno) != NULL
1228 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1229 fprintf_unfiltered (gdb_stdlog, "(%s)",
1230 gdbarch_register_name (gdbarch, regno));
1232 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1233 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1235 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1236 int size = register_size (gdbarch, regno);
1237 gdb_byte *buf = register_buffer (regno);
1239 fprintf_unfiltered (gdb_stdlog, " = ");
1240 for (int i = 0; i < size; i++)
1242 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1244 if (size <= sizeof (LONGEST))
1246 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1248 fprintf_unfiltered (gdb_stdlog, " %s %s",
1249 core_addr_to_string_nz (val), plongest (val));
1252 fprintf_unfiltered (gdb_stdlog, "\n");
1256 reg_flush_command (const char *command, int from_tty)
1258 /* Force-flush the register cache. */
1259 registers_changed ();
1261 printf_filtered (_("Register cache flushed.\n"));
1265 register_dump::dump (ui_file *file)
1267 auto descr = regcache_descr (m_gdbarch);
1269 int footnote_nr = 0;
1270 int footnote_register_offset = 0;
1271 int footnote_register_type_name_null = 0;
1272 long register_offset = 0;
1274 gdb_assert (descr->nr_cooked_registers
1275 == gdbarch_num_cooked_regs (m_gdbarch));
1277 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1281 fprintf_unfiltered (file, " %-10s", "Name");
1284 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1288 else if (p[0] == '\0')
1290 fprintf_unfiltered (file, " %-10s", p);
1295 fprintf_unfiltered (file, " %4s", "Nr");
1297 fprintf_unfiltered (file, " %4d", regnum);
1299 /* Relative number. */
1301 fprintf_unfiltered (file, " %4s", "Rel");
1302 else if (regnum < gdbarch_num_regs (m_gdbarch))
1303 fprintf_unfiltered (file, " %4d", regnum);
1305 fprintf_unfiltered (file, " %4d",
1306 (regnum - gdbarch_num_regs (m_gdbarch)));
1310 fprintf_unfiltered (file, " %6s ", "Offset");
1313 fprintf_unfiltered (file, " %6ld",
1314 descr->register_offset[regnum]);
1315 if (register_offset != descr->register_offset[regnum]
1317 && (descr->register_offset[regnum]
1318 != (descr->register_offset[regnum - 1]
1319 + descr->sizeof_register[regnum - 1])))
1322 if (!footnote_register_offset)
1323 footnote_register_offset = ++footnote_nr;
1324 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1327 fprintf_unfiltered (file, " ");
1328 register_offset = (descr->register_offset[regnum]
1329 + descr->sizeof_register[regnum]);
1334 fprintf_unfiltered (file, " %5s ", "Size");
1336 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1341 std::string name_holder;
1347 static const char blt[] = "builtin_type";
1349 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1352 if (!footnote_register_type_name_null)
1353 footnote_register_type_name_null = ++footnote_nr;
1354 name_holder = string_printf ("*%d",
1355 footnote_register_type_name_null);
1356 t = name_holder.c_str ();
1358 /* Chop a leading builtin_type. */
1359 if (startswith (t, blt))
1362 fprintf_unfiltered (file, " %-15s", t);
1365 /* Leading space always present. */
1366 fprintf_unfiltered (file, " ");
1368 dump_reg (file, regnum);
1370 fprintf_unfiltered (file, "\n");
1373 if (footnote_register_offset)
1374 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1375 footnote_register_offset);
1376 if (footnote_register_type_name_null)
1377 fprintf_unfiltered (file,
1378 "*%d: Register type's name NULL.\n",
1379 footnote_register_type_name_null);
1383 #include "gdbsupport/selftest.h"
1384 #include "selftest-arch.h"
1385 #include "target-float.h"
1387 namespace selftests {
1389 class regcache_access : public regcache
1393 /* Return the number of elements in current_regcache. */
1396 current_regcache_size ()
1398 return std::distance (regcache::current_regcache.begin (),
1399 regcache::current_regcache.end ());
1404 current_regcache_test (void)
1406 /* It is empty at the start. */
1407 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1409 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1411 /* Get regcache from ptid1, a new regcache is added to
1412 current_regcache. */
1413 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1417 SELF_CHECK (regcache != NULL);
1418 SELF_CHECK (regcache->ptid () == ptid1);
1419 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1421 /* Get regcache from ptid2, a new regcache is added to
1422 current_regcache. */
1423 regcache = get_thread_arch_aspace_regcache (ptid2,
1426 SELF_CHECK (regcache != NULL);
1427 SELF_CHECK (regcache->ptid () == ptid2);
1428 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1430 /* Get regcache from ptid3, a new regcache is added to
1431 current_regcache. */
1432 regcache = get_thread_arch_aspace_regcache (ptid3,
1435 SELF_CHECK (regcache != NULL);
1436 SELF_CHECK (regcache->ptid () == ptid3);
1437 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1439 /* Get regcache from ptid2 again, nothing is added to
1440 current_regcache. */
1441 regcache = get_thread_arch_aspace_regcache (ptid2,
1444 SELF_CHECK (regcache != NULL);
1445 SELF_CHECK (regcache->ptid () == ptid2);
1446 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1448 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1449 current_regcache. */
1450 registers_changed_ptid (ptid2);
1451 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1454 class target_ops_no_register : public test_target_ops
1457 target_ops_no_register ()
1458 : test_target_ops {}
1463 fetch_registers_called = 0;
1464 store_registers_called = 0;
1465 xfer_partial_called = 0;
1468 void fetch_registers (regcache *regs, int regno) override;
1469 void store_registers (regcache *regs, int regno) override;
1471 enum target_xfer_status xfer_partial (enum target_object object,
1472 const char *annex, gdb_byte *readbuf,
1473 const gdb_byte *writebuf,
1474 ULONGEST offset, ULONGEST len,
1475 ULONGEST *xfered_len) override;
1477 unsigned int fetch_registers_called = 0;
1478 unsigned int store_registers_called = 0;
1479 unsigned int xfer_partial_called = 0;
1483 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1485 /* Mark register available. */
1486 regs->raw_supply_zeroed (regno);
1487 this->fetch_registers_called++;
1491 target_ops_no_register::store_registers (regcache *regs, int regno)
1493 this->store_registers_called++;
1496 enum target_xfer_status
1497 target_ops_no_register::xfer_partial (enum target_object object,
1498 const char *annex, gdb_byte *readbuf,
1499 const gdb_byte *writebuf,
1500 ULONGEST offset, ULONGEST len,
1501 ULONGEST *xfered_len)
1503 this->xfer_partial_called++;
1506 return TARGET_XFER_OK;
1509 class readwrite_regcache : public regcache
1512 readwrite_regcache (struct gdbarch *gdbarch)
1513 : regcache (gdbarch, nullptr)
1517 /* Test regcache::cooked_read gets registers from raw registers and
1518 memory instead of target to_{fetch,store}_registers. */
1521 cooked_read_test (struct gdbarch *gdbarch)
1523 /* Error out if debugging something, because we're going to push the
1524 test target, which would pop any existing target. */
1525 if (current_top_target ()->stratum () >= process_stratum)
1526 error (_("target already pushed"));
1528 /* Create a mock environment. An inferior with a thread, with a
1529 process_stratum target pushed. */
1531 target_ops_no_register mock_target;
1532 ptid_t mock_ptid (1, 1);
1533 inferior mock_inferior (mock_ptid.pid ());
1534 address_space mock_aspace {};
1535 mock_inferior.gdbarch = gdbarch;
1536 mock_inferior.aspace = &mock_aspace;
1537 thread_info mock_thread (&mock_inferior, mock_ptid);
1539 /* Add the mock inferior to the inferior list so that look ups by
1540 target+ptid can find it. */
1541 scoped_restore restore_inferior_list
1542 = make_scoped_restore (&inferior_list);
1543 inferior_list = &mock_inferior;
1545 /* Switch to the mock inferior. */
1546 scoped_restore_current_inferior restore_current_inferior;
1547 set_current_inferior (&mock_inferior);
1549 /* Push the process_stratum target so we can mock accessing
1551 push_target (&mock_target);
1553 /* Pop it again on exit (return/exception). */
1558 pop_all_targets_at_and_above (process_stratum);
1562 /* Switch to the mock thread. */
1563 scoped_restore restore_inferior_ptid
1564 = make_scoped_restore (&inferior_ptid, mock_ptid);
1566 /* Test that read one raw register from regcache_no_target will go
1567 to the target layer. */
1569 /* Find a raw register which size isn't zero. */
1571 for (nonzero_regnum = 0;
1572 nonzero_regnum < gdbarch_num_regs (gdbarch);
1575 if (register_size (gdbarch, nonzero_regnum) != 0)
1579 readwrite_regcache readwrite (gdbarch);
1580 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1582 readwrite.raw_read (nonzero_regnum, buf.data ());
1584 /* raw_read calls target_fetch_registers. */
1585 SELF_CHECK (mock_target.fetch_registers_called > 0);
1586 mock_target.reset ();
1588 /* Mark all raw registers valid, so the following raw registers
1589 accesses won't go to target. */
1590 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1591 readwrite.raw_update (i);
1593 mock_target.reset ();
1594 /* Then, read all raw and pseudo registers, and don't expect calling
1595 to_{fetch,store}_registers. */
1596 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1598 if (register_size (gdbarch, regnum) == 0)
1601 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1603 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1604 inner_buf.data ()));
1606 SELF_CHECK (mock_target.fetch_registers_called == 0);
1607 SELF_CHECK (mock_target.store_registers_called == 0);
1609 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1610 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1611 SELF_CHECK (mock_target.xfer_partial_called == 0);
1613 mock_target.reset ();
1616 readonly_detached_regcache readonly (readwrite);
1618 /* GDB may go to target layer to fetch all registers and memory for
1619 readonly regcache. */
1620 mock_target.reset ();
1622 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1624 if (register_size (gdbarch, regnum) == 0)
1627 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1628 enum register_status status = readonly.cooked_read (regnum,
1631 if (regnum < gdbarch_num_regs (gdbarch))
1633 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1635 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1636 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1637 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1638 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1639 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1640 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1641 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1642 || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
1644 /* Raw registers. If raw registers are not in save_reggroup,
1645 their status are unknown. */
1646 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1647 SELF_CHECK (status == REG_VALID);
1649 SELF_CHECK (status == REG_UNKNOWN);
1652 SELF_CHECK (status == REG_VALID);
1656 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1657 SELF_CHECK (status == REG_VALID);
1660 /* If pseudo registers are not in save_reggroup, some of
1661 them can be computed from saved raw registers, but some
1662 of them are unknown. */
1663 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1665 if (bfd_arch == bfd_arch_frv
1666 || bfd_arch == bfd_arch_m32c
1667 || bfd_arch == bfd_arch_mep
1668 || bfd_arch == bfd_arch_sh)
1669 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1670 else if (bfd_arch == bfd_arch_mips
1671 || bfd_arch == bfd_arch_h8300)
1672 SELF_CHECK (status == REG_UNKNOWN);
1674 SELF_CHECK (status == REG_VALID);
1678 SELF_CHECK (mock_target.fetch_registers_called == 0);
1679 SELF_CHECK (mock_target.store_registers_called == 0);
1680 SELF_CHECK (mock_target.xfer_partial_called == 0);
1682 mock_target.reset ();
1686 /* Test regcache::cooked_write by writing some expected contents to
1687 registers, and checking that contents read from registers and the
1688 expected contents are the same. */
1691 cooked_write_test (struct gdbarch *gdbarch)
1693 /* Error out if debugging something, because we're going to push the
1694 test target, which would pop any existing target. */
1695 if (current_top_target ()->stratum () >= process_stratum)
1696 error (_("target already pushed"));
1698 /* Create a mock environment. A process_stratum target pushed. */
1700 target_ops_no_register mock_target;
1702 /* Push the process_stratum target so we can mock accessing
1704 push_target (&mock_target);
1706 /* Pop it again on exit (return/exception). */
1711 pop_all_targets_at_and_above (process_stratum);
1715 readwrite_regcache readwrite (gdbarch);
1717 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1719 for (auto regnum = 0; regnum < num_regs; regnum++)
1721 if (register_size (gdbarch, regnum) == 0
1722 || gdbarch_cannot_store_register (gdbarch, regnum))
1725 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1727 if ((bfd_arch == bfd_arch_sparc
1728 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1729 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1730 && gdbarch_ptr_bit (gdbarch) == 64
1731 && (regnum >= gdbarch_num_regs (gdbarch)
1732 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1733 || (bfd_arch == bfd_arch_spu
1734 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1735 TARGET_OBJECT_SPU. */
1736 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
1739 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1740 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1741 const auto type = register_type (gdbarch, regnum);
1743 if (TYPE_CODE (type) == TYPE_CODE_FLT
1744 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1746 /* Generate valid float format. */
1747 target_float_from_string (expected.data (), type, "1.25");
1749 else if (TYPE_CODE (type) == TYPE_CODE_INT
1750 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1751 || TYPE_CODE (type) == TYPE_CODE_PTR
1752 || TYPE_CODE (type) == TYPE_CODE_UNION
1753 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
1755 if (bfd_arch == bfd_arch_ia64
1756 || (regnum >= gdbarch_num_regs (gdbarch)
1757 && (bfd_arch == bfd_arch_xtensa
1758 || bfd_arch == bfd_arch_bfin
1759 || bfd_arch == bfd_arch_m32c
1760 /* m68hc11 pseudo registers are in memory. */
1761 || bfd_arch == bfd_arch_m68hc11
1762 || bfd_arch == bfd_arch_m68hc12
1763 || bfd_arch == bfd_arch_s390))
1764 || (bfd_arch == bfd_arch_frv
1765 /* FRV pseudo registers except iacc0. */
1766 && regnum > gdbarch_num_regs (gdbarch)))
1768 /* Skip setting the expected values for some architecture
1771 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1773 /* RL78_PC_REGNUM */
1774 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1779 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1783 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
1785 /* No idea how to test flags. */
1790 /* If we don't know how to create the expected value for the
1791 this type, make it fail. */
1795 readwrite.cooked_write (regnum, expected.data ());
1797 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1798 SELF_CHECK (expected == buf);
1802 } // namespace selftests
1803 #endif /* GDB_SELF_TEST */
1806 _initialize_regcache (void)
1808 regcache_descr_handle
1809 = gdbarch_data_register_post_init (init_regcache_descr);
1811 gdb::observers::target_changed.attach (regcache_observer_target_changed);
1812 gdb::observers::thread_ptid_changed.attach
1813 (regcache::regcache_thread_ptid_changed);
1815 add_com ("flushregs", class_maintenance, reg_flush_command,
1816 _("Force gdb to flush its register cache (maintainer command)."));
1819 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1821 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1822 selftests::cooked_read_test);
1823 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1824 selftests::cooked_write_test);