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 /* 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;
228 static struct regcache *
229 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
232 struct regcache_descr *descr;
233 struct regcache *regcache;
235 gdb_assert (gdbarch != NULL);
236 descr = regcache_descr (gdbarch);
237 regcache = XNEW (struct regcache);
238 regcache->descr = descr;
239 regcache->readonly_p = readonly_p;
243 = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
244 regcache->register_status
245 = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
250 = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
251 regcache->register_status
252 = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
254 regcache->aspace = aspace;
255 regcache->ptid = minus_one_ptid;
260 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
262 return regcache_xmalloc_1 (gdbarch, aspace, 1);
266 regcache_xfree (struct regcache *regcache)
268 if (regcache == NULL)
270 xfree (regcache->registers);
271 xfree (regcache->register_status);
276 do_regcache_xfree (void *data)
278 regcache_xfree ((struct regcache *) data);
282 make_cleanup_regcache_xfree (struct regcache *regcache)
284 return make_cleanup (do_regcache_xfree, regcache);
287 /* Cleanup routines for invalidating a register. */
289 struct register_to_invalidate
291 struct regcache *regcache;
296 do_regcache_invalidate (void *data)
298 struct register_to_invalidate *reg = (struct register_to_invalidate *) data;
300 regcache_invalidate (reg->regcache, reg->regnum);
303 static struct cleanup *
304 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
306 struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
308 reg->regcache = regcache;
309 reg->regnum = regnum;
310 return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
313 /* Return REGCACHE's architecture. */
316 get_regcache_arch (const struct regcache *regcache)
318 return regcache->descr->gdbarch;
321 struct address_space *
322 get_regcache_aspace (const struct regcache *regcache)
324 return regcache->aspace;
327 /* Return a pointer to register REGNUM's buffer cache. */
330 register_buffer (const struct regcache *regcache, int regnum)
332 return regcache->registers + regcache->descr->register_offset[regnum];
336 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
339 struct gdbarch *gdbarch = dst->descr->gdbarch;
340 gdb_byte buf[MAX_REGISTER_SIZE];
343 /* The DST should be `read-only', if it wasn't then the save would
344 end up trying to write the register values back out to the
346 gdb_assert (dst->readonly_p);
347 /* Clear the dest. */
348 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
349 memset (dst->register_status, 0,
350 dst->descr->sizeof_cooked_register_status);
351 /* Copy over any registers (identified by their membership in the
352 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
353 gdbarch_num_pseudo_regs) range is checked since some architectures need
354 to save/restore `cooked' registers that live in memory. */
355 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
357 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
359 enum register_status status = cooked_read (src, regnum, buf);
361 if (status == REG_VALID)
362 memcpy (register_buffer (dst, regnum), buf,
363 register_size (gdbarch, regnum));
366 gdb_assert (status != REG_UNKNOWN);
368 memset (register_buffer (dst, regnum), 0,
369 register_size (gdbarch, regnum));
371 dst->register_status[regnum] = status;
377 regcache_restore (struct regcache *dst,
378 regcache_cooked_read_ftype *cooked_read,
379 void *cooked_read_context)
381 struct gdbarch *gdbarch = dst->descr->gdbarch;
382 gdb_byte buf[MAX_REGISTER_SIZE];
385 /* The dst had better not be read-only. If it is, the `restore'
386 doesn't make much sense. */
387 gdb_assert (!dst->readonly_p);
388 /* Copy over any registers, being careful to only restore those that
389 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
390 + gdbarch_num_pseudo_regs) range is checked since some architectures need
391 to save/restore `cooked' registers that live in memory. */
392 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
394 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
396 enum register_status status;
398 status = cooked_read (cooked_read_context, regnum, buf);
399 if (status == REG_VALID)
400 regcache_cooked_write (dst, regnum, buf);
405 static enum register_status
406 do_cooked_read (void *src, int regnum, gdb_byte *buf)
408 struct regcache *regcache = (struct regcache *) src;
410 return regcache_cooked_read (regcache, regnum, buf);
413 static void regcache_cpy_no_passthrough (struct regcache *dst,
414 struct regcache *src);
417 regcache_cpy (struct regcache *dst, struct regcache *src)
419 gdb_assert (src != NULL && dst != NULL);
420 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
421 gdb_assert (src != dst);
422 gdb_assert (src->readonly_p || dst->readonly_p);
424 if (!src->readonly_p)
425 regcache_save (dst, do_cooked_read, src);
426 else if (!dst->readonly_p)
427 regcache_restore (dst, do_cooked_read, src);
429 regcache_cpy_no_passthrough (dst, src);
432 /* Copy/duplicate the contents of a register cache. Unlike regcache_cpy,
433 which is pass-through, this does not go through to the target.
434 Only values values already in the cache are transferred. The SRC and DST
435 buffers must not overlap. */
438 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
440 gdb_assert (src != NULL && dst != NULL);
441 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
442 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
443 move of data into a thread's regcache. Doing this would be silly
444 - it would mean that regcache->register_status would be
445 completely invalid. */
446 gdb_assert (dst->readonly_p && src->readonly_p);
448 memcpy (dst->registers, src->registers,
449 dst->descr->sizeof_cooked_registers);
450 memcpy (dst->register_status, src->register_status,
451 dst->descr->sizeof_cooked_register_status);
455 regcache_dup (struct regcache *src)
457 struct regcache *newbuf;
459 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
460 regcache_cpy (newbuf, src);
465 regcache_register_status (const struct regcache *regcache, int regnum)
467 gdb_assert (regcache != NULL);
468 gdb_assert (regnum >= 0);
469 if (regcache->readonly_p)
470 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
472 gdb_assert (regnum < regcache->descr->nr_raw_registers);
474 return (enum register_status) regcache->register_status[regnum];
478 regcache_invalidate (struct regcache *regcache, int regnum)
480 gdb_assert (regcache != NULL);
481 gdb_assert (regnum >= 0);
482 gdb_assert (!regcache->readonly_p);
483 gdb_assert (regnum < regcache->descr->nr_raw_registers);
484 regcache->register_status[regnum] = REG_UNKNOWN;
488 /* Global structure containing the current regcache. */
490 /* NOTE: this is a write-through cache. There is no "dirty" bit for
491 recording if the register values have been changed (eg. by the
492 user). Therefore all registers must be written back to the
493 target when appropriate. */
497 struct regcache *regcache;
498 struct regcache_list *next;
501 static struct regcache_list *current_regcache;
504 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
505 struct address_space *aspace)
507 struct regcache_list *list;
508 struct regcache *new_regcache;
510 for (list = current_regcache; list; list = list->next)
511 if (ptid_equal (list->regcache->ptid, ptid)
512 && get_regcache_arch (list->regcache) == gdbarch)
513 return list->regcache;
515 new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
516 new_regcache->ptid = ptid;
518 list = XNEW (struct regcache_list);
519 list->regcache = new_regcache;
520 list->next = current_regcache;
521 current_regcache = list;
527 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
529 struct address_space *aspace;
531 /* For the benefit of "maint print registers" & co when debugging an
532 executable, allow dumping the regcache even when there is no
533 thread selected (target_thread_address_space internal-errors if
534 no address space is found). Note that normal user commands will
535 fail higher up on the call stack due to no
536 target_has_registers. */
537 aspace = (ptid_equal (null_ptid, ptid)
539 : target_thread_address_space (ptid));
541 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
544 static ptid_t current_thread_ptid;
545 static struct gdbarch *current_thread_arch;
548 get_thread_regcache (ptid_t ptid)
550 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
552 current_thread_ptid = ptid;
553 current_thread_arch = target_thread_architecture (ptid);
556 return get_thread_arch_regcache (ptid, current_thread_arch);
560 get_current_regcache (void)
562 return get_thread_regcache (inferior_ptid);
565 /* See common/common-regcache.h. */
568 get_thread_regcache_for_ptid (ptid_t ptid)
570 return get_thread_regcache (ptid);
573 /* Observer for the target_changed event. */
576 regcache_observer_target_changed (struct target_ops *target)
578 registers_changed ();
581 /* Update global variables old ptids to hold NEW_PTID if they were
584 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
586 struct regcache_list *list;
588 for (list = current_regcache; list; list = list->next)
589 if (ptid_equal (list->regcache->ptid, old_ptid))
590 list->regcache->ptid = new_ptid;
593 /* Low level examining and depositing of registers.
595 The caller is responsible for making sure that the inferior is
596 stopped before calling the fetching routines, or it will get
597 garbage. (a change from GDB version 3, in which the caller got the
598 value from the last stop). */
600 /* REGISTERS_CHANGED ()
602 Indicate that registers may have changed, so invalidate the cache. */
605 registers_changed_ptid (ptid_t ptid)
607 struct regcache_list *list, **list_link;
609 list = current_regcache;
610 list_link = ¤t_regcache;
613 if (ptid_match (list->regcache->ptid, ptid))
615 struct regcache_list *dead = list;
617 *list_link = list->next;
618 regcache_xfree (list->regcache);
624 list_link = &list->next;
628 if (ptid_match (current_thread_ptid, ptid))
630 current_thread_ptid = null_ptid;
631 current_thread_arch = NULL;
634 if (ptid_match (inferior_ptid, ptid))
636 /* We just deleted the regcache of the current thread. Need to
637 forget about any frames we have cached, too. */
638 reinit_frame_cache ();
643 registers_changed (void)
645 registers_changed_ptid (minus_one_ptid);
647 /* Force cleanup of any alloca areas if using C alloca instead of
648 a builtin alloca. This particular call is used to clean up
649 areas allocated by low level target code which may build up
650 during lengthy interactions between gdb and the target before
651 gdb gives control to the user (ie watchpoints). */
656 regcache_raw_update (struct regcache *regcache, int regnum)
658 gdb_assert (regcache != NULL);
659 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
661 /* Make certain that the register cache is up-to-date with respect
662 to the current thread. This switching shouldn't be necessary
663 only there is still only one target side register cache. Sigh!
664 On the bright side, at least there is a regcache object. */
666 if (!regcache->readonly_p
667 && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
669 target_fetch_registers (regcache, regnum);
671 /* A number of targets can't access the whole set of raw
672 registers (because the debug API provides no means to get at
674 if (regcache->register_status[regnum] == REG_UNKNOWN)
675 regcache->register_status[regnum] = REG_UNAVAILABLE;
680 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
682 gdb_assert (buf != NULL);
683 regcache_raw_update (regcache, regnum);
685 if (regcache->register_status[regnum] != REG_VALID)
686 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
688 memcpy (buf, register_buffer (regcache, regnum),
689 regcache->descr->sizeof_register[regnum]);
691 return (enum register_status) regcache->register_status[regnum];
695 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
698 enum register_status status;
700 gdb_assert (regcache != NULL);
701 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
702 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
703 status = regcache_raw_read (regcache, regnum, buf);
704 if (status == REG_VALID)
705 *val = extract_signed_integer
706 (buf, regcache->descr->sizeof_register[regnum],
707 gdbarch_byte_order (regcache->descr->gdbarch));
714 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
718 enum register_status status;
720 gdb_assert (regcache != NULL);
721 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
722 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
723 status = regcache_raw_read (regcache, regnum, buf);
724 if (status == REG_VALID)
725 *val = extract_unsigned_integer
726 (buf, regcache->descr->sizeof_register[regnum],
727 gdbarch_byte_order (regcache->descr->gdbarch));
734 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
738 gdb_assert (regcache != NULL);
739 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
740 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
741 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
742 gdbarch_byte_order (regcache->descr->gdbarch), val);
743 regcache_raw_write (regcache, regnum, buf);
747 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
752 gdb_assert (regcache != NULL);
753 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
754 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
755 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
756 gdbarch_byte_order (regcache->descr->gdbarch), val);
757 regcache_raw_write (regcache, regnum, buf);
761 regcache_raw_get_signed (struct regcache *regcache, int regnum)
764 enum register_status status;
766 status = regcache_raw_read_signed (regcache, regnum, &value);
767 if (status == REG_UNAVAILABLE)
768 throw_error (NOT_AVAILABLE_ERROR,
769 _("Register %d is not available"), regnum);
774 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
776 gdb_assert (regnum >= 0);
777 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
778 if (regnum < regcache->descr->nr_raw_registers)
779 return regcache_raw_read (regcache, regnum, buf);
780 else if (regcache->readonly_p
781 && regcache->register_status[regnum] != REG_UNKNOWN)
783 /* Read-only register cache, perhaps the cooked value was
785 if (regcache->register_status[regnum] == REG_VALID)
786 memcpy (buf, register_buffer (regcache, regnum),
787 regcache->descr->sizeof_register[regnum]);
789 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
791 return (enum register_status) regcache->register_status[regnum];
793 else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
795 struct value *mark, *computed;
796 enum register_status result = REG_VALID;
798 mark = value_mark ();
800 computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
802 if (value_entirely_available (computed))
803 memcpy (buf, value_contents_raw (computed),
804 regcache->descr->sizeof_register[regnum]);
807 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
808 result = REG_UNAVAILABLE;
811 value_free_to_mark (mark);
816 return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
821 regcache_cooked_read_value (struct regcache *regcache, int regnum)
823 gdb_assert (regnum >= 0);
824 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
826 if (regnum < regcache->descr->nr_raw_registers
827 || (regcache->readonly_p
828 && regcache->register_status[regnum] != REG_UNKNOWN)
829 || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
831 struct value *result;
833 result = allocate_value (register_type (regcache->descr->gdbarch,
835 VALUE_LVAL (result) = lval_register;
836 VALUE_REGNUM (result) = regnum;
838 /* It is more efficient in general to do this delegation in this
839 direction than in the other one, even though the value-based
841 if (regcache_cooked_read (regcache, regnum,
842 value_contents_raw (result)) == REG_UNAVAILABLE)
843 mark_value_bytes_unavailable (result, 0,
844 TYPE_LENGTH (value_type (result)));
849 return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
854 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
857 enum register_status status;
860 gdb_assert (regcache != NULL);
861 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
862 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
863 status = regcache_cooked_read (regcache, regnum, buf);
864 if (status == REG_VALID)
865 *val = extract_signed_integer
866 (buf, regcache->descr->sizeof_register[regnum],
867 gdbarch_byte_order (regcache->descr->gdbarch));
874 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
877 enum register_status status;
880 gdb_assert (regcache != NULL);
881 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
882 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
883 status = regcache_cooked_read (regcache, regnum, buf);
884 if (status == REG_VALID)
885 *val = extract_unsigned_integer
886 (buf, regcache->descr->sizeof_register[regnum],
887 gdbarch_byte_order (regcache->descr->gdbarch));
894 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
899 gdb_assert (regcache != NULL);
900 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
901 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
902 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
903 gdbarch_byte_order (regcache->descr->gdbarch), val);
904 regcache_cooked_write (regcache, regnum, buf);
908 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
913 gdb_assert (regcache != NULL);
914 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
915 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
916 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
917 gdbarch_byte_order (regcache->descr->gdbarch), val);
918 regcache_cooked_write (regcache, regnum, buf);
921 /* See regcache.h. */
924 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
927 memcpy (register_buffer (regcache, regnum), buf,
928 regcache->descr->sizeof_register[regnum]);
929 regcache->register_status[regnum] = REG_VALID;
933 regcache_raw_write (struct regcache *regcache, int regnum,
936 struct cleanup *old_chain;
938 gdb_assert (regcache != NULL && buf != NULL);
939 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
940 gdb_assert (!regcache->readonly_p);
942 /* On the sparc, writing %g0 is a no-op, so we don't even want to
943 change the registers array if something writes to this register. */
944 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
947 /* If we have a valid copy of the register, and new value == old
948 value, then don't bother doing the actual store. */
949 if (regcache_register_status (regcache, regnum) == REG_VALID
950 && (memcmp (register_buffer (regcache, regnum), buf,
951 regcache->descr->sizeof_register[regnum]) == 0))
954 target_prepare_to_store (regcache);
955 regcache_raw_set_cached_value (regcache, regnum, buf);
957 /* Register a cleanup function for invalidating the register after it is
958 written, in case of a failure. */
959 old_chain = make_cleanup_regcache_invalidate (regcache, regnum);
961 target_store_registers (regcache, regnum);
963 /* The target did not throw an error so we can discard invalidating the
964 register and restore the cleanup chain to what it was. */
965 discard_cleanups (old_chain);
969 regcache_cooked_write (struct regcache *regcache, int regnum,
972 gdb_assert (regnum >= 0);
973 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
974 if (regnum < regcache->descr->nr_raw_registers)
975 regcache_raw_write (regcache, regnum, buf);
977 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
981 /* Perform a partial register transfer using a read, modify, write
984 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
986 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
989 static enum register_status
990 regcache_xfer_part (struct regcache *regcache, int regnum,
991 int offset, int len, void *in, const void *out,
992 enum register_status (*read) (struct regcache *regcache,
995 void (*write) (struct regcache *regcache, int regnum,
996 const gdb_byte *buf))
998 struct regcache_descr *descr = regcache->descr;
999 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1000 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
1002 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
1003 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
1004 /* Something to do? */
1005 if (offset + len == 0)
1007 /* Read (when needed) ... */
1010 || offset + len < descr->sizeof_register[regnum])
1012 enum register_status status;
1014 gdb_assert (read != NULL);
1015 status = read (regcache, regnum, reg);
1016 if (status != REG_VALID)
1019 /* ... modify ... */
1021 memcpy (in, reg + offset, len);
1023 memcpy (reg + offset, out, len);
1024 /* ... write (when needed). */
1027 gdb_assert (write != NULL);
1028 write (regcache, regnum, reg);
1034 enum register_status
1035 regcache_raw_read_part (struct regcache *regcache, int regnum,
1036 int offset, int len, gdb_byte *buf)
1038 struct regcache_descr *descr = regcache->descr;
1040 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1041 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1042 regcache_raw_read, regcache_raw_write);
1046 regcache_raw_write_part (struct regcache *regcache, int regnum,
1047 int offset, int len, const gdb_byte *buf)
1049 struct regcache_descr *descr = regcache->descr;
1051 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1052 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1053 regcache_raw_read, regcache_raw_write);
1056 enum register_status
1057 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1058 int offset, int len, gdb_byte *buf)
1060 struct regcache_descr *descr = regcache->descr;
1062 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1063 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1064 regcache_cooked_read, regcache_cooked_write);
1068 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1069 int offset, int len, const gdb_byte *buf)
1071 struct regcache_descr *descr = regcache->descr;
1073 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1074 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1075 regcache_cooked_read, regcache_cooked_write);
1078 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1081 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1086 gdb_assert (regcache != NULL);
1087 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1088 gdb_assert (!regcache->readonly_p);
1090 regbuf = register_buffer (regcache, regnum);
1091 size = regcache->descr->sizeof_register[regnum];
1095 memcpy (regbuf, buf, size);
1096 regcache->register_status[regnum] = REG_VALID;
1100 /* This memset not strictly necessary, but better than garbage
1101 in case the register value manages to escape somewhere (due
1102 to a bug, no less). */
1103 memset (regbuf, 0, size);
1104 regcache->register_status[regnum] = REG_UNAVAILABLE;
1108 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1111 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1116 gdb_assert (regcache != NULL && buf != NULL);
1117 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1119 regbuf = register_buffer (regcache, regnum);
1120 size = regcache->descr->sizeof_register[regnum];
1121 memcpy (buf, regbuf, size);
1124 /* Transfer a single or all registers belonging to a certain register
1125 set to or from a buffer. This is the main worker function for
1126 regcache_supply_regset and regcache_collect_regset. */
1129 regcache_transfer_regset (const struct regset *regset,
1130 const struct regcache *regcache,
1131 struct regcache *out_regcache,
1132 int regnum, const void *in_buf,
1133 void *out_buf, size_t size)
1135 const struct regcache_map_entry *map;
1136 int offs = 0, count;
1138 for (map = (const struct regcache_map_entry *) regset->regmap;
1139 (count = map->count) != 0;
1142 int regno = map->regno;
1143 int slot_size = map->size;
1145 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1146 slot_size = regcache->descr->sizeof_register[regno];
1148 if (regno == REGCACHE_MAP_SKIP
1150 && (regnum < regno || regnum >= regno + count)))
1151 offs += count * slot_size;
1153 else if (regnum == -1)
1154 for (; count--; regno++, offs += slot_size)
1156 if (offs + slot_size > size)
1160 regcache_raw_collect (regcache, regno,
1161 (gdb_byte *) out_buf + offs);
1163 regcache_raw_supply (out_regcache, regno, in_buf
1164 ? (const gdb_byte *) in_buf + offs
1169 /* Transfer a single register and return. */
1170 offs += (regnum - regno) * slot_size;
1171 if (offs + slot_size > size)
1175 regcache_raw_collect (regcache, regnum,
1176 (gdb_byte *) out_buf + offs);
1178 regcache_raw_supply (out_regcache, regnum, in_buf
1179 ? (const gdb_byte *) in_buf + offs
1186 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1187 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1188 If BUF is NULL, set the register(s) to "unavailable" status. */
1191 regcache_supply_regset (const struct regset *regset,
1192 struct regcache *regcache,
1193 int regnum, const void *buf, size_t size)
1195 regcache_transfer_regset (regset, regcache, regcache, regnum,
1199 /* Collect register REGNUM from REGCACHE to BUF, using the register
1200 map in REGSET. If REGNUM is -1, do this for all registers in
1204 regcache_collect_regset (const struct regset *regset,
1205 const struct regcache *regcache,
1206 int regnum, void *buf, size_t size)
1208 regcache_transfer_regset (regset, regcache, NULL, regnum,
1213 /* Special handling for register PC. */
1216 regcache_read_pc (struct regcache *regcache)
1218 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1222 if (gdbarch_read_pc_p (gdbarch))
1223 pc_val = gdbarch_read_pc (gdbarch, regcache);
1224 /* Else use per-frame method on get_current_frame. */
1225 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1229 if (regcache_cooked_read_unsigned (regcache,
1230 gdbarch_pc_regnum (gdbarch),
1231 &raw_val) == REG_UNAVAILABLE)
1232 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1234 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1237 internal_error (__FILE__, __LINE__,
1238 _("regcache_read_pc: Unable to find PC"));
1243 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1245 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1247 if (gdbarch_write_pc_p (gdbarch))
1248 gdbarch_write_pc (gdbarch, regcache, pc);
1249 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1250 regcache_cooked_write_unsigned (regcache,
1251 gdbarch_pc_regnum (gdbarch), pc);
1253 internal_error (__FILE__, __LINE__,
1254 _("regcache_write_pc: Unable to update PC"));
1256 /* Writing the PC (for instance, from "load") invalidates the
1258 reinit_frame_cache ();
1263 reg_flush_command (char *command, int from_tty)
1265 /* Force-flush the register cache. */
1266 registers_changed ();
1268 printf_filtered (_("Register cache flushed.\n"));
1271 enum regcache_dump_what
1273 regcache_dump_none, regcache_dump_raw,
1274 regcache_dump_cooked, regcache_dump_groups,
1275 regcache_dump_remote
1279 regcache_dump (struct regcache *regcache, struct ui_file *file,
1280 enum regcache_dump_what what_to_dump)
1282 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1283 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1285 int footnote_nr = 0;
1286 int footnote_register_size = 0;
1287 int footnote_register_offset = 0;
1288 int footnote_register_type_name_null = 0;
1289 long register_offset = 0;
1290 gdb_byte buf[MAX_REGISTER_SIZE];
1293 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1294 regcache->descr->nr_raw_registers);
1295 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1296 regcache->descr->nr_cooked_registers);
1297 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1298 regcache->descr->sizeof_raw_registers);
1299 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1300 regcache->descr->sizeof_raw_register_status);
1301 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1302 gdbarch_num_regs (gdbarch));
1303 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1304 gdbarch_num_pseudo_regs (gdbarch));
1307 gdb_assert (regcache->descr->nr_cooked_registers
1308 == (gdbarch_num_regs (gdbarch)
1309 + gdbarch_num_pseudo_regs (gdbarch)));
1311 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1315 fprintf_unfiltered (file, " %-10s", "Name");
1318 const char *p = gdbarch_register_name (gdbarch, regnum);
1322 else if (p[0] == '\0')
1324 fprintf_unfiltered (file, " %-10s", p);
1329 fprintf_unfiltered (file, " %4s", "Nr");
1331 fprintf_unfiltered (file, " %4d", regnum);
1333 /* Relative number. */
1335 fprintf_unfiltered (file, " %4s", "Rel");
1336 else if (regnum < gdbarch_num_regs (gdbarch))
1337 fprintf_unfiltered (file, " %4d", regnum);
1339 fprintf_unfiltered (file, " %4d",
1340 (regnum - gdbarch_num_regs (gdbarch)));
1344 fprintf_unfiltered (file, " %6s ", "Offset");
1347 fprintf_unfiltered (file, " %6ld",
1348 regcache->descr->register_offset[regnum]);
1349 if (register_offset != regcache->descr->register_offset[regnum]
1351 && (regcache->descr->register_offset[regnum]
1352 != (regcache->descr->register_offset[regnum - 1]
1353 + regcache->descr->sizeof_register[regnum - 1])))
1356 if (!footnote_register_offset)
1357 footnote_register_offset = ++footnote_nr;
1358 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1361 fprintf_unfiltered (file, " ");
1362 register_offset = (regcache->descr->register_offset[regnum]
1363 + regcache->descr->sizeof_register[regnum]);
1368 fprintf_unfiltered (file, " %5s ", "Size");
1370 fprintf_unfiltered (file, " %5ld",
1371 regcache->descr->sizeof_register[regnum]);
1381 static const char blt[] = "builtin_type";
1383 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1388 if (!footnote_register_type_name_null)
1389 footnote_register_type_name_null = ++footnote_nr;
1390 n = xstrprintf ("*%d", footnote_register_type_name_null);
1391 make_cleanup (xfree, n);
1394 /* Chop a leading builtin_type. */
1395 if (startswith (t, blt))
1398 fprintf_unfiltered (file, " %-15s", t);
1401 /* Leading space always present. */
1402 fprintf_unfiltered (file, " ");
1405 if (what_to_dump == regcache_dump_raw)
1408 fprintf_unfiltered (file, "Raw value");
1409 else if (regnum >= regcache->descr->nr_raw_registers)
1410 fprintf_unfiltered (file, "<cooked>");
1411 else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1412 fprintf_unfiltered (file, "<invalid>");
1413 else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1414 fprintf_unfiltered (file, "<unavailable>");
1417 regcache_raw_read (regcache, regnum, buf);
1418 print_hex_chars (file, buf,
1419 regcache->descr->sizeof_register[regnum],
1420 gdbarch_byte_order (gdbarch));
1424 /* Value, cooked. */
1425 if (what_to_dump == regcache_dump_cooked)
1428 fprintf_unfiltered (file, "Cooked value");
1431 enum register_status status;
1433 status = regcache_cooked_read (regcache, regnum, buf);
1434 if (status == REG_UNKNOWN)
1435 fprintf_unfiltered (file, "<invalid>");
1436 else if (status == REG_UNAVAILABLE)
1437 fprintf_unfiltered (file, "<unavailable>");
1439 print_hex_chars (file, buf,
1440 regcache->descr->sizeof_register[regnum],
1441 gdbarch_byte_order (gdbarch));
1445 /* Group members. */
1446 if (what_to_dump == regcache_dump_groups)
1449 fprintf_unfiltered (file, "Groups");
1452 const char *sep = "";
1453 struct reggroup *group;
1455 for (group = reggroup_next (gdbarch, NULL);
1457 group = reggroup_next (gdbarch, group))
1459 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1461 fprintf_unfiltered (file,
1462 "%s%s", sep, reggroup_name (group));
1469 /* Remote packet configuration. */
1470 if (what_to_dump == regcache_dump_remote)
1474 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1476 else if (regnum < regcache->descr->nr_raw_registers)
1480 if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1482 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1486 fprintf_unfiltered (file, "\n");
1489 if (footnote_register_size)
1490 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1491 footnote_register_size);
1492 if (footnote_register_offset)
1493 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1494 footnote_register_offset);
1495 if (footnote_register_type_name_null)
1496 fprintf_unfiltered (file,
1497 "*%d: Register type's name NULL.\n",
1498 footnote_register_type_name_null);
1499 do_cleanups (cleanups);
1503 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1506 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1511 if (!file.open (args, "w"))
1512 perror_with_name (_("maintenance print architecture"));
1513 regcache_dump (get_current_regcache (), &file, what_to_dump);
1518 maintenance_print_registers (char *args, int from_tty)
1520 regcache_print (args, regcache_dump_none);
1524 maintenance_print_raw_registers (char *args, int from_tty)
1526 regcache_print (args, regcache_dump_raw);
1530 maintenance_print_cooked_registers (char *args, int from_tty)
1532 regcache_print (args, regcache_dump_cooked);
1536 maintenance_print_register_groups (char *args, int from_tty)
1538 regcache_print (args, regcache_dump_groups);
1542 maintenance_print_remote_registers (char *args, int from_tty)
1544 regcache_print (args, regcache_dump_remote);
1547 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1550 _initialize_regcache (void)
1552 regcache_descr_handle
1553 = gdbarch_data_register_post_init (init_regcache_descr);
1555 observer_attach_target_changed (regcache_observer_target_changed);
1556 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1558 add_com ("flushregs", class_maintenance, reg_flush_command,
1559 _("Force gdb to flush its register cache (maintainer command)"));
1561 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1562 _("Print the internal register configuration.\n"
1563 "Takes an optional file parameter."), &maintenanceprintlist);
1564 add_cmd ("raw-registers", class_maintenance,
1565 maintenance_print_raw_registers,
1566 _("Print the internal register configuration "
1567 "including raw values.\n"
1568 "Takes an optional file parameter."), &maintenanceprintlist);
1569 add_cmd ("cooked-registers", class_maintenance,
1570 maintenance_print_cooked_registers,
1571 _("Print the internal register configuration "
1572 "including cooked values.\n"
1573 "Takes an optional file parameter."), &maintenanceprintlist);
1574 add_cmd ("register-groups", class_maintenance,
1575 maintenance_print_register_groups,
1576 _("Print the internal register configuration "
1577 "including each register's group.\n"
1578 "Takes an optional file parameter."),
1579 &maintenanceprintlist);
1580 add_cmd ("remote-registers", class_maintenance,
1581 maintenance_print_remote_registers, _("\
1582 Print the internal register configuration including each register's\n\
1583 remote register number and buffer offset in the g/G packets.\n\
1584 Takes an optional file parameter."),
1585 &maintenanceprintlist);