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 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 #include "reggroups.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include "gdbcmd.h" /* For maintenanceprintlist. */
38 * Here is the actual register cache.
41 /* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created */
44 struct gdbarch_data *regcache_descr_handle;
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch *gdbarch;
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
54 registers then those registers and not the PC lives in the raw
57 long sizeof_raw_registers;
58 long sizeof_raw_register_valid_p;
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
64 both raw registers and memory by the architecture methods
65 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
66 int nr_cooked_registers;
67 long sizeof_cooked_registers;
68 long sizeof_cooked_register_valid_p;
70 /* Offset and size (in 8 bit bytes), of reach register in the
71 register cache. All registers (including those in the range
72 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
73 Assigning all registers an offset makes it possible to keep
74 legacy code, such as that found in read_register_bytes() and
75 write_register_bytes() working. */
76 long *register_offset;
77 long *sizeof_register;
79 /* Cached table containing the type of each register. */
80 struct type **register_type;
84 init_regcache_descr (struct gdbarch *gdbarch)
87 struct regcache_descr *descr;
88 gdb_assert (gdbarch != NULL);
90 /* Create an initial, zero filled, table. */
91 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
92 descr->gdbarch = gdbarch;
94 /* Total size of the register space. The raw registers are mapped
95 directly onto the raw register cache while the pseudo's are
96 either mapped onto raw-registers or memory. */
97 descr->nr_cooked_registers = gdbarch_num_regs (current_gdbarch)
98 + gdbarch_num_pseudo_regs (current_gdbarch);
99 descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (current_gdbarch)
100 + gdbarch_num_pseudo_regs
103 /* Fill in a table of register types. */
105 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
106 for (i = 0; i < descr->nr_cooked_registers; i++)
107 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
109 /* Construct a strictly RAW register cache. Don't allow pseudo's
110 into the register cache. */
111 descr->nr_raw_registers = gdbarch_num_regs (current_gdbarch);
113 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
114 array. This pretects GDB from erant code that accesses elements
115 of the global register_valid_p[] array in the range
116 [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
117 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
119 /* Lay out the register cache.
121 NOTE: cagney/2002-05-22: Only register_type() is used when
122 constructing the register cache. It is assumed that the
123 register's raw size, virtual size and type length are all the
128 descr->sizeof_register
129 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
130 descr->register_offset
131 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
132 for (i = 0; i < descr->nr_cooked_registers; i++)
134 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
135 descr->register_offset[i] = offset;
136 offset += descr->sizeof_register[i];
137 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
139 /* Set the real size of the register cache buffer. */
140 descr->sizeof_cooked_registers = offset;
143 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
144 the raw registers. Unfortunately some code still accesses the
145 register array directly using the global registers[]. Until that
146 code has been purged, play safe and over allocating the register
148 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
153 static struct regcache_descr *
154 regcache_descr (struct gdbarch *gdbarch)
156 return gdbarch_data (gdbarch, regcache_descr_handle);
159 /* Utility functions returning useful register attributes stored in
160 the regcache descr. */
163 register_type (struct gdbarch *gdbarch, int regnum)
165 struct regcache_descr *descr = regcache_descr (gdbarch);
166 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
167 return descr->register_type[regnum];
170 /* Utility functions returning useful register attributes stored in
171 the regcache descr. */
174 register_size (struct gdbarch *gdbarch, int regnum)
176 struct regcache_descr *descr = regcache_descr (gdbarch);
178 gdb_assert (regnum >= 0
179 && regnum < (gdbarch_num_regs (current_gdbarch)
180 + gdbarch_num_pseudo_regs (current_gdbarch)));
181 size = descr->sizeof_register[regnum];
185 /* The register cache for storing raw register values. */
189 struct regcache_descr *descr;
190 /* The register buffers. A read-only register cache can hold the
191 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
192 register cache can only hold [0 .. gdbarch_num_regs). */
194 /* Register cache status:
195 register_valid_p[REG] == 0 if REG value is not in the cache
196 > 0 if REG value is in the cache
197 < 0 if REG value is permanently unavailable */
198 signed char *register_valid_p;
199 /* Is this a read-only cache? A read-only cache is used for saving
200 the target's register state (e.g, across an inferior function
201 call or just before forcing a function return). A read-only
202 cache can only be updated via the methods regcache_dup() and
203 regcache_cpy(). The actual contents are determined by the
204 reggroup_save and reggroup_restore methods. */
209 regcache_xmalloc (struct gdbarch *gdbarch)
211 struct regcache_descr *descr;
212 struct regcache *regcache;
213 gdb_assert (gdbarch != NULL);
214 descr = regcache_descr (gdbarch);
215 regcache = XMALLOC (struct regcache);
216 regcache->descr = descr;
218 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
219 regcache->register_valid_p
220 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
221 regcache->readonly_p = 1;
226 regcache_xfree (struct regcache *regcache)
228 if (regcache == NULL)
230 xfree (regcache->registers);
231 xfree (regcache->register_valid_p);
236 do_regcache_xfree (void *data)
238 regcache_xfree (data);
242 make_cleanup_regcache_xfree (struct regcache *regcache)
244 return make_cleanup (do_regcache_xfree, regcache);
247 /* Return REGCACHE's architecture. */
250 get_regcache_arch (const struct regcache *regcache)
252 return regcache->descr->gdbarch;
255 /* Return a pointer to register REGNUM's buffer cache. */
258 register_buffer (const struct regcache *regcache, int regnum)
260 return regcache->registers + regcache->descr->register_offset[regnum];
264 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
267 struct gdbarch *gdbarch = dst->descr->gdbarch;
268 gdb_byte buf[MAX_REGISTER_SIZE];
270 /* The DST should be `read-only', if it wasn't then the save would
271 end up trying to write the register values back out to the
273 gdb_assert (dst->readonly_p);
274 /* Clear the dest. */
275 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
276 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
277 /* Copy over any registers (identified by their membership in the
278 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
279 gdbarch_num_pseudo_regs) range is checked since some architectures need
280 to save/restore `cooked' registers that live in memory. */
281 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
283 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
285 int valid = cooked_read (src, regnum, buf);
288 memcpy (register_buffer (dst, regnum), buf,
289 register_size (gdbarch, regnum));
290 dst->register_valid_p[regnum] = 1;
297 regcache_restore (struct regcache *dst,
298 regcache_cooked_read_ftype *cooked_read,
299 void *cooked_read_context)
301 struct gdbarch *gdbarch = dst->descr->gdbarch;
302 gdb_byte buf[MAX_REGISTER_SIZE];
304 /* The dst had better not be read-only. If it is, the `restore'
305 doesn't make much sense. */
306 gdb_assert (!dst->readonly_p);
307 /* Copy over any registers, being careful to only restore those that
308 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
309 + gdbarch_num_pseudo_regs) range is checked since some architectures need
310 to save/restore `cooked' registers that live in memory. */
311 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
313 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
315 int valid = cooked_read (cooked_read_context, regnum, buf);
317 regcache_cooked_write (dst, regnum, buf);
323 do_cooked_read (void *src, int regnum, gdb_byte *buf)
325 struct regcache *regcache = src;
326 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
327 /* Don't even think about fetching a register from a read-only
328 cache when the register isn't yet valid. There isn't a target
329 from which the register value can be fetched. */
331 regcache_cooked_read (regcache, regnum, buf);
337 regcache_cpy (struct regcache *dst, struct regcache *src)
341 gdb_assert (src != NULL && dst != NULL);
342 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
343 gdb_assert (src != dst);
344 gdb_assert (src->readonly_p || dst->readonly_p);
345 if (!src->readonly_p)
346 regcache_save (dst, do_cooked_read, src);
347 else if (!dst->readonly_p)
348 regcache_restore (dst, do_cooked_read, src);
350 regcache_cpy_no_passthrough (dst, src);
354 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
357 gdb_assert (src != NULL && dst != NULL);
358 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
359 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
360 move of data into the current_regcache(). Doing this would be
361 silly - it would mean that valid_p would be completely invalid. */
362 gdb_assert (dst != current_regcache);
363 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
364 memcpy (dst->register_valid_p, src->register_valid_p,
365 dst->descr->sizeof_raw_register_valid_p);
369 regcache_dup (struct regcache *src)
371 struct regcache *newbuf;
372 gdb_assert (current_regcache != NULL);
373 newbuf = regcache_xmalloc (src->descr->gdbarch);
374 regcache_cpy (newbuf, src);
379 regcache_dup_no_passthrough (struct regcache *src)
381 struct regcache *newbuf;
382 gdb_assert (current_regcache != NULL);
383 newbuf = regcache_xmalloc (src->descr->gdbarch);
384 regcache_cpy_no_passthrough (newbuf, src);
389 regcache_valid_p (const struct regcache *regcache, int regnum)
391 gdb_assert (regcache != NULL);
392 gdb_assert (regnum >= 0);
393 if (regcache->readonly_p)
394 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
396 gdb_assert (regnum < regcache->descr->nr_raw_registers);
398 return regcache->register_valid_p[regnum];
402 regcache_invalidate (struct regcache *regcache, int regnum)
404 gdb_assert (regcache != NULL);
405 gdb_assert (regnum >= 0);
406 gdb_assert (!regcache->readonly_p);
407 gdb_assert (regnum < regcache->descr->nr_raw_registers);
408 regcache->register_valid_p[regnum] = 0;
412 /* Global structure containing the current regcache. */
413 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
414 deprecated_register_valid[] currently point into this structure. */
415 struct regcache *current_regcache;
417 /* NOTE: this is a write-through cache. There is no "dirty" bit for
418 recording if the register values have been changed (eg. by the
419 user). Therefore all registers must be written back to the
420 target when appropriate. */
422 /* The thread/process associated with the current set of registers. */
424 static ptid_t registers_ptid;
426 /* Observer for the target_changed event. */
429 regcache_observer_target_changed (struct target_ops *target)
431 registers_changed ();
434 /* Low level examining and depositing of registers.
436 The caller is responsible for making sure that the inferior is
437 stopped before calling the fetching routines, or it will get
438 garbage. (a change from GDB version 3, in which the caller got the
439 value from the last stop). */
441 /* REGISTERS_CHANGED ()
443 Indicate that registers may have changed, so invalidate the cache. */
446 registers_changed (void)
450 registers_ptid = pid_to_ptid (-1);
452 /* Force cleanup of any alloca areas if using C alloca instead of
453 a builtin alloca. This particular call is used to clean up
454 areas allocated by low level target code which may build up
455 during lengthy interactions between gdb and the target before
456 gdb gives control to the user (ie watchpoints). */
459 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
460 regcache_invalidate (current_regcache, i);
465 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
467 gdb_assert (regcache != NULL && buf != NULL);
468 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
469 /* Make certain that the register cache is up-to-date with respect
470 to the current thread. This switching shouldn't be necessary
471 only there is still only one target side register cache. Sigh!
472 On the bright side, at least there is a regcache object. */
473 if (!regcache->readonly_p)
475 gdb_assert (regcache == current_regcache);
476 if (! ptid_equal (registers_ptid, inferior_ptid))
478 registers_changed ();
479 registers_ptid = inferior_ptid;
481 if (!regcache_valid_p (regcache, regnum))
482 target_fetch_registers (regcache, regnum);
484 /* FIXME: cagney/2004-08-07: At present a number of targets
485 forget (or didn't know that they needed) to set this leading to
486 panics. Also is the problem that targets need to indicate
487 that a register is in one of the possible states: valid,
488 undefined, unknown. The last of which isn't yet
490 gdb_assert (regcache_valid_p (regcache, regnum));
493 /* Copy the value directly into the register cache. */
494 memcpy (buf, register_buffer (regcache, regnum),
495 regcache->descr->sizeof_register[regnum]);
499 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
502 gdb_assert (regcache != NULL);
503 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
504 buf = alloca (regcache->descr->sizeof_register[regnum]);
505 regcache_raw_read (regcache, regnum, buf);
506 (*val) = extract_signed_integer (buf,
507 regcache->descr->sizeof_register[regnum]);
511 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
515 gdb_assert (regcache != NULL);
516 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
517 buf = alloca (regcache->descr->sizeof_register[regnum]);
518 regcache_raw_read (regcache, regnum, buf);
519 (*val) = extract_unsigned_integer (buf,
520 regcache->descr->sizeof_register[regnum]);
524 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
527 gdb_assert (regcache != NULL);
528 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
529 buf = alloca (regcache->descr->sizeof_register[regnum]);
530 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
531 regcache_raw_write (regcache, regnum, buf);
535 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
539 gdb_assert (regcache != NULL);
540 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
541 buf = alloca (regcache->descr->sizeof_register[regnum]);
542 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
543 regcache_raw_write (regcache, regnum, buf);
547 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
549 gdb_assert (regnum >= 0);
550 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
551 if (regnum < regcache->descr->nr_raw_registers)
552 regcache_raw_read (regcache, regnum, buf);
553 else if (regcache->readonly_p
554 && regnum < regcache->descr->nr_cooked_registers
555 && regcache->register_valid_p[regnum])
556 /* Read-only register cache, perhaps the cooked value was cached? */
557 memcpy (buf, register_buffer (regcache, regnum),
558 regcache->descr->sizeof_register[regnum]);
560 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
565 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
569 gdb_assert (regcache != NULL);
570 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
571 buf = alloca (regcache->descr->sizeof_register[regnum]);
572 regcache_cooked_read (regcache, regnum, buf);
573 (*val) = extract_signed_integer (buf,
574 regcache->descr->sizeof_register[regnum]);
578 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
582 gdb_assert (regcache != NULL);
583 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
584 buf = alloca (regcache->descr->sizeof_register[regnum]);
585 regcache_cooked_read (regcache, regnum, buf);
586 (*val) = extract_unsigned_integer (buf,
587 regcache->descr->sizeof_register[regnum]);
591 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
595 gdb_assert (regcache != NULL);
596 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
597 buf = alloca (regcache->descr->sizeof_register[regnum]);
598 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
599 regcache_cooked_write (regcache, regnum, buf);
603 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
607 gdb_assert (regcache != NULL);
608 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
609 buf = alloca (regcache->descr->sizeof_register[regnum]);
610 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
611 regcache_cooked_write (regcache, regnum, buf);
615 regcache_raw_write (struct regcache *regcache, int regnum,
618 gdb_assert (regcache != NULL && buf != NULL);
619 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
620 gdb_assert (!regcache->readonly_p);
622 /* On the sparc, writing %g0 is a no-op, so we don't even want to
623 change the registers array if something writes to this register. */
624 if (gdbarch_cannot_store_register (current_gdbarch, regnum))
627 /* Make certain that the correct cache is selected. */
628 gdb_assert (regcache == current_regcache);
629 if (! ptid_equal (registers_ptid, inferior_ptid))
631 registers_changed ();
632 registers_ptid = inferior_ptid;
635 /* If we have a valid copy of the register, and new value == old
636 value, then don't bother doing the actual store. */
637 if (regcache_valid_p (regcache, regnum)
638 && (memcmp (register_buffer (regcache, regnum), buf,
639 regcache->descr->sizeof_register[regnum]) == 0))
642 target_prepare_to_store (regcache);
643 memcpy (register_buffer (regcache, regnum), buf,
644 regcache->descr->sizeof_register[regnum]);
645 regcache->register_valid_p[regnum] = 1;
646 target_store_registers (regcache, regnum);
650 regcache_cooked_write (struct regcache *regcache, int regnum,
653 gdb_assert (regnum >= 0);
654 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
655 if (regnum < regcache->descr->nr_raw_registers)
656 regcache_raw_write (regcache, regnum, buf);
658 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
662 /* Perform a partial register transfer using a read, modify, write
665 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
667 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
671 regcache_xfer_part (struct regcache *regcache, int regnum,
672 int offset, int len, void *in, const void *out,
673 void (*read) (struct regcache *regcache, int regnum,
675 void (*write) (struct regcache *regcache, int regnum,
676 const gdb_byte *buf))
678 struct regcache_descr *descr = regcache->descr;
679 gdb_byte reg[MAX_REGISTER_SIZE];
680 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
681 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
682 /* Something to do? */
683 if (offset + len == 0)
685 /* Read (when needed) ... */
688 || offset + len < descr->sizeof_register[regnum])
690 gdb_assert (read != NULL);
691 read (regcache, regnum, reg);
695 memcpy (in, reg + offset, len);
697 memcpy (reg + offset, out, len);
698 /* ... write (when needed). */
701 gdb_assert (write != NULL);
702 write (regcache, regnum, reg);
707 regcache_raw_read_part (struct regcache *regcache, int regnum,
708 int offset, int len, gdb_byte *buf)
710 struct regcache_descr *descr = regcache->descr;
711 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
712 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
713 regcache_raw_read, regcache_raw_write);
717 regcache_raw_write_part (struct regcache *regcache, int regnum,
718 int offset, int len, const gdb_byte *buf)
720 struct regcache_descr *descr = regcache->descr;
721 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
722 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
723 regcache_raw_read, regcache_raw_write);
727 regcache_cooked_read_part (struct regcache *regcache, int regnum,
728 int offset, int len, gdb_byte *buf)
730 struct regcache_descr *descr = regcache->descr;
731 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
732 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
733 regcache_cooked_read, regcache_cooked_write);
737 regcache_cooked_write_part (struct regcache *regcache, int regnum,
738 int offset, int len, const gdb_byte *buf)
740 struct regcache_descr *descr = regcache->descr;
741 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
742 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
743 regcache_cooked_read, regcache_cooked_write);
746 /* Hack to keep code that view the register buffer as raw bytes
750 register_offset_hack (struct gdbarch *gdbarch, int regnum)
752 struct regcache_descr *descr = regcache_descr (gdbarch);
753 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
754 return descr->register_offset[regnum];
757 /* Return the contents of register REGNUM as an unsigned integer. */
760 read_register (int regnum)
762 gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
763 gdb_assert (current_regcache != NULL);
764 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
765 regcache_cooked_read (current_regcache, regnum, buf);
766 return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
770 read_register_pid (int regnum, ptid_t ptid)
776 if (ptid_equal (ptid, inferior_ptid))
777 return read_register (regnum);
779 save_ptid = inferior_ptid;
781 inferior_ptid = ptid;
783 retval = read_register (regnum);
785 inferior_ptid = save_ptid;
790 /* Store VALUE into the raw contents of register number REGNUM. */
793 write_register (int regnum, LONGEST val)
797 size = register_size (current_gdbarch, regnum);
799 store_signed_integer (buf, size, (LONGEST) val);
800 gdb_assert (current_regcache != NULL);
801 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
802 regcache_cooked_write (current_regcache, regnum, buf);
806 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
810 if (ptid_equal (ptid, inferior_ptid))
812 write_register (regnum, val);
816 save_ptid = inferior_ptid;
818 inferior_ptid = ptid;
820 write_register (regnum, val);
822 inferior_ptid = save_ptid;
825 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
828 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
833 gdb_assert (regcache != NULL);
834 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
835 gdb_assert (!regcache->readonly_p);
837 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
838 CURRENT_REGCACHE specially here. */
839 if (regcache == current_regcache
840 && !ptid_equal (registers_ptid, inferior_ptid))
842 registers_changed ();
843 registers_ptid = inferior_ptid;
846 regbuf = register_buffer (regcache, regnum);
847 size = regcache->descr->sizeof_register[regnum];
850 memcpy (regbuf, buf, size);
852 memset (regbuf, 0, size);
854 /* Mark the register as cached. */
855 regcache->register_valid_p[regnum] = 1;
858 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
861 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
866 gdb_assert (regcache != NULL && buf != NULL);
867 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
869 regbuf = register_buffer (regcache, regnum);
870 size = regcache->descr->sizeof_register[regnum];
871 memcpy (buf, regbuf, size);
875 /* read_pc, write_pc, etc. Special handling for register PC. */
877 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
878 read_sp(), will eventually be replaced by per-frame methods.
879 Instead of relying on the global INFERIOR_PTID, they will use the
880 contextual information provided by the FRAME. These functions do
881 not belong in the register cache. */
883 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
884 write_pc_pid() and write_pc(), all need to be replaced by something
885 that does not rely on global state. But what? */
888 read_pc_pid (ptid_t ptid)
890 struct regcache *regcache = current_regcache;
891 struct gdbarch *gdbarch = get_regcache_arch (regcache);
893 ptid_t saved_inferior_ptid;
896 /* In case ptid != inferior_ptid. */
897 saved_inferior_ptid = inferior_ptid;
898 inferior_ptid = ptid;
900 if (gdbarch_read_pc_p (gdbarch))
901 pc_val = gdbarch_read_pc (gdbarch, regcache);
902 /* Else use per-frame method on get_current_frame. */
903 else if (PC_REGNUM >= 0)
906 regcache_cooked_read_unsigned (regcache, PC_REGNUM, &raw_val);
907 pc_val = gdbarch_addr_bits_remove (current_gdbarch, raw_val);
910 internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
912 inferior_ptid = saved_inferior_ptid;
919 return read_pc_pid (inferior_ptid);
923 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
925 struct regcache *regcache = current_regcache;
926 struct gdbarch *gdbarch = get_regcache_arch (regcache);
928 ptid_t saved_inferior_ptid;
930 /* In case ptid != inferior_ptid. */
931 saved_inferior_ptid = inferior_ptid;
932 inferior_ptid = ptid;
934 if (gdbarch_write_pc_p (gdbarch))
935 gdbarch_write_pc (gdbarch, regcache, pc);
936 else if (PC_REGNUM >= 0)
937 regcache_cooked_write_unsigned (regcache, PC_REGNUM, pc);
939 internal_error (__FILE__, __LINE__,
940 _("write_pc_pid: Unable to update PC"));
942 inferior_ptid = saved_inferior_ptid;
946 write_pc (CORE_ADDR pc)
948 write_pc_pid (pc, inferior_ptid);
953 reg_flush_command (char *command, int from_tty)
955 /* Force-flush the register cache. */
956 registers_changed ();
958 printf_filtered (_("Register cache flushed.\n"));
962 build_regcache (void)
964 current_regcache = regcache_xmalloc (current_gdbarch);
965 current_regcache->readonly_p = 0;
969 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
970 const unsigned char *buf, long len)
976 for (i = 0; i < len; i++)
977 fprintf_unfiltered (file, "%02x", buf[i]);
979 case BFD_ENDIAN_LITTLE:
980 for (i = len - 1; i >= 0; i--)
981 fprintf_unfiltered (file, "%02x", buf[i]);
984 internal_error (__FILE__, __LINE__, _("Bad switch"));
988 enum regcache_dump_what
990 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
994 regcache_dump (struct regcache *regcache, struct ui_file *file,
995 enum regcache_dump_what what_to_dump)
997 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
998 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1000 int footnote_nr = 0;
1001 int footnote_register_size = 0;
1002 int footnote_register_offset = 0;
1003 int footnote_register_type_name_null = 0;
1004 long register_offset = 0;
1005 unsigned char buf[MAX_REGISTER_SIZE];
1008 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1009 regcache->descr->nr_raw_registers);
1010 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1011 regcache->descr->nr_cooked_registers);
1012 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1013 regcache->descr->sizeof_raw_registers);
1014 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1015 regcache->descr->sizeof_raw_register_valid_p);
1016 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1017 gdbarch_num_regs (current_gdbarch));
1018 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1019 gdbarch_num_pseudo_regs (current_gdbarch));
1022 gdb_assert (regcache->descr->nr_cooked_registers
1023 == (gdbarch_num_regs (current_gdbarch)
1024 + gdbarch_num_pseudo_regs (current_gdbarch)));
1026 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1030 fprintf_unfiltered (file, " %-10s", "Name");
1033 const char *p = gdbarch_register_name (current_gdbarch, regnum);
1036 else if (p[0] == '\0')
1038 fprintf_unfiltered (file, " %-10s", p);
1043 fprintf_unfiltered (file, " %4s", "Nr");
1045 fprintf_unfiltered (file, " %4d", regnum);
1047 /* Relative number. */
1049 fprintf_unfiltered (file, " %4s", "Rel");
1050 else if (regnum < gdbarch_num_regs (current_gdbarch))
1051 fprintf_unfiltered (file, " %4d", regnum);
1053 fprintf_unfiltered (file, " %4d",
1054 (regnum - gdbarch_num_regs (current_gdbarch)));
1058 fprintf_unfiltered (file, " %6s ", "Offset");
1061 fprintf_unfiltered (file, " %6ld",
1062 regcache->descr->register_offset[regnum]);
1063 if (register_offset != regcache->descr->register_offset[regnum]
1065 && (regcache->descr->register_offset[regnum]
1066 != (regcache->descr->register_offset[regnum - 1]
1067 + regcache->descr->sizeof_register[regnum - 1])))
1070 if (!footnote_register_offset)
1071 footnote_register_offset = ++footnote_nr;
1072 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1075 fprintf_unfiltered (file, " ");
1076 register_offset = (regcache->descr->register_offset[regnum]
1077 + regcache->descr->sizeof_register[regnum]);
1082 fprintf_unfiltered (file, " %5s ", "Size");
1084 fprintf_unfiltered (file, " %5ld",
1085 regcache->descr->sizeof_register[regnum]);
1094 static const char blt[] = "builtin_type";
1095 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1099 if (!footnote_register_type_name_null)
1100 footnote_register_type_name_null = ++footnote_nr;
1101 n = xstrprintf ("*%d", footnote_register_type_name_null);
1102 make_cleanup (xfree, n);
1105 /* Chop a leading builtin_type. */
1106 if (strncmp (t, blt, strlen (blt)) == 0)
1109 fprintf_unfiltered (file, " %-15s", t);
1112 /* Leading space always present. */
1113 fprintf_unfiltered (file, " ");
1116 if (what_to_dump == regcache_dump_raw)
1119 fprintf_unfiltered (file, "Raw value");
1120 else if (regnum >= regcache->descr->nr_raw_registers)
1121 fprintf_unfiltered (file, "<cooked>");
1122 else if (!regcache_valid_p (regcache, regnum))
1123 fprintf_unfiltered (file, "<invalid>");
1126 regcache_raw_read (regcache, regnum, buf);
1127 fprintf_unfiltered (file, "0x");
1128 dump_endian_bytes (file,
1129 gdbarch_byte_order (current_gdbarch), buf,
1130 regcache->descr->sizeof_register[regnum]);
1134 /* Value, cooked. */
1135 if (what_to_dump == regcache_dump_cooked)
1138 fprintf_unfiltered (file, "Cooked value");
1141 regcache_cooked_read (regcache, regnum, buf);
1142 fprintf_unfiltered (file, "0x");
1143 dump_endian_bytes (file,
1144 gdbarch_byte_order (current_gdbarch), buf,
1145 regcache->descr->sizeof_register[regnum]);
1149 /* Group members. */
1150 if (what_to_dump == regcache_dump_groups)
1153 fprintf_unfiltered (file, "Groups");
1156 const char *sep = "";
1157 struct reggroup *group;
1158 for (group = reggroup_next (gdbarch, NULL);
1160 group = reggroup_next (gdbarch, group))
1162 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1164 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1171 fprintf_unfiltered (file, "\n");
1174 if (footnote_register_size)
1175 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1176 footnote_register_size);
1177 if (footnote_register_offset)
1178 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1179 footnote_register_offset);
1180 if (footnote_register_type_name_null)
1181 fprintf_unfiltered (file,
1182 "*%d: Register type's name NULL.\n",
1183 footnote_register_type_name_null);
1184 do_cleanups (cleanups);
1188 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1191 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1194 struct ui_file *file = gdb_fopen (args, "w");
1196 perror_with_name (_("maintenance print architecture"));
1197 regcache_dump (current_regcache, file, what_to_dump);
1198 ui_file_delete (file);
1203 maintenance_print_registers (char *args, int from_tty)
1205 regcache_print (args, regcache_dump_none);
1209 maintenance_print_raw_registers (char *args, int from_tty)
1211 regcache_print (args, regcache_dump_raw);
1215 maintenance_print_cooked_registers (char *args, int from_tty)
1217 regcache_print (args, regcache_dump_cooked);
1221 maintenance_print_register_groups (char *args, int from_tty)
1223 regcache_print (args, regcache_dump_groups);
1226 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1229 _initialize_regcache (void)
1231 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1232 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1233 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1235 observer_attach_target_changed (regcache_observer_target_changed);
1237 add_com ("flushregs", class_maintenance, reg_flush_command,
1238 _("Force gdb to flush its register cache (maintainer command)"));
1240 /* Initialize the thread/process associated with the current set of
1241 registers. For now, -1 is special, and means `no current process'. */
1242 registers_ptid = pid_to_ptid (-1);
1244 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1245 Print the internal register configuration.\n\
1246 Takes an optional file parameter."), &maintenanceprintlist);
1247 add_cmd ("raw-registers", class_maintenance,
1248 maintenance_print_raw_registers, _("\
1249 Print the internal register configuration including raw values.\n\
1250 Takes an optional file parameter."), &maintenanceprintlist);
1251 add_cmd ("cooked-registers", class_maintenance,
1252 maintenance_print_cooked_registers, _("\
1253 Print the internal register configuration including cooked values.\n\
1254 Takes an optional file parameter."), &maintenanceprintlist);
1255 add_cmd ("register-groups", class_maintenance,
1256 maintenance_print_register_groups, _("\
1257 Print the internal register configuration including each register's group.\n\
1258 Takes an optional file parameter."),
1259 &maintenanceprintlist);