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/>. */
20 #ifndef GDB_REGCACHE_H
21 #define GDB_REGCACHE_H
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
;
36 extern struct regcache
*get_thread_regcache (process_stratum_target
*target
,
39 /* Get the regcache of THREAD. */
40 extern struct regcache
*get_thread_regcache (thread_info
*thread
);
42 extern regcache
*get_thread_arch_regcache (inferior
*inf_for_target_calls
,
43 ptid_t ptid
, gdbarch
*arch
);
45 extern enum register_status
46 regcache_raw_read_signed (struct regcache
*regcache
,
47 int regnum
, LONGEST
*val
);
49 extern void regcache_raw_write_signed (struct regcache
*regcache
,
50 int regnum
, LONGEST val
);
51 extern void regcache_raw_write_unsigned (struct regcache
*regcache
,
52 int regnum
, ULONGEST val
);
54 /* Return the register's value in signed or throw if it's not
57 extern LONGEST
regcache_raw_get_signed (struct regcache
*regcache
,
60 /* Read a register as a signed/unsigned quantity. */
61 extern enum register_status
62 regcache_cooked_read_signed (struct regcache
*regcache
,
63 int regnum
, LONGEST
*val
);
64 extern enum register_status
65 regcache_cooked_read_unsigned (struct regcache
*regcache
,
66 int regnum
, ULONGEST
*val
);
67 extern void regcache_cooked_write_signed (struct regcache
*regcache
,
68 int regnum
, LONGEST val
);
69 extern void regcache_cooked_write_unsigned (struct regcache
*regcache
,
70 int regnum
, ULONGEST val
);
72 /* Special routines to read/write the PC. */
74 /* For regcache_read_pc see gdbsupport/common-regcache.h. */
75 extern void regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
);
77 /* Mapping between register numbers and offsets in a buffer, for use
78 in the '*regset' functions below and with traditional frame caches.
79 In an array of 'regcache_map_entry' each element is interpreted
82 - If 'regno' is a register number: Map register 'regno' to the
83 current offset (starting with 0) and increase the current offset
84 by 'size' (or the register's size, if 'size' is zero). Repeat
85 this with consecutive register numbers up to 'regno+count-1'.
87 For each described register, if 'size' is larger than the
88 register's size, the register's value is assumed to be stored in
89 the first N (where N is the register size) bytes at the current
90 offset. The remaining 'size' - N bytes are filled with zeroes by
91 'regcache_collect_regset' and ignored by other consumers.
93 If 'size' is smaller than the register's size, only the first
94 'size' bytes of a register's value are assumed to be stored at
95 the current offset. 'regcache_collect_regset' copies the first
96 'size' bytes of a register's value to the output buffer.
97 'regcache_supply_regset' copies the bytes from the input buffer
98 into the first 'size' bytes of the register's value leaving the
99 remaining bytes of the register's value unchanged. Frame caches
100 read the 'size' bytes from the stack frame and zero extend them
101 to generate the register's value.
103 - If 'regno' is REGCACHE_MAP_SKIP: Add 'count*size' to the current
106 - If count=0: End of the map. */
108 struct regcache_map_entry
115 /* Special value for the 'regno' field in the struct above. */
119 REGCACHE_MAP_SKIP
= -1,
122 /* Calculate and return the total size of all the registers in a
123 regcache_map_entry. */
126 regcache_map_entry_size (const struct regcache_map_entry
*map
)
129 for (int i
= 0; map
[i
].count
!= 0; i
++)
130 size
+= (map
[i
].count
* map
[i
].size
);
134 /* Transfer a set of registers (as described by REGSET) between
135 REGCACHE and BUF. If REGNUM == -1, transfer all registers
136 belonging to the regset, otherwise just the register numbered
137 REGNUM. The REGSET's 'regmap' field must point to an array of
138 'struct regcache_map_entry'.
140 These functions are suitable for the 'regset_supply' and
141 'regset_collect' fields in a regset structure. */
143 extern void regcache_supply_regset (const struct regset
*regset
,
144 struct regcache
*regcache
,
145 int regnum
, const void *buf
,
147 extern void regcache_collect_regset (const struct regset
*regset
,
148 const struct regcache
*regcache
,
149 int regnum
, void *buf
, size_t size
);
152 /* Return true if a set of registers contains the value of the
153 register numbered REGNUM. The size of the set of registers is
154 given in SIZE, and the layout of the set of registers is described
157 extern bool regcache_map_supplies (const struct regcache_map_entry
*map
,
158 int regnum
, struct gdbarch
*gdbarch
,
161 /* The type of a register. This function is slightly more efficient
162 then its gdbarch vector counterpart since it returns a precomputed
163 value stored in a table. */
165 extern struct type
*register_type (struct gdbarch
*gdbarch
, int regnum
);
168 /* Return the size of register REGNUM. All registers should have only
171 extern int register_size (struct gdbarch
*gdbarch
, int regnum
);
173 using register_read_ftype
174 = gdb::function_view
<register_status (int, gdb::array_view
<gdb_byte
>)>;
176 /* A (register_number, register_value) pair. */
181 gdb::byte_vector data
;
184 /* Buffer of registers. */
186 class reg_buffer
: public reg_buffer_common
189 reg_buffer (gdbarch
*gdbarch
, bool has_pseudo
);
191 DISABLE_COPY_AND_ASSIGN (reg_buffer
);
193 /* Return regcache's architecture. */
194 gdbarch
*arch () const;
196 /* See gdbsupport/common-regcache.h. */
197 enum register_status
get_register_status (int regnum
) const override
;
199 /* See gdbsupport/common-regcache.h. */
200 void raw_collect (int regnum
, gdb::array_view
<gdb_byte
> dst
) const override
;
202 /* Deprecated overload of the above. */
203 void raw_collect (int regnum
, void *dst
) const;
205 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
206 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
207 If ADDR_LEN is greater than the register size, then the integer will be
208 sign or zero extended. If ADDR_LEN is smaller than the register size, then
209 the most significant bytes of the integer will be truncated. */
210 void raw_collect_integer (int regnum
, gdb_byte
*addr
, int addr_len
,
211 bool is_signed
) const;
213 /* Collect part of register REGNUM from this register buffer. Start at OFFSET
214 in register. The size is given by the size of DST. */
215 void raw_collect_part (int regnum
, int offset
,
216 gdb::array_view
<gdb_byte
> dst
) const;
218 /* Deprecated overload of the above. */
219 void raw_collect_part (int regnum
, int offset
, int len
, gdb_byte
*dst
) const
220 { raw_collect_part (regnum
, offset
, gdb::make_array_view (dst
, len
)); }
222 /* See gdbsupport/common-regcache.h. */
223 void raw_supply (int regnum
, gdb::array_view
<const gdb_byte
> src
) override
;
225 /* Deprecated overload of the above. */
226 void raw_supply (int regnum
, const void *src
);
228 void raw_supply (int regnum
, const reg_buffer
&src
)
229 { raw_supply (regnum
, src
.register_buffer (regnum
)); }
231 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored
232 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
233 If the register size is greater than ADDR_LEN, then the integer will be
234 sign or zero extended. If the register size is smaller than the integer,
235 then the most significant bytes of the integer will be truncated. */
236 void raw_supply_integer (int regnum
, const gdb_byte
*addr
, int addr_len
,
239 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
240 as calling raw_supply with NULL (which will set the state to
242 void raw_supply_zeroed (int regnum
);
244 /* See gdbsupport/common-regcache.h. */
245 void raw_supply_part_zeroed (int regnum
, int offset
, size_t size
) override
;
247 /* Supply part of register REGNUM to this register buffer. Start at OFFSET in
248 the register. The size is given by the size of SRC. The rest of the
249 register left untouched. */
250 void raw_supply_part (int regnum
, int offset
,
251 gdb::array_view
<const gdb_byte
> src
);
253 void invalidate (int regnum
);
255 virtual ~reg_buffer () = default;
257 /* See gdbsupport/common-regcache.h. */
258 bool raw_compare (int regnum
, const void *buf
, int offset
) const override
;
260 /* See gdbsupport/common-regcache.h. */
261 int register_size (int regnum
) const override
;
264 /* Assert on the range of REGNUM. */
265 void assert_regnum (int regnum
) const;
267 int num_raw_registers () const;
269 /* Return a view on register REGNUM's buffer cache. */
270 template <typename ElemType
>
271 gdb::array_view
<ElemType
> register_buffer (int regnum
) const;
272 gdb::array_view
<const gdb_byte
> register_buffer (int regnum
) const;
273 gdb::array_view
<gdb_byte
> register_buffer (int regnum
);
275 /* Save a register cache. The set of registers saved into the
276 regcache determined by the save_reggroup. COOKED_READ returns
277 zero iff the register's value can't be returned. */
278 void save (register_read_ftype cooked_read
);
280 struct regcache_descr
*m_descr
;
283 /* The register buffers. */
284 std::unique_ptr
<gdb_byte
[]> m_registers
;
285 /* Register cache status. */
286 std::unique_ptr
<register_status
[]> m_register_status
;
288 friend class regcache
;
289 friend class detached_regcache
;
292 /* An abstract class which only has methods doing read. */
294 class readable_regcache
: public reg_buffer
297 readable_regcache (gdbarch
*gdbarch
, bool has_pseudo
)
298 : reg_buffer (gdbarch
, has_pseudo
)
301 /* Transfer a raw register [0..NUM_REGS) from core-gdb to this regcache,
302 return its value in *BUF and return its availability status. */
303 register_status
raw_read (int regnum
, gdb::array_view
<gdb_byte
> dst
);
305 /* Deprecated overload of the above. */
306 register_status
raw_read (int regnum
, gdb_byte
*dst
);
308 template<typename T
, typename
= RequireLongest
<T
>>
309 register_status
raw_read (int regnum
, T
*val
);
311 /* Partial transfer of raw registers. Return the status of the register. */
312 register_status
raw_read_part (int regnum
, int offset
,
313 gdb::array_view
<gdb_byte
> dst
);
315 /* Deprecated overload of the above. */
316 register_status
raw_read_part (int regnum
, int offset
, int len
,
318 { return raw_read_part (regnum
, offset
, gdb::make_array_view (dst
, len
)); }
320 /* Make certain that the register REGNUM is up-to-date. */
321 virtual void raw_update (int regnum
) = 0;
323 /* Transfer a raw register [0..NUM_REGS+NUM_PSEUDO_REGS) from core-gdb to
324 this regcache, return its value in DST and return its availability status. */
325 register_status
cooked_read (int regnum
, gdb::array_view
<gdb_byte
> dst
);
326 register_status
cooked_read (int regnum
, gdb_byte
*dst
);
328 template<typename T
, typename
= RequireLongest
<T
>>
329 register_status
cooked_read (int regnum
, T
*val
);
331 /* Partial transfer of a cooked register. */
332 register_status
cooked_read_part (int regnum
, int offset
,
333 gdb::array_view
<gdb_byte
> dst
);
335 /* Deprecated overload of the above. */
336 register_status
cooked_read_part (int regnum
, int offset
, int len
, gdb_byte
*src
)
337 { return cooked_read_part (regnum
, offset
, gdb::make_array_view (src
, len
)); }
339 /* Read register REGNUM from the regcache and return a new value. This
340 will call mark_value_bytes_unavailable as appropriate. */
341 struct value
*cooked_read_value (int regnum
);
345 /* Perform a partial register transfer using a read, modify, write
346 operation. Will fail if register is currently invalid. */
347 register_status
read_part (int regnum
, int offset
,
348 gdb::array_view
<gdb_byte
> dst
, bool is_raw
);
351 /* Buffer of registers, can be read and written. */
353 class detached_regcache
: public readable_regcache
356 detached_regcache (gdbarch
*gdbarch
, bool has_pseudo
)
357 : readable_regcache (gdbarch
, has_pseudo
)
360 void raw_update (int regnum
) override
363 DISABLE_COPY_AND_ASSIGN (detached_regcache
);
366 class readonly_detached_regcache
;
368 /* The register cache for storing raw register values. */
370 class regcache
: public detached_regcache
373 DISABLE_COPY_AND_ASSIGN (regcache
);
375 /* Restore 'this' regcache. The set of registers restored into
376 the regcache determined by the restore_reggroup.
377 Writes to regcache will go through to the target. SRC is a
378 read-only register cache. */
379 void restore (readonly_detached_regcache
*src
);
381 /* Update the value of raw register REGNUM (in the range [0..NUM_REGS)) and
382 transfer its value to core-gdb. */
384 void raw_write (int regnum
, gdb::array_view
<const gdb_byte
> src
);
386 /* Deprecated overload of the above. */
387 void raw_write (int regnum
, const gdb_byte
*src
);
389 template<typename T
, typename
= RequireLongest
<T
>>
390 void raw_write (int regnum
, T val
);
392 /* Transfer of pseudo-registers. */
393 void cooked_write (int regnum
, gdb::array_view
<const gdb_byte
> src
);
395 /* Deprecated overload of the above. */
396 void cooked_write (int regnum
, const gdb_byte
*src
);
398 template<typename T
, typename
= RequireLongest
<T
>>
399 void cooked_write (int regnum
, T val
);
401 void raw_update (int regnum
) override
;
403 /* Partial transfer of raw registers. Perform read, modify, write style
405 void raw_write_part (int regnum
, int offset
,
406 gdb::array_view
<const gdb_byte
> src
);
408 /* Deprecated overload of the above. */
409 void raw_write_part (int regnum
, int offset
, int len
, const gdb_byte
*src
)
410 { raw_write_part (regnum
, offset
, gdb::make_array_view (src
, len
)); }
412 /* Partial transfer of a cooked register. Perform read, modify, write style
414 void cooked_write_part (int regnum
, int offset
,
415 gdb::array_view
<const gdb_byte
> src
);
417 /* Deprecated overload of the above. */
418 void cooked_write_part (int regnum
, int offset
, int len
, const gdb_byte
*src
)
419 { cooked_write_part (regnum
, offset
, gdb::make_array_view (src
, len
)); }
421 /* Transfer a set of registers (as described by REGSET) between
422 REGCACHE and BUF. If REGNUM == -1, transfer all registers
423 belonging to the regset, otherwise just the register numbered
424 REGNUM. The REGSET's 'regmap' field must point to an array of
425 'struct regcache_map_entry'. The valid register numbers in each
426 entry in 'struct regcache_map_entry' are offset by REGBASE. */
428 void supply_regset (const struct regset
*regset
, int regbase
,
429 int regnum
, const void *buf
, size_t size
);
431 void collect_regset (const struct regset
*regset
, int regbase
, int regnum
,
432 void *buf
, size_t size
) const;
434 /* Same as the above, but with REGBASE == 0. */
436 void supply_regset (const struct regset
*regset
,
437 int regnum
, const void *buf
, size_t size
)
439 supply_regset (regset
, 0, regnum
, buf
, size
);
442 void collect_regset (const struct regset
*regset
, int regnum
,
443 void *buf
, size_t size
) const
445 collect_regset (regset
, 0, regnum
, buf
, size
);
448 /* Return REGCACHE's ptid. */
452 gdb_assert (m_ptid
!= minus_one_ptid
);
457 void set_ptid (const ptid_t ptid
)
462 /* Return a string with the contents of a register, suitable for debug output. */
463 std::string
register_debug_string (int regno
);
466 regcache (inferior
*inf_for_target_calls
, gdbarch
*gdbarch
);
470 /* Helper function for transfer_regset. Copies across a single register. */
471 void transfer_regset_register (struct regcache
*out_regcache
, int regnum
,
472 const gdb_byte
*in_buf
, gdb_byte
*out_buf
,
473 int slot_size
, int offs
) const;
475 /* Transfer a single or all registers belonging to a certain register
476 set to or from a buffer. This is the main worker function for
477 regcache_supply_regset and regcache_collect_regset. */
478 void transfer_regset (const struct regset
*regset
, int regbase
,
479 struct regcache
*out_regcache
,
480 int regnum
, const gdb_byte
*in_buf
,
481 gdb_byte
*out_buf
, size_t size
) const;
483 /* Perform a partial register transfer using a read, modify, write
485 register_status
write_part (int regnum
, int offset
,
486 gdb::array_view
<const gdb_byte
> src
,
489 /* The inferior to switch to, to make target calls.
491 This may not be the inferior of thread M_PTID. For instance, this
492 regcache might be for a fork child we are about to detach, so there will
493 never be an inferior for that thread / process. Nevertheless, we need to
494 be able to switch to the target stack that can handle register reads /
495 writes for this regcache, and that's what this inferior is for. */
496 inferior
*m_inf_for_target_calls
;
498 /* If this is a read-write cache, which thread's registers is
502 friend regcache
*get_thread_arch_regcache (inferior
*inf_for_target_calls
,
503 ptid_t ptid
, gdbarch
*gdbarch
);
506 using regcache_up
= std::unique_ptr
<regcache
>;
508 class readonly_detached_regcache
: public readable_regcache
511 readonly_detached_regcache (regcache
&src
);
513 /* Create a readonly regcache by getting contents from COOKED_READ. */
515 readonly_detached_regcache (gdbarch
*gdbarch
, register_read_ftype cooked_read
)
516 : readable_regcache (gdbarch
, true)
521 DISABLE_COPY_AND_ASSIGN (readonly_detached_regcache
);
523 void raw_update (int regnum
) override
527 extern void registers_changed (void);
528 extern void registers_changed_ptid (process_stratum_target
*target
,
531 /* Indicate that registers of THREAD may have changed, so invalidate
533 extern void registers_changed_thread (thread_info
*thread
);
535 /* An abstract base class for register dump. */
540 void dump (ui_out
*out
, const char *name
);
541 virtual ~register_dump () = default;
544 register_dump (gdbarch
*arch
)
548 /* Number of additional table headers. */
549 virtual int num_additional_headers () = 0;
551 /* Add the additional headers to OUT. */
552 virtual void additional_headers (ui_out
*out
) = 0;
554 /* Dump the register REGNUM contents. */
555 virtual void dump_reg (ui_out
*out
, int regnum
) = 0;
560 #endif /* GDB_REGCACHE_H */