1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2020 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"
25 #include "scoped-mock-context.h"
29 #include "reggroups.h"
30 #include "observable.h"
32 #include <forward_list>
37 * Here is the actual register cache.
40 /* Per-architecture object describing the layout of a register cache.
41 Computed once when the architecture is created. */
43 struct gdbarch_data
*regcache_descr_handle
;
47 /* The architecture this descriptor belongs to. */
48 struct gdbarch
*gdbarch
;
50 /* The raw register cache. Each raw (or hard) register is supplied
51 by the target interface. The raw cache should not contain
52 redundant information - if the PC is constructed from two
53 registers then those registers and not the PC lives in the raw
55 long sizeof_raw_registers
;
57 /* The cooked register space. Each cooked register in the range
58 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
59 register. The remaining [NR_RAW_REGISTERS
60 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
61 both raw registers and memory by the architecture methods
62 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
63 int nr_cooked_registers
;
64 long sizeof_cooked_registers
;
66 /* Offset and size (in 8 bit bytes), of each register in the
67 register cache. All registers (including those in the range
68 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
70 long *register_offset
;
71 long *sizeof_register
;
73 /* Cached table containing the type of each register. */
74 struct type
**register_type
;
78 init_regcache_descr (struct gdbarch
*gdbarch
)
81 struct regcache_descr
*descr
;
82 gdb_assert (gdbarch
!= NULL
);
84 /* Create an initial, zero filled, table. */
85 descr
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct regcache_descr
);
86 descr
->gdbarch
= gdbarch
;
88 /* Total size of the register space. The raw registers are mapped
89 directly onto the raw register cache while the pseudo's are
90 either mapped onto raw-registers or memory. */
91 descr
->nr_cooked_registers
= gdbarch_num_cooked_regs (gdbarch
);
93 /* Fill in a table of register types. */
95 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
,
97 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
98 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
100 /* Construct a strictly RAW register cache. Don't allow pseudo's
101 into the register cache. */
103 /* Lay out the register cache.
105 NOTE: cagney/2002-05-22: Only register_type () is used when
106 constructing the register cache. It is assumed that the
107 register's raw size, virtual size and type length are all the
113 descr
->sizeof_register
114 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
115 descr
->register_offset
116 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
117 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
119 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
120 descr
->register_offset
[i
] = offset
;
121 offset
+= descr
->sizeof_register
[i
];
123 /* Set the real size of the raw register cache buffer. */
124 descr
->sizeof_raw_registers
= offset
;
126 for (; i
< descr
->nr_cooked_registers
; i
++)
128 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
129 descr
->register_offset
[i
] = offset
;
130 offset
+= descr
->sizeof_register
[i
];
132 /* Set the real size of the readonly register cache buffer. */
133 descr
->sizeof_cooked_registers
= offset
;
139 static struct regcache_descr
*
140 regcache_descr (struct gdbarch
*gdbarch
)
142 return (struct regcache_descr
*) gdbarch_data (gdbarch
,
143 regcache_descr_handle
);
146 /* Utility functions returning useful register attributes stored in
147 the regcache descr. */
150 register_type (struct gdbarch
*gdbarch
, int regnum
)
152 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
154 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
155 return descr
->register_type
[regnum
];
158 /* Utility functions returning useful register attributes stored in
159 the regcache descr. */
162 register_size (struct gdbarch
*gdbarch
, int regnum
)
164 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
167 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_cooked_regs (gdbarch
));
168 size
= descr
->sizeof_register
[regnum
];
172 /* See gdbsupport/common-regcache.h. */
175 regcache_register_size (const struct regcache
*regcache
, int n
)
177 return register_size (regcache
->arch (), n
);
180 reg_buffer::reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
)
181 : m_has_pseudo (has_pseudo
)
183 gdb_assert (gdbarch
!= NULL
);
184 m_descr
= regcache_descr (gdbarch
);
188 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_cooked_registers
] ());
189 m_register_status
.reset
190 (new register_status
[m_descr
->nr_cooked_registers
] ());
194 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_raw_registers
] ());
195 m_register_status
.reset
196 (new register_status
[gdbarch_num_regs (gdbarch
)] ());
200 regcache::regcache (process_stratum_target
*target
, gdbarch
*gdbarch
,
201 const address_space
*aspace_
)
202 /* The register buffers. A read/write register cache can only hold
203 [0 .. gdbarch_num_regs). */
204 : detached_regcache (gdbarch
, false), m_aspace (aspace_
), m_target (target
)
206 m_ptid
= minus_one_ptid
;
209 readonly_detached_regcache::readonly_detached_regcache (regcache
&src
)
210 : readonly_detached_regcache (src
.arch (),
211 [&src
] (int regnum
, gdb_byte
*buf
)
213 return src
.cooked_read (regnum
, buf
);
219 reg_buffer::arch () const
221 return m_descr
->gdbarch
;
224 /* Return a pointer to register REGNUM's buffer cache. */
227 reg_buffer::register_buffer (int regnum
) const
229 return m_registers
.get () + m_descr
->register_offset
[regnum
];
233 reg_buffer::save (register_read_ftype cooked_read
)
235 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
238 /* It should have pseudo registers. */
239 gdb_assert (m_has_pseudo
);
240 /* Clear the dest. */
241 memset (m_registers
.get (), 0, m_descr
->sizeof_cooked_registers
);
242 memset (m_register_status
.get (), REG_UNKNOWN
, m_descr
->nr_cooked_registers
);
243 /* Copy over any registers (identified by their membership in the
244 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
245 gdbarch_num_pseudo_regs) range is checked since some architectures need
246 to save/restore `cooked' registers that live in memory. */
247 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
249 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
251 gdb_byte
*dst_buf
= register_buffer (regnum
);
252 enum register_status status
= cooked_read (regnum
, dst_buf
);
254 gdb_assert (status
!= REG_UNKNOWN
);
256 if (status
!= REG_VALID
)
257 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
259 m_register_status
[regnum
] = status
;
265 regcache::restore (readonly_detached_regcache
*src
)
267 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
270 gdb_assert (src
!= NULL
);
271 gdb_assert (src
->m_has_pseudo
);
273 gdb_assert (gdbarch
== src
->arch ());
275 /* Copy over any registers, being careful to only restore those that
276 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
277 + gdbarch_num_pseudo_regs) range is checked since some architectures need
278 to save/restore `cooked' registers that live in memory. */
279 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
281 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
283 if (src
->m_register_status
[regnum
] == REG_VALID
)
284 cooked_write (regnum
, src
->register_buffer (regnum
));
289 /* See gdbsupport/common-regcache.h. */
292 reg_buffer::get_register_status (int regnum
) const
294 assert_regnum (regnum
);
296 return m_register_status
[regnum
];
300 reg_buffer::invalidate (int regnum
)
302 assert_regnum (regnum
);
303 m_register_status
[regnum
] = REG_UNKNOWN
;
307 reg_buffer::assert_regnum (int regnum
) const
309 gdb_assert (regnum
>= 0);
311 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
313 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
316 /* Global structure containing the current regcache. */
318 /* NOTE: this is a write-through cache. There is no "dirty" bit for
319 recording if the register values have been changed (eg. by the
320 user). Therefore all registers must be written back to the
321 target when appropriate. */
323 /* Key for the hash map keeping the regcaches. */
325 struct target_ptid_arch
327 target_ptid_arch (process_stratum_target
*target
, ptid_t ptid
, gdbarch
*arch
)
328 : target (target
), ptid (ptid
), arch (arch
)
331 process_stratum_target
*target
;
335 bool operator== (const target_ptid_arch
&other
) const
337 return (this->target
== other
.target
338 && this->ptid
== other
.ptid
339 && this->arch
== other
.arch
);
343 /* Hash function for target_ptid_arch. */
345 struct hash_target_ptid_arch
347 size_t operator() (const target_ptid_arch
&val
) const
350 std::hash
<long> h_long
;
351 return h_ptid (val
.ptid
) + h_long ((long) val
.arch
);
355 using target_ptid_arch_regcache_map
356 = std::unordered_map
<target_ptid_arch
, regcache
*, hash_target_ptid_arch
>;
358 /* Hash map containing the regcaches. */
360 static target_ptid_arch_regcache_map the_regcaches
;
363 get_thread_arch_aspace_regcache (process_stratum_target
*target
,
364 ptid_t ptid
, gdbarch
*arch
,
365 struct address_space
*aspace
)
367 gdb_assert (target
!= nullptr);
369 /* Look up a regcache for this (target, ptid, arch). */
370 target_ptid_arch
key (target
, ptid
, arch
);
371 auto it
= the_regcaches
.find (key
);
372 if (it
!= the_regcaches
.end ())
375 /* It does not exist, create it. */
376 regcache
*new_regcache
= new regcache (target
, arch
, aspace
);
377 new_regcache
->set_ptid (ptid
);
379 the_regcaches
[key
] = new_regcache
;
385 get_thread_arch_regcache (process_stratum_target
*target
, ptid_t ptid
,
386 struct gdbarch
*gdbarch
)
388 scoped_restore_current_inferior restore_current_inferior
;
389 set_current_inferior (find_inferior_ptid (target
, ptid
));
390 address_space
*aspace
= target_thread_address_space (ptid
);
392 return get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
395 static process_stratum_target
*current_thread_target
;
396 static ptid_t current_thread_ptid
;
397 static struct gdbarch
*current_thread_arch
;
400 get_thread_regcache (process_stratum_target
*target
, ptid_t ptid
)
402 if (!current_thread_arch
403 || target
!= current_thread_target
404 || current_thread_ptid
!= ptid
)
406 gdb_assert (ptid
!= null_ptid
);
408 current_thread_ptid
= ptid
;
409 current_thread_target
= target
;
411 scoped_restore_current_inferior restore_current_inferior
;
412 set_current_inferior (find_inferior_ptid (target
, ptid
));
413 current_thread_arch
= target_thread_architecture (ptid
);
416 return get_thread_arch_regcache (target
, ptid
, current_thread_arch
);
419 /* See regcache.h. */
422 get_thread_regcache (thread_info
*thread
)
424 return get_thread_regcache (thread
->inf
->process_target (),
429 get_current_regcache (void)
431 return get_thread_regcache (inferior_thread ());
434 /* See gdbsupport/common-regcache.h. */
437 get_thread_regcache_for_ptid (ptid_t ptid
)
439 /* This function doesn't take a process_stratum_target parameter
440 because it's a gdbsupport/ routine implemented by both gdb and
441 gdbserver. It always refers to a ptid of the current target. */
442 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
443 return get_thread_regcache (proc_target
, ptid
);
446 /* Observer for the target_changed event. */
449 regcache_observer_target_changed (struct target_ops
*target
)
451 registers_changed ();
454 /* Update global variables old ptids to hold NEW_PTID if they were
457 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
459 std::vector
<target_ptid_arch
> keys_to_update
;
461 /* Find all the regcaches to updates. */
462 for (auto &pair
: the_regcaches
)
464 regcache
*rc
= pair
.second
;
465 if (rc
->ptid () == old_ptid
)
466 keys_to_update
.push_back (pair
.first
);
469 for (const target_ptid_arch
&old_key
: keys_to_update
)
471 /* Get the regcache, delete the hash map entry. */
472 auto it
= the_regcaches
.find (old_key
);
473 gdb_assert (it
!= the_regcaches
.end ());
474 regcache
*rc
= it
->second
;
476 the_regcaches
.erase (it
);
478 /* Insert the regcache back, with an updated key. */
479 target_ptid_arch
new_key (rc
->target (), new_ptid
, rc
->arch ());
480 rc
->set_ptid (new_ptid
);
481 the_regcaches
[new_key
] = rc
;
485 /* Low level examining and depositing of registers.
487 The caller is responsible for making sure that the inferior is
488 stopped before calling the fetching routines, or it will get
489 garbage. (a change from GDB version 3, in which the caller got the
490 value from the last stop). */
492 /* REGISTERS_CHANGED ()
494 Indicate that registers may have changed, so invalidate the cache. */
497 registers_changed_ptid (process_stratum_target
*target
, ptid_t ptid
)
499 /* If we have a non-minus_one_ptid, we must have a non-NULL target. */
500 if (ptid
!= minus_one_ptid
)
501 gdb_assert (target
!= nullptr);
503 for (auto iter
= the_regcaches
.begin (); iter
!= the_regcaches
.end (); )
505 regcache
*rc
= iter
->second
;
507 if ((target
== nullptr || rc
->target () == target
)
508 && rc
->ptid ().matches (ptid
))
511 iter
= the_regcaches
.erase (iter
);
517 if ((target
== nullptr || current_thread_target
== target
)
518 && current_thread_ptid
.matches (ptid
))
520 current_thread_target
= NULL
;
521 current_thread_ptid
= null_ptid
;
522 current_thread_arch
= NULL
;
525 if ((target
== nullptr || current_inferior ()->process_target () == target
)
526 && inferior_ptid
.matches (ptid
))
528 /* We just deleted the regcache of the current thread. Need to
529 forget about any frames we have cached, too. */
530 reinit_frame_cache ();
534 /* See regcache.h. */
537 registers_changed_thread (thread_info
*thread
)
539 registers_changed_ptid (thread
->inf
->process_target (), thread
->ptid
);
543 registers_changed (void)
545 registers_changed_ptid (nullptr, minus_one_ptid
);
549 regcache::raw_update (int regnum
)
551 assert_regnum (regnum
);
553 /* Make certain that the register cache is up-to-date with respect
554 to the current thread. This switching shouldn't be necessary
555 only there is still only one target side register cache. Sigh!
556 On the bright side, at least there is a regcache object. */
558 if (get_register_status (regnum
) == REG_UNKNOWN
)
560 target_fetch_registers (this, regnum
);
562 /* A number of targets can't access the whole set of raw
563 registers (because the debug API provides no means to get at
565 if (m_register_status
[regnum
] == REG_UNKNOWN
)
566 m_register_status
[regnum
] = REG_UNAVAILABLE
;
571 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
573 gdb_assert (buf
!= NULL
);
576 if (m_register_status
[regnum
] != REG_VALID
)
577 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
579 memcpy (buf
, register_buffer (regnum
),
580 m_descr
->sizeof_register
[regnum
]);
582 return m_register_status
[regnum
];
586 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
588 gdb_assert (regcache
!= NULL
);
589 return regcache
->raw_read (regnum
, val
);
592 template<typename T
, typename
>
594 readable_regcache::raw_read (int regnum
, T
*val
)
597 enum register_status status
;
599 assert_regnum (regnum
);
600 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
601 status
= raw_read (regnum
, buf
);
602 if (status
== REG_VALID
)
603 *val
= extract_integer
<T
> (buf
,
604 m_descr
->sizeof_register
[regnum
],
605 gdbarch_byte_order (m_descr
->gdbarch
));
612 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
615 gdb_assert (regcache
!= NULL
);
616 return regcache
->raw_read (regnum
, val
);
620 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
622 gdb_assert (regcache
!= NULL
);
623 regcache
->raw_write (regnum
, val
);
626 template<typename T
, typename
>
628 regcache::raw_write (int regnum
, T val
)
632 assert_regnum (regnum
);
633 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
634 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
635 gdbarch_byte_order (m_descr
->gdbarch
), val
);
636 raw_write (regnum
, buf
);
640 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
643 gdb_assert (regcache
!= NULL
);
644 regcache
->raw_write (regnum
, val
);
648 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
651 enum register_status status
;
653 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
654 if (status
== REG_UNAVAILABLE
)
655 throw_error (NOT_AVAILABLE_ERROR
,
656 _("Register %d is not available"), regnum
);
661 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
663 gdb_assert (regnum
>= 0);
664 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
665 if (regnum
< num_raw_registers ())
666 return raw_read (regnum
, buf
);
667 else if (m_has_pseudo
668 && m_register_status
[regnum
] != REG_UNKNOWN
)
670 if (m_register_status
[regnum
] == REG_VALID
)
671 memcpy (buf
, register_buffer (regnum
),
672 m_descr
->sizeof_register
[regnum
]);
674 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
676 return m_register_status
[regnum
];
678 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
680 struct value
*mark
, *computed
;
681 enum register_status result
= REG_VALID
;
683 mark
= value_mark ();
685 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
687 if (value_entirely_available (computed
))
688 memcpy (buf
, value_contents_raw (computed
),
689 m_descr
->sizeof_register
[regnum
]);
692 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
693 result
= REG_UNAVAILABLE
;
696 value_free_to_mark (mark
);
701 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
706 readable_regcache::cooked_read_value (int regnum
)
708 gdb_assert (regnum
>= 0);
709 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
711 if (regnum
< num_raw_registers ()
712 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
713 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
715 struct value
*result
;
717 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
718 VALUE_LVAL (result
) = lval_register
;
719 VALUE_REGNUM (result
) = regnum
;
721 /* It is more efficient in general to do this delegation in this
722 direction than in the other one, even though the value-based
724 if (cooked_read (regnum
,
725 value_contents_raw (result
)) == REG_UNAVAILABLE
)
726 mark_value_bytes_unavailable (result
, 0,
727 TYPE_LENGTH (value_type (result
)));
732 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
737 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
740 gdb_assert (regcache
!= NULL
);
741 return regcache
->cooked_read (regnum
, val
);
744 template<typename T
, typename
>
746 readable_regcache::cooked_read (int regnum
, T
*val
)
748 enum register_status status
;
751 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
752 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
753 status
= cooked_read (regnum
, buf
);
754 if (status
== REG_VALID
)
755 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
756 gdbarch_byte_order (m_descr
->gdbarch
));
763 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
766 gdb_assert (regcache
!= NULL
);
767 return regcache
->cooked_read (regnum
, val
);
771 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
774 gdb_assert (regcache
!= NULL
);
775 regcache
->cooked_write (regnum
, val
);
778 template<typename T
, typename
>
780 regcache::cooked_write (int regnum
, T val
)
784 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
785 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
786 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
787 gdbarch_byte_order (m_descr
->gdbarch
), val
);
788 cooked_write (regnum
, buf
);
792 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
795 gdb_assert (regcache
!= NULL
);
796 regcache
->cooked_write (regnum
, val
);
800 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
803 gdb_assert (buf
!= NULL
);
804 assert_regnum (regnum
);
806 /* On the sparc, writing %g0 is a no-op, so we don't even want to
807 change the registers array if something writes to this register. */
808 if (gdbarch_cannot_store_register (arch (), regnum
))
811 /* If we have a valid copy of the register, and new value == old
812 value, then don't bother doing the actual store. */
813 if (get_register_status (regnum
) == REG_VALID
814 && (memcmp (register_buffer (regnum
), buf
,
815 m_descr
->sizeof_register
[regnum
]) == 0))
818 target_prepare_to_store (this);
819 raw_supply (regnum
, buf
);
821 /* Invalidate the register after it is written, in case of a
824 = make_scope_exit ([&] { this->invalidate (regnum
); });
826 target_store_registers (this, regnum
);
828 /* The target did not throw an error so we can discard invalidating
830 invalidator
.release ();
834 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
836 gdb_assert (regnum
>= 0);
837 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
838 if (regnum
< num_raw_registers ())
839 raw_write (regnum
, buf
);
841 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
845 /* See regcache.h. */
848 readable_regcache::read_part (int regnum
, int offset
, int len
,
849 gdb_byte
*out
, bool is_raw
)
851 int reg_size
= register_size (arch (), regnum
);
853 gdb_assert (out
!= NULL
);
854 gdb_assert (offset
>= 0 && offset
<= reg_size
);
855 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
857 if (offset
== 0 && len
== 0)
863 if (offset
== 0 && len
== reg_size
)
865 /* Read the full register. */
866 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
869 enum register_status status
;
870 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
872 /* Read full register to buffer. */
873 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
874 if (status
!= REG_VALID
)
878 memcpy (out
, reg
+ offset
, len
);
882 /* See regcache.h. */
885 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
888 int reg_size
= register_size (arch (), regnum
);
890 gdb_assert (out
!= nullptr);
891 gdb_assert (offset
>= 0 && offset
<= reg_size
);
892 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
894 if (offset
== 0 && len
== 0)
900 if (offset
== 0 && len
== reg_size
)
902 /* Collect the full register. */
903 return raw_collect (regnum
, out
);
906 /* Read to buffer, then write out. */
907 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
908 raw_collect (regnum
, reg
);
909 memcpy (out
, reg
+ offset
, len
);
912 /* See regcache.h. */
915 regcache::write_part (int regnum
, int offset
, int len
,
916 const gdb_byte
*in
, bool is_raw
)
918 int reg_size
= register_size (arch (), regnum
);
920 gdb_assert (in
!= NULL
);
921 gdb_assert (offset
>= 0 && offset
<= reg_size
);
922 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
924 if (offset
== 0 && len
== 0)
930 if (offset
== 0 && len
== reg_size
)
932 /* Write the full register. */
933 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
937 enum register_status status
;
938 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
940 /* Read existing register to buffer. */
941 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
942 if (status
!= REG_VALID
)
945 /* Update buffer, then write back to regcache. */
946 memcpy (reg
+ offset
, in
, len
);
947 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
951 /* See regcache.h. */
954 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
957 int reg_size
= register_size (arch (), regnum
);
959 gdb_assert (in
!= nullptr);
960 gdb_assert (offset
>= 0 && offset
<= reg_size
);
961 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
963 if (offset
== 0 && len
== 0)
969 if (offset
== 0 && len
== reg_size
)
971 /* Supply the full register. */
972 return raw_supply (regnum
, in
);
975 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
977 /* Read existing value to buffer. */
978 raw_collect (regnum
, reg
);
980 /* Write to buffer, then write out. */
981 memcpy (reg
+ offset
, in
, len
);
982 raw_supply (regnum
, reg
);
986 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
989 assert_regnum (regnum
);
990 return read_part (regnum
, offset
, len
, buf
, true);
993 /* See regcache.h. */
996 regcache::raw_write_part (int regnum
, int offset
, int len
,
999 assert_regnum (regnum
);
1000 write_part (regnum
, offset
, len
, buf
, true);
1003 /* See regcache.h. */
1005 enum register_status
1006 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
1009 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1010 return read_part (regnum
, offset
, len
, buf
, false);
1013 /* See regcache.h. */
1016 regcache::cooked_write_part (int regnum
, int offset
, int len
,
1017 const gdb_byte
*buf
)
1019 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1020 write_part (regnum
, offset
, len
, buf
, false);
1023 /* See gdbsupport/common-regcache.h. */
1026 reg_buffer::raw_supply (int regnum
, const void *buf
)
1031 assert_regnum (regnum
);
1033 regbuf
= register_buffer (regnum
);
1034 size
= m_descr
->sizeof_register
[regnum
];
1038 memcpy (regbuf
, buf
, size
);
1039 m_register_status
[regnum
] = REG_VALID
;
1043 /* This memset not strictly necessary, but better than garbage
1044 in case the register value manages to escape somewhere (due
1045 to a bug, no less). */
1046 memset (regbuf
, 0, size
);
1047 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1051 /* See regcache.h. */
1054 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1055 int addr_len
, bool is_signed
)
1057 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1061 assert_regnum (regnum
);
1063 regbuf
= register_buffer (regnum
);
1064 regsize
= m_descr
->sizeof_register
[regnum
];
1066 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1068 m_register_status
[regnum
] = REG_VALID
;
1071 /* See regcache.h. */
1074 reg_buffer::raw_supply_zeroed (int regnum
)
1079 assert_regnum (regnum
);
1081 regbuf
= register_buffer (regnum
);
1082 size
= m_descr
->sizeof_register
[regnum
];
1084 memset (regbuf
, 0, size
);
1085 m_register_status
[regnum
] = REG_VALID
;
1088 /* See gdbsupport/common-regcache.h. */
1091 reg_buffer::raw_collect (int regnum
, void *buf
) const
1096 gdb_assert (buf
!= NULL
);
1097 assert_regnum (regnum
);
1099 regbuf
= register_buffer (regnum
);
1100 size
= m_descr
->sizeof_register
[regnum
];
1101 memcpy (buf
, regbuf
, size
);
1104 /* See regcache.h. */
1107 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1108 bool is_signed
) const
1110 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1111 const gdb_byte
*regbuf
;
1114 assert_regnum (regnum
);
1116 regbuf
= register_buffer (regnum
);
1117 regsize
= m_descr
->sizeof_register
[regnum
];
1119 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1123 /* See regcache.h. */
1126 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1127 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1128 int slot_size
, int offs
) const
1130 struct gdbarch
*gdbarch
= arch ();
1131 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1133 /* Use part versions and reg_size to prevent possible buffer overflows when
1134 accessing the regcache. */
1136 if (out_buf
!= nullptr)
1138 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1140 /* Ensure any additional space is cleared. */
1141 if (slot_size
> reg_size
)
1142 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1144 else if (in_buf
!= nullptr)
1145 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1148 /* Invalidate the register. */
1149 out_regcache
->raw_supply (regnum
, nullptr);
1153 /* See regcache.h. */
1156 regcache::transfer_regset (const struct regset
*regset
,
1157 struct regcache
*out_regcache
,
1158 int regnum
, const gdb_byte
*in_buf
,
1159 gdb_byte
*out_buf
, size_t size
) const
1161 const struct regcache_map_entry
*map
;
1162 int offs
= 0, count
;
1164 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1165 (count
= map
->count
) != 0;
1168 int regno
= map
->regno
;
1169 int slot_size
= map
->size
;
1171 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1172 slot_size
= m_descr
->sizeof_register
[regno
];
1174 if (regno
== REGCACHE_MAP_SKIP
1176 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1177 offs
+= count
* slot_size
;
1179 else if (regnum
== -1)
1180 for (; count
--; regno
++, offs
+= slot_size
)
1182 if (offs
+ slot_size
> size
)
1185 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1190 /* Transfer a single register and return. */
1191 offs
+= (regnum
- regno
) * slot_size
;
1192 if (offs
+ slot_size
> size
)
1195 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1202 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1203 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1204 If BUF is NULL, set the register(s) to "unavailable" status. */
1207 regcache_supply_regset (const struct regset
*regset
,
1208 struct regcache
*regcache
,
1209 int regnum
, const void *buf
, size_t size
)
1211 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1215 regcache::supply_regset (const struct regset
*regset
,
1216 int regnum
, const void *buf
, size_t size
)
1218 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1221 /* Collect register REGNUM from REGCACHE to BUF, using the register
1222 map in REGSET. If REGNUM is -1, do this for all registers in
1226 regcache_collect_regset (const struct regset
*regset
,
1227 const struct regcache
*regcache
,
1228 int regnum
, void *buf
, size_t size
)
1230 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1234 regcache::collect_regset (const struct regset
*regset
,
1235 int regnum
, void *buf
, size_t size
) const
1237 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1240 /* See gdbsupport/common-regcache.h. */
1243 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1245 gdb_assert (buf
!= NULL
);
1246 assert_regnum (regnum
);
1248 const char *regbuf
= (const char *) register_buffer (regnum
);
1249 size_t size
= m_descr
->sizeof_register
[regnum
];
1250 gdb_assert (size
>= offset
);
1252 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1255 /* Special handling for register PC. */
1258 regcache_read_pc (struct regcache
*regcache
)
1260 struct gdbarch
*gdbarch
= regcache
->arch ();
1264 if (gdbarch_read_pc_p (gdbarch
))
1265 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1266 /* Else use per-frame method on get_current_frame. */
1267 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1271 if (regcache_cooked_read_unsigned (regcache
,
1272 gdbarch_pc_regnum (gdbarch
),
1273 &raw_val
) == REG_UNAVAILABLE
)
1274 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1276 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1279 internal_error (__FILE__
, __LINE__
,
1280 _("regcache_read_pc: Unable to find PC"));
1284 /* See gdbsupport/common-regcache.h. */
1287 regcache_read_pc_protected (regcache
*regcache
)
1292 pc
= regcache_read_pc (regcache
);
1294 catch (const gdb_exception_error
&ex
)
1303 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1305 struct gdbarch
*gdbarch
= regcache
->arch ();
1307 if (gdbarch_write_pc_p (gdbarch
))
1308 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1309 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1310 regcache_cooked_write_unsigned (regcache
,
1311 gdbarch_pc_regnum (gdbarch
), pc
);
1313 internal_error (__FILE__
, __LINE__
,
1314 _("regcache_write_pc: Unable to update PC"));
1316 /* Writing the PC (for instance, from "load") invalidates the
1318 reinit_frame_cache ();
1322 reg_buffer::num_raw_registers () const
1324 return gdbarch_num_regs (arch ());
1328 regcache::debug_print_register (const char *func
, int regno
)
1330 struct gdbarch
*gdbarch
= arch ();
1332 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1333 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1334 && gdbarch_register_name (gdbarch
, regno
) != NULL
1335 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1336 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1337 gdbarch_register_name (gdbarch
, regno
));
1339 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1340 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1342 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1343 int size
= register_size (gdbarch
, regno
);
1344 gdb_byte
*buf
= register_buffer (regno
);
1346 fprintf_unfiltered (gdb_stdlog
, " = ");
1347 for (int i
= 0; i
< size
; i
++)
1349 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1351 if (size
<= sizeof (LONGEST
))
1353 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1355 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1356 core_addr_to_string_nz (val
), plongest (val
));
1359 fprintf_unfiltered (gdb_stdlog
, "\n");
1363 reg_flush_command (const char *command
, int from_tty
)
1365 /* Force-flush the register cache. */
1366 registers_changed ();
1368 printf_filtered (_("Register cache flushed.\n"));
1372 register_dump::dump (ui_file
*file
)
1374 auto descr
= regcache_descr (m_gdbarch
);
1376 int footnote_nr
= 0;
1377 int footnote_register_offset
= 0;
1378 int footnote_register_type_name_null
= 0;
1379 long register_offset
= 0;
1381 gdb_assert (descr
->nr_cooked_registers
1382 == gdbarch_num_cooked_regs (m_gdbarch
));
1384 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1388 fprintf_unfiltered (file
, " %-10s", "Name");
1391 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1395 else if (p
[0] == '\0')
1397 fprintf_unfiltered (file
, " %-10s", p
);
1402 fprintf_unfiltered (file
, " %4s", "Nr");
1404 fprintf_unfiltered (file
, " %4d", regnum
);
1406 /* Relative number. */
1408 fprintf_unfiltered (file
, " %4s", "Rel");
1409 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1410 fprintf_unfiltered (file
, " %4d", regnum
);
1412 fprintf_unfiltered (file
, " %4d",
1413 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1417 fprintf_unfiltered (file
, " %6s ", "Offset");
1420 fprintf_unfiltered (file
, " %6ld",
1421 descr
->register_offset
[regnum
]);
1422 if (register_offset
!= descr
->register_offset
[regnum
]
1424 && (descr
->register_offset
[regnum
]
1425 != (descr
->register_offset
[regnum
- 1]
1426 + descr
->sizeof_register
[regnum
- 1])))
1429 if (!footnote_register_offset
)
1430 footnote_register_offset
= ++footnote_nr
;
1431 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1434 fprintf_unfiltered (file
, " ");
1435 register_offset
= (descr
->register_offset
[regnum
]
1436 + descr
->sizeof_register
[regnum
]);
1441 fprintf_unfiltered (file
, " %5s ", "Size");
1443 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1448 std::string name_holder
;
1454 static const char blt
[] = "builtin_type";
1456 t
= register_type (m_gdbarch
, regnum
)->name ();
1459 if (!footnote_register_type_name_null
)
1460 footnote_register_type_name_null
= ++footnote_nr
;
1461 name_holder
= string_printf ("*%d",
1462 footnote_register_type_name_null
);
1463 t
= name_holder
.c_str ();
1465 /* Chop a leading builtin_type. */
1466 if (startswith (t
, blt
))
1469 fprintf_unfiltered (file
, " %-15s", t
);
1472 /* Leading space always present. */
1473 fprintf_unfiltered (file
, " ");
1475 dump_reg (file
, regnum
);
1477 fprintf_unfiltered (file
, "\n");
1480 if (footnote_register_offset
)
1481 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1482 footnote_register_offset
);
1483 if (footnote_register_type_name_null
)
1484 fprintf_unfiltered (file
,
1485 "*%d: Register type's name NULL.\n",
1486 footnote_register_type_name_null
);
1490 #include "gdbsupport/selftest.h"
1491 #include "selftest-arch.h"
1492 #include "target-float.h"
1494 namespace selftests
{
1496 class regcache_access
: public regcache
1500 /* Return the number of elements in current_regcache. */
1503 current_regcache_size ()
1505 return the_regcaches
.size ();
1509 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1512 test_get_thread_arch_aspace_regcache (process_stratum_target
*target
,
1513 ptid_t ptid
, struct gdbarch
*gdbarch
,
1514 address_space
*aspace
)
1516 struct regcache
*regcache
1517 = get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
1518 SELF_CHECK (regcache
!= NULL
);
1519 SELF_CHECK (regcache
->target () == target
);
1520 SELF_CHECK (regcache
->ptid () == ptid
);
1521 SELF_CHECK (regcache
->aspace () == aspace
);
1525 current_regcache_test (void)
1527 /* It is empty at the start. */
1528 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1530 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1532 test_target_ops test_target1
;
1533 test_target_ops test_target2
;
1535 /* Get regcache from (target1,ptid1), a new regcache is added to
1536 current_regcache. */
1537 test_get_thread_arch_aspace_regcache (&test_target1
, ptid1
,
1540 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1542 /* Get regcache from (target1,ptid2), a new regcache is added to
1543 current_regcache. */
1544 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1547 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1549 /* Get regcache from (target1,ptid3), a new regcache is added to
1550 current_regcache. */
1551 test_get_thread_arch_aspace_regcache (&test_target1
, ptid3
,
1554 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1556 /* Get regcache from (target1,ptid2) again, nothing is added to
1557 current_regcache. */
1558 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1561 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1563 /* Get regcache from (target2,ptid2), a new regcache is added to
1564 current_regcache, since this time we're using a differen
1566 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1569 SELF_CHECK (regcache_access::current_regcache_size () == 4);
1571 /* Mark that (target1,ptid2) changed. The regcache of (target1,
1572 ptid2) should be removed from current_regcache. */
1573 registers_changed_ptid (&test_target1
, ptid2
);
1574 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1576 /* Get the regcache from (target2,ptid2) again, confirming the
1577 registers_changed_ptid call above did not delete it. */
1578 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1581 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1583 /* Confirm that marking all regcaches of all targets as changed
1584 clears current_regcache. */
1585 registers_changed_ptid (nullptr, minus_one_ptid
);
1586 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1589 class target_ops_no_register
: public test_target_ops
1592 target_ops_no_register ()
1593 : test_target_ops
{}
1598 fetch_registers_called
= 0;
1599 store_registers_called
= 0;
1600 xfer_partial_called
= 0;
1603 void fetch_registers (regcache
*regs
, int regno
) override
;
1604 void store_registers (regcache
*regs
, int regno
) override
;
1606 enum target_xfer_status
xfer_partial (enum target_object object
,
1607 const char *annex
, gdb_byte
*readbuf
,
1608 const gdb_byte
*writebuf
,
1609 ULONGEST offset
, ULONGEST len
,
1610 ULONGEST
*xfered_len
) override
;
1612 unsigned int fetch_registers_called
= 0;
1613 unsigned int store_registers_called
= 0;
1614 unsigned int xfer_partial_called
= 0;
1618 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1620 /* Mark register available. */
1621 regs
->raw_supply_zeroed (regno
);
1622 this->fetch_registers_called
++;
1626 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1628 this->store_registers_called
++;
1631 enum target_xfer_status
1632 target_ops_no_register::xfer_partial (enum target_object object
,
1633 const char *annex
, gdb_byte
*readbuf
,
1634 const gdb_byte
*writebuf
,
1635 ULONGEST offset
, ULONGEST len
,
1636 ULONGEST
*xfered_len
)
1638 this->xfer_partial_called
++;
1641 return TARGET_XFER_OK
;
1644 class readwrite_regcache
: public regcache
1647 readwrite_regcache (process_stratum_target
*target
,
1648 struct gdbarch
*gdbarch
)
1649 : regcache (target
, gdbarch
, nullptr)
1653 /* Test regcache::cooked_read gets registers from raw registers and
1654 memory instead of target to_{fetch,store}_registers. */
1657 cooked_read_test (struct gdbarch
*gdbarch
)
1659 scoped_mock_context
<target_ops_no_register
> mockctx (gdbarch
);
1661 /* Test that read one raw register from regcache_no_target will go
1662 to the target layer. */
1664 /* Find a raw register which size isn't zero. */
1666 for (nonzero_regnum
= 0;
1667 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1670 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1674 readwrite_regcache
readwrite (&mockctx
.mock_target
, gdbarch
);
1675 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1677 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1679 /* raw_read calls target_fetch_registers. */
1680 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
> 0);
1681 mockctx
.mock_target
.reset ();
1683 /* Mark all raw registers valid, so the following raw registers
1684 accesses won't go to target. */
1685 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1686 readwrite
.raw_update (i
);
1688 mockctx
.mock_target
.reset ();
1689 /* Then, read all raw and pseudo registers, and don't expect calling
1690 to_{fetch,store}_registers. */
1691 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1693 if (register_size (gdbarch
, regnum
) == 0)
1696 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1698 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1699 inner_buf
.data ()));
1701 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1702 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1703 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1705 mockctx
.mock_target
.reset ();
1708 readonly_detached_regcache
readonly (readwrite
);
1710 /* GDB may go to target layer to fetch all registers and memory for
1711 readonly regcache. */
1712 mockctx
.mock_target
.reset ();
1714 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1716 if (register_size (gdbarch
, regnum
) == 0)
1719 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1720 enum register_status status
= readonly
.cooked_read (regnum
,
1723 if (regnum
< gdbarch_num_regs (gdbarch
))
1725 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1727 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1728 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1729 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1730 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1731 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1732 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1733 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1734 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1736 /* Raw registers. If raw registers are not in save_reggroup,
1737 their status are unknown. */
1738 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1739 SELF_CHECK (status
== REG_VALID
);
1741 SELF_CHECK (status
== REG_UNKNOWN
);
1744 SELF_CHECK (status
== REG_VALID
);
1748 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1749 SELF_CHECK (status
== REG_VALID
);
1752 /* If pseudo registers are not in save_reggroup, some of
1753 them can be computed from saved raw registers, but some
1754 of them are unknown. */
1755 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1757 if (bfd_arch
== bfd_arch_frv
1758 || bfd_arch
== bfd_arch_m32c
1759 || bfd_arch
== bfd_arch_mep
1760 || bfd_arch
== bfd_arch_sh
)
1761 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1762 else if (bfd_arch
== bfd_arch_mips
1763 || bfd_arch
== bfd_arch_h8300
)
1764 SELF_CHECK (status
== REG_UNKNOWN
);
1766 SELF_CHECK (status
== REG_VALID
);
1770 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1771 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1772 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1774 mockctx
.mock_target
.reset ();
1778 /* Test regcache::cooked_write by writing some expected contents to
1779 registers, and checking that contents read from registers and the
1780 expected contents are the same. */
1783 cooked_write_test (struct gdbarch
*gdbarch
)
1785 /* Error out if debugging something, because we're going to push the
1786 test target, which would pop any existing target. */
1787 if (current_top_target ()->stratum () >= process_stratum
)
1788 error (_("target already pushed"));
1790 /* Create a mock environment. A process_stratum target pushed. */
1792 target_ops_no_register mock_target
;
1794 /* Push the process_stratum target so we can mock accessing
1796 push_target (&mock_target
);
1798 /* Pop it again on exit (return/exception). */
1803 pop_all_targets_at_and_above (process_stratum
);
1807 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1809 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1811 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1813 if (register_size (gdbarch
, regnum
) == 0
1814 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1817 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1819 if (bfd_arch
== bfd_arch_sparc
1820 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1821 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1822 && gdbarch_ptr_bit (gdbarch
) == 64
1823 && (regnum
>= gdbarch_num_regs (gdbarch
)
1824 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1827 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1828 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1829 const auto type
= register_type (gdbarch
, regnum
);
1831 if (type
->code () == TYPE_CODE_FLT
1832 || type
->code () == TYPE_CODE_DECFLOAT
)
1834 /* Generate valid float format. */
1835 target_float_from_string (expected
.data (), type
, "1.25");
1837 else if (type
->code () == TYPE_CODE_INT
1838 || type
->code () == TYPE_CODE_ARRAY
1839 || type
->code () == TYPE_CODE_PTR
1840 || type
->code () == TYPE_CODE_UNION
1841 || type
->code () == TYPE_CODE_STRUCT
)
1843 if (bfd_arch
== bfd_arch_ia64
1844 || (regnum
>= gdbarch_num_regs (gdbarch
)
1845 && (bfd_arch
== bfd_arch_xtensa
1846 || bfd_arch
== bfd_arch_bfin
1847 || bfd_arch
== bfd_arch_m32c
1848 /* m68hc11 pseudo registers are in memory. */
1849 || bfd_arch
== bfd_arch_m68hc11
1850 || bfd_arch
== bfd_arch_m68hc12
1851 || bfd_arch
== bfd_arch_s390
))
1852 || (bfd_arch
== bfd_arch_frv
1853 /* FRV pseudo registers except iacc0. */
1854 && regnum
> gdbarch_num_regs (gdbarch
)))
1856 /* Skip setting the expected values for some architecture
1859 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1861 /* RL78_PC_REGNUM */
1862 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1867 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1871 else if (type
->code () == TYPE_CODE_FLAGS
)
1873 /* No idea how to test flags. */
1878 /* If we don't know how to create the expected value for the
1879 this type, make it fail. */
1883 readwrite
.cooked_write (regnum
, expected
.data ());
1885 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1886 SELF_CHECK (expected
== buf
);
1890 } // namespace selftests
1891 #endif /* GDB_SELF_TEST */
1893 void _initialize_regcache ();
1895 _initialize_regcache ()
1897 regcache_descr_handle
1898 = gdbarch_data_register_post_init (init_regcache_descr
);
1900 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1901 gdb::observers::thread_ptid_changed
.attach (regcache_thread_ptid_changed
);
1903 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1904 _("Force gdb to flush its register cache (maintainer command)."));
1907 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1909 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1910 selftests::cooked_read_test
);
1911 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1912 selftests::cooked_write_test
);