Automatic date update in version.in
[binutils-gdb.git] / gdb / regcache.h
blob1d049fe7ae8de794197076f9ede74fe8b9ab9ff4
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 REGCACHE_H
21 #define REGCACHE_H
23 #include "gdbsupport/array-view.h"
24 #include "gdbsupport/common-regcache.h"
25 #include "gdbsupport/function-view.h"
27 struct regcache;
28 struct regset;
29 struct gdbarch;
30 class thread_info;
31 struct process_stratum_target;
32 struct inferior;
34 extern struct regcache *get_thread_regcache (process_stratum_target *target,
35 ptid_t ptid);
37 /* Get the regcache of THREAD. */
38 extern struct regcache *get_thread_regcache (thread_info *thread);
40 extern regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
41 ptid_t ptid, gdbarch *arch);
43 extern enum register_status
44 regcache_raw_read_signed (struct regcache *regcache,
45 int regnum, LONGEST *val);
47 extern void regcache_raw_write_signed (struct regcache *regcache,
48 int regnum, LONGEST val);
49 extern void regcache_raw_write_unsigned (struct regcache *regcache,
50 int regnum, ULONGEST val);
52 /* Return the register's value in signed or throw if it's not
53 available. */
55 extern LONGEST regcache_raw_get_signed (struct regcache *regcache,
56 int regnum);
58 /* Read a register as a signed/unsigned quantity. */
59 extern enum register_status
60 regcache_cooked_read_signed (struct regcache *regcache,
61 int regnum, LONGEST *val);
62 extern enum register_status
63 regcache_cooked_read_unsigned (struct regcache *regcache,
64 int regnum, ULONGEST *val);
65 extern void regcache_cooked_write_signed (struct regcache *regcache,
66 int regnum, LONGEST val);
67 extern void regcache_cooked_write_unsigned (struct regcache *regcache,
68 int regnum, ULONGEST val);
70 /* Special routines to read/write the PC. */
72 /* For regcache_read_pc see gdbsupport/common-regcache.h. */
73 extern void regcache_write_pc (struct regcache *regcache, CORE_ADDR pc);
75 /* Mapping between register numbers and offsets in a buffer, for use
76 in the '*regset' functions below and with traditional frame caches.
77 In an array of 'regcache_map_entry' each element is interpreted
78 like follows:
80 - If 'regno' is a register number: Map register 'regno' to the
81 current offset (starting with 0) and increase the current offset
82 by 'size' (or the register's size, if 'size' is zero). Repeat
83 this with consecutive register numbers up to 'regno+count-1'.
85 For each described register, if 'size' is larger than the
86 register's size, the register's value is assumed to be stored in
87 the first N (where N is the register size) bytes at the current
88 offset. The remaining 'size' - N bytes are filled with zeroes by
89 'regcache_collect_regset' and ignored by other consumers.
91 If 'size' is smaller than the register's size, only the first
92 'size' bytes of a register's value are assumed to be stored at
93 the current offset. 'regcache_collect_regset' copies the first
94 'size' bytes of a register's value to the output buffer.
95 'regcache_supply_regset' copies the bytes from the input buffer
96 into the first 'size' bytes of the register's value leaving the
97 remaining bytes of the register's value unchanged. Frame caches
98 read the 'size' bytes from the stack frame and zero extend them
99 to generate the register's value.
101 - If 'regno' is REGCACHE_MAP_SKIP: Add 'count*size' to the current
102 offset.
104 - If count=0: End of the map. */
106 struct regcache_map_entry
108 int count;
109 int regno;
110 int size;
113 /* Special value for the 'regno' field in the struct above. */
115 enum
117 REGCACHE_MAP_SKIP = -1,
120 /* Calculate and return the total size of all the registers in a
121 regcache_map_entry. */
123 static inline int
124 regcache_map_entry_size (const struct regcache_map_entry *map)
126 int size = 0;
127 for (int i = 0; map[i].count != 0; i++)
128 size += (map[i].count * map[i].size);
129 return size;
132 /* Transfer a set of registers (as described by REGSET) between
133 REGCACHE and BUF. If REGNUM == -1, transfer all registers
134 belonging to the regset, otherwise just the register numbered
135 REGNUM. The REGSET's 'regmap' field must point to an array of
136 'struct regcache_map_entry'.
138 These functions are suitable for the 'regset_supply' and
139 'regset_collect' fields in a regset structure. */
141 extern void regcache_supply_regset (const struct regset *regset,
142 struct regcache *regcache,
143 int regnum, const void *buf,
144 size_t size);
145 extern void regcache_collect_regset (const struct regset *regset,
146 const struct regcache *regcache,
147 int regnum, void *buf, size_t size);
150 /* Return true if a set of registers contains the value of the
151 register numbered REGNUM. The size of the set of registers is
152 given in SIZE, and the layout of the set of registers is described
153 by MAP. */
155 extern bool regcache_map_supplies (const struct regcache_map_entry *map,
156 int regnum, struct gdbarch *gdbarch,
157 size_t size);
159 /* The type of a register. This function is slightly more efficient
160 then its gdbarch vector counterpart since it returns a precomputed
161 value stored in a table. */
163 extern struct type *register_type (struct gdbarch *gdbarch, int regnum);
166 /* Return the size of register REGNUM. All registers should have only
167 one size. */
169 extern int register_size (struct gdbarch *gdbarch, int regnum);
171 using register_read_ftype
172 = gdb::function_view<register_status (int, gdb::array_view<gdb_byte>)>;
174 /* A (register_number, register_value) pair. */
176 struct cached_reg_t
178 int num;
179 gdb::unique_xmalloc_ptr<gdb_byte> data;
181 cached_reg_t () = default;
182 cached_reg_t (cached_reg_t &&rhs) = default;
185 /* Buffer of registers. */
187 class reg_buffer : public reg_buffer_common
189 public:
190 reg_buffer (gdbarch *gdbarch, bool has_pseudo);
192 DISABLE_COPY_AND_ASSIGN (reg_buffer);
194 /* Return regcache's architecture. */
195 gdbarch *arch () const;
197 /* See gdbsupport/common-regcache.h. */
198 enum register_status get_register_status (int regnum) const override;
200 /* See gdbsupport/common-regcache.h. */
201 void raw_collect (int regnum, gdb::array_view<gdb_byte> dst) const override;
203 /* Deprecated overload of the above. */
204 void raw_collect (int regnum, void *dst) const;
206 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
207 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
208 If ADDR_LEN is greater than the register size, then the integer will be
209 sign or zero extended. If ADDR_LEN is smaller than the register size, then
210 the most significant bytes of the integer will be truncated. */
211 void raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
212 bool is_signed) const;
214 /* Collect part of register REGNUM from this register buffer. Start at OFFSET
215 in register. The size is given by the size of DST. */
216 void raw_collect_part (int regnum, int offset,
217 gdb::array_view<gdb_byte> dst) const;
219 /* Deprecated overload of the above. */
220 void raw_collect_part (int regnum, int offset, int len, gdb_byte *dst) const
221 { raw_collect_part (regnum, offset, gdb::make_array_view (dst, len)); }
223 /* See gdbsupport/common-regcache.h. */
224 void raw_supply (int regnum, gdb::array_view<const gdb_byte> src) override;
226 /* Deprecated overload of the above. */
227 void raw_supply (int regnum, const void *src);
229 void raw_supply (int regnum, const reg_buffer &src)
230 { raw_supply (regnum, src.register_buffer (regnum)); }
232 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored
233 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
234 If the register size is greater than ADDR_LEN, then the integer will be
235 sign or zero extended. If the register size is smaller than the integer,
236 then the most significant bytes of the integer will be truncated. */
237 void raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
238 bool is_signed);
240 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
241 as calling raw_supply with NULL (which will set the state to
242 unavailable). */
243 void raw_supply_zeroed (int regnum);
245 /* Supply part of register REGNUM to this register buffer. Start at OFFSET in
246 the register. The size is given by the size of SRC. The rest of the
247 register left untouched. */
248 void raw_supply_part (int regnum, int offset,
249 gdb::array_view<const gdb_byte> src);
251 void invalidate (int regnum);
253 virtual ~reg_buffer () = default;
255 /* See gdbsupport/common-regcache.h. */
256 bool raw_compare (int regnum, const void *buf, int offset) const override;
258 protected:
259 /* Assert on the range of REGNUM. */
260 void assert_regnum (int regnum) const;
262 int num_raw_registers () const;
264 /* Return a view on register REGNUM's buffer cache. */
265 template <typename ElemType>
266 gdb::array_view<ElemType> register_buffer (int regnum) const;
267 gdb::array_view<const gdb_byte> register_buffer (int regnum) const;
268 gdb::array_view<gdb_byte> register_buffer (int regnum);
270 /* Save a register cache. The set of registers saved into the
271 regcache determined by the save_reggroup. COOKED_READ returns
272 zero iff the register's value can't be returned. */
273 void save (register_read_ftype cooked_read);
275 struct regcache_descr *m_descr;
277 bool m_has_pseudo;
278 /* The register buffers. */
279 std::unique_ptr<gdb_byte[]> m_registers;
280 /* Register cache status. */
281 std::unique_ptr<register_status[]> m_register_status;
283 friend class regcache;
284 friend class detached_regcache;
287 /* An abstract class which only has methods doing read. */
289 class readable_regcache : public reg_buffer
291 public:
292 readable_regcache (gdbarch *gdbarch, bool has_pseudo)
293 : reg_buffer (gdbarch, has_pseudo)
296 /* Transfer a raw register [0..NUM_REGS) from core-gdb to this regcache,
297 return its value in *BUF and return its availability status. */
298 register_status raw_read (int regnum, gdb::array_view<gdb_byte> dst);
300 /* Deprecated overload of the above. */
301 register_status raw_read (int regnum, gdb_byte *dst);
303 template<typename T, typename = RequireLongest<T>>
304 register_status raw_read (int regnum, T *val);
306 /* Partial transfer of raw registers. Return the status of the register. */
307 register_status raw_read_part (int regnum, int offset,
308 gdb::array_view<gdb_byte> dst);
310 /* Deprecated overload of the above. */
311 register_status raw_read_part (int regnum, int offset, int len,
312 gdb_byte *dst)
313 { return raw_read_part (regnum, offset, gdb::make_array_view (dst, len)); }
315 /* Make certain that the register REGNUM is up-to-date. */
316 virtual void raw_update (int regnum) = 0;
318 /* Transfer a raw register [0..NUM_REGS+NUM_PSEUDO_REGS) from core-gdb to
319 this regcache, return its value in DST and return its availability status. */
320 register_status cooked_read (int regnum, gdb::array_view<gdb_byte> dst);
321 register_status cooked_read (int regnum, gdb_byte *dst);
323 template<typename T, typename = RequireLongest<T>>
324 register_status cooked_read (int regnum, T *val);
326 /* Partial transfer of a cooked register. */
327 register_status cooked_read_part (int regnum, int offset,
328 gdb::array_view<gdb_byte> dst);
330 /* Deprecated overload of the above. */
331 register_status cooked_read_part (int regnum, int offset, int len, gdb_byte *src)
332 { return cooked_read_part (regnum, offset, gdb::make_array_view (src, len)); }
334 /* Read register REGNUM from the regcache and return a new value. This
335 will call mark_value_bytes_unavailable as appropriate. */
336 struct value *cooked_read_value (int regnum);
338 protected:
340 /* Perform a partial register transfer using a read, modify, write
341 operation. Will fail if register is currently invalid. */
342 register_status read_part (int regnum, int offset,
343 gdb::array_view<gdb_byte> dst, bool is_raw);
346 /* Buffer of registers, can be read and written. */
348 class detached_regcache : public readable_regcache
350 public:
351 detached_regcache (gdbarch *gdbarch, bool has_pseudo)
352 : readable_regcache (gdbarch, has_pseudo)
355 void raw_update (int regnum) override
358 DISABLE_COPY_AND_ASSIGN (detached_regcache);
361 class readonly_detached_regcache;
363 /* The register cache for storing raw register values. */
365 class regcache : public detached_regcache
367 public:
368 DISABLE_COPY_AND_ASSIGN (regcache);
370 /* Restore 'this' regcache. The set of registers restored into
371 the regcache determined by the restore_reggroup.
372 Writes to regcache will go through to the target. SRC is a
373 read-only register cache. */
374 void restore (readonly_detached_regcache *src);
376 /* Update the value of raw register REGNUM (in the range [0..NUM_REGS)) and
377 transfer its value to core-gdb. */
379 void raw_write (int regnum, gdb::array_view<const gdb_byte> src);
381 /* Deprecated overload of the above. */
382 void raw_write (int regnum, const gdb_byte *src);
384 template<typename T, typename = RequireLongest<T>>
385 void raw_write (int regnum, T val);
387 /* Transfer of pseudo-registers. */
388 void cooked_write (int regnum, gdb::array_view<const gdb_byte> src);
390 /* Deprecated overload of the above. */
391 void cooked_write (int regnum, const gdb_byte *src);
393 template<typename T, typename = RequireLongest<T>>
394 void cooked_write (int regnum, T val);
396 void raw_update (int regnum) override;
398 /* Partial transfer of raw registers. Perform read, modify, write style
399 operations. */
400 void raw_write_part (int regnum, int offset,
401 gdb::array_view<const gdb_byte> src);
403 /* Deprecated overload of the above. */
404 void raw_write_part (int regnum, int offset, int len, const gdb_byte *src)
405 { raw_write_part (regnum, offset, gdb::make_array_view (src, len)); }
407 /* Partial transfer of a cooked register. Perform read, modify, write style
408 operations. */
409 void cooked_write_part (int regnum, int offset,
410 gdb::array_view<const gdb_byte> src);
412 /* Deprecated overload of the above. */
413 void cooked_write_part (int regnum, int offset, int len, const gdb_byte *src)
414 { cooked_write_part (regnum, offset, gdb::make_array_view (src, len)); }
416 /* Transfer a set of registers (as described by REGSET) between
417 REGCACHE and BUF. If REGNUM == -1, transfer all registers
418 belonging to the regset, otherwise just the register numbered
419 REGNUM. The REGSET's 'regmap' field must point to an array of
420 'struct regcache_map_entry'. The valid register numbers in each
421 entry in 'struct regcache_map_entry' are offset by REGBASE. */
423 void supply_regset (const struct regset *regset, int regbase,
424 int regnum, const void *buf, size_t size);
426 void collect_regset (const struct regset *regset, int regbase, int regnum,
427 void *buf, size_t size) const;
429 /* Same as the above, but with REGBASE == 0. */
431 void supply_regset (const struct regset *regset,
432 int regnum, const void *buf, size_t size)
434 supply_regset (regset, 0, regnum, buf, size);
437 void collect_regset (const struct regset *regset, int regnum,
438 void *buf, size_t size) const
440 collect_regset (regset, 0, regnum, buf, size);
443 /* Return REGCACHE's ptid. */
445 ptid_t ptid () const
447 gdb_assert (m_ptid != minus_one_ptid);
449 return m_ptid;
452 void set_ptid (const ptid_t ptid)
454 this->m_ptid = ptid;
457 /* Return a string with the contents of a register, suitable for debug output. */
458 std::string register_debug_string (int regno);
460 protected:
461 regcache (inferior *inf_for_target_calls, gdbarch *gdbarch);
463 private:
465 /* Helper function for transfer_regset. Copies across a single register. */
466 void transfer_regset_register (struct regcache *out_regcache, int regnum,
467 const gdb_byte *in_buf, gdb_byte *out_buf,
468 int slot_size, int offs) const;
470 /* Transfer a single or all registers belonging to a certain register
471 set to or from a buffer. This is the main worker function for
472 regcache_supply_regset and regcache_collect_regset. */
473 void transfer_regset (const struct regset *regset, int regbase,
474 struct regcache *out_regcache,
475 int regnum, const gdb_byte *in_buf,
476 gdb_byte *out_buf, size_t size) const;
478 /* Perform a partial register transfer using a read, modify, write
479 operation. */
480 register_status write_part (int regnum, int offset,
481 gdb::array_view<const gdb_byte> src,
482 bool is_raw);
484 /* The inferior to switch to, to make target calls.
486 This may not be the inferior of thread M_PTID. For instance, this
487 regcache might be for a fork child we are about to detach, so there will
488 never be an inferior for that thread / process. Nevertheless, we need to
489 be able to switch to the target stack that can handle register reads /
490 writes for this regcache, and that's what this inferior is for. */
491 inferior *m_inf_for_target_calls;
493 /* If this is a read-write cache, which thread's registers is
494 it connected to? */
495 ptid_t m_ptid;
497 friend regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
498 ptid_t ptid, gdbarch *gdbarch);
501 using regcache_up = std::unique_ptr<regcache>;
503 class readonly_detached_regcache : public readable_regcache
505 public:
506 readonly_detached_regcache (regcache &src);
508 /* Create a readonly regcache by getting contents from COOKED_READ. */
510 readonly_detached_regcache (gdbarch *gdbarch, register_read_ftype cooked_read)
511 : readable_regcache (gdbarch, true)
513 save (cooked_read);
516 DISABLE_COPY_AND_ASSIGN (readonly_detached_regcache);
518 void raw_update (int regnum) override
522 extern void registers_changed (void);
523 extern void registers_changed_ptid (process_stratum_target *target,
524 ptid_t ptid);
526 /* Indicate that registers of THREAD may have changed, so invalidate
527 the cache. */
528 extern void registers_changed_thread (thread_info *thread);
530 /* An abstract base class for register dump. */
532 class register_dump
534 public:
535 void dump (ui_file *file);
536 virtual ~register_dump () = default;
538 protected:
539 register_dump (gdbarch *arch)
540 : m_gdbarch (arch)
543 /* Dump the register REGNUM contents. If REGNUM is -1, print the
544 header. */
545 virtual void dump_reg (ui_file *file, int regnum) = 0;
547 gdbarch *m_gdbarch;
550 #endif /* REGCACHE_H */