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 /* The register cache for storing raw register values. */
194 struct regcache_descr *descr;
196 /* The address space of this register cache (for registers where it
197 makes sense, like PC or SP). */
198 struct address_space *aspace;
200 /* The register buffers. A read-only register cache can hold the
201 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
202 register cache can only hold [0 .. gdbarch_num_regs). */
204 /* Register cache status. */
205 signed char *register_status;
206 /* Is this a read-only cache? A read-only cache is used for saving
207 the target's register state (e.g, across an inferior function
208 call or just before forcing a function return). A read-only
209 cache can only be updated via the methods regcache_dup() and
210 regcache_cpy(). The actual contents are determined by the
211 reggroup_save and reggroup_restore methods. */
213 /* If this is a read-write cache, which thread's registers is
218 static struct regcache *
219 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
222 struct regcache_descr *descr;
223 struct regcache *regcache;
225 gdb_assert (gdbarch != NULL);
226 descr = regcache_descr (gdbarch);
227 regcache = XNEW (struct regcache);
228 regcache->descr = descr;
229 regcache->readonly_p = readonly_p;
233 = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
234 regcache->register_status
235 = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
240 = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
241 regcache->register_status
242 = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
244 regcache->aspace = aspace;
245 regcache->ptid = minus_one_ptid;
250 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
252 return regcache_xmalloc_1 (gdbarch, aspace, 1);
256 regcache_xfree (struct regcache *regcache)
258 if (regcache == NULL)
260 xfree (regcache->registers);
261 xfree (regcache->register_status);
266 do_regcache_xfree (void *data)
268 regcache_xfree ((struct regcache *) data);
272 make_cleanup_regcache_xfree (struct regcache *regcache)
274 return make_cleanup (do_regcache_xfree, regcache);
277 /* Cleanup routines for invalidating a register. */
279 struct register_to_invalidate
281 struct regcache *regcache;
286 do_regcache_invalidate (void *data)
288 struct register_to_invalidate *reg = (struct register_to_invalidate *) data;
290 regcache_invalidate (reg->regcache, reg->regnum);
293 static struct cleanup *
294 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
296 struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
298 reg->regcache = regcache;
299 reg->regnum = regnum;
300 return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
303 /* Return REGCACHE's architecture. */
306 get_regcache_arch (const struct regcache *regcache)
308 return regcache->descr->gdbarch;
311 struct address_space *
312 get_regcache_aspace (const struct regcache *regcache)
314 return regcache->aspace;
317 /* Return a pointer to register REGNUM's buffer cache. */
320 register_buffer (const struct regcache *regcache, int regnum)
322 return regcache->registers + regcache->descr->register_offset[regnum];
326 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
329 struct gdbarch *gdbarch = dst->descr->gdbarch;
330 gdb_byte buf[MAX_REGISTER_SIZE];
333 /* The DST should be `read-only', if it wasn't then the save would
334 end up trying to write the register values back out to the
336 gdb_assert (dst->readonly_p);
337 /* Clear the dest. */
338 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
339 memset (dst->register_status, 0,
340 dst->descr->sizeof_cooked_register_status);
341 /* Copy over any registers (identified by their membership in the
342 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
343 gdbarch_num_pseudo_regs) range is checked since some architectures need
344 to save/restore `cooked' registers that live in memory. */
345 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
347 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
349 enum register_status status = cooked_read (src, regnum, buf);
351 if (status == REG_VALID)
352 memcpy (register_buffer (dst, regnum), buf,
353 register_size (gdbarch, regnum));
356 gdb_assert (status != REG_UNKNOWN);
358 memset (register_buffer (dst, regnum), 0,
359 register_size (gdbarch, regnum));
361 dst->register_status[regnum] = status;
367 regcache_restore (struct regcache *dst,
368 regcache_cooked_read_ftype *cooked_read,
369 void *cooked_read_context)
371 struct gdbarch *gdbarch = dst->descr->gdbarch;
372 gdb_byte buf[MAX_REGISTER_SIZE];
375 /* The dst had better not be read-only. If it is, the `restore'
376 doesn't make much sense. */
377 gdb_assert (!dst->readonly_p);
378 /* Copy over any registers, being careful to only restore those that
379 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
380 + gdbarch_num_pseudo_regs) range is checked since some architectures need
381 to save/restore `cooked' registers that live in memory. */
382 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
384 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
386 enum register_status status;
388 status = cooked_read (cooked_read_context, regnum, buf);
389 if (status == REG_VALID)
390 regcache_cooked_write (dst, regnum, buf);
395 static enum register_status
396 do_cooked_read (void *src, int regnum, gdb_byte *buf)
398 struct regcache *regcache = (struct regcache *) src;
400 return regcache_cooked_read (regcache, regnum, buf);
403 static void regcache_cpy_no_passthrough (struct regcache *dst,
404 struct regcache *src);
407 regcache_cpy (struct regcache *dst, struct regcache *src)
409 gdb_assert (src != NULL && dst != NULL);
410 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
411 gdb_assert (src != dst);
412 gdb_assert (src->readonly_p || dst->readonly_p);
414 if (!src->readonly_p)
415 regcache_save (dst, do_cooked_read, src);
416 else if (!dst->readonly_p)
417 regcache_restore (dst, do_cooked_read, src);
419 regcache_cpy_no_passthrough (dst, src);
422 /* Copy/duplicate the contents of a register cache. Unlike regcache_cpy,
423 which is pass-through, this does not go through to the target.
424 Only values values already in the cache are transferred. The SRC and DST
425 buffers must not overlap. */
428 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
430 gdb_assert (src != NULL && dst != NULL);
431 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
432 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
433 move of data into a thread's regcache. Doing this would be silly
434 - it would mean that regcache->register_status would be
435 completely invalid. */
436 gdb_assert (dst->readonly_p && src->readonly_p);
438 memcpy (dst->registers, src->registers,
439 dst->descr->sizeof_cooked_registers);
440 memcpy (dst->register_status, src->register_status,
441 dst->descr->sizeof_cooked_register_status);
445 regcache_dup (struct regcache *src)
447 struct regcache *newbuf;
449 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
450 regcache_cpy (newbuf, src);
455 regcache_register_status (const struct regcache *regcache, int regnum)
457 gdb_assert (regcache != NULL);
458 gdb_assert (regnum >= 0);
459 if (regcache->readonly_p)
460 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
462 gdb_assert (regnum < regcache->descr->nr_raw_registers);
464 return (enum register_status) regcache->register_status[regnum];
468 regcache_invalidate (struct regcache *regcache, int regnum)
470 gdb_assert (regcache != NULL);
471 gdb_assert (regnum >= 0);
472 gdb_assert (!regcache->readonly_p);
473 gdb_assert (regnum < regcache->descr->nr_raw_registers);
474 regcache->register_status[regnum] = REG_UNKNOWN;
478 /* Global structure containing the current regcache. */
480 /* NOTE: this is a write-through cache. There is no "dirty" bit for
481 recording if the register values have been changed (eg. by the
482 user). Therefore all registers must be written back to the
483 target when appropriate. */
487 struct regcache *regcache;
488 struct regcache_list *next;
491 static struct regcache_list *current_regcache;
494 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
495 struct address_space *aspace)
497 struct regcache_list *list;
498 struct regcache *new_regcache;
500 for (list = current_regcache; list; list = list->next)
501 if (ptid_equal (list->regcache->ptid, ptid)
502 && get_regcache_arch (list->regcache) == gdbarch)
503 return list->regcache;
505 new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
506 new_regcache->ptid = ptid;
508 list = XNEW (struct regcache_list);
509 list->regcache = new_regcache;
510 list->next = current_regcache;
511 current_regcache = list;
517 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
519 struct address_space *aspace;
521 /* For the benefit of "maint print registers" & co when debugging an
522 executable, allow dumping the regcache even when there is no
523 thread selected (target_thread_address_space internal-errors if
524 no address space is found). Note that normal user commands will
525 fail higher up on the call stack due to no
526 target_has_registers. */
527 aspace = (ptid_equal (null_ptid, ptid)
529 : target_thread_address_space (ptid));
531 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
534 static ptid_t current_thread_ptid;
535 static struct gdbarch *current_thread_arch;
538 get_thread_regcache (ptid_t ptid)
540 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
542 current_thread_ptid = ptid;
543 current_thread_arch = target_thread_architecture (ptid);
546 return get_thread_arch_regcache (ptid, current_thread_arch);
550 get_current_regcache (void)
552 return get_thread_regcache (inferior_ptid);
555 /* See common/common-regcache.h. */
558 get_thread_regcache_for_ptid (ptid_t ptid)
560 return get_thread_regcache (ptid);
563 /* Observer for the target_changed event. */
566 regcache_observer_target_changed (struct target_ops *target)
568 registers_changed ();
571 /* Update global variables old ptids to hold NEW_PTID if they were
574 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
576 struct regcache_list *list;
578 for (list = current_regcache; list; list = list->next)
579 if (ptid_equal (list->regcache->ptid, old_ptid))
580 list->regcache->ptid = new_ptid;
583 /* Low level examining and depositing of registers.
585 The caller is responsible for making sure that the inferior is
586 stopped before calling the fetching routines, or it will get
587 garbage. (a change from GDB version 3, in which the caller got the
588 value from the last stop). */
590 /* REGISTERS_CHANGED ()
592 Indicate that registers may have changed, so invalidate the cache. */
595 registers_changed_ptid (ptid_t ptid)
597 struct regcache_list *list, **list_link;
599 list = current_regcache;
600 list_link = ¤t_regcache;
603 if (ptid_match (list->regcache->ptid, ptid))
605 struct regcache_list *dead = list;
607 *list_link = list->next;
608 regcache_xfree (list->regcache);
614 list_link = &list->next;
618 if (ptid_match (current_thread_ptid, ptid))
620 current_thread_ptid = null_ptid;
621 current_thread_arch = NULL;
624 if (ptid_match (inferior_ptid, ptid))
626 /* We just deleted the regcache of the current thread. Need to
627 forget about any frames we have cached, too. */
628 reinit_frame_cache ();
633 registers_changed (void)
635 registers_changed_ptid (minus_one_ptid);
637 /* Force cleanup of any alloca areas if using C alloca instead of
638 a builtin alloca. This particular call is used to clean up
639 areas allocated by low level target code which may build up
640 during lengthy interactions between gdb and the target before
641 gdb gives control to the user (ie watchpoints). */
646 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
648 gdb_assert (regcache != NULL && buf != NULL);
649 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
650 /* Make certain that the register cache is up-to-date with respect
651 to the current thread. This switching shouldn't be necessary
652 only there is still only one target side register cache. Sigh!
653 On the bright side, at least there is a regcache object. */
654 if (!regcache->readonly_p
655 && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
657 struct cleanup *old_chain = save_inferior_ptid ();
659 inferior_ptid = regcache->ptid;
660 target_fetch_registers (regcache, regnum);
661 do_cleanups (old_chain);
663 /* A number of targets can't access the whole set of raw
664 registers (because the debug API provides no means to get at
666 if (regcache->register_status[regnum] == REG_UNKNOWN)
667 regcache->register_status[regnum] = REG_UNAVAILABLE;
670 if (regcache->register_status[regnum] != REG_VALID)
671 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
673 memcpy (buf, register_buffer (regcache, regnum),
674 regcache->descr->sizeof_register[regnum]);
676 return (enum register_status) regcache->register_status[regnum];
680 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
683 enum register_status status;
685 gdb_assert (regcache != NULL);
686 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
687 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
688 status = regcache_raw_read (regcache, regnum, buf);
689 if (status == REG_VALID)
690 *val = extract_signed_integer
691 (buf, regcache->descr->sizeof_register[regnum],
692 gdbarch_byte_order (regcache->descr->gdbarch));
699 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
703 enum register_status status;
705 gdb_assert (regcache != NULL);
706 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
707 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
708 status = regcache_raw_read (regcache, regnum, buf);
709 if (status == REG_VALID)
710 *val = extract_unsigned_integer
711 (buf, regcache->descr->sizeof_register[regnum],
712 gdbarch_byte_order (regcache->descr->gdbarch));
719 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
723 gdb_assert (regcache != NULL);
724 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
725 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
726 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
727 gdbarch_byte_order (regcache->descr->gdbarch), val);
728 regcache_raw_write (regcache, regnum, buf);
732 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
737 gdb_assert (regcache != NULL);
738 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
739 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
740 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
741 gdbarch_byte_order (regcache->descr->gdbarch), val);
742 regcache_raw_write (regcache, regnum, buf);
746 regcache_raw_get_signed (struct regcache *regcache, int regnum)
749 enum register_status status;
751 status = regcache_raw_read_signed (regcache, regnum, &value);
752 if (status == REG_UNAVAILABLE)
753 throw_error (NOT_AVAILABLE_ERROR,
754 _("Register %d is not available"), regnum);
759 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
761 gdb_assert (regnum >= 0);
762 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
763 if (regnum < regcache->descr->nr_raw_registers)
764 return regcache_raw_read (regcache, regnum, buf);
765 else if (regcache->readonly_p
766 && regcache->register_status[regnum] != REG_UNKNOWN)
768 /* Read-only register cache, perhaps the cooked value was
770 if (regcache->register_status[regnum] == REG_VALID)
771 memcpy (buf, register_buffer (regcache, regnum),
772 regcache->descr->sizeof_register[regnum]);
774 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
776 return (enum register_status) regcache->register_status[regnum];
778 else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
780 struct value *mark, *computed;
781 enum register_status result = REG_VALID;
783 mark = value_mark ();
785 computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
787 if (value_entirely_available (computed))
788 memcpy (buf, value_contents_raw (computed),
789 regcache->descr->sizeof_register[regnum]);
792 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
793 result = REG_UNAVAILABLE;
796 value_free_to_mark (mark);
801 return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
806 regcache_cooked_read_value (struct regcache *regcache, int regnum)
808 gdb_assert (regnum >= 0);
809 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
811 if (regnum < regcache->descr->nr_raw_registers
812 || (regcache->readonly_p
813 && regcache->register_status[regnum] != REG_UNKNOWN)
814 || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
816 struct value *result;
818 result = allocate_value (register_type (regcache->descr->gdbarch,
820 VALUE_LVAL (result) = lval_register;
821 VALUE_REGNUM (result) = regnum;
823 /* It is more efficient in general to do this delegation in this
824 direction than in the other one, even though the value-based
826 if (regcache_cooked_read (regcache, regnum,
827 value_contents_raw (result)) == REG_UNAVAILABLE)
828 mark_value_bytes_unavailable (result, 0,
829 TYPE_LENGTH (value_type (result)));
834 return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
839 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
842 enum register_status status;
845 gdb_assert (regcache != NULL);
846 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
847 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
848 status = regcache_cooked_read (regcache, regnum, buf);
849 if (status == REG_VALID)
850 *val = extract_signed_integer
851 (buf, regcache->descr->sizeof_register[regnum],
852 gdbarch_byte_order (regcache->descr->gdbarch));
859 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
862 enum register_status status;
865 gdb_assert (regcache != NULL);
866 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
867 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
868 status = regcache_cooked_read (regcache, regnum, buf);
869 if (status == REG_VALID)
870 *val = extract_unsigned_integer
871 (buf, regcache->descr->sizeof_register[regnum],
872 gdbarch_byte_order (regcache->descr->gdbarch));
879 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
884 gdb_assert (regcache != NULL);
885 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
886 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
887 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
888 gdbarch_byte_order (regcache->descr->gdbarch), val);
889 regcache_cooked_write (regcache, regnum, buf);
893 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
898 gdb_assert (regcache != NULL);
899 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
900 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
901 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
902 gdbarch_byte_order (regcache->descr->gdbarch), val);
903 regcache_cooked_write (regcache, regnum, buf);
906 /* See regcache.h. */
909 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
912 memcpy (register_buffer (regcache, regnum), buf,
913 regcache->descr->sizeof_register[regnum]);
914 regcache->register_status[regnum] = REG_VALID;
918 regcache_raw_write (struct regcache *regcache, int regnum,
921 struct cleanup *chain_before_save_inferior;
922 struct cleanup *chain_before_invalidate_register;
924 gdb_assert (regcache != NULL && buf != NULL);
925 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
926 gdb_assert (!regcache->readonly_p);
928 /* On the sparc, writing %g0 is a no-op, so we don't even want to
929 change the registers array if something writes to this register. */
930 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
933 /* If we have a valid copy of the register, and new value == old
934 value, then don't bother doing the actual store. */
935 if (regcache_register_status (regcache, regnum) == REG_VALID
936 && (memcmp (register_buffer (regcache, regnum), buf,
937 regcache->descr->sizeof_register[regnum]) == 0))
940 chain_before_save_inferior = save_inferior_ptid ();
941 inferior_ptid = regcache->ptid;
943 target_prepare_to_store (regcache);
944 regcache_raw_set_cached_value (regcache, regnum, buf);
946 /* Register a cleanup function for invalidating the register after it is
947 written, in case of a failure. */
948 chain_before_invalidate_register
949 = make_cleanup_regcache_invalidate (regcache, regnum);
951 target_store_registers (regcache, regnum);
953 /* The target did not throw an error so we can discard invalidating the
954 register and restore the cleanup chain to what it was. */
955 discard_cleanups (chain_before_invalidate_register);
957 do_cleanups (chain_before_save_inferior);
961 regcache_cooked_write (struct regcache *regcache, int regnum,
964 gdb_assert (regnum >= 0);
965 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
966 if (regnum < regcache->descr->nr_raw_registers)
967 regcache_raw_write (regcache, regnum, buf);
969 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
973 /* Perform a partial register transfer using a read, modify, write
976 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
978 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
981 static enum register_status
982 regcache_xfer_part (struct regcache *regcache, int regnum,
983 int offset, int len, void *in, const void *out,
984 enum register_status (*read) (struct regcache *regcache,
987 void (*write) (struct regcache *regcache, int regnum,
988 const gdb_byte *buf))
990 struct regcache_descr *descr = regcache->descr;
991 struct gdbarch *gdbarch = get_regcache_arch (regcache);
992 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
994 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
995 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
996 /* Something to do? */
997 if (offset + len == 0)
999 /* Read (when needed) ... */
1002 || offset + len < descr->sizeof_register[regnum])
1004 enum register_status status;
1006 gdb_assert (read != NULL);
1007 status = read (regcache, regnum, reg);
1008 if (status != REG_VALID)
1011 /* ... modify ... */
1013 memcpy (in, reg + offset, len);
1015 memcpy (reg + offset, out, len);
1016 /* ... write (when needed). */
1019 gdb_assert (write != NULL);
1020 write (regcache, regnum, reg);
1026 enum register_status
1027 regcache_raw_read_part (struct regcache *regcache, int regnum,
1028 int offset, int len, gdb_byte *buf)
1030 struct regcache_descr *descr = regcache->descr;
1032 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1033 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1034 regcache_raw_read, regcache_raw_write);
1038 regcache_raw_write_part (struct regcache *regcache, int regnum,
1039 int offset, int len, const gdb_byte *buf)
1041 struct regcache_descr *descr = regcache->descr;
1043 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1044 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1045 regcache_raw_read, regcache_raw_write);
1048 enum register_status
1049 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1050 int offset, int len, gdb_byte *buf)
1052 struct regcache_descr *descr = regcache->descr;
1054 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1055 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1056 regcache_cooked_read, regcache_cooked_write);
1060 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1061 int offset, int len, const gdb_byte *buf)
1063 struct regcache_descr *descr = regcache->descr;
1065 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1066 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1067 regcache_cooked_read, regcache_cooked_write);
1070 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1073 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1078 gdb_assert (regcache != NULL);
1079 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1080 gdb_assert (!regcache->readonly_p);
1082 regbuf = register_buffer (regcache, regnum);
1083 size = regcache->descr->sizeof_register[regnum];
1087 memcpy (regbuf, buf, size);
1088 regcache->register_status[regnum] = REG_VALID;
1092 /* This memset not strictly necessary, but better than garbage
1093 in case the register value manages to escape somewhere (due
1094 to a bug, no less). */
1095 memset (regbuf, 0, size);
1096 regcache->register_status[regnum] = REG_UNAVAILABLE;
1100 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1103 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1108 gdb_assert (regcache != NULL && buf != NULL);
1109 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1111 regbuf = register_buffer (regcache, regnum);
1112 size = regcache->descr->sizeof_register[regnum];
1113 memcpy (buf, regbuf, size);
1116 /* Transfer a single or all registers belonging to a certain register
1117 set to or from a buffer. This is the main worker function for
1118 regcache_supply_regset and regcache_collect_regset. */
1121 regcache_transfer_regset (const struct regset *regset,
1122 const struct regcache *regcache,
1123 struct regcache *out_regcache,
1124 int regnum, const void *in_buf,
1125 void *out_buf, size_t size)
1127 const struct regcache_map_entry *map;
1128 int offs = 0, count;
1130 for (map = (const struct regcache_map_entry *) regset->regmap;
1131 (count = map->count) != 0;
1134 int regno = map->regno;
1135 int slot_size = map->size;
1137 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1138 slot_size = regcache->descr->sizeof_register[regno];
1140 if (regno == REGCACHE_MAP_SKIP
1142 && (regnum < regno || regnum >= regno + count)))
1143 offs += count * slot_size;
1145 else if (regnum == -1)
1146 for (; count--; regno++, offs += slot_size)
1148 if (offs + slot_size > size)
1152 regcache_raw_collect (regcache, regno,
1153 (gdb_byte *) out_buf + offs);
1155 regcache_raw_supply (out_regcache, regno, in_buf
1156 ? (const gdb_byte *) in_buf + offs
1161 /* Transfer a single register and return. */
1162 offs += (regnum - regno) * slot_size;
1163 if (offs + slot_size > size)
1167 regcache_raw_collect (regcache, regnum,
1168 (gdb_byte *) out_buf + offs);
1170 regcache_raw_supply (out_regcache, regnum, in_buf
1171 ? (const gdb_byte *) in_buf + offs
1178 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1179 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1180 If BUF is NULL, set the register(s) to "unavailable" status. */
1183 regcache_supply_regset (const struct regset *regset,
1184 struct regcache *regcache,
1185 int regnum, const void *buf, size_t size)
1187 regcache_transfer_regset (regset, regcache, regcache, regnum,
1191 /* Collect register REGNUM from REGCACHE to BUF, using the register
1192 map in REGSET. If REGNUM is -1, do this for all registers in
1196 regcache_collect_regset (const struct regset *regset,
1197 const struct regcache *regcache,
1198 int regnum, void *buf, size_t size)
1200 regcache_transfer_regset (regset, regcache, NULL, regnum,
1205 /* Special handling for register PC. */
1208 regcache_read_pc (struct regcache *regcache)
1210 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1214 if (gdbarch_read_pc_p (gdbarch))
1215 pc_val = gdbarch_read_pc (gdbarch, regcache);
1216 /* Else use per-frame method on get_current_frame. */
1217 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1221 if (regcache_cooked_read_unsigned (regcache,
1222 gdbarch_pc_regnum (gdbarch),
1223 &raw_val) == REG_UNAVAILABLE)
1224 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1226 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1229 internal_error (__FILE__, __LINE__,
1230 _("regcache_read_pc: Unable to find PC"));
1235 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1237 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1239 if (gdbarch_write_pc_p (gdbarch))
1240 gdbarch_write_pc (gdbarch, regcache, pc);
1241 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1242 regcache_cooked_write_unsigned (regcache,
1243 gdbarch_pc_regnum (gdbarch), pc);
1245 internal_error (__FILE__, __LINE__,
1246 _("regcache_write_pc: Unable to update PC"));
1248 /* Writing the PC (for instance, from "load") invalidates the
1250 reinit_frame_cache ();
1255 reg_flush_command (char *command, int from_tty)
1257 /* Force-flush the register cache. */
1258 registers_changed ();
1260 printf_filtered (_("Register cache flushed.\n"));
1263 enum regcache_dump_what
1265 regcache_dump_none, regcache_dump_raw,
1266 regcache_dump_cooked, regcache_dump_groups,
1267 regcache_dump_remote
1271 regcache_dump (struct regcache *regcache, struct ui_file *file,
1272 enum regcache_dump_what what_to_dump)
1274 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1275 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1277 int footnote_nr = 0;
1278 int footnote_register_size = 0;
1279 int footnote_register_offset = 0;
1280 int footnote_register_type_name_null = 0;
1281 long register_offset = 0;
1282 gdb_byte buf[MAX_REGISTER_SIZE];
1285 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1286 regcache->descr->nr_raw_registers);
1287 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1288 regcache->descr->nr_cooked_registers);
1289 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1290 regcache->descr->sizeof_raw_registers);
1291 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1292 regcache->descr->sizeof_raw_register_status);
1293 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1294 gdbarch_num_regs (gdbarch));
1295 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1296 gdbarch_num_pseudo_regs (gdbarch));
1299 gdb_assert (regcache->descr->nr_cooked_registers
1300 == (gdbarch_num_regs (gdbarch)
1301 + gdbarch_num_pseudo_regs (gdbarch)));
1303 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1307 fprintf_unfiltered (file, " %-10s", "Name");
1310 const char *p = gdbarch_register_name (gdbarch, regnum);
1314 else if (p[0] == '\0')
1316 fprintf_unfiltered (file, " %-10s", p);
1321 fprintf_unfiltered (file, " %4s", "Nr");
1323 fprintf_unfiltered (file, " %4d", regnum);
1325 /* Relative number. */
1327 fprintf_unfiltered (file, " %4s", "Rel");
1328 else if (regnum < gdbarch_num_regs (gdbarch))
1329 fprintf_unfiltered (file, " %4d", regnum);
1331 fprintf_unfiltered (file, " %4d",
1332 (regnum - gdbarch_num_regs (gdbarch)));
1336 fprintf_unfiltered (file, " %6s ", "Offset");
1339 fprintf_unfiltered (file, " %6ld",
1340 regcache->descr->register_offset[regnum]);
1341 if (register_offset != regcache->descr->register_offset[regnum]
1343 && (regcache->descr->register_offset[regnum]
1344 != (regcache->descr->register_offset[regnum - 1]
1345 + regcache->descr->sizeof_register[regnum - 1])))
1348 if (!footnote_register_offset)
1349 footnote_register_offset = ++footnote_nr;
1350 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1353 fprintf_unfiltered (file, " ");
1354 register_offset = (regcache->descr->register_offset[regnum]
1355 + regcache->descr->sizeof_register[regnum]);
1360 fprintf_unfiltered (file, " %5s ", "Size");
1362 fprintf_unfiltered (file, " %5ld",
1363 regcache->descr->sizeof_register[regnum]);
1373 static const char blt[] = "builtin_type";
1375 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1380 if (!footnote_register_type_name_null)
1381 footnote_register_type_name_null = ++footnote_nr;
1382 n = xstrprintf ("*%d", footnote_register_type_name_null);
1383 make_cleanup (xfree, n);
1386 /* Chop a leading builtin_type. */
1387 if (startswith (t, blt))
1390 fprintf_unfiltered (file, " %-15s", t);
1393 /* Leading space always present. */
1394 fprintf_unfiltered (file, " ");
1397 if (what_to_dump == regcache_dump_raw)
1400 fprintf_unfiltered (file, "Raw value");
1401 else if (regnum >= regcache->descr->nr_raw_registers)
1402 fprintf_unfiltered (file, "<cooked>");
1403 else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1404 fprintf_unfiltered (file, "<invalid>");
1405 else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1406 fprintf_unfiltered (file, "<unavailable>");
1409 regcache_raw_read (regcache, regnum, buf);
1410 print_hex_chars (file, buf,
1411 regcache->descr->sizeof_register[regnum],
1412 gdbarch_byte_order (gdbarch));
1416 /* Value, cooked. */
1417 if (what_to_dump == regcache_dump_cooked)
1420 fprintf_unfiltered (file, "Cooked value");
1423 enum register_status status;
1425 status = regcache_cooked_read (regcache, regnum, buf);
1426 if (status == REG_UNKNOWN)
1427 fprintf_unfiltered (file, "<invalid>");
1428 else if (status == REG_UNAVAILABLE)
1429 fprintf_unfiltered (file, "<unavailable>");
1431 print_hex_chars (file, buf,
1432 regcache->descr->sizeof_register[regnum],
1433 gdbarch_byte_order (gdbarch));
1437 /* Group members. */
1438 if (what_to_dump == regcache_dump_groups)
1441 fprintf_unfiltered (file, "Groups");
1444 const char *sep = "";
1445 struct reggroup *group;
1447 for (group = reggroup_next (gdbarch, NULL);
1449 group = reggroup_next (gdbarch, group))
1451 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1453 fprintf_unfiltered (file,
1454 "%s%s", sep, reggroup_name (group));
1461 /* Remote packet configuration. */
1462 if (what_to_dump == regcache_dump_remote)
1466 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1468 else if (regnum < regcache->descr->nr_raw_registers)
1472 if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1474 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1478 fprintf_unfiltered (file, "\n");
1481 if (footnote_register_size)
1482 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1483 footnote_register_size);
1484 if (footnote_register_offset)
1485 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1486 footnote_register_offset);
1487 if (footnote_register_type_name_null)
1488 fprintf_unfiltered (file,
1489 "*%d: Register type's name NULL.\n",
1490 footnote_register_type_name_null);
1491 do_cleanups (cleanups);
1495 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1498 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1501 struct cleanup *cleanups;
1502 struct ui_file *file = gdb_fopen (args, "w");
1505 perror_with_name (_("maintenance print architecture"));
1506 cleanups = make_cleanup_ui_file_delete (file);
1507 regcache_dump (get_current_regcache (), file, what_to_dump);
1508 do_cleanups (cleanups);
1513 maintenance_print_registers (char *args, int from_tty)
1515 regcache_print (args, regcache_dump_none);
1519 maintenance_print_raw_registers (char *args, int from_tty)
1521 regcache_print (args, regcache_dump_raw);
1525 maintenance_print_cooked_registers (char *args, int from_tty)
1527 regcache_print (args, regcache_dump_cooked);
1531 maintenance_print_register_groups (char *args, int from_tty)
1533 regcache_print (args, regcache_dump_groups);
1537 maintenance_print_remote_registers (char *args, int from_tty)
1539 regcache_print (args, regcache_dump_remote);
1542 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1545 _initialize_regcache (void)
1547 regcache_descr_handle
1548 = gdbarch_data_register_post_init (init_regcache_descr);
1550 observer_attach_target_changed (regcache_observer_target_changed);
1551 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1553 add_com ("flushregs", class_maintenance, reg_flush_command,
1554 _("Force gdb to flush its register cache (maintainer command)"));
1556 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1557 _("Print the internal register configuration.\n"
1558 "Takes an optional file parameter."), &maintenanceprintlist);
1559 add_cmd ("raw-registers", class_maintenance,
1560 maintenance_print_raw_registers,
1561 _("Print the internal register configuration "
1562 "including raw values.\n"
1563 "Takes an optional file parameter."), &maintenanceprintlist);
1564 add_cmd ("cooked-registers", class_maintenance,
1565 maintenance_print_cooked_registers,
1566 _("Print the internal register configuration "
1567 "including cooked values.\n"
1568 "Takes an optional file parameter."), &maintenanceprintlist);
1569 add_cmd ("register-groups", class_maintenance,
1570 maintenance_print_register_groups,
1571 _("Print the internal register configuration "
1572 "including each register's group.\n"
1573 "Takes an optional file parameter."),
1574 &maintenanceprintlist);
1575 add_cmd ("remote-registers", class_maintenance,
1576 maintenance_print_remote_registers, _("\
1577 Print the internal register configuration including each register's\n\
1578 remote register number and buffer offset in the g/G packets.\n\
1579 Takes an optional file parameter."),
1580 &maintenanceprintlist);