ld x86_64 tests: Accept x86-64-v3 as a needed ISA
[binutils-gdb.git] / gdb / regcache.c
blob2e48c025809148e1779ff469b167ec7ea8387577
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2023 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/>. */
20 #include "defs.h"
21 #include "inferior.h"
22 #include "gdbthread.h"
23 #include "target.h"
24 #include "test-target.h"
25 #include "scoped-mock-context.h"
26 #include "gdbarch.h"
27 #include "gdbcmd.h"
28 #include "regcache.h"
29 #include "reggroups.h"
30 #include "observable.h"
31 #include "regset.h"
32 #include <unordered_map>
33 #include "cli/cli-cmds.h"
36 * DATA STRUCTURE
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 struct regcache_descr
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch = nullptr;
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
53 cache. */
54 long sizeof_raw_registers = 0;
56 /* The cooked register space. Each cooked register in the range
57 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
58 register. The remaining [NR_RAW_REGISTERS
59 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
60 both raw registers and memory by the architecture methods
61 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
62 int nr_cooked_registers = 0;
63 long sizeof_cooked_registers = 0;
65 /* Offset and size (in 8 bit bytes), of each register in the
66 register cache. All registers (including those in the range
67 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
68 offset. */
69 long *register_offset = nullptr;
70 long *sizeof_register = nullptr;
72 /* Cached table containing the type of each register. */
73 struct type **register_type = nullptr;
76 static const registry<gdbarch>::key<struct regcache_descr>
77 regcache_descr_handle;
79 static struct regcache_descr *
80 init_regcache_descr (struct gdbarch *gdbarch)
82 int i;
83 struct regcache_descr *descr;
84 gdb_assert (gdbarch != NULL);
86 /* Create an initial, zero filled, table. */
87 descr = new struct regcache_descr;
88 descr->gdbarch = gdbarch;
90 /* Total size of the register space. The raw registers are mapped
91 directly onto the raw register cache while the pseudo's are
92 either mapped onto raw-registers or memory. */
93 descr->nr_cooked_registers = gdbarch_num_cooked_regs (gdbarch);
95 /* Fill in a table of register types. */
96 descr->register_type
97 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
98 struct type *);
99 for (i = 0; i < descr->nr_cooked_registers; i++)
100 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
102 /* Construct a strictly RAW register cache. Don't allow pseudo's
103 into the register cache. */
105 /* Lay out the register cache.
107 NOTE: cagney/2002-05-22: Only register_type () is used when
108 constructing the register cache. It is assumed that the
109 register's raw size, virtual size and type length are all the
110 same. */
113 long offset = 0;
115 descr->sizeof_register
116 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
117 descr->register_offset
118 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
119 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
121 descr->sizeof_register[i] = descr->register_type[i]->length ();
122 descr->register_offset[i] = offset;
123 offset += descr->sizeof_register[i];
125 /* Set the real size of the raw register cache buffer. */
126 descr->sizeof_raw_registers = offset;
128 for (; i < descr->nr_cooked_registers; i++)
130 descr->sizeof_register[i] = descr->register_type[i]->length ();
131 descr->register_offset[i] = offset;
132 offset += descr->sizeof_register[i];
134 /* Set the real size of the readonly register cache buffer. */
135 descr->sizeof_cooked_registers = offset;
138 return descr;
141 static struct regcache_descr *
142 regcache_descr (struct gdbarch *gdbarch)
144 struct regcache_descr *result = regcache_descr_handle.get (gdbarch);
145 if (result == nullptr)
147 result = init_regcache_descr (gdbarch);
148 regcache_descr_handle.set (gdbarch, result);
151 return result;
154 /* Utility functions returning useful register attributes stored in
155 the regcache descr. */
157 struct type *
158 register_type (struct gdbarch *gdbarch, int regnum)
160 struct regcache_descr *descr = regcache_descr (gdbarch);
162 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
163 return descr->register_type[regnum];
166 /* Utility functions returning useful register attributes stored in
167 the regcache descr. */
170 register_size (struct gdbarch *gdbarch, int regnum)
172 struct regcache_descr *descr = regcache_descr (gdbarch);
173 int size;
175 gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
176 size = descr->sizeof_register[regnum];
177 return size;
180 /* See gdbsupport/common-regcache.h. */
183 regcache_register_size (const struct regcache *regcache, int n)
185 return register_size (regcache->arch (), n);
188 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
189 : m_has_pseudo (has_pseudo)
191 gdb_assert (gdbarch != NULL);
192 m_descr = regcache_descr (gdbarch);
194 /* We don't zero-initialize the M_REGISTERS array, as the bytes it contains
195 aren't meaningful as long as the corresponding register status is not
196 REG_VALID. */
197 if (has_pseudo)
199 m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers]);
200 m_register_status.reset
201 (new register_status[m_descr->nr_cooked_registers] ());
203 else
205 m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers]);
206 m_register_status.reset
207 (new register_status[gdbarch_num_regs (gdbarch)] ());
211 regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch,
212 const address_space *aspace_)
213 /* The register buffers. A read/write register cache can only hold
214 [0 .. gdbarch_num_regs). */
215 : detached_regcache (gdbarch, false), m_aspace (aspace_),
216 m_inf_for_target_calls (inf_for_target_calls)
218 m_ptid = minus_one_ptid;
221 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
222 : readonly_detached_regcache (src.arch (),
223 [&src] (int regnum, gdb_byte *buf)
225 return src.cooked_read (regnum, buf);
230 gdbarch *
231 reg_buffer::arch () const
233 return m_descr->gdbarch;
236 /* Return a pointer to register REGNUM's buffer cache. */
238 gdb_byte *
239 reg_buffer::register_buffer (int regnum) const
241 return m_registers.get () + m_descr->register_offset[regnum];
244 void
245 reg_buffer::save (register_read_ftype cooked_read)
247 struct gdbarch *gdbarch = m_descr->gdbarch;
248 int regnum;
250 /* It should have pseudo registers. */
251 gdb_assert (m_has_pseudo);
252 /* Clear the dest. */
253 memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
254 memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
255 /* Copy over any registers (identified by their membership in the
256 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
257 gdbarch_num_pseudo_regs) range is checked since some architectures need
258 to save/restore `cooked' registers that live in memory. */
259 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
261 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
263 gdb_byte *dst_buf = register_buffer (regnum);
264 enum register_status status = cooked_read (regnum, dst_buf);
266 gdb_assert (status != REG_UNKNOWN);
268 if (status != REG_VALID)
269 memset (dst_buf, 0, register_size (gdbarch, regnum));
271 m_register_status[regnum] = status;
276 void
277 regcache::restore (readonly_detached_regcache *src)
279 struct gdbarch *gdbarch = m_descr->gdbarch;
280 int regnum;
282 gdb_assert (src != NULL);
283 gdb_assert (src->m_has_pseudo);
285 gdb_assert (gdbarch == src->arch ());
287 /* Copy over any registers, being careful to only restore those that
288 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
289 + gdbarch_num_pseudo_regs) range is checked since some architectures need
290 to save/restore `cooked' registers that live in memory. */
291 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
293 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
295 if (src->m_register_status[regnum] == REG_VALID)
296 cooked_write (regnum, src->register_buffer (regnum));
301 /* See gdbsupport/common-regcache.h. */
303 enum register_status
304 reg_buffer::get_register_status (int regnum) const
306 assert_regnum (regnum);
308 return m_register_status[regnum];
311 void
312 reg_buffer::invalidate (int regnum)
314 assert_regnum (regnum);
315 m_register_status[regnum] = REG_UNKNOWN;
318 void
319 reg_buffer::assert_regnum (int regnum) const
321 gdb_assert (regnum >= 0);
322 if (m_has_pseudo)
323 gdb_assert (regnum < m_descr->nr_cooked_registers);
324 else
325 gdb_assert (regnum < gdbarch_num_regs (arch ()));
328 /* Type to map a ptid to a list of regcaches (one thread may have multiple
329 regcaches, associated to different gdbarches). */
331 using ptid_regcache_map
332 = std::unordered_multimap<ptid_t, regcache_up>;
334 /* Type holding regcaches for a given pid. */
336 using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>;
338 /* Type holding regcaches for a given target. */
340 using target_pid_ptid_regcache_map
341 = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>;
343 /* Global structure containing the existing regcaches. */
345 /* NOTE: this is a write-through cache. There is no "dirty" bit for
346 recording if the register values have been changed (eg. by the
347 user). Therefore all registers must be written back to the
348 target when appropriate. */
349 static target_pid_ptid_regcache_map regcaches;
351 struct regcache *
352 get_thread_arch_aspace_regcache (inferior *inf_for_target_calls,
353 ptid_t ptid, gdbarch *arch,
354 struct address_space *aspace)
356 gdb_assert (inf_for_target_calls != nullptr);
358 process_stratum_target *proc_target = inf_for_target_calls->process_target ();
359 gdb_assert (proc_target != nullptr);
361 /* Find the map for this target. */
362 pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[proc_target];
364 /* Find the map for this pid. */
365 ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()];
367 /* Check first if a regcache for this arch already exists. */
368 auto range = ptid_regc_map.equal_range (ptid);
369 for (auto it = range.first; it != range.second; ++it)
371 if (it->second->arch () == arch)
372 return it->second.get ();
375 /* It does not exist, create it. */
376 regcache *new_regcache = new regcache (inf_for_target_calls, arch, aspace);
377 new_regcache->set_ptid (ptid);
378 /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
379 constructor explicitly instead of implicitly. */
380 ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
382 return new_regcache;
385 struct regcache *
386 get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
387 struct gdbarch *gdbarch)
389 scoped_restore_current_inferior restore_current_inferior;
390 inferior *inf = find_inferior_ptid (target, ptid);
391 set_current_inferior (inf);
392 address_space *aspace = target_thread_address_space (ptid);
394 return get_thread_arch_aspace_regcache (inf, ptid, gdbarch, aspace);
397 static process_stratum_target *current_thread_target;
398 static ptid_t current_thread_ptid;
399 static struct gdbarch *current_thread_arch;
401 struct regcache *
402 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
404 if (!current_thread_arch
405 || target != current_thread_target
406 || current_thread_ptid != ptid)
408 gdb_assert (ptid != null_ptid);
410 current_thread_ptid = ptid;
411 current_thread_target = target;
413 scoped_restore_current_inferior restore_current_inferior;
414 set_current_inferior (find_inferior_ptid (target, ptid));
415 current_thread_arch = target_thread_architecture (ptid);
418 return get_thread_arch_regcache (target, ptid, current_thread_arch);
421 /* See regcache.h. */
423 struct regcache *
424 get_thread_regcache (thread_info *thread)
426 return get_thread_regcache (thread->inf->process_target (),
427 thread->ptid);
430 struct regcache *
431 get_current_regcache (void)
433 return get_thread_regcache (inferior_thread ());
436 /* See gdbsupport/common-regcache.h. */
438 struct regcache *
439 get_thread_regcache_for_ptid (ptid_t ptid)
441 /* This function doesn't take a process_stratum_target parameter
442 because it's a gdbsupport/ routine implemented by both gdb and
443 gdbserver. It always refers to a ptid of the current target. */
444 process_stratum_target *proc_target = current_inferior ()->process_target ();
445 return get_thread_regcache (proc_target, ptid);
448 /* Observer for the target_changed event. */
450 static void
451 regcache_observer_target_changed (struct target_ops *target)
453 registers_changed ();
456 /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
457 static void
458 regcache_thread_ptid_changed (process_stratum_target *target,
459 ptid_t old_ptid, ptid_t new_ptid)
461 /* Look up map for target. */
462 auto pid_ptid_regc_map_it = regcaches.find (target);
463 if (pid_ptid_regc_map_it == regcaches.end ())
464 return;
466 /* Look up map for pid. */
467 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
468 auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ());
469 if (ptid_regc_map_it == pid_ptid_regc_map.end ())
470 return;
472 /* Update all regcaches belonging to old_ptid. */
473 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
474 auto range = ptid_regc_map.equal_range (old_ptid);
475 for (auto it = range.first; it != range.second;)
477 regcache_up rc = std::move (it->second);
478 rc->set_ptid (new_ptid);
480 /* Remove old before inserting new, to avoid rehashing,
481 which would invalidate iterators. */
482 it = ptid_regc_map.erase (it);
483 ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
487 /* Low level examining and depositing of registers.
489 The caller is responsible for making sure that the inferior is
490 stopped before calling the fetching routines, or it will get
491 garbage. (a change from GDB version 3, in which the caller got the
492 value from the last stop). */
494 /* REGISTERS_CHANGED ()
496 Indicate that registers may have changed, so invalidate the cache. */
498 void
499 registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
501 if (target == nullptr)
503 /* Since there can be ptid clashes between targets, it's not valid to
504 pass a ptid without saying to which target it belongs. */
505 gdb_assert (ptid == minus_one_ptid);
507 /* Delete all the regcaches of all targets. */
508 regcaches.clear ();
510 else if (ptid.is_pid ())
512 /* Non-NULL target and pid ptid, delete all regcaches belonging
513 to this (TARGET, PID). */
515 /* Look up map for target. */
516 auto pid_ptid_regc_map_it = regcaches.find (target);
517 if (pid_ptid_regc_map_it != regcaches.end ())
519 pid_ptid_regcache_map &pid_ptid_regc_map
520 = pid_ptid_regc_map_it->second;
522 pid_ptid_regc_map.erase (ptid.pid ());
525 else if (ptid != minus_one_ptid)
527 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
528 to this (TARGET, PTID). */
530 /* Look up map for target. */
531 auto pid_ptid_regc_map_it = regcaches.find (target);
532 if (pid_ptid_regc_map_it != regcaches.end ())
534 pid_ptid_regcache_map &pid_ptid_regc_map
535 = pid_ptid_regc_map_it->second;
537 /* Look up map for pid. */
538 auto ptid_regc_map_it
539 = pid_ptid_regc_map.find (ptid.pid ());
540 if (ptid_regc_map_it != pid_ptid_regc_map.end ())
542 ptid_regcache_map &ptid_regc_map
543 = ptid_regc_map_it->second;
545 ptid_regc_map.erase (ptid);
549 else
551 /* Non-NULL target and minus_one_ptid, delete all regcaches
552 associated to this target. */
553 regcaches.erase (target);
556 if ((target == nullptr || current_thread_target == target)
557 && current_thread_ptid.matches (ptid))
559 current_thread_target = NULL;
560 current_thread_ptid = null_ptid;
561 current_thread_arch = NULL;
564 if ((target == nullptr || current_inferior ()->process_target () == target)
565 && inferior_ptid.matches (ptid))
567 /* We just deleted the regcache of the current thread. Need to
568 forget about any frames we have cached, too. */
569 reinit_frame_cache ();
573 /* See regcache.h. */
575 void
576 registers_changed_thread (thread_info *thread)
578 registers_changed_ptid (thread->inf->process_target (), thread->ptid);
581 void
582 registers_changed (void)
584 registers_changed_ptid (nullptr, minus_one_ptid);
587 void
588 regcache::raw_update (int regnum)
590 assert_regnum (regnum);
592 /* Make certain that the register cache is up-to-date with respect
593 to the current thread. This switching shouldn't be necessary
594 only there is still only one target side register cache. Sigh!
595 On the bright side, at least there is a regcache object. */
597 if (get_register_status (regnum) == REG_UNKNOWN)
599 gdb::optional<scoped_restore_current_thread> maybe_restore_thread
600 = maybe_switch_inferior (m_inf_for_target_calls);
602 target_fetch_registers (this, regnum);
604 /* A number of targets can't access the whole set of raw
605 registers (because the debug API provides no means to get at
606 them). */
607 if (m_register_status[regnum] == REG_UNKNOWN)
608 m_register_status[regnum] = REG_UNAVAILABLE;
612 enum register_status
613 readable_regcache::raw_read (int regnum, gdb_byte *buf)
615 gdb_assert (buf != NULL);
616 raw_update (regnum);
618 if (m_register_status[regnum] != REG_VALID)
619 memset (buf, 0, m_descr->sizeof_register[regnum]);
620 else
621 memcpy (buf, register_buffer (regnum),
622 m_descr->sizeof_register[regnum]);
624 return m_register_status[regnum];
627 enum register_status
628 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
630 gdb_assert (regcache != NULL);
631 return regcache->raw_read (regnum, val);
634 template<typename T, typename>
635 enum register_status
636 readable_regcache::raw_read (int regnum, T *val)
638 assert_regnum (regnum);
639 size_t len = m_descr->sizeof_register[regnum];
640 gdb_byte *buf = (gdb_byte *) alloca (len);
641 register_status status = raw_read (regnum, buf);
642 if (status == REG_VALID)
643 *val = extract_integer<T> ({buf, len},
644 gdbarch_byte_order (m_descr->gdbarch));
645 else
646 *val = 0;
647 return status;
650 enum register_status
651 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
652 ULONGEST *val)
654 gdb_assert (regcache != NULL);
655 return regcache->raw_read (regnum, val);
658 void
659 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
661 gdb_assert (regcache != NULL);
662 regcache->raw_write (regnum, val);
665 template<typename T, typename>
666 void
667 regcache::raw_write (int regnum, T val)
669 gdb_byte *buf;
671 assert_regnum (regnum);
672 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
673 store_integer (buf, m_descr->sizeof_register[regnum],
674 gdbarch_byte_order (m_descr->gdbarch), val);
675 raw_write (regnum, buf);
678 void
679 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
680 ULONGEST val)
682 gdb_assert (regcache != NULL);
683 regcache->raw_write (regnum, val);
686 LONGEST
687 regcache_raw_get_signed (struct regcache *regcache, int regnum)
689 LONGEST value;
690 enum register_status status;
692 status = regcache_raw_read_signed (regcache, regnum, &value);
693 if (status == REG_UNAVAILABLE)
694 throw_error (NOT_AVAILABLE_ERROR,
695 _("Register %d is not available"), regnum);
696 return value;
699 enum register_status
700 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
702 gdb_assert (regnum >= 0);
703 gdb_assert (regnum < m_descr->nr_cooked_registers);
704 if (regnum < num_raw_registers ())
705 return raw_read (regnum, buf);
706 else if (m_has_pseudo
707 && m_register_status[regnum] != REG_UNKNOWN)
709 if (m_register_status[regnum] == REG_VALID)
710 memcpy (buf, register_buffer (regnum),
711 m_descr->sizeof_register[regnum]);
712 else
713 memset (buf, 0, m_descr->sizeof_register[regnum]);
715 return m_register_status[regnum];
717 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
719 struct value *computed;
720 enum register_status result = REG_VALID;
722 scoped_value_mark mark;
724 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
725 this, regnum);
726 if (computed->entirely_available ())
727 memcpy (buf, computed->contents_raw ().data (),
728 m_descr->sizeof_register[regnum]);
729 else
731 memset (buf, 0, m_descr->sizeof_register[regnum]);
732 result = REG_UNAVAILABLE;
735 return result;
737 else
738 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
739 regnum, buf);
742 struct value *
743 readable_regcache::cooked_read_value (int regnum)
745 gdb_assert (regnum >= 0);
746 gdb_assert (regnum < m_descr->nr_cooked_registers);
748 if (regnum < num_raw_registers ()
749 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
750 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
752 struct value *result;
754 result = value::allocate (register_type (m_descr->gdbarch, regnum));
755 result->set_lval (lval_register);
756 VALUE_REGNUM (result) = regnum;
758 /* It is more efficient in general to do this delegation in this
759 direction than in the other one, even though the value-based
760 API is preferred. */
761 if (cooked_read (regnum,
762 result->contents_raw ().data ()) == REG_UNAVAILABLE)
763 result->mark_bytes_unavailable (0,
764 result->type ()->length ());
766 return result;
768 else
769 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
770 this, regnum);
773 enum register_status
774 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
775 LONGEST *val)
777 gdb_assert (regcache != NULL);
778 return regcache->cooked_read (regnum, val);
781 template<typename T, typename>
782 enum register_status
783 readable_regcache::cooked_read (int regnum, T *val)
785 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
786 size_t len = m_descr->sizeof_register[regnum];
787 gdb_byte *buf = (gdb_byte *) alloca (len);
788 register_status status = cooked_read (regnum, buf);
789 if (status == REG_VALID)
790 *val = extract_integer<T> ({buf, len},
791 gdbarch_byte_order (m_descr->gdbarch));
792 else
793 *val = 0;
794 return status;
797 enum register_status
798 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
799 ULONGEST *val)
801 gdb_assert (regcache != NULL);
802 return regcache->cooked_read (regnum, val);
805 void
806 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
807 LONGEST val)
809 gdb_assert (regcache != NULL);
810 regcache->cooked_write (regnum, val);
813 template<typename T, typename>
814 void
815 regcache::cooked_write (int regnum, T val)
817 gdb_byte *buf;
819 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
820 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
821 store_integer (buf, m_descr->sizeof_register[regnum],
822 gdbarch_byte_order (m_descr->gdbarch), val);
823 cooked_write (regnum, buf);
826 void
827 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
828 ULONGEST val)
830 gdb_assert (regcache != NULL);
831 regcache->cooked_write (regnum, val);
834 void
835 regcache::raw_write (int regnum, const gdb_byte *buf)
838 gdb_assert (buf != NULL);
839 assert_regnum (regnum);
841 /* On the sparc, writing %g0 is a no-op, so we don't even want to
842 change the registers array if something writes to this register. */
843 if (gdbarch_cannot_store_register (arch (), regnum))
844 return;
846 /* If we have a valid copy of the register, and new value == old
847 value, then don't bother doing the actual store. */
848 if (get_register_status (regnum) == REG_VALID
849 && (memcmp (register_buffer (regnum), buf,
850 m_descr->sizeof_register[regnum]) == 0))
851 return;
853 gdb::optional<scoped_restore_current_thread> maybe_restore_thread
854 = maybe_switch_inferior (m_inf_for_target_calls);
856 target_prepare_to_store (this);
857 raw_supply (regnum, buf);
859 /* Invalidate the register after it is written, in case of a
860 failure. */
861 auto invalidator
862 = make_scope_exit ([&] { this->invalidate (regnum); });
864 target_store_registers (this, regnum);
866 /* The target did not throw an error so we can discard invalidating
867 the register. */
868 invalidator.release ();
871 void
872 regcache::cooked_write (int regnum, const gdb_byte *buf)
874 gdb_assert (regnum >= 0);
875 gdb_assert (regnum < m_descr->nr_cooked_registers);
876 if (regnum < num_raw_registers ())
877 raw_write (regnum, buf);
878 else
879 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
880 regnum, buf);
883 /* See regcache.h. */
885 enum register_status
886 readable_regcache::read_part (int regnum, int offset, int len,
887 gdb_byte *out, bool is_raw)
889 int reg_size = register_size (arch (), regnum);
891 gdb_assert (out != NULL);
892 gdb_assert (offset >= 0 && offset <= reg_size);
893 gdb_assert (len >= 0 && offset + len <= reg_size);
895 if (offset == 0 && len == 0)
897 /* Nothing to do. */
898 return REG_VALID;
901 if (offset == 0 && len == reg_size)
903 /* Read the full register. */
904 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
907 enum register_status status;
908 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
910 /* Read full register to buffer. */
911 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
912 if (status != REG_VALID)
913 return status;
915 /* Copy out. */
916 memcpy (out, reg + offset, len);
917 return REG_VALID;
920 /* See regcache.h. */
922 void
923 reg_buffer::raw_collect_part (int regnum, int offset, int len,
924 gdb_byte *out) const
926 int reg_size = register_size (arch (), regnum);
928 gdb_assert (out != nullptr);
929 gdb_assert (offset >= 0 && offset <= reg_size);
930 gdb_assert (len >= 0 && offset + len <= reg_size);
932 if (offset == 0 && len == 0)
934 /* Nothing to do. */
935 return;
938 if (offset == 0 && len == reg_size)
940 /* Collect the full register. */
941 return raw_collect (regnum, out);
944 /* Read to buffer, then write out. */
945 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
946 raw_collect (regnum, reg);
947 memcpy (out, reg + offset, len);
950 /* See regcache.h. */
952 enum register_status
953 regcache::write_part (int regnum, int offset, int len,
954 const gdb_byte *in, bool is_raw)
956 int reg_size = register_size (arch (), regnum);
958 gdb_assert (in != NULL);
959 gdb_assert (offset >= 0 && offset <= reg_size);
960 gdb_assert (len >= 0 && offset + len <= reg_size);
962 if (offset == 0 && len == 0)
964 /* Nothing to do. */
965 return REG_VALID;
968 if (offset == 0 && len == reg_size)
970 /* Write the full register. */
971 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
972 return REG_VALID;
975 enum register_status status;
976 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
978 /* Read existing register to buffer. */
979 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
980 if (status != REG_VALID)
981 return status;
983 /* Update buffer, then write back to regcache. */
984 memcpy (reg + offset, in, len);
985 is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
986 return REG_VALID;
989 /* See regcache.h. */
991 void
992 reg_buffer::raw_supply_part (int regnum, int offset, int len,
993 const gdb_byte *in)
995 int reg_size = register_size (arch (), regnum);
997 gdb_assert (in != nullptr);
998 gdb_assert (offset >= 0 && offset <= reg_size);
999 gdb_assert (len >= 0 && offset + len <= reg_size);
1001 if (offset == 0 && len == 0)
1003 /* Nothing to do. */
1004 return;
1007 if (offset == 0 && len == reg_size)
1009 /* Supply the full register. */
1010 return raw_supply (regnum, in);
1013 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
1015 /* Read existing value to buffer. */
1016 raw_collect (regnum, reg);
1018 /* Write to buffer, then write out. */
1019 memcpy (reg + offset, in, len);
1020 raw_supply (regnum, reg);
1023 enum register_status
1024 readable_regcache::raw_read_part (int regnum, int offset, int len,
1025 gdb_byte *buf)
1027 assert_regnum (regnum);
1028 return read_part (regnum, offset, len, buf, true);
1031 /* See regcache.h. */
1033 void
1034 regcache::raw_write_part (int regnum, int offset, int len,
1035 const gdb_byte *buf)
1037 assert_regnum (regnum);
1038 write_part (regnum, offset, len, buf, true);
1041 /* See regcache.h. */
1043 enum register_status
1044 readable_regcache::cooked_read_part (int regnum, int offset, int len,
1045 gdb_byte *buf)
1047 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1048 return read_part (regnum, offset, len, buf, false);
1051 /* See regcache.h. */
1053 void
1054 regcache::cooked_write_part (int regnum, int offset, int len,
1055 const gdb_byte *buf)
1057 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1058 write_part (regnum, offset, len, buf, false);
1061 /* See gdbsupport/common-regcache.h. */
1063 void
1064 reg_buffer::raw_supply (int regnum, const void *buf)
1066 void *regbuf;
1067 size_t size;
1069 assert_regnum (regnum);
1071 regbuf = register_buffer (regnum);
1072 size = m_descr->sizeof_register[regnum];
1074 if (buf)
1076 memcpy (regbuf, buf, size);
1077 m_register_status[regnum] = REG_VALID;
1079 else
1081 /* This memset not strictly necessary, but better than garbage
1082 in case the register value manages to escape somewhere (due
1083 to a bug, no less). */
1084 memset (regbuf, 0, size);
1085 m_register_status[regnum] = REG_UNAVAILABLE;
1089 /* See regcache.h. */
1091 void
1092 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1093 int addr_len, bool is_signed)
1095 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1096 gdb_byte *regbuf;
1097 size_t regsize;
1099 assert_regnum (regnum);
1101 regbuf = register_buffer (regnum);
1102 regsize = m_descr->sizeof_register[regnum];
1104 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1105 byte_order);
1106 m_register_status[regnum] = REG_VALID;
1109 /* See regcache.h. */
1111 void
1112 reg_buffer::raw_supply_zeroed (int regnum)
1114 void *regbuf;
1115 size_t size;
1117 assert_regnum (regnum);
1119 regbuf = register_buffer (regnum);
1120 size = m_descr->sizeof_register[regnum];
1122 memset (regbuf, 0, size);
1123 m_register_status[regnum] = REG_VALID;
1126 /* See gdbsupport/common-regcache.h. */
1128 void
1129 reg_buffer::raw_collect (int regnum, void *buf) const
1131 const void *regbuf;
1132 size_t size;
1134 gdb_assert (buf != NULL);
1135 assert_regnum (regnum);
1137 regbuf = register_buffer (regnum);
1138 size = m_descr->sizeof_register[regnum];
1139 memcpy (buf, regbuf, size);
1142 /* See regcache.h. */
1144 void
1145 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1146 bool is_signed) const
1148 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1149 const gdb_byte *regbuf;
1150 size_t regsize;
1152 assert_regnum (regnum);
1154 regbuf = register_buffer (regnum);
1155 regsize = m_descr->sizeof_register[regnum];
1157 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1158 byte_order);
1161 /* See regcache.h. */
1163 void
1164 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1165 const gdb_byte *in_buf, gdb_byte *out_buf,
1166 int slot_size, int offs) const
1168 struct gdbarch *gdbarch = arch ();
1169 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1171 /* Use part versions and reg_size to prevent possible buffer overflows when
1172 accessing the regcache. */
1174 if (out_buf != nullptr)
1176 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1178 /* Ensure any additional space is cleared. */
1179 if (slot_size > reg_size)
1180 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1182 else if (in_buf != nullptr)
1184 /* Zero-extend the register value if the slot is smaller than the register. */
1185 if (slot_size < register_size (gdbarch, regnum))
1186 out_regcache->raw_supply_zeroed (regnum);
1187 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1189 else
1191 /* Invalidate the register. */
1192 out_regcache->raw_supply (regnum, nullptr);
1196 /* See regcache.h. */
1198 void
1199 regcache::transfer_regset (const struct regset *regset, int regbase,
1200 struct regcache *out_regcache,
1201 int regnum, const gdb_byte *in_buf,
1202 gdb_byte *out_buf, size_t size) const
1204 const struct regcache_map_entry *map;
1205 int offs = 0, count;
1207 for (map = (const struct regcache_map_entry *) regset->regmap;
1208 (count = map->count) != 0;
1209 map++)
1211 int regno = map->regno;
1212 int slot_size = map->size;
1214 if (regno != REGCACHE_MAP_SKIP)
1215 regno += regbase;
1217 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1218 slot_size = m_descr->sizeof_register[regno];
1220 if (regno == REGCACHE_MAP_SKIP
1221 || (regnum != -1
1222 && (regnum < regno || regnum >= regno + count)))
1223 offs += count * slot_size;
1225 else if (regnum == -1)
1226 for (; count--; regno++, offs += slot_size)
1228 if (offs + slot_size > size)
1229 break;
1231 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1232 slot_size, offs);
1234 else
1236 /* Transfer a single register and return. */
1237 offs += (regnum - regno) * slot_size;
1238 if (offs + slot_size > size)
1239 return;
1241 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1242 slot_size, offs);
1243 return;
1248 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1249 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1250 If BUF is NULL, set the register(s) to "unavailable" status. */
1252 void
1253 regcache_supply_regset (const struct regset *regset,
1254 struct regcache *regcache,
1255 int regnum, const void *buf, size_t size)
1257 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1260 /* See regcache.h. */
1262 void
1263 regcache::supply_regset (const struct regset *regset, int regbase,
1264 int regnum, const void *buf, size_t size)
1266 transfer_regset (regset, regbase, this, regnum, (const gdb_byte *) buf,
1267 nullptr, size);
1270 /* Collect register REGNUM from REGCACHE to BUF, using the register
1271 map in REGSET. If REGNUM is -1, do this for all registers in
1272 REGSET. */
1274 void
1275 regcache_collect_regset (const struct regset *regset,
1276 const struct regcache *regcache,
1277 int regnum, void *buf, size_t size)
1279 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1282 /* See regcache.h */
1284 void
1285 regcache::collect_regset (const struct regset *regset, int regbase,
1286 int regnum, void *buf, size_t size) const
1288 transfer_regset (regset, regbase, nullptr, regnum, nullptr, (gdb_byte *) buf,
1289 size);
1292 bool
1293 regcache_map_supplies (const struct regcache_map_entry *map, int regnum,
1294 struct gdbarch *gdbarch, size_t size)
1296 int offs = 0, count;
1298 for (; (count = map->count) != 0; map++)
1300 int regno = map->regno;
1301 int slot_size = map->size;
1303 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1304 slot_size = register_size (gdbarch, regno);
1306 if (regno != REGCACHE_MAP_SKIP && regnum >= regno
1307 && regnum < regno + count)
1308 return offs + (regnum - regno + 1) * slot_size <= size;
1310 offs += count * slot_size;
1311 if (offs >= size)
1312 return false;
1314 return false;
1317 /* See gdbsupport/common-regcache.h. */
1319 bool
1320 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1322 gdb_assert (buf != NULL);
1323 assert_regnum (regnum);
1325 const char *regbuf = (const char *) register_buffer (regnum);
1326 size_t size = m_descr->sizeof_register[regnum];
1327 gdb_assert (size >= offset);
1329 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1332 /* Special handling for register PC. */
1334 CORE_ADDR
1335 regcache_read_pc (struct regcache *regcache)
1337 struct gdbarch *gdbarch = regcache->arch ();
1339 CORE_ADDR pc_val;
1341 if (gdbarch_read_pc_p (gdbarch))
1342 pc_val = gdbarch_read_pc (gdbarch, regcache);
1343 /* Else use per-frame method on get_current_frame. */
1344 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1346 ULONGEST raw_val;
1348 if (regcache_cooked_read_unsigned (regcache,
1349 gdbarch_pc_regnum (gdbarch),
1350 &raw_val) == REG_UNAVAILABLE)
1351 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1353 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1355 else
1356 internal_error (_("regcache_read_pc: Unable to find PC"));
1357 return pc_val;
1360 /* See gdbsupport/common-regcache.h. */
1362 CORE_ADDR
1363 regcache_read_pc_protected (regcache *regcache)
1365 CORE_ADDR pc;
1368 pc = regcache_read_pc (regcache);
1370 catch (const gdb_exception_error &ex)
1372 pc = 0;
1375 return pc;
1378 void
1379 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1381 struct gdbarch *gdbarch = regcache->arch ();
1383 if (gdbarch_write_pc_p (gdbarch))
1384 gdbarch_write_pc (gdbarch, regcache, pc);
1385 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1386 regcache_cooked_write_unsigned (regcache,
1387 gdbarch_pc_regnum (gdbarch), pc);
1388 else
1389 internal_error (_("regcache_write_pc: Unable to update PC"));
1391 /* Writing the PC (for instance, from "load") invalidates the
1392 current frame. */
1393 reinit_frame_cache ();
1397 reg_buffer::num_raw_registers () const
1399 return gdbarch_num_regs (arch ());
1402 void
1403 regcache::debug_print_register (const char *func, int regno)
1405 struct gdbarch *gdbarch = arch ();
1407 gdb_printf (gdb_stdlog, "%s ", func);
1408 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1409 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1410 gdb_printf (gdb_stdlog, "(%s)",
1411 gdbarch_register_name (gdbarch, regno));
1412 else
1413 gdb_printf (gdb_stdlog, "(%d)", regno);
1414 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1416 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1417 int size = register_size (gdbarch, regno);
1418 gdb_byte *buf = register_buffer (regno);
1420 gdb_printf (gdb_stdlog, " = ");
1421 for (int i = 0; i < size; i++)
1423 gdb_printf (gdb_stdlog, "%02x", buf[i]);
1425 if (size <= sizeof (LONGEST))
1427 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1429 gdb_printf (gdb_stdlog, " %s %s",
1430 core_addr_to_string_nz (val), plongest (val));
1433 gdb_printf (gdb_stdlog, "\n");
1436 /* Implement 'maint flush register-cache' command. */
1438 static void
1439 reg_flush_command (const char *command, int from_tty)
1441 /* Force-flush the register cache. */
1442 registers_changed ();
1443 if (from_tty)
1444 gdb_printf (_("Register cache flushed.\n"));
1447 void
1448 register_dump::dump (ui_file *file)
1450 auto descr = regcache_descr (m_gdbarch);
1451 int regnum;
1452 int footnote_nr = 0;
1453 int footnote_register_offset = 0;
1454 int footnote_register_type_name_null = 0;
1455 long register_offset = 0;
1457 gdb_assert (descr->nr_cooked_registers
1458 == gdbarch_num_cooked_regs (m_gdbarch));
1460 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1462 /* Name. */
1463 if (regnum < 0)
1464 gdb_printf (file, " %-10s", "Name");
1465 else
1467 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1469 if (p[0] == '\0')
1470 p = "''";
1471 gdb_printf (file, " %-10s", p);
1474 /* Number. */
1475 if (regnum < 0)
1476 gdb_printf (file, " %4s", "Nr");
1477 else
1478 gdb_printf (file, " %4d", regnum);
1480 /* Relative number. */
1481 if (regnum < 0)
1482 gdb_printf (file, " %4s", "Rel");
1483 else if (regnum < gdbarch_num_regs (m_gdbarch))
1484 gdb_printf (file, " %4d", regnum);
1485 else
1486 gdb_printf (file, " %4d",
1487 (regnum - gdbarch_num_regs (m_gdbarch)));
1489 /* Offset. */
1490 if (regnum < 0)
1491 gdb_printf (file, " %6s ", "Offset");
1492 else
1494 gdb_printf (file, " %6ld",
1495 descr->register_offset[regnum]);
1496 if (register_offset != descr->register_offset[regnum]
1497 || (regnum > 0
1498 && (descr->register_offset[regnum]
1499 != (descr->register_offset[regnum - 1]
1500 + descr->sizeof_register[regnum - 1])))
1503 if (!footnote_register_offset)
1504 footnote_register_offset = ++footnote_nr;
1505 gdb_printf (file, "*%d", footnote_register_offset);
1507 else
1508 gdb_printf (file, " ");
1509 register_offset = (descr->register_offset[regnum]
1510 + descr->sizeof_register[regnum]);
1513 /* Size. */
1514 if (regnum < 0)
1515 gdb_printf (file, " %5s ", "Size");
1516 else
1517 gdb_printf (file, " %5ld", descr->sizeof_register[regnum]);
1519 /* Type. */
1521 const char *t;
1522 std::string name_holder;
1524 if (regnum < 0)
1525 t = "Type";
1526 else
1528 static const char blt[] = "builtin_type";
1530 t = register_type (m_gdbarch, regnum)->name ();
1531 if (t == NULL)
1533 if (!footnote_register_type_name_null)
1534 footnote_register_type_name_null = ++footnote_nr;
1535 name_holder = string_printf ("*%d",
1536 footnote_register_type_name_null);
1537 t = name_holder.c_str ();
1539 /* Chop a leading builtin_type. */
1540 if (startswith (t, blt))
1541 t += strlen (blt);
1543 gdb_printf (file, " %-15s", t);
1546 /* Leading space always present. */
1547 gdb_printf (file, " ");
1549 dump_reg (file, regnum);
1551 gdb_printf (file, "\n");
1554 if (footnote_register_offset)
1555 gdb_printf (file, "*%d: Inconsistent register offsets.\n",
1556 footnote_register_offset);
1557 if (footnote_register_type_name_null)
1558 gdb_printf (file,
1559 "*%d: Register type's name NULL.\n",
1560 footnote_register_type_name_null);
1563 #if GDB_SELF_TEST
1564 #include "gdbsupport/selftest.h"
1565 #include "selftest-arch.h"
1566 #include "target-float.h"
1568 namespace selftests {
1570 static size_t
1571 regcaches_size ()
1573 size_t size = 0;
1575 for (auto pid_ptid_regc_map_it = regcaches.cbegin ();
1576 pid_ptid_regc_map_it != regcaches.cend ();
1577 ++pid_ptid_regc_map_it)
1579 const pid_ptid_regcache_map &pid_ptid_regc_map
1580 = pid_ptid_regc_map_it->second;
1582 for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin ();
1583 ptid_regc_map_it != pid_ptid_regc_map.cend ();
1584 ++ptid_regc_map_it)
1586 const ptid_regcache_map &ptid_regc_map
1587 = ptid_regc_map_it->second;
1589 size += ptid_regc_map.size ();
1593 return size;
1596 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1598 static int
1599 regcache_count (process_stratum_target *target, ptid_t ptid)
1601 /* Look up map for target. */
1602 auto pid_ptid_regc_map_it = regcaches.find (target);
1603 if (pid_ptid_regc_map_it != regcaches.end ())
1605 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
1607 /* Look map for pid. */
1608 auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ());
1609 if (ptid_regc_map_it != pid_ptid_regc_map.end ())
1611 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
1612 auto range = ptid_regc_map.equal_range (ptid);
1614 return std::distance (range.first, range.second);
1618 return 0;
1621 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1623 static void
1624 get_thread_arch_aspace_regcache_and_check (inferior *inf_for_target_calls,
1625 ptid_t ptid)
1627 /* We currently only test with a single gdbarch. Any gdbarch will do, so use
1628 the current inferior's gdbarch. Also use the current inferior's address
1629 space. */
1630 gdbarch *arch = inf_for_target_calls->arch ();
1631 address_space *aspace = inf_for_target_calls->aspace;
1632 regcache *regcache = get_thread_arch_aspace_regcache (inf_for_target_calls,
1633 ptid, arch, aspace);
1635 SELF_CHECK (regcache != NULL);
1636 SELF_CHECK (regcache->ptid () == ptid);
1637 SELF_CHECK (regcache->arch () == arch);
1638 SELF_CHECK (regcache->aspace () == aspace);
1641 /* The data that the regcaches selftests must hold onto for the duration of the
1642 test. */
1644 struct regcache_test_data
1646 regcache_test_data ()
1647 /* The specific arch doesn't matter. */
1648 : test_ctx_1 (current_inferior ()->arch ()),
1649 test_ctx_2 (current_inferior ()->arch ())
1651 /* Ensure the regcaches container is empty at the start. */
1652 registers_changed ();
1655 ~regcache_test_data ()
1657 /* Make sure to leave the global regcaches container empty. */
1658 registers_changed ();
1661 scoped_mock_context<test_target_ops> test_ctx_1;
1662 scoped_mock_context<test_target_ops> test_ctx_2;
1665 using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
1667 /* Set up a few regcaches from two different targets, for use in
1668 regcache-management tests.
1670 Return a pointer, because the `regcache_test_data` type is not moveable. */
1672 static regcache_test_data_up
1673 populate_regcaches_for_test ()
1675 regcache_test_data_up data (new regcache_test_data);
1676 size_t expected_regcache_size = 0;
1678 SELF_CHECK (regcaches_size () == 0);
1680 /* Populate the regcache container with a few regcaches for the two test
1681 targets. */
1682 for (int pid : { 1, 2 })
1684 for (long lwp : { 1, 2, 3 })
1686 get_thread_arch_aspace_regcache_and_check
1687 (&data->test_ctx_1.mock_inferior, ptid_t (pid, lwp));
1688 expected_regcache_size++;
1689 SELF_CHECK (regcaches_size () == expected_regcache_size);
1691 get_thread_arch_aspace_regcache_and_check
1692 (&data->test_ctx_2.mock_inferior, ptid_t (pid, lwp));
1693 expected_regcache_size++;
1694 SELF_CHECK (regcaches_size () == expected_regcache_size);
1698 return data;
1701 static void
1702 get_thread_arch_aspace_regcache_test ()
1704 /* populate_regcaches_for_test already tests most of the
1705 get_thread_arch_aspace_regcache functionality. */
1706 regcache_test_data_up data = populate_regcaches_for_test ();
1707 size_t regcaches_size_before = regcaches_size ();
1709 /* Test that getting an existing regcache doesn't create a new one. */
1710 get_thread_arch_aspace_regcache_and_check (&data->test_ctx_1.mock_inferior,
1711 ptid_t (2, 2));
1712 SELF_CHECK (regcaches_size () == regcaches_size_before);
1715 /* Test marking all regcaches of all targets as changed. */
1717 static void
1718 registers_changed_ptid_all_test ()
1720 regcache_test_data_up data = populate_regcaches_for_test ();
1722 registers_changed_ptid (nullptr, minus_one_ptid);
1723 SELF_CHECK (regcaches_size () == 0);
1726 /* Test marking regcaches of a specific target as changed. */
1728 static void
1729 registers_changed_ptid_target_test ()
1731 regcache_test_data_up data = populate_regcaches_for_test ();
1733 registers_changed_ptid (&data->test_ctx_1.mock_target, minus_one_ptid);
1734 SELF_CHECK (regcaches_size () == 6);
1736 /* Check that we deleted the regcache for the right target. */
1737 SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target,
1738 ptid_t (2, 2)) == 0);
1739 SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target,
1740 ptid_t (2, 2)) == 1);
1743 /* Test marking regcaches of a specific (target, pid) as changed. */
1745 static void
1746 registers_changed_ptid_target_pid_test ()
1748 regcache_test_data_up data = populate_regcaches_for_test ();
1750 registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2));
1751 SELF_CHECK (regcaches_size () == 9);
1753 /* Regcaches from target1 should not exist, while regcaches from target2
1754 should exist. */
1755 SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target,
1756 ptid_t (2, 2)) == 0);
1757 SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target,
1758 ptid_t (2, 2)) == 1);
1761 /* Test marking regcaches of a specific (target, ptid) as changed. */
1763 static void
1764 registers_changed_ptid_target_ptid_test ()
1766 regcache_test_data_up data = populate_regcaches_for_test ();
1768 registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2, 2));
1769 SELF_CHECK (regcaches_size () == 11);
1771 /* Check that we deleted the regcache for the right target. */
1772 SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target,
1773 ptid_t (2, 2)) == 0);
1774 SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target,
1775 ptid_t (2, 2)) == 1);
1778 class target_ops_no_register : public test_target_ops
1780 public:
1781 target_ops_no_register ()
1782 : test_target_ops {}
1785 void reset ()
1787 fetch_registers_called = 0;
1788 store_registers_called = 0;
1789 xfer_partial_called = 0;
1792 void fetch_registers (regcache *regs, int regno) override;
1793 void store_registers (regcache *regs, int regno) override;
1795 enum target_xfer_status xfer_partial (enum target_object object,
1796 const char *annex, gdb_byte *readbuf,
1797 const gdb_byte *writebuf,
1798 ULONGEST offset, ULONGEST len,
1799 ULONGEST *xfered_len) override;
1801 unsigned int fetch_registers_called = 0;
1802 unsigned int store_registers_called = 0;
1803 unsigned int xfer_partial_called = 0;
1806 void
1807 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1809 /* Mark register available. */
1810 regs->raw_supply_zeroed (regno);
1811 this->fetch_registers_called++;
1814 void
1815 target_ops_no_register::store_registers (regcache *regs, int regno)
1817 this->store_registers_called++;
1820 enum target_xfer_status
1821 target_ops_no_register::xfer_partial (enum target_object object,
1822 const char *annex, gdb_byte *readbuf,
1823 const gdb_byte *writebuf,
1824 ULONGEST offset, ULONGEST len,
1825 ULONGEST *xfered_len)
1827 this->xfer_partial_called++;
1829 *xfered_len = len;
1830 return TARGET_XFER_OK;
1833 class readwrite_regcache : public regcache
1835 public:
1836 readwrite_regcache (inferior *inf_for_target_calls,
1837 struct gdbarch *gdbarch)
1838 : regcache (inf_for_target_calls, gdbarch, nullptr)
1842 /* Return true if regcache::cooked_{read,write}_test should be skipped for
1843 GDBARCH. */
1845 static bool
1846 selftest_skiparch (struct gdbarch *gdbarch)
1848 const char *name = gdbarch_bfd_arch_info (gdbarch)->printable_name;
1850 /* Avoid warning:
1851 Running selftest regcache::cooked_{read,write}_test::m68hc11.
1852 warning: No frame soft register found in the symbol table.
1853 Stack backtrace will not work.
1854 We could instead capture the output and then filter out the warning, but
1855 that seems more trouble than it's worth. */
1856 return (strcmp (name, "m68hc11") == 0
1857 || strcmp (name, "m68hc12") == 0
1858 || strcmp (name, "m68hc12:HCS12") == 0);
1861 /* Test regcache::cooked_read gets registers from raw registers and
1862 memory instead of target to_{fetch,store}_registers. */
1864 static void
1865 cooked_read_test (struct gdbarch *gdbarch)
1867 if (selftest_skiparch (gdbarch))
1868 return;
1870 scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1872 /* Test that read one raw register from regcache_no_target will go
1873 to the target layer. */
1875 /* Find a raw register which size isn't zero. */
1876 int nonzero_regnum;
1877 for (nonzero_regnum = 0;
1878 nonzero_regnum < gdbarch_num_regs (gdbarch);
1879 nonzero_regnum++)
1881 if (register_size (gdbarch, nonzero_regnum) != 0)
1882 break;
1885 readwrite_regcache readwrite (&mockctx.mock_inferior, gdbarch);
1886 readwrite.set_ptid (mockctx.mock_ptid);
1887 gdb::byte_vector buf (register_size (gdbarch, nonzero_regnum));
1889 readwrite.raw_read (nonzero_regnum, buf.data ());
1891 /* raw_read calls target_fetch_registers. */
1892 SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1893 mockctx.mock_target.reset ();
1895 /* Mark all raw registers valid, so the following raw registers
1896 accesses won't go to target. */
1897 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1898 readwrite.raw_update (i);
1900 mockctx.mock_target.reset ();
1901 /* Then, read all raw and pseudo registers, and don't expect calling
1902 to_{fetch,store}_registers. */
1903 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1905 if (register_size (gdbarch, regnum) == 0)
1906 continue;
1908 gdb::byte_vector inner_buf (register_size (gdbarch, regnum));
1910 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1911 inner_buf.data ()));
1913 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1914 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1915 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1917 mockctx.mock_target.reset ();
1920 readonly_detached_regcache readonly (readwrite);
1922 /* GDB may go to target layer to fetch all registers and memory for
1923 readonly regcache. */
1924 mockctx.mock_target.reset ();
1926 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1928 if (register_size (gdbarch, regnum) == 0)
1929 continue;
1931 gdb::byte_vector inner_buf (register_size (gdbarch, regnum));
1932 enum register_status status = readonly.cooked_read (regnum,
1933 inner_buf.data ());
1935 if (regnum < gdbarch_num_regs (gdbarch))
1937 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1939 if (bfd_arch == bfd_arch_amdgcn
1940 || bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1941 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1942 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1943 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1944 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1945 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1946 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1947 || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
1949 /* Raw registers. If raw registers are not in save_reggroup,
1950 their status are unknown. */
1951 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1952 SELF_CHECK (status == REG_VALID);
1953 else
1954 SELF_CHECK (status == REG_UNKNOWN);
1956 else
1957 SELF_CHECK (status == REG_VALID);
1959 else
1961 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1962 SELF_CHECK (status == REG_VALID);
1963 else
1965 /* If pseudo registers are not in save_reggroup, some of
1966 them can be computed from saved raw registers, but some
1967 of them are unknown. */
1968 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1970 if (bfd_arch == bfd_arch_frv
1971 || bfd_arch == bfd_arch_m32c
1972 || bfd_arch == bfd_arch_mep
1973 || bfd_arch == bfd_arch_sh)
1974 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1975 else if (bfd_arch == bfd_arch_mips
1976 || bfd_arch == bfd_arch_h8300)
1977 SELF_CHECK (status == REG_UNKNOWN);
1978 else
1979 SELF_CHECK (status == REG_VALID);
1983 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1984 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1985 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1987 mockctx.mock_target.reset ();
1991 /* Test regcache::cooked_write by writing some expected contents to
1992 registers, and checking that contents read from registers and the
1993 expected contents are the same. */
1995 static void
1996 cooked_write_test (struct gdbarch *gdbarch)
1998 if (selftest_skiparch (gdbarch))
1999 return;
2001 /* Create a mock environment. A process_stratum target pushed. */
2002 scoped_mock_context<target_ops_no_register> ctx (gdbarch);
2003 readwrite_regcache readwrite (&ctx.mock_inferior, gdbarch);
2004 readwrite.set_ptid (ctx.mock_ptid);
2005 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
2007 for (auto regnum = 0; regnum < num_regs; regnum++)
2009 if (register_size (gdbarch, regnum) == 0
2010 || gdbarch_cannot_store_register (gdbarch, regnum))
2011 continue;
2013 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2015 if (bfd_arch == bfd_arch_sparc
2016 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
2017 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
2018 && gdbarch_ptr_bit (gdbarch) == 64
2019 && (regnum >= gdbarch_num_regs (gdbarch)
2020 && regnum <= gdbarch_num_regs (gdbarch) + 4))
2021 continue;
2023 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
2024 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
2025 const auto type = register_type (gdbarch, regnum);
2027 if (type->code () == TYPE_CODE_FLT
2028 || type->code () == TYPE_CODE_DECFLOAT)
2030 /* Generate valid float format. */
2031 target_float_from_string (expected.data (), type, "1.25");
2033 else if (type->code () == TYPE_CODE_INT
2034 || type->code () == TYPE_CODE_ARRAY
2035 || type->code () == TYPE_CODE_PTR
2036 || type->code () == TYPE_CODE_UNION
2037 || type->code () == TYPE_CODE_STRUCT)
2039 if (bfd_arch == bfd_arch_ia64
2040 || (regnum >= gdbarch_num_regs (gdbarch)
2041 && (bfd_arch == bfd_arch_xtensa
2042 || bfd_arch == bfd_arch_bfin
2043 || bfd_arch == bfd_arch_m32c
2044 /* m68hc11 pseudo registers are in memory. */
2045 || bfd_arch == bfd_arch_m68hc11
2046 || bfd_arch == bfd_arch_m68hc12
2047 || bfd_arch == bfd_arch_s390))
2048 || (bfd_arch == bfd_arch_frv
2049 /* FRV pseudo registers except iacc0. */
2050 && regnum > gdbarch_num_regs (gdbarch)))
2052 /* Skip setting the expected values for some architecture
2053 registers. */
2055 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
2057 /* RL78_PC_REGNUM */
2058 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
2059 expected[j] = j;
2061 else
2063 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
2064 expected[j] = j;
2067 else if (type->code () == TYPE_CODE_FLAGS)
2069 /* No idea how to test flags. */
2070 continue;
2072 else
2074 /* If we don't know how to create the expected value for the
2075 this type, make it fail. */
2076 SELF_CHECK (0);
2079 readwrite.cooked_write (regnum, expected.data ());
2081 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2082 SELF_CHECK (expected == buf);
2086 /* Verify that when two threads with the same ptid exist (from two different
2087 targets) and one of them changes ptid, we only update the appropriate
2088 regcaches. */
2090 static void
2091 regcache_thread_ptid_changed ()
2093 /* This test relies on the global regcache list to initially be empty. */
2094 registers_changed ();
2096 /* Any arch will do. */
2097 gdbarch *arch = current_inferior ()->arch ();
2099 /* Prepare two targets with one thread each, with the same ptid. */
2100 scoped_mock_context<test_target_ops> target1 (arch);
2101 scoped_mock_context<test_target_ops> target2 (arch);
2103 ptid_t old_ptid (111, 222);
2104 ptid_t new_ptid (111, 333);
2106 target1.mock_inferior.pid = old_ptid.pid ();
2107 target1.mock_thread.ptid = old_ptid;
2108 target1.mock_inferior.ptid_thread_map.clear ();
2109 target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread;
2111 target2.mock_inferior.pid = old_ptid.pid ();
2112 target2.mock_thread.ptid = old_ptid;
2113 target2.mock_inferior.ptid_thread_map.clear ();
2114 target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread;
2116 gdb_assert (regcaches.empty ());
2118 /* Populate the regcaches container. */
2119 get_thread_arch_aspace_regcache (&target1.mock_inferior, old_ptid, arch,
2120 nullptr);
2121 get_thread_arch_aspace_regcache (&target2.mock_inferior, old_ptid, arch,
2122 nullptr);
2124 gdb_assert (regcaches.size () == 2);
2125 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
2126 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
2127 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2128 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2130 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
2132 gdb_assert (regcaches.size () == 2);
2133 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
2134 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
2135 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2136 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2138 /* Leave the regcache list empty. */
2139 registers_changed ();
2140 gdb_assert (regcaches.empty ());
2143 } // namespace selftests
2144 #endif /* GDB_SELF_TEST */
2146 void _initialize_regcache ();
2147 void
2148 _initialize_regcache ()
2150 struct cmd_list_element *c;
2152 gdb::observers::target_changed.attach (regcache_observer_target_changed,
2153 "regcache");
2154 gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed,
2155 "regcache");
2157 cmd_list_element *maintenance_flush_register_cache_cmd
2158 = add_cmd ("register-cache", class_maintenance, reg_flush_command,
2159 _("Force gdb to flush its register and frame cache."),
2160 &maintenanceflushlist);
2161 c = add_com_alias ("flushregs", maintenance_flush_register_cache_cmd,
2162 class_maintenance, 0);
2163 deprecate_cmd (c, "maintenance flush register-cache");
2165 #if GDB_SELF_TEST
2166 selftests::register_test ("get_thread_arch_aspace_regcache",
2167 selftests::get_thread_arch_aspace_regcache_test);
2168 selftests::register_test ("registers_changed_ptid_all",
2169 selftests::registers_changed_ptid_all_test);
2170 selftests::register_test ("registers_changed_ptid_target",
2171 selftests::registers_changed_ptid_target_test);
2172 selftests::register_test ("registers_changed_ptid_target_pid",
2173 selftests::registers_changed_ptid_target_pid_test);
2174 selftests::register_test ("registers_changed_ptid_target_ptid",
2175 selftests::registers_changed_ptid_target_ptid_test);
2177 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2178 selftests::cooked_read_test);
2179 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2180 selftests::cooked_write_test);
2181 selftests::register_test ("regcache_thread_ptid_changed",
2182 selftests::regcache_thread_ptid_changed);
2183 #endif