1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "reggroups.h"
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_regs (gdbarch
)
91 + gdbarch_num_pseudo_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
];
122 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
124 /* Set the real size of the raw register cache buffer. */
125 descr
->sizeof_raw_registers
= offset
;
127 for (; i
< descr
->nr_cooked_registers
; i
++)
129 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
130 descr
->register_offset
[i
] = offset
;
131 offset
+= descr
->sizeof_register
[i
];
132 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
134 /* Set the real size of the readonly register cache buffer. */
135 descr
->sizeof_cooked_registers
= offset
;
141 static struct regcache_descr
*
142 regcache_descr (struct gdbarch
*gdbarch
)
144 return (struct regcache_descr
*) gdbarch_data (gdbarch
,
145 regcache_descr_handle
);
148 /* Utility functions returning useful register attributes stored in
149 the regcache descr. */
152 register_type (struct gdbarch
*gdbarch
, int regnum
)
154 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
156 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
157 return descr
->register_type
[regnum
];
160 /* Utility functions returning useful register attributes stored in
161 the regcache descr. */
164 register_size (struct gdbarch
*gdbarch
, int regnum
)
166 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
169 gdb_assert (regnum
>= 0
170 && regnum
< (gdbarch_num_regs (gdbarch
)
171 + gdbarch_num_pseudo_regs (gdbarch
)));
172 size
= descr
->sizeof_register
[regnum
];
176 /* See common/common-regcache.h. */
179 regcache_register_size (const struct regcache
*regcache
, int n
)
181 return register_size (regcache
->arch (), n
);
184 reg_buffer::reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
)
185 : m_has_pseudo (has_pseudo
)
187 gdb_assert (gdbarch
!= NULL
);
188 m_descr
= regcache_descr (gdbarch
);
192 m_registers
= XCNEWVEC (gdb_byte
, m_descr
->sizeof_cooked_registers
);
193 m_register_status
= XCNEWVEC (signed char,
194 m_descr
->nr_cooked_registers
);
198 m_registers
= XCNEWVEC (gdb_byte
, m_descr
->sizeof_raw_registers
);
199 m_register_status
= XCNEWVEC (signed char, gdbarch_num_regs (gdbarch
));
203 regcache::regcache (gdbarch
*gdbarch
, const address_space
*aspace_
)
204 /* The register buffers. A read/write register cache can only hold
205 [0 .. gdbarch_num_regs). */
206 : detached_regcache (gdbarch
, false), m_aspace (aspace_
)
208 m_ptid
= minus_one_ptid
;
211 static enum register_status
212 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
214 struct regcache
*regcache
= (struct regcache
*) src
;
216 return regcache_cooked_read (regcache
, regnum
, buf
);
219 readonly_detached_regcache::readonly_detached_regcache (const regcache
&src
)
220 : readonly_detached_regcache (src
.arch (), do_cooked_read
, (void *) &src
)
225 reg_buffer::arch () const
227 return m_descr
->gdbarch
;
230 /* See regcache.h. */
233 regcache_get_ptid (const struct regcache
*regcache
)
235 gdb_assert (!ptid_equal (regcache
->ptid (), minus_one_ptid
));
237 return regcache
->ptid ();
240 /* Cleanup class for invalidating a register. */
242 class regcache_invalidator
246 regcache_invalidator (struct regcache
*regcache
, int regnum
)
247 : m_regcache (regcache
),
252 ~regcache_invalidator ()
254 if (m_regcache
!= nullptr)
255 regcache_invalidate (m_regcache
, m_regnum
);
258 DISABLE_COPY_AND_ASSIGN (regcache_invalidator
);
262 m_regcache
= nullptr;
267 struct regcache
*m_regcache
;
271 /* Return a pointer to register REGNUM's buffer cache. */
274 reg_buffer::register_buffer (int regnum
) const
276 return m_registers
+ m_descr
->register_offset
[regnum
];
280 reg_buffer::save (regcache_cooked_read_ftype
*cooked_read
,
283 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
286 /* It should have pseudo registers. */
287 gdb_assert (m_has_pseudo
);
288 /* Clear the dest. */
289 memset (m_registers
, 0, m_descr
->sizeof_cooked_registers
);
290 memset (m_register_status
, 0, m_descr
->nr_cooked_registers
);
291 /* Copy over any registers (identified by their membership in the
292 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
293 gdbarch_num_pseudo_regs) range is checked since some architectures need
294 to save/restore `cooked' registers that live in memory. */
295 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
297 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
299 gdb_byte
*dst_buf
= register_buffer (regnum
);
300 enum register_status status
= cooked_read (src
, regnum
, dst_buf
);
302 gdb_assert (status
!= REG_UNKNOWN
);
304 if (status
!= REG_VALID
)
305 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
307 m_register_status
[regnum
] = status
;
313 regcache::restore (readonly_detached_regcache
*src
)
315 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
318 gdb_assert (src
!= NULL
);
319 gdb_assert (src
->m_has_pseudo
);
321 gdb_assert (gdbarch
== src
->arch ());
323 /* Copy over any registers, being careful to only restore those that
324 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
325 + gdbarch_num_pseudo_regs) range is checked since some architectures need
326 to save/restore `cooked' registers that live in memory. */
327 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
329 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
331 if (src
->m_register_status
[regnum
] == REG_VALID
)
332 cooked_write (regnum
, src
->register_buffer (regnum
));
338 regcache_register_status (const struct regcache
*regcache
, int regnum
)
340 gdb_assert (regcache
!= NULL
);
341 return regcache
->get_register_status (regnum
);
345 reg_buffer::get_register_status (int regnum
) const
347 assert_regnum (regnum
);
349 return (enum register_status
) m_register_status
[regnum
];
353 regcache_invalidate (struct regcache
*regcache
, int regnum
)
355 gdb_assert (regcache
!= NULL
);
356 regcache
->invalidate (regnum
);
360 detached_regcache::invalidate (int regnum
)
362 assert_regnum (regnum
);
363 m_register_status
[regnum
] = REG_UNKNOWN
;
367 reg_buffer::assert_regnum (int regnum
) const
369 gdb_assert (regnum
>= 0);
371 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
373 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
376 /* Global structure containing the current regcache. */
378 /* NOTE: this is a write-through cache. There is no "dirty" bit for
379 recording if the register values have been changed (eg. by the
380 user). Therefore all registers must be written back to the
381 target when appropriate. */
382 std::forward_list
<regcache
*> regcache::current_regcache
;
385 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
386 struct address_space
*aspace
)
388 for (const auto ®cache
: regcache::current_regcache
)
389 if (ptid_equal (regcache
->ptid (), ptid
) && regcache
->arch () == gdbarch
)
392 regcache
*new_regcache
= new regcache (gdbarch
, aspace
);
394 regcache::current_regcache
.push_front (new_regcache
);
395 new_regcache
->set_ptid (ptid
);
401 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
403 address_space
*aspace
= target_thread_address_space (ptid
);
405 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
408 static ptid_t current_thread_ptid
;
409 static struct gdbarch
*current_thread_arch
;
412 get_thread_regcache (ptid_t ptid
)
414 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
416 current_thread_ptid
= ptid
;
417 current_thread_arch
= target_thread_architecture (ptid
);
420 return get_thread_arch_regcache (ptid
, current_thread_arch
);
424 get_current_regcache (void)
426 return get_thread_regcache (inferior_ptid
);
429 /* See common/common-regcache.h. */
432 get_thread_regcache_for_ptid (ptid_t ptid
)
434 return get_thread_regcache (ptid
);
437 /* Observer for the target_changed event. */
440 regcache_observer_target_changed (struct target_ops
*target
)
442 registers_changed ();
445 /* Update global variables old ptids to hold NEW_PTID if they were
448 regcache::regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
450 for (auto ®cache
: regcache::current_regcache
)
452 if (ptid_equal (regcache
->ptid (), old_ptid
))
453 regcache
->set_ptid (new_ptid
);
457 /* Low level examining and depositing of registers.
459 The caller is responsible for making sure that the inferior is
460 stopped before calling the fetching routines, or it will get
461 garbage. (a change from GDB version 3, in which the caller got the
462 value from the last stop). */
464 /* REGISTERS_CHANGED ()
466 Indicate that registers may have changed, so invalidate the cache. */
469 registers_changed_ptid (ptid_t ptid
)
471 for (auto oit
= regcache::current_regcache
.before_begin (),
472 it
= std::next (oit
);
473 it
!= regcache::current_regcache
.end ();
476 if (ptid_match ((*it
)->ptid (), ptid
))
479 it
= regcache::current_regcache
.erase_after (oit
);
485 if (ptid_match (current_thread_ptid
, ptid
))
487 current_thread_ptid
= null_ptid
;
488 current_thread_arch
= NULL
;
491 if (ptid_match (inferior_ptid
, ptid
))
493 /* We just deleted the regcache of the current thread. Need to
494 forget about any frames we have cached, too. */
495 reinit_frame_cache ();
500 registers_changed (void)
502 registers_changed_ptid (minus_one_ptid
);
504 /* Force cleanup of any alloca areas if using C alloca instead of
505 a builtin alloca. This particular call is used to clean up
506 areas allocated by low level target code which may build up
507 during lengthy interactions between gdb and the target before
508 gdb gives control to the user (ie watchpoints). */
513 regcache_raw_update (struct regcache
*regcache
, int regnum
)
515 gdb_assert (regcache
!= NULL
);
517 regcache
->raw_update (regnum
);
521 regcache::raw_update (int regnum
)
523 assert_regnum (regnum
);
525 /* Make certain that the register cache is up-to-date with respect
526 to the current thread. This switching shouldn't be necessary
527 only there is still only one target side register cache. Sigh!
528 On the bright side, at least there is a regcache object. */
530 if (get_register_status (regnum
) == REG_UNKNOWN
)
532 target_fetch_registers (this, regnum
);
534 /* A number of targets can't access the whole set of raw
535 registers (because the debug API provides no means to get at
537 if (m_register_status
[regnum
] == REG_UNKNOWN
)
538 m_register_status
[regnum
] = REG_UNAVAILABLE
;
543 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
545 return regcache
->raw_read (regnum
, buf
);
549 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
551 gdb_assert (buf
!= NULL
);
554 if (m_register_status
[regnum
] != REG_VALID
)
555 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
557 memcpy (buf
, register_buffer (regnum
),
558 m_descr
->sizeof_register
[regnum
]);
560 return (enum register_status
) m_register_status
[regnum
];
564 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
566 gdb_assert (regcache
!= NULL
);
567 return regcache
->raw_read (regnum
, val
);
570 template<typename T
, typename
>
572 readable_regcache::raw_read (int regnum
, T
*val
)
575 enum register_status status
;
577 assert_regnum (regnum
);
578 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
579 status
= raw_read (regnum
, buf
);
580 if (status
== REG_VALID
)
581 *val
= extract_integer
<T
> (buf
,
582 m_descr
->sizeof_register
[regnum
],
583 gdbarch_byte_order (m_descr
->gdbarch
));
590 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
593 gdb_assert (regcache
!= NULL
);
594 return regcache
->raw_read (regnum
, val
);
598 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
600 gdb_assert (regcache
!= NULL
);
601 regcache
->raw_write (regnum
, val
);
604 template<typename T
, typename
>
606 regcache::raw_write (int regnum
, T val
)
610 assert_regnum (regnum
);
611 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
612 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
613 gdbarch_byte_order (m_descr
->gdbarch
), val
);
614 raw_write (regnum
, buf
);
618 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
621 gdb_assert (regcache
!= NULL
);
622 regcache
->raw_write (regnum
, val
);
626 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
629 enum register_status status
;
631 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
632 if (status
== REG_UNAVAILABLE
)
633 throw_error (NOT_AVAILABLE_ERROR
,
634 _("Register %d is not available"), regnum
);
639 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
641 return regcache
->cooked_read (regnum
, buf
);
645 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
647 gdb_assert (regnum
>= 0);
648 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
649 if (regnum
< num_raw_registers ())
650 return raw_read (regnum
, buf
);
651 else if (m_has_pseudo
652 && m_register_status
[regnum
] != REG_UNKNOWN
)
654 if (m_register_status
[regnum
] == REG_VALID
)
655 memcpy (buf
, register_buffer (regnum
),
656 m_descr
->sizeof_register
[regnum
]);
658 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
660 return (enum register_status
) m_register_status
[regnum
];
662 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
664 struct value
*mark
, *computed
;
665 enum register_status result
= REG_VALID
;
667 mark
= value_mark ();
669 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
671 if (value_entirely_available (computed
))
672 memcpy (buf
, value_contents_raw (computed
),
673 m_descr
->sizeof_register
[regnum
]);
676 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
677 result
= REG_UNAVAILABLE
;
680 value_free_to_mark (mark
);
685 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
690 regcache_cooked_read_value (struct regcache
*regcache
, int regnum
)
692 return regcache
->cooked_read_value (regnum
);
696 readable_regcache::cooked_read_value (int regnum
)
698 gdb_assert (regnum
>= 0);
699 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
701 if (regnum
< num_raw_registers ()
702 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
703 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
705 struct value
*result
;
707 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
708 VALUE_LVAL (result
) = lval_register
;
709 VALUE_REGNUM (result
) = regnum
;
711 /* It is more efficient in general to do this delegation in this
712 direction than in the other one, even though the value-based
714 if (cooked_read (regnum
,
715 value_contents_raw (result
)) == REG_UNAVAILABLE
)
716 mark_value_bytes_unavailable (result
, 0,
717 TYPE_LENGTH (value_type (result
)));
722 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
727 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
730 gdb_assert (regcache
!= NULL
);
731 return regcache
->cooked_read (regnum
, val
);
734 template<typename T
, typename
>
736 readable_regcache::cooked_read (int regnum
, T
*val
)
738 enum register_status status
;
741 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
742 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
743 status
= cooked_read (regnum
, buf
);
744 if (status
== REG_VALID
)
745 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
746 gdbarch_byte_order (m_descr
->gdbarch
));
753 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
756 gdb_assert (regcache
!= NULL
);
757 return regcache
->cooked_read (regnum
, val
);
761 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
764 gdb_assert (regcache
!= NULL
);
765 regcache
->cooked_write (regnum
, val
);
768 template<typename T
, typename
>
770 regcache::cooked_write (int regnum
, T val
)
774 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
775 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
776 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
777 gdbarch_byte_order (m_descr
->gdbarch
), val
);
778 cooked_write (regnum
, buf
);
782 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
785 gdb_assert (regcache
!= NULL
);
786 regcache
->cooked_write (regnum
, val
);
790 regcache_raw_write (struct regcache
*regcache
, int regnum
,
793 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
794 regcache
->raw_write (regnum
, buf
);
798 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
801 gdb_assert (buf
!= NULL
);
802 assert_regnum (regnum
);
804 /* On the sparc, writing %g0 is a no-op, so we don't even want to
805 change the registers array if something writes to this register. */
806 if (gdbarch_cannot_store_register (arch (), regnum
))
809 /* If we have a valid copy of the register, and new value == old
810 value, then don't bother doing the actual store. */
811 if (get_register_status (regnum
) == REG_VALID
812 && (memcmp (register_buffer (regnum
), buf
,
813 m_descr
->sizeof_register
[regnum
]) == 0))
816 target_prepare_to_store (this);
817 raw_supply (regnum
, buf
);
819 /* Invalidate the register after it is written, in case of a
821 regcache_invalidator
invalidator (this, regnum
);
823 target_store_registers (this, regnum
);
825 /* The target did not throw an error so we can discard invalidating
827 invalidator
.release ();
831 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
834 regcache
->cooked_write (regnum
, buf
);
838 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
840 gdb_assert (regnum
>= 0);
841 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
842 if (regnum
< num_raw_registers ())
843 raw_write (regnum
, buf
);
845 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
849 /* Perform a partial register transfer using a read, modify, write
852 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
854 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
858 readable_regcache::read_part (int regnum
, int offset
, int len
, void *in
,
861 struct gdbarch
*gdbarch
= arch ();
862 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
864 gdb_assert (in
!= NULL
);
865 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
866 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
867 /* Something to do? */
868 if (offset
+ len
== 0)
870 /* Read (when needed) ... */
871 enum register_status status
;
874 status
= raw_read (regnum
, reg
);
876 status
= cooked_read (regnum
, reg
);
877 if (status
!= REG_VALID
)
881 memcpy (in
, reg
+ offset
, len
);
887 regcache::write_part (int regnum
, int offset
, int len
,
888 const void *out
, bool is_raw
)
890 struct gdbarch
*gdbarch
= arch ();
891 gdb_byte
*reg
= (gdb_byte
*) alloca (register_size (gdbarch
, regnum
));
893 gdb_assert (out
!= NULL
);
894 gdb_assert (offset
>= 0 && offset
<= m_descr
->sizeof_register
[regnum
]);
895 gdb_assert (len
>= 0 && offset
+ len
<= m_descr
->sizeof_register
[regnum
]);
896 /* Something to do? */
897 if (offset
+ len
== 0)
899 /* Read (when needed) ... */
901 || offset
+ len
< m_descr
->sizeof_register
[regnum
])
903 enum register_status status
;
906 status
= raw_read (regnum
, reg
);
908 status
= cooked_read (regnum
, reg
);
909 if (status
!= REG_VALID
)
913 memcpy (reg
+ offset
, out
, len
);
914 /* ... write (when needed). */
916 raw_write (regnum
, reg
);
918 cooked_write (regnum
, reg
);
924 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
925 int offset
, int len
, gdb_byte
*buf
)
927 return regcache
->raw_read_part (regnum
, offset
, len
, buf
);
931 readable_regcache::raw_read_part (int regnum
, int offset
, int len
, gdb_byte
*buf
)
933 assert_regnum (regnum
);
934 return read_part (regnum
, offset
, len
, buf
, true);
938 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
939 int offset
, int len
, const gdb_byte
*buf
)
941 regcache
->raw_write_part (regnum
, offset
, len
, buf
);
945 regcache::raw_write_part (int regnum
, int offset
, int len
,
948 assert_regnum (regnum
);
949 write_part (regnum
, offset
, len
, buf
, true);
953 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
954 int offset
, int len
, gdb_byte
*buf
)
956 return regcache
->cooked_read_part (regnum
, offset
, len
, buf
);
961 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
964 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
965 return read_part (regnum
, offset
, len
, buf
, false);
969 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
970 int offset
, int len
, const gdb_byte
*buf
)
972 regcache
->cooked_write_part (regnum
, offset
, len
, buf
);
976 regcache::cooked_write_part (int regnum
, int offset
, int len
,
979 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
980 write_part (regnum
, offset
, len
, buf
, false);
983 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
986 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
988 gdb_assert (regcache
!= NULL
);
989 regcache
->raw_supply (regnum
, buf
);
993 detached_regcache::raw_supply (int regnum
, const void *buf
)
998 assert_regnum (regnum
);
1000 regbuf
= register_buffer (regnum
);
1001 size
= m_descr
->sizeof_register
[regnum
];
1005 memcpy (regbuf
, buf
, size
);
1006 m_register_status
[regnum
] = REG_VALID
;
1010 /* This memset not strictly necessary, but better than garbage
1011 in case the register value manages to escape somewhere (due
1012 to a bug, no less). */
1013 memset (regbuf
, 0, size
);
1014 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1018 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1019 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1020 the register size is greater than ADDR_LEN, then the integer will be sign or
1021 zero extended. If the register size is smaller than the integer, then the
1022 most significant bytes of the integer will be truncated. */
1025 detached_regcache::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1026 int addr_len
, bool is_signed
)
1028 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1032 assert_regnum (regnum
);
1034 regbuf
= register_buffer (regnum
);
1035 regsize
= m_descr
->sizeof_register
[regnum
];
1037 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1039 m_register_status
[regnum
] = REG_VALID
;
1042 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1043 as calling raw_supply with NULL (which will set the state to
1047 detached_regcache::raw_supply_zeroed (int regnum
)
1052 assert_regnum (regnum
);
1054 regbuf
= register_buffer (regnum
);
1055 size
= m_descr
->sizeof_register
[regnum
];
1057 memset (regbuf
, 0, size
);
1058 m_register_status
[regnum
] = REG_VALID
;
1061 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1064 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
1066 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
1067 regcache
->raw_collect (regnum
, buf
);
1071 regcache::raw_collect (int regnum
, void *buf
) const
1076 gdb_assert (buf
!= NULL
);
1077 assert_regnum (regnum
);
1079 regbuf
= register_buffer (regnum
);
1080 size
= m_descr
->sizeof_register
[regnum
];
1081 memcpy (buf
, regbuf
, size
);
1084 /* Transfer a single or all registers belonging to a certain register
1085 set to or from a buffer. This is the main worker function for
1086 regcache_supply_regset and regcache_collect_regset. */
1088 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1089 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1090 If ADDR_LEN is greater than the register size, then the integer will be sign
1091 or zero extended. If ADDR_LEN is smaller than the register size, then the
1092 most significant bytes of the integer will be truncated. */
1095 regcache::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1096 bool is_signed
) const
1098 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1099 const gdb_byte
*regbuf
;
1102 assert_regnum (regnum
);
1104 regbuf
= register_buffer (regnum
);
1105 regsize
= m_descr
->sizeof_register
[regnum
];
1107 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1112 regcache::transfer_regset (const struct regset
*regset
,
1113 struct regcache
*out_regcache
,
1114 int regnum
, const void *in_buf
,
1115 void *out_buf
, size_t size
) const
1117 const struct regcache_map_entry
*map
;
1118 int offs
= 0, count
;
1120 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1121 (count
= map
->count
) != 0;
1124 int regno
= map
->regno
;
1125 int slot_size
= map
->size
;
1127 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1128 slot_size
= m_descr
->sizeof_register
[regno
];
1130 if (regno
== REGCACHE_MAP_SKIP
1132 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1133 offs
+= count
* slot_size
;
1135 else if (regnum
== -1)
1136 for (; count
--; regno
++, offs
+= slot_size
)
1138 if (offs
+ slot_size
> size
)
1142 raw_collect (regno
, (gdb_byte
*) out_buf
+ offs
);
1144 out_regcache
->raw_supply (regno
, in_buf
1145 ? (const gdb_byte
*) in_buf
+ offs
1150 /* Transfer a single register and return. */
1151 offs
+= (regnum
- regno
) * slot_size
;
1152 if (offs
+ slot_size
> size
)
1156 raw_collect (regnum
, (gdb_byte
*) out_buf
+ offs
);
1158 out_regcache
->raw_supply (regnum
, in_buf
1159 ? (const gdb_byte
*) in_buf
+ offs
1166 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1167 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1168 If BUF is NULL, set the register(s) to "unavailable" status. */
1171 regcache_supply_regset (const struct regset
*regset
,
1172 struct regcache
*regcache
,
1173 int regnum
, const void *buf
, size_t size
)
1175 regcache
->supply_regset (regset
, regnum
, buf
, size
);
1179 regcache::supply_regset (const struct regset
*regset
,
1180 int regnum
, const void *buf
, size_t size
)
1182 transfer_regset (regset
, this, regnum
, buf
, NULL
, size
);
1185 /* Collect register REGNUM from REGCACHE to BUF, using the register
1186 map in REGSET. If REGNUM is -1, do this for all registers in
1190 regcache_collect_regset (const struct regset
*regset
,
1191 const struct regcache
*regcache
,
1192 int regnum
, void *buf
, size_t size
)
1194 regcache
->collect_regset (regset
, regnum
, buf
, size
);
1198 regcache::collect_regset (const struct regset
*regset
,
1199 int regnum
, void *buf
, size_t size
) const
1201 transfer_regset (regset
, NULL
, regnum
, NULL
, buf
, size
);
1205 /* Special handling for register PC. */
1208 regcache_read_pc (struct regcache
*regcache
)
1210 struct gdbarch
*gdbarch
= regcache
->arch ();
1214 if (gdbarch_read_pc_p (gdbarch
))
1215 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1216 /* Else use per-frame method on get_current_frame. */
1217 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1221 if (regcache_cooked_read_unsigned (regcache
,
1222 gdbarch_pc_regnum (gdbarch
),
1223 &raw_val
) == REG_UNAVAILABLE
)
1224 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1226 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1229 internal_error (__FILE__
, __LINE__
,
1230 _("regcache_read_pc: Unable to find PC"));
1235 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1237 struct gdbarch
*gdbarch
= regcache
->arch ();
1239 if (gdbarch_write_pc_p (gdbarch
))
1240 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1241 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1242 regcache_cooked_write_unsigned (regcache
,
1243 gdbarch_pc_regnum (gdbarch
), pc
);
1245 internal_error (__FILE__
, __LINE__
,
1246 _("regcache_write_pc: Unable to update PC"));
1248 /* Writing the PC (for instance, from "load") invalidates the
1250 reinit_frame_cache ();
1254 reg_buffer::num_raw_registers () const
1256 return gdbarch_num_regs (arch ());
1260 regcache::debug_print_register (const char *func
, int regno
)
1262 struct gdbarch
*gdbarch
= arch ();
1264 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1265 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1266 && gdbarch_register_name (gdbarch
, regno
) != NULL
1267 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1268 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1269 gdbarch_register_name (gdbarch
, regno
));
1271 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1272 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1274 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1275 int size
= register_size (gdbarch
, regno
);
1276 gdb_byte
*buf
= register_buffer (regno
);
1278 fprintf_unfiltered (gdb_stdlog
, " = ");
1279 for (int i
= 0; i
< size
; i
++)
1281 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1283 if (size
<= sizeof (LONGEST
))
1285 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1287 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1288 core_addr_to_string_nz (val
), plongest (val
));
1291 fprintf_unfiltered (gdb_stdlog
, "\n");
1295 reg_flush_command (const char *command
, int from_tty
)
1297 /* Force-flush the register cache. */
1298 registers_changed ();
1300 printf_filtered (_("Register cache flushed.\n"));
1303 /* An abstract base class for register dump. */
1308 void dump (ui_file
*file
)
1310 auto descr
= regcache_descr (m_gdbarch
);
1312 int footnote_nr
= 0;
1313 int footnote_register_offset
= 0;
1314 int footnote_register_type_name_null
= 0;
1315 long register_offset
= 0;
1317 gdb_assert (descr
->nr_cooked_registers
1318 == (gdbarch_num_regs (m_gdbarch
)
1319 + gdbarch_num_pseudo_regs (m_gdbarch
)));
1321 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1325 fprintf_unfiltered (file
, " %-10s", "Name");
1328 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1332 else if (p
[0] == '\0')
1334 fprintf_unfiltered (file
, " %-10s", p
);
1339 fprintf_unfiltered (file
, " %4s", "Nr");
1341 fprintf_unfiltered (file
, " %4d", regnum
);
1343 /* Relative number. */
1345 fprintf_unfiltered (file
, " %4s", "Rel");
1346 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1347 fprintf_unfiltered (file
, " %4d", regnum
);
1349 fprintf_unfiltered (file
, " %4d",
1350 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1354 fprintf_unfiltered (file
, " %6s ", "Offset");
1357 fprintf_unfiltered (file
, " %6ld",
1358 descr
->register_offset
[regnum
]);
1359 if (register_offset
!= descr
->register_offset
[regnum
]
1361 && (descr
->register_offset
[regnum
]
1362 != (descr
->register_offset
[regnum
- 1]
1363 + descr
->sizeof_register
[regnum
- 1])))
1366 if (!footnote_register_offset
)
1367 footnote_register_offset
= ++footnote_nr
;
1368 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1371 fprintf_unfiltered (file
, " ");
1372 register_offset
= (descr
->register_offset
[regnum
]
1373 + descr
->sizeof_register
[regnum
]);
1378 fprintf_unfiltered (file
, " %5s ", "Size");
1380 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1385 std::string name_holder
;
1391 static const char blt
[] = "builtin_type";
1393 t
= TYPE_NAME (register_type (m_gdbarch
, regnum
));
1396 if (!footnote_register_type_name_null
)
1397 footnote_register_type_name_null
= ++footnote_nr
;
1398 name_holder
= string_printf ("*%d",
1399 footnote_register_type_name_null
);
1400 t
= name_holder
.c_str ();
1402 /* Chop a leading builtin_type. */
1403 if (startswith (t
, blt
))
1406 fprintf_unfiltered (file
, " %-15s", t
);
1409 /* Leading space always present. */
1410 fprintf_unfiltered (file
, " ");
1412 dump_reg (file
, regnum
);
1414 fprintf_unfiltered (file
, "\n");
1417 if (footnote_register_offset
)
1418 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1419 footnote_register_offset
);
1420 if (footnote_register_type_name_null
)
1421 fprintf_unfiltered (file
,
1422 "*%d: Register type's name NULL.\n",
1423 footnote_register_type_name_null
);
1426 virtual ~register_dump () {};
1429 register_dump (gdbarch
*arch
)
1433 /* Dump the register REGNUM contents. If REGNUM is -1, print the
1435 virtual void dump_reg (ui_file
*file
, int regnum
) = 0;
1440 /* Dump registers from regcache, used for dump raw registers and
1441 cooked registers. */
1443 class register_dump_regcache
: public register_dump
1446 register_dump_regcache (regcache
*regcache
, bool dump_pseudo
)
1447 : register_dump (regcache
->arch ()), m_regcache (regcache
),
1448 m_dump_pseudo (dump_pseudo
)
1453 void dump_reg (ui_file
*file
, int regnum
) override
1458 fprintf_unfiltered (file
, "Cooked value");
1460 fprintf_unfiltered (file
, "Raw value");
1464 if (regnum
< gdbarch_num_regs (m_gdbarch
) || m_dump_pseudo
)
1466 auto size
= register_size (m_gdbarch
, regnum
);
1471 gdb::def_vector
<gdb_byte
> buf (size
);
1472 auto status
= m_regcache
->cooked_read (regnum
, buf
.data ());
1474 if (status
== REG_UNKNOWN
)
1475 fprintf_unfiltered (file
, "<invalid>");
1476 else if (status
== REG_UNAVAILABLE
)
1477 fprintf_unfiltered (file
, "<unavailable>");
1480 print_hex_chars (file
, buf
.data (), size
,
1481 gdbarch_byte_order (m_gdbarch
), true);
1486 /* Just print "<cooked>" for pseudo register when
1487 regcache_dump_raw. */
1488 fprintf_unfiltered (file
, "<cooked>");
1494 regcache
*m_regcache
;
1496 /* Dump pseudo registers or not. */
1497 const bool m_dump_pseudo
;
1500 /* Dump from reg_buffer, used when there is no thread or
1503 class register_dump_reg_buffer
: public register_dump
, reg_buffer
1506 register_dump_reg_buffer (gdbarch
*gdbarch
, bool dump_pseudo
)
1507 : register_dump (gdbarch
), reg_buffer (gdbarch
, dump_pseudo
)
1512 void dump_reg (ui_file
*file
, int regnum
) override
1517 fprintf_unfiltered (file
, "Cooked value");
1519 fprintf_unfiltered (file
, "Raw value");
1523 if (regnum
< gdbarch_num_regs (m_gdbarch
) || m_has_pseudo
)
1525 auto size
= register_size (m_gdbarch
, regnum
);
1530 auto status
= get_register_status (regnum
);
1532 gdb_assert (status
!= REG_VALID
);
1534 if (status
== REG_UNKNOWN
)
1535 fprintf_unfiltered (file
, "<invalid>");
1537 fprintf_unfiltered (file
, "<unavailable>");
1541 /* Just print "<cooked>" for pseudo register when
1542 regcache_dump_raw. */
1543 fprintf_unfiltered (file
, "<cooked>");
1549 /* For "maint print registers". */
1551 class register_dump_none
: public register_dump
1554 register_dump_none (gdbarch
*arch
)
1555 : register_dump (arch
)
1559 void dump_reg (ui_file
*file
, int regnum
) override
1563 /* For "maint print remote-registers". */
1565 class register_dump_remote
: public register_dump
1568 register_dump_remote (gdbarch
*arch
)
1569 : register_dump (arch
)
1573 void dump_reg (ui_file
*file
, int regnum
) override
1577 fprintf_unfiltered (file
, "Rmt Nr g/G Offset");
1579 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1583 if (remote_register_number_and_offset (m_gdbarch
, regnum
,
1585 fprintf_unfiltered (file
, "%7d %11d", pnum
, poffset
);
1590 /* For "maint print register-groups". */
1592 class register_dump_groups
: public register_dump
1595 register_dump_groups (gdbarch
*arch
)
1596 : register_dump (arch
)
1600 void dump_reg (ui_file
*file
, int regnum
) override
1603 fprintf_unfiltered (file
, "Groups");
1606 const char *sep
= "";
1607 struct reggroup
*group
;
1609 for (group
= reggroup_next (m_gdbarch
, NULL
);
1611 group
= reggroup_next (m_gdbarch
, group
))
1613 if (gdbarch_register_reggroup_p (m_gdbarch
, regnum
, group
))
1615 fprintf_unfiltered (file
,
1616 "%s%s", sep
, reggroup_name (group
));
1624 enum regcache_dump_what
1626 regcache_dump_none
, regcache_dump_raw
,
1627 regcache_dump_cooked
, regcache_dump_groups
,
1628 regcache_dump_remote
1632 regcache_print (const char *args
, enum regcache_dump_what what_to_dump
)
1634 /* Where to send output. */
1642 if (!file
.open (args
, "w"))
1643 perror_with_name (_("maintenance print architecture"));
1647 std::unique_ptr
<register_dump
> dump
;
1648 std::unique_ptr
<regcache
> regs
;
1651 if (target_has_registers
)
1652 gdbarch
= get_current_regcache ()->arch ();
1654 gdbarch
= target_gdbarch ();
1656 switch (what_to_dump
)
1658 case regcache_dump_none
:
1659 dump
.reset (new register_dump_none (gdbarch
));
1661 case regcache_dump_remote
:
1662 dump
.reset (new register_dump_remote (gdbarch
));
1664 case regcache_dump_groups
:
1665 dump
.reset (new register_dump_groups (gdbarch
));
1667 case regcache_dump_raw
:
1668 case regcache_dump_cooked
:
1670 auto dump_pseudo
= (what_to_dump
== regcache_dump_cooked
);
1672 if (target_has_registers
)
1673 dump
.reset (new register_dump_regcache (get_current_regcache (),
1677 /* For the benefit of "maint print registers" & co when
1678 debugging an executable, allow dumping a regcache even when
1679 there is no thread selected / no registers. */
1680 dump
.reset (new register_dump_reg_buffer (target_gdbarch (),
1691 maintenance_print_registers (const char *args
, int from_tty
)
1693 regcache_print (args
, regcache_dump_none
);
1697 maintenance_print_raw_registers (const char *args
, int from_tty
)
1699 regcache_print (args
, regcache_dump_raw
);
1703 maintenance_print_cooked_registers (const char *args
, int from_tty
)
1705 regcache_print (args
, regcache_dump_cooked
);
1709 maintenance_print_register_groups (const char *args
, int from_tty
)
1711 regcache_print (args
, regcache_dump_groups
);
1715 maintenance_print_remote_registers (const char *args
, int from_tty
)
1717 regcache_print (args
, regcache_dump_remote
);
1721 #include "selftest.h"
1722 #include "selftest-arch.h"
1723 #include "gdbthread.h"
1724 #include "target-float.h"
1726 namespace selftests
{
1728 class regcache_access
: public regcache
1732 /* Return the number of elements in current_regcache. */
1735 current_regcache_size ()
1737 return std::distance (regcache::current_regcache
.begin (),
1738 regcache::current_regcache
.end ());
1743 current_regcache_test (void)
1745 /* It is empty at the start. */
1746 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1748 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1750 /* Get regcache from ptid1, a new regcache is added to
1751 current_regcache. */
1752 regcache
*regcache
= get_thread_arch_aspace_regcache (ptid1
,
1756 SELF_CHECK (regcache
!= NULL
);
1757 SELF_CHECK (regcache
->ptid () == ptid1
);
1758 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1760 /* Get regcache from ptid2, a new regcache is added to
1761 current_regcache. */
1762 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1765 SELF_CHECK (regcache
!= NULL
);
1766 SELF_CHECK (regcache
->ptid () == ptid2
);
1767 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1769 /* Get regcache from ptid3, a new regcache is added to
1770 current_regcache. */
1771 regcache
= get_thread_arch_aspace_regcache (ptid3
,
1774 SELF_CHECK (regcache
!= NULL
);
1775 SELF_CHECK (regcache
->ptid () == ptid3
);
1776 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1778 /* Get regcache from ptid2 again, nothing is added to
1779 current_regcache. */
1780 regcache
= get_thread_arch_aspace_regcache (ptid2
,
1783 SELF_CHECK (regcache
!= NULL
);
1784 SELF_CHECK (regcache
->ptid () == ptid2
);
1785 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1787 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1788 current_regcache. */
1789 registers_changed_ptid (ptid2
);
1790 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1793 static void test_target_fetch_registers (target_ops
*self
, regcache
*regs
,
1795 static void test_target_store_registers (target_ops
*self
, regcache
*regs
,
1797 static enum target_xfer_status
1798 test_target_xfer_partial (struct target_ops
*ops
,
1799 enum target_object object
,
1800 const char *annex
, gdb_byte
*readbuf
,
1801 const gdb_byte
*writebuf
,
1802 ULONGEST offset
, ULONGEST len
,
1803 ULONGEST
*xfered_len
);
1805 class target_ops_no_register
: public test_target_ops
1808 target_ops_no_register ()
1809 : test_target_ops
{}
1811 to_fetch_registers
= test_target_fetch_registers
;
1812 to_store_registers
= test_target_store_registers
;
1813 to_xfer_partial
= test_target_xfer_partial
;
1820 fetch_registers_called
= 0;
1821 store_registers_called
= 0;
1822 xfer_partial_called
= 0;
1825 unsigned int fetch_registers_called
= 0;
1826 unsigned int store_registers_called
= 0;
1827 unsigned int xfer_partial_called
= 0;
1831 test_target_fetch_registers (target_ops
*self
, regcache
*regs
, int regno
)
1833 auto ops
= static_cast<target_ops_no_register
*> (self
->to_data
);
1835 /* Mark register available. */
1836 regs
->raw_supply_zeroed (regno
);
1837 ops
->fetch_registers_called
++;
1841 test_target_store_registers (target_ops
*self
, regcache
*regs
, int regno
)
1843 auto ops
= static_cast<target_ops_no_register
*> (self
->to_data
);
1845 ops
->store_registers_called
++;
1848 static enum target_xfer_status
1849 test_target_xfer_partial (struct target_ops
*self
, enum target_object object
,
1850 const char *annex
, gdb_byte
*readbuf
,
1851 const gdb_byte
*writebuf
,
1852 ULONGEST offset
, ULONGEST len
, ULONGEST
*xfered_len
)
1854 auto ops
= static_cast<target_ops_no_register
*> (self
->to_data
);
1856 ops
->xfer_partial_called
++;
1859 return TARGET_XFER_OK
;
1862 class readwrite_regcache
: public regcache
1865 readwrite_regcache (struct gdbarch
*gdbarch
)
1866 : regcache (gdbarch
, nullptr)
1870 /* Test regcache::cooked_read gets registers from raw registers and
1871 memory instead of target to_{fetch,store}_registers. */
1874 cooked_read_test (struct gdbarch
*gdbarch
)
1876 /* Error out if debugging something, because we're going to push the
1877 test target, which would pop any existing target. */
1878 if (current_target
.to_stratum
>= process_stratum
)
1879 error (_("target already pushed"));
1881 /* Create a mock environment. An inferior with a thread, with a
1882 process_stratum target pushed. */
1884 target_ops_no_register mock_target
;
1885 ptid_t
mock_ptid (1, 1);
1886 inferior
mock_inferior (mock_ptid
.pid ());
1887 address_space mock_aspace
{};
1888 mock_inferior
.gdbarch
= gdbarch
;
1889 mock_inferior
.aspace
= &mock_aspace
;
1890 thread_info
mock_thread (&mock_inferior
, mock_ptid
);
1892 scoped_restore restore_thread_list
1893 = make_scoped_restore (&thread_list
, &mock_thread
);
1895 /* Add the mock inferior to the inferior list so that look ups by
1896 target+ptid can find it. */
1897 scoped_restore restore_inferior_list
1898 = make_scoped_restore (&inferior_list
);
1899 inferior_list
= &mock_inferior
;
1901 /* Switch to the mock inferior. */
1902 scoped_restore_current_inferior restore_current_inferior
;
1903 set_current_inferior (&mock_inferior
);
1905 /* Push the process_stratum target so we can mock accessing
1907 push_target (&mock_target
);
1909 /* Pop it again on exit (return/exception). */
1914 pop_all_targets_at_and_above (process_stratum
);
1918 /* Switch to the mock thread. */
1919 scoped_restore restore_inferior_ptid
1920 = make_scoped_restore (&inferior_ptid
, mock_ptid
);
1922 /* Test that read one raw register from regcache_no_target will go
1923 to the target layer. */
1926 /* Find a raw register which size isn't zero. */
1927 for (regnum
= 0; regnum
< gdbarch_num_regs (gdbarch
); regnum
++)
1929 if (register_size (gdbarch
, regnum
) != 0)
1933 readwrite_regcache
readwrite (gdbarch
);
1934 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1936 readwrite
.raw_read (regnum
, buf
.data ());
1938 /* raw_read calls target_fetch_registers. */
1939 SELF_CHECK (mock_target
.fetch_registers_called
> 0);
1940 mock_target
.reset ();
1942 /* Mark all raw registers valid, so the following raw registers
1943 accesses won't go to target. */
1944 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1945 readwrite
.raw_update (i
);
1947 mock_target
.reset ();
1948 /* Then, read all raw and pseudo registers, and don't expect calling
1949 to_{fetch,store}_registers. */
1950 for (int regnum
= 0;
1951 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1954 if (register_size (gdbarch
, regnum
) == 0)
1957 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1959 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
, buf
.data ()));
1961 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
1962 SELF_CHECK (mock_target
.store_registers_called
== 0);
1964 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1965 if (gdbarch_bfd_arch_info (gdbarch
)->arch
!= bfd_arch_spu
)
1966 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
1968 mock_target
.reset ();
1971 readonly_detached_regcache
readonly (readwrite
);
1973 /* GDB may go to target layer to fetch all registers and memory for
1974 readonly regcache. */
1975 mock_target
.reset ();
1977 for (int regnum
= 0;
1978 regnum
< gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
1981 if (register_size (gdbarch
, regnum
) == 0)
1984 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
));
1985 enum register_status status
= readonly
.cooked_read (regnum
,
1988 if (regnum
< gdbarch_num_regs (gdbarch
))
1990 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1992 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1993 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1994 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1995 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1996 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1997 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1998 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
)
2000 /* Raw registers. If raw registers are not in save_reggroup,
2001 their status are unknown. */
2002 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
2003 SELF_CHECK (status
== REG_VALID
);
2005 SELF_CHECK (status
== REG_UNKNOWN
);
2008 SELF_CHECK (status
== REG_VALID
);
2012 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
2013 SELF_CHECK (status
== REG_VALID
);
2016 /* If pseudo registers are not in save_reggroup, some of
2017 them can be computed from saved raw registers, but some
2018 of them are unknown. */
2019 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
2021 if (bfd_arch
== bfd_arch_frv
2022 || bfd_arch
== bfd_arch_m32c
2023 || bfd_arch
== bfd_arch_mep
2024 || bfd_arch
== bfd_arch_sh
)
2025 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
2026 else if (bfd_arch
== bfd_arch_mips
2027 || bfd_arch
== bfd_arch_h8300
)
2028 SELF_CHECK (status
== REG_UNKNOWN
);
2030 SELF_CHECK (status
== REG_VALID
);
2034 SELF_CHECK (mock_target
.fetch_registers_called
== 0);
2035 SELF_CHECK (mock_target
.store_registers_called
== 0);
2036 SELF_CHECK (mock_target
.xfer_partial_called
== 0);
2038 mock_target
.reset ();
2042 /* Test regcache::cooked_write by writing some expected contents to
2043 registers, and checking that contents read from registers and the
2044 expected contents are the same. */
2047 cooked_write_test (struct gdbarch
*gdbarch
)
2049 /* Error out if debugging something, because we're going to push the
2050 test target, which would pop any existing target. */
2051 if (current_target
.to_stratum
>= process_stratum
)
2052 error (_("target already pushed"));
2054 /* Create a mock environment. A process_stratum target pushed. */
2056 target_ops_no_register mock_target
;
2058 /* Push the process_stratum target so we can mock accessing
2060 push_target (&mock_target
);
2062 /* Pop it again on exit (return/exception). */
2067 pop_all_targets_at_and_above (process_stratum
);
2071 readwrite_regcache
readwrite (gdbarch
);
2073 const int num_regs
= (gdbarch_num_regs (gdbarch
)
2074 + gdbarch_num_pseudo_regs (gdbarch
));
2076 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
2078 if (register_size (gdbarch
, regnum
) == 0
2079 || gdbarch_cannot_store_register (gdbarch
, regnum
))
2082 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
2084 if ((bfd_arch
== bfd_arch_sparc
2085 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
2086 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
2087 && gdbarch_ptr_bit (gdbarch
) == 64
2088 && (regnum
>= gdbarch_num_regs (gdbarch
)
2089 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
2090 || (bfd_arch
== bfd_arch_sh
2091 /* FPSCR_C_REGNUM in sh64 is hard to test. */
2092 && gdbarch_bfd_arch_info (gdbarch
)->mach
== bfd_mach_sh5
2094 || (bfd_arch
== bfd_arch_spu
2095 /* SPU pseudo registers except SPU_SP_REGNUM are got by
2096 TARGET_OBJECT_SPU. */
2097 && regnum
>= gdbarch_num_regs (gdbarch
) && regnum
!= 130))
2100 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
2101 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
2102 const auto type
= register_type (gdbarch
, regnum
);
2104 if (TYPE_CODE (type
) == TYPE_CODE_FLT
2105 || TYPE_CODE (type
) == TYPE_CODE_DECFLOAT
)
2107 /* Generate valid float format. */
2108 target_float_from_string (expected
.data (), type
, "1.25");
2110 else if (TYPE_CODE (type
) == TYPE_CODE_INT
2111 || TYPE_CODE (type
) == TYPE_CODE_ARRAY
2112 || TYPE_CODE (type
) == TYPE_CODE_PTR
2113 || TYPE_CODE (type
) == TYPE_CODE_UNION
2114 || TYPE_CODE (type
) == TYPE_CODE_STRUCT
)
2116 if (bfd_arch
== bfd_arch_ia64
2117 || (regnum
>= gdbarch_num_regs (gdbarch
)
2118 && (bfd_arch
== bfd_arch_xtensa
2119 || bfd_arch
== bfd_arch_bfin
2120 || bfd_arch
== bfd_arch_m32c
2121 /* m68hc11 pseudo registers are in memory. */
2122 || bfd_arch
== bfd_arch_m68hc11
2123 || bfd_arch
== bfd_arch_m68hc12
2124 || bfd_arch
== bfd_arch_s390
))
2125 || (bfd_arch
== bfd_arch_frv
2126 /* FRV pseudo registers except iacc0. */
2127 && regnum
> gdbarch_num_regs (gdbarch
)))
2129 /* Skip setting the expected values for some architecture
2132 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
2134 /* RL78_PC_REGNUM */
2135 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
2140 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
2144 else if (TYPE_CODE (type
) == TYPE_CODE_FLAGS
)
2146 /* No idea how to test flags. */
2151 /* If we don't know how to create the expected value for the
2152 this type, make it fail. */
2156 readwrite
.cooked_write (regnum
, expected
.data ());
2158 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
2159 SELF_CHECK (expected
== buf
);
2163 } // namespace selftests
2164 #endif /* GDB_SELF_TEST */
2167 _initialize_regcache (void)
2169 regcache_descr_handle
2170 = gdbarch_data_register_post_init (init_regcache_descr
);
2172 observer_attach_target_changed (regcache_observer_target_changed
);
2173 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed
);
2175 add_com ("flushregs", class_maintenance
, reg_flush_command
,
2176 _("Force gdb to flush its register cache (maintainer command)"));
2178 add_cmd ("registers", class_maintenance
, maintenance_print_registers
,
2179 _("Print the internal register configuration.\n"
2180 "Takes an optional file parameter."), &maintenanceprintlist
);
2181 add_cmd ("raw-registers", class_maintenance
,
2182 maintenance_print_raw_registers
,
2183 _("Print the internal register configuration "
2184 "including raw values.\n"
2185 "Takes an optional file parameter."), &maintenanceprintlist
);
2186 add_cmd ("cooked-registers", class_maintenance
,
2187 maintenance_print_cooked_registers
,
2188 _("Print the internal register configuration "
2189 "including cooked values.\n"
2190 "Takes an optional file parameter."), &maintenanceprintlist
);
2191 add_cmd ("register-groups", class_maintenance
,
2192 maintenance_print_register_groups
,
2193 _("Print the internal register configuration "
2194 "including each register's group.\n"
2195 "Takes an optional file parameter."),
2196 &maintenanceprintlist
);
2197 add_cmd ("remote-registers", class_maintenance
,
2198 maintenance_print_remote_registers
, _("\
2199 Print the internal register configuration including each register's\n\
2200 remote register number and buffer offset in the g/G packets.\n\
2201 Takes an optional file parameter."),
2202 &maintenanceprintlist
);
2205 selftests::register_test ("current_regcache", selftests::current_regcache_test
);
2207 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2208 selftests::cooked_read_test
);
2209 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2210 selftests::cooked_write_test
);