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
.reset (new gdb_byte
[m_descr
->sizeof_cooked_registers
] ());
189 m_register_status
.reset
190 (new register_status
[m_descr
->nr_cooked_registers
] ());
194 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_raw_registers
] ());
195 m_register_status
.reset
196 (new register_status
[gdbarch_num_regs (gdbarch
)] ());
200 regcache::regcache (gdbarch
*gdbarch
, const address_space
*aspace_
)
201 /* The register buffers. A read/write register cache can only hold
202 [0 .. gdbarch_num_regs). */
203 : detached_regcache (gdbarch
, false), m_aspace (aspace_
)
205 m_ptid
= minus_one_ptid
;
208 static enum register_status
209 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
211 struct regcache
*regcache
= (struct regcache
*) src
;
213 return regcache
->cooked_read (regnum
, buf
);
216 readonly_detached_regcache::readonly_detached_regcache (const regcache
&src
)
217 : readonly_detached_regcache (src
.arch (), do_cooked_read
, (void *) &src
)
222 reg_buffer::arch () const
224 return m_descr
->gdbarch
;
227 /* Cleanup class for invalidating a register. */
229 class regcache_invalidator
233 regcache_invalidator (struct regcache
*regcache
, int regnum
)
234 : m_regcache (regcache
),
239 ~regcache_invalidator ()
241 if (m_regcache
!= nullptr)
242 m_regcache
->invalidate (m_regnum
);
245 DISABLE_COPY_AND_ASSIGN (regcache_invalidator
);
249 m_regcache
= nullptr;
254 struct regcache
*m_regcache
;
258 /* Return a pointer to register REGNUM's buffer cache. */
261 reg_buffer::register_buffer (int regnum
) const
263 return m_registers
.get () + m_descr
->register_offset
[regnum
];
267 reg_buffer::save (regcache_cooked_read_ftype
*cooked_read
,
270 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
273 /* It should have pseudo registers. */
274 gdb_assert (m_has_pseudo
);
275 /* Clear the dest. */
276 memset (m_registers
.get (), 0, m_descr
->sizeof_cooked_registers
);
277 memset (m_register_status
.get (), REG_UNKNOWN
, m_descr
->nr_cooked_registers
);
278 /* Copy over any registers (identified by their membership in the
279 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
280 gdbarch_num_pseudo_regs) range is checked since some architectures need
281 to save/restore `cooked' registers that live in memory. */
282 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
284 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
286 gdb_byte
*dst_buf
= register_buffer (regnum
);
287 enum register_status status
= cooked_read (src
, regnum
, dst_buf
);
289 gdb_assert (status
!= REG_UNKNOWN
);
291 if (status
!= REG_VALID
)
292 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
294 m_register_status
[regnum
] = status
;
300 regcache::restore (readonly_detached_regcache
*src
)
302 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
305 gdb_assert (src
!= NULL
);
306 gdb_assert (src
->m_has_pseudo
);
308 gdb_assert (gdbarch
== src
->arch ());
310 /* Copy over any registers, being careful to only restore those that
311 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
312 + gdbarch_num_pseudo_regs) range is checked since some architectures need
313 to save/restore `cooked' registers that live in memory. */
314 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
316 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
318 if (src
->m_register_status
[regnum
] == REG_VALID
)
319 cooked_write (regnum
, src
->register_buffer (regnum
));
324 /* See common/common-regcache.h. */
327 reg_buffer::get_register_status (int regnum
) const
329 assert_regnum (regnum
);
331 return m_register_status
[regnum
];
335 reg_buffer::invalidate (int regnum
)
337 assert_regnum (regnum
);
338 m_register_status
[regnum
] = REG_UNKNOWN
;
342 reg_buffer::assert_regnum (int regnum
) const
344 gdb_assert (regnum
>= 0);
346 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
348 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
351 /* Global structure containing the current regcache. */
353 /* NOTE: this is a write-through cache. There is no "dirty" bit for
354 recording if the register values have been changed (eg. by the
355 user). Therefore all registers must be written back to the
356 target when appropriate. */
357 std::forward_list
<regcache
*> regcache::current_regcache
;
360 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
361 struct address_space
*aspace
)
363 for (const auto ®cache
: regcache::current_regcache
)
364 if (ptid_equal (regcache
->ptid (), ptid
) && regcache
->arch () == gdbarch
)
367 regcache
*new_regcache
= new regcache (gdbarch
, aspace
);
369 regcache::current_regcache
.push_front (new_regcache
);
370 new_regcache
->set_ptid (ptid
);
376 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
378 address_space
*aspace
= target_thread_address_space (ptid
);
380 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
383 static ptid_t current_thread_ptid
;
384 static struct gdbarch
*current_thread_arch
;
387 get_thread_regcache (ptid_t ptid
)
389 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
391 current_thread_ptid
= ptid
;
392 current_thread_arch
= target_thread_architecture (ptid
);
395 return get_thread_arch_regcache (ptid
, current_thread_arch
);
399 get_current_regcache (void)
401 return get_thread_regcache (inferior_ptid
);
404 /* See common/common-regcache.h. */
407 get_thread_regcache_for_ptid (ptid_t ptid
)
409 return get_thread_regcache (ptid
);
412 /* Observer for the target_changed event. */
415 regcache_observer_target_changed (struct target_ops
*target
)
417 registers_changed ();
420 /* Update global variables old ptids to hold NEW_PTID if they were
423 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
425 for (auto ®cache
: regcache::current_regcache
)
427 if (ptid_equal (regcache
->ptid (), old_ptid
))
428 regcache
->set_ptid (new_ptid
);
432 /* Low level examining and depositing of registers.
434 The caller is responsible for making sure that the inferior is
435 stopped before calling the fetching routines, or it will get
436 garbage. (a change from GDB version 3, in which the caller got the
437 value from the last stop). */
439 /* REGISTERS_CHANGED ()
441 Indicate that registers may have changed, so invalidate the cache. */
444 registers_changed_ptid (ptid_t ptid
)
446 for (auto oit
= regcache::current_regcache
.before_begin (),
447 it
= std::next (oit
);
448 it
!= regcache::current_regcache
.end ();
451 if (ptid_match ((*it
)->ptid (), ptid
))
454 it
= regcache::current_regcache
.erase_after (oit
);
460 if (ptid_match (current_thread_ptid
, ptid
))
462 current_thread_ptid
= null_ptid
;
463 current_thread_arch
= NULL
;
466 if (ptid_match (inferior_ptid
, ptid
))
468 /* We just deleted the regcache of the current thread. Need to
469 forget about any frames we have cached, too. */
470 reinit_frame_cache ();
475 registers_changed (void)
477 registers_changed_ptid (minus_one_ptid
);
479 /* Force cleanup of any alloca areas if using C alloca instead of
480 a builtin alloca. This particular call is used to clean up
481 areas allocated by low level target code which may build up
482 during lengthy interactions between gdb and the target before
483 gdb gives control to the user (ie watchpoints). */
488 regcache::raw_update (int regnum
)
490 assert_regnum (regnum
);
492 /* Make certain that the register cache is up-to-date with respect
493 to the current thread. This switching shouldn't be necessary
494 only there is still only one target side register cache. Sigh!
495 On the bright side, at least there is a regcache object. */
497 if (get_register_status (regnum
) == REG_UNKNOWN
)
499 target_fetch_registers (this, regnum
);
501 /* A number of targets can't access the whole set of raw
502 registers (because the debug API provides no means to get at
504 if (m_register_status
[regnum
] == REG_UNKNOWN
)
505 m_register_status
[regnum
] = REG_UNAVAILABLE
;
510 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
512 gdb_assert (buf
!= NULL
);
515 if (m_register_status
[regnum
] != REG_VALID
)
516 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
518 memcpy (buf
, register_buffer (regnum
),
519 m_descr
->sizeof_register
[regnum
]);
521 return m_register_status
[regnum
];
525 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
527 gdb_assert (regcache
!= NULL
);
528 return regcache
->raw_read (regnum
, val
);
531 template<typename T
, typename
>
533 readable_regcache::raw_read (int regnum
, T
*val
)
536 enum register_status status
;
538 assert_regnum (regnum
);
539 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
540 status
= raw_read (regnum
, buf
);
541 if (status
== REG_VALID
)
542 *val
= extract_integer
<T
> (buf
,
543 m_descr
->sizeof_register
[regnum
],
544 gdbarch_byte_order (m_descr
->gdbarch
));
551 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
554 gdb_assert (regcache
!= NULL
);
555 return regcache
->raw_read (regnum
, val
);
559 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
561 gdb_assert (regcache
!= NULL
);
562 regcache
->raw_write (regnum
, val
);
565 template<typename T
, typename
>
567 regcache::raw_write (int regnum
, T val
)
571 assert_regnum (regnum
);
572 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
573 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
574 gdbarch_byte_order (m_descr
->gdbarch
), val
);
575 raw_write (regnum
, buf
);
579 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
582 gdb_assert (regcache
!= NULL
);
583 regcache
->raw_write (regnum
, val
);
587 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
590 enum register_status status
;
592 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
593 if (status
== REG_UNAVAILABLE
)
594 throw_error (NOT_AVAILABLE_ERROR
,
595 _("Register %d is not available"), regnum
);
600 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
602 gdb_assert (regnum
>= 0);
603 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
604 if (regnum
< num_raw_registers ())
605 return raw_read (regnum
, buf
);
606 else if (m_has_pseudo
607 && m_register_status
[regnum
] != REG_UNKNOWN
)
609 if (m_register_status
[regnum
] == REG_VALID
)
610 memcpy (buf
, register_buffer (regnum
),
611 m_descr
->sizeof_register
[regnum
]);
613 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
615 return m_register_status
[regnum
];
617 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
619 struct value
*mark
, *computed
;
620 enum register_status result
= REG_VALID
;
622 mark
= value_mark ();
624 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
626 if (value_entirely_available (computed
))
627 memcpy (buf
, value_contents_raw (computed
),
628 m_descr
->sizeof_register
[regnum
]);
631 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
632 result
= REG_UNAVAILABLE
;
635 value_free_to_mark (mark
);
640 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
645 readable_regcache::cooked_read_value (int regnum
)
647 gdb_assert (regnum
>= 0);
648 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
650 if (regnum
< num_raw_registers ()
651 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
652 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
654 struct value
*result
;
656 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
657 VALUE_LVAL (result
) = lval_register
;
658 VALUE_REGNUM (result
) = regnum
;
660 /* It is more efficient in general to do this delegation in this
661 direction than in the other one, even though the value-based
663 if (cooked_read (regnum
,
664 value_contents_raw (result
)) == REG_UNAVAILABLE
)
665 mark_value_bytes_unavailable (result
, 0,
666 TYPE_LENGTH (value_type (result
)));
671 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
676 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
679 gdb_assert (regcache
!= NULL
);
680 return regcache
->cooked_read (regnum
, val
);
683 template<typename T
, typename
>
685 readable_regcache::cooked_read (int regnum
, T
*val
)
687 enum register_status status
;
690 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
691 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
692 status
= cooked_read (regnum
, buf
);
693 if (status
== REG_VALID
)
694 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
695 gdbarch_byte_order (m_descr
->gdbarch
));
702 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
705 gdb_assert (regcache
!= NULL
);
706 return regcache
->cooked_read (regnum
, val
);
710 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
713 gdb_assert (regcache
!= NULL
);
714 regcache
->cooked_write (regnum
, val
);
717 template<typename T
, typename
>
719 regcache::cooked_write (int regnum
, T val
)
723 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
724 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
725 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
726 gdbarch_byte_order (m_descr
->gdbarch
), val
);
727 cooked_write (regnum
, buf
);
731 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
734 gdb_assert (regcache
!= NULL
);
735 regcache
->cooked_write (regnum
, val
);
739 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
742 gdb_assert (buf
!= NULL
);
743 assert_regnum (regnum
);
745 /* On the sparc, writing %g0 is a no-op, so we don't even want to
746 change the registers array if something writes to this register. */
747 if (gdbarch_cannot_store_register (arch (), regnum
))
750 /* If we have a valid copy of the register, and new value == old
751 value, then don't bother doing the actual store. */
752 if (get_register_status (regnum
) == REG_VALID
753 && (memcmp (register_buffer (regnum
), buf
,
754 m_descr
->sizeof_register
[regnum
]) == 0))
757 target_prepare_to_store (this);
758 raw_supply (regnum
, buf
);
760 /* Invalidate the register after it is written, in case of a
762 regcache_invalidator
invalidator (this, regnum
);
764 target_store_registers (this, regnum
);
766 /* The target did not throw an error so we can discard invalidating
768 invalidator
.release ();
772 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
774 gdb_assert (regnum
>= 0);
775 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
776 if (regnum
< num_raw_registers ())
777 raw_write (regnum
, buf
);
779 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
783 /* Perform a partial register transfer using a read, modify, write
787 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
790 struct gdbarch
*gdbarch
= arch ();
791 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
793 gdb_assert (in
!= NULL
);
794 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
795 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
796 /* Something to do? */
797 if (offset
+ len
== 0)
799 /* Read (when needed) ... */
800 enum register_status status
;
803 status
= raw_read (regnum
, reg
);
805 status
= cooked_read (regnum
, reg
);
806 if (status
!= REG_VALID
)
810 memcpy (in
, reg
+ offset
, len
);
816 regcache::write_part (int regnum
, int offset
, int len
,
817 const void *out
, bool is_raw
)
819 struct gdbarch
*gdbarch
= arch ();
820 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
822 gdb_assert (out
!= NULL
);
823 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
824 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
825 /* Something to do? */
826 if (offset
+ len
== 0)
828 /* Read (when needed) ... */
830 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
832 enum register_status status
;
835 status
= raw_read (regnum
, reg
);
837 status
= cooked_read (regnum
, reg
);
838 if (status
!= REG_VALID
)
842 memcpy (reg
+ offset
, out
, len
);
843 /* ... write (when needed). */
845 raw_write (regnum
, reg
);
847 cooked_write (regnum
, reg
);
853 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
855 assert_regnum (regnum
);
856 return read_part (regnum
, offset
, len
, buf
, true);
859 /* See regcache.h. */
862 regcache::raw_write_part (int regnum
, int offset
, int len
,
865 assert_regnum (regnum
);
866 write_part (regnum
, offset
, len
, buf
, true);
870 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
873 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
874 return read_part (regnum
, offset
, len
, buf
, false);
878 regcache::cooked_write_part (int regnum
, int offset
, int len
,
881 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
882 write_part (regnum
, offset
, len
, buf
, false);
885 /* See common/common-regcache.h. */
888 reg_buffer::raw_supply (int regnum
, const void *buf
)
893 assert_regnum (regnum
);
895 regbuf
= register_buffer (regnum
);
896 size
= m_descr
->sizeof_register
[regnum
];
900 memcpy (regbuf
, buf
, size
);
901 m_register_status
[regnum
] = REG_VALID
;
905 /* This memset not strictly necessary, but better than garbage
906 in case the register value manages to escape somewhere (due
907 to a bug, no less). */
908 memset (regbuf
, 0, size
);
909 m_register_status
[regnum
] = REG_UNAVAILABLE
;
913 /* See regcache.h. */
916 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
917 int addr_len
, bool is_signed
)
919 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
923 assert_regnum (regnum
);
925 regbuf
= register_buffer (regnum
);
926 regsize
= m_descr
->sizeof_register
[regnum
];
928 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
930 m_register_status
[regnum
] = REG_VALID
;
933 /* See regcache.h. */
936 reg_buffer::raw_supply_zeroed (int regnum
)
941 assert_regnum (regnum
);
943 regbuf
= register_buffer (regnum
);
944 size
= m_descr
->sizeof_register
[regnum
];
946 memset (regbuf
, 0, size
);
947 m_register_status
[regnum
] = REG_VALID
;
950 /* See common/common-regcache.h. */
953 reg_buffer::raw_collect (int regnum
, void *buf
) const
958 gdb_assert (buf
!= NULL
);
959 assert_regnum (regnum
);
961 regbuf
= register_buffer (regnum
);
962 size
= m_descr
->sizeof_register
[regnum
];
963 memcpy (buf
, regbuf
, size
);
966 /* See regcache.h. */
969 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
970 bool is_signed
) const
972 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
973 const gdb_byte
*regbuf
;
976 assert_regnum (regnum
);
978 regbuf
= register_buffer (regnum
);
979 regsize
= m_descr
->sizeof_register
[regnum
];
981 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
985 /* Transfer a single or all registers belonging to a certain register
986 set to or from a buffer. This is the main worker function for
987 regcache_supply_regset and regcache_collect_regset. */
990 regcache::transfer_regset (const struct regset
*regset
,
991 struct regcache
*out_regcache
,
992 int regnum
, const void *in_buf
,
993 void *out_buf
, size_t size
) const
995 const struct regcache_map_entry
*map
;
998 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
999 (count
= map
->count
) != 0;
1002 int regno
= map
->regno
;
1003 int slot_size
= map
->size
;
1005 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1006 slot_size
= m_descr
->sizeof_register
[regno
];
1008 if (regno
== REGCACHE_MAP_SKIP
1010 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1011 offs
+= count
* slot_size
;
1013 else if (regnum
== -1)
1014 for (; count
--; regno
++, offs
+= slot_size
)
1016 if (offs
+ slot_size
> size
)
1020 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1022 out_regcache
->raw_supply (regno
, in_buf
1023 ? (const gdb_byte
*) in_buf
+ offs
1028 /* Transfer a single register and return. */
1029 offs
+= (regnum
- regno
) * slot_size
;
1030 if (offs
+ slot_size
> size
)
1034 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1036 out_regcache
->raw_supply (regnum
, in_buf
1037 ? (const gdb_byte
*) in_buf
+ offs
1044 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1045 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1046 If BUF is NULL, set the register(s) to "unavailable" status. */
1049 regcache_supply_regset (const struct regset
*regset
,
1050 struct regcache
*regcache
,
1051 int regnum
, const void *buf
, size_t size
)
1053 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1057 regcache::supply_regset (const struct regset
*regset
,
1058 int regnum
, const void *buf
, size_t size
)
1060 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1063 /* Collect register REGNUM from REGCACHE to BUF, using the register
1064 map in REGSET. If REGNUM is -1, do this for all registers in
1068 regcache_collect_regset (const struct regset
*regset
,
1069 const struct regcache
*regcache
,
1070 int regnum
, void *buf
, size_t size
)
1072 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1076 regcache::collect_regset (const struct regset
*regset
,
1077 int regnum
, void *buf
, size_t size
) const
1079 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1082 /* See common/common-regcache.h. */
1085 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1087 gdb_assert (buf
!= NULL
);
1088 assert_regnum (regnum
);
1090 const char *regbuf
= (const char *) register_buffer (regnum
);
1091 size_t size
= m_descr
->sizeof_register
[regnum
];
1092 gdb_assert (size
>= offset
);
1094 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1097 /* Special handling for register PC. */
1100 regcache_read_pc (struct regcache
*regcache
)
1102 struct gdbarch
*gdbarch
= regcache
->arch ();
1106 if (gdbarch_read_pc_p (gdbarch
))
1107 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1108 /* Else use per-frame method on get_current_frame. */
1109 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1113 if (regcache_cooked_read_unsigned (regcache
,
1114 gdbarch_pc_regnum (gdbarch
),
1115 &raw_val
) == REG_UNAVAILABLE
)
1116 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1118 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1121 internal_error (__FILE__
, __LINE__
,
1122 _("regcache_read_pc: Unable to find PC"));
1127 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1129 struct gdbarch
*gdbarch
= regcache
->arch ();
1131 if (gdbarch_write_pc_p (gdbarch
))
1132 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1133 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1134 regcache_cooked_write_unsigned (regcache
,
1135 gdbarch_pc_regnum (gdbarch
), pc
);
1137 internal_error (__FILE__
, __LINE__
,
1138 _("regcache_write_pc: Unable to update PC"));
1140 /* Writing the PC (for instance, from "load") invalidates the
1142 reinit_frame_cache ();
1146 reg_buffer::num_raw_registers () const
1148 return gdbarch_num_regs (arch ());
1152 regcache::debug_print_register (const char *func
, int regno
)
1154 struct gdbarch
*gdbarch
= arch ();
1156 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1157 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1158 && gdbarch_register_name (gdbarch
, regno
) != NULL
1159 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1160 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1161 gdbarch_register_name (gdbarch
, regno
));
1163 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1164 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1166 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1167 int size
= register_size (gdbarch
, regno
);
1168 gdb_byte
*buf
= register_buffer (regno
);
1170 fprintf_unfiltered (gdb_stdlog
, " = ");
1171 for (int i
= 0; i
< size
; i
++)
1173 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1175 if (size
<= sizeof (LONGEST
))
1177 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1179 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1180 core_addr_to_string_nz (val
), plongest (val
));
1183 fprintf_unfiltered (gdb_stdlog
, "\n");
1187 reg_flush_command (const char *command
, int from_tty
)
1189 /* Force-flush the register cache. */
1190 registers_changed ();
1192 printf_filtered (_("Register cache flushed.\n"));
1196 register_dump::dump (ui_file
*file
)
1198 auto descr
= regcache_descr (m_gdbarch
);
1200 int footnote_nr
= 0;
1201 int footnote_register_offset
= 0;
1202 int footnote_register_type_name_null
= 0;
1203 long register_offset
= 0;
1205 gdb_assert (descr
->nr_cooked_registers
1206 == (gdbarch_num_regs (m_gdbarch
)
1207 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1209 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1213 fprintf_unfiltered (file
, " %-10s", "Name");
1216 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1220 else if (p
[0] == '\0')
1222 fprintf_unfiltered (file
, " %-10s", p
);
1227 fprintf_unfiltered (file
, " %4s", "Nr");
1229 fprintf_unfiltered (file
, " %4d", regnum
);
1231 /* Relative number. */
1233 fprintf_unfiltered (file
, " %4s", "Rel");
1234 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1235 fprintf_unfiltered (file
, " %4d", regnum
);
1237 fprintf_unfiltered (file
, " %4d",
1238 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1242 fprintf_unfiltered (file
, " %6s ", "Offset");
1245 fprintf_unfiltered (file
, " %6ld",
1246 descr
->register_offset
[regnum
]);
1247 if (register_offset
!= descr
->register_offset
[regnum
]
1249 && (descr
->register_offset
[regnum
]
1250 != (descr
->register_offset
[regnum
- 1]
1251 + descr
->sizeof_register
[regnum
- 1])))
1254 if (!footnote_register_offset
)
1255 footnote_register_offset
= ++footnote_nr
;
1256 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1259 fprintf_unfiltered (file
, " ");
1260 register_offset
= (descr
->register_offset
[regnum
]
1261 + descr
->sizeof_register
[regnum
]);
1266 fprintf_unfiltered (file
, " %5s ", "Size");
1268 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1273 std::string name_holder
;
1279 static const char blt
[] = "builtin_type";
1281 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1284 if (!footnote_register_type_name_null
)
1285 footnote_register_type_name_null
= ++footnote_nr
;
1286 name_holder
= string_printf ("*%d",
1287 footnote_register_type_name_null
);
1288 t
= name_holder
.c_str ();
1290 /* Chop a leading builtin_type. */
1291 if (startswith (t
, blt
))
1294 fprintf_unfiltered (file
, " %-15s", t
);
1297 /* Leading space always present. */
1298 fprintf_unfiltered (file
, " ");
1300 dump_reg (file
, regnum
);
1302 fprintf_unfiltered (file
, "\n");
1305 if (footnote_register_offset
)
1306 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1307 footnote_register_offset
);
1308 if (footnote_register_type_name_null
)
1309 fprintf_unfiltered (file
,
1310 "*%d: Register type's name NULL.\n",
1311 footnote_register_type_name_null
);
1315 #include "selftest.h"
1316 #include "selftest-arch.h"
1317 #include "gdbthread.h"
1318 #include "target-float.h"
1320 namespace selftests
{
1322 class regcache_access
: public regcache
1326 /* Return the number of elements in current_regcache. */
1329 current_regcache_size ()
1331 return std::distance (regcache::current_regcache
.begin (),
1332 regcache::current_regcache
.end ());
1337 current_regcache_test (void)
1339 /* It is empty at the start. */
1340 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1342 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1344 /* Get regcache from ptid1, a new regcache is added to
1345 current_regcache. */
1346 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1350 SELF_CHECK (regcache
!= NULL
);
1351 SELF_CHECK (regcache
->ptid () == ptid1
);
1352 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1354 /* Get regcache from ptid2, a new regcache is added to
1355 current_regcache. */
1356 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1359 SELF_CHECK (regcache
!= NULL
);
1360 SELF_CHECK (regcache
->ptid () == ptid2
);
1361 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1363 /* Get regcache from ptid3, a new regcache is added to
1364 current_regcache. */
1365 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1368 SELF_CHECK (regcache
!= NULL
);
1369 SELF_CHECK (regcache
->ptid () == ptid3
);
1370 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1372 /* Get regcache from ptid2 again, nothing is added to
1373 current_regcache. */
1374 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1377 SELF_CHECK (regcache
!= NULL
);
1378 SELF_CHECK (regcache
->ptid () == ptid2
);
1379 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1381 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1382 current_regcache. */
1383 registers_changed_ptid (ptid2
);
1384 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1387 class target_ops_no_register
: public test_target_ops
1390 target_ops_no_register ()
1391 : test_target_ops
{}
1396 fetch_registers_called
= 0;
1397 store_registers_called
= 0;
1398 xfer_partial_called
= 0;
1401 void fetch_registers (regcache
*regs
, int regno
) override
;
1402 void store_registers (regcache
*regs
, int regno
) override
;
1404 enum target_xfer_status
xfer_partial (enum target_object object
,
1405 const char *annex
, gdb_byte
*readbuf
,
1406 const gdb_byte
*writebuf
,
1407 ULONGEST offset
, ULONGEST len
,
1408 ULONGEST
*xfered_len
) override
;
1410 unsigned int fetch_registers_called
= 0;
1411 unsigned int store_registers_called
= 0;
1412 unsigned int xfer_partial_called
= 0;
1416 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1418 /* Mark register available. */
1419 regs
->raw_supply_zeroed (regno
);
1420 this->fetch_registers_called
++;
1424 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1426 this->store_registers_called
++;
1429 enum target_xfer_status
1430 target_ops_no_register::xfer_partial (enum target_object object
,
1431 const char *annex
, gdb_byte
*readbuf
,
1432 const gdb_byte
*writebuf
,
1433 ULONGEST offset
, ULONGEST len
,
1434 ULONGEST
*xfered_len
)
1436 this->xfer_partial_called
++;
1439 return TARGET_XFER_OK
;
1442 class readwrite_regcache
: public regcache
1445 readwrite_regcache (struct gdbarch
*gdbarch
)
1446 : regcache (gdbarch
, nullptr)
1450 /* Test regcache::cooked_read gets registers from raw registers and
1451 memory instead of target to_{fetch,store}_registers. */
1454 cooked_read_test (struct gdbarch
*gdbarch
)
1456 /* Error out if debugging something, because we're going to push the
1457 test target, which would pop any existing target. */
1458 if (current_top_target ()->to_stratum
>= process_stratum
)
1459 error (_("target already pushed"));
1461 /* Create a mock environment. An inferior with a thread, with a
1462 process_stratum target pushed. */
1464 target_ops_no_register mock_target
;
1465 ptid_t
mock_ptid (1, 1);
1466 inferior
mock_inferior (mock_ptid
.pid ());
1467 address_space mock_aspace
{};
1468 mock_inferior
.gdbarch
= gdbarch
;
1469 mock_inferior
.aspace
= &mock_aspace
;
1470 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1472 scoped_restore restore_thread_list
1473 = make_scoped_restore (&thread_list
, &mock_thread
);
1475 /* Add the mock inferior to the inferior list so that look ups by
1476 target+ptid can find it. */
1477 scoped_restore restore_inferior_list
1478 = make_scoped_restore (&inferior_list
);
1479 inferior_list
= &mock_inferior
;
1481 /* Switch to the mock inferior. */
1482 scoped_restore_current_inferior restore_current_inferior
;
1483 set_current_inferior (&mock_inferior
);
1485 /* Push the process_stratum target so we can mock accessing
1487 push_target (&mock_target
);
1489 /* Pop it again on exit (return/exception). */
1494 pop_all_targets_at_and_above (process_stratum
);
1498 /* Switch to the mock thread. */
1499 scoped_restore restore_inferior_ptid
1500 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1502 /* Test that read one raw register from regcache_no_target will go
1503 to the target layer. */
1506 /* Find a raw register which size isn't zero. */
1507 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1509 if (register_size (gdbarch
, regnum
) != 0)
1513 readwrite_regcache
readwrite (gdbarch
);
1514 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1516 readwrite
.raw_read (regnum
, buf
.data ());
1518 /* raw_read calls target_fetch_registers. */
1519 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1520 mock_target
.reset ();
1522 /* Mark all raw registers valid, so the following raw registers
1523 accesses won't go to target. */
1524 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1525 readwrite
.raw_update (i
);
1527 mock_target
.reset ();
1528 /* Then, read all raw and pseudo registers, and don't expect calling
1529 to_{fetch,store}_registers. */
1530 for (int regnum
= 0;
1531 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1534 if (register_size (gdbarch
, regnum
) == 0)
1537 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1539 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1541 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1542 SELF_CHECK (mock_target
.store_registers_called
== 0);
1544 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1545 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1546 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1548 mock_target
.reset ();
1551 readonly_detached_regcache
readonly (readwrite
);
1553 /* GDB may go to target layer to fetch all registers and memory for
1554 readonly regcache. */
1555 mock_target
.reset ();
1557 for (int regnum
= 0;
1558 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1561 if (register_size (gdbarch
, regnum
) == 0)
1564 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1565 enum register_status status
= readonly
.cooked_read (regnum
,
1568 if (regnum
< gdbarch_num_regs (gdbarch
))
1570 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1572 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1573 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1574 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1575 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1576 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1577 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1578 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1579 || bfd_arch
== bfd_arch_riscv
)
1581 /* Raw registers. If raw registers are not in save_reggroup,
1582 their status are unknown. */
1583 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1584 SELF_CHECK (status
== REG_VALID
);
1586 SELF_CHECK (status
== REG_UNKNOWN
);
1589 SELF_CHECK (status
== REG_VALID
);
1593 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1594 SELF_CHECK (status
== REG_VALID
);
1597 /* If pseudo registers are not in save_reggroup, some of
1598 them can be computed from saved raw registers, but some
1599 of them are unknown. */
1600 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1602 if (bfd_arch
== bfd_arch_frv
1603 || bfd_arch
== bfd_arch_m32c
1604 || bfd_arch
== bfd_arch_mep
1605 || bfd_arch
== bfd_arch_sh
)
1606 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1607 else if (bfd_arch
== bfd_arch_mips
1608 || bfd_arch
== bfd_arch_h8300
)
1609 SELF_CHECK (status
== REG_UNKNOWN
);
1611 SELF_CHECK (status
== REG_VALID
);
1615 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1616 SELF_CHECK (mock_target
.store_registers_called
== 0);
1617 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1619 mock_target
.reset ();
1623 /* Test regcache::cooked_write by writing some expected contents to
1624 registers, and checking that contents read from registers and the
1625 expected contents are the same. */
1628 cooked_write_test (struct gdbarch
*gdbarch
)
1630 /* Error out if debugging something, because we're going to push the
1631 test target, which would pop any existing target. */
1632 if (current_top_target ()->to_stratum
>= process_stratum
)
1633 error (_("target already pushed"));
1635 /* Create a mock environment. A process_stratum target pushed. */
1637 target_ops_no_register mock_target
;
1639 /* Push the process_stratum target so we can mock accessing
1641 push_target (&mock_target
);
1643 /* Pop it again on exit (return/exception). */
1648 pop_all_targets_at_and_above (process_stratum
);
1652 readwrite_regcache
readwrite (gdbarch
);
1654 const int num_regs
= (gdbarch_num_regs (gdbarch
)
1655 + gdbarch_num_pseudo_regs (gdbarch
));
1657 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1659 if (register_size (gdbarch
, regnum
) == 0
1660 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1663 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1665 if ((bfd_arch
== bfd_arch_sparc
1666 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1667 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1668 && gdbarch_ptr_bit (gdbarch
) == 64
1669 && (regnum
>= gdbarch_num_regs (gdbarch
)
1670 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1671 || (bfd_arch
== bfd_arch_spu
1672 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1673 TARGET_OBJECT_SPU. */
1674 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
1677 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1678 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1679 const auto type
= register_type (gdbarch
, regnum
);
1681 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1682 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1684 /* Generate valid float format. */
1685 target_float_from_string (expected
.data (), type
, "1.25");
1687 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1688 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1689 || TYPE_CODE (type
) == TYPE_CODE_PTR
1690 || TYPE_CODE (type
) == TYPE_CODE_UNION
1691 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1693 if (bfd_arch
== bfd_arch_ia64
1694 || (regnum
>= gdbarch_num_regs (gdbarch
)
1695 && (bfd_arch
== bfd_arch_xtensa
1696 || bfd_arch
== bfd_arch_bfin
1697 || bfd_arch
== bfd_arch_m32c
1698 /* m68hc11 pseudo registers are in memory. */
1699 || bfd_arch
== bfd_arch_m68hc11
1700 || bfd_arch
== bfd_arch_m68hc12
1701 || bfd_arch
== bfd_arch_s390
))
1702 || (bfd_arch
== bfd_arch_frv
1703 /* FRV pseudo registers except iacc0. */
1704 && regnum
> gdbarch_num_regs (gdbarch
)))
1706 /* Skip setting the expected values for some architecture
1709 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1711 /* RL78_PC_REGNUM */
1712 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1717 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1721 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1723 /* No idea how to test flags. */
1728 /* If we don't know how to create the expected value for the
1729 this type, make it fail. */
1733 readwrite
.cooked_write (regnum
, expected
.data ());
1735 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1736 SELF_CHECK (expected
== buf
);
1740 } // namespace selftests
1741 #endif /* GDB_SELF_TEST */
1744 _initialize_regcache (void)
1746 regcache_descr_handle
1747 = gdbarch_data_register_post_init (init_regcache_descr
);
1749 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1750 gdb::observers::thread_ptid_changed
.attach
1751 (regcache::regcache_thread_ptid_changed
);
1753 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1754 _("Force gdb to flush its register cache (maintainer command)"));
1757 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1759 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1760 selftests::cooked_read_test
);
1761 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1762 selftests::cooked_write_test
);