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"
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 (process_stratum_target
*target
, gdbarch
*gdbarch
,
200 const address_space
*aspace_
)
201 /* The register buffers. A read/write register cache can only hold
202 [0 .. gdbarch_num_regs). */
203 : detached_regcache (gdbarch
, false), m_aspace (aspace_
), m_target (target
)
205 m_ptid
= minus_one_ptid
;
208 readonly_detached_regcache::readonly_detached_regcache (regcache
&src
)
209 : readonly_detached_regcache (src
.arch (),
210 [&src
] (int regnum
, gdb_byte
*buf
)
212 return src
.cooked_read (regnum
, buf
);
218 reg_buffer::arch () const
220 return m_descr
->gdbarch
;
223 /* Return a pointer to register REGNUM's buffer cache. */
226 reg_buffer::register_buffer (int regnum
) const
228 return m_registers
.get () + m_descr
->register_offset
[regnum
];
232 reg_buffer::save (register_read_ftype cooked_read
)
234 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
237 /* It should have pseudo registers. */
238 gdb_assert (m_has_pseudo
);
239 /* Clear the dest. */
240 memset (m_registers
.get (), 0, m_descr
->sizeof_cooked_registers
);
241 memset (m_register_status
.get (), REG_UNKNOWN
, m_descr
->nr_cooked_registers
);
242 /* Copy over any registers (identified by their membership in the
243 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
244 gdbarch_num_pseudo_regs) range is checked since some architectures need
245 to save/restore `cooked' registers that live in memory. */
246 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
248 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
250 gdb_byte
*dst_buf
= register_buffer (regnum
);
251 enum register_status status
= cooked_read (regnum
, dst_buf
);
253 gdb_assert (status
!= REG_UNKNOWN
);
255 if (status
!= REG_VALID
)
256 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
258 m_register_status
[regnum
] = status
;
264 regcache::restore (readonly_detached_regcache
*src
)
266 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
269 gdb_assert (src
!= NULL
);
270 gdb_assert (src
->m_has_pseudo
);
272 gdb_assert (gdbarch
== src
->arch ());
274 /* Copy over any registers, being careful to only restore those that
275 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
276 + gdbarch_num_pseudo_regs) range is checked since some architectures need
277 to save/restore `cooked' registers that live in memory. */
278 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
280 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
282 if (src
->m_register_status
[regnum
] == REG_VALID
)
283 cooked_write (regnum
, src
->register_buffer (regnum
));
288 /* See gdbsupport/common-regcache.h. */
291 reg_buffer::get_register_status (int regnum
) const
293 assert_regnum (regnum
);
295 return m_register_status
[regnum
];
299 reg_buffer::invalidate (int regnum
)
301 assert_regnum (regnum
);
302 m_register_status
[regnum
] = REG_UNKNOWN
;
306 reg_buffer::assert_regnum (int regnum
) const
308 gdb_assert (regnum
>= 0);
310 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
312 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
315 /* Global structure containing the current regcache. */
317 /* NOTE: this is a write-through cache. There is no "dirty" bit for
318 recording if the register values have been changed (eg. by the
319 user). Therefore all registers must be written back to the
320 target when appropriate. */
322 /* Key for the hash map keeping the regcaches. */
324 struct target_ptid_arch
326 target_ptid_arch (process_stratum_target
*target
, ptid_t ptid
, gdbarch
*arch
)
327 : target (target
), ptid (ptid
), arch (arch
)
330 process_stratum_target
*target
;
334 bool operator== (const target_ptid_arch
&other
) const
336 return (this->target
== other
.target
337 && this->ptid
== other
.ptid
338 && this->arch
== other
.arch
);
342 /* Hash function for target_ptid_arch. */
344 struct hash_target_ptid_arch
346 size_t operator() (const target_ptid_arch
&val
) const
349 std::hash
<long> h_long
;
350 return h_ptid (val
.ptid
) + h_long ((long) val
.arch
);
354 using target_ptid_arch_regcache_map
355 = std::unordered_map
<target_ptid_arch
, regcache
*, hash_target_ptid_arch
>;
357 /* Hash map containing the regcaches. */
359 static target_ptid_arch_regcache_map the_regcaches
;
362 get_thread_arch_aspace_regcache (process_stratum_target
*target
,
363 ptid_t ptid
, gdbarch
*arch
,
364 struct address_space
*aspace
)
366 gdb_assert (target
!= nullptr);
368 /* Look up a regcache for this (target, ptid, arch). */
369 target_ptid_arch
key (target
, ptid
, arch
);
370 auto it
= the_regcaches
.find (key
);
371 if (it
!= the_regcaches
.end ())
374 /* It does not exist, create it. */
375 regcache
*new_regcache
= new regcache (target
, arch
, aspace
);
376 new_regcache
->set_ptid (ptid
);
378 the_regcaches
[key
] = new_regcache
;
384 get_thread_arch_regcache (process_stratum_target
*target
, ptid_t ptid
,
385 struct gdbarch
*gdbarch
)
387 scoped_restore_current_inferior restore_current_inferior
;
388 set_current_inferior (find_inferior_ptid (target
, ptid
));
389 address_space
*aspace
= target_thread_address_space (ptid
);
391 return get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
394 static process_stratum_target
*current_thread_target
;
395 static ptid_t current_thread_ptid
;
396 static struct gdbarch
*current_thread_arch
;
399 get_thread_regcache (process_stratum_target
*target
, ptid_t ptid
)
401 if (!current_thread_arch
402 || target
!= current_thread_target
403 || current_thread_ptid
!= ptid
)
405 gdb_assert (ptid
!= null_ptid
);
407 current_thread_ptid
= ptid
;
408 current_thread_target
= target
;
410 scoped_restore_current_inferior restore_current_inferior
;
411 set_current_inferior (find_inferior_ptid (target
, ptid
));
412 current_thread_arch
= target_thread_architecture (ptid
);
415 return get_thread_arch_regcache (target
, ptid
, current_thread_arch
);
418 /* See regcache.h. */
421 get_thread_regcache (thread_info
*thread
)
423 return get_thread_regcache (thread
->inf
->process_target (),
428 get_current_regcache (void)
430 return get_thread_regcache (inferior_thread ());
433 /* See gdbsupport/common-regcache.h. */
436 get_thread_regcache_for_ptid (ptid_t ptid
)
438 /* This function doesn't take a process_stratum_target parameter
439 because it's a gdbsupport/ routine implemented by both gdb and
440 gdbserver. It always refers to a ptid of the current target. */
441 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
442 return get_thread_regcache (proc_target
, ptid
);
445 /* Observer for the target_changed event. */
448 regcache_observer_target_changed (struct target_ops
*target
)
450 registers_changed ();
453 /* Update global variables old ptids to hold NEW_PTID if they were
456 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
458 std::vector
<target_ptid_arch
> keys_to_update
;
460 /* Find all the regcaches to updates. */
461 for (auto &pair
: the_regcaches
)
463 regcache
*rc
= pair
.second
;
464 if (rc
->ptid () == old_ptid
)
465 keys_to_update
.push_back (pair
.first
);
468 for (const target_ptid_arch
&old_key
: keys_to_update
)
470 /* Get the regcache, delete the hash map entry. */
471 auto it
= the_regcaches
.find (old_key
);
472 gdb_assert (it
!= the_regcaches
.end ());
473 regcache
*rc
= it
->second
;
475 the_regcaches
.erase (it
);
477 /* Insert the regcache back, with an updated key. */
478 target_ptid_arch
new_key (rc
->target (), new_ptid
, rc
->arch ());
479 rc
->set_ptid (new_ptid
);
480 the_regcaches
[new_key
] = rc
;
484 /* Low level examining and depositing of registers.
486 The caller is responsible for making sure that the inferior is
487 stopped before calling the fetching routines, or it will get
488 garbage. (a change from GDB version 3, in which the caller got the
489 value from the last stop). */
491 /* REGISTERS_CHANGED ()
493 Indicate that registers may have changed, so invalidate the cache. */
496 registers_changed_ptid (process_stratum_target
*target
, ptid_t ptid
)
498 /* If we have a non-minus_one_ptid, we must have a non-NULL target. */
499 if (ptid
!= minus_one_ptid
)
500 gdb_assert (target
!= nullptr);
502 for (auto iter
= the_regcaches
.begin (); iter
!= the_regcaches
.end (); )
504 regcache
*rc
= iter
->second
;
506 if ((target
== nullptr || rc
->target () == target
)
507 && rc
->ptid ().matches (ptid
))
510 iter
= the_regcaches
.erase (iter
);
516 if ((target
== nullptr || current_thread_target
== target
)
517 && current_thread_ptid
.matches (ptid
))
519 current_thread_target
= NULL
;
520 current_thread_ptid
= null_ptid
;
521 current_thread_arch
= NULL
;
524 if ((target
== nullptr || current_inferior ()->process_target () == target
)
525 && inferior_ptid
.matches (ptid
))
527 /* We just deleted the regcache of the current thread. Need to
528 forget about any frames we have cached, too. */
529 reinit_frame_cache ();
533 /* See regcache.h. */
536 registers_changed_thread (thread_info
*thread
)
538 registers_changed_ptid (thread
->inf
->process_target (), thread
->ptid
);
542 registers_changed (void)
544 registers_changed_ptid (nullptr, minus_one_ptid
);
548 regcache::raw_update (int regnum
)
550 assert_regnum (regnum
);
552 /* Make certain that the register cache is up-to-date with respect
553 to the current thread. This switching shouldn't be necessary
554 only there is still only one target side register cache. Sigh!
555 On the bright side, at least there is a regcache object. */
557 if (get_register_status (regnum
) == REG_UNKNOWN
)
559 target_fetch_registers (this, regnum
);
561 /* A number of targets can't access the whole set of raw
562 registers (because the debug API provides no means to get at
564 if (m_register_status
[regnum
] == REG_UNKNOWN
)
565 m_register_status
[regnum
] = REG_UNAVAILABLE
;
570 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
572 gdb_assert (buf
!= NULL
);
575 if (m_register_status
[regnum
] != REG_VALID
)
576 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
578 memcpy (buf
, register_buffer (regnum
),
579 m_descr
->sizeof_register
[regnum
]);
581 return m_register_status
[regnum
];
585 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
587 gdb_assert (regcache
!= NULL
);
588 return regcache
->raw_read (regnum
, val
);
591 template<typename T
, typename
>
593 readable_regcache::raw_read (int regnum
, T
*val
)
596 enum register_status status
;
598 assert_regnum (regnum
);
599 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
600 status
= raw_read (regnum
, buf
);
601 if (status
== REG_VALID
)
602 *val
= extract_integer
<T
> (buf
,
603 m_descr
->sizeof_register
[regnum
],
604 gdbarch_byte_order (m_descr
->gdbarch
));
611 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
614 gdb_assert (regcache
!= NULL
);
615 return regcache
->raw_read (regnum
, val
);
619 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
621 gdb_assert (regcache
!= NULL
);
622 regcache
->raw_write (regnum
, val
);
625 template<typename T
, typename
>
627 regcache::raw_write (int regnum
, T val
)
631 assert_regnum (regnum
);
632 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
633 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
634 gdbarch_byte_order (m_descr
->gdbarch
), val
);
635 raw_write (regnum
, buf
);
639 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
642 gdb_assert (regcache
!= NULL
);
643 regcache
->raw_write (regnum
, val
);
647 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
650 enum register_status status
;
652 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
653 if (status
== REG_UNAVAILABLE
)
654 throw_error (NOT_AVAILABLE_ERROR
,
655 _("Register %d is not available"), regnum
);
660 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
662 gdb_assert (regnum
>= 0);
663 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
664 if (regnum
< num_raw_registers ())
665 return raw_read (regnum
, buf
);
666 else if (m_has_pseudo
667 && m_register_status
[regnum
] != REG_UNKNOWN
)
669 if (m_register_status
[regnum
] == REG_VALID
)
670 memcpy (buf
, register_buffer (regnum
),
671 m_descr
->sizeof_register
[regnum
]);
673 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
675 return m_register_status
[regnum
];
677 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
679 struct value
*mark
, *computed
;
680 enum register_status result
= REG_VALID
;
682 mark
= value_mark ();
684 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
686 if (value_entirely_available (computed
))
687 memcpy (buf
, value_contents_raw (computed
),
688 m_descr
->sizeof_register
[regnum
]);
691 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
692 result
= REG_UNAVAILABLE
;
695 value_free_to_mark (mark
);
700 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
705 readable_regcache::cooked_read_value (int regnum
)
707 gdb_assert (regnum
>= 0);
708 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
710 if (regnum
< num_raw_registers ()
711 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
712 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
714 struct value
*result
;
716 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
717 VALUE_LVAL (result
) = lval_register
;
718 VALUE_REGNUM (result
) = regnum
;
720 /* It is more efficient in general to do this delegation in this
721 direction than in the other one, even though the value-based
723 if (cooked_read (regnum
,
724 value_contents_raw (result
)) == REG_UNAVAILABLE
)
725 mark_value_bytes_unavailable (result
, 0,
726 TYPE_LENGTH (value_type (result
)));
731 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
736 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
739 gdb_assert (regcache
!= NULL
);
740 return regcache
->cooked_read (regnum
, val
);
743 template<typename T
, typename
>
745 readable_regcache::cooked_read (int regnum
, T
*val
)
747 enum register_status status
;
750 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
751 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
752 status
= cooked_read (regnum
, buf
);
753 if (status
== REG_VALID
)
754 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
755 gdbarch_byte_order (m_descr
->gdbarch
));
762 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
765 gdb_assert (regcache
!= NULL
);
766 return regcache
->cooked_read (regnum
, val
);
770 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
773 gdb_assert (regcache
!= NULL
);
774 regcache
->cooked_write (regnum
, val
);
777 template<typename T
, typename
>
779 regcache::cooked_write (int regnum
, T val
)
783 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
784 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
785 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
786 gdbarch_byte_order (m_descr
->gdbarch
), val
);
787 cooked_write (regnum
, buf
);
791 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
794 gdb_assert (regcache
!= NULL
);
795 regcache
->cooked_write (regnum
, val
);
799 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
802 gdb_assert (buf
!= NULL
);
803 assert_regnum (regnum
);
805 /* On the sparc, writing %g0 is a no-op, so we don't even want to
806 change the registers array if something writes to this register. */
807 if (gdbarch_cannot_store_register (arch (), regnum
))
810 /* If we have a valid copy of the register, and new value == old
811 value, then don't bother doing the actual store. */
812 if (get_register_status (regnum
) == REG_VALID
813 && (memcmp (register_buffer (regnum
), buf
,
814 m_descr
->sizeof_register
[regnum
]) == 0))
817 target_prepare_to_store (this);
818 raw_supply (regnum
, buf
);
820 /* Invalidate the register after it is written, in case of a
823 = make_scope_exit ([&] { this->invalidate (regnum
); });
825 target_store_registers (this, regnum
);
827 /* The target did not throw an error so we can discard invalidating
829 invalidator
.release ();
833 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
835 gdb_assert (regnum
>= 0);
836 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
837 if (regnum
< num_raw_registers ())
838 raw_write (regnum
, buf
);
840 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
844 /* See regcache.h. */
847 readable_regcache::read_part (int regnum
, int offset
, int len
,
848 gdb_byte
*out
, bool is_raw
)
850 int reg_size
= register_size (arch (), regnum
);
852 gdb_assert (out
!= NULL
);
853 gdb_assert (offset
>= 0 && offset
<= reg_size
);
854 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
856 if (offset
== 0 && len
== 0)
862 if (offset
== 0 && len
== reg_size
)
864 /* Read the full register. */
865 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
868 enum register_status status
;
869 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
871 /* Read full register to buffer. */
872 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
873 if (status
!= REG_VALID
)
877 memcpy (out
, reg
+ offset
, len
);
881 /* See regcache.h. */
884 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
887 int reg_size
= register_size (arch (), regnum
);
889 gdb_assert (out
!= nullptr);
890 gdb_assert (offset
>= 0 && offset
<= reg_size
);
891 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
893 if (offset
== 0 && len
== 0)
899 if (offset
== 0 && len
== reg_size
)
901 /* Collect the full register. */
902 return raw_collect (regnum
, out
);
905 /* Read to buffer, then write out. */
906 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
907 raw_collect (regnum
, reg
);
908 memcpy (out
, reg
+ offset
, len
);
911 /* See regcache.h. */
914 regcache::write_part (int regnum
, int offset
, int len
,
915 const gdb_byte
*in
, bool is_raw
)
917 int reg_size
= register_size (arch (), regnum
);
919 gdb_assert (in
!= NULL
);
920 gdb_assert (offset
>= 0 && offset
<= reg_size
);
921 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
923 if (offset
== 0 && len
== 0)
929 if (offset
== 0 && len
== reg_size
)
931 /* Write the full register. */
932 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
936 enum register_status status
;
937 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
939 /* Read existing register to buffer. */
940 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
941 if (status
!= REG_VALID
)
944 /* Update buffer, then write back to regcache. */
945 memcpy (reg
+ offset
, in
, len
);
946 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
950 /* See regcache.h. */
953 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
956 int reg_size
= register_size (arch (), regnum
);
958 gdb_assert (in
!= nullptr);
959 gdb_assert (offset
>= 0 && offset
<= reg_size
);
960 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
962 if (offset
== 0 && len
== 0)
968 if (offset
== 0 && len
== reg_size
)
970 /* Supply the full register. */
971 return raw_supply (regnum
, in
);
974 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
976 /* Read existing value to buffer. */
977 raw_collect (regnum
, reg
);
979 /* Write to buffer, then write out. */
980 memcpy (reg
+ offset
, in
, len
);
981 raw_supply (regnum
, reg
);
985 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
988 assert_regnum (regnum
);
989 return read_part (regnum
, offset
, len
, buf
, true);
992 /* See regcache.h. */
995 regcache::raw_write_part (int regnum
, int offset
, int len
,
998 assert_regnum (regnum
);
999 write_part (regnum
, offset
, len
, buf
, true);
1002 /* See regcache.h. */
1004 enum register_status
1005 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
1008 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1009 return read_part (regnum
, offset
, len
, buf
, false);
1012 /* See regcache.h. */
1015 regcache::cooked_write_part (int regnum
, int offset
, int len
,
1016 const gdb_byte
*buf
)
1018 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1019 write_part (regnum
, offset
, len
, buf
, false);
1022 /* See gdbsupport/common-regcache.h. */
1025 reg_buffer::raw_supply (int regnum
, const void *buf
)
1030 assert_regnum (regnum
);
1032 regbuf
= register_buffer (regnum
);
1033 size
= m_descr
->sizeof_register
[regnum
];
1037 memcpy (regbuf
, buf
, size
);
1038 m_register_status
[regnum
] = REG_VALID
;
1042 /* This memset not strictly necessary, but better than garbage
1043 in case the register value manages to escape somewhere (due
1044 to a bug, no less). */
1045 memset (regbuf
, 0, size
);
1046 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1050 /* See regcache.h. */
1053 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1054 int addr_len
, bool is_signed
)
1056 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1060 assert_regnum (regnum
);
1062 regbuf
= register_buffer (regnum
);
1063 regsize
= m_descr
->sizeof_register
[regnum
];
1065 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1067 m_register_status
[regnum
] = REG_VALID
;
1070 /* See regcache.h. */
1073 reg_buffer::raw_supply_zeroed (int regnum
)
1078 assert_regnum (regnum
);
1080 regbuf
= register_buffer (regnum
);
1081 size
= m_descr
->sizeof_register
[regnum
];
1083 memset (regbuf
, 0, size
);
1084 m_register_status
[regnum
] = REG_VALID
;
1087 /* See gdbsupport/common-regcache.h. */
1090 reg_buffer::raw_collect (int regnum
, void *buf
) const
1095 gdb_assert (buf
!= NULL
);
1096 assert_regnum (regnum
);
1098 regbuf
= register_buffer (regnum
);
1099 size
= m_descr
->sizeof_register
[regnum
];
1100 memcpy (buf
, regbuf
, size
);
1103 /* See regcache.h. */
1106 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1107 bool is_signed
) const
1109 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1110 const gdb_byte
*regbuf
;
1113 assert_regnum (regnum
);
1115 regbuf
= register_buffer (regnum
);
1116 regsize
= m_descr
->sizeof_register
[regnum
];
1118 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1122 /* See regcache.h. */
1125 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1126 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1127 int slot_size
, int offs
) const
1129 struct gdbarch
*gdbarch
= arch ();
1130 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1132 /* Use part versions and reg_size to prevent possible buffer overflows when
1133 accessing the regcache. */
1135 if (out_buf
!= nullptr)
1137 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1139 /* Ensure any additional space is cleared. */
1140 if (slot_size
> reg_size
)
1141 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1143 else if (in_buf
!= nullptr)
1144 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1147 /* Invalidate the register. */
1148 out_regcache
->raw_supply (regnum
, nullptr);
1152 /* See regcache.h. */
1155 regcache::transfer_regset (const struct regset
*regset
,
1156 struct regcache
*out_regcache
,
1157 int regnum
, const gdb_byte
*in_buf
,
1158 gdb_byte
*out_buf
, size_t size
) const
1160 const struct regcache_map_entry
*map
;
1161 int offs
= 0, count
;
1163 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1164 (count
= map
->count
) != 0;
1167 int regno
= map
->regno
;
1168 int slot_size
= map
->size
;
1170 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1171 slot_size
= m_descr
->sizeof_register
[regno
];
1173 if (regno
== REGCACHE_MAP_SKIP
1175 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1176 offs
+= count
* slot_size
;
1178 else if (regnum
== -1)
1179 for (; count
--; regno
++, offs
+= slot_size
)
1181 if (offs
+ slot_size
> size
)
1184 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1189 /* Transfer a single register and return. */
1190 offs
+= (regnum
- regno
) * slot_size
;
1191 if (offs
+ slot_size
> size
)
1194 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1201 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1202 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1203 If BUF is NULL, set the register(s) to "unavailable" status. */
1206 regcache_supply_regset (const struct regset
*regset
,
1207 struct regcache
*regcache
,
1208 int regnum
, const void *buf
, size_t size
)
1210 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1214 regcache::supply_regset (const struct regset
*regset
,
1215 int regnum
, const void *buf
, size_t size
)
1217 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1220 /* Collect register REGNUM from REGCACHE to BUF, using the register
1221 map in REGSET. If REGNUM is -1, do this for all registers in
1225 regcache_collect_regset (const struct regset
*regset
,
1226 const struct regcache
*regcache
,
1227 int regnum
, void *buf
, size_t size
)
1229 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1233 regcache::collect_regset (const struct regset
*regset
,
1234 int regnum
, void *buf
, size_t size
) const
1236 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1239 /* See gdbsupport/common-regcache.h. */
1242 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1244 gdb_assert (buf
!= NULL
);
1245 assert_regnum (regnum
);
1247 const char *regbuf
= (const char *) register_buffer (regnum
);
1248 size_t size
= m_descr
->sizeof_register
[regnum
];
1249 gdb_assert (size
>= offset
);
1251 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1254 /* Special handling for register PC. */
1257 regcache_read_pc (struct regcache
*regcache
)
1259 struct gdbarch
*gdbarch
= regcache
->arch ();
1263 if (gdbarch_read_pc_p (gdbarch
))
1264 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1265 /* Else use per-frame method on get_current_frame. */
1266 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1270 if (regcache_cooked_read_unsigned (regcache
,
1271 gdbarch_pc_regnum (gdbarch
),
1272 &raw_val
) == REG_UNAVAILABLE
)
1273 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1275 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1278 internal_error (__FILE__
, __LINE__
,
1279 _("regcache_read_pc: Unable to find PC"));
1284 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1286 struct gdbarch
*gdbarch
= regcache
->arch ();
1288 if (gdbarch_write_pc_p (gdbarch
))
1289 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1290 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1291 regcache_cooked_write_unsigned (regcache
,
1292 gdbarch_pc_regnum (gdbarch
), pc
);
1294 internal_error (__FILE__
, __LINE__
,
1295 _("regcache_write_pc: Unable to update PC"));
1297 /* Writing the PC (for instance, from "load") invalidates the
1299 reinit_frame_cache ();
1303 reg_buffer::num_raw_registers () const
1305 return gdbarch_num_regs (arch ());
1309 regcache::debug_print_register (const char *func
, int regno
)
1311 struct gdbarch
*gdbarch
= arch ();
1313 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1314 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1315 && gdbarch_register_name (gdbarch
, regno
) != NULL
1316 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1317 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1318 gdbarch_register_name (gdbarch
, regno
));
1320 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1321 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1323 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1324 int size
= register_size (gdbarch
, regno
);
1325 gdb_byte
*buf
= register_buffer (regno
);
1327 fprintf_unfiltered (gdb_stdlog
, " = ");
1328 for (int i
= 0; i
< size
; i
++)
1330 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1332 if (size
<= sizeof (LONGEST
))
1334 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1336 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1337 core_addr_to_string_nz (val
), plongest (val
));
1340 fprintf_unfiltered (gdb_stdlog
, "\n");
1344 reg_flush_command (const char *command
, int from_tty
)
1346 /* Force-flush the register cache. */
1347 registers_changed ();
1349 printf_filtered (_("Register cache flushed.\n"));
1353 register_dump::dump (ui_file
*file
)
1355 auto descr
= regcache_descr (m_gdbarch
);
1357 int footnote_nr
= 0;
1358 int footnote_register_offset
= 0;
1359 int footnote_register_type_name_null
= 0;
1360 long register_offset
= 0;
1362 gdb_assert (descr
->nr_cooked_registers
1363 == gdbarch_num_cooked_regs (m_gdbarch
));
1365 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1369 fprintf_unfiltered (file
, " %-10s", "Name");
1372 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1376 else if (p
[0] == '\0')
1378 fprintf_unfiltered (file
, " %-10s", p
);
1383 fprintf_unfiltered (file
, " %4s", "Nr");
1385 fprintf_unfiltered (file
, " %4d", regnum
);
1387 /* Relative number. */
1389 fprintf_unfiltered (file
, " %4s", "Rel");
1390 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1391 fprintf_unfiltered (file
, " %4d", regnum
);
1393 fprintf_unfiltered (file
, " %4d",
1394 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1398 fprintf_unfiltered (file
, " %6s ", "Offset");
1401 fprintf_unfiltered (file
, " %6ld",
1402 descr
->register_offset
[regnum
]);
1403 if (register_offset
!= descr
->register_offset
[regnum
]
1405 && (descr
->register_offset
[regnum
]
1406 != (descr
->register_offset
[regnum
- 1]
1407 + descr
->sizeof_register
[regnum
- 1])))
1410 if (!footnote_register_offset
)
1411 footnote_register_offset
= ++footnote_nr
;
1412 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1415 fprintf_unfiltered (file
, " ");
1416 register_offset
= (descr
->register_offset
[regnum
]
1417 + descr
->sizeof_register
[regnum
]);
1422 fprintf_unfiltered (file
, " %5s ", "Size");
1424 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1429 std::string name_holder
;
1435 static const char blt
[] = "builtin_type";
1437 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1440 if (!footnote_register_type_name_null
)
1441 footnote_register_type_name_null
= ++footnote_nr
;
1442 name_holder
= string_printf ("*%d",
1443 footnote_register_type_name_null
);
1444 t
= name_holder
.c_str ();
1446 /* Chop a leading builtin_type. */
1447 if (startswith (t
, blt
))
1450 fprintf_unfiltered (file
, " %-15s", t
);
1453 /* Leading space always present. */
1454 fprintf_unfiltered (file
, " ");
1456 dump_reg (file
, regnum
);
1458 fprintf_unfiltered (file
, "\n");
1461 if (footnote_register_offset
)
1462 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1463 footnote_register_offset
);
1464 if (footnote_register_type_name_null
)
1465 fprintf_unfiltered (file
,
1466 "*%d: Register type's name NULL.\n",
1467 footnote_register_type_name_null
);
1471 #include "gdbsupport/selftest.h"
1472 #include "selftest-arch.h"
1473 #include "target-float.h"
1475 namespace selftests
{
1477 class regcache_access
: public regcache
1481 /* Return the number of elements in current_regcache. */
1484 current_regcache_size ()
1486 return the_regcaches
.size ();
1490 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1493 test_get_thread_arch_aspace_regcache (process_stratum_target
*target
,
1494 ptid_t ptid
, struct gdbarch
*gdbarch
,
1495 address_space
*aspace
)
1497 struct regcache
*regcache
1498 = get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
1499 SELF_CHECK (regcache
!= NULL
);
1500 SELF_CHECK (regcache
->target () == target
);
1501 SELF_CHECK (regcache
->ptid () == ptid
);
1502 SELF_CHECK (regcache
->aspace () == aspace
);
1506 current_regcache_test (void)
1508 /* It is empty at the start. */
1509 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1511 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1513 test_target_ops test_target1
;
1514 test_target_ops test_target2
;
1516 /* Get regcache from (target1,ptid1), a new regcache is added to
1517 current_regcache. */
1518 test_get_thread_arch_aspace_regcache (&test_target1
, ptid1
,
1521 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1523 /* Get regcache from (target1,ptid2), a new regcache is added to
1524 current_regcache. */
1525 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1528 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1530 /* Get regcache from (target1,ptid3), a new regcache is added to
1531 current_regcache. */
1532 test_get_thread_arch_aspace_regcache (&test_target1
, ptid3
,
1535 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1537 /* Get regcache from (target1,ptid2) again, nothing is added to
1538 current_regcache. */
1539 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1542 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1544 /* Get regcache from (target2,ptid2), a new regcache is added to
1545 current_regcache, since this time we're using a differen
1547 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1550 SELF_CHECK (regcache_access::current_regcache_size () == 4);
1552 /* Mark that (target1,ptid2) changed. The regcache of (target1,
1553 ptid2) should be removed from current_regcache. */
1554 registers_changed_ptid (&test_target1
, ptid2
);
1555 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1557 /* Get the regcache from (target2,ptid2) again, confirming the
1558 registers_changed_ptid call above did not delete it. */
1559 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1562 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1564 /* Confirm that marking all regcaches of all targets as changed
1565 clears current_regcache. */
1566 registers_changed_ptid (nullptr, minus_one_ptid
);
1567 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1570 class target_ops_no_register
: public test_target_ops
1573 target_ops_no_register ()
1574 : test_target_ops
{}
1579 fetch_registers_called
= 0;
1580 store_registers_called
= 0;
1581 xfer_partial_called
= 0;
1584 void fetch_registers (regcache
*regs
, int regno
) override
;
1585 void store_registers (regcache
*regs
, int regno
) override
;
1587 enum target_xfer_status
xfer_partial (enum target_object object
,
1588 const char *annex
, gdb_byte
*readbuf
,
1589 const gdb_byte
*writebuf
,
1590 ULONGEST offset
, ULONGEST len
,
1591 ULONGEST
*xfered_len
) override
;
1593 unsigned int fetch_registers_called
= 0;
1594 unsigned int store_registers_called
= 0;
1595 unsigned int xfer_partial_called
= 0;
1599 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1601 /* Mark register available. */
1602 regs
->raw_supply_zeroed (regno
);
1603 this->fetch_registers_called
++;
1607 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1609 this->store_registers_called
++;
1612 enum target_xfer_status
1613 target_ops_no_register::xfer_partial (enum target_object object
,
1614 const char *annex
, gdb_byte
*readbuf
,
1615 const gdb_byte
*writebuf
,
1616 ULONGEST offset
, ULONGEST len
,
1617 ULONGEST
*xfered_len
)
1619 this->xfer_partial_called
++;
1622 return TARGET_XFER_OK
;
1625 class readwrite_regcache
: public regcache
1628 readwrite_regcache (process_stratum_target
*target
,
1629 struct gdbarch
*gdbarch
)
1630 : regcache (target
, gdbarch
, nullptr)
1634 /* Test regcache::cooked_read gets registers from raw registers and
1635 memory instead of target to_{fetch,store}_registers. */
1638 cooked_read_test (struct gdbarch
*gdbarch
)
1640 /* Error out if debugging something, because we're going to push the
1641 test target, which would pop any existing target. */
1642 if (current_top_target ()->stratum () >= process_stratum
)
1643 error (_("target already pushed"));
1645 /* Create a mock environment. An inferior with a thread, with a
1646 process_stratum target pushed. */
1648 target_ops_no_register mock_target
;
1649 ptid_t
mock_ptid (1, 1);
1650 inferior
mock_inferior (mock_ptid
.pid ());
1651 address_space mock_aspace
{};
1652 mock_inferior
.gdbarch
= gdbarch
;
1653 mock_inferior
.aspace
= &mock_aspace
;
1654 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1655 mock_inferior
.thread_map
[mock_ptid
] = &mock_thread
;
1657 /* Add the mock inferior to the inferior list so that look ups by
1658 target+ptid can find it. */
1659 scoped_restore restore_inferior_list
1660 = make_scoped_restore (&inferior_list
);
1661 inferior_list
= &mock_inferior
;
1663 /* Switch to the mock inferior. */
1664 scoped_restore_current_inferior restore_current_inferior
;
1665 set_current_inferior (&mock_inferior
);
1667 /* Push the process_stratum target so we can mock accessing
1669 push_target (&mock_target
);
1671 /* Pop it again on exit (return/exception). */
1676 pop_all_targets_at_and_above (process_stratum
);
1680 /* Switch to the mock thread. */
1681 scoped_restore restore_inferior_ptid
1682 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1684 /* Test that read one raw register from regcache_no_target will go
1685 to the target layer. */
1687 /* Find a raw register which size isn't zero. */
1689 for (nonzero_regnum
= 0;
1690 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1693 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1697 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1698 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1700 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1702 /* raw_read calls target_fetch_registers. */
1703 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1704 mock_target
.reset ();
1706 /* Mark all raw registers valid, so the following raw registers
1707 accesses won't go to target. */
1708 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1709 readwrite
.raw_update (i
);
1711 mock_target
.reset ();
1712 /* Then, read all raw and pseudo registers, and don't expect calling
1713 to_{fetch,store}_registers. */
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
));
1721 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1722 inner_buf
.data ()));
1724 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1725 SELF_CHECK (mock_target
.store_registers_called
== 0);
1726 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1728 mock_target
.reset ();
1731 readonly_detached_regcache
readonly (readwrite
);
1733 /* GDB may go to target layer to fetch all registers and memory for
1734 readonly regcache. */
1735 mock_target
.reset ();
1737 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1739 if (register_size (gdbarch
, regnum
) == 0)
1742 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1743 enum register_status status
= readonly
.cooked_read (regnum
,
1746 if (regnum
< gdbarch_num_regs (gdbarch
))
1748 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1750 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1751 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1752 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1753 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1754 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1755 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1756 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1757 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1759 /* Raw registers. If raw registers are not in save_reggroup,
1760 their status are unknown. */
1761 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1762 SELF_CHECK (status
== REG_VALID
);
1764 SELF_CHECK (status
== REG_UNKNOWN
);
1767 SELF_CHECK (status
== REG_VALID
);
1771 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1772 SELF_CHECK (status
== REG_VALID
);
1775 /* If pseudo registers are not in save_reggroup, some of
1776 them can be computed from saved raw registers, but some
1777 of them are unknown. */
1778 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1780 if (bfd_arch
== bfd_arch_frv
1781 || bfd_arch
== bfd_arch_m32c
1782 || bfd_arch
== bfd_arch_mep
1783 || bfd_arch
== bfd_arch_sh
)
1784 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1785 else if (bfd_arch
== bfd_arch_mips
1786 || bfd_arch
== bfd_arch_h8300
)
1787 SELF_CHECK (status
== REG_UNKNOWN
);
1789 SELF_CHECK (status
== REG_VALID
);
1793 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1794 SELF_CHECK (mock_target
.store_registers_called
== 0);
1795 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1797 mock_target
.reset ();
1801 /* Test regcache::cooked_write by writing some expected contents to
1802 registers, and checking that contents read from registers and the
1803 expected contents are the same. */
1806 cooked_write_test (struct gdbarch
*gdbarch
)
1808 /* Error out if debugging something, because we're going to push the
1809 test target, which would pop any existing target. */
1810 if (current_top_target ()->stratum () >= process_stratum
)
1811 error (_("target already pushed"));
1813 /* Create a mock environment. A process_stratum target pushed. */
1815 target_ops_no_register mock_target
;
1817 /* Push the process_stratum target so we can mock accessing
1819 push_target (&mock_target
);
1821 /* Pop it again on exit (return/exception). */
1826 pop_all_targets_at_and_above (process_stratum
);
1830 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1832 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1834 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1836 if (register_size (gdbarch
, regnum
) == 0
1837 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1840 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1842 if (bfd_arch
== bfd_arch_sparc
1843 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1844 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1845 && gdbarch_ptr_bit (gdbarch
) == 64
1846 && (regnum
>= gdbarch_num_regs (gdbarch
)
1847 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1850 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1851 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1852 const auto type
= register_type (gdbarch
, regnum
);
1854 if (TYPE_CODE (type
) == TYPE_CODE_FLT
1855 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
1857 /* Generate valid float format. */
1858 target_float_from_string (expected
.data (), type
, "1.25");
1860 else if (TYPE_CODE (type
) == TYPE_CODE_INT
1861 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
1862 || TYPE_CODE (type
) == TYPE_CODE_PTR
1863 || TYPE_CODE (type
) == TYPE_CODE_UNION
1864 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
1866 if (bfd_arch
== bfd_arch_ia64
1867 || (regnum
>= gdbarch_num_regs (gdbarch
)
1868 && (bfd_arch
== bfd_arch_xtensa
1869 || bfd_arch
== bfd_arch_bfin
1870 || bfd_arch
== bfd_arch_m32c
1871 /* m68hc11 pseudo registers are in memory. */
1872 || bfd_arch
== bfd_arch_m68hc11
1873 || bfd_arch
== bfd_arch_m68hc12
1874 || bfd_arch
== bfd_arch_s390
))
1875 || (bfd_arch
== bfd_arch_frv
1876 /* FRV pseudo registers except iacc0. */
1877 && regnum
> gdbarch_num_regs (gdbarch
)))
1879 /* Skip setting the expected values for some architecture
1882 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1884 /* RL78_PC_REGNUM */
1885 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1890 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1894 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
1896 /* No idea how to test flags. */
1901 /* If we don't know how to create the expected value for the
1902 this type, make it fail. */
1906 readwrite
.cooked_write (regnum
, expected
.data ());
1908 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1909 SELF_CHECK (expected
== buf
);
1913 } // namespace selftests
1914 #endif /* GDB_SELF_TEST */
1917 _initialize_regcache (void)
1919 regcache_descr_handle
1920 = gdbarch_data_register_post_init (init_regcache_descr
);
1922 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1923 gdb::observers::thread_ptid_changed
.attach (regcache_thread_ptid_changed
);
1925 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1926 _("Force gdb to flush its register cache (maintainer command)."));
1929 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1931 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1932 selftests::cooked_read_test
);
1933 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1934 selftests::cooked_write_test
);