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 /* Cleanup class for invalidating a register. */
228 class regcache_invalidator
232 regcache_invalidator (struct regcache
*regcache
, int regnum
)
233 : m_regcache (regcache
),
238 ~regcache_invalidator ()
240 if (m_regcache
!= nullptr)
241 regcache_invalidate (m_regcache
, m_regnum
);
244 DISABLE_COPY_AND_ASSIGN (regcache_invalidator
);
248 m_regcache
= nullptr;
253 struct regcache
*m_regcache
;
257 /* Return a pointer to register REGNUM's buffer cache. */
260 reg_buffer::register_buffer (int regnum
) const
262 return m_registers
+ m_descr
->register_offset
[regnum
];
266 reg_buffer::save (regcache_cooked_read_ftype
*cooked_read
,
269 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
272 /* It should have pseudo registers. */
273 gdb_assert (m_has_pseudo
);
274 /* Clear the dest. */
275 memset (m_registers
, 0, m_descr
->sizeof_cooked_registers
);
276 memset (m_register_status
, 0, m_descr
->nr_cooked_registers
);
277 /* Copy over any registers (identified by their membership in the
278 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
279 gdbarch_num_pseudo_regs) range is checked since some architectures need
280 to save/restore `cooked' registers that live in memory. */
281 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
283 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
285 gdb_byte
*dst_buf
= register_buffer (regnum
);
286 enum register_status status
= cooked_read (src
, regnum
, dst_buf
);
288 gdb_assert (status
!= REG_UNKNOWN
);
290 if (status
!= REG_VALID
)
291 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
293 m_register_status
[regnum
] = status
;
299 regcache::restore (readonly_detached_regcache
*src
)
301 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
304 gdb_assert (src
!= NULL
);
305 gdb_assert (src
->m_has_pseudo
);
307 gdb_assert (gdbarch
== src
->arch ());
309 /* Copy over any registers, being careful to only restore those that
310 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
311 + gdbarch_num_pseudo_regs) range is checked since some architectures need
312 to save/restore `cooked' registers that live in memory. */
313 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
315 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
317 if (src
->m_register_status
[regnum
] == REG_VALID
)
318 cooked_write (regnum
, src
->register_buffer (regnum
));
324 reg_buffer::get_register_status (int regnum
) const
326 assert_regnum (regnum
);
328 return (enum register_status
) m_register_status
[regnum
];
332 regcache_invalidate (struct regcache
*regcache
, int regnum
)
334 gdb_assert (regcache
!= NULL
);
335 regcache
->invalidate (regnum
);
339 detached_regcache::invalidate (int regnum
)
341 assert_regnum (regnum
);
342 m_register_status
[regnum
] = REG_UNKNOWN
;
346 reg_buffer::assert_regnum (int regnum
) const
348 gdb_assert (regnum
>= 0);
350 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
352 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
355 /* Global structure containing the current regcache. */
357 /* NOTE: this is a write-through cache. There is no "dirty" bit for
358 recording if the register values have been changed (eg. by the
359 user). Therefore all registers must be written back to the
360 target when appropriate. */
361 std::forward_list
<regcache
*> regcache::current_regcache
;
364 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
365 struct address_space
*aspace
)
367 for (const auto ®cache
: regcache::current_regcache
)
368 if (ptid_equal (regcache
->ptid (), ptid
) && regcache
->arch () == gdbarch
)
371 regcache
*new_regcache
= new regcache (gdbarch
, aspace
);
373 regcache::current_regcache
.push_front (new_regcache
);
374 new_regcache
->set_ptid (ptid
);
380 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
382 address_space
*aspace
= target_thread_address_space (ptid
);
384 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
387 static ptid_t current_thread_ptid
;
388 static struct gdbarch
*current_thread_arch
;
391 get_thread_regcache (ptid_t ptid
)
393 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
395 current_thread_ptid
= ptid
;
396 current_thread_arch
= target_thread_architecture (ptid
);
399 return get_thread_arch_regcache (ptid
, current_thread_arch
);
403 get_current_regcache (void)
405 return get_thread_regcache (inferior_ptid
);
408 /* See common/common-regcache.h. */
411 get_thread_regcache_for_ptid (ptid_t ptid
)
413 return get_thread_regcache (ptid
);
416 /* Observer for the target_changed event. */
419 regcache_observer_target_changed (struct target_ops
*target
)
421 registers_changed ();
424 /* Update global variables old ptids to hold NEW_PTID if they were
427 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
429 for (auto ®cache
: regcache::current_regcache
)
431 if (ptid_equal (regcache
->ptid (), old_ptid
))
432 regcache
->set_ptid (new_ptid
);
436 /* Low level examining and depositing of registers.
438 The caller is responsible for making sure that the inferior is
439 stopped before calling the fetching routines, or it will get
440 garbage. (a change from GDB version 3, in which the caller got the
441 value from the last stop). */
443 /* REGISTERS_CHANGED ()
445 Indicate that registers may have changed, so invalidate the cache. */
448 registers_changed_ptid (ptid_t ptid
)
450 for (auto oit
= regcache::current_regcache
.before_begin (),
451 it
= std::next (oit
);
452 it
!= regcache::current_regcache
.end ();
455 if (ptid_match ((*it
)->ptid (), ptid
))
458 it
= regcache::current_regcache
.erase_after (oit
);
464 if (ptid_match (current_thread_ptid
, ptid
))
466 current_thread_ptid
= null_ptid
;
467 current_thread_arch
= NULL
;
470 if (ptid_match (inferior_ptid
, ptid
))
472 /* We just deleted the regcache of the current thread. Need to
473 forget about any frames we have cached, too. */
474 reinit_frame_cache ();
479 registers_changed (void)
481 registers_changed_ptid (minus_one_ptid
);
483 /* Force cleanup of any alloca areas if using C alloca instead of
484 a builtin alloca. This particular call is used to clean up
485 areas allocated by low level target code which may build up
486 during lengthy interactions between gdb and the target before
487 gdb gives control to the user (ie watchpoints). */
492 regcache_raw_update (struct regcache
*regcache
, int regnum
)
494 gdb_assert (regcache
!= NULL
);
496 regcache
->raw_update (regnum
);
500 regcache::raw_update (int regnum
)
502 assert_regnum (regnum
);
504 /* Make certain that the register cache is up-to-date with respect
505 to the current thread. This switching shouldn't be necessary
506 only there is still only one target side register cache. Sigh!
507 On the bright side, at least there is a regcache object. */
509 if (get_register_status (regnum
) == REG_UNKNOWN
)
511 target_fetch_registers (this, regnum
);
513 /* A number of targets can't access the whole set of raw
514 registers (because the debug API provides no means to get at
516 if (m_register_status
[regnum
] == REG_UNKNOWN
)
517 m_register_status
[regnum
] = REG_UNAVAILABLE
;
522 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
524 return regcache
->raw_read (regnum
, buf
);
528 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
530 gdb_assert (buf
!= NULL
);
533 if (m_register_status
[regnum
] != REG_VALID
)
534 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
536 memcpy (buf
, register_buffer (regnum
),
537 m_descr
->sizeof_register
[regnum
]);
539 return (enum register_status
) m_register_status
[regnum
];
543 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
545 gdb_assert (regcache
!= NULL
);
546 return regcache
->raw_read (regnum
, val
);
549 template<typename T
, typename
>
551 readable_regcache::raw_read (int regnum
, T
*val
)
554 enum register_status status
;
556 assert_regnum (regnum
);
557 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
558 status
= raw_read (regnum
, buf
);
559 if (status
== REG_VALID
)
560 *val
= extract_integer
<T
> (buf
,
561 m_descr
->sizeof_register
[regnum
],
562 gdbarch_byte_order (m_descr
->gdbarch
));
569 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
572 gdb_assert (regcache
!= NULL
);
573 return regcache
->raw_read (regnum
, val
);
577 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
579 gdb_assert (regcache
!= NULL
);
580 regcache
->raw_write (regnum
, val
);
583 template<typename T
, typename
>
585 regcache::raw_write (int regnum
, T val
)
589 assert_regnum (regnum
);
590 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
591 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
592 gdbarch_byte_order (m_descr
->gdbarch
), val
);
593 raw_write (regnum
, buf
);
597 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
600 gdb_assert (regcache
!= NULL
);
601 regcache
->raw_write (regnum
, val
);
605 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
608 enum register_status status
;
610 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
611 if (status
== REG_UNAVAILABLE
)
612 throw_error (NOT_AVAILABLE_ERROR
,
613 _("Register %d is not available"), regnum
);
618 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
620 return regcache
->cooked_read (regnum
, buf
);
624 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
626 gdb_assert (regnum
>= 0);
627 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
628 if (regnum
< num_raw_registers ())
629 return raw_read (regnum
, buf
);
630 else if (m_has_pseudo
631 && m_register_status
[regnum
] != REG_UNKNOWN
)
633 if (m_register_status
[regnum
] == REG_VALID
)
634 memcpy (buf
, register_buffer (regnum
),
635 m_descr
->sizeof_register
[regnum
]);
637 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
639 return (enum register_status
) m_register_status
[regnum
];
641 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
643 struct value
*mark
, *computed
;
644 enum register_status result
= REG_VALID
;
646 mark
= value_mark ();
648 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
650 if (value_entirely_available (computed
))
651 memcpy (buf
, value_contents_raw (computed
),
652 m_descr
->sizeof_register
[regnum
]);
655 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
656 result
= REG_UNAVAILABLE
;
659 value_free_to_mark (mark
);
664 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
669 regcache_cooked_read_value (struct regcache
*regcache
, int regnum
)
671 return regcache
->cooked_read_value (regnum
);
675 readable_regcache::cooked_read_value (int regnum
)
677 gdb_assert (regnum
>= 0);
678 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
680 if (regnum
< num_raw_registers ()
681 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
682 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
684 struct value
*result
;
686 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
687 VALUE_LVAL (result
) = lval_register
;
688 VALUE_REGNUM (result
) = regnum
;
690 /* It is more efficient in general to do this delegation in this
691 direction than in the other one, even though the value-based
693 if (cooked_read (regnum
,
694 value_contents_raw (result
)) == REG_UNAVAILABLE
)
695 mark_value_bytes_unavailable (result
, 0,
696 TYPE_LENGTH (value_type (result
)));
701 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
706 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
709 gdb_assert (regcache
!= NULL
);
710 return regcache
->cooked_read (regnum
, val
);
713 template<typename T
, typename
>
715 readable_regcache::cooked_read (int regnum
, T
*val
)
717 enum register_status status
;
720 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
721 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
722 status
= cooked_read (regnum
, buf
);
723 if (status
== REG_VALID
)
724 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
725 gdbarch_byte_order (m_descr
->gdbarch
));
732 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
735 gdb_assert (regcache
!= NULL
);
736 return regcache
->cooked_read (regnum
, val
);
740 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
743 gdb_assert (regcache
!= NULL
);
744 regcache
->cooked_write (regnum
, val
);
747 template<typename T
, typename
>
749 regcache::cooked_write (int regnum
, T val
)
753 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
754 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
755 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
756 gdbarch_byte_order (m_descr
->gdbarch
), val
);
757 cooked_write (regnum
, buf
);
761 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
764 gdb_assert (regcache
!= NULL
);
765 regcache
->cooked_write (regnum
, val
);
769 regcache_raw_write (struct regcache
*regcache
, int regnum
,
772 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
773 regcache
->raw_write (regnum
, buf
);
777 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
780 gdb_assert (buf
!= NULL
);
781 assert_regnum (regnum
);
783 /* On the sparc, writing %g0 is a no-op, so we don't even want to
784 change the registers array if something writes to this register. */
785 if (gdbarch_cannot_store_register (arch (), regnum
))
788 /* If we have a valid copy of the register, and new value == old
789 value, then don't bother doing the actual store. */
790 if (get_register_status (regnum
) == REG_VALID
791 && (memcmp (register_buffer (regnum
), buf
,
792 m_descr
->sizeof_register
[regnum
]) == 0))
795 target_prepare_to_store (this);
796 raw_supply (regnum
, buf
);
798 /* Invalidate the register after it is written, in case of a
800 regcache_invalidator
invalidator (this, regnum
);
802 target_store_registers (this, regnum
);
804 /* The target did not throw an error so we can discard invalidating
806 invalidator
.release ();
810 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
813 regcache
->cooked_write (regnum
, buf
);
817 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
819 gdb_assert (regnum
>= 0);
820 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
821 if (regnum
< num_raw_registers ())
822 raw_write (regnum
, buf
);
824 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
828 /* Perform a partial register transfer using a read, modify, write
832 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
835 struct gdbarch
*gdbarch
= arch ();
836 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
838 gdb_assert (in
!= NULL
);
839 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
840 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
841 /* Something to do? */
842 if (offset
+ len
== 0)
844 /* Read (when needed) ... */
845 enum register_status status
;
848 status
= raw_read (regnum
, reg
);
850 status
= cooked_read (regnum
, reg
);
851 if (status
!= REG_VALID
)
855 memcpy (in
, reg
+ offset
, len
);
861 regcache::write_part (int regnum
, int offset
, int len
,
862 const void *out
, bool is_raw
)
864 struct gdbarch
*gdbarch
= arch ();
865 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
867 gdb_assert (out
!= NULL
);
868 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
869 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
870 /* Something to do? */
871 if (offset
+ len
== 0)
873 /* Read (when needed) ... */
875 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
877 enum register_status status
;
880 status
= raw_read (regnum
, reg
);
882 status
= cooked_read (regnum
, reg
);
883 if (status
!= REG_VALID
)
887 memcpy (reg
+ offset
, out
, len
);
888 /* ... write (when needed). */
890 raw_write (regnum
, reg
);
892 cooked_write (regnum
, reg
);
898 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
899 int offset
, int len
, gdb_byte
*buf
)
901 return regcache
->raw_read_part (regnum
, offset
, len
, buf
);
905 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
907 assert_regnum (regnum
);
908 return read_part (regnum
, offset
, len
, buf
, true);
912 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
913 int offset
, int len
, const gdb_byte
*buf
)
915 regcache
->raw_write_part (regnum
, offset
, len
, buf
);
919 regcache::raw_write_part (int regnum
, int offset
, int len
,
922 assert_regnum (regnum
);
923 write_part (regnum
, offset
, len
, buf
, true);
927 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
928 int offset
, int len
, gdb_byte
*buf
)
930 return regcache
->cooked_read_part (regnum
, offset
, len
, buf
);
935 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
938 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
939 return read_part (regnum
, offset
, len
, buf
, false);
943 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
944 int offset
, int len
, const gdb_byte
*buf
)
946 regcache
->cooked_write_part (regnum
, offset
, len
, buf
);
950 regcache::cooked_write_part (int regnum
, int offset
, int len
,
953 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
954 write_part (regnum
, offset
, len
, buf
, false);
957 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
960 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
962 gdb_assert (regcache
!= NULL
);
963 regcache
->raw_supply (regnum
, buf
);
967 detached_regcache::raw_supply (int regnum
, const void *buf
)
972 assert_regnum (regnum
);
974 regbuf
= register_buffer (regnum
);
975 size
= m_descr
->sizeof_register
[regnum
];
979 memcpy (regbuf
, buf
, size
);
980 m_register_status
[regnum
] = REG_VALID
;
984 /* This memset not strictly necessary, but better than garbage
985 in case the register value manages to escape somewhere (due
986 to a bug, no less). */
987 memset (regbuf
, 0, size
);
988 m_register_status
[regnum
] = REG_UNAVAILABLE
;
992 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
993 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
994 the register size is greater than ADDR_LEN, then the integer will be sign or
995 zero extended. If the register size is smaller than the integer, then the
996 most significant bytes of the integer will be truncated. */
999 detached_regcache::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1000 int addr_len
, bool is_signed
)
1002 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1006 assert_regnum (regnum
);
1008 regbuf
= register_buffer (regnum
);
1009 regsize
= m_descr
->sizeof_register
[regnum
];
1011 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1013 m_register_status
[regnum
] = REG_VALID
;
1016 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1017 as calling raw_supply with NULL (which will set the state to
1021 detached_regcache::raw_supply_zeroed (int regnum
)
1026 assert_regnum (regnum
);
1028 regbuf
= register_buffer (regnum
);
1029 size
= m_descr
->sizeof_register
[regnum
];
1031 memset (regbuf
, 0, size
);
1032 m_register_status
[regnum
] = REG_VALID
;
1035 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1038 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
1040 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
1041 regcache
->raw_collect (regnum
, buf
);
1045 regcache::raw_collect (int regnum
, void *buf
) const
1050 gdb_assert (buf
!= NULL
);
1051 assert_regnum (regnum
);
1053 regbuf
= register_buffer (regnum
);
1054 size
= m_descr
->sizeof_register
[regnum
];
1055 memcpy (buf
, regbuf
, size
);
1058 /* Transfer a single or all registers belonging to a certain register
1059 set to or from a buffer. This is the main worker function for
1060 regcache_supply_regset and regcache_collect_regset. */
1062 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1063 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1064 If ADDR_LEN is greater than the register size, then the integer will be sign
1065 or zero extended. If ADDR_LEN is smaller than the register size, then the
1066 most significant bytes of the integer will be truncated. */
1069 regcache::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1070 bool is_signed
) const
1072 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1073 const gdb_byte
*regbuf
;
1076 assert_regnum (regnum
);
1078 regbuf
= register_buffer (regnum
);
1079 regsize
= m_descr
->sizeof_register
[regnum
];
1081 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1086 regcache::transfer_regset (const struct regset
*regset
,
1087 struct regcache
*out_regcache
,
1088 int regnum
, const void *in_buf
,
1089 void *out_buf
, size_t size
) const
1091 const struct regcache_map_entry
*map
;
1092 int offs
= 0, count
;
1094 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1095 (count
= map
->count
) != 0;
1098 int regno
= map
->regno
;
1099 int slot_size
= map
->size
;
1101 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1102 slot_size
= m_descr
->sizeof_register
[regno
];
1104 if (regno
== REGCACHE_MAP_SKIP
1106 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1107 offs
+= count
* slot_size
;
1109 else if (regnum
== -1)
1110 for (; count
--; regno
++, offs
+= slot_size
)
1112 if (offs
+ slot_size
> size
)
1116 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1118 out_regcache
->raw_supply (regno
, in_buf
1119 ? (const gdb_byte
*) in_buf
+ offs
1124 /* Transfer a single register and return. */
1125 offs
+= (regnum
- regno
) * slot_size
;
1126 if (offs
+ slot_size
> size
)
1130 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1132 out_regcache
->raw_supply (regnum
, in_buf
1133 ? (const gdb_byte
*) in_buf
+ offs
1140 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1141 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1142 If BUF is NULL, set the register(s) to "unavailable" status. */
1145 regcache_supply_regset (const struct regset
*regset
,
1146 struct regcache
*regcache
,
1147 int regnum
, const void *buf
, size_t size
)
1149 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1153 regcache::supply_regset (const struct regset
*regset
,
1154 int regnum
, const void *buf
, size_t size
)
1156 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1159 /* Collect register REGNUM from REGCACHE to BUF, using the register
1160 map in REGSET. If REGNUM is -1, do this for all registers in
1164 regcache_collect_regset (const struct regset
*regset
,
1165 const struct regcache
*regcache
,
1166 int regnum
, void *buf
, size_t size
)
1168 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1172 regcache::collect_regset (const struct regset
*regset
,
1173 int regnum
, void *buf
, size_t size
) const
1175 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1179 /* Special handling for register PC. */
1182 regcache_read_pc (struct regcache
*regcache
)
1184 struct gdbarch
*gdbarch
= regcache
->arch ();
1188 if (gdbarch_read_pc_p (gdbarch
))
1189 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1190 /* Else use per-frame method on get_current_frame. */
1191 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1195 if (regcache_cooked_read_unsigned (regcache
,
1196 gdbarch_pc_regnum (gdbarch
),
1197 &raw_val
) == REG_UNAVAILABLE
)
1198 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1200 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1203 internal_error (__FILE__
, __LINE__
,
1204 _("regcache_read_pc: Unable to find PC"));
1209 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1211 struct gdbarch
*gdbarch
= regcache
->arch ();
1213 if (gdbarch_write_pc_p (gdbarch
))
1214 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1215 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1216 regcache_cooked_write_unsigned (regcache
,
1217 gdbarch_pc_regnum (gdbarch
), pc
);
1219 internal_error (__FILE__
, __LINE__
,
1220 _("regcache_write_pc: Unable to update PC"));
1222 /* Writing the PC (for instance, from "load") invalidates the
1224 reinit_frame_cache ();
1228 reg_buffer::num_raw_registers () const
1230 return gdbarch_num_regs (arch ());
1234 regcache::debug_print_register (const char *func
, int regno
)
1236 struct gdbarch
*gdbarch
= arch ();
1238 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1239 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1240 && gdbarch_register_name (gdbarch
, regno
) != NULL
1241 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1242 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1243 gdbarch_register_name (gdbarch
, regno
));
1245 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1246 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1248 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1249 int size
= register_size (gdbarch
, regno
);
1250 gdb_byte
*buf
= register_buffer (regno
);
1252 fprintf_unfiltered (gdb_stdlog
, " = ");
1253 for (int i
= 0; i
< size
; i
++)
1255 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1257 if (size
<= sizeof (LONGEST
))
1259 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1261 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1262 core_addr_to_string_nz (val
), plongest (val
));
1265 fprintf_unfiltered (gdb_stdlog
, "\n");
1269 reg_flush_command (const char *command
, int from_tty
)
1271 /* Force-flush the register cache. */
1272 registers_changed ();
1274 printf_filtered (_("Register cache flushed.\n"));
1278 register_dump::dump (ui_file
*file
)
1280 auto descr
= regcache_descr (m_gdbarch
);
1282 int footnote_nr
= 0;
1283 int footnote_register_offset
= 0;
1284 int footnote_register_type_name_null
= 0;
1285 long register_offset
= 0;
1287 gdb_assert (descr
->nr_cooked_registers
1288 == (gdbarch_num_regs (m_gdbarch
)
1289 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1291 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1295 fprintf_unfiltered (file
, " %-10s", "Name");
1298 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1302 else if (p
[0] == '\0')
1304 fprintf_unfiltered (file
, " %-10s", p
);
1309 fprintf_unfiltered (file
, " %4s", "Nr");
1311 fprintf_unfiltered (file
, " %4d", regnum
);
1313 /* Relative number. */
1315 fprintf_unfiltered (file
, " %4s", "Rel");
1316 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1317 fprintf_unfiltered (file
, " %4d", regnum
);
1319 fprintf_unfiltered (file
, " %4d",
1320 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1324 fprintf_unfiltered (file
, " %6s ", "Offset");
1327 fprintf_unfiltered (file
, " %6ld",
1328 descr
->register_offset
[regnum
]);
1329 if (register_offset
!= descr
->register_offset
[regnum
]
1331 && (descr
->register_offset
[regnum
]
1332 != (descr
->register_offset
[regnum
- 1]
1333 + descr
->sizeof_register
[regnum
- 1])))
1336 if (!footnote_register_offset
)
1337 footnote_register_offset
= ++footnote_nr
;
1338 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1341 fprintf_unfiltered (file
, " ");
1342 register_offset
= (descr
->register_offset
[regnum
]
1343 + descr
->sizeof_register
[regnum
]);
1348 fprintf_unfiltered (file
, " %5s ", "Size");
1350 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1355 std::string name_holder
;
1361 static const char blt
[] = "builtin_type";
1363 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1366 if (!footnote_register_type_name_null
)
1367 footnote_register_type_name_null
= ++footnote_nr
;
1368 name_holder
= string_printf ("*%d",
1369 footnote_register_type_name_null
);
1370 t
= name_holder
.c_str ();
1372 /* Chop a leading builtin_type. */
1373 if (startswith (t
, blt
))
1376 fprintf_unfiltered (file
, " %-15s", t
);
1379 /* Leading space always present. */
1380 fprintf_unfiltered (file
, " ");
1382 dump_reg (file
, regnum
);
1384 fprintf_unfiltered (file
, "\n");
1387 if (footnote_register_offset
)
1388 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1389 footnote_register_offset
);
1390 if (footnote_register_type_name_null
)
1391 fprintf_unfiltered (file
,
1392 "*%d: Register type's name NULL.\n",
1393 footnote_register_type_name_null
);
1397 #include "selftest.h"
1398 #include "selftest-arch.h"
1399 #include "gdbthread.h"
1400 #include "target-float.h"
1402 namespace selftests
{
1404 class regcache_access
: public regcache
1408 /* Return the number of elements in current_regcache. */
1411 current_regcache_size ()
1413 return std::distance (regcache::current_regcache
.begin (),
1414 regcache::current_regcache
.end ());
1419 current_regcache_test (void)
1421 /* It is empty at the start. */
1422 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1424 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1426 /* Get regcache from ptid1, a new regcache is added to
1427 current_regcache. */
1428 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1432 SELF_CHECK (regcache
!= NULL
);
1433 SELF_CHECK (regcache
->ptid () == ptid1
);
1434 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1436 /* Get regcache from ptid2, a new regcache is added to
1437 current_regcache. */
1438 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1441 SELF_CHECK (regcache
!= NULL
);
1442 SELF_CHECK (regcache
->ptid () == ptid2
);
1443 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1445 /* Get regcache from ptid3, a new regcache is added to
1446 current_regcache. */
1447 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1450 SELF_CHECK (regcache
!= NULL
);
1451 SELF_CHECK (regcache
->ptid () == ptid3
);
1452 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1454 /* Get regcache from ptid2 again, nothing is added to
1455 current_regcache. */
1456 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1459 SELF_CHECK (regcache
!= NULL
);
1460 SELF_CHECK (regcache
->ptid () == ptid2
);
1461 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1463 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1464 current_regcache. */
1465 registers_changed_ptid (ptid2
);
1466 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1469 class target_ops_no_register
: public test_target_ops
1472 target_ops_no_register ()
1473 : test_target_ops
{}
1478 fetch_registers_called
= 0;
1479 store_registers_called
= 0;
1480 xfer_partial_called
= 0;
1483 void fetch_registers (regcache
*regs
, int regno
) override
;
1484 void store_registers (regcache
*regs
, int regno
) override
;
1486 enum target_xfer_status
xfer_partial (enum target_object object
,
1487 const char *annex
, gdb_byte
*readbuf
,
1488 const gdb_byte
*writebuf
,
1489 ULONGEST offset
, ULONGEST len
,
1490 ULONGEST
*xfered_len
) override
;
1492 unsigned int fetch_registers_called
= 0;
1493 unsigned int store_registers_called
= 0;
1494 unsigned int xfer_partial_called
= 0;
1498 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1500 /* Mark register available. */
1501 regs
->raw_supply_zeroed (regno
);
1502 this->fetch_registers_called
++;
1506 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1508 this->store_registers_called
++;
1511 enum target_xfer_status
1512 target_ops_no_register::xfer_partial (enum target_object object
,
1513 const char *annex
, gdb_byte
*readbuf
,
1514 const gdb_byte
*writebuf
,
1515 ULONGEST offset
, ULONGEST len
,
1516 ULONGEST
*xfered_len
)
1518 this->xfer_partial_called
++;
1521 return TARGET_XFER_OK
;
1524 class readwrite_regcache
: public regcache
1527 readwrite_regcache (struct gdbarch
*gdbarch
)
1528 : regcache (gdbarch
, nullptr)
1532 /* Test regcache::cooked_read gets registers from raw registers and
1533 memory instead of target to_{fetch,store}_registers. */
1536 cooked_read_test (struct gdbarch
*gdbarch
)
1538 /* Error out if debugging something, because we're going to push the
1539 test target, which would pop any existing target. */
1540 if (target_stack
->to_stratum
>= process_stratum
)
1541 error (_("target already pushed"));
1543 /* Create a mock environment. An inferior with a thread, with a
1544 process_stratum target pushed. */
1546 target_ops_no_register mock_target
;
1547 ptid_t
mock_ptid (1, 1);
1548 inferior
mock_inferior (mock_ptid
.pid ());
1549 address_space mock_aspace
{};
1550 mock_inferior
.gdbarch
= gdbarch
;
1551 mock_inferior
.aspace
= &mock_aspace
;
1552 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1554 scoped_restore restore_thread_list
1555 = make_scoped_restore (&thread_list
, &mock_thread
);
1557 /* Add the mock inferior to the inferior list so that look ups by
1558 target+ptid can find it. */
1559 scoped_restore restore_inferior_list
1560 = make_scoped_restore (&inferior_list
);
1561 inferior_list
= &mock_inferior
;
1563 /* Switch to the mock inferior. */
1564 scoped_restore_current_inferior restore_current_inferior
;
1565 set_current_inferior (&mock_inferior
);
1567 /* Push the process_stratum target so we can mock accessing
1569 push_target (&mock_target
);
1571 /* Pop it again on exit (return/exception). */
1576 pop_all_targets_at_and_above (process_stratum
);
1580 /* Switch to the mock thread. */
1581 scoped_restore restore_inferior_ptid
1582 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1584 /* Test that read one raw register from regcache_no_target will go
1585 to the target layer. */
1588 /* Find a raw register which size isn't zero. */
1589 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1591 if (register_size (gdbarch
, regnum
) != 0)
1595 readwrite_regcache
readwrite (gdbarch
);
1596 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1598 readwrite
.raw_read (regnum
, buf
.data ());
1600 /* raw_read calls target_fetch_registers. */
1601 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1602 mock_target
.reset ();
1604 /* Mark all raw registers valid, so the following raw registers
1605 accesses won't go to target. */
1606 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1607 readwrite
.raw_update (i
);
1609 mock_target
.reset ();
1610 /* Then, read all raw and pseudo registers, and don't expect calling
1611 to_{fetch,store}_registers. */
1612 for (int regnum
= 0;
1613 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1616 if (register_size (gdbarch
, regnum
) == 0)
1619 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1621 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1623 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1624 SELF_CHECK (mock_target
.store_registers_called
== 0);
1626 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1627 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1628 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1630 mock_target
.reset ();
1633 readonly_detached_regcache
readonly (readwrite
);
1635 /* GDB may go to target layer to fetch all registers and memory for
1636 readonly regcache. */
1637 mock_target
.reset ();
1639 for (int regnum
= 0;
1640 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1643 if (register_size (gdbarch
, regnum
) == 0)
1646 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1647 enum register_status status
= readonly
.cooked_read (regnum
,
1650 if (regnum
< gdbarch_num_regs (gdbarch
))
1652 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1654 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1655 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1656 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1657 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1658 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1659 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1660 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1661 || bfd_arch
== bfd_arch_riscv
)
1663 /* Raw registers. If raw registers are not in save_reggroup,
1664 their status are unknown. */
1665 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1666 SELF_CHECK (status
== REG_VALID
);
1668 SELF_CHECK (status
== REG_UNKNOWN
);
1671 SELF_CHECK (status
== REG_VALID
);
1675 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1676 SELF_CHECK (status
== REG_VALID
);
1679 /* If pseudo registers are not in save_reggroup, some of
1680 them can be computed from saved raw registers, but some
1681 of them are unknown. */
1682 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1684 if (bfd_arch
== bfd_arch_frv
1685 || bfd_arch
== bfd_arch_m32c
1686 || bfd_arch
== bfd_arch_mep
1687 || bfd_arch
== bfd_arch_sh
)
1688 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1689 else if (bfd_arch
== bfd_arch_mips
1690 || bfd_arch
== bfd_arch_h8300
)
1691 SELF_CHECK (status
== REG_UNKNOWN
);
1693 SELF_CHECK (status
== REG_VALID
);
1697 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1698 SELF_CHECK (mock_target
.store_registers_called
== 0);
1699 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1701 mock_target
.reset ();
1705 /* Test regcache::cooked_write by writing some expected contents to
1706 registers, and checking that contents read from registers and the
1707 expected contents are the same. */
1710 cooked_write_test (struct gdbarch
*gdbarch
)
1712 /* Error out if debugging something, because we're going to push the
1713 test target, which would pop any existing target. */
1714 if (target_stack
->to_stratum
>= process_stratum
)
1715 error (_("target already pushed"));
1717 /* Create a mock environment. A process_stratum target pushed. */
1719 target_ops_no_register mock_target
;
1721 /* Push the process_stratum target so we can mock accessing
1723 push_target (&mock_target
);
1725 /* Pop it again on exit (return/exception). */
1730 pop_all_targets_at_and_above (process_stratum
);
1734 readwrite_regcache
readwrite (gdbarch
);
1736 const int num_regs
= (gdbarch_num_regs (gdbarch
)
1737 + gdbarch_num_pseudo_regs (gdbarch
));
1739 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1741 if (register_size (gdbarch
, regnum
) == 0
1742 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1745 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1747 if ((bfd_arch
== bfd_arch_sparc
1748 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1749 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1750 && gdbarch_ptr_bit (gdbarch
) == 64
1751 && (regnum
>= gdbarch_num_regs (gdbarch
)
1752 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1753 || (bfd_arch
== bfd_arch_spu
1754 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1755 TARGET_OBJECT_SPU. */
1756 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
1759 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1760 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1761 const auto type
= register_type (gdbarch
, regnum
);
1763 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1764 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1766 /* Generate valid float format. */
1767 target_float_from_string (expected
.data (), type
, "1.25");
1769 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1770 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1771 || TYPE_CODE (type
) == TYPE_CODE_PTR
1772 || TYPE_CODE (type
) == TYPE_CODE_UNION
1773 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1775 if (bfd_arch
== bfd_arch_ia64
1776 || (regnum
>= gdbarch_num_regs (gdbarch
)
1777 && (bfd_arch
== bfd_arch_xtensa
1778 || bfd_arch
== bfd_arch_bfin
1779 || bfd_arch
== bfd_arch_m32c
1780 /* m68hc11 pseudo registers are in memory. */
1781 || bfd_arch
== bfd_arch_m68hc11
1782 || bfd_arch
== bfd_arch_m68hc12
1783 || bfd_arch
== bfd_arch_s390
))
1784 || (bfd_arch
== bfd_arch_frv
1785 /* FRV pseudo registers except iacc0. */
1786 && regnum
> gdbarch_num_regs (gdbarch
)))
1788 /* Skip setting the expected values for some architecture
1791 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1793 /* RL78_PC_REGNUM */
1794 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1799 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1803 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1805 /* No idea how to test flags. */
1810 /* If we don't know how to create the expected value for the
1811 this type, make it fail. */
1815 readwrite
.cooked_write (regnum
, expected
.data ());
1817 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1818 SELF_CHECK (expected
== buf
);
1822 } // namespace selftests
1823 #endif /* GDB_SELF_TEST */
1826 _initialize_regcache (void)
1828 regcache_descr_handle
1829 = gdbarch_data_register_post_init (init_regcache_descr
);
1831 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1832 gdb::observers::thread_ptid_changed
.attach
1833 (regcache::regcache_thread_ptid_changed
);
1835 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1836 _("Force gdb to flush its register cache (maintainer command)"));
1839 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1841 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1842 selftests::cooked_read_test
);
1843 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1844 selftests::cooked_write_test
);