1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2021 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>
33 #include "cli/cli-cmds.h"
38 * Here is the actual register cache.
41 /* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created. */
44 static struct gdbarch_data
*regcache_descr_handle
;
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch
*gdbarch
;
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
54 registers then those registers and not the PC lives in the raw
56 long sizeof_raw_registers
;
58 /* The cooked register space. Each cooked register in the range
59 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
60 register. The remaining [NR_RAW_REGISTERS
61 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
62 both raw registers and memory by the architecture methods
63 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
64 int nr_cooked_registers
;
65 long sizeof_cooked_registers
;
67 /* Offset and size (in 8 bit bytes), of each register in the
68 register cache. All registers (including those in the range
69 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
71 long *register_offset
;
72 long *sizeof_register
;
74 /* Cached table containing the type of each register. */
75 struct type
**register_type
;
79 init_regcache_descr (struct gdbarch
*gdbarch
)
82 struct regcache_descr
*descr
;
83 gdb_assert (gdbarch
!= NULL
);
85 /* Create an initial, zero filled, table. */
86 descr
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct regcache_descr
);
87 descr
->gdbarch
= gdbarch
;
89 /* Total size of the register space. The raw registers are mapped
90 directly onto the raw register cache while the pseudo's are
91 either mapped onto raw-registers or memory. */
92 descr
->nr_cooked_registers
= gdbarch_num_cooked_regs (gdbarch
);
94 /* Fill in a table of register types. */
96 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
,
98 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
99 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
101 /* Construct a strictly RAW register cache. Don't allow pseudo's
102 into the register cache. */
104 /* Lay out the register cache.
106 NOTE: cagney/2002-05-22: Only register_type () is used when
107 constructing the register cache. It is assumed that the
108 register's raw size, virtual size and type length are all the
114 descr
->sizeof_register
115 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
116 descr
->register_offset
117 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
118 for (i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
120 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
121 descr
->register_offset
[i
] = offset
;
122 offset
+= descr
->sizeof_register
[i
];
124 /* Set the real size of the raw register cache buffer. */
125 descr
->sizeof_raw_registers
= offset
;
127 for (; i
< descr
->nr_cooked_registers
; i
++)
129 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
130 descr
->register_offset
[i
] = offset
;
131 offset
+= descr
->sizeof_register
[i
];
133 /* Set the real size of the readonly register cache buffer. */
134 descr
->sizeof_cooked_registers
= offset
;
140 static struct regcache_descr
*
141 regcache_descr (struct gdbarch
*gdbarch
)
143 return (struct regcache_descr
*) gdbarch_data (gdbarch
,
144 regcache_descr_handle
);
147 /* Utility functions returning useful register attributes stored in
148 the regcache descr. */
151 register_type (struct gdbarch
*gdbarch
, int regnum
)
153 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
155 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
156 return descr
->register_type
[regnum
];
159 /* Utility functions returning useful register attributes stored in
160 the regcache descr. */
163 register_size (struct gdbarch
*gdbarch
, int regnum
)
165 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
168 gdb_assert (regnum
>= 0 && regnum
< gdbarch_num_cooked_regs (gdbarch
));
169 size
= descr
->sizeof_register
[regnum
];
173 /* See gdbsupport/common-regcache.h. */
176 regcache_register_size (const struct regcache
*regcache
, int n
)
178 return register_size (regcache
->arch (), n
);
181 reg_buffer::reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
)
182 : m_has_pseudo (has_pseudo
)
184 gdb_assert (gdbarch
!= NULL
);
185 m_descr
= regcache_descr (gdbarch
);
187 /* We don't zero-initialize the M_REGISTERS array, as the bytes it contains
188 aren't meaningful as long as the corresponding register status is not
192 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_cooked_registers
]);
193 m_register_status
.reset
194 (new register_status
[m_descr
->nr_cooked_registers
] ());
198 m_registers
.reset (new gdb_byte
[m_descr
->sizeof_raw_registers
]);
199 m_register_status
.reset
200 (new register_status
[gdbarch_num_regs (gdbarch
)] ());
204 regcache::regcache (process_stratum_target
*target
, gdbarch
*gdbarch
,
205 const address_space
*aspace_
)
206 /* The register buffers. A read/write register cache can only hold
207 [0 .. gdbarch_num_regs). */
208 : detached_regcache (gdbarch
, false), m_aspace (aspace_
), m_target (target
)
210 m_ptid
= minus_one_ptid
;
213 readonly_detached_regcache::readonly_detached_regcache (regcache
&src
)
214 : readonly_detached_regcache (src
.arch (),
215 [&src
] (int regnum
, gdb_byte
*buf
)
217 return src
.cooked_read (regnum
, buf
);
223 reg_buffer::arch () const
225 return m_descr
->gdbarch
;
228 /* Return a pointer to register REGNUM's buffer cache. */
231 reg_buffer::register_buffer (int regnum
) const
233 return m_registers
.get () + m_descr
->register_offset
[regnum
];
237 reg_buffer::save (register_read_ftype cooked_read
)
239 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
242 /* It should have pseudo registers. */
243 gdb_assert (m_has_pseudo
);
244 /* Clear the dest. */
245 memset (m_registers
.get (), 0, m_descr
->sizeof_cooked_registers
);
246 memset (m_register_status
.get (), REG_UNKNOWN
, m_descr
->nr_cooked_registers
);
247 /* Copy over any registers (identified by their membership in the
248 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
249 gdbarch_num_pseudo_regs) range is checked since some architectures need
250 to save/restore `cooked' registers that live in memory. */
251 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
253 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
255 gdb_byte
*dst_buf
= register_buffer (regnum
);
256 enum register_status status
= cooked_read (regnum
, dst_buf
);
258 gdb_assert (status
!= REG_UNKNOWN
);
260 if (status
!= REG_VALID
)
261 memset (dst_buf
, 0, register_size (gdbarch
, regnum
));
263 m_register_status
[regnum
] = status
;
269 regcache::restore (readonly_detached_regcache
*src
)
271 struct gdbarch
*gdbarch
= m_descr
->gdbarch
;
274 gdb_assert (src
!= NULL
);
275 gdb_assert (src
->m_has_pseudo
);
277 gdb_assert (gdbarch
== src
->arch ());
279 /* Copy over any registers, being careful to only restore those that
280 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
281 + gdbarch_num_pseudo_regs) range is checked since some architectures need
282 to save/restore `cooked' registers that live in memory. */
283 for (regnum
= 0; regnum
< m_descr
->nr_cooked_registers
; regnum
++)
285 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
287 if (src
->m_register_status
[regnum
] == REG_VALID
)
288 cooked_write (regnum
, src
->register_buffer (regnum
));
293 /* See gdbsupport/common-regcache.h. */
296 reg_buffer::get_register_status (int regnum
) const
298 assert_regnum (regnum
);
300 return m_register_status
[regnum
];
304 reg_buffer::invalidate (int regnum
)
306 assert_regnum (regnum
);
307 m_register_status
[regnum
] = REG_UNKNOWN
;
311 reg_buffer::assert_regnum (int regnum
) const
313 gdb_assert (regnum
>= 0);
315 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
317 gdb_assert (regnum
< gdbarch_num_regs (arch ()));
320 /* Type to map a ptid to a list of regcaches (one thread may have multiple
321 regcaches, associated to different gdbarches). */
323 using ptid_regcache_map
324 = std::unordered_multimap
<ptid_t
, regcache_up
, hash_ptid
>;
326 /* Type holding regcaches for a given pid. */
328 using pid_ptid_regcache_map
= std::unordered_map
<int, ptid_regcache_map
>;
330 /* Type holding regcaches for a given target. */
332 using target_pid_ptid_regcache_map
333 = std::unordered_map
<process_stratum_target
*, pid_ptid_regcache_map
>;
335 /* Global structure containing the existing regcaches. */
337 /* NOTE: this is a write-through cache. There is no "dirty" bit for
338 recording if the register values have been changed (eg. by the
339 user). Therefore all registers must be written back to the
340 target when appropriate. */
341 static target_pid_ptid_regcache_map regcaches
;
344 get_thread_arch_aspace_regcache (process_stratum_target
*target
,
345 ptid_t ptid
, gdbarch
*arch
,
346 struct address_space
*aspace
)
348 gdb_assert (target
!= nullptr);
350 /* Find the map for this target. */
351 pid_ptid_regcache_map
&pid_ptid_regc_map
= regcaches
[target
];
353 /* Find the map for this pid. */
354 ptid_regcache_map
&ptid_regc_map
= pid_ptid_regc_map
[ptid
.pid ()];
356 /* Check first if a regcache for this arch already exists. */
357 auto range
= ptid_regc_map
.equal_range (ptid
);
358 for (auto it
= range
.first
; it
!= range
.second
; ++it
)
360 if (it
->second
->arch () == arch
)
361 return it
->second
.get ();
364 /* It does not exist, create it. */
365 regcache
*new_regcache
= new regcache (target
, arch
, aspace
);
366 new_regcache
->set_ptid (ptid
);
367 /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
368 constructor explictly instead of implicitly. */
369 ptid_regc_map
.insert (std::make_pair (ptid
, regcache_up (new_regcache
)));
375 get_thread_arch_regcache (process_stratum_target
*target
, ptid_t ptid
,
376 struct gdbarch
*gdbarch
)
378 scoped_restore_current_inferior restore_current_inferior
;
379 set_current_inferior (find_inferior_ptid (target
, ptid
));
380 address_space
*aspace
= target_thread_address_space (ptid
);
382 return get_thread_arch_aspace_regcache (target
, ptid
, gdbarch
, aspace
);
385 static process_stratum_target
*current_thread_target
;
386 static ptid_t current_thread_ptid
;
387 static struct gdbarch
*current_thread_arch
;
390 get_thread_regcache (process_stratum_target
*target
, ptid_t ptid
)
392 if (!current_thread_arch
393 || target
!= current_thread_target
394 || current_thread_ptid
!= ptid
)
396 gdb_assert (ptid
!= null_ptid
);
398 current_thread_ptid
= ptid
;
399 current_thread_target
= target
;
401 scoped_restore_current_inferior restore_current_inferior
;
402 set_current_inferior (find_inferior_ptid (target
, ptid
));
403 current_thread_arch
= target_thread_architecture (ptid
);
406 return get_thread_arch_regcache (target
, ptid
, current_thread_arch
);
409 /* See regcache.h. */
412 get_thread_regcache (thread_info
*thread
)
414 return get_thread_regcache (thread
->inf
->process_target (),
419 get_current_regcache (void)
421 return get_thread_regcache (inferior_thread ());
424 /* See gdbsupport/common-regcache.h. */
427 get_thread_regcache_for_ptid (ptid_t ptid
)
429 /* This function doesn't take a process_stratum_target parameter
430 because it's a gdbsupport/ routine implemented by both gdb and
431 gdbserver. It always refers to a ptid of the current target. */
432 process_stratum_target
*proc_target
= current_inferior ()->process_target ();
433 return get_thread_regcache (proc_target
, ptid
);
436 /* Observer for the target_changed event. */
439 regcache_observer_target_changed (struct target_ops
*target
)
441 registers_changed ();
444 /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
446 regcache_thread_ptid_changed (process_stratum_target
*target
,
447 ptid_t old_ptid
, ptid_t new_ptid
)
449 /* Look up map for target. */
450 auto pid_ptid_regc_map_it
= regcaches
.find (target
);
451 if (pid_ptid_regc_map_it
== regcaches
.end ())
454 /* Look up map for pid. */
455 pid_ptid_regcache_map
&pid_ptid_regc_map
= pid_ptid_regc_map_it
->second
;
456 auto ptid_regc_map_it
= pid_ptid_regc_map
.find (old_ptid
.pid ());
457 if (ptid_regc_map_it
== pid_ptid_regc_map
.end ())
460 /* Update all regcaches belonging to old_ptid. */
461 ptid_regcache_map
&ptid_regc_map
= ptid_regc_map_it
->second
;
462 auto range
= ptid_regc_map
.equal_range (old_ptid
);
463 for (auto it
= range
.first
; it
!= range
.second
;)
465 regcache_up rc
= std::move (it
->second
);
466 rc
->set_ptid (new_ptid
);
468 /* Remove old before inserting new, to avoid rehashing,
469 which would invalidate iterators. */
470 it
= ptid_regc_map
.erase (it
);
471 ptid_regc_map
.insert (std::make_pair (new_ptid
, std::move (rc
)));
475 /* Low level examining and depositing of registers.
477 The caller is responsible for making sure that the inferior is
478 stopped before calling the fetching routines, or it will get
479 garbage. (a change from GDB version 3, in which the caller got the
480 value from the last stop). */
482 /* REGISTERS_CHANGED ()
484 Indicate that registers may have changed, so invalidate the cache. */
487 registers_changed_ptid (process_stratum_target
*target
, ptid_t ptid
)
489 if (target
== nullptr)
491 /* Since there can be ptid clashes between targets, it's not valid to
492 pass a ptid without saying to which target it belongs. */
493 gdb_assert (ptid
== minus_one_ptid
);
495 /* Delete all the regcaches of all targets. */
498 else if (ptid
.is_pid ())
500 /* Non-NULL target and pid ptid, delete all regcaches belonging
501 to this (TARGET, PID). */
503 /* Look up map for target. */
504 auto pid_ptid_regc_map_it
= regcaches
.find (target
);
505 if (pid_ptid_regc_map_it
!= regcaches
.end ())
507 pid_ptid_regcache_map
&pid_ptid_regc_map
508 = pid_ptid_regc_map_it
->second
;
510 pid_ptid_regc_map
.erase (ptid
.pid ());
513 else if (ptid
!= minus_one_ptid
)
515 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
516 to this (TARGET, PTID). */
518 /* Look up map for target. */
519 auto pid_ptid_regc_map_it
= regcaches
.find (target
);
520 if (pid_ptid_regc_map_it
!= regcaches
.end ())
522 pid_ptid_regcache_map
&pid_ptid_regc_map
523 = pid_ptid_regc_map_it
->second
;
525 /* Look up map for pid. */
526 auto ptid_regc_map_it
527 = pid_ptid_regc_map
.find (ptid
.pid ());
528 if (ptid_regc_map_it
!= pid_ptid_regc_map
.end ())
530 ptid_regcache_map
&ptid_regc_map
531 = ptid_regc_map_it
->second
;
533 ptid_regc_map
.erase (ptid
);
539 /* Non-NULL target and minus_one_ptid, delete all regcaches
540 associated to this target. */
541 regcaches
.erase (target
);
544 if ((target
== nullptr || current_thread_target
== target
)
545 && current_thread_ptid
.matches (ptid
))
547 current_thread_target
= NULL
;
548 current_thread_ptid
= null_ptid
;
549 current_thread_arch
= NULL
;
552 if ((target
== nullptr || current_inferior ()->process_target () == target
)
553 && inferior_ptid
.matches (ptid
))
555 /* We just deleted the regcache of the current thread. Need to
556 forget about any frames we have cached, too. */
557 reinit_frame_cache ();
561 /* See regcache.h. */
564 registers_changed_thread (thread_info
*thread
)
566 registers_changed_ptid (thread
->inf
->process_target (), thread
->ptid
);
570 registers_changed (void)
572 registers_changed_ptid (nullptr, minus_one_ptid
);
576 regcache::raw_update (int regnum
)
578 assert_regnum (regnum
);
580 /* Make certain that the register cache is up-to-date with respect
581 to the current thread. This switching shouldn't be necessary
582 only there is still only one target side register cache. Sigh!
583 On the bright side, at least there is a regcache object. */
585 if (get_register_status (regnum
) == REG_UNKNOWN
)
587 target_fetch_registers (this, regnum
);
589 /* A number of targets can't access the whole set of raw
590 registers (because the debug API provides no means to get at
592 if (m_register_status
[regnum
] == REG_UNKNOWN
)
593 m_register_status
[regnum
] = REG_UNAVAILABLE
;
598 readable_regcache::raw_read (int regnum
, gdb_byte
*buf
)
600 gdb_assert (buf
!= NULL
);
603 if (m_register_status
[regnum
] != REG_VALID
)
604 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
606 memcpy (buf
, register_buffer (regnum
),
607 m_descr
->sizeof_register
[regnum
]);
609 return m_register_status
[regnum
];
613 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
615 gdb_assert (regcache
!= NULL
);
616 return regcache
->raw_read (regnum
, val
);
619 template<typename T
, typename
>
621 readable_regcache::raw_read (int regnum
, T
*val
)
623 assert_regnum (regnum
);
624 size_t len
= m_descr
->sizeof_register
[regnum
];
625 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
626 register_status status
= raw_read (regnum
, buf
);
627 if (status
== REG_VALID
)
628 *val
= extract_integer
<T
> ({buf
, len
},
629 gdbarch_byte_order (m_descr
->gdbarch
));
636 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
639 gdb_assert (regcache
!= NULL
);
640 return regcache
->raw_read (regnum
, val
);
644 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
646 gdb_assert (regcache
!= NULL
);
647 regcache
->raw_write (regnum
, val
);
650 template<typename T
, typename
>
652 regcache::raw_write (int regnum
, T val
)
656 assert_regnum (regnum
);
657 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
658 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
659 gdbarch_byte_order (m_descr
->gdbarch
), val
);
660 raw_write (regnum
, buf
);
664 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
667 gdb_assert (regcache
!= NULL
);
668 regcache
->raw_write (regnum
, val
);
672 regcache_raw_get_signed (struct regcache
*regcache
, int regnum
)
675 enum register_status status
;
677 status
= regcache_raw_read_signed (regcache
, regnum
, &value
);
678 if (status
== REG_UNAVAILABLE
)
679 throw_error (NOT_AVAILABLE_ERROR
,
680 _("Register %d is not available"), regnum
);
685 readable_regcache::cooked_read (int regnum
, gdb_byte
*buf
)
687 gdb_assert (regnum
>= 0);
688 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
689 if (regnum
< num_raw_registers ())
690 return raw_read (regnum
, buf
);
691 else if (m_has_pseudo
692 && m_register_status
[regnum
] != REG_UNKNOWN
)
694 if (m_register_status
[regnum
] == REG_VALID
)
695 memcpy (buf
, register_buffer (regnum
),
696 m_descr
->sizeof_register
[regnum
]);
698 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
700 return m_register_status
[regnum
];
702 else if (gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
704 struct value
*mark
, *computed
;
705 enum register_status result
= REG_VALID
;
707 mark
= value_mark ();
709 computed
= gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
711 if (value_entirely_available (computed
))
712 memcpy (buf
, value_contents_raw (computed
).data (),
713 m_descr
->sizeof_register
[regnum
]);
716 memset (buf
, 0, m_descr
->sizeof_register
[regnum
]);
717 result
= REG_UNAVAILABLE
;
720 value_free_to_mark (mark
);
725 return gdbarch_pseudo_register_read (m_descr
->gdbarch
, this,
730 readable_regcache::cooked_read_value (int regnum
)
732 gdb_assert (regnum
>= 0);
733 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
735 if (regnum
< num_raw_registers ()
736 || (m_has_pseudo
&& m_register_status
[regnum
] != REG_UNKNOWN
)
737 || !gdbarch_pseudo_register_read_value_p (m_descr
->gdbarch
))
739 struct value
*result
;
741 result
= allocate_value (register_type (m_descr
->gdbarch
, regnum
));
742 VALUE_LVAL (result
) = lval_register
;
743 VALUE_REGNUM (result
) = regnum
;
745 /* It is more efficient in general to do this delegation in this
746 direction than in the other one, even though the value-based
748 if (cooked_read (regnum
,
749 value_contents_raw (result
).data ()) == REG_UNAVAILABLE
)
750 mark_value_bytes_unavailable (result
, 0,
751 TYPE_LENGTH (value_type (result
)));
756 return gdbarch_pseudo_register_read_value (m_descr
->gdbarch
,
761 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
764 gdb_assert (regcache
!= NULL
);
765 return regcache
->cooked_read (regnum
, val
);
768 template<typename T
, typename
>
770 readable_regcache::cooked_read (int regnum
, T
*val
)
772 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
773 size_t len
= m_descr
->sizeof_register
[regnum
];
774 gdb_byte
*buf
= (gdb_byte
*) alloca (len
);
775 register_status status
= cooked_read (regnum
, buf
);
776 if (status
== REG_VALID
)
777 *val
= extract_integer
<T
> ({buf
, len
},
778 gdbarch_byte_order (m_descr
->gdbarch
));
785 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
788 gdb_assert (regcache
!= NULL
);
789 return regcache
->cooked_read (regnum
, val
);
793 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
796 gdb_assert (regcache
!= NULL
);
797 regcache
->cooked_write (regnum
, val
);
800 template<typename T
, typename
>
802 regcache::cooked_write (int regnum
, T val
)
806 gdb_assert (regnum
>=0 && regnum
< m_descr
->nr_cooked_registers
);
807 buf
= (gdb_byte
*) alloca (m_descr
->sizeof_register
[regnum
]);
808 store_integer (buf
, m_descr
->sizeof_register
[regnum
],
809 gdbarch_byte_order (m_descr
->gdbarch
), val
);
810 cooked_write (regnum
, buf
);
814 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
817 gdb_assert (regcache
!= NULL
);
818 regcache
->cooked_write (regnum
, val
);
822 regcache::raw_write (int regnum
, const gdb_byte
*buf
)
825 gdb_assert (buf
!= NULL
);
826 assert_regnum (regnum
);
828 /* On the sparc, writing %g0 is a no-op, so we don't even want to
829 change the registers array if something writes to this register. */
830 if (gdbarch_cannot_store_register (arch (), regnum
))
833 /* If we have a valid copy of the register, and new value == old
834 value, then don't bother doing the actual store. */
835 if (get_register_status (regnum
) == REG_VALID
836 && (memcmp (register_buffer (regnum
), buf
,
837 m_descr
->sizeof_register
[regnum
]) == 0))
840 target_prepare_to_store (this);
841 raw_supply (regnum
, buf
);
843 /* Invalidate the register after it is written, in case of a
846 = make_scope_exit ([&] { this->invalidate (regnum
); });
848 target_store_registers (this, regnum
);
850 /* The target did not throw an error so we can discard invalidating
852 invalidator
.release ();
856 regcache::cooked_write (int regnum
, const gdb_byte
*buf
)
858 gdb_assert (regnum
>= 0);
859 gdb_assert (regnum
< m_descr
->nr_cooked_registers
);
860 if (regnum
< num_raw_registers ())
861 raw_write (regnum
, buf
);
863 gdbarch_pseudo_register_write (m_descr
->gdbarch
, this,
867 /* See regcache.h. */
870 readable_regcache::read_part (int regnum
, int offset
, int len
,
871 gdb_byte
*out
, bool is_raw
)
873 int reg_size
= register_size (arch (), regnum
);
875 gdb_assert (out
!= NULL
);
876 gdb_assert (offset
>= 0 && offset
<= reg_size
);
877 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
879 if (offset
== 0 && len
== 0)
885 if (offset
== 0 && len
== reg_size
)
887 /* Read the full register. */
888 return (is_raw
) ? raw_read (regnum
, out
) : cooked_read (regnum
, out
);
891 enum register_status status
;
892 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
894 /* Read full register to buffer. */
895 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
896 if (status
!= REG_VALID
)
900 memcpy (out
, reg
+ offset
, len
);
904 /* See regcache.h. */
907 reg_buffer::raw_collect_part (int regnum
, int offset
, int len
,
910 int reg_size
= register_size (arch (), regnum
);
912 gdb_assert (out
!= nullptr);
913 gdb_assert (offset
>= 0 && offset
<= reg_size
);
914 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
916 if (offset
== 0 && len
== 0)
922 if (offset
== 0 && len
== reg_size
)
924 /* Collect the full register. */
925 return raw_collect (regnum
, out
);
928 /* Read to buffer, then write out. */
929 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
930 raw_collect (regnum
, reg
);
931 memcpy (out
, reg
+ offset
, len
);
934 /* See regcache.h. */
937 regcache::write_part (int regnum
, int offset
, int len
,
938 const gdb_byte
*in
, bool is_raw
)
940 int reg_size
= register_size (arch (), regnum
);
942 gdb_assert (in
!= NULL
);
943 gdb_assert (offset
>= 0 && offset
<= reg_size
);
944 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
946 if (offset
== 0 && len
== 0)
952 if (offset
== 0 && len
== reg_size
)
954 /* Write the full register. */
955 (is_raw
) ? raw_write (regnum
, in
) : cooked_write (regnum
, in
);
959 enum register_status status
;
960 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
962 /* Read existing register to buffer. */
963 status
= (is_raw
) ? raw_read (regnum
, reg
) : cooked_read (regnum
, reg
);
964 if (status
!= REG_VALID
)
967 /* Update buffer, then write back to regcache. */
968 memcpy (reg
+ offset
, in
, len
);
969 is_raw
? raw_write (regnum
, reg
) : cooked_write (regnum
, reg
);
973 /* See regcache.h. */
976 reg_buffer::raw_supply_part (int regnum
, int offset
, int len
,
979 int reg_size
= register_size (arch (), regnum
);
981 gdb_assert (in
!= nullptr);
982 gdb_assert (offset
>= 0 && offset
<= reg_size
);
983 gdb_assert (len
>= 0 && offset
+ len
<= reg_size
);
985 if (offset
== 0 && len
== 0)
991 if (offset
== 0 && len
== reg_size
)
993 /* Supply the full register. */
994 return raw_supply (regnum
, in
);
997 gdb_byte
*reg
= (gdb_byte
*) alloca (reg_size
);
999 /* Read existing value to buffer. */
1000 raw_collect (regnum
, reg
);
1002 /* Write to buffer, then write out. */
1003 memcpy (reg
+ offset
, in
, len
);
1004 raw_supply (regnum
, reg
);
1007 enum register_status
1008 readable_regcache::raw_read_part (int regnum
, int offset
, int len
,
1011 assert_regnum (regnum
);
1012 return read_part (regnum
, offset
, len
, buf
, true);
1015 /* See regcache.h. */
1018 regcache::raw_write_part (int regnum
, int offset
, int len
,
1019 const gdb_byte
*buf
)
1021 assert_regnum (regnum
);
1022 write_part (regnum
, offset
, len
, buf
, true);
1025 /* See regcache.h. */
1027 enum register_status
1028 readable_regcache::cooked_read_part (int regnum
, int offset
, int len
,
1031 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1032 return read_part (regnum
, offset
, len
, buf
, false);
1035 /* See regcache.h. */
1038 regcache::cooked_write_part (int regnum
, int offset
, int len
,
1039 const gdb_byte
*buf
)
1041 gdb_assert (regnum
>= 0 && regnum
< m_descr
->nr_cooked_registers
);
1042 write_part (regnum
, offset
, len
, buf
, false);
1045 /* See gdbsupport/common-regcache.h. */
1048 reg_buffer::raw_supply (int regnum
, const void *buf
)
1053 assert_regnum (regnum
);
1055 regbuf
= register_buffer (regnum
);
1056 size
= m_descr
->sizeof_register
[regnum
];
1060 memcpy (regbuf
, buf
, size
);
1061 m_register_status
[regnum
] = REG_VALID
;
1065 /* This memset not strictly necessary, but better than garbage
1066 in case the register value manages to escape somewhere (due
1067 to a bug, no less). */
1068 memset (regbuf
, 0, size
);
1069 m_register_status
[regnum
] = REG_UNAVAILABLE
;
1073 /* See regcache.h. */
1076 reg_buffer::raw_supply_integer (int regnum
, const gdb_byte
*addr
,
1077 int addr_len
, bool is_signed
)
1079 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1083 assert_regnum (regnum
);
1085 regbuf
= register_buffer (regnum
);
1086 regsize
= m_descr
->sizeof_register
[regnum
];
1088 copy_integer_to_size (regbuf
, regsize
, addr
, addr_len
, is_signed
,
1090 m_register_status
[regnum
] = REG_VALID
;
1093 /* See regcache.h. */
1096 reg_buffer::raw_supply_zeroed (int regnum
)
1101 assert_regnum (regnum
);
1103 regbuf
= register_buffer (regnum
);
1104 size
= m_descr
->sizeof_register
[regnum
];
1106 memset (regbuf
, 0, size
);
1107 m_register_status
[regnum
] = REG_VALID
;
1110 /* See gdbsupport/common-regcache.h. */
1113 reg_buffer::raw_collect (int regnum
, void *buf
) const
1118 gdb_assert (buf
!= NULL
);
1119 assert_regnum (regnum
);
1121 regbuf
= register_buffer (regnum
);
1122 size
= m_descr
->sizeof_register
[regnum
];
1123 memcpy (buf
, regbuf
, size
);
1126 /* See regcache.h. */
1129 reg_buffer::raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
1130 bool is_signed
) const
1132 enum bfd_endian byte_order
= gdbarch_byte_order (m_descr
->gdbarch
);
1133 const gdb_byte
*regbuf
;
1136 assert_regnum (regnum
);
1138 regbuf
= register_buffer (regnum
);
1139 regsize
= m_descr
->sizeof_register
[regnum
];
1141 copy_integer_to_size (addr
, addr_len
, regbuf
, regsize
, is_signed
,
1145 /* See regcache.h. */
1148 regcache::transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
1149 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
1150 int slot_size
, int offs
) const
1152 struct gdbarch
*gdbarch
= arch ();
1153 int reg_size
= std::min (register_size (gdbarch
, regnum
), slot_size
);
1155 /* Use part versions and reg_size to prevent possible buffer overflows when
1156 accessing the regcache. */
1158 if (out_buf
!= nullptr)
1160 raw_collect_part (regnum
, 0, reg_size
, out_buf
+ offs
);
1162 /* Ensure any additional space is cleared. */
1163 if (slot_size
> reg_size
)
1164 memset (out_buf
+ offs
+ reg_size
, 0, slot_size
- reg_size
);
1166 else if (in_buf
!= nullptr)
1167 out_regcache
->raw_supply_part (regnum
, 0, reg_size
, in_buf
+ offs
);
1170 /* Invalidate the register. */
1171 out_regcache
->raw_supply (regnum
, nullptr);
1175 /* See regcache.h. */
1178 regcache::transfer_regset (const struct regset
*regset
,
1179 struct regcache
*out_regcache
,
1180 int regnum
, const gdb_byte
*in_buf
,
1181 gdb_byte
*out_buf
, size_t size
) const
1183 const struct regcache_map_entry
*map
;
1184 int offs
= 0, count
;
1186 for (map
= (const struct regcache_map_entry
*) regset
->regmap
;
1187 (count
= map
->count
) != 0;
1190 int regno
= map
->regno
;
1191 int slot_size
= map
->size
;
1193 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1194 slot_size
= m_descr
->sizeof_register
[regno
];
1196 if (regno
== REGCACHE_MAP_SKIP
1198 && (regnum
< regno
|| regnum
>= regno
+ count
)))
1199 offs
+= count
* slot_size
;
1201 else if (regnum
== -1)
1202 for (; count
--; regno
++, offs
+= slot_size
)
1204 if (offs
+ slot_size
> size
)
1207 transfer_regset_register (out_regcache
, regno
, in_buf
, out_buf
,
1212 /* Transfer a single register and return. */
1213 offs
+= (regnum
- regno
) * slot_size
;
1214 if (offs
+ slot_size
> size
)
1217 transfer_regset_register (out_regcache
, regnum
, in_buf
, out_buf
,
1224 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1225 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1226 If BUF is NULL, set the register(s) to "unavailable" status. */
1229 regcache_supply_regset (const struct regset
*regset
,
1230 struct regcache
*regcache
,
1231 int regnum
, const void *buf
, size_t size
)
1233 regcache
->supply_regset (regset
, regnum
, (const gdb_byte
*) buf
, size
);
1237 regcache::supply_regset (const struct regset
*regset
,
1238 int regnum
, const void *buf
, size_t size
)
1240 transfer_regset (regset
, this, regnum
, (const gdb_byte
*) buf
, nullptr, size
);
1243 /* Collect register REGNUM from REGCACHE to BUF, using the register
1244 map in REGSET. If REGNUM is -1, do this for all registers in
1248 regcache_collect_regset (const struct regset
*regset
,
1249 const struct regcache
*regcache
,
1250 int regnum
, void *buf
, size_t size
)
1252 regcache
->collect_regset (regset
, regnum
, (gdb_byte
*) buf
, size
);
1256 regcache::collect_regset (const struct regset
*regset
,
1257 int regnum
, void *buf
, size_t size
) const
1259 transfer_regset (regset
, nullptr, regnum
, nullptr, (gdb_byte
*) buf
, size
);
1262 /* See regcache.h */
1265 regcache_map_supplies (const struct regcache_map_entry
*map
, int regnum
,
1266 struct gdbarch
*gdbarch
, size_t size
)
1268 int offs
= 0, count
;
1270 for (; (count
= map
->count
) != 0; map
++)
1272 int regno
= map
->regno
;
1273 int slot_size
= map
->size
;
1275 if (slot_size
== 0 && regno
!= REGCACHE_MAP_SKIP
)
1276 slot_size
= register_size (gdbarch
, regno
);
1278 if (regno
!= REGCACHE_MAP_SKIP
&& regnum
>= regno
1279 && regnum
< regno
+ count
)
1280 return offs
+ (regnum
- regno
+ 1) * slot_size
<= size
;
1282 offs
+= count
* slot_size
;
1289 /* See gdbsupport/common-regcache.h. */
1292 reg_buffer::raw_compare (int regnum
, const void *buf
, int offset
) const
1294 gdb_assert (buf
!= NULL
);
1295 assert_regnum (regnum
);
1297 const char *regbuf
= (const char *) register_buffer (regnum
);
1298 size_t size
= m_descr
->sizeof_register
[regnum
];
1299 gdb_assert (size
>= offset
);
1301 return (memcmp (buf
, regbuf
+ offset
, size
- offset
) == 0);
1304 /* Special handling for register PC. */
1307 regcache_read_pc (struct regcache
*regcache
)
1309 struct gdbarch
*gdbarch
= regcache
->arch ();
1313 if (gdbarch_read_pc_p (gdbarch
))
1314 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1315 /* Else use per-frame method on get_current_frame. */
1316 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1320 if (regcache_cooked_read_unsigned (regcache
,
1321 gdbarch_pc_regnum (gdbarch
),
1322 &raw_val
) == REG_UNAVAILABLE
)
1323 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1325 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1328 internal_error (__FILE__
, __LINE__
,
1329 _("regcache_read_pc: Unable to find PC"));
1333 /* See gdbsupport/common-regcache.h. */
1336 regcache_read_pc_protected (regcache
*regcache
)
1341 pc
= regcache_read_pc (regcache
);
1343 catch (const gdb_exception_error
&ex
)
1352 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1354 struct gdbarch
*gdbarch
= regcache
->arch ();
1356 if (gdbarch_write_pc_p (gdbarch
))
1357 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1358 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1359 regcache_cooked_write_unsigned (regcache
,
1360 gdbarch_pc_regnum (gdbarch
), pc
);
1362 internal_error (__FILE__
, __LINE__
,
1363 _("regcache_write_pc: Unable to update PC"));
1365 /* Writing the PC (for instance, from "load") invalidates the
1367 reinit_frame_cache ();
1371 reg_buffer::num_raw_registers () const
1373 return gdbarch_num_regs (arch ());
1377 regcache::debug_print_register (const char *func
, int regno
)
1379 struct gdbarch
*gdbarch
= arch ();
1381 fprintf_unfiltered (gdb_stdlog
, "%s ", func
);
1382 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
)
1383 && gdbarch_register_name (gdbarch
, regno
) != NULL
1384 && gdbarch_register_name (gdbarch
, regno
)[0] != '\0')
1385 fprintf_unfiltered (gdb_stdlog
, "(%s)",
1386 gdbarch_register_name (gdbarch
, regno
));
1388 fprintf_unfiltered (gdb_stdlog
, "(%d)", regno
);
1389 if (regno
>= 0 && regno
< gdbarch_num_regs (gdbarch
))
1391 enum bfd_endian byte_order
= gdbarch_byte_order (gdbarch
);
1392 int size
= register_size (gdbarch
, regno
);
1393 gdb_byte
*buf
= register_buffer (regno
);
1395 fprintf_unfiltered (gdb_stdlog
, " = ");
1396 for (int i
= 0; i
< size
; i
++)
1398 fprintf_unfiltered (gdb_stdlog
, "%02x", buf
[i
]);
1400 if (size
<= sizeof (LONGEST
))
1402 ULONGEST val
= extract_unsigned_integer (buf
, size
, byte_order
);
1404 fprintf_unfiltered (gdb_stdlog
, " %s %s",
1405 core_addr_to_string_nz (val
), plongest (val
));
1408 fprintf_unfiltered (gdb_stdlog
, "\n");
1411 /* Implement 'maint flush register-cache' command. */
1414 reg_flush_command (const char *command
, int from_tty
)
1416 /* Force-flush the register cache. */
1417 registers_changed ();
1419 printf_filtered (_("Register cache flushed.\n"));
1423 register_dump::dump (ui_file
*file
)
1425 auto descr
= regcache_descr (m_gdbarch
);
1427 int footnote_nr
= 0;
1428 int footnote_register_offset
= 0;
1429 int footnote_register_type_name_null
= 0;
1430 long register_offset
= 0;
1432 gdb_assert (descr
->nr_cooked_registers
1433 == gdbarch_num_cooked_regs (m_gdbarch
));
1435 for (regnum
= -1; regnum
< descr
->nr_cooked_registers
; regnum
++)
1439 fprintf_unfiltered (file
, " %-10s", "Name");
1442 const char *p
= gdbarch_register_name (m_gdbarch
, regnum
);
1446 else if (p
[0] == '\0')
1448 fprintf_unfiltered (file
, " %-10s", p
);
1453 fprintf_unfiltered (file
, " %4s", "Nr");
1455 fprintf_unfiltered (file
, " %4d", regnum
);
1457 /* Relative number. */
1459 fprintf_unfiltered (file
, " %4s", "Rel");
1460 else if (regnum
< gdbarch_num_regs (m_gdbarch
))
1461 fprintf_unfiltered (file
, " %4d", regnum
);
1463 fprintf_unfiltered (file
, " %4d",
1464 (regnum
- gdbarch_num_regs (m_gdbarch
)));
1468 fprintf_unfiltered (file
, " %6s ", "Offset");
1471 fprintf_unfiltered (file
, " %6ld",
1472 descr
->register_offset
[regnum
]);
1473 if (register_offset
!= descr
->register_offset
[regnum
]
1475 && (descr
->register_offset
[regnum
]
1476 != (descr
->register_offset
[regnum
- 1]
1477 + descr
->sizeof_register
[regnum
- 1])))
1480 if (!footnote_register_offset
)
1481 footnote_register_offset
= ++footnote_nr
;
1482 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1485 fprintf_unfiltered (file
, " ");
1486 register_offset
= (descr
->register_offset
[regnum
]
1487 + descr
->sizeof_register
[regnum
]);
1492 fprintf_unfiltered (file
, " %5s ", "Size");
1494 fprintf_unfiltered (file
, " %5ld", descr
->sizeof_register
[regnum
]);
1499 std::string name_holder
;
1505 static const char blt
[] = "builtin_type";
1507 t
= register_type (m_gdbarch
, regnum
)->name ();
1510 if (!footnote_register_type_name_null
)
1511 footnote_register_type_name_null
= ++footnote_nr
;
1512 name_holder
= string_printf ("*%d",
1513 footnote_register_type_name_null
);
1514 t
= name_holder
.c_str ();
1516 /* Chop a leading builtin_type. */
1517 if (startswith (t
, blt
))
1520 fprintf_unfiltered (file
, " %-15s", t
);
1523 /* Leading space always present. */
1524 fprintf_unfiltered (file
, " ");
1526 dump_reg (file
, regnum
);
1528 fprintf_unfiltered (file
, "\n");
1531 if (footnote_register_offset
)
1532 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1533 footnote_register_offset
);
1534 if (footnote_register_type_name_null
)
1535 fprintf_unfiltered (file
,
1536 "*%d: Register type's name NULL.\n",
1537 footnote_register_type_name_null
);
1541 #include "gdbsupport/selftest.h"
1542 #include "selftest-arch.h"
1543 #include "target-float.h"
1545 namespace selftests
{
1552 for (auto pid_ptid_regc_map_it
= regcaches
.cbegin ();
1553 pid_ptid_regc_map_it
!= regcaches
.cend ();
1554 ++pid_ptid_regc_map_it
)
1556 const pid_ptid_regcache_map
&pid_ptid_regc_map
1557 = pid_ptid_regc_map_it
->second
;
1559 for (auto ptid_regc_map_it
= pid_ptid_regc_map
.cbegin ();
1560 ptid_regc_map_it
!= pid_ptid_regc_map
.cend ();
1563 const ptid_regcache_map
&ptid_regc_map
1564 = ptid_regc_map_it
->second
;
1566 size
+= ptid_regc_map
.size ();
1573 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1576 regcache_count (process_stratum_target
*target
, ptid_t ptid
)
1578 /* Look up map for target. */
1579 auto pid_ptid_regc_map_it
= regcaches
.find (target
);
1580 if (pid_ptid_regc_map_it
!= regcaches
.end ())
1582 pid_ptid_regcache_map
&pid_ptid_regc_map
= pid_ptid_regc_map_it
->second
;
1584 /* Look map for pid. */
1585 auto ptid_regc_map_it
= pid_ptid_regc_map
.find (ptid
.pid ());
1586 if (ptid_regc_map_it
!= pid_ptid_regc_map
.end ())
1588 ptid_regcache_map
&ptid_regc_map
= ptid_regc_map_it
->second
;
1589 auto range
= ptid_regc_map
.equal_range (ptid
);
1591 return std::distance (range
.first
, range
.second
);
1598 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1601 get_thread_arch_aspace_regcache_and_check (process_stratum_target
*target
,
1604 /* We currently only test with a single gdbarch. Any gdbarch will do, so use
1605 the current inferior's gdbarch. Also use the current inferior's address
1607 gdbarch
*arch
= current_inferior ()->gdbarch
;
1608 address_space
*aspace
= current_inferior ()->aspace
;
1610 = get_thread_arch_aspace_regcache (target
, ptid
, arch
, aspace
);
1612 SELF_CHECK (regcache
!= NULL
);
1613 SELF_CHECK (regcache
->target () == target
);
1614 SELF_CHECK (regcache
->ptid () == ptid
);
1615 SELF_CHECK (regcache
->arch () == arch
);
1616 SELF_CHECK (regcache
->aspace () == aspace
);
1619 /* The data that the regcaches selftests must hold onto for the duration of the
1622 struct regcache_test_data
1624 regcache_test_data ()
1626 /* Ensure the regcaches container is empty at the start. */
1627 registers_changed ();
1630 ~regcache_test_data ()
1632 /* Make sure to leave the global regcaches container empty. */
1633 registers_changed ();
1636 test_target_ops test_target1
;
1637 test_target_ops test_target2
;
1640 using regcache_test_data_up
= std::unique_ptr
<regcache_test_data
>;
1642 /* Set up a few regcaches from two different targets, for use in
1643 regcache-management tests.
1645 Return a pointer, because the `regcache_test_data` type is not moveable. */
1647 static regcache_test_data_up
1648 populate_regcaches_for_test ()
1650 regcache_test_data_up
data (new regcache_test_data
);
1651 size_t expected_regcache_size
= 0;
1653 SELF_CHECK (regcaches_size () == 0);
1655 /* Populate the regcache container with a few regcaches for the two test
1657 for (int pid
: { 1, 2 })
1659 for (long lwp
: { 1, 2, 3 })
1661 get_thread_arch_aspace_regcache_and_check
1662 (&data
->test_target1
, ptid_t (pid
, lwp
));
1663 expected_regcache_size
++;
1664 SELF_CHECK (regcaches_size () == expected_regcache_size
);
1666 get_thread_arch_aspace_regcache_and_check
1667 (&data
->test_target2
, ptid_t (pid
, lwp
));
1668 expected_regcache_size
++;
1669 SELF_CHECK (regcaches_size () == expected_regcache_size
);
1677 get_thread_arch_aspace_regcache_test ()
1679 /* populate_regcaches_for_test already tests most of the
1680 get_thread_arch_aspace_regcache functionality. */
1681 regcache_test_data_up data
= populate_regcaches_for_test ();
1682 size_t regcaches_size_before
= regcaches_size ();
1684 /* Test that getting an existing regcache doesn't create a new one. */
1685 get_thread_arch_aspace_regcache_and_check (&data
->test_target1
, ptid_t (2, 2));
1686 SELF_CHECK (regcaches_size () == regcaches_size_before
);
1689 /* Test marking all regcaches of all targets as changed. */
1692 registers_changed_ptid_all_test ()
1694 regcache_test_data_up data
= populate_regcaches_for_test ();
1696 registers_changed_ptid (nullptr, minus_one_ptid
);
1697 SELF_CHECK (regcaches_size () == 0);
1700 /* Test marking regcaches of a specific target as changed. */
1703 registers_changed_ptid_target_test ()
1705 regcache_test_data_up data
= populate_regcaches_for_test ();
1707 registers_changed_ptid (&data
->test_target1
, minus_one_ptid
);
1708 SELF_CHECK (regcaches_size () == 6);
1710 /* Check that we deleted the regcache for the right target. */
1711 SELF_CHECK (regcache_count (&data
->test_target1
, ptid_t (2, 2)) == 0);
1712 SELF_CHECK (regcache_count (&data
->test_target2
, ptid_t (2, 2)) == 1);
1715 /* Test marking regcaches of a specific (target, pid) as changed. */
1718 registers_changed_ptid_target_pid_test ()
1720 regcache_test_data_up data
= populate_regcaches_for_test ();
1722 registers_changed_ptid (&data
->test_target1
, ptid_t (2));
1723 SELF_CHECK (regcaches_size () == 9);
1725 /* Regcaches from target1 should not exist, while regcaches from target2
1727 SELF_CHECK (regcache_count (&data
->test_target1
, ptid_t (2, 2)) == 0);
1728 SELF_CHECK (regcache_count (&data
->test_target2
, ptid_t (2, 2)) == 1);
1731 /* Test marking regcaches of a specific (target, ptid) as changed. */
1734 registers_changed_ptid_target_ptid_test ()
1736 regcache_test_data_up data
= populate_regcaches_for_test ();
1738 registers_changed_ptid (&data
->test_target1
, ptid_t (2, 2));
1739 SELF_CHECK (regcaches_size () == 11);
1741 /* Check that we deleted the regcache for the right target. */
1742 SELF_CHECK (regcache_count (&data
->test_target1
, ptid_t (2, 2)) == 0);
1743 SELF_CHECK (regcache_count (&data
->test_target2
, ptid_t (2, 2)) == 1);
1746 class target_ops_no_register
: public test_target_ops
1749 target_ops_no_register ()
1750 : test_target_ops
{}
1755 fetch_registers_called
= 0;
1756 store_registers_called
= 0;
1757 xfer_partial_called
= 0;
1760 void fetch_registers (regcache
*regs
, int regno
) override
;
1761 void store_registers (regcache
*regs
, int regno
) override
;
1763 enum target_xfer_status
xfer_partial (enum target_object object
,
1764 const char *annex
, gdb_byte
*readbuf
,
1765 const gdb_byte
*writebuf
,
1766 ULONGEST offset
, ULONGEST len
,
1767 ULONGEST
*xfered_len
) override
;
1769 unsigned int fetch_registers_called
= 0;
1770 unsigned int store_registers_called
= 0;
1771 unsigned int xfer_partial_called
= 0;
1775 target_ops_no_register::fetch_registers (regcache
*regs
, int regno
)
1777 /* Mark register available. */
1778 regs
->raw_supply_zeroed (regno
);
1779 this->fetch_registers_called
++;
1783 target_ops_no_register::store_registers (regcache
*regs
, int regno
)
1785 this->store_registers_called
++;
1788 enum target_xfer_status
1789 target_ops_no_register::xfer_partial (enum target_object object
,
1790 const char *annex
, gdb_byte
*readbuf
,
1791 const gdb_byte
*writebuf
,
1792 ULONGEST offset
, ULONGEST len
,
1793 ULONGEST
*xfered_len
)
1795 this->xfer_partial_called
++;
1798 return TARGET_XFER_OK
;
1801 class readwrite_regcache
: public regcache
1804 readwrite_regcache (process_stratum_target
*target
,
1805 struct gdbarch
*gdbarch
)
1806 : regcache (target
, gdbarch
, nullptr)
1810 /* Test regcache::cooked_read gets registers from raw registers and
1811 memory instead of target to_{fetch,store}_registers. */
1814 cooked_read_test (struct gdbarch
*gdbarch
)
1816 scoped_mock_context
<target_ops_no_register
> mockctx (gdbarch
);
1818 /* Test that read one raw register from regcache_no_target will go
1819 to the target layer. */
1821 /* Find a raw register which size isn't zero. */
1823 for (nonzero_regnum
= 0;
1824 nonzero_regnum
< gdbarch_num_regs (gdbarch
);
1827 if (register_size (gdbarch
, nonzero_regnum
) != 0)
1831 readwrite_regcache
readwrite (&mockctx
.mock_target
, gdbarch
);
1832 gdb::def_vector
<gdb_byte
> buf (register_size (gdbarch
, nonzero_regnum
));
1834 readwrite
.raw_read (nonzero_regnum
, buf
.data ());
1836 /* raw_read calls target_fetch_registers. */
1837 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
> 0);
1838 mockctx
.mock_target
.reset ();
1840 /* Mark all raw registers valid, so the following raw registers
1841 accesses won't go to target. */
1842 for (auto i
= 0; i
< gdbarch_num_regs (gdbarch
); i
++)
1843 readwrite
.raw_update (i
);
1845 mockctx
.mock_target
.reset ();
1846 /* Then, read all raw and pseudo registers, and don't expect calling
1847 to_{fetch,store}_registers. */
1848 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1850 if (register_size (gdbarch
, regnum
) == 0)
1853 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1855 SELF_CHECK (REG_VALID
== readwrite
.cooked_read (regnum
,
1856 inner_buf
.data ()));
1858 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1859 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1860 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1862 mockctx
.mock_target
.reset ();
1865 readonly_detached_regcache
readonly (readwrite
);
1867 /* GDB may go to target layer to fetch all registers and memory for
1868 readonly regcache. */
1869 mockctx
.mock_target
.reset ();
1871 for (int regnum
= 0; regnum
< gdbarch_num_cooked_regs (gdbarch
); regnum
++)
1873 if (register_size (gdbarch
, regnum
) == 0)
1876 gdb::def_vector
<gdb_byte
> inner_buf (register_size (gdbarch
, regnum
));
1877 enum register_status status
= readonly
.cooked_read (regnum
,
1880 if (regnum
< gdbarch_num_regs (gdbarch
))
1882 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1884 if (bfd_arch
== bfd_arch_frv
|| bfd_arch
== bfd_arch_h8300
1885 || bfd_arch
== bfd_arch_m32c
|| bfd_arch
== bfd_arch_sh
1886 || bfd_arch
== bfd_arch_alpha
|| bfd_arch
== bfd_arch_v850
1887 || bfd_arch
== bfd_arch_msp430
|| bfd_arch
== bfd_arch_mep
1888 || bfd_arch
== bfd_arch_mips
|| bfd_arch
== bfd_arch_v850_rh850
1889 || bfd_arch
== bfd_arch_tic6x
|| bfd_arch
== bfd_arch_mn10300
1890 || bfd_arch
== bfd_arch_rl78
|| bfd_arch
== bfd_arch_score
1891 || bfd_arch
== bfd_arch_riscv
|| bfd_arch
== bfd_arch_csky
)
1893 /* Raw registers. If raw registers are not in save_reggroup,
1894 their status are unknown. */
1895 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1896 SELF_CHECK (status
== REG_VALID
);
1898 SELF_CHECK (status
== REG_UNKNOWN
);
1901 SELF_CHECK (status
== REG_VALID
);
1905 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
1906 SELF_CHECK (status
== REG_VALID
);
1909 /* If pseudo registers are not in save_reggroup, some of
1910 them can be computed from saved raw registers, but some
1911 of them are unknown. */
1912 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1914 if (bfd_arch
== bfd_arch_frv
1915 || bfd_arch
== bfd_arch_m32c
1916 || bfd_arch
== bfd_arch_mep
1917 || bfd_arch
== bfd_arch_sh
)
1918 SELF_CHECK (status
== REG_VALID
|| status
== REG_UNKNOWN
);
1919 else if (bfd_arch
== bfd_arch_mips
1920 || bfd_arch
== bfd_arch_h8300
)
1921 SELF_CHECK (status
== REG_UNKNOWN
);
1923 SELF_CHECK (status
== REG_VALID
);
1927 SELF_CHECK (mockctx
.mock_target
.fetch_registers_called
== 0);
1928 SELF_CHECK (mockctx
.mock_target
.store_registers_called
== 0);
1929 SELF_CHECK (mockctx
.mock_target
.xfer_partial_called
== 0);
1931 mockctx
.mock_target
.reset ();
1935 /* Test regcache::cooked_write by writing some expected contents to
1936 registers, and checking that contents read from registers and the
1937 expected contents are the same. */
1940 cooked_write_test (struct gdbarch
*gdbarch
)
1942 /* Error out if debugging something, because we're going to push the
1943 test target, which would pop any existing target. */
1944 if (current_inferior ()->top_target ()->stratum () >= process_stratum
)
1945 error (_("target already pushed"));
1947 /* Create a mock environment. A process_stratum target pushed. */
1949 target_ops_no_register mock_target
;
1951 /* Push the process_stratum target so we can mock accessing
1953 current_inferior ()->push_target (&mock_target
);
1955 /* Pop it again on exit (return/exception). */
1960 pop_all_targets_at_and_above (process_stratum
);
1964 readwrite_regcache
readwrite (&mock_target
, gdbarch
);
1966 const int num_regs
= gdbarch_num_cooked_regs (gdbarch
);
1968 for (auto regnum
= 0; regnum
< num_regs
; regnum
++)
1970 if (register_size (gdbarch
, regnum
) == 0
1971 || gdbarch_cannot_store_register (gdbarch
, regnum
))
1974 auto bfd_arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1976 if (bfd_arch
== bfd_arch_sparc
1977 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1978 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1979 && gdbarch_ptr_bit (gdbarch
) == 64
1980 && (regnum
>= gdbarch_num_regs (gdbarch
)
1981 && regnum
<= gdbarch_num_regs (gdbarch
) + 4))
1984 std::vector
<gdb_byte
> expected (register_size (gdbarch
, regnum
), 0);
1985 std::vector
<gdb_byte
> buf (register_size (gdbarch
, regnum
), 0);
1986 const auto type
= register_type (gdbarch
, regnum
);
1988 if (type
->code () == TYPE_CODE_FLT
1989 || type
->code () == TYPE_CODE_DECFLOAT
)
1991 /* Generate valid float format. */
1992 target_float_from_string (expected
.data (), type
, "1.25");
1994 else if (type
->code () == TYPE_CODE_INT
1995 || type
->code () == TYPE_CODE_ARRAY
1996 || type
->code () == TYPE_CODE_PTR
1997 || type
->code () == TYPE_CODE_UNION
1998 || type
->code () == TYPE_CODE_STRUCT
)
2000 if (bfd_arch
== bfd_arch_ia64
2001 || (regnum
>= gdbarch_num_regs (gdbarch
)
2002 && (bfd_arch
== bfd_arch_xtensa
2003 || bfd_arch
== bfd_arch_bfin
2004 || bfd_arch
== bfd_arch_m32c
2005 /* m68hc11 pseudo registers are in memory. */
2006 || bfd_arch
== bfd_arch_m68hc11
2007 || bfd_arch
== bfd_arch_m68hc12
2008 || bfd_arch
== bfd_arch_s390
))
2009 || (bfd_arch
== bfd_arch_frv
2010 /* FRV pseudo registers except iacc0. */
2011 && regnum
> gdbarch_num_regs (gdbarch
)))
2013 /* Skip setting the expected values for some architecture
2016 else if (bfd_arch
== bfd_arch_rl78
&& regnum
== 40)
2018 /* RL78_PC_REGNUM */
2019 for (auto j
= 0; j
< register_size (gdbarch
, regnum
) - 1; j
++)
2024 for (auto j
= 0; j
< register_size (gdbarch
, regnum
); j
++)
2028 else if (type
->code () == TYPE_CODE_FLAGS
)
2030 /* No idea how to test flags. */
2035 /* If we don't know how to create the expected value for the
2036 this type, make it fail. */
2040 readwrite
.cooked_write (regnum
, expected
.data ());
2042 SELF_CHECK (readwrite
.cooked_read (regnum
, buf
.data ()) == REG_VALID
);
2043 SELF_CHECK (expected
== buf
);
2047 /* Verify that when two threads with the same ptid exist (from two different
2048 targets) and one of them changes ptid, we only update the appropriate
2052 regcache_thread_ptid_changed ()
2054 /* This test relies on the global regcache list to initially be empty. */
2055 registers_changed ();
2057 /* Any arch will do. */
2058 gdbarch
*arch
= current_inferior ()->gdbarch
;
2060 /* Prepare two targets with one thread each, with the same ptid. */
2061 scoped_mock_context
<test_target_ops
> target1 (arch
);
2062 scoped_mock_context
<test_target_ops
> target2 (arch
);
2064 ptid_t
old_ptid (111, 222);
2065 ptid_t
new_ptid (111, 333);
2067 target1
.mock_inferior
.pid
= old_ptid
.pid ();
2068 target1
.mock_thread
.ptid
= old_ptid
;
2069 target1
.mock_inferior
.ptid_thread_map
.clear ();
2070 target1
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target1
.mock_thread
;
2072 target2
.mock_inferior
.pid
= old_ptid
.pid ();
2073 target2
.mock_thread
.ptid
= old_ptid
;
2074 target2
.mock_inferior
.ptid_thread_map
.clear ();
2075 target2
.mock_inferior
.ptid_thread_map
[old_ptid
] = &target2
.mock_thread
;
2077 gdb_assert (regcaches
.empty ());
2079 /* Populate the regcaches container. */
2080 get_thread_arch_aspace_regcache (&target1
.mock_target
, old_ptid
, arch
,
2082 get_thread_arch_aspace_regcache (&target2
.mock_target
, old_ptid
, arch
,
2085 gdb_assert (regcaches
.size () == 2);
2086 gdb_assert (regcache_count (&target1
.mock_target
, old_ptid
) == 1);
2087 gdb_assert (regcache_count (&target1
.mock_target
, new_ptid
) == 0);
2088 gdb_assert (regcache_count (&target2
.mock_target
, old_ptid
) == 1);
2089 gdb_assert (regcache_count (&target2
.mock_target
, new_ptid
) == 0);
2091 thread_change_ptid (&target1
.mock_target
, old_ptid
, new_ptid
);
2093 gdb_assert (regcaches
.size () == 2);
2094 gdb_assert (regcache_count (&target1
.mock_target
, old_ptid
) == 0);
2095 gdb_assert (regcache_count (&target1
.mock_target
, new_ptid
) == 1);
2096 gdb_assert (regcache_count (&target2
.mock_target
, old_ptid
) == 1);
2097 gdb_assert (regcache_count (&target2
.mock_target
, new_ptid
) == 0);
2099 /* Leave the regcache list empty. */
2100 registers_changed ();
2101 gdb_assert (regcaches
.empty ());
2104 } // namespace selftests
2105 #endif /* GDB_SELF_TEST */
2107 void _initialize_regcache ();
2109 _initialize_regcache ()
2111 struct cmd_list_element
*c
;
2113 regcache_descr_handle
2114 = gdbarch_data_register_post_init (init_regcache_descr
);
2116 gdb::observers::target_changed
.attach (regcache_observer_target_changed
,
2118 gdb::observers::thread_ptid_changed
.attach (regcache_thread_ptid_changed
,
2121 cmd_list_element
*maintenance_flush_register_cache_cmd
2122 = add_cmd ("register-cache", class_maintenance
, reg_flush_command
,
2123 _("Force gdb to flush its register and frame cache."),
2124 &maintenanceflushlist
);
2125 c
= add_com_alias ("flushregs", maintenance_flush_register_cache_cmd
,
2126 class_maintenance
, 0);
2127 deprecate_cmd (c
, "maintenance flush register-cache");
2130 selftests::register_test ("get_thread_arch_aspace_regcache",
2131 selftests::get_thread_arch_aspace_regcache_test
);
2132 selftests::register_test ("registers_changed_ptid_all",
2133 selftests::registers_changed_ptid_all_test
);
2134 selftests::register_test ("registers_changed_ptid_target",
2135 selftests::registers_changed_ptid_target_test
);
2136 selftests::register_test ("registers_changed_ptid_target_pid",
2137 selftests::registers_changed_ptid_target_pid_test
);
2138 selftests::register_test ("registers_changed_ptid_target_ptid",
2139 selftests::registers_changed_ptid_target_ptid_test
);
2141 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2142 selftests::cooked_read_test
);
2143 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2144 selftests::cooked_write_test
);
2145 selftests::register_test ("regcache_thread_ptid_changed",
2146 selftests::regcache_thread_ptid_changed
);