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 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
, struct type
*);
104 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
105 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
107 /* Construct a strictly RAW register cache. Don't allow pseudo's
108 into the register cache. */
109 descr
->nr_raw_registers
= gdbarch_num_regs (gdbarch
);
111 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
112 array. This pretects GDB from erant code that accesses elements
113 of the global register_valid_p[] array in the range
114 [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
115 descr
->sizeof_raw_register_valid_p
= descr
->sizeof_cooked_register_valid_p
;
117 /* Lay out the register cache.
119 NOTE: cagney/2002-05-22: Only register_type() is used when
120 constructing the register cache. It is assumed that the
121 register's raw size, virtual size and type length are all the
126 descr
->sizeof_register
127 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
128 descr
->register_offset
129 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
130 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
132 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
133 descr
->register_offset
[i
] = offset
;
134 offset
+= descr
->sizeof_register
[i
];
135 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
137 /* Set the real size of the register cache buffer. */
138 descr
->sizeof_cooked_registers
= offset
;
141 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
142 the raw registers. Unfortunately some code still accesses the
143 register array directly using the global registers[]. Until that
144 code has been purged, play safe and over allocating the register
146 descr
->sizeof_raw_registers
= descr
->sizeof_cooked_registers
;
151 static struct regcache_descr
*
152 regcache_descr (struct gdbarch
*gdbarch
)
154 return gdbarch_data (gdbarch
, regcache_descr_handle
);
157 /* Utility functions returning useful register attributes stored in
158 the regcache descr. */
161 register_type (struct gdbarch
*gdbarch
, int regnum
)
163 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
164 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
165 return descr
->register_type
[regnum
];
168 /* Utility functions returning useful register attributes stored in
169 the regcache descr. */
172 register_size (struct gdbarch
*gdbarch
, int regnum
)
174 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
176 gdb_assert (regnum
>= 0
177 && regnum
< (gdbarch_num_regs (gdbarch
)
178 + gdbarch_num_pseudo_regs (gdbarch
)));
179 size
= descr
->sizeof_register
[regnum
];
183 /* The register cache for storing raw register values. */
187 struct regcache_descr
*descr
;
189 /* The address space of this register cache (for registers where it
190 makes sense, like PC or SP). */
191 struct address_space
*aspace
;
193 /* The register buffers. A read-only register cache can hold the
194 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
195 register cache can only hold [0 .. gdbarch_num_regs). */
197 /* Register cache status:
198 register_valid_p[REG] == 0 if REG value is not in the cache
199 > 0 if REG value is in the cache
200 < 0 if REG value is permanently unavailable */
201 signed char *register_valid_p
;
202 /* Is this a read-only cache? A read-only cache is used for saving
203 the target's register state (e.g, across an inferior function
204 call or just before forcing a function return). A read-only
205 cache can only be updated via the methods regcache_dup() and
206 regcache_cpy(). The actual contents are determined by the
207 reggroup_save and reggroup_restore methods. */
209 /* If this is a read-write cache, which thread's registers is
215 regcache_xmalloc (struct gdbarch
*gdbarch
)
217 struct regcache_descr
*descr
;
218 struct regcache
*regcache
;
219 gdb_assert (gdbarch
!= NULL
);
220 descr
= regcache_descr (gdbarch
);
221 regcache
= XMALLOC (struct regcache
);
222 regcache
->descr
= descr
;
224 = XCALLOC (descr
->sizeof_raw_registers
, gdb_byte
);
225 regcache
->register_valid_p
226 = XCALLOC (descr
->sizeof_raw_register_valid_p
, gdb_byte
);
227 regcache
->aspace
= NULL
;
228 regcache
->readonly_p
= 1;
229 regcache
->ptid
= minus_one_ptid
;
234 regcache_xfree (struct regcache
*regcache
)
236 if (regcache
== NULL
)
238 xfree (regcache
->registers
);
239 xfree (regcache
->register_valid_p
);
244 do_regcache_xfree (void *data
)
246 regcache_xfree (data
);
250 make_cleanup_regcache_xfree (struct regcache
*regcache
)
252 return make_cleanup (do_regcache_xfree
, regcache
);
255 /* Return REGCACHE's architecture. */
258 get_regcache_arch (const struct regcache
*regcache
)
260 return regcache
->descr
->gdbarch
;
263 struct address_space
*
264 get_regcache_aspace (const struct regcache
*regcache
)
266 return regcache
->aspace
;
269 /* Return a pointer to register REGNUM's buffer cache. */
272 register_buffer (const struct regcache
*regcache
, int regnum
)
274 return regcache
->registers
+ regcache
->descr
->register_offset
[regnum
];
278 regcache_save (struct regcache
*dst
, regcache_cooked_read_ftype
*cooked_read
,
281 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
282 gdb_byte buf
[MAX_REGISTER_SIZE
];
284 /* The DST should be `read-only', if it wasn't then the save would
285 end up trying to write the register values back out to the
287 gdb_assert (dst
->readonly_p
);
288 /* Clear the dest. */
289 memset (dst
->registers
, 0, dst
->descr
->sizeof_cooked_registers
);
290 memset (dst
->register_valid_p
, 0, dst
->descr
->sizeof_cooked_register_valid_p
);
291 /* Copy over any registers (identified by their membership in the
292 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
293 gdbarch_num_pseudo_regs) range is checked since some architectures need
294 to save/restore `cooked' registers that live in memory. */
295 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
297 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
299 int valid
= cooked_read (src
, regnum
, buf
);
302 memcpy (register_buffer (dst
, regnum
), buf
,
303 register_size (gdbarch
, regnum
));
304 dst
->register_valid_p
[regnum
] = 1;
311 regcache_restore (struct regcache
*dst
,
312 regcache_cooked_read_ftype
*cooked_read
,
313 void *cooked_read_context
)
315 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
316 gdb_byte buf
[MAX_REGISTER_SIZE
];
318 /* The dst had better not be read-only. If it is, the `restore'
319 doesn't make much sense. */
320 gdb_assert (!dst
->readonly_p
);
321 /* Copy over any registers, being careful to only restore those that
322 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
323 + gdbarch_num_pseudo_regs) range is checked since some architectures need
324 to save/restore `cooked' registers that live in memory. */
325 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
327 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
329 int valid
= cooked_read (cooked_read_context
, regnum
, buf
);
331 regcache_cooked_write (dst
, regnum
, buf
);
337 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
339 struct regcache
*regcache
= src
;
340 if (!regcache
->register_valid_p
[regnum
] && regcache
->readonly_p
)
341 /* Don't even think about fetching a register from a read-only
342 cache when the register isn't yet valid. There isn't a target
343 from which the register value can be fetched. */
345 regcache_cooked_read (regcache
, regnum
, buf
);
351 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
356 gdb_assert (src
!= NULL
&& dst
!= NULL
);
357 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
358 gdb_assert (src
!= dst
);
359 gdb_assert (src
->readonly_p
|| dst
->readonly_p
);
361 dst
->aspace
= src
->aspace
;
363 if (!src
->readonly_p
)
364 regcache_save (dst
, do_cooked_read
, src
);
365 else if (!dst
->readonly_p
)
366 regcache_restore (dst
, do_cooked_read
, src
);
368 regcache_cpy_no_passthrough (dst
, src
);
372 regcache_cpy_no_passthrough (struct regcache
*dst
, struct regcache
*src
)
375 gdb_assert (src
!= NULL
&& dst
!= NULL
);
376 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
377 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
378 move of data into the current regcache. Doing this would be
379 silly - it would mean that valid_p would be completely invalid. */
380 gdb_assert (dst
->readonly_p
);
382 dst
->aspace
= src
->aspace
;
383 memcpy (dst
->registers
, src
->registers
, dst
->descr
->sizeof_raw_registers
);
384 memcpy (dst
->register_valid_p
, src
->register_valid_p
,
385 dst
->descr
->sizeof_raw_register_valid_p
);
389 regcache_dup (struct regcache
*src
)
391 struct regcache
*newbuf
;
392 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
);
393 regcache_cpy (newbuf
, src
);
398 regcache_dup_no_passthrough (struct regcache
*src
)
400 struct regcache
*newbuf
;
401 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
);
402 regcache_cpy_no_passthrough (newbuf
, src
);
407 regcache_valid_p (const struct regcache
*regcache
, int regnum
)
409 gdb_assert (regcache
!= NULL
);
410 gdb_assert (regnum
>= 0);
411 if (regcache
->readonly_p
)
412 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
414 gdb_assert (regnum
< regcache
->descr
->nr_raw_registers
);
416 return regcache
->register_valid_p
[regnum
];
420 regcache_invalidate (struct regcache
*regcache
, int regnum
)
422 gdb_assert (regcache
!= NULL
);
423 gdb_assert (regnum
>= 0);
424 gdb_assert (!regcache
->readonly_p
);
425 gdb_assert (regnum
< regcache
->descr
->nr_raw_registers
);
426 regcache
->register_valid_p
[regnum
] = 0;
430 /* Global structure containing the current regcache. */
432 /* NOTE: this is a write-through cache. There is no "dirty" bit for
433 recording if the register values have been changed (eg. by the
434 user). Therefore all registers must be written back to the
435 target when appropriate. */
439 struct regcache
*regcache
;
440 struct regcache_list
*next
;
443 static struct regcache_list
*current_regcache
;
446 get_thread_arch_regcache (ptid_t ptid
, struct gdbarch
*gdbarch
)
448 struct regcache_list
*list
;
449 struct regcache
*new_regcache
;
451 for (list
= current_regcache
; list
; list
= list
->next
)
452 if (ptid_equal (list
->regcache
->ptid
, ptid
)
453 && get_regcache_arch (list
->regcache
) == gdbarch
)
454 return list
->regcache
;
456 new_regcache
= regcache_xmalloc (gdbarch
);
457 new_regcache
->readonly_p
= 0;
458 new_regcache
->ptid
= ptid
;
459 new_regcache
->aspace
= target_thread_address_space (ptid
);
460 gdb_assert (new_regcache
->aspace
!= NULL
);
462 list
= xmalloc (sizeof (struct regcache_list
));
463 list
->regcache
= new_regcache
;
464 list
->next
= current_regcache
;
465 current_regcache
= list
;
470 static ptid_t current_thread_ptid
;
471 static struct gdbarch
*current_thread_arch
;
474 get_thread_regcache (ptid_t ptid
)
476 if (!current_thread_arch
|| !ptid_equal (current_thread_ptid
, ptid
))
478 current_thread_ptid
= ptid
;
479 current_thread_arch
= target_thread_architecture (ptid
);
482 return get_thread_arch_regcache (ptid
, current_thread_arch
);
486 get_current_regcache (void)
488 return get_thread_regcache (inferior_ptid
);
492 /* Observer for the target_changed event. */
495 regcache_observer_target_changed (struct target_ops
*target
)
497 registers_changed ();
500 /* Update global variables old ptids to hold NEW_PTID if they were
503 regcache_thread_ptid_changed (ptid_t old_ptid
, ptid_t new_ptid
)
505 struct regcache_list
*list
;
507 for (list
= current_regcache
; list
; list
= list
->next
)
508 if (ptid_equal (list
->regcache
->ptid
, old_ptid
))
509 list
->regcache
->ptid
= new_ptid
;
512 /* Low level examining and depositing of registers.
514 The caller is responsible for making sure that the inferior is
515 stopped before calling the fetching routines, or it will get
516 garbage. (a change from GDB version 3, in which the caller got the
517 value from the last stop). */
519 /* REGISTERS_CHANGED ()
521 Indicate that registers may have changed, so invalidate the cache. */
524 registers_changed (void)
526 struct regcache_list
*list
, *next
;
528 for (list
= current_regcache
; list
; list
= next
)
531 regcache_xfree (list
->regcache
);
535 current_regcache
= NULL
;
537 current_thread_ptid
= null_ptid
;
538 current_thread_arch
= NULL
;
540 /* Need to forget about any frames we have cached, too. */
541 reinit_frame_cache ();
543 /* Force cleanup of any alloca areas if using C alloca instead of
544 a builtin alloca. This particular call is used to clean up
545 areas allocated by low level target code which may build up
546 during lengthy interactions between gdb and the target before
547 gdb gives control to the user (ie watchpoints). */
553 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
555 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
556 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
557 /* Make certain that the register cache is up-to-date with respect
558 to the current thread. This switching shouldn't be necessary
559 only there is still only one target side register cache. Sigh!
560 On the bright side, at least there is a regcache object. */
561 if (!regcache
->readonly_p
)
563 if (!regcache_valid_p (regcache
, regnum
))
565 struct cleanup
*old_chain
= save_inferior_ptid ();
566 inferior_ptid
= regcache
->ptid
;
567 target_fetch_registers (regcache
, regnum
);
568 do_cleanups (old_chain
);
571 /* FIXME: cagney/2004-08-07: At present a number of targets
572 forget (or didn't know that they needed) to set this leading to
573 panics. Also is the problem that targets need to indicate
574 that a register is in one of the possible states: valid,
575 undefined, unknown. The last of which isn't yet
577 gdb_assert (regcache_valid_p (regcache
, regnum
));
580 /* Copy the value directly into the register cache. */
581 memcpy (buf
, register_buffer (regcache
, regnum
),
582 regcache
->descr
->sizeof_register
[regnum
]);
586 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
589 gdb_assert (regcache
!= NULL
);
590 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
591 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
592 regcache_raw_read (regcache
, regnum
, buf
);
593 (*val
) = extract_signed_integer
594 (buf
, regcache
->descr
->sizeof_register
[regnum
],
595 gdbarch_byte_order (regcache
->descr
->gdbarch
));
599 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
603 gdb_assert (regcache
!= NULL
);
604 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
605 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
606 regcache_raw_read (regcache
, regnum
, buf
);
607 (*val
) = extract_unsigned_integer
608 (buf
, regcache
->descr
->sizeof_register
[regnum
],
609 gdbarch_byte_order (regcache
->descr
->gdbarch
));
613 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
616 gdb_assert (regcache
!= NULL
);
617 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
618 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
619 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
620 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
621 regcache_raw_write (regcache
, regnum
, buf
);
625 regcache_raw_write_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 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
633 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
634 regcache_raw_write (regcache
, regnum
, buf
);
638 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
640 gdb_assert (regnum
>= 0);
641 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
642 if (regnum
< regcache
->descr
->nr_raw_registers
)
643 regcache_raw_read (regcache
, regnum
, buf
);
644 else if (regcache
->readonly_p
645 && regnum
< regcache
->descr
->nr_cooked_registers
646 && regcache
->register_valid_p
[regnum
])
647 /* Read-only register cache, perhaps the cooked value was cached? */
648 memcpy (buf
, register_buffer (regcache
, regnum
),
649 regcache
->descr
->sizeof_register
[regnum
]);
651 gdbarch_pseudo_register_read (regcache
->descr
->gdbarch
, regcache
,
656 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
660 gdb_assert (regcache
!= NULL
);
661 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
662 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
663 regcache_cooked_read (regcache
, regnum
, buf
);
664 (*val
) = extract_signed_integer
665 (buf
, regcache
->descr
->sizeof_register
[regnum
],
666 gdbarch_byte_order (regcache
->descr
->gdbarch
));
670 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
674 gdb_assert (regcache
!= NULL
);
675 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
676 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
677 regcache_cooked_read (regcache
, regnum
, buf
);
678 (*val
) = extract_unsigned_integer
679 (buf
, regcache
->descr
->sizeof_register
[regnum
],
680 gdbarch_byte_order (regcache
->descr
->gdbarch
));
684 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
688 gdb_assert (regcache
!= NULL
);
689 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
690 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
691 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
692 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
693 regcache_cooked_write (regcache
, regnum
, buf
);
697 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
701 gdb_assert (regcache
!= NULL
);
702 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
703 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
704 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
],
705 gdbarch_byte_order (regcache
->descr
->gdbarch
), val
);
706 regcache_cooked_write (regcache
, regnum
, buf
);
710 regcache_raw_write (struct regcache
*regcache
, int regnum
,
713 struct cleanup
*old_chain
;
715 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
716 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
717 gdb_assert (!regcache
->readonly_p
);
719 /* On the sparc, writing %g0 is a no-op, so we don't even want to
720 change the registers array if something writes to this register. */
721 if (gdbarch_cannot_store_register (get_regcache_arch (regcache
), regnum
))
724 /* If we have a valid copy of the register, and new value == old
725 value, then don't bother doing the actual store. */
726 if (regcache_valid_p (regcache
, regnum
)
727 && (memcmp (register_buffer (regcache
, regnum
), buf
,
728 regcache
->descr
->sizeof_register
[regnum
]) == 0))
731 old_chain
= save_inferior_ptid ();
732 inferior_ptid
= regcache
->ptid
;
734 target_prepare_to_store (regcache
);
735 memcpy (register_buffer (regcache
, regnum
), buf
,
736 regcache
->descr
->sizeof_register
[regnum
]);
737 regcache
->register_valid_p
[regnum
] = 1;
738 target_store_registers (regcache
, regnum
);
740 do_cleanups (old_chain
);
744 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
747 gdb_assert (regnum
>= 0);
748 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
749 if (regnum
< regcache
->descr
->nr_raw_registers
)
750 regcache_raw_write (regcache
, regnum
, buf
);
752 gdbarch_pseudo_register_write (regcache
->descr
->gdbarch
, regcache
,
756 /* Perform a partial register transfer using a read, modify, write
759 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
761 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
765 regcache_xfer_part (struct regcache
*regcache
, int regnum
,
766 int offset
, int len
, void *in
, const void *out
,
767 void (*read
) (struct regcache
*regcache
, int regnum
,
769 void (*write
) (struct regcache
*regcache
, int regnum
,
770 const gdb_byte
*buf
))
772 struct regcache_descr
*descr
= regcache
->descr
;
773 gdb_byte reg
[MAX_REGISTER_SIZE
];
774 gdb_assert (offset
>= 0 && offset
<= descr
->sizeof_register
[regnum
]);
775 gdb_assert (len
>= 0 && offset
+ len
<= descr
->sizeof_register
[regnum
]);
776 /* Something to do? */
777 if (offset
+ len
== 0)
779 /* Read (when needed) ... */
782 || offset
+ len
< descr
->sizeof_register
[regnum
])
784 gdb_assert (read
!= NULL
);
785 read (regcache
, regnum
, reg
);
789 memcpy (in
, reg
+ offset
, len
);
791 memcpy (reg
+ offset
, out
, len
);
792 /* ... write (when needed). */
795 gdb_assert (write
!= NULL
);
796 write (regcache
, regnum
, reg
);
801 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
802 int offset
, int len
, gdb_byte
*buf
)
804 struct regcache_descr
*descr
= regcache
->descr
;
805 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
806 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
807 regcache_raw_read
, regcache_raw_write
);
811 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
812 int offset
, int len
, const gdb_byte
*buf
)
814 struct regcache_descr
*descr
= regcache
->descr
;
815 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
816 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
817 regcache_raw_read
, regcache_raw_write
);
821 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
822 int offset
, int len
, gdb_byte
*buf
)
824 struct regcache_descr
*descr
= regcache
->descr
;
825 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
826 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
827 regcache_cooked_read
, regcache_cooked_write
);
831 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
832 int offset
, int len
, const gdb_byte
*buf
)
834 struct regcache_descr
*descr
= regcache
->descr
;
835 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
836 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
837 regcache_cooked_read
, regcache_cooked_write
);
840 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
843 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
848 gdb_assert (regcache
!= NULL
);
849 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
850 gdb_assert (!regcache
->readonly_p
);
852 regbuf
= register_buffer (regcache
, regnum
);
853 size
= regcache
->descr
->sizeof_register
[regnum
];
856 memcpy (regbuf
, buf
, size
);
858 memset (regbuf
, 0, size
);
860 /* Mark the register as cached. */
861 regcache
->register_valid_p
[regnum
] = 1;
864 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
867 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
872 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
873 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
875 regbuf
= register_buffer (regcache
, regnum
);
876 size
= regcache
->descr
->sizeof_register
[regnum
];
877 memcpy (buf
, regbuf
, size
);
881 /* Special handling for register PC. */
884 regcache_read_pc (struct regcache
*regcache
)
886 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
890 if (gdbarch_read_pc_p (gdbarch
))
891 pc_val
= gdbarch_read_pc (gdbarch
, regcache
);
892 /* Else use per-frame method on get_current_frame. */
893 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
896 regcache_cooked_read_unsigned (regcache
,
897 gdbarch_pc_regnum (gdbarch
),
899 pc_val
= gdbarch_addr_bits_remove (gdbarch
, raw_val
);
902 internal_error (__FILE__
, __LINE__
,
903 _("regcache_read_pc: Unable to find PC"));
908 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
910 struct gdbarch
*gdbarch
= get_regcache_arch (regcache
);
912 if (gdbarch_write_pc_p (gdbarch
))
913 gdbarch_write_pc (gdbarch
, regcache
, pc
);
914 else if (gdbarch_pc_regnum (gdbarch
) >= 0)
915 regcache_cooked_write_unsigned (regcache
,
916 gdbarch_pc_regnum (gdbarch
), pc
);
918 internal_error (__FILE__
, __LINE__
,
919 _("regcache_write_pc: Unable to update PC"));
921 /* Writing the PC (for instance, from "load") invalidates the
923 reinit_frame_cache ();
928 reg_flush_command (char *command
, int from_tty
)
930 /* Force-flush the register cache. */
931 registers_changed ();
933 printf_filtered (_("Register cache flushed.\n"));
937 dump_endian_bytes (struct ui_file
*file
, enum bfd_endian endian
,
938 const unsigned char *buf
, long len
)
944 for (i
= 0; i
< len
; i
++)
945 fprintf_unfiltered (file
, "%02x", buf
[i
]);
947 case BFD_ENDIAN_LITTLE
:
948 for (i
= len
- 1; i
>= 0; i
--)
949 fprintf_unfiltered (file
, "%02x", buf
[i
]);
952 internal_error (__FILE__
, __LINE__
, _("Bad switch"));
956 enum regcache_dump_what
958 regcache_dump_none
, regcache_dump_raw
, regcache_dump_cooked
, regcache_dump_groups
962 regcache_dump (struct regcache
*regcache
, struct ui_file
*file
,
963 enum regcache_dump_what what_to_dump
)
965 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
966 struct gdbarch
*gdbarch
= regcache
->descr
->gdbarch
;
969 int footnote_register_size
= 0;
970 int footnote_register_offset
= 0;
971 int footnote_register_type_name_null
= 0;
972 long register_offset
= 0;
973 unsigned char buf
[MAX_REGISTER_SIZE
];
976 fprintf_unfiltered (file
, "nr_raw_registers %d\n",
977 regcache
->descr
->nr_raw_registers
);
978 fprintf_unfiltered (file
, "nr_cooked_registers %d\n",
979 regcache
->descr
->nr_cooked_registers
);
980 fprintf_unfiltered (file
, "sizeof_raw_registers %ld\n",
981 regcache
->descr
->sizeof_raw_registers
);
982 fprintf_unfiltered (file
, "sizeof_raw_register_valid_p %ld\n",
983 regcache
->descr
->sizeof_raw_register_valid_p
);
984 fprintf_unfiltered (file
, "gdbarch_num_regs %d\n",
985 gdbarch_num_regs (gdbarch
));
986 fprintf_unfiltered (file
, "gdbarch_num_pseudo_regs %d\n",
987 gdbarch_num_pseudo_regs (gdbarch
));
990 gdb_assert (regcache
->descr
->nr_cooked_registers
991 == (gdbarch_num_regs (gdbarch
)
992 + gdbarch_num_pseudo_regs (gdbarch
)));
994 for (regnum
= -1; regnum
< regcache
->descr
->nr_cooked_registers
; regnum
++)
998 fprintf_unfiltered (file
, " %-10s", "Name");
1001 const char *p
= gdbarch_register_name (gdbarch
, regnum
);
1004 else if (p
[0] == '\0')
1006 fprintf_unfiltered (file
, " %-10s", p
);
1011 fprintf_unfiltered (file
, " %4s", "Nr");
1013 fprintf_unfiltered (file
, " %4d", regnum
);
1015 /* Relative number. */
1017 fprintf_unfiltered (file
, " %4s", "Rel");
1018 else if (regnum
< gdbarch_num_regs (gdbarch
))
1019 fprintf_unfiltered (file
, " %4d", regnum
);
1021 fprintf_unfiltered (file
, " %4d",
1022 (regnum
- gdbarch_num_regs (gdbarch
)));
1026 fprintf_unfiltered (file
, " %6s ", "Offset");
1029 fprintf_unfiltered (file
, " %6ld",
1030 regcache
->descr
->register_offset
[regnum
]);
1031 if (register_offset
!= regcache
->descr
->register_offset
[regnum
]
1033 && (regcache
->descr
->register_offset
[regnum
]
1034 != (regcache
->descr
->register_offset
[regnum
- 1]
1035 + regcache
->descr
->sizeof_register
[regnum
- 1])))
1038 if (!footnote_register_offset
)
1039 footnote_register_offset
= ++footnote_nr
;
1040 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1043 fprintf_unfiltered (file
, " ");
1044 register_offset
= (regcache
->descr
->register_offset
[regnum
]
1045 + regcache
->descr
->sizeof_register
[regnum
]);
1050 fprintf_unfiltered (file
, " %5s ", "Size");
1052 fprintf_unfiltered (file
, " %5ld",
1053 regcache
->descr
->sizeof_register
[regnum
]);
1062 static const char blt
[] = "builtin_type";
1063 t
= TYPE_NAME (register_type (regcache
->descr
->gdbarch
, regnum
));
1067 if (!footnote_register_type_name_null
)
1068 footnote_register_type_name_null
= ++footnote_nr
;
1069 n
= xstrprintf ("*%d", footnote_register_type_name_null
);
1070 make_cleanup (xfree
, n
);
1073 /* Chop a leading builtin_type. */
1074 if (strncmp (t
, blt
, strlen (blt
)) == 0)
1077 fprintf_unfiltered (file
, " %-15s", t
);
1080 /* Leading space always present. */
1081 fprintf_unfiltered (file
, " ");
1084 if (what_to_dump
== regcache_dump_raw
)
1087 fprintf_unfiltered (file
, "Raw value");
1088 else if (regnum
>= regcache
->descr
->nr_raw_registers
)
1089 fprintf_unfiltered (file
, "<cooked>");
1090 else if (!regcache_valid_p (regcache
, regnum
))
1091 fprintf_unfiltered (file
, "<invalid>");
1094 regcache_raw_read (regcache
, regnum
, buf
);
1095 fprintf_unfiltered (file
, "0x");
1096 dump_endian_bytes (file
,
1097 gdbarch_byte_order (gdbarch
), buf
,
1098 regcache
->descr
->sizeof_register
[regnum
]);
1102 /* Value, cooked. */
1103 if (what_to_dump
== regcache_dump_cooked
)
1106 fprintf_unfiltered (file
, "Cooked value");
1109 regcache_cooked_read (regcache
, regnum
, buf
);
1110 fprintf_unfiltered (file
, "0x");
1111 dump_endian_bytes (file
,
1112 gdbarch_byte_order (gdbarch
), buf
,
1113 regcache
->descr
->sizeof_register
[regnum
]);
1117 /* Group members. */
1118 if (what_to_dump
== regcache_dump_groups
)
1121 fprintf_unfiltered (file
, "Groups");
1124 const char *sep
= "";
1125 struct reggroup
*group
;
1126 for (group
= reggroup_next (gdbarch
, NULL
);
1128 group
= reggroup_next (gdbarch
, group
))
1130 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
1132 fprintf_unfiltered (file
, "%s%s", sep
, reggroup_name (group
));
1139 fprintf_unfiltered (file
, "\n");
1142 if (footnote_register_size
)
1143 fprintf_unfiltered (file
, "*%d: Inconsistent register sizes.\n",
1144 footnote_register_size
);
1145 if (footnote_register_offset
)
1146 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1147 footnote_register_offset
);
1148 if (footnote_register_type_name_null
)
1149 fprintf_unfiltered (file
,
1150 "*%d: Register type's name NULL.\n",
1151 footnote_register_type_name_null
);
1152 do_cleanups (cleanups
);
1156 regcache_print (char *args
, enum regcache_dump_what what_to_dump
)
1159 regcache_dump (get_current_regcache (), gdb_stdout
, what_to_dump
);
1162 struct cleanup
*cleanups
;
1163 struct ui_file
*file
= gdb_fopen (args
, "w");
1165 perror_with_name (_("maintenance print architecture"));
1166 cleanups
= make_cleanup_ui_file_delete (file
);
1167 regcache_dump (get_current_regcache (), file
, what_to_dump
);
1168 do_cleanups (cleanups
);
1173 maintenance_print_registers (char *args
, int from_tty
)
1175 regcache_print (args
, regcache_dump_none
);
1179 maintenance_print_raw_registers (char *args
, int from_tty
)
1181 regcache_print (args
, regcache_dump_raw
);
1185 maintenance_print_cooked_registers (char *args
, int from_tty
)
1187 regcache_print (args
, regcache_dump_cooked
);
1191 maintenance_print_register_groups (char *args
, int from_tty
)
1193 regcache_print (args
, regcache_dump_groups
);
1196 extern initialize_file_ftype _initialize_regcache
; /* -Wmissing-prototype */
1199 _initialize_regcache (void)
1201 regcache_descr_handle
= gdbarch_data_register_post_init (init_regcache_descr
);
1203 observer_attach_target_changed (regcache_observer_target_changed
);
1204 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed
);
1206 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1207 _("Force gdb to flush its register cache (maintainer command)"));
1209 add_cmd ("registers", class_maintenance
, maintenance_print_registers
, _("\
1210 Print the internal register configuration.\n\
1211 Takes an optional file parameter."), &maintenanceprintlist
);
1212 add_cmd ("raw-registers", class_maintenance
,
1213 maintenance_print_raw_registers
, _("\
1214 Print the internal register configuration including raw values.\n\
1215 Takes an optional file parameter."), &maintenanceprintlist
);
1216 add_cmd ("cooked-registers", class_maintenance
,
1217 maintenance_print_cooked_registers
, _("\
1218 Print the internal register configuration including cooked values.\n\
1219 Takes an optional file parameter."), &maintenanceprintlist
);
1220 add_cmd ("register-groups", class_maintenance
,
1221 maintenance_print_register_groups
, _("\
1222 Print the internal register configuration including each register's group.\n\
1223 Takes an optional file parameter."),
1224 &maintenanceprintlist
);