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"
27 #include "observable.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
];
121 /* Set the real size of the raw register cache buffer. */
122 descr
->sizeof_raw_registers
= offset
;
124 for (; i
< descr
->nr_cooked_registers
; i
++)
126 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
127 descr
->register_offset
[i
] = offset
;
128 offset
+= descr
->sizeof_register
[i
];
130 /* Set the real size of the readonly register cache buffer. */
131 descr
->sizeof_cooked_registers
= offset
;
137 static struct regcache_descr
*
138 regcache_descr (struct gdbarch
*gdbarch
)
140 return (struct regcache_descr
*) gdbarch_data (gdbarch
,
141 regcache_descr_handle
);
144 /* Utility functions returning useful register attributes stored in
145 the regcache descr. */
148 register_type (struct gdbarch
*gdbarch
, int regnum
)
150 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
152 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
153 return descr
->register_type
[regnum
];
156 /* Utility functions returning useful register attributes stored in
157 the regcache descr. */
160 register_size (struct gdbarch
*gdbarch
, int regnum
)
162 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
165 gdb_assert (regnum
>= 0
166 && regnum
< (gdbarch_num_regs (gdbarch
)
167 + gdbarch_num_pseudo_regs (gdbarch
)));
168 size
= descr
->sizeof_register
[regnum
];
172 /* See common/common-regcache.h. */
175 regcache_register_size (const struct regcache
*regcache
, int n
)
177 return register_size (regcache
->arch (), n
);
180 reg_buffer::reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
)
181 : m_has_pseudo (has_pseudo
)
183 gdb_assert (gdbarch
!= NULL
);
184 m_descr
= regcache_descr (gdbarch
);
188 m_registers
= XCNEWVEC (gdb_byte
, m_descr
->sizeof_cooked_registers
);
189 m_register_status
= XCNEWVEC (signed char,
190 m_descr
->nr_cooked_registers
);
194 m_registers
= XCNEWVEC (gdb_byte
, m_descr
->sizeof_raw_registers
);
195 m_register_status
= XCNEWVEC (signed char, 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 static enum register_status
208 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
210 struct regcache
*regcache
= (struct regcache
*) src
;
212 return regcache_cooked_read (regcache
, regnum
, buf
);
215 readonly_detached_regcache::readonly_detached_regcache (const regcache
&src
)
216 : readonly_detached_regcache (src
.arch (), do_cooked_read
, (void *) &src
)
221 reg_buffer::arch () const
223 return m_descr
->gdbarch
;
226 /* See regcache.h. */
229 regcache_get_ptid (const struct regcache
*regcache
)
231 gdb_assert (!ptid_equal (regcache
->ptid (), minus_one_ptid
));
233 return regcache
->ptid ();
236 /* Cleanup class for invalidating a register. */
238 class regcache_invalidator
242 regcache_invalidator (struct regcache
*regcache
, int regnum
)
243 : m_regcache (regcache
),
248 ~regcache_invalidator ()
250 if (m_regcache
!= nullptr)
251 regcache_invalidate (m_regcache
, m_regnum
);
254 DISABLE_COPY_AND_ASSIGN (regcache_invalidator
);
258 m_regcache
= nullptr;
263 struct regcache
*m_regcache
;
267 /* Return a pointer to register REGNUM's buffer cache. */
270 reg_buffer::register_buffer (int regnum
) const
272 return m_registers
+ m_descr
->register_offset
[regnum
];
276 reg_buffer::save (regcache_cooked_read_ftype
*cooked_read
,
279 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
282 /* It should have pseudo registers. */
283 gdb_assert (m_has_pseudo
);
284 /* Clear the dest. */
285 memset (m_registers
, 0, m_descr
->sizeof_cooked_registers
);
286 memset (m_register_status
, 0, m_descr
->nr_cooked_registers
);
287 /* Copy over any registers (identified by their membership in the
288 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
289 gdbarch_num_pseudo_regs) range is checked since some architectures need
290 to save/restore `cooked' registers that live in memory. */
291 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
293 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
295 gdb_byte
*dst_buf
= register_buffer (regnum
);
296 enum register_status status
= cooked_read (src
, regnum
, dst_buf
);
298 gdb_assert (status
!= REG_UNKNOWN
);
300 if (status
!= REG_VALID
)
301 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
303 m_register_status
[regnum
] = status
;
309 regcache::restore (readonly_detached_regcache
*src
)
311 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
314 gdb_assert (src
!= NULL
);
315 gdb_assert (src
->m_has_pseudo
);
317 gdb_assert (gdbarch
== src
->arch ());
319 /* Copy over any registers, being careful to only restore those that
320 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
321 + gdbarch_num_pseudo_regs) range is checked since some architectures need
322 to save/restore `cooked' registers that live in memory. */
323 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
325 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
327 if (src
->m_register_status
[regnum
] == REG_VALID
)
328 cooked_write (regnum
, src
->register_buffer (regnum
));
334 regcache_register_status (const struct regcache
*regcache
, int regnum
)
336 gdb_assert (regcache
!= NULL
);
337 return regcache
->get_register_status (regnum
);
341 reg_buffer::get_register_status (int regnum
) const
343 assert_regnum (regnum
);
345 return (enum register_status
) m_register_status
[regnum
];
349 regcache_invalidate (struct regcache
*regcache
, int regnum
)
351 gdb_assert (regcache
!= NULL
);
352 regcache
->invalidate (regnum
);
356 detached_regcache::invalidate (int regnum
)
358 assert_regnum (regnum
);
359 m_register_status
[regnum
] = REG_UNKNOWN
;
363 reg_buffer::assert_regnum (int regnum
) const
365 gdb_assert (regnum
>= 0);
367 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
369 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
372 /* Global structure containing the current regcache. */
374 /* NOTE: this is a write-through cache. There is no "dirty" bit for
375 recording if the register values have been changed (eg. by the
376 user). Therefore all registers must be written back to the
377 target when appropriate. */
378 std::forward_list
<regcache
*> regcache::current_regcache
;
381 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
382 struct address_space
*aspace
)
384 for (const auto ®cache
: regcache::current_regcache
)
385 if (ptid_equal (regcache
->ptid (), ptid
) && regcache
->arch () == gdbarch
)
388 regcache
*new_regcache
= new regcache (gdbarch
, aspace
);
390 regcache::current_regcache
.push_front (new_regcache
);
391 new_regcache
->set_ptid (ptid
);
397 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
399 address_space
*aspace
= target_thread_address_space (ptid
);
401 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
404 static ptid_t current_thread_ptid
;
405 static struct gdbarch
*current_thread_arch
;
408 get_thread_regcache (ptid_t ptid
)
410 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
412 current_thread_ptid
= ptid
;
413 current_thread_arch
= target_thread_architecture (ptid
);
416 return get_thread_arch_regcache (ptid
, current_thread_arch
);
420 get_current_regcache (void)
422 return get_thread_regcache (inferior_ptid
);
425 /* See common/common-regcache.h. */
428 get_thread_regcache_for_ptid (ptid_t ptid
)
430 return get_thread_regcache (ptid
);
433 /* Observer for the target_changed event. */
436 regcache_observer_target_changed (struct target_ops
*target
)
438 registers_changed ();
441 /* Update global variables old ptids to hold NEW_PTID if they were
444 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
446 for (auto ®cache
: regcache::current_regcache
)
448 if (ptid_equal (regcache
->ptid (), old_ptid
))
449 regcache
->set_ptid (new_ptid
);
453 /* Low level examining and depositing of registers.
455 The caller is responsible for making sure that the inferior is
456 stopped before calling the fetching routines, or it will get
457 garbage. (a change from GDB version 3, in which the caller got the
458 value from the last stop). */
460 /* REGISTERS_CHANGED ()
462 Indicate that registers may have changed, so invalidate the cache. */
465 registers_changed_ptid (ptid_t ptid
)
467 for (auto oit
= regcache::current_regcache
.before_begin (),
468 it
= std::next (oit
);
469 it
!= regcache::current_regcache
.end ();
472 if (ptid_match ((*it
)->ptid (), ptid
))
475 it
= regcache::current_regcache
.erase_after (oit
);
481 if (ptid_match (current_thread_ptid
, ptid
))
483 current_thread_ptid
= null_ptid
;
484 current_thread_arch
= NULL
;
487 if (ptid_match (inferior_ptid
, ptid
))
489 /* We just deleted the regcache of the current thread. Need to
490 forget about any frames we have cached, too. */
491 reinit_frame_cache ();
496 registers_changed (void)
498 registers_changed_ptid (minus_one_ptid
);
500 /* Force cleanup of any alloca areas if using C alloca instead of
501 a builtin alloca. This particular call is used to clean up
502 areas allocated by low level target code which may build up
503 during lengthy interactions between gdb and the target before
504 gdb gives control to the user (ie watchpoints). */
509 regcache_raw_update (struct regcache
*regcache
, int regnum
)
511 gdb_assert (regcache
!= NULL
);
513 regcache
->raw_update (regnum
);
517 regcache::raw_update (int regnum
)
519 assert_regnum (regnum
);
521 /* Make certain that the register cache is up-to-date with respect
522 to the current thread. This switching shouldn't be necessary
523 only there is still only one target side register cache. Sigh!
524 On the bright side, at least there is a regcache object. */
526 if (get_register_status (regnum
) == REG_UNKNOWN
)
528 target_fetch_registers (this, regnum
);
530 /* A number of targets can't access the whole set of raw
531 registers (because the debug API provides no means to get at
533 if (m_register_status
[regnum
] == REG_UNKNOWN
)
534 m_register_status
[regnum
] = REG_UNAVAILABLE
;
539 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
541 return regcache
->raw_read (regnum
, buf
);
545 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
547 gdb_assert (buf
!= NULL
);
550 if (m_register_status
[regnum
] != REG_VALID
)
551 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
553 memcpy (buf
, register_buffer (regnum
),
554 m_descr
->sizeof_register
[regnum
]);
556 return (enum register_status
) m_register_status
[regnum
];
560 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
562 gdb_assert (regcache
!= NULL
);
563 return regcache
->raw_read (regnum
, val
);
566 template<typename T
, typename
>
568 readable_regcache::raw_read (int regnum
, T
*val
)
571 enum register_status status
;
573 assert_regnum (regnum
);
574 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
575 status
= raw_read (regnum
, buf
);
576 if (status
== REG_VALID
)
577 *val
= extract_integer
<T
> (buf
,
578 m_descr
->sizeof_register
[regnum
],
579 gdbarch_byte_order (m_descr
->gdbarch
));
586 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
589 gdb_assert (regcache
!= NULL
);
590 return regcache
->raw_read (regnum
, val
);
594 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
596 gdb_assert (regcache
!= NULL
);
597 regcache
->raw_write (regnum
, val
);
600 template<typename T
, typename
>
602 regcache::raw_write (int regnum
, T val
)
606 assert_regnum (regnum
);
607 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
608 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
609 gdbarch_byte_order (m_descr
->gdbarch
), val
);
610 raw_write (regnum
, buf
);
614 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
617 gdb_assert (regcache
!= NULL
);
618 regcache
->raw_write (regnum
, val
);
622 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
625 enum register_status status
;
627 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
628 if (status
== REG_UNAVAILABLE
)
629 throw_error (NOT_AVAILABLE_ERROR
,
630 _("Register %d is not available"), regnum
);
635 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
637 return regcache
->cooked_read (regnum
, buf
);
641 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
643 gdb_assert (regnum
>= 0);
644 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
645 if (regnum
< num_raw_registers ())
646 return raw_read (regnum
, buf
);
647 else if (m_has_pseudo
648 && m_register_status
[regnum
] != REG_UNKNOWN
)
650 if (m_register_status
[regnum
] == REG_VALID
)
651 memcpy (buf
, register_buffer (regnum
),
652 m_descr
->sizeof_register
[regnum
]);
654 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
656 return (enum register_status
) m_register_status
[regnum
];
658 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
660 struct value
*mark
, *computed
;
661 enum register_status result
= REG_VALID
;
663 mark
= value_mark ();
665 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
667 if (value_entirely_available (computed
))
668 memcpy (buf
, value_contents_raw (computed
),
669 m_descr
->sizeof_register
[regnum
]);
672 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
673 result
= REG_UNAVAILABLE
;
676 value_free_to_mark (mark
);
681 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
686 regcache_cooked_read_value (struct regcache
*regcache
, int regnum
)
688 return regcache
->cooked_read_value (regnum
);
692 readable_regcache::cooked_read_value (int regnum
)
694 gdb_assert (regnum
>= 0);
695 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
697 if (regnum
< num_raw_registers ()
698 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
699 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
701 struct value
*result
;
703 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
704 VALUE_LVAL (result
) = lval_register
;
705 VALUE_REGNUM (result
) = regnum
;
707 /* It is more efficient in general to do this delegation in this
708 direction than in the other one, even though the value-based
710 if (cooked_read (regnum
,
711 value_contents_raw (result
)) == REG_UNAVAILABLE
)
712 mark_value_bytes_unavailable (result
, 0,
713 TYPE_LENGTH (value_type (result
)));
718 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
723 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
726 gdb_assert (regcache
!= NULL
);
727 return regcache
->cooked_read (regnum
, val
);
730 template<typename T
, typename
>
732 readable_regcache::cooked_read (int regnum
, T
*val
)
734 enum register_status status
;
737 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
738 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
739 status
= cooked_read (regnum
, buf
);
740 if (status
== REG_VALID
)
741 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
742 gdbarch_byte_order (m_descr
->gdbarch
));
749 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
752 gdb_assert (regcache
!= NULL
);
753 return regcache
->cooked_read (regnum
, val
);
757 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
760 gdb_assert (regcache
!= NULL
);
761 regcache
->cooked_write (regnum
, val
);
764 template<typename T
, typename
>
766 regcache::cooked_write (int regnum
, T val
)
770 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
771 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
772 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
773 gdbarch_byte_order (m_descr
->gdbarch
), val
);
774 cooked_write (regnum
, buf
);
778 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
781 gdb_assert (regcache
!= NULL
);
782 regcache
->cooked_write (regnum
, val
);
786 regcache_raw_write (struct regcache
*regcache
, int regnum
,
789 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
790 regcache
->raw_write (regnum
, buf
);
794 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
797 gdb_assert (buf
!= NULL
);
798 assert_regnum (regnum
);
800 /* On the sparc, writing %g0 is a no-op, so we don't even want to
801 change the registers array if something writes to this register. */
802 if (gdbarch_cannot_store_register (arch (), regnum
))
805 /* If we have a valid copy of the register, and new value == old
806 value, then don't bother doing the actual store. */
807 if (get_register_status (regnum
) == REG_VALID
808 && (memcmp (register_buffer (regnum
), buf
,
809 m_descr
->sizeof_register
[regnum
]) == 0))
812 target_prepare_to_store (this);
813 raw_supply (regnum
, buf
);
815 /* Invalidate the register after it is written, in case of a
817 regcache_invalidator
invalidator (this, regnum
);
819 target_store_registers (this, regnum
);
821 /* The target did not throw an error so we can discard invalidating
823 invalidator
.release ();
827 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
830 regcache
->cooked_write (regnum
, buf
);
834 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
836 gdb_assert (regnum
>= 0);
837 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
838 if (regnum
< num_raw_registers ())
839 raw_write (regnum
, buf
);
841 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
845 /* Perform a partial register transfer using a read, modify, write
848 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
850 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
854 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
857 struct gdbarch
*gdbarch
= arch ();
858 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
860 gdb_assert (in
!= NULL
);
861 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
862 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
863 /* Something to do? */
864 if (offset
+ len
== 0)
866 /* Read (when needed) ... */
867 enum register_status status
;
870 status
= raw_read (regnum
, reg
);
872 status
= cooked_read (regnum
, reg
);
873 if (status
!= REG_VALID
)
877 memcpy (in
, reg
+ offset
, len
);
883 regcache::write_part (int regnum
, int offset
, int len
,
884 const void *out
, bool is_raw
)
886 struct gdbarch
*gdbarch
= arch ();
887 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
889 gdb_assert (out
!= NULL
);
890 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
891 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
892 /* Something to do? */
893 if (offset
+ len
== 0)
895 /* Read (when needed) ... */
897 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
899 enum register_status status
;
902 status
= raw_read (regnum
, reg
);
904 status
= cooked_read (regnum
, reg
);
905 if (status
!= REG_VALID
)
909 memcpy (reg
+ offset
, out
, len
);
910 /* ... write (when needed). */
912 raw_write (regnum
, reg
);
914 cooked_write (regnum
, reg
);
920 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
921 int offset
, int len
, gdb_byte
*buf
)
923 return regcache
->raw_read_part (regnum
, offset
, len
, buf
);
927 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
929 assert_regnum (regnum
);
930 return read_part (regnum
, offset
, len
, buf
, true);
934 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
935 int offset
, int len
, const gdb_byte
*buf
)
937 regcache
->raw_write_part (regnum
, offset
, len
, buf
);
941 regcache::raw_write_part (int regnum
, int offset
, int len
,
944 assert_regnum (regnum
);
945 write_part (regnum
, offset
, len
, buf
, true);
949 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
950 int offset
, int len
, gdb_byte
*buf
)
952 return regcache
->cooked_read_part (regnum
, offset
, len
, buf
);
957 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
960 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
961 return read_part (regnum
, offset
, len
, buf
, false);
965 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
966 int offset
, int len
, const gdb_byte
*buf
)
968 regcache
->cooked_write_part (regnum
, offset
, len
, buf
);
972 regcache::cooked_write_part (int regnum
, int offset
, int len
,
975 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
976 write_part (regnum
, offset
, len
, buf
, false);
979 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
982 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
984 gdb_assert (regcache
!= NULL
);
985 regcache
->raw_supply (regnum
, buf
);
989 detached_regcache::raw_supply (int regnum
, const void *buf
)
994 assert_regnum (regnum
);
996 regbuf
= register_buffer (regnum
);
997 size
= m_descr
->sizeof_register
[regnum
];
1001 memcpy (regbuf
, buf
, size
);
1002 m_register_status
[regnum
] = REG_VALID
;
1006 /* This memset not strictly necessary, but better than garbage
1007 in case the register value manages to escape somewhere (due
1008 to a bug, no less). */
1009 memset (regbuf
, 0, size
);
1010 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1014 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1015 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1016 the register size is greater than ADDR_LEN, then the integer will be sign or
1017 zero extended. If the register size is smaller than the integer, then the
1018 most significant bytes of the integer will be truncated. */
1021 detached_regcache::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1022 int addr_len
, bool is_signed
)
1024 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1028 assert_regnum (regnum
);
1030 regbuf
= register_buffer (regnum
);
1031 regsize
= m_descr
->sizeof_register
[regnum
];
1033 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1035 m_register_status
[regnum
] = REG_VALID
;
1038 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1039 as calling raw_supply with NULL (which will set the state to
1043 detached_regcache::raw_supply_zeroed (int regnum
)
1048 assert_regnum (regnum
);
1050 regbuf
= register_buffer (regnum
);
1051 size
= m_descr
->sizeof_register
[regnum
];
1053 memset (regbuf
, 0, size
);
1054 m_register_status
[regnum
] = REG_VALID
;
1057 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1060 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
1062 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
1063 regcache
->raw_collect (regnum
, buf
);
1067 regcache::raw_collect (int regnum
, void *buf
) const
1072 gdb_assert (buf
!= NULL
);
1073 assert_regnum (regnum
);
1075 regbuf
= register_buffer (regnum
);
1076 size
= m_descr
->sizeof_register
[regnum
];
1077 memcpy (buf
, regbuf
, size
);
1080 /* Transfer a single or all registers belonging to a certain register
1081 set to or from a buffer. This is the main worker function for
1082 regcache_supply_regset and regcache_collect_regset. */
1084 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1085 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1086 If ADDR_LEN is greater than the register size, then the integer will be sign
1087 or zero extended. If ADDR_LEN is smaller than the register size, then the
1088 most significant bytes of the integer will be truncated. */
1091 regcache::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1092 bool is_signed
) const
1094 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1095 const gdb_byte
*regbuf
;
1098 assert_regnum (regnum
);
1100 regbuf
= register_buffer (regnum
);
1101 regsize
= m_descr
->sizeof_register
[regnum
];
1103 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1108 regcache::transfer_regset (const struct regset
*regset
,
1109 struct regcache
*out_regcache
,
1110 int regnum
, const void *in_buf
,
1111 void *out_buf
, size_t size
) const
1113 const struct regcache_map_entry
*map
;
1114 int offs
= 0, count
;
1116 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1117 (count
= map
->count
) != 0;
1120 int regno
= map
->regno
;
1121 int slot_size
= map
->size
;
1123 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1124 slot_size
= m_descr
->sizeof_register
[regno
];
1126 if (regno
== REGCACHE_MAP_SKIP
1128 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1129 offs
+= count
* slot_size
;
1131 else if (regnum
== -1)
1132 for (; count
--; regno
++, offs
+= slot_size
)
1134 if (offs
+ slot_size
> size
)
1138 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1140 out_regcache
->raw_supply (regno
, in_buf
1141 ? (const gdb_byte
*) in_buf
+ offs
1146 /* Transfer a single register and return. */
1147 offs
+= (regnum
- regno
) * slot_size
;
1148 if (offs
+ slot_size
> size
)
1152 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1154 out_regcache
->raw_supply (regnum
, in_buf
1155 ? (const gdb_byte
*) in_buf
+ offs
1162 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1163 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1164 If BUF is NULL, set the register(s) to "unavailable" status. */
1167 regcache_supply_regset (const struct regset
*regset
,
1168 struct regcache
*regcache
,
1169 int regnum
, const void *buf
, size_t size
)
1171 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1175 regcache::supply_regset (const struct regset
*regset
,
1176 int regnum
, const void *buf
, size_t size
)
1178 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1181 /* Collect register REGNUM from REGCACHE to BUF, using the register
1182 map in REGSET. If REGNUM is -1, do this for all registers in
1186 regcache_collect_regset (const struct regset
*regset
,
1187 const struct regcache
*regcache
,
1188 int regnum
, void *buf
, size_t size
)
1190 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1194 regcache::collect_regset (const struct regset
*regset
,
1195 int regnum
, void *buf
, size_t size
) const
1197 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1201 /* Special handling for register PC. */
1204 regcache_read_pc (struct regcache
*regcache
)
1206 struct gdbarch
*gdbarch
= regcache
->arch ();
1210 if (gdbarch_read_pc_p (gdbarch
))
1211 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1212 /* Else use per-frame method on get_current_frame. */
1213 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1217 if (regcache_cooked_read_unsigned (regcache
,
1218 gdbarch_pc_regnum (gdbarch
),
1219 &raw_val
) == REG_UNAVAILABLE
)
1220 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1222 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1225 internal_error (__FILE__
, __LINE__
,
1226 _("regcache_read_pc: Unable to find PC"));
1231 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1233 struct gdbarch
*gdbarch
= regcache
->arch ();
1235 if (gdbarch_write_pc_p (gdbarch
))
1236 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1237 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1238 regcache_cooked_write_unsigned (regcache
,
1239 gdbarch_pc_regnum (gdbarch
), pc
);
1241 internal_error (__FILE__
, __LINE__
,
1242 _("regcache_write_pc: Unable to update PC"));
1244 /* Writing the PC (for instance, from "load") invalidates the
1246 reinit_frame_cache ();
1250 reg_buffer::num_raw_registers () const
1252 return gdbarch_num_regs (arch ());
1256 regcache::debug_print_register (const char *func
, int regno
)
1258 struct gdbarch
*gdbarch
= arch ();
1260 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1261 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1262 && gdbarch_register_name (gdbarch
, regno
) != NULL
1263 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1264 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1265 gdbarch_register_name (gdbarch
, regno
));
1267 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1268 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1270 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1271 int size
= register_size (gdbarch
, regno
);
1272 gdb_byte
*buf
= register_buffer (regno
);
1274 fprintf_unfiltered (gdb_stdlog
, " = ");
1275 for (int i
= 0; i
< size
; i
++)
1277 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1279 if (size
<= sizeof (LONGEST
))
1281 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1283 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1284 core_addr_to_string_nz (val
), plongest (val
));
1287 fprintf_unfiltered (gdb_stdlog
, "\n");
1291 reg_flush_command (const char *command
, int from_tty
)
1293 /* Force-flush the register cache. */
1294 registers_changed ();
1296 printf_filtered (_("Register cache flushed.\n"));
1300 register_dump::dump (ui_file
*file
)
1302 auto descr
= regcache_descr (m_gdbarch
);
1304 int footnote_nr
= 0;
1305 int footnote_register_offset
= 0;
1306 int footnote_register_type_name_null
= 0;
1307 long register_offset
= 0;
1309 gdb_assert (descr
->nr_cooked_registers
1310 == (gdbarch_num_regs (m_gdbarch
)
1311 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1313 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1317 fprintf_unfiltered (file
, " %-10s", "Name");
1320 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1324 else if (p
[0] == '\0')
1326 fprintf_unfiltered (file
, " %-10s", p
);
1331 fprintf_unfiltered (file
, " %4s", "Nr");
1333 fprintf_unfiltered (file
, " %4d", regnum
);
1335 /* Relative number. */
1337 fprintf_unfiltered (file
, " %4s", "Rel");
1338 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1339 fprintf_unfiltered (file
, " %4d", regnum
);
1341 fprintf_unfiltered (file
, " %4d",
1342 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1346 fprintf_unfiltered (file
, " %6s ", "Offset");
1349 fprintf_unfiltered (file
, " %6ld",
1350 descr
->register_offset
[regnum
]);
1351 if (register_offset
!= descr
->register_offset
[regnum
]
1353 && (descr
->register_offset
[regnum
]
1354 != (descr
->register_offset
[regnum
- 1]
1355 + descr
->sizeof_register
[regnum
- 1])))
1358 if (!footnote_register_offset
)
1359 footnote_register_offset
= ++footnote_nr
;
1360 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1363 fprintf_unfiltered (file
, " ");
1364 register_offset
= (descr
->register_offset
[regnum
]
1365 + descr
->sizeof_register
[regnum
]);
1370 fprintf_unfiltered (file
, " %5s ", "Size");
1372 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1377 std::string name_holder
;
1383 static const char blt
[] = "builtin_type";
1385 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1388 if (!footnote_register_type_name_null
)
1389 footnote_register_type_name_null
= ++footnote_nr
;
1390 name_holder
= string_printf ("*%d",
1391 footnote_register_type_name_null
);
1392 t
= name_holder
.c_str ();
1394 /* Chop a leading builtin_type. */
1395 if (startswith (t
, blt
))
1398 fprintf_unfiltered (file
, " %-15s", t
);
1401 /* Leading space always present. */
1402 fprintf_unfiltered (file
, " ");
1404 dump_reg (file
, regnum
);
1406 fprintf_unfiltered (file
, "\n");
1409 if (footnote_register_offset
)
1410 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1411 footnote_register_offset
);
1412 if (footnote_register_type_name_null
)
1413 fprintf_unfiltered (file
,
1414 "*%d: Register type's name NULL.\n",
1415 footnote_register_type_name_null
);
1419 #include "selftest.h"
1420 #include "selftest-arch.h"
1421 #include "gdbthread.h"
1422 #include "target-float.h"
1424 namespace selftests
{
1426 class regcache_access
: public regcache
1430 /* Return the number of elements in current_regcache. */
1433 current_regcache_size ()
1435 return std::distance (regcache::current_regcache
.begin (),
1436 regcache::current_regcache
.end ());
1441 current_regcache_test (void)
1443 /* It is empty at the start. */
1444 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1446 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1448 /* Get regcache from ptid1, a new regcache is added to
1449 current_regcache. */
1450 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1454 SELF_CHECK (regcache
!= NULL
);
1455 SELF_CHECK (regcache
->ptid () == ptid1
);
1456 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1458 /* Get regcache from ptid2, a new regcache is added to
1459 current_regcache. */
1460 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1463 SELF_CHECK (regcache
!= NULL
);
1464 SELF_CHECK (regcache
->ptid () == ptid2
);
1465 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1467 /* Get regcache from ptid3, a new regcache is added to
1468 current_regcache. */
1469 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1472 SELF_CHECK (regcache
!= NULL
);
1473 SELF_CHECK (regcache
->ptid () == ptid3
);
1474 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1476 /* Get regcache from ptid2 again, nothing is added to
1477 current_regcache. */
1478 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1481 SELF_CHECK (regcache
!= NULL
);
1482 SELF_CHECK (regcache
->ptid () == ptid2
);
1483 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1485 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1486 current_regcache. */
1487 registers_changed_ptid (ptid2
);
1488 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1491 class target_ops_no_register
: public test_target_ops
1494 target_ops_no_register ()
1495 : test_target_ops
{}
1500 fetch_registers_called
= 0;
1501 store_registers_called
= 0;
1502 xfer_partial_called
= 0;
1505 void fetch_registers (regcache
*regs
, int regno
) override
;
1506 void store_registers (regcache
*regs
, int regno
) override
;
1508 enum target_xfer_status
xfer_partial (enum target_object object
,
1509 const char *annex
, gdb_byte
*readbuf
,
1510 const gdb_byte
*writebuf
,
1511 ULONGEST offset
, ULONGEST len
,
1512 ULONGEST
*xfered_len
) override
;
1514 unsigned int fetch_registers_called
= 0;
1515 unsigned int store_registers_called
= 0;
1516 unsigned int xfer_partial_called
= 0;
1520 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1522 /* Mark register available. */
1523 regs
->raw_supply_zeroed (regno
);
1524 this->fetch_registers_called
++;
1528 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1530 this->store_registers_called
++;
1533 enum target_xfer_status
1534 target_ops_no_register::xfer_partial (enum target_object object
,
1535 const char *annex
, gdb_byte
*readbuf
,
1536 const gdb_byte
*writebuf
,
1537 ULONGEST offset
, ULONGEST len
,
1538 ULONGEST
*xfered_len
)
1540 this->xfer_partial_called
++;
1543 return TARGET_XFER_OK
;
1546 class readwrite_regcache
: public regcache
1549 readwrite_regcache (struct gdbarch
*gdbarch
)
1550 : regcache (gdbarch
, nullptr)
1554 /* Test regcache::cooked_read gets registers from raw registers and
1555 memory instead of target to_{fetch,store}_registers. */
1558 cooked_read_test (struct gdbarch
*gdbarch
)
1560 /* Error out if debugging something, because we're going to push the
1561 test target, which would pop any existing target. */
1562 if (target_stack
->to_stratum
>= process_stratum
)
1563 error (_("target already pushed"));
1565 /* Create a mock environment. An inferior with a thread, with a
1566 process_stratum target pushed. */
1568 target_ops_no_register mock_target
;
1569 ptid_t
mock_ptid (1, 1);
1570 inferior
mock_inferior (mock_ptid
.pid ());
1571 address_space mock_aspace
{};
1572 mock_inferior
.gdbarch
= gdbarch
;
1573 mock_inferior
.aspace
= &mock_aspace
;
1574 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1576 scoped_restore restore_thread_list
1577 = make_scoped_restore (&thread_list
, &mock_thread
);
1579 /* Add the mock inferior to the inferior list so that look ups by
1580 target+ptid can find it. */
1581 scoped_restore restore_inferior_list
1582 = make_scoped_restore (&inferior_list
);
1583 inferior_list
= &mock_inferior
;
1585 /* Switch to the mock inferior. */
1586 scoped_restore_current_inferior restore_current_inferior
;
1587 set_current_inferior (&mock_inferior
);
1589 /* Push the process_stratum target so we can mock accessing
1591 push_target (&mock_target
);
1593 /* Pop it again on exit (return/exception). */
1598 pop_all_targets_at_and_above (process_stratum
);
1602 /* Switch to the mock thread. */
1603 scoped_restore restore_inferior_ptid
1604 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1606 /* Test that read one raw register from regcache_no_target will go
1607 to the target layer. */
1610 /* Find a raw register which size isn't zero. */
1611 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1613 if (register_size (gdbarch
, regnum
) != 0)
1617 readwrite_regcache
readwrite (gdbarch
);
1618 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1620 readwrite
.raw_read (regnum
, buf
.data ());
1622 /* raw_read calls target_fetch_registers. */
1623 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1624 mock_target
.reset ();
1626 /* Mark all raw registers valid, so the following raw registers
1627 accesses won't go to target. */
1628 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1629 readwrite
.raw_update (i
);
1631 mock_target
.reset ();
1632 /* Then, read all raw and pseudo registers, and don't expect calling
1633 to_{fetch,store}_registers. */
1634 for (int regnum
= 0;
1635 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1638 if (register_size (gdbarch
, regnum
) == 0)
1641 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1643 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1645 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1646 SELF_CHECK (mock_target
.store_registers_called
== 0);
1648 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1649 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1650 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1652 mock_target
.reset ();
1655 readonly_detached_regcache
readonly (readwrite
);
1657 /* GDB may go to target layer to fetch all registers and memory for
1658 readonly regcache. */
1659 mock_target
.reset ();
1661 for (int regnum
= 0;
1662 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1665 if (register_size (gdbarch
, regnum
) == 0)
1668 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1669 enum register_status status
= readonly
.cooked_read (regnum
,
1672 if (regnum
< gdbarch_num_regs (gdbarch
))
1674 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1676 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1677 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1678 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1679 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1680 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1681 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1682 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1683 || bfd_arch
== bfd_arch_riscv
)
1685 /* Raw registers. If raw registers are not in save_reggroup,
1686 their status are unknown. */
1687 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1688 SELF_CHECK (status
== REG_VALID
);
1690 SELF_CHECK (status
== REG_UNKNOWN
);
1693 SELF_CHECK (status
== REG_VALID
);
1697 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1698 SELF_CHECK (status
== REG_VALID
);
1701 /* If pseudo registers are not in save_reggroup, some of
1702 them can be computed from saved raw registers, but some
1703 of them are unknown. */
1704 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1706 if (bfd_arch
== bfd_arch_frv
1707 || bfd_arch
== bfd_arch_m32c
1708 || bfd_arch
== bfd_arch_mep
1709 || bfd_arch
== bfd_arch_sh
)
1710 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1711 else if (bfd_arch
== bfd_arch_mips
1712 || bfd_arch
== bfd_arch_h8300
)
1713 SELF_CHECK (status
== REG_UNKNOWN
);
1715 SELF_CHECK (status
== REG_VALID
);
1719 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1720 SELF_CHECK (mock_target
.store_registers_called
== 0);
1721 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1723 mock_target
.reset ();
1727 /* Test regcache::cooked_write by writing some expected contents to
1728 registers, and checking that contents read from registers and the
1729 expected contents are the same. */
1732 cooked_write_test (struct gdbarch
*gdbarch
)
1734 /* Error out if debugging something, because we're going to push the
1735 test target, which would pop any existing target. */
1736 if (target_stack
->to_stratum
>= process_stratum
)
1737 error (_("target already pushed"));
1739 /* Create a mock environment. A process_stratum target pushed. */
1741 target_ops_no_register mock_target
;
1743 /* Push the process_stratum target so we can mock accessing
1745 push_target (&mock_target
);
1747 /* Pop it again on exit (return/exception). */
1752 pop_all_targets_at_and_above (process_stratum
);
1756 readwrite_regcache
readwrite (gdbarch
);
1758 const int num_regs
= (gdbarch_num_regs (gdbarch
)
1759 + gdbarch_num_pseudo_regs (gdbarch
));
1761 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1763 if (register_size (gdbarch
, regnum
) == 0
1764 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1767 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1769 if ((bfd_arch
== bfd_arch_sparc
1770 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1771 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1772 && gdbarch_ptr_bit (gdbarch
) == 64
1773 && (regnum
>= gdbarch_num_regs (gdbarch
)
1774 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1775 || (bfd_arch
== bfd_arch_spu
1776 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1777 TARGET_OBJECT_SPU. */
1778 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
1781 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1782 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1783 const auto type
= register_type (gdbarch
, regnum
);
1785 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1786 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1788 /* Generate valid float format. */
1789 target_float_from_string (expected
.data (), type
, "1.25");
1791 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1792 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1793 || TYPE_CODE (type
) == TYPE_CODE_PTR
1794 || TYPE_CODE (type
) == TYPE_CODE_UNION
1795 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1797 if (bfd_arch
== bfd_arch_ia64
1798 || (regnum
>= gdbarch_num_regs (gdbarch
)
1799 && (bfd_arch
== bfd_arch_xtensa
1800 || bfd_arch
== bfd_arch_bfin
1801 || bfd_arch
== bfd_arch_m32c
1802 /* m68hc11 pseudo registers are in memory. */
1803 || bfd_arch
== bfd_arch_m68hc11
1804 || bfd_arch
== bfd_arch_m68hc12
1805 || bfd_arch
== bfd_arch_s390
))
1806 || (bfd_arch
== bfd_arch_frv
1807 /* FRV pseudo registers except iacc0. */
1808 && regnum
> gdbarch_num_regs (gdbarch
)))
1810 /* Skip setting the expected values for some architecture
1813 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1815 /* RL78_PC_REGNUM */
1816 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1821 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1825 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1827 /* No idea how to test flags. */
1832 /* If we don't know how to create the expected value for the
1833 this type, make it fail. */
1837 readwrite
.cooked_write (regnum
, expected
.data ());
1839 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1840 SELF_CHECK (expected
== buf
);
1844 } // namespace selftests
1845 #endif /* GDB_SELF_TEST */
1848 _initialize_regcache (void)
1850 regcache_descr_handle
1851 = gdbarch_data_register_post_init (init_regcache_descr
);
1853 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1854 gdb::observers::thread_ptid_changed
.attach
1855 (regcache::regcache_thread_ptid_changed
);
1857 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1858 _("Force gdb to flush its register cache (maintainer command)"));
1861 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1863 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1864 selftests::cooked_read_test
);
1865 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1866 selftests::cooked_write_test
);