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,
4 2001, 2002, 2004 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 2 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 #include "reggroups.h"
30 #include "gdb_assert.h"
31 #include "gdb_string.h"
32 #include "gdbcmd.h" /* For maintenanceprintlist. */
38 * Here is the actual register cache.
41 /* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created */
44 struct gdbarch_data
*regcache_descr_handle
;
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch
*gdbarch
;
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
54 registers then those registers and not the PC lives in the raw
57 long sizeof_raw_registers
;
58 long sizeof_raw_register_valid_p
;
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
64 both raw registers and memory by the architecture methods
65 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
66 int nr_cooked_registers
;
67 long sizeof_cooked_registers
;
68 long sizeof_cooked_register_valid_p
;
70 /* Offset and size (in 8 bit bytes), of reach register in the
71 register cache. All registers (including those in the range
72 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
73 Assigning all registers an offset makes it possible to keep
74 legacy code, such as that found in read_register_bytes() and
75 write_register_bytes() working. */
76 long *register_offset
;
77 long *sizeof_register
;
79 /* Cached table containing the type of each register. */
80 struct type
**register_type
;
84 init_regcache_descr (struct gdbarch
*gdbarch
)
87 struct regcache_descr
*descr
;
88 gdb_assert (gdbarch
!= NULL
);
90 /* Create an initial, zero filled, table. */
91 descr
= GDBARCH_OBSTACK_ZALLOC (gdbarch
, struct regcache_descr
);
92 descr
->gdbarch
= gdbarch
;
94 /* Total size of the register space. The raw registers are mapped
95 directly onto the raw register cache while the pseudo's are
96 either mapped onto raw-registers or memory. */
97 descr
->nr_cooked_registers
= NUM_REGS
+ NUM_PSEUDO_REGS
;
98 descr
->sizeof_cooked_register_valid_p
= NUM_REGS
+ NUM_PSEUDO_REGS
;
100 /* Fill in a table of register types. */
102 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, struct type
*);
103 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
104 descr
->register_type
[i
] = gdbarch_register_type (gdbarch
, i
);
106 /* Construct a strictly RAW register cache. Don't allow pseudo's
107 into the register cache. */
108 descr
->nr_raw_registers
= NUM_REGS
;
110 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111 array. This pretects GDB from erant code that accesses elements
112 of the global register_valid_p[] array in the range [NUM_REGS
113 .. NUM_REGS + NUM_PSEUDO_REGS). */
114 descr
->sizeof_raw_register_valid_p
= descr
->sizeof_cooked_register_valid_p
;
116 /* Lay out the register cache.
118 NOTE: cagney/2002-05-22: Only register_type() is used when
119 constructing the register cache. It is assumed that the
120 register's raw size, virtual size and type length are all the
125 descr
->sizeof_register
126 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
127 descr
->register_offset
128 = GDBARCH_OBSTACK_CALLOC (gdbarch
, descr
->nr_cooked_registers
, long);
129 for (i
= 0; i
< descr
->nr_cooked_registers
; i
++)
131 descr
->sizeof_register
[i
] = TYPE_LENGTH (descr
->register_type
[i
]);
132 descr
->register_offset
[i
] = offset
;
133 offset
+= descr
->sizeof_register
[i
];
134 gdb_assert (MAX_REGISTER_SIZE
>= descr
->sizeof_register
[i
]);
136 /* Set the real size of the register cache buffer. */
137 descr
->sizeof_cooked_registers
= offset
;
140 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
141 the raw registers. Unfortunately some code still accesses the
142 register array directly using the global registers[]. Until that
143 code has been purged, play safe and over allocating the register
145 descr
->sizeof_raw_registers
= descr
->sizeof_cooked_registers
;
150 static struct regcache_descr
*
151 regcache_descr (struct gdbarch
*gdbarch
)
153 return gdbarch_data (gdbarch
, regcache_descr_handle
);
156 /* Utility functions returning useful register attributes stored in
157 the regcache descr. */
160 register_type (struct gdbarch
*gdbarch
, int regnum
)
162 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
163 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
164 return descr
->register_type
[regnum
];
167 /* Utility functions returning useful register attributes stored in
168 the regcache descr. */
171 register_size (struct gdbarch
*gdbarch
, int regnum
)
173 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
175 gdb_assert (regnum
>= 0 && regnum
< (NUM_REGS
+ NUM_PSEUDO_REGS
));
176 size
= descr
->sizeof_register
[regnum
];
180 /* The register cache for storing raw register values. */
184 struct regcache_descr
*descr
;
185 /* The register buffers. A read-only register cache can hold the
186 full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
187 register cache can only hold [0 .. NUM_REGS). */
189 /* Register cache status:
190 register_valid_p[REG] == 0 if REG value is not in the cache
191 > 0 if REG value is in the cache
192 < 0 if REG value is permanently unavailable */
193 signed char *register_valid_p
;
194 /* Is this a read-only cache? A read-only cache is used for saving
195 the target's register state (e.g, across an inferior function
196 call or just before forcing a function return). A read-only
197 cache can only be updated via the methods regcache_dup() and
198 regcache_cpy(). The actual contents are determined by the
199 reggroup_save and reggroup_restore methods. */
204 regcache_xmalloc (struct gdbarch
*gdbarch
)
206 struct regcache_descr
*descr
;
207 struct regcache
*regcache
;
208 gdb_assert (gdbarch
!= NULL
);
209 descr
= regcache_descr (gdbarch
);
210 regcache
= XMALLOC (struct regcache
);
211 regcache
->descr
= descr
;
213 = XCALLOC (descr
->sizeof_raw_registers
, gdb_byte
);
214 regcache
->register_valid_p
215 = XCALLOC (descr
->sizeof_raw_register_valid_p
, gdb_byte
);
216 regcache
->readonly_p
= 1;
221 regcache_xfree (struct regcache
*regcache
)
223 if (regcache
== NULL
)
225 xfree (regcache
->registers
);
226 xfree (regcache
->register_valid_p
);
231 do_regcache_xfree (void *data
)
233 regcache_xfree (data
);
237 make_cleanup_regcache_xfree (struct regcache
*regcache
)
239 return make_cleanup (do_regcache_xfree
, regcache
);
242 /* Return REGCACHE's architecture. */
245 get_regcache_arch (const struct regcache
*regcache
)
247 return regcache
->descr
->gdbarch
;
250 /* Return a pointer to register REGNUM's buffer cache. */
253 register_buffer (const struct regcache
*regcache
, int regnum
)
255 return regcache
->registers
+ regcache
->descr
->register_offset
[regnum
];
259 regcache_save (struct regcache
*dst
, regcache_cooked_read_ftype
*cooked_read
,
262 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
263 gdb_byte buf
[MAX_REGISTER_SIZE
];
265 /* The DST should be `read-only', if it wasn't then the save would
266 end up trying to write the register values back out to the
268 gdb_assert (dst
->readonly_p
);
269 /* Clear the dest. */
270 memset (dst
->registers
, 0, dst
->descr
->sizeof_cooked_registers
);
271 memset (dst
->register_valid_p
, 0, dst
->descr
->sizeof_cooked_register_valid_p
);
272 /* Copy over any registers (identified by their membership in the
273 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
274 NUM_PSEUDO_REGS) range is checked since some architectures need
275 to save/restore `cooked' registers that live in memory. */
276 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
278 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, save_reggroup
))
280 int valid
= cooked_read (src
, regnum
, buf
);
283 memcpy (register_buffer (dst
, regnum
), buf
,
284 register_size (gdbarch
, regnum
));
285 dst
->register_valid_p
[regnum
] = 1;
292 regcache_restore (struct regcache
*dst
,
293 regcache_cooked_read_ftype
*cooked_read
,
294 void *cooked_read_context
)
296 struct gdbarch
*gdbarch
= dst
->descr
->gdbarch
;
297 gdb_byte buf
[MAX_REGISTER_SIZE
];
299 /* The dst had better not be read-only. If it is, the `restore'
300 doesn't make much sense. */
301 gdb_assert (!dst
->readonly_p
);
302 /* Copy over any registers, being careful to only restore those that
303 were both saved and need to be restored. The full [0 .. NUM_REGS
304 + NUM_PSEUDO_REGS) range is checked since some architectures need
305 to save/restore `cooked' registers that live in memory. */
306 for (regnum
= 0; regnum
< dst
->descr
->nr_cooked_registers
; regnum
++)
308 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, restore_reggroup
))
310 int valid
= cooked_read (cooked_read_context
, regnum
, buf
);
312 regcache_cooked_write (dst
, regnum
, buf
);
318 do_cooked_read (void *src
, int regnum
, gdb_byte
*buf
)
320 struct regcache
*regcache
= src
;
321 if (!regcache
->register_valid_p
[regnum
] && regcache
->readonly_p
)
322 /* Don't even think about fetching a register from a read-only
323 cache when the register isn't yet valid. There isn't a target
324 from which the register value can be fetched. */
326 regcache_cooked_read (regcache
, regnum
, buf
);
332 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
336 gdb_assert (src
!= NULL
&& dst
!= NULL
);
337 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
338 gdb_assert (src
!= dst
);
339 gdb_assert (src
->readonly_p
|| dst
->readonly_p
);
340 if (!src
->readonly_p
)
341 regcache_save (dst
, do_cooked_read
, src
);
342 else if (!dst
->readonly_p
)
343 regcache_restore (dst
, do_cooked_read
, src
);
345 regcache_cpy_no_passthrough (dst
, src
);
349 regcache_cpy_no_passthrough (struct regcache
*dst
, struct regcache
*src
)
352 gdb_assert (src
!= NULL
&& dst
!= NULL
);
353 gdb_assert (src
->descr
->gdbarch
== dst
->descr
->gdbarch
);
354 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
355 move of data into the current_regcache(). Doing this would be
356 silly - it would mean that valid_p would be completely invalid. */
357 gdb_assert (dst
!= current_regcache
);
358 memcpy (dst
->registers
, src
->registers
, dst
->descr
->sizeof_raw_registers
);
359 memcpy (dst
->register_valid_p
, src
->register_valid_p
,
360 dst
->descr
->sizeof_raw_register_valid_p
);
364 regcache_dup (struct regcache
*src
)
366 struct regcache
*newbuf
;
367 gdb_assert (current_regcache
!= NULL
);
368 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
);
369 regcache_cpy (newbuf
, src
);
374 regcache_dup_no_passthrough (struct regcache
*src
)
376 struct regcache
*newbuf
;
377 gdb_assert (current_regcache
!= NULL
);
378 newbuf
= regcache_xmalloc (src
->descr
->gdbarch
);
379 regcache_cpy_no_passthrough (newbuf
, src
);
384 regcache_valid_p (struct regcache
*regcache
, int regnum
)
386 gdb_assert (regcache
!= NULL
);
387 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
388 return regcache
->register_valid_p
[regnum
];
392 deprecated_grub_regcache_for_registers (struct regcache
*regcache
)
394 return regcache
->registers
;
397 /* Global structure containing the current regcache. */
398 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
399 deprecated_register_valid[] currently point into this structure. */
400 struct regcache
*current_regcache
;
402 /* NOTE: this is a write-through cache. There is no "dirty" bit for
403 recording if the register values have been changed (eg. by the
404 user). Therefore all registers must be written back to the
405 target when appropriate. */
407 /* The thread/process associated with the current set of registers. */
409 static ptid_t registers_ptid
;
417 Returns 0 if the value is not in the cache (needs fetch).
418 >0 if the value is in the cache.
419 <0 if the value is permanently unavailable (don't ask again). */
422 register_cached (int regnum
)
424 return current_regcache
->register_valid_p
[regnum
];
427 /* Record that REGNUM's value is cached if STATE is >0, uncached but
428 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
431 set_register_cached (int regnum
, int state
)
433 gdb_assert (regnum
>= 0);
434 gdb_assert (regnum
< current_regcache
->descr
->nr_raw_registers
);
435 current_regcache
->register_valid_p
[regnum
] = state
;
438 /* Observer for the target_changed event. */
441 regcache_observer_target_changed (struct target_ops
*target
)
443 registers_changed ();
446 /* Low level examining and depositing of registers.
448 The caller is responsible for making sure that the inferior is
449 stopped before calling the fetching routines, or it will get
450 garbage. (a change from GDB version 3, in which the caller got the
451 value from the last stop). */
453 /* REGISTERS_CHANGED ()
455 Indicate that registers may have changed, so invalidate the cache. */
458 registers_changed (void)
462 registers_ptid
= pid_to_ptid (-1);
464 /* Force cleanup of any alloca areas if using C alloca instead of
465 a builtin alloca. This particular call is used to clean up
466 areas allocated by low level target code which may build up
467 during lengthy interactions between gdb and the target before
468 gdb gives control to the user (ie watchpoints). */
471 for (i
= 0; i
< current_regcache
->descr
->nr_raw_registers
; i
++)
472 set_register_cached (i
, 0);
474 if (deprecated_registers_changed_hook
)
475 deprecated_registers_changed_hook ();
478 /* DEPRECATED_REGISTERS_FETCHED ()
480 Indicate that all registers have been fetched, so mark them all valid. */
482 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
483 code was blatting the registers[] array and then calling this.
484 Since targets should only be using regcache_raw_supply() the need for
485 this function/hack is eliminated. */
488 deprecated_registers_fetched (void)
492 for (i
= 0; i
< NUM_REGS
; i
++)
493 set_register_cached (i
, 1);
494 /* Do not assume that the pseudo-regs have also been fetched.
495 Fetching all real regs NEVER accounts for pseudo-regs. */
498 /* deprecated_read_register_bytes and deprecated_write_register_bytes
499 are generally a *BAD* idea. They are inefficient because they need
500 to check for partial updates, which can only be done by scanning
501 through all of the registers and seeing if the bytes that are being
502 read/written fall inside of an invalid register. [The main reason
503 this is necessary is that register sizes can vary, so a simple
504 index won't suffice.] It is far better to call read_register_gen
505 and write_register_gen if you want to get at the raw register
506 contents, as it only takes a regnum as an argument, and therefore
507 can't do a partial register update.
509 Prior to the recent fixes to check for partial updates, both read
510 and deprecated_write_register_bytes always checked to see if any
511 registers were stale, and then called target_fetch_registers (-1)
512 to update the whole set. This caused really slowed things down for
515 /* Copy INLEN bytes of consecutive data from registers
516 starting with the INREGBYTE'th byte of register data
517 into memory at MYADDR. */
520 deprecated_read_register_bytes (int in_start
, gdb_byte
*in_buf
, int in_len
)
522 int in_end
= in_start
+ in_len
;
524 gdb_byte reg_buf
[MAX_REGISTER_SIZE
];
526 /* See if we are trying to read bytes from out-of-date registers. If so,
527 update just those registers. */
529 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
538 reg_start
= DEPRECATED_REGISTER_BYTE (regnum
);
539 reg_len
= register_size (current_gdbarch
, regnum
);
540 reg_end
= reg_start
+ reg_len
;
542 if (reg_end
<= in_start
|| in_end
<= reg_start
)
543 /* The range the user wants to read doesn't overlap with regnum. */
546 if (REGISTER_NAME (regnum
) != NULL
&& *REGISTER_NAME (regnum
) != '\0')
547 /* Force the cache to fetch the entire register. */
548 deprecated_read_register_gen (regnum
, reg_buf
);
550 /* Legacy note: This function, for some reason, allows a NULL
551 input buffer. If the buffer is NULL, the registers are still
552 fetched, just the final transfer is skipped. */
556 /* start = max (reg_start, in_start) */
557 if (reg_start
> in_start
)
562 /* end = min (reg_end, in_end) */
563 if (reg_end
< in_end
)
568 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
569 for (byte
= start
; byte
< end
; byte
++)
571 in_buf
[byte
- in_start
] = reg_buf
[byte
- reg_start
];
577 regcache_raw_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
579 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
580 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
581 /* Make certain that the register cache is up-to-date with respect
582 to the current thread. This switching shouldn't be necessary
583 only there is still only one target side register cache. Sigh!
584 On the bright side, at least there is a regcache object. */
585 if (!regcache
->readonly_p
)
587 gdb_assert (regcache
== current_regcache
);
588 if (! ptid_equal (registers_ptid
, inferior_ptid
))
590 registers_changed ();
591 registers_ptid
= inferior_ptid
;
593 if (!register_cached (regnum
))
594 target_fetch_registers (regnum
);
596 /* FIXME: cagney/2004-08-07: At present a number of targets
597 forget (or didn't know that they needed) to set this leading to
598 panics. Also is the problem that targets need to indicate
599 that a register is in one of the possible states: valid,
600 undefined, unknown. The last of which isn't yet
602 gdb_assert (register_cached (regnum
));
605 /* Copy the value directly into the register cache. */
606 memcpy (buf
, register_buffer (regcache
, regnum
),
607 regcache
->descr
->sizeof_register
[regnum
]);
611 regcache_raw_read_signed (struct regcache
*regcache
, int regnum
, LONGEST
*val
)
614 gdb_assert (regcache
!= NULL
);
615 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
616 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
617 regcache_raw_read (regcache
, regnum
, buf
);
618 (*val
) = extract_signed_integer (buf
,
619 regcache
->descr
->sizeof_register
[regnum
]);
623 regcache_raw_read_unsigned (struct regcache
*regcache
, int regnum
,
627 gdb_assert (regcache
!= NULL
);
628 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
629 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
630 regcache_raw_read (regcache
, regnum
, buf
);
631 (*val
) = extract_unsigned_integer (buf
,
632 regcache
->descr
->sizeof_register
[regnum
]);
636 regcache_raw_write_signed (struct regcache
*regcache
, int regnum
, LONGEST val
)
639 gdb_assert (regcache
!= NULL
);
640 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
641 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
642 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
], val
);
643 regcache_raw_write (regcache
, regnum
, buf
);
647 regcache_raw_write_unsigned (struct regcache
*regcache
, int regnum
,
651 gdb_assert (regcache
!= NULL
);
652 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_raw_registers
);
653 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
654 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
], val
);
655 regcache_raw_write (regcache
, regnum
, buf
);
659 deprecated_read_register_gen (int regnum
, gdb_byte
*buf
)
661 gdb_assert (current_regcache
!= NULL
);
662 gdb_assert (current_regcache
->descr
->gdbarch
== current_gdbarch
);
663 regcache_cooked_read (current_regcache
, regnum
, buf
);
667 regcache_cooked_read (struct regcache
*regcache
, int regnum
, gdb_byte
*buf
)
669 gdb_assert (regnum
>= 0);
670 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
671 if (regnum
< regcache
->descr
->nr_raw_registers
)
672 regcache_raw_read (regcache
, regnum
, buf
);
673 else if (regcache
->readonly_p
674 && regnum
< regcache
->descr
->nr_cooked_registers
675 && regcache
->register_valid_p
[regnum
])
676 /* Read-only register cache, perhaps the cooked value was cached? */
677 memcpy (buf
, register_buffer (regcache
, regnum
),
678 regcache
->descr
->sizeof_register
[regnum
]);
680 gdbarch_pseudo_register_read (regcache
->descr
->gdbarch
, regcache
,
685 regcache_cooked_read_signed (struct regcache
*regcache
, int regnum
,
689 gdb_assert (regcache
!= NULL
);
690 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
691 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
692 regcache_cooked_read (regcache
, regnum
, buf
);
693 (*val
) = extract_signed_integer (buf
,
694 regcache
->descr
->sizeof_register
[regnum
]);
698 regcache_cooked_read_unsigned (struct regcache
*regcache
, int regnum
,
702 gdb_assert (regcache
!= NULL
);
703 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_cooked_registers
);
704 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
705 regcache_cooked_read (regcache
, regnum
, buf
);
706 (*val
) = extract_unsigned_integer (buf
,
707 regcache
->descr
->sizeof_register
[regnum
]);
711 regcache_cooked_write_signed (struct regcache
*regcache
, int regnum
,
715 gdb_assert (regcache
!= NULL
);
716 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
717 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
718 store_signed_integer (buf
, regcache
->descr
->sizeof_register
[regnum
], val
);
719 regcache_cooked_write (regcache
, regnum
, buf
);
723 regcache_cooked_write_unsigned (struct regcache
*regcache
, int regnum
,
727 gdb_assert (regcache
!= NULL
);
728 gdb_assert (regnum
>=0 && regnum
< regcache
->descr
->nr_cooked_registers
);
729 buf
= alloca (regcache
->descr
->sizeof_register
[regnum
]);
730 store_unsigned_integer (buf
, regcache
->descr
->sizeof_register
[regnum
], val
);
731 regcache_cooked_write (regcache
, regnum
, buf
);
735 regcache_raw_write (struct regcache
*regcache
, int regnum
,
738 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
739 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
740 gdb_assert (!regcache
->readonly_p
);
742 /* On the sparc, writing %g0 is a no-op, so we don't even want to
743 change the registers array if something writes to this register. */
744 if (CANNOT_STORE_REGISTER (regnum
))
747 /* Make certain that the correct cache is selected. */
748 gdb_assert (regcache
== current_regcache
);
749 if (! ptid_equal (registers_ptid
, inferior_ptid
))
751 registers_changed ();
752 registers_ptid
= inferior_ptid
;
755 /* If we have a valid copy of the register, and new value == old
756 value, then don't bother doing the actual store. */
757 if (regcache_valid_p (regcache
, regnum
)
758 && (memcmp (register_buffer (regcache
, regnum
), buf
,
759 regcache
->descr
->sizeof_register
[regnum
]) == 0))
762 target_prepare_to_store ();
763 memcpy (register_buffer (regcache
, regnum
), buf
,
764 regcache
->descr
->sizeof_register
[regnum
]);
765 regcache
->register_valid_p
[regnum
] = 1;
766 target_store_registers (regnum
);
770 deprecated_write_register_gen (int regnum
, gdb_byte
*buf
)
772 gdb_assert (current_regcache
!= NULL
);
773 gdb_assert (current_regcache
->descr
->gdbarch
== current_gdbarch
);
774 regcache_cooked_write (current_regcache
, regnum
, buf
);
778 regcache_cooked_write (struct regcache
*regcache
, int regnum
,
781 gdb_assert (regnum
>= 0);
782 gdb_assert (regnum
< regcache
->descr
->nr_cooked_registers
);
783 if (regnum
< regcache
->descr
->nr_raw_registers
)
784 regcache_raw_write (regcache
, regnum
, buf
);
786 gdbarch_pseudo_register_write (regcache
->descr
->gdbarch
, regcache
,
790 /* Copy INLEN bytes of consecutive data from memory at MYADDR
791 into registers starting with the MYREGSTART'th byte of register data. */
794 deprecated_write_register_bytes (int myregstart
, gdb_byte
*myaddr
, int inlen
)
796 int myregend
= myregstart
+ inlen
;
799 target_prepare_to_store ();
801 /* Scan through the registers updating any that are covered by the
802 range myregstart<=>myregend using write_register_gen, which does
803 nice things like handling threads, and avoiding updates when the
804 new and old contents are the same. */
806 for (regnum
= 0; regnum
< NUM_REGS
+ NUM_PSEUDO_REGS
; regnum
++)
808 int regstart
, regend
;
810 regstart
= DEPRECATED_REGISTER_BYTE (regnum
);
811 regend
= regstart
+ register_size (current_gdbarch
, regnum
);
813 /* Is this register completely outside the range the user is writing? */
814 if (myregend
<= regstart
|| regend
<= myregstart
)
817 /* Is this register completely within the range the user is writing? */
818 else if (myregstart
<= regstart
&& regend
<= myregend
)
819 deprecated_write_register_gen (regnum
, myaddr
+ (regstart
- myregstart
));
821 /* The register partially overlaps the range being written. */
824 gdb_byte regbuf
[MAX_REGISTER_SIZE
];
825 /* What's the overlap between this register's bytes and
826 those the caller wants to write? */
827 int overlapstart
= max (regstart
, myregstart
);
828 int overlapend
= min (regend
, myregend
);
830 /* We may be doing a partial update of an invalid register.
831 Update it from the target before scribbling on it. */
832 deprecated_read_register_gen (regnum
, regbuf
);
834 target_store_registers (regnum
);
839 /* Perform a partial register transfer using a read, modify, write
842 typedef void (regcache_read_ftype
) (struct regcache
*regcache
, int regnum
,
844 typedef void (regcache_write_ftype
) (struct regcache
*regcache
, int regnum
,
848 regcache_xfer_part (struct regcache
*regcache
, int regnum
,
849 int offset
, int len
, void *in
, const void *out
,
850 void (*read
) (struct regcache
*regcache
, int regnum
,
852 void (*write
) (struct regcache
*regcache
, int regnum
,
853 const gdb_byte
*buf
))
855 struct regcache_descr
*descr
= regcache
->descr
;
856 gdb_byte reg
[MAX_REGISTER_SIZE
];
857 gdb_assert (offset
>= 0 && offset
<= descr
->sizeof_register
[regnum
]);
858 gdb_assert (len
>= 0 && offset
+ len
<= descr
->sizeof_register
[regnum
]);
859 /* Something to do? */
860 if (offset
+ len
== 0)
862 /* Read (when needed) ... */
865 || offset
+ len
< descr
->sizeof_register
[regnum
])
867 gdb_assert (read
!= NULL
);
868 read (regcache
, regnum
, reg
);
872 memcpy (in
, reg
+ offset
, len
);
874 memcpy (reg
+ offset
, out
, len
);
875 /* ... write (when needed). */
878 gdb_assert (write
!= NULL
);
879 write (regcache
, regnum
, reg
);
884 regcache_raw_read_part (struct regcache
*regcache
, int regnum
,
885 int offset
, int len
, gdb_byte
*buf
)
887 struct regcache_descr
*descr
= regcache
->descr
;
888 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
889 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
890 regcache_raw_read
, regcache_raw_write
);
894 regcache_raw_write_part (struct regcache
*regcache
, int regnum
,
895 int offset
, int len
, const gdb_byte
*buf
)
897 struct regcache_descr
*descr
= regcache
->descr
;
898 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_raw_registers
);
899 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
900 regcache_raw_read
, regcache_raw_write
);
904 regcache_cooked_read_part (struct regcache
*regcache
, int regnum
,
905 int offset
, int len
, gdb_byte
*buf
)
907 struct regcache_descr
*descr
= regcache
->descr
;
908 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
909 regcache_xfer_part (regcache
, regnum
, offset
, len
, buf
, NULL
,
910 regcache_cooked_read
, regcache_cooked_write
);
914 regcache_cooked_write_part (struct regcache
*regcache
, int regnum
,
915 int offset
, int len
, const gdb_byte
*buf
)
917 struct regcache_descr
*descr
= regcache
->descr
;
918 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
919 regcache_xfer_part (regcache
, regnum
, offset
, len
, NULL
, buf
,
920 regcache_cooked_read
, regcache_cooked_write
);
923 /* Hack to keep code that view the register buffer as raw bytes
927 register_offset_hack (struct gdbarch
*gdbarch
, int regnum
)
929 struct regcache_descr
*descr
= regcache_descr (gdbarch
);
930 gdb_assert (regnum
>= 0 && regnum
< descr
->nr_cooked_registers
);
931 return descr
->register_offset
[regnum
];
934 /* Hack to keep code using register_bytes working. */
937 deprecated_register_bytes (void)
939 return current_regcache
->descr
->sizeof_raw_registers
;
942 /* Return the contents of register REGNUM as an unsigned integer. */
945 read_register (int regnum
)
947 gdb_byte
*buf
= alloca (register_size (current_gdbarch
, regnum
));
948 deprecated_read_register_gen (regnum
, buf
);
949 return (extract_unsigned_integer (buf
, register_size (current_gdbarch
, regnum
)));
953 read_register_pid (int regnum
, ptid_t ptid
)
959 if (ptid_equal (ptid
, inferior_ptid
))
960 return read_register (regnum
);
962 save_ptid
= inferior_ptid
;
964 inferior_ptid
= ptid
;
966 retval
= read_register (regnum
);
968 inferior_ptid
= save_ptid
;
973 /* Store VALUE into the raw contents of register number REGNUM. */
976 write_register (int regnum
, LONGEST val
)
980 size
= register_size (current_gdbarch
, regnum
);
982 store_signed_integer (buf
, size
, (LONGEST
) val
);
983 deprecated_write_register_gen (regnum
, buf
);
987 write_register_pid (int regnum
, CORE_ADDR val
, ptid_t ptid
)
991 if (ptid_equal (ptid
, inferior_ptid
))
993 write_register (regnum
, val
);
997 save_ptid
= inferior_ptid
;
999 inferior_ptid
= ptid
;
1001 write_register (regnum
, val
);
1003 inferior_ptid
= save_ptid
;
1006 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1009 regcache_raw_supply (struct regcache
*regcache
, int regnum
, const void *buf
)
1014 gdb_assert (regcache
!= NULL
);
1015 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
1016 gdb_assert (!regcache
->readonly_p
);
1018 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1019 CURRENT_REGCACHE specially here. */
1020 if (regcache
== current_regcache
1021 && !ptid_equal (registers_ptid
, inferior_ptid
))
1023 registers_changed ();
1024 registers_ptid
= inferior_ptid
;
1027 regbuf
= register_buffer (regcache
, regnum
);
1028 size
= regcache
->descr
->sizeof_register
[regnum
];
1031 memcpy (regbuf
, buf
, size
);
1033 memset (regbuf
, 0, size
);
1035 /* Mark the register as cached. */
1036 regcache
->register_valid_p
[regnum
] = 1;
1039 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1042 regcache_raw_collect (const struct regcache
*regcache
, int regnum
, void *buf
)
1047 gdb_assert (regcache
!= NULL
&& buf
!= NULL
);
1048 gdb_assert (regnum
>= 0 && regnum
< regcache
->descr
->nr_raw_registers
);
1050 regbuf
= register_buffer (regcache
, regnum
);
1051 size
= regcache
->descr
->sizeof_register
[regnum
];
1052 memcpy (buf
, regbuf
, size
);
1056 /* read_pc, write_pc, read_sp, etc. Special handling for registers
1059 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
1060 read_sp(), will eventually be replaced by per-frame methods.
1061 Instead of relying on the global INFERIOR_PTID, they will use the
1062 contextual information provided by the FRAME. These functions do
1063 not belong in the register cache. */
1065 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1066 write_pc_pid() and write_pc(), all need to be replaced by something
1067 that does not rely on global state. But what? */
1070 read_pc_pid (ptid_t ptid
)
1072 ptid_t saved_inferior_ptid
;
1075 /* In case ptid != inferior_ptid. */
1076 saved_inferior_ptid
= inferior_ptid
;
1077 inferior_ptid
= ptid
;
1079 if (TARGET_READ_PC_P ())
1080 pc_val
= TARGET_READ_PC (ptid
);
1081 /* Else use per-frame method on get_current_frame. */
1082 else if (PC_REGNUM
>= 0)
1084 CORE_ADDR raw_val
= read_register_pid (PC_REGNUM
, ptid
);
1085 pc_val
= ADDR_BITS_REMOVE (raw_val
);
1088 internal_error (__FILE__
, __LINE__
, _("read_pc_pid: Unable to find PC"));
1090 inferior_ptid
= saved_inferior_ptid
;
1097 return read_pc_pid (inferior_ptid
);
1101 generic_target_write_pc (CORE_ADDR pc
, ptid_t ptid
)
1104 write_register_pid (PC_REGNUM
, pc
, ptid
);
1106 internal_error (__FILE__
, __LINE__
,
1107 _("generic_target_write_pc"));
1111 write_pc_pid (CORE_ADDR pc
, ptid_t ptid
)
1113 ptid_t saved_inferior_ptid
;
1115 /* In case ptid != inferior_ptid. */
1116 saved_inferior_ptid
= inferior_ptid
;
1117 inferior_ptid
= ptid
;
1119 TARGET_WRITE_PC (pc
, ptid
);
1121 inferior_ptid
= saved_inferior_ptid
;
1125 write_pc (CORE_ADDR pc
)
1127 write_pc_pid (pc
, inferior_ptid
);
1130 /* Cope with strage ways of getting to the stack and frame pointers */
1135 if (TARGET_READ_SP_P ())
1136 return TARGET_READ_SP ();
1137 else if (gdbarch_unwind_sp_p (current_gdbarch
))
1138 return get_frame_sp (get_current_frame ());
1139 else if (SP_REGNUM
>= 0)
1140 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1141 about the architecture so put it at the end. */
1142 return read_register (SP_REGNUM
);
1143 internal_error (__FILE__
, __LINE__
, _("read_sp: Unable to find SP"));
1147 reg_flush_command (char *command
, int from_tty
)
1149 /* Force-flush the register cache. */
1150 registers_changed ();
1152 printf_filtered (_("Register cache flushed.\n"));
1156 build_regcache (void)
1158 current_regcache
= regcache_xmalloc (current_gdbarch
);
1159 current_regcache
->readonly_p
= 0;
1163 dump_endian_bytes (struct ui_file
*file
, enum bfd_endian endian
,
1164 const unsigned char *buf
, long len
)
1169 case BFD_ENDIAN_BIG
:
1170 for (i
= 0; i
< len
; i
++)
1171 fprintf_unfiltered (file
, "%02x", buf
[i
]);
1173 case BFD_ENDIAN_LITTLE
:
1174 for (i
= len
- 1; i
>= 0; i
--)
1175 fprintf_unfiltered (file
, "%02x", buf
[i
]);
1178 internal_error (__FILE__
, __LINE__
, _("Bad switch"));
1182 enum regcache_dump_what
1184 regcache_dump_none
, regcache_dump_raw
, regcache_dump_cooked
, regcache_dump_groups
1188 regcache_dump (struct regcache
*regcache
, struct ui_file
*file
,
1189 enum regcache_dump_what what_to_dump
)
1191 struct cleanup
*cleanups
= make_cleanup (null_cleanup
, NULL
);
1192 struct gdbarch
*gdbarch
= regcache
->descr
->gdbarch
;
1194 int footnote_nr
= 0;
1195 int footnote_register_size
= 0;
1196 int footnote_register_offset
= 0;
1197 int footnote_register_type_name_null
= 0;
1198 long register_offset
= 0;
1199 unsigned char buf
[MAX_REGISTER_SIZE
];
1202 fprintf_unfiltered (file
, "nr_raw_registers %d\n",
1203 regcache
->descr
->nr_raw_registers
);
1204 fprintf_unfiltered (file
, "nr_cooked_registers %d\n",
1205 regcache
->descr
->nr_cooked_registers
);
1206 fprintf_unfiltered (file
, "sizeof_raw_registers %ld\n",
1207 regcache
->descr
->sizeof_raw_registers
);
1208 fprintf_unfiltered (file
, "sizeof_raw_register_valid_p %ld\n",
1209 regcache
->descr
->sizeof_raw_register_valid_p
);
1210 fprintf_unfiltered (file
, "NUM_REGS %d\n", NUM_REGS
);
1211 fprintf_unfiltered (file
, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS
);
1214 gdb_assert (regcache
->descr
->nr_cooked_registers
1215 == (NUM_REGS
+ NUM_PSEUDO_REGS
));
1217 for (regnum
= -1; regnum
< regcache
->descr
->nr_cooked_registers
; regnum
++)
1221 fprintf_unfiltered (file
, " %-10s", "Name");
1224 const char *p
= REGISTER_NAME (regnum
);
1227 else if (p
[0] == '\0')
1229 fprintf_unfiltered (file
, " %-10s", p
);
1234 fprintf_unfiltered (file
, " %4s", "Nr");
1236 fprintf_unfiltered (file
, " %4d", regnum
);
1238 /* Relative number. */
1240 fprintf_unfiltered (file
, " %4s", "Rel");
1241 else if (regnum
< NUM_REGS
)
1242 fprintf_unfiltered (file
, " %4d", regnum
);
1244 fprintf_unfiltered (file
, " %4d", (regnum
- NUM_REGS
));
1248 fprintf_unfiltered (file
, " %6s ", "Offset");
1251 fprintf_unfiltered (file
, " %6ld",
1252 regcache
->descr
->register_offset
[regnum
]);
1253 if (register_offset
!= regcache
->descr
->register_offset
[regnum
]
1254 || register_offset
!= DEPRECATED_REGISTER_BYTE (regnum
)
1256 && (regcache
->descr
->register_offset
[regnum
]
1257 != (regcache
->descr
->register_offset
[regnum
- 1]
1258 + regcache
->descr
->sizeof_register
[regnum
- 1])))
1261 if (!footnote_register_offset
)
1262 footnote_register_offset
= ++footnote_nr
;
1263 fprintf_unfiltered (file
, "*%d", footnote_register_offset
);
1266 fprintf_unfiltered (file
, " ");
1267 register_offset
= (regcache
->descr
->register_offset
[regnum
]
1268 + regcache
->descr
->sizeof_register
[regnum
]);
1273 fprintf_unfiltered (file
, " %5s ", "Size");
1275 fprintf_unfiltered (file
, " %5ld",
1276 regcache
->descr
->sizeof_register
[regnum
]);
1285 static const char blt
[] = "builtin_type";
1286 t
= TYPE_NAME (register_type (regcache
->descr
->gdbarch
, regnum
));
1290 if (!footnote_register_type_name_null
)
1291 footnote_register_type_name_null
= ++footnote_nr
;
1292 n
= xstrprintf ("*%d", footnote_register_type_name_null
);
1293 make_cleanup (xfree
, n
);
1296 /* Chop a leading builtin_type. */
1297 if (strncmp (t
, blt
, strlen (blt
)) == 0)
1300 fprintf_unfiltered (file
, " %-15s", t
);
1303 /* Leading space always present. */
1304 fprintf_unfiltered (file
, " ");
1307 if (what_to_dump
== regcache_dump_raw
)
1310 fprintf_unfiltered (file
, "Raw value");
1311 else if (regnum
>= regcache
->descr
->nr_raw_registers
)
1312 fprintf_unfiltered (file
, "<cooked>");
1313 else if (!regcache_valid_p (regcache
, regnum
))
1314 fprintf_unfiltered (file
, "<invalid>");
1317 regcache_raw_read (regcache
, regnum
, buf
);
1318 fprintf_unfiltered (file
, "0x");
1319 dump_endian_bytes (file
, TARGET_BYTE_ORDER
, buf
,
1320 regcache
->descr
->sizeof_register
[regnum
]);
1324 /* Value, cooked. */
1325 if (what_to_dump
== regcache_dump_cooked
)
1328 fprintf_unfiltered (file
, "Cooked value");
1331 regcache_cooked_read (regcache
, regnum
, buf
);
1332 fprintf_unfiltered (file
, "0x");
1333 dump_endian_bytes (file
, TARGET_BYTE_ORDER
, buf
,
1334 regcache
->descr
->sizeof_register
[regnum
]);
1338 /* Group members. */
1339 if (what_to_dump
== regcache_dump_groups
)
1342 fprintf_unfiltered (file
, "Groups");
1345 const char *sep
= "";
1346 struct reggroup
*group
;
1347 for (group
= reggroup_next (gdbarch
, NULL
);
1349 group
= reggroup_next (gdbarch
, group
))
1351 if (gdbarch_register_reggroup_p (gdbarch
, regnum
, group
))
1353 fprintf_unfiltered (file
, "%s%s", sep
, reggroup_name (group
));
1360 fprintf_unfiltered (file
, "\n");
1363 if (footnote_register_size
)
1364 fprintf_unfiltered (file
, "*%d: Inconsistent register sizes.\n",
1365 footnote_register_size
);
1366 if (footnote_register_offset
)
1367 fprintf_unfiltered (file
, "*%d: Inconsistent register offsets.\n",
1368 footnote_register_offset
);
1369 if (footnote_register_type_name_null
)
1370 fprintf_unfiltered (file
,
1371 "*%d: Register type's name NULL.\n",
1372 footnote_register_type_name_null
);
1373 do_cleanups (cleanups
);
1377 regcache_print (char *args
, enum regcache_dump_what what_to_dump
)
1380 regcache_dump (current_regcache
, gdb_stdout
, what_to_dump
);
1383 struct ui_file
*file
= gdb_fopen (args
, "w");
1385 perror_with_name (_("maintenance print architecture"));
1386 regcache_dump (current_regcache
, file
, what_to_dump
);
1387 ui_file_delete (file
);
1392 maintenance_print_registers (char *args
, int from_tty
)
1394 regcache_print (args
, regcache_dump_none
);
1398 maintenance_print_raw_registers (char *args
, int from_tty
)
1400 regcache_print (args
, regcache_dump_raw
);
1404 maintenance_print_cooked_registers (char *args
, int from_tty
)
1406 regcache_print (args
, regcache_dump_cooked
);
1410 maintenance_print_register_groups (char *args
, int from_tty
)
1412 regcache_print (args
, regcache_dump_groups
);
1415 extern initialize_file_ftype _initialize_regcache
; /* -Wmissing-prototype */
1418 _initialize_regcache (void)
1420 regcache_descr_handle
= gdbarch_data_register_post_init (init_regcache_descr
);
1421 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache
);
1422 deprecated_register_gdbarch_swap (NULL
, 0, build_regcache
);
1424 observer_attach_target_changed (regcache_observer_target_changed
);
1426 add_com ("flushregs", class_maintenance
, reg_flush_command
,
1427 _("Force gdb to flush its register cache (maintainer command)"));
1429 /* Initialize the thread/process associated with the current set of
1430 registers. For now, -1 is special, and means `no current process'. */
1431 registers_ptid
= pid_to_ptid (-1);
1433 add_cmd ("registers", class_maintenance
, maintenance_print_registers
, _("\
1434 Print the internal register configuration.\n\
1435 Takes an optional file parameter."), &maintenanceprintlist
);
1436 add_cmd ("raw-registers", class_maintenance
,
1437 maintenance_print_raw_registers
, _("\
1438 Print the internal register configuration including raw values.\n\
1439 Takes an optional file parameter."), &maintenanceprintlist
);
1440 add_cmd ("cooked-registers", class_maintenance
,
1441 maintenance_print_cooked_registers
, _("\
1442 Print the internal register configuration including cooked values.\n\
1443 Takes an optional file parameter."), &maintenanceprintlist
);
1444 add_cmd ("register-groups", class_maintenance
,
1445 maintenance_print_register_groups
, _("\
1446 Print the internal register configuration including each register's group.\n\
1447 Takes an optional file parameter."),
1448 &maintenanceprintlist
);