1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-1987, 1989, 1991, 1994-1996, 1998, 2000-2002,
4 2004, 2007-2012 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. */
32 #include "exceptions.h"
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_status;
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_status;
70 /* Offset and size (in 8 bit bytes), of each register in the
71 register cache. All registers (including those in the range
72 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
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 (gdbarch)
96 + gdbarch_num_pseudo_regs (gdbarch);
97 descr->sizeof_cooked_register_status
98 = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
100 /* Fill in a table of register types. */
102 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
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 (gdbarch);
110 descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
112 /* Lay out the register cache.
114 NOTE: cagney/2002-05-22: Only register_type() is used when
115 constructing the register cache. It is assumed that the
116 register's raw size, virtual size and type length are all the
122 descr->sizeof_register
123 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
124 descr->register_offset
125 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
126 for (i = 0; i < descr->nr_raw_registers; i++)
128 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
129 descr->register_offset[i] = offset;
130 offset += descr->sizeof_register[i];
131 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
133 /* Set the real size of the raw register cache buffer. */
134 descr->sizeof_raw_registers = offset;
136 for (; i < descr->nr_cooked_registers; i++)
138 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
139 descr->register_offset[i] = offset;
140 offset += descr->sizeof_register[i];
141 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
143 /* Set the real size of the readonly register cache buffer. */
144 descr->sizeof_cooked_registers = offset;
150 static struct regcache_descr *
151 regcache_descr (struct gdbarch *gdbarch)
153 return gdbarch_data (gdbarch, regcache_descr_handle);
156 /* Utility functions returning useful register attributes stored in
157 the regcache descr. */
160 register_type (struct gdbarch *gdbarch, int regnum)
162 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);
177 gdb_assert (regnum >= 0
178 && regnum < (gdbarch_num_regs (gdbarch)
179 + gdbarch_num_pseudo_regs (gdbarch)));
180 size = descr->sizeof_register[regnum];
184 /* The register cache for storing raw register values. */
188 struct regcache_descr *descr;
190 /* The address space of this register cache (for registers where it
191 makes sense, like PC or SP). */
192 struct address_space *aspace;
194 /* The register buffers. A read-only register cache can hold the
195 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
196 register cache can only hold [0 .. gdbarch_num_regs). */
198 /* Register cache status. */
199 signed char *register_status;
200 /* Is this a read-only cache? A read-only cache is used for saving
201 the target's register state (e.g, across an inferior function
202 call or just before forcing a function return). A read-only
203 cache can only be updated via the methods regcache_dup() and
204 regcache_cpy(). The actual contents are determined by the
205 reggroup_save and reggroup_restore methods. */
207 /* If this is a read-write cache, which thread's registers is
212 static struct regcache *
213 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
216 struct regcache_descr *descr;
217 struct regcache *regcache;
219 gdb_assert (gdbarch != NULL);
220 descr = regcache_descr (gdbarch);
221 regcache = XMALLOC (struct regcache);
222 regcache->descr = descr;
223 regcache->readonly_p = readonly_p;
227 = XCALLOC (descr->sizeof_cooked_registers, gdb_byte);
228 regcache->register_status
229 = XCALLOC (descr->sizeof_cooked_register_status, gdb_byte);
234 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
235 regcache->register_status
236 = XCALLOC (descr->sizeof_raw_register_status, gdb_byte);
238 regcache->aspace = aspace;
239 regcache->ptid = minus_one_ptid;
244 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
246 return regcache_xmalloc_1 (gdbarch, aspace, 1);
250 regcache_xfree (struct regcache *regcache)
252 if (regcache == NULL)
254 xfree (regcache->registers);
255 xfree (regcache->register_status);
260 do_regcache_xfree (void *data)
262 regcache_xfree (data);
266 make_cleanup_regcache_xfree (struct regcache *regcache)
268 return make_cleanup (do_regcache_xfree, regcache);
271 /* Return REGCACHE's architecture. */
274 get_regcache_arch (const struct regcache *regcache)
276 return regcache->descr->gdbarch;
279 struct address_space *
280 get_regcache_aspace (const struct regcache *regcache)
282 return regcache->aspace;
285 /* Return a pointer to register REGNUM's buffer cache. */
288 register_buffer (const struct regcache *regcache, int regnum)
290 return regcache->registers + regcache->descr->register_offset[regnum];
294 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
297 struct gdbarch *gdbarch = dst->descr->gdbarch;
298 gdb_byte buf[MAX_REGISTER_SIZE];
301 /* The DST should be `read-only', if it wasn't then the save would
302 end up trying to write the register values back out to the
304 gdb_assert (dst->readonly_p);
305 /* Clear the dest. */
306 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
307 memset (dst->register_status, 0,
308 dst->descr->sizeof_cooked_register_status);
309 /* Copy over any registers (identified by their membership in the
310 save_reggroup) and mark them as valid. 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, save_reggroup))
317 enum register_status status = cooked_read (src, regnum, buf);
319 if (status == REG_VALID)
320 memcpy (register_buffer (dst, regnum), buf,
321 register_size (gdbarch, regnum));
324 gdb_assert (status != REG_UNKNOWN);
326 memset (register_buffer (dst, regnum), 0,
327 register_size (gdbarch, regnum));
329 dst->register_status[regnum] = status;
335 regcache_restore (struct regcache *dst,
336 regcache_cooked_read_ftype *cooked_read,
337 void *cooked_read_context)
339 struct gdbarch *gdbarch = dst->descr->gdbarch;
340 gdb_byte buf[MAX_REGISTER_SIZE];
343 /* The dst had better not be read-only. If it is, the `restore'
344 doesn't make much sense. */
345 gdb_assert (!dst->readonly_p);
346 /* Copy over any registers, being careful to only restore those that
347 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
348 + gdbarch_num_pseudo_regs) range is checked since some architectures need
349 to save/restore `cooked' registers that live in memory. */
350 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
352 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
354 enum register_status status;
356 status = cooked_read (cooked_read_context, regnum, buf);
357 if (status == REG_VALID)
358 regcache_cooked_write (dst, regnum, buf);
363 static enum register_status
364 do_cooked_read (void *src, int regnum, gdb_byte *buf)
366 struct regcache *regcache = src;
368 return regcache_cooked_read (regcache, regnum, buf);
372 regcache_cpy (struct regcache *dst, struct regcache *src)
374 gdb_assert (src != NULL && dst != NULL);
375 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
376 gdb_assert (src != dst);
377 gdb_assert (src->readonly_p || dst->readonly_p);
379 if (!src->readonly_p)
380 regcache_save (dst, do_cooked_read, src);
381 else if (!dst->readonly_p)
382 regcache_restore (dst, do_cooked_read, src);
384 regcache_cpy_no_passthrough (dst, src);
388 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
390 gdb_assert (src != NULL && dst != NULL);
391 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
392 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
393 move of data into a thread's regcache. Doing this would be silly
394 - it would mean that regcache->register_status would be
395 completely invalid. */
396 gdb_assert (dst->readonly_p && src->readonly_p);
398 memcpy (dst->registers, src->registers,
399 dst->descr->sizeof_cooked_registers);
400 memcpy (dst->register_status, src->register_status,
401 dst->descr->sizeof_cooked_register_status);
405 regcache_dup (struct regcache *src)
407 struct regcache *newbuf;
409 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
410 regcache_cpy (newbuf, src);
415 regcache_register_status (const struct regcache *regcache, int regnum)
417 gdb_assert (regcache != NULL);
418 gdb_assert (regnum >= 0);
419 if (regcache->readonly_p)
420 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
422 gdb_assert (regnum < regcache->descr->nr_raw_registers);
424 return regcache->register_status[regnum];
428 regcache_invalidate (struct regcache *regcache, int regnum)
430 gdb_assert (regcache != NULL);
431 gdb_assert (regnum >= 0);
432 gdb_assert (!regcache->readonly_p);
433 gdb_assert (regnum < regcache->descr->nr_raw_registers);
434 regcache->register_status[regnum] = REG_UNKNOWN;
438 /* Global structure containing the current regcache. */
440 /* NOTE: this is a write-through cache. There is no "dirty" bit for
441 recording if the register values have been changed (eg. by the
442 user). Therefore all registers must be written back to the
443 target when appropriate. */
447 struct regcache *regcache;
448 struct regcache_list *next;
451 static struct regcache_list *current_regcache;
454 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
455 struct address_space *aspace)
457 struct regcache_list *list;
458 struct regcache *new_regcache;
460 for (list = current_regcache; list; list = list->next)
461 if (ptid_equal (list->regcache->ptid, ptid)
462 && get_regcache_arch (list->regcache) == gdbarch)
463 return list->regcache;
465 new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
466 new_regcache->ptid = ptid;
468 list = xmalloc (sizeof (struct regcache_list));
469 list->regcache = new_regcache;
470 list->next = current_regcache;
471 current_regcache = list;
477 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
479 struct address_space *aspace;
481 /* For the benefit of "maint print registers" & co when debugging an
482 executable, allow dumping the regcache even when there is no
483 thread selected (target_thread_address_space internal-errors if
484 no address space is found). Note that normal user commands will
485 fail higher up on the call stack due to no
486 target_has_registers. */
487 aspace = (ptid_equal (null_ptid, ptid)
489 : target_thread_address_space (ptid));
491 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
494 static ptid_t current_thread_ptid;
495 static struct gdbarch *current_thread_arch;
498 get_thread_regcache (ptid_t ptid)
500 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
502 current_thread_ptid = ptid;
503 current_thread_arch = target_thread_architecture (ptid);
506 return get_thread_arch_regcache (ptid, current_thread_arch);
510 get_current_regcache (void)
512 return get_thread_regcache (inferior_ptid);
516 /* Observer for the target_changed event. */
519 regcache_observer_target_changed (struct target_ops *target)
521 registers_changed ();
524 /* Update global variables old ptids to hold NEW_PTID if they were
527 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
529 struct regcache_list *list;
531 for (list = current_regcache; list; list = list->next)
532 if (ptid_equal (list->regcache->ptid, old_ptid))
533 list->regcache->ptid = new_ptid;
536 /* Low level examining and depositing of registers.
538 The caller is responsible for making sure that the inferior is
539 stopped before calling the fetching routines, or it will get
540 garbage. (a change from GDB version 3, in which the caller got the
541 value from the last stop). */
543 /* REGISTERS_CHANGED ()
545 Indicate that registers may have changed, so invalidate the cache. */
548 registers_changed_ptid (ptid_t ptid)
550 struct regcache_list *list, **list_link;
552 list = current_regcache;
553 list_link = ¤t_regcache;
556 if (ptid_match (list->regcache->ptid, ptid))
558 struct regcache_list *dead = list;
560 *list_link = list->next;
561 regcache_xfree (list->regcache);
567 list_link = &list->next;
571 if (ptid_match (current_thread_ptid, ptid))
573 current_thread_ptid = null_ptid;
574 current_thread_arch = NULL;
577 if (ptid_match (inferior_ptid, ptid))
579 /* We just deleted the regcache of the current thread. Need to
580 forget about any frames we have cached, too. */
581 reinit_frame_cache ();
586 registers_changed (void)
588 registers_changed_ptid (minus_one_ptid);
590 /* Force cleanup of any alloca areas if using C alloca instead of
591 a builtin alloca. This particular call is used to clean up
592 areas allocated by low level target code which may build up
593 during lengthy interactions between gdb and the target before
594 gdb gives control to the user (ie watchpoints). */
599 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
601 gdb_assert (regcache != NULL && buf != NULL);
602 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
603 /* Make certain that the register cache is up-to-date with respect
604 to the current thread. This switching shouldn't be necessary
605 only there is still only one target side register cache. Sigh!
606 On the bright side, at least there is a regcache object. */
607 if (!regcache->readonly_p
608 && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
610 struct cleanup *old_chain = save_inferior_ptid ();
612 inferior_ptid = regcache->ptid;
613 target_fetch_registers (regcache, regnum);
614 do_cleanups (old_chain);
616 /* A number of targets can't access the whole set of raw
617 registers (because the debug API provides no means to get at
619 if (regcache->register_status[regnum] == REG_UNKNOWN)
620 regcache->register_status[regnum] = REG_UNAVAILABLE;
623 if (regcache->register_status[regnum] != REG_VALID)
624 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
626 memcpy (buf, register_buffer (regcache, regnum),
627 regcache->descr->sizeof_register[regnum]);
629 return regcache->register_status[regnum];
633 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
636 enum register_status status;
638 gdb_assert (regcache != NULL);
639 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
640 buf = alloca (regcache->descr->sizeof_register[regnum]);
641 status = regcache_raw_read (regcache, regnum, buf);
642 if (status == REG_VALID)
643 *val = extract_signed_integer
644 (buf, regcache->descr->sizeof_register[regnum],
645 gdbarch_byte_order (regcache->descr->gdbarch));
652 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
656 enum register_status status;
658 gdb_assert (regcache != NULL);
659 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
660 buf = alloca (regcache->descr->sizeof_register[regnum]);
661 status = regcache_raw_read (regcache, regnum, buf);
662 if (status == REG_VALID)
663 *val = extract_unsigned_integer
664 (buf, regcache->descr->sizeof_register[regnum],
665 gdbarch_byte_order (regcache->descr->gdbarch));
672 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
676 gdb_assert (regcache != NULL);
677 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
678 buf = alloca (regcache->descr->sizeof_register[regnum]);
679 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
680 gdbarch_byte_order (regcache->descr->gdbarch), val);
681 regcache_raw_write (regcache, regnum, buf);
685 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
690 gdb_assert (regcache != NULL);
691 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
692 buf = alloca (regcache->descr->sizeof_register[regnum]);
693 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
694 gdbarch_byte_order (regcache->descr->gdbarch), val);
695 regcache_raw_write (regcache, regnum, buf);
699 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
701 gdb_assert (regnum >= 0);
702 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
703 if (regnum < regcache->descr->nr_raw_registers)
704 return regcache_raw_read (regcache, regnum, buf);
705 else if (regcache->readonly_p
706 && regcache->register_status[regnum] != REG_UNKNOWN)
708 /* Read-only register cache, perhaps the cooked value was
710 struct gdbarch *gdbarch = regcache->descr->gdbarch;
712 if (regcache->register_status[regnum] == REG_VALID)
713 memcpy (buf, register_buffer (regcache, regnum),
714 regcache->descr->sizeof_register[regnum]);
716 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
718 return regcache->register_status[regnum];
720 else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
722 struct value *mark, *computed;
723 enum register_status result = REG_VALID;
725 mark = value_mark ();
727 computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
729 if (value_entirely_available (computed))
730 memcpy (buf, value_contents_raw (computed),
731 regcache->descr->sizeof_register[regnum]);
734 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
735 result = REG_UNAVAILABLE;
738 value_free_to_mark (mark);
743 return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
748 regcache_cooked_read_value (struct regcache *regcache, int regnum)
750 gdb_assert (regnum >= 0);
751 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
753 if (regnum < regcache->descr->nr_raw_registers
754 || (regcache->readonly_p
755 && regcache->register_status[regnum] != REG_UNKNOWN)
756 || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
758 struct value *result;
760 result = allocate_value (register_type (regcache->descr->gdbarch,
762 VALUE_LVAL (result) = lval_register;
763 VALUE_REGNUM (result) = regnum;
765 /* It is more efficient in general to do this delegation in this
766 direction than in the other one, even though the value-based
768 if (regcache_cooked_read (regcache, regnum,
769 value_contents_raw (result)) == REG_UNAVAILABLE)
770 mark_value_bytes_unavailable (result, 0,
771 TYPE_LENGTH (value_type (result)));
776 return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
781 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
784 enum register_status status;
787 gdb_assert (regcache != NULL);
788 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
789 buf = alloca (regcache->descr->sizeof_register[regnum]);
790 status = regcache_cooked_read (regcache, regnum, buf);
791 if (status == REG_VALID)
792 *val = extract_signed_integer
793 (buf, regcache->descr->sizeof_register[regnum],
794 gdbarch_byte_order (regcache->descr->gdbarch));
801 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
804 enum register_status status;
807 gdb_assert (regcache != NULL);
808 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
809 buf = alloca (regcache->descr->sizeof_register[regnum]);
810 status = regcache_cooked_read (regcache, regnum, buf);
811 if (status == REG_VALID)
812 *val = extract_unsigned_integer
813 (buf, regcache->descr->sizeof_register[regnum],
814 gdbarch_byte_order (regcache->descr->gdbarch));
821 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
826 gdb_assert (regcache != NULL);
827 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
828 buf = alloca (regcache->descr->sizeof_register[regnum]);
829 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
830 gdbarch_byte_order (regcache->descr->gdbarch), val);
831 regcache_cooked_write (regcache, regnum, buf);
835 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
840 gdb_assert (regcache != NULL);
841 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
842 buf = alloca (regcache->descr->sizeof_register[regnum]);
843 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
844 gdbarch_byte_order (regcache->descr->gdbarch), val);
845 regcache_cooked_write (regcache, regnum, buf);
849 regcache_raw_write (struct regcache *regcache, int regnum,
852 struct cleanup *old_chain;
854 gdb_assert (regcache != NULL && buf != NULL);
855 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
856 gdb_assert (!regcache->readonly_p);
858 /* On the sparc, writing %g0 is a no-op, so we don't even want to
859 change the registers array if something writes to this register. */
860 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
863 /* If we have a valid copy of the register, and new value == old
864 value, then don't bother doing the actual store. */
865 if (regcache_register_status (regcache, regnum) == REG_VALID
866 && (memcmp (register_buffer (regcache, regnum), buf,
867 regcache->descr->sizeof_register[regnum]) == 0))
870 old_chain = save_inferior_ptid ();
871 inferior_ptid = regcache->ptid;
873 target_prepare_to_store (regcache);
874 memcpy (register_buffer (regcache, regnum), buf,
875 regcache->descr->sizeof_register[regnum]);
876 regcache->register_status[regnum] = REG_VALID;
877 target_store_registers (regcache, regnum);
879 do_cleanups (old_chain);
883 regcache_cooked_write (struct regcache *regcache, int regnum,
886 gdb_assert (regnum >= 0);
887 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
888 if (regnum < regcache->descr->nr_raw_registers)
889 regcache_raw_write (regcache, regnum, buf);
891 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
895 /* Perform a partial register transfer using a read, modify, write
898 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
900 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
903 static enum register_status
904 regcache_xfer_part (struct regcache *regcache, int regnum,
905 int offset, int len, void *in, const void *out,
906 enum register_status (*read) (struct regcache *regcache,
909 void (*write) (struct regcache *regcache, int regnum,
910 const gdb_byte *buf))
912 struct regcache_descr *descr = regcache->descr;
913 gdb_byte reg[MAX_REGISTER_SIZE];
915 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
916 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
917 /* Something to do? */
918 if (offset + len == 0)
920 /* Read (when needed) ... */
923 || offset + len < descr->sizeof_register[regnum])
925 enum register_status status;
927 gdb_assert (read != NULL);
928 status = read (regcache, regnum, reg);
929 if (status != REG_VALID)
934 memcpy (in, reg + offset, len);
936 memcpy (reg + offset, out, len);
937 /* ... write (when needed). */
940 gdb_assert (write != NULL);
941 write (regcache, regnum, reg);
948 regcache_raw_read_part (struct regcache *regcache, int regnum,
949 int offset, int len, gdb_byte *buf)
951 struct regcache_descr *descr = regcache->descr;
953 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
954 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
955 regcache_raw_read, regcache_raw_write);
959 regcache_raw_write_part (struct regcache *regcache, int regnum,
960 int offset, int len, const gdb_byte *buf)
962 struct regcache_descr *descr = regcache->descr;
964 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
965 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
966 regcache_raw_read, regcache_raw_write);
970 regcache_cooked_read_part (struct regcache *regcache, int regnum,
971 int offset, int len, gdb_byte *buf)
973 struct regcache_descr *descr = regcache->descr;
975 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
976 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
977 regcache_cooked_read, regcache_cooked_write);
981 regcache_cooked_write_part (struct regcache *regcache, int regnum,
982 int offset, int len, const gdb_byte *buf)
984 struct regcache_descr *descr = regcache->descr;
986 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
987 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
988 regcache_cooked_read, regcache_cooked_write);
991 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
994 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
999 gdb_assert (regcache != NULL);
1000 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1001 gdb_assert (!regcache->readonly_p);
1003 regbuf = register_buffer (regcache, regnum);
1004 size = regcache->descr->sizeof_register[regnum];
1008 memcpy (regbuf, buf, size);
1009 regcache->register_status[regnum] = REG_VALID;
1013 /* This memset not strictly necessary, but better than garbage
1014 in case the register value manages to escape somewhere (due
1015 to a bug, no less). */
1016 memset (regbuf, 0, size);
1017 regcache->register_status[regnum] = REG_UNAVAILABLE;
1021 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1024 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1029 gdb_assert (regcache != NULL && buf != NULL);
1030 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1032 regbuf = register_buffer (regcache, regnum);
1033 size = regcache->descr->sizeof_register[regnum];
1034 memcpy (buf, regbuf, size);
1038 /* Special handling for register PC. */
1041 regcache_read_pc (struct regcache *regcache)
1043 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1047 if (gdbarch_read_pc_p (gdbarch))
1048 pc_val = gdbarch_read_pc (gdbarch, regcache);
1049 /* Else use per-frame method on get_current_frame. */
1050 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1054 if (regcache_cooked_read_unsigned (regcache,
1055 gdbarch_pc_regnum (gdbarch),
1056 &raw_val) == REG_UNAVAILABLE)
1057 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1059 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1062 internal_error (__FILE__, __LINE__,
1063 _("regcache_read_pc: Unable to find PC"));
1068 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1070 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1072 if (gdbarch_write_pc_p (gdbarch))
1073 gdbarch_write_pc (gdbarch, regcache, pc);
1074 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1075 regcache_cooked_write_unsigned (regcache,
1076 gdbarch_pc_regnum (gdbarch), pc);
1078 internal_error (__FILE__, __LINE__,
1079 _("regcache_write_pc: Unable to update PC"));
1081 /* Writing the PC (for instance, from "load") invalidates the
1083 reinit_frame_cache ();
1088 reg_flush_command (char *command, int from_tty)
1090 /* Force-flush the register cache. */
1091 registers_changed ();
1093 printf_filtered (_("Register cache flushed.\n"));
1097 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1098 const unsigned char *buf, long len)
1104 case BFD_ENDIAN_BIG:
1105 for (i = 0; i < len; i++)
1106 fprintf_unfiltered (file, "%02x", buf[i]);
1108 case BFD_ENDIAN_LITTLE:
1109 for (i = len - 1; i >= 0; i--)
1110 fprintf_unfiltered (file, "%02x", buf[i]);
1113 internal_error (__FILE__, __LINE__, _("Bad switch"));
1117 enum regcache_dump_what
1119 regcache_dump_none, regcache_dump_raw,
1120 regcache_dump_cooked, regcache_dump_groups,
1121 regcache_dump_remote
1125 regcache_dump (struct regcache *regcache, struct ui_file *file,
1126 enum regcache_dump_what what_to_dump)
1128 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1129 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1131 int footnote_nr = 0;
1132 int footnote_register_size = 0;
1133 int footnote_register_offset = 0;
1134 int footnote_register_type_name_null = 0;
1135 long register_offset = 0;
1136 unsigned char buf[MAX_REGISTER_SIZE];
1139 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1140 regcache->descr->nr_raw_registers);
1141 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1142 regcache->descr->nr_cooked_registers);
1143 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1144 regcache->descr->sizeof_raw_registers);
1145 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1146 regcache->descr->sizeof_raw_register_status);
1147 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1148 gdbarch_num_regs (gdbarch));
1149 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1150 gdbarch_num_pseudo_regs (gdbarch));
1153 gdb_assert (regcache->descr->nr_cooked_registers
1154 == (gdbarch_num_regs (gdbarch)
1155 + gdbarch_num_pseudo_regs (gdbarch)));
1157 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1161 fprintf_unfiltered (file, " %-10s", "Name");
1164 const char *p = gdbarch_register_name (gdbarch, regnum);
1168 else if (p[0] == '\0')
1170 fprintf_unfiltered (file, " %-10s", p);
1175 fprintf_unfiltered (file, " %4s", "Nr");
1177 fprintf_unfiltered (file, " %4d", regnum);
1179 /* Relative number. */
1181 fprintf_unfiltered (file, " %4s", "Rel");
1182 else if (regnum < gdbarch_num_regs (gdbarch))
1183 fprintf_unfiltered (file, " %4d", regnum);
1185 fprintf_unfiltered (file, " %4d",
1186 (regnum - gdbarch_num_regs (gdbarch)));
1190 fprintf_unfiltered (file, " %6s ", "Offset");
1193 fprintf_unfiltered (file, " %6ld",
1194 regcache->descr->register_offset[regnum]);
1195 if (register_offset != regcache->descr->register_offset[regnum]
1197 && (regcache->descr->register_offset[regnum]
1198 != (regcache->descr->register_offset[regnum - 1]
1199 + regcache->descr->sizeof_register[regnum - 1])))
1202 if (!footnote_register_offset)
1203 footnote_register_offset = ++footnote_nr;
1204 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1207 fprintf_unfiltered (file, " ");
1208 register_offset = (regcache->descr->register_offset[regnum]
1209 + regcache->descr->sizeof_register[regnum]);
1214 fprintf_unfiltered (file, " %5s ", "Size");
1216 fprintf_unfiltered (file, " %5ld",
1217 regcache->descr->sizeof_register[regnum]);
1227 static const char blt[] = "builtin_type";
1229 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1234 if (!footnote_register_type_name_null)
1235 footnote_register_type_name_null = ++footnote_nr;
1236 n = xstrprintf ("*%d", footnote_register_type_name_null);
1237 make_cleanup (xfree, n);
1240 /* Chop a leading builtin_type. */
1241 if (strncmp (t, blt, strlen (blt)) == 0)
1244 fprintf_unfiltered (file, " %-15s", t);
1247 /* Leading space always present. */
1248 fprintf_unfiltered (file, " ");
1251 if (what_to_dump == regcache_dump_raw)
1254 fprintf_unfiltered (file, "Raw value");
1255 else if (regnum >= regcache->descr->nr_raw_registers)
1256 fprintf_unfiltered (file, "<cooked>");
1257 else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1258 fprintf_unfiltered (file, "<invalid>");
1259 else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1260 fprintf_unfiltered (file, "<unavailable>");
1263 regcache_raw_read (regcache, regnum, buf);
1264 fprintf_unfiltered (file, "0x");
1265 dump_endian_bytes (file,
1266 gdbarch_byte_order (gdbarch), buf,
1267 regcache->descr->sizeof_register[regnum]);
1271 /* Value, cooked. */
1272 if (what_to_dump == regcache_dump_cooked)
1275 fprintf_unfiltered (file, "Cooked value");
1278 enum register_status status;
1280 status = regcache_cooked_read (regcache, regnum, buf);
1281 if (status == REG_UNKNOWN)
1282 fprintf_unfiltered (file, "<invalid>");
1283 else if (status == REG_UNAVAILABLE)
1284 fprintf_unfiltered (file, "<unavailable>");
1287 fprintf_unfiltered (file, "0x");
1288 dump_endian_bytes (file,
1289 gdbarch_byte_order (gdbarch), buf,
1290 regcache->descr->sizeof_register[regnum]);
1295 /* Group members. */
1296 if (what_to_dump == regcache_dump_groups)
1299 fprintf_unfiltered (file, "Groups");
1302 const char *sep = "";
1303 struct reggroup *group;
1305 for (group = reggroup_next (gdbarch, NULL);
1307 group = reggroup_next (gdbarch, group))
1309 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1311 fprintf_unfiltered (file,
1312 "%s%s", sep, reggroup_name (group));
1319 /* Remote packet configuration. */
1320 if (what_to_dump == regcache_dump_remote)
1324 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1326 else if (regnum < regcache->descr->nr_raw_registers)
1330 if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1332 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1336 fprintf_unfiltered (file, "\n");
1339 if (footnote_register_size)
1340 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1341 footnote_register_size);
1342 if (footnote_register_offset)
1343 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1344 footnote_register_offset);
1345 if (footnote_register_type_name_null)
1346 fprintf_unfiltered (file,
1347 "*%d: Register type's name NULL.\n",
1348 footnote_register_type_name_null);
1349 do_cleanups (cleanups);
1353 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1356 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1359 struct cleanup *cleanups;
1360 struct ui_file *file = gdb_fopen (args, "w");
1363 perror_with_name (_("maintenance print architecture"));
1364 cleanups = make_cleanup_ui_file_delete (file);
1365 regcache_dump (get_current_regcache (), file, what_to_dump);
1366 do_cleanups (cleanups);
1371 maintenance_print_registers (char *args, int from_tty)
1373 regcache_print (args, regcache_dump_none);
1377 maintenance_print_raw_registers (char *args, int from_tty)
1379 regcache_print (args, regcache_dump_raw);
1383 maintenance_print_cooked_registers (char *args, int from_tty)
1385 regcache_print (args, regcache_dump_cooked);
1389 maintenance_print_register_groups (char *args, int from_tty)
1391 regcache_print (args, regcache_dump_groups);
1395 maintenance_print_remote_registers (char *args, int from_tty)
1397 regcache_print (args, regcache_dump_remote);
1400 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1403 _initialize_regcache (void)
1405 regcache_descr_handle
1406 = gdbarch_data_register_post_init (init_regcache_descr);
1408 observer_attach_target_changed (regcache_observer_target_changed);
1409 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1411 add_com ("flushregs", class_maintenance, reg_flush_command,
1412 _("Force gdb to flush its register cache (maintainer command)"));
1414 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1415 _("Print the internal register configuration.\n"
1416 "Takes an optional file parameter."), &maintenanceprintlist);
1417 add_cmd ("raw-registers", class_maintenance,
1418 maintenance_print_raw_registers,
1419 _("Print the internal register configuration "
1420 "including raw values.\n"
1421 "Takes an optional file parameter."), &maintenanceprintlist);
1422 add_cmd ("cooked-registers", class_maintenance,
1423 maintenance_print_cooked_registers,
1424 _("Print the internal register configuration "
1425 "including cooked values.\n"
1426 "Takes an optional file parameter."), &maintenanceprintlist);
1427 add_cmd ("register-groups", class_maintenance,
1428 maintenance_print_register_groups,
1429 _("Print the internal register configuration "
1430 "including each register's group.\n"
1431 "Takes an optional file parameter."),
1432 &maintenanceprintlist);
1433 add_cmd ("remote-registers", class_maintenance,
1434 maintenance_print_remote_registers, _("\
1435 Print the internal register configuration including each register's\n\
1436 remote register number and buffer offset in the g/G packets.\n\
1437 Takes an optional file parameter."),
1438 &maintenanceprintlist);