1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2018 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/>. */
26 #include "reggroups.h"
31 #include <forward_list>
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created. */
42 struct gdbarch_data *regcache_descr_handle;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
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
54 long sizeof_raw_registers;
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;
63 long sizeof_cooked_registers;
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
69 long *register_offset;
70 long *sizeof_register;
72 /* Cached table containing the type of each register. */
73 struct type **register_type;
77 init_regcache_descr (struct gdbarch *gdbarch)
80 struct regcache_descr *descr;
81 gdb_assert (gdbarch != NULL);
83 /* Create an initial, zero filled, table. */
84 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
85 descr->gdbarch = gdbarch;
87 /* Total size of the register space. The raw registers are mapped
88 directly onto the raw register cache while the pseudo's are
89 either mapped onto raw-registers or memory. */
90 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
91 + gdbarch_num_pseudo_regs (gdbarch);
93 /* Fill in a table of register types. */
95 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
97 for (i = 0; i < descr->nr_cooked_registers; i++)
98 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
100 /* Construct a strictly RAW register cache. Don't allow pseudo's
101 into the register cache. */
103 /* Lay out the register cache.
105 NOTE: cagney/2002-05-22: Only register_type() is used when
106 constructing the register cache. It is assumed that the
107 register's raw size, virtual size and type length are all the
113 descr->sizeof_register
114 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
115 descr->register_offset
116 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
117 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
119 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
120 descr->register_offset[i] = offset;
121 offset += descr->sizeof_register[i];
122 gdb_assert (MAX_REGISTER_SIZE >= 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];
132 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
134 /* Set the real size of the readonly register cache buffer. */
135 descr->sizeof_cooked_registers = offset;
141 static struct regcache_descr *
142 regcache_descr (struct gdbarch *gdbarch)
144 return (struct regcache_descr *) gdbarch_data (gdbarch,
145 regcache_descr_handle);
148 /* Utility functions returning useful register attributes stored in
149 the regcache descr. */
152 register_type (struct gdbarch *gdbarch, int regnum)
154 struct regcache_descr *descr = regcache_descr (gdbarch);
156 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
157 return descr->register_type[regnum];
160 /* Utility functions returning useful register attributes stored in
161 the regcache descr. */
164 register_size (struct gdbarch *gdbarch, int regnum)
166 struct regcache_descr *descr = regcache_descr (gdbarch);
169 gdb_assert (regnum >= 0
170 && regnum < (gdbarch_num_regs (gdbarch)
171 + gdbarch_num_pseudo_regs (gdbarch)));
172 size = descr->sizeof_register[regnum];
176 /* See common/common-regcache.h. */
179 regcache_register_size (const struct regcache *regcache, int n)
181 return register_size (regcache->arch (), n);
184 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
185 : m_has_pseudo (has_pseudo)
187 gdb_assert (gdbarch != NULL);
188 m_descr = regcache_descr (gdbarch);
192 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
193 m_register_status = XCNEWVEC (signed char,
194 m_descr->nr_cooked_registers);
198 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
199 m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch));
203 regcache::regcache (gdbarch *gdbarch, const address_space *aspace_,
205 /* The register buffers. A read-only register cache can hold the
206 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a
207 read/write register cache can only hold [0 .. gdbarch_num_regs). */
208 : detached_regcache (gdbarch, readonly_p_),
209 m_aspace (aspace_), m_readonly_p (readonly_p_)
211 m_ptid = minus_one_ptid;
214 static enum register_status
215 do_cooked_read (void *src, int regnum, gdb_byte *buf)
217 struct regcache *regcache = (struct regcache *) src;
219 return regcache_cooked_read (regcache, regnum, buf);
222 readonly_detached_regcache::readonly_detached_regcache (const regcache &src)
223 : readonly_detached_regcache (src.arch (), do_cooked_read, (void *) &src)
228 reg_buffer::arch () const
230 return m_descr->gdbarch;
233 /* See regcache.h. */
236 regcache_get_ptid (const struct regcache *regcache)
238 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
240 return regcache->ptid ();
243 /* Cleanup class for invalidating a register. */
245 class regcache_invalidator
249 regcache_invalidator (struct regcache *regcache, int regnum)
250 : m_regcache (regcache),
255 ~regcache_invalidator ()
257 if (m_regcache != nullptr)
258 regcache_invalidate (m_regcache, m_regnum);
261 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
265 m_regcache = nullptr;
270 struct regcache *m_regcache;
274 /* Return a pointer to register REGNUM's buffer cache. */
277 reg_buffer::register_buffer (int regnum) const
279 return m_registers + m_descr->register_offset[regnum];
283 reg_buffer::save (regcache_cooked_read_ftype *cooked_read,
286 struct gdbarch *gdbarch = m_descr->gdbarch;
289 /* It should have pseudo registers. */
290 gdb_assert (m_has_pseudo);
291 /* Clear the dest. */
292 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
293 memset (m_register_status, 0, m_descr->nr_cooked_registers);
294 /* Copy over any registers (identified by their membership in the
295 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
296 gdbarch_num_pseudo_regs) range is checked since some architectures need
297 to save/restore `cooked' registers that live in memory. */
298 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
300 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
302 gdb_byte *dst_buf = register_buffer (regnum);
303 enum register_status status = cooked_read (src, regnum, dst_buf);
305 gdb_assert (status != REG_UNKNOWN);
307 if (status != REG_VALID)
308 memset (dst_buf, 0, register_size (gdbarch, regnum));
310 m_register_status[regnum] = status;
316 regcache::restore (readonly_detached_regcache *src)
318 struct gdbarch *gdbarch = m_descr->gdbarch;
321 gdb_assert (src != NULL);
322 gdb_assert (!m_readonly_p);
323 gdb_assert (src->m_has_pseudo);
325 gdb_assert (gdbarch == src->arch ());
327 /* Copy over any registers, being careful to only restore those that
328 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
329 + gdbarch_num_pseudo_regs) range is checked since some architectures need
330 to save/restore `cooked' registers that live in memory. */
331 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
333 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
335 if (src->m_register_status[regnum] == REG_VALID)
336 cooked_write (regnum, src->register_buffer (regnum));
342 regcache_register_status (const struct regcache *regcache, int regnum)
344 gdb_assert (regcache != NULL);
345 return regcache->get_register_status (regnum);
349 reg_buffer::get_register_status (int regnum) const
351 assert_regnum (regnum);
353 return (enum register_status) m_register_status[regnum];
357 regcache_invalidate (struct regcache *regcache, int regnum)
359 gdb_assert (regcache != NULL);
360 regcache->invalidate (regnum);
364 regcache::invalidate (int regnum)
366 gdb_assert (!m_readonly_p);
367 assert_regnum (regnum);
368 m_register_status[regnum] = REG_UNKNOWN;
372 reg_buffer::assert_regnum (int regnum) const
374 gdb_assert (regnum >= 0);
376 gdb_assert (regnum < m_descr->nr_cooked_registers);
378 gdb_assert (regnum < gdbarch_num_regs (arch ()));
381 /* Global structure containing the current regcache. */
383 /* NOTE: this is a write-through cache. There is no "dirty" bit for
384 recording if the register values have been changed (eg. by the
385 user). Therefore all registers must be written back to the
386 target when appropriate. */
387 std::forward_list<regcache *> regcache::current_regcache;
390 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
391 struct address_space *aspace)
393 for (const auto ®cache : regcache::current_regcache)
394 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
397 regcache *new_regcache = new regcache (gdbarch, aspace, false);
399 regcache::current_regcache.push_front (new_regcache);
400 new_regcache->set_ptid (ptid);
406 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
408 address_space *aspace = target_thread_address_space (ptid);
410 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
413 static ptid_t current_thread_ptid;
414 static struct gdbarch *current_thread_arch;
417 get_thread_regcache (ptid_t ptid)
419 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
421 current_thread_ptid = ptid;
422 current_thread_arch = target_thread_architecture (ptid);
425 return get_thread_arch_regcache (ptid, current_thread_arch);
429 get_current_regcache (void)
431 return get_thread_regcache (inferior_ptid);
434 /* See common/common-regcache.h. */
437 get_thread_regcache_for_ptid (ptid_t ptid)
439 return get_thread_regcache (ptid);
442 /* Observer for the target_changed event. */
445 regcache_observer_target_changed (struct target_ops *target)
447 registers_changed ();
450 /* Update global variables old ptids to hold NEW_PTID if they were
453 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
455 for (auto ®cache : regcache::current_regcache)
457 if (ptid_equal (regcache->ptid (), old_ptid))
458 regcache->set_ptid (new_ptid);
462 /* Low level examining and depositing of registers.
464 The caller is responsible for making sure that the inferior is
465 stopped before calling the fetching routines, or it will get
466 garbage. (a change from GDB version 3, in which the caller got the
467 value from the last stop). */
469 /* REGISTERS_CHANGED ()
471 Indicate that registers may have changed, so invalidate the cache. */
474 registers_changed_ptid (ptid_t ptid)
476 for (auto oit = regcache::current_regcache.before_begin (),
477 it = std::next (oit);
478 it != regcache::current_regcache.end ();
481 if (ptid_match ((*it)->ptid (), ptid))
484 it = regcache::current_regcache.erase_after (oit);
490 if (ptid_match (current_thread_ptid, ptid))
492 current_thread_ptid = null_ptid;
493 current_thread_arch = NULL;
496 if (ptid_match (inferior_ptid, ptid))
498 /* We just deleted the regcache of the current thread. Need to
499 forget about any frames we have cached, too. */
500 reinit_frame_cache ();
505 registers_changed (void)
507 registers_changed_ptid (minus_one_ptid);
509 /* Force cleanup of any alloca areas if using C alloca instead of
510 a builtin alloca. This particular call is used to clean up
511 areas allocated by low level target code which may build up
512 during lengthy interactions between gdb and the target before
513 gdb gives control to the user (ie watchpoints). */
518 regcache_raw_update (struct regcache *regcache, int regnum)
520 gdb_assert (regcache != NULL);
522 regcache->raw_update (regnum);
526 regcache::raw_update (int regnum)
528 assert_regnum (regnum);
530 /* Make certain that the register cache is up-to-date with respect
531 to the current thread. This switching shouldn't be necessary
532 only there is still only one target side register cache. Sigh!
533 On the bright side, at least there is a regcache object. */
535 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
537 target_fetch_registers (this, regnum);
539 /* A number of targets can't access the whole set of raw
540 registers (because the debug API provides no means to get at
542 if (m_register_status[regnum] == REG_UNKNOWN)
543 m_register_status[regnum] = REG_UNAVAILABLE;
548 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
550 return regcache->raw_read (regnum, buf);
554 readable_regcache::raw_read (int regnum, gdb_byte *buf)
556 gdb_assert (buf != NULL);
559 if (m_register_status[regnum] != REG_VALID)
560 memset (buf, 0, m_descr->sizeof_register[regnum]);
562 memcpy (buf, register_buffer (regnum),
563 m_descr->sizeof_register[regnum]);
565 return (enum register_status) m_register_status[regnum];
569 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
571 gdb_assert (regcache != NULL);
572 return regcache->raw_read (regnum, val);
575 template<typename T, typename>
577 readable_regcache::raw_read (int regnum, T *val)
580 enum register_status status;
582 assert_regnum (regnum);
583 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
584 status = raw_read (regnum, buf);
585 if (status == REG_VALID)
586 *val = extract_integer<T> (buf,
587 m_descr->sizeof_register[regnum],
588 gdbarch_byte_order (m_descr->gdbarch));
595 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
598 gdb_assert (regcache != NULL);
599 return regcache->raw_read (regnum, val);
603 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
605 gdb_assert (regcache != NULL);
606 regcache->raw_write (regnum, val);
609 template<typename T, typename>
611 regcache::raw_write (int regnum, T val)
615 assert_regnum (regnum);
616 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
617 store_integer (buf, m_descr->sizeof_register[regnum],
618 gdbarch_byte_order (m_descr->gdbarch), val);
619 raw_write (regnum, buf);
623 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
626 gdb_assert (regcache != NULL);
627 regcache->raw_write (regnum, val);
631 regcache_raw_get_signed (struct regcache *regcache, int regnum)
634 enum register_status status;
636 status = regcache_raw_read_signed (regcache, regnum, &value);
637 if (status == REG_UNAVAILABLE)
638 throw_error (NOT_AVAILABLE_ERROR,
639 _("Register %d is not available"), regnum);
644 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
646 return regcache->cooked_read (regnum, buf);
650 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
652 gdb_assert (regnum >= 0);
653 gdb_assert (regnum < m_descr->nr_cooked_registers);
654 if (regnum < num_raw_registers ())
655 return raw_read (regnum, buf);
656 else if (m_has_pseudo
657 && m_register_status[regnum] != REG_UNKNOWN)
659 if (m_register_status[regnum] == REG_VALID)
660 memcpy (buf, register_buffer (regnum),
661 m_descr->sizeof_register[regnum]);
663 memset (buf, 0, m_descr->sizeof_register[regnum]);
665 return (enum register_status) m_register_status[regnum];
667 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
669 struct value *mark, *computed;
670 enum register_status result = REG_VALID;
672 mark = value_mark ();
674 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
676 if (value_entirely_available (computed))
677 memcpy (buf, value_contents_raw (computed),
678 m_descr->sizeof_register[regnum]);
681 memset (buf, 0, m_descr->sizeof_register[regnum]);
682 result = REG_UNAVAILABLE;
685 value_free_to_mark (mark);
690 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
695 regcache_cooked_read_value (struct regcache *regcache, int regnum)
697 return regcache->cooked_read_value (regnum);
701 readable_regcache::cooked_read_value (int regnum)
703 gdb_assert (regnum >= 0);
704 gdb_assert (regnum < m_descr->nr_cooked_registers);
706 if (regnum < num_raw_registers ()
707 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
708 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
710 struct value *result;
712 result = allocate_value (register_type (m_descr->gdbarch, regnum));
713 VALUE_LVAL (result) = lval_register;
714 VALUE_REGNUM (result) = regnum;
716 /* It is more efficient in general to do this delegation in this
717 direction than in the other one, even though the value-based
719 if (cooked_read (regnum,
720 value_contents_raw (result)) == REG_UNAVAILABLE)
721 mark_value_bytes_unavailable (result, 0,
722 TYPE_LENGTH (value_type (result)));
727 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
732 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
735 gdb_assert (regcache != NULL);
736 return regcache->cooked_read (regnum, val);
739 template<typename T, typename>
741 readable_regcache::cooked_read (int regnum, T *val)
743 enum register_status status;
746 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
747 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
748 status = cooked_read (regnum, buf);
749 if (status == REG_VALID)
750 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
751 gdbarch_byte_order (m_descr->gdbarch));
758 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
761 gdb_assert (regcache != NULL);
762 return regcache->cooked_read (regnum, val);
766 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
769 gdb_assert (regcache != NULL);
770 regcache->cooked_write (regnum, val);
773 template<typename T, typename>
775 regcache::cooked_write (int regnum, T val)
779 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
780 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
781 store_integer (buf, m_descr->sizeof_register[regnum],
782 gdbarch_byte_order (m_descr->gdbarch), val);
783 cooked_write (regnum, buf);
787 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
790 gdb_assert (regcache != NULL);
791 regcache->cooked_write (regnum, val);
795 regcache_raw_write (struct regcache *regcache, int regnum,
798 gdb_assert (regcache != NULL && buf != NULL);
799 regcache->raw_write (regnum, buf);
803 regcache::raw_write (int regnum, const gdb_byte *buf)
806 gdb_assert (buf != NULL);
807 assert_regnum (regnum);
808 gdb_assert (!m_readonly_p);
810 /* On the sparc, writing %g0 is a no-op, so we don't even want to
811 change the registers array if something writes to this register. */
812 if (gdbarch_cannot_store_register (arch (), regnum))
815 /* If we have a valid copy of the register, and new value == old
816 value, then don't bother doing the actual store. */
817 if (get_register_status (regnum) == REG_VALID
818 && (memcmp (register_buffer (regnum), buf,
819 m_descr->sizeof_register[regnum]) == 0))
822 target_prepare_to_store (this);
823 raw_supply (regnum, buf);
825 /* Invalidate the register after it is written, in case of a
827 regcache_invalidator invalidator (this, regnum);
829 target_store_registers (this, regnum);
831 /* The target did not throw an error so we can discard invalidating
833 invalidator.release ();
837 regcache_cooked_write (struct regcache *regcache, int regnum,
840 regcache->cooked_write (regnum, buf);
844 regcache::cooked_write (int regnum, const gdb_byte *buf)
846 gdb_assert (regnum >= 0);
847 gdb_assert (regnum < m_descr->nr_cooked_registers);
848 if (regnum < num_raw_registers ())
849 raw_write (regnum, buf);
851 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
855 /* Perform a partial register transfer using a read, modify, write
858 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
860 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
864 readable_regcache::read_part (int regnum, int offset, int len, void *in,
867 struct gdbarch *gdbarch = arch ();
868 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
870 gdb_assert (in != NULL);
871 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
872 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
873 /* Something to do? */
874 if (offset + len == 0)
876 /* Read (when needed) ... */
877 enum register_status status;
880 status = raw_read (regnum, reg);
882 status = cooked_read (regnum, reg);
883 if (status != REG_VALID)
887 memcpy (in, reg + offset, len);
893 regcache::write_part (int regnum, int offset, int len,
894 const void *out, bool is_raw)
896 struct gdbarch *gdbarch = arch ();
897 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
899 gdb_assert (out != NULL);
900 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
901 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
902 /* Something to do? */
903 if (offset + len == 0)
905 /* Read (when needed) ... */
907 || offset + len < m_descr->sizeof_register[regnum])
909 enum register_status status;
912 status = raw_read (regnum, reg);
914 status = cooked_read (regnum, reg);
915 if (status != REG_VALID)
919 memcpy (reg + offset, out, len);
920 /* ... write (when needed). */
922 raw_write (regnum, reg);
924 cooked_write (regnum, reg);
930 regcache_raw_read_part (struct regcache *regcache, int regnum,
931 int offset, int len, gdb_byte *buf)
933 return regcache->raw_read_part (regnum, offset, len, buf);
937 readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
939 assert_regnum (regnum);
940 return read_part (regnum, offset, len, buf, true);
944 regcache_raw_write_part (struct regcache *regcache, int regnum,
945 int offset, int len, const gdb_byte *buf)
947 regcache->raw_write_part (regnum, offset, len, buf);
951 regcache::raw_write_part (int regnum, int offset, int len,
954 assert_regnum (regnum);
955 write_part (regnum, offset, len, buf, true);
959 regcache_cooked_read_part (struct regcache *regcache, int regnum,
960 int offset, int len, gdb_byte *buf)
962 return regcache->cooked_read_part (regnum, offset, len, buf);
967 readable_regcache::cooked_read_part (int regnum, int offset, int len,
970 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
971 return read_part (regnum, offset, len, buf, false);
975 regcache_cooked_write_part (struct regcache *regcache, int regnum,
976 int offset, int len, const gdb_byte *buf)
978 regcache->cooked_write_part (regnum, offset, len, buf);
982 regcache::cooked_write_part (int regnum, int offset, int len,
985 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
986 write_part (regnum, offset, len, buf, false);
989 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
992 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
994 gdb_assert (regcache != NULL);
995 regcache->raw_supply (regnum, buf);
999 detached_regcache::raw_supply (int regnum, const void *buf)
1004 assert_regnum (regnum);
1006 regbuf = register_buffer (regnum);
1007 size = m_descr->sizeof_register[regnum];
1011 memcpy (regbuf, buf, size);
1012 m_register_status[regnum] = REG_VALID;
1016 /* This memset not strictly necessary, but better than garbage
1017 in case the register value manages to escape somewhere (due
1018 to a bug, no less). */
1019 memset (regbuf, 0, size);
1020 m_register_status[regnum] = REG_UNAVAILABLE;
1024 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1025 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1026 the register size is greater than ADDR_LEN, then the integer will be sign or
1027 zero extended. If the register size is smaller than the integer, then the
1028 most significant bytes of the integer will be truncated. */
1031 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1034 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1038 assert_regnum (regnum);
1039 gdb_assert (!m_readonly_p);
1041 regbuf = register_buffer (regnum);
1042 regsize = m_descr->sizeof_register[regnum];
1044 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1046 m_register_status[regnum] = REG_VALID;
1049 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1050 as calling raw_supply with NULL (which will set the state to
1054 regcache::raw_supply_zeroed (int regnum)
1059 assert_regnum (regnum);
1060 gdb_assert (!m_readonly_p);
1062 regbuf = register_buffer (regnum);
1063 size = m_descr->sizeof_register[regnum];
1065 memset (regbuf, 0, size);
1066 m_register_status[regnum] = REG_VALID;
1069 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1072 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1074 gdb_assert (regcache != NULL && buf != NULL);
1075 regcache->raw_collect (regnum, buf);
1079 regcache::raw_collect (int regnum, void *buf) const
1084 gdb_assert (buf != NULL);
1085 assert_regnum (regnum);
1087 regbuf = register_buffer (regnum);
1088 size = m_descr->sizeof_register[regnum];
1089 memcpy (buf, regbuf, size);
1092 /* Transfer a single or all registers belonging to a certain register
1093 set to or from a buffer. This is the main worker function for
1094 regcache_supply_regset and regcache_collect_regset. */
1096 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1097 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1098 If ADDR_LEN is greater than the register size, then the integer will be sign
1099 or zero extended. If ADDR_LEN is smaller than the register size, then the
1100 most significant bytes of the integer will be truncated. */
1103 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1104 bool is_signed) const
1106 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1107 const gdb_byte *regbuf;
1110 assert_regnum (regnum);
1112 regbuf = register_buffer (regnum);
1113 regsize = m_descr->sizeof_register[regnum];
1115 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1120 regcache::transfer_regset (const struct regset *regset,
1121 struct regcache *out_regcache,
1122 int regnum, const void *in_buf,
1123 void *out_buf, size_t size) const
1125 const struct regcache_map_entry *map;
1126 int offs = 0, count;
1128 for (map = (const struct regcache_map_entry *) regset->regmap;
1129 (count = map->count) != 0;
1132 int regno = map->regno;
1133 int slot_size = map->size;
1135 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1136 slot_size = m_descr->sizeof_register[regno];
1138 if (regno == REGCACHE_MAP_SKIP
1140 && (regnum < regno || regnum >= regno + count)))
1141 offs += count * slot_size;
1143 else if (regnum == -1)
1144 for (; count--; regno++, offs += slot_size)
1146 if (offs + slot_size > size)
1150 raw_collect (regno, (gdb_byte *) out_buf + offs);
1152 out_regcache->raw_supply (regno, in_buf
1153 ? (const gdb_byte *) in_buf + offs
1158 /* Transfer a single register and return. */
1159 offs += (regnum - regno) * slot_size;
1160 if (offs + slot_size > size)
1164 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1166 out_regcache->raw_supply (regnum, in_buf
1167 ? (const gdb_byte *) in_buf + offs
1174 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1175 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1176 If BUF is NULL, set the register(s) to "unavailable" status. */
1179 regcache_supply_regset (const struct regset *regset,
1180 struct regcache *regcache,
1181 int regnum, const void *buf, size_t size)
1183 regcache->supply_regset (regset, regnum, buf, size);
1187 regcache::supply_regset (const struct regset *regset,
1188 int regnum, const void *buf, size_t size)
1190 transfer_regset (regset, this, regnum, buf, NULL, size);
1193 /* Collect register REGNUM from REGCACHE to BUF, using the register
1194 map in REGSET. If REGNUM is -1, do this for all registers in
1198 regcache_collect_regset (const struct regset *regset,
1199 const struct regcache *regcache,
1200 int regnum, void *buf, size_t size)
1202 regcache->collect_regset (regset, regnum, buf, size);
1206 regcache::collect_regset (const struct regset *regset,
1207 int regnum, void *buf, size_t size) const
1209 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1213 /* Special handling for register PC. */
1216 regcache_read_pc (struct regcache *regcache)
1218 struct gdbarch *gdbarch = regcache->arch ();
1222 if (gdbarch_read_pc_p (gdbarch))
1223 pc_val = gdbarch_read_pc (gdbarch, regcache);
1224 /* Else use per-frame method on get_current_frame. */
1225 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1229 if (regcache_cooked_read_unsigned (regcache,
1230 gdbarch_pc_regnum (gdbarch),
1231 &raw_val) == REG_UNAVAILABLE)
1232 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1234 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1237 internal_error (__FILE__, __LINE__,
1238 _("regcache_read_pc: Unable to find PC"));
1243 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1245 struct gdbarch *gdbarch = regcache->arch ();
1247 if (gdbarch_write_pc_p (gdbarch))
1248 gdbarch_write_pc (gdbarch, regcache, pc);
1249 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1250 regcache_cooked_write_unsigned (regcache,
1251 gdbarch_pc_regnum (gdbarch), pc);
1253 internal_error (__FILE__, __LINE__,
1254 _("regcache_write_pc: Unable to update PC"));
1256 /* Writing the PC (for instance, from "load") invalidates the
1258 reinit_frame_cache ();
1262 reg_buffer::num_raw_registers () const
1264 return gdbarch_num_regs (arch ());
1268 regcache::debug_print_register (const char *func, int regno)
1270 struct gdbarch *gdbarch = arch ();
1272 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1273 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1274 && gdbarch_register_name (gdbarch, regno) != NULL
1275 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1276 fprintf_unfiltered (gdb_stdlog, "(%s)",
1277 gdbarch_register_name (gdbarch, regno));
1279 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1280 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1282 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1283 int size = register_size (gdbarch, regno);
1284 gdb_byte *buf = register_buffer (regno);
1286 fprintf_unfiltered (gdb_stdlog, " = ");
1287 for (int i = 0; i < size; i++)
1289 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1291 if (size <= sizeof (LONGEST))
1293 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1295 fprintf_unfiltered (gdb_stdlog, " %s %s",
1296 core_addr_to_string_nz (val), plongest (val));
1299 fprintf_unfiltered (gdb_stdlog, "\n");
1303 reg_flush_command (const char *command, int from_tty)
1305 /* Force-flush the register cache. */
1306 registers_changed ();
1308 printf_filtered (_("Register cache flushed.\n"));
1311 /* An abstract base class for register dump. */
1316 void dump (ui_file *file)
1318 auto descr = regcache_descr (m_gdbarch);
1320 int footnote_nr = 0;
1321 int footnote_register_offset = 0;
1322 int footnote_register_type_name_null = 0;
1323 long register_offset = 0;
1325 gdb_assert (descr->nr_cooked_registers
1326 == (gdbarch_num_regs (m_gdbarch)
1327 + gdbarch_num_pseudo_regs (m_gdbarch)));
1329 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1333 fprintf_unfiltered (file, " %-10s", "Name");
1336 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1340 else if (p[0] == '\0')
1342 fprintf_unfiltered (file, " %-10s", p);
1347 fprintf_unfiltered (file, " %4s", "Nr");
1349 fprintf_unfiltered (file, " %4d", regnum);
1351 /* Relative number. */
1353 fprintf_unfiltered (file, " %4s", "Rel");
1354 else if (regnum < gdbarch_num_regs (m_gdbarch))
1355 fprintf_unfiltered (file, " %4d", regnum);
1357 fprintf_unfiltered (file, " %4d",
1358 (regnum - gdbarch_num_regs (m_gdbarch)));
1362 fprintf_unfiltered (file, " %6s ", "Offset");
1365 fprintf_unfiltered (file, " %6ld",
1366 descr->register_offset[regnum]);
1367 if (register_offset != descr->register_offset[regnum]
1369 && (descr->register_offset[regnum]
1370 != (descr->register_offset[regnum - 1]
1371 + descr->sizeof_register[regnum - 1])))
1374 if (!footnote_register_offset)
1375 footnote_register_offset = ++footnote_nr;
1376 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1379 fprintf_unfiltered (file, " ");
1380 register_offset = (descr->register_offset[regnum]
1381 + descr->sizeof_register[regnum]);
1386 fprintf_unfiltered (file, " %5s ", "Size");
1388 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1393 std::string name_holder;
1399 static const char blt[] = "builtin_type";
1401 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1404 if (!footnote_register_type_name_null)
1405 footnote_register_type_name_null = ++footnote_nr;
1406 name_holder = string_printf ("*%d",
1407 footnote_register_type_name_null);
1408 t = name_holder.c_str ();
1410 /* Chop a leading builtin_type. */
1411 if (startswith (t, blt))
1414 fprintf_unfiltered (file, " %-15s", t);
1417 /* Leading space always present. */
1418 fprintf_unfiltered (file, " ");
1420 dump_reg (file, regnum);
1422 fprintf_unfiltered (file, "\n");
1425 if (footnote_register_offset)
1426 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1427 footnote_register_offset);
1428 if (footnote_register_type_name_null)
1429 fprintf_unfiltered (file,
1430 "*%d: Register type's name NULL.\n",
1431 footnote_register_type_name_null);
1434 virtual ~register_dump () {};
1437 register_dump (gdbarch *arch)
1441 /* Dump the register REGNUM contents. If REGNUM is -1, print the
1443 virtual void dump_reg (ui_file *file, int regnum) = 0;
1448 /* Dump registers from regcache, used for dump raw registers and
1449 cooked registers. */
1451 class register_dump_regcache : public register_dump
1454 register_dump_regcache (regcache *regcache, bool dump_pseudo)
1455 : register_dump (regcache->arch ()), m_regcache (regcache),
1456 m_dump_pseudo (dump_pseudo)
1461 void dump_reg (ui_file *file, int regnum) override
1466 fprintf_unfiltered (file, "Cooked value");
1468 fprintf_unfiltered (file, "Raw value");
1472 if (regnum < gdbarch_num_regs (m_gdbarch) || m_dump_pseudo)
1474 auto size = register_size (m_gdbarch, regnum);
1479 gdb::def_vector<gdb_byte> buf (size);
1480 auto status = m_regcache->cooked_read (regnum, buf.data ());
1482 if (status == REG_UNKNOWN)
1483 fprintf_unfiltered (file, "<invalid>");
1484 else if (status == REG_UNAVAILABLE)
1485 fprintf_unfiltered (file, "<unavailable>");
1488 print_hex_chars (file, buf.data (), size,
1489 gdbarch_byte_order (m_gdbarch), true);
1494 /* Just print "<cooked>" for pseudo register when
1495 regcache_dump_raw. */
1496 fprintf_unfiltered (file, "<cooked>");
1502 regcache *m_regcache;
1504 /* Dump pseudo registers or not. */
1505 const bool m_dump_pseudo;
1508 /* Dump from reg_buffer, used when there is no thread or
1511 class register_dump_reg_buffer : public register_dump, reg_buffer
1514 register_dump_reg_buffer (gdbarch *gdbarch, bool dump_pseudo)
1515 : register_dump (gdbarch), reg_buffer (gdbarch, dump_pseudo)
1520 void dump_reg (ui_file *file, int regnum) override
1525 fprintf_unfiltered (file, "Cooked value");
1527 fprintf_unfiltered (file, "Raw value");
1531 if (regnum < gdbarch_num_regs (m_gdbarch) || m_has_pseudo)
1533 auto size = register_size (m_gdbarch, regnum);
1538 auto status = get_register_status (regnum);
1540 gdb_assert (status != REG_VALID);
1542 if (status == REG_UNKNOWN)
1543 fprintf_unfiltered (file, "<invalid>");
1545 fprintf_unfiltered (file, "<unavailable>");
1549 /* Just print "<cooked>" for pseudo register when
1550 regcache_dump_raw. */
1551 fprintf_unfiltered (file, "<cooked>");
1557 /* For "maint print registers". */
1559 class register_dump_none : public register_dump
1562 register_dump_none (gdbarch *arch)
1563 : register_dump (arch)
1567 void dump_reg (ui_file *file, int regnum) override
1571 /* For "maint print remote-registers". */
1573 class register_dump_remote : public register_dump
1576 register_dump_remote (gdbarch *arch)
1577 : register_dump (arch)
1581 void dump_reg (ui_file *file, int regnum) override
1585 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1587 else if (regnum < gdbarch_num_regs (m_gdbarch))
1591 if (remote_register_number_and_offset (m_gdbarch, regnum,
1593 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1598 /* For "maint print register-groups". */
1600 class register_dump_groups : public register_dump
1603 register_dump_groups (gdbarch *arch)
1604 : register_dump (arch)
1608 void dump_reg (ui_file *file, int regnum) override
1611 fprintf_unfiltered (file, "Groups");
1614 const char *sep = "";
1615 struct reggroup *group;
1617 for (group = reggroup_next (m_gdbarch, NULL);
1619 group = reggroup_next (m_gdbarch, group))
1621 if (gdbarch_register_reggroup_p (m_gdbarch, regnum, group))
1623 fprintf_unfiltered (file,
1624 "%s%s", sep, reggroup_name (group));
1632 enum regcache_dump_what
1634 regcache_dump_none, regcache_dump_raw,
1635 regcache_dump_cooked, regcache_dump_groups,
1636 regcache_dump_remote
1640 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
1642 /* Where to send output. */
1650 if (!file.open (args, "w"))
1651 perror_with_name (_("maintenance print architecture"));
1655 std::unique_ptr<register_dump> dump;
1656 std::unique_ptr<regcache> regs;
1659 if (target_has_registers)
1660 gdbarch = get_current_regcache ()->arch ();
1662 gdbarch = target_gdbarch ();
1664 switch (what_to_dump)
1666 case regcache_dump_none:
1667 dump.reset (new register_dump_none (gdbarch));
1669 case regcache_dump_remote:
1670 dump.reset (new register_dump_remote (gdbarch));
1672 case regcache_dump_groups:
1673 dump.reset (new register_dump_groups (gdbarch));
1675 case regcache_dump_raw:
1676 case regcache_dump_cooked:
1678 auto dump_pseudo = (what_to_dump == regcache_dump_cooked);
1680 if (target_has_registers)
1681 dump.reset (new register_dump_regcache (get_current_regcache (),
1685 /* For the benefit of "maint print registers" & co when
1686 debugging an executable, allow dumping a regcache even when
1687 there is no thread selected / no registers. */
1688 dump.reset (new register_dump_reg_buffer (target_gdbarch (),
1699 maintenance_print_registers (const char *args, int from_tty)
1701 regcache_print (args, regcache_dump_none);
1705 maintenance_print_raw_registers (const char *args, int from_tty)
1707 regcache_print (args, regcache_dump_raw);
1711 maintenance_print_cooked_registers (const char *args, int from_tty)
1713 regcache_print (args, regcache_dump_cooked);
1717 maintenance_print_register_groups (const char *args, int from_tty)
1719 regcache_print (args, regcache_dump_groups);
1723 maintenance_print_remote_registers (const char *args, int from_tty)
1725 regcache_print (args, regcache_dump_remote);
1729 #include "selftest.h"
1730 #include "selftest-arch.h"
1731 #include "gdbthread.h"
1732 #include "target-float.h"
1734 namespace selftests {
1736 class regcache_access : public regcache
1740 /* Return the number of elements in current_regcache. */
1743 current_regcache_size ()
1745 return std::distance (regcache::current_regcache.begin (),
1746 regcache::current_regcache.end ());
1751 current_regcache_test (void)
1753 /* It is empty at the start. */
1754 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1756 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1758 /* Get regcache from ptid1, a new regcache is added to
1759 current_regcache. */
1760 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1764 SELF_CHECK (regcache != NULL);
1765 SELF_CHECK (regcache->ptid () == ptid1);
1766 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1768 /* Get regcache from ptid2, a new regcache is added to
1769 current_regcache. */
1770 regcache = get_thread_arch_aspace_regcache (ptid2,
1773 SELF_CHECK (regcache != NULL);
1774 SELF_CHECK (regcache->ptid () == ptid2);
1775 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1777 /* Get regcache from ptid3, a new regcache is added to
1778 current_regcache. */
1779 regcache = get_thread_arch_aspace_regcache (ptid3,
1782 SELF_CHECK (regcache != NULL);
1783 SELF_CHECK (regcache->ptid () == ptid3);
1784 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1786 /* Get regcache from ptid2 again, nothing is added to
1787 current_regcache. */
1788 regcache = get_thread_arch_aspace_regcache (ptid2,
1791 SELF_CHECK (regcache != NULL);
1792 SELF_CHECK (regcache->ptid () == ptid2);
1793 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1795 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1796 current_regcache. */
1797 registers_changed_ptid (ptid2);
1798 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1801 static void test_target_fetch_registers (target_ops *self, regcache *regs,
1803 static void test_target_store_registers (target_ops *self, regcache *regs,
1805 static enum target_xfer_status
1806 test_target_xfer_partial (struct target_ops *ops,
1807 enum target_object object,
1808 const char *annex, gdb_byte *readbuf,
1809 const gdb_byte *writebuf,
1810 ULONGEST offset, ULONGEST len,
1811 ULONGEST *xfered_len);
1813 class target_ops_no_register : public test_target_ops
1816 target_ops_no_register ()
1817 : test_target_ops {}
1819 to_fetch_registers = test_target_fetch_registers;
1820 to_store_registers = test_target_store_registers;
1821 to_xfer_partial = test_target_xfer_partial;
1828 fetch_registers_called = 0;
1829 store_registers_called = 0;
1830 xfer_partial_called = 0;
1833 unsigned int fetch_registers_called = 0;
1834 unsigned int store_registers_called = 0;
1835 unsigned int xfer_partial_called = 0;
1839 test_target_fetch_registers (target_ops *self, regcache *regs, int regno)
1841 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1843 /* Mark register available. */
1844 regs->raw_supply_zeroed (regno);
1845 ops->fetch_registers_called++;
1849 test_target_store_registers (target_ops *self, regcache *regs, int regno)
1851 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1853 ops->store_registers_called++;
1856 static enum target_xfer_status
1857 test_target_xfer_partial (struct target_ops *self, enum target_object object,
1858 const char *annex, gdb_byte *readbuf,
1859 const gdb_byte *writebuf,
1860 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1862 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1864 ops->xfer_partial_called++;
1867 return TARGET_XFER_OK;
1870 class readwrite_regcache : public regcache
1873 readwrite_regcache (struct gdbarch *gdbarch)
1874 : regcache (gdbarch, nullptr, false)
1878 /* Test regcache::cooked_read gets registers from raw registers and
1879 memory instead of target to_{fetch,store}_registers. */
1882 cooked_read_test (struct gdbarch *gdbarch)
1884 /* Error out if debugging something, because we're going to push the
1885 test target, which would pop any existing target. */
1886 if (current_target.to_stratum >= process_stratum)
1887 error (_("target already pushed"));
1889 /* Create a mock environment. An inferior with a thread, with a
1890 process_stratum target pushed. */
1892 target_ops_no_register mock_target;
1893 ptid_t mock_ptid (1, 1);
1894 inferior mock_inferior (mock_ptid.pid ());
1895 address_space mock_aspace {};
1896 mock_inferior.gdbarch = gdbarch;
1897 mock_inferior.aspace = &mock_aspace;
1898 thread_info mock_thread (&mock_inferior, mock_ptid);
1900 scoped_restore restore_thread_list
1901 = make_scoped_restore (&thread_list, &mock_thread);
1903 /* Add the mock inferior to the inferior list so that look ups by
1904 target+ptid can find it. */
1905 scoped_restore restore_inferior_list
1906 = make_scoped_restore (&inferior_list);
1907 inferior_list = &mock_inferior;
1909 /* Switch to the mock inferior. */
1910 scoped_restore_current_inferior restore_current_inferior;
1911 set_current_inferior (&mock_inferior);
1913 /* Push the process_stratum target so we can mock accessing
1915 push_target (&mock_target);
1917 /* Pop it again on exit (return/exception). */
1922 pop_all_targets_at_and_above (process_stratum);
1926 /* Switch to the mock thread. */
1927 scoped_restore restore_inferior_ptid
1928 = make_scoped_restore (&inferior_ptid, mock_ptid);
1930 /* Test that read one raw register from regcache_no_target will go
1931 to the target layer. */
1934 /* Find a raw register which size isn't zero. */
1935 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1937 if (register_size (gdbarch, regnum) != 0)
1941 readwrite_regcache readwrite (gdbarch);
1942 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1944 readwrite.raw_read (regnum, buf.data ());
1946 /* raw_read calls target_fetch_registers. */
1947 SELF_CHECK (mock_target.fetch_registers_called > 0);
1948 mock_target.reset ();
1950 /* Mark all raw registers valid, so the following raw registers
1951 accesses won't go to target. */
1952 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1953 readwrite.raw_update (i);
1955 mock_target.reset ();
1956 /* Then, read all raw and pseudo registers, and don't expect calling
1957 to_{fetch,store}_registers. */
1958 for (int regnum = 0;
1959 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1962 if (register_size (gdbarch, regnum) == 0)
1965 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1967 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1969 SELF_CHECK (mock_target.fetch_registers_called == 0);
1970 SELF_CHECK (mock_target.store_registers_called == 0);
1972 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1973 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1974 SELF_CHECK (mock_target.xfer_partial_called == 0);
1976 mock_target.reset ();
1979 readonly_detached_regcache readonly (readwrite);
1981 /* GDB may go to target layer to fetch all registers and memory for
1982 readonly regcache. */
1983 mock_target.reset ();
1985 for (int regnum = 0;
1986 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1989 if (register_size (gdbarch, regnum) == 0)
1992 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1993 enum register_status status = readonly.cooked_read (regnum,
1996 if (regnum < gdbarch_num_regs (gdbarch))
1998 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2000 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
2001 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
2002 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
2003 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
2004 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
2005 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
2006 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score)
2008 /* Raw registers. If raw registers are not in save_reggroup,
2009 their status are unknown. */
2010 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
2011 SELF_CHECK (status == REG_VALID);
2013 SELF_CHECK (status == REG_UNKNOWN);
2016 SELF_CHECK (status == REG_VALID);
2020 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
2021 SELF_CHECK (status == REG_VALID);
2024 /* If pseudo registers are not in save_reggroup, some of
2025 them can be computed from saved raw registers, but some
2026 of them are unknown. */
2027 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2029 if (bfd_arch == bfd_arch_frv
2030 || bfd_arch == bfd_arch_m32c
2031 || bfd_arch == bfd_arch_mep
2032 || bfd_arch == bfd_arch_sh)
2033 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
2034 else if (bfd_arch == bfd_arch_mips
2035 || bfd_arch == bfd_arch_h8300)
2036 SELF_CHECK (status == REG_UNKNOWN);
2038 SELF_CHECK (status == REG_VALID);
2042 SELF_CHECK (mock_target.fetch_registers_called == 0);
2043 SELF_CHECK (mock_target.store_registers_called == 0);
2044 SELF_CHECK (mock_target.xfer_partial_called == 0);
2046 mock_target.reset ();
2050 /* Test regcache::cooked_write by writing some expected contents to
2051 registers, and checking that contents read from registers and the
2052 expected contents are the same. */
2055 cooked_write_test (struct gdbarch *gdbarch)
2057 /* Error out if debugging something, because we're going to push the
2058 test target, which would pop any existing target. */
2059 if (current_target.to_stratum >= process_stratum)
2060 error (_("target already pushed"));
2062 /* Create a mock environment. A process_stratum target pushed. */
2064 target_ops_no_register mock_target;
2066 /* Push the process_stratum target so we can mock accessing
2068 push_target (&mock_target);
2070 /* Pop it again on exit (return/exception). */
2075 pop_all_targets_at_and_above (process_stratum);
2079 readwrite_regcache readwrite (gdbarch);
2081 const int num_regs = (gdbarch_num_regs (gdbarch)
2082 + gdbarch_num_pseudo_regs (gdbarch));
2084 for (auto regnum = 0; regnum < num_regs; regnum++)
2086 if (register_size (gdbarch, regnum) == 0
2087 || gdbarch_cannot_store_register (gdbarch, regnum))
2090 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2092 if ((bfd_arch == bfd_arch_sparc
2093 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
2094 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
2095 && gdbarch_ptr_bit (gdbarch) == 64
2096 && (regnum >= gdbarch_num_regs (gdbarch)
2097 && regnum <= gdbarch_num_regs (gdbarch) + 4))
2098 || (bfd_arch == bfd_arch_sh
2099 /* FPSCR_C_REGNUM in sh64 is hard to test. */
2100 && gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh5
2102 || (bfd_arch == bfd_arch_spu
2103 /* SPU pseudo registers except SPU_SP_REGNUM are got by
2104 TARGET_OBJECT_SPU. */
2105 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
2108 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
2109 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
2110 const auto type = register_type (gdbarch, regnum);
2112 if (TYPE_CODE (type) == TYPE_CODE_FLT
2113 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2115 /* Generate valid float format. */
2116 target_float_from_string (expected.data (), type, "1.25");
2118 else if (TYPE_CODE (type) == TYPE_CODE_INT
2119 || TYPE_CODE (type) == TYPE_CODE_ARRAY
2120 || TYPE_CODE (type) == TYPE_CODE_PTR
2121 || TYPE_CODE (type) == TYPE_CODE_UNION
2122 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
2124 if (bfd_arch == bfd_arch_ia64
2125 || (regnum >= gdbarch_num_regs (gdbarch)
2126 && (bfd_arch == bfd_arch_xtensa
2127 || bfd_arch == bfd_arch_bfin
2128 || bfd_arch == bfd_arch_m32c
2129 /* m68hc11 pseudo registers are in memory. */
2130 || bfd_arch == bfd_arch_m68hc11
2131 || bfd_arch == bfd_arch_m68hc12
2132 || bfd_arch == bfd_arch_s390))
2133 || (bfd_arch == bfd_arch_frv
2134 /* FRV pseudo registers except iacc0. */
2135 && regnum > gdbarch_num_regs (gdbarch)))
2137 /* Skip setting the expected values for some architecture
2140 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
2142 /* RL78_PC_REGNUM */
2143 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
2148 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
2152 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
2154 /* No idea how to test flags. */
2159 /* If we don't know how to create the expected value for the
2160 this type, make it fail. */
2164 readwrite.cooked_write (regnum, expected.data ());
2166 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2167 SELF_CHECK (expected == buf);
2171 } // namespace selftests
2172 #endif /* GDB_SELF_TEST */
2175 _initialize_regcache (void)
2177 regcache_descr_handle
2178 = gdbarch_data_register_post_init (init_regcache_descr);
2180 observer_attach_target_changed (regcache_observer_target_changed);
2181 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
2183 add_com ("flushregs", class_maintenance, reg_flush_command,
2184 _("Force gdb to flush its register cache (maintainer command)"));
2186 add_cmd ("registers", class_maintenance, maintenance_print_registers,
2187 _("Print the internal register configuration.\n"
2188 "Takes an optional file parameter."), &maintenanceprintlist);
2189 add_cmd ("raw-registers", class_maintenance,
2190 maintenance_print_raw_registers,
2191 _("Print the internal register configuration "
2192 "including raw values.\n"
2193 "Takes an optional file parameter."), &maintenanceprintlist);
2194 add_cmd ("cooked-registers", class_maintenance,
2195 maintenance_print_cooked_registers,
2196 _("Print the internal register configuration "
2197 "including cooked values.\n"
2198 "Takes an optional file parameter."), &maintenanceprintlist);
2199 add_cmd ("register-groups", class_maintenance,
2200 maintenance_print_register_groups,
2201 _("Print the internal register configuration "
2202 "including each register's group.\n"
2203 "Takes an optional file parameter."),
2204 &maintenanceprintlist);
2205 add_cmd ("remote-registers", class_maintenance,
2206 maintenance_print_remote_registers, _("\
2207 Print the internal register configuration including each register's\n\
2208 remote register number and buffer offset in the g/G packets.\n\
2209 Takes an optional file parameter."),
2210 &maintenanceprintlist);
2213 selftests::register_test ("current_regcache", selftests::current_regcache_test);
2215 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2216 selftests::cooked_read_test);
2217 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2218 selftests::cooked_write_test);