1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "reggroups.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdbcmd.h" /* For maintenanceprintlist. */
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created. */
42 struct gdbarch_data *regcache_descr_handle;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
55 long sizeof_raw_registers;
56 long sizeof_raw_register_valid_p;
58 /* The cooked register space. Each cooked register in the range
59 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
60 register. The remaining [NR_RAW_REGISTERS
61 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
62 both raw registers and memory by the architecture methods
63 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
64 int nr_cooked_registers;
65 long sizeof_cooked_registers;
66 long sizeof_cooked_register_valid_p;
68 /* Offset and size (in 8 bit bytes), of reach register in the
69 register cache. All registers (including those in the range
70 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
72 long *register_offset;
73 long *sizeof_register;
75 /* Cached table containing the type of each register. */
76 struct type **register_type;
80 init_regcache_descr (struct gdbarch *gdbarch)
83 struct regcache_descr *descr;
84 gdb_assert (gdbarch != NULL);
86 /* Create an initial, zero filled, table. */
87 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
88 descr->gdbarch = gdbarch;
90 /* Total size of the register space. The raw registers are mapped
91 directly onto the raw register cache while the pseudo's are
92 either mapped onto raw-registers or memory. */
93 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
94 + gdbarch_num_pseudo_regs (gdbarch);
95 descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (gdbarch)
96 + gdbarch_num_pseudo_regs
99 /* Fill in a table of register types. */
101 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
103 for (i = 0; i < descr->nr_cooked_registers; i++)
104 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
106 /* Construct a strictly RAW register cache. Don't allow pseudo's
107 into the register cache. */
108 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
109 descr->sizeof_raw_register_valid_p = gdbarch_num_regs (gdbarch);
111 /* Lay out the register cache.
113 NOTE: cagney/2002-05-22: Only register_type() is used when
114 constructing the register cache. It is assumed that the
115 register's raw size, virtual size and type length are all the
121 descr->sizeof_register
122 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
123 descr->register_offset
124 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
125 for (i = 0; i < descr->nr_raw_registers; i++)
127 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
128 descr->register_offset[i] = offset;
129 offset += descr->sizeof_register[i];
130 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
132 /* Set the real size of the raw register cache buffer. */
133 descr->sizeof_raw_registers = offset;
135 for (; i < descr->nr_cooked_registers; i++)
137 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
138 descr->register_offset[i] = offset;
139 offset += descr->sizeof_register[i];
140 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
142 /* Set the real size of the readonly register cache buffer. */
143 descr->sizeof_cooked_registers = offset;
149 static struct regcache_descr *
150 regcache_descr (struct gdbarch *gdbarch)
152 return gdbarch_data (gdbarch, regcache_descr_handle);
155 /* Utility functions returning useful register attributes stored in
156 the regcache descr. */
159 register_type (struct gdbarch *gdbarch, int regnum)
161 struct regcache_descr *descr = regcache_descr (gdbarch);
163 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164 return descr->register_type[regnum];
167 /* Utility functions returning useful register attributes stored in
168 the regcache descr. */
171 register_size (struct gdbarch *gdbarch, int regnum)
173 struct regcache_descr *descr = regcache_descr (gdbarch);
176 gdb_assert (regnum >= 0
177 && regnum < (gdbarch_num_regs (gdbarch)
178 + gdbarch_num_pseudo_regs (gdbarch)));
179 size = descr->sizeof_register[regnum];
183 /* The register cache for storing raw register values. */
187 struct regcache_descr *descr;
189 /* The address space of this register cache (for registers where it
190 makes sense, like PC or SP). */
191 struct address_space *aspace;
193 /* The register buffers. A read-only register cache can hold the
194 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
195 register cache can only hold [0 .. gdbarch_num_regs). */
197 /* Register cache status:
198 register_valid_p[REG] == 0 if REG value is not in the cache
199 > 0 if REG value is in the cache
200 < 0 if REG value is permanently unavailable */
201 signed char *register_valid_p;
202 /* Is this a read-only cache? A read-only cache is used for saving
203 the target's register state (e.g, across an inferior function
204 call or just before forcing a function return). A read-only
205 cache can only be updated via the methods regcache_dup() and
206 regcache_cpy(). The actual contents are determined by the
207 reggroup_save and reggroup_restore methods. */
209 /* If this is a read-write cache, which thread's registers is
214 static struct regcache *
215 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
218 struct regcache_descr *descr;
219 struct regcache *regcache;
221 gdb_assert (gdbarch != NULL);
222 descr = regcache_descr (gdbarch);
223 regcache = XMALLOC (struct regcache);
224 regcache->descr = descr;
225 regcache->readonly_p = readonly_p;
229 = XCALLOC (descr->sizeof_cooked_registers, gdb_byte);
230 regcache->register_valid_p
231 = XCALLOC (descr->sizeof_cooked_register_valid_p, gdb_byte);
236 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
237 regcache->register_valid_p
238 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
240 regcache->aspace = aspace;
241 regcache->ptid = minus_one_ptid;
246 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
248 return regcache_xmalloc_1 (gdbarch, aspace, 1);
252 regcache_xfree (struct regcache *regcache)
254 if (regcache == NULL)
256 xfree (regcache->registers);
257 xfree (regcache->register_valid_p);
262 do_regcache_xfree (void *data)
264 regcache_xfree (data);
268 make_cleanup_regcache_xfree (struct regcache *regcache)
270 return make_cleanup (do_regcache_xfree, regcache);
273 /* Return REGCACHE's architecture. */
276 get_regcache_arch (const struct regcache *regcache)
278 return regcache->descr->gdbarch;
281 struct address_space *
282 get_regcache_aspace (const struct regcache *regcache)
284 return regcache->aspace;
287 /* Return a pointer to register REGNUM's buffer cache. */
290 register_buffer (const struct regcache *regcache, int regnum)
292 return regcache->registers + regcache->descr->register_offset[regnum];
296 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
299 struct gdbarch *gdbarch = dst->descr->gdbarch;
300 gdb_byte buf[MAX_REGISTER_SIZE];
303 /* The DST should be `read-only', if it wasn't then the save would
304 end up trying to write the register values back out to the
306 gdb_assert (dst->readonly_p);
307 /* Clear the dest. */
308 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
309 memset (dst->register_valid_p, 0,
310 dst->descr->sizeof_cooked_register_valid_p);
311 /* Copy over any registers (identified by their membership in the
312 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
313 gdbarch_num_pseudo_regs) range is checked since some architectures need
314 to save/restore `cooked' registers that live in memory. */
315 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
317 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
319 int valid = cooked_read (src, regnum, buf);
323 memcpy (register_buffer (dst, regnum), buf,
324 register_size (gdbarch, regnum));
325 dst->register_valid_p[regnum] = 1;
332 regcache_restore (struct regcache *dst,
333 regcache_cooked_read_ftype *cooked_read,
334 void *cooked_read_context)
336 struct gdbarch *gdbarch = dst->descr->gdbarch;
337 gdb_byte buf[MAX_REGISTER_SIZE];
340 /* The dst had better not be read-only. If it is, the `restore'
341 doesn't make much sense. */
342 gdb_assert (!dst->readonly_p);
343 /* Copy over any registers, being careful to only restore those that
344 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
345 + gdbarch_num_pseudo_regs) range is checked since some architectures need
346 to save/restore `cooked' registers that live in memory. */
347 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
349 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
351 int valid = cooked_read (cooked_read_context, regnum, buf);
354 regcache_cooked_write (dst, regnum, buf);
360 do_cooked_read (void *src, int regnum, gdb_byte *buf)
362 struct regcache *regcache = src;
364 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
365 /* Don't even think about fetching a register from a read-only
366 cache when the register isn't yet valid. There isn't a target
367 from which the register value can be fetched. */
369 regcache_cooked_read (regcache, regnum, buf);
375 regcache_cpy (struct regcache *dst, struct regcache *src)
377 gdb_assert (src != NULL && dst != NULL);
378 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
379 gdb_assert (src != dst);
380 gdb_assert (src->readonly_p || dst->readonly_p);
382 if (!src->readonly_p)
383 regcache_save (dst, do_cooked_read, src);
384 else if (!dst->readonly_p)
385 regcache_restore (dst, do_cooked_read, src);
387 regcache_cpy_no_passthrough (dst, src);
391 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
393 gdb_assert (src != NULL && dst != NULL);
394 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
395 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
396 move of data into the current regcache. Doing this would be
397 silly - it would mean that valid_p would be completely invalid. */
398 gdb_assert (dst->readonly_p && src->readonly_p);
400 memcpy (dst->registers, src->registers,
401 dst->descr->sizeof_cooked_registers);
402 memcpy (dst->register_valid_p, src->register_valid_p,
403 dst->descr->sizeof_cooked_register_valid_p);
407 regcache_dup (struct regcache *src)
409 struct regcache *newbuf;
411 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
412 regcache_cpy (newbuf, src);
417 regcache_valid_p (const struct regcache *regcache, int regnum)
419 gdb_assert (regcache != NULL);
420 gdb_assert (regnum >= 0);
421 if (regcache->readonly_p)
422 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
424 gdb_assert (regnum < regcache->descr->nr_raw_registers);
426 return regcache->register_valid_p[regnum];
430 regcache_invalidate (struct regcache *regcache, int regnum)
432 gdb_assert (regcache != NULL);
433 gdb_assert (regnum >= 0);
434 gdb_assert (!regcache->readonly_p);
435 gdb_assert (regnum < regcache->descr->nr_raw_registers);
436 regcache->register_valid_p[regnum] = 0;
440 /* Global structure containing the current regcache. */
442 /* NOTE: this is a write-through cache. There is no "dirty" bit for
443 recording if the register values have been changed (eg. by the
444 user). Therefore all registers must be written back to the
445 target when appropriate. */
449 struct regcache *regcache;
450 struct regcache_list *next;
453 static struct regcache_list *current_regcache;
456 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
458 struct regcache_list *list;
459 struct regcache *new_regcache;
461 for (list = current_regcache; list; list = list->next)
462 if (ptid_equal (list->regcache->ptid, ptid)
463 && get_regcache_arch (list->regcache) == gdbarch)
464 return list->regcache;
466 new_regcache = regcache_xmalloc_1 (gdbarch,
467 target_thread_address_space (ptid), 0);
468 new_regcache->ptid = ptid;
469 gdb_assert (new_regcache->aspace != NULL);
471 list = xmalloc (sizeof (struct regcache_list));
472 list->regcache = new_regcache;
473 list->next = current_regcache;
474 current_regcache = list;
479 static ptid_t current_thread_ptid;
480 static struct gdbarch *current_thread_arch;
483 get_thread_regcache (ptid_t ptid)
485 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
487 current_thread_ptid = ptid;
488 current_thread_arch = target_thread_architecture (ptid);
491 return get_thread_arch_regcache (ptid, current_thread_arch);
495 get_current_regcache (void)
497 return get_thread_regcache (inferior_ptid);
501 /* Observer for the target_changed event. */
504 regcache_observer_target_changed (struct target_ops *target)
506 registers_changed ();
509 /* Update global variables old ptids to hold NEW_PTID if they were
512 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
514 struct regcache_list *list;
516 for (list = current_regcache; list; list = list->next)
517 if (ptid_equal (list->regcache->ptid, old_ptid))
518 list->regcache->ptid = new_ptid;
521 /* Low level examining and depositing of registers.
523 The caller is responsible for making sure that the inferior is
524 stopped before calling the fetching routines, or it will get
525 garbage. (a change from GDB version 3, in which the caller got the
526 value from the last stop). */
528 /* REGISTERS_CHANGED ()
530 Indicate that registers may have changed, so invalidate the cache. */
533 registers_changed_ptid (ptid_t ptid)
535 struct regcache_list *list, **list_link;
537 list = current_regcache;
538 list_link = ¤t_regcache;
541 if (ptid_match (list->regcache->ptid, ptid))
543 struct regcache_list *dead = list;
545 *list_link = list->next;
546 regcache_xfree (list->regcache);
552 list_link = &list->next;
556 current_regcache = NULL;
558 current_thread_ptid = null_ptid;
559 current_thread_arch = NULL;
561 /* Need to forget about any frames we have cached, too. */
562 reinit_frame_cache ();
564 /* Force cleanup of any alloca areas if using C alloca instead of
565 a builtin alloca. This particular call is used to clean up
566 areas allocated by low level target code which may build up
567 during lengthy interactions between gdb and the target before
568 gdb gives control to the user (ie watchpoints). */
573 registers_changed (void)
575 registers_changed_ptid (minus_one_ptid);
579 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
581 gdb_assert (regcache != NULL && buf != NULL);
582 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
583 /* Make certain that the register cache is up-to-date with respect
584 to the current thread. This switching shouldn't be necessary
585 only there is still only one target side register cache. Sigh!
586 On the bright side, at least there is a regcache object. */
587 if (!regcache->readonly_p)
589 if (!regcache_valid_p (regcache, regnum))
591 struct cleanup *old_chain = save_inferior_ptid ();
593 inferior_ptid = regcache->ptid;
594 target_fetch_registers (regcache, regnum);
595 do_cleanups (old_chain);
598 /* FIXME: cagney/2004-08-07: At present a number of targets
599 forget (or didn't know that they needed) to set this leading to
600 panics. Also is the problem that targets need to indicate
601 that a register is in one of the possible states: valid,
602 undefined, unknown. The last of which isn't yet
604 gdb_assert (regcache_valid_p (regcache, regnum));
607 /* Copy the value directly into the register cache. */
608 memcpy (buf, register_buffer (regcache, regnum),
609 regcache->descr->sizeof_register[regnum]);
613 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
617 gdb_assert (regcache != NULL);
618 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
619 buf = alloca (regcache->descr->sizeof_register[regnum]);
620 regcache_raw_read (regcache, regnum, buf);
621 (*val) = extract_signed_integer
622 (buf, regcache->descr->sizeof_register[regnum],
623 gdbarch_byte_order (regcache->descr->gdbarch));
627 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
632 gdb_assert (regcache != NULL);
633 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
634 buf = alloca (regcache->descr->sizeof_register[regnum]);
635 regcache_raw_read (regcache, regnum, buf);
636 (*val) = extract_unsigned_integer
637 (buf, regcache->descr->sizeof_register[regnum],
638 gdbarch_byte_order (regcache->descr->gdbarch));
642 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
646 gdb_assert (regcache != NULL);
647 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
648 buf = alloca (regcache->descr->sizeof_register[regnum]);
649 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
650 gdbarch_byte_order (regcache->descr->gdbarch), val);
651 regcache_raw_write (regcache, regnum, buf);
655 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
660 gdb_assert (regcache != NULL);
661 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
662 buf = alloca (regcache->descr->sizeof_register[regnum]);
663 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
664 gdbarch_byte_order (regcache->descr->gdbarch), val);
665 regcache_raw_write (regcache, regnum, buf);
669 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
671 gdb_assert (regnum >= 0);
672 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
673 if (regnum < regcache->descr->nr_raw_registers)
674 regcache_raw_read (regcache, regnum, buf);
675 else if (regcache->readonly_p
676 && regnum < regcache->descr->nr_cooked_registers
677 && regcache->register_valid_p[regnum])
678 /* Read-only register cache, perhaps the cooked value was cached? */
679 memcpy (buf, register_buffer (regcache, regnum),
680 regcache->descr->sizeof_register[regnum]);
682 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
687 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
692 gdb_assert (regcache != NULL);
693 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
694 buf = alloca (regcache->descr->sizeof_register[regnum]);
695 regcache_cooked_read (regcache, regnum, buf);
696 (*val) = extract_signed_integer
697 (buf, regcache->descr->sizeof_register[regnum],
698 gdbarch_byte_order (regcache->descr->gdbarch));
702 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
707 gdb_assert (regcache != NULL);
708 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
709 buf = alloca (regcache->descr->sizeof_register[regnum]);
710 regcache_cooked_read (regcache, regnum, buf);
711 (*val) = extract_unsigned_integer
712 (buf, regcache->descr->sizeof_register[regnum],
713 gdbarch_byte_order (regcache->descr->gdbarch));
717 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
722 gdb_assert (regcache != NULL);
723 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
724 buf = alloca (regcache->descr->sizeof_register[regnum]);
725 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
726 gdbarch_byte_order (regcache->descr->gdbarch), val);
727 regcache_cooked_write (regcache, regnum, buf);
731 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
736 gdb_assert (regcache != NULL);
737 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
738 buf = alloca (regcache->descr->sizeof_register[regnum]);
739 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
740 gdbarch_byte_order (regcache->descr->gdbarch), val);
741 regcache_cooked_write (regcache, regnum, buf);
745 regcache_raw_write (struct regcache *regcache, int regnum,
748 struct cleanup *old_chain;
750 gdb_assert (regcache != NULL && buf != NULL);
751 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
752 gdb_assert (!regcache->readonly_p);
754 /* On the sparc, writing %g0 is a no-op, so we don't even want to
755 change the registers array if something writes to this register. */
756 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
759 /* If we have a valid copy of the register, and new value == old
760 value, then don't bother doing the actual store. */
761 if (regcache_valid_p (regcache, regnum)
762 && (memcmp (register_buffer (regcache, regnum), buf,
763 regcache->descr->sizeof_register[regnum]) == 0))
766 old_chain = save_inferior_ptid ();
767 inferior_ptid = regcache->ptid;
769 target_prepare_to_store (regcache);
770 memcpy (register_buffer (regcache, regnum), buf,
771 regcache->descr->sizeof_register[regnum]);
772 regcache->register_valid_p[regnum] = 1;
773 target_store_registers (regcache, regnum);
775 do_cleanups (old_chain);
779 regcache_cooked_write (struct regcache *regcache, int regnum,
782 gdb_assert (regnum >= 0);
783 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
784 if (regnum < regcache->descr->nr_raw_registers)
785 regcache_raw_write (regcache, regnum, buf);
787 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
791 /* Perform a partial register transfer using a read, modify, write
794 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
796 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
800 regcache_xfer_part (struct regcache *regcache, int regnum,
801 int offset, int len, void *in, const void *out,
802 void (*read) (struct regcache *regcache, int regnum,
804 void (*write) (struct regcache *regcache, int regnum,
805 const gdb_byte *buf))
807 struct regcache_descr *descr = regcache->descr;
808 gdb_byte reg[MAX_REGISTER_SIZE];
810 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
811 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
812 /* Something to do? */
813 if (offset + len == 0)
815 /* Read (when needed) ... */
818 || offset + len < descr->sizeof_register[regnum])
820 gdb_assert (read != NULL);
821 read (regcache, regnum, reg);
825 memcpy (in, reg + offset, len);
827 memcpy (reg + offset, out, len);
828 /* ... write (when needed). */
831 gdb_assert (write != NULL);
832 write (regcache, regnum, reg);
837 regcache_raw_read_part (struct regcache *regcache, int regnum,
838 int offset, int len, gdb_byte *buf)
840 struct regcache_descr *descr = regcache->descr;
842 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
843 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
844 regcache_raw_read, regcache_raw_write);
848 regcache_raw_write_part (struct regcache *regcache, int regnum,
849 int offset, int len, const gdb_byte *buf)
851 struct regcache_descr *descr = regcache->descr;
853 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
854 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
855 regcache_raw_read, regcache_raw_write);
859 regcache_cooked_read_part (struct regcache *regcache, int regnum,
860 int offset, int len, gdb_byte *buf)
862 struct regcache_descr *descr = regcache->descr;
864 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
865 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
866 regcache_cooked_read, regcache_cooked_write);
870 regcache_cooked_write_part (struct regcache *regcache, int regnum,
871 int offset, int len, const gdb_byte *buf)
873 struct regcache_descr *descr = regcache->descr;
875 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
876 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
877 regcache_cooked_read, regcache_cooked_write);
880 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
883 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
888 gdb_assert (regcache != NULL);
889 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
890 gdb_assert (!regcache->readonly_p);
892 regbuf = register_buffer (regcache, regnum);
893 size = regcache->descr->sizeof_register[regnum];
896 memcpy (regbuf, buf, size);
898 memset (regbuf, 0, size);
900 /* Mark the register as cached. */
901 regcache->register_valid_p[regnum] = 1;
904 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
907 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
912 gdb_assert (regcache != NULL && buf != NULL);
913 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
915 regbuf = register_buffer (regcache, regnum);
916 size = regcache->descr->sizeof_register[regnum];
917 memcpy (buf, regbuf, size);
921 /* Special handling for register PC. */
924 regcache_read_pc (struct regcache *regcache)
926 struct gdbarch *gdbarch = get_regcache_arch (regcache);
930 if (gdbarch_read_pc_p (gdbarch))
931 pc_val = gdbarch_read_pc (gdbarch, regcache);
932 /* Else use per-frame method on get_current_frame. */
933 else if (gdbarch_pc_regnum (gdbarch) >= 0)
937 regcache_cooked_read_unsigned (regcache,
938 gdbarch_pc_regnum (gdbarch),
940 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
943 internal_error (__FILE__, __LINE__,
944 _("regcache_read_pc: Unable to find PC"));
949 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
951 struct gdbarch *gdbarch = get_regcache_arch (regcache);
953 if (gdbarch_write_pc_p (gdbarch))
954 gdbarch_write_pc (gdbarch, regcache, pc);
955 else if (gdbarch_pc_regnum (gdbarch) >= 0)
956 regcache_cooked_write_unsigned (regcache,
957 gdbarch_pc_regnum (gdbarch), pc);
959 internal_error (__FILE__, __LINE__,
960 _("regcache_write_pc: Unable to update PC"));
962 /* Writing the PC (for instance, from "load") invalidates the
964 reinit_frame_cache ();
969 reg_flush_command (char *command, int from_tty)
971 /* Force-flush the register cache. */
972 registers_changed ();
974 printf_filtered (_("Register cache flushed.\n"));
978 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
979 const unsigned char *buf, long len)
986 for (i = 0; i < len; i++)
987 fprintf_unfiltered (file, "%02x", buf[i]);
989 case BFD_ENDIAN_LITTLE:
990 for (i = len - 1; i >= 0; i--)
991 fprintf_unfiltered (file, "%02x", buf[i]);
994 internal_error (__FILE__, __LINE__, _("Bad switch"));
998 enum regcache_dump_what
1000 regcache_dump_none, regcache_dump_raw,
1001 regcache_dump_cooked, regcache_dump_groups
1005 regcache_dump (struct regcache *regcache, struct ui_file *file,
1006 enum regcache_dump_what what_to_dump)
1008 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1009 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1011 int footnote_nr = 0;
1012 int footnote_register_size = 0;
1013 int footnote_register_offset = 0;
1014 int footnote_register_type_name_null = 0;
1015 long register_offset = 0;
1016 unsigned char buf[MAX_REGISTER_SIZE];
1019 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1020 regcache->descr->nr_raw_registers);
1021 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1022 regcache->descr->nr_cooked_registers);
1023 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1024 regcache->descr->sizeof_raw_registers);
1025 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1026 regcache->descr->sizeof_raw_register_valid_p);
1027 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1028 gdbarch_num_regs (gdbarch));
1029 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1030 gdbarch_num_pseudo_regs (gdbarch));
1033 gdb_assert (regcache->descr->nr_cooked_registers
1034 == (gdbarch_num_regs (gdbarch)
1035 + gdbarch_num_pseudo_regs (gdbarch)));
1037 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1041 fprintf_unfiltered (file, " %-10s", "Name");
1044 const char *p = gdbarch_register_name (gdbarch, regnum);
1048 else if (p[0] == '\0')
1050 fprintf_unfiltered (file, " %-10s", p);
1055 fprintf_unfiltered (file, " %4s", "Nr");
1057 fprintf_unfiltered (file, " %4d", regnum);
1059 /* Relative number. */
1061 fprintf_unfiltered (file, " %4s", "Rel");
1062 else if (regnum < gdbarch_num_regs (gdbarch))
1063 fprintf_unfiltered (file, " %4d", regnum);
1065 fprintf_unfiltered (file, " %4d",
1066 (regnum - gdbarch_num_regs (gdbarch)));
1070 fprintf_unfiltered (file, " %6s ", "Offset");
1073 fprintf_unfiltered (file, " %6ld",
1074 regcache->descr->register_offset[regnum]);
1075 if (register_offset != regcache->descr->register_offset[regnum]
1077 && (regcache->descr->register_offset[regnum]
1078 != (regcache->descr->register_offset[regnum - 1]
1079 + regcache->descr->sizeof_register[regnum - 1])))
1082 if (!footnote_register_offset)
1083 footnote_register_offset = ++footnote_nr;
1084 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1087 fprintf_unfiltered (file, " ");
1088 register_offset = (regcache->descr->register_offset[regnum]
1089 + regcache->descr->sizeof_register[regnum]);
1094 fprintf_unfiltered (file, " %5s ", "Size");
1096 fprintf_unfiltered (file, " %5ld",
1097 regcache->descr->sizeof_register[regnum]);
1107 static const char blt[] = "builtin_type";
1109 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1114 if (!footnote_register_type_name_null)
1115 footnote_register_type_name_null = ++footnote_nr;
1116 n = xstrprintf ("*%d", footnote_register_type_name_null);
1117 make_cleanup (xfree, n);
1120 /* Chop a leading builtin_type. */
1121 if (strncmp (t, blt, strlen (blt)) == 0)
1124 fprintf_unfiltered (file, " %-15s", t);
1127 /* Leading space always present. */
1128 fprintf_unfiltered (file, " ");
1131 if (what_to_dump == regcache_dump_raw)
1134 fprintf_unfiltered (file, "Raw value");
1135 else if (regnum >= regcache->descr->nr_raw_registers)
1136 fprintf_unfiltered (file, "<cooked>");
1137 else if (!regcache_valid_p (regcache, regnum))
1138 fprintf_unfiltered (file, "<invalid>");
1141 regcache_raw_read (regcache, regnum, buf);
1142 fprintf_unfiltered (file, "0x");
1143 dump_endian_bytes (file,
1144 gdbarch_byte_order (gdbarch), buf,
1145 regcache->descr->sizeof_register[regnum]);
1149 /* Value, cooked. */
1150 if (what_to_dump == regcache_dump_cooked)
1153 fprintf_unfiltered (file, "Cooked value");
1156 regcache_cooked_read (regcache, regnum, buf);
1157 fprintf_unfiltered (file, "0x");
1158 dump_endian_bytes (file,
1159 gdbarch_byte_order (gdbarch), buf,
1160 regcache->descr->sizeof_register[regnum]);
1164 /* Group members. */
1165 if (what_to_dump == regcache_dump_groups)
1168 fprintf_unfiltered (file, "Groups");
1171 const char *sep = "";
1172 struct reggroup *group;
1174 for (group = reggroup_next (gdbarch, NULL);
1176 group = reggroup_next (gdbarch, group))
1178 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1180 fprintf_unfiltered (file,
1181 "%s%s", sep, reggroup_name (group));
1188 fprintf_unfiltered (file, "\n");
1191 if (footnote_register_size)
1192 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1193 footnote_register_size);
1194 if (footnote_register_offset)
1195 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1196 footnote_register_offset);
1197 if (footnote_register_type_name_null)
1198 fprintf_unfiltered (file,
1199 "*%d: Register type's name NULL.\n",
1200 footnote_register_type_name_null);
1201 do_cleanups (cleanups);
1205 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1208 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1211 struct cleanup *cleanups;
1212 struct ui_file *file = gdb_fopen (args, "w");
1215 perror_with_name (_("maintenance print architecture"));
1216 cleanups = make_cleanup_ui_file_delete (file);
1217 regcache_dump (get_current_regcache (), file, what_to_dump);
1218 do_cleanups (cleanups);
1223 maintenance_print_registers (char *args, int from_tty)
1225 regcache_print (args, regcache_dump_none);
1229 maintenance_print_raw_registers (char *args, int from_tty)
1231 regcache_print (args, regcache_dump_raw);
1235 maintenance_print_cooked_registers (char *args, int from_tty)
1237 regcache_print (args, regcache_dump_cooked);
1241 maintenance_print_register_groups (char *args, int from_tty)
1243 regcache_print (args, regcache_dump_groups);
1246 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1249 _initialize_regcache (void)
1251 regcache_descr_handle
1252 = gdbarch_data_register_post_init (init_regcache_descr);
1254 observer_attach_target_changed (regcache_observer_target_changed);
1255 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1257 add_com ("flushregs", class_maintenance, reg_flush_command,
1258 _("Force gdb to flush its register cache (maintainer command)"));
1260 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1261 _("Print the internal register configuration.\n"
1262 "Takes an optional file parameter."), &maintenanceprintlist);
1263 add_cmd ("raw-registers", class_maintenance,
1264 maintenance_print_raw_registers,
1265 _("Print the internal register configuration "
1266 "including raw values.\n"
1267 "Takes an optional file parameter."), &maintenanceprintlist);
1268 add_cmd ("cooked-registers", class_maintenance,
1269 maintenance_print_cooked_registers,
1270 _("Print the internal register configuration "
1271 "including cooked values.\n"
1272 "Takes an optional file parameter."), &maintenanceprintlist);
1273 add_cmd ("register-groups", class_maintenance,
1274 maintenance_print_register_groups,
1275 _("Print the internal register configuration "
1276 "including each register's group.\n"
1277 "Takes an optional file parameter."),
1278 &maintenanceprintlist);