1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "reggroups.h"
29 #include <forward_list>
34 * Here is the actual register cache.
37 /* Per-architecture object describing the layout of a register cache.
38 Computed once when the architecture is created. */
40 struct gdbarch_data *regcache_descr_handle;
44 /* The architecture this descriptor belongs to. */
45 struct gdbarch *gdbarch;
47 /* The raw register cache. Each raw (or hard) register is supplied
48 by the target interface. The raw cache should not contain
49 redundant information - if the PC is constructed from two
50 registers then those registers and not the PC lives in the raw
52 long sizeof_raw_registers;
54 /* The cooked register space. Each cooked register in the range
55 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
56 register. The remaining [NR_RAW_REGISTERS
57 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
58 both raw registers and memory by the architecture methods
59 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
60 int nr_cooked_registers;
61 long sizeof_cooked_registers;
63 /* Offset and size (in 8 bit bytes), of each register in the
64 register cache. All registers (including those in the range
65 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
67 long *register_offset;
68 long *sizeof_register;
70 /* Cached table containing the type of each register. */
71 struct type **register_type;
75 init_regcache_descr (struct gdbarch *gdbarch)
78 struct regcache_descr *descr;
79 gdb_assert (gdbarch != NULL);
81 /* Create an initial, zero filled, table. */
82 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
83 descr->gdbarch = gdbarch;
85 /* Total size of the register space. The raw registers are mapped
86 directly onto the raw register cache while the pseudo's are
87 either mapped onto raw-registers or memory. */
88 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
89 + gdbarch_num_pseudo_regs (gdbarch);
91 /* Fill in a table of register types. */
93 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
95 for (i = 0; i < descr->nr_cooked_registers; i++)
96 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
98 /* Construct a strictly RAW register cache. Don't allow pseudo's
99 into the register cache. */
101 /* Lay out the register cache.
103 NOTE: cagney/2002-05-22: Only register_type() is used when
104 constructing the register cache. It is assumed that the
105 register's raw size, virtual size and type length are all the
111 descr->sizeof_register
112 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
113 descr->register_offset
114 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
115 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
117 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
118 descr->register_offset[i] = offset;
119 offset += descr->sizeof_register[i];
120 gdb_assert (MAX_REGISTER_SIZE >= 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];
130 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
132 /* Set the real size of the readonly register cache buffer. */
133 descr->sizeof_cooked_registers = offset;
139 static struct regcache_descr *
140 regcache_descr (struct gdbarch *gdbarch)
142 return (struct regcache_descr *) gdbarch_data (gdbarch,
143 regcache_descr_handle);
146 /* Utility functions returning useful register attributes stored in
147 the regcache descr. */
150 register_type (struct gdbarch *gdbarch, int regnum)
152 struct regcache_descr *descr = regcache_descr (gdbarch);
154 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
155 return descr->register_type[regnum];
158 /* Utility functions returning useful register attributes stored in
159 the regcache descr. */
162 register_size (struct gdbarch *gdbarch, int regnum)
164 struct regcache_descr *descr = regcache_descr (gdbarch);
167 gdb_assert (regnum >= 0
168 && regnum < (gdbarch_num_regs (gdbarch)
169 + gdbarch_num_pseudo_regs (gdbarch)));
170 size = descr->sizeof_register[regnum];
174 /* See common/common-regcache.h. */
177 regcache_register_size (const struct regcache *regcache, int n)
179 return register_size (regcache->arch (), n);
182 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
183 : m_has_pseudo (has_pseudo)
185 gdb_assert (gdbarch != NULL);
186 m_descr = regcache_descr (gdbarch);
190 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
191 m_register_status = XCNEWVEC (signed char,
192 m_descr->nr_cooked_registers);
196 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
197 m_register_status = XCNEWVEC (signed char, 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 static enum register_status
210 do_cooked_read (void *src, int regnum, gdb_byte *buf)
212 struct regcache *regcache = (struct regcache *) src;
214 return regcache_cooked_read (regcache, regnum, buf);
217 readonly_detached_regcache::readonly_detached_regcache (const regcache &src)
218 : readonly_detached_regcache (src.arch (), do_cooked_read, (void *) &src)
223 reg_buffer::arch () const
225 return m_descr->gdbarch;
228 /* See regcache.h. */
231 regcache_get_ptid (const struct regcache *regcache)
233 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
235 return regcache->ptid ();
238 /* Cleanup class for invalidating a register. */
240 class regcache_invalidator
244 regcache_invalidator (struct regcache *regcache, int regnum)
245 : m_regcache (regcache),
250 ~regcache_invalidator ()
252 if (m_regcache != nullptr)
253 regcache_invalidate (m_regcache, m_regnum);
256 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
260 m_regcache = nullptr;
265 struct regcache *m_regcache;
269 /* Return a pointer to register REGNUM's buffer cache. */
272 reg_buffer::register_buffer (int regnum) const
274 return m_registers + m_descr->register_offset[regnum];
278 reg_buffer::save (regcache_cooked_read_ftype *cooked_read,
281 struct gdbarch *gdbarch = m_descr->gdbarch;
284 /* It should have pseudo registers. */
285 gdb_assert (m_has_pseudo);
286 /* Clear the dest. */
287 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
288 memset (m_register_status, 0, m_descr->nr_cooked_registers);
289 /* Copy over any registers (identified by their membership in the
290 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
291 gdbarch_num_pseudo_regs) range is checked since some architectures need
292 to save/restore `cooked' registers that live in memory. */
293 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
295 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
297 gdb_byte *dst_buf = register_buffer (regnum);
298 enum register_status status = cooked_read (src, regnum, dst_buf);
300 gdb_assert (status != REG_UNKNOWN);
302 if (status != REG_VALID)
303 memset (dst_buf, 0, register_size (gdbarch, regnum));
305 m_register_status[regnum] = status;
311 regcache::restore (readonly_detached_regcache *src)
313 struct gdbarch *gdbarch = m_descr->gdbarch;
316 gdb_assert (src != NULL);
317 gdb_assert (src->m_has_pseudo);
319 gdb_assert (gdbarch == src->arch ());
321 /* Copy over any registers, being careful to only restore those that
322 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
323 + gdbarch_num_pseudo_regs) range is checked since some architectures need
324 to save/restore `cooked' registers that live in memory. */
325 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
327 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
329 if (src->m_register_status[regnum] == REG_VALID)
330 cooked_write (regnum, src->register_buffer (regnum));
336 regcache_register_status (const struct regcache *regcache, int regnum)
338 gdb_assert (regcache != NULL);
339 return regcache->get_register_status (regnum);
343 reg_buffer::get_register_status (int regnum) const
345 assert_regnum (regnum);
347 return (enum register_status) m_register_status[regnum];
351 regcache_invalidate (struct regcache *regcache, int regnum)
353 gdb_assert (regcache != NULL);
354 regcache->invalidate (regnum);
358 detached_regcache::invalidate (int regnum)
360 assert_regnum (regnum);
361 m_register_status[regnum] = REG_UNKNOWN;
365 reg_buffer::assert_regnum (int regnum) const
367 gdb_assert (regnum >= 0);
369 gdb_assert (regnum < m_descr->nr_cooked_registers);
371 gdb_assert (regnum < gdbarch_num_regs (arch ()));
374 /* Global structure containing the current regcache. */
376 /* NOTE: this is a write-through cache. There is no "dirty" bit for
377 recording if the register values have been changed (eg. by the
378 user). Therefore all registers must be written back to the
379 target when appropriate. */
380 std::forward_list<regcache *> regcache::current_regcache;
383 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
384 struct address_space *aspace)
386 for (const auto ®cache : regcache::current_regcache)
387 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
390 regcache *new_regcache = new regcache (gdbarch, aspace);
392 regcache::current_regcache.push_front (new_regcache);
393 new_regcache->set_ptid (ptid);
399 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
401 address_space *aspace = target_thread_address_space (ptid);
403 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
406 static ptid_t current_thread_ptid;
407 static struct gdbarch *current_thread_arch;
410 get_thread_regcache (ptid_t ptid)
412 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
414 current_thread_ptid = ptid;
415 current_thread_arch = target_thread_architecture (ptid);
418 return get_thread_arch_regcache (ptid, current_thread_arch);
422 get_current_regcache (void)
424 return get_thread_regcache (inferior_ptid);
427 /* See common/common-regcache.h. */
430 get_thread_regcache_for_ptid (ptid_t ptid)
432 return get_thread_regcache (ptid);
435 /* Observer for the target_changed event. */
438 regcache_observer_target_changed (struct target_ops *target)
440 registers_changed ();
443 /* Update global variables old ptids to hold NEW_PTID if they were
446 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
448 for (auto ®cache : regcache::current_regcache)
450 if (ptid_equal (regcache->ptid (), old_ptid))
451 regcache->set_ptid (new_ptid);
455 /* Low level examining and depositing of registers.
457 The caller is responsible for making sure that the inferior is
458 stopped before calling the fetching routines, or it will get
459 garbage. (a change from GDB version 3, in which the caller got the
460 value from the last stop). */
462 /* REGISTERS_CHANGED ()
464 Indicate that registers may have changed, so invalidate the cache. */
467 registers_changed_ptid (ptid_t ptid)
469 for (auto oit = regcache::current_regcache.before_begin (),
470 it = std::next (oit);
471 it != regcache::current_regcache.end ();
474 if (ptid_match ((*it)->ptid (), ptid))
477 it = regcache::current_regcache.erase_after (oit);
483 if (ptid_match (current_thread_ptid, ptid))
485 current_thread_ptid = null_ptid;
486 current_thread_arch = NULL;
489 if (ptid_match (inferior_ptid, ptid))
491 /* We just deleted the regcache of the current thread. Need to
492 forget about any frames we have cached, too. */
493 reinit_frame_cache ();
498 registers_changed (void)
500 registers_changed_ptid (minus_one_ptid);
502 /* Force cleanup of any alloca areas if using C alloca instead of
503 a builtin alloca. This particular call is used to clean up
504 areas allocated by low level target code which may build up
505 during lengthy interactions between gdb and the target before
506 gdb gives control to the user (ie watchpoints). */
511 regcache_raw_update (struct regcache *regcache, int regnum)
513 gdb_assert (regcache != NULL);
515 regcache->raw_update (regnum);
519 regcache::raw_update (int regnum)
521 assert_regnum (regnum);
523 /* Make certain that the register cache is up-to-date with respect
524 to the current thread. This switching shouldn't be necessary
525 only there is still only one target side register cache. Sigh!
526 On the bright side, at least there is a regcache object. */
528 if (get_register_status (regnum) == REG_UNKNOWN)
530 target_fetch_registers (this, regnum);
532 /* A number of targets can't access the whole set of raw
533 registers (because the debug API provides no means to get at
535 if (m_register_status[regnum] == REG_UNKNOWN)
536 m_register_status[regnum] = REG_UNAVAILABLE;
541 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
543 return regcache->raw_read (regnum, buf);
547 readable_regcache::raw_read (int regnum, gdb_byte *buf)
549 gdb_assert (buf != NULL);
552 if (m_register_status[regnum] != REG_VALID)
553 memset (buf, 0, m_descr->sizeof_register[regnum]);
555 memcpy (buf, register_buffer (regnum),
556 m_descr->sizeof_register[regnum]);
558 return (enum register_status) m_register_status[regnum];
562 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
564 gdb_assert (regcache != NULL);
565 return regcache->raw_read (regnum, val);
568 template<typename T, typename>
570 readable_regcache::raw_read (int regnum, T *val)
573 enum register_status status;
575 assert_regnum (regnum);
576 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
577 status = raw_read (regnum, buf);
578 if (status == REG_VALID)
579 *val = extract_integer<T> (buf,
580 m_descr->sizeof_register[regnum],
581 gdbarch_byte_order (m_descr->gdbarch));
588 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
591 gdb_assert (regcache != NULL);
592 return regcache->raw_read (regnum, val);
596 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
598 gdb_assert (regcache != NULL);
599 regcache->raw_write (regnum, val);
602 template<typename T, typename>
604 regcache::raw_write (int regnum, T val)
608 assert_regnum (regnum);
609 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
610 store_integer (buf, m_descr->sizeof_register[regnum],
611 gdbarch_byte_order (m_descr->gdbarch), val);
612 raw_write (regnum, buf);
616 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
619 gdb_assert (regcache != NULL);
620 regcache->raw_write (regnum, val);
624 regcache_raw_get_signed (struct regcache *regcache, int regnum)
627 enum register_status status;
629 status = regcache_raw_read_signed (regcache, regnum, &value);
630 if (status == REG_UNAVAILABLE)
631 throw_error (NOT_AVAILABLE_ERROR,
632 _("Register %d is not available"), regnum);
637 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
639 return regcache->cooked_read (regnum, buf);
643 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
645 gdb_assert (regnum >= 0);
646 gdb_assert (regnum < m_descr->nr_cooked_registers);
647 if (regnum < num_raw_registers ())
648 return raw_read (regnum, buf);
649 else if (m_has_pseudo
650 && m_register_status[regnum] != REG_UNKNOWN)
652 if (m_register_status[regnum] == REG_VALID)
653 memcpy (buf, register_buffer (regnum),
654 m_descr->sizeof_register[regnum]);
656 memset (buf, 0, m_descr->sizeof_register[regnum]);
658 return (enum register_status) m_register_status[regnum];
660 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
662 struct value *mark, *computed;
663 enum register_status result = REG_VALID;
665 mark = value_mark ();
667 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
669 if (value_entirely_available (computed))
670 memcpy (buf, value_contents_raw (computed),
671 m_descr->sizeof_register[regnum]);
674 memset (buf, 0, m_descr->sizeof_register[regnum]);
675 result = REG_UNAVAILABLE;
678 value_free_to_mark (mark);
683 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
688 regcache_cooked_read_value (struct regcache *regcache, int regnum)
690 return regcache->cooked_read_value (regnum);
694 readable_regcache::cooked_read_value (int regnum)
696 gdb_assert (regnum >= 0);
697 gdb_assert (regnum < m_descr->nr_cooked_registers);
699 if (regnum < num_raw_registers ()
700 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
701 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
703 struct value *result;
705 result = allocate_value (register_type (m_descr->gdbarch, regnum));
706 VALUE_LVAL (result) = lval_register;
707 VALUE_REGNUM (result) = regnum;
709 /* It is more efficient in general to do this delegation in this
710 direction than in the other one, even though the value-based
712 if (cooked_read (regnum,
713 value_contents_raw (result)) == REG_UNAVAILABLE)
714 mark_value_bytes_unavailable (result, 0,
715 TYPE_LENGTH (value_type (result)));
720 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
725 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
728 gdb_assert (regcache != NULL);
729 return regcache->cooked_read (regnum, val);
732 template<typename T, typename>
734 readable_regcache::cooked_read (int regnum, T *val)
736 enum register_status status;
739 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
740 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
741 status = cooked_read (regnum, buf);
742 if (status == REG_VALID)
743 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
744 gdbarch_byte_order (m_descr->gdbarch));
751 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
754 gdb_assert (regcache != NULL);
755 return regcache->cooked_read (regnum, val);
759 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
762 gdb_assert (regcache != NULL);
763 regcache->cooked_write (regnum, val);
766 template<typename T, typename>
768 regcache::cooked_write (int regnum, T val)
772 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
773 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
774 store_integer (buf, m_descr->sizeof_register[regnum],
775 gdbarch_byte_order (m_descr->gdbarch), val);
776 cooked_write (regnum, buf);
780 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
783 gdb_assert (regcache != NULL);
784 regcache->cooked_write (regnum, val);
788 regcache_raw_write (struct regcache *regcache, int regnum,
791 gdb_assert (regcache != NULL && buf != NULL);
792 regcache->raw_write (regnum, buf);
796 regcache::raw_write (int regnum, const gdb_byte *buf)
799 gdb_assert (buf != NULL);
800 assert_regnum (regnum);
802 /* On the sparc, writing %g0 is a no-op, so we don't even want to
803 change the registers array if something writes to this register. */
804 if (gdbarch_cannot_store_register (arch (), regnum))
807 /* If we have a valid copy of the register, and new value == old
808 value, then don't bother doing the actual store. */
809 if (get_register_status (regnum) == REG_VALID
810 && (memcmp (register_buffer (regnum), buf,
811 m_descr->sizeof_register[regnum]) == 0))
814 target_prepare_to_store (this);
815 raw_supply (regnum, buf);
817 /* Invalidate the register after it is written, in case of a
819 regcache_invalidator invalidator (this, regnum);
821 target_store_registers (this, regnum);
823 /* The target did not throw an error so we can discard invalidating
825 invalidator.release ();
829 regcache_cooked_write (struct regcache *regcache, int regnum,
832 regcache->cooked_write (regnum, buf);
836 regcache::cooked_write (int regnum, const gdb_byte *buf)
838 gdb_assert (regnum >= 0);
839 gdb_assert (regnum < m_descr->nr_cooked_registers);
840 if (regnum < num_raw_registers ())
841 raw_write (regnum, buf);
843 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
847 /* Perform a partial register transfer using a read, modify, write
850 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
852 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
856 readable_regcache::read_part (int regnum, int offset, int len, void *in,
859 struct gdbarch *gdbarch = arch ();
860 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
862 gdb_assert (in != NULL);
863 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
864 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
865 /* Something to do? */
866 if (offset + len == 0)
868 /* Read (when needed) ... */
869 enum register_status status;
872 status = raw_read (regnum, reg);
874 status = cooked_read (regnum, reg);
875 if (status != REG_VALID)
879 memcpy (in, reg + offset, len);
885 regcache::write_part (int regnum, int offset, int len,
886 const void *out, bool is_raw)
888 struct gdbarch *gdbarch = arch ();
889 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
891 gdb_assert (out != NULL);
892 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
893 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
894 /* Something to do? */
895 if (offset + len == 0)
897 /* Read (when needed) ... */
899 || offset + len < m_descr->sizeof_register[regnum])
901 enum register_status status;
904 status = raw_read (regnum, reg);
906 status = cooked_read (regnum, reg);
907 if (status != REG_VALID)
911 memcpy (reg + offset, out, len);
912 /* ... write (when needed). */
914 raw_write (regnum, reg);
916 cooked_write (regnum, reg);
922 regcache_raw_read_part (struct regcache *regcache, int regnum,
923 int offset, int len, gdb_byte *buf)
925 return regcache->raw_read_part (regnum, offset, len, buf);
929 readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
931 assert_regnum (regnum);
932 return read_part (regnum, offset, len, buf, true);
936 regcache_raw_write_part (struct regcache *regcache, int regnum,
937 int offset, int len, const gdb_byte *buf)
939 regcache->raw_write_part (regnum, offset, len, buf);
943 regcache::raw_write_part (int regnum, int offset, int len,
946 assert_regnum (regnum);
947 write_part (regnum, offset, len, buf, true);
951 regcache_cooked_read_part (struct regcache *regcache, int regnum,
952 int offset, int len, gdb_byte *buf)
954 return regcache->cooked_read_part (regnum, offset, len, buf);
959 readable_regcache::cooked_read_part (int regnum, int offset, int len,
962 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
963 return read_part (regnum, offset, len, buf, false);
967 regcache_cooked_write_part (struct regcache *regcache, int regnum,
968 int offset, int len, const gdb_byte *buf)
970 regcache->cooked_write_part (regnum, offset, len, buf);
974 regcache::cooked_write_part (int regnum, int offset, int len,
977 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
978 write_part (regnum, offset, len, buf, false);
981 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
984 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
986 gdb_assert (regcache != NULL);
987 regcache->raw_supply (regnum, buf);
991 detached_regcache::raw_supply (int regnum, const void *buf)
996 assert_regnum (regnum);
998 regbuf = register_buffer (regnum);
999 size = m_descr->sizeof_register[regnum];
1003 memcpy (regbuf, buf, size);
1004 m_register_status[regnum] = REG_VALID;
1008 /* This memset not strictly necessary, but better than garbage
1009 in case the register value manages to escape somewhere (due
1010 to a bug, no less). */
1011 memset (regbuf, 0, size);
1012 m_register_status[regnum] = REG_UNAVAILABLE;
1016 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1017 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1018 the register size is greater than ADDR_LEN, then the integer will be sign or
1019 zero extended. If the register size is smaller than the integer, then the
1020 most significant bytes of the integer will be truncated. */
1023 detached_regcache::raw_supply_integer (int regnum, const gdb_byte *addr,
1024 int addr_len, bool is_signed)
1026 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1030 assert_regnum (regnum);
1032 regbuf = register_buffer (regnum);
1033 regsize = m_descr->sizeof_register[regnum];
1035 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1037 m_register_status[regnum] = REG_VALID;
1040 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1041 as calling raw_supply with NULL (which will set the state to
1045 detached_regcache::raw_supply_zeroed (int regnum)
1050 assert_regnum (regnum);
1052 regbuf = register_buffer (regnum);
1053 size = m_descr->sizeof_register[regnum];
1055 memset (regbuf, 0, size);
1056 m_register_status[regnum] = REG_VALID;
1059 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1062 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1064 gdb_assert (regcache != NULL && buf != NULL);
1065 regcache->raw_collect (regnum, buf);
1069 regcache::raw_collect (int regnum, void *buf) const
1074 gdb_assert (buf != NULL);
1075 assert_regnum (regnum);
1077 regbuf = register_buffer (regnum);
1078 size = m_descr->sizeof_register[regnum];
1079 memcpy (buf, regbuf, size);
1082 /* Transfer a single or all registers belonging to a certain register
1083 set to or from a buffer. This is the main worker function for
1084 regcache_supply_regset and regcache_collect_regset. */
1086 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1087 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1088 If ADDR_LEN is greater than the register size, then the integer will be sign
1089 or zero extended. If ADDR_LEN is smaller than the register size, then the
1090 most significant bytes of the integer will be truncated. */
1093 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1094 bool is_signed) const
1096 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1097 const gdb_byte *regbuf;
1100 assert_regnum (regnum);
1102 regbuf = register_buffer (regnum);
1103 regsize = m_descr->sizeof_register[regnum];
1105 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1110 regcache::transfer_regset (const struct regset *regset,
1111 struct regcache *out_regcache,
1112 int regnum, const void *in_buf,
1113 void *out_buf, size_t size) const
1115 const struct regcache_map_entry *map;
1116 int offs = 0, count;
1118 for (map = (const struct regcache_map_entry *) regset->regmap;
1119 (count = map->count) != 0;
1122 int regno = map->regno;
1123 int slot_size = map->size;
1125 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1126 slot_size = m_descr->sizeof_register[regno];
1128 if (regno == REGCACHE_MAP_SKIP
1130 && (regnum < regno || regnum >= regno + count)))
1131 offs += count * slot_size;
1133 else if (regnum == -1)
1134 for (; count--; regno++, offs += slot_size)
1136 if (offs + slot_size > size)
1140 raw_collect (regno, (gdb_byte *) out_buf + offs);
1142 out_regcache->raw_supply (regno, in_buf
1143 ? (const gdb_byte *) in_buf + offs
1148 /* Transfer a single register and return. */
1149 offs += (regnum - regno) * slot_size;
1150 if (offs + slot_size > size)
1154 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1156 out_regcache->raw_supply (regnum, in_buf
1157 ? (const gdb_byte *) in_buf + offs
1164 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1165 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1166 If BUF is NULL, set the register(s) to "unavailable" status. */
1169 regcache_supply_regset (const struct regset *regset,
1170 struct regcache *regcache,
1171 int regnum, const void *buf, size_t size)
1173 regcache->supply_regset (regset, regnum, buf, size);
1177 regcache::supply_regset (const struct regset *regset,
1178 int regnum, const void *buf, size_t size)
1180 transfer_regset (regset, this, regnum, buf, NULL, size);
1183 /* Collect register REGNUM from REGCACHE to BUF, using the register
1184 map in REGSET. If REGNUM is -1, do this for all registers in
1188 regcache_collect_regset (const struct regset *regset,
1189 const struct regcache *regcache,
1190 int regnum, void *buf, size_t size)
1192 regcache->collect_regset (regset, regnum, buf, size);
1196 regcache::collect_regset (const struct regset *regset,
1197 int regnum, void *buf, size_t size) const
1199 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1203 /* Special handling for register PC. */
1206 regcache_read_pc (struct regcache *regcache)
1208 struct gdbarch *gdbarch = regcache->arch ();
1212 if (gdbarch_read_pc_p (gdbarch))
1213 pc_val = gdbarch_read_pc (gdbarch, regcache);
1214 /* Else use per-frame method on get_current_frame. */
1215 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1219 if (regcache_cooked_read_unsigned (regcache,
1220 gdbarch_pc_regnum (gdbarch),
1221 &raw_val) == REG_UNAVAILABLE)
1222 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1224 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1227 internal_error (__FILE__, __LINE__,
1228 _("regcache_read_pc: Unable to find PC"));
1233 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1235 struct gdbarch *gdbarch = regcache->arch ();
1237 if (gdbarch_write_pc_p (gdbarch))
1238 gdbarch_write_pc (gdbarch, regcache, pc);
1239 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1240 regcache_cooked_write_unsigned (regcache,
1241 gdbarch_pc_regnum (gdbarch), pc);
1243 internal_error (__FILE__, __LINE__,
1244 _("regcache_write_pc: Unable to update PC"));
1246 /* Writing the PC (for instance, from "load") invalidates the
1248 reinit_frame_cache ();
1252 reg_buffer::num_raw_registers () const
1254 return gdbarch_num_regs (arch ());
1258 regcache::debug_print_register (const char *func, int regno)
1260 struct gdbarch *gdbarch = arch ();
1262 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1263 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1264 && gdbarch_register_name (gdbarch, regno) != NULL
1265 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1266 fprintf_unfiltered (gdb_stdlog, "(%s)",
1267 gdbarch_register_name (gdbarch, regno));
1269 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1270 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1272 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1273 int size = register_size (gdbarch, regno);
1274 gdb_byte *buf = register_buffer (regno);
1276 fprintf_unfiltered (gdb_stdlog, " = ");
1277 for (int i = 0; i < size; i++)
1279 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1281 if (size <= sizeof (LONGEST))
1283 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1285 fprintf_unfiltered (gdb_stdlog, " %s %s",
1286 core_addr_to_string_nz (val), plongest (val));
1289 fprintf_unfiltered (gdb_stdlog, "\n");
1293 reg_flush_command (const char *command, int from_tty)
1295 /* Force-flush the register cache. */
1296 registers_changed ();
1298 printf_filtered (_("Register cache flushed.\n"));
1302 register_dump::dump (ui_file *file)
1304 auto descr = regcache_descr (m_gdbarch);
1306 int footnote_nr = 0;
1307 int footnote_register_offset = 0;
1308 int footnote_register_type_name_null = 0;
1309 long register_offset = 0;
1311 gdb_assert (descr->nr_cooked_registers
1312 == (gdbarch_num_regs (m_gdbarch)
1313 + gdbarch_num_pseudo_regs (m_gdbarch)));
1315 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1319 fprintf_unfiltered (file, " %-10s", "Name");
1322 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1326 else if (p[0] == '\0')
1328 fprintf_unfiltered (file, " %-10s", p);
1333 fprintf_unfiltered (file, " %4s", "Nr");
1335 fprintf_unfiltered (file, " %4d", regnum);
1337 /* Relative number. */
1339 fprintf_unfiltered (file, " %4s", "Rel");
1340 else if (regnum < gdbarch_num_regs (m_gdbarch))
1341 fprintf_unfiltered (file, " %4d", regnum);
1343 fprintf_unfiltered (file, " %4d",
1344 (regnum - gdbarch_num_regs (m_gdbarch)));
1348 fprintf_unfiltered (file, " %6s ", "Offset");
1351 fprintf_unfiltered (file, " %6ld",
1352 descr->register_offset[regnum]);
1353 if (register_offset != descr->register_offset[regnum]
1355 && (descr->register_offset[regnum]
1356 != (descr->register_offset[regnum - 1]
1357 + descr->sizeof_register[regnum - 1])))
1360 if (!footnote_register_offset)
1361 footnote_register_offset = ++footnote_nr;
1362 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1365 fprintf_unfiltered (file, " ");
1366 register_offset = (descr->register_offset[regnum]
1367 + descr->sizeof_register[regnum]);
1372 fprintf_unfiltered (file, " %5s ", "Size");
1374 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1379 std::string name_holder;
1385 static const char blt[] = "builtin_type";
1387 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1390 if (!footnote_register_type_name_null)
1391 footnote_register_type_name_null = ++footnote_nr;
1392 name_holder = string_printf ("*%d",
1393 footnote_register_type_name_null);
1394 t = name_holder.c_str ();
1396 /* Chop a leading builtin_type. */
1397 if (startswith (t, blt))
1400 fprintf_unfiltered (file, " %-15s", t);
1403 /* Leading space always present. */
1404 fprintf_unfiltered (file, " ");
1406 dump_reg (file, regnum);
1408 fprintf_unfiltered (file, "\n");
1411 if (footnote_register_offset)
1412 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1413 footnote_register_offset);
1414 if (footnote_register_type_name_null)
1415 fprintf_unfiltered (file,
1416 "*%d: Register type's name NULL.\n",
1417 footnote_register_type_name_null);
1421 #include "selftest.h"
1422 #include "selftest-arch.h"
1423 #include "gdbthread.h"
1424 #include "target-float.h"
1426 namespace selftests {
1428 class regcache_access : public regcache
1432 /* Return the number of elements in current_regcache. */
1435 current_regcache_size ()
1437 return std::distance (regcache::current_regcache.begin (),
1438 regcache::current_regcache.end ());
1443 current_regcache_test (void)
1445 /* It is empty at the start. */
1446 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1448 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1450 /* Get regcache from ptid1, a new regcache is added to
1451 current_regcache. */
1452 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1456 SELF_CHECK (regcache != NULL);
1457 SELF_CHECK (regcache->ptid () == ptid1);
1458 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1460 /* Get regcache from ptid2, a new regcache is added to
1461 current_regcache. */
1462 regcache = get_thread_arch_aspace_regcache (ptid2,
1465 SELF_CHECK (regcache != NULL);
1466 SELF_CHECK (regcache->ptid () == ptid2);
1467 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1469 /* Get regcache from ptid3, a new regcache is added to
1470 current_regcache. */
1471 regcache = get_thread_arch_aspace_regcache (ptid3,
1474 SELF_CHECK (regcache != NULL);
1475 SELF_CHECK (regcache->ptid () == ptid3);
1476 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1478 /* Get regcache from ptid2 again, nothing is added to
1479 current_regcache. */
1480 regcache = get_thread_arch_aspace_regcache (ptid2,
1483 SELF_CHECK (regcache != NULL);
1484 SELF_CHECK (regcache->ptid () == ptid2);
1485 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1487 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1488 current_regcache. */
1489 registers_changed_ptid (ptid2);
1490 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1493 static void test_target_fetch_registers (target_ops *self, regcache *regs,
1495 static void test_target_store_registers (target_ops *self, regcache *regs,
1497 static enum target_xfer_status
1498 test_target_xfer_partial (struct target_ops *ops,
1499 enum target_object object,
1500 const char *annex, gdb_byte *readbuf,
1501 const gdb_byte *writebuf,
1502 ULONGEST offset, ULONGEST len,
1503 ULONGEST *xfered_len);
1505 class target_ops_no_register : public test_target_ops
1508 target_ops_no_register ()
1509 : test_target_ops {}
1511 to_fetch_registers = test_target_fetch_registers;
1512 to_store_registers = test_target_store_registers;
1513 to_xfer_partial = test_target_xfer_partial;
1520 fetch_registers_called = 0;
1521 store_registers_called = 0;
1522 xfer_partial_called = 0;
1525 unsigned int fetch_registers_called = 0;
1526 unsigned int store_registers_called = 0;
1527 unsigned int xfer_partial_called = 0;
1531 test_target_fetch_registers (target_ops *self, regcache *regs, int regno)
1533 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1535 /* Mark register available. */
1536 regs->raw_supply_zeroed (regno);
1537 ops->fetch_registers_called++;
1541 test_target_store_registers (target_ops *self, regcache *regs, int regno)
1543 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1545 ops->store_registers_called++;
1548 static enum target_xfer_status
1549 test_target_xfer_partial (struct target_ops *self, enum target_object object,
1550 const char *annex, gdb_byte *readbuf,
1551 const gdb_byte *writebuf,
1552 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1554 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1556 ops->xfer_partial_called++;
1559 return TARGET_XFER_OK;
1562 class readwrite_regcache : public regcache
1565 readwrite_regcache (struct gdbarch *gdbarch)
1566 : regcache (gdbarch, nullptr)
1570 /* Test regcache::cooked_read gets registers from raw registers and
1571 memory instead of target to_{fetch,store}_registers. */
1574 cooked_read_test (struct gdbarch *gdbarch)
1576 /* Error out if debugging something, because we're going to push the
1577 test target, which would pop any existing target. */
1578 if (current_target.to_stratum >= process_stratum)
1579 error (_("target already pushed"));
1581 /* Create a mock environment. An inferior with a thread, with a
1582 process_stratum target pushed. */
1584 target_ops_no_register mock_target;
1585 ptid_t mock_ptid (1, 1);
1586 inferior mock_inferior (mock_ptid.pid ());
1587 address_space mock_aspace {};
1588 mock_inferior.gdbarch = gdbarch;
1589 mock_inferior.aspace = &mock_aspace;
1590 thread_info mock_thread (&mock_inferior, mock_ptid);
1592 scoped_restore restore_thread_list
1593 = make_scoped_restore (&thread_list, &mock_thread);
1595 /* Add the mock inferior to the inferior list so that look ups by
1596 target+ptid can find it. */
1597 scoped_restore restore_inferior_list
1598 = make_scoped_restore (&inferior_list);
1599 inferior_list = &mock_inferior;
1601 /* Switch to the mock inferior. */
1602 scoped_restore_current_inferior restore_current_inferior;
1603 set_current_inferior (&mock_inferior);
1605 /* Push the process_stratum target so we can mock accessing
1607 push_target (&mock_target);
1609 /* Pop it again on exit (return/exception). */
1614 pop_all_targets_at_and_above (process_stratum);
1618 /* Switch to the mock thread. */
1619 scoped_restore restore_inferior_ptid
1620 = make_scoped_restore (&inferior_ptid, mock_ptid);
1622 /* Test that read one raw register from regcache_no_target will go
1623 to the target layer. */
1626 /* Find a raw register which size isn't zero. */
1627 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1629 if (register_size (gdbarch, regnum) != 0)
1633 readwrite_regcache readwrite (gdbarch);
1634 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1636 readwrite.raw_read (regnum, buf.data ());
1638 /* raw_read calls target_fetch_registers. */
1639 SELF_CHECK (mock_target.fetch_registers_called > 0);
1640 mock_target.reset ();
1642 /* Mark all raw registers valid, so the following raw registers
1643 accesses won't go to target. */
1644 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1645 readwrite.raw_update (i);
1647 mock_target.reset ();
1648 /* Then, read all raw and pseudo registers, and don't expect calling
1649 to_{fetch,store}_registers. */
1650 for (int regnum = 0;
1651 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1654 if (register_size (gdbarch, regnum) == 0)
1657 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1659 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1661 SELF_CHECK (mock_target.fetch_registers_called == 0);
1662 SELF_CHECK (mock_target.store_registers_called == 0);
1664 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1665 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1666 SELF_CHECK (mock_target.xfer_partial_called == 0);
1668 mock_target.reset ();
1671 readonly_detached_regcache readonly (readwrite);
1673 /* GDB may go to target layer to fetch all registers and memory for
1674 readonly regcache. */
1675 mock_target.reset ();
1677 for (int regnum = 0;
1678 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1681 if (register_size (gdbarch, regnum) == 0)
1684 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1685 enum register_status status = readonly.cooked_read (regnum,
1688 if (regnum < gdbarch_num_regs (gdbarch))
1690 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1692 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1693 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1694 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1695 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1696 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1697 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1698 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score)
1700 /* Raw registers. If raw registers are not in save_reggroup,
1701 their status are unknown. */
1702 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1703 SELF_CHECK (status == REG_VALID);
1705 SELF_CHECK (status == REG_UNKNOWN);
1708 SELF_CHECK (status == REG_VALID);
1712 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1713 SELF_CHECK (status == REG_VALID);
1716 /* If pseudo registers are not in save_reggroup, some of
1717 them can be computed from saved raw registers, but some
1718 of them are unknown. */
1719 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1721 if (bfd_arch == bfd_arch_frv
1722 || bfd_arch == bfd_arch_m32c
1723 || bfd_arch == bfd_arch_mep
1724 || bfd_arch == bfd_arch_sh)
1725 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1726 else if (bfd_arch == bfd_arch_mips
1727 || bfd_arch == bfd_arch_h8300)
1728 SELF_CHECK (status == REG_UNKNOWN);
1730 SELF_CHECK (status == REG_VALID);
1734 SELF_CHECK (mock_target.fetch_registers_called == 0);
1735 SELF_CHECK (mock_target.store_registers_called == 0);
1736 SELF_CHECK (mock_target.xfer_partial_called == 0);
1738 mock_target.reset ();
1742 /* Test regcache::cooked_write by writing some expected contents to
1743 registers, and checking that contents read from registers and the
1744 expected contents are the same. */
1747 cooked_write_test (struct gdbarch *gdbarch)
1749 /* Error out if debugging something, because we're going to push the
1750 test target, which would pop any existing target. */
1751 if (current_target.to_stratum >= process_stratum)
1752 error (_("target already pushed"));
1754 /* Create a mock environment. A process_stratum target pushed. */
1756 target_ops_no_register mock_target;
1758 /* Push the process_stratum target so we can mock accessing
1760 push_target (&mock_target);
1762 /* Pop it again on exit (return/exception). */
1767 pop_all_targets_at_and_above (process_stratum);
1771 readwrite_regcache readwrite (gdbarch);
1773 const int num_regs = (gdbarch_num_regs (gdbarch)
1774 + gdbarch_num_pseudo_regs (gdbarch));
1776 for (auto regnum = 0; regnum < num_regs; regnum++)
1778 if (register_size (gdbarch, regnum) == 0
1779 || gdbarch_cannot_store_register (gdbarch, regnum))
1782 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1784 if ((bfd_arch == bfd_arch_sparc
1785 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1786 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1787 && gdbarch_ptr_bit (gdbarch) == 64
1788 && (regnum >= gdbarch_num_regs (gdbarch)
1789 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1790 || (bfd_arch == bfd_arch_sh
1791 /* FPSCR_C_REGNUM in sh64 is hard to test. */
1792 && gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh5
1794 || (bfd_arch == bfd_arch_spu
1795 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1796 TARGET_OBJECT_SPU. */
1797 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
1800 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1801 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1802 const auto type = register_type (gdbarch, regnum);
1804 if (TYPE_CODE (type) == TYPE_CODE_FLT
1805 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1807 /* Generate valid float format. */
1808 target_float_from_string (expected.data (), type, "1.25");
1810 else if (TYPE_CODE (type) == TYPE_CODE_INT
1811 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1812 || TYPE_CODE (type) == TYPE_CODE_PTR
1813 || TYPE_CODE (type) == TYPE_CODE_UNION
1814 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
1816 if (bfd_arch == bfd_arch_ia64
1817 || (regnum >= gdbarch_num_regs (gdbarch)
1818 && (bfd_arch == bfd_arch_xtensa
1819 || bfd_arch == bfd_arch_bfin
1820 || bfd_arch == bfd_arch_m32c
1821 /* m68hc11 pseudo registers are in memory. */
1822 || bfd_arch == bfd_arch_m68hc11
1823 || bfd_arch == bfd_arch_m68hc12
1824 || bfd_arch == bfd_arch_s390))
1825 || (bfd_arch == bfd_arch_frv
1826 /* FRV pseudo registers except iacc0. */
1827 && regnum > gdbarch_num_regs (gdbarch)))
1829 /* Skip setting the expected values for some architecture
1832 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1834 /* RL78_PC_REGNUM */
1835 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1840 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1844 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
1846 /* No idea how to test flags. */
1851 /* If we don't know how to create the expected value for the
1852 this type, make it fail. */
1856 readwrite.cooked_write (regnum, expected.data ());
1858 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1859 SELF_CHECK (expected == buf);
1863 } // namespace selftests
1864 #endif /* GDB_SELF_TEST */
1867 _initialize_regcache (void)
1869 regcache_descr_handle
1870 = gdbarch_data_register_post_init (init_regcache_descr);
1872 observer_attach_target_changed (regcache_observer_target_changed);
1873 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
1875 add_com ("flushregs", class_maintenance, reg_flush_command,
1876 _("Force gdb to flush its register cache (maintainer command)"));
1879 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1881 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1882 selftests::cooked_read_test);
1883 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1884 selftests::cooked_write_test);