1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986-2024 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/>. */
23 #include "gdbsupport/array-view.h"
24 #include "gdbsupport/common-regcache.h"
25 #include "gdbsupport/function-view.h"
26 #include "gdbsupport/traits.h"
32 struct process_stratum_target
;
35 extern struct regcache
*get_thread_regcache (process_stratum_target
*target
,
38 /* Get the regcache of THREAD. */
39 extern struct regcache
*get_thread_regcache (thread_info
*thread
);
41 extern regcache
*get_thread_arch_regcache (inferior
*inf_for_target_calls
,
42 ptid_t ptid
, gdbarch
*arch
);
44 extern enum register_status
45 regcache_raw_read_signed (struct regcache
*regcache
,
46 int regnum
, LONGEST
*val
);
48 extern void regcache_raw_write_signed (struct regcache
*regcache
,
49 int regnum
, LONGEST val
);
50 extern void regcache_raw_write_unsigned (struct regcache
*regcache
,
51 int regnum
, ULONGEST val
);
53 /* Return the register's value in signed or throw if it's not
56 extern LONGEST
regcache_raw_get_signed (struct regcache
*regcache
,
59 /* Read a register as a signed/unsigned quantity. */
60 extern enum register_status
61 regcache_cooked_read_signed (struct regcache
*regcache
,
62 int regnum
, LONGEST
*val
);
63 extern enum register_status
64 regcache_cooked_read_unsigned (struct regcache
*regcache
,
65 int regnum
, ULONGEST
*val
);
66 extern void regcache_cooked_write_signed (struct regcache
*regcache
,
67 int regnum
, LONGEST val
);
68 extern void regcache_cooked_write_unsigned (struct regcache
*regcache
,
69 int regnum
, ULONGEST val
);
71 /* Special routines to read/write the PC. */
73 /* For regcache_read_pc see gdbsupport/common-regcache.h. */
74 extern void regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
);
76 /* Mapping between register numbers and offsets in a buffer, for use
77 in the '*regset' functions below and with traditional frame caches.
78 In an array of 'regcache_map_entry' each element is interpreted
81 - If 'regno' is a register number: Map register 'regno' to the
82 current offset (starting with 0) and increase the current offset
83 by 'size' (or the register's size, if 'size' is zero). Repeat
84 this with consecutive register numbers up to 'regno+count-1'.
86 For each described register, if 'size' is larger than the
87 register's size, the register's value is assumed to be stored in
88 the first N (where N is the register size) bytes at the current
89 offset. The remaining 'size' - N bytes are filled with zeroes by
90 'regcache_collect_regset' and ignored by other consumers.
92 If 'size' is smaller than the register's size, only the first
93 'size' bytes of a register's value are assumed to be stored at
94 the current offset. 'regcache_collect_regset' copies the first
95 'size' bytes of a register's value to the output buffer.
96 'regcache_supply_regset' copies the bytes from the input buffer
97 into the first 'size' bytes of the register's value leaving the
98 remaining bytes of the register's value unchanged. Frame caches
99 read the 'size' bytes from the stack frame and zero extend them
100 to generate the register's value.
102 - If 'regno' is REGCACHE_MAP_SKIP: Add 'count*size' to the current
105 - If count=0: End of the map. */
107 struct regcache_map_entry
114 /* Special value for the 'regno' field in the struct above. */
118 REGCACHE_MAP_SKIP
= -1,
121 /* Calculate and return the total size of all the registers in a
122 regcache_map_entry. */
125 regcache_map_entry_size (const struct regcache_map_entry
*map
)
128 for (int i
= 0; map
[i
].count
!= 0; i
++)
129 size
+= (map
[i
].count
* map
[i
].size
);
133 /* Transfer a set of registers (as described by REGSET) between
134 REGCACHE and BUF. If REGNUM == -1, transfer all registers
135 belonging to the regset, otherwise just the register numbered
136 REGNUM. The REGSET's 'regmap' field must point to an array of
137 'struct regcache_map_entry'.
139 These functions are suitable for the 'regset_supply' and
140 'regset_collect' fields in a regset structure. */
142 extern void regcache_supply_regset (const struct regset
*regset
,
143 struct regcache
*regcache
,
144 int regnum
, const void *buf
,
146 extern void regcache_collect_regset (const struct regset
*regset
,
147 const struct regcache
*regcache
,
148 int regnum
, void *buf
, size_t size
);
151 /* Return true if a set of registers contains the value of the
152 register numbered REGNUM. The size of the set of registers is
153 given in SIZE, and the layout of the set of registers is described
156 extern bool regcache_map_supplies (const struct regcache_map_entry
*map
,
157 int regnum
, struct gdbarch
*gdbarch
,
160 /* The type of a register. This function is slightly more efficient
161 then its gdbarch vector counterpart since it returns a precomputed
162 value stored in a table. */
164 extern struct type
*register_type (struct gdbarch
*gdbarch
, int regnum
);
167 /* Return the size of register REGNUM. All registers should have only
170 extern int register_size (struct gdbarch
*gdbarch
, int regnum
);
172 using register_read_ftype
173 = gdb::function_view
<register_status (int, gdb::array_view
<gdb_byte
>)>;
175 /* A (register_number, register_value) pair. */
180 gdb::unique_xmalloc_ptr
<gdb_byte
> data
;
182 cached_reg_t () = default;
183 cached_reg_t (cached_reg_t
&&rhs
) = default;
186 /* Buffer of registers. */
188 class reg_buffer
: public reg_buffer_common
191 reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
);
193 DISABLE_COPY_AND_ASSIGN (reg_buffer
);
195 /* Return regcache's architecture. */
196 gdbarch
*arch () const;
198 /* See gdbsupport/common-regcache.h. */
199 enum register_status
get_register_status (int regnum
) const override
;
201 /* See gdbsupport/common-regcache.h. */
202 void raw_collect (int regnum
, gdb::array_view
<gdb_byte
> dst
) const override
;
204 /* Deprecated overload of the above. */
205 void raw_collect (int regnum
, void *dst
) const;
207 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
208 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
209 If ADDR_LEN is greater than the register size, then the integer will be
210 sign or zero extended. If ADDR_LEN is smaller than the register size, then
211 the most significant bytes of the integer will be truncated. */
212 void raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
213 bool is_signed
) const;
215 /* Collect part of register REGNUM from this register buffer. Start at OFFSET
216 in register. The size is given by the size of DST. */
217 void raw_collect_part (int regnum
, int offset
,
218 gdb::array_view
<gdb_byte
> dst
) const;
220 /* Deprecated overload of the above. */
221 void raw_collect_part (int regnum
, int offset
, int len
, gdb_byte
*dst
) const
222 { raw_collect_part (regnum
, offset
, gdb::make_array_view (dst
, len
)); }
224 /* See gdbsupport/common-regcache.h. */
225 void raw_supply (int regnum
, gdb::array_view
<const gdb_byte
> src
) override
;
227 /* Deprecated overload of the above. */
228 void raw_supply (int regnum
, const void *src
);
230 void raw_supply (int regnum
, const reg_buffer
&src
)
231 { raw_supply (regnum
, src
.register_buffer (regnum
)); }
233 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored
234 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
235 If the register size is greater than ADDR_LEN, then the integer will be
236 sign or zero extended. If the register size is smaller than the integer,
237 then the most significant bytes of the integer will be truncated. */
238 void raw_supply_integer (int regnum
, const gdb_byte
*addr
, int addr_len
,
241 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
242 as calling raw_supply with NULL (which will set the state to
244 void raw_supply_zeroed (int regnum
);
246 /* Supply part of register REGNUM to this register buffer. Start at OFFSET in
247 the register. The size is given by the size of SRC. The rest of the
248 register left untouched. */
249 void raw_supply_part (int regnum
, int offset
,
250 gdb::array_view
<const gdb_byte
> src
);
252 void invalidate (int regnum
);
254 virtual ~reg_buffer () = default;
256 /* See gdbsupport/common-regcache.h. */
257 bool raw_compare (int regnum
, const void *buf
, int offset
) const override
;
260 /* Assert on the range of REGNUM. */
261 void assert_regnum (int regnum
) const;
263 int num_raw_registers () const;
265 /* Return a view on register REGNUM's buffer cache. */
266 template <typename ElemType
>
267 gdb::array_view
<ElemType
> register_buffer (int regnum
) const;
268 gdb::array_view
<const gdb_byte
> register_buffer (int regnum
) const;
269 gdb::array_view
<gdb_byte
> register_buffer (int regnum
);
271 /* Save a register cache. The set of registers saved into the
272 regcache determined by the save_reggroup. COOKED_READ returns
273 zero iff the register's value can't be returned. */
274 void save (register_read_ftype cooked_read
);
276 struct regcache_descr
*m_descr
;
279 /* The register buffers. */
280 std::unique_ptr
<gdb_byte
[]> m_registers
;
281 /* Register cache status. */
282 std::unique_ptr
<register_status
[]> m_register_status
;
284 friend class regcache
;
285 friend class detached_regcache
;
288 /* An abstract class which only has methods doing read. */
290 class readable_regcache
: public reg_buffer
293 readable_regcache (gdbarch
*gdbarch
, bool has_pseudo
)
294 : reg_buffer (gdbarch
, has_pseudo
)
297 /* Transfer a raw register [0..NUM_REGS) from core-gdb to this regcache,
298 return its value in *BUF and return its availability status. */
299 register_status
raw_read (int regnum
, gdb::array_view
<gdb_byte
> dst
);
301 /* Deprecated overload of the above. */
302 register_status
raw_read (int regnum
, gdb_byte
*dst
);
304 template<typename T
, typename
= RequireLongest
<T
>>
305 register_status
raw_read (int regnum
, T
*val
);
307 /* Partial transfer of raw registers. Return the status of the register. */
308 register_status
raw_read_part (int regnum
, int offset
,
309 gdb::array_view
<gdb_byte
> dst
);
311 /* Deprecated overload of the above. */
312 register_status
raw_read_part (int regnum
, int offset
, int len
,
314 { return raw_read_part (regnum
, offset
, gdb::make_array_view (dst
, len
)); }
316 /* Make certain that the register REGNUM is up-to-date. */
317 virtual void raw_update (int regnum
) = 0;
319 /* Transfer a raw register [0..NUM_REGS+NUM_PSEUDO_REGS) from core-gdb to
320 this regcache, return its value in DST and return its availability status. */
321 register_status
cooked_read (int regnum
, gdb::array_view
<gdb_byte
> dst
);
322 register_status
cooked_read (int regnum
, gdb_byte
*dst
);
324 template<typename T
, typename
= RequireLongest
<T
>>
325 register_status
cooked_read (int regnum
, T
*val
);
327 /* Partial transfer of a cooked register. */
328 register_status
cooked_read_part (int regnum
, int offset
,
329 gdb::array_view
<gdb_byte
> dst
);
331 /* Deprecated overload of the above. */
332 register_status
cooked_read_part (int regnum
, int offset
, int len
, gdb_byte
*src
)
333 { return cooked_read_part (regnum
, offset
, gdb::make_array_view (src
, len
)); }
335 /* Read register REGNUM from the regcache and return a new value. This
336 will call mark_value_bytes_unavailable as appropriate. */
337 struct value
*cooked_read_value (int regnum
);
341 /* Perform a partial register transfer using a read, modify, write
342 operation. Will fail if register is currently invalid. */
343 register_status
read_part (int regnum
, int offset
,
344 gdb::array_view
<gdb_byte
> dst
, bool is_raw
);
347 /* Buffer of registers, can be read and written. */
349 class detached_regcache
: public readable_regcache
352 detached_regcache (gdbarch
*gdbarch
, bool has_pseudo
)
353 : readable_regcache (gdbarch
, has_pseudo
)
356 void raw_update (int regnum
) override
359 DISABLE_COPY_AND_ASSIGN (detached_regcache
);
362 class readonly_detached_regcache
;
364 /* The register cache for storing raw register values. */
366 class regcache
: public detached_regcache
369 DISABLE_COPY_AND_ASSIGN (regcache
);
371 /* Restore 'this' regcache. The set of registers restored into
372 the regcache determined by the restore_reggroup.
373 Writes to regcache will go through to the target. SRC is a
374 read-only register cache. */
375 void restore (readonly_detached_regcache
*src
);
377 /* Update the value of raw register REGNUM (in the range [0..NUM_REGS)) and
378 transfer its value to core-gdb. */
380 void raw_write (int regnum
, gdb::array_view
<const gdb_byte
> src
);
382 /* Deprecated overload of the above. */
383 void raw_write (int regnum
, const gdb_byte
*src
);
385 template<typename T
, typename
= RequireLongest
<T
>>
386 void raw_write (int regnum
, T val
);
388 /* Transfer of pseudo-registers. */
389 void cooked_write (int regnum
, gdb::array_view
<const gdb_byte
> src
);
391 /* Deprecated overload of the above. */
392 void cooked_write (int regnum
, const gdb_byte
*src
);
394 template<typename T
, typename
= RequireLongest
<T
>>
395 void cooked_write (int regnum
, T val
);
397 void raw_update (int regnum
) override
;
399 /* Partial transfer of raw registers. Perform read, modify, write style
401 void raw_write_part (int regnum
, int offset
,
402 gdb::array_view
<const gdb_byte
> src
);
404 /* Deprecated overload of the above. */
405 void raw_write_part (int regnum
, int offset
, int len
, const gdb_byte
*src
)
406 { raw_write_part (regnum
, offset
, gdb::make_array_view (src
, len
)); }
408 /* Partial transfer of a cooked register. Perform read, modify, write style
410 void cooked_write_part (int regnum
, int offset
,
411 gdb::array_view
<const gdb_byte
> src
);
413 /* Deprecated overload of the above. */
414 void cooked_write_part (int regnum
, int offset
, int len
, const gdb_byte
*src
)
415 { cooked_write_part (regnum
, offset
, gdb::make_array_view (src
, len
)); }
417 /* Transfer a set of registers (as described by REGSET) between
418 REGCACHE and BUF. If REGNUM == -1, transfer all registers
419 belonging to the regset, otherwise just the register numbered
420 REGNUM. The REGSET's 'regmap' field must point to an array of
421 'struct regcache_map_entry'. The valid register numbers in each
422 entry in 'struct regcache_map_entry' are offset by REGBASE. */
424 void supply_regset (const struct regset
*regset
, int regbase
,
425 int regnum
, const void *buf
, size_t size
);
427 void collect_regset (const struct regset
*regset
, int regbase
, int regnum
,
428 void *buf
, size_t size
) const;
430 /* Same as the above, but with REGBASE == 0. */
432 void supply_regset (const struct regset
*regset
,
433 int regnum
, const void *buf
, size_t size
)
435 supply_regset (regset
, 0, regnum
, buf
, size
);
438 void collect_regset (const struct regset
*regset
, int regnum
,
439 void *buf
, size_t size
) const
441 collect_regset (regset
, 0, regnum
, buf
, size
);
444 /* Return REGCACHE's ptid. */
448 gdb_assert (m_ptid
!= minus_one_ptid
);
453 void set_ptid (const ptid_t ptid
)
458 /* Return a string with the contents of a register, suitable for debug output. */
459 std::string
register_debug_string (int regno
);
462 regcache (inferior
*inf_for_target_calls
, gdbarch
*gdbarch
);
466 /* Helper function for transfer_regset. Copies across a single register. */
467 void transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
468 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
469 int slot_size
, int offs
) const;
471 /* Transfer a single or all registers belonging to a certain register
472 set to or from a buffer. This is the main worker function for
473 regcache_supply_regset and regcache_collect_regset. */
474 void transfer_regset (const struct regset
*regset
, int regbase
,
475 struct regcache
*out_regcache
,
476 int regnum
, const gdb_byte
*in_buf
,
477 gdb_byte
*out_buf
, size_t size
) const;
479 /* Perform a partial register transfer using a read, modify, write
481 register_status
write_part (int regnum
, int offset
,
482 gdb::array_view
<const gdb_byte
> src
,
485 /* The inferior to switch to, to make target calls.
487 This may not be the inferior of thread M_PTID. For instance, this
488 regcache might be for a fork child we are about to detach, so there will
489 never be an inferior for that thread / process. Nevertheless, we need to
490 be able to switch to the target stack that can handle register reads /
491 writes for this regcache, and that's what this inferior is for. */
492 inferior
*m_inf_for_target_calls
;
494 /* If this is a read-write cache, which thread's registers is
498 friend regcache
*get_thread_arch_regcache (inferior
*inf_for_target_calls
,
499 ptid_t ptid
, gdbarch
*gdbarch
);
502 using regcache_up
= std::unique_ptr
<regcache
>;
504 class readonly_detached_regcache
: public readable_regcache
507 readonly_detached_regcache (regcache
&src
);
509 /* Create a readonly regcache by getting contents from COOKED_READ. */
511 readonly_detached_regcache (gdbarch
*gdbarch
, register_read_ftype cooked_read
)
512 : readable_regcache (gdbarch
, true)
517 DISABLE_COPY_AND_ASSIGN (readonly_detached_regcache
);
519 void raw_update (int regnum
) override
523 extern void registers_changed (void);
524 extern void registers_changed_ptid (process_stratum_target
*target
,
527 /* Indicate that registers of THREAD may have changed, so invalidate
529 extern void registers_changed_thread (thread_info
*thread
);
531 /* An abstract base class for register dump. */
536 void dump (ui_file
*file
);
537 virtual ~register_dump () = default;
540 register_dump (gdbarch
*arch
)
544 /* Dump the register REGNUM contents. If REGNUM is -1, print the
546 virtual void dump_reg (ui_file
*file
, int regnum
) = 0;
551 #endif /* REGCACHE_H */