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. */
322 std::forward_list
<regcache
*> regcache::current_regcache
;
325 get_thread_arch_aspace_regcache (process_stratum_target
*target
,
326 ptid_t ptid
, struct gdbarch
*gdbarch
,
327 struct address_space
*aspace
)
329 gdb_assert (target
!= nullptr);
331 for (const auto ®cache
: regcache::current_regcache
)
332 if (regcache
->target () == target
333 && regcache
->ptid () == ptid
334 && regcache
->arch () == gdbarch
)
337 regcache
*new_regcache
= new regcache (target
, gdbarch
, aspace
);
339 regcache::current_regcache
.push_front (new_regcache
);
340 new_regcache
->set_ptid (ptid
);
346 get_thread_arch_regcache (process_stratum_target
*target
, ptid_t ptid
,
347 struct gdbarch
*gdbarch
)
349 scoped_restore_current_inferior restore_current_inferior
;
350 set_current_inferior (find_inferior_ptid (target
, ptid
));
351 address_space
*aspace
= target_thread_address_space (ptid
);
353 return get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
356 static process_stratum_target
*current_thread_target
;
357 static ptid_t current_thread_ptid
;
358 static struct gdbarch
*current_thread_arch
;
361 get_thread_regcache (process_stratum_target
*target
, ptid_t ptid
)
363 if (!current_thread_arch
364 || target
!= current_thread_target
365 || current_thread_ptid
!= ptid
)
367 gdb_assert (ptid
!= null_ptid
);
369 current_thread_ptid
= ptid
;
370 current_thread_target
= target
;
372 scoped_restore_current_inferior restore_current_inferior
;
373 set_current_inferior (find_inferior_ptid (target
, ptid
));
374 current_thread_arch
= target_thread_architecture (ptid
);
377 return get_thread_arch_regcache (target
, ptid
, current_thread_arch
);
380 /* See regcache.h. */
383 get_thread_regcache (thread_info
*thread
)
385 return get_thread_regcache (thread
->inf
->process_target (),
390 get_current_regcache (void)
392 return get_thread_regcache (inferior_thread ());
395 /* See gdbsupport/common-regcache.h. */
398 get_thread_regcache_for_ptid (ptid_t ptid
)
400 /* This function doesn't take a process_stratum_target parameter
401 because it's a gdbsupport/ routine implemented by both gdb and
402 gdbserver. It always refers to a ptid of the current target. */
403 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
404 return get_thread_regcache (proc_target
, ptid
);
407 /* Observer for the target_changed event. */
410 regcache_observer_target_changed (struct target_ops
*target
)
412 registers_changed ();
415 /* Update global variables old ptids to hold NEW_PTID if they were
418 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
420 for (auto ®cache
: regcache::current_regcache
)
422 if (regcache
->ptid () == old_ptid
)
423 regcache
->set_ptid (new_ptid
);
427 /* Low level examining and depositing of registers.
429 The caller is responsible for making sure that the inferior is
430 stopped before calling the fetching routines, or it will get
431 garbage. (a change from GDB version 3, in which the caller got the
432 value from the last stop). */
434 /* REGISTERS_CHANGED ()
436 Indicate that registers may have changed, so invalidate the cache. */
439 registers_changed_ptid (process_stratum_target
*target
, ptid_t ptid
)
441 for (auto oit
= regcache::current_regcache
.before_begin (),
442 it
= std::next (oit
);
443 it
!= regcache::current_regcache
.end ();
446 struct regcache
*regcache
= *it
;
447 if ((target
== nullptr || regcache
->target () == target
)
448 && regcache
->ptid ().matches (ptid
))
451 it
= regcache::current_regcache
.erase_after (oit
);
457 if ((target
== nullptr || current_thread_target
== target
)
458 && current_thread_ptid
.matches (ptid
))
460 current_thread_target
= NULL
;
461 current_thread_ptid
= null_ptid
;
462 current_thread_arch
= NULL
;
465 if ((target
== nullptr || current_inferior ()->process_target () == target
)
466 && inferior_ptid
.matches (ptid
))
468 /* We just deleted the regcache of the current thread. Need to
469 forget about any frames we have cached, too. */
470 reinit_frame_cache ();
474 /* See regcache.h. */
477 registers_changed_thread (thread_info
*thread
)
479 registers_changed_ptid (thread
->inf
->process_target (), thread
->ptid
);
483 registers_changed (void)
485 registers_changed_ptid (nullptr, minus_one_ptid
);
489 regcache::raw_update (int regnum
)
491 assert_regnum (regnum
);
493 /* Make certain that the register cache is up-to-date with respect
494 to the current thread. This switching shouldn't be necessary
495 only there is still only one target side register cache. Sigh!
496 On the bright side, at least there is a regcache object. */
498 if (get_register_status (regnum
) == REG_UNKNOWN
)
500 target_fetch_registers (this, regnum
);
502 /* A number of targets can't access the whole set of raw
503 registers (because the debug API provides no means to get at
505 if (m_register_status
[regnum
] == REG_UNKNOWN
)
506 m_register_status
[regnum
] = REG_UNAVAILABLE
;
511 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
513 gdb_assert (buf
!= NULL
);
516 if (m_register_status
[regnum
] != REG_VALID
)
517 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
519 memcpy (buf
, register_buffer (regnum
),
520 m_descr
->sizeof_register
[regnum
]);
522 return m_register_status
[regnum
];
526 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
528 gdb_assert (regcache
!= NULL
);
529 return regcache
->raw_read (regnum
, val
);
532 template<typename T
, typename
>
534 readable_regcache::raw_read (int regnum
, T
*val
)
537 enum register_status status
;
539 assert_regnum (regnum
);
540 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
541 status
= raw_read (regnum
, buf
);
542 if (status
== REG_VALID
)
543 *val
= extract_integer
<T
> (buf
,
544 m_descr
->sizeof_register
[regnum
],
545 gdbarch_byte_order (m_descr
->gdbarch
));
552 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
555 gdb_assert (regcache
!= NULL
);
556 return regcache
->raw_read (regnum
, val
);
560 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
562 gdb_assert (regcache
!= NULL
);
563 regcache
->raw_write (regnum
, val
);
566 template<typename T
, typename
>
568 regcache::raw_write (int regnum
, T val
)
572 assert_regnum (regnum
);
573 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
574 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
575 gdbarch_byte_order (m_descr
->gdbarch
), val
);
576 raw_write (regnum
, buf
);
580 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
583 gdb_assert (regcache
!= NULL
);
584 regcache
->raw_write (regnum
, val
);
588 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
591 enum register_status status
;
593 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
594 if (status
== REG_UNAVAILABLE
)
595 throw_error (NOT_AVAILABLE_ERROR
,
596 _("Register %d is not available"), regnum
);
601 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
603 gdb_assert (regnum
>= 0);
604 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
605 if (regnum
< num_raw_registers ())
606 return raw_read (regnum
, buf
);
607 else if (m_has_pseudo
608 && m_register_status
[regnum
] != REG_UNKNOWN
)
610 if (m_register_status
[regnum
] == REG_VALID
)
611 memcpy (buf
, register_buffer (regnum
),
612 m_descr
->sizeof_register
[regnum
]);
614 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
616 return m_register_status
[regnum
];
618 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
620 struct value
*mark
, *computed
;
621 enum register_status result
= REG_VALID
;
623 mark
= value_mark ();
625 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
627 if (value_entirely_available (computed
))
628 memcpy (buf
, value_contents_raw (computed
),
629 m_descr
->sizeof_register
[regnum
]);
632 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
633 result
= REG_UNAVAILABLE
;
636 value_free_to_mark (mark
);
641 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
646 readable_regcache::cooked_read_value (int regnum
)
648 gdb_assert (regnum
>= 0);
649 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
651 if (regnum
< num_raw_registers ()
652 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
653 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
655 struct value
*result
;
657 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
658 VALUE_LVAL (result
) = lval_register
;
659 VALUE_REGNUM (result
) = regnum
;
661 /* It is more efficient in general to do this delegation in this
662 direction than in the other one, even though the value-based
664 if (cooked_read (regnum
,
665 value_contents_raw (result
)) == REG_UNAVAILABLE
)
666 mark_value_bytes_unavailable (result
, 0,
667 TYPE_LENGTH (value_type (result
)));
672 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
677 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
680 gdb_assert (regcache
!= NULL
);
681 return regcache
->cooked_read (regnum
, val
);
684 template<typename T
, typename
>
686 readable_regcache::cooked_read (int regnum
, T
*val
)
688 enum register_status status
;
691 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
692 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
693 status
= cooked_read (regnum
, buf
);
694 if (status
== REG_VALID
)
695 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
696 gdbarch_byte_order (m_descr
->gdbarch
));
703 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
706 gdb_assert (regcache
!= NULL
);
707 return regcache
->cooked_read (regnum
, val
);
711 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
714 gdb_assert (regcache
!= NULL
);
715 regcache
->cooked_write (regnum
, val
);
718 template<typename T
, typename
>
720 regcache::cooked_write (int regnum
, T val
)
724 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
725 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
726 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
727 gdbarch_byte_order (m_descr
->gdbarch
), val
);
728 cooked_write (regnum
, buf
);
732 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
735 gdb_assert (regcache
!= NULL
);
736 regcache
->cooked_write (regnum
, val
);
740 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
743 gdb_assert (buf
!= NULL
);
744 assert_regnum (regnum
);
746 /* On the sparc, writing %g0 is a no-op, so we don't even want to
747 change the registers array if something writes to this register. */
748 if (gdbarch_cannot_store_register (arch (), regnum
))
751 /* If we have a valid copy of the register, and new value == old
752 value, then don't bother doing the actual store. */
753 if (get_register_status (regnum
) == REG_VALID
754 && (memcmp (register_buffer (regnum
), buf
,
755 m_descr
->sizeof_register
[regnum
]) == 0))
758 target_prepare_to_store (this);
759 raw_supply (regnum
, buf
);
761 /* Invalidate the register after it is written, in case of a
764 = make_scope_exit ([&] { this->invalidate (regnum
); });
766 target_store_registers (this, regnum
);
768 /* The target did not throw an error so we can discard invalidating
770 invalidator
.release ();
774 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
776 gdb_assert (regnum
>= 0);
777 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
778 if (regnum
< num_raw_registers ())
779 raw_write (regnum
, buf
);
781 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
785 /* See regcache.h. */
788 readable_regcache::read_part (int regnum
, int offset
, int len
,
789 gdb_byte
*out
, bool is_raw
)
791 int reg_size
= register_size (arch (), regnum
);
793 gdb_assert (out
!= NULL
);
794 gdb_assert (offset
>= 0 && offset
<= reg_size
);
795 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
797 if (offset
== 0 && len
== 0)
803 if (offset
== 0 && len
== reg_size
)
805 /* Read the full register. */
806 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
809 enum register_status status
;
810 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
812 /* Read full register to buffer. */
813 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
814 if (status
!= REG_VALID
)
818 memcpy (out
, reg
+ offset
, len
);
822 /* See regcache.h. */
825 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
828 int reg_size
= register_size (arch (), regnum
);
830 gdb_assert (out
!= nullptr);
831 gdb_assert (offset
>= 0 && offset
<= reg_size
);
832 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
834 if (offset
== 0 && len
== 0)
840 if (offset
== 0 && len
== reg_size
)
842 /* Collect the full register. */
843 return raw_collect (regnum
, out
);
846 /* Read to buffer, then write out. */
847 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
848 raw_collect (regnum
, reg
);
849 memcpy (out
, reg
+ offset
, len
);
852 /* See regcache.h. */
855 regcache::write_part (int regnum
, int offset
, int len
,
856 const gdb_byte
*in
, bool is_raw
)
858 int reg_size
= register_size (arch (), regnum
);
860 gdb_assert (in
!= NULL
);
861 gdb_assert (offset
>= 0 && offset
<= reg_size
);
862 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
864 if (offset
== 0 && len
== 0)
870 if (offset
== 0 && len
== reg_size
)
872 /* Write the full register. */
873 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
877 enum register_status status
;
878 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
880 /* Read existing register to buffer. */
881 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
882 if (status
!= REG_VALID
)
885 /* Update buffer, then write back to regcache. */
886 memcpy (reg
+ offset
, in
, len
);
887 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
891 /* See regcache.h. */
894 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
897 int reg_size
= register_size (arch (), regnum
);
899 gdb_assert (in
!= nullptr);
900 gdb_assert (offset
>= 0 && offset
<= reg_size
);
901 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
903 if (offset
== 0 && len
== 0)
909 if (offset
== 0 && len
== reg_size
)
911 /* Supply the full register. */
912 return raw_supply (regnum
, in
);
915 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
917 /* Read existing value to buffer. */
918 raw_collect (regnum
, reg
);
920 /* Write to buffer, then write out. */
921 memcpy (reg
+ offset
, in
, len
);
922 raw_supply (regnum
, reg
);
926 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
929 assert_regnum (regnum
);
930 return read_part (regnum
, offset
, len
, buf
, true);
933 /* See regcache.h. */
936 regcache::raw_write_part (int regnum
, int offset
, int len
,
939 assert_regnum (regnum
);
940 write_part (regnum
, offset
, len
, buf
, true);
943 /* See regcache.h. */
946 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
949 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
950 return read_part (regnum
, offset
, len
, buf
, false);
953 /* See regcache.h. */
956 regcache::cooked_write_part (int regnum
, int offset
, int len
,
959 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
960 write_part (regnum
, offset
, len
, buf
, false);
963 /* See gdbsupport/common-regcache.h. */
966 reg_buffer::raw_supply (int regnum
, const void *buf
)
971 assert_regnum (regnum
);
973 regbuf
= register_buffer (regnum
);
974 size
= m_descr
->sizeof_register
[regnum
];
978 memcpy (regbuf
, buf
, size
);
979 m_register_status
[regnum
] = REG_VALID
;
983 /* This memset not strictly necessary, but better than garbage
984 in case the register value manages to escape somewhere (due
985 to a bug, no less). */
986 memset (regbuf
, 0, size
);
987 m_register_status
[regnum
] = REG_UNAVAILABLE
;
991 /* See regcache.h. */
994 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
995 int addr_len
, bool is_signed
)
997 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1001 assert_regnum (regnum
);
1003 regbuf
= register_buffer (regnum
);
1004 regsize
= m_descr
->sizeof_register
[regnum
];
1006 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1008 m_register_status
[regnum
] = REG_VALID
;
1011 /* See regcache.h. */
1014 reg_buffer::raw_supply_zeroed (int regnum
)
1019 assert_regnum (regnum
);
1021 regbuf
= register_buffer (regnum
);
1022 size
= m_descr
->sizeof_register
[regnum
];
1024 memset (regbuf
, 0, size
);
1025 m_register_status
[regnum
] = REG_VALID
;
1028 /* See gdbsupport/common-regcache.h. */
1031 reg_buffer::raw_collect (int regnum
, void *buf
) const
1036 gdb_assert (buf
!= NULL
);
1037 assert_regnum (regnum
);
1039 regbuf
= register_buffer (regnum
);
1040 size
= m_descr
->sizeof_register
[regnum
];
1041 memcpy (buf
, regbuf
, size
);
1044 /* See regcache.h. */
1047 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1048 bool is_signed
) const
1050 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1051 const gdb_byte
*regbuf
;
1054 assert_regnum (regnum
);
1056 regbuf
= register_buffer (regnum
);
1057 regsize
= m_descr
->sizeof_register
[regnum
];
1059 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1063 /* See regcache.h. */
1066 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1067 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1068 int slot_size
, int offs
) const
1070 struct gdbarch
*gdbarch
= arch ();
1071 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1073 /* Use part versions and reg_size to prevent possible buffer overflows when
1074 accessing the regcache. */
1076 if (out_buf
!= nullptr)
1078 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1080 /* Ensure any additional space is cleared. */
1081 if (slot_size
> reg_size
)
1082 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1084 else if (in_buf
!= nullptr)
1085 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1088 /* Invalidate the register. */
1089 out_regcache
->raw_supply (regnum
, nullptr);
1093 /* See regcache.h. */
1096 regcache::transfer_regset (const struct regset
*regset
,
1097 struct regcache
*out_regcache
,
1098 int regnum
, const gdb_byte
*in_buf
,
1099 gdb_byte
*out_buf
, size_t size
) const
1101 const struct regcache_map_entry
*map
;
1102 int offs
= 0, count
;
1104 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1105 (count
= map
->count
) != 0;
1108 int regno
= map
->regno
;
1109 int slot_size
= map
->size
;
1111 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1112 slot_size
= m_descr
->sizeof_register
[regno
];
1114 if (regno
== REGCACHE_MAP_SKIP
1116 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1117 offs
+= count
* slot_size
;
1119 else if (regnum
== -1)
1120 for (; count
--; regno
++, offs
+= slot_size
)
1122 if (offs
+ slot_size
> size
)
1125 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1130 /* Transfer a single register and return. */
1131 offs
+= (regnum
- regno
) * slot_size
;
1132 if (offs
+ slot_size
> size
)
1135 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1142 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1143 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1144 If BUF is NULL, set the register(s) to "unavailable" status. */
1147 regcache_supply_regset (const struct regset
*regset
,
1148 struct regcache
*regcache
,
1149 int regnum
, const void *buf
, size_t size
)
1151 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1155 regcache::supply_regset (const struct regset
*regset
,
1156 int regnum
, const void *buf
, size_t size
)
1158 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1161 /* Collect register REGNUM from REGCACHE to BUF, using the register
1162 map in REGSET. If REGNUM is -1, do this for all registers in
1166 regcache_collect_regset (const struct regset
*regset
,
1167 const struct regcache
*regcache
,
1168 int regnum
, void *buf
, size_t size
)
1170 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1174 regcache::collect_regset (const struct regset
*regset
,
1175 int regnum
, void *buf
, size_t size
) const
1177 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1180 /* See gdbsupport/common-regcache.h. */
1183 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1185 gdb_assert (buf
!= NULL
);
1186 assert_regnum (regnum
);
1188 const char *regbuf
= (const char *) register_buffer (regnum
);
1189 size_t size
= m_descr
->sizeof_register
[regnum
];
1190 gdb_assert (size
>= offset
);
1192 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1195 /* Special handling for register PC. */
1198 regcache_read_pc (struct regcache
*regcache
)
1200 struct gdbarch
*gdbarch
= regcache
->arch ();
1204 if (gdbarch_read_pc_p (gdbarch
))
1205 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1206 /* Else use per-frame method on get_current_frame. */
1207 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1211 if (regcache_cooked_read_unsigned (regcache
,
1212 gdbarch_pc_regnum (gdbarch
),
1213 &raw_val
) == REG_UNAVAILABLE
)
1214 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1216 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1219 internal_error (__FILE__
, __LINE__
,
1220 _("regcache_read_pc: Unable to find PC"));
1224 /* See gdbsupport/common-regcache.h. */
1227 regcache_read_pc_protected (regcache
*regcache
)
1232 pc
= regcache_read_pc (regcache
);
1234 catch (const gdb_exception_error
&ex
)
1243 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1245 struct gdbarch
*gdbarch
= regcache
->arch ();
1247 if (gdbarch_write_pc_p (gdbarch
))
1248 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1249 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1250 regcache_cooked_write_unsigned (regcache
,
1251 gdbarch_pc_regnum (gdbarch
), pc
);
1253 internal_error (__FILE__
, __LINE__
,
1254 _("regcache_write_pc: Unable to update PC"));
1256 /* Writing the PC (for instance, from "load") invalidates the
1258 reinit_frame_cache ();
1262 reg_buffer::num_raw_registers () const
1264 return gdbarch_num_regs (arch ());
1268 regcache::debug_print_register (const char *func
, int regno
)
1270 struct gdbarch
*gdbarch
= arch ();
1272 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1273 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1274 && gdbarch_register_name (gdbarch
, regno
) != NULL
1275 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1276 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1277 gdbarch_register_name (gdbarch
, regno
));
1279 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1280 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1282 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1283 int size
= register_size (gdbarch
, regno
);
1284 gdb_byte
*buf
= register_buffer (regno
);
1286 fprintf_unfiltered (gdb_stdlog
, " = ");
1287 for (int i
= 0; i
< size
; i
++)
1289 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1291 if (size
<= sizeof (LONGEST
))
1293 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1295 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1296 core_addr_to_string_nz (val
), plongest (val
));
1299 fprintf_unfiltered (gdb_stdlog
, "\n");
1303 reg_flush_command (const char *command
, int from_tty
)
1305 /* Force-flush the register cache. */
1306 registers_changed ();
1308 printf_filtered (_("Register cache flushed.\n"));
1312 register_dump::dump (ui_file
*file
)
1314 auto descr
= regcache_descr (m_gdbarch
);
1316 int footnote_nr
= 0;
1317 int footnote_register_offset
= 0;
1318 int footnote_register_type_name_null
= 0;
1319 long register_offset
= 0;
1321 gdb_assert (descr
->nr_cooked_registers
1322 == gdbarch_num_cooked_regs (m_gdbarch
));
1324 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1328 fprintf_unfiltered (file
, " %-10s", "Name");
1331 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1335 else if (p
[0] == '\0')
1337 fprintf_unfiltered (file
, " %-10s", p
);
1342 fprintf_unfiltered (file
, " %4s", "Nr");
1344 fprintf_unfiltered (file
, " %4d", regnum
);
1346 /* Relative number. */
1348 fprintf_unfiltered (file
, " %4s", "Rel");
1349 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1350 fprintf_unfiltered (file
, " %4d", regnum
);
1352 fprintf_unfiltered (file
, " %4d",
1353 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1357 fprintf_unfiltered (file
, " %6s ", "Offset");
1360 fprintf_unfiltered (file
, " %6ld",
1361 descr
->register_offset
[regnum
]);
1362 if (register_offset
!= descr
->register_offset
[regnum
]
1364 && (descr
->register_offset
[regnum
]
1365 != (descr
->register_offset
[regnum
- 1]
1366 + descr
->sizeof_register
[regnum
- 1])))
1369 if (!footnote_register_offset
)
1370 footnote_register_offset
= ++footnote_nr
;
1371 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1374 fprintf_unfiltered (file
, " ");
1375 register_offset
= (descr
->register_offset
[regnum
]
1376 + descr
->sizeof_register
[regnum
]);
1381 fprintf_unfiltered (file
, " %5s ", "Size");
1383 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1388 std::string name_holder
;
1394 static const char blt
[] = "builtin_type";
1396 t
= register_type (m_gdbarch
, regnum
)->name ();
1399 if (!footnote_register_type_name_null
)
1400 footnote_register_type_name_null
= ++footnote_nr
;
1401 name_holder
= string_printf ("*%d",
1402 footnote_register_type_name_null
);
1403 t
= name_holder
.c_str ();
1405 /* Chop a leading builtin_type. */
1406 if (startswith (t
, blt
))
1409 fprintf_unfiltered (file
, " %-15s", t
);
1412 /* Leading space always present. */
1413 fprintf_unfiltered (file
, " ");
1415 dump_reg (file
, regnum
);
1417 fprintf_unfiltered (file
, "\n");
1420 if (footnote_register_offset
)
1421 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1422 footnote_register_offset
);
1423 if (footnote_register_type_name_null
)
1424 fprintf_unfiltered (file
,
1425 "*%d: Register type's name NULL.\n",
1426 footnote_register_type_name_null
);
1430 #include "gdbsupport/selftest.h"
1431 #include "selftest-arch.h"
1432 #include "target-float.h"
1434 namespace selftests
{
1436 class regcache_access
: public regcache
1440 /* Return the number of elements in current_regcache. */
1443 current_regcache_size ()
1445 return std::distance (regcache::current_regcache
.begin (),
1446 regcache::current_regcache
.end ());
1450 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1453 test_get_thread_arch_aspace_regcache (process_stratum_target
*target
,
1454 ptid_t ptid
, struct gdbarch
*gdbarch
,
1455 address_space
*aspace
)
1457 struct regcache
*regcache
1458 = get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
1459 SELF_CHECK (regcache
!= NULL
);
1460 SELF_CHECK (regcache
->target () == target
);
1461 SELF_CHECK (regcache
->ptid () == ptid
);
1462 SELF_CHECK (regcache
->aspace () == aspace
);
1466 current_regcache_test (void)
1468 /* It is empty at the start. */
1469 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1471 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1473 test_target_ops test_target1
;
1474 test_target_ops test_target2
;
1476 /* Get regcache from (target1,ptid1), a new regcache is added to
1477 current_regcache. */
1478 test_get_thread_arch_aspace_regcache (&test_target1
, ptid1
,
1481 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1483 /* Get regcache from (target1,ptid2), a new regcache is added to
1484 current_regcache. */
1485 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1488 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1490 /* Get regcache from (target1,ptid3), a new regcache is added to
1491 current_regcache. */
1492 test_get_thread_arch_aspace_regcache (&test_target1
, ptid3
,
1495 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1497 /* Get regcache from (target1,ptid2) again, nothing is added to
1498 current_regcache. */
1499 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1502 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1504 /* Get regcache from (target2,ptid2), a new regcache is added to
1505 current_regcache, since this time we're using a differen
1507 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1510 SELF_CHECK (regcache_access::current_regcache_size () == 4);
1512 /* Mark that (target1,ptid2) changed. The regcache of (target1,
1513 ptid2) should be removed from current_regcache. */
1514 registers_changed_ptid (&test_target1
, ptid2
);
1515 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1517 /* Get the regcache from (target2,ptid2) again, confirming the
1518 registers_changed_ptid call above did not delete it. */
1519 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1522 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1524 /* Confirm that marking all regcaches of all targets as changed
1525 clears current_regcache. */
1526 registers_changed_ptid (nullptr, minus_one_ptid
);
1527 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1530 class target_ops_no_register
: public test_target_ops
1533 target_ops_no_register ()
1534 : test_target_ops
{}
1539 fetch_registers_called
= 0;
1540 store_registers_called
= 0;
1541 xfer_partial_called
= 0;
1544 void fetch_registers (regcache
*regs
, int regno
) override
;
1545 void store_registers (regcache
*regs
, int regno
) override
;
1547 enum target_xfer_status
xfer_partial (enum target_object object
,
1548 const char *annex
, gdb_byte
*readbuf
,
1549 const gdb_byte
*writebuf
,
1550 ULONGEST offset
, ULONGEST len
,
1551 ULONGEST
*xfered_len
) override
;
1553 unsigned int fetch_registers_called
= 0;
1554 unsigned int store_registers_called
= 0;
1555 unsigned int xfer_partial_called
= 0;
1559 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1561 /* Mark register available. */
1562 regs
->raw_supply_zeroed (regno
);
1563 this->fetch_registers_called
++;
1567 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1569 this->store_registers_called
++;
1572 enum target_xfer_status
1573 target_ops_no_register::xfer_partial (enum target_object object
,
1574 const char *annex
, gdb_byte
*readbuf
,
1575 const gdb_byte
*writebuf
,
1576 ULONGEST offset
, ULONGEST len
,
1577 ULONGEST
*xfered_len
)
1579 this->xfer_partial_called
++;
1582 return TARGET_XFER_OK
;
1585 class readwrite_regcache
: public regcache
1588 readwrite_regcache (process_stratum_target
*target
,
1589 struct gdbarch
*gdbarch
)
1590 : regcache (target
, gdbarch
, nullptr)
1594 /* Test regcache::cooked_read gets registers from raw registers and
1595 memory instead of target to_{fetch,store}_registers. */
1598 cooked_read_test (struct gdbarch
*gdbarch
)
1600 scoped_mock_context
<target_ops_no_register
> mockctx (gdbarch
);
1602 /* Test that read one raw register from regcache_no_target will go
1603 to the target layer. */
1605 /* Find a raw register which size isn't zero. */
1607 for (nonzero_regnum
= 0;
1608 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1611 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1615 readwrite_regcache
readwrite (&mockctx
.mock_target
, gdbarch
);
1616 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1618 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1620 /* raw_read calls target_fetch_registers. */
1621 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
> 0);
1622 mockctx
.mock_target
.reset ();
1624 /* Mark all raw registers valid, so the following raw registers
1625 accesses won't go to target. */
1626 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1627 readwrite
.raw_update (i
);
1629 mockctx
.mock_target
.reset ();
1630 /* Then, read all raw and pseudo registers, and don't expect calling
1631 to_{fetch,store}_registers. */
1632 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1634 if (register_size (gdbarch
, regnum
) == 0)
1637 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1639 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1640 inner_buf
.data ()));
1642 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1643 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1644 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1646 mockctx
.mock_target
.reset ();
1649 readonly_detached_regcache
readonly (readwrite
);
1651 /* GDB may go to target layer to fetch all registers and memory for
1652 readonly regcache. */
1653 mockctx
.mock_target
.reset ();
1655 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1657 if (register_size (gdbarch
, regnum
) == 0)
1660 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1661 enum register_status status
= readonly
.cooked_read (regnum
,
1664 if (regnum
< gdbarch_num_regs (gdbarch
))
1666 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1668 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1669 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1670 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1671 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1672 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1673 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1674 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1675 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1677 /* Raw registers. If raw registers are not in save_reggroup,
1678 their status are unknown. */
1679 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1680 SELF_CHECK (status
== REG_VALID
);
1682 SELF_CHECK (status
== REG_UNKNOWN
);
1685 SELF_CHECK (status
== REG_VALID
);
1689 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1690 SELF_CHECK (status
== REG_VALID
);
1693 /* If pseudo registers are not in save_reggroup, some of
1694 them can be computed from saved raw registers, but some
1695 of them are unknown. */
1696 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1698 if (bfd_arch
== bfd_arch_frv
1699 || bfd_arch
== bfd_arch_m32c
1700 || bfd_arch
== bfd_arch_mep
1701 || bfd_arch
== bfd_arch_sh
)
1702 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1703 else if (bfd_arch
== bfd_arch_mips
1704 || bfd_arch
== bfd_arch_h8300
)
1705 SELF_CHECK (status
== REG_UNKNOWN
);
1707 SELF_CHECK (status
== REG_VALID
);
1711 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1712 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1713 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1715 mockctx
.mock_target
.reset ();
1719 /* Test regcache::cooked_write by writing some expected contents to
1720 registers, and checking that contents read from registers and the
1721 expected contents are the same. */
1724 cooked_write_test (struct gdbarch
*gdbarch
)
1726 /* Error out if debugging something, because we're going to push the
1727 test target, which would pop any existing target. */
1728 if (current_top_target ()->stratum () >= process_stratum
)
1729 error (_("target already pushed"));
1731 /* Create a mock environment. A process_stratum target pushed. */
1733 target_ops_no_register mock_target
;
1735 /* Push the process_stratum target so we can mock accessing
1737 push_target (&mock_target
);
1739 /* Pop it again on exit (return/exception). */
1744 pop_all_targets_at_and_above (process_stratum
);
1748 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1750 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1752 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1754 if (register_size (gdbarch
, regnum
) == 0
1755 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1758 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1760 if (bfd_arch
== bfd_arch_sparc
1761 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1762 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1763 && gdbarch_ptr_bit (gdbarch
) == 64
1764 && (regnum
>= gdbarch_num_regs (gdbarch
)
1765 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1768 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1769 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1770 const auto type
= register_type (gdbarch
, regnum
);
1772 if (type
->code () == TYPE_CODE_FLT
1773 || type
->code () == TYPE_CODE_DECFLOAT
)
1775 /* Generate valid float format. */
1776 target_float_from_string (expected
.data (), type
, "1.25");
1778 else if (type
->code () == TYPE_CODE_INT
1779 || type
->code () == TYPE_CODE_ARRAY
1780 || type
->code () == TYPE_CODE_PTR
1781 || type
->code () == TYPE_CODE_UNION
1782 || type
->code () == TYPE_CODE_STRUCT
)
1784 if (bfd_arch
== bfd_arch_ia64
1785 || (regnum
>= gdbarch_num_regs (gdbarch
)
1786 && (bfd_arch
== bfd_arch_xtensa
1787 || bfd_arch
== bfd_arch_bfin
1788 || bfd_arch
== bfd_arch_m32c
1789 /* m68hc11 pseudo registers are in memory. */
1790 || bfd_arch
== bfd_arch_m68hc11
1791 || bfd_arch
== bfd_arch_m68hc12
1792 || bfd_arch
== bfd_arch_s390
))
1793 || (bfd_arch
== bfd_arch_frv
1794 /* FRV pseudo registers except iacc0. */
1795 && regnum
> gdbarch_num_regs (gdbarch
)))
1797 /* Skip setting the expected values for some architecture
1800 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1802 /* RL78_PC_REGNUM */
1803 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1808 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1812 else if (type
->code () == TYPE_CODE_FLAGS
)
1814 /* No idea how to test flags. */
1819 /* If we don't know how to create the expected value for the
1820 this type, make it fail. */
1824 readwrite
.cooked_write (regnum
, expected
.data ());
1826 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1827 SELF_CHECK (expected
== buf
);
1831 } // namespace selftests
1832 #endif /* GDB_SELF_TEST */
1834 void _initialize_regcache ();
1836 _initialize_regcache ()
1838 regcache_descr_handle
1839 = gdbarch_data_register_post_init (init_regcache_descr
);
1841 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1842 gdb::observers::thread_ptid_changed
.attach
1843 (regcache::regcache_thread_ptid_changed
);
1845 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1846 _("Force gdb to flush its register cache (maintainer command)."));
1849 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
1851 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1852 selftests::cooked_read_test
);
1853 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1854 selftests::cooked_write_test
);