1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2017 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 regcache::regcache (gdbarch *gdbarch, address_space *aspace_,
186 : m_aspace (aspace_), m_readonly_p (readonly_p_)
188 gdb_assert (gdbarch != NULL);
189 m_descr = regcache_descr (gdbarch);
193 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
194 m_register_status = XCNEWVEC (signed char,
195 m_descr->nr_cooked_registers);
199 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
200 m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch));
202 m_ptid = minus_one_ptid;
205 static enum register_status
206 do_cooked_read (void *src, int regnum, gdb_byte *buf)
208 struct regcache *regcache = (struct regcache *) src;
210 return regcache_cooked_read (regcache, regnum, buf);
213 regcache::regcache (readonly_t, const regcache &src)
214 : regcache (src.arch (), src.aspace (), true)
216 gdb_assert (!src.m_readonly_p);
217 save (do_cooked_read, (void *) &src);
221 regcache::arch () const
223 return m_descr->gdbarch;
226 /* See regcache.h. */
229 regcache_get_ptid (const struct regcache *regcache)
231 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
233 return regcache->ptid ();
236 /* Cleanup class for invalidating a register. */
238 class regcache_invalidator
242 regcache_invalidator (struct regcache *regcache, int regnum)
243 : m_regcache (regcache),
248 ~regcache_invalidator ()
250 if (m_regcache != nullptr)
251 regcache_invalidate (m_regcache, m_regnum);
254 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
258 m_regcache = nullptr;
263 struct regcache *m_regcache;
267 struct address_space *
268 get_regcache_aspace (const struct regcache *regcache)
270 return regcache->aspace ();
273 /* Return a pointer to register REGNUM's buffer cache. */
276 regcache::register_buffer (int regnum) const
278 return m_registers + m_descr->register_offset[regnum];
282 regcache_save (struct regcache *regcache,
283 regcache_cooked_read_ftype *cooked_read, void *src)
285 regcache->save (cooked_read, src);
289 regcache::save (regcache_cooked_read_ftype *cooked_read,
292 struct gdbarch *gdbarch = m_descr->gdbarch;
295 /* The DST should be `read-only', if it wasn't then the save would
296 end up trying to write the register values back out to the
298 gdb_assert (m_readonly_p);
299 /* Clear the dest. */
300 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
301 memset (m_register_status, 0, m_descr->nr_cooked_registers);
302 /* Copy over any registers (identified by their membership in the
303 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
304 gdbarch_num_pseudo_regs) range is checked since some architectures need
305 to save/restore `cooked' registers that live in memory. */
306 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
308 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
310 gdb_byte *dst_buf = register_buffer (regnum);
311 enum register_status status = cooked_read (src, regnum, dst_buf);
313 gdb_assert (status != REG_UNKNOWN);
315 if (status != REG_VALID)
316 memset (dst_buf, 0, register_size (gdbarch, regnum));
318 m_register_status[regnum] = status;
324 regcache::restore (struct regcache *src)
326 struct gdbarch *gdbarch = m_descr->gdbarch;
329 /* The dst had better not be read-only. If it is, the `restore'
330 doesn't make much sense. */
331 gdb_assert (!m_readonly_p);
332 gdb_assert (src->m_readonly_p);
333 /* Copy over any registers, being careful to only restore those that
334 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
335 + gdbarch_num_pseudo_regs) range is checked since some architectures need
336 to save/restore `cooked' registers that live in memory. */
337 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
339 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
341 if (src->m_register_status[regnum] == REG_VALID)
342 cooked_write (regnum, src->register_buffer (regnum));
348 regcache_cpy (struct regcache *dst, struct regcache *src)
350 gdb_assert (src != NULL && dst != NULL);
351 gdb_assert (src->m_descr->gdbarch == dst->m_descr->gdbarch);
352 gdb_assert (src != dst);
353 gdb_assert (src->m_readonly_p && !dst->m_readonly_p);
359 regcache_dup (struct regcache *src)
361 return new regcache (regcache::readonly, *src);
365 regcache_register_status (const struct regcache *regcache, int regnum)
367 gdb_assert (regcache != NULL);
368 return regcache->get_register_status (regnum);
372 regcache::get_register_status (int regnum) const
374 gdb_assert (regnum >= 0);
376 gdb_assert (regnum < m_descr->nr_cooked_registers);
378 gdb_assert (regnum < num_raw_registers ());
380 return (enum register_status) m_register_status[regnum];
384 regcache_invalidate (struct regcache *regcache, int regnum)
386 gdb_assert (regcache != NULL);
387 regcache->invalidate (regnum);
391 regcache::invalidate (int regnum)
393 gdb_assert (!m_readonly_p);
394 assert_regnum (regnum);
395 m_register_status[regnum] = REG_UNKNOWN;
399 regcache::assert_regnum (int regnum) const
401 gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (arch ()));
404 /* Global structure containing the current regcache. */
406 /* NOTE: this is a write-through cache. There is no "dirty" bit for
407 recording if the register values have been changed (eg. by the
408 user). Therefore all registers must be written back to the
409 target when appropriate. */
410 std::forward_list<regcache *> regcache::current_regcache;
413 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
414 struct address_space *aspace)
416 for (const auto ®cache : regcache::current_regcache)
417 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
420 regcache *new_regcache = new regcache (gdbarch, aspace, false);
422 regcache::current_regcache.push_front (new_regcache);
423 new_regcache->set_ptid (ptid);
429 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
431 address_space *aspace = target_thread_address_space (ptid);
433 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
436 static ptid_t current_thread_ptid;
437 static struct gdbarch *current_thread_arch;
440 get_thread_regcache (ptid_t ptid)
442 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
444 current_thread_ptid = ptid;
445 current_thread_arch = target_thread_architecture (ptid);
448 return get_thread_arch_regcache (ptid, current_thread_arch);
452 get_current_regcache (void)
454 return get_thread_regcache (inferior_ptid);
457 /* See common/common-regcache.h. */
460 get_thread_regcache_for_ptid (ptid_t ptid)
462 return get_thread_regcache (ptid);
465 /* Observer for the target_changed event. */
468 regcache_observer_target_changed (struct target_ops *target)
470 registers_changed ();
473 /* Update global variables old ptids to hold NEW_PTID if they were
476 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
478 for (auto ®cache : regcache::current_regcache)
480 if (ptid_equal (regcache->ptid (), old_ptid))
481 regcache->set_ptid (new_ptid);
485 /* Low level examining and depositing of registers.
487 The caller is responsible for making sure that the inferior is
488 stopped before calling the fetching routines, or it will get
489 garbage. (a change from GDB version 3, in which the caller got the
490 value from the last stop). */
492 /* REGISTERS_CHANGED ()
494 Indicate that registers may have changed, so invalidate the cache. */
497 registers_changed_ptid (ptid_t ptid)
499 for (auto oit = regcache::current_regcache.before_begin (),
500 it = std::next (oit);
501 it != regcache::current_regcache.end ();
504 if (ptid_match ((*it)->ptid (), ptid))
507 it = regcache::current_regcache.erase_after (oit);
513 if (ptid_match (current_thread_ptid, ptid))
515 current_thread_ptid = null_ptid;
516 current_thread_arch = NULL;
519 if (ptid_match (inferior_ptid, ptid))
521 /* We just deleted the regcache of the current thread. Need to
522 forget about any frames we have cached, too. */
523 reinit_frame_cache ();
528 registers_changed (void)
530 registers_changed_ptid (minus_one_ptid);
532 /* Force cleanup of any alloca areas if using C alloca instead of
533 a builtin alloca. This particular call is used to clean up
534 areas allocated by low level target code which may build up
535 during lengthy interactions between gdb and the target before
536 gdb gives control to the user (ie watchpoints). */
541 regcache_raw_update (struct regcache *regcache, int regnum)
543 gdb_assert (regcache != NULL);
545 regcache->raw_update (regnum);
549 regcache::raw_update (int regnum)
551 assert_regnum (regnum);
553 /* Make certain that the register cache is up-to-date with respect
554 to the current thread. This switching shouldn't be necessary
555 only there is still only one target side register cache. Sigh!
556 On the bright side, at least there is a regcache object. */
558 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
560 target_fetch_registers (this, regnum);
562 /* A number of targets can't access the whole set of raw
563 registers (because the debug API provides no means to get at
565 if (m_register_status[regnum] == REG_UNKNOWN)
566 m_register_status[regnum] = REG_UNAVAILABLE;
571 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
573 return regcache->raw_read (regnum, buf);
577 regcache::raw_read (int regnum, gdb_byte *buf)
579 gdb_assert (buf != NULL);
582 if (m_register_status[regnum] != REG_VALID)
583 memset (buf, 0, m_descr->sizeof_register[regnum]);
585 memcpy (buf, register_buffer (regnum),
586 m_descr->sizeof_register[regnum]);
588 return (enum register_status) m_register_status[regnum];
592 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
594 gdb_assert (regcache != NULL);
595 return regcache->raw_read (regnum, val);
598 template<typename T, typename>
600 regcache::raw_read (int regnum, T *val)
603 enum register_status status;
605 assert_regnum (regnum);
606 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
607 status = raw_read (regnum, buf);
608 if (status == REG_VALID)
609 *val = extract_integer<T> (buf,
610 m_descr->sizeof_register[regnum],
611 gdbarch_byte_order (m_descr->gdbarch));
618 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
621 gdb_assert (regcache != NULL);
622 return regcache->raw_read (regnum, val);
626 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
628 gdb_assert (regcache != NULL);
629 regcache->raw_write (regnum, val);
632 template<typename T, typename>
634 regcache::raw_write (int regnum, T val)
638 assert_regnum (regnum);
639 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
640 store_integer (buf, m_descr->sizeof_register[regnum],
641 gdbarch_byte_order (m_descr->gdbarch), val);
642 raw_write (regnum, buf);
646 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
649 gdb_assert (regcache != NULL);
650 regcache->raw_write (regnum, val);
654 regcache_raw_get_signed (struct regcache *regcache, int regnum)
657 enum register_status status;
659 status = regcache_raw_read_signed (regcache, regnum, &value);
660 if (status == REG_UNAVAILABLE)
661 throw_error (NOT_AVAILABLE_ERROR,
662 _("Register %d is not available"), regnum);
667 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
669 return regcache->cooked_read (regnum, buf);
673 regcache::cooked_read (int regnum, gdb_byte *buf)
675 gdb_assert (regnum >= 0);
676 gdb_assert (regnum < m_descr->nr_cooked_registers);
677 if (regnum < num_raw_registers ())
678 return raw_read (regnum, buf);
679 else if (m_readonly_p
680 && m_register_status[regnum] != REG_UNKNOWN)
682 /* Read-only register cache, perhaps the cooked value was
684 if (m_register_status[regnum] == REG_VALID)
685 memcpy (buf, register_buffer (regnum),
686 m_descr->sizeof_register[regnum]);
688 memset (buf, 0, m_descr->sizeof_register[regnum]);
690 return (enum register_status) m_register_status[regnum];
692 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
694 struct value *mark, *computed;
695 enum register_status result = REG_VALID;
697 mark = value_mark ();
699 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
701 if (value_entirely_available (computed))
702 memcpy (buf, value_contents_raw (computed),
703 m_descr->sizeof_register[regnum]);
706 memset (buf, 0, m_descr->sizeof_register[regnum]);
707 result = REG_UNAVAILABLE;
710 value_free_to_mark (mark);
715 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
720 regcache_cooked_read_value (struct regcache *regcache, int regnum)
722 return regcache->cooked_read_value (regnum);
726 regcache::cooked_read_value (int regnum)
728 gdb_assert (regnum >= 0);
729 gdb_assert (regnum < m_descr->nr_cooked_registers);
731 if (regnum < num_raw_registers ()
732 || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN)
733 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
735 struct value *result;
737 result = allocate_value (register_type (m_descr->gdbarch, regnum));
738 VALUE_LVAL (result) = lval_register;
739 VALUE_REGNUM (result) = regnum;
741 /* It is more efficient in general to do this delegation in this
742 direction than in the other one, even though the value-based
744 if (cooked_read (regnum,
745 value_contents_raw (result)) == REG_UNAVAILABLE)
746 mark_value_bytes_unavailable (result, 0,
747 TYPE_LENGTH (value_type (result)));
752 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
757 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
760 gdb_assert (regcache != NULL);
761 return regcache->cooked_read (regnum, val);
764 template<typename T, typename>
766 regcache::cooked_read (int regnum, T *val)
768 enum register_status status;
771 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
772 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
773 status = cooked_read (regnum, buf);
774 if (status == REG_VALID)
775 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
776 gdbarch_byte_order (m_descr->gdbarch));
783 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
786 gdb_assert (regcache != NULL);
787 return regcache->cooked_read (regnum, val);
791 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
794 gdb_assert (regcache != NULL);
795 regcache->cooked_write (regnum, val);
798 template<typename T, typename>
800 regcache::cooked_write (int regnum, T val)
804 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
805 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
806 store_integer (buf, m_descr->sizeof_register[regnum],
807 gdbarch_byte_order (m_descr->gdbarch), val);
808 cooked_write (regnum, buf);
812 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
815 gdb_assert (regcache != NULL);
816 regcache->cooked_write (regnum, val);
819 /* See regcache.h. */
822 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
825 regcache->raw_set_cached_value (regnum, buf);
829 regcache::raw_set_cached_value (int regnum, const gdb_byte *buf)
831 memcpy (register_buffer (regnum), buf,
832 m_descr->sizeof_register[regnum]);
833 m_register_status[regnum] = REG_VALID;
837 regcache_raw_write (struct regcache *regcache, int regnum,
840 gdb_assert (regcache != NULL && buf != NULL);
841 regcache->raw_write (regnum, buf);
845 regcache::raw_write (int regnum, const gdb_byte *buf)
848 gdb_assert (buf != NULL);
849 assert_regnum (regnum);
850 gdb_assert (!m_readonly_p);
852 /* On the sparc, writing %g0 is a no-op, so we don't even want to
853 change the registers array if something writes to this register. */
854 if (gdbarch_cannot_store_register (arch (), regnum))
857 /* If we have a valid copy of the register, and new value == old
858 value, then don't bother doing the actual store. */
859 if (get_register_status (regnum) == REG_VALID
860 && (memcmp (register_buffer (regnum), buf,
861 m_descr->sizeof_register[regnum]) == 0))
864 target_prepare_to_store (this);
865 raw_set_cached_value (regnum, buf);
867 /* Invalidate the register after it is written, in case of a
869 regcache_invalidator invalidator (this, regnum);
871 target_store_registers (this, regnum);
873 /* The target did not throw an error so we can discard invalidating
875 invalidator.release ();
879 regcache_cooked_write (struct regcache *regcache, int regnum,
882 regcache->cooked_write (regnum, buf);
886 regcache::cooked_write (int regnum, const gdb_byte *buf)
888 gdb_assert (regnum >= 0);
889 gdb_assert (regnum < m_descr->nr_cooked_registers);
890 if (regnum < num_raw_registers ())
891 raw_write (regnum, buf);
893 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
897 /* Perform a partial register transfer using a read, modify, write
900 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
902 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
906 regcache::xfer_part (int regnum, int offset, int len, void *in,
907 const void *out, bool is_raw)
909 struct gdbarch *gdbarch = arch ();
910 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
912 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
913 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
914 /* Something to do? */
915 if (offset + len == 0)
917 /* Read (when needed) ... */
920 || offset + len < m_descr->sizeof_register[regnum])
922 enum register_status status;
925 status = raw_read (regnum, reg);
927 status = cooked_read (regnum, reg);
928 if (status != REG_VALID)
933 memcpy (in, reg + offset, len);
935 memcpy (reg + offset, out, len);
936 /* ... write (when needed). */
940 raw_write (regnum, reg);
942 cooked_write (regnum, reg);
949 regcache_raw_read_part (struct regcache *regcache, int regnum,
950 int offset, int len, gdb_byte *buf)
952 return regcache->raw_read_part (regnum, offset, len, buf);
956 regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
958 assert_regnum (regnum);
959 return xfer_part (regnum, offset, len, buf, NULL, true);
963 regcache_raw_write_part (struct regcache *regcache, int regnum,
964 int offset, int len, const gdb_byte *buf)
966 regcache->raw_write_part (regnum, offset, len, buf);
970 regcache::raw_write_part (int regnum, int offset, int len,
973 assert_regnum (regnum);
974 xfer_part (regnum, offset, len, NULL, buf, true);
978 regcache_cooked_read_part (struct regcache *regcache, int regnum,
979 int offset, int len, gdb_byte *buf)
981 return regcache->cooked_read_part (regnum, offset, len, buf);
986 regcache::cooked_read_part (int regnum, int offset, int len, gdb_byte *buf)
988 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
989 return xfer_part (regnum, offset, len, buf, NULL, false);
993 regcache_cooked_write_part (struct regcache *regcache, int regnum,
994 int offset, int len, const gdb_byte *buf)
996 regcache->cooked_write_part (regnum, offset, len, buf);
1000 regcache::cooked_write_part (int regnum, int offset, int len,
1001 const gdb_byte *buf)
1003 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1004 xfer_part (regnum, offset, len, NULL, buf, false);
1007 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1010 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1012 gdb_assert (regcache != NULL);
1013 regcache->raw_supply (regnum, buf);
1017 regcache::raw_supply (int regnum, const void *buf)
1022 assert_regnum (regnum);
1023 gdb_assert (!m_readonly_p);
1025 regbuf = register_buffer (regnum);
1026 size = m_descr->sizeof_register[regnum];
1030 memcpy (regbuf, buf, size);
1031 m_register_status[regnum] = REG_VALID;
1035 /* This memset not strictly necessary, but better than garbage
1036 in case the register value manages to escape somewhere (due
1037 to a bug, no less). */
1038 memset (regbuf, 0, size);
1039 m_register_status[regnum] = REG_UNAVAILABLE;
1043 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1044 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1045 the register size is greater than ADDR_LEN, then the integer will be sign or
1046 zero extended. If the register size is smaller than the integer, then the
1047 most significant bytes of the integer will be truncated. */
1050 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1053 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1057 assert_regnum (regnum);
1058 gdb_assert (!m_readonly_p);
1060 regbuf = register_buffer (regnum);
1061 regsize = m_descr->sizeof_register[regnum];
1063 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1065 m_register_status[regnum] = REG_VALID;
1068 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1069 as calling raw_supply with NULL (which will set the state to
1073 regcache::raw_supply_zeroed (int regnum)
1078 assert_regnum (regnum);
1079 gdb_assert (!m_readonly_p);
1081 regbuf = register_buffer (regnum);
1082 size = m_descr->sizeof_register[regnum];
1084 memset (regbuf, 0, size);
1085 m_register_status[regnum] = REG_VALID;
1088 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1091 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1093 gdb_assert (regcache != NULL && buf != NULL);
1094 regcache->raw_collect (regnum, buf);
1098 regcache::raw_collect (int regnum, void *buf) const
1103 gdb_assert (buf != NULL);
1104 assert_regnum (regnum);
1106 regbuf = register_buffer (regnum);
1107 size = m_descr->sizeof_register[regnum];
1108 memcpy (buf, regbuf, size);
1111 /* Transfer a single or all registers belonging to a certain register
1112 set to or from a buffer. This is the main worker function for
1113 regcache_supply_regset and regcache_collect_regset. */
1115 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1116 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1117 If ADDR_LEN is greater than the register size, then the integer will be sign
1118 or zero extended. If ADDR_LEN is smaller than the register size, then the
1119 most significant bytes of the integer will be truncated. */
1122 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1123 bool is_signed) const
1125 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1126 const gdb_byte *regbuf;
1129 assert_regnum (regnum);
1131 regbuf = register_buffer (regnum);
1132 regsize = m_descr->sizeof_register[regnum];
1134 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1139 regcache::transfer_regset (const struct regset *regset,
1140 struct regcache *out_regcache,
1141 int regnum, const void *in_buf,
1142 void *out_buf, size_t size) const
1144 const struct regcache_map_entry *map;
1145 int offs = 0, count;
1147 for (map = (const struct regcache_map_entry *) regset->regmap;
1148 (count = map->count) != 0;
1151 int regno = map->regno;
1152 int slot_size = map->size;
1154 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1155 slot_size = m_descr->sizeof_register[regno];
1157 if (regno == REGCACHE_MAP_SKIP
1159 && (regnum < regno || regnum >= regno + count)))
1160 offs += count * slot_size;
1162 else if (regnum == -1)
1163 for (; count--; regno++, offs += slot_size)
1165 if (offs + slot_size > size)
1169 raw_collect (regno, (gdb_byte *) out_buf + offs);
1171 out_regcache->raw_supply (regno, in_buf
1172 ? (const gdb_byte *) in_buf + offs
1177 /* Transfer a single register and return. */
1178 offs += (regnum - regno) * slot_size;
1179 if (offs + slot_size > size)
1183 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1185 out_regcache->raw_supply (regnum, in_buf
1186 ? (const gdb_byte *) in_buf + offs
1193 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1194 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1195 If BUF is NULL, set the register(s) to "unavailable" status. */
1198 regcache_supply_regset (const struct regset *regset,
1199 struct regcache *regcache,
1200 int regnum, const void *buf, size_t size)
1202 regcache->supply_regset (regset, regnum, buf, size);
1206 regcache::supply_regset (const struct regset *regset,
1207 int regnum, const void *buf, size_t size)
1209 transfer_regset (regset, this, regnum, buf, NULL, size);
1212 /* Collect register REGNUM from REGCACHE to BUF, using the register
1213 map in REGSET. If REGNUM is -1, do this for all registers in
1217 regcache_collect_regset (const struct regset *regset,
1218 const struct regcache *regcache,
1219 int regnum, void *buf, size_t size)
1221 regcache->collect_regset (regset, regnum, buf, size);
1225 regcache::collect_regset (const struct regset *regset,
1226 int regnum, void *buf, size_t size) const
1228 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1232 /* Special handling for register PC. */
1235 regcache_read_pc (struct regcache *regcache)
1237 struct gdbarch *gdbarch = regcache->arch ();
1241 if (gdbarch_read_pc_p (gdbarch))
1242 pc_val = gdbarch_read_pc (gdbarch, regcache);
1243 /* Else use per-frame method on get_current_frame. */
1244 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1248 if (regcache_cooked_read_unsigned (regcache,
1249 gdbarch_pc_regnum (gdbarch),
1250 &raw_val) == REG_UNAVAILABLE)
1251 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1253 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1256 internal_error (__FILE__, __LINE__,
1257 _("regcache_read_pc: Unable to find PC"));
1262 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1264 struct gdbarch *gdbarch = regcache->arch ();
1266 if (gdbarch_write_pc_p (gdbarch))
1267 gdbarch_write_pc (gdbarch, regcache, pc);
1268 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1269 regcache_cooked_write_unsigned (regcache,
1270 gdbarch_pc_regnum (gdbarch), pc);
1272 internal_error (__FILE__, __LINE__,
1273 _("regcache_write_pc: Unable to update PC"));
1275 /* Writing the PC (for instance, from "load") invalidates the
1277 reinit_frame_cache ();
1281 regcache::num_raw_registers () const
1283 return gdbarch_num_regs (arch ());
1287 regcache::debug_print_register (const char *func, int regno)
1289 struct gdbarch *gdbarch = arch ();
1291 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1292 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1293 && gdbarch_register_name (gdbarch, regno) != NULL
1294 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1295 fprintf_unfiltered (gdb_stdlog, "(%s)",
1296 gdbarch_register_name (gdbarch, regno));
1298 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1299 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1301 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1302 int size = register_size (gdbarch, regno);
1303 gdb_byte *buf = register_buffer (regno);
1305 fprintf_unfiltered (gdb_stdlog, " = ");
1306 for (int i = 0; i < size; i++)
1308 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1310 if (size <= sizeof (LONGEST))
1312 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1314 fprintf_unfiltered (gdb_stdlog, " %s %s",
1315 core_addr_to_string_nz (val), plongest (val));
1318 fprintf_unfiltered (gdb_stdlog, "\n");
1322 reg_flush_command (char *command, int from_tty)
1324 /* Force-flush the register cache. */
1325 registers_changed ();
1327 printf_filtered (_("Register cache flushed.\n"));
1331 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1333 struct gdbarch *gdbarch = m_descr->gdbarch;
1335 int footnote_nr = 0;
1336 int footnote_register_size = 0;
1337 int footnote_register_offset = 0;
1338 int footnote_register_type_name_null = 0;
1339 long register_offset = 0;
1341 gdb_assert (m_descr->nr_cooked_registers
1342 == (gdbarch_num_regs (gdbarch)
1343 + gdbarch_num_pseudo_regs (gdbarch)));
1345 for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1349 fprintf_unfiltered (file, " %-10s", "Name");
1352 const char *p = gdbarch_register_name (gdbarch, regnum);
1356 else if (p[0] == '\0')
1358 fprintf_unfiltered (file, " %-10s", p);
1363 fprintf_unfiltered (file, " %4s", "Nr");
1365 fprintf_unfiltered (file, " %4d", regnum);
1367 /* Relative number. */
1369 fprintf_unfiltered (file, " %4s", "Rel");
1370 else if (regnum < gdbarch_num_regs (gdbarch))
1371 fprintf_unfiltered (file, " %4d", regnum);
1373 fprintf_unfiltered (file, " %4d",
1374 (regnum - gdbarch_num_regs (gdbarch)));
1378 fprintf_unfiltered (file, " %6s ", "Offset");
1381 fprintf_unfiltered (file, " %6ld",
1382 m_descr->register_offset[regnum]);
1383 if (register_offset != m_descr->register_offset[regnum]
1385 && (m_descr->register_offset[regnum]
1386 != (m_descr->register_offset[regnum - 1]
1387 + m_descr->sizeof_register[regnum - 1])))
1390 if (!footnote_register_offset)
1391 footnote_register_offset = ++footnote_nr;
1392 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1395 fprintf_unfiltered (file, " ");
1396 register_offset = (m_descr->register_offset[regnum]
1397 + m_descr->sizeof_register[regnum]);
1402 fprintf_unfiltered (file, " %5s ", "Size");
1404 fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1409 std::string name_holder;
1415 static const char blt[] = "builtin_type";
1417 t = TYPE_NAME (register_type (arch (), regnum));
1420 if (!footnote_register_type_name_null)
1421 footnote_register_type_name_null = ++footnote_nr;
1422 name_holder = string_printf ("*%d",
1423 footnote_register_type_name_null);
1424 t = name_holder.c_str ();
1426 /* Chop a leading builtin_type. */
1427 if (startswith (t, blt))
1430 fprintf_unfiltered (file, " %-15s", t);
1433 /* Leading space always present. */
1434 fprintf_unfiltered (file, " ");
1437 if (what_to_dump == regcache_dump_raw)
1440 fprintf_unfiltered (file, "Raw value");
1441 else if (regnum >= num_raw_registers ())
1442 fprintf_unfiltered (file, "<cooked>");
1443 else if (get_register_status (regnum) == REG_UNKNOWN)
1444 fprintf_unfiltered (file, "<invalid>");
1445 else if (get_register_status (regnum) == REG_UNAVAILABLE)
1446 fprintf_unfiltered (file, "<unavailable>");
1449 raw_update (regnum);
1450 print_hex_chars (file, register_buffer (regnum),
1451 m_descr->sizeof_register[regnum],
1452 gdbarch_byte_order (gdbarch), true);
1456 /* Value, cooked. */
1457 if (what_to_dump == regcache_dump_cooked)
1460 fprintf_unfiltered (file, "Cooked value");
1463 const gdb_byte *buf = NULL;
1464 enum register_status status;
1465 struct value *value = NULL;
1467 if (regnum < num_raw_registers ())
1469 raw_update (regnum);
1470 status = get_register_status (regnum);
1471 buf = register_buffer (regnum);
1475 value = cooked_read_value (regnum);
1477 if (!value_optimized_out (value)
1478 && value_entirely_available (value))
1481 buf = value_contents_all (value);
1484 status = REG_UNAVAILABLE;
1487 if (status == REG_UNKNOWN)
1488 fprintf_unfiltered (file, "<invalid>");
1489 else if (status == REG_UNAVAILABLE)
1490 fprintf_unfiltered (file, "<unavailable>");
1492 print_hex_chars (file, buf,
1493 m_descr->sizeof_register[regnum],
1494 gdbarch_byte_order (gdbarch), true);
1498 release_value (value);
1504 /* Group members. */
1505 if (what_to_dump == regcache_dump_groups)
1508 fprintf_unfiltered (file, "Groups");
1511 const char *sep = "";
1512 struct reggroup *group;
1514 for (group = reggroup_next (gdbarch, NULL);
1516 group = reggroup_next (gdbarch, group))
1518 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1520 fprintf_unfiltered (file,
1521 "%s%s", sep, reggroup_name (group));
1528 /* Remote packet configuration. */
1529 if (what_to_dump == regcache_dump_remote)
1533 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1535 else if (regnum < num_raw_registers ())
1539 if (remote_register_number_and_offset (arch (), regnum,
1541 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1545 fprintf_unfiltered (file, "\n");
1548 if (footnote_register_size)
1549 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1550 footnote_register_size);
1551 if (footnote_register_offset)
1552 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1553 footnote_register_offset);
1554 if (footnote_register_type_name_null)
1555 fprintf_unfiltered (file,
1556 "*%d: Register type's name NULL.\n",
1557 footnote_register_type_name_null);
1561 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
1563 /* Where to send output. */
1571 if (!file.open (args, "w"))
1572 perror_with_name (_("maintenance print architecture"));
1576 if (target_has_registers)
1577 get_current_regcache ()->dump (out, what_to_dump);
1580 /* For the benefit of "maint print registers" & co when
1581 debugging an executable, allow dumping a regcache even when
1582 there is no thread selected / no registers. */
1583 regcache dummy_regs (target_gdbarch (), nullptr);
1584 dummy_regs.dump (out, what_to_dump);
1589 maintenance_print_registers (const char *args, int from_tty)
1591 regcache_print (args, regcache_dump_none);
1595 maintenance_print_raw_registers (const char *args, int from_tty)
1597 regcache_print (args, regcache_dump_raw);
1601 maintenance_print_cooked_registers (const char *args, int from_tty)
1603 regcache_print (args, regcache_dump_cooked);
1607 maintenance_print_register_groups (const char *args, int from_tty)
1609 regcache_print (args, regcache_dump_groups);
1613 maintenance_print_remote_registers (const char *args, int from_tty)
1615 regcache_print (args, regcache_dump_remote);
1619 #include "selftest.h"
1621 namespace selftests {
1623 class regcache_access : public regcache
1627 /* Return the number of elements in current_regcache. */
1630 current_regcache_size ()
1632 return std::distance (regcache::current_regcache.begin (),
1633 regcache::current_regcache.end ());
1638 current_regcache_test (void)
1640 /* It is empty at the start. */
1641 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1643 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1645 /* Get regcache from ptid1, a new regcache is added to
1646 current_regcache. */
1647 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1651 SELF_CHECK (regcache != NULL);
1652 SELF_CHECK (regcache->ptid () == ptid1);
1653 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1655 /* Get regcache from ptid2, a new regcache is added to
1656 current_regcache. */
1657 regcache = get_thread_arch_aspace_regcache (ptid2,
1660 SELF_CHECK (regcache != NULL);
1661 SELF_CHECK (regcache->ptid () == ptid2);
1662 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1664 /* Get regcache from ptid3, a new regcache is added to
1665 current_regcache. */
1666 regcache = get_thread_arch_aspace_regcache (ptid3,
1669 SELF_CHECK (regcache != NULL);
1670 SELF_CHECK (regcache->ptid () == ptid3);
1671 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1673 /* Get regcache from ptid2 again, nothing is added to
1674 current_regcache. */
1675 regcache = get_thread_arch_aspace_regcache (ptid2,
1678 SELF_CHECK (regcache != NULL);
1679 SELF_CHECK (regcache->ptid () == ptid2);
1680 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1682 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1683 current_regcache. */
1684 registers_changed_ptid (ptid2);
1685 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1688 } // namespace selftests
1689 #endif /* GDB_SELF_TEST */
1692 _initialize_regcache (void)
1694 regcache_descr_handle
1695 = gdbarch_data_register_post_init (init_regcache_descr);
1697 observer_attach_target_changed (regcache_observer_target_changed);
1698 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
1700 add_com ("flushregs", class_maintenance, reg_flush_command,
1701 _("Force gdb to flush its register cache (maintainer command)"));
1703 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1704 _("Print the internal register configuration.\n"
1705 "Takes an optional file parameter."), &maintenanceprintlist);
1706 add_cmd ("raw-registers", class_maintenance,
1707 maintenance_print_raw_registers,
1708 _("Print the internal register configuration "
1709 "including raw values.\n"
1710 "Takes an optional file parameter."), &maintenanceprintlist);
1711 add_cmd ("cooked-registers", class_maintenance,
1712 maintenance_print_cooked_registers,
1713 _("Print the internal register configuration "
1714 "including cooked values.\n"
1715 "Takes an optional file parameter."), &maintenanceprintlist);
1716 add_cmd ("register-groups", class_maintenance,
1717 maintenance_print_register_groups,
1718 _("Print the internal register configuration "
1719 "including each register's group.\n"
1720 "Takes an optional file parameter."),
1721 &maintenanceprintlist);
1722 add_cmd ("remote-registers", class_maintenance,
1723 maintenance_print_remote_registers, _("\
1724 Print the internal register configuration including each register's\n\
1725 remote register number and buffer offset in the g/G packets.\n\
1726 Takes an optional file parameter."),
1727 &maintenanceprintlist);
1730 selftests::register_test ("current_regcache", selftests::current_regcache_test);