1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2 
3    Copyright (C) 1986-2024 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/array-view.h"
24 #include "gdbsupport/common-regcache.h"
25 #include "gdbsupport/function-view.h"
26 #include "gdbsupport/traits.h"
27 
28 struct regcache;
29 struct regset;
30 struct gdbarch;
31 class thread_info;
32 struct process_stratum_target;
33 struct inferior;
34 
35 extern struct regcache *get_thread_regcache (process_stratum_target *target,
36                                                        ptid_t ptid);
37 
38 /* Get the regcache of THREAD.  */
39 extern struct regcache *get_thread_regcache (thread_info *thread);
40 
41 extern regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
42                                                      ptid_t ptid, gdbarch *arch);
43 
44 extern enum register_status
45   regcache_raw_read_signed (struct regcache *regcache,
46                                   int regnum, LONGEST *val);
47 
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);
52 
53 /* Return the register's value in signed or throw if it's not
54    available.  */
55 
56 extern LONGEST regcache_raw_get_signed (struct regcache *regcache,
57                                                   int regnum);
58 
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);
70 
71 /* Special routines to read/write the PC.  */
72 
73 /* For regcache_read_pc see gdbsupport/common-regcache.h.  */
74 extern void regcache_write_pc (struct regcache *regcache, CORE_ADDR pc);
75 
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
79    like follows:
80 
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'.
85 
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.
91 
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.
101 
102    - If 'regno' is REGCACHE_MAP_SKIP: Add 'count*size' to the current
103      offset.
104 
105    - If count=0: End of the map.  */
106 
107 struct regcache_map_entry
108 {
109   int count;
110   int regno;
111   int size;
112 };
113 
114 /* Special value for the 'regno' field in the struct above.  */
115 
116 enum
117   {
118     REGCACHE_MAP_SKIP = -1,
119   };
120 
121 /* Calculate and return the total size of all the registers in a
122    regcache_map_entry.  */
123 
124 static inline int
regcache_map_entry_size(const struct regcache_map_entry * map)125 regcache_map_entry_size (const struct regcache_map_entry *map)
126 {
127   int size = 0;
128   for (int i = 0; map[i].count != 0; i++)
129     size += (map[i].count * map[i].size);
130   return size;
131 }
132 
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'.
138 
139    These functions are suitable for the 'regset_supply' and
140    'regset_collect' fields in a regset structure.  */
141 
142 extern void regcache_supply_regset (const struct regset *regset,
143                                             struct regcache *regcache,
144                                             int regnum, const void *buf,
145                                             size_t size);
146 extern void regcache_collect_regset (const struct regset *regset,
147                                              const struct regcache *regcache,
148                                              int regnum, void *buf, size_t size);
149 
150 
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
154    by MAP.  */
155 
156 extern bool regcache_map_supplies (const struct regcache_map_entry *map,
157                                            int regnum, struct gdbarch *gdbarch,
158                                            size_t size);
159 
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.  */
163 
164 extern struct type *register_type (struct gdbarch *gdbarch, int regnum);
165 
166 
167 /* Return the size of register REGNUM.  All registers should have only
168    one size.  */
169 
170 extern int register_size (struct gdbarch *gdbarch, int regnum);
171 
172 using register_read_ftype
173   = gdb::function_view<register_status (int, gdb::array_view<gdb_byte>)>;
174 
175 /* A (register_number, register_value) pair.  */
176 
177 struct cached_reg_t
178 {
179   int num;
180   gdb::unique_xmalloc_ptr<gdb_byte> data;
181 
182   cached_reg_t () = default;
183   cached_reg_t (cached_reg_t &&rhs) = default;
184 };
185 
186 /* Buffer of registers.  */
187 
188 class reg_buffer : public reg_buffer_common
189 {
190 public:
191   reg_buffer (gdbarch *gdbarch, bool has_pseudo);
192 
193   DISABLE_COPY_AND_ASSIGN (reg_buffer);
194 
195   /* Return regcache's architecture.  */
196   gdbarch *arch () const;
197 
198   /* See gdbsupport/common-regcache.h.  */
199   enum register_status get_register_status (int regnum) const override;
200 
201   /* See gdbsupport/common-regcache.h.  */
202   void raw_collect (int regnum, gdb::array_view<gdb_byte> dst) const override;
203 
204   /* Deprecated overload of the above.  */
205   void raw_collect (int regnum, void *dst) const;
206 
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;
214 
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;
219 
220   /* Deprecated overload of the above.  */
raw_collect_part(int regnum,int offset,int len,gdb_byte * dst)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)); }
223 
224   /* See gdbsupport/common-regcache.h.  */
225   void raw_supply (int regnum, gdb::array_view<const gdb_byte> src) override;
226 
227   /* Deprecated overload of the above.  */
228   void raw_supply (int regnum, const void *src);
229 
raw_supply(int regnum,const reg_buffer & src)230   void raw_supply (int regnum, const reg_buffer &src)
231   { raw_supply (regnum, src.register_buffer (regnum)); }
232 
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,
239                                  bool is_signed);
240 
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
243      unavailable).  */
244   void raw_supply_zeroed (int regnum);
245 
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);
251 
252   void invalidate (int regnum);
253 
254   virtual ~reg_buffer () = default;
255 
256   /* See gdbsupport/common-regcache.h.  */
257   bool raw_compare (int regnum, const void *buf, int offset) const override;
258 
259 protected:
260   /* Assert on the range of REGNUM.  */
261   void assert_regnum (int regnum) const;
262 
263   int num_raw_registers () const;
264 
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);
270 
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);
275 
276   struct regcache_descr *m_descr;
277 
278   bool m_has_pseudo;
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;
283 
284   friend class regcache;
285   friend class detached_regcache;
286 };
287 
288 /* An abstract class which only has methods doing read.  */
289 
290 class readable_regcache : public reg_buffer
291 {
292 public:
readable_regcache(gdbarch * gdbarch,bool has_pseudo)293   readable_regcache (gdbarch *gdbarch, bool has_pseudo)
294     : reg_buffer (gdbarch, has_pseudo)
295   {}
296 
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);
300 
301   /* Deprecated overload of the above.  */
302   register_status raw_read (int regnum, gdb_byte *dst);
303 
304   template<typename T, typename = RequireLongest<T>>
305   register_status raw_read (int regnum, T *val);
306 
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);
310 
311   /* Deprecated overload of the above.  */
raw_read_part(int regnum,int offset,int len,gdb_byte * dst)312   register_status raw_read_part (int regnum, int offset, int len,
313                                          gdb_byte *dst)
314   { return raw_read_part (regnum, offset, gdb::make_array_view (dst, len)); }
315 
316   /* Make certain that the register REGNUM is up-to-date.  */
317   virtual void raw_update (int regnum) = 0;
318 
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);
323 
324   template<typename T, typename = RequireLongest<T>>
325   register_status cooked_read (int regnum, T *val);
326 
327   /* Partial transfer of a cooked register.  */
328   register_status cooked_read_part (int regnum, int offset,
329                                             gdb::array_view<gdb_byte> dst);
330 
331   /* Deprecated overload of the above.  */
cooked_read_part(int regnum,int offset,int len,gdb_byte * src)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)); }
334 
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);
338 
339 protected:
340 
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);
345 };
346 
347 /* Buffer of registers, can be read and written.  */
348 
349 class detached_regcache : public readable_regcache
350 {
351 public:
detached_regcache(gdbarch * gdbarch,bool has_pseudo)352   detached_regcache (gdbarch *gdbarch, bool has_pseudo)
353     : readable_regcache (gdbarch, has_pseudo)
354   {}
355 
raw_update(int regnum)356   void raw_update (int regnum) override
357   {}
358 
359   DISABLE_COPY_AND_ASSIGN (detached_regcache);
360 };
361 
362 class readonly_detached_regcache;
363 
364 /* The register cache for storing raw register values.  */
365 
366 class regcache : public detached_regcache
367 {
368 public:
369   DISABLE_COPY_AND_ASSIGN (regcache);
370 
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);
376 
377   /* Update the value of raw register REGNUM (in the range [0..NUM_REGS)) and
378      transfer its value to core-gdb.  */
379 
380   void raw_write (int regnum, gdb::array_view<const gdb_byte> src);
381 
382   /* Deprecated overload of the above.  */
383   void raw_write (int regnum, const gdb_byte *src);
384 
385   template<typename T, typename = RequireLongest<T>>
386   void raw_write (int regnum, T val);
387 
388   /* Transfer of pseudo-registers.  */
389   void cooked_write (int regnum, gdb::array_view<const gdb_byte> src);
390 
391   /* Deprecated overload of the above.  */
392   void cooked_write (int regnum, const gdb_byte *src);
393 
394   template<typename T, typename = RequireLongest<T>>
395   void cooked_write (int regnum, T val);
396 
397   void raw_update (int regnum) override;
398 
399   /* Partial transfer of raw registers.  Perform read, modify, write style
400      operations.  */
401   void raw_write_part (int regnum, int offset,
402                            gdb::array_view<const gdb_byte> src);
403 
404   /* Deprecated overload of the above.  */
raw_write_part(int regnum,int offset,int len,const gdb_byte * src)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)); }
407 
408   /* Partial transfer of a cooked register.  Perform read, modify, write style
409      operations.  */
410   void cooked_write_part (int regnum, int offset,
411                                 gdb::array_view<const gdb_byte> src);
412 
413   /* Deprecated overload of the above.  */
cooked_write_part(int regnum,int offset,int len,const gdb_byte * src)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)); }
416 
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.  */
423 
424   void supply_regset (const struct regset *regset, int regbase,
425                           int regnum, const void *buf, size_t size);
426 
427   void collect_regset (const struct regset *regset, int regbase, int regnum,
428                            void *buf, size_t size) const;
429 
430   /* Same as the above, but with REGBASE == 0.  */
431 
supply_regset(const struct regset * regset,int regnum,const void * buf,size_t size)432   void supply_regset (const struct regset *regset,
433                           int regnum, const void *buf, size_t size)
434   {
435     supply_regset (regset, 0, regnum, buf, size);
436   }
437 
collect_regset(const struct regset * regset,int regnum,void * buf,size_t size)438   void collect_regset (const struct regset *regset, int regnum,
439                            void *buf, size_t size) const
440   {
441     collect_regset (regset, 0, regnum, buf, size);
442   }
443 
444   /* Return REGCACHE's ptid.  */
445 
ptid()446   ptid_t ptid () const
447   {
448     gdb_assert (m_ptid != minus_one_ptid);
449 
450     return m_ptid;
451   }
452 
set_ptid(const ptid_t ptid)453   void set_ptid (const ptid_t ptid)
454   {
455     this->m_ptid = ptid;
456   }
457 
458   /* Return a string with the contents of a register, suitable for debug output.  */
459   std::string register_debug_string (int regno);
460 
461 protected:
462   regcache (inferior *inf_for_target_calls, gdbarch *gdbarch);
463 
464 private:
465 
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;
470 
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;
478 
479   /* Perform a partial register transfer using a read, modify, write
480      operation.  */
481   register_status write_part (int regnum, int offset,
482                                     gdb::array_view<const gdb_byte> src,
483                                     bool is_raw);
484 
485   /* The inferior to switch to, to make target calls.
486 
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;
493 
494   /* If this is a read-write cache, which thread's registers is
495      it connected to?  */
496   ptid_t m_ptid;
497 
498   friend regcache *get_thread_arch_regcache (inferior *inf_for_target_calls,
499                                                        ptid_t ptid, gdbarch *gdbarch);
500 };
501 
502 using regcache_up = std::unique_ptr<regcache>;
503 
504 class readonly_detached_regcache : public readable_regcache
505 {
506 public:
507   readonly_detached_regcache (regcache &src);
508 
509   /* Create a readonly regcache by getting contents from COOKED_READ.  */
510 
readonly_detached_regcache(gdbarch * gdbarch,register_read_ftype cooked_read)511   readonly_detached_regcache (gdbarch *gdbarch, register_read_ftype cooked_read)
512     : readable_regcache (gdbarch, true)
513   {
514     save (cooked_read);
515   }
516 
517   DISABLE_COPY_AND_ASSIGN (readonly_detached_regcache);
518 
raw_update(int regnum)519   void raw_update (int regnum) override
520   {}
521 };
522 
523 extern void registers_changed (void);
524 extern void registers_changed_ptid (process_stratum_target *target,
525                                             ptid_t ptid);
526 
527 /* Indicate that registers of THREAD may have changed, so invalidate
528    the cache.  */
529 extern void registers_changed_thread (thread_info *thread);
530 
531 /* An abstract base class for register dump.  */
532 
533 class register_dump
534 {
535 public:
536   void dump (ui_file *file);
537   virtual ~register_dump () = default;
538 
539 protected:
register_dump(gdbarch * arch)540   register_dump (gdbarch *arch)
541     : m_gdbarch (arch)
542   {}
543 
544   /* Dump the register REGNUM contents.  If REGNUM is -1, print the
545      header.  */
546   virtual void dump_reg (ui_file *file, int regnum) = 0;
547 
548   gdbarch *m_gdbarch;
549 };
550 
551 #endif /* REGCACHE_H */
552