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_status
;
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_status
;
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
72 long *register_offset
;
73 long *sizeof_register
;
75 /* Cached table containing the type of each register. */
76 struct type
**register_type
;
80 init_regcache_descr (struct gdbarch
*gdbarch
)
83 struct regcache_descr
*descr
;
84 gdb_assert (gdbarch
!= NULL
);
86 /* Create an initial, zero filled, table. */
87 descr
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct regcache_descr
);
88 descr
->gdbarch
= gdbarch
;
90 /* Total size of the register space. The raw registers are mapped
91 directly onto the raw register cache while the pseudo's are
92 either mapped onto raw-registers or memory. */
93 descr
->nr_cooked_registers
= gdbarch_num_regs (gdbarch
)
94 + gdbarch_num_pseudo_regs (gdbarch
);
95 descr
->sizeof_cooked_register_status
96 = gdbarch_num_regs (gdbarch
) + gdbarch_num_pseudo_regs (gdbarch
);
98 /* Fill in a table of register types. */
100 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
,
102 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
103 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
105 /* Construct a strictly RAW register cache. Don't allow pseudo's
106 into the register cache. */
107 descr
->nr_raw_registers
= gdbarch_num_regs (gdbarch
);
108 descr
->sizeof_raw_register_status
= gdbarch_num_regs (gdbarch
);
110 /* Lay out the register cache.
112 NOTE: cagney/2002-05-22: Only register_type() is used when
113 constructing the register cache. It is assumed that the
114 register's raw size, virtual size and type length are all the
120 descr
->sizeof_register
121 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
122 descr
->register_offset
123 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
124 for (i
= 0; i
< descr
->nr_raw_registers
; i
++)
126 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
127 descr
->register_offset
[i
] = offset
;
128 offset
+= descr
->sizeof_register
[i
];
129 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
131 /* Set the real size of the raw register cache buffer. */
132 descr
->sizeof_raw_registers
= offset
;
134 for (; i
< descr
->nr_cooked_registers
; i
++)
136 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
137 descr
->register_offset
[i
] = offset
;
138 offset
+= descr
->sizeof_register
[i
];
139 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
141 /* Set the real size of the readonly register cache buffer. */
142 descr
->sizeof_cooked_registers
= offset
;
148 static struct regcache_descr
*
149 regcache_descr (struct gdbarch
*gdbarch
)
151 return gdbarch_data (gdbarch
, regcache_descr_handle
);
154 /* Utility functions returning useful register attributes stored in
155 the regcache descr. */
158 register_type (struct gdbarch
*gdbarch
, int regnum
)
160 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
162 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
163 return descr
->register_type
[regnum
];
166 /* Utility functions returning useful register attributes stored in
167 the regcache descr. */
170 register_size (struct gdbarch
*gdbarch
, int regnum
)
172 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
175 gdb_assert (regnum
>= 0
176 && regnum
< (gdbarch_num_regs (gdbarch
)
177 + gdbarch_num_pseudo_regs (gdbarch
)));
178 size
= descr
->sizeof_register
[regnum
];
182 /* The register cache for storing raw register values. */
186 struct regcache_descr
*descr
;
188 /* The address space of this register cache (for registers where it
189 makes sense, like PC or SP). */
190 struct address_space
*aspace
;
192 /* The register buffers. A read-only register cache can hold the
193 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
194 register cache can only hold [0 .. gdbarch_num_regs). */
196 /* Register cache status. */
197 signed char *register_status
;
198 /* Is this a read-only cache? A read-only cache is used for saving
199 the target's register state (e.g, across an inferior function
200 call or just before forcing a function return). A read-only
201 cache can only be updated via the methods regcache_dup() and
202 regcache_cpy(). The actual contents are determined by the
203 reggroup_save and reggroup_restore methods. */
205 /* If this is a read-write cache, which thread's registers is
210 static struct regcache
*
211 regcache_xmalloc_1 (struct gdbarch
*gdbarch
, struct address_space
*aspace
,
214 struct regcache_descr
*descr
;
215 struct regcache
*regcache
;
217 gdb_assert (gdbarch
!= NULL
);
218 descr
= regcache_descr (gdbarch
);
219 regcache
= XMALLOC (struct regcache
);
220 regcache
->descr
= descr
;
221 regcache
->readonly_p
= readonly_p
;
225 = XCALLOC (descr
->sizeof_cooked_registers
, gdb_byte
);
226 regcache
->register_status
227 = XCALLOC (descr
->sizeof_cooked_register_status
, gdb_byte
);
232 = XCALLOC (descr
->sizeof_raw_registers
, gdb_byte
);
233 regcache
->register_status
234 = XCALLOC (descr
->sizeof_raw_register_status
, gdb_byte
);
236 regcache
->aspace
= aspace
;
237 regcache
->ptid
= minus_one_ptid
;
242 regcache_xmalloc (struct gdbarch
*gdbarch
, struct address_space
*aspace
)
244 return regcache_xmalloc_1 (gdbarch
, aspace
, 1);
248 regcache_xfree (struct regcache
*regcache
)
250 if (regcache
== NULL
)
252 xfree (regcache
->registers
);
253 xfree (regcache
->register_status
);
258 do_regcache_xfree (void *data
)
260 regcache_xfree (data
);
264 make_cleanup_regcache_xfree (struct regcache
*regcache
)
266 return make_cleanup (do_regcache_xfree
, regcache
);
269 /* Return REGCACHE's architecture. */
272 get_regcache_arch (const struct regcache
*regcache
)
274 return regcache
->descr
->gdbarch
;
277 struct address_space
*
278 get_regcache_aspace (const struct regcache
*regcache
)
280 return regcache
->aspace
;
283 /* Return a pointer to register REGNUM's buffer cache. */
286 register_buffer (const struct regcache
*regcache
, int regnum
)
288 return regcache
->registers
+ regcache
->descr
->register_offset
[regnum
];
292 regcache_save (struct regcache
*dst
, regcache_cooked_read_ftype
*cooked_read
,
295 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
296 gdb_byte buf
[MAX_REGISTER_SIZE
];
299 /* The DST should be `read-only', if it wasn't then the save would
300 end up trying to write the register values back out to the
302 gdb_assert (dst
->readonly_p
);
303 /* Clear the dest. */
304 memset (dst
->registers
, 0, dst
->descr
->sizeof_cooked_registers
);
305 memset (dst
->register_status
, 0,
306 dst
->descr
->sizeof_cooked_register_status
);
307 /* Copy over any registers (identified by their membership in the
308 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
309 gdbarch_num_pseudo_regs) range is checked since some architectures need
310 to save/restore `cooked' registers that live in memory. */
311 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
313 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
315 int valid
= cooked_read (src
, regnum
, buf
);
319 memcpy (register_buffer (dst
, regnum
), buf
,
320 register_size (gdbarch
, regnum
));
321 dst
->register_status
[regnum
] = REG_VALID
;
328 regcache_restore (struct regcache
*dst
,
329 regcache_cooked_read_ftype
*cooked_read
,
330 void *cooked_read_context
)
332 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
333 gdb_byte buf
[MAX_REGISTER_SIZE
];
336 /* The dst had better not be read-only. If it is, the `restore'
337 doesn't make much sense. */
338 gdb_assert (!dst
->readonly_p
);
339 /* Copy over any registers, being careful to only restore those that
340 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
341 + gdbarch_num_pseudo_regs) range is checked since some architectures need
342 to save/restore `cooked' registers that live in memory. */
343 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
345 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
347 int valid
= cooked_read (cooked_read_context
, regnum
, buf
);
350 regcache_cooked_write (dst
, regnum
, buf
);
356 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
358 struct regcache
*regcache
= src
;
360 if (regcache
->register_status
[regnum
] == REG_UNKNOWN
&& regcache
->readonly_p
)
361 /* Don't even think about fetching a register from a read-only
362 cache when the register isn't yet valid. There isn't a target
363 from which the register value can be fetched. */
365 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_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
455 struct regcache_list
*list
;
456 struct regcache
*new_regcache
;
458 for (list
= current_regcache
; list
; list
= list
->next
)
459 if (ptid_equal (list
->regcache
->ptid
, ptid
)
460 && get_regcache_arch (list
->regcache
) == gdbarch
)
461 return list
->regcache
;
463 new_regcache
= regcache_xmalloc_1 (gdbarch
,
464 target_thread_address_space (ptid
), 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
;
533 int wildcard
= ptid_equal (ptid
, minus_one_ptid
);
535 list
= current_regcache
;
536 list_link
= ¤t_regcache
;
539 if (ptid_match (list
->regcache
->ptid
, ptid
))
541 struct regcache_list
*dead
= list
;
543 *list_link
= list
->next
;
544 regcache_xfree (list
->regcache
);
550 list_link
= &list
->next
;
554 if (wildcard
|| ptid_equal (ptid
, current_thread_ptid
))
556 current_thread_ptid
= null_ptid
;
557 current_thread_arch
= NULL
;
560 if (wildcard
|| ptid_equal (ptid
, inferior_ptid
))
562 /* We just deleted the regcache of the current thread. Need to
563 forget about any frames we have cached, too. */
564 reinit_frame_cache ();
569 registers_changed (void)
571 registers_changed_ptid (minus_one_ptid
);
573 /* Force cleanup of any alloca areas if using C alloca instead of
574 a builtin alloca. This particular call is used to clean up
575 areas allocated by low level target code which may build up
576 during lengthy interactions between gdb and the target before
577 gdb gives control to the user (ie watchpoints). */
582 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
584 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
585 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
586 /* Make certain that the register cache is up-to-date with respect
587 to the current thread. This switching shouldn't be necessary
588 only there is still only one target side register cache. Sigh!
589 On the bright side, at least there is a regcache object. */
590 if (!regcache
->readonly_p
)
592 if (regcache_register_status (regcache
, regnum
) == REG_UNKNOWN
)
594 struct cleanup
*old_chain
= save_inferior_ptid ();
596 inferior_ptid
= regcache
->ptid
;
597 target_fetch_registers (regcache
, regnum
);
598 do_cleanups (old_chain
);
601 /* FIXME: cagney/2004-08-07: At present a number of targets
602 forget (or didn't know that they needed) to set this leading to
603 panics. Also is the problem that targets need to indicate
604 that a register is in one of the possible states: valid,
605 undefined, unknown. The last of which isn't yet
607 gdb_assert (regcache_register_status (regcache
, regnum
) == REG_VALID
);
610 /* Copy the value directly into the register cache. */
611 memcpy (buf
, register_buffer (regcache
, regnum
),
612 regcache
->descr
->sizeof_register
[regnum
]);
616 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
620 gdb_assert (regcache
!= NULL
);
621 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
622 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
623 regcache_raw_read (regcache
, regnum
, buf
);
624 (*val
) = extract_signed_integer
625 (buf
, regcache
->descr
->sizeof_register
[regnum
],
626 gdbarch_byte_order (regcache
->descr
->gdbarch
));
630 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
635 gdb_assert (regcache
!= NULL
);
636 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
637 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
638 regcache_raw_read (regcache
, regnum
, buf
);
639 (*val
) = extract_unsigned_integer
640 (buf
, regcache
->descr
->sizeof_register
[regnum
],
641 gdbarch_byte_order (regcache
->descr
->gdbarch
));
645 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
649 gdb_assert (regcache
!= NULL
);
650 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
651 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
652 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
653 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
654 regcache_raw_write (regcache
, regnum
, buf
);
658 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
663 gdb_assert (regcache
!= NULL
);
664 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
665 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
666 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
667 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
668 regcache_raw_write (regcache
, regnum
, buf
);
672 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
674 gdb_assert (regnum
>= 0);
675 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
676 if (regnum
< regcache
->descr
->nr_raw_registers
)
677 regcache_raw_read (regcache
, regnum
, buf
);
678 else if (regcache
->readonly_p
679 && regnum
< regcache
->descr
->nr_cooked_registers
680 && regcache
->register_status
[regnum
] == REG_VALID
)
681 /* Read-only register cache, and the cooked value was cached. */
682 memcpy (buf
, register_buffer (regcache
, regnum
),
683 regcache
->descr
->sizeof_register
[regnum
]);
685 gdbarch_pseudo_register_read (regcache
->descr
->gdbarch
, regcache
,
690 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
695 gdb_assert (regcache
!= NULL
);
696 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
697 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
698 regcache_cooked_read (regcache
, regnum
, buf
);
699 (*val
) = extract_signed_integer
700 (buf
, regcache
->descr
->sizeof_register
[regnum
],
701 gdbarch_byte_order (regcache
->descr
->gdbarch
));
705 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
710 gdb_assert (regcache
!= NULL
);
711 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
712 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
713 regcache_cooked_read (regcache
, regnum
, buf
);
714 (*val
) = extract_unsigned_integer
715 (buf
, regcache
->descr
->sizeof_register
[regnum
],
716 gdbarch_byte_order (regcache
->descr
->gdbarch
));
720 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
725 gdb_assert (regcache
!= NULL
);
726 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
727 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
728 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
729 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
730 regcache_cooked_write (regcache
, regnum
, buf
);
734 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
739 gdb_assert (regcache
!= NULL
);
740 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
741 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
742 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
743 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
744 regcache_cooked_write (regcache
, regnum
, buf
);
748 regcache_raw_write (struct regcache
*regcache
, int regnum
,
751 struct cleanup
*old_chain
;
753 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
754 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
755 gdb_assert (!regcache
->readonly_p
);
757 /* On the sparc, writing %g0 is a no-op, so we don't even want to
758 change the registers array if something writes to this register. */
759 if (gdbarch_cannot_store_register (get_regcache_arch (regcache
), regnum
))
762 /* If we have a valid copy of the register, and new value == old
763 value, then don't bother doing the actual store. */
764 if (regcache_register_status (regcache
, regnum
) == REG_VALID
765 && (memcmp (register_buffer (regcache
, regnum
), buf
,
766 regcache
->descr
->sizeof_register
[regnum
]) == 0))
769 old_chain
= save_inferior_ptid ();
770 inferior_ptid
= regcache
->ptid
;
772 target_prepare_to_store (regcache
);
773 memcpy (register_buffer (regcache
, regnum
), buf
,
774 regcache
->descr
->sizeof_register
[regnum
]);
775 regcache
->register_status
[regnum
] = REG_VALID
;
776 target_store_registers (regcache
, regnum
);
778 do_cleanups (old_chain
);
782 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
785 gdb_assert (regnum
>= 0);
786 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
787 if (regnum
< regcache
->descr
->nr_raw_registers
)
788 regcache_raw_write (regcache
, regnum
, buf
);
790 gdbarch_pseudo_register_write (regcache
->descr
->gdbarch
, regcache
,
794 /* Perform a partial register transfer using a read, modify, write
797 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
799 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
803 regcache_xfer_part (struct regcache
*regcache
, int regnum
,
804 int offset
, int len
, void *in
, const void *out
,
805 void (*read
) (struct regcache
*regcache
, int regnum
,
807 void (*write
) (struct regcache
*regcache
, int regnum
,
808 const gdb_byte
*buf
))
810 struct regcache_descr
*descr
= regcache
->descr
;
811 gdb_byte reg
[MAX_REGISTER_SIZE
];
813 gdb_assert (offset
>= 0 && offset
<= descr
->sizeof_register
[regnum
]);
814 gdb_assert (len
>= 0 && offset
+ len
<= descr
->sizeof_register
[regnum
]);
815 /* Something to do? */
816 if (offset
+ len
== 0)
818 /* Read (when needed) ... */
821 || offset
+ len
< descr
->sizeof_register
[regnum
])
823 gdb_assert (read
!= NULL
);
824 read (regcache
, regnum
, reg
);
828 memcpy (in
, reg
+ offset
, len
);
830 memcpy (reg
+ offset
, out
, len
);
831 /* ... write (when needed). */
834 gdb_assert (write
!= NULL
);
835 write (regcache
, regnum
, reg
);
840 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
841 int offset
, int len
, gdb_byte
*buf
)
843 struct regcache_descr
*descr
= regcache
->descr
;
845 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
846 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
847 regcache_raw_read
, regcache_raw_write
);
851 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
852 int offset
, int len
, const gdb_byte
*buf
)
854 struct regcache_descr
*descr
= regcache
->descr
;
856 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
857 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
858 regcache_raw_read
, regcache_raw_write
);
862 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
863 int offset
, int len
, gdb_byte
*buf
)
865 struct regcache_descr
*descr
= regcache
->descr
;
867 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
868 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
869 regcache_cooked_read
, regcache_cooked_write
);
873 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
874 int offset
, int len
, const gdb_byte
*buf
)
876 struct regcache_descr
*descr
= regcache
->descr
;
878 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
879 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
880 regcache_cooked_read
, regcache_cooked_write
);
883 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
886 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
891 gdb_assert (regcache
!= NULL
);
892 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
893 gdb_assert (!regcache
->readonly_p
);
895 regbuf
= register_buffer (regcache
, regnum
);
896 size
= regcache
->descr
->sizeof_register
[regnum
];
900 memcpy (regbuf
, buf
, size
);
901 regcache
->register_status
[regnum
] = REG_VALID
;
905 /* This memset not strictly necessary, but better than garbage
906 in case the register value manages to escape somewhere (due
907 to a bug, no less). */
908 memset (regbuf
, 0, size
);
909 regcache
->register_status
[regnum
] = REG_UNAVAILABLE
;
913 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
916 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
921 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
922 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
924 regbuf
= register_buffer (regcache
, regnum
);
925 size
= regcache
->descr
->sizeof_register
[regnum
];
926 memcpy (buf
, regbuf
, size
);
930 /* Special handling for register PC. */
933 regcache_read_pc (struct regcache
*regcache
)
935 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
939 if (gdbarch_read_pc_p (gdbarch
))
940 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
941 /* Else use per-frame method on get_current_frame. */
942 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
946 regcache_cooked_read_unsigned (regcache
,
947 gdbarch_pc_regnum (gdbarch
),
949 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
952 internal_error (__FILE__
, __LINE__
,
953 _("regcache_read_pc: Unable to find PC"));
958 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
960 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
962 if (gdbarch_write_pc_p (gdbarch
))
963 gdbarch_write_pc (gdbarch
, regcache
, pc
);
964 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
965 regcache_cooked_write_unsigned (regcache
,
966 gdbarch_pc_regnum (gdbarch
), pc
);
968 internal_error (__FILE__
, __LINE__
,
969 _("regcache_write_pc: Unable to update PC"));
971 /* Writing the PC (for instance, from "load") invalidates the
973 reinit_frame_cache ();
978 reg_flush_command (char *command
, int from_tty
)
980 /* Force-flush the register cache. */
981 registers_changed ();
983 printf_filtered (_("Register cache flushed.\n"));
987 dump_endian_bytes (struct ui_file
*file
, enum bfd_endian endian
,
988 const unsigned char *buf
, long len
)
995 for (i
= 0; i
< len
; i
++)
996 fprintf_unfiltered (file
, "%02x", buf
[i
]);
998 case BFD_ENDIAN_LITTLE
:
999 for (i
= len
- 1; i
>= 0; i
--)
1000 fprintf_unfiltered (file
, "%02x", buf
[i
]);
1003 internal_error (__FILE__
, __LINE__
, _("Bad switch"));
1007 enum regcache_dump_what
1009 regcache_dump_none
, regcache_dump_raw
,
1010 regcache_dump_cooked
, regcache_dump_groups
1014 regcache_dump (struct regcache
*regcache
, struct ui_file
*file
,
1015 enum regcache_dump_what what_to_dump
)
1017 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
1018 struct gdbarch
*gdbarch
= regcache
->descr
->gdbarch
;
1020 int footnote_nr
= 0;
1021 int footnote_register_size
= 0;
1022 int footnote_register_offset
= 0;
1023 int footnote_register_type_name_null
= 0;
1024 long register_offset
= 0;
1025 unsigned char buf
[MAX_REGISTER_SIZE
];
1028 fprintf_unfiltered (file
, "nr_raw_registers %d\n",
1029 regcache
->descr
->nr_raw_registers
);
1030 fprintf_unfiltered (file
, "nr_cooked_registers %d\n",
1031 regcache
->descr
->nr_cooked_registers
);
1032 fprintf_unfiltered (file
, "sizeof_raw_registers %ld\n",
1033 regcache
->descr
->sizeof_raw_registers
);
1034 fprintf_unfiltered (file
, "sizeof_raw_register_status %ld\n",
1035 regcache
->descr
->sizeof_raw_register_status
);
1036 fprintf_unfiltered (file
, "gdbarch_num_regs %d\n",
1037 gdbarch_num_regs (gdbarch
));
1038 fprintf_unfiltered (file
, "gdbarch_num_pseudo_regs %d\n",
1039 gdbarch_num_pseudo_regs (gdbarch
));
1042 gdb_assert (regcache
->descr
->nr_cooked_registers
1043 == (gdbarch_num_regs (gdbarch
)
1044 + gdbarch_num_pseudo_regs (gdbarch
)));
1046 for (regnum
= -1; regnum
< regcache
->descr
->nr_cooked_registers
; regnum
++)
1050 fprintf_unfiltered (file
, " %-10s", "Name");
1053 const char *p
= gdbarch_register_name (gdbarch
, regnum
);
1057 else if (p
[0] == '\0')
1059 fprintf_unfiltered (file
, " %-10s", p
);
1064 fprintf_unfiltered (file
, " %4s", "Nr");
1066 fprintf_unfiltered (file
, " %4d", regnum
);
1068 /* Relative number. */
1070 fprintf_unfiltered (file
, " %4s", "Rel");
1071 else if (regnum
< gdbarch_num_regs (gdbarch
))
1072 fprintf_unfiltered (file
, " %4d", regnum
);
1074 fprintf_unfiltered (file
, " %4d",
1075 (regnum
- gdbarch_num_regs (gdbarch
)));
1079 fprintf_unfiltered (file
, " %6s ", "Offset");
1082 fprintf_unfiltered (file
, " %6ld",
1083 regcache
->descr
->register_offset
[regnum
]);
1084 if (register_offset
!= regcache
->descr
->register_offset
[regnum
]
1086 && (regcache
->descr
->register_offset
[regnum
]
1087 != (regcache
->descr
->register_offset
[regnum
- 1]
1088 + regcache
->descr
->sizeof_register
[regnum
- 1])))
1091 if (!footnote_register_offset
)
1092 footnote_register_offset
= ++footnote_nr
;
1093 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1096 fprintf_unfiltered (file
, " ");
1097 register_offset
= (regcache
->descr
->register_offset
[regnum
]
1098 + regcache
->descr
->sizeof_register
[regnum
]);
1103 fprintf_unfiltered (file
, " %5s ", "Size");
1105 fprintf_unfiltered (file
, " %5ld",
1106 regcache
->descr
->sizeof_register
[regnum
]);
1116 static const char blt
[] = "builtin_type";
1118 t
= TYPE_NAME (register_type (regcache
->descr
->gdbarch
, regnum
));
1123 if (!footnote_register_type_name_null
)
1124 footnote_register_type_name_null
= ++footnote_nr
;
1125 n
= xstrprintf ("*%d", footnote_register_type_name_null
);
1126 make_cleanup (xfree
, n
);
1129 /* Chop a leading builtin_type. */
1130 if (strncmp (t
, blt
, strlen (blt
)) == 0)
1133 fprintf_unfiltered (file
, " %-15s", t
);
1136 /* Leading space always present. */
1137 fprintf_unfiltered (file
, " ");
1140 if (what_to_dump
== regcache_dump_raw
)
1143 fprintf_unfiltered (file
, "Raw value");
1144 else if (regnum
>= regcache
->descr
->nr_raw_registers
)
1145 fprintf_unfiltered (file
, "<cooked>");
1146 else if (regcache_register_status (regcache
, regnum
) == REG_UNKNOWN
)
1147 fprintf_unfiltered (file
, "<invalid>");
1148 else if (regcache_register_status (regcache
, regnum
) == REG_UNAVAILABLE
)
1149 fprintf_unfiltered (file
, "<unavailable>");
1152 regcache_raw_read (regcache
, regnum
, buf
);
1153 fprintf_unfiltered (file
, "0x");
1154 dump_endian_bytes (file
,
1155 gdbarch_byte_order (gdbarch
), buf
,
1156 regcache
->descr
->sizeof_register
[regnum
]);
1160 /* Value, cooked. */
1161 if (what_to_dump
== regcache_dump_cooked
)
1164 fprintf_unfiltered (file
, "Cooked value");
1167 /* FIXME: no way for cooked reads to signal unavailable
1169 regcache_cooked_read (regcache
, regnum
, buf
);
1170 fprintf_unfiltered (file
, "0x");
1171 dump_endian_bytes (file
,
1172 gdbarch_byte_order (gdbarch
), buf
,
1173 regcache
->descr
->sizeof_register
[regnum
]);
1177 /* Group members. */
1178 if (what_to_dump
== regcache_dump_groups
)
1181 fprintf_unfiltered (file
, "Groups");
1184 const char *sep
= "";
1185 struct reggroup
*group
;
1187 for (group
= reggroup_next (gdbarch
, NULL
);
1189 group
= reggroup_next (gdbarch
, group
))
1191 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
1193 fprintf_unfiltered (file
,
1194 "%s%s", sep
, reggroup_name (group
));
1201 fprintf_unfiltered (file
, "\n");
1204 if (footnote_register_size
)
1205 fprintf_unfiltered (file
, "*%d: Inconsistent register sizes.\n",
1206 footnote_register_size
);
1207 if (footnote_register_offset
)
1208 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1209 footnote_register_offset
);
1210 if (footnote_register_type_name_null
)
1211 fprintf_unfiltered (file
,
1212 "*%d: Register type's name NULL.\n",
1213 footnote_register_type_name_null
);
1214 do_cleanups (cleanups
);
1218 regcache_print (char *args
, enum regcache_dump_what what_to_dump
)
1221 regcache_dump (get_current_regcache (), gdb_stdout
, what_to_dump
);
1224 struct cleanup
*cleanups
;
1225 struct ui_file
*file
= gdb_fopen (args
, "w");
1228 perror_with_name (_("maintenance print architecture"));
1229 cleanups
= make_cleanup_ui_file_delete (file
);
1230 regcache_dump (get_current_regcache (), file
, what_to_dump
);
1231 do_cleanups (cleanups
);
1236 maintenance_print_registers (char *args
, int from_tty
)
1238 regcache_print (args
, regcache_dump_none
);
1242 maintenance_print_raw_registers (char *args
, int from_tty
)
1244 regcache_print (args
, regcache_dump_raw
);
1248 maintenance_print_cooked_registers (char *args
, int from_tty
)
1250 regcache_print (args
, regcache_dump_cooked
);
1254 maintenance_print_register_groups (char *args
, int from_tty
)
1256 regcache_print (args
, regcache_dump_groups
);
1259 extern initialize_file_ftype _initialize_regcache
; /* -Wmissing-prototype */
1262 _initialize_regcache (void)
1264 regcache_descr_handle
1265 = gdbarch_data_register_post_init (init_regcache_descr
);
1267 observer_attach_target_changed (regcache_observer_target_changed
);
1268 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed
);
1270 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1271 _("Force gdb to flush its register cache (maintainer command)"));
1273 add_cmd ("registers", class_maintenance
, maintenance_print_registers
,
1274 _("Print the internal register configuration.\n"
1275 "Takes an optional file parameter."), &maintenanceprintlist
);
1276 add_cmd ("raw-registers", class_maintenance
,
1277 maintenance_print_raw_registers
,
1278 _("Print the internal register configuration "
1279 "including raw values.\n"
1280 "Takes an optional file parameter."), &maintenanceprintlist
);
1281 add_cmd ("cooked-registers", class_maintenance
,
1282 maintenance_print_cooked_registers
,
1283 _("Print the internal register configuration "
1284 "including cooked values.\n"
1285 "Takes an optional file parameter."), &maintenanceprintlist
);
1286 add_cmd ("register-groups", class_maintenance
,
1287 maintenance_print_register_groups
,
1288 _("Print the internal register configuration "
1289 "including each register's group.\n"
1290 "Takes an optional file parameter."),
1291 &maintenanceprintlist
);