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 (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 (int regnum
)
494 assert_regnum (regnum
);
496 /* Make certain that the register cache is up-to-date with respect
497 to the current thread. This switching shouldn't be necessary
498 only there is still only one target side register cache. Sigh!
499 On the bright side, at least there is a regcache object. */
501 if (get_register_status (regnum
) == REG_UNKNOWN
)
503 target_fetch_registers (this, regnum
);
505 /* A number of targets can't access the whole set of raw
506 registers (because the debug API provides no means to get at
508 if (m_register_status
[regnum
] == REG_UNKNOWN
)
509 m_register_status
[regnum
] = REG_UNAVAILABLE
;
514 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
516 gdb_assert (buf
!= NULL
);
519 if (m_register_status
[regnum
] != REG_VALID
)
520 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
522 memcpy (buf
, register_buffer (regnum
),
523 m_descr
->sizeof_register
[regnum
]);
525 return (enum register_status
) m_register_status
[regnum
];
529 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
531 gdb_assert (regcache
!= NULL
);
532 return regcache
->raw_read (regnum
, val
);
535 template<typename T
, typename
>
537 readable_regcache::raw_read (int regnum
, T
*val
)
540 enum register_status status
;
542 assert_regnum (regnum
);
543 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
544 status
= raw_read (regnum
, buf
);
545 if (status
== REG_VALID
)
546 *val
= extract_integer
<T
> (buf
,
547 m_descr
->sizeof_register
[regnum
],
548 gdbarch_byte_order (m_descr
->gdbarch
));
555 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
558 gdb_assert (regcache
!= NULL
);
559 return regcache
->raw_read (regnum
, val
);
563 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
565 gdb_assert (regcache
!= NULL
);
566 regcache
->raw_write (regnum
, val
);
569 template<typename T
, typename
>
571 regcache::raw_write (int regnum
, T val
)
575 assert_regnum (regnum
);
576 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
577 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
578 gdbarch_byte_order (m_descr
->gdbarch
), val
);
579 raw_write (regnum
, buf
);
583 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
586 gdb_assert (regcache
!= NULL
);
587 regcache
->raw_write (regnum
, val
);
591 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
594 enum register_status status
;
596 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
597 if (status
== REG_UNAVAILABLE
)
598 throw_error (NOT_AVAILABLE_ERROR
,
599 _("Register %d is not available"), regnum
);
604 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
606 gdb_assert (regnum
>= 0);
607 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
608 if (regnum
< num_raw_registers ())
609 return raw_read (regnum
, buf
);
610 else if (m_has_pseudo
611 && m_register_status
[regnum
] != REG_UNKNOWN
)
613 if (m_register_status
[regnum
] == REG_VALID
)
614 memcpy (buf
, register_buffer (regnum
),
615 m_descr
->sizeof_register
[regnum
]);
617 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
619 return (enum register_status
) m_register_status
[regnum
];
621 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
623 struct value
*mark
, *computed
;
624 enum register_status result
= REG_VALID
;
626 mark
= value_mark ();
628 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
630 if (value_entirely_available (computed
))
631 memcpy (buf
, value_contents_raw (computed
),
632 m_descr
->sizeof_register
[regnum
]);
635 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
636 result
= REG_UNAVAILABLE
;
639 value_free_to_mark (mark
);
644 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
649 regcache_cooked_read_value (struct regcache
*regcache
, int regnum
)
651 return regcache
->cooked_read_value (regnum
);
655 readable_regcache::cooked_read_value (int regnum
)
657 gdb_assert (regnum
>= 0);
658 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
660 if (regnum
< num_raw_registers ()
661 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
662 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
664 struct value
*result
;
666 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
667 VALUE_LVAL (result
) = lval_register
;
668 VALUE_REGNUM (result
) = regnum
;
670 /* It is more efficient in general to do this delegation in this
671 direction than in the other one, even though the value-based
673 if (cooked_read (regnum
,
674 value_contents_raw (result
)) == REG_UNAVAILABLE
)
675 mark_value_bytes_unavailable (result
, 0,
676 TYPE_LENGTH (value_type (result
)));
681 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
686 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
689 gdb_assert (regcache
!= NULL
);
690 return regcache
->cooked_read (regnum
, val
);
693 template<typename T
, typename
>
695 readable_regcache::cooked_read (int regnum
, T
*val
)
697 enum register_status status
;
700 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
701 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
702 status
= cooked_read (regnum
, buf
);
703 if (status
== REG_VALID
)
704 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
705 gdbarch_byte_order (m_descr
->gdbarch
));
712 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
715 gdb_assert (regcache
!= NULL
);
716 return regcache
->cooked_read (regnum
, val
);
720 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
723 gdb_assert (regcache
!= NULL
);
724 regcache
->cooked_write (regnum
, val
);
727 template<typename T
, typename
>
729 regcache::cooked_write (int regnum
, T val
)
733 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
734 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
735 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
736 gdbarch_byte_order (m_descr
->gdbarch
), val
);
737 cooked_write (regnum
, buf
);
741 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
744 gdb_assert (regcache
!= NULL
);
745 regcache
->cooked_write (regnum
, val
);
749 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
752 gdb_assert (buf
!= NULL
);
753 assert_regnum (regnum
);
755 /* On the sparc, writing %g0 is a no-op, so we don't even want to
756 change the registers array if something writes to this register. */
757 if (gdbarch_cannot_store_register (arch (), regnum
))
760 /* If we have a valid copy of the register, and new value == old
761 value, then don't bother doing the actual store. */
762 if (get_register_status (regnum
) == REG_VALID
763 && (memcmp (register_buffer (regnum
), buf
,
764 m_descr
->sizeof_register
[regnum
]) == 0))
767 target_prepare_to_store (this);
768 raw_supply (regnum
, buf
);
770 /* Invalidate the register after it is written, in case of a
772 regcache_invalidator
invalidator (this, regnum
);
774 target_store_registers (this, regnum
);
776 /* The target did not throw an error so we can discard invalidating
778 invalidator
.release ();
782 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
785 regcache
->cooked_write (regnum
, buf
);
789 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
791 gdb_assert (regnum
>= 0);
792 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
793 if (regnum
< num_raw_registers ())
794 raw_write (regnum
, buf
);
796 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
800 /* Perform a partial register transfer using a read, modify, write
804 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
807 struct gdbarch
*gdbarch
= arch ();
808 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
810 gdb_assert (in
!= NULL
);
811 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
812 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
813 /* Something to do? */
814 if (offset
+ len
== 0)
816 /* Read (when needed) ... */
817 enum register_status status
;
820 status
= raw_read (regnum
, reg
);
822 status
= cooked_read (regnum
, reg
);
823 if (status
!= REG_VALID
)
827 memcpy (in
, reg
+ offset
, len
);
833 regcache::write_part (int regnum
, int offset
, int len
,
834 const void *out
, bool is_raw
)
836 struct gdbarch
*gdbarch
= arch ();
837 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
839 gdb_assert (out
!= NULL
);
840 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
841 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
842 /* Something to do? */
843 if (offset
+ len
== 0)
845 /* Read (when needed) ... */
847 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
849 enum register_status status
;
852 status
= raw_read (regnum
, reg
);
854 status
= cooked_read (regnum
, reg
);
855 if (status
!= REG_VALID
)
859 memcpy (reg
+ offset
, out
, len
);
860 /* ... write (when needed). */
862 raw_write (regnum
, reg
);
864 cooked_write (regnum
, reg
);
870 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
872 assert_regnum (regnum
);
873 return read_part (regnum
, offset
, len
, buf
, true);
877 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
878 int offset
, int len
, const gdb_byte
*buf
)
880 regcache
->raw_write_part (regnum
, offset
, len
, buf
);
884 regcache::raw_write_part (int regnum
, int offset
, int len
,
887 assert_regnum (regnum
);
888 write_part (regnum
, offset
, len
, buf
, true);
892 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
893 int offset
, int len
, gdb_byte
*buf
)
895 return regcache
->cooked_read_part (regnum
, offset
, len
, buf
);
900 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
903 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
904 return read_part (regnum
, offset
, len
, buf
, false);
908 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
909 int offset
, int len
, const gdb_byte
*buf
)
911 regcache
->cooked_write_part (regnum
, offset
, len
, buf
);
915 regcache::cooked_write_part (int regnum
, int offset
, int len
,
918 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
919 write_part (regnum
, offset
, len
, buf
, false);
922 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
925 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
927 gdb_assert (regcache
!= NULL
);
928 regcache
->raw_supply (regnum
, buf
);
932 detached_regcache::raw_supply (int regnum
, const void *buf
)
937 assert_regnum (regnum
);
939 regbuf
= register_buffer (regnum
);
940 size
= m_descr
->sizeof_register
[regnum
];
944 memcpy (regbuf
, buf
, size
);
945 m_register_status
[regnum
] = REG_VALID
;
949 /* This memset not strictly necessary, but better than garbage
950 in case the register value manages to escape somewhere (due
951 to a bug, no less). */
952 memset (regbuf
, 0, size
);
953 m_register_status
[regnum
] = REG_UNAVAILABLE
;
957 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
958 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
959 the register size is greater than ADDR_LEN, then the integer will be sign or
960 zero extended. If the register size is smaller than the integer, then the
961 most significant bytes of the integer will be truncated. */
964 detached_regcache::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
965 int addr_len
, bool is_signed
)
967 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
971 assert_regnum (regnum
);
973 regbuf
= register_buffer (regnum
);
974 regsize
= m_descr
->sizeof_register
[regnum
];
976 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
978 m_register_status
[regnum
] = REG_VALID
;
981 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
982 as calling raw_supply with NULL (which will set the state to
986 detached_regcache::raw_supply_zeroed (int regnum
)
991 assert_regnum (regnum
);
993 regbuf
= register_buffer (regnum
);
994 size
= m_descr
->sizeof_register
[regnum
];
996 memset (regbuf
, 0, size
);
997 m_register_status
[regnum
] = REG_VALID
;
1000 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1003 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
1005 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
1006 regcache
->raw_collect (regnum
, buf
);
1010 regcache::raw_collect (int regnum
, void *buf
) const
1015 gdb_assert (buf
!= NULL
);
1016 assert_regnum (regnum
);
1018 regbuf
= register_buffer (regnum
);
1019 size
= m_descr
->sizeof_register
[regnum
];
1020 memcpy (buf
, regbuf
, size
);
1023 /* Transfer a single or all registers belonging to a certain register
1024 set to or from a buffer. This is the main worker function for
1025 regcache_supply_regset and regcache_collect_regset. */
1027 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1028 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1029 If ADDR_LEN is greater than the register size, then the integer will be sign
1030 or zero extended. If ADDR_LEN is smaller than the register size, then the
1031 most significant bytes of the integer will be truncated. */
1034 regcache::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1035 bool is_signed
) const
1037 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1038 const gdb_byte
*regbuf
;
1041 assert_regnum (regnum
);
1043 regbuf
= register_buffer (regnum
);
1044 regsize
= m_descr
->sizeof_register
[regnum
];
1046 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1051 regcache::transfer_regset (const struct regset
*regset
,
1052 struct regcache
*out_regcache
,
1053 int regnum
, const void *in_buf
,
1054 void *out_buf
, size_t size
) const
1056 const struct regcache_map_entry
*map
;
1057 int offs
= 0, count
;
1059 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1060 (count
= map
->count
) != 0;
1063 int regno
= map
->regno
;
1064 int slot_size
= map
->size
;
1066 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1067 slot_size
= m_descr
->sizeof_register
[regno
];
1069 if (regno
== REGCACHE_MAP_SKIP
1071 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1072 offs
+= count
* slot_size
;
1074 else if (regnum
== -1)
1075 for (; count
--; regno
++, offs
+= slot_size
)
1077 if (offs
+ slot_size
> size
)
1081 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1083 out_regcache
->raw_supply (regno
, in_buf
1084 ? (const gdb_byte
*) in_buf
+ offs
1089 /* Transfer a single register and return. */
1090 offs
+= (regnum
- regno
) * slot_size
;
1091 if (offs
+ slot_size
> size
)
1095 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1097 out_regcache
->raw_supply (regnum
, in_buf
1098 ? (const gdb_byte
*) in_buf
+ offs
1105 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1106 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1107 If BUF is NULL, set the register(s) to "unavailable" status. */
1110 regcache_supply_regset (const struct regset
*regset
,
1111 struct regcache
*regcache
,
1112 int regnum
, const void *buf
, size_t size
)
1114 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1118 regcache::supply_regset (const struct regset
*regset
,
1119 int regnum
, const void *buf
, size_t size
)
1121 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1124 /* Collect register REGNUM from REGCACHE to BUF, using the register
1125 map in REGSET. If REGNUM is -1, do this for all registers in
1129 regcache_collect_regset (const struct regset
*regset
,
1130 const struct regcache
*regcache
,
1131 int regnum
, void *buf
, size_t size
)
1133 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1137 regcache::collect_regset (const struct regset
*regset
,
1138 int regnum
, void *buf
, size_t size
) const
1140 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1144 /* Special handling for register PC. */
1147 regcache_read_pc (struct regcache
*regcache
)
1149 struct gdbarch
*gdbarch
= regcache
->arch ();
1153 if (gdbarch_read_pc_p (gdbarch
))
1154 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1155 /* Else use per-frame method on get_current_frame. */
1156 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1160 if (regcache_cooked_read_unsigned (regcache
,
1161 gdbarch_pc_regnum (gdbarch
),
1162 &raw_val
) == REG_UNAVAILABLE
)
1163 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1165 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1168 internal_error (__FILE__
, __LINE__
,
1169 _("regcache_read_pc: Unable to find PC"));
1174 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1176 struct gdbarch
*gdbarch
= regcache
->arch ();
1178 if (gdbarch_write_pc_p (gdbarch
))
1179 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1180 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1181 regcache_cooked_write_unsigned (regcache
,
1182 gdbarch_pc_regnum (gdbarch
), pc
);
1184 internal_error (__FILE__
, __LINE__
,
1185 _("regcache_write_pc: Unable to update PC"));
1187 /* Writing the PC (for instance, from "load") invalidates the
1189 reinit_frame_cache ();
1193 reg_buffer::num_raw_registers () const
1195 return gdbarch_num_regs (arch ());
1199 regcache::debug_print_register (const char *func
, int regno
)
1201 struct gdbarch
*gdbarch
= arch ();
1203 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1204 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1205 && gdbarch_register_name (gdbarch
, regno
) != NULL
1206 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1207 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1208 gdbarch_register_name (gdbarch
, regno
));
1210 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1211 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1213 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1214 int size
= register_size (gdbarch
, regno
);
1215 gdb_byte
*buf
= register_buffer (regno
);
1217 fprintf_unfiltered (gdb_stdlog
, " = ");
1218 for (int i
= 0; i
< size
; i
++)
1220 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1222 if (size
<= sizeof (LONGEST
))
1224 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1226 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1227 core_addr_to_string_nz (val
), plongest (val
));
1230 fprintf_unfiltered (gdb_stdlog
, "\n");
1234 reg_flush_command (const char *command
, int from_tty
)
1236 /* Force-flush the register cache. */
1237 registers_changed ();
1239 printf_filtered (_("Register cache flushed.\n"));
1243 register_dump::dump (ui_file
*file
)
1245 auto descr
= regcache_descr (m_gdbarch
);
1247 int footnote_nr
= 0;
1248 int footnote_register_offset
= 0;
1249 int footnote_register_type_name_null
= 0;
1250 long register_offset
= 0;
1252 gdb_assert (descr
->nr_cooked_registers
1253 == (gdbarch_num_regs (m_gdbarch
)
1254 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1256 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1260 fprintf_unfiltered (file
, " %-10s", "Name");
1263 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1267 else if (p
[0] == '\0')
1269 fprintf_unfiltered (file
, " %-10s", p
);
1274 fprintf_unfiltered (file
, " %4s", "Nr");
1276 fprintf_unfiltered (file
, " %4d", regnum
);
1278 /* Relative number. */
1280 fprintf_unfiltered (file
, " %4s", "Rel");
1281 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1282 fprintf_unfiltered (file
, " %4d", regnum
);
1284 fprintf_unfiltered (file
, " %4d",
1285 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1289 fprintf_unfiltered (file
, " %6s ", "Offset");
1292 fprintf_unfiltered (file
, " %6ld",
1293 descr
->register_offset
[regnum
]);
1294 if (register_offset
!= descr
->register_offset
[regnum
]
1296 && (descr
->register_offset
[regnum
]
1297 != (descr
->register_offset
[regnum
- 1]
1298 + descr
->sizeof_register
[regnum
- 1])))
1301 if (!footnote_register_offset
)
1302 footnote_register_offset
= ++footnote_nr
;
1303 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1306 fprintf_unfiltered (file
, " ");
1307 register_offset
= (descr
->register_offset
[regnum
]
1308 + descr
->sizeof_register
[regnum
]);
1313 fprintf_unfiltered (file
, " %5s ", "Size");
1315 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1320 std::string name_holder
;
1326 static const char blt
[] = "builtin_type";
1328 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1331 if (!footnote_register_type_name_null
)
1332 footnote_register_type_name_null
= ++footnote_nr
;
1333 name_holder
= string_printf ("*%d",
1334 footnote_register_type_name_null
);
1335 t
= name_holder
.c_str ();
1337 /* Chop a leading builtin_type. */
1338 if (startswith (t
, blt
))
1341 fprintf_unfiltered (file
, " %-15s", t
);
1344 /* Leading space always present. */
1345 fprintf_unfiltered (file
, " ");
1347 dump_reg (file
, regnum
);
1349 fprintf_unfiltered (file
, "\n");
1352 if (footnote_register_offset
)
1353 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1354 footnote_register_offset
);
1355 if (footnote_register_type_name_null
)
1356 fprintf_unfiltered (file
,
1357 "*%d: Register type's name NULL.\n",
1358 footnote_register_type_name_null
);
1362 #include "selftest.h"
1363 #include "selftest-arch.h"
1364 #include "gdbthread.h"
1365 #include "target-float.h"
1367 namespace selftests
{
1369 class regcache_access
: public regcache
1373 /* Return the number of elements in current_regcache. */
1376 current_regcache_size ()
1378 return std::distance (regcache::current_regcache
.begin (),
1379 regcache::current_regcache
.end ());
1384 current_regcache_test (void)
1386 /* It is empty at the start. */
1387 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1389 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1391 /* Get regcache from ptid1, a new regcache is added to
1392 current_regcache. */
1393 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1397 SELF_CHECK (regcache
!= NULL
);
1398 SELF_CHECK (regcache
->ptid () == ptid1
);
1399 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1401 /* Get regcache from ptid2, a new regcache is added to
1402 current_regcache. */
1403 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1406 SELF_CHECK (regcache
!= NULL
);
1407 SELF_CHECK (regcache
->ptid () == ptid2
);
1408 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1410 /* Get regcache from ptid3, a new regcache is added to
1411 current_regcache. */
1412 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1415 SELF_CHECK (regcache
!= NULL
);
1416 SELF_CHECK (regcache
->ptid () == ptid3
);
1417 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1419 /* Get regcache from ptid2 again, nothing is added to
1420 current_regcache. */
1421 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1424 SELF_CHECK (regcache
!= NULL
);
1425 SELF_CHECK (regcache
->ptid () == ptid2
);
1426 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1428 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1429 current_regcache. */
1430 registers_changed_ptid (ptid2
);
1431 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1434 class target_ops_no_register
: public test_target_ops
1437 target_ops_no_register ()
1438 : test_target_ops
{}
1443 fetch_registers_called
= 0;
1444 store_registers_called
= 0;
1445 xfer_partial_called
= 0;
1448 void fetch_registers (regcache
*regs
, int regno
) override
;
1449 void store_registers (regcache
*regs
, int regno
) override
;
1451 enum target_xfer_status
xfer_partial (enum target_object object
,
1452 const char *annex
, gdb_byte
*readbuf
,
1453 const gdb_byte
*writebuf
,
1454 ULONGEST offset
, ULONGEST len
,
1455 ULONGEST
*xfered_len
) override
;
1457 unsigned int fetch_registers_called
= 0;
1458 unsigned int store_registers_called
= 0;
1459 unsigned int xfer_partial_called
= 0;
1463 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1465 /* Mark register available. */
1466 regs
->raw_supply_zeroed (regno
);
1467 this->fetch_registers_called
++;
1471 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1473 this->store_registers_called
++;
1476 enum target_xfer_status
1477 target_ops_no_register::xfer_partial (enum target_object object
,
1478 const char *annex
, gdb_byte
*readbuf
,
1479 const gdb_byte
*writebuf
,
1480 ULONGEST offset
, ULONGEST len
,
1481 ULONGEST
*xfered_len
)
1483 this->xfer_partial_called
++;
1486 return TARGET_XFER_OK
;
1489 class readwrite_regcache
: public regcache
1492 readwrite_regcache (struct gdbarch
*gdbarch
)
1493 : regcache (gdbarch
, nullptr)
1497 /* Test regcache::cooked_read gets registers from raw registers and
1498 memory instead of target to_{fetch,store}_registers. */
1501 cooked_read_test (struct gdbarch
*gdbarch
)
1503 /* Error out if debugging something, because we're going to push the
1504 test target, which would pop any existing target. */
1505 if (target_stack
->to_stratum
>= process_stratum
)
1506 error (_("target already pushed"));
1508 /* Create a mock environment. An inferior with a thread, with a
1509 process_stratum target pushed. */
1511 target_ops_no_register mock_target
;
1512 ptid_t
mock_ptid (1, 1);
1513 inferior
mock_inferior (mock_ptid
.pid ());
1514 address_space mock_aspace
{};
1515 mock_inferior
.gdbarch
= gdbarch
;
1516 mock_inferior
.aspace
= &mock_aspace
;
1517 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1519 scoped_restore restore_thread_list
1520 = make_scoped_restore (&thread_list
, &mock_thread
);
1522 /* Add the mock inferior to the inferior list so that look ups by
1523 target+ptid can find it. */
1524 scoped_restore restore_inferior_list
1525 = make_scoped_restore (&inferior_list
);
1526 inferior_list
= &mock_inferior
;
1528 /* Switch to the mock inferior. */
1529 scoped_restore_current_inferior restore_current_inferior
;
1530 set_current_inferior (&mock_inferior
);
1532 /* Push the process_stratum target so we can mock accessing
1534 push_target (&mock_target
);
1536 /* Pop it again on exit (return/exception). */
1541 pop_all_targets_at_and_above (process_stratum
);
1545 /* Switch to the mock thread. */
1546 scoped_restore restore_inferior_ptid
1547 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1549 /* Test that read one raw register from regcache_no_target will go
1550 to the target layer. */
1553 /* Find a raw register which size isn't zero. */
1554 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1556 if (register_size (gdbarch
, regnum
) != 0)
1560 readwrite_regcache
readwrite (gdbarch
);
1561 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1563 readwrite
.raw_read (regnum
, buf
.data ());
1565 /* raw_read calls target_fetch_registers. */
1566 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1567 mock_target
.reset ();
1569 /* Mark all raw registers valid, so the following raw registers
1570 accesses won't go to target. */
1571 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1572 readwrite
.raw_update (i
);
1574 mock_target
.reset ();
1575 /* Then, read all raw and pseudo registers, and don't expect calling
1576 to_{fetch,store}_registers. */
1577 for (int regnum
= 0;
1578 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1581 if (register_size (gdbarch
, regnum
) == 0)
1584 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1586 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1588 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1589 SELF_CHECK (mock_target
.store_registers_called
== 0);
1591 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1592 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1593 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1595 mock_target
.reset ();
1598 readonly_detached_regcache
readonly (readwrite
);
1600 /* GDB may go to target layer to fetch all registers and memory for
1601 readonly regcache. */
1602 mock_target
.reset ();
1604 for (int regnum
= 0;
1605 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1608 if (register_size (gdbarch
, regnum
) == 0)
1611 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1612 enum register_status status
= readonly
.cooked_read (regnum
,
1615 if (regnum
< gdbarch_num_regs (gdbarch
))
1617 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1619 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1620 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1621 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1622 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1623 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1624 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1625 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1626 || bfd_arch
== bfd_arch_riscv
)
1628 /* Raw registers. If raw registers are not in save_reggroup,
1629 their status are unknown. */
1630 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1631 SELF_CHECK (status
== REG_VALID
);
1633 SELF_CHECK (status
== REG_UNKNOWN
);
1636 SELF_CHECK (status
== REG_VALID
);
1640 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1641 SELF_CHECK (status
== REG_VALID
);
1644 /* If pseudo registers are not in save_reggroup, some of
1645 them can be computed from saved raw registers, but some
1646 of them are unknown. */
1647 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1649 if (bfd_arch
== bfd_arch_frv
1650 || bfd_arch
== bfd_arch_m32c
1651 || bfd_arch
== bfd_arch_mep
1652 || bfd_arch
== bfd_arch_sh
)
1653 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1654 else if (bfd_arch
== bfd_arch_mips
1655 || bfd_arch
== bfd_arch_h8300
)
1656 SELF_CHECK (status
== REG_UNKNOWN
);
1658 SELF_CHECK (status
== REG_VALID
);
1662 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1663 SELF_CHECK (mock_target
.store_registers_called
== 0);
1664 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1666 mock_target
.reset ();
1670 /* Test regcache::cooked_write by writing some expected contents to
1671 registers, and checking that contents read from registers and the
1672 expected contents are the same. */
1675 cooked_write_test (struct gdbarch
*gdbarch
)
1677 /* Error out if debugging something, because we're going to push the
1678 test target, which would pop any existing target. */
1679 if (target_stack
->to_stratum
>= process_stratum
)
1680 error (_("target already pushed"));
1682 /* Create a mock environment. A process_stratum target pushed. */
1684 target_ops_no_register mock_target
;
1686 /* Push the process_stratum target so we can mock accessing
1688 push_target (&mock_target
);
1690 /* Pop it again on exit (return/exception). */
1695 pop_all_targets_at_and_above (process_stratum
);
1699 readwrite_regcache
readwrite (gdbarch
);
1701 const int num_regs
= (gdbarch_num_regs (gdbarch
)
1702 + gdbarch_num_pseudo_regs (gdbarch
));
1704 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1706 if (register_size (gdbarch
, regnum
) == 0
1707 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1710 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1712 if ((bfd_arch
== bfd_arch_sparc
1713 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1714 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1715 && gdbarch_ptr_bit (gdbarch
) == 64
1716 && (regnum
>= gdbarch_num_regs (gdbarch
)
1717 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1718 || (bfd_arch
== bfd_arch_spu
1719 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1720 TARGET_OBJECT_SPU. */
1721 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
1724 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1725 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1726 const auto type
= register_type (gdbarch
, regnum
);
1728 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1729 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1731 /* Generate valid float format. */
1732 target_float_from_string (expected
.data (), type
, "1.25");
1734 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1735 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1736 || TYPE_CODE (type
) == TYPE_CODE_PTR
1737 || TYPE_CODE (type
) == TYPE_CODE_UNION
1738 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1740 if (bfd_arch
== bfd_arch_ia64
1741 || (regnum
>= gdbarch_num_regs (gdbarch
)
1742 && (bfd_arch
== bfd_arch_xtensa
1743 || bfd_arch
== bfd_arch_bfin
1744 || bfd_arch
== bfd_arch_m32c
1745 /* m68hc11 pseudo registers are in memory. */
1746 || bfd_arch
== bfd_arch_m68hc11
1747 || bfd_arch
== bfd_arch_m68hc12
1748 || bfd_arch
== bfd_arch_s390
))
1749 || (bfd_arch
== bfd_arch_frv
1750 /* FRV pseudo registers except iacc0. */
1751 && regnum
> gdbarch_num_regs (gdbarch
)))
1753 /* Skip setting the expected values for some architecture
1756 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1758 /* RL78_PC_REGNUM */
1759 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1764 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1768 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1770 /* No idea how to test flags. */
1775 /* If we don't know how to create the expected value for the
1776 this type, make it fail. */
1780 readwrite
.cooked_write (regnum
, expected
.data ());
1782 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1783 SELF_CHECK (expected
== buf
);
1787 } // namespace selftests
1788 #endif /* GDB_SELF_TEST */
1791 _initialize_regcache (void)
1793 regcache_descr_handle
1794 = gdbarch_data_register_post_init (init_regcache_descr
);
1796 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1797 gdb::observers::thread_ptid_changed
.attach
1798 (regcache::regcache_thread_ptid_changed
);
1800 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1801 _("Force gdb to flush its register cache (maintainer command)"));
1804 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1806 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1807 selftests::cooked_read_test
);
1808 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1809 selftests::cooked_write_test
);