Document the GDB 10.2 release in gdb/ChangeLog
[binutils-gdb.git] / gdb / regcache.c
blobd2386308b8226ea4c40ddbbc4ac36d6700a18272
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/>. */
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 static struct gdbarch_data *regcache_descr_handle;
46 struct regcache_descr
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
55 cache. */
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
70 offset. */
71 long *register_offset;
72 long *sizeof_register;
74 /* Cached table containing the type of each register. */
75 struct type **register_type;
78 static void *
79 init_regcache_descr (struct gdbarch *gdbarch)
81 int i;
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. */
95 descr->register_type
96 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
97 struct type *);
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
109 same. */
112 long offset = 0;
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;
137 return descr;
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. */
150 struct type *
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);
166 int size;
168 gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
169 size = descr->sizeof_register[regnum];
170 return size;
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 if (has_pseudo)
189 m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers] ());
190 m_register_status.reset
191 (new register_status[m_descr->nr_cooked_registers] ());
193 else
195 m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers] ());
196 m_register_status.reset
197 (new register_status[gdbarch_num_regs (gdbarch)] ());
201 regcache::regcache (process_stratum_target *target, gdbarch *gdbarch,
202 const address_space *aspace_)
203 /* The register buffers. A read/write register cache can only hold
204 [0 .. gdbarch_num_regs). */
205 : detached_regcache (gdbarch, false), m_aspace (aspace_), m_target (target)
207 m_ptid = minus_one_ptid;
210 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
211 : readonly_detached_regcache (src.arch (),
212 [&src] (int regnum, gdb_byte *buf)
214 return src.cooked_read (regnum, buf);
219 gdbarch *
220 reg_buffer::arch () const
222 return m_descr->gdbarch;
225 /* Return a pointer to register REGNUM's buffer cache. */
227 gdb_byte *
228 reg_buffer::register_buffer (int regnum) const
230 return m_registers.get () + m_descr->register_offset[regnum];
233 void
234 reg_buffer::save (register_read_ftype cooked_read)
236 struct gdbarch *gdbarch = m_descr->gdbarch;
237 int regnum;
239 /* It should have pseudo registers. */
240 gdb_assert (m_has_pseudo);
241 /* Clear the dest. */
242 memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
243 memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
244 /* Copy over any registers (identified by their membership in the
245 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
246 gdbarch_num_pseudo_regs) range is checked since some architectures need
247 to save/restore `cooked' registers that live in memory. */
248 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
250 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
252 gdb_byte *dst_buf = register_buffer (regnum);
253 enum register_status status = cooked_read (regnum, dst_buf);
255 gdb_assert (status != REG_UNKNOWN);
257 if (status != REG_VALID)
258 memset (dst_buf, 0, register_size (gdbarch, regnum));
260 m_register_status[regnum] = status;
265 void
266 regcache::restore (readonly_detached_regcache *src)
268 struct gdbarch *gdbarch = m_descr->gdbarch;
269 int regnum;
271 gdb_assert (src != NULL);
272 gdb_assert (src->m_has_pseudo);
274 gdb_assert (gdbarch == src->arch ());
276 /* Copy over any registers, being careful to only restore those that
277 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
278 + gdbarch_num_pseudo_regs) range is checked since some architectures need
279 to save/restore `cooked' registers that live in memory. */
280 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
282 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
284 if (src->m_register_status[regnum] == REG_VALID)
285 cooked_write (regnum, src->register_buffer (regnum));
290 /* See gdbsupport/common-regcache.h. */
292 enum register_status
293 reg_buffer::get_register_status (int regnum) const
295 assert_regnum (regnum);
297 return m_register_status[regnum];
300 void
301 reg_buffer::invalidate (int regnum)
303 assert_regnum (regnum);
304 m_register_status[regnum] = REG_UNKNOWN;
307 void
308 reg_buffer::assert_regnum (int regnum) const
310 gdb_assert (regnum >= 0);
311 if (m_has_pseudo)
312 gdb_assert (regnum < m_descr->nr_cooked_registers);
313 else
314 gdb_assert (regnum < gdbarch_num_regs (arch ()));
317 /* Type to map a ptid to a list of regcaches (one thread may have multiple
318 regcaches, associated to different gdbarches). */
320 using ptid_regcache_map
321 = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
323 /* Type holding regcaches for a given pid. */
325 using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>;
327 /* Type holding regcaches for a given target. */
329 using target_pid_ptid_regcache_map
330 = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>;
332 /* Global structure containing the existing regcaches. */
334 /* NOTE: this is a write-through cache. There is no "dirty" bit for
335 recording if the register values have been changed (eg. by the
336 user). Therefore all registers must be written back to the
337 target when appropriate. */
338 static target_pid_ptid_regcache_map regcaches;
340 struct regcache *
341 get_thread_arch_aspace_regcache (process_stratum_target *target,
342 ptid_t ptid, gdbarch *arch,
343 struct address_space *aspace)
345 gdb_assert (target != nullptr);
347 /* Find the map for this target. */
348 pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[target];
350 /* Find the map for this pid. */
351 ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()];
353 /* Check first if a regcache for this arch already exists. */
354 auto range = ptid_regc_map.equal_range (ptid);
355 for (auto it = range.first; it != range.second; ++it)
357 if (it->second->arch () == arch)
358 return it->second.get ();
361 /* It does not exist, create it. */
362 regcache *new_regcache = new regcache (target, arch, aspace);
363 new_regcache->set_ptid (ptid);
364 /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
365 constructor explictly instead of implicitly. */
366 ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
368 return new_regcache;
371 struct regcache *
372 get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
373 struct gdbarch *gdbarch)
375 scoped_restore_current_inferior restore_current_inferior;
376 set_current_inferior (find_inferior_ptid (target, ptid));
377 address_space *aspace = target_thread_address_space (ptid);
379 return get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
382 static process_stratum_target *current_thread_target;
383 static ptid_t current_thread_ptid;
384 static struct gdbarch *current_thread_arch;
386 struct regcache *
387 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
389 if (!current_thread_arch
390 || target != current_thread_target
391 || current_thread_ptid != ptid)
393 gdb_assert (ptid != null_ptid);
395 current_thread_ptid = ptid;
396 current_thread_target = target;
398 scoped_restore_current_inferior restore_current_inferior;
399 set_current_inferior (find_inferior_ptid (target, ptid));
400 current_thread_arch = target_thread_architecture (ptid);
403 return get_thread_arch_regcache (target, ptid, current_thread_arch);
406 /* See regcache.h. */
408 struct regcache *
409 get_thread_regcache (thread_info *thread)
411 return get_thread_regcache (thread->inf->process_target (),
412 thread->ptid);
415 struct regcache *
416 get_current_regcache (void)
418 return get_thread_regcache (inferior_thread ());
421 /* See gdbsupport/common-regcache.h. */
423 struct regcache *
424 get_thread_regcache_for_ptid (ptid_t ptid)
426 /* This function doesn't take a process_stratum_target parameter
427 because it's a gdbsupport/ routine implemented by both gdb and
428 gdbserver. It always refers to a ptid of the current target. */
429 process_stratum_target *proc_target = current_inferior ()->process_target ();
430 return get_thread_regcache (proc_target, ptid);
433 /* Observer for the target_changed event. */
435 static void
436 regcache_observer_target_changed (struct target_ops *target)
438 registers_changed ();
441 /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
442 static void
443 regcache_thread_ptid_changed (process_stratum_target *target,
444 ptid_t old_ptid, ptid_t new_ptid)
446 /* Look up map for target. */
447 auto pid_ptid_regc_map_it = regcaches.find (target);
448 if (pid_ptid_regc_map_it == regcaches.end ())
449 return;
451 /* Look up map for pid. */
452 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
453 auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ());
454 if (ptid_regc_map_it == pid_ptid_regc_map.end ())
455 return;
457 /* Update all regcaches belonging to old_ptid. */
458 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
459 auto range = ptid_regc_map.equal_range (old_ptid);
460 for (auto it = range.first; it != range.second;)
462 regcache_up rc = std::move (it->second);
463 rc->set_ptid (new_ptid);
465 /* Remove old before inserting new, to avoid rehashing,
466 which would invalidate iterators. */
467 it = ptid_regc_map.erase (it);
468 ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
472 /* Low level examining and depositing of registers.
474 The caller is responsible for making sure that the inferior is
475 stopped before calling the fetching routines, or it will get
476 garbage. (a change from GDB version 3, in which the caller got the
477 value from the last stop). */
479 /* REGISTERS_CHANGED ()
481 Indicate that registers may have changed, so invalidate the cache. */
483 void
484 registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
486 if (target == nullptr)
488 /* Since there can be ptid clashes between targets, it's not valid to
489 pass a ptid without saying to which target it belongs. */
490 gdb_assert (ptid == minus_one_ptid);
492 /* Delete all the regcaches of all targets. */
493 regcaches.clear ();
495 else if (ptid.is_pid ())
497 /* Non-NULL target and pid ptid, delete all regcaches belonging
498 to this (TARGET, PID). */
500 /* Look up map for target. */
501 auto pid_ptid_regc_map_it = regcaches.find (target);
502 if (pid_ptid_regc_map_it != regcaches.end ())
504 pid_ptid_regcache_map &pid_ptid_regc_map
505 = pid_ptid_regc_map_it->second;
507 pid_ptid_regc_map.erase (ptid.pid ());
510 else if (ptid != minus_one_ptid)
512 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
513 to this (TARGET, PTID). */
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 /* Look up map for pid. */
523 auto ptid_regc_map_it
524 = pid_ptid_regc_map.find (ptid.pid ());
525 if (ptid_regc_map_it != pid_ptid_regc_map.end ())
527 ptid_regcache_map &ptid_regc_map
528 = ptid_regc_map_it->second;
530 ptid_regc_map.erase (ptid);
534 else
536 /* Non-NULL target and minus_one_ptid, delete all regcaches
537 associated to this target. */
538 regcaches.erase (target);
541 if ((target == nullptr || current_thread_target == target)
542 && current_thread_ptid.matches (ptid))
544 current_thread_target = NULL;
545 current_thread_ptid = null_ptid;
546 current_thread_arch = NULL;
549 if ((target == nullptr || current_inferior ()->process_target () == target)
550 && inferior_ptid.matches (ptid))
552 /* We just deleted the regcache of the current thread. Need to
553 forget about any frames we have cached, too. */
554 reinit_frame_cache ();
558 /* See regcache.h. */
560 void
561 registers_changed_thread (thread_info *thread)
563 registers_changed_ptid (thread->inf->process_target (), thread->ptid);
566 void
567 registers_changed (void)
569 registers_changed_ptid (nullptr, minus_one_ptid);
572 void
573 regcache::raw_update (int regnum)
575 assert_regnum (regnum);
577 /* Make certain that the register cache is up-to-date with respect
578 to the current thread. This switching shouldn't be necessary
579 only there is still only one target side register cache. Sigh!
580 On the bright side, at least there is a regcache object. */
582 if (get_register_status (regnum) == REG_UNKNOWN)
584 target_fetch_registers (this, regnum);
586 /* A number of targets can't access the whole set of raw
587 registers (because the debug API provides no means to get at
588 them). */
589 if (m_register_status[regnum] == REG_UNKNOWN)
590 m_register_status[regnum] = REG_UNAVAILABLE;
594 enum register_status
595 readable_regcache::raw_read (int regnum, gdb_byte *buf)
597 gdb_assert (buf != NULL);
598 raw_update (regnum);
600 if (m_register_status[regnum] != REG_VALID)
601 memset (buf, 0, m_descr->sizeof_register[regnum]);
602 else
603 memcpy (buf, register_buffer (regnum),
604 m_descr->sizeof_register[regnum]);
606 return m_register_status[regnum];
609 enum register_status
610 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
612 gdb_assert (regcache != NULL);
613 return regcache->raw_read (regnum, val);
616 template<typename T, typename>
617 enum register_status
618 readable_regcache::raw_read (int regnum, T *val)
620 gdb_byte *buf;
621 enum register_status status;
623 assert_regnum (regnum);
624 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
625 status = raw_read (regnum, buf);
626 if (status == REG_VALID)
627 *val = extract_integer<T> (buf,
628 m_descr->sizeof_register[regnum],
629 gdbarch_byte_order (m_descr->gdbarch));
630 else
631 *val = 0;
632 return status;
635 enum register_status
636 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
637 ULONGEST *val)
639 gdb_assert (regcache != NULL);
640 return regcache->raw_read (regnum, val);
643 void
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>
651 void
652 regcache::raw_write (int regnum, T val)
654 gdb_byte *buf;
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);
663 void
664 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
665 ULONGEST val)
667 gdb_assert (regcache != NULL);
668 regcache->raw_write (regnum, val);
671 LONGEST
672 regcache_raw_get_signed (struct regcache *regcache, int regnum)
674 LONGEST value;
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);
681 return value;
684 enum register_status
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]);
697 else
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,
710 this, regnum);
711 if (value_entirely_available (computed))
712 memcpy (buf, value_contents_raw (computed),
713 m_descr->sizeof_register[regnum]);
714 else
716 memset (buf, 0, m_descr->sizeof_register[regnum]);
717 result = REG_UNAVAILABLE;
720 value_free_to_mark (mark);
722 return result;
724 else
725 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
726 regnum, buf);
729 struct value *
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
747 API is preferred. */
748 if (cooked_read (regnum,
749 value_contents_raw (result)) == REG_UNAVAILABLE)
750 mark_value_bytes_unavailable (result, 0,
751 TYPE_LENGTH (value_type (result)));
753 return result;
755 else
756 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
757 this, regnum);
760 enum register_status
761 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
762 LONGEST *val)
764 gdb_assert (regcache != NULL);
765 return regcache->cooked_read (regnum, val);
768 template<typename T, typename>
769 enum register_status
770 readable_regcache::cooked_read (int regnum, T *val)
772 enum register_status status;
773 gdb_byte *buf;
775 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
776 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
777 status = cooked_read (regnum, buf);
778 if (status == REG_VALID)
779 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
780 gdbarch_byte_order (m_descr->gdbarch));
781 else
782 *val = 0;
783 return status;
786 enum register_status
787 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
788 ULONGEST *val)
790 gdb_assert (regcache != NULL);
791 return regcache->cooked_read (regnum, val);
794 void
795 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
796 LONGEST val)
798 gdb_assert (regcache != NULL);
799 regcache->cooked_write (regnum, val);
802 template<typename T, typename>
803 void
804 regcache::cooked_write (int regnum, T val)
806 gdb_byte *buf;
808 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
809 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
810 store_integer (buf, m_descr->sizeof_register[regnum],
811 gdbarch_byte_order (m_descr->gdbarch), val);
812 cooked_write (regnum, buf);
815 void
816 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
817 ULONGEST val)
819 gdb_assert (regcache != NULL);
820 regcache->cooked_write (regnum, val);
823 void
824 regcache::raw_write (int regnum, const gdb_byte *buf)
827 gdb_assert (buf != NULL);
828 assert_regnum (regnum);
830 /* On the sparc, writing %g0 is a no-op, so we don't even want to
831 change the registers array if something writes to this register. */
832 if (gdbarch_cannot_store_register (arch (), regnum))
833 return;
835 /* If we have a valid copy of the register, and new value == old
836 value, then don't bother doing the actual store. */
837 if (get_register_status (regnum) == REG_VALID
838 && (memcmp (register_buffer (regnum), buf,
839 m_descr->sizeof_register[regnum]) == 0))
840 return;
842 target_prepare_to_store (this);
843 raw_supply (regnum, buf);
845 /* Invalidate the register after it is written, in case of a
846 failure. */
847 auto invalidator
848 = make_scope_exit ([&] { this->invalidate (regnum); });
850 target_store_registers (this, regnum);
852 /* The target did not throw an error so we can discard invalidating
853 the register. */
854 invalidator.release ();
857 void
858 regcache::cooked_write (int regnum, const gdb_byte *buf)
860 gdb_assert (regnum >= 0);
861 gdb_assert (regnum < m_descr->nr_cooked_registers);
862 if (regnum < num_raw_registers ())
863 raw_write (regnum, buf);
864 else
865 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
866 regnum, buf);
869 /* See regcache.h. */
871 enum register_status
872 readable_regcache::read_part (int regnum, int offset, int len,
873 gdb_byte *out, bool is_raw)
875 int reg_size = register_size (arch (), regnum);
877 gdb_assert (out != NULL);
878 gdb_assert (offset >= 0 && offset <= reg_size);
879 gdb_assert (len >= 0 && offset + len <= reg_size);
881 if (offset == 0 && len == 0)
883 /* Nothing to do. */
884 return REG_VALID;
887 if (offset == 0 && len == reg_size)
889 /* Read the full register. */
890 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
893 enum register_status status;
894 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
896 /* Read full register to buffer. */
897 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
898 if (status != REG_VALID)
899 return status;
901 /* Copy out. */
902 memcpy (out, reg + offset, len);
903 return REG_VALID;
906 /* See regcache.h. */
908 void
909 reg_buffer::raw_collect_part (int regnum, int offset, int len,
910 gdb_byte *out) const
912 int reg_size = register_size (arch (), regnum);
914 gdb_assert (out != nullptr);
915 gdb_assert (offset >= 0 && offset <= reg_size);
916 gdb_assert (len >= 0 && offset + len <= reg_size);
918 if (offset == 0 && len == 0)
920 /* Nothing to do. */
921 return;
924 if (offset == 0 && len == reg_size)
926 /* Collect the full register. */
927 return raw_collect (regnum, out);
930 /* Read to buffer, then write out. */
931 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
932 raw_collect (regnum, reg);
933 memcpy (out, reg + offset, len);
936 /* See regcache.h. */
938 enum register_status
939 regcache::write_part (int regnum, int offset, int len,
940 const gdb_byte *in, bool is_raw)
942 int reg_size = register_size (arch (), regnum);
944 gdb_assert (in != NULL);
945 gdb_assert (offset >= 0 && offset <= reg_size);
946 gdb_assert (len >= 0 && offset + len <= reg_size);
948 if (offset == 0 && len == 0)
950 /* Nothing to do. */
951 return REG_VALID;
954 if (offset == 0 && len == reg_size)
956 /* Write the full register. */
957 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
958 return REG_VALID;
961 enum register_status status;
962 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
964 /* Read existing register to buffer. */
965 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
966 if (status != REG_VALID)
967 return status;
969 /* Update buffer, then write back to regcache. */
970 memcpy (reg + offset, in, len);
971 is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
972 return REG_VALID;
975 /* See regcache.h. */
977 void
978 reg_buffer::raw_supply_part (int regnum, int offset, int len,
979 const gdb_byte *in)
981 int reg_size = register_size (arch (), regnum);
983 gdb_assert (in != nullptr);
984 gdb_assert (offset >= 0 && offset <= reg_size);
985 gdb_assert (len >= 0 && offset + len <= reg_size);
987 if (offset == 0 && len == 0)
989 /* Nothing to do. */
990 return;
993 if (offset == 0 && len == reg_size)
995 /* Supply the full register. */
996 return raw_supply (regnum, in);
999 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
1001 /* Read existing value to buffer. */
1002 raw_collect (regnum, reg);
1004 /* Write to buffer, then write out. */
1005 memcpy (reg + offset, in, len);
1006 raw_supply (regnum, reg);
1009 enum register_status
1010 readable_regcache::raw_read_part (int regnum, int offset, int len,
1011 gdb_byte *buf)
1013 assert_regnum (regnum);
1014 return read_part (regnum, offset, len, buf, true);
1017 /* See regcache.h. */
1019 void
1020 regcache::raw_write_part (int regnum, int offset, int len,
1021 const gdb_byte *buf)
1023 assert_regnum (regnum);
1024 write_part (regnum, offset, len, buf, true);
1027 /* See regcache.h. */
1029 enum register_status
1030 readable_regcache::cooked_read_part (int regnum, int offset, int len,
1031 gdb_byte *buf)
1033 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1034 return read_part (regnum, offset, len, buf, false);
1037 /* See regcache.h. */
1039 void
1040 regcache::cooked_write_part (int regnum, int offset, int len,
1041 const gdb_byte *buf)
1043 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1044 write_part (regnum, offset, len, buf, false);
1047 /* See gdbsupport/common-regcache.h. */
1049 void
1050 reg_buffer::raw_supply (int regnum, const void *buf)
1052 void *regbuf;
1053 size_t size;
1055 assert_regnum (regnum);
1057 regbuf = register_buffer (regnum);
1058 size = m_descr->sizeof_register[regnum];
1060 if (buf)
1062 memcpy (regbuf, buf, size);
1063 m_register_status[regnum] = REG_VALID;
1065 else
1067 /* This memset not strictly necessary, but better than garbage
1068 in case the register value manages to escape somewhere (due
1069 to a bug, no less). */
1070 memset (regbuf, 0, size);
1071 m_register_status[regnum] = REG_UNAVAILABLE;
1075 /* See regcache.h. */
1077 void
1078 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1079 int addr_len, bool is_signed)
1081 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1082 gdb_byte *regbuf;
1083 size_t regsize;
1085 assert_regnum (regnum);
1087 regbuf = register_buffer (regnum);
1088 regsize = m_descr->sizeof_register[regnum];
1090 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1091 byte_order);
1092 m_register_status[regnum] = REG_VALID;
1095 /* See regcache.h. */
1097 void
1098 reg_buffer::raw_supply_zeroed (int regnum)
1100 void *regbuf;
1101 size_t size;
1103 assert_regnum (regnum);
1105 regbuf = register_buffer (regnum);
1106 size = m_descr->sizeof_register[regnum];
1108 memset (regbuf, 0, size);
1109 m_register_status[regnum] = REG_VALID;
1112 /* See gdbsupport/common-regcache.h. */
1114 void
1115 reg_buffer::raw_collect (int regnum, void *buf) const
1117 const void *regbuf;
1118 size_t size;
1120 gdb_assert (buf != NULL);
1121 assert_regnum (regnum);
1123 regbuf = register_buffer (regnum);
1124 size = m_descr->sizeof_register[regnum];
1125 memcpy (buf, regbuf, size);
1128 /* See regcache.h. */
1130 void
1131 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1132 bool is_signed) const
1134 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1135 const gdb_byte *regbuf;
1136 size_t regsize;
1138 assert_regnum (regnum);
1140 regbuf = register_buffer (regnum);
1141 regsize = m_descr->sizeof_register[regnum];
1143 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1144 byte_order);
1147 /* See regcache.h. */
1149 void
1150 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1151 const gdb_byte *in_buf, gdb_byte *out_buf,
1152 int slot_size, int offs) const
1154 struct gdbarch *gdbarch = arch ();
1155 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1157 /* Use part versions and reg_size to prevent possible buffer overflows when
1158 accessing the regcache. */
1160 if (out_buf != nullptr)
1162 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1164 /* Ensure any additional space is cleared. */
1165 if (slot_size > reg_size)
1166 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1168 else if (in_buf != nullptr)
1169 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1170 else
1172 /* Invalidate the register. */
1173 out_regcache->raw_supply (regnum, nullptr);
1177 /* See regcache.h. */
1179 void
1180 regcache::transfer_regset (const struct regset *regset,
1181 struct regcache *out_regcache,
1182 int regnum, const gdb_byte *in_buf,
1183 gdb_byte *out_buf, size_t size) const
1185 const struct regcache_map_entry *map;
1186 int offs = 0, count;
1188 for (map = (const struct regcache_map_entry *) regset->regmap;
1189 (count = map->count) != 0;
1190 map++)
1192 int regno = map->regno;
1193 int slot_size = map->size;
1195 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1196 slot_size = m_descr->sizeof_register[regno];
1198 if (regno == REGCACHE_MAP_SKIP
1199 || (regnum != -1
1200 && (regnum < regno || regnum >= regno + count)))
1201 offs += count * slot_size;
1203 else if (regnum == -1)
1204 for (; count--; regno++, offs += slot_size)
1206 if (offs + slot_size > size)
1207 break;
1209 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1210 slot_size, offs);
1212 else
1214 /* Transfer a single register and return. */
1215 offs += (regnum - regno) * slot_size;
1216 if (offs + slot_size > size)
1217 return;
1219 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1220 slot_size, offs);
1221 return;
1226 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1227 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1228 If BUF is NULL, set the register(s) to "unavailable" status. */
1230 void
1231 regcache_supply_regset (const struct regset *regset,
1232 struct regcache *regcache,
1233 int regnum, const void *buf, size_t size)
1235 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1238 void
1239 regcache::supply_regset (const struct regset *regset,
1240 int regnum, const void *buf, size_t size)
1242 transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
1245 /* Collect register REGNUM from REGCACHE to BUF, using the register
1246 map in REGSET. If REGNUM is -1, do this for all registers in
1247 REGSET. */
1249 void
1250 regcache_collect_regset (const struct regset *regset,
1251 const struct regcache *regcache,
1252 int regnum, void *buf, size_t size)
1254 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1257 void
1258 regcache::collect_regset (const struct regset *regset,
1259 int regnum, void *buf, size_t size) const
1261 transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1264 /* See gdbsupport/common-regcache.h. */
1266 bool
1267 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1269 gdb_assert (buf != NULL);
1270 assert_regnum (regnum);
1272 const char *regbuf = (const char *) register_buffer (regnum);
1273 size_t size = m_descr->sizeof_register[regnum];
1274 gdb_assert (size >= offset);
1276 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1279 /* Special handling for register PC. */
1281 CORE_ADDR
1282 regcache_read_pc (struct regcache *regcache)
1284 struct gdbarch *gdbarch = regcache->arch ();
1286 CORE_ADDR pc_val;
1288 if (gdbarch_read_pc_p (gdbarch))
1289 pc_val = gdbarch_read_pc (gdbarch, regcache);
1290 /* Else use per-frame method on get_current_frame. */
1291 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1293 ULONGEST raw_val;
1295 if (regcache_cooked_read_unsigned (regcache,
1296 gdbarch_pc_regnum (gdbarch),
1297 &raw_val) == REG_UNAVAILABLE)
1298 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1300 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1302 else
1303 internal_error (__FILE__, __LINE__,
1304 _("regcache_read_pc: Unable to find PC"));
1305 return pc_val;
1308 /* See gdbsupport/common-regcache.h. */
1310 CORE_ADDR
1311 regcache_read_pc_protected (regcache *regcache)
1313 CORE_ADDR pc;
1316 pc = regcache_read_pc (regcache);
1318 catch (const gdb_exception_error &ex)
1320 pc = 0;
1323 return pc;
1326 void
1327 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1329 struct gdbarch *gdbarch = regcache->arch ();
1331 if (gdbarch_write_pc_p (gdbarch))
1332 gdbarch_write_pc (gdbarch, regcache, pc);
1333 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1334 regcache_cooked_write_unsigned (regcache,
1335 gdbarch_pc_regnum (gdbarch), pc);
1336 else
1337 internal_error (__FILE__, __LINE__,
1338 _("regcache_write_pc: Unable to update PC"));
1340 /* Writing the PC (for instance, from "load") invalidates the
1341 current frame. */
1342 reinit_frame_cache ();
1346 reg_buffer::num_raw_registers () const
1348 return gdbarch_num_regs (arch ());
1351 void
1352 regcache::debug_print_register (const char *func, int regno)
1354 struct gdbarch *gdbarch = arch ();
1356 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1357 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1358 && gdbarch_register_name (gdbarch, regno) != NULL
1359 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1360 fprintf_unfiltered (gdb_stdlog, "(%s)",
1361 gdbarch_register_name (gdbarch, regno));
1362 else
1363 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1364 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1366 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1367 int size = register_size (gdbarch, regno);
1368 gdb_byte *buf = register_buffer (regno);
1370 fprintf_unfiltered (gdb_stdlog, " = ");
1371 for (int i = 0; i < size; i++)
1373 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1375 if (size <= sizeof (LONGEST))
1377 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1379 fprintf_unfiltered (gdb_stdlog, " %s %s",
1380 core_addr_to_string_nz (val), plongest (val));
1383 fprintf_unfiltered (gdb_stdlog, "\n");
1386 /* Implement 'maint flush register-cache' command. */
1388 static void
1389 reg_flush_command (const char *command, int from_tty)
1391 /* Force-flush the register cache. */
1392 registers_changed ();
1393 if (from_tty)
1394 printf_filtered (_("Register cache flushed.\n"));
1397 void
1398 register_dump::dump (ui_file *file)
1400 auto descr = regcache_descr (m_gdbarch);
1401 int regnum;
1402 int footnote_nr = 0;
1403 int footnote_register_offset = 0;
1404 int footnote_register_type_name_null = 0;
1405 long register_offset = 0;
1407 gdb_assert (descr->nr_cooked_registers
1408 == gdbarch_num_cooked_regs (m_gdbarch));
1410 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1412 /* Name. */
1413 if (regnum < 0)
1414 fprintf_unfiltered (file, " %-10s", "Name");
1415 else
1417 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1419 if (p == NULL)
1420 p = "";
1421 else if (p[0] == '\0')
1422 p = "''";
1423 fprintf_unfiltered (file, " %-10s", p);
1426 /* Number. */
1427 if (regnum < 0)
1428 fprintf_unfiltered (file, " %4s", "Nr");
1429 else
1430 fprintf_unfiltered (file, " %4d", regnum);
1432 /* Relative number. */
1433 if (regnum < 0)
1434 fprintf_unfiltered (file, " %4s", "Rel");
1435 else if (regnum < gdbarch_num_regs (m_gdbarch))
1436 fprintf_unfiltered (file, " %4d", regnum);
1437 else
1438 fprintf_unfiltered (file, " %4d",
1439 (regnum - gdbarch_num_regs (m_gdbarch)));
1441 /* Offset. */
1442 if (regnum < 0)
1443 fprintf_unfiltered (file, " %6s ", "Offset");
1444 else
1446 fprintf_unfiltered (file, " %6ld",
1447 descr->register_offset[regnum]);
1448 if (register_offset != descr->register_offset[regnum]
1449 || (regnum > 0
1450 && (descr->register_offset[regnum]
1451 != (descr->register_offset[regnum - 1]
1452 + descr->sizeof_register[regnum - 1])))
1455 if (!footnote_register_offset)
1456 footnote_register_offset = ++footnote_nr;
1457 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1459 else
1460 fprintf_unfiltered (file, " ");
1461 register_offset = (descr->register_offset[regnum]
1462 + descr->sizeof_register[regnum]);
1465 /* Size. */
1466 if (regnum < 0)
1467 fprintf_unfiltered (file, " %5s ", "Size");
1468 else
1469 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1471 /* Type. */
1473 const char *t;
1474 std::string name_holder;
1476 if (regnum < 0)
1477 t = "Type";
1478 else
1480 static const char blt[] = "builtin_type";
1482 t = register_type (m_gdbarch, regnum)->name ();
1483 if (t == NULL)
1485 if (!footnote_register_type_name_null)
1486 footnote_register_type_name_null = ++footnote_nr;
1487 name_holder = string_printf ("*%d",
1488 footnote_register_type_name_null);
1489 t = name_holder.c_str ();
1491 /* Chop a leading builtin_type. */
1492 if (startswith (t, blt))
1493 t += strlen (blt);
1495 fprintf_unfiltered (file, " %-15s", t);
1498 /* Leading space always present. */
1499 fprintf_unfiltered (file, " ");
1501 dump_reg (file, regnum);
1503 fprintf_unfiltered (file, "\n");
1506 if (footnote_register_offset)
1507 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1508 footnote_register_offset);
1509 if (footnote_register_type_name_null)
1510 fprintf_unfiltered (file,
1511 "*%d: Register type's name NULL.\n",
1512 footnote_register_type_name_null);
1515 #if GDB_SELF_TEST
1516 #include "gdbsupport/selftest.h"
1517 #include "selftest-arch.h"
1518 #include "target-float.h"
1520 namespace selftests {
1522 static size_t
1523 regcaches_size ()
1525 size_t size = 0;
1527 for (auto pid_ptid_regc_map_it = regcaches.cbegin ();
1528 pid_ptid_regc_map_it != regcaches.cend ();
1529 ++pid_ptid_regc_map_it)
1531 const pid_ptid_regcache_map &pid_ptid_regc_map
1532 = pid_ptid_regc_map_it->second;
1534 for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin ();
1535 ptid_regc_map_it != pid_ptid_regc_map.cend ();
1536 ++ptid_regc_map_it)
1538 const ptid_regcache_map &ptid_regc_map
1539 = ptid_regc_map_it->second;
1541 size += ptid_regc_map.size ();
1545 return size;
1548 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1550 static int
1551 regcache_count (process_stratum_target *target, ptid_t ptid)
1553 /* Look up map for target. */
1554 auto pid_ptid_regc_map_it = regcaches.find (target);
1555 if (pid_ptid_regc_map_it != regcaches.end ())
1557 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
1559 /* Look map for pid. */
1560 auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ());
1561 if (ptid_regc_map_it != pid_ptid_regc_map.end ())
1563 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
1564 auto range = ptid_regc_map.equal_range (ptid);
1566 return std::distance (range.first, range.second);
1570 return 0;
1573 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1575 static void
1576 get_thread_arch_aspace_regcache_and_check (process_stratum_target *target,
1577 ptid_t ptid)
1579 /* We currently only test with a single gdbarch. Any gdbarch will do, so use
1580 the current inferior's gdbarch. Also use the current inferior's address
1581 space. */
1582 gdbarch *arch = current_inferior ()->gdbarch;
1583 address_space *aspace = current_inferior ()->aspace;
1584 regcache *regcache
1585 = get_thread_arch_aspace_regcache (target, ptid, arch, aspace);
1587 SELF_CHECK (regcache != NULL);
1588 SELF_CHECK (regcache->target () == target);
1589 SELF_CHECK (regcache->ptid () == ptid);
1590 SELF_CHECK (regcache->arch () == arch);
1591 SELF_CHECK (regcache->aspace () == aspace);
1594 /* The data that the regcaches selftests must hold onto for the duration of the
1595 test. */
1597 struct regcache_test_data
1599 regcache_test_data ()
1601 /* Ensure the regcaches container is empty at the start. */
1602 registers_changed ();
1605 ~regcache_test_data ()
1607 /* Make sure to leave the global regcaches container empty. */
1608 registers_changed ();
1611 test_target_ops test_target1;
1612 test_target_ops test_target2;
1615 using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
1617 /* Set up a few regcaches from two different targets, for use in
1618 regcache-management tests.
1620 Return a pointer, because the `regcache_test_data` type is not moveable. */
1622 static regcache_test_data_up
1623 populate_regcaches_for_test ()
1625 regcache_test_data_up data (new regcache_test_data);
1626 size_t expected_regcache_size = 0;
1628 SELF_CHECK (regcaches_size () == 0);
1630 /* Populate the regcache container with a few regcaches for the two test
1631 targets. */
1632 for (int pid : { 1, 2 })
1634 for (long lwp : { 1, 2, 3 })
1636 get_thread_arch_aspace_regcache_and_check
1637 (&data->test_target1, ptid_t (pid, lwp));
1638 expected_regcache_size++;
1639 SELF_CHECK (regcaches_size () == expected_regcache_size);
1641 get_thread_arch_aspace_regcache_and_check
1642 (&data->test_target2, ptid_t (pid, lwp));
1643 expected_regcache_size++;
1644 SELF_CHECK (regcaches_size () == expected_regcache_size);
1648 return data;
1651 static void
1652 get_thread_arch_aspace_regcache_test ()
1654 /* populate_regcaches_for_test already tests most of the
1655 get_thread_arch_aspace_regcache functionality. */
1656 regcache_test_data_up data = populate_regcaches_for_test ();
1657 size_t regcaches_size_before = regcaches_size ();
1659 /* Test that getting an existing regcache doesn't create a new one. */
1660 get_thread_arch_aspace_regcache_and_check (&data->test_target1, ptid_t (2, 2));
1661 SELF_CHECK (regcaches_size () == regcaches_size_before);
1664 /* Test marking all regcaches of all targets as changed. */
1666 static void
1667 registers_changed_ptid_all_test ()
1669 regcache_test_data_up data = populate_regcaches_for_test ();
1671 registers_changed_ptid (nullptr, minus_one_ptid);
1672 SELF_CHECK (regcaches_size () == 0);
1675 /* Test marking regcaches of a specific target as changed. */
1677 static void
1678 registers_changed_ptid_target_test ()
1680 regcache_test_data_up data = populate_regcaches_for_test ();
1682 registers_changed_ptid (&data->test_target1, minus_one_ptid);
1683 SELF_CHECK (regcaches_size () == 6);
1685 /* Check that we deleted the regcache for the right target. */
1686 SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1687 SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1690 /* Test marking regcaches of a specific (target, pid) as changed. */
1692 static void
1693 registers_changed_ptid_target_pid_test ()
1695 regcache_test_data_up data = populate_regcaches_for_test ();
1697 registers_changed_ptid (&data->test_target1, ptid_t (2));
1698 SELF_CHECK (regcaches_size () == 9);
1700 /* Regcaches from target1 should not exist, while regcaches from target2
1701 should exist. */
1702 SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1703 SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1706 /* Test marking regcaches of a specific (target, ptid) as changed. */
1708 static void
1709 registers_changed_ptid_target_ptid_test ()
1711 regcache_test_data_up data = populate_regcaches_for_test ();
1713 registers_changed_ptid (&data->test_target1, ptid_t (2, 2));
1714 SELF_CHECK (regcaches_size () == 11);
1716 /* Check that we deleted the regcache for the right target. */
1717 SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1718 SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1721 class target_ops_no_register : public test_target_ops
1723 public:
1724 target_ops_no_register ()
1725 : test_target_ops {}
1728 void reset ()
1730 fetch_registers_called = 0;
1731 store_registers_called = 0;
1732 xfer_partial_called = 0;
1735 void fetch_registers (regcache *regs, int regno) override;
1736 void store_registers (regcache *regs, int regno) override;
1738 enum target_xfer_status xfer_partial (enum target_object object,
1739 const char *annex, gdb_byte *readbuf,
1740 const gdb_byte *writebuf,
1741 ULONGEST offset, ULONGEST len,
1742 ULONGEST *xfered_len) override;
1744 unsigned int fetch_registers_called = 0;
1745 unsigned int store_registers_called = 0;
1746 unsigned int xfer_partial_called = 0;
1749 void
1750 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1752 /* Mark register available. */
1753 regs->raw_supply_zeroed (regno);
1754 this->fetch_registers_called++;
1757 void
1758 target_ops_no_register::store_registers (regcache *regs, int regno)
1760 this->store_registers_called++;
1763 enum target_xfer_status
1764 target_ops_no_register::xfer_partial (enum target_object object,
1765 const char *annex, gdb_byte *readbuf,
1766 const gdb_byte *writebuf,
1767 ULONGEST offset, ULONGEST len,
1768 ULONGEST *xfered_len)
1770 this->xfer_partial_called++;
1772 *xfered_len = len;
1773 return TARGET_XFER_OK;
1776 class readwrite_regcache : public regcache
1778 public:
1779 readwrite_regcache (process_stratum_target *target,
1780 struct gdbarch *gdbarch)
1781 : regcache (target, gdbarch, nullptr)
1785 /* Test regcache::cooked_read gets registers from raw registers and
1786 memory instead of target to_{fetch,store}_registers. */
1788 static void
1789 cooked_read_test (struct gdbarch *gdbarch)
1791 scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1793 /* Test that read one raw register from regcache_no_target will go
1794 to the target layer. */
1796 /* Find a raw register which size isn't zero. */
1797 int nonzero_regnum;
1798 for (nonzero_regnum = 0;
1799 nonzero_regnum < gdbarch_num_regs (gdbarch);
1800 nonzero_regnum++)
1802 if (register_size (gdbarch, nonzero_regnum) != 0)
1803 break;
1806 readwrite_regcache readwrite (&mockctx.mock_target, gdbarch);
1807 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1809 readwrite.raw_read (nonzero_regnum, buf.data ());
1811 /* raw_read calls target_fetch_registers. */
1812 SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1813 mockctx.mock_target.reset ();
1815 /* Mark all raw registers valid, so the following raw registers
1816 accesses won't go to target. */
1817 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1818 readwrite.raw_update (i);
1820 mockctx.mock_target.reset ();
1821 /* Then, read all raw and pseudo registers, and don't expect calling
1822 to_{fetch,store}_registers. */
1823 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1825 if (register_size (gdbarch, regnum) == 0)
1826 continue;
1828 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1830 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1831 inner_buf.data ()));
1833 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1834 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1835 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1837 mockctx.mock_target.reset ();
1840 readonly_detached_regcache readonly (readwrite);
1842 /* GDB may go to target layer to fetch all registers and memory for
1843 readonly regcache. */
1844 mockctx.mock_target.reset ();
1846 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1848 if (register_size (gdbarch, regnum) == 0)
1849 continue;
1851 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1852 enum register_status status = readonly.cooked_read (regnum,
1853 inner_buf.data ());
1855 if (regnum < gdbarch_num_regs (gdbarch))
1857 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1859 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1860 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1861 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1862 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1863 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1864 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1865 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1866 || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
1868 /* Raw registers. If raw registers are not in save_reggroup,
1869 their status are unknown. */
1870 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1871 SELF_CHECK (status == REG_VALID);
1872 else
1873 SELF_CHECK (status == REG_UNKNOWN);
1875 else
1876 SELF_CHECK (status == REG_VALID);
1878 else
1880 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1881 SELF_CHECK (status == REG_VALID);
1882 else
1884 /* If pseudo registers are not in save_reggroup, some of
1885 them can be computed from saved raw registers, but some
1886 of them are unknown. */
1887 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1889 if (bfd_arch == bfd_arch_frv
1890 || bfd_arch == bfd_arch_m32c
1891 || bfd_arch == bfd_arch_mep
1892 || bfd_arch == bfd_arch_sh)
1893 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1894 else if (bfd_arch == bfd_arch_mips
1895 || bfd_arch == bfd_arch_h8300)
1896 SELF_CHECK (status == REG_UNKNOWN);
1897 else
1898 SELF_CHECK (status == REG_VALID);
1902 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1903 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1904 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1906 mockctx.mock_target.reset ();
1910 /* Test regcache::cooked_write by writing some expected contents to
1911 registers, and checking that contents read from registers and the
1912 expected contents are the same. */
1914 static void
1915 cooked_write_test (struct gdbarch *gdbarch)
1917 /* Error out if debugging something, because we're going to push the
1918 test target, which would pop any existing target. */
1919 if (current_inferior ()->top_target ()->stratum () >= process_stratum)
1920 error (_("target already pushed"));
1922 /* Create a mock environment. A process_stratum target pushed. */
1924 target_ops_no_register mock_target;
1926 /* Push the process_stratum target so we can mock accessing
1927 registers. */
1928 current_inferior ()->push_target (&mock_target);
1930 /* Pop it again on exit (return/exception). */
1931 struct on_exit
1933 ~on_exit ()
1935 pop_all_targets_at_and_above (process_stratum);
1937 } pop_targets;
1939 readwrite_regcache readwrite (&mock_target, gdbarch);
1941 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1943 for (auto regnum = 0; regnum < num_regs; regnum++)
1945 if (register_size (gdbarch, regnum) == 0
1946 || gdbarch_cannot_store_register (gdbarch, regnum))
1947 continue;
1949 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1951 if (bfd_arch == bfd_arch_sparc
1952 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1953 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1954 && gdbarch_ptr_bit (gdbarch) == 64
1955 && (regnum >= gdbarch_num_regs (gdbarch)
1956 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1957 continue;
1959 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1960 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1961 const auto type = register_type (gdbarch, regnum);
1963 if (type->code () == TYPE_CODE_FLT
1964 || type->code () == TYPE_CODE_DECFLOAT)
1966 /* Generate valid float format. */
1967 target_float_from_string (expected.data (), type, "1.25");
1969 else if (type->code () == TYPE_CODE_INT
1970 || type->code () == TYPE_CODE_ARRAY
1971 || type->code () == TYPE_CODE_PTR
1972 || type->code () == TYPE_CODE_UNION
1973 || type->code () == TYPE_CODE_STRUCT)
1975 if (bfd_arch == bfd_arch_ia64
1976 || (regnum >= gdbarch_num_regs (gdbarch)
1977 && (bfd_arch == bfd_arch_xtensa
1978 || bfd_arch == bfd_arch_bfin
1979 || bfd_arch == bfd_arch_m32c
1980 /* m68hc11 pseudo registers are in memory. */
1981 || bfd_arch == bfd_arch_m68hc11
1982 || bfd_arch == bfd_arch_m68hc12
1983 || bfd_arch == bfd_arch_s390))
1984 || (bfd_arch == bfd_arch_frv
1985 /* FRV pseudo registers except iacc0. */
1986 && regnum > gdbarch_num_regs (gdbarch)))
1988 /* Skip setting the expected values for some architecture
1989 registers. */
1991 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1993 /* RL78_PC_REGNUM */
1994 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1995 expected[j] = j;
1997 else
1999 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
2000 expected[j] = j;
2003 else if (type->code () == TYPE_CODE_FLAGS)
2005 /* No idea how to test flags. */
2006 continue;
2008 else
2010 /* If we don't know how to create the expected value for the
2011 this type, make it fail. */
2012 SELF_CHECK (0);
2015 readwrite.cooked_write (regnum, expected.data ());
2017 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2018 SELF_CHECK (expected == buf);
2022 /* Verify that when two threads with the same ptid exist (from two different
2023 targets) and one of them changes ptid, we only update the appropriate
2024 regcaches. */
2026 static void
2027 regcache_thread_ptid_changed ()
2029 /* This test relies on the global regcache list to initially be empty. */
2030 registers_changed ();
2032 /* Any arch will do. */
2033 gdbarch *arch = current_inferior ()->gdbarch;
2035 /* Prepare two targets with one thread each, with the same ptid. */
2036 scoped_mock_context<test_target_ops> target1 (arch);
2037 scoped_mock_context<test_target_ops> target2 (arch);
2038 target2.mock_inferior.next = &target1.mock_inferior;
2040 ptid_t old_ptid (111, 222);
2041 ptid_t new_ptid (111, 333);
2043 target1.mock_inferior.pid = old_ptid.pid ();
2044 target1.mock_thread.ptid = old_ptid;
2045 target2.mock_inferior.pid = old_ptid.pid ();
2046 target2.mock_thread.ptid = old_ptid;
2048 gdb_assert (regcaches.empty ());
2050 /* Populate the regcaches container. */
2051 get_thread_arch_aspace_regcache (&target1.mock_target, old_ptid, arch,
2052 nullptr);
2053 get_thread_arch_aspace_regcache (&target2.mock_target, old_ptid, arch,
2054 nullptr);
2056 gdb_assert (regcaches.size () == 2);
2057 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
2058 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
2059 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2060 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2062 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
2064 gdb_assert (regcaches.size () == 2);
2065 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
2066 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
2067 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2068 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2070 /* Leave the regcache list empty. */
2071 registers_changed ();
2072 gdb_assert (regcaches.empty ());
2075 } // namespace selftests
2076 #endif /* GDB_SELF_TEST */
2078 void _initialize_regcache ();
2079 void
2080 _initialize_regcache ()
2082 struct cmd_list_element *c;
2084 regcache_descr_handle
2085 = gdbarch_data_register_post_init (init_regcache_descr);
2087 gdb::observers::target_changed.attach (regcache_observer_target_changed,
2088 "regcache");
2089 gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed,
2090 "regcache");
2092 add_cmd ("register-cache", class_maintenance, reg_flush_command,
2093 _("Force gdb to flush its register and frame cache."),
2094 &maintenanceflushlist);
2095 c = add_com_alias ("flushregs", "maintenance flush register-cache",
2096 class_maintenance, 0);
2097 deprecate_cmd (c, "maintenance flush register-cache");
2099 #if GDB_SELF_TEST
2100 selftests::register_test ("get_thread_arch_aspace_regcache",
2101 selftests::get_thread_arch_aspace_regcache_test);
2102 selftests::register_test ("registers_changed_ptid_all",
2103 selftests::registers_changed_ptid_all_test);
2104 selftests::register_test ("registers_changed_ptid_target",
2105 selftests::registers_changed_ptid_target_test);
2106 selftests::register_test ("registers_changed_ptid_target_pid",
2107 selftests::registers_changed_ptid_target_pid_test);
2108 selftests::register_test ("registers_changed_ptid_target_ptid",
2109 selftests::registers_changed_ptid_target_ptid_test);
2111 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2112 selftests::cooked_read_test);
2113 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2114 selftests::cooked_write_test);
2115 selftests::register_test ("regcache_thread_ptid_changed",
2116 selftests::regcache_thread_ptid_changed);
2117 #endif