xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/regcache.h (revision 8b657b0747480f8989760d71343d6dd33f8d4cf9)
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2023 Free Software Foundation, Inc.
4 
5    This file is part of GDB.
6 
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.
11 
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.
16 
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/>.  */
19 
20 #ifndef REGCACHE_H
21 #define REGCACHE_H
22 
23 #include "gdbsupport/common-regcache.h"
24 #include "gdbsupport/function-view.h"
25 
26 struct regcache;
27 struct regset;
28 struct gdbarch;
29 struct address_space;
30 class thread_info;
31 struct process_stratum_target;
32 
33 extern struct regcache *get_current_regcache (void);
34 extern struct regcache *get_thread_regcache (process_stratum_target *target,
35 					     ptid_t ptid);
36 
37 /* Get the regcache of THREAD.  */
38 extern struct regcache *get_thread_regcache (thread_info *thread);
39 
40 extern struct regcache *get_thread_arch_regcache
41   (process_stratum_target *targ, ptid_t, struct gdbarch *);
42 extern struct regcache *get_thread_arch_aspace_regcache
43   (process_stratum_target *target, ptid_t,
44    struct gdbarch *, struct address_space *);
45 
46 extern enum register_status
47   regcache_raw_read_signed (struct regcache *regcache,
48 			    int regnum, LONGEST *val);
49 
50 extern void regcache_raw_write_signed (struct regcache *regcache,
51 				       int regnum, LONGEST val);
52 extern void regcache_raw_write_unsigned (struct regcache *regcache,
53 					 int regnum, ULONGEST val);
54 
55 /* Return the register's value in signed or throw if it's not
56    available.  */
57 
58 extern LONGEST regcache_raw_get_signed (struct regcache *regcache,
59 					int regnum);
60 
61 /* Read a register as a signed/unsigned quantity.  */
62 extern enum register_status
63   regcache_cooked_read_signed (struct regcache *regcache,
64 			       int regnum, LONGEST *val);
65 extern enum register_status
66   regcache_cooked_read_unsigned (struct regcache *regcache,
67 				 int regnum, ULONGEST *val);
68 extern void regcache_cooked_write_signed (struct regcache *regcache,
69 					  int regnum, LONGEST val);
70 extern void regcache_cooked_write_unsigned (struct regcache *regcache,
71 					    int regnum, ULONGEST val);
72 
73 /* Special routines to read/write the PC.  */
74 
75 /* For regcache_read_pc see gdbsupport/common-regcache.h.  */
76 extern void regcache_write_pc (struct regcache *regcache, CORE_ADDR pc);
77 
78 /* Mapping between register numbers and offsets in a buffer, for use
79    in the '*regset' functions below and with traditional frame caches.
80    In an array of 'regcache_map_entry' each element is interpreted
81    like follows:
82 
83    - If 'regno' is a register number: Map register 'regno' to the
84      current offset (starting with 0) and increase the current offset
85      by 'size' (or the register's size, if 'size' is zero).  Repeat
86      this with consecutive register numbers up to 'regno+count-1'.
87 
88      For each described register, if 'size' is larger than the
89      register's size, the register's value is assumed to be stored in
90      the first N (where N is the register size) bytes at the current
91      offset.  The remaining 'size' - N bytes are filled with zeroes by
92      'regcache_collect_regset' and ignored by other consumers.
93 
94      If 'size' is smaller than the register's size, only the first
95      'size' bytes of a register's value are assumed to be stored at
96      the current offset.  'regcache_collect_regset' copies the first
97      'size' bytes of a register's value to the output buffer.
98      'regcache_supply_regset' copies the bytes from the input buffer
99      into the first 'size' bytes of the register's value leaving the
100      remaining bytes of the register's value unchanged.  Frame caches
101      read the 'size' bytes from the stack frame and zero extend them
102      to generate the register's value.
103 
104    - If 'regno' is REGCACHE_MAP_SKIP: Add 'count*size' to the current
105      offset.
106 
107    - If count=0: End of the map.  */
108 
109 struct regcache_map_entry
110 {
111   int count;
112   int regno;
113   int size;
114 };
115 
116 /* Special value for the 'regno' field in the struct above.  */
117 
118 enum
119   {
120     REGCACHE_MAP_SKIP = -1,
121   };
122 
123 /* Calculate and return the total size of all the registers in a
124    regcache_map_entry.  */
125 
126 static inline int
127 regcache_map_entry_size (const struct regcache_map_entry *map)
128 {
129   int size = 0;
130   for (int i = 0; map[i].count != 0; i++)
131     size += (map[i].count * map[i].size);
132   return size;
133 }
134 
135 /* Transfer a set of registers (as described by REGSET) between
136    REGCACHE and BUF.  If REGNUM == -1, transfer all registers
137    belonging to the regset, otherwise just the register numbered
138    REGNUM.  The REGSET's 'regmap' field must point to an array of
139    'struct regcache_map_entry'.
140 
141    These functions are suitable for the 'regset_supply' and
142    'regset_collect' fields in a regset structure.  */
143 
144 extern void regcache_supply_regset (const struct regset *regset,
145 				    struct regcache *regcache,
146 				    int regnum, const void *buf,
147 				    size_t size);
148 extern void regcache_collect_regset (const struct regset *regset,
149 				     const struct regcache *regcache,
150 				     int regnum, void *buf, size_t size);
151 
152 
153 /* Return true if a set of registers contains the value of the
154    register numbered REGNUM.  The size of the set of registers is
155    given in SIZE, and the layout of the set of registers is described
156    by MAP.  */
157 
158 extern bool regcache_map_supplies (const struct regcache_map_entry *map,
159 				   int regnum, struct gdbarch *gdbarch,
160 				   size_t size);
161 
162 /* The type of a register.  This function is slightly more efficient
163    then its gdbarch vector counterpart since it returns a precomputed
164    value stored in a table.  */
165 
166 extern struct type *register_type (struct gdbarch *gdbarch, int regnum);
167 
168 
169 /* Return the size of register REGNUM.  All registers should have only
170    one size.  */
171 
172 extern int register_size (struct gdbarch *gdbarch, int regnum);
173 
174 typedef gdb::function_view<register_status (int regnum, gdb_byte *buf)>
175   register_read_ftype;
176 
177 /* A (register_number, register_value) pair.  */
178 
179 struct cached_reg_t
180 {
181   int num;
182   gdb_byte *data;
183 };
184 
185 /* Buffer of registers.  */
186 
187 class reg_buffer : public reg_buffer_common
188 {
189 public:
190   reg_buffer (gdbarch *gdbarch, bool has_pseudo);
191 
192   DISABLE_COPY_AND_ASSIGN (reg_buffer);
193 
194   /* Return regcache's architecture.  */
195   gdbarch *arch () const;
196 
197   /* See gdbsupport/common-regcache.h.  */
198   enum register_status get_register_status (int regnum) const override;
199 
200   /* See gdbsupport/common-regcache.h.  */
201   void raw_collect (int regnum, void *buf) const override;
202 
203   /* Collect register REGNUM from REGCACHE.  Store collected value as an integer
204      at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
205      If ADDR_LEN is greater than the register size, then the integer will be
206      sign or zero extended.  If ADDR_LEN is smaller than the register size, then
207      the most significant bytes of the integer will be truncated.  */
208   void raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
209 			    bool is_signed) const;
210 
211   /* Collect register REGNUM from REGCACHE, starting at OFFSET in register,
212      reading only LEN.  */
213   void raw_collect_part (int regnum, int offset, int len, gdb_byte *out) const;
214 
215   /* See gdbsupport/common-regcache.h.  */
216   void raw_supply (int regnum, const void *buf) override;
217 
218   void raw_supply (int regnum, const reg_buffer &src)
219   {
220     raw_supply (regnum, src.register_buffer (regnum));
221   }
222 
223   /* Supply register REGNUM to REGCACHE.  Value to supply is an integer stored
224      at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
225      If the register size is greater than ADDR_LEN, then the integer will be
226      sign or zero extended.  If the register size is smaller than the integer,
227      then the most significant bytes of the integer will be truncated.  */
228   void raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
229 			   bool is_signed);
230 
231   /* Supply register REGNUM with zeroed value to REGCACHE.  This is not the same
232      as calling raw_supply with NULL (which will set the state to
233      unavailable).  */
234   void raw_supply_zeroed (int regnum);
235 
236   /* Supply register REGNUM to REGCACHE, starting at OFFSET in register, writing
237      only LEN, without editing the rest of the register.  */
238   void raw_supply_part (int regnum, int offset, int len, const gdb_byte *in);
239 
240   void invalidate (int regnum);
241 
242   virtual ~reg_buffer () = default;
243 
244   /* See gdbsupport/common-regcache.h.  */
245   bool raw_compare (int regnum, const void *buf, int offset) const override;
246 
247 protected:
248   /* Assert on the range of REGNUM.  */
249   void assert_regnum (int regnum) const;
250 
251   int num_raw_registers () const;
252 
253   gdb_byte *register_buffer (int regnum) const;
254 
255   /* Save a register cache.  The set of registers saved into the
256      regcache determined by the save_reggroup.  COOKED_READ returns
257      zero iff the register's value can't be returned.  */
258   void save (register_read_ftype cooked_read);
259 
260   struct regcache_descr *m_descr;
261 
262   bool m_has_pseudo;
263   /* The register buffers.  */
264   std::unique_ptr<gdb_byte[]> m_registers;
265   /* Register cache status.  */
266   std::unique_ptr<register_status[]> m_register_status;
267 
268   friend class regcache;
269   friend class detached_regcache;
270 };
271 
272 /* An abstract class which only has methods doing read.  */
273 
274 class readable_regcache : public reg_buffer
275 {
276 public:
277   readable_regcache (gdbarch *gdbarch, bool has_pseudo)
278     : reg_buffer (gdbarch, has_pseudo)
279   {}
280 
281   /* Transfer a raw register [0..NUM_REGS) from core-gdb to this regcache,
282      return its value in *BUF and return its availability status.  */
283 
284   enum register_status raw_read (int regnum, gdb_byte *buf);
285   template<typename T, typename = RequireLongest<T>>
286   enum register_status raw_read (int regnum, T *val);
287 
288   /* Partial transfer of raw registers.  Return the status of the register.  */
289   enum register_status raw_read_part (int regnum, int offset, int len,
290 				      gdb_byte *buf);
291 
292   /* Make certain that the register REGNUM is up-to-date.  */
293   virtual void raw_update (int regnum) = 0;
294 
295   /* Transfer a raw register [0..NUM_REGS+NUM_PSEUDO_REGS) from core-gdb to
296      this regcache, return its value in *BUF and return its availability status.  */
297   enum register_status cooked_read (int regnum, gdb_byte *buf);
298   template<typename T, typename = RequireLongest<T>>
299   enum register_status cooked_read (int regnum, T *val);
300 
301   /* Partial transfer of a cooked register.  */
302   enum register_status cooked_read_part (int regnum, int offset, int len,
303 					 gdb_byte *buf);
304 
305   /* Read register REGNUM from the regcache and return a new value.  This
306      will call mark_value_bytes_unavailable as appropriate.  */
307   struct value *cooked_read_value (int regnum);
308 
309 protected:
310 
311   /* Perform a partial register transfer using a read, modify, write
312      operation.  Will fail if register is currently invalid.  */
313   enum register_status read_part (int regnum, int offset, int len,
314 				  gdb_byte *out, bool is_raw);
315 };
316 
317 /* Buffer of registers, can be read and written.  */
318 
319 class detached_regcache : public readable_regcache
320 {
321 public:
322   detached_regcache (gdbarch *gdbarch, bool has_pseudo)
323     : readable_regcache (gdbarch, has_pseudo)
324   {}
325 
326   void raw_update (int regnum) override
327   {}
328 
329   DISABLE_COPY_AND_ASSIGN (detached_regcache);
330 };
331 
332 class readonly_detached_regcache;
333 
334 /* The register cache for storing raw register values.  */
335 
336 class regcache : public detached_regcache
337 {
338 public:
339   DISABLE_COPY_AND_ASSIGN (regcache);
340 
341   /* Return REGCACHE's address space.  */
342   const address_space *aspace () const
343   {
344     return m_aspace;
345   }
346 
347   /* Restore 'this' regcache.  The set of registers restored into
348      the regcache determined by the restore_reggroup.
349      Writes to regcache will go through to the target.  SRC is a
350      read-only register cache.  */
351   void restore (readonly_detached_regcache *src);
352 
353   /* Update the value of raw register REGNUM (in the range [0..NUM_REGS)) and
354      transfer its value to core-gdb.  */
355 
356   void raw_write (int regnum, const gdb_byte *buf);
357 
358   template<typename T, typename = RequireLongest<T>>
359   void raw_write (int regnum, T val);
360 
361   /* Transfer of pseudo-registers.  */
362   void cooked_write (int regnum, const gdb_byte *buf);
363 
364   template<typename T, typename = RequireLongest<T>>
365   void cooked_write (int regnum, T val);
366 
367   void raw_update (int regnum) override;
368 
369   /* Partial transfer of raw registers.  Perform read, modify, write style
370      operations.  */
371   void raw_write_part (int regnum, int offset, int len, const gdb_byte *buf);
372 
373   /* Partial transfer of a cooked register.  Perform read, modify, write style
374      operations.  */
375   void cooked_write_part (int regnum, int offset, int len,
376 			  const gdb_byte *buf);
377 
378   /* Transfer a set of registers (as described by REGSET) between
379      REGCACHE and BUF.  If REGNUM == -1, transfer all registers
380      belonging to the regset, otherwise just the register numbered
381      REGNUM.  The REGSET's 'regmap' field must point to an array of
382      'struct regcache_map_entry'.  The valid register numbers in each
383      entry in 'struct regcache_map_entry' are offset by REGBASE.  */
384 
385   void supply_regset (const struct regset *regset, int regbase,
386 		      int regnum, const void *buf, size_t size);
387 
388   void collect_regset (const struct regset *regset, int regbase, int regnum,
389 		       void *buf, size_t size) const;
390 
391   /* Same as the above, but with REGBASE == 0.  */
392 
393   void supply_regset (const struct regset *regset,
394 		      int regnum, const void *buf, size_t size)
395   {
396     supply_regset (regset, 0, regnum, buf, size);
397   }
398 
399   void collect_regset (const struct regset *regset, int regnum,
400 		       void *buf, size_t size) const
401   {
402     collect_regset (regset, 0, regnum, buf, size);
403   }
404 
405   /* Return REGCACHE's ptid.  */
406 
407   ptid_t ptid () const
408   {
409     gdb_assert (m_ptid != minus_one_ptid);
410 
411     return m_ptid;
412   }
413 
414   void set_ptid (const ptid_t ptid)
415   {
416     this->m_ptid = ptid;
417   }
418 
419   process_stratum_target *target () const
420   {
421     return m_target;
422   }
423 
424 /* Dump the contents of a register from the register cache to the target
425    debug.  */
426   void debug_print_register (const char *func, int regno);
427 
428 protected:
429   regcache (process_stratum_target *target, gdbarch *gdbarch,
430 	    const address_space *aspace);
431 
432 private:
433 
434   /* Helper function for transfer_regset.  Copies across a single register.  */
435   void transfer_regset_register (struct regcache *out_regcache, int regnum,
436 				 const gdb_byte *in_buf, gdb_byte *out_buf,
437 				 int slot_size, int offs) const;
438 
439   /* Transfer a single or all registers belonging to a certain register
440      set to or from a buffer.  This is the main worker function for
441      regcache_supply_regset and regcache_collect_regset.  */
442   void transfer_regset (const struct regset *regset, int regbase,
443 			struct regcache *out_regcache,
444 			int regnum, const gdb_byte *in_buf,
445 			gdb_byte *out_buf, size_t size) const;
446 
447   /* Perform a partial register transfer using a read, modify, write
448      operation.  */
449   enum register_status write_part (int regnum, int offset, int len,
450 				   const gdb_byte *in, bool is_raw);
451 
452   /* The address space of this register cache (for registers where it
453      makes sense, like PC or SP).  */
454   const address_space * const m_aspace;
455 
456   /* If this is a read-write cache, which thread's registers is
457      it connected to?  */
458   process_stratum_target *m_target;
459   ptid_t m_ptid;
460 
461   friend struct regcache *
462   get_thread_arch_aspace_regcache (process_stratum_target *target, ptid_t ptid,
463 				   struct gdbarch *gdbarch,
464 				   struct address_space *aspace);
465 };
466 
467 using regcache_up = std::unique_ptr<regcache>;
468 
469 class readonly_detached_regcache : public readable_regcache
470 {
471 public:
472   readonly_detached_regcache (regcache &src);
473 
474   /* Create a readonly regcache by getting contents from COOKED_READ.  */
475 
476   readonly_detached_regcache (gdbarch *gdbarch, register_read_ftype cooked_read)
477     : readable_regcache (gdbarch, true)
478   {
479     save (cooked_read);
480   }
481 
482   DISABLE_COPY_AND_ASSIGN (readonly_detached_regcache);
483 
484   void raw_update (int regnum) override
485   {}
486 };
487 
488 extern void registers_changed (void);
489 extern void registers_changed_ptid (process_stratum_target *target,
490 				    ptid_t ptid);
491 
492 /* Indicate that registers of THREAD may have changed, so invalidate
493    the cache.  */
494 extern void registers_changed_thread (thread_info *thread);
495 
496 /* An abstract base class for register dump.  */
497 
498 class register_dump
499 {
500 public:
501   void dump (ui_file *file);
502   virtual ~register_dump () = default;
503 
504 protected:
505   register_dump (gdbarch *arch)
506     : m_gdbarch (arch)
507   {}
508 
509   /* Dump the register REGNUM contents.  If REGNUM is -1, print the
510      header.  */
511   virtual void dump_reg (ui_file *file, int regnum) = 0;
512 
513   gdbarch *m_gdbarch;
514 };
515 
516 #endif /* REGCACHE_H */
517