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"
35 * Here is the actual register cache.
38 /* Per-architecture object describing the layout of a register cache.
39 Computed once when the architecture is created. */
41 struct gdbarch_data *regcache_descr_handle;
45 /* The architecture this descriptor belongs to. */
46 struct gdbarch *gdbarch;
48 /* The raw register cache. Each raw (or hard) register is supplied
49 by the target interface. The raw cache should not contain
50 redundant information - if the PC is constructed from two
51 registers then those registers and not the PC lives in the raw
54 long sizeof_raw_registers;
55 long sizeof_raw_register_status;
57 /* The cooked register space. Each cooked register in the range
58 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
59 register. The remaining [NR_RAW_REGISTERS
60 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
61 both raw registers and memory by the architecture methods
62 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
63 int nr_cooked_registers;
64 long sizeof_cooked_registers;
65 long sizeof_cooked_register_status;
67 /* Offset and size (in 8 bit bytes), of each register in the
68 register cache. All registers (including those in the range
69 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
71 long *register_offset;
72 long *sizeof_register;
74 /* Cached table containing the type of each register. */
75 struct type **register_type;
79 init_regcache_descr (struct gdbarch *gdbarch)
82 struct regcache_descr *descr;
83 gdb_assert (gdbarch != NULL);
85 /* Create an initial, zero filled, table. */
86 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
87 descr->gdbarch = gdbarch;
89 /* Total size of the register space. The raw registers are mapped
90 directly onto the raw register cache while the pseudo's are
91 either mapped onto raw-registers or memory. */
92 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
93 + gdbarch_num_pseudo_regs (gdbarch);
94 descr->sizeof_cooked_register_status
95 = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
97 /* Fill in a table of register types. */
99 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
101 for (i = 0; i < descr->nr_cooked_registers; i++)
102 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
104 /* Construct a strictly RAW register cache. Don't allow pseudo's
105 into the register cache. */
106 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
107 descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
109 /* Lay out the register cache.
111 NOTE: cagney/2002-05-22: Only register_type() is used when
112 constructing the register cache. It is assumed that the
113 register's raw size, virtual size and type length are all the
119 descr->sizeof_register
120 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
121 descr->register_offset
122 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
123 for (i = 0; i < descr->nr_raw_registers; i++)
125 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
126 descr->register_offset[i] = offset;
127 offset += descr->sizeof_register[i];
128 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
130 /* Set the real size of the raw register cache buffer. */
131 descr->sizeof_raw_registers = offset;
133 for (; i < descr->nr_cooked_registers; i++)
135 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
136 descr->register_offset[i] = offset;
137 offset += descr->sizeof_register[i];
138 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
140 /* Set the real size of the readonly register cache buffer. */
141 descr->sizeof_cooked_registers = offset;
147 static struct regcache_descr *
148 regcache_descr (struct gdbarch *gdbarch)
150 return (struct regcache_descr *) gdbarch_data (gdbarch,
151 regcache_descr_handle);
154 /* Utility functions returning useful register attributes stored in
155 the regcache descr. */
158 register_type (struct gdbarch *gdbarch, int regnum)
160 struct regcache_descr *descr = regcache_descr (gdbarch);
162 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
163 return descr->register_type[regnum];
166 /* Utility functions returning useful register attributes stored in
167 the regcache descr. */
170 register_size (struct gdbarch *gdbarch, int regnum)
172 struct regcache_descr *descr = regcache_descr (gdbarch);
175 gdb_assert (regnum >= 0
176 && regnum < (gdbarch_num_regs (gdbarch)
177 + gdbarch_num_pseudo_regs (gdbarch)));
178 size = descr->sizeof_register[regnum];
182 /* See common/common-regcache.h. */
185 regcache_register_size (const struct regcache *regcache, int n)
187 return register_size (get_regcache_arch (regcache), n);
190 regcache::regcache (gdbarch *gdbarch, address_space *aspace_,
192 : m_aspace (aspace_), m_readonly_p (readonly_p_)
194 gdb_assert (gdbarch != NULL);
195 m_descr = regcache_descr (gdbarch);
199 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
200 m_register_status = XCNEWVEC (signed char,
201 m_descr->sizeof_cooked_register_status);
205 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
206 m_register_status = XCNEWVEC (signed char,
207 m_descr->sizeof_raw_register_status);
209 m_ptid = minus_one_ptid;
213 regcache::arch () const
215 return m_descr->gdbarch;
218 /* See regcache.h. */
221 regcache_get_ptid (const struct regcache *regcache)
223 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
225 return regcache->ptid ();
229 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
231 return new regcache (gdbarch, aspace);
235 regcache_xfree (struct regcache *regcache)
237 if (regcache == NULL)
244 do_regcache_xfree (void *data)
246 regcache_xfree ((struct regcache *) data);
250 make_cleanup_regcache_xfree (struct regcache *regcache)
252 return make_cleanup (do_regcache_xfree, regcache);
255 /* Cleanup routines for invalidating a register. */
257 struct register_to_invalidate
259 struct regcache *regcache;
264 do_regcache_invalidate (void *data)
266 struct register_to_invalidate *reg = (struct register_to_invalidate *) data;
268 regcache_invalidate (reg->regcache, reg->regnum);
271 static struct cleanup *
272 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
274 struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
276 reg->regcache = regcache;
277 reg->regnum = regnum;
278 return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
281 /* Return REGCACHE's architecture. */
284 get_regcache_arch (const struct regcache *regcache)
286 return regcache->arch ();
289 struct address_space *
290 get_regcache_aspace (const struct regcache *regcache)
292 return regcache->aspace ();
295 /* Return a pointer to register REGNUM's buffer cache. */
298 regcache::register_buffer (int regnum) const
300 return m_registers + m_descr->register_offset[regnum];
304 regcache_save (struct regcache *regcache,
305 regcache_cooked_read_ftype *cooked_read, void *src)
307 regcache->save (cooked_read, src);
311 regcache::save (regcache_cooked_read_ftype *cooked_read,
314 struct gdbarch *gdbarch = m_descr->gdbarch;
315 gdb_byte buf[MAX_REGISTER_SIZE];
318 /* The DST should be `read-only', if it wasn't then the save would
319 end up trying to write the register values back out to the
321 gdb_assert (m_readonly_p);
322 /* Clear the dest. */
323 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
324 memset (m_register_status, 0, m_descr->sizeof_cooked_register_status);
325 /* Copy over any registers (identified by their membership in the
326 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
327 gdbarch_num_pseudo_regs) range is checked since some architectures need
328 to save/restore `cooked' registers that live in memory. */
329 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
331 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
333 enum register_status status = cooked_read (src, regnum, buf);
335 if (status == REG_VALID)
336 memcpy (register_buffer (regnum), buf,
337 register_size (gdbarch, regnum));
340 gdb_assert (status != REG_UNKNOWN);
342 memset (register_buffer (regnum), 0,
343 register_size (gdbarch, regnum));
345 m_register_status[regnum] = status;
351 regcache::restore (struct regcache *src)
353 struct gdbarch *gdbarch = m_descr->gdbarch;
356 /* The dst had better not be read-only. If it is, the `restore'
357 doesn't make much sense. */
358 gdb_assert (!m_readonly_p);
359 gdb_assert (src->m_readonly_p);
360 /* Copy over any registers, being careful to only restore those that
361 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
362 + gdbarch_num_pseudo_regs) range is checked since some architectures need
363 to save/restore `cooked' registers that live in memory. */
364 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
366 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
368 if (src->m_register_status[regnum] == REG_VALID)
369 cooked_write (regnum, src->register_buffer (regnum));
374 static enum register_status
375 do_cooked_read (void *src, int regnum, gdb_byte *buf)
377 struct regcache *regcache = (struct regcache *) src;
379 return regcache_cooked_read (regcache, regnum, buf);
383 regcache_cpy (struct regcache *dst, struct regcache *src)
385 gdb_assert (src != NULL && dst != NULL);
386 gdb_assert (src->m_descr->gdbarch == dst->m_descr->gdbarch);
387 gdb_assert (src != dst);
388 gdb_assert (src->m_readonly_p || dst->m_readonly_p);
390 if (!src->m_readonly_p)
391 regcache_save (dst, do_cooked_read, src);
392 else if (!dst->m_readonly_p)
395 dst->cpy_no_passthrough (src);
398 /* Copy/duplicate the contents of a register cache. Unlike regcache_cpy,
399 which is pass-through, this does not go through to the target.
400 Only values values already in the cache are transferred. The SRC and DST
401 buffers must not overlap. */
404 regcache::cpy_no_passthrough (struct regcache *src)
406 gdb_assert (src != NULL);
407 gdb_assert (src->m_descr->gdbarch == m_descr->gdbarch);
408 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
409 move of data into a thread's regcache. Doing this would be silly
410 - it would mean that regcache->register_status would be
411 completely invalid. */
412 gdb_assert (m_readonly_p && src->m_readonly_p);
414 memcpy (m_registers, src->m_registers,
415 m_descr->sizeof_cooked_registers);
416 memcpy (m_register_status, src->m_register_status,
417 m_descr->sizeof_cooked_register_status);
421 regcache_dup (struct regcache *src)
423 struct regcache *newbuf;
425 gdb_assert (!src->m_readonly_p);
426 newbuf = regcache_xmalloc (src->arch (), get_regcache_aspace (src));
427 newbuf->save (do_cooked_read, src);
432 regcache_register_status (const struct regcache *regcache, int regnum)
434 gdb_assert (regcache != NULL);
435 return regcache->get_register_status (regnum);
439 regcache::get_register_status (int regnum) const
441 gdb_assert (regnum >= 0);
443 gdb_assert (regnum < m_descr->nr_cooked_registers);
445 gdb_assert (regnum < m_descr->nr_raw_registers);
447 return (enum register_status) m_register_status[regnum];
451 regcache_invalidate (struct regcache *regcache, int regnum)
453 gdb_assert (regcache != NULL);
454 regcache->invalidate (regnum);
458 regcache::invalidate (int regnum)
460 gdb_assert (regnum >= 0);
461 gdb_assert (!m_readonly_p);
462 gdb_assert (regnum < m_descr->nr_raw_registers);
463 m_register_status[regnum] = REG_UNKNOWN;
466 /* Global structure containing the current regcache. */
468 /* NOTE: this is a write-through cache. There is no "dirty" bit for
469 recording if the register values have been changed (eg. by the
470 user). Therefore all registers must be written back to the
471 target when appropriate. */
475 struct regcache *regcache;
476 struct regcache_list *next;
479 static struct regcache_list *current_regcache;
482 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
483 struct address_space *aspace)
485 struct regcache_list *list;
486 struct regcache *new_regcache;
488 for (list = current_regcache; list; list = list->next)
489 if (ptid_equal (list->regcache->ptid (), ptid)
490 && get_regcache_arch (list->regcache) == gdbarch)
491 return list->regcache;
493 new_regcache = new regcache (gdbarch, aspace, false);
494 new_regcache->set_ptid (ptid);
496 list = XNEW (struct regcache_list);
497 list->regcache = new_regcache;
498 list->next = current_regcache;
499 current_regcache = list;
505 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
507 struct address_space *aspace;
509 /* For the benefit of "maint print registers" & co when debugging an
510 executable, allow dumping the regcache even when there is no
511 thread selected (target_thread_address_space internal-errors if
512 no address space is found). Note that normal user commands will
513 fail higher up on the call stack due to no
514 target_has_registers. */
515 aspace = (ptid_equal (null_ptid, ptid)
517 : target_thread_address_space (ptid));
519 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
522 static ptid_t current_thread_ptid;
523 static struct gdbarch *current_thread_arch;
526 get_thread_regcache (ptid_t ptid)
528 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
530 current_thread_ptid = ptid;
531 current_thread_arch = target_thread_architecture (ptid);
534 return get_thread_arch_regcache (ptid, current_thread_arch);
538 get_current_regcache (void)
540 return get_thread_regcache (inferior_ptid);
543 /* See common/common-regcache.h. */
546 get_thread_regcache_for_ptid (ptid_t ptid)
548 return get_thread_regcache (ptid);
551 /* Observer for the target_changed event. */
554 regcache_observer_target_changed (struct target_ops *target)
556 registers_changed ();
559 /* Update global variables old ptids to hold NEW_PTID if they were
562 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
564 struct regcache_list *list;
566 for (list = current_regcache; list; list = list->next)
567 if (ptid_equal (list->regcache->ptid (), old_ptid))
568 list->regcache->set_ptid (new_ptid);
571 /* Low level examining and depositing of registers.
573 The caller is responsible for making sure that the inferior is
574 stopped before calling the fetching routines, or it will get
575 garbage. (a change from GDB version 3, in which the caller got the
576 value from the last stop). */
578 /* REGISTERS_CHANGED ()
580 Indicate that registers may have changed, so invalidate the cache. */
583 registers_changed_ptid (ptid_t ptid)
585 struct regcache_list *list, **list_link;
587 list = current_regcache;
588 list_link = ¤t_regcache;
591 if (ptid_match (list->regcache->ptid (), ptid))
593 struct regcache_list *dead = list;
595 *list_link = list->next;
596 regcache_xfree (list->regcache);
602 list_link = &list->next;
606 if (ptid_match (current_thread_ptid, ptid))
608 current_thread_ptid = null_ptid;
609 current_thread_arch = NULL;
612 if (ptid_match (inferior_ptid, ptid))
614 /* We just deleted the regcache of the current thread. Need to
615 forget about any frames we have cached, too. */
616 reinit_frame_cache ();
621 registers_changed (void)
623 registers_changed_ptid (minus_one_ptid);
625 /* Force cleanup of any alloca areas if using C alloca instead of
626 a builtin alloca. This particular call is used to clean up
627 areas allocated by low level target code which may build up
628 during lengthy interactions between gdb and the target before
629 gdb gives control to the user (ie watchpoints). */
634 regcache_raw_update (struct regcache *regcache, int regnum)
636 gdb_assert (regcache != NULL);
638 regcache->raw_update (regnum);
642 regcache::raw_update (int regnum)
644 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
646 /* Make certain that the register cache is up-to-date with respect
647 to the current thread. This switching shouldn't be necessary
648 only there is still only one target side register cache. Sigh!
649 On the bright side, at least there is a regcache object. */
651 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
653 target_fetch_registers (this, regnum);
655 /* A number of targets can't access the whole set of raw
656 registers (because the debug API provides no means to get at
658 if (m_register_status[regnum] == REG_UNKNOWN)
659 m_register_status[regnum] = REG_UNAVAILABLE;
664 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
666 return regcache->raw_read (regnum, buf);
670 regcache::raw_read (int regnum, gdb_byte *buf)
672 gdb_assert (buf != NULL);
675 if (m_register_status[regnum] != REG_VALID)
676 memset (buf, 0, m_descr->sizeof_register[regnum]);
678 memcpy (buf, register_buffer (regnum),
679 m_descr->sizeof_register[regnum]);
681 return (enum register_status) m_register_status[regnum];
685 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
687 gdb_assert (regcache != NULL);
688 return regcache->raw_read_signed (regnum, val);
692 regcache::raw_read_signed (int regnum, LONGEST *val)
695 enum register_status status;
697 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
698 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
699 status = raw_read (regnum, buf);
700 if (status == REG_VALID)
701 *val = extract_signed_integer
702 (buf, m_descr->sizeof_register[regnum],
703 gdbarch_byte_order (m_descr->gdbarch));
710 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
713 gdb_assert (regcache != NULL);
714 return regcache->raw_read_unsigned (regnum, val);
719 regcache::raw_read_unsigned (int regnum, ULONGEST *val)
722 enum register_status status;
724 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
725 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
726 status = raw_read (regnum, buf);
727 if (status == REG_VALID)
728 *val = extract_unsigned_integer
729 (buf, m_descr->sizeof_register[regnum],
730 gdbarch_byte_order (m_descr->gdbarch));
737 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
739 gdb_assert (regcache != NULL);
740 regcache->raw_write_signed (regnum, val);
744 regcache::raw_write_signed (int regnum, LONGEST val)
748 gdb_assert (regnum >=0 && regnum < m_descr->nr_raw_registers);
749 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
750 store_signed_integer (buf, m_descr->sizeof_register[regnum],
751 gdbarch_byte_order (m_descr->gdbarch), val);
752 raw_write (regnum, buf);
756 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
759 gdb_assert (regcache != NULL);
760 regcache->raw_write_unsigned (regnum, val);
764 regcache::raw_write_unsigned (int regnum, ULONGEST val)
768 gdb_assert (regnum >=0 && regnum < m_descr->nr_raw_registers);
769 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
770 store_unsigned_integer (buf, m_descr->sizeof_register[regnum],
771 gdbarch_byte_order (m_descr->gdbarch), val);
772 raw_write (regnum, buf);
776 regcache_raw_get_signed (struct regcache *regcache, int regnum)
779 enum register_status status;
781 status = regcache_raw_read_signed (regcache, regnum, &value);
782 if (status == REG_UNAVAILABLE)
783 throw_error (NOT_AVAILABLE_ERROR,
784 _("Register %d is not available"), regnum);
789 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
791 return regcache->cooked_read (regnum, buf);
795 regcache::cooked_read (int regnum, gdb_byte *buf)
797 gdb_assert (regnum >= 0);
798 gdb_assert (regnum < m_descr->nr_cooked_registers);
799 if (regnum < m_descr->nr_raw_registers)
800 return raw_read (regnum, buf);
801 else if (m_readonly_p
802 && m_register_status[regnum] != REG_UNKNOWN)
804 /* Read-only register cache, perhaps the cooked value was
806 if (m_register_status[regnum] == REG_VALID)
807 memcpy (buf, register_buffer (regnum),
808 m_descr->sizeof_register[regnum]);
810 memset (buf, 0, m_descr->sizeof_register[regnum]);
812 return (enum register_status) m_register_status[regnum];
814 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
816 struct value *mark, *computed;
817 enum register_status result = REG_VALID;
819 mark = value_mark ();
821 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
823 if (value_entirely_available (computed))
824 memcpy (buf, value_contents_raw (computed),
825 m_descr->sizeof_register[regnum]);
828 memset (buf, 0, m_descr->sizeof_register[regnum]);
829 result = REG_UNAVAILABLE;
832 value_free_to_mark (mark);
837 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
842 regcache_cooked_read_value (struct regcache *regcache, int regnum)
844 return regcache->cooked_read_value (regnum);
848 regcache::cooked_read_value (int regnum)
850 gdb_assert (regnum >= 0);
851 gdb_assert (regnum < m_descr->nr_cooked_registers);
853 if (regnum < m_descr->nr_raw_registers
854 || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN)
855 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
857 struct value *result;
859 result = allocate_value (register_type (m_descr->gdbarch, regnum));
860 VALUE_LVAL (result) = lval_register;
861 VALUE_REGNUM (result) = regnum;
863 /* It is more efficient in general to do this delegation in this
864 direction than in the other one, even though the value-based
866 if (cooked_read (regnum,
867 value_contents_raw (result)) == REG_UNAVAILABLE)
868 mark_value_bytes_unavailable (result, 0,
869 TYPE_LENGTH (value_type (result)));
874 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
879 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
882 gdb_assert (regcache != NULL);
883 return regcache->cooked_read_signed (regnum, val);
887 regcache::cooked_read_signed (int regnum, LONGEST *val)
889 enum register_status status;
892 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
893 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
894 status = cooked_read (regnum, buf);
895 if (status == REG_VALID)
896 *val = extract_signed_integer
897 (buf, m_descr->sizeof_register[regnum],
898 gdbarch_byte_order (m_descr->gdbarch));
905 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
908 gdb_assert (regcache != NULL);
909 return regcache->cooked_read_unsigned (regnum, val);
913 regcache::cooked_read_unsigned (int regnum, ULONGEST *val)
915 enum register_status status;
918 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
919 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
920 status = cooked_read (regnum, buf);
921 if (status == REG_VALID)
922 *val = extract_unsigned_integer
923 (buf, m_descr->sizeof_register[regnum],
924 gdbarch_byte_order (m_descr->gdbarch));
931 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
934 gdb_assert (regcache != NULL);
935 regcache->cooked_write_signed (regnum, val);
939 regcache::cooked_write_signed (int regnum, LONGEST val)
943 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
944 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
945 store_signed_integer (buf, m_descr->sizeof_register[regnum],
946 gdbarch_byte_order (m_descr->gdbarch), val);
947 cooked_write (regnum, buf);
951 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
954 gdb_assert (regcache != NULL);
955 regcache->cooked_write_unsigned (regnum, val);
959 regcache::cooked_write_unsigned (int regnum, ULONGEST val)
963 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
964 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
965 store_unsigned_integer (buf, m_descr->sizeof_register[regnum],
966 gdbarch_byte_order (m_descr->gdbarch), val);
967 cooked_write (regnum, buf);
970 /* See regcache.h. */
973 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
976 regcache->raw_set_cached_value (regnum, buf);
980 regcache::raw_set_cached_value (int regnum, const gdb_byte *buf)
982 memcpy (register_buffer (regnum), buf,
983 m_descr->sizeof_register[regnum]);
984 m_register_status[regnum] = REG_VALID;
988 regcache_raw_write (struct regcache *regcache, int regnum,
991 gdb_assert (regcache != NULL && buf != NULL);
992 regcache->raw_write (regnum, buf);
996 regcache::raw_write (int regnum, const gdb_byte *buf)
998 struct cleanup *old_chain;
1000 gdb_assert (buf != NULL);
1001 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1002 gdb_assert (!m_readonly_p);
1004 /* On the sparc, writing %g0 is a no-op, so we don't even want to
1005 change the registers array if something writes to this register. */
1006 if (gdbarch_cannot_store_register (arch (), regnum))
1009 /* If we have a valid copy of the register, and new value == old
1010 value, then don't bother doing the actual store. */
1011 if (get_register_status (regnum) == REG_VALID
1012 && (memcmp (register_buffer (regnum), buf,
1013 m_descr->sizeof_register[regnum]) == 0))
1016 target_prepare_to_store (this);
1017 raw_set_cached_value (regnum, buf);
1019 /* Register a cleanup function for invalidating the register after it is
1020 written, in case of a failure. */
1021 old_chain = make_cleanup_regcache_invalidate (this, regnum);
1023 target_store_registers (this, regnum);
1025 /* The target did not throw an error so we can discard invalidating the
1026 register and restore the cleanup chain to what it was. */
1027 discard_cleanups (old_chain);
1031 regcache_cooked_write (struct regcache *regcache, int regnum,
1032 const gdb_byte *buf)
1034 regcache->cooked_write (regnum, buf);
1038 regcache::cooked_write (int regnum, const gdb_byte *buf)
1040 gdb_assert (regnum >= 0);
1041 gdb_assert (regnum < m_descr->nr_cooked_registers);
1042 if (regnum < m_descr->nr_raw_registers)
1043 raw_write (regnum, buf);
1045 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
1049 /* Perform a partial register transfer using a read, modify, write
1052 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
1054 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
1057 enum register_status
1058 regcache::xfer_part (int regnum, int offset, int len, void *in,
1060 enum register_status (*read) (struct regcache *regcache,
1063 void (*write) (struct regcache *regcache, int regnum,
1064 const gdb_byte *buf))
1066 struct gdbarch *gdbarch = arch ();
1067 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
1069 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
1070 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
1071 /* Something to do? */
1072 if (offset + len == 0)
1074 /* Read (when needed) ... */
1077 || offset + len < m_descr->sizeof_register[regnum])
1079 enum register_status status;
1081 gdb_assert (read != NULL);
1082 status = read (this, regnum, reg);
1083 if (status != REG_VALID)
1086 /* ... modify ... */
1088 memcpy (in, reg + offset, len);
1090 memcpy (reg + offset, out, len);
1091 /* ... write (when needed). */
1094 gdb_assert (write != NULL);
1095 write (this, regnum, reg);
1101 enum register_status
1102 regcache_raw_read_part (struct regcache *regcache, int regnum,
1103 int offset, int len, gdb_byte *buf)
1105 return regcache->raw_read_part (regnum, offset, len, buf);
1108 enum register_status
1109 regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
1111 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1112 return xfer_part (regnum, offset, len, buf, NULL,
1113 regcache_raw_read, regcache_raw_write);
1117 regcache_raw_write_part (struct regcache *regcache, int regnum,
1118 int offset, int len, const gdb_byte *buf)
1120 regcache->raw_write_part (regnum, offset, len, buf);
1124 regcache::raw_write_part (int regnum, int offset, int len,
1125 const gdb_byte *buf)
1127 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1128 xfer_part (regnum, offset, len, NULL, buf, regcache_raw_read,
1129 regcache_raw_write);
1132 enum register_status
1133 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1134 int offset, int len, gdb_byte *buf)
1136 return regcache->cooked_read_part (regnum, offset, len, buf);
1140 enum register_status
1141 regcache::cooked_read_part (int regnum, int offset, int len, gdb_byte *buf)
1143 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1144 return xfer_part (regnum, offset, len, buf, NULL,
1145 regcache_cooked_read, regcache_cooked_write);
1149 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1150 int offset, int len, const gdb_byte *buf)
1152 regcache->cooked_write_part (regnum, offset, len, buf);
1156 regcache::cooked_write_part (int regnum, int offset, int len,
1157 const gdb_byte *buf)
1159 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1160 xfer_part (regnum, offset, len, NULL, buf,
1161 regcache_cooked_read, regcache_cooked_write);
1164 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1167 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1169 gdb_assert (regcache != NULL);
1170 regcache->raw_supply (regnum, buf);
1174 regcache::raw_supply (int regnum, const void *buf)
1179 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1180 gdb_assert (!m_readonly_p);
1182 regbuf = register_buffer (regnum);
1183 size = m_descr->sizeof_register[regnum];
1187 memcpy (regbuf, buf, size);
1188 m_register_status[regnum] = REG_VALID;
1192 /* This memset not strictly necessary, but better than garbage
1193 in case the register value manages to escape somewhere (due
1194 to a bug, no less). */
1195 memset (regbuf, 0, size);
1196 m_register_status[regnum] = REG_UNAVAILABLE;
1200 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1203 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1205 gdb_assert (regcache != NULL && buf != NULL);
1206 regcache->raw_collect (regnum, buf);
1210 regcache::raw_collect (int regnum, void *buf) const
1215 gdb_assert (buf != NULL);
1216 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1218 regbuf = register_buffer (regnum);
1219 size = m_descr->sizeof_register[regnum];
1220 memcpy (buf, regbuf, size);
1223 /* Transfer a single or all registers belonging to a certain register
1224 set to or from a buffer. This is the main worker function for
1225 regcache_supply_regset and regcache_collect_regset. */
1228 regcache::transfer_regset (const struct regset *regset,
1229 struct regcache *out_regcache,
1230 int regnum, const void *in_buf,
1231 void *out_buf, size_t size) const
1233 const struct regcache_map_entry *map;
1234 int offs = 0, count;
1236 for (map = (const struct regcache_map_entry *) regset->regmap;
1237 (count = map->count) != 0;
1240 int regno = map->regno;
1241 int slot_size = map->size;
1243 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1244 slot_size = m_descr->sizeof_register[regno];
1246 if (regno == REGCACHE_MAP_SKIP
1248 && (regnum < regno || regnum >= regno + count)))
1249 offs += count * slot_size;
1251 else if (regnum == -1)
1252 for (; count--; regno++, offs += slot_size)
1254 if (offs + slot_size > size)
1258 raw_collect (regno, (gdb_byte *) out_buf + offs);
1260 out_regcache->raw_supply (regno, in_buf
1261 ? (const gdb_byte *) in_buf + offs
1266 /* Transfer a single register and return. */
1267 offs += (regnum - regno) * slot_size;
1268 if (offs + slot_size > size)
1272 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1274 out_regcache->raw_supply (regnum, in_buf
1275 ? (const gdb_byte *) in_buf + offs
1282 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1283 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1284 If BUF is NULL, set the register(s) to "unavailable" status. */
1287 regcache_supply_regset (const struct regset *regset,
1288 struct regcache *regcache,
1289 int regnum, const void *buf, size_t size)
1291 regcache->supply_regset (regset, regnum, buf, size);
1295 regcache::supply_regset (const struct regset *regset,
1296 int regnum, const void *buf, size_t size)
1298 transfer_regset (regset, this, regnum, buf, NULL, size);
1301 /* Collect register REGNUM from REGCACHE to BUF, using the register
1302 map in REGSET. If REGNUM is -1, do this for all registers in
1306 regcache_collect_regset (const struct regset *regset,
1307 const struct regcache *regcache,
1308 int regnum, void *buf, size_t size)
1310 regcache->collect_regset (regset, regnum, buf, size);
1314 regcache::collect_regset (const struct regset *regset,
1315 int regnum, void *buf, size_t size) const
1317 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1321 /* Special handling for register PC. */
1324 regcache_read_pc (struct regcache *regcache)
1326 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1330 if (gdbarch_read_pc_p (gdbarch))
1331 pc_val = gdbarch_read_pc (gdbarch, regcache);
1332 /* Else use per-frame method on get_current_frame. */
1333 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1337 if (regcache_cooked_read_unsigned (regcache,
1338 gdbarch_pc_regnum (gdbarch),
1339 &raw_val) == REG_UNAVAILABLE)
1340 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1342 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1345 internal_error (__FILE__, __LINE__,
1346 _("regcache_read_pc: Unable to find PC"));
1351 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1353 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1355 if (gdbarch_write_pc_p (gdbarch))
1356 gdbarch_write_pc (gdbarch, regcache, pc);
1357 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1358 regcache_cooked_write_unsigned (regcache,
1359 gdbarch_pc_regnum (gdbarch), pc);
1361 internal_error (__FILE__, __LINE__,
1362 _("regcache_write_pc: Unable to update PC"));
1364 /* Writing the PC (for instance, from "load") invalidates the
1366 reinit_frame_cache ();
1370 regcache::debug_print_register (const char *func, int regno)
1372 struct gdbarch *gdbarch = arch ();
1374 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1375 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1376 && gdbarch_register_name (gdbarch, regno) != NULL
1377 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1378 fprintf_unfiltered (gdb_stdlog, "(%s)",
1379 gdbarch_register_name (gdbarch, regno));
1381 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1382 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1384 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1385 int size = register_size (gdbarch, regno);
1386 gdb_byte *buf = register_buffer (regno);
1388 fprintf_unfiltered (gdb_stdlog, " = ");
1389 for (int i = 0; i < size; i++)
1391 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1393 if (size <= sizeof (LONGEST))
1395 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1397 fprintf_unfiltered (gdb_stdlog, " %s %s",
1398 core_addr_to_string_nz (val), plongest (val));
1401 fprintf_unfiltered (gdb_stdlog, "\n");
1405 reg_flush_command (char *command, int from_tty)
1407 /* Force-flush the register cache. */
1408 registers_changed ();
1410 printf_filtered (_("Register cache flushed.\n"));
1414 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1416 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1417 struct gdbarch *gdbarch = m_descr->gdbarch;
1419 int footnote_nr = 0;
1420 int footnote_register_size = 0;
1421 int footnote_register_offset = 0;
1422 int footnote_register_type_name_null = 0;
1423 long register_offset = 0;
1424 gdb_byte buf[MAX_REGISTER_SIZE];
1427 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1428 m_descr->nr_raw_registers);
1429 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1430 m_descr->nr_cooked_registers);
1431 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1432 m_descr->sizeof_raw_registers);
1433 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1434 m_descr->sizeof_raw_register_status);
1435 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1436 gdbarch_num_regs (gdbarch));
1437 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1438 gdbarch_num_pseudo_regs (gdbarch));
1441 gdb_assert (m_descr->nr_cooked_registers
1442 == (gdbarch_num_regs (gdbarch)
1443 + gdbarch_num_pseudo_regs (gdbarch)));
1445 for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1449 fprintf_unfiltered (file, " %-10s", "Name");
1452 const char *p = gdbarch_register_name (gdbarch, regnum);
1456 else if (p[0] == '\0')
1458 fprintf_unfiltered (file, " %-10s", p);
1463 fprintf_unfiltered (file, " %4s", "Nr");
1465 fprintf_unfiltered (file, " %4d", regnum);
1467 /* Relative number. */
1469 fprintf_unfiltered (file, " %4s", "Rel");
1470 else if (regnum < gdbarch_num_regs (gdbarch))
1471 fprintf_unfiltered (file, " %4d", regnum);
1473 fprintf_unfiltered (file, " %4d",
1474 (regnum - gdbarch_num_regs (gdbarch)));
1478 fprintf_unfiltered (file, " %6s ", "Offset");
1481 fprintf_unfiltered (file, " %6ld",
1482 m_descr->register_offset[regnum]);
1483 if (register_offset != m_descr->register_offset[regnum]
1485 && (m_descr->register_offset[regnum]
1486 != (m_descr->register_offset[regnum - 1]
1487 + m_descr->sizeof_register[regnum - 1])))
1490 if (!footnote_register_offset)
1491 footnote_register_offset = ++footnote_nr;
1492 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1495 fprintf_unfiltered (file, " ");
1496 register_offset = (m_descr->register_offset[regnum]
1497 + m_descr->sizeof_register[regnum]);
1502 fprintf_unfiltered (file, " %5s ", "Size");
1504 fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1514 static const char blt[] = "builtin_type";
1516 t = TYPE_NAME (register_type (arch (), regnum));
1521 if (!footnote_register_type_name_null)
1522 footnote_register_type_name_null = ++footnote_nr;
1523 n = xstrprintf ("*%d", footnote_register_type_name_null);
1524 make_cleanup (xfree, n);
1527 /* Chop a leading builtin_type. */
1528 if (startswith (t, blt))
1531 fprintf_unfiltered (file, " %-15s", t);
1534 /* Leading space always present. */
1535 fprintf_unfiltered (file, " ");
1538 if (what_to_dump == regcache_dump_raw)
1541 fprintf_unfiltered (file, "Raw value");
1542 else if (regnum >= m_descr->nr_raw_registers)
1543 fprintf_unfiltered (file, "<cooked>");
1544 else if (get_register_status (regnum) == REG_UNKNOWN)
1545 fprintf_unfiltered (file, "<invalid>");
1546 else if (get_register_status (regnum) == REG_UNAVAILABLE)
1547 fprintf_unfiltered (file, "<unavailable>");
1550 raw_read (regnum, buf);
1551 print_hex_chars (file, buf,
1552 m_descr->sizeof_register[regnum],
1553 gdbarch_byte_order (gdbarch));
1557 /* Value, cooked. */
1558 if (what_to_dump == regcache_dump_cooked)
1561 fprintf_unfiltered (file, "Cooked value");
1564 enum register_status status;
1566 status = cooked_read (regnum, buf);
1567 if (status == REG_UNKNOWN)
1568 fprintf_unfiltered (file, "<invalid>");
1569 else if (status == REG_UNAVAILABLE)
1570 fprintf_unfiltered (file, "<unavailable>");
1572 print_hex_chars (file, buf,
1573 m_descr->sizeof_register[regnum],
1574 gdbarch_byte_order (gdbarch));
1578 /* Group members. */
1579 if (what_to_dump == regcache_dump_groups)
1582 fprintf_unfiltered (file, "Groups");
1585 const char *sep = "";
1586 struct reggroup *group;
1588 for (group = reggroup_next (gdbarch, NULL);
1590 group = reggroup_next (gdbarch, group))
1592 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1594 fprintf_unfiltered (file,
1595 "%s%s", sep, reggroup_name (group));
1602 /* Remote packet configuration. */
1603 if (what_to_dump == regcache_dump_remote)
1607 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1609 else if (regnum < m_descr->nr_raw_registers)
1613 if (remote_register_number_and_offset (arch (), regnum,
1615 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1619 fprintf_unfiltered (file, "\n");
1622 if (footnote_register_size)
1623 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1624 footnote_register_size);
1625 if (footnote_register_offset)
1626 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1627 footnote_register_offset);
1628 if (footnote_register_type_name_null)
1629 fprintf_unfiltered (file,
1630 "*%d: Register type's name NULL.\n",
1631 footnote_register_type_name_null);
1632 do_cleanups (cleanups);
1636 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1639 get_current_regcache ()->dump (gdb_stdout, what_to_dump);
1644 if (!file.open (args, "w"))
1645 perror_with_name (_("maintenance print architecture"));
1646 get_current_regcache ()->dump (&file, what_to_dump);
1651 maintenance_print_registers (char *args, int from_tty)
1653 regcache_print (args, regcache_dump_none);
1657 maintenance_print_raw_registers (char *args, int from_tty)
1659 regcache_print (args, regcache_dump_raw);
1663 maintenance_print_cooked_registers (char *args, int from_tty)
1665 regcache_print (args, regcache_dump_cooked);
1669 maintenance_print_register_groups (char *args, int from_tty)
1671 regcache_print (args, regcache_dump_groups);
1675 maintenance_print_remote_registers (char *args, int from_tty)
1677 regcache_print (args, regcache_dump_remote);
1680 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1683 _initialize_regcache (void)
1685 regcache_descr_handle
1686 = gdbarch_data_register_post_init (init_regcache_descr);
1688 observer_attach_target_changed (regcache_observer_target_changed);
1689 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1691 add_com ("flushregs", class_maintenance, reg_flush_command,
1692 _("Force gdb to flush its register cache (maintainer command)"));
1694 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1695 _("Print the internal register configuration.\n"
1696 "Takes an optional file parameter."), &maintenanceprintlist);
1697 add_cmd ("raw-registers", class_maintenance,
1698 maintenance_print_raw_registers,
1699 _("Print the internal register configuration "
1700 "including raw values.\n"
1701 "Takes an optional file parameter."), &maintenanceprintlist);
1702 add_cmd ("cooked-registers", class_maintenance,
1703 maintenance_print_cooked_registers,
1704 _("Print the internal register configuration "
1705 "including cooked values.\n"
1706 "Takes an optional file parameter."), &maintenanceprintlist);
1707 add_cmd ("register-groups", class_maintenance,
1708 maintenance_print_register_groups,
1709 _("Print the internal register configuration "
1710 "including each register's group.\n"
1711 "Takes an optional file parameter."),
1712 &maintenanceprintlist);
1713 add_cmd ("remote-registers", class_maintenance,
1714 maintenance_print_remote_registers, _("\
1715 Print the internal register configuration including each register's\n\
1716 remote register number and buffer offset in the g/G packets.\n\
1717 Takes an optional file parameter."),
1718 &maintenanceprintlist);