1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2014 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "reggroups.h"
27 #include "gdb_assert.h"
30 #include "exceptions.h"
37 * Here is the actual register cache.
40 /* Per-architecture object describing the layout of a register cache.
41 Computed once when the architecture is created. */
43 struct gdbarch_data *regcache_descr_handle;
47 /* The architecture this descriptor belongs to. */
48 struct gdbarch *gdbarch;
50 /* The raw register cache. Each raw (or hard) register is supplied
51 by the target interface. The raw cache should not contain
52 redundant information - if the PC is constructed from two
53 registers then those registers and not the PC lives in the raw
56 long sizeof_raw_registers;
57 long sizeof_raw_register_status;
59 /* The cooked register space. Each cooked register in the range
60 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
61 register. The remaining [NR_RAW_REGISTERS
62 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
63 both raw registers and memory by the architecture methods
64 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
65 int nr_cooked_registers;
66 long sizeof_cooked_registers;
67 long sizeof_cooked_register_status;
69 /* Offset and size (in 8 bit bytes), of each register in the
70 register cache. All registers (including those in the range
71 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
73 long *register_offset;
74 long *sizeof_register;
76 /* Cached table containing the type of each register. */
77 struct type **register_type;
81 init_regcache_descr (struct gdbarch *gdbarch)
84 struct regcache_descr *descr;
85 gdb_assert (gdbarch != NULL);
87 /* Create an initial, zero filled, table. */
88 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
89 descr->gdbarch = gdbarch;
91 /* Total size of the register space. The raw registers are mapped
92 directly onto the raw register cache while the pseudo's are
93 either mapped onto raw-registers or memory. */
94 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
95 + gdbarch_num_pseudo_regs (gdbarch);
96 descr->sizeof_cooked_register_status
97 = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
99 /* Fill in a table of register types. */
101 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
103 for (i = 0; i < descr->nr_cooked_registers; i++)
104 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
106 /* Construct a strictly RAW register cache. Don't allow pseudo's
107 into the register cache. */
108 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
109 descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
111 /* Lay out the register cache.
113 NOTE: cagney/2002-05-22: Only register_type() is used when
114 constructing the register cache. It is assumed that the
115 register's raw size, virtual size and type length are all the
121 descr->sizeof_register
122 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
123 descr->register_offset
124 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
125 for (i = 0; i < descr->nr_raw_registers; i++)
127 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
128 descr->register_offset[i] = offset;
129 offset += descr->sizeof_register[i];
130 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
132 /* Set the real size of the raw register cache buffer. */
133 descr->sizeof_raw_registers = offset;
135 for (; i < descr->nr_cooked_registers; i++)
137 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
138 descr->register_offset[i] = offset;
139 offset += descr->sizeof_register[i];
140 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
142 /* Set the real size of the readonly register cache buffer. */
143 descr->sizeof_cooked_registers = offset;
149 static struct regcache_descr *
150 regcache_descr (struct gdbarch *gdbarch)
152 return gdbarch_data (gdbarch, regcache_descr_handle);
155 /* Utility functions returning useful register attributes stored in
156 the regcache descr. */
159 register_type (struct gdbarch *gdbarch, int regnum)
161 struct regcache_descr *descr = regcache_descr (gdbarch);
163 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164 return descr->register_type[regnum];
167 /* Utility functions returning useful register attributes stored in
168 the regcache descr. */
171 register_size (struct gdbarch *gdbarch, int regnum)
173 struct regcache_descr *descr = regcache_descr (gdbarch);
176 gdb_assert (regnum >= 0
177 && regnum < (gdbarch_num_regs (gdbarch)
178 + gdbarch_num_pseudo_regs (gdbarch)));
179 size = descr->sizeof_register[regnum];
183 /* The register cache for storing raw register values. */
187 struct regcache_descr *descr;
189 /* The address space of this register cache (for registers where it
190 makes sense, like PC or SP). */
191 struct address_space *aspace;
193 /* The register buffers. A read-only register cache can hold the
194 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
195 register cache can only hold [0 .. gdbarch_num_regs). */
197 /* Register cache status. */
198 signed char *register_status;
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. */
206 /* If this is a read-write cache, which thread's registers is
211 static struct regcache *
212 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
215 struct regcache_descr *descr;
216 struct regcache *regcache;
218 gdb_assert (gdbarch != NULL);
219 descr = regcache_descr (gdbarch);
220 regcache = XNEW (struct regcache);
221 regcache->descr = descr;
222 regcache->readonly_p = readonly_p;
226 = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
227 regcache->register_status
228 = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
233 = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
234 regcache->register_status
235 = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
237 regcache->aspace = aspace;
238 regcache->ptid = minus_one_ptid;
243 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
245 return regcache_xmalloc_1 (gdbarch, aspace, 1);
249 regcache_xfree (struct regcache *regcache)
251 if (regcache == NULL)
253 xfree (regcache->registers);
254 xfree (regcache->register_status);
259 do_regcache_xfree (void *data)
261 regcache_xfree (data);
265 make_cleanup_regcache_xfree (struct regcache *regcache)
267 return make_cleanup (do_regcache_xfree, regcache);
270 /* Return REGCACHE's architecture. */
273 get_regcache_arch (const struct regcache *regcache)
275 return regcache->descr->gdbarch;
278 struct address_space *
279 get_regcache_aspace (const struct regcache *regcache)
281 return regcache->aspace;
284 /* Return a pointer to register REGNUM's buffer cache. */
287 register_buffer (const struct regcache *regcache, int regnum)
289 return regcache->registers + regcache->descr->register_offset[regnum];
293 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
296 struct gdbarch *gdbarch = dst->descr->gdbarch;
297 gdb_byte buf[MAX_REGISTER_SIZE];
300 /* The DST should be `read-only', if it wasn't then the save would
301 end up trying to write the register values back out to the
303 gdb_assert (dst->readonly_p);
304 /* Clear the dest. */
305 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
306 memset (dst->register_status, 0,
307 dst->descr->sizeof_cooked_register_status);
308 /* Copy over any registers (identified by their membership in the
309 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
310 gdbarch_num_pseudo_regs) range is checked since some architectures need
311 to save/restore `cooked' registers that live in memory. */
312 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
314 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
316 enum register_status status = cooked_read (src, regnum, buf);
318 if (status == REG_VALID)
319 memcpy (register_buffer (dst, regnum), buf,
320 register_size (gdbarch, regnum));
323 gdb_assert (status != REG_UNKNOWN);
325 memset (register_buffer (dst, regnum), 0,
326 register_size (gdbarch, regnum));
328 dst->register_status[regnum] = status;
334 regcache_restore (struct regcache *dst,
335 regcache_cooked_read_ftype *cooked_read,
336 void *cooked_read_context)
338 struct gdbarch *gdbarch = dst->descr->gdbarch;
339 gdb_byte buf[MAX_REGISTER_SIZE];
342 /* The dst had better not be read-only. If it is, the `restore'
343 doesn't make much sense. */
344 gdb_assert (!dst->readonly_p);
345 /* Copy over any registers, being careful to only restore those that
346 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
347 + gdbarch_num_pseudo_regs) range is checked since some architectures need
348 to save/restore `cooked' registers that live in memory. */
349 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
351 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
353 enum register_status status;
355 status = cooked_read (cooked_read_context, regnum, buf);
356 if (status == REG_VALID)
357 regcache_cooked_write (dst, regnum, buf);
362 static enum register_status
363 do_cooked_read (void *src, int regnum, gdb_byte *buf)
365 struct regcache *regcache = src;
367 return regcache_cooked_read (regcache, regnum, buf);
371 regcache_cpy (struct regcache *dst, struct regcache *src)
373 gdb_assert (src != NULL && dst != NULL);
374 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
375 gdb_assert (src != dst);
376 gdb_assert (src->readonly_p || dst->readonly_p);
378 if (!src->readonly_p)
379 regcache_save (dst, do_cooked_read, src);
380 else if (!dst->readonly_p)
381 regcache_restore (dst, do_cooked_read, src);
383 regcache_cpy_no_passthrough (dst, src);
387 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
389 gdb_assert (src != NULL && dst != NULL);
390 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
391 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
392 move of data into a thread's regcache. Doing this would be silly
393 - it would mean that regcache->register_status would be
394 completely invalid. */
395 gdb_assert (dst->readonly_p && src->readonly_p);
397 memcpy (dst->registers, src->registers,
398 dst->descr->sizeof_cooked_registers);
399 memcpy (dst->register_status, src->register_status,
400 dst->descr->sizeof_cooked_register_status);
404 regcache_dup (struct regcache *src)
406 struct regcache *newbuf;
408 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
409 regcache_cpy (newbuf, src);
414 regcache_register_status (const struct regcache *regcache, int regnum)
416 gdb_assert (regcache != NULL);
417 gdb_assert (regnum >= 0);
418 if (regcache->readonly_p)
419 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
421 gdb_assert (regnum < regcache->descr->nr_raw_registers);
423 return regcache->register_status[regnum];
427 regcache_invalidate (struct regcache *regcache, int regnum)
429 gdb_assert (regcache != NULL);
430 gdb_assert (regnum >= 0);
431 gdb_assert (!regcache->readonly_p);
432 gdb_assert (regnum < regcache->descr->nr_raw_registers);
433 regcache->register_status[regnum] = REG_UNKNOWN;
437 /* Global structure containing the current regcache. */
439 /* NOTE: this is a write-through cache. There is no "dirty" bit for
440 recording if the register values have been changed (eg. by the
441 user). Therefore all registers must be written back to the
442 target when appropriate. */
446 struct regcache *regcache;
447 struct regcache_list *next;
450 static struct regcache_list *current_regcache;
453 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
454 struct address_space *aspace)
456 struct regcache_list *list;
457 struct regcache *new_regcache;
459 for (list = current_regcache; list; list = list->next)
460 if (ptid_equal (list->regcache->ptid, ptid)
461 && get_regcache_arch (list->regcache) == gdbarch)
462 return list->regcache;
464 new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
465 new_regcache->ptid = ptid;
467 list = xmalloc (sizeof (struct regcache_list));
468 list->regcache = new_regcache;
469 list->next = current_regcache;
470 current_regcache = list;
476 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
478 struct address_space *aspace;
480 /* For the benefit of "maint print registers" & co when debugging an
481 executable, allow dumping the regcache even when there is no
482 thread selected (target_thread_address_space internal-errors if
483 no address space is found). Note that normal user commands will
484 fail higher up on the call stack due to no
485 target_has_registers. */
486 aspace = (ptid_equal (null_ptid, ptid)
488 : target_thread_address_space (ptid));
490 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
493 static ptid_t current_thread_ptid;
494 static struct gdbarch *current_thread_arch;
497 get_thread_regcache (ptid_t ptid)
499 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
501 current_thread_ptid = ptid;
502 current_thread_arch = target_thread_architecture (ptid);
505 return get_thread_arch_regcache (ptid, current_thread_arch);
509 get_current_regcache (void)
511 return get_thread_regcache (inferior_ptid);
515 /* Observer for the target_changed event. */
518 regcache_observer_target_changed (struct target_ops *target)
520 registers_changed ();
523 /* Update global variables old ptids to hold NEW_PTID if they were
526 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
528 struct regcache_list *list;
530 for (list = current_regcache; list; list = list->next)
531 if (ptid_equal (list->regcache->ptid, old_ptid))
532 list->regcache->ptid = new_ptid;
535 /* Low level examining and depositing of registers.
537 The caller is responsible for making sure that the inferior is
538 stopped before calling the fetching routines, or it will get
539 garbage. (a change from GDB version 3, in which the caller got the
540 value from the last stop). */
542 /* REGISTERS_CHANGED ()
544 Indicate that registers may have changed, so invalidate the cache. */
547 registers_changed_ptid (ptid_t ptid)
549 struct regcache_list *list, **list_link;
551 list = current_regcache;
552 list_link = ¤t_regcache;
555 if (ptid_match (list->regcache->ptid, ptid))
557 struct regcache_list *dead = list;
559 *list_link = list->next;
560 regcache_xfree (list->regcache);
566 list_link = &list->next;
570 if (ptid_match (current_thread_ptid, ptid))
572 current_thread_ptid = null_ptid;
573 current_thread_arch = NULL;
576 if (ptid_match (inferior_ptid, ptid))
578 /* We just deleted the regcache of the current thread. Need to
579 forget about any frames we have cached, too. */
580 reinit_frame_cache ();
585 registers_changed (void)
587 registers_changed_ptid (minus_one_ptid);
589 /* Force cleanup of any alloca areas if using C alloca instead of
590 a builtin alloca. This particular call is used to clean up
591 areas allocated by low level target code which may build up
592 during lengthy interactions between gdb and the target before
593 gdb gives control to the user (ie watchpoints). */
598 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
600 gdb_assert (regcache != NULL && buf != NULL);
601 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
602 /* Make certain that the register cache is up-to-date with respect
603 to the current thread. This switching shouldn't be necessary
604 only there is still only one target side register cache. Sigh!
605 On the bright side, at least there is a regcache object. */
606 if (!regcache->readonly_p
607 && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
609 struct cleanup *old_chain = save_inferior_ptid ();
611 inferior_ptid = regcache->ptid;
612 target_fetch_registers (regcache, regnum);
613 do_cleanups (old_chain);
615 /* A number of targets can't access the whole set of raw
616 registers (because the debug API provides no means to get at
618 if (regcache->register_status[regnum] == REG_UNKNOWN)
619 regcache->register_status[regnum] = REG_UNAVAILABLE;
622 if (regcache->register_status[regnum] != REG_VALID)
623 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
625 memcpy (buf, register_buffer (regcache, regnum),
626 regcache->descr->sizeof_register[regnum]);
628 return regcache->register_status[regnum];
632 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
635 enum register_status status;
637 gdb_assert (regcache != NULL);
638 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
639 buf = alloca (regcache->descr->sizeof_register[regnum]);
640 status = regcache_raw_read (regcache, regnum, buf);
641 if (status == REG_VALID)
642 *val = extract_signed_integer
643 (buf, regcache->descr->sizeof_register[regnum],
644 gdbarch_byte_order (regcache->descr->gdbarch));
651 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
655 enum register_status status;
657 gdb_assert (regcache != NULL);
658 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
659 buf = alloca (regcache->descr->sizeof_register[regnum]);
660 status = regcache_raw_read (regcache, regnum, buf);
661 if (status == REG_VALID)
662 *val = extract_unsigned_integer
663 (buf, regcache->descr->sizeof_register[regnum],
664 gdbarch_byte_order (regcache->descr->gdbarch));
671 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
675 gdb_assert (regcache != NULL);
676 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
677 buf = alloca (regcache->descr->sizeof_register[regnum]);
678 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
679 gdbarch_byte_order (regcache->descr->gdbarch), val);
680 regcache_raw_write (regcache, regnum, buf);
684 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
689 gdb_assert (regcache != NULL);
690 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
691 buf = alloca (regcache->descr->sizeof_register[regnum]);
692 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
693 gdbarch_byte_order (regcache->descr->gdbarch), val);
694 regcache_raw_write (regcache, regnum, buf);
698 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
700 gdb_assert (regnum >= 0);
701 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
702 if (regnum < regcache->descr->nr_raw_registers)
703 return regcache_raw_read (regcache, regnum, buf);
704 else if (regcache->readonly_p
705 && regcache->register_status[regnum] != REG_UNKNOWN)
707 /* Read-only register cache, perhaps the cooked value was
709 if (regcache->register_status[regnum] == REG_VALID)
710 memcpy (buf, register_buffer (regcache, regnum),
711 regcache->descr->sizeof_register[regnum]);
713 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
715 return regcache->register_status[regnum];
717 else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
719 struct value *mark, *computed;
720 enum register_status result = REG_VALID;
722 mark = value_mark ();
724 computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
726 if (value_entirely_available (computed))
727 memcpy (buf, value_contents_raw (computed),
728 regcache->descr->sizeof_register[regnum]);
731 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
732 result = REG_UNAVAILABLE;
735 value_free_to_mark (mark);
740 return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
745 regcache_cooked_read_value (struct regcache *regcache, int regnum)
747 gdb_assert (regnum >= 0);
748 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
750 if (regnum < regcache->descr->nr_raw_registers
751 || (regcache->readonly_p
752 && regcache->register_status[regnum] != REG_UNKNOWN)
753 || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
755 struct value *result;
757 result = allocate_value (register_type (regcache->descr->gdbarch,
759 VALUE_LVAL (result) = lval_register;
760 VALUE_REGNUM (result) = regnum;
762 /* It is more efficient in general to do this delegation in this
763 direction than in the other one, even though the value-based
765 if (regcache_cooked_read (regcache, regnum,
766 value_contents_raw (result)) == REG_UNAVAILABLE)
767 mark_value_bytes_unavailable (result, 0,
768 TYPE_LENGTH (value_type (result)));
773 return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
778 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
781 enum register_status status;
784 gdb_assert (regcache != NULL);
785 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
786 buf = alloca (regcache->descr->sizeof_register[regnum]);
787 status = regcache_cooked_read (regcache, regnum, buf);
788 if (status == REG_VALID)
789 *val = extract_signed_integer
790 (buf, regcache->descr->sizeof_register[regnum],
791 gdbarch_byte_order (regcache->descr->gdbarch));
798 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
801 enum register_status status;
804 gdb_assert (regcache != NULL);
805 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
806 buf = alloca (regcache->descr->sizeof_register[regnum]);
807 status = regcache_cooked_read (regcache, regnum, buf);
808 if (status == REG_VALID)
809 *val = extract_unsigned_integer
810 (buf, regcache->descr->sizeof_register[regnum],
811 gdbarch_byte_order (regcache->descr->gdbarch));
818 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
823 gdb_assert (regcache != NULL);
824 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
825 buf = alloca (regcache->descr->sizeof_register[regnum]);
826 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
827 gdbarch_byte_order (regcache->descr->gdbarch), val);
828 regcache_cooked_write (regcache, regnum, buf);
832 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
837 gdb_assert (regcache != NULL);
838 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
839 buf = alloca (regcache->descr->sizeof_register[regnum]);
840 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
841 gdbarch_byte_order (regcache->descr->gdbarch), val);
842 regcache_cooked_write (regcache, regnum, buf);
846 regcache_raw_write (struct regcache *regcache, int regnum,
849 struct cleanup *old_chain;
851 gdb_assert (regcache != NULL && buf != NULL);
852 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
853 gdb_assert (!regcache->readonly_p);
855 /* On the sparc, writing %g0 is a no-op, so we don't even want to
856 change the registers array if something writes to this register. */
857 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
860 /* If we have a valid copy of the register, and new value == old
861 value, then don't bother doing the actual store. */
862 if (regcache_register_status (regcache, regnum) == REG_VALID
863 && (memcmp (register_buffer (regcache, regnum), buf,
864 regcache->descr->sizeof_register[regnum]) == 0))
867 old_chain = save_inferior_ptid ();
868 inferior_ptid = regcache->ptid;
870 target_prepare_to_store (regcache);
871 memcpy (register_buffer (regcache, regnum), buf,
872 regcache->descr->sizeof_register[regnum]);
873 regcache->register_status[regnum] = REG_VALID;
874 target_store_registers (regcache, regnum);
876 do_cleanups (old_chain);
880 regcache_cooked_write (struct regcache *regcache, int regnum,
883 gdb_assert (regnum >= 0);
884 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
885 if (regnum < regcache->descr->nr_raw_registers)
886 regcache_raw_write (regcache, regnum, buf);
888 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
892 /* Perform a partial register transfer using a read, modify, write
895 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
897 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
900 static enum register_status
901 regcache_xfer_part (struct regcache *regcache, int regnum,
902 int offset, int len, void *in, const void *out,
903 enum register_status (*read) (struct regcache *regcache,
906 void (*write) (struct regcache *regcache, int regnum,
907 const gdb_byte *buf))
909 struct regcache_descr *descr = regcache->descr;
910 gdb_byte reg[MAX_REGISTER_SIZE];
912 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
913 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
914 /* Something to do? */
915 if (offset + len == 0)
917 /* Read (when needed) ... */
920 || offset + len < descr->sizeof_register[regnum])
922 enum register_status status;
924 gdb_assert (read != NULL);
925 status = read (regcache, regnum, reg);
926 if (status != REG_VALID)
931 memcpy (in, reg + offset, len);
933 memcpy (reg + offset, out, len);
934 /* ... write (when needed). */
937 gdb_assert (write != NULL);
938 write (regcache, regnum, reg);
945 regcache_raw_read_part (struct regcache *regcache, int regnum,
946 int offset, int len, gdb_byte *buf)
948 struct regcache_descr *descr = regcache->descr;
950 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
951 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
952 regcache_raw_read, regcache_raw_write);
956 regcache_raw_write_part (struct regcache *regcache, int regnum,
957 int offset, int len, const gdb_byte *buf)
959 struct regcache_descr *descr = regcache->descr;
961 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
962 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
963 regcache_raw_read, regcache_raw_write);
967 regcache_cooked_read_part (struct regcache *regcache, int regnum,
968 int offset, int len, gdb_byte *buf)
970 struct regcache_descr *descr = regcache->descr;
972 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
973 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
974 regcache_cooked_read, regcache_cooked_write);
978 regcache_cooked_write_part (struct regcache *regcache, int regnum,
979 int offset, int len, const gdb_byte *buf)
981 struct regcache_descr *descr = regcache->descr;
983 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
984 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
985 regcache_cooked_read, regcache_cooked_write);
988 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
991 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
996 gdb_assert (regcache != NULL);
997 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
998 gdb_assert (!regcache->readonly_p);
1000 regbuf = register_buffer (regcache, regnum);
1001 size = regcache->descr->sizeof_register[regnum];
1005 memcpy (regbuf, buf, size);
1006 regcache->register_status[regnum] = REG_VALID;
1010 /* This memset not strictly necessary, but better than garbage
1011 in case the register value manages to escape somewhere (due
1012 to a bug, no less). */
1013 memset (regbuf, 0, size);
1014 regcache->register_status[regnum] = REG_UNAVAILABLE;
1018 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1021 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1026 gdb_assert (regcache != NULL && buf != NULL);
1027 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1029 regbuf = register_buffer (regcache, regnum);
1030 size = regcache->descr->sizeof_register[regnum];
1031 memcpy (buf, regbuf, size);
1035 /* Special handling for register PC. */
1038 regcache_read_pc (struct regcache *regcache)
1040 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1044 if (gdbarch_read_pc_p (gdbarch))
1045 pc_val = gdbarch_read_pc (gdbarch, regcache);
1046 /* Else use per-frame method on get_current_frame. */
1047 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1051 if (regcache_cooked_read_unsigned (regcache,
1052 gdbarch_pc_regnum (gdbarch),
1053 &raw_val) == REG_UNAVAILABLE)
1054 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1056 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1059 internal_error (__FILE__, __LINE__,
1060 _("regcache_read_pc: Unable to find PC"));
1065 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1067 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1069 if (gdbarch_write_pc_p (gdbarch))
1070 gdbarch_write_pc (gdbarch, regcache, pc);
1071 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1072 regcache_cooked_write_unsigned (regcache,
1073 gdbarch_pc_regnum (gdbarch), pc);
1075 internal_error (__FILE__, __LINE__,
1076 _("regcache_write_pc: Unable to update PC"));
1078 /* Writing the PC (for instance, from "load") invalidates the
1080 reinit_frame_cache ();
1085 reg_flush_command (char *command, int from_tty)
1087 /* Force-flush the register cache. */
1088 registers_changed ();
1090 printf_filtered (_("Register cache flushed.\n"));
1093 enum regcache_dump_what
1095 regcache_dump_none, regcache_dump_raw,
1096 regcache_dump_cooked, regcache_dump_groups,
1097 regcache_dump_remote
1101 regcache_dump (struct regcache *regcache, struct ui_file *file,
1102 enum regcache_dump_what what_to_dump)
1104 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1105 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1107 int footnote_nr = 0;
1108 int footnote_register_size = 0;
1109 int footnote_register_offset = 0;
1110 int footnote_register_type_name_null = 0;
1111 long register_offset = 0;
1112 gdb_byte buf[MAX_REGISTER_SIZE];
1115 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1116 regcache->descr->nr_raw_registers);
1117 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1118 regcache->descr->nr_cooked_registers);
1119 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1120 regcache->descr->sizeof_raw_registers);
1121 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1122 regcache->descr->sizeof_raw_register_status);
1123 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1124 gdbarch_num_regs (gdbarch));
1125 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1126 gdbarch_num_pseudo_regs (gdbarch));
1129 gdb_assert (regcache->descr->nr_cooked_registers
1130 == (gdbarch_num_regs (gdbarch)
1131 + gdbarch_num_pseudo_regs (gdbarch)));
1133 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1137 fprintf_unfiltered (file, " %-10s", "Name");
1140 const char *p = gdbarch_register_name (gdbarch, regnum);
1144 else if (p[0] == '\0')
1146 fprintf_unfiltered (file, " %-10s", p);
1151 fprintf_unfiltered (file, " %4s", "Nr");
1153 fprintf_unfiltered (file, " %4d", regnum);
1155 /* Relative number. */
1157 fprintf_unfiltered (file, " %4s", "Rel");
1158 else if (regnum < gdbarch_num_regs (gdbarch))
1159 fprintf_unfiltered (file, " %4d", regnum);
1161 fprintf_unfiltered (file, " %4d",
1162 (regnum - gdbarch_num_regs (gdbarch)));
1166 fprintf_unfiltered (file, " %6s ", "Offset");
1169 fprintf_unfiltered (file, " %6ld",
1170 regcache->descr->register_offset[regnum]);
1171 if (register_offset != regcache->descr->register_offset[regnum]
1173 && (regcache->descr->register_offset[regnum]
1174 != (regcache->descr->register_offset[regnum - 1]
1175 + regcache->descr->sizeof_register[regnum - 1])))
1178 if (!footnote_register_offset)
1179 footnote_register_offset = ++footnote_nr;
1180 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1183 fprintf_unfiltered (file, " ");
1184 register_offset = (regcache->descr->register_offset[regnum]
1185 + regcache->descr->sizeof_register[regnum]);
1190 fprintf_unfiltered (file, " %5s ", "Size");
1192 fprintf_unfiltered (file, " %5ld",
1193 regcache->descr->sizeof_register[regnum]);
1203 static const char blt[] = "builtin_type";
1205 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1210 if (!footnote_register_type_name_null)
1211 footnote_register_type_name_null = ++footnote_nr;
1212 n = xstrprintf ("*%d", footnote_register_type_name_null);
1213 make_cleanup (xfree, n);
1216 /* Chop a leading builtin_type. */
1217 if (strncmp (t, blt, strlen (blt)) == 0)
1220 fprintf_unfiltered (file, " %-15s", t);
1223 /* Leading space always present. */
1224 fprintf_unfiltered (file, " ");
1227 if (what_to_dump == regcache_dump_raw)
1230 fprintf_unfiltered (file, "Raw value");
1231 else if (regnum >= regcache->descr->nr_raw_registers)
1232 fprintf_unfiltered (file, "<cooked>");
1233 else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1234 fprintf_unfiltered (file, "<invalid>");
1235 else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1236 fprintf_unfiltered (file, "<unavailable>");
1239 regcache_raw_read (regcache, regnum, buf);
1240 print_hex_chars (file, buf,
1241 regcache->descr->sizeof_register[regnum],
1242 gdbarch_byte_order (gdbarch));
1246 /* Value, cooked. */
1247 if (what_to_dump == regcache_dump_cooked)
1250 fprintf_unfiltered (file, "Cooked value");
1253 enum register_status status;
1255 status = regcache_cooked_read (regcache, regnum, buf);
1256 if (status == REG_UNKNOWN)
1257 fprintf_unfiltered (file, "<invalid>");
1258 else if (status == REG_UNAVAILABLE)
1259 fprintf_unfiltered (file, "<unavailable>");
1261 print_hex_chars (file, buf,
1262 regcache->descr->sizeof_register[regnum],
1263 gdbarch_byte_order (gdbarch));
1267 /* Group members. */
1268 if (what_to_dump == regcache_dump_groups)
1271 fprintf_unfiltered (file, "Groups");
1274 const char *sep = "";
1275 struct reggroup *group;
1277 for (group = reggroup_next (gdbarch, NULL);
1279 group = reggroup_next (gdbarch, group))
1281 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1283 fprintf_unfiltered (file,
1284 "%s%s", sep, reggroup_name (group));
1291 /* Remote packet configuration. */
1292 if (what_to_dump == regcache_dump_remote)
1296 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1298 else if (regnum < regcache->descr->nr_raw_registers)
1302 if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1304 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1308 fprintf_unfiltered (file, "\n");
1311 if (footnote_register_size)
1312 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1313 footnote_register_size);
1314 if (footnote_register_offset)
1315 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1316 footnote_register_offset);
1317 if (footnote_register_type_name_null)
1318 fprintf_unfiltered (file,
1319 "*%d: Register type's name NULL.\n",
1320 footnote_register_type_name_null);
1321 do_cleanups (cleanups);
1325 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1328 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1331 struct cleanup *cleanups;
1332 struct ui_file *file = gdb_fopen (args, "w");
1335 perror_with_name (_("maintenance print architecture"));
1336 cleanups = make_cleanup_ui_file_delete (file);
1337 regcache_dump (get_current_regcache (), file, what_to_dump);
1338 do_cleanups (cleanups);
1343 maintenance_print_registers (char *args, int from_tty)
1345 regcache_print (args, regcache_dump_none);
1349 maintenance_print_raw_registers (char *args, int from_tty)
1351 regcache_print (args, regcache_dump_raw);
1355 maintenance_print_cooked_registers (char *args, int from_tty)
1357 regcache_print (args, regcache_dump_cooked);
1361 maintenance_print_register_groups (char *args, int from_tty)
1363 regcache_print (args, regcache_dump_groups);
1367 maintenance_print_remote_registers (char *args, int from_tty)
1369 regcache_print (args, regcache_dump_remote);
1372 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1375 _initialize_regcache (void)
1377 regcache_descr_handle
1378 = gdbarch_data_register_post_init (init_regcache_descr);
1380 observer_attach_target_changed (regcache_observer_target_changed);
1381 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1383 add_com ("flushregs", class_maintenance, reg_flush_command,
1384 _("Force gdb to flush its register cache (maintainer command)"));
1386 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1387 _("Print the internal register configuration.\n"
1388 "Takes an optional file parameter."), &maintenanceprintlist);
1389 add_cmd ("raw-registers", class_maintenance,
1390 maintenance_print_raw_registers,
1391 _("Print the internal register configuration "
1392 "including raw values.\n"
1393 "Takes an optional file parameter."), &maintenanceprintlist);
1394 add_cmd ("cooked-registers", class_maintenance,
1395 maintenance_print_cooked_registers,
1396 _("Print the internal register configuration "
1397 "including cooked values.\n"
1398 "Takes an optional file parameter."), &maintenanceprintlist);
1399 add_cmd ("register-groups", class_maintenance,
1400 maintenance_print_register_groups,
1401 _("Print the internal register configuration "
1402 "including each register's group.\n"
1403 "Takes an optional file parameter."),
1404 &maintenanceprintlist);
1405 add_cmd ("remote-registers", class_maintenance,
1406 maintenance_print_remote_registers, _("\
1407 Print the internal register configuration including each register's\n\
1408 remote register number and buffer offset in the g/G packets.\n\
1409 Takes an optional file parameter."),
1410 &maintenanceprintlist);