1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2019 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/>. */
22 #include "gdbthread.h"
24 #include "test-target.h"
28 #include "reggroups.h"
29 #include "observable.h"
31 #include <forward_list>
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created. */
42 struct gdbarch_data
*regcache_descr_handle
;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch
*gdbarch
;
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
54 long sizeof_raw_registers
;
56 /* The cooked register space. Each cooked register in the range
57 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
58 register. The remaining [NR_RAW_REGISTERS
59 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
60 both raw registers and memory by the architecture methods
61 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
62 int nr_cooked_registers
;
63 long sizeof_cooked_registers
;
65 /* Offset and size (in 8 bit bytes), of each register in the
66 register cache. All registers (including those in the range
67 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
69 long *register_offset
;
70 long *sizeof_register
;
72 /* Cached table containing the type of each register. */
73 struct type
**register_type
;
77 init_regcache_descr (struct gdbarch
*gdbarch
)
80 struct regcache_descr
*descr
;
81 gdb_assert (gdbarch
!= NULL
);
83 /* Create an initial, zero filled, table. */
84 descr
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct regcache_descr
);
85 descr
->gdbarch
= gdbarch
;
87 /* Total size of the register space. The raw registers are mapped
88 directly onto the raw register cache while the pseudo's are
89 either mapped onto raw-registers or memory. */
90 descr
->nr_cooked_registers
= gdbarch_num_cooked_regs (gdbarch
);
92 /* Fill in a table of register types. */
94 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
,
96 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
97 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
99 /* Construct a strictly RAW register cache. Don't allow pseudo's
100 into the register cache. */
102 /* Lay out the register cache.
104 NOTE: cagney/2002-05-22: Only register_type() is used when
105 constructing the register cache. It is assumed that the
106 register's raw size, virtual size and type length are all the
112 descr
->sizeof_register
113 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
114 descr
->register_offset
115 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
116 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
118 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
119 descr
->register_offset
[i
] = offset
;
120 offset
+= descr
->sizeof_register
[i
];
122 /* Set the real size of the raw register cache buffer. */
123 descr
->sizeof_raw_registers
= offset
;
125 for (; i
< descr
->nr_cooked_registers
; i
++)
127 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
128 descr
->register_offset
[i
] = offset
;
129 offset
+= descr
->sizeof_register
[i
];
131 /* Set the real size of the readonly register cache buffer. */
132 descr
->sizeof_cooked_registers
= offset
;
138 static struct regcache_descr
*
139 regcache_descr (struct gdbarch
*gdbarch
)
141 return (struct regcache_descr
*) gdbarch_data (gdbarch
,
142 regcache_descr_handle
);
145 /* Utility functions returning useful register attributes stored in
146 the regcache descr. */
149 register_type (struct gdbarch
*gdbarch
, int regnum
)
151 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
153 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
154 return descr
->register_type
[regnum
];
157 /* Utility functions returning useful register attributes stored in
158 the regcache descr. */
161 register_size (struct gdbarch
*gdbarch
, int regnum
)
163 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
166 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_cooked_regs (gdbarch
));
167 size
= descr
->sizeof_register
[regnum
];
171 /* See gdbsupport/common-regcache.h. */
174 regcache_register_size (const struct regcache
*regcache
, int n
)
176 return register_size (regcache
->arch (), n
);
179 reg_buffer::reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
)
180 : m_has_pseudo (has_pseudo
)
182 gdb_assert (gdbarch
!= NULL
);
183 m_descr
= regcache_descr (gdbarch
);
187 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_cooked_registers
] ());
188 m_register_status
.reset
189 (new register_status
[m_descr
->nr_cooked_registers
] ());
193 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_raw_registers
] ());
194 m_register_status
.reset
195 (new register_status
[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 readonly_detached_regcache::readonly_detached_regcache (regcache
&src
)
208 : readonly_detached_regcache (src
.arch (),
209 [&src
] (int regnum
, gdb_byte
*buf
)
211 return src
.cooked_read (regnum
, buf
);
217 reg_buffer::arch () const
219 return m_descr
->gdbarch
;
222 /* Return a pointer to register REGNUM's buffer cache. */
225 reg_buffer::register_buffer (int regnum
) const
227 return m_registers
.get () + m_descr
->register_offset
[regnum
];
231 reg_buffer::save (register_read_ftype cooked_read
)
233 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
236 /* It should have pseudo registers. */
237 gdb_assert (m_has_pseudo
);
238 /* Clear the dest. */
239 memset (m_registers
.get (), 0, m_descr
->sizeof_cooked_registers
);
240 memset (m_register_status
.get (), REG_UNKNOWN
, m_descr
->nr_cooked_registers
);
241 /* Copy over any registers (identified by their membership in the
242 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
243 gdbarch_num_pseudo_regs) range is checked since some architectures need
244 to save/restore `cooked' registers that live in memory. */
245 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
247 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
249 gdb_byte
*dst_buf
= register_buffer (regnum
);
250 enum register_status status
= cooked_read (regnum
, dst_buf
);
252 gdb_assert (status
!= REG_UNKNOWN
);
254 if (status
!= REG_VALID
)
255 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
257 m_register_status
[regnum
] = status
;
263 regcache::restore (readonly_detached_regcache
*src
)
265 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
268 gdb_assert (src
!= NULL
);
269 gdb_assert (src
->m_has_pseudo
);
271 gdb_assert (gdbarch
== src
->arch ());
273 /* Copy over any registers, being careful to only restore those that
274 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
275 + gdbarch_num_pseudo_regs) range is checked since some architectures need
276 to save/restore `cooked' registers that live in memory. */
277 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
279 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
281 if (src
->m_register_status
[regnum
] == REG_VALID
)
282 cooked_write (regnum
, src
->register_buffer (regnum
));
287 /* See gdbsupport/common-regcache.h. */
290 reg_buffer::get_register_status (int regnum
) const
292 assert_regnum (regnum
);
294 return m_register_status
[regnum
];
298 reg_buffer::invalidate (int regnum
)
300 assert_regnum (regnum
);
301 m_register_status
[regnum
] = REG_UNKNOWN
;
305 reg_buffer::assert_regnum (int regnum
) const
307 gdb_assert (regnum
>= 0);
309 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
311 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
314 /* Global structure containing the current regcache. */
316 /* NOTE: this is a write-through cache. There is no "dirty" bit for
317 recording if the register values have been changed (eg. by the
318 user). Therefore all registers must be written back to the
319 target when appropriate. */
321 /* Key for the hash map keeping the regcaches. */
325 ptid_arch (ptid_t ptid
, gdbarch
*arch
)
326 : ptid (ptid
), arch (arch
)
332 bool operator== (const ptid_arch
&other
) const
334 return this->ptid
== other
.ptid
&& this->arch
== other
.arch
;
338 /* Hash function for ptid_arch. */
340 struct hash_ptid_arch
342 size_t operator() (const ptid_arch
&val
) const
345 std::hash
<long> h_long
;
346 return h_ptid (val
.ptid
) + h_long ((long) val
.arch
);
350 using ptid_arch_regcache_map
= std::unordered_map
<ptid_arch
, regcache
*, hash_ptid_arch
>;
352 /* Hash map containing the regcaches. */
354 static ptid_arch_regcache_map the_regcaches
;
357 get_thread_arch_aspace_regcache (ptid_t ptid
, gdbarch
*arch
,
358 struct address_space
*aspace
)
360 /* Look up a regcache for this (ptid, arch). */
361 ptid_arch
key (ptid
, arch
);
362 auto it
= the_regcaches
.find (key
);
363 if (it
!= the_regcaches
.end ())
366 /* It does not exist, create it. */
367 regcache
*new_regcache
= new regcache (arch
, aspace
);
368 new_regcache
->set_ptid (ptid
);
370 the_regcaches
[key
] = new_regcache
;
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
|| 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
);
398 /* See regcache.h. */
401 get_thread_regcache (thread_info
*thread
)
403 return get_thread_regcache (thread
->ptid
);
407 get_current_regcache (void)
409 return get_thread_regcache (inferior_thread ());
412 /* See gdbsupport/common-regcache.h. */
415 get_thread_regcache_for_ptid (ptid_t ptid
)
417 return get_thread_regcache (ptid
);
420 /* Observer for the target_changed event. */
423 regcache_observer_target_changed (struct target_ops
*target
)
425 registers_changed ();
428 /* Update global variables old ptids to hold NEW_PTID if they were
431 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
433 std::vector
<ptid_arch
> keys_to_update
;
435 /* Find all the regcaches to updates. */
436 for (auto &pair
: the_regcaches
)
438 regcache
*rc
= pair
.second
;
439 if (rc
->ptid () == old_ptid
)
440 keys_to_update
.push_back (pair
.first
);
443 for (const ptid_arch
&old_key
: keys_to_update
)
445 /* Get the regcache, delete the hash map entry. */
446 auto it
= the_regcaches
.find (old_key
);
447 gdb_assert (it
!= the_regcaches
.end ());
448 regcache
*rc
= it
->second
;
450 the_regcaches
.erase (it
);
452 /* Insert the regcache back, with an updated key. */
453 ptid_arch
new_key (new_ptid
, rc
->arch ());
454 rc
->set_ptid (new_ptid
);
455 the_regcaches
[new_key
] = rc
;
459 /* Low level examining and depositing of registers.
461 The caller is responsible for making sure that the inferior is
462 stopped before calling the fetching routines, or it will get
463 garbage. (a change from GDB version 3, in which the caller got the
464 value from the last stop). */
466 /* REGISTERS_CHANGED ()
468 Indicate that registers may have changed, so invalidate the cache. */
471 registers_changed_ptid (ptid_t ptid
)
473 for (auto iter
= the_regcaches
.begin (); iter
!= the_regcaches
.end (); )
475 regcache
*rc
= iter
->second
;
477 if (rc
->ptid ().matches (ptid
))
480 iter
= the_regcaches
.erase (iter
);
486 if (current_thread_ptid
.matches (ptid
))
488 current_thread_ptid
= null_ptid
;
489 current_thread_arch
= NULL
;
492 if (inferior_ptid
.matches (ptid
))
494 /* We just deleted the regcache of the current thread. Need to
495 forget about any frames we have cached, too. */
496 reinit_frame_cache ();
500 /* See regcache.h. */
503 registers_changed_thread (thread_info
*thread
)
505 registers_changed_ptid (thread
->ptid
);
509 registers_changed (void)
511 registers_changed_ptid (minus_one_ptid
);
515 regcache::raw_update (int regnum
)
517 assert_regnum (regnum
);
519 /* Make certain that the register cache is up-to-date with respect
520 to the current thread. This switching shouldn't be necessary
521 only there is still only one target side register cache. Sigh!
522 On the bright side, at least there is a regcache object. */
524 if (get_register_status (regnum
) == REG_UNKNOWN
)
526 target_fetch_registers (this, regnum
);
528 /* A number of targets can't access the whole set of raw
529 registers (because the debug API provides no means to get at
531 if (m_register_status
[regnum
] == REG_UNKNOWN
)
532 m_register_status
[regnum
] = REG_UNAVAILABLE
;
537 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
539 gdb_assert (buf
!= NULL
);
542 if (m_register_status
[regnum
] != REG_VALID
)
543 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
545 memcpy (buf
, register_buffer (regnum
),
546 m_descr
->sizeof_register
[regnum
]);
548 return m_register_status
[regnum
];
552 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
554 gdb_assert (regcache
!= NULL
);
555 return regcache
->raw_read (regnum
, val
);
558 template<typename T
, typename
>
560 readable_regcache::raw_read (int regnum
, T
*val
)
563 enum register_status status
;
565 assert_regnum (regnum
);
566 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
567 status
= raw_read (regnum
, buf
);
568 if (status
== REG_VALID
)
569 *val
= extract_integer
<T
> (buf
,
570 m_descr
->sizeof_register
[regnum
],
571 gdbarch_byte_order (m_descr
->gdbarch
));
578 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
581 gdb_assert (regcache
!= NULL
);
582 return regcache
->raw_read (regnum
, val
);
586 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
588 gdb_assert (regcache
!= NULL
);
589 regcache
->raw_write (regnum
, val
);
592 template<typename T
, typename
>
594 regcache::raw_write (int regnum
, T val
)
598 assert_regnum (regnum
);
599 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
600 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
601 gdbarch_byte_order (m_descr
->gdbarch
), val
);
602 raw_write (regnum
, buf
);
606 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
609 gdb_assert (regcache
!= NULL
);
610 regcache
->raw_write (regnum
, val
);
614 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
617 enum register_status status
;
619 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
620 if (status
== REG_UNAVAILABLE
)
621 throw_error (NOT_AVAILABLE_ERROR
,
622 _("Register %d is not available"), regnum
);
627 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
629 gdb_assert (regnum
>= 0);
630 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
631 if (regnum
< num_raw_registers ())
632 return raw_read (regnum
, buf
);
633 else if (m_has_pseudo
634 && m_register_status
[regnum
] != REG_UNKNOWN
)
636 if (m_register_status
[regnum
] == REG_VALID
)
637 memcpy (buf
, register_buffer (regnum
),
638 m_descr
->sizeof_register
[regnum
]);
640 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
642 return m_register_status
[regnum
];
644 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
646 struct value
*mark
, *computed
;
647 enum register_status result
= REG_VALID
;
649 mark
= value_mark ();
651 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
653 if (value_entirely_available (computed
))
654 memcpy (buf
, value_contents_raw (computed
),
655 m_descr
->sizeof_register
[regnum
]);
658 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
659 result
= REG_UNAVAILABLE
;
662 value_free_to_mark (mark
);
667 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
672 readable_regcache::cooked_read_value (int regnum
)
674 gdb_assert (regnum
>= 0);
675 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
677 if (regnum
< num_raw_registers ()
678 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
679 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
681 struct value
*result
;
683 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
684 VALUE_LVAL (result
) = lval_register
;
685 VALUE_REGNUM (result
) = regnum
;
687 /* It is more efficient in general to do this delegation in this
688 direction than in the other one, even though the value-based
690 if (cooked_read (regnum
,
691 value_contents_raw (result
)) == REG_UNAVAILABLE
)
692 mark_value_bytes_unavailable (result
, 0,
693 TYPE_LENGTH (value_type (result
)));
698 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
703 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
706 gdb_assert (regcache
!= NULL
);
707 return regcache
->cooked_read (regnum
, val
);
710 template<typename T
, typename
>
712 readable_regcache::cooked_read (int regnum
, T
*val
)
714 enum register_status status
;
717 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
718 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
719 status
= cooked_read (regnum
, buf
);
720 if (status
== REG_VALID
)
721 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
722 gdbarch_byte_order (m_descr
->gdbarch
));
729 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
732 gdb_assert (regcache
!= NULL
);
733 return regcache
->cooked_read (regnum
, val
);
737 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
740 gdb_assert (regcache
!= NULL
);
741 regcache
->cooked_write (regnum
, val
);
744 template<typename T
, typename
>
746 regcache::cooked_write (int regnum
, T val
)
750 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
751 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
752 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
753 gdbarch_byte_order (m_descr
->gdbarch
), val
);
754 cooked_write (regnum
, buf
);
758 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
761 gdb_assert (regcache
!= NULL
);
762 regcache
->cooked_write (regnum
, val
);
766 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
769 gdb_assert (buf
!= NULL
);
770 assert_regnum (regnum
);
772 /* On the sparc, writing %g0 is a no-op, so we don't even want to
773 change the registers array if something writes to this register. */
774 if (gdbarch_cannot_store_register (arch (), regnum
))
777 /* If we have a valid copy of the register, and new value == old
778 value, then don't bother doing the actual store. */
779 if (get_register_status (regnum
) == REG_VALID
780 && (memcmp (register_buffer (regnum
), buf
,
781 m_descr
->sizeof_register
[regnum
]) == 0))
784 target_prepare_to_store (this);
785 raw_supply (regnum
, buf
);
787 /* Invalidate the register after it is written, in case of a
790 = make_scope_exit ([&] { this->invalidate (regnum
); });
792 target_store_registers (this, regnum
);
794 /* The target did not throw an error so we can discard invalidating
796 invalidator
.release ();
800 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
802 gdb_assert (regnum
>= 0);
803 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
804 if (regnum
< num_raw_registers ())
805 raw_write (regnum
, buf
);
807 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
811 /* See regcache.h. */
814 readable_regcache::read_part (int regnum
, int offset
, int len
,
815 gdb_byte
*out
, bool is_raw
)
817 int reg_size
= register_size (arch (), regnum
);
819 gdb_assert (out
!= NULL
);
820 gdb_assert (offset
>= 0 && offset
<= reg_size
);
821 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
823 if (offset
== 0 && len
== 0)
829 if (offset
== 0 && len
== reg_size
)
831 /* Read the full register. */
832 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
835 enum register_status status
;
836 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
838 /* Read full register to buffer. */
839 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
840 if (status
!= REG_VALID
)
844 memcpy (out
, reg
+ offset
, len
);
848 /* See regcache.h. */
851 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
854 int reg_size
= register_size (arch (), regnum
);
856 gdb_assert (out
!= nullptr);
857 gdb_assert (offset
>= 0 && offset
<= reg_size
);
858 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
860 if (offset
== 0 && len
== 0)
866 if (offset
== 0 && len
== reg_size
)
868 /* Collect the full register. */
869 return raw_collect (regnum
, out
);
872 /* Read to buffer, then write out. */
873 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
874 raw_collect (regnum
, reg
);
875 memcpy (out
, reg
+ offset
, len
);
878 /* See regcache.h. */
881 regcache::write_part (int regnum
, int offset
, int len
,
882 const gdb_byte
*in
, bool is_raw
)
884 int reg_size
= register_size (arch (), regnum
);
886 gdb_assert (in
!= NULL
);
887 gdb_assert (offset
>= 0 && offset
<= reg_size
);
888 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
890 if (offset
== 0 && len
== 0)
896 if (offset
== 0 && len
== reg_size
)
898 /* Write the full register. */
899 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
903 enum register_status status
;
904 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
906 /* Read existing register to buffer. */
907 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
908 if (status
!= REG_VALID
)
911 /* Update buffer, then write back to regcache. */
912 memcpy (reg
+ offset
, in
, len
);
913 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
917 /* See regcache.h. */
920 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
923 int reg_size
= register_size (arch (), regnum
);
925 gdb_assert (in
!= nullptr);
926 gdb_assert (offset
>= 0 && offset
<= reg_size
);
927 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
929 if (offset
== 0 && len
== 0)
935 if (offset
== 0 && len
== reg_size
)
937 /* Supply the full register. */
938 return raw_supply (regnum
, in
);
941 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
943 /* Read existing value to buffer. */
944 raw_collect (regnum
, reg
);
946 /* Write to buffer, then write out. */
947 memcpy (reg
+ offset
, in
, len
);
948 raw_supply (regnum
, reg
);
952 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
955 assert_regnum (regnum
);
956 return read_part (regnum
, offset
, len
, buf
, true);
959 /* See regcache.h. */
962 regcache::raw_write_part (int regnum
, int offset
, int len
,
965 assert_regnum (regnum
);
966 write_part (regnum
, offset
, len
, buf
, true);
969 /* See regcache.h. */
972 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
975 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
976 return read_part (regnum
, offset
, len
, buf
, false);
979 /* See regcache.h. */
982 regcache::cooked_write_part (int regnum
, int offset
, int len
,
985 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
986 write_part (regnum
, offset
, len
, buf
, false);
989 /* See gdbsupport/common-regcache.h. */
992 reg_buffer::raw_supply (int regnum
, const void *buf
)
997 assert_regnum (regnum
);
999 regbuf
= register_buffer (regnum
);
1000 size
= m_descr
->sizeof_register
[regnum
];
1004 memcpy (regbuf
, buf
, size
);
1005 m_register_status
[regnum
] = REG_VALID
;
1009 /* This memset not strictly necessary, but better than garbage
1010 in case the register value manages to escape somewhere (due
1011 to a bug, no less). */
1012 memset (regbuf
, 0, size
);
1013 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1017 /* See regcache.h. */
1020 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1021 int addr_len
, bool is_signed
)
1023 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1027 assert_regnum (regnum
);
1029 regbuf
= register_buffer (regnum
);
1030 regsize
= m_descr
->sizeof_register
[regnum
];
1032 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1034 m_register_status
[regnum
] = REG_VALID
;
1037 /* See regcache.h. */
1040 reg_buffer::raw_supply_zeroed (int regnum
)
1045 assert_regnum (regnum
);
1047 regbuf
= register_buffer (regnum
);
1048 size
= m_descr
->sizeof_register
[regnum
];
1050 memset (regbuf
, 0, size
);
1051 m_register_status
[regnum
] = REG_VALID
;
1054 /* See gdbsupport/common-regcache.h. */
1057 reg_buffer::raw_collect (int regnum
, void *buf
) const
1062 gdb_assert (buf
!= NULL
);
1063 assert_regnum (regnum
);
1065 regbuf
= register_buffer (regnum
);
1066 size
= m_descr
->sizeof_register
[regnum
];
1067 memcpy (buf
, regbuf
, size
);
1070 /* See regcache.h. */
1073 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1074 bool is_signed
) const
1076 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1077 const gdb_byte
*regbuf
;
1080 assert_regnum (regnum
);
1082 regbuf
= register_buffer (regnum
);
1083 regsize
= m_descr
->sizeof_register
[regnum
];
1085 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1089 /* See regcache.h. */
1092 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1093 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1094 int slot_size
, int offs
) const
1096 struct gdbarch
*gdbarch
= arch ();
1097 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1099 /* Use part versions and reg_size to prevent possible buffer overflows when
1100 accessing the regcache. */
1102 if (out_buf
!= nullptr)
1104 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1106 /* Ensure any additional space is cleared. */
1107 if (slot_size
> reg_size
)
1108 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1110 else if (in_buf
!= nullptr)
1111 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1114 /* Invalidate the register. */
1115 out_regcache
->raw_supply (regnum
, nullptr);
1119 /* See regcache.h. */
1122 regcache::transfer_regset (const struct regset
*regset
,
1123 struct regcache
*out_regcache
,
1124 int regnum
, const gdb_byte
*in_buf
,
1125 gdb_byte
*out_buf
, size_t size
) const
1127 const struct regcache_map_entry
*map
;
1128 int offs
= 0, count
;
1130 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1131 (count
= map
->count
) != 0;
1134 int regno
= map
->regno
;
1135 int slot_size
= map
->size
;
1137 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1138 slot_size
= m_descr
->sizeof_register
[regno
];
1140 if (regno
== REGCACHE_MAP_SKIP
1142 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1143 offs
+= count
* slot_size
;
1145 else if (regnum
== -1)
1146 for (; count
--; regno
++, offs
+= slot_size
)
1148 if (offs
+ slot_size
> size
)
1151 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1156 /* Transfer a single register and return. */
1157 offs
+= (regnum
- regno
) * slot_size
;
1158 if (offs
+ slot_size
> size
)
1161 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1168 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1169 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1170 If BUF is NULL, set the register(s) to "unavailable" status. */
1173 regcache_supply_regset (const struct regset
*regset
,
1174 struct regcache
*regcache
,
1175 int regnum
, const void *buf
, size_t size
)
1177 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1181 regcache::supply_regset (const struct regset
*regset
,
1182 int regnum
, const void *buf
, size_t size
)
1184 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1187 /* Collect register REGNUM from REGCACHE to BUF, using the register
1188 map in REGSET. If REGNUM is -1, do this for all registers in
1192 regcache_collect_regset (const struct regset
*regset
,
1193 const struct regcache
*regcache
,
1194 int regnum
, void *buf
, size_t size
)
1196 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1200 regcache::collect_regset (const struct regset
*regset
,
1201 int regnum
, void *buf
, size_t size
) const
1203 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1206 /* See gdbsupport/common-regcache.h. */
1209 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1211 gdb_assert (buf
!= NULL
);
1212 assert_regnum (regnum
);
1214 const char *regbuf
= (const char *) register_buffer (regnum
);
1215 size_t size
= m_descr
->sizeof_register
[regnum
];
1216 gdb_assert (size
>= offset
);
1218 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1221 /* Special handling for register PC. */
1224 regcache_read_pc (struct regcache
*regcache
)
1226 struct gdbarch
*gdbarch
= regcache
->arch ();
1230 if (gdbarch_read_pc_p (gdbarch
))
1231 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1232 /* Else use per-frame method on get_current_frame. */
1233 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1237 if (regcache_cooked_read_unsigned (regcache
,
1238 gdbarch_pc_regnum (gdbarch
),
1239 &raw_val
) == REG_UNAVAILABLE
)
1240 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1242 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1245 internal_error (__FILE__
, __LINE__
,
1246 _("regcache_read_pc: Unable to find PC"));
1251 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1253 struct gdbarch
*gdbarch
= regcache
->arch ();
1255 if (gdbarch_write_pc_p (gdbarch
))
1256 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1257 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1258 regcache_cooked_write_unsigned (regcache
,
1259 gdbarch_pc_regnum (gdbarch
), pc
);
1261 internal_error (__FILE__
, __LINE__
,
1262 _("regcache_write_pc: Unable to update PC"));
1264 /* Writing the PC (for instance, from "load") invalidates the
1266 reinit_frame_cache ();
1270 reg_buffer::num_raw_registers () const
1272 return gdbarch_num_regs (arch ());
1276 regcache::debug_print_register (const char *func
, int regno
)
1278 struct gdbarch
*gdbarch
= arch ();
1280 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1281 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1282 && gdbarch_register_name (gdbarch
, regno
) != NULL
1283 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1284 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1285 gdbarch_register_name (gdbarch
, regno
));
1287 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1288 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1290 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1291 int size
= register_size (gdbarch
, regno
);
1292 gdb_byte
*buf
= register_buffer (regno
);
1294 fprintf_unfiltered (gdb_stdlog
, " = ");
1295 for (int i
= 0; i
< size
; i
++)
1297 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1299 if (size
<= sizeof (LONGEST
))
1301 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1303 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1304 core_addr_to_string_nz (val
), plongest (val
));
1307 fprintf_unfiltered (gdb_stdlog
, "\n");
1311 reg_flush_command (const char *command
, int from_tty
)
1313 /* Force-flush the register cache. */
1314 registers_changed ();
1316 printf_filtered (_("Register cache flushed.\n"));
1320 register_dump::dump (ui_file
*file
)
1322 auto descr
= regcache_descr (m_gdbarch
);
1324 int footnote_nr
= 0;
1325 int footnote_register_offset
= 0;
1326 int footnote_register_type_name_null
= 0;
1327 long register_offset
= 0;
1329 gdb_assert (descr
->nr_cooked_registers
1330 == gdbarch_num_cooked_regs (m_gdbarch
));
1332 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1336 fprintf_unfiltered (file
, " %-10s", "Name");
1339 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1343 else if (p
[0] == '\0')
1345 fprintf_unfiltered (file
, " %-10s", p
);
1350 fprintf_unfiltered (file
, " %4s", "Nr");
1352 fprintf_unfiltered (file
, " %4d", regnum
);
1354 /* Relative number. */
1356 fprintf_unfiltered (file
, " %4s", "Rel");
1357 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1358 fprintf_unfiltered (file
, " %4d", regnum
);
1360 fprintf_unfiltered (file
, " %4d",
1361 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1365 fprintf_unfiltered (file
, " %6s ", "Offset");
1368 fprintf_unfiltered (file
, " %6ld",
1369 descr
->register_offset
[regnum
]);
1370 if (register_offset
!= descr
->register_offset
[regnum
]
1372 && (descr
->register_offset
[regnum
]
1373 != (descr
->register_offset
[regnum
- 1]
1374 + descr
->sizeof_register
[regnum
- 1])))
1377 if (!footnote_register_offset
)
1378 footnote_register_offset
= ++footnote_nr
;
1379 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1382 fprintf_unfiltered (file
, " ");
1383 register_offset
= (descr
->register_offset
[regnum
]
1384 + descr
->sizeof_register
[regnum
]);
1389 fprintf_unfiltered (file
, " %5s ", "Size");
1391 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1396 std::string name_holder
;
1402 static const char blt
[] = "builtin_type";
1404 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1407 if (!footnote_register_type_name_null
)
1408 footnote_register_type_name_null
= ++footnote_nr
;
1409 name_holder
= string_printf ("*%d",
1410 footnote_register_type_name_null
);
1411 t
= name_holder
.c_str ();
1413 /* Chop a leading builtin_type. */
1414 if (startswith (t
, blt
))
1417 fprintf_unfiltered (file
, " %-15s", t
);
1420 /* Leading space always present. */
1421 fprintf_unfiltered (file
, " ");
1423 dump_reg (file
, regnum
);
1425 fprintf_unfiltered (file
, "\n");
1428 if (footnote_register_offset
)
1429 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1430 footnote_register_offset
);
1431 if (footnote_register_type_name_null
)
1432 fprintf_unfiltered (file
,
1433 "*%d: Register type's name NULL.\n",
1434 footnote_register_type_name_null
);
1438 #include "gdbsupport/selftest.h"
1439 #include "selftest-arch.h"
1440 #include "target-float.h"
1442 namespace selftests
{
1444 class regcache_access
: public regcache
1448 /* Return the number of elements in current_regcache. */
1451 current_regcache_size ()
1453 return the_regcaches
.size ();
1458 current_regcache_test (void)
1460 /* It is empty at the start. */
1461 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1463 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1465 /* Get regcache from ptid1, a new regcache is added to
1466 current_regcache. */
1467 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1471 SELF_CHECK (regcache
!= NULL
);
1472 SELF_CHECK (regcache
->ptid () == ptid1
);
1473 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1475 /* Get regcache from ptid2, a new regcache is added to
1476 current_regcache. */
1477 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1480 SELF_CHECK (regcache
!= NULL
);
1481 SELF_CHECK (regcache
->ptid () == ptid2
);
1482 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1484 /* Get regcache from ptid3, a new regcache is added to
1485 current_regcache. */
1486 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1489 SELF_CHECK (regcache
!= NULL
);
1490 SELF_CHECK (regcache
->ptid () == ptid3
);
1491 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1493 /* Get regcache from ptid2 again, nothing is added to
1494 current_regcache. */
1495 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1498 SELF_CHECK (regcache
!= NULL
);
1499 SELF_CHECK (regcache
->ptid () == ptid2
);
1500 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1502 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1503 current_regcache. */
1504 registers_changed_ptid (ptid2
);
1505 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1508 class target_ops_no_register
: public test_target_ops
1511 target_ops_no_register ()
1512 : test_target_ops
{}
1517 fetch_registers_called
= 0;
1518 store_registers_called
= 0;
1519 xfer_partial_called
= 0;
1522 void fetch_registers (regcache
*regs
, int regno
) override
;
1523 void store_registers (regcache
*regs
, int regno
) override
;
1525 enum target_xfer_status
xfer_partial (enum target_object object
,
1526 const char *annex
, gdb_byte
*readbuf
,
1527 const gdb_byte
*writebuf
,
1528 ULONGEST offset
, ULONGEST len
,
1529 ULONGEST
*xfered_len
) override
;
1531 unsigned int fetch_registers_called
= 0;
1532 unsigned int store_registers_called
= 0;
1533 unsigned int xfer_partial_called
= 0;
1537 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1539 /* Mark register available. */
1540 regs
->raw_supply_zeroed (regno
);
1541 this->fetch_registers_called
++;
1545 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1547 this->store_registers_called
++;
1550 enum target_xfer_status
1551 target_ops_no_register::xfer_partial (enum target_object object
,
1552 const char *annex
, gdb_byte
*readbuf
,
1553 const gdb_byte
*writebuf
,
1554 ULONGEST offset
, ULONGEST len
,
1555 ULONGEST
*xfered_len
)
1557 this->xfer_partial_called
++;
1560 return TARGET_XFER_OK
;
1563 class readwrite_regcache
: public regcache
1566 readwrite_regcache (struct gdbarch
*gdbarch
)
1567 : regcache (gdbarch
, nullptr)
1571 /* Test regcache::cooked_read gets registers from raw registers and
1572 memory instead of target to_{fetch,store}_registers. */
1575 cooked_read_test (struct gdbarch
*gdbarch
)
1577 /* Error out if debugging something, because we're going to push the
1578 test target, which would pop any existing target. */
1579 if (current_top_target ()->stratum () >= process_stratum
)
1580 error (_("target already pushed"));
1582 /* Create a mock environment. An inferior with a thread, with a
1583 process_stratum target pushed. */
1585 target_ops_no_register mock_target
;
1586 ptid_t
mock_ptid (1, 1);
1587 inferior
mock_inferior (mock_ptid
.pid ());
1588 address_space mock_aspace
{};
1589 mock_inferior
.gdbarch
= gdbarch
;
1590 mock_inferior
.aspace
= &mock_aspace
;
1591 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1592 mock_inferior
.thread_map
[mock_ptid
] = &mock_thread
;
1594 /* Add the mock inferior to the inferior list so that look ups by
1595 target+ptid can find it. */
1596 scoped_restore restore_inferior_list
1597 = make_scoped_restore (&inferior_list
);
1598 inferior_list
= &mock_inferior
;
1600 /* Switch to the mock inferior. */
1601 scoped_restore_current_inferior restore_current_inferior
;
1602 set_current_inferior (&mock_inferior
);
1604 /* Push the process_stratum target so we can mock accessing
1606 push_target (&mock_target
);
1608 /* Pop it again on exit (return/exception). */
1613 pop_all_targets_at_and_above (process_stratum
);
1617 /* Switch to the mock thread. */
1618 scoped_restore restore_inferior_ptid
1619 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1621 /* Test that read one raw register from regcache_no_target will go
1622 to the target layer. */
1624 /* Find a raw register which size isn't zero. */
1626 for (nonzero_regnum
= 0;
1627 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1630 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1634 readwrite_regcache
readwrite (gdbarch
);
1635 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1637 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1639 /* raw_read calls target_fetch_registers. */
1640 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1641 mock_target
.reset ();
1643 /* Mark all raw registers valid, so the following raw registers
1644 accesses won't go to target. */
1645 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1646 readwrite
.raw_update (i
);
1648 mock_target
.reset ();
1649 /* Then, read all raw and pseudo registers, and don't expect calling
1650 to_{fetch,store}_registers. */
1651 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1653 if (register_size (gdbarch
, regnum
) == 0)
1656 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1658 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1659 inner_buf
.data ()));
1661 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1662 SELF_CHECK (mock_target
.store_registers_called
== 0);
1663 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1665 mock_target
.reset ();
1668 readonly_detached_regcache
readonly (readwrite
);
1670 /* GDB may go to target layer to fetch all registers and memory for
1671 readonly regcache. */
1672 mock_target
.reset ();
1674 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1676 if (register_size (gdbarch
, regnum
) == 0)
1679 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1680 enum register_status status
= readonly
.cooked_read (regnum
,
1683 if (regnum
< gdbarch_num_regs (gdbarch
))
1685 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1687 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1688 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1689 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1690 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1691 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1692 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1693 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1694 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1696 /* Raw registers. If raw registers are not in save_reggroup,
1697 their status are unknown. */
1698 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1699 SELF_CHECK (status
== REG_VALID
);
1701 SELF_CHECK (status
== REG_UNKNOWN
);
1704 SELF_CHECK (status
== REG_VALID
);
1708 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1709 SELF_CHECK (status
== REG_VALID
);
1712 /* If pseudo registers are not in save_reggroup, some of
1713 them can be computed from saved raw registers, but some
1714 of them are unknown. */
1715 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1717 if (bfd_arch
== bfd_arch_frv
1718 || bfd_arch
== bfd_arch_m32c
1719 || bfd_arch
== bfd_arch_mep
1720 || bfd_arch
== bfd_arch_sh
)
1721 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1722 else if (bfd_arch
== bfd_arch_mips
1723 || bfd_arch
== bfd_arch_h8300
)
1724 SELF_CHECK (status
== REG_UNKNOWN
);
1726 SELF_CHECK (status
== REG_VALID
);
1730 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1731 SELF_CHECK (mock_target
.store_registers_called
== 0);
1732 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1734 mock_target
.reset ();
1738 /* Test regcache::cooked_write by writing some expected contents to
1739 registers, and checking that contents read from registers and the
1740 expected contents are the same. */
1743 cooked_write_test (struct gdbarch
*gdbarch
)
1745 /* Error out if debugging something, because we're going to push the
1746 test target, which would pop any existing target. */
1747 if (current_top_target ()->stratum () >= process_stratum
)
1748 error (_("target already pushed"));
1750 /* Create a mock environment. A process_stratum target pushed. */
1752 target_ops_no_register mock_target
;
1754 /* Push the process_stratum target so we can mock accessing
1756 push_target (&mock_target
);
1758 /* Pop it again on exit (return/exception). */
1763 pop_all_targets_at_and_above (process_stratum
);
1767 readwrite_regcache
readwrite (gdbarch
);
1769 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1771 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1773 if (register_size (gdbarch
, regnum
) == 0
1774 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1777 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1779 if (bfd_arch
== bfd_arch_sparc
1780 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1781 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1782 && gdbarch_ptr_bit (gdbarch
) == 64
1783 && (regnum
>= gdbarch_num_regs (gdbarch
)
1784 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1787 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1788 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1789 const auto type
= register_type (gdbarch
, regnum
);
1791 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1792 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1794 /* Generate valid float format. */
1795 target_float_from_string (expected
.data (), type
, "1.25");
1797 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1798 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1799 || TYPE_CODE (type
) == TYPE_CODE_PTR
1800 || TYPE_CODE (type
) == TYPE_CODE_UNION
1801 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1803 if (bfd_arch
== bfd_arch_ia64
1804 || (regnum
>= gdbarch_num_regs (gdbarch
)
1805 && (bfd_arch
== bfd_arch_xtensa
1806 || bfd_arch
== bfd_arch_bfin
1807 || bfd_arch
== bfd_arch_m32c
1808 /* m68hc11 pseudo registers are in memory. */
1809 || bfd_arch
== bfd_arch_m68hc11
1810 || bfd_arch
== bfd_arch_m68hc12
1811 || bfd_arch
== bfd_arch_s390
))
1812 || (bfd_arch
== bfd_arch_frv
1813 /* FRV pseudo registers except iacc0. */
1814 && regnum
> gdbarch_num_regs (gdbarch
)))
1816 /* Skip setting the expected values for some architecture
1819 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1821 /* RL78_PC_REGNUM */
1822 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1827 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1831 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1833 /* No idea how to test flags. */
1838 /* If we don't know how to create the expected value for the
1839 this type, make it fail. */
1843 readwrite
.cooked_write (regnum
, expected
.data ());
1845 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1846 SELF_CHECK (expected
== buf
);
1850 } // namespace selftests
1851 #endif /* GDB_SELF_TEST */
1854 _initialize_regcache (void)
1856 regcache_descr_handle
1857 = gdbarch_data_register_post_init (init_regcache_descr
);
1859 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1860 gdb::observers::thread_ptid_changed
.attach (regcache_thread_ptid_changed
);
1862 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1863 _("Force gdb to flush its register cache (maintainer command)."));
1866 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1868 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1869 selftests::cooked_read_test
);
1870 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1871 selftests::cooked_write_test
);