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 m_regcache
->invalidate (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 detached_regcache::invalidate (int regnum
)
334 assert_regnum (regnum
);
335 m_register_status
[regnum
] = REG_UNKNOWN
;
339 reg_buffer::assert_regnum (int regnum
) const
341 gdb_assert (regnum
>= 0);
343 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
345 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
348 /* Global structure containing the current regcache. */
350 /* NOTE: this is a write-through cache. There is no "dirty" bit for
351 recording if the register values have been changed (eg. by the
352 user). Therefore all registers must be written back to the
353 target when appropriate. */
354 std::forward_list
<regcache
*> regcache::current_regcache
;
357 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
358 struct address_space
*aspace
)
360 for (const auto ®cache
: regcache::current_regcache
)
361 if (ptid_equal (regcache
->ptid (), ptid
) && regcache
->arch () == gdbarch
)
364 regcache
*new_regcache
= new regcache (gdbarch
, aspace
);
366 regcache::current_regcache
.push_front (new_regcache
);
367 new_regcache
->set_ptid (ptid
);
373 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
375 address_space
*aspace
= target_thread_address_space (ptid
);
377 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
380 static ptid_t current_thread_ptid
;
381 static struct gdbarch
*current_thread_arch
;
384 get_thread_regcache (ptid_t ptid
)
386 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
388 current_thread_ptid
= ptid
;
389 current_thread_arch
= target_thread_architecture (ptid
);
392 return get_thread_arch_regcache (ptid
, current_thread_arch
);
396 get_current_regcache (void)
398 return get_thread_regcache (inferior_ptid
);
401 /* See common/common-regcache.h. */
404 get_thread_regcache_for_ptid (ptid_t ptid
)
406 return get_thread_regcache (ptid
);
409 /* Observer for the target_changed event. */
412 regcache_observer_target_changed (struct target_ops
*target
)
414 registers_changed ();
417 /* Update global variables old ptids to hold NEW_PTID if they were
420 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
422 for (auto ®cache
: regcache::current_regcache
)
424 if (ptid_equal (regcache
->ptid (), old_ptid
))
425 regcache
->set_ptid (new_ptid
);
429 /* Low level examining and depositing of registers.
431 The caller is responsible for making sure that the inferior is
432 stopped before calling the fetching routines, or it will get
433 garbage. (a change from GDB version 3, in which the caller got the
434 value from the last stop). */
436 /* REGISTERS_CHANGED ()
438 Indicate that registers may have changed, so invalidate the cache. */
441 registers_changed_ptid (ptid_t ptid
)
443 for (auto oit
= regcache::current_regcache
.before_begin (),
444 it
= std::next (oit
);
445 it
!= regcache::current_regcache
.end ();
448 if (ptid_match ((*it
)->ptid (), ptid
))
451 it
= regcache::current_regcache
.erase_after (oit
);
457 if (ptid_match (current_thread_ptid
, ptid
))
459 current_thread_ptid
= null_ptid
;
460 current_thread_arch
= NULL
;
463 if (ptid_match (inferior_ptid
, ptid
))
465 /* We just deleted the regcache of the current thread. Need to
466 forget about any frames we have cached, too. */
467 reinit_frame_cache ();
472 registers_changed (void)
474 registers_changed_ptid (minus_one_ptid
);
476 /* Force cleanup of any alloca areas if using C alloca instead of
477 a builtin alloca. This particular call is used to clean up
478 areas allocated by low level target code which may build up
479 during lengthy interactions between gdb and the target before
480 gdb gives control to the user (ie watchpoints). */
485 regcache::raw_update (int regnum
)
487 assert_regnum (regnum
);
489 /* Make certain that the register cache is up-to-date with respect
490 to the current thread. This switching shouldn't be necessary
491 only there is still only one target side register cache. Sigh!
492 On the bright side, at least there is a regcache object. */
494 if (get_register_status (regnum
) == REG_UNKNOWN
)
496 target_fetch_registers (this, regnum
);
498 /* A number of targets can't access the whole set of raw
499 registers (because the debug API provides no means to get at
501 if (m_register_status
[regnum
] == REG_UNKNOWN
)
502 m_register_status
[regnum
] = REG_UNAVAILABLE
;
507 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
509 gdb_assert (buf
!= NULL
);
512 if (m_register_status
[regnum
] != REG_VALID
)
513 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
515 memcpy (buf
, register_buffer (regnum
),
516 m_descr
->sizeof_register
[regnum
]);
518 return (enum register_status
) m_register_status
[regnum
];
522 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
524 gdb_assert (regcache
!= NULL
);
525 return regcache
->raw_read (regnum
, val
);
528 template<typename T
, typename
>
530 readable_regcache::raw_read (int regnum
, T
*val
)
533 enum register_status status
;
535 assert_regnum (regnum
);
536 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
537 status
= raw_read (regnum
, buf
);
538 if (status
== REG_VALID
)
539 *val
= extract_integer
<T
> (buf
,
540 m_descr
->sizeof_register
[regnum
],
541 gdbarch_byte_order (m_descr
->gdbarch
));
548 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
551 gdb_assert (regcache
!= NULL
);
552 return regcache
->raw_read (regnum
, val
);
556 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
558 gdb_assert (regcache
!= NULL
);
559 regcache
->raw_write (regnum
, val
);
562 template<typename T
, typename
>
564 regcache::raw_write (int regnum
, T val
)
568 assert_regnum (regnum
);
569 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
570 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
571 gdbarch_byte_order (m_descr
->gdbarch
), val
);
572 raw_write (regnum
, buf
);
576 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
579 gdb_assert (regcache
!= NULL
);
580 regcache
->raw_write (regnum
, val
);
584 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
587 enum register_status status
;
589 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
590 if (status
== REG_UNAVAILABLE
)
591 throw_error (NOT_AVAILABLE_ERROR
,
592 _("Register %d is not available"), regnum
);
597 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
599 gdb_assert (regnum
>= 0);
600 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
601 if (regnum
< num_raw_registers ())
602 return raw_read (regnum
, buf
);
603 else if (m_has_pseudo
604 && m_register_status
[regnum
] != REG_UNKNOWN
)
606 if (m_register_status
[regnum
] == REG_VALID
)
607 memcpy (buf
, register_buffer (regnum
),
608 m_descr
->sizeof_register
[regnum
]);
610 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
612 return (enum register_status
) m_register_status
[regnum
];
614 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
616 struct value
*mark
, *computed
;
617 enum register_status result
= REG_VALID
;
619 mark
= value_mark ();
621 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
623 if (value_entirely_available (computed
))
624 memcpy (buf
, value_contents_raw (computed
),
625 m_descr
->sizeof_register
[regnum
]);
628 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
629 result
= REG_UNAVAILABLE
;
632 value_free_to_mark (mark
);
637 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
642 readable_regcache::cooked_read_value (int regnum
)
644 gdb_assert (regnum
>= 0);
645 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
647 if (regnum
< num_raw_registers ()
648 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
649 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
651 struct value
*result
;
653 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
654 VALUE_LVAL (result
) = lval_register
;
655 VALUE_REGNUM (result
) = regnum
;
657 /* It is more efficient in general to do this delegation in this
658 direction than in the other one, even though the value-based
660 if (cooked_read (regnum
,
661 value_contents_raw (result
)) == REG_UNAVAILABLE
)
662 mark_value_bytes_unavailable (result
, 0,
663 TYPE_LENGTH (value_type (result
)));
668 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
673 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
676 gdb_assert (regcache
!= NULL
);
677 return regcache
->cooked_read (regnum
, val
);
680 template<typename T
, typename
>
682 readable_regcache::cooked_read (int regnum
, T
*val
)
684 enum register_status status
;
687 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
688 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
689 status
= cooked_read (regnum
, buf
);
690 if (status
== REG_VALID
)
691 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
692 gdbarch_byte_order (m_descr
->gdbarch
));
699 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
702 gdb_assert (regcache
!= NULL
);
703 return regcache
->cooked_read (regnum
, val
);
707 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
710 gdb_assert (regcache
!= NULL
);
711 regcache
->cooked_write (regnum
, val
);
714 template<typename T
, typename
>
716 regcache::cooked_write (int regnum
, T val
)
720 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
721 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
722 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
723 gdbarch_byte_order (m_descr
->gdbarch
), val
);
724 cooked_write (regnum
, buf
);
728 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
731 gdb_assert (regcache
!= NULL
);
732 regcache
->cooked_write (regnum
, val
);
736 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
739 gdb_assert (buf
!= NULL
);
740 assert_regnum (regnum
);
742 /* On the sparc, writing %g0 is a no-op, so we don't even want to
743 change the registers array if something writes to this register. */
744 if (gdbarch_cannot_store_register (arch (), regnum
))
747 /* If we have a valid copy of the register, and new value == old
748 value, then don't bother doing the actual store. */
749 if (get_register_status (regnum
) == REG_VALID
750 && (memcmp (register_buffer (regnum
), buf
,
751 m_descr
->sizeof_register
[regnum
]) == 0))
754 target_prepare_to_store (this);
755 raw_supply (regnum
, buf
);
757 /* Invalidate the register after it is written, in case of a
759 regcache_invalidator
invalidator (this, regnum
);
761 target_store_registers (this, regnum
);
763 /* The target did not throw an error so we can discard invalidating
765 invalidator
.release ();
769 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
771 gdb_assert (regnum
>= 0);
772 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
773 if (regnum
< num_raw_registers ())
774 raw_write (regnum
, buf
);
776 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
780 /* Perform a partial register transfer using a read, modify, write
784 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
787 struct gdbarch
*gdbarch
= arch ();
788 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
790 gdb_assert (in
!= NULL
);
791 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
792 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
793 /* Something to do? */
794 if (offset
+ len
== 0)
796 /* Read (when needed) ... */
797 enum register_status status
;
800 status
= raw_read (regnum
, reg
);
802 status
= cooked_read (regnum
, reg
);
803 if (status
!= REG_VALID
)
807 memcpy (in
, reg
+ offset
, len
);
813 regcache::write_part (int regnum
, int offset
, int len
,
814 const void *out
, bool is_raw
)
816 struct gdbarch
*gdbarch
= arch ();
817 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
819 gdb_assert (out
!= NULL
);
820 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
821 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
822 /* Something to do? */
823 if (offset
+ len
== 0)
825 /* Read (when needed) ... */
827 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
829 enum register_status status
;
832 status
= raw_read (regnum
, reg
);
834 status
= cooked_read (regnum
, reg
);
835 if (status
!= REG_VALID
)
839 memcpy (reg
+ offset
, out
, len
);
840 /* ... write (when needed). */
842 raw_write (regnum
, reg
);
844 cooked_write (regnum
, reg
);
850 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
852 assert_regnum (regnum
);
853 return read_part (regnum
, offset
, len
, buf
, true);
856 /* See regcache.h. */
859 regcache::raw_write_part (int regnum
, int offset
, int len
,
862 assert_regnum (regnum
);
863 write_part (regnum
, offset
, len
, buf
, true);
867 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
870 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
871 return read_part (regnum
, offset
, len
, buf
, false);
875 regcache::cooked_write_part (int regnum
, int offset
, int len
,
878 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
879 write_part (regnum
, offset
, len
, buf
, false);
883 detached_regcache::raw_supply (int regnum
, const void *buf
)
888 assert_regnum (regnum
);
890 regbuf
= register_buffer (regnum
);
891 size
= m_descr
->sizeof_register
[regnum
];
895 memcpy (regbuf
, buf
, size
);
896 m_register_status
[regnum
] = REG_VALID
;
900 /* This memset not strictly necessary, but better than garbage
901 in case the register value manages to escape somewhere (due
902 to a bug, no less). */
903 memset (regbuf
, 0, size
);
904 m_register_status
[regnum
] = REG_UNAVAILABLE
;
908 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
909 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
910 the register size is greater than ADDR_LEN, then the integer will be sign or
911 zero extended. If the register size is smaller than the integer, then the
912 most significant bytes of the integer will be truncated. */
915 detached_regcache::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
916 int addr_len
, bool is_signed
)
918 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
922 assert_regnum (regnum
);
924 regbuf
= register_buffer (regnum
);
925 regsize
= m_descr
->sizeof_register
[regnum
];
927 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
929 m_register_status
[regnum
] = REG_VALID
;
932 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
933 as calling raw_supply with NULL (which will set the state to
937 detached_regcache::raw_supply_zeroed (int regnum
)
942 assert_regnum (regnum
);
944 regbuf
= register_buffer (regnum
);
945 size
= m_descr
->sizeof_register
[regnum
];
947 memset (regbuf
, 0, size
);
948 m_register_status
[regnum
] = REG_VALID
;
951 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
954 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
956 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
957 regcache
->raw_collect (regnum
, buf
);
961 regcache::raw_collect (int regnum
, void *buf
) const
966 gdb_assert (buf
!= NULL
);
967 assert_regnum (regnum
);
969 regbuf
= register_buffer (regnum
);
970 size
= m_descr
->sizeof_register
[regnum
];
971 memcpy (buf
, regbuf
, size
);
974 /* Transfer a single or all registers belonging to a certain register
975 set to or from a buffer. This is the main worker function for
976 regcache_supply_regset and regcache_collect_regset. */
978 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
979 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
980 If ADDR_LEN is greater than the register size, then the integer will be sign
981 or zero extended. If ADDR_LEN is smaller than the register size, then the
982 most significant bytes of the integer will be truncated. */
985 regcache::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
986 bool is_signed
) const
988 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
989 const gdb_byte
*regbuf
;
992 assert_regnum (regnum
);
994 regbuf
= register_buffer (regnum
);
995 regsize
= m_descr
->sizeof_register
[regnum
];
997 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1002 regcache::transfer_regset (const struct regset
*regset
,
1003 struct regcache
*out_regcache
,
1004 int regnum
, const void *in_buf
,
1005 void *out_buf
, size_t size
) const
1007 const struct regcache_map_entry
*map
;
1008 int offs
= 0, count
;
1010 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1011 (count
= map
->count
) != 0;
1014 int regno
= map
->regno
;
1015 int slot_size
= map
->size
;
1017 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1018 slot_size
= m_descr
->sizeof_register
[regno
];
1020 if (regno
== REGCACHE_MAP_SKIP
1022 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1023 offs
+= count
* slot_size
;
1025 else if (regnum
== -1)
1026 for (; count
--; regno
++, offs
+= slot_size
)
1028 if (offs
+ slot_size
> size
)
1032 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1034 out_regcache
->raw_supply (regno
, in_buf
1035 ? (const gdb_byte
*) in_buf
+ offs
1040 /* Transfer a single register and return. */
1041 offs
+= (regnum
- regno
) * slot_size
;
1042 if (offs
+ slot_size
> size
)
1046 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1048 out_regcache
->raw_supply (regnum
, in_buf
1049 ? (const gdb_byte
*) in_buf
+ offs
1056 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1057 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1058 If BUF is NULL, set the register(s) to "unavailable" status. */
1061 regcache_supply_regset (const struct regset
*regset
,
1062 struct regcache
*regcache
,
1063 int regnum
, const void *buf
, size_t size
)
1065 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1069 regcache::supply_regset (const struct regset
*regset
,
1070 int regnum
, const void *buf
, size_t size
)
1072 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1075 /* Collect register REGNUM from REGCACHE to BUF, using the register
1076 map in REGSET. If REGNUM is -1, do this for all registers in
1080 regcache_collect_regset (const struct regset
*regset
,
1081 const struct regcache
*regcache
,
1082 int regnum
, void *buf
, size_t size
)
1084 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1088 regcache::collect_regset (const struct regset
*regset
,
1089 int regnum
, void *buf
, size_t size
) const
1091 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1095 /* Special handling for register PC. */
1098 regcache_read_pc (struct regcache
*regcache
)
1100 struct gdbarch
*gdbarch
= regcache
->arch ();
1104 if (gdbarch_read_pc_p (gdbarch
))
1105 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1106 /* Else use per-frame method on get_current_frame. */
1107 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1111 if (regcache_cooked_read_unsigned (regcache
,
1112 gdbarch_pc_regnum (gdbarch
),
1113 &raw_val
) == REG_UNAVAILABLE
)
1114 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1116 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1119 internal_error (__FILE__
, __LINE__
,
1120 _("regcache_read_pc: Unable to find PC"));
1125 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1127 struct gdbarch
*gdbarch
= regcache
->arch ();
1129 if (gdbarch_write_pc_p (gdbarch
))
1130 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1131 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1132 regcache_cooked_write_unsigned (regcache
,
1133 gdbarch_pc_regnum (gdbarch
), pc
);
1135 internal_error (__FILE__
, __LINE__
,
1136 _("regcache_write_pc: Unable to update PC"));
1138 /* Writing the PC (for instance, from "load") invalidates the
1140 reinit_frame_cache ();
1144 reg_buffer::num_raw_registers () const
1146 return gdbarch_num_regs (arch ());
1150 regcache::debug_print_register (const char *func
, int regno
)
1152 struct gdbarch
*gdbarch
= arch ();
1154 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1155 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1156 && gdbarch_register_name (gdbarch
, regno
) != NULL
1157 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1158 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1159 gdbarch_register_name (gdbarch
, regno
));
1161 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1162 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1164 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1165 int size
= register_size (gdbarch
, regno
);
1166 gdb_byte
*buf
= register_buffer (regno
);
1168 fprintf_unfiltered (gdb_stdlog
, " = ");
1169 for (int i
= 0; i
< size
; i
++)
1171 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1173 if (size
<= sizeof (LONGEST
))
1175 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1177 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1178 core_addr_to_string_nz (val
), plongest (val
));
1181 fprintf_unfiltered (gdb_stdlog
, "\n");
1185 reg_flush_command (const char *command
, int from_tty
)
1187 /* Force-flush the register cache. */
1188 registers_changed ();
1190 printf_filtered (_("Register cache flushed.\n"));
1194 register_dump::dump (ui_file
*file
)
1196 auto descr
= regcache_descr (m_gdbarch
);
1198 int footnote_nr
= 0;
1199 int footnote_register_offset
= 0;
1200 int footnote_register_type_name_null
= 0;
1201 long register_offset
= 0;
1203 gdb_assert (descr
->nr_cooked_registers
1204 == (gdbarch_num_regs (m_gdbarch
)
1205 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1207 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1211 fprintf_unfiltered (file
, " %-10s", "Name");
1214 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1218 else if (p
[0] == '\0')
1220 fprintf_unfiltered (file
, " %-10s", p
);
1225 fprintf_unfiltered (file
, " %4s", "Nr");
1227 fprintf_unfiltered (file
, " %4d", regnum
);
1229 /* Relative number. */
1231 fprintf_unfiltered (file
, " %4s", "Rel");
1232 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1233 fprintf_unfiltered (file
, " %4d", regnum
);
1235 fprintf_unfiltered (file
, " %4d",
1236 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1240 fprintf_unfiltered (file
, " %6s ", "Offset");
1243 fprintf_unfiltered (file
, " %6ld",
1244 descr
->register_offset
[regnum
]);
1245 if (register_offset
!= descr
->register_offset
[regnum
]
1247 && (descr
->register_offset
[regnum
]
1248 != (descr
->register_offset
[regnum
- 1]
1249 + descr
->sizeof_register
[regnum
- 1])))
1252 if (!footnote_register_offset
)
1253 footnote_register_offset
= ++footnote_nr
;
1254 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1257 fprintf_unfiltered (file
, " ");
1258 register_offset
= (descr
->register_offset
[regnum
]
1259 + descr
->sizeof_register
[regnum
]);
1264 fprintf_unfiltered (file
, " %5s ", "Size");
1266 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1271 std::string name_holder
;
1277 static const char blt
[] = "builtin_type";
1279 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1282 if (!footnote_register_type_name_null
)
1283 footnote_register_type_name_null
= ++footnote_nr
;
1284 name_holder
= string_printf ("*%d",
1285 footnote_register_type_name_null
);
1286 t
= name_holder
.c_str ();
1288 /* Chop a leading builtin_type. */
1289 if (startswith (t
, blt
))
1292 fprintf_unfiltered (file
, " %-15s", t
);
1295 /* Leading space always present. */
1296 fprintf_unfiltered (file
, " ");
1298 dump_reg (file
, regnum
);
1300 fprintf_unfiltered (file
, "\n");
1303 if (footnote_register_offset
)
1304 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1305 footnote_register_offset
);
1306 if (footnote_register_type_name_null
)
1307 fprintf_unfiltered (file
,
1308 "*%d: Register type's name NULL.\n",
1309 footnote_register_type_name_null
);
1313 #include "selftest.h"
1314 #include "selftest-arch.h"
1315 #include "gdbthread.h"
1316 #include "target-float.h"
1318 namespace selftests
{
1320 class regcache_access
: public regcache
1324 /* Return the number of elements in current_regcache. */
1327 current_regcache_size ()
1329 return std::distance (regcache::current_regcache
.begin (),
1330 regcache::current_regcache
.end ());
1335 current_regcache_test (void)
1337 /* It is empty at the start. */
1338 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1340 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1342 /* Get regcache from ptid1, a new regcache is added to
1343 current_regcache. */
1344 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1348 SELF_CHECK (regcache
!= NULL
);
1349 SELF_CHECK (regcache
->ptid () == ptid1
);
1350 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1352 /* Get regcache from ptid2, a new regcache is added to
1353 current_regcache. */
1354 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1357 SELF_CHECK (regcache
!= NULL
);
1358 SELF_CHECK (regcache
->ptid () == ptid2
);
1359 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1361 /* Get regcache from ptid3, a new regcache is added to
1362 current_regcache. */
1363 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1366 SELF_CHECK (regcache
!= NULL
);
1367 SELF_CHECK (regcache
->ptid () == ptid3
);
1368 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1370 /* Get regcache from ptid2 again, nothing is added to
1371 current_regcache. */
1372 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1375 SELF_CHECK (regcache
!= NULL
);
1376 SELF_CHECK (regcache
->ptid () == ptid2
);
1377 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1379 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1380 current_regcache. */
1381 registers_changed_ptid (ptid2
);
1382 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1385 class target_ops_no_register
: public test_target_ops
1388 target_ops_no_register ()
1389 : test_target_ops
{}
1394 fetch_registers_called
= 0;
1395 store_registers_called
= 0;
1396 xfer_partial_called
= 0;
1399 void fetch_registers (regcache
*regs
, int regno
) override
;
1400 void store_registers (regcache
*regs
, int regno
) override
;
1402 enum target_xfer_status
xfer_partial (enum target_object object
,
1403 const char *annex
, gdb_byte
*readbuf
,
1404 const gdb_byte
*writebuf
,
1405 ULONGEST offset
, ULONGEST len
,
1406 ULONGEST
*xfered_len
) override
;
1408 unsigned int fetch_registers_called
= 0;
1409 unsigned int store_registers_called
= 0;
1410 unsigned int xfer_partial_called
= 0;
1414 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1416 /* Mark register available. */
1417 regs
->raw_supply_zeroed (regno
);
1418 this->fetch_registers_called
++;
1422 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1424 this->store_registers_called
++;
1427 enum target_xfer_status
1428 target_ops_no_register::xfer_partial (enum target_object object
,
1429 const char *annex
, gdb_byte
*readbuf
,
1430 const gdb_byte
*writebuf
,
1431 ULONGEST offset
, ULONGEST len
,
1432 ULONGEST
*xfered_len
)
1434 this->xfer_partial_called
++;
1437 return TARGET_XFER_OK
;
1440 class readwrite_regcache
: public regcache
1443 readwrite_regcache (struct gdbarch
*gdbarch
)
1444 : regcache (gdbarch
, nullptr)
1448 /* Test regcache::cooked_read gets registers from raw registers and
1449 memory instead of target to_{fetch,store}_registers. */
1452 cooked_read_test (struct gdbarch
*gdbarch
)
1454 /* Error out if debugging something, because we're going to push the
1455 test target, which would pop any existing target. */
1456 if (target_stack
->to_stratum
>= process_stratum
)
1457 error (_("target already pushed"));
1459 /* Create a mock environment. An inferior with a thread, with a
1460 process_stratum target pushed. */
1462 target_ops_no_register mock_target
;
1463 ptid_t
mock_ptid (1, 1);
1464 inferior
mock_inferior (mock_ptid
.pid ());
1465 address_space mock_aspace
{};
1466 mock_inferior
.gdbarch
= gdbarch
;
1467 mock_inferior
.aspace
= &mock_aspace
;
1468 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1470 scoped_restore restore_thread_list
1471 = make_scoped_restore (&thread_list
, &mock_thread
);
1473 /* Add the mock inferior to the inferior list so that look ups by
1474 target+ptid can find it. */
1475 scoped_restore restore_inferior_list
1476 = make_scoped_restore (&inferior_list
);
1477 inferior_list
= &mock_inferior
;
1479 /* Switch to the mock inferior. */
1480 scoped_restore_current_inferior restore_current_inferior
;
1481 set_current_inferior (&mock_inferior
);
1483 /* Push the process_stratum target so we can mock accessing
1485 push_target (&mock_target
);
1487 /* Pop it again on exit (return/exception). */
1492 pop_all_targets_at_and_above (process_stratum
);
1496 /* Switch to the mock thread. */
1497 scoped_restore restore_inferior_ptid
1498 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1500 /* Test that read one raw register from regcache_no_target will go
1501 to the target layer. */
1504 /* Find a raw register which size isn't zero. */
1505 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1507 if (register_size (gdbarch
, regnum
) != 0)
1511 readwrite_regcache
readwrite (gdbarch
);
1512 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1514 readwrite
.raw_read (regnum
, buf
.data ());
1516 /* raw_read calls target_fetch_registers. */
1517 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1518 mock_target
.reset ();
1520 /* Mark all raw registers valid, so the following raw registers
1521 accesses won't go to target. */
1522 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1523 readwrite
.raw_update (i
);
1525 mock_target
.reset ();
1526 /* Then, read all raw and pseudo registers, and don't expect calling
1527 to_{fetch,store}_registers. */
1528 for (int regnum
= 0;
1529 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1532 if (register_size (gdbarch
, regnum
) == 0)
1535 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1537 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1539 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1540 SELF_CHECK (mock_target
.store_registers_called
== 0);
1542 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1543 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1544 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1546 mock_target
.reset ();
1549 readonly_detached_regcache
readonly (readwrite
);
1551 /* GDB may go to target layer to fetch all registers and memory for
1552 readonly regcache. */
1553 mock_target
.reset ();
1555 for (int regnum
= 0;
1556 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1559 if (register_size (gdbarch
, regnum
) == 0)
1562 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1563 enum register_status status
= readonly
.cooked_read (regnum
,
1566 if (regnum
< gdbarch_num_regs (gdbarch
))
1568 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1570 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1571 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1572 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1573 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1574 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1575 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1576 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1577 || bfd_arch
== bfd_arch_riscv
)
1579 /* Raw registers. If raw registers are not in save_reggroup,
1580 their status are unknown. */
1581 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1582 SELF_CHECK (status
== REG_VALID
);
1584 SELF_CHECK (status
== REG_UNKNOWN
);
1587 SELF_CHECK (status
== REG_VALID
);
1591 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1592 SELF_CHECK (status
== REG_VALID
);
1595 /* If pseudo registers are not in save_reggroup, some of
1596 them can be computed from saved raw registers, but some
1597 of them are unknown. */
1598 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1600 if (bfd_arch
== bfd_arch_frv
1601 || bfd_arch
== bfd_arch_m32c
1602 || bfd_arch
== bfd_arch_mep
1603 || bfd_arch
== bfd_arch_sh
)
1604 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1605 else if (bfd_arch
== bfd_arch_mips
1606 || bfd_arch
== bfd_arch_h8300
)
1607 SELF_CHECK (status
== REG_UNKNOWN
);
1609 SELF_CHECK (status
== REG_VALID
);
1613 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1614 SELF_CHECK (mock_target
.store_registers_called
== 0);
1615 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1617 mock_target
.reset ();
1621 /* Test regcache::cooked_write by writing some expected contents to
1622 registers, and checking that contents read from registers and the
1623 expected contents are the same. */
1626 cooked_write_test (struct gdbarch
*gdbarch
)
1628 /* Error out if debugging something, because we're going to push the
1629 test target, which would pop any existing target. */
1630 if (target_stack
->to_stratum
>= process_stratum
)
1631 error (_("target already pushed"));
1633 /* Create a mock environment. A process_stratum target pushed. */
1635 target_ops_no_register mock_target
;
1637 /* Push the process_stratum target so we can mock accessing
1639 push_target (&mock_target
);
1641 /* Pop it again on exit (return/exception). */
1646 pop_all_targets_at_and_above (process_stratum
);
1650 readwrite_regcache
readwrite (gdbarch
);
1652 const int num_regs
= (gdbarch_num_regs (gdbarch
)
1653 + gdbarch_num_pseudo_regs (gdbarch
));
1655 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1657 if (register_size (gdbarch
, regnum
) == 0
1658 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1661 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1663 if ((bfd_arch
== bfd_arch_sparc
1664 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1665 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1666 && gdbarch_ptr_bit (gdbarch
) == 64
1667 && (regnum
>= gdbarch_num_regs (gdbarch
)
1668 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1669 || (bfd_arch
== bfd_arch_spu
1670 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1671 TARGET_OBJECT_SPU. */
1672 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
1675 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1676 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1677 const auto type
= register_type (gdbarch
, regnum
);
1679 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1680 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1682 /* Generate valid float format. */
1683 target_float_from_string (expected
.data (), type
, "1.25");
1685 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1686 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1687 || TYPE_CODE (type
) == TYPE_CODE_PTR
1688 || TYPE_CODE (type
) == TYPE_CODE_UNION
1689 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1691 if (bfd_arch
== bfd_arch_ia64
1692 || (regnum
>= gdbarch_num_regs (gdbarch
)
1693 && (bfd_arch
== bfd_arch_xtensa
1694 || bfd_arch
== bfd_arch_bfin
1695 || bfd_arch
== bfd_arch_m32c
1696 /* m68hc11 pseudo registers are in memory. */
1697 || bfd_arch
== bfd_arch_m68hc11
1698 || bfd_arch
== bfd_arch_m68hc12
1699 || bfd_arch
== bfd_arch_s390
))
1700 || (bfd_arch
== bfd_arch_frv
1701 /* FRV pseudo registers except iacc0. */
1702 && regnum
> gdbarch_num_regs (gdbarch
)))
1704 /* Skip setting the expected values for some architecture
1707 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1709 /* RL78_PC_REGNUM */
1710 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1715 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1719 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1721 /* No idea how to test flags. */
1726 /* If we don't know how to create the expected value for the
1727 this type, make it fail. */
1731 readwrite
.cooked_write (regnum
, expected
.data ());
1733 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1734 SELF_CHECK (expected
== buf
);
1738 } // namespace selftests
1739 #endif /* GDB_SELF_TEST */
1742 _initialize_regcache (void)
1744 regcache_descr_handle
1745 = gdbarch_data_register_post_init (init_regcache_descr
);
1747 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1748 gdb::observers::thread_ptid_changed
.attach
1749 (regcache::regcache_thread_ptid_changed
);
1751 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1752 _("Force gdb to flush its register cache (maintainer command)"));
1755 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1757 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1758 selftests::cooked_read_test
);
1759 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1760 selftests::cooked_write_test
);