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 <unordered_map>
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 /* Type to map a ptid to a list of regcaches (one thread may have multiple
317 regcaches, associated to different gdbarches). */
319 using ptid_regcache_map
320 = std::unordered_multimap
<ptid_t
, regcache_up
, hash_ptid
>;
322 /* Type to map a target to a ptid_regcache_map, holding the regcaches for the
323 threads defined by that target. */
325 using target_ptid_regcache_map
326 = std::unordered_map
<process_stratum_target
*, ptid_regcache_map
>;
328 /* Global structure containing the existing regcaches. */
330 /* NOTE: this is a write-through cache. There is no "dirty" bit for
331 recording if the register values have been changed (eg. by the
332 user). Therefore all registers must be written back to the
333 target when appropriate. */
334 static target_ptid_regcache_map regcaches
;
337 get_thread_arch_aspace_regcache (process_stratum_target
*target
,
338 ptid_t ptid
, gdbarch
*arch
,
339 struct address_space
*aspace
)
341 gdb_assert (target
!= nullptr);
343 /* Find the ptid -> regcache map for this target. */
344 auto &ptid_regc_map
= regcaches
[target
];
346 /* Check first if a regcache for this arch already exists. */
347 auto range
= ptid_regc_map
.equal_range (ptid
);
348 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
350 if (it
->second
->arch () == arch
)
351 return it
->second
.get ();
354 /* It does not exist, create it. */
355 regcache
*new_regcache
= new regcache (target
, arch
, aspace
);
356 new_regcache
->set_ptid (ptid
);
357 /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
358 constructor explictly instead of implicitly. */
359 ptid_regc_map
.insert (std::make_pair (ptid
, regcache_up (new_regcache
)));
365 get_thread_arch_regcache (process_stratum_target
*target
, ptid_t ptid
,
366 struct gdbarch
*gdbarch
)
368 scoped_restore_current_inferior restore_current_inferior
;
369 set_current_inferior (find_inferior_ptid (target
, ptid
));
370 address_space
*aspace
= target_thread_address_space (ptid
);
372 return get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
375 static process_stratum_target
*current_thread_target
;
376 static ptid_t current_thread_ptid
;
377 static struct gdbarch
*current_thread_arch
;
380 get_thread_regcache (process_stratum_target
*target
, ptid_t ptid
)
382 if (!current_thread_arch
383 || target
!= current_thread_target
384 || current_thread_ptid
!= ptid
)
386 gdb_assert (ptid
!= null_ptid
);
388 current_thread_ptid
= ptid
;
389 current_thread_target
= target
;
391 scoped_restore_current_inferior restore_current_inferior
;
392 set_current_inferior (find_inferior_ptid (target
, ptid
));
393 current_thread_arch
= target_thread_architecture (ptid
);
396 return get_thread_arch_regcache (target
, ptid
, current_thread_arch
);
399 /* See regcache.h. */
402 get_thread_regcache (thread_info
*thread
)
404 return get_thread_regcache (thread
->inf
->process_target (),
409 get_current_regcache (void)
411 return get_thread_regcache (inferior_thread ());
414 /* See gdbsupport/common-regcache.h. */
417 get_thread_regcache_for_ptid (ptid_t ptid
)
419 /* This function doesn't take a process_stratum_target parameter
420 because it's a gdbsupport/ routine implemented by both gdb and
421 gdbserver. It always refers to a ptid of the current target. */
422 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
423 return get_thread_regcache (proc_target
, ptid
);
426 /* Observer for the target_changed event. */
429 regcache_observer_target_changed (struct target_ops
*target
)
431 registers_changed ();
434 /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
436 regcache_thread_ptid_changed (process_stratum_target
*target
,
437 ptid_t old_ptid
, ptid_t new_ptid
)
439 auto ptid_regc_map_it
= regcaches
.find (target
);
441 if (ptid_regc_map_it
== regcaches
.end ())
444 auto &ptid_regc_map
= ptid_regc_map_it
->second
;
445 auto range
= ptid_regc_map
.equal_range (old_ptid
);
446 for (auto it
= range
.first
; it
!= range
.second
;)
448 regcache_up rc
= std::move (it
->second
);
449 rc
->set_ptid (new_ptid
);
451 /* Remove old before inserting new, to avoid rehashing,
452 which would invalidate iterators. */
453 it
= ptid_regc_map
.erase (it
);
454 ptid_regc_map
.insert (std::make_pair (new_ptid
, std::move (rc
)));
458 /* Low level examining and depositing of registers.
460 The caller is responsible for making sure that the inferior is
461 stopped before calling the fetching routines, or it will get
462 garbage. (a change from GDB version 3, in which the caller got the
463 value from the last stop). */
465 /* REGISTERS_CHANGED ()
467 Indicate that registers may have changed, so invalidate the cache. */
470 registers_changed_ptid (process_stratum_target
*target
, ptid_t ptid
)
472 if (target
== nullptr)
474 /* Since there can be ptid clashes between targets, it's not valid to
475 pass a ptid without saying to which target it belongs. */
476 gdb_assert (ptid
== minus_one_ptid
);
478 /* Delete all the regcaches of all targets. */
481 else if (ptid
!= minus_one_ptid
)
483 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
484 to this (TARGET, PTID). */
485 auto ptid_regc_map_it
= regcaches
.find (target
);
486 if (ptid_regc_map_it
!= regcaches
.end ())
488 auto &ptid_regc_map
= ptid_regc_map_it
->second
;
489 ptid_regc_map
.erase (ptid
);
494 /* Non-NULL target and minus_one_ptid, delete all regcaches
495 associated to this target. */
496 regcaches
.erase (target
);
499 if ((target
== nullptr || current_thread_target
== target
)
500 && current_thread_ptid
.matches (ptid
))
502 current_thread_target
= NULL
;
503 current_thread_ptid
= null_ptid
;
504 current_thread_arch
= NULL
;
507 if ((target
== nullptr || current_inferior ()->process_target () == target
)
508 && inferior_ptid
.matches (ptid
))
510 /* We just deleted the regcache of the current thread. Need to
511 forget about any frames we have cached, too. */
512 reinit_frame_cache ();
516 /* See regcache.h. */
519 registers_changed_thread (thread_info
*thread
)
521 registers_changed_ptid (thread
->inf
->process_target (), thread
->ptid
);
525 registers_changed (void)
527 registers_changed_ptid (nullptr, minus_one_ptid
);
531 regcache::raw_update (int regnum
)
533 assert_regnum (regnum
);
535 /* Make certain that the register cache is up-to-date with respect
536 to the current thread. This switching shouldn't be necessary
537 only there is still only one target side register cache. Sigh!
538 On the bright side, at least there is a regcache object. */
540 if (get_register_status (regnum
) == REG_UNKNOWN
)
542 target_fetch_registers (this, regnum
);
544 /* A number of targets can't access the whole set of raw
545 registers (because the debug API provides no means to get at
547 if (m_register_status
[regnum
] == REG_UNKNOWN
)
548 m_register_status
[regnum
] = REG_UNAVAILABLE
;
553 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
555 gdb_assert (buf
!= NULL
);
558 if (m_register_status
[regnum
] != REG_VALID
)
559 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
561 memcpy (buf
, register_buffer (regnum
),
562 m_descr
->sizeof_register
[regnum
]);
564 return m_register_status
[regnum
];
568 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
570 gdb_assert (regcache
!= NULL
);
571 return regcache
->raw_read (regnum
, val
);
574 template<typename T
, typename
>
576 readable_regcache::raw_read (int regnum
, T
*val
)
579 enum register_status status
;
581 assert_regnum (regnum
);
582 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
583 status
= raw_read (regnum
, buf
);
584 if (status
== REG_VALID
)
585 *val
= extract_integer
<T
> (buf
,
586 m_descr
->sizeof_register
[regnum
],
587 gdbarch_byte_order (m_descr
->gdbarch
));
594 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
597 gdb_assert (regcache
!= NULL
);
598 return regcache
->raw_read (regnum
, val
);
602 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
604 gdb_assert (regcache
!= NULL
);
605 regcache
->raw_write (regnum
, val
);
608 template<typename T
, typename
>
610 regcache::raw_write (int regnum
, T val
)
614 assert_regnum (regnum
);
615 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
616 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
617 gdbarch_byte_order (m_descr
->gdbarch
), val
);
618 raw_write (regnum
, buf
);
622 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
625 gdb_assert (regcache
!= NULL
);
626 regcache
->raw_write (regnum
, val
);
630 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
633 enum register_status status
;
635 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
636 if (status
== REG_UNAVAILABLE
)
637 throw_error (NOT_AVAILABLE_ERROR
,
638 _("Register %d is not available"), regnum
);
643 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
645 gdb_assert (regnum
>= 0);
646 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
647 if (regnum
< num_raw_registers ())
648 return raw_read (regnum
, buf
);
649 else if (m_has_pseudo
650 && m_register_status
[regnum
] != REG_UNKNOWN
)
652 if (m_register_status
[regnum
] == REG_VALID
)
653 memcpy (buf
, register_buffer (regnum
),
654 m_descr
->sizeof_register
[regnum
]);
656 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
658 return m_register_status
[regnum
];
660 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
662 struct value
*mark
, *computed
;
663 enum register_status result
= REG_VALID
;
665 mark
= value_mark ();
667 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
669 if (value_entirely_available (computed
))
670 memcpy (buf
, value_contents_raw (computed
),
671 m_descr
->sizeof_register
[regnum
]);
674 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
675 result
= REG_UNAVAILABLE
;
678 value_free_to_mark (mark
);
683 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
688 readable_regcache::cooked_read_value (int regnum
)
690 gdb_assert (regnum
>= 0);
691 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
693 if (regnum
< num_raw_registers ()
694 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
695 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
697 struct value
*result
;
699 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
700 VALUE_LVAL (result
) = lval_register
;
701 VALUE_REGNUM (result
) = regnum
;
703 /* It is more efficient in general to do this delegation in this
704 direction than in the other one, even though the value-based
706 if (cooked_read (regnum
,
707 value_contents_raw (result
)) == REG_UNAVAILABLE
)
708 mark_value_bytes_unavailable (result
, 0,
709 TYPE_LENGTH (value_type (result
)));
714 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
719 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
722 gdb_assert (regcache
!= NULL
);
723 return regcache
->cooked_read (regnum
, val
);
726 template<typename T
, typename
>
728 readable_regcache::cooked_read (int regnum
, T
*val
)
730 enum register_status status
;
733 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
734 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
735 status
= cooked_read (regnum
, buf
);
736 if (status
== REG_VALID
)
737 *val
= extract_integer
<T
> (buf
, m_descr
->sizeof_register
[regnum
],
738 gdbarch_byte_order (m_descr
->gdbarch
));
745 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
748 gdb_assert (regcache
!= NULL
);
749 return regcache
->cooked_read (regnum
, val
);
753 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
756 gdb_assert (regcache
!= NULL
);
757 regcache
->cooked_write (regnum
, val
);
760 template<typename T
, typename
>
762 regcache::cooked_write (int regnum
, T val
)
766 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
767 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
768 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
769 gdbarch_byte_order (m_descr
->gdbarch
), val
);
770 cooked_write (regnum
, buf
);
774 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
777 gdb_assert (regcache
!= NULL
);
778 regcache
->cooked_write (regnum
, val
);
782 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
785 gdb_assert (buf
!= NULL
);
786 assert_regnum (regnum
);
788 /* On the sparc, writing %g0 is a no-op, so we don't even want to
789 change the registers array if something writes to this register. */
790 if (gdbarch_cannot_store_register (arch (), regnum
))
793 /* If we have a valid copy of the register, and new value == old
794 value, then don't bother doing the actual store. */
795 if (get_register_status (regnum
) == REG_VALID
796 && (memcmp (register_buffer (regnum
), buf
,
797 m_descr
->sizeof_register
[regnum
]) == 0))
800 target_prepare_to_store (this);
801 raw_supply (regnum
, buf
);
803 /* Invalidate the register after it is written, in case of a
806 = make_scope_exit ([&] { this->invalidate (regnum
); });
808 target_store_registers (this, regnum
);
810 /* The target did not throw an error so we can discard invalidating
812 invalidator
.release ();
816 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
818 gdb_assert (regnum
>= 0);
819 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
820 if (regnum
< num_raw_registers ())
821 raw_write (regnum
, buf
);
823 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
827 /* See regcache.h. */
830 readable_regcache::read_part (int regnum
, int offset
, int len
,
831 gdb_byte
*out
, bool is_raw
)
833 int reg_size
= register_size (arch (), regnum
);
835 gdb_assert (out
!= NULL
);
836 gdb_assert (offset
>= 0 && offset
<= reg_size
);
837 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
839 if (offset
== 0 && len
== 0)
845 if (offset
== 0 && len
== reg_size
)
847 /* Read the full register. */
848 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
851 enum register_status status
;
852 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
854 /* Read full register to buffer. */
855 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
856 if (status
!= REG_VALID
)
860 memcpy (out
, reg
+ offset
, len
);
864 /* See regcache.h. */
867 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
870 int reg_size
= register_size (arch (), regnum
);
872 gdb_assert (out
!= nullptr);
873 gdb_assert (offset
>= 0 && offset
<= reg_size
);
874 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
876 if (offset
== 0 && len
== 0)
882 if (offset
== 0 && len
== reg_size
)
884 /* Collect the full register. */
885 return raw_collect (regnum
, out
);
888 /* Read to buffer, then write out. */
889 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
890 raw_collect (regnum
, reg
);
891 memcpy (out
, reg
+ offset
, len
);
894 /* See regcache.h. */
897 regcache::write_part (int regnum
, int offset
, int len
,
898 const gdb_byte
*in
, bool is_raw
)
900 int reg_size
= register_size (arch (), regnum
);
902 gdb_assert (in
!= NULL
);
903 gdb_assert (offset
>= 0 && offset
<= reg_size
);
904 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
906 if (offset
== 0 && len
== 0)
912 if (offset
== 0 && len
== reg_size
)
914 /* Write the full register. */
915 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
919 enum register_status status
;
920 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
922 /* Read existing register to buffer. */
923 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
924 if (status
!= REG_VALID
)
927 /* Update buffer, then write back to regcache. */
928 memcpy (reg
+ offset
, in
, len
);
929 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
933 /* See regcache.h. */
936 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
939 int reg_size
= register_size (arch (), regnum
);
941 gdb_assert (in
!= nullptr);
942 gdb_assert (offset
>= 0 && offset
<= reg_size
);
943 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
945 if (offset
== 0 && len
== 0)
951 if (offset
== 0 && len
== reg_size
)
953 /* Supply the full register. */
954 return raw_supply (regnum
, in
);
957 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
959 /* Read existing value to buffer. */
960 raw_collect (regnum
, reg
);
962 /* Write to buffer, then write out. */
963 memcpy (reg
+ offset
, in
, len
);
964 raw_supply (regnum
, reg
);
968 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
971 assert_regnum (regnum
);
972 return read_part (regnum
, offset
, len
, buf
, true);
975 /* See regcache.h. */
978 regcache::raw_write_part (int regnum
, int offset
, int len
,
981 assert_regnum (regnum
);
982 write_part (regnum
, offset
, len
, buf
, true);
985 /* See regcache.h. */
988 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
991 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
992 return read_part (regnum
, offset
, len
, buf
, false);
995 /* See regcache.h. */
998 regcache::cooked_write_part (int regnum
, int offset
, int len
,
1001 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1002 write_part (regnum
, offset
, len
, buf
, false);
1005 /* See gdbsupport/common-regcache.h. */
1008 reg_buffer::raw_supply (int regnum
, const void *buf
)
1013 assert_regnum (regnum
);
1015 regbuf
= register_buffer (regnum
);
1016 size
= m_descr
->sizeof_register
[regnum
];
1020 memcpy (regbuf
, buf
, size
);
1021 m_register_status
[regnum
] = REG_VALID
;
1025 /* This memset not strictly necessary, but better than garbage
1026 in case the register value manages to escape somewhere (due
1027 to a bug, no less). */
1028 memset (regbuf
, 0, size
);
1029 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1033 /* See regcache.h. */
1036 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1037 int addr_len
, bool is_signed
)
1039 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1043 assert_regnum (regnum
);
1045 regbuf
= register_buffer (regnum
);
1046 regsize
= m_descr
->sizeof_register
[regnum
];
1048 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1050 m_register_status
[regnum
] = REG_VALID
;
1053 /* See regcache.h. */
1056 reg_buffer::raw_supply_zeroed (int regnum
)
1061 assert_regnum (regnum
);
1063 regbuf
= register_buffer (regnum
);
1064 size
= m_descr
->sizeof_register
[regnum
];
1066 memset (regbuf
, 0, size
);
1067 m_register_status
[regnum
] = REG_VALID
;
1070 /* See gdbsupport/common-regcache.h. */
1073 reg_buffer::raw_collect (int regnum
, void *buf
) const
1078 gdb_assert (buf
!= NULL
);
1079 assert_regnum (regnum
);
1081 regbuf
= register_buffer (regnum
);
1082 size
= m_descr
->sizeof_register
[regnum
];
1083 memcpy (buf
, regbuf
, size
);
1086 /* See regcache.h. */
1089 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1090 bool is_signed
) const
1092 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1093 const gdb_byte
*regbuf
;
1096 assert_regnum (regnum
);
1098 regbuf
= register_buffer (regnum
);
1099 regsize
= m_descr
->sizeof_register
[regnum
];
1101 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1105 /* See regcache.h. */
1108 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1109 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1110 int slot_size
, int offs
) const
1112 struct gdbarch
*gdbarch
= arch ();
1113 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1115 /* Use part versions and reg_size to prevent possible buffer overflows when
1116 accessing the regcache. */
1118 if (out_buf
!= nullptr)
1120 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1122 /* Ensure any additional space is cleared. */
1123 if (slot_size
> reg_size
)
1124 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1126 else if (in_buf
!= nullptr)
1127 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1130 /* Invalidate the register. */
1131 out_regcache
->raw_supply (regnum
, nullptr);
1135 /* See regcache.h. */
1138 regcache::transfer_regset (const struct regset
*regset
,
1139 struct regcache
*out_regcache
,
1140 int regnum
, const gdb_byte
*in_buf
,
1141 gdb_byte
*out_buf
, size_t size
) const
1143 const struct regcache_map_entry
*map
;
1144 int offs
= 0, count
;
1146 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1147 (count
= map
->count
) != 0;
1150 int regno
= map
->regno
;
1151 int slot_size
= map
->size
;
1153 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1154 slot_size
= m_descr
->sizeof_register
[regno
];
1156 if (regno
== REGCACHE_MAP_SKIP
1158 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1159 offs
+= count
* slot_size
;
1161 else if (regnum
== -1)
1162 for (; count
--; regno
++, offs
+= slot_size
)
1164 if (offs
+ slot_size
> size
)
1167 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1172 /* Transfer a single register and return. */
1173 offs
+= (regnum
- regno
) * slot_size
;
1174 if (offs
+ slot_size
> size
)
1177 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1184 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1185 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1186 If BUF is NULL, set the register(s) to "unavailable" status. */
1189 regcache_supply_regset (const struct regset
*regset
,
1190 struct regcache
*regcache
,
1191 int regnum
, const void *buf
, size_t size
)
1193 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1197 regcache::supply_regset (const struct regset
*regset
,
1198 int regnum
, const void *buf
, size_t size
)
1200 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1203 /* Collect register REGNUM from REGCACHE to BUF, using the register
1204 map in REGSET. If REGNUM is -1, do this for all registers in
1208 regcache_collect_regset (const struct regset
*regset
,
1209 const struct regcache
*regcache
,
1210 int regnum
, void *buf
, size_t size
)
1212 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1216 regcache::collect_regset (const struct regset
*regset
,
1217 int regnum
, void *buf
, size_t size
) const
1219 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1222 /* See gdbsupport/common-regcache.h. */
1225 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1227 gdb_assert (buf
!= NULL
);
1228 assert_regnum (regnum
);
1230 const char *regbuf
= (const char *) register_buffer (regnum
);
1231 size_t size
= m_descr
->sizeof_register
[regnum
];
1232 gdb_assert (size
>= offset
);
1234 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1237 /* Special handling for register PC. */
1240 regcache_read_pc (struct regcache
*regcache
)
1242 struct gdbarch
*gdbarch
= regcache
->arch ();
1246 if (gdbarch_read_pc_p (gdbarch
))
1247 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1248 /* Else use per-frame method on get_current_frame. */
1249 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1253 if (regcache_cooked_read_unsigned (regcache
,
1254 gdbarch_pc_regnum (gdbarch
),
1255 &raw_val
) == REG_UNAVAILABLE
)
1256 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1258 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1261 internal_error (__FILE__
, __LINE__
,
1262 _("regcache_read_pc: Unable to find PC"));
1266 /* See gdbsupport/common-regcache.h. */
1269 regcache_read_pc_protected (regcache
*regcache
)
1274 pc
= regcache_read_pc (regcache
);
1276 catch (const gdb_exception_error
&ex
)
1285 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1287 struct gdbarch
*gdbarch
= regcache
->arch ();
1289 if (gdbarch_write_pc_p (gdbarch
))
1290 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1291 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1292 regcache_cooked_write_unsigned (regcache
,
1293 gdbarch_pc_regnum (gdbarch
), pc
);
1295 internal_error (__FILE__
, __LINE__
,
1296 _("regcache_write_pc: Unable to update PC"));
1298 /* Writing the PC (for instance, from "load") invalidates the
1300 reinit_frame_cache ();
1304 reg_buffer::num_raw_registers () const
1306 return gdbarch_num_regs (arch ());
1310 regcache::debug_print_register (const char *func
, int regno
)
1312 struct gdbarch
*gdbarch
= arch ();
1314 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1315 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1316 && gdbarch_register_name (gdbarch
, regno
) != NULL
1317 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1318 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1319 gdbarch_register_name (gdbarch
, regno
));
1321 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1322 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1324 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1325 int size
= register_size (gdbarch
, regno
);
1326 gdb_byte
*buf
= register_buffer (regno
);
1328 fprintf_unfiltered (gdb_stdlog
, " = ");
1329 for (int i
= 0; i
< size
; i
++)
1331 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1333 if (size
<= sizeof (LONGEST
))
1335 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1337 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1338 core_addr_to_string_nz (val
), plongest (val
));
1341 fprintf_unfiltered (gdb_stdlog
, "\n");
1345 reg_flush_command (const char *command
, int from_tty
)
1347 /* Force-flush the register cache. */
1348 registers_changed ();
1350 printf_filtered (_("Register cache flushed.\n"));
1354 register_dump::dump (ui_file
*file
)
1356 auto descr
= regcache_descr (m_gdbarch
);
1358 int footnote_nr
= 0;
1359 int footnote_register_offset
= 0;
1360 int footnote_register_type_name_null
= 0;
1361 long register_offset
= 0;
1363 gdb_assert (descr
->nr_cooked_registers
1364 == gdbarch_num_cooked_regs (m_gdbarch
));
1366 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1370 fprintf_unfiltered (file
, " %-10s", "Name");
1373 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1377 else if (p
[0] == '\0')
1379 fprintf_unfiltered (file
, " %-10s", p
);
1384 fprintf_unfiltered (file
, " %4s", "Nr");
1386 fprintf_unfiltered (file
, " %4d", regnum
);
1388 /* Relative number. */
1390 fprintf_unfiltered (file
, " %4s", "Rel");
1391 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1392 fprintf_unfiltered (file
, " %4d", regnum
);
1394 fprintf_unfiltered (file
, " %4d",
1395 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1399 fprintf_unfiltered (file
, " %6s ", "Offset");
1402 fprintf_unfiltered (file
, " %6ld",
1403 descr
->register_offset
[regnum
]);
1404 if (register_offset
!= descr
->register_offset
[regnum
]
1406 && (descr
->register_offset
[regnum
]
1407 != (descr
->register_offset
[regnum
- 1]
1408 + descr
->sizeof_register
[regnum
- 1])))
1411 if (!footnote_register_offset
)
1412 footnote_register_offset
= ++footnote_nr
;
1413 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1416 fprintf_unfiltered (file
, " ");
1417 register_offset
= (descr
->register_offset
[regnum
]
1418 + descr
->sizeof_register
[regnum
]);
1423 fprintf_unfiltered (file
, " %5s ", "Size");
1425 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1430 std::string name_holder
;
1436 static const char blt
[] = "builtin_type";
1438 t
= register_type (m_gdbarch
, regnum
)->name ();
1441 if (!footnote_register_type_name_null
)
1442 footnote_register_type_name_null
= ++footnote_nr
;
1443 name_holder
= string_printf ("*%d",
1444 footnote_register_type_name_null
);
1445 t
= name_holder
.c_str ();
1447 /* Chop a leading builtin_type. */
1448 if (startswith (t
, blt
))
1451 fprintf_unfiltered (file
, " %-15s", t
);
1454 /* Leading space always present. */
1455 fprintf_unfiltered (file
, " ");
1457 dump_reg (file
, regnum
);
1459 fprintf_unfiltered (file
, "\n");
1462 if (footnote_register_offset
)
1463 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1464 footnote_register_offset
);
1465 if (footnote_register_type_name_null
)
1466 fprintf_unfiltered (file
,
1467 "*%d: Register type's name NULL.\n",
1468 footnote_register_type_name_null
);
1472 #include "gdbsupport/selftest.h"
1473 #include "selftest-arch.h"
1474 #include "target-float.h"
1476 namespace selftests
{
1482 for (auto it
= regcaches
.begin (); it
!= regcaches
.end (); ++it
)
1484 auto &ptid_regc_map
= it
->second
;
1485 size
+= ptid_regc_map
.size ();
1491 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1494 test_get_thread_arch_aspace_regcache (process_stratum_target
*target
,
1495 ptid_t ptid
, struct gdbarch
*gdbarch
,
1496 address_space
*aspace
)
1498 struct regcache
*regcache
1499 = get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
1500 SELF_CHECK (regcache
!= NULL
);
1501 SELF_CHECK (regcache
->target () == target
);
1502 SELF_CHECK (regcache
->ptid () == ptid
);
1503 SELF_CHECK (regcache
->aspace () == aspace
);
1509 /* It is empty at the start. */
1510 SELF_CHECK (regcaches_size () == 0);
1512 ptid_t
ptid1 (1), ptid2 (2), ptid3 (3);
1514 test_target_ops test_target1
;
1515 test_target_ops test_target2
;
1517 /* Get regcache from (target1,ptid1), a new regcache is added to
1519 test_get_thread_arch_aspace_regcache (&test_target1
, ptid1
,
1522 SELF_CHECK (regcaches_size () == 1);
1524 /* Get regcache from (target1,ptid2), a new regcache is added to
1526 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1529 SELF_CHECK (regcaches_size () == 2);
1531 /* Get regcache from (target1,ptid3), a new regcache is added to
1533 test_get_thread_arch_aspace_regcache (&test_target1
, ptid3
,
1536 SELF_CHECK (regcaches_size () == 3);
1538 /* Get regcache from (target1,ptid2) again, nothing is added to
1540 test_get_thread_arch_aspace_regcache (&test_target1
, ptid2
,
1543 SELF_CHECK (regcaches_size () == 3);
1545 /* Get regcache from (target2,ptid2), a new regcache is added to
1546 REGCACHES, since this time we're using a different target. */
1547 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1550 SELF_CHECK (regcaches_size () == 4);
1552 /* Mark that (target1,ptid2) changed. The regcache of (target1,
1553 ptid2) should be removed from REGCACHES. */
1554 registers_changed_ptid (&test_target1
, ptid2
);
1555 SELF_CHECK (regcaches_size () == 3);
1557 /* Get the regcache from (target2,ptid2) again, confirming the
1558 registers_changed_ptid call above did not delete it. */
1559 test_get_thread_arch_aspace_regcache (&test_target2
, ptid2
,
1562 SELF_CHECK (regcaches_size () == 3);
1564 /* Confirm that marking all regcaches of all targets as changed
1565 clears REGCACHES. */
1566 registers_changed_ptid (nullptr, minus_one_ptid
);
1567 SELF_CHECK (regcaches_size () == 0);
1570 class target_ops_no_register
: public test_target_ops
1573 target_ops_no_register ()
1574 : test_target_ops
{}
1579 fetch_registers_called
= 0;
1580 store_registers_called
= 0;
1581 xfer_partial_called
= 0;
1584 void fetch_registers (regcache
*regs
, int regno
) override
;
1585 void store_registers (regcache
*regs
, int regno
) override
;
1587 enum target_xfer_status
xfer_partial (enum target_object object
,
1588 const char *annex
, gdb_byte
*readbuf
,
1589 const gdb_byte
*writebuf
,
1590 ULONGEST offset
, ULONGEST len
,
1591 ULONGEST
*xfered_len
) override
;
1593 unsigned int fetch_registers_called
= 0;
1594 unsigned int store_registers_called
= 0;
1595 unsigned int xfer_partial_called
= 0;
1599 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1601 /* Mark register available. */
1602 regs
->raw_supply_zeroed (regno
);
1603 this->fetch_registers_called
++;
1607 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1609 this->store_registers_called
++;
1612 enum target_xfer_status
1613 target_ops_no_register::xfer_partial (enum target_object object
,
1614 const char *annex
, gdb_byte
*readbuf
,
1615 const gdb_byte
*writebuf
,
1616 ULONGEST offset
, ULONGEST len
,
1617 ULONGEST
*xfered_len
)
1619 this->xfer_partial_called
++;
1622 return TARGET_XFER_OK
;
1625 class readwrite_regcache
: public regcache
1628 readwrite_regcache (process_stratum_target
*target
,
1629 struct gdbarch
*gdbarch
)
1630 : regcache (target
, gdbarch
, nullptr)
1634 /* Test regcache::cooked_read gets registers from raw registers and
1635 memory instead of target to_{fetch,store}_registers. */
1638 cooked_read_test (struct gdbarch
*gdbarch
)
1640 scoped_mock_context
<target_ops_no_register
> mockctx (gdbarch
);
1642 /* Test that read one raw register from regcache_no_target will go
1643 to the target layer. */
1645 /* Find a raw register which size isn't zero. */
1647 for (nonzero_regnum
= 0;
1648 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1651 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1655 readwrite_regcache
readwrite (&mockctx
.mock_target
, gdbarch
);
1656 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1658 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1660 /* raw_read calls target_fetch_registers. */
1661 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
> 0);
1662 mockctx
.mock_target
.reset ();
1664 /* Mark all raw registers valid, so the following raw registers
1665 accesses won't go to target. */
1666 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1667 readwrite
.raw_update (i
);
1669 mockctx
.mock_target
.reset ();
1670 /* Then, read all raw and pseudo registers, and don't expect calling
1671 to_{fetch,store}_registers. */
1672 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1674 if (register_size (gdbarch
, regnum
) == 0)
1677 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1679 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1680 inner_buf
.data ()));
1682 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1683 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1684 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1686 mockctx
.mock_target
.reset ();
1689 readonly_detached_regcache
readonly (readwrite
);
1691 /* GDB may go to target layer to fetch all registers and memory for
1692 readonly regcache. */
1693 mockctx
.mock_target
.reset ();
1695 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1697 if (register_size (gdbarch
, regnum
) == 0)
1700 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1701 enum register_status status
= readonly
.cooked_read (regnum
,
1704 if (regnum
< gdbarch_num_regs (gdbarch
))
1706 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1708 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1709 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1710 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1711 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1712 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1713 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1714 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1715 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1717 /* Raw registers. If raw registers are not in save_reggroup,
1718 their status are unknown. */
1719 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1720 SELF_CHECK (status
== REG_VALID
);
1722 SELF_CHECK (status
== REG_UNKNOWN
);
1725 SELF_CHECK (status
== REG_VALID
);
1729 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1730 SELF_CHECK (status
== REG_VALID
);
1733 /* If pseudo registers are not in save_reggroup, some of
1734 them can be computed from saved raw registers, but some
1735 of them are unknown. */
1736 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1738 if (bfd_arch
== bfd_arch_frv
1739 || bfd_arch
== bfd_arch_m32c
1740 || bfd_arch
== bfd_arch_mep
1741 || bfd_arch
== bfd_arch_sh
)
1742 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1743 else if (bfd_arch
== bfd_arch_mips
1744 || bfd_arch
== bfd_arch_h8300
)
1745 SELF_CHECK (status
== REG_UNKNOWN
);
1747 SELF_CHECK (status
== REG_VALID
);
1751 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1752 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1753 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1755 mockctx
.mock_target
.reset ();
1759 /* Test regcache::cooked_write by writing some expected contents to
1760 registers, and checking that contents read from registers and the
1761 expected contents are the same. */
1764 cooked_write_test (struct gdbarch
*gdbarch
)
1766 /* Error out if debugging something, because we're going to push the
1767 test target, which would pop any existing target. */
1768 if (current_top_target ()->stratum () >= process_stratum
)
1769 error (_("target already pushed"));
1771 /* Create a mock environment. A process_stratum target pushed. */
1773 target_ops_no_register mock_target
;
1775 /* Push the process_stratum target so we can mock accessing
1777 push_target (&mock_target
);
1779 /* Pop it again on exit (return/exception). */
1784 pop_all_targets_at_and_above (process_stratum
);
1788 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1790 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1792 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1794 if (register_size (gdbarch
, regnum
) == 0
1795 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1798 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1800 if (bfd_arch
== bfd_arch_sparc
1801 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1802 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1803 && gdbarch_ptr_bit (gdbarch
) == 64
1804 && (regnum
>= gdbarch_num_regs (gdbarch
)
1805 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1808 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1809 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1810 const auto type
= register_type (gdbarch
, regnum
);
1812 if (type
->code () == TYPE_CODE_FLT
1813 || type
->code () == TYPE_CODE_DECFLOAT
)
1815 /* Generate valid float format. */
1816 target_float_from_string (expected
.data (), type
, "1.25");
1818 else if (type
->code () == TYPE_CODE_INT
1819 || type
->code () == TYPE_CODE_ARRAY
1820 || type
->code () == TYPE_CODE_PTR
1821 || type
->code () == TYPE_CODE_UNION
1822 || type
->code () == TYPE_CODE_STRUCT
)
1824 if (bfd_arch
== bfd_arch_ia64
1825 || (regnum
>= gdbarch_num_regs (gdbarch
)
1826 && (bfd_arch
== bfd_arch_xtensa
1827 || bfd_arch
== bfd_arch_bfin
1828 || bfd_arch
== bfd_arch_m32c
1829 /* m68hc11 pseudo registers are in memory. */
1830 || bfd_arch
== bfd_arch_m68hc11
1831 || bfd_arch
== bfd_arch_m68hc12
1832 || bfd_arch
== bfd_arch_s390
))
1833 || (bfd_arch
== bfd_arch_frv
1834 /* FRV pseudo registers except iacc0. */
1835 && regnum
> gdbarch_num_regs (gdbarch
)))
1837 /* Skip setting the expected values for some architecture
1840 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
1842 /* RL78_PC_REGNUM */
1843 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
1848 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
1852 else if (type
->code () == TYPE_CODE_FLAGS
)
1854 /* No idea how to test flags. */
1859 /* If we don't know how to create the expected value for the
1860 this type, make it fail. */
1864 readwrite
.cooked_write (regnum
, expected
.data ());
1866 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
1867 SELF_CHECK (expected
== buf
);
1871 /* Verify that when two threads with the same ptid exist (from two different
1872 targets) and one of them changes ptid, we only update the appropriate
1876 regcache_thread_ptid_changed ()
1878 /* This test relies on the global regcache list to initially be empty. */
1879 registers_changed ();
1881 /* Any arch will do. */
1882 gdbarch
*arch
= current_inferior ()->gdbarch
;
1884 /* Prepare two targets with one thread each, with the same ptid. */
1885 scoped_mock_context
<test_target_ops
> target1 (arch
);
1886 scoped_mock_context
<test_target_ops
> target2 (arch
);
1887 target2
.mock_inferior
.next
= &target1
.mock_inferior
;
1889 ptid_t
old_ptid (111, 222);
1890 ptid_t
new_ptid (111, 333);
1892 target1
.mock_inferior
.pid
= old_ptid
.pid ();
1893 target1
.mock_thread
.ptid
= old_ptid
;
1894 target2
.mock_inferior
.pid
= old_ptid
.pid ();
1895 target2
.mock_thread
.ptid
= old_ptid
;
1897 gdb_assert (regcaches
.empty ());
1899 /* Populate the regcaches container. */
1900 get_thread_arch_aspace_regcache (&target1
.mock_target
, old_ptid
, arch
,
1902 get_thread_arch_aspace_regcache (&target2
.mock_target
, old_ptid
, arch
,
1905 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1906 auto regcache_count
= [] (process_stratum_target
*target
, ptid_t ptid
)
1909 auto ptid_regc_map_it
= regcaches
.find (target
);
1910 if (ptid_regc_map_it
!= regcaches
.end ())
1912 auto &ptid_regc_map
= ptid_regc_map_it
->second
;
1913 auto range
= ptid_regc_map
.equal_range (ptid
);
1914 return std::distance (range
.first
, range
.second
);
1919 gdb_assert (regcaches
.size () == 2);
1920 gdb_assert (regcache_count (&target1
.mock_target
, old_ptid
) == 1);
1921 gdb_assert (regcache_count (&target1
.mock_target
, new_ptid
) == 0);
1922 gdb_assert (regcache_count (&target2
.mock_target
, old_ptid
) == 1);
1923 gdb_assert (regcache_count (&target2
.mock_target
, new_ptid
) == 0);
1925 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
1927 gdb_assert (regcaches
.size () == 2);
1928 gdb_assert (regcache_count (&target1
.mock_target
, old_ptid
) == 0);
1929 gdb_assert (regcache_count (&target1
.mock_target
, new_ptid
) == 1);
1930 gdb_assert (regcache_count (&target2
.mock_target
, old_ptid
) == 1);
1931 gdb_assert (regcache_count (&target2
.mock_target
, new_ptid
) == 0);
1933 /* Leave the regcache list empty. */
1934 registers_changed ();
1935 gdb_assert (regcaches
.empty ());
1938 } // namespace selftests
1939 #endif /* GDB_SELF_TEST */
1941 void _initialize_regcache ();
1943 _initialize_regcache ()
1945 regcache_descr_handle
1946 = gdbarch_data_register_post_init (init_regcache_descr
);
1948 gdb::observers::target_changed
.attach (regcache_observer_target_changed
);
1949 gdb::observers::thread_ptid_changed
.attach (regcache_thread_ptid_changed
);
1951 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1952 _("Force gdb to flush its register cache (maintainer command)."));
1955 selftests::register_test ("regcaches", selftests::regcaches_test
);
1957 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1958 selftests::cooked_read_test
);
1959 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1960 selftests::cooked_write_test
);
1961 selftests::register_test ("regcache_thread_ptid_changed",
1962 selftests::regcache_thread_ptid_changed
);