1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6 This file is part of GDB.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
27 #include "reggroups.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdbcmd.h" /* For maintenanceprintlist. */
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created */
42 struct gdbarch_data
*regcache_descr_handle
;
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch
*gdbarch
;
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
55 long sizeof_raw_registers
;
56 long sizeof_raw_register_valid_p
;
58 /* The cooked register space. Each cooked register in the range
59 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
60 register. The remaining [NR_RAW_REGISTERS
61 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
62 both raw registers and memory by the architecture methods
63 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
64 int nr_cooked_registers
;
65 long sizeof_cooked_registers
;
66 long sizeof_cooked_register_valid_p
;
68 /* Offset and size (in 8 bit bytes), of reach register in the
69 register cache. All registers (including those in the range
70 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an 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 (gdbarch
)
96 + gdbarch_num_pseudo_regs (gdbarch
);
97 descr
->sizeof_cooked_register_valid_p
= gdbarch_num_regs (gdbarch
)
98 + gdbarch_num_pseudo_regs
101 /* Fill in a table of register types. */
103 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
,
105 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
106 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
108 /* Construct a strictly RAW register cache. Don't allow pseudo's
109 into the register cache. */
110 descr
->nr_raw_registers
= gdbarch_num_regs (gdbarch
);
112 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
113 array. This pretects GDB from erant code that accesses elements
114 of the global register_valid_p[] array in the range
115 [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
116 descr
->sizeof_raw_register_valid_p
= descr
->sizeof_cooked_register_valid_p
;
118 /* Lay out the register cache.
120 NOTE: cagney/2002-05-22: Only register_type() is used when
121 constructing the register cache. It is assumed that the
122 register's raw size, virtual size and type length are all the
128 descr
->sizeof_register
129 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
130 descr
->register_offset
131 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
132 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
134 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
135 descr
->register_offset
[i
] = offset
;
136 offset
+= descr
->sizeof_register
[i
];
137 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
139 /* Set the real size of the register cache buffer. */
140 descr
->sizeof_cooked_registers
= offset
;
143 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
144 the raw registers. Unfortunately some code still accesses the
145 register array directly using the global registers[]. Until that
146 code has been purged, play safe and over allocating the register
148 descr
->sizeof_raw_registers
= descr
->sizeof_cooked_registers
;
153 static struct regcache_descr
*
154 regcache_descr (struct gdbarch
*gdbarch
)
156 return gdbarch_data (gdbarch
, regcache_descr_handle
);
159 /* Utility functions returning useful register attributes stored in
160 the regcache descr. */
163 register_type (struct gdbarch
*gdbarch
, int regnum
)
165 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
167 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
168 return descr
->register_type
[regnum
];
171 /* Utility functions returning useful register attributes stored in
172 the regcache descr. */
175 register_size (struct gdbarch
*gdbarch
, int regnum
)
177 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
180 gdb_assert (regnum
>= 0
181 && regnum
< (gdbarch_num_regs (gdbarch
)
182 + gdbarch_num_pseudo_regs (gdbarch
)));
183 size
= descr
->sizeof_register
[regnum
];
187 /* The register cache for storing raw register values. */
191 struct regcache_descr
*descr
;
193 /* The address space of this register cache (for registers where it
194 makes sense, like PC or SP). */
195 struct address_space
*aspace
;
197 /* The register buffers. A read-only register cache can hold the
198 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
199 register cache can only hold [0 .. gdbarch_num_regs). */
201 /* Register cache status:
202 register_valid_p[REG] == 0 if REG value is not in the cache
203 > 0 if REG value is in the cache
204 < 0 if REG value is permanently unavailable */
205 signed char *register_valid_p
;
206 /* Is this a read-only cache? A read-only cache is used for saving
207 the target's register state (e.g, across an inferior function
208 call or just before forcing a function return). A read-only
209 cache can only be updated via the methods regcache_dup() and
210 regcache_cpy(). The actual contents are determined by the
211 reggroup_save and reggroup_restore methods. */
213 /* If this is a read-write cache, which thread's registers is
219 regcache_xmalloc (struct gdbarch
*gdbarch
, struct address_space
*aspace
)
221 struct regcache_descr
*descr
;
222 struct regcache
*regcache
;
224 gdb_assert (gdbarch
!= NULL
);
225 descr
= regcache_descr (gdbarch
);
226 regcache
= XMALLOC (struct regcache
);
227 regcache
->descr
= descr
;
229 = XCALLOC (descr
->sizeof_raw_registers
, gdb_byte
);
230 regcache
->register_valid_p
231 = XCALLOC (descr
->sizeof_raw_register_valid_p
, gdb_byte
);
232 regcache
->aspace
= aspace
;
233 regcache
->readonly_p
= 1;
234 regcache
->ptid
= minus_one_ptid
;
239 regcache_xfree (struct regcache
*regcache
)
241 if (regcache
== NULL
)
243 xfree (regcache
->registers
);
244 xfree (regcache
->register_valid_p
);
249 do_regcache_xfree (void *data
)
251 regcache_xfree (data
);
255 make_cleanup_regcache_xfree (struct regcache
*regcache
)
257 return make_cleanup (do_regcache_xfree
, regcache
);
260 /* Return REGCACHE's architecture. */
263 get_regcache_arch (const struct regcache
*regcache
)
265 return regcache
->descr
->gdbarch
;
268 struct address_space
*
269 get_regcache_aspace (const struct regcache
*regcache
)
271 return regcache
->aspace
;
274 /* Return a pointer to register REGNUM's buffer cache. */
277 register_buffer (const struct regcache
*regcache
, int regnum
)
279 return regcache
->registers
+ regcache
->descr
->register_offset
[regnum
];
283 regcache_save (struct regcache
*dst
, regcache_cooked_read_ftype
*cooked_read
,
286 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
287 gdb_byte buf
[MAX_REGISTER_SIZE
];
290 /* The DST should be `read-only', if it wasn't then the save would
291 end up trying to write the register values back out to the
293 gdb_assert (dst
->readonly_p
);
294 /* Clear the dest. */
295 memset (dst
->registers
, 0, dst
->descr
->sizeof_cooked_registers
);
296 memset (dst
->register_valid_p
, 0,
297 dst
->descr
->sizeof_cooked_register_valid_p
);
298 /* Copy over any registers (identified by their membership in the
299 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
300 gdbarch_num_pseudo_regs) range is checked since some architectures need
301 to save/restore `cooked' registers that live in memory. */
302 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
304 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
306 int valid
= cooked_read (src
, regnum
, buf
);
310 memcpy (register_buffer (dst
, regnum
), buf
,
311 register_size (gdbarch
, regnum
));
312 dst
->register_valid_p
[regnum
] = 1;
319 regcache_restore (struct regcache
*dst
,
320 regcache_cooked_read_ftype
*cooked_read
,
321 void *cooked_read_context
)
323 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
324 gdb_byte buf
[MAX_REGISTER_SIZE
];
327 /* The dst had better not be read-only. If it is, the `restore'
328 doesn't make much sense. */
329 gdb_assert (!dst
->readonly_p
);
330 /* Copy over any registers, being careful to only restore those that
331 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
332 + gdbarch_num_pseudo_regs) range is checked since some architectures need
333 to save/restore `cooked' registers that live in memory. */
334 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
336 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
338 int valid
= cooked_read (cooked_read_context
, regnum
, buf
);
341 regcache_cooked_write (dst
, regnum
, buf
);
347 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
349 struct regcache
*regcache
= src
;
351 if (!regcache
->register_valid_p
[regnum
] && regcache
->readonly_p
)
352 /* Don't even think about fetching a register from a read-only
353 cache when the register isn't yet valid. There isn't a target
354 from which the register value can be fetched. */
356 regcache_cooked_read (regcache
, regnum
, buf
);
362 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
364 gdb_assert (src
!= NULL
&& dst
!= NULL
);
365 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
366 gdb_assert (src
!= dst
);
367 gdb_assert (src
->readonly_p
|| dst
->readonly_p
);
369 if (!src
->readonly_p
)
370 regcache_save (dst
, do_cooked_read
, src
);
371 else if (!dst
->readonly_p
)
372 regcache_restore (dst
, do_cooked_read
, src
);
374 regcache_cpy_no_passthrough (dst
, src
);
378 regcache_cpy_no_passthrough (struct regcache
*dst
, struct regcache
*src
)
380 gdb_assert (src
!= NULL
&& dst
!= NULL
);
381 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
382 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
383 move of data into the current regcache. Doing this would be
384 silly - it would mean that valid_p would be completely invalid. */
385 gdb_assert (dst
->readonly_p
);
387 memcpy (dst
->registers
, src
->registers
, dst
->descr
->sizeof_raw_registers
);
388 memcpy (dst
->register_valid_p
, src
->register_valid_p
,
389 dst
->descr
->sizeof_raw_register_valid_p
);
393 regcache_dup (struct regcache
*src
)
395 struct regcache
*newbuf
;
397 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
, get_regcache_aspace (src
));
398 regcache_cpy (newbuf
, src
);
403 regcache_dup_no_passthrough (struct regcache
*src
)
405 struct regcache
*newbuf
;
407 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
, get_regcache_aspace (src
));
408 regcache_cpy_no_passthrough (newbuf
, src
);
413 regcache_valid_p (const struct regcache
*regcache
, int regnum
)
415 gdb_assert (regcache
!= NULL
);
416 gdb_assert (regnum
>= 0);
417 if (regcache
->readonly_p
)
418 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
420 gdb_assert (regnum
< regcache
->descr
->nr_raw_registers
);
422 return regcache
->register_valid_p
[regnum
];
426 regcache_invalidate (struct regcache
*regcache
, int regnum
)
428 gdb_assert (regcache
!= NULL
);
429 gdb_assert (regnum
>= 0);
430 gdb_assert (!regcache
->readonly_p
);
431 gdb_assert (regnum
< regcache
->descr
->nr_raw_registers
);
432 regcache
->register_valid_p
[regnum
] = 0;
436 /* Global structure containing the current regcache. */
438 /* NOTE: this is a write-through cache. There is no "dirty" bit for
439 recording if the register values have been changed (eg. by the
440 user). Therefore all registers must be written back to the
441 target when appropriate. */
445 struct regcache
*regcache
;
446 struct regcache_list
*next
;
449 static struct regcache_list
*current_regcache
;
452 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
454 struct regcache_list
*list
;
455 struct regcache
*new_regcache
;
457 for (list
= current_regcache
; list
; list
= list
->next
)
458 if (ptid_equal (list
->regcache
->ptid
, ptid
)
459 && get_regcache_arch (list
->regcache
) == gdbarch
)
460 return list
->regcache
;
462 new_regcache
= regcache_xmalloc (gdbarch
,
463 target_thread_address_space (ptid
));
464 new_regcache
->readonly_p
= 0;
465 new_regcache
->ptid
= ptid
;
466 gdb_assert (new_regcache
->aspace
!= NULL
);
468 list
= xmalloc (sizeof (struct regcache_list
));
469 list
->regcache
= new_regcache
;
470 list
->next
= current_regcache
;
471 current_regcache
= list
;
476 static ptid_t current_thread_ptid
;
477 static struct gdbarch
*current_thread_arch
;
480 get_thread_regcache (ptid_t ptid
)
482 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
484 current_thread_ptid
= ptid
;
485 current_thread_arch
= target_thread_architecture (ptid
);
488 return get_thread_arch_regcache (ptid
, current_thread_arch
);
492 get_current_regcache (void)
494 return get_thread_regcache (inferior_ptid
);
498 /* Observer for the target_changed event. */
501 regcache_observer_target_changed (struct target_ops
*target
)
503 registers_changed ();
506 /* Update global variables old ptids to hold NEW_PTID if they were
509 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
511 struct regcache_list
*list
;
513 for (list
= current_regcache
; list
; list
= list
->next
)
514 if (ptid_equal (list
->regcache
->ptid
, old_ptid
))
515 list
->regcache
->ptid
= new_ptid
;
518 /* Low level examining and depositing of registers.
520 The caller is responsible for making sure that the inferior is
521 stopped before calling the fetching routines, or it will get
522 garbage. (a change from GDB version 3, in which the caller got the
523 value from the last stop). */
525 /* REGISTERS_CHANGED ()
527 Indicate that registers may have changed, so invalidate the cache. */
530 registers_changed_ptid (ptid_t ptid
)
532 struct regcache_list
*list
, **list_link
;
534 list
= current_regcache
;
535 list_link
= ¤t_regcache
;
538 if (ptid_match (list
->regcache
->ptid
, ptid
))
540 struct regcache_list
*dead
= list
;
542 *list_link
= list
->next
;
543 regcache_xfree (list
->regcache
);
549 list_link
= &list
->next
;
553 current_regcache
= NULL
;
555 current_thread_ptid
= null_ptid
;
556 current_thread_arch
= NULL
;
558 /* Need to forget about any frames we have cached, too. */
559 reinit_frame_cache ();
561 /* Force cleanup of any alloca areas if using C alloca instead of
562 a builtin alloca. This particular call is used to clean up
563 areas allocated by low level target code which may build up
564 during lengthy interactions between gdb and the target before
565 gdb gives control to the user (ie watchpoints). */
570 registers_changed (void)
572 registers_changed_ptid (minus_one_ptid
);
576 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
578 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
579 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
580 /* Make certain that the register cache is up-to-date with respect
581 to the current thread. This switching shouldn't be necessary
582 only there is still only one target side register cache. Sigh!
583 On the bright side, at least there is a regcache object. */
584 if (!regcache
->readonly_p
)
586 if (!regcache_valid_p (regcache
, regnum
))
588 struct cleanup
*old_chain
= save_inferior_ptid ();
590 inferior_ptid
= regcache
->ptid
;
591 target_fetch_registers (regcache
, regnum
);
592 do_cleanups (old_chain
);
595 /* FIXME: cagney/2004-08-07: At present a number of targets
596 forget (or didn't know that they needed) to set this leading to
597 panics. Also is the problem that targets need to indicate
598 that a register is in one of the possible states: valid,
599 undefined, unknown. The last of which isn't yet
601 gdb_assert (regcache_valid_p (regcache
, regnum
));
604 /* Copy the value directly into the register cache. */
605 memcpy (buf
, register_buffer (regcache
, regnum
),
606 regcache
->descr
->sizeof_register
[regnum
]);
610 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
614 gdb_assert (regcache
!= NULL
);
615 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
616 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
617 regcache_raw_read (regcache
, regnum
, buf
);
618 (*val
) = extract_signed_integer
619 (buf
, regcache
->descr
->sizeof_register
[regnum
],
620 gdbarch_byte_order (regcache
->descr
->gdbarch
));
624 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
629 gdb_assert (regcache
!= NULL
);
630 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
631 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
632 regcache_raw_read (regcache
, regnum
, buf
);
633 (*val
) = extract_unsigned_integer
634 (buf
, regcache
->descr
->sizeof_register
[regnum
],
635 gdbarch_byte_order (regcache
->descr
->gdbarch
));
639 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
643 gdb_assert (regcache
!= NULL
);
644 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
645 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
646 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
647 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
648 regcache_raw_write (regcache
, regnum
, buf
);
652 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
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 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
661 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
662 regcache_raw_write (regcache
, regnum
, buf
);
666 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
668 gdb_assert (regnum
>= 0);
669 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
670 if (regnum
< regcache
->descr
->nr_raw_registers
)
671 regcache_raw_read (regcache
, regnum
, buf
);
672 else if (regcache
->readonly_p
673 && regnum
< regcache
->descr
->nr_cooked_registers
674 && regcache
->register_valid_p
[regnum
])
675 /* Read-only register cache, perhaps the cooked value was cached? */
676 memcpy (buf
, register_buffer (regcache
, regnum
),
677 regcache
->descr
->sizeof_register
[regnum
]);
679 gdbarch_pseudo_register_read (regcache
->descr
->gdbarch
, regcache
,
684 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
689 gdb_assert (regcache
!= NULL
);
690 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
691 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
692 regcache_cooked_read (regcache
, regnum
, buf
);
693 (*val
) = extract_signed_integer
694 (buf
, regcache
->descr
->sizeof_register
[regnum
],
695 gdbarch_byte_order (regcache
->descr
->gdbarch
));
699 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
704 gdb_assert (regcache
!= NULL
);
705 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
706 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
707 regcache_cooked_read (regcache
, regnum
, buf
);
708 (*val
) = extract_unsigned_integer
709 (buf
, regcache
->descr
->sizeof_register
[regnum
],
710 gdbarch_byte_order (regcache
->descr
->gdbarch
));
714 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
719 gdb_assert (regcache
!= NULL
);
720 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
721 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
722 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
723 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
724 regcache_cooked_write (regcache
, regnum
, buf
);
728 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
733 gdb_assert (regcache
!= NULL
);
734 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
735 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
736 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
737 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
738 regcache_cooked_write (regcache
, regnum
, buf
);
742 regcache_raw_write (struct regcache
*regcache
, int regnum
,
745 struct cleanup
*old_chain
;
747 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
748 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
749 gdb_assert (!regcache
->readonly_p
);
751 /* On the sparc, writing %g0 is a no-op, so we don't even want to
752 change the registers array if something writes to this register. */
753 if (gdbarch_cannot_store_register (get_regcache_arch (regcache
), regnum
))
756 /* If we have a valid copy of the register, and new value == old
757 value, then don't bother doing the actual store. */
758 if (regcache_valid_p (regcache
, regnum
)
759 && (memcmp (register_buffer (regcache
, regnum
), buf
,
760 regcache
->descr
->sizeof_register
[regnum
]) == 0))
763 old_chain
= save_inferior_ptid ();
764 inferior_ptid
= regcache
->ptid
;
766 target_prepare_to_store (regcache
);
767 memcpy (register_buffer (regcache
, regnum
), buf
,
768 regcache
->descr
->sizeof_register
[regnum
]);
769 regcache
->register_valid_p
[regnum
] = 1;
770 target_store_registers (regcache
, regnum
);
772 do_cleanups (old_chain
);
776 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
779 gdb_assert (regnum
>= 0);
780 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
781 if (regnum
< regcache
->descr
->nr_raw_registers
)
782 regcache_raw_write (regcache
, regnum
, buf
);
784 gdbarch_pseudo_register_write (regcache
->descr
->gdbarch
, regcache
,
788 /* Perform a partial register transfer using a read, modify, write
791 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
793 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
797 regcache_xfer_part (struct regcache
*regcache
, int regnum
,
798 int offset
, int len
, void *in
, const void *out
,
799 void (*read
) (struct regcache
*regcache
, int regnum
,
801 void (*write
) (struct regcache
*regcache
, int regnum
,
802 const gdb_byte
*buf
))
804 struct regcache_descr
*descr
= regcache
->descr
;
805 gdb_byte reg
[MAX_REGISTER_SIZE
];
807 gdb_assert (offset
>= 0 && offset
<= descr
->sizeof_register
[regnum
]);
808 gdb_assert (len
>= 0 && offset
+ len
<= descr
->sizeof_register
[regnum
]);
809 /* Something to do? */
810 if (offset
+ len
== 0)
812 /* Read (when needed) ... */
815 || offset
+ len
< descr
->sizeof_register
[regnum
])
817 gdb_assert (read
!= NULL
);
818 read (regcache
, regnum
, reg
);
822 memcpy (in
, reg
+ offset
, len
);
824 memcpy (reg
+ offset
, out
, len
);
825 /* ... write (when needed). */
828 gdb_assert (write
!= NULL
);
829 write (regcache
, regnum
, reg
);
834 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
835 int offset
, int len
, gdb_byte
*buf
)
837 struct regcache_descr
*descr
= regcache
->descr
;
839 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
840 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
841 regcache_raw_read
, regcache_raw_write
);
845 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
846 int offset
, int len
, const gdb_byte
*buf
)
848 struct regcache_descr
*descr
= regcache
->descr
;
850 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
851 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
852 regcache_raw_read
, regcache_raw_write
);
856 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
857 int offset
, int len
, gdb_byte
*buf
)
859 struct regcache_descr
*descr
= regcache
->descr
;
861 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
862 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
863 regcache_cooked_read
, regcache_cooked_write
);
867 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
868 int offset
, int len
, const gdb_byte
*buf
)
870 struct regcache_descr
*descr
= regcache
->descr
;
872 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
873 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
874 regcache_cooked_read
, regcache_cooked_write
);
877 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
880 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
885 gdb_assert (regcache
!= NULL
);
886 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
887 gdb_assert (!regcache
->readonly_p
);
889 regbuf
= register_buffer (regcache
, regnum
);
890 size
= regcache
->descr
->sizeof_register
[regnum
];
893 memcpy (regbuf
, buf
, size
);
895 memset (regbuf
, 0, size
);
897 /* Mark the register as cached. */
898 regcache
->register_valid_p
[regnum
] = 1;
901 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
904 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
909 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
910 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
912 regbuf
= register_buffer (regcache
, regnum
);
913 size
= regcache
->descr
->sizeof_register
[regnum
];
914 memcpy (buf
, regbuf
, size
);
918 /* Special handling for register PC. */
921 regcache_read_pc (struct regcache
*regcache
)
923 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
927 if (gdbarch_read_pc_p (gdbarch
))
928 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
929 /* Else use per-frame method on get_current_frame. */
930 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
934 regcache_cooked_read_unsigned (regcache
,
935 gdbarch_pc_regnum (gdbarch
),
937 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
940 internal_error (__FILE__
, __LINE__
,
941 _("regcache_read_pc: Unable to find PC"));
946 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
948 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
950 if (gdbarch_write_pc_p (gdbarch
))
951 gdbarch_write_pc (gdbarch
, regcache
, pc
);
952 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
953 regcache_cooked_write_unsigned (regcache
,
954 gdbarch_pc_regnum (gdbarch
), pc
);
956 internal_error (__FILE__
, __LINE__
,
957 _("regcache_write_pc: Unable to update PC"));
959 /* Writing the PC (for instance, from "load") invalidates the
961 reinit_frame_cache ();
966 reg_flush_command (char *command
, int from_tty
)
968 /* Force-flush the register cache. */
969 registers_changed ();
971 printf_filtered (_("Register cache flushed.\n"));
975 dump_endian_bytes (struct ui_file
*file
, enum bfd_endian endian
,
976 const unsigned char *buf
, long len
)
983 for (i
= 0; i
< len
; i
++)
984 fprintf_unfiltered (file
, "%02x", buf
[i
]);
986 case BFD_ENDIAN_LITTLE
:
987 for (i
= len
- 1; i
>= 0; i
--)
988 fprintf_unfiltered (file
, "%02x", buf
[i
]);
991 internal_error (__FILE__
, __LINE__
, _("Bad switch"));
995 enum regcache_dump_what
997 regcache_dump_none
, regcache_dump_raw
,
998 regcache_dump_cooked
, regcache_dump_groups
1002 regcache_dump (struct regcache
*regcache
, struct ui_file
*file
,
1003 enum regcache_dump_what what_to_dump
)
1005 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
1006 struct gdbarch
*gdbarch
= regcache
->descr
->gdbarch
;
1008 int footnote_nr
= 0;
1009 int footnote_register_size
= 0;
1010 int footnote_register_offset
= 0;
1011 int footnote_register_type_name_null
= 0;
1012 long register_offset
= 0;
1013 unsigned char buf
[MAX_REGISTER_SIZE
];
1016 fprintf_unfiltered (file
, "nr_raw_registers %d\n",
1017 regcache
->descr
->nr_raw_registers
);
1018 fprintf_unfiltered (file
, "nr_cooked_registers %d\n",
1019 regcache
->descr
->nr_cooked_registers
);
1020 fprintf_unfiltered (file
, "sizeof_raw_registers %ld\n",
1021 regcache
->descr
->sizeof_raw_registers
);
1022 fprintf_unfiltered (file
, "sizeof_raw_register_valid_p %ld\n",
1023 regcache
->descr
->sizeof_raw_register_valid_p
);
1024 fprintf_unfiltered (file
, "gdbarch_num_regs %d\n",
1025 gdbarch_num_regs (gdbarch
));
1026 fprintf_unfiltered (file
, "gdbarch_num_pseudo_regs %d\n",
1027 gdbarch_num_pseudo_regs (gdbarch
));
1030 gdb_assert (regcache
->descr
->nr_cooked_registers
1031 == (gdbarch_num_regs (gdbarch
)
1032 + gdbarch_num_pseudo_regs (gdbarch
)));
1034 for (regnum
= -1; regnum
< regcache
->descr
->nr_cooked_registers
; regnum
++)
1038 fprintf_unfiltered (file
, " %-10s", "Name");
1041 const char *p
= gdbarch_register_name (gdbarch
, regnum
);
1045 else if (p
[0] == '\0')
1047 fprintf_unfiltered (file
, " %-10s", p
);
1052 fprintf_unfiltered (file
, " %4s", "Nr");
1054 fprintf_unfiltered (file
, " %4d", regnum
);
1056 /* Relative number. */
1058 fprintf_unfiltered (file
, " %4s", "Rel");
1059 else if (regnum
< gdbarch_num_regs (gdbarch
))
1060 fprintf_unfiltered (file
, " %4d", regnum
);
1062 fprintf_unfiltered (file
, " %4d",
1063 (regnum
- gdbarch_num_regs (gdbarch
)));
1067 fprintf_unfiltered (file
, " %6s ", "Offset");
1070 fprintf_unfiltered (file
, " %6ld",
1071 regcache
->descr
->register_offset
[regnum
]);
1072 if (register_offset
!= regcache
->descr
->register_offset
[regnum
]
1074 && (regcache
->descr
->register_offset
[regnum
]
1075 != (regcache
->descr
->register_offset
[regnum
- 1]
1076 + regcache
->descr
->sizeof_register
[regnum
- 1])))
1079 if (!footnote_register_offset
)
1080 footnote_register_offset
= ++footnote_nr
;
1081 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1084 fprintf_unfiltered (file
, " ");
1085 register_offset
= (regcache
->descr
->register_offset
[regnum
]
1086 + regcache
->descr
->sizeof_register
[regnum
]);
1091 fprintf_unfiltered (file
, " %5s ", "Size");
1093 fprintf_unfiltered (file
, " %5ld",
1094 regcache
->descr
->sizeof_register
[regnum
]);
1104 static const char blt
[] = "builtin_type";
1106 t
= TYPE_NAME (register_type (regcache
->descr
->gdbarch
, regnum
));
1111 if (!footnote_register_type_name_null
)
1112 footnote_register_type_name_null
= ++footnote_nr
;
1113 n
= xstrprintf ("*%d", footnote_register_type_name_null
);
1114 make_cleanup (xfree
, n
);
1117 /* Chop a leading builtin_type. */
1118 if (strncmp (t
, blt
, strlen (blt
)) == 0)
1121 fprintf_unfiltered (file
, " %-15s", t
);
1124 /* Leading space always present. */
1125 fprintf_unfiltered (file
, " ");
1128 if (what_to_dump
== regcache_dump_raw
)
1131 fprintf_unfiltered (file
, "Raw value");
1132 else if (regnum
>= regcache
->descr
->nr_raw_registers
)
1133 fprintf_unfiltered (file
, "<cooked>");
1134 else if (!regcache_valid_p (regcache
, regnum
))
1135 fprintf_unfiltered (file
, "<invalid>");
1138 regcache_raw_read (regcache
, regnum
, buf
);
1139 fprintf_unfiltered (file
, "0x");
1140 dump_endian_bytes (file
,
1141 gdbarch_byte_order (gdbarch
), buf
,
1142 regcache
->descr
->sizeof_register
[regnum
]);
1146 /* Value, cooked. */
1147 if (what_to_dump
== regcache_dump_cooked
)
1150 fprintf_unfiltered (file
, "Cooked value");
1153 regcache_cooked_read (regcache
, regnum
, buf
);
1154 fprintf_unfiltered (file
, "0x");
1155 dump_endian_bytes (file
,
1156 gdbarch_byte_order (gdbarch
), buf
,
1157 regcache
->descr
->sizeof_register
[regnum
]);
1161 /* Group members. */
1162 if (what_to_dump
== regcache_dump_groups
)
1165 fprintf_unfiltered (file
, "Groups");
1168 const char *sep
= "";
1169 struct reggroup
*group
;
1171 for (group
= reggroup_next (gdbarch
, NULL
);
1173 group
= reggroup_next (gdbarch
, group
))
1175 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
1177 fprintf_unfiltered (file
,
1178 "%s%s", sep
, reggroup_name (group
));
1185 fprintf_unfiltered (file
, "\n");
1188 if (footnote_register_size
)
1189 fprintf_unfiltered (file
, "*%d: Inconsistent register sizes.\n",
1190 footnote_register_size
);
1191 if (footnote_register_offset
)
1192 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1193 footnote_register_offset
);
1194 if (footnote_register_type_name_null
)
1195 fprintf_unfiltered (file
,
1196 "*%d: Register type's name NULL.\n",
1197 footnote_register_type_name_null
);
1198 do_cleanups (cleanups
);
1202 regcache_print (char *args
, enum regcache_dump_what what_to_dump
)
1205 regcache_dump (get_current_regcache (), gdb_stdout
, what_to_dump
);
1208 struct cleanup
*cleanups
;
1209 struct ui_file
*file
= gdb_fopen (args
, "w");
1212 perror_with_name (_("maintenance print architecture"));
1213 cleanups
= make_cleanup_ui_file_delete (file
);
1214 regcache_dump (get_current_regcache (), file
, what_to_dump
);
1215 do_cleanups (cleanups
);
1220 maintenance_print_registers (char *args
, int from_tty
)
1222 regcache_print (args
, regcache_dump_none
);
1226 maintenance_print_raw_registers (char *args
, int from_tty
)
1228 regcache_print (args
, regcache_dump_raw
);
1232 maintenance_print_cooked_registers (char *args
, int from_tty
)
1234 regcache_print (args
, regcache_dump_cooked
);
1238 maintenance_print_register_groups (char *args
, int from_tty
)
1240 regcache_print (args
, regcache_dump_groups
);
1243 extern initialize_file_ftype _initialize_regcache
; /* -Wmissing-prototype */
1246 _initialize_regcache (void)
1248 regcache_descr_handle
1249 = gdbarch_data_register_post_init (init_regcache_descr
);
1251 observer_attach_target_changed (regcache_observer_target_changed
);
1252 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed
);
1254 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1255 _("Force gdb to flush its register cache (maintainer command)"));
1257 add_cmd ("registers", class_maintenance
, maintenance_print_registers
,
1258 _("Print the internal register configuration.\n"
1259 "Takes an optional file parameter."), &maintenanceprintlist
);
1260 add_cmd ("raw-registers", class_maintenance
,
1261 maintenance_print_raw_registers
,
1262 _("Print the internal register configuration "
1263 "including raw values.\n"
1264 "Takes an optional file parameter."), &maintenanceprintlist
);
1265 add_cmd ("cooked-registers", class_maintenance
,
1266 maintenance_print_cooked_registers
,
1267 _("Print the internal register configuration "
1268 "including cooked values.\n"
1269 "Takes an optional file parameter."), &maintenanceprintlist
);
1270 add_cmd ("register-groups", class_maintenance
,
1271 maintenance_print_register_groups
,
1272 _("Print the internal register configuration "
1273 "including each register's group.\n"
1274 "Takes an optional file parameter."),
1275 &maintenanceprintlist
);