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"
29 #include "exceptions.h"
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 each 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
= XNEW (struct regcache
);
220 regcache
->descr
= descr
;
221 regcache
->readonly_p
= readonly_p
;
225 = XCNEWVEC (gdb_byte
, descr
->sizeof_cooked_registers
);
226 regcache
->register_status
227 = XCNEWVEC (signed char, descr
->sizeof_cooked_register_status
);
232 = XCNEWVEC (gdb_byte
, descr
->sizeof_raw_registers
);
233 regcache
->register_status
234 = XCNEWVEC (signed char, descr
->sizeof_raw_register_status
);
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 /* Cleanup routines for invalidating a register. */
271 struct register_to_invalidate
273 struct regcache
*regcache
;
278 do_regcache_invalidate (void *data
)
280 struct register_to_invalidate
*reg
= data
;
282 regcache_invalidate (reg
->regcache
, reg
->regnum
);
285 static struct cleanup
*
286 make_cleanup_regcache_invalidate (struct regcache
*regcache
, int regnum
)
288 struct register_to_invalidate
* reg
= XNEW (struct register_to_invalidate
);
290 reg
->regcache
= regcache
;
291 reg
->regnum
= regnum
;
292 return make_cleanup_dtor (do_regcache_invalidate
, (void *) reg
, xfree
);
295 /* Return REGCACHE's architecture. */
298 get_regcache_arch (const struct regcache
*regcache
)
300 return regcache
->descr
->gdbarch
;
303 struct address_space
*
304 get_regcache_aspace (const struct regcache
*regcache
)
306 return regcache
->aspace
;
309 /* Return a pointer to register REGNUM's buffer cache. */
312 register_buffer (const struct regcache
*regcache
, int regnum
)
314 return regcache
->registers
+ regcache
->descr
->register_offset
[regnum
];
318 regcache_save (struct regcache
*dst
, regcache_cooked_read_ftype
*cooked_read
,
321 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
322 gdb_byte buf
[MAX_REGISTER_SIZE
];
325 /* The DST should be `read-only', if it wasn't then the save would
326 end up trying to write the register values back out to the
328 gdb_assert (dst
->readonly_p
);
329 /* Clear the dest. */
330 memset (dst
->registers
, 0, dst
->descr
->sizeof_cooked_registers
);
331 memset (dst
->register_status
, 0,
332 dst
->descr
->sizeof_cooked_register_status
);
333 /* Copy over any registers (identified by their membership in the
334 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
335 gdbarch_num_pseudo_regs) range is checked since some architectures need
336 to save/restore `cooked' registers that live in memory. */
337 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
339 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
341 enum register_status status
= cooked_read (src
, regnum
, buf
);
343 if (status
== REG_VALID
)
344 memcpy (register_buffer (dst
, regnum
), buf
,
345 register_size (gdbarch
, regnum
));
348 gdb_assert (status
!= REG_UNKNOWN
);
350 memset (register_buffer (dst
, regnum
), 0,
351 register_size (gdbarch
, regnum
));
353 dst
->register_status
[regnum
] = status
;
359 regcache_restore (struct regcache
*dst
,
360 regcache_cooked_read_ftype
*cooked_read
,
361 void *cooked_read_context
)
363 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
364 gdb_byte buf
[MAX_REGISTER_SIZE
];
367 /* The dst had better not be read-only. If it is, the `restore'
368 doesn't make much sense. */
369 gdb_assert (!dst
->readonly_p
);
370 /* Copy over any registers, being careful to only restore those that
371 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
372 + gdbarch_num_pseudo_regs) range is checked since some architectures need
373 to save/restore `cooked' registers that live in memory. */
374 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
376 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
378 enum register_status status
;
380 status
= cooked_read (cooked_read_context
, regnum
, buf
);
381 if (status
== REG_VALID
)
382 regcache_cooked_write (dst
, regnum
, buf
);
387 static enum register_status
388 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
390 struct regcache
*regcache
= src
;
392 return regcache_cooked_read (regcache
, regnum
, buf
);
396 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
398 gdb_assert (src
!= NULL
&& dst
!= NULL
);
399 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
400 gdb_assert (src
!= dst
);
401 gdb_assert (src
->readonly_p
|| dst
->readonly_p
);
403 if (!src
->readonly_p
)
404 regcache_save (dst
, do_cooked_read
, src
);
405 else if (!dst
->readonly_p
)
406 regcache_restore (dst
, do_cooked_read
, src
);
408 regcache_cpy_no_passthrough (dst
, src
);
412 regcache_cpy_no_passthrough (struct regcache
*dst
, struct regcache
*src
)
414 gdb_assert (src
!= NULL
&& dst
!= NULL
);
415 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
416 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
417 move of data into a thread's regcache. Doing this would be silly
418 - it would mean that regcache->register_status would be
419 completely invalid. */
420 gdb_assert (dst
->readonly_p
&& src
->readonly_p
);
422 memcpy (dst
->registers
, src
->registers
,
423 dst
->descr
->sizeof_cooked_registers
);
424 memcpy (dst
->register_status
, src
->register_status
,
425 dst
->descr
->sizeof_cooked_register_status
);
429 regcache_dup (struct regcache
*src
)
431 struct regcache
*newbuf
;
433 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
, get_regcache_aspace (src
));
434 regcache_cpy (newbuf
, src
);
439 regcache_register_status (const struct regcache
*regcache
, int regnum
)
441 gdb_assert (regcache
!= NULL
);
442 gdb_assert (regnum
>= 0);
443 if (regcache
->readonly_p
)
444 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
446 gdb_assert (regnum
< regcache
->descr
->nr_raw_registers
);
448 return regcache
->register_status
[regnum
];
452 regcache_invalidate (struct regcache
*regcache
, int regnum
)
454 gdb_assert (regcache
!= NULL
);
455 gdb_assert (regnum
>= 0);
456 gdb_assert (!regcache
->readonly_p
);
457 gdb_assert (regnum
< regcache
->descr
->nr_raw_registers
);
458 regcache
->register_status
[regnum
] = REG_UNKNOWN
;
462 /* Global structure containing the current regcache. */
464 /* NOTE: this is a write-through cache. There is no "dirty" bit for
465 recording if the register values have been changed (eg. by the
466 user). Therefore all registers must be written back to the
467 target when appropriate. */
471 struct regcache
*regcache
;
472 struct regcache_list
*next
;
475 static struct regcache_list
*current_regcache
;
478 get_thread_arch_aspace_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
,
479 struct address_space
*aspace
)
481 struct regcache_list
*list
;
482 struct regcache
*new_regcache
;
484 for (list
= current_regcache
; list
; list
= list
->next
)
485 if (ptid_equal (list
->regcache
->ptid
, ptid
)
486 && get_regcache_arch (list
->regcache
) == gdbarch
)
487 return list
->regcache
;
489 new_regcache
= regcache_xmalloc_1 (gdbarch
, aspace
, 0);
490 new_regcache
->ptid
= ptid
;
492 list
= xmalloc (sizeof (struct regcache_list
));
493 list
->regcache
= new_regcache
;
494 list
->next
= current_regcache
;
495 current_regcache
= list
;
501 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
503 struct address_space
*aspace
;
505 /* For the benefit of "maint print registers" & co when debugging an
506 executable, allow dumping the regcache even when there is no
507 thread selected (target_thread_address_space internal-errors if
508 no address space is found). Note that normal user commands will
509 fail higher up on the call stack due to no
510 target_has_registers. */
511 aspace
= (ptid_equal (null_ptid
, ptid
)
513 : target_thread_address_space (ptid
));
515 return get_thread_arch_aspace_regcache (ptid
, gdbarch
, aspace
);
518 static ptid_t current_thread_ptid
;
519 static struct gdbarch
*current_thread_arch
;
522 get_thread_regcache (ptid_t ptid
)
524 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
526 current_thread_ptid
= ptid
;
527 current_thread_arch
= target_thread_architecture (ptid
);
530 return get_thread_arch_regcache (ptid
, current_thread_arch
);
534 get_current_regcache (void)
536 return get_thread_regcache (inferior_ptid
);
540 /* Observer for the target_changed event. */
543 regcache_observer_target_changed (struct target_ops
*target
)
545 registers_changed ();
548 /* Update global variables old ptids to hold NEW_PTID if they were
551 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
553 struct regcache_list
*list
;
555 for (list
= current_regcache
; list
; list
= list
->next
)
556 if (ptid_equal (list
->regcache
->ptid
, old_ptid
))
557 list
->regcache
->ptid
= new_ptid
;
560 /* Low level examining and depositing of registers.
562 The caller is responsible for making sure that the inferior is
563 stopped before calling the fetching routines, or it will get
564 garbage. (a change from GDB version 3, in which the caller got the
565 value from the last stop). */
567 /* REGISTERS_CHANGED ()
569 Indicate that registers may have changed, so invalidate the cache. */
572 registers_changed_ptid (ptid_t ptid
)
574 struct regcache_list
*list
, **list_link
;
576 list
= current_regcache
;
577 list_link
= ¤t_regcache
;
580 if (ptid_match (list
->regcache
->ptid
, ptid
))
582 struct regcache_list
*dead
= list
;
584 *list_link
= list
->next
;
585 regcache_xfree (list
->regcache
);
591 list_link
= &list
->next
;
595 if (ptid_match (current_thread_ptid
, ptid
))
597 current_thread_ptid
= null_ptid
;
598 current_thread_arch
= NULL
;
601 if (ptid_match (inferior_ptid
, ptid
))
603 /* We just deleted the regcache of the current thread. Need to
604 forget about any frames we have cached, too. */
605 reinit_frame_cache ();
610 registers_changed (void)
612 registers_changed_ptid (minus_one_ptid
);
614 /* Force cleanup of any alloca areas if using C alloca instead of
615 a builtin alloca. This particular call is used to clean up
616 areas allocated by low level target code which may build up
617 during lengthy interactions between gdb and the target before
618 gdb gives control to the user (ie watchpoints). */
623 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
625 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
626 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
627 /* Make certain that the register cache is up-to-date with respect
628 to the current thread. This switching shouldn't be necessary
629 only there is still only one target side register cache. Sigh!
630 On the bright side, at least there is a regcache object. */
631 if (!regcache
->readonly_p
632 && regcache_register_status (regcache
, regnum
) == REG_UNKNOWN
)
634 struct cleanup
*old_chain
= save_inferior_ptid ();
636 inferior_ptid
= regcache
->ptid
;
637 target_fetch_registers (regcache
, regnum
);
638 do_cleanups (old_chain
);
640 /* A number of targets can't access the whole set of raw
641 registers (because the debug API provides no means to get at
643 if (regcache
->register_status
[regnum
] == REG_UNKNOWN
)
644 regcache
->register_status
[regnum
] = REG_UNAVAILABLE
;
647 if (regcache
->register_status
[regnum
] != REG_VALID
)
648 memset (buf
, 0, regcache
->descr
->sizeof_register
[regnum
]);
650 memcpy (buf
, register_buffer (regcache
, regnum
),
651 regcache
->descr
->sizeof_register
[regnum
]);
653 return regcache
->register_status
[regnum
];
657 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
660 enum register_status status
;
662 gdb_assert (regcache
!= NULL
);
663 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
664 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
665 status
= regcache_raw_read (regcache
, regnum
, buf
);
666 if (status
== REG_VALID
)
667 *val
= extract_signed_integer
668 (buf
, regcache
->descr
->sizeof_register
[regnum
],
669 gdbarch_byte_order (regcache
->descr
->gdbarch
));
676 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
680 enum register_status status
;
682 gdb_assert (regcache
!= NULL
);
683 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
684 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
685 status
= regcache_raw_read (regcache
, regnum
, buf
);
686 if (status
== REG_VALID
)
687 *val
= extract_unsigned_integer
688 (buf
, regcache
->descr
->sizeof_register
[regnum
],
689 gdbarch_byte_order (regcache
->descr
->gdbarch
));
696 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
700 gdb_assert (regcache
!= NULL
);
701 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
702 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
703 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
704 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
705 regcache_raw_write (regcache
, regnum
, buf
);
709 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
714 gdb_assert (regcache
!= NULL
);
715 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
716 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
717 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
718 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
719 regcache_raw_write (regcache
, regnum
, buf
);
723 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
725 gdb_assert (regnum
>= 0);
726 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
727 if (regnum
< regcache
->descr
->nr_raw_registers
)
728 return regcache_raw_read (regcache
, regnum
, buf
);
729 else if (regcache
->readonly_p
730 && regcache
->register_status
[regnum
] != REG_UNKNOWN
)
732 /* Read-only register cache, perhaps the cooked value was
734 if (regcache
->register_status
[regnum
] == REG_VALID
)
735 memcpy (buf
, register_buffer (regcache
, regnum
),
736 regcache
->descr
->sizeof_register
[regnum
]);
738 memset (buf
, 0, regcache
->descr
->sizeof_register
[regnum
]);
740 return regcache
->register_status
[regnum
];
742 else if (gdbarch_pseudo_register_read_value_p (regcache
->descr
->gdbarch
))
744 struct value
*mark
, *computed
;
745 enum register_status result
= REG_VALID
;
747 mark
= value_mark ();
749 computed
= gdbarch_pseudo_register_read_value (regcache
->descr
->gdbarch
,
751 if (value_entirely_available (computed
))
752 memcpy (buf
, value_contents_raw (computed
),
753 regcache
->descr
->sizeof_register
[regnum
]);
756 memset (buf
, 0, regcache
->descr
->sizeof_register
[regnum
]);
757 result
= REG_UNAVAILABLE
;
760 value_free_to_mark (mark
);
765 return gdbarch_pseudo_register_read (regcache
->descr
->gdbarch
, regcache
,
770 regcache_cooked_read_value (struct regcache
*regcache
, int regnum
)
772 gdb_assert (regnum
>= 0);
773 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
775 if (regnum
< regcache
->descr
->nr_raw_registers
776 || (regcache
->readonly_p
777 && regcache
->register_status
[regnum
] != REG_UNKNOWN
)
778 || !gdbarch_pseudo_register_read_value_p (regcache
->descr
->gdbarch
))
780 struct value
*result
;
782 result
= allocate_value (register_type (regcache
->descr
->gdbarch
,
784 VALUE_LVAL (result
) = lval_register
;
785 VALUE_REGNUM (result
) = regnum
;
787 /* It is more efficient in general to do this delegation in this
788 direction than in the other one, even though the value-based
790 if (regcache_cooked_read (regcache
, regnum
,
791 value_contents_raw (result
)) == REG_UNAVAILABLE
)
792 mark_value_bytes_unavailable (result
, 0,
793 TYPE_LENGTH (value_type (result
)));
798 return gdbarch_pseudo_register_read_value (regcache
->descr
->gdbarch
,
803 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
806 enum register_status status
;
809 gdb_assert (regcache
!= NULL
);
810 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
811 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
812 status
= regcache_cooked_read (regcache
, regnum
, buf
);
813 if (status
== REG_VALID
)
814 *val
= extract_signed_integer
815 (buf
, regcache
->descr
->sizeof_register
[regnum
],
816 gdbarch_byte_order (regcache
->descr
->gdbarch
));
823 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
826 enum register_status status
;
829 gdb_assert (regcache
!= NULL
);
830 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
831 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
832 status
= regcache_cooked_read (regcache
, regnum
, buf
);
833 if (status
== REG_VALID
)
834 *val
= extract_unsigned_integer
835 (buf
, regcache
->descr
->sizeof_register
[regnum
],
836 gdbarch_byte_order (regcache
->descr
->gdbarch
));
843 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
848 gdb_assert (regcache
!= NULL
);
849 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
850 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
851 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
852 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
853 regcache_cooked_write (regcache
, regnum
, buf
);
857 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
862 gdb_assert (regcache
!= NULL
);
863 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
864 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
865 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
866 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
867 regcache_cooked_write (regcache
, regnum
, buf
);
871 regcache_raw_write (struct regcache
*regcache
, int regnum
,
874 struct cleanup
*chain_before_save_inferior
;
875 struct cleanup
*chain_before_invalidate_register
;
877 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
878 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
879 gdb_assert (!regcache
->readonly_p
);
881 /* On the sparc, writing %g0 is a no-op, so we don't even want to
882 change the registers array if something writes to this register. */
883 if (gdbarch_cannot_store_register (get_regcache_arch (regcache
), regnum
))
886 /* If we have a valid copy of the register, and new value == old
887 value, then don't bother doing the actual store. */
888 if (regcache_register_status (regcache
, regnum
) == REG_VALID
889 && (memcmp (register_buffer (regcache
, regnum
), buf
,
890 regcache
->descr
->sizeof_register
[regnum
]) == 0))
893 chain_before_save_inferior
= save_inferior_ptid ();
894 inferior_ptid
= regcache
->ptid
;
896 target_prepare_to_store (regcache
);
897 memcpy (register_buffer (regcache
, regnum
), buf
,
898 regcache
->descr
->sizeof_register
[regnum
]);
899 regcache
->register_status
[regnum
] = REG_VALID
;
901 /* Register a cleanup function for invalidating the register after it is
902 written, in case of a failure. */
903 chain_before_invalidate_register
904 = make_cleanup_regcache_invalidate (regcache
, regnum
);
906 target_store_registers (regcache
, regnum
);
908 /* The target did not throw an error so we can discard invalidating the
909 register and restore the cleanup chain to what it was. */
910 discard_cleanups (chain_before_invalidate_register
);
912 do_cleanups (chain_before_save_inferior
);
916 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
919 gdb_assert (regnum
>= 0);
920 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
921 if (regnum
< regcache
->descr
->nr_raw_registers
)
922 regcache_raw_write (regcache
, regnum
, buf
);
924 gdbarch_pseudo_register_write (regcache
->descr
->gdbarch
, regcache
,
928 /* Perform a partial register transfer using a read, modify, write
931 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
933 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
936 static enum register_status
937 regcache_xfer_part (struct regcache
*regcache
, int regnum
,
938 int offset
, int len
, void *in
, const void *out
,
939 enum register_status (*read
) (struct regcache
*regcache
,
942 void (*write
) (struct regcache
*regcache
, int regnum
,
943 const gdb_byte
*buf
))
945 struct regcache_descr
*descr
= regcache
->descr
;
946 gdb_byte reg
[MAX_REGISTER_SIZE
];
948 gdb_assert (offset
>= 0 && offset
<= descr
->sizeof_register
[regnum
]);
949 gdb_assert (len
>= 0 && offset
+ len
<= descr
->sizeof_register
[regnum
]);
950 /* Something to do? */
951 if (offset
+ len
== 0)
953 /* Read (when needed) ... */
956 || offset
+ len
< descr
->sizeof_register
[regnum
])
958 enum register_status status
;
960 gdb_assert (read
!= NULL
);
961 status
= read (regcache
, regnum
, reg
);
962 if (status
!= REG_VALID
)
967 memcpy (in
, reg
+ offset
, len
);
969 memcpy (reg
+ offset
, out
, len
);
970 /* ... write (when needed). */
973 gdb_assert (write
!= NULL
);
974 write (regcache
, regnum
, reg
);
981 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
982 int offset
, int len
, gdb_byte
*buf
)
984 struct regcache_descr
*descr
= regcache
->descr
;
986 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
987 return regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
988 regcache_raw_read
, regcache_raw_write
);
992 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
993 int offset
, int len
, const gdb_byte
*buf
)
995 struct regcache_descr
*descr
= regcache
->descr
;
997 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
998 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
999 regcache_raw_read
, regcache_raw_write
);
1002 enum register_status
1003 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
1004 int offset
, int len
, gdb_byte
*buf
)
1006 struct regcache_descr
*descr
= regcache
->descr
;
1008 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
1009 return regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
1010 regcache_cooked_read
, regcache_cooked_write
);
1014 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
1015 int offset
, int len
, const gdb_byte
*buf
)
1017 struct regcache_descr
*descr
= regcache
->descr
;
1019 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
1020 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
1021 regcache_cooked_read
, regcache_cooked_write
);
1024 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1027 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
1032 gdb_assert (regcache
!= NULL
);
1033 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
1034 gdb_assert (!regcache
->readonly_p
);
1036 regbuf
= register_buffer (regcache
, regnum
);
1037 size
= regcache
->descr
->sizeof_register
[regnum
];
1041 memcpy (regbuf
, buf
, size
);
1042 regcache
->register_status
[regnum
] = REG_VALID
;
1046 /* This memset not strictly necessary, but better than garbage
1047 in case the register value manages to escape somewhere (due
1048 to a bug, no less). */
1049 memset (regbuf
, 0, size
);
1050 regcache
->register_status
[regnum
] = REG_UNAVAILABLE
;
1054 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1057 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
1062 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
1063 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
1065 regbuf
= register_buffer (regcache
, regnum
);
1066 size
= regcache
->descr
->sizeof_register
[regnum
];
1067 memcpy (buf
, regbuf
, size
);
1071 /* Special handling for register PC. */
1074 regcache_read_pc (struct regcache
*regcache
)
1076 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1080 if (gdbarch_read_pc_p (gdbarch
))
1081 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
1082 /* Else use per-frame method on get_current_frame. */
1083 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1087 if (regcache_cooked_read_unsigned (regcache
,
1088 gdbarch_pc_regnum (gdbarch
),
1089 &raw_val
) == REG_UNAVAILABLE
)
1090 throw_error (NOT_AVAILABLE_ERROR
, _("PC register is not available"));
1092 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
1095 internal_error (__FILE__
, __LINE__
,
1096 _("regcache_read_pc: Unable to find PC"));
1101 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
1103 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
1105 if (gdbarch_write_pc_p (gdbarch
))
1106 gdbarch_write_pc (gdbarch
, regcache
, pc
);
1107 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
1108 regcache_cooked_write_unsigned (regcache
,
1109 gdbarch_pc_regnum (gdbarch
), pc
);
1111 internal_error (__FILE__
, __LINE__
,
1112 _("regcache_write_pc: Unable to update PC"));
1114 /* Writing the PC (for instance, from "load") invalidates the
1116 reinit_frame_cache ();
1121 reg_flush_command (char *command
, int from_tty
)
1123 /* Force-flush the register cache. */
1124 registers_changed ();
1126 printf_filtered (_("Register cache flushed.\n"));
1129 enum regcache_dump_what
1131 regcache_dump_none
, regcache_dump_raw
,
1132 regcache_dump_cooked
, regcache_dump_groups
,
1133 regcache_dump_remote
1137 regcache_dump (struct regcache
*regcache
, struct ui_file
*file
,
1138 enum regcache_dump_what what_to_dump
)
1140 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
1141 struct gdbarch
*gdbarch
= regcache
->descr
->gdbarch
;
1143 int footnote_nr
= 0;
1144 int footnote_register_size
= 0;
1145 int footnote_register_offset
= 0;
1146 int footnote_register_type_name_null
= 0;
1147 long register_offset
= 0;
1148 gdb_byte buf
[MAX_REGISTER_SIZE
];
1151 fprintf_unfiltered (file
, "nr_raw_registers %d\n",
1152 regcache
->descr
->nr_raw_registers
);
1153 fprintf_unfiltered (file
, "nr_cooked_registers %d\n",
1154 regcache
->descr
->nr_cooked_registers
);
1155 fprintf_unfiltered (file
, "sizeof_raw_registers %ld\n",
1156 regcache
->descr
->sizeof_raw_registers
);
1157 fprintf_unfiltered (file
, "sizeof_raw_register_status %ld\n",
1158 regcache
->descr
->sizeof_raw_register_status
);
1159 fprintf_unfiltered (file
, "gdbarch_num_regs %d\n",
1160 gdbarch_num_regs (gdbarch
));
1161 fprintf_unfiltered (file
, "gdbarch_num_pseudo_regs %d\n",
1162 gdbarch_num_pseudo_regs (gdbarch
));
1165 gdb_assert (regcache
->descr
->nr_cooked_registers
1166 == (gdbarch_num_regs (gdbarch
)
1167 + gdbarch_num_pseudo_regs (gdbarch
)));
1169 for (regnum
= -1; regnum
< regcache
->descr
->nr_cooked_registers
; regnum
++)
1173 fprintf_unfiltered (file
, " %-10s", "Name");
1176 const char *p
= gdbarch_register_name (gdbarch
, regnum
);
1180 else if (p
[0] == '\0')
1182 fprintf_unfiltered (file
, " %-10s", p
);
1187 fprintf_unfiltered (file
, " %4s", "Nr");
1189 fprintf_unfiltered (file
, " %4d", regnum
);
1191 /* Relative number. */
1193 fprintf_unfiltered (file
, " %4s", "Rel");
1194 else if (regnum
< gdbarch_num_regs (gdbarch
))
1195 fprintf_unfiltered (file
, " %4d", regnum
);
1197 fprintf_unfiltered (file
, " %4d",
1198 (regnum
- gdbarch_num_regs (gdbarch
)));
1202 fprintf_unfiltered (file
, " %6s ", "Offset");
1205 fprintf_unfiltered (file
, " %6ld",
1206 regcache
->descr
->register_offset
[regnum
]);
1207 if (register_offset
!= regcache
->descr
->register_offset
[regnum
]
1209 && (regcache
->descr
->register_offset
[regnum
]
1210 != (regcache
->descr
->register_offset
[regnum
- 1]
1211 + regcache
->descr
->sizeof_register
[regnum
- 1])))
1214 if (!footnote_register_offset
)
1215 footnote_register_offset
= ++footnote_nr
;
1216 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1219 fprintf_unfiltered (file
, " ");
1220 register_offset
= (regcache
->descr
->register_offset
[regnum
]
1221 + regcache
->descr
->sizeof_register
[regnum
]);
1226 fprintf_unfiltered (file
, " %5s ", "Size");
1228 fprintf_unfiltered (file
, " %5ld",
1229 regcache
->descr
->sizeof_register
[regnum
]);
1239 static const char blt
[] = "builtin_type";
1241 t
= TYPE_NAME (register_type (regcache
->descr
->gdbarch
, regnum
));
1246 if (!footnote_register_type_name_null
)
1247 footnote_register_type_name_null
= ++footnote_nr
;
1248 n
= xstrprintf ("*%d", footnote_register_type_name_null
);
1249 make_cleanup (xfree
, n
);
1252 /* Chop a leading builtin_type. */
1253 if (strncmp (t
, blt
, strlen (blt
)) == 0)
1256 fprintf_unfiltered (file
, " %-15s", t
);
1259 /* Leading space always present. */
1260 fprintf_unfiltered (file
, " ");
1263 if (what_to_dump
== regcache_dump_raw
)
1266 fprintf_unfiltered (file
, "Raw value");
1267 else if (regnum
>= regcache
->descr
->nr_raw_registers
)
1268 fprintf_unfiltered (file
, "<cooked>");
1269 else if (regcache_register_status (regcache
, regnum
) == REG_UNKNOWN
)
1270 fprintf_unfiltered (file
, "<invalid>");
1271 else if (regcache_register_status (regcache
, regnum
) == REG_UNAVAILABLE
)
1272 fprintf_unfiltered (file
, "<unavailable>");
1275 regcache_raw_read (regcache
, regnum
, buf
);
1276 print_hex_chars (file
, buf
,
1277 regcache
->descr
->sizeof_register
[regnum
],
1278 gdbarch_byte_order (gdbarch
));
1282 /* Value, cooked. */
1283 if (what_to_dump
== regcache_dump_cooked
)
1286 fprintf_unfiltered (file
, "Cooked value");
1289 enum register_status status
;
1291 status
= regcache_cooked_read (regcache
, regnum
, buf
);
1292 if (status
== REG_UNKNOWN
)
1293 fprintf_unfiltered (file
, "<invalid>");
1294 else if (status
== REG_UNAVAILABLE
)
1295 fprintf_unfiltered (file
, "<unavailable>");
1297 print_hex_chars (file
, buf
,
1298 regcache
->descr
->sizeof_register
[regnum
],
1299 gdbarch_byte_order (gdbarch
));
1303 /* Group members. */
1304 if (what_to_dump
== regcache_dump_groups
)
1307 fprintf_unfiltered (file
, "Groups");
1310 const char *sep
= "";
1311 struct reggroup
*group
;
1313 for (group
= reggroup_next (gdbarch
, NULL
);
1315 group
= reggroup_next (gdbarch
, group
))
1317 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
1319 fprintf_unfiltered (file
,
1320 "%s%s", sep
, reggroup_name (group
));
1327 /* Remote packet configuration. */
1328 if (what_to_dump
== regcache_dump_remote
)
1332 fprintf_unfiltered (file
, "Rmt Nr g/G Offset");
1334 else if (regnum
< regcache
->descr
->nr_raw_registers
)
1338 if (remote_register_number_and_offset (get_regcache_arch (regcache
), regnum
,
1340 fprintf_unfiltered (file
, "%7d %11d", pnum
, poffset
);
1344 fprintf_unfiltered (file
, "\n");
1347 if (footnote_register_size
)
1348 fprintf_unfiltered (file
, "*%d: Inconsistent register sizes.\n",
1349 footnote_register_size
);
1350 if (footnote_register_offset
)
1351 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1352 footnote_register_offset
);
1353 if (footnote_register_type_name_null
)
1354 fprintf_unfiltered (file
,
1355 "*%d: Register type's name NULL.\n",
1356 footnote_register_type_name_null
);
1357 do_cleanups (cleanups
);
1361 regcache_print (char *args
, enum regcache_dump_what what_to_dump
)
1364 regcache_dump (get_current_regcache (), gdb_stdout
, what_to_dump
);
1367 struct cleanup
*cleanups
;
1368 struct ui_file
*file
= gdb_fopen (args
, "w");
1371 perror_with_name (_("maintenance print architecture"));
1372 cleanups
= make_cleanup_ui_file_delete (file
);
1373 regcache_dump (get_current_regcache (), file
, what_to_dump
);
1374 do_cleanups (cleanups
);
1379 maintenance_print_registers (char *args
, int from_tty
)
1381 regcache_print (args
, regcache_dump_none
);
1385 maintenance_print_raw_registers (char *args
, int from_tty
)
1387 regcache_print (args
, regcache_dump_raw
);
1391 maintenance_print_cooked_registers (char *args
, int from_tty
)
1393 regcache_print (args
, regcache_dump_cooked
);
1397 maintenance_print_register_groups (char *args
, int from_tty
)
1399 regcache_print (args
, regcache_dump_groups
);
1403 maintenance_print_remote_registers (char *args
, int from_tty
)
1405 regcache_print (args
, regcache_dump_remote
);
1408 extern initialize_file_ftype _initialize_regcache
; /* -Wmissing-prototype */
1411 _initialize_regcache (void)
1413 regcache_descr_handle
1414 = gdbarch_data_register_post_init (init_regcache_descr
);
1416 observer_attach_target_changed (regcache_observer_target_changed
);
1417 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed
);
1419 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1420 _("Force gdb to flush its register cache (maintainer command)"));
1422 add_cmd ("registers", class_maintenance
, maintenance_print_registers
,
1423 _("Print the internal register configuration.\n"
1424 "Takes an optional file parameter."), &maintenanceprintlist
);
1425 add_cmd ("raw-registers", class_maintenance
,
1426 maintenance_print_raw_registers
,
1427 _("Print the internal register configuration "
1428 "including raw values.\n"
1429 "Takes an optional file parameter."), &maintenanceprintlist
);
1430 add_cmd ("cooked-registers", class_maintenance
,
1431 maintenance_print_cooked_registers
,
1432 _("Print the internal register configuration "
1433 "including cooked values.\n"
1434 "Takes an optional file parameter."), &maintenanceprintlist
);
1435 add_cmd ("register-groups", class_maintenance
,
1436 maintenance_print_register_groups
,
1437 _("Print the internal register configuration "
1438 "including each register's group.\n"
1439 "Takes an optional file parameter."),
1440 &maintenanceprintlist
);
1441 add_cmd ("remote-registers", class_maintenance
,
1442 maintenance_print_remote_registers
, _("\
1443 Print the internal register configuration including each register's\n\
1444 remote register number and buffer offset in the g/G packets.\n\
1445 Takes an optional file parameter."),
1446 &maintenanceprintlist
);