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 static std::forward_list
<regcache
*> regcaches
;
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
: regcaches
)
332 if (regcache
->target () == target
333 && regcache
->ptid () == ptid
334 && regcache
->arch () == gdbarch
)
337 regcache
*new_regcache
= new regcache (target
, gdbarch
, aspace
);
339 regcaches
.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 regcaches related to OLD_PTID to now use NEW_PTID. */
417 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
419 for (auto ®cache
: regcaches
)
421 if (regcache
->ptid () == old_ptid
)
422 regcache
->set_ptid (new_ptid
);
426 /* Low level examining and depositing of registers.
428 The caller is responsible for making sure that the inferior is
429 stopped before calling the fetching routines, or it will get
430 garbage. (a change from GDB version 3, in which the caller got the
431 value from the last stop). */
433 /* REGISTERS_CHANGED ()
435 Indicate that registers may have changed, so invalidate the cache. */
438 registers_changed_ptid (process_stratum_target
*target
, ptid_t ptid
)
440 for (auto oit
= regcaches
.before_begin (), it
= std::next (oit
);
441 it
!= regcaches
.end (); )
443 struct regcache
*regcache
= *it
;
444 if ((target
== nullptr || regcache
->target () == target
)
445 && regcache
->ptid ().matches (ptid
))
448 it
= regcaches
.erase_after (oit
);
454 if ((target
== nullptr || current_thread_target
== target
)
455 && current_thread_ptid
.matches (ptid
))
457 current_thread_target
= NULL
;
458 current_thread_ptid
= null_ptid
;
459 current_thread_arch
= NULL
;
462 if ((target
== nullptr || current_inferior ()->process_target () == target
)
463 && inferior_ptid
.matches (ptid
))
465 /* We just deleted the regcache of the current thread. Need to
466 forget about any frames we have cached, too. */
467 reinit_frame_cache ();
471 /* See regcache.h. */
474 registers_changed_thread (thread_info
*thread
)
476 registers_changed_ptid (thread
->inf
->process_target (), thread
->ptid
);
480 registers_changed (void)
482 registers_changed_ptid (nullptr, minus_one_ptid
);
486 regcache::raw_update (int regnum
)
488 assert_regnum (regnum
);
490 /* Make certain that the register cache is up-to-date with respect
491 to the current thread. This switching shouldn't be necessary
492 only there is still only one target side register cache. Sigh!
493 On the bright side, at least there is a regcache object. */
495 if (get_register_status (regnum
) == REG_UNKNOWN
)
497 target_fetch_registers (this, regnum
);
499 /* A number of targets can't access the whole set of raw
500 registers (because the debug API provides no means to get at
502 if (m_register_status
[regnum
] == REG_UNKNOWN
)
503 m_register_status
[regnum
] = REG_UNAVAILABLE
;
508 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
510 gdb_assert (buf
!= NULL
);
513 if (m_register_status
[regnum
] != REG_VALID
)
514 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
516 memcpy (buf
, register_buffer (regnum
),
517 m_descr
->sizeof_register
[regnum
]);
519 return m_register_status
[regnum
];
523 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
525 gdb_assert (regcache
!= NULL
);
526 return regcache
->raw_read (regnum
, val
);
529 template<typename T
, typename
>
531 readable_regcache::raw_read (int regnum
, T
*val
)
534 enum register_status status
;
536 assert_regnum (regnum
);
537 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
538 status
= raw_read (regnum
, buf
);
539 if (status
== REG_VALID
)
540 *val
= extract_integer
<T
> (buf
,
541 m_descr
->sizeof_register
[regnum
],
542 gdbarch_byte_order (m_descr
->gdbarch
));
549 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
552 gdb_assert (regcache
!= NULL
);
553 return regcache
->raw_read (regnum
, val
);
557 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
559 gdb_assert (regcache
!= NULL
);
560 regcache
->raw_write (regnum
, val
);
563 template<typename T
, typename
>
565 regcache::raw_write (int regnum
, T val
)
569 assert_regnum (regnum
);
570 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
571 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
572 gdbarch_byte_order (m_descr
->gdbarch
), val
);
573 raw_write (regnum
, buf
);
577 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
580 gdb_assert (regcache
!= NULL
);
581 regcache
->raw_write (regnum
, val
);
585 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
588 enum register_status status
;
590 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
591 if (status
== REG_UNAVAILABLE
)
592 throw_error (NOT_AVAILABLE_ERROR
,
593 _("Register %d is not available"), regnum
);
598 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
600 gdb_assert (regnum
>= 0);
601 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
602 if (regnum
< num_raw_registers ())
603 return raw_read (regnum
, buf
);
604 else if (m_has_pseudo
605 && m_register_status
[regnum
] != REG_UNKNOWN
)
607 if (m_register_status
[regnum
] == REG_VALID
)
608 memcpy (buf
, register_buffer (regnum
),
609 m_descr
->sizeof_register
[regnum
]);
611 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
613 return m_register_status
[regnum
];
615 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
617 struct value
*mark
, *computed
;
618 enum register_status result
= REG_VALID
;
620 mark
= value_mark ();
622 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
624 if (value_entirely_available (computed
))
625 memcpy (buf
, value_contents_raw (computed
),
626 m_descr
->sizeof_register
[regnum
]);
629 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
630 result
= REG_UNAVAILABLE
;
633 value_free_to_mark (mark
);
638 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
643 readable_regcache::cooked_read_value (int regnum
)
645 gdb_assert (regnum
>= 0);
646 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
648 if (regnum
< num_raw_registers ()
649 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
650 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
652 struct value
*result
;
654 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
655 VALUE_LVAL (result
) = lval_register
;
656 VALUE_REGNUM (result
) = regnum
;
658 /* It is more efficient in general to do this delegation in this
659 direction than in the other one, even though the value-based
661 if (cooked_read (regnum
,
662 value_contents_raw (result
)) == REG_UNAVAILABLE
)
663 mark_value_bytes_unavailable (result
, 0,
664 TYPE_LENGTH (value_type (result
)));
669 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
674 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
677 gdb_assert (regcache
!= NULL
);
678 return regcache
->cooked_read (regnum
, val
);
681 template<typename T
, typename
>
683 readable_regcache::cooked_read (int regnum
, T
*val
)
685 enum register_status status
;
688 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
689 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
690 status
= cooked_read (regnum
, buf
);
691 if (status
== REG_VALID
)
692 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
693 gdbarch_byte_order (m_descr
->gdbarch
));
700 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
703 gdb_assert (regcache
!= NULL
);
704 return regcache
->cooked_read (regnum
, val
);
708 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
711 gdb_assert (regcache
!= NULL
);
712 regcache
->cooked_write (regnum
, val
);
715 template<typename T
, typename
>
717 regcache::cooked_write (int regnum
, T val
)
721 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
722 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
723 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
724 gdbarch_byte_order (m_descr
->gdbarch
), val
);
725 cooked_write (regnum
, buf
);
729 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
732 gdb_assert (regcache
!= NULL
);
733 regcache
->cooked_write (regnum
, val
);
737 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
740 gdb_assert (buf
!= NULL
);
741 assert_regnum (regnum
);
743 /* On the sparc, writing %g0 is a no-op, so we don't even want to
744 change the registers array if something writes to this register. */
745 if (gdbarch_cannot_store_register (arch (), regnum
))
748 /* If we have a valid copy of the register, and new value == old
749 value, then don't bother doing the actual store. */
750 if (get_register_status (regnum
) == REG_VALID
751 && (memcmp (register_buffer (regnum
), buf
,
752 m_descr
->sizeof_register
[regnum
]) == 0))
755 target_prepare_to_store (this);
756 raw_supply (regnum
, buf
);
758 /* Invalidate the register after it is written, in case of a
761 = make_scope_exit ([&] { this->invalidate (regnum
); });
763 target_store_registers (this, regnum
);
765 /* The target did not throw an error so we can discard invalidating
767 invalidator
.release ();
771 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
773 gdb_assert (regnum
>= 0);
774 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
775 if (regnum
< num_raw_registers ())
776 raw_write (regnum
, buf
);
778 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
782 /* See regcache.h. */
785 readable_regcache::read_part (int regnum
, int offset
, int len
,
786 gdb_byte
*out
, bool is_raw
)
788 int reg_size
= register_size (arch (), regnum
);
790 gdb_assert (out
!= NULL
);
791 gdb_assert (offset
>= 0 && offset
<= reg_size
);
792 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
794 if (offset
== 0 && len
== 0)
800 if (offset
== 0 && len
== reg_size
)
802 /* Read the full register. */
803 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
806 enum register_status status
;
807 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
809 /* Read full register to buffer. */
810 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
811 if (status
!= REG_VALID
)
815 memcpy (out
, reg
+ offset
, len
);
819 /* See regcache.h. */
822 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
825 int reg_size
= register_size (arch (), regnum
);
827 gdb_assert (out
!= nullptr);
828 gdb_assert (offset
>= 0 && offset
<= reg_size
);
829 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
831 if (offset
== 0 && len
== 0)
837 if (offset
== 0 && len
== reg_size
)
839 /* Collect the full register. */
840 return raw_collect (regnum
, out
);
843 /* Read to buffer, then write out. */
844 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
845 raw_collect (regnum
, reg
);
846 memcpy (out
, reg
+ offset
, len
);
849 /* See regcache.h. */
852 regcache::write_part (int regnum
, int offset
, int len
,
853 const gdb_byte
*in
, bool is_raw
)
855 int reg_size
= register_size (arch (), regnum
);
857 gdb_assert (in
!= NULL
);
858 gdb_assert (offset
>= 0 && offset
<= reg_size
);
859 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
861 if (offset
== 0 && len
== 0)
867 if (offset
== 0 && len
== reg_size
)
869 /* Write the full register. */
870 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
874 enum register_status status
;
875 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
877 /* Read existing register to buffer. */
878 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
879 if (status
!= REG_VALID
)
882 /* Update buffer, then write back to regcache. */
883 memcpy (reg
+ offset
, in
, len
);
884 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
888 /* See regcache.h. */
891 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
894 int reg_size
= register_size (arch (), regnum
);
896 gdb_assert (in
!= nullptr);
897 gdb_assert (offset
>= 0 && offset
<= reg_size
);
898 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
900 if (offset
== 0 && len
== 0)
906 if (offset
== 0 && len
== reg_size
)
908 /* Supply the full register. */
909 return raw_supply (regnum
, in
);
912 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
914 /* Read existing value to buffer. */
915 raw_collect (regnum
, reg
);
917 /* Write to buffer, then write out. */
918 memcpy (reg
+ offset
, in
, len
);
919 raw_supply (regnum
, reg
);
923 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
926 assert_regnum (regnum
);
927 return read_part (regnum
, offset
, len
, buf
, true);
930 /* See regcache.h. */
933 regcache::raw_write_part (int regnum
, int offset
, int len
,
936 assert_regnum (regnum
);
937 write_part (regnum
, offset
, len
, buf
, true);
940 /* See regcache.h. */
943 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
946 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
947 return read_part (regnum
, offset
, len
, buf
, false);
950 /* See regcache.h. */
953 regcache::cooked_write_part (int regnum
, int offset
, int len
,
956 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
957 write_part (regnum
, offset
, len
, buf
, false);
960 /* See gdbsupport/common-regcache.h. */
963 reg_buffer::raw_supply (int regnum
, const void *buf
)
968 assert_regnum (regnum
);
970 regbuf
= register_buffer (regnum
);
971 size
= m_descr
->sizeof_register
[regnum
];
975 memcpy (regbuf
, buf
, size
);
976 m_register_status
[regnum
] = REG_VALID
;
980 /* This memset not strictly necessary, but better than garbage
981 in case the register value manages to escape somewhere (due
982 to a bug, no less). */
983 memset (regbuf
, 0, size
);
984 m_register_status
[regnum
] = REG_UNAVAILABLE
;
988 /* See regcache.h. */
991 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
992 int addr_len
, bool is_signed
)
994 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
998 assert_regnum (regnum
);
1000 regbuf
= register_buffer (regnum
);
1001 regsize
= m_descr
->sizeof_register
[regnum
];
1003 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1005 m_register_status
[regnum
] = REG_VALID
;
1008 /* See regcache.h. */
1011 reg_buffer::raw_supply_zeroed (int regnum
)
1016 assert_regnum (regnum
);
1018 regbuf
= register_buffer (regnum
);
1019 size
= m_descr
->sizeof_register
[regnum
];
1021 memset (regbuf
, 0, size
);
1022 m_register_status
[regnum
] = REG_VALID
;
1025 /* See gdbsupport/common-regcache.h. */
1028 reg_buffer::raw_collect (int regnum
, void *buf
) const
1033 gdb_assert (buf
!= NULL
);
1034 assert_regnum (regnum
);
1036 regbuf
= register_buffer (regnum
);
1037 size
= m_descr
->sizeof_register
[regnum
];
1038 memcpy (buf
, regbuf
, size
);
1041 /* See regcache.h. */
1044 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1045 bool is_signed
) const
1047 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1048 const gdb_byte
*regbuf
;
1051 assert_regnum (regnum
);
1053 regbuf
= register_buffer (regnum
);
1054 regsize
= m_descr
->sizeof_register
[regnum
];
1056 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1060 /* See regcache.h. */
1063 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1064 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1065 int slot_size
, int offs
) const
1067 struct gdbarch
*gdbarch
= arch ();
1068 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1070 /* Use part versions and reg_size to prevent possible buffer overflows when
1071 accessing the regcache. */
1073 if (out_buf
!= nullptr)
1075 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1077 /* Ensure any additional space is cleared. */
1078 if (slot_size
> reg_size
)
1079 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1081 else if (in_buf
!= nullptr)
1082 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1085 /* Invalidate the register. */
1086 out_regcache
->raw_supply (regnum
, nullptr);
1090 /* See regcache.h. */
1093 regcache::transfer_regset (const struct regset
*regset
,
1094 struct regcache
*out_regcache
,
1095 int regnum
, const gdb_byte
*in_buf
,
1096 gdb_byte
*out_buf
, size_t size
) const
1098 const struct regcache_map_entry
*map
;
1099 int offs
= 0, count
;
1101 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1102 (count
= map
->count
) != 0;
1105 int regno
= map
->regno
;
1106 int slot_size
= map
->size
;
1108 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1109 slot_size
= m_descr
->sizeof_register
[regno
];
1111 if (regno
== REGCACHE_MAP_SKIP
1113 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1114 offs
+= count
* slot_size
;
1116 else if (regnum
== -1)
1117 for (; count
--; regno
++, offs
+= slot_size
)
1119 if (offs
+ slot_size
> size
)
1122 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1127 /* Transfer a single register and return. */
1128 offs
+= (regnum
- regno
) * slot_size
;
1129 if (offs
+ slot_size
> size
)
1132 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1139 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1140 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1141 If BUF is NULL, set the register(s) to "unavailable" status. */
1144 regcache_supply_regset (const struct regset
*regset
,
1145 struct regcache
*regcache
,
1146 int regnum
, const void *buf
, size_t size
)
1148 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1152 regcache::supply_regset (const struct regset
*regset
,
1153 int regnum
, const void *buf
, size_t size
)
1155 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1158 /* Collect register REGNUM from REGCACHE to BUF, using the register
1159 map in REGSET. If REGNUM is -1, do this for all registers in
1163 regcache_collect_regset (const struct regset
*regset
,
1164 const struct regcache
*regcache
,
1165 int regnum
, void *buf
, size_t size
)
1167 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1171 regcache::collect_regset (const struct regset
*regset
,
1172 int regnum
, void *buf
, size_t size
) const
1174 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1177 /* See gdbsupport/common-regcache.h. */
1180 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1182 gdb_assert (buf
!= NULL
);
1183 assert_regnum (regnum
);
1185 const char *regbuf
= (const char *) register_buffer (regnum
);
1186 size_t size
= m_descr
->sizeof_register
[regnum
];
1187 gdb_assert (size
>= offset
);
1189 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1192 /* Special handling for register PC. */
1195 regcache_read_pc (struct regcache
*regcache
)
1197 struct gdbarch
*gdbarch
= regcache
->arch ();
1201 if (gdbarch_read_pc_p (gdbarch
))
1202 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1203 /* Else use per-frame method on get_current_frame. */
1204 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1208 if (regcache_cooked_read_unsigned (regcache
,
1209 gdbarch_pc_regnum (gdbarch
),
1210 &raw_val
) == REG_UNAVAILABLE
)
1211 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1213 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1216 internal_error (__FILE__
, __LINE__
,
1217 _("regcache_read_pc: Unable to find PC"));
1221 /* See gdbsupport/common-regcache.h. */
1224 regcache_read_pc_protected (regcache
*regcache
)
1229 pc
= regcache_read_pc (regcache
);
1231 catch (const gdb_exception_error
&ex
)
1240 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1242 struct gdbarch
*gdbarch
= regcache
->arch ();
1244 if (gdbarch_write_pc_p (gdbarch
))
1245 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1246 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1247 regcache_cooked_write_unsigned (regcache
,
1248 gdbarch_pc_regnum (gdbarch
), pc
);
1250 internal_error (__FILE__
, __LINE__
,
1251 _("regcache_write_pc: Unable to update PC"));
1253 /* Writing the PC (for instance, from "load") invalidates the
1255 reinit_frame_cache ();
1259 reg_buffer::num_raw_registers () const
1261 return gdbarch_num_regs (arch ());
1265 regcache::debug_print_register (const char *func
, int regno
)
1267 struct gdbarch
*gdbarch
= arch ();
1269 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1270 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1271 && gdbarch_register_name (gdbarch
, regno
) != NULL
1272 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1273 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1274 gdbarch_register_name (gdbarch
, regno
));
1276 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1277 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1279 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1280 int size
= register_size (gdbarch
, regno
);
1281 gdb_byte
*buf
= register_buffer (regno
);
1283 fprintf_unfiltered (gdb_stdlog
, " = ");
1284 for (int i
= 0; i
< size
; i
++)
1286 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1288 if (size
<= sizeof (LONGEST
))
1290 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1292 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1293 core_addr_to_string_nz (val
), plongest (val
));
1296 fprintf_unfiltered (gdb_stdlog
, "\n");
1300 reg_flush_command (const char *command
, int from_tty
)
1302 /* Force-flush the register cache. */
1303 registers_changed ();
1305 printf_filtered (_("Register cache flushed.\n"));
1309 register_dump::dump (ui_file
*file
)
1311 auto descr
= regcache_descr (m_gdbarch
);
1313 int footnote_nr
= 0;
1314 int footnote_register_offset
= 0;
1315 int footnote_register_type_name_null
= 0;
1316 long register_offset
= 0;
1318 gdb_assert (descr
->nr_cooked_registers
1319 == gdbarch_num_cooked_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
= register_type (m_gdbarch
, regnum
)->name ();
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
);
1427 #include "gdbsupport/selftest.h"
1428 #include "selftest-arch.h"
1429 #include "target-float.h"
1431 namespace selftests
{
1436 return std::distance (regcaches
.begin (),
1440 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1443 test_get_thread_arch_aspace_regcache (process_stratum_target
*target
,
1444 ptid_t ptid
, struct gdbarch
*gdbarch
,
1445 address_space
*aspace
)
1447 struct regcache
*regcache
1448 = get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
1449 SELF_CHECK (regcache
!= NULL
);
1450 SELF_CHECK (regcache
->target () == target
);
1451 SELF_CHECK (regcache
->ptid () == ptid
);
1452 SELF_CHECK (regcache
->aspace () == aspace
);
1458 /* It is empty at the start. */
1459 SELF_CHECK (regcaches_size () == 0);
1461 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1463 test_target_ops test_target1
;
1464 test_target_ops test_target2
;
1466 /* Get regcache from (target1,ptid1), a new regcache is added to
1468 test_get_thread_arch_aspace_regcache (&test_target1
, ptid1
,
1471 SELF_CHECK (regcaches_size () == 1);
1473 /* Get regcache from (target1,ptid2), a new regcache is added to
1475 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1478 SELF_CHECK (regcaches_size () == 2);
1480 /* Get regcache from (target1,ptid3), a new regcache is added to
1482 test_get_thread_arch_aspace_regcache (&test_target1
, ptid3
,
1485 SELF_CHECK (regcaches_size () == 3);
1487 /* Get regcache from (target1,ptid2) again, nothing is added to
1489 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1492 SELF_CHECK (regcaches_size () == 3);
1494 /* Get regcache from (target2,ptid2), a new regcache is added to
1495 REGCACHES, since this time we're using a different target. */
1496 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1499 SELF_CHECK (regcaches_size () == 4);
1501 /* Mark that (target1,ptid2) changed. The regcache of (target1,
1502 ptid2) should be removed from REGCACHES. */
1503 registers_changed_ptid (&test_target1
, ptid2
);
1504 SELF_CHECK (regcaches_size () == 3);
1506 /* Get the regcache from (target2,ptid2) again, confirming the
1507 registers_changed_ptid call above did not delete it. */
1508 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1511 SELF_CHECK (regcaches_size () == 3);
1513 /* Confirm that marking all regcaches of all targets as changed
1514 clears REGCACHES. */
1515 registers_changed_ptid (nullptr, minus_one_ptid
);
1516 SELF_CHECK (regcaches_size () == 0);
1519 class target_ops_no_register
: public test_target_ops
1522 target_ops_no_register ()
1523 : test_target_ops
{}
1528 fetch_registers_called
= 0;
1529 store_registers_called
= 0;
1530 xfer_partial_called
= 0;
1533 void fetch_registers (regcache
*regs
, int regno
) override
;
1534 void store_registers (regcache
*regs
, int regno
) override
;
1536 enum target_xfer_status
xfer_partial (enum target_object object
,
1537 const char *annex
, gdb_byte
*readbuf
,
1538 const gdb_byte
*writebuf
,
1539 ULONGEST offset
, ULONGEST len
,
1540 ULONGEST
*xfered_len
) override
;
1542 unsigned int fetch_registers_called
= 0;
1543 unsigned int store_registers_called
= 0;
1544 unsigned int xfer_partial_called
= 0;
1548 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1550 /* Mark register available. */
1551 regs
->raw_supply_zeroed (regno
);
1552 this->fetch_registers_called
++;
1556 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1558 this->store_registers_called
++;
1561 enum target_xfer_status
1562 target_ops_no_register::xfer_partial (enum target_object object
,
1563 const char *annex
, gdb_byte
*readbuf
,
1564 const gdb_byte
*writebuf
,
1565 ULONGEST offset
, ULONGEST len
,
1566 ULONGEST
*xfered_len
)
1568 this->xfer_partial_called
++;
1571 return TARGET_XFER_OK
;
1574 class readwrite_regcache
: public regcache
1577 readwrite_regcache (process_stratum_target
*target
,
1578 struct gdbarch
*gdbarch
)
1579 : regcache (target
, gdbarch
, nullptr)
1583 /* Test regcache::cooked_read gets registers from raw registers and
1584 memory instead of target to_{fetch,store}_registers. */
1587 cooked_read_test (struct gdbarch
*gdbarch
)
1589 scoped_mock_context
<target_ops_no_register
> mockctx (gdbarch
);
1591 /* Test that read one raw register from regcache_no_target will go
1592 to the target layer. */
1594 /* Find a raw register which size isn't zero. */
1596 for (nonzero_regnum
= 0;
1597 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1600 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1604 readwrite_regcache
readwrite (&mockctx
.mock_target
, gdbarch
);
1605 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1607 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1609 /* raw_read calls target_fetch_registers. */
1610 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
> 0);
1611 mockctx
.mock_target
.reset ();
1613 /* Mark all raw registers valid, so the following raw registers
1614 accesses won't go to target. */
1615 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1616 readwrite
.raw_update (i
);
1618 mockctx
.mock_target
.reset ();
1619 /* Then, read all raw and pseudo registers, and don't expect calling
1620 to_{fetch,store}_registers. */
1621 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1623 if (register_size (gdbarch
, regnum
) == 0)
1626 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1628 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1629 inner_buf
.data ()));
1631 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1632 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1633 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1635 mockctx
.mock_target
.reset ();
1638 readonly_detached_regcache
readonly (readwrite
);
1640 /* GDB may go to target layer to fetch all registers and memory for
1641 readonly regcache. */
1642 mockctx
.mock_target
.reset ();
1644 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1646 if (register_size (gdbarch
, regnum
) == 0)
1649 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1650 enum register_status status
= readonly
.cooked_read (regnum
,
1653 if (regnum
< gdbarch_num_regs (gdbarch
))
1655 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1657 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1658 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1659 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1660 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1661 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1662 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1663 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1664 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1666 /* Raw registers. If raw registers are not in save_reggroup,
1667 their status are unknown. */
1668 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1669 SELF_CHECK (status
== REG_VALID
);
1671 SELF_CHECK (status
== REG_UNKNOWN
);
1674 SELF_CHECK (status
== REG_VALID
);
1678 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1679 SELF_CHECK (status
== REG_VALID
);
1682 /* If pseudo registers are not in save_reggroup, some of
1683 them can be computed from saved raw registers, but some
1684 of them are unknown. */
1685 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1687 if (bfd_arch
== bfd_arch_frv
1688 || bfd_arch
== bfd_arch_m32c
1689 || bfd_arch
== bfd_arch_mep
1690 || bfd_arch
== bfd_arch_sh
)
1691 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1692 else if (bfd_arch
== bfd_arch_mips
1693 || bfd_arch
== bfd_arch_h8300
)
1694 SELF_CHECK (status
== REG_UNKNOWN
);
1696 SELF_CHECK (status
== REG_VALID
);
1700 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1701 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1702 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1704 mockctx
.mock_target
.reset ();
1708 /* Test regcache::cooked_write by writing some expected contents to
1709 registers, and checking that contents read from registers and the
1710 expected contents are the same. */
1713 cooked_write_test (struct gdbarch
*gdbarch
)
1715 /* Error out if debugging something, because we're going to push the
1716 test target, which would pop any existing target. */
1717 if (current_top_target ()->stratum () >= process_stratum
)
1718 error (_("target already pushed"));
1720 /* Create a mock environment. A process_stratum target pushed. */
1722 target_ops_no_register mock_target
;
1724 /* Push the process_stratum target so we can mock accessing
1726 push_target (&mock_target
);
1728 /* Pop it again on exit (return/exception). */
1733 pop_all_targets_at_and_above (process_stratum
);
1737 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1739 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1741 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1743 if (register_size (gdbarch
, regnum
) == 0
1744 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1747 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1749 if (bfd_arch
== bfd_arch_sparc
1750 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1751 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1752 && gdbarch_ptr_bit (gdbarch
) == 64
1753 && (regnum
>= gdbarch_num_regs (gdbarch
)
1754 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1757 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1758 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1759 const auto type
= register_type (gdbarch
, regnum
);
1761 if (type
->code () == TYPE_CODE_FLT
1762 || type
->code () == TYPE_CODE_DECFLOAT
)
1764 /* Generate valid float format. */
1765 target_float_from_string (expected
.data (), type
, "1.25");
1767 else if (type
->code () == TYPE_CODE_INT
1768 || type
->code () == TYPE_CODE_ARRAY
1769 || type
->code () == TYPE_CODE_PTR
1770 || type
->code () == TYPE_CODE_UNION
1771 || type
->code () == TYPE_CODE_STRUCT
)
1773 if (bfd_arch
== bfd_arch_ia64
1774 || (regnum
>= gdbarch_num_regs (gdbarch
)
1775 && (bfd_arch
== bfd_arch_xtensa
1776 || bfd_arch
== bfd_arch_bfin
1777 || bfd_arch
== bfd_arch_m32c
1778 /* m68hc11 pseudo registers are in memory. */
1779 || bfd_arch
== bfd_arch_m68hc11
1780 || bfd_arch
== bfd_arch_m68hc12
1781 || bfd_arch
== bfd_arch_s390
))
1782 || (bfd_arch
== bfd_arch_frv
1783 /* FRV pseudo registers except iacc0. */
1784 && regnum
> gdbarch_num_regs (gdbarch
)))
1786 /* Skip setting the expected values for some architecture
1789 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1791 /* RL78_PC_REGNUM */
1792 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1797 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1801 else if (type
->code () == TYPE_CODE_FLAGS
)
1803 /* No idea how to test flags. */
1808 /* If we don't know how to create the expected value for the
1809 this type, make it fail. */
1813 readwrite
.cooked_write (regnum
, expected
.data ());
1815 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1816 SELF_CHECK (expected
== buf
);
1820 } // namespace selftests
1821 #endif /* GDB_SELF_TEST */
1823 void _initialize_regcache ();
1825 _initialize_regcache ()
1827 regcache_descr_handle
1828 = gdbarch_data_register_post_init (init_regcache_descr
);
1830 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1831 gdb::observers::thread_ptid_changed
.attach (regcache_thread_ptid_changed
);
1833 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1834 _("Force gdb to flush its register cache (maintainer command)."));
1837 selftests::register_test ("regcaches", selftests::regcaches_test
);
1839 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1840 selftests::cooked_read_test
);
1841 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1842 selftests::cooked_write_test
);