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
);
190 = XCNEWVEC (register_status
, m_descr
->nr_cooked_registers
);
194 m_registers
= XCNEWVEC (gdb_byte
, m_descr
->sizeof_raw_registers
);
196 = XCNEWVEC (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
+ 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
, 0, m_descr
->sizeof_cooked_registers
);
277 memset (m_register_status
, 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
));
325 reg_buffer::get_register_status (int regnum
) const
327 assert_regnum (regnum
);
329 return m_register_status
[regnum
];
333 detached_regcache::invalidate (int regnum
)
335 assert_regnum (regnum
);
336 m_register_status
[regnum
] = REG_UNKNOWN
;
340 reg_buffer::assert_regnum (int regnum
) const
342 gdb_assert (regnum
>= 0);
344 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
346 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
349 /* Global structure containing the current regcache. */
351 /* NOTE: this is a write-through cache. There is no "dirty" bit for
352 recording if the register values have been changed (eg. by the
353 user). Therefore all registers must be written back to the
354 target when appropriate. */
355 std::forward_list
<regcache
*> regcache::current_regcache
;
358 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
359 struct address_space
*aspace
)
361 for (const auto ®cache
: regcache::current_regcache
)
362 if (ptid_equal (regcache
->ptid (), ptid
) && regcache
->arch () == gdbarch
)
365 regcache
*new_regcache
= new regcache (gdbarch
, aspace
);
367 regcache::current_regcache
.push_front (new_regcache
);
368 new_regcache
->set_ptid (ptid
);
374 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
376 address_space
*aspace
= target_thread_address_space (ptid
);
378 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
381 static ptid_t current_thread_ptid
;
382 static struct gdbarch
*current_thread_arch
;
385 get_thread_regcache (ptid_t ptid
)
387 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
389 current_thread_ptid
= ptid
;
390 current_thread_arch
= target_thread_architecture (ptid
);
393 return get_thread_arch_regcache (ptid
, current_thread_arch
);
397 get_current_regcache (void)
399 return get_thread_regcache (inferior_ptid
);
402 /* See common/common-regcache.h. */
405 get_thread_regcache_for_ptid (ptid_t ptid
)
407 return get_thread_regcache (ptid
);
410 /* Observer for the target_changed event. */
413 regcache_observer_target_changed (struct target_ops
*target
)
415 registers_changed ();
418 /* Update global variables old ptids to hold NEW_PTID if they were
421 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
423 for (auto ®cache
: regcache::current_regcache
)
425 if (ptid_equal (regcache
->ptid (), old_ptid
))
426 regcache
->set_ptid (new_ptid
);
430 /* Low level examining and depositing of registers.
432 The caller is responsible for making sure that the inferior is
433 stopped before calling the fetching routines, or it will get
434 garbage. (a change from GDB version 3, in which the caller got the
435 value from the last stop). */
437 /* REGISTERS_CHANGED ()
439 Indicate that registers may have changed, so invalidate the cache. */
442 registers_changed_ptid (ptid_t ptid
)
444 for (auto oit
= regcache::current_regcache
.before_begin (),
445 it
= std::next (oit
);
446 it
!= regcache::current_regcache
.end ();
449 if (ptid_match ((*it
)->ptid (), ptid
))
452 it
= regcache::current_regcache
.erase_after (oit
);
458 if (ptid_match (current_thread_ptid
, ptid
))
460 current_thread_ptid
= null_ptid
;
461 current_thread_arch
= NULL
;
464 if (ptid_match (inferior_ptid
, ptid
))
466 /* We just deleted the regcache of the current thread. Need to
467 forget about any frames we have cached, too. */
468 reinit_frame_cache ();
473 registers_changed (void)
475 registers_changed_ptid (minus_one_ptid
);
477 /* Force cleanup of any alloca areas if using C alloca instead of
478 a builtin alloca. This particular call is used to clean up
479 areas allocated by low level target code which may build up
480 during lengthy interactions between gdb and the target before
481 gdb gives control to the user (ie watchpoints). */
486 regcache::raw_update (int regnum
)
488 assert_regnum (regnum
);
490 /* Make certain that the register cache is up-to-date with respect
491 to the current thread. This switching shouldn't be necessary
492 only there is still only one target side register cache. Sigh!
493 On the bright side, at least there is a regcache object. */
495 if (get_register_status (regnum
) == REG_UNKNOWN
)
497 target_fetch_registers (this, regnum
);
499 /* A number of targets can't access the whole set of raw
500 registers (because the debug API provides no means to get at
502 if (m_register_status
[regnum
] == REG_UNKNOWN
)
503 m_register_status
[regnum
] = REG_UNAVAILABLE
;
508 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
510 gdb_assert (buf
!= NULL
);
513 if (m_register_status
[regnum
] != REG_VALID
)
514 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
516 memcpy (buf
, register_buffer (regnum
),
517 m_descr
->sizeof_register
[regnum
]);
519 return m_register_status
[regnum
];
523 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
525 gdb_assert (regcache
!= NULL
);
526 return regcache
->raw_read (regnum
, val
);
529 template<typename T
, typename
>
531 readable_regcache::raw_read (int regnum
, T
*val
)
534 enum register_status status
;
536 assert_regnum (regnum
);
537 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
538 status
= raw_read (regnum
, buf
);
539 if (status
== REG_VALID
)
540 *val
= extract_integer
<T
> (buf
,
541 m_descr
->sizeof_register
[regnum
],
542 gdbarch_byte_order (m_descr
->gdbarch
));
549 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
552 gdb_assert (regcache
!= NULL
);
553 return regcache
->raw_read (regnum
, val
);
557 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
559 gdb_assert (regcache
!= NULL
);
560 regcache
->raw_write (regnum
, val
);
563 template<typename T
, typename
>
565 regcache::raw_write (int regnum
, T val
)
569 assert_regnum (regnum
);
570 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
571 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
572 gdbarch_byte_order (m_descr
->gdbarch
), val
);
573 raw_write (regnum
, buf
);
577 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
580 gdb_assert (regcache
!= NULL
);
581 regcache
->raw_write (regnum
, val
);
585 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
588 enum register_status status
;
590 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
591 if (status
== REG_UNAVAILABLE
)
592 throw_error (NOT_AVAILABLE_ERROR
,
593 _("Register %d is not available"), regnum
);
598 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
600 gdb_assert (regnum
>= 0);
601 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
602 if (regnum
< num_raw_registers ())
603 return raw_read (regnum
, buf
);
604 else if (m_has_pseudo
605 && m_register_status
[regnum
] != REG_UNKNOWN
)
607 if (m_register_status
[regnum
] == REG_VALID
)
608 memcpy (buf
, register_buffer (regnum
),
609 m_descr
->sizeof_register
[regnum
]);
611 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
613 return m_register_status
[regnum
];
615 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
617 struct value
*mark
, *computed
;
618 enum register_status result
= REG_VALID
;
620 mark
= value_mark ();
622 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
624 if (value_entirely_available (computed
))
625 memcpy (buf
, value_contents_raw (computed
),
626 m_descr
->sizeof_register
[regnum
]);
629 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
630 result
= REG_UNAVAILABLE
;
633 value_free_to_mark (mark
);
638 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
643 readable_regcache::cooked_read_value (int regnum
)
645 gdb_assert (regnum
>= 0);
646 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
648 if (regnum
< num_raw_registers ()
649 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
650 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
652 struct value
*result
;
654 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
655 VALUE_LVAL (result
) = lval_register
;
656 VALUE_REGNUM (result
) = regnum
;
658 /* It is more efficient in general to do this delegation in this
659 direction than in the other one, even though the value-based
661 if (cooked_read (regnum
,
662 value_contents_raw (result
)) == REG_UNAVAILABLE
)
663 mark_value_bytes_unavailable (result
, 0,
664 TYPE_LENGTH (value_type (result
)));
669 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
674 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
677 gdb_assert (regcache
!= NULL
);
678 return regcache
->cooked_read (regnum
, val
);
681 template<typename T
, typename
>
683 readable_regcache::cooked_read (int regnum
, T
*val
)
685 enum register_status status
;
688 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
689 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
690 status
= cooked_read (regnum
, buf
);
691 if (status
== REG_VALID
)
692 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
693 gdbarch_byte_order (m_descr
->gdbarch
));
700 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
703 gdb_assert (regcache
!= NULL
);
704 return regcache
->cooked_read (regnum
, val
);
708 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
711 gdb_assert (regcache
!= NULL
);
712 regcache
->cooked_write (regnum
, val
);
715 template<typename T
, typename
>
717 regcache::cooked_write (int regnum
, T val
)
721 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
722 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
723 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
724 gdbarch_byte_order (m_descr
->gdbarch
), val
);
725 cooked_write (regnum
, buf
);
729 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
732 gdb_assert (regcache
!= NULL
);
733 regcache
->cooked_write (regnum
, val
);
737 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
740 gdb_assert (buf
!= NULL
);
741 assert_regnum (regnum
);
743 /* On the sparc, writing %g0 is a no-op, so we don't even want to
744 change the registers array if something writes to this register. */
745 if (gdbarch_cannot_store_register (arch (), regnum
))
748 /* If we have a valid copy of the register, and new value == old
749 value, then don't bother doing the actual store. */
750 if (get_register_status (regnum
) == REG_VALID
751 && (memcmp (register_buffer (regnum
), buf
,
752 m_descr
->sizeof_register
[regnum
]) == 0))
755 target_prepare_to_store (this);
756 raw_supply (regnum
, buf
);
758 /* Invalidate the register after it is written, in case of a
760 regcache_invalidator
invalidator (this, regnum
);
762 target_store_registers (this, regnum
);
764 /* The target did not throw an error so we can discard invalidating
766 invalidator
.release ();
770 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
772 gdb_assert (regnum
>= 0);
773 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
774 if (regnum
< num_raw_registers ())
775 raw_write (regnum
, buf
);
777 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
781 /* Perform a partial register transfer using a read, modify, write
785 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
788 struct gdbarch
*gdbarch
= arch ();
789 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
791 gdb_assert (in
!= NULL
);
792 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
793 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
794 /* Something to do? */
795 if (offset
+ len
== 0)
797 /* Read (when needed) ... */
798 enum register_status status
;
801 status
= raw_read (regnum
, reg
);
803 status
= cooked_read (regnum
, reg
);
804 if (status
!= REG_VALID
)
808 memcpy (in
, reg
+ offset
, len
);
814 regcache::write_part (int regnum
, int offset
, int len
,
815 const void *out
, bool is_raw
)
817 struct gdbarch
*gdbarch
= arch ();
818 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
820 gdb_assert (out
!= NULL
);
821 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
822 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
823 /* Something to do? */
824 if (offset
+ len
== 0)
826 /* Read (when needed) ... */
828 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
830 enum register_status status
;
833 status
= raw_read (regnum
, reg
);
835 status
= cooked_read (regnum
, reg
);
836 if (status
!= REG_VALID
)
840 memcpy (reg
+ offset
, out
, len
);
841 /* ... write (when needed). */
843 raw_write (regnum
, reg
);
845 cooked_write (regnum
, reg
);
851 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
853 assert_regnum (regnum
);
854 return read_part (regnum
, offset
, len
, buf
, true);
857 /* See regcache.h. */
860 regcache::raw_write_part (int regnum
, int offset
, int len
,
863 assert_regnum (regnum
);
864 write_part (regnum
, offset
, len
, buf
, true);
868 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
871 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
872 return read_part (regnum
, offset
, len
, buf
, false);
876 regcache::cooked_write_part (int regnum
, int offset
, int len
,
879 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
880 write_part (regnum
, offset
, len
, buf
, false);
884 detached_regcache::raw_supply (int regnum
, const void *buf
)
889 assert_regnum (regnum
);
891 regbuf
= register_buffer (regnum
);
892 size
= m_descr
->sizeof_register
[regnum
];
896 memcpy (regbuf
, buf
, size
);
897 m_register_status
[regnum
] = REG_VALID
;
901 /* This memset not strictly necessary, but better than garbage
902 in case the register value manages to escape somewhere (due
903 to a bug, no less). */
904 memset (regbuf
, 0, size
);
905 m_register_status
[regnum
] = REG_UNAVAILABLE
;
909 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
910 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
911 the register size is greater than ADDR_LEN, then the integer will be sign or
912 zero extended. If the register size is smaller than the integer, then the
913 most significant bytes of the integer will be truncated. */
916 detached_regcache::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 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
934 as calling raw_supply with NULL (which will set the state to
938 detached_regcache::raw_supply_zeroed (int regnum
)
943 assert_regnum (regnum
);
945 regbuf
= register_buffer (regnum
);
946 size
= m_descr
->sizeof_register
[regnum
];
948 memset (regbuf
, 0, size
);
949 m_register_status
[regnum
] = REG_VALID
;
953 regcache::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 /* Transfer a single or all registers belonging to a certain register
967 set to or from a buffer. This is the main worker function for
968 regcache_supply_regset and regcache_collect_regset. */
970 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
971 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
972 If ADDR_LEN is greater than the register size, then the integer will be sign
973 or zero extended. If ADDR_LEN is smaller than the register size, then the
974 most significant bytes of the integer will be truncated. */
977 regcache::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
978 bool is_signed
) const
980 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
981 const gdb_byte
*regbuf
;
984 assert_regnum (regnum
);
986 regbuf
= register_buffer (regnum
);
987 regsize
= m_descr
->sizeof_register
[regnum
];
989 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
994 regcache::transfer_regset (const struct regset
*regset
,
995 struct regcache
*out_regcache
,
996 int regnum
, const void *in_buf
,
997 void *out_buf
, size_t size
) const
999 const struct regcache_map_entry
*map
;
1000 int offs
= 0, count
;
1002 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1003 (count
= map
->count
) != 0;
1006 int regno
= map
->regno
;
1007 int slot_size
= map
->size
;
1009 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1010 slot_size
= m_descr
->sizeof_register
[regno
];
1012 if (regno
== REGCACHE_MAP_SKIP
1014 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1015 offs
+= count
* slot_size
;
1017 else if (regnum
== -1)
1018 for (; count
--; regno
++, offs
+= slot_size
)
1020 if (offs
+ slot_size
> size
)
1024 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1026 out_regcache
->raw_supply (regno
, in_buf
1027 ? (const gdb_byte
*) in_buf
+ offs
1032 /* Transfer a single register and return. */
1033 offs
+= (regnum
- regno
) * slot_size
;
1034 if (offs
+ slot_size
> size
)
1038 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1040 out_regcache
->raw_supply (regnum
, in_buf
1041 ? (const gdb_byte
*) in_buf
+ offs
1048 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1049 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1050 If BUF is NULL, set the register(s) to "unavailable" status. */
1053 regcache_supply_regset (const struct regset
*regset
,
1054 struct regcache
*regcache
,
1055 int regnum
, const void *buf
, size_t size
)
1057 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1061 regcache::supply_regset (const struct regset
*regset
,
1062 int regnum
, const void *buf
, size_t size
)
1064 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1067 /* Collect register REGNUM from REGCACHE to BUF, using the register
1068 map in REGSET. If REGNUM is -1, do this for all registers in
1072 regcache_collect_regset (const struct regset
*regset
,
1073 const struct regcache
*regcache
,
1074 int regnum
, void *buf
, size_t size
)
1076 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1080 regcache::collect_regset (const struct regset
*regset
,
1081 int regnum
, void *buf
, size_t size
) const
1083 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1087 /* Special handling for register PC. */
1090 regcache_read_pc (struct regcache
*regcache
)
1092 struct gdbarch
*gdbarch
= regcache
->arch ();
1096 if (gdbarch_read_pc_p (gdbarch
))
1097 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1098 /* Else use per-frame method on get_current_frame. */
1099 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1103 if (regcache_cooked_read_unsigned (regcache
,
1104 gdbarch_pc_regnum (gdbarch
),
1105 &raw_val
) == REG_UNAVAILABLE
)
1106 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1108 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1111 internal_error (__FILE__
, __LINE__
,
1112 _("regcache_read_pc: Unable to find PC"));
1117 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1119 struct gdbarch
*gdbarch
= regcache
->arch ();
1121 if (gdbarch_write_pc_p (gdbarch
))
1122 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1123 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1124 regcache_cooked_write_unsigned (regcache
,
1125 gdbarch_pc_regnum (gdbarch
), pc
);
1127 internal_error (__FILE__
, __LINE__
,
1128 _("regcache_write_pc: Unable to update PC"));
1130 /* Writing the PC (for instance, from "load") invalidates the
1132 reinit_frame_cache ();
1136 reg_buffer::num_raw_registers () const
1138 return gdbarch_num_regs (arch ());
1142 regcache::debug_print_register (const char *func
, int regno
)
1144 struct gdbarch
*gdbarch
= arch ();
1146 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1147 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1148 && gdbarch_register_name (gdbarch
, regno
) != NULL
1149 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1150 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1151 gdbarch_register_name (gdbarch
, regno
));
1153 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1154 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1156 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1157 int size
= register_size (gdbarch
, regno
);
1158 gdb_byte
*buf
= register_buffer (regno
);
1160 fprintf_unfiltered (gdb_stdlog
, " = ");
1161 for (int i
= 0; i
< size
; i
++)
1163 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1165 if (size
<= sizeof (LONGEST
))
1167 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1169 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1170 core_addr_to_string_nz (val
), plongest (val
));
1173 fprintf_unfiltered (gdb_stdlog
, "\n");
1177 reg_flush_command (const char *command
, int from_tty
)
1179 /* Force-flush the register cache. */
1180 registers_changed ();
1182 printf_filtered (_("Register cache flushed.\n"));
1186 register_dump::dump (ui_file
*file
)
1188 auto descr
= regcache_descr (m_gdbarch
);
1190 int footnote_nr
= 0;
1191 int footnote_register_offset
= 0;
1192 int footnote_register_type_name_null
= 0;
1193 long register_offset
= 0;
1195 gdb_assert (descr
->nr_cooked_registers
1196 == (gdbarch_num_regs (m_gdbarch
)
1197 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1199 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1203 fprintf_unfiltered (file
, " %-10s", "Name");
1206 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1210 else if (p
[0] == '\0')
1212 fprintf_unfiltered (file
, " %-10s", p
);
1217 fprintf_unfiltered (file
, " %4s", "Nr");
1219 fprintf_unfiltered (file
, " %4d", regnum
);
1221 /* Relative number. */
1223 fprintf_unfiltered (file
, " %4s", "Rel");
1224 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1225 fprintf_unfiltered (file
, " %4d", regnum
);
1227 fprintf_unfiltered (file
, " %4d",
1228 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1232 fprintf_unfiltered (file
, " %6s ", "Offset");
1235 fprintf_unfiltered (file
, " %6ld",
1236 descr
->register_offset
[regnum
]);
1237 if (register_offset
!= descr
->register_offset
[regnum
]
1239 && (descr
->register_offset
[regnum
]
1240 != (descr
->register_offset
[regnum
- 1]
1241 + descr
->sizeof_register
[regnum
- 1])))
1244 if (!footnote_register_offset
)
1245 footnote_register_offset
= ++footnote_nr
;
1246 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1249 fprintf_unfiltered (file
, " ");
1250 register_offset
= (descr
->register_offset
[regnum
]
1251 + descr
->sizeof_register
[regnum
]);
1256 fprintf_unfiltered (file
, " %5s ", "Size");
1258 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1263 std::string name_holder
;
1269 static const char blt
[] = "builtin_type";
1271 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1274 if (!footnote_register_type_name_null
)
1275 footnote_register_type_name_null
= ++footnote_nr
;
1276 name_holder
= string_printf ("*%d",
1277 footnote_register_type_name_null
);
1278 t
= name_holder
.c_str ();
1280 /* Chop a leading builtin_type. */
1281 if (startswith (t
, blt
))
1284 fprintf_unfiltered (file
, " %-15s", t
);
1287 /* Leading space always present. */
1288 fprintf_unfiltered (file
, " ");
1290 dump_reg (file
, regnum
);
1292 fprintf_unfiltered (file
, "\n");
1295 if (footnote_register_offset
)
1296 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1297 footnote_register_offset
);
1298 if (footnote_register_type_name_null
)
1299 fprintf_unfiltered (file
,
1300 "*%d: Register type's name NULL.\n",
1301 footnote_register_type_name_null
);
1305 #include "selftest.h"
1306 #include "selftest-arch.h"
1307 #include "gdbthread.h"
1308 #include "target-float.h"
1310 namespace selftests
{
1312 class regcache_access
: public regcache
1316 /* Return the number of elements in current_regcache. */
1319 current_regcache_size ()
1321 return std::distance (regcache::current_regcache
.begin (),
1322 regcache::current_regcache
.end ());
1327 current_regcache_test (void)
1329 /* It is empty at the start. */
1330 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1332 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1334 /* Get regcache from ptid1, a new regcache is added to
1335 current_regcache. */
1336 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1340 SELF_CHECK (regcache
!= NULL
);
1341 SELF_CHECK (regcache
->ptid () == ptid1
);
1342 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1344 /* Get regcache from ptid2, a new regcache is added to
1345 current_regcache. */
1346 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1349 SELF_CHECK (regcache
!= NULL
);
1350 SELF_CHECK (regcache
->ptid () == ptid2
);
1351 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1353 /* Get regcache from ptid3, a new regcache is added to
1354 current_regcache. */
1355 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1358 SELF_CHECK (regcache
!= NULL
);
1359 SELF_CHECK (regcache
->ptid () == ptid3
);
1360 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1362 /* Get regcache from ptid2 again, nothing is added to
1363 current_regcache. */
1364 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1367 SELF_CHECK (regcache
!= NULL
);
1368 SELF_CHECK (regcache
->ptid () == ptid2
);
1369 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1371 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1372 current_regcache. */
1373 registers_changed_ptid (ptid2
);
1374 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1377 class target_ops_no_register
: public test_target_ops
1380 target_ops_no_register ()
1381 : test_target_ops
{}
1386 fetch_registers_called
= 0;
1387 store_registers_called
= 0;
1388 xfer_partial_called
= 0;
1391 void fetch_registers (regcache
*regs
, int regno
) override
;
1392 void store_registers (regcache
*regs
, int regno
) override
;
1394 enum target_xfer_status
xfer_partial (enum target_object object
,
1395 const char *annex
, gdb_byte
*readbuf
,
1396 const gdb_byte
*writebuf
,
1397 ULONGEST offset
, ULONGEST len
,
1398 ULONGEST
*xfered_len
) override
;
1400 unsigned int fetch_registers_called
= 0;
1401 unsigned int store_registers_called
= 0;
1402 unsigned int xfer_partial_called
= 0;
1406 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1408 /* Mark register available. */
1409 regs
->raw_supply_zeroed (regno
);
1410 this->fetch_registers_called
++;
1414 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1416 this->store_registers_called
++;
1419 enum target_xfer_status
1420 target_ops_no_register::xfer_partial (enum target_object object
,
1421 const char *annex
, gdb_byte
*readbuf
,
1422 const gdb_byte
*writebuf
,
1423 ULONGEST offset
, ULONGEST len
,
1424 ULONGEST
*xfered_len
)
1426 this->xfer_partial_called
++;
1429 return TARGET_XFER_OK
;
1432 class readwrite_regcache
: public regcache
1435 readwrite_regcache (struct gdbarch
*gdbarch
)
1436 : regcache (gdbarch
, nullptr)
1440 /* Test regcache::cooked_read gets registers from raw registers and
1441 memory instead of target to_{fetch,store}_registers. */
1444 cooked_read_test (struct gdbarch
*gdbarch
)
1446 /* Error out if debugging something, because we're going to push the
1447 test target, which would pop any existing target. */
1448 if (current_top_target ()->to_stratum
>= process_stratum
)
1449 error (_("target already pushed"));
1451 /* Create a mock environment. An inferior with a thread, with a
1452 process_stratum target pushed. */
1454 target_ops_no_register mock_target
;
1455 ptid_t
mock_ptid (1, 1);
1456 inferior
mock_inferior (mock_ptid
.pid ());
1457 address_space mock_aspace
{};
1458 mock_inferior
.gdbarch
= gdbarch
;
1459 mock_inferior
.aspace
= &mock_aspace
;
1460 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1462 scoped_restore restore_thread_list
1463 = make_scoped_restore (&thread_list
, &mock_thread
);
1465 /* Add the mock inferior to the inferior list so that look ups by
1466 target+ptid can find it. */
1467 scoped_restore restore_inferior_list
1468 = make_scoped_restore (&inferior_list
);
1469 inferior_list
= &mock_inferior
;
1471 /* Switch to the mock inferior. */
1472 scoped_restore_current_inferior restore_current_inferior
;
1473 set_current_inferior (&mock_inferior
);
1475 /* Push the process_stratum target so we can mock accessing
1477 push_target (&mock_target
);
1479 /* Pop it again on exit (return/exception). */
1484 pop_all_targets_at_and_above (process_stratum
);
1488 /* Switch to the mock thread. */
1489 scoped_restore restore_inferior_ptid
1490 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1492 /* Test that read one raw register from regcache_no_target will go
1493 to the target layer. */
1496 /* Find a raw register which size isn't zero. */
1497 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1499 if (register_size (gdbarch
, regnum
) != 0)
1503 readwrite_regcache
readwrite (gdbarch
);
1504 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1506 readwrite
.raw_read (regnum
, buf
.data ());
1508 /* raw_read calls target_fetch_registers. */
1509 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1510 mock_target
.reset ();
1512 /* Mark all raw registers valid, so the following raw registers
1513 accesses won't go to target. */
1514 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1515 readwrite
.raw_update (i
);
1517 mock_target
.reset ();
1518 /* Then, read all raw and pseudo registers, and don't expect calling
1519 to_{fetch,store}_registers. */
1520 for (int regnum
= 0;
1521 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1524 if (register_size (gdbarch
, regnum
) == 0)
1527 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1529 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1531 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1532 SELF_CHECK (mock_target
.store_registers_called
== 0);
1534 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1535 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1536 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1538 mock_target
.reset ();
1541 readonly_detached_regcache
readonly (readwrite
);
1543 /* GDB may go to target layer to fetch all registers and memory for
1544 readonly regcache. */
1545 mock_target
.reset ();
1547 for (int regnum
= 0;
1548 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1551 if (register_size (gdbarch
, regnum
) == 0)
1554 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1555 enum register_status status
= readonly
.cooked_read (regnum
,
1558 if (regnum
< gdbarch_num_regs (gdbarch
))
1560 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1562 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1563 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1564 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1565 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1566 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1567 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1568 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1569 || bfd_arch
== bfd_arch_riscv
)
1571 /* Raw registers. If raw registers are not in save_reggroup,
1572 their status are unknown. */
1573 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1574 SELF_CHECK (status
== REG_VALID
);
1576 SELF_CHECK (status
== REG_UNKNOWN
);
1579 SELF_CHECK (status
== REG_VALID
);
1583 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1584 SELF_CHECK (status
== REG_VALID
);
1587 /* If pseudo registers are not in save_reggroup, some of
1588 them can be computed from saved raw registers, but some
1589 of them are unknown. */
1590 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1592 if (bfd_arch
== bfd_arch_frv
1593 || bfd_arch
== bfd_arch_m32c
1594 || bfd_arch
== bfd_arch_mep
1595 || bfd_arch
== bfd_arch_sh
)
1596 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1597 else if (bfd_arch
== bfd_arch_mips
1598 || bfd_arch
== bfd_arch_h8300
)
1599 SELF_CHECK (status
== REG_UNKNOWN
);
1601 SELF_CHECK (status
== REG_VALID
);
1605 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1606 SELF_CHECK (mock_target
.store_registers_called
== 0);
1607 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1609 mock_target
.reset ();
1613 /* Test regcache::cooked_write by writing some expected contents to
1614 registers, and checking that contents read from registers and the
1615 expected contents are the same. */
1618 cooked_write_test (struct gdbarch
*gdbarch
)
1620 /* Error out if debugging something, because we're going to push the
1621 test target, which would pop any existing target. */
1622 if (current_top_target ()->to_stratum
>= process_stratum
)
1623 error (_("target already pushed"));
1625 /* Create a mock environment. A process_stratum target pushed. */
1627 target_ops_no_register mock_target
;
1629 /* Push the process_stratum target so we can mock accessing
1631 push_target (&mock_target
);
1633 /* Pop it again on exit (return/exception). */
1638 pop_all_targets_at_and_above (process_stratum
);
1642 readwrite_regcache
readwrite (gdbarch
);
1644 const int num_regs
= (gdbarch_num_regs (gdbarch
)
1645 + gdbarch_num_pseudo_regs (gdbarch
));
1647 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1649 if (register_size (gdbarch
, regnum
) == 0
1650 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1653 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1655 if ((bfd_arch
== bfd_arch_sparc
1656 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1657 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1658 && gdbarch_ptr_bit (gdbarch
) == 64
1659 && (regnum
>= gdbarch_num_regs (gdbarch
)
1660 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1661 || (bfd_arch
== bfd_arch_spu
1662 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1663 TARGET_OBJECT_SPU. */
1664 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
1667 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1668 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1669 const auto type
= register_type (gdbarch
, regnum
);
1671 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1672 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1674 /* Generate valid float format. */
1675 target_float_from_string (expected
.data (), type
, "1.25");
1677 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1678 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1679 || TYPE_CODE (type
) == TYPE_CODE_PTR
1680 || TYPE_CODE (type
) == TYPE_CODE_UNION
1681 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1683 if (bfd_arch
== bfd_arch_ia64
1684 || (regnum
>= gdbarch_num_regs (gdbarch
)
1685 && (bfd_arch
== bfd_arch_xtensa
1686 || bfd_arch
== bfd_arch_bfin
1687 || bfd_arch
== bfd_arch_m32c
1688 /* m68hc11 pseudo registers are in memory. */
1689 || bfd_arch
== bfd_arch_m68hc11
1690 || bfd_arch
== bfd_arch_m68hc12
1691 || bfd_arch
== bfd_arch_s390
))
1692 || (bfd_arch
== bfd_arch_frv
1693 /* FRV pseudo registers except iacc0. */
1694 && regnum
> gdbarch_num_regs (gdbarch
)))
1696 /* Skip setting the expected values for some architecture
1699 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1701 /* RL78_PC_REGNUM */
1702 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1707 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1711 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1713 /* No idea how to test flags. */
1718 /* If we don't know how to create the expected value for the
1719 this type, make it fail. */
1723 readwrite
.cooked_write (regnum
, expected
.data ());
1725 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1726 SELF_CHECK (expected
== buf
);
1730 } // namespace selftests
1731 #endif /* GDB_SELF_TEST */
1734 _initialize_regcache (void)
1736 regcache_descr_handle
1737 = gdbarch_data_register_post_init (init_regcache_descr
);
1739 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1740 gdb::observers::thread_ptid_changed
.attach
1741 (regcache::regcache_thread_ptid_changed
);
1743 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1744 _("Force gdb to flush its register cache (maintainer command)"));
1747 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1749 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1750 selftests::cooked_read_test
);
1751 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1752 selftests::cooked_write_test
);