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 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 offset.
71 Assigning all registers an offset makes it possible to keep
72 legacy code, such as that found in read_register_bytes() and
73 write_register_bytes() working. */
74 long *register_offset;
75 long *sizeof_register;
77 /* Cached table containing the type of each register. */
78 struct type **register_type;
82 init_regcache_descr (struct gdbarch *gdbarch)
85 struct regcache_descr *descr;
86 gdb_assert (gdbarch != NULL);
88 /* Create an initial, zero filled, table. */
89 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
90 descr->gdbarch = gdbarch;
92 /* Total size of the register space. The raw registers are mapped
93 directly onto the raw register cache while the pseudo's are
94 either mapped onto raw-registers or memory. */
95 descr->nr_cooked_registers = gdbarch_num_regs (current_gdbarch)
96 + gdbarch_num_pseudo_regs (current_gdbarch);
97 descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (current_gdbarch)
98 + gdbarch_num_pseudo_regs
101 /* Fill in a table of register types. */
103 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
104 for (i = 0; i < descr->nr_cooked_registers; i++)
105 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
107 /* Construct a strictly RAW register cache. Don't allow pseudo's
108 into the register cache. */
109 descr->nr_raw_registers = gdbarch_num_regs (current_gdbarch);
111 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
112 array. This pretects GDB from erant code that accesses elements
113 of the global register_valid_p[] array in the range
114 [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
115 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
117 /* Lay out the register cache.
119 NOTE: cagney/2002-05-22: Only register_type() is used when
120 constructing the register cache. It is assumed that the
121 register's raw size, virtual size and type length are all the
126 descr->sizeof_register
127 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
128 descr->register_offset
129 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
130 for (i = 0; i < descr->nr_cooked_registers; i++)
132 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
133 descr->register_offset[i] = offset;
134 offset += descr->sizeof_register[i];
135 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
137 /* Set the real size of the register cache buffer. */
138 descr->sizeof_cooked_registers = offset;
141 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
142 the raw registers. Unfortunately some code still accesses the
143 register array directly using the global registers[]. Until that
144 code has been purged, play safe and over allocating the register
146 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
151 static struct regcache_descr *
152 regcache_descr (struct gdbarch *gdbarch)
154 return gdbarch_data (gdbarch, regcache_descr_handle);
157 /* Utility functions returning useful register attributes stored in
158 the regcache descr. */
161 register_type (struct gdbarch *gdbarch, int regnum)
163 struct regcache_descr *descr = regcache_descr (gdbarch);
164 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
165 return descr->register_type[regnum];
168 /* Utility functions returning useful register attributes stored in
169 the regcache descr. */
172 register_size (struct gdbarch *gdbarch, int regnum)
174 struct regcache_descr *descr = regcache_descr (gdbarch);
176 gdb_assert (regnum >= 0
177 && regnum < (gdbarch_num_regs (current_gdbarch)
178 + gdbarch_num_pseudo_regs (current_gdbarch)));
179 size = descr->sizeof_register[regnum];
183 /* The register cache for storing raw register values. */
187 struct regcache_descr *descr;
188 /* The register buffers. A read-only register cache can hold the
189 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
190 register cache can only hold [0 .. gdbarch_num_regs). */
192 /* Register cache status:
193 register_valid_p[REG] == 0 if REG value is not in the cache
194 > 0 if REG value is in the cache
195 < 0 if REG value is permanently unavailable */
196 signed char *register_valid_p;
197 /* Is this a read-only cache? A read-only cache is used for saving
198 the target's register state (e.g, across an inferior function
199 call or just before forcing a function return). A read-only
200 cache can only be updated via the methods regcache_dup() and
201 regcache_cpy(). The actual contents are determined by the
202 reggroup_save and reggroup_restore methods. */
204 /* If this is a read-write cache, which thread's registers is
210 regcache_xmalloc (struct gdbarch *gdbarch)
212 struct regcache_descr *descr;
213 struct regcache *regcache;
214 gdb_assert (gdbarch != NULL);
215 descr = regcache_descr (gdbarch);
216 regcache = XMALLOC (struct regcache);
217 regcache->descr = descr;
219 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
220 regcache->register_valid_p
221 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
222 regcache->readonly_p = 1;
223 regcache->ptid = minus_one_ptid;
228 regcache_xfree (struct regcache *regcache)
230 if (regcache == NULL)
232 xfree (regcache->registers);
233 xfree (regcache->register_valid_p);
238 do_regcache_xfree (void *data)
240 regcache_xfree (data);
244 make_cleanup_regcache_xfree (struct regcache *regcache)
246 return make_cleanup (do_regcache_xfree, regcache);
249 /* Return REGCACHE's architecture. */
252 get_regcache_arch (const struct regcache *regcache)
254 return regcache->descr->gdbarch;
257 /* Return a pointer to register REGNUM's buffer cache. */
260 register_buffer (const struct regcache *regcache, int regnum)
262 return regcache->registers + regcache->descr->register_offset[regnum];
266 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
269 struct gdbarch *gdbarch = dst->descr->gdbarch;
270 gdb_byte buf[MAX_REGISTER_SIZE];
272 /* The DST should be `read-only', if it wasn't then the save would
273 end up trying to write the register values back out to the
275 gdb_assert (dst->readonly_p);
276 /* Clear the dest. */
277 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
278 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
279 /* Copy over any registers (identified by their membership in the
280 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
281 gdbarch_num_pseudo_regs) range is checked since some architectures need
282 to save/restore `cooked' registers that live in memory. */
283 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
285 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
287 int valid = cooked_read (src, regnum, buf);
290 memcpy (register_buffer (dst, regnum), buf,
291 register_size (gdbarch, regnum));
292 dst->register_valid_p[regnum] = 1;
299 regcache_restore (struct regcache *dst,
300 regcache_cooked_read_ftype *cooked_read,
301 void *cooked_read_context)
303 struct gdbarch *gdbarch = dst->descr->gdbarch;
304 gdb_byte buf[MAX_REGISTER_SIZE];
306 /* The dst had better not be read-only. If it is, the `restore'
307 doesn't make much sense. */
308 gdb_assert (!dst->readonly_p);
309 /* Copy over any registers, being careful to only restore those that
310 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
311 + gdbarch_num_pseudo_regs) range is checked since some architectures need
312 to save/restore `cooked' registers that live in memory. */
313 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
315 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
317 int valid = cooked_read (cooked_read_context, regnum, buf);
319 regcache_cooked_write (dst, regnum, buf);
325 do_cooked_read (void *src, int regnum, gdb_byte *buf)
327 struct regcache *regcache = src;
328 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
329 /* Don't even think about fetching a register from a read-only
330 cache when the register isn't yet valid. There isn't a target
331 from which the register value can be fetched. */
333 regcache_cooked_read (regcache, regnum, buf);
339 regcache_cpy (struct regcache *dst, struct regcache *src)
343 gdb_assert (src != NULL && dst != NULL);
344 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
345 gdb_assert (src != dst);
346 gdb_assert (src->readonly_p || dst->readonly_p);
347 if (!src->readonly_p)
348 regcache_save (dst, do_cooked_read, src);
349 else if (!dst->readonly_p)
350 regcache_restore (dst, do_cooked_read, src);
352 regcache_cpy_no_passthrough (dst, src);
356 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
359 gdb_assert (src != NULL && dst != NULL);
360 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
361 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
362 move of data into the current regcache. Doing this would be
363 silly - it would mean that valid_p would be completely invalid. */
364 gdb_assert (dst->readonly_p);
365 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
366 memcpy (dst->register_valid_p, src->register_valid_p,
367 dst->descr->sizeof_raw_register_valid_p);
371 regcache_dup (struct regcache *src)
373 struct regcache *newbuf;
374 newbuf = regcache_xmalloc (src->descr->gdbarch);
375 regcache_cpy (newbuf, src);
380 regcache_dup_no_passthrough (struct regcache *src)
382 struct regcache *newbuf;
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 static 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 struct regcache *get_thread_regcache (ptid_t ptid)
424 /* NOTE: uweigand/2007-05-05: We need to detect the thread's
425 current architecture at this point. */
426 struct gdbarch *thread_gdbarch = current_gdbarch;
428 if (current_regcache && ptid_equal (current_regcache->ptid, ptid)
429 && get_regcache_arch (current_regcache) == thread_gdbarch)
430 return current_regcache;
432 if (current_regcache)
433 regcache_xfree (current_regcache);
435 current_regcache = regcache_xmalloc (thread_gdbarch);
436 current_regcache->readonly_p = 0;
437 current_regcache->ptid = ptid;
439 return current_regcache;
442 struct regcache *get_current_regcache (void)
444 return get_thread_regcache (inferior_ptid);
448 /* Observer for the target_changed event. */
451 regcache_observer_target_changed (struct target_ops *target)
453 registers_changed ();
456 /* Low level examining and depositing of registers.
458 The caller is responsible for making sure that the inferior is
459 stopped before calling the fetching routines, or it will get
460 garbage. (a change from GDB version 3, in which the caller got the
461 value from the last stop). */
463 /* REGISTERS_CHANGED ()
465 Indicate that registers may have changed, so invalidate the cache. */
468 registers_changed (void)
472 regcache_xfree (current_regcache);
473 current_regcache = NULL;
475 /* Force cleanup of any alloca areas if using C alloca instead of
476 a builtin alloca. This particular call is used to clean up
477 areas allocated by low level target code which may build up
478 during lengthy interactions between gdb and the target before
479 gdb gives control to the user (ie watchpoints). */
485 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
487 gdb_assert (regcache != NULL && buf != NULL);
488 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
489 /* Make certain that the register cache is up-to-date with respect
490 to the current thread. This switching shouldn't be necessary
491 only there is still only one target side register cache. Sigh!
492 On the bright side, at least there is a regcache object. */
493 if (!regcache->readonly_p)
495 if (!regcache_valid_p (regcache, regnum))
497 struct cleanup *old_chain = save_inferior_ptid ();
498 inferior_ptid = regcache->ptid;
499 target_fetch_registers (regcache, regnum);
500 do_cleanups (old_chain);
503 /* FIXME: cagney/2004-08-07: At present a number of targets
504 forget (or didn't know that they needed) to set this leading to
505 panics. Also is the problem that targets need to indicate
506 that a register is in one of the possible states: valid,
507 undefined, unknown. The last of which isn't yet
509 gdb_assert (regcache_valid_p (regcache, regnum));
512 /* Copy the value directly into the register cache. */
513 memcpy (buf, register_buffer (regcache, regnum),
514 regcache->descr->sizeof_register[regnum]);
518 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
521 gdb_assert (regcache != NULL);
522 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
523 buf = alloca (regcache->descr->sizeof_register[regnum]);
524 regcache_raw_read (regcache, regnum, buf);
525 (*val) = extract_signed_integer (buf,
526 regcache->descr->sizeof_register[regnum]);
530 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
534 gdb_assert (regcache != NULL);
535 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
536 buf = alloca (regcache->descr->sizeof_register[regnum]);
537 regcache_raw_read (regcache, regnum, buf);
538 (*val) = extract_unsigned_integer (buf,
539 regcache->descr->sizeof_register[regnum]);
543 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
546 gdb_assert (regcache != NULL);
547 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
548 buf = alloca (regcache->descr->sizeof_register[regnum]);
549 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
550 regcache_raw_write (regcache, regnum, buf);
554 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
558 gdb_assert (regcache != NULL);
559 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
560 buf = alloca (regcache->descr->sizeof_register[regnum]);
561 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
562 regcache_raw_write (regcache, regnum, buf);
566 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
568 gdb_assert (regnum >= 0);
569 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
570 if (regnum < regcache->descr->nr_raw_registers)
571 regcache_raw_read (regcache, regnum, buf);
572 else if (regcache->readonly_p
573 && regnum < regcache->descr->nr_cooked_registers
574 && regcache->register_valid_p[regnum])
575 /* Read-only register cache, perhaps the cooked value was cached? */
576 memcpy (buf, register_buffer (regcache, regnum),
577 regcache->descr->sizeof_register[regnum]);
579 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
584 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
588 gdb_assert (regcache != NULL);
589 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
590 buf = alloca (regcache->descr->sizeof_register[regnum]);
591 regcache_cooked_read (regcache, regnum, buf);
592 (*val) = extract_signed_integer (buf,
593 regcache->descr->sizeof_register[regnum]);
597 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
601 gdb_assert (regcache != NULL);
602 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
603 buf = alloca (regcache->descr->sizeof_register[regnum]);
604 regcache_cooked_read (regcache, regnum, buf);
605 (*val) = extract_unsigned_integer (buf,
606 regcache->descr->sizeof_register[regnum]);
610 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
614 gdb_assert (regcache != NULL);
615 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
616 buf = alloca (regcache->descr->sizeof_register[regnum]);
617 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
618 regcache_cooked_write (regcache, regnum, buf);
622 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
626 gdb_assert (regcache != NULL);
627 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
628 buf = alloca (regcache->descr->sizeof_register[regnum]);
629 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
630 regcache_cooked_write (regcache, regnum, buf);
634 regcache_raw_write (struct regcache *regcache, int regnum,
637 struct cleanup *old_chain;
639 gdb_assert (regcache != NULL && buf != NULL);
640 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
641 gdb_assert (!regcache->readonly_p);
643 /* On the sparc, writing %g0 is a no-op, so we don't even want to
644 change the registers array if something writes to this register. */
645 if (gdbarch_cannot_store_register (current_gdbarch, regnum))
648 /* If we have a valid copy of the register, and new value == old
649 value, then don't bother doing the actual store. */
650 if (regcache_valid_p (regcache, regnum)
651 && (memcmp (register_buffer (regcache, regnum), buf,
652 regcache->descr->sizeof_register[regnum]) == 0))
655 old_chain = save_inferior_ptid ();
656 inferior_ptid = regcache->ptid;
658 target_prepare_to_store (regcache);
659 memcpy (register_buffer (regcache, regnum), buf,
660 regcache->descr->sizeof_register[regnum]);
661 regcache->register_valid_p[regnum] = 1;
662 target_store_registers (regcache, regnum);
664 do_cleanups (old_chain);
668 regcache_cooked_write (struct regcache *regcache, int regnum,
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_write (regcache, regnum, buf);
676 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
680 /* Perform a partial register transfer using a read, modify, write
683 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
685 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
689 regcache_xfer_part (struct regcache *regcache, int regnum,
690 int offset, int len, void *in, const void *out,
691 void (*read) (struct regcache *regcache, int regnum,
693 void (*write) (struct regcache *regcache, int regnum,
694 const gdb_byte *buf))
696 struct regcache_descr *descr = regcache->descr;
697 gdb_byte reg[MAX_REGISTER_SIZE];
698 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
699 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
700 /* Something to do? */
701 if (offset + len == 0)
703 /* Read (when needed) ... */
706 || offset + len < descr->sizeof_register[regnum])
708 gdb_assert (read != NULL);
709 read (regcache, regnum, reg);
713 memcpy (in, reg + offset, len);
715 memcpy (reg + offset, out, len);
716 /* ... write (when needed). */
719 gdb_assert (write != NULL);
720 write (regcache, regnum, reg);
725 regcache_raw_read_part (struct regcache *regcache, int regnum,
726 int offset, int len, gdb_byte *buf)
728 struct regcache_descr *descr = regcache->descr;
729 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
730 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
731 regcache_raw_read, regcache_raw_write);
735 regcache_raw_write_part (struct regcache *regcache, int regnum,
736 int offset, int len, const gdb_byte *buf)
738 struct regcache_descr *descr = regcache->descr;
739 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
740 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
741 regcache_raw_read, regcache_raw_write);
745 regcache_cooked_read_part (struct regcache *regcache, int regnum,
746 int offset, int len, gdb_byte *buf)
748 struct regcache_descr *descr = regcache->descr;
749 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
750 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
751 regcache_cooked_read, regcache_cooked_write);
755 regcache_cooked_write_part (struct regcache *regcache, int regnum,
756 int offset, int len, const gdb_byte *buf)
758 struct regcache_descr *descr = regcache->descr;
759 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
760 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
761 regcache_cooked_read, regcache_cooked_write);
764 /* Hack to keep code that view the register buffer as raw bytes
768 register_offset_hack (struct gdbarch *gdbarch, int regnum)
770 struct regcache_descr *descr = regcache_descr (gdbarch);
771 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
772 return descr->register_offset[regnum];
776 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
779 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
784 gdb_assert (regcache != NULL);
785 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
786 gdb_assert (!regcache->readonly_p);
788 regbuf = register_buffer (regcache, regnum);
789 size = regcache->descr->sizeof_register[regnum];
792 memcpy (regbuf, buf, size);
794 memset (regbuf, 0, size);
796 /* Mark the register as cached. */
797 regcache->register_valid_p[regnum] = 1;
800 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
803 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
808 gdb_assert (regcache != NULL && buf != NULL);
809 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
811 regbuf = register_buffer (regcache, regnum);
812 size = regcache->descr->sizeof_register[regnum];
813 memcpy (buf, regbuf, size);
817 /* read_pc, write_pc, etc. Special handling for register PC. */
819 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
820 read_sp(), will eventually be replaced by per-frame methods.
821 Instead of relying on the global INFERIOR_PTID, they will use the
822 contextual information provided by the FRAME. These functions do
823 not belong in the register cache. */
825 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
826 write_pc_pid() and write_pc(), all need to be replaced by something
827 that does not rely on global state. But what? */
830 read_pc_pid (ptid_t ptid)
832 struct regcache *regcache = get_thread_regcache (ptid);
833 struct gdbarch *gdbarch = get_regcache_arch (regcache);
837 if (gdbarch_read_pc_p (gdbarch))
838 pc_val = gdbarch_read_pc (gdbarch, regcache);
839 /* Else use per-frame method on get_current_frame. */
840 else if (gdbarch_pc_regnum (current_gdbarch) >= 0)
843 regcache_cooked_read_unsigned (regcache,
844 gdbarch_pc_regnum (current_gdbarch),
846 pc_val = gdbarch_addr_bits_remove (current_gdbarch, raw_val);
849 internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
857 return read_pc_pid (inferior_ptid);
861 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
863 struct regcache *regcache = get_thread_regcache (ptid);
864 struct gdbarch *gdbarch = get_regcache_arch (regcache);
866 if (gdbarch_write_pc_p (gdbarch))
867 gdbarch_write_pc (gdbarch, regcache, pc);
868 else if (gdbarch_pc_regnum (current_gdbarch) >= 0)
869 regcache_cooked_write_unsigned (regcache,
870 gdbarch_pc_regnum (current_gdbarch), pc);
872 internal_error (__FILE__, __LINE__,
873 _("write_pc_pid: Unable to update PC"));
877 write_pc (CORE_ADDR pc)
879 write_pc_pid (pc, inferior_ptid);
884 reg_flush_command (char *command, int from_tty)
886 /* Force-flush the register cache. */
887 registers_changed ();
889 printf_filtered (_("Register cache flushed.\n"));
893 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
894 const unsigned char *buf, long len)
900 for (i = 0; i < len; i++)
901 fprintf_unfiltered (file, "%02x", buf[i]);
903 case BFD_ENDIAN_LITTLE:
904 for (i = len - 1; i >= 0; i--)
905 fprintf_unfiltered (file, "%02x", buf[i]);
908 internal_error (__FILE__, __LINE__, _("Bad switch"));
912 enum regcache_dump_what
914 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
918 regcache_dump (struct regcache *regcache, struct ui_file *file,
919 enum regcache_dump_what what_to_dump)
921 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
922 struct gdbarch *gdbarch = regcache->descr->gdbarch;
925 int footnote_register_size = 0;
926 int footnote_register_offset = 0;
927 int footnote_register_type_name_null = 0;
928 long register_offset = 0;
929 unsigned char buf[MAX_REGISTER_SIZE];
932 fprintf_unfiltered (file, "nr_raw_registers %d\n",
933 regcache->descr->nr_raw_registers);
934 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
935 regcache->descr->nr_cooked_registers);
936 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
937 regcache->descr->sizeof_raw_registers);
938 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
939 regcache->descr->sizeof_raw_register_valid_p);
940 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
941 gdbarch_num_regs (current_gdbarch));
942 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
943 gdbarch_num_pseudo_regs (current_gdbarch));
946 gdb_assert (regcache->descr->nr_cooked_registers
947 == (gdbarch_num_regs (current_gdbarch)
948 + gdbarch_num_pseudo_regs (current_gdbarch)));
950 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
954 fprintf_unfiltered (file, " %-10s", "Name");
957 const char *p = gdbarch_register_name (current_gdbarch, regnum);
960 else if (p[0] == '\0')
962 fprintf_unfiltered (file, " %-10s", p);
967 fprintf_unfiltered (file, " %4s", "Nr");
969 fprintf_unfiltered (file, " %4d", regnum);
971 /* Relative number. */
973 fprintf_unfiltered (file, " %4s", "Rel");
974 else if (regnum < gdbarch_num_regs (current_gdbarch))
975 fprintf_unfiltered (file, " %4d", regnum);
977 fprintf_unfiltered (file, " %4d",
978 (regnum - gdbarch_num_regs (current_gdbarch)));
982 fprintf_unfiltered (file, " %6s ", "Offset");
985 fprintf_unfiltered (file, " %6ld",
986 regcache->descr->register_offset[regnum]);
987 if (register_offset != regcache->descr->register_offset[regnum]
989 && (regcache->descr->register_offset[regnum]
990 != (regcache->descr->register_offset[regnum - 1]
991 + regcache->descr->sizeof_register[regnum - 1])))
994 if (!footnote_register_offset)
995 footnote_register_offset = ++footnote_nr;
996 fprintf_unfiltered (file, "*%d", footnote_register_offset);
999 fprintf_unfiltered (file, " ");
1000 register_offset = (regcache->descr->register_offset[regnum]
1001 + regcache->descr->sizeof_register[regnum]);
1006 fprintf_unfiltered (file, " %5s ", "Size");
1008 fprintf_unfiltered (file, " %5ld",
1009 regcache->descr->sizeof_register[regnum]);
1018 static const char blt[] = "builtin_type";
1019 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1023 if (!footnote_register_type_name_null)
1024 footnote_register_type_name_null = ++footnote_nr;
1025 n = xstrprintf ("*%d", footnote_register_type_name_null);
1026 make_cleanup (xfree, n);
1029 /* Chop a leading builtin_type. */
1030 if (strncmp (t, blt, strlen (blt)) == 0)
1033 fprintf_unfiltered (file, " %-15s", t);
1036 /* Leading space always present. */
1037 fprintf_unfiltered (file, " ");
1040 if (what_to_dump == regcache_dump_raw)
1043 fprintf_unfiltered (file, "Raw value");
1044 else if (regnum >= regcache->descr->nr_raw_registers)
1045 fprintf_unfiltered (file, "<cooked>");
1046 else if (!regcache_valid_p (regcache, regnum))
1047 fprintf_unfiltered (file, "<invalid>");
1050 regcache_raw_read (regcache, regnum, buf);
1051 fprintf_unfiltered (file, "0x");
1052 dump_endian_bytes (file,
1053 gdbarch_byte_order (current_gdbarch), buf,
1054 regcache->descr->sizeof_register[regnum]);
1058 /* Value, cooked. */
1059 if (what_to_dump == regcache_dump_cooked)
1062 fprintf_unfiltered (file, "Cooked value");
1065 regcache_cooked_read (regcache, regnum, buf);
1066 fprintf_unfiltered (file, "0x");
1067 dump_endian_bytes (file,
1068 gdbarch_byte_order (current_gdbarch), buf,
1069 regcache->descr->sizeof_register[regnum]);
1073 /* Group members. */
1074 if (what_to_dump == regcache_dump_groups)
1077 fprintf_unfiltered (file, "Groups");
1080 const char *sep = "";
1081 struct reggroup *group;
1082 for (group = reggroup_next (gdbarch, NULL);
1084 group = reggroup_next (gdbarch, group))
1086 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1088 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1095 fprintf_unfiltered (file, "\n");
1098 if (footnote_register_size)
1099 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1100 footnote_register_size);
1101 if (footnote_register_offset)
1102 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1103 footnote_register_offset);
1104 if (footnote_register_type_name_null)
1105 fprintf_unfiltered (file,
1106 "*%d: Register type's name NULL.\n",
1107 footnote_register_type_name_null);
1108 do_cleanups (cleanups);
1112 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1115 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1118 struct ui_file *file = gdb_fopen (args, "w");
1120 perror_with_name (_("maintenance print architecture"));
1121 regcache_dump (get_current_regcache (), file, what_to_dump);
1122 ui_file_delete (file);
1127 maintenance_print_registers (char *args, int from_tty)
1129 regcache_print (args, regcache_dump_none);
1133 maintenance_print_raw_registers (char *args, int from_tty)
1135 regcache_print (args, regcache_dump_raw);
1139 maintenance_print_cooked_registers (char *args, int from_tty)
1141 regcache_print (args, regcache_dump_cooked);
1145 maintenance_print_register_groups (char *args, int from_tty)
1147 regcache_print (args, regcache_dump_groups);
1150 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1153 _initialize_regcache (void)
1155 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1157 observer_attach_target_changed (regcache_observer_target_changed);
1159 add_com ("flushregs", class_maintenance, reg_flush_command,
1160 _("Force gdb to flush its register cache (maintainer command)"));
1162 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1163 Print the internal register configuration.\n\
1164 Takes an optional file parameter."), &maintenanceprintlist);
1165 add_cmd ("raw-registers", class_maintenance,
1166 maintenance_print_raw_registers, _("\
1167 Print the internal register configuration including raw values.\n\
1168 Takes an optional file parameter."), &maintenanceprintlist);
1169 add_cmd ("cooked-registers", class_maintenance,
1170 maintenance_print_cooked_registers, _("\
1171 Print the internal register configuration including cooked values.\n\
1172 Takes an optional file parameter."), &maintenanceprintlist);
1173 add_cmd ("register-groups", class_maintenance,
1174 maintenance_print_register_groups, _("\
1175 Print the internal register configuration including each register's group.\n\
1176 Takes an optional file parameter."),
1177 &maintenanceprintlist);