1 /* Register support routines for the remote server for GDB.
2 Copyright (C) 2001-2024 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "gdbthread.h"
22 #include "gdbsupport/rsp-low.h"
23 #include "gdbsupport/gdb-checked-static-cast.h"
25 #ifndef IN_PROCESS_AGENT
28 get_thread_regcache (struct thread_info
*thread
, int fetch
)
30 struct regcache
*regcache
;
32 regcache
= thread_regcache_data (thread
);
34 /* Threads' regcaches are created lazily, because biarch targets add
35 the main thread/lwp before seeing it stop for the first time, and
36 it is only after the target sees the thread stop for the first
37 time that the target has a chance of determining the process's
38 architecture. IOW, when we first add the process's main thread
39 we don't know which architecture/tdesc its regcache should
43 struct process_info
*proc
= get_thread_process (thread
);
45 gdb_assert (proc
->tdesc
!= NULL
);
47 regcache
= new_register_cache (proc
->tdesc
);
48 set_thread_regcache_data (thread
, regcache
);
51 if (fetch
&& regcache
->registers_valid
== 0)
53 scoped_restore_current_thread restore_thread
;
55 switch_to_thread (thread
);
56 /* Invalidate all registers, to prevent stale left-overs. */
57 memset (regcache
->register_status
, REG_UNAVAILABLE
,
58 regcache
->tdesc
->reg_defs
.size ());
59 fetch_inferior_registers (regcache
, -1);
60 regcache
->registers_valid
= 1;
66 /* See gdbsupport/common-regcache.h. */
69 get_thread_regcache_for_ptid (ptid_t ptid
)
71 return get_thread_regcache (find_thread_ptid (ptid
), 1);
75 regcache_invalidate_thread (struct thread_info
*thread
)
77 struct regcache
*regcache
;
79 regcache
= thread_regcache_data (thread
);
84 if (regcache
->registers_valid
)
86 scoped_restore_current_thread restore_thread
;
88 switch_to_thread (thread
);
89 store_inferior_registers (regcache
, -1);
92 regcache
->registers_valid
= 0;
98 regcache_invalidate_pid (int pid
)
100 /* Only invalidate the regcaches of threads of this process. */
101 for_each_thread (pid
, regcache_invalidate_thread
);
104 /* See regcache.h. */
107 regcache_invalidate (void)
109 /* Only update the threads of the current process. */
110 int pid
= current_thread
->id
.pid ();
112 regcache_invalidate_pid (pid
);
118 init_register_cache (struct regcache
*regcache
,
119 const struct target_desc
*tdesc
,
120 unsigned char *regbuf
)
124 #ifndef IN_PROCESS_AGENT
125 /* Make sure to zero-initialize the register cache when it is
126 created, in case there are registers the target never
127 fetches. This way they'll read as zero instead of
129 regcache
->tdesc
= tdesc
;
131 = (unsigned char *) xcalloc (1, tdesc
->registers_size
);
132 regcache
->registers_owned
= 1;
133 regcache
->register_status
134 = (unsigned char *) xmalloc (tdesc
->reg_defs
.size ());
135 memset ((void *) regcache
->register_status
, REG_UNAVAILABLE
,
136 tdesc
->reg_defs
.size ());
138 gdb_assert_not_reached ("can't allocate memory from the heap");
143 regcache
->tdesc
= tdesc
;
144 regcache
->registers
= regbuf
;
145 regcache
->registers_owned
= 0;
146 #ifndef IN_PROCESS_AGENT
147 regcache
->register_status
= NULL
;
151 regcache
->registers_valid
= 0;
156 #ifndef IN_PROCESS_AGENT
159 new_register_cache (const struct target_desc
*tdesc
)
161 struct regcache
*regcache
= new struct regcache
;
163 gdb_assert (tdesc
->registers_size
!= 0);
165 return init_register_cache (regcache
, tdesc
, NULL
);
169 free_register_cache (struct regcache
*regcache
)
173 if (regcache
->registers_owned
)
174 free (regcache
->registers
);
175 free (regcache
->register_status
);
183 regcache_cpy (struct regcache
*dst
, struct regcache
*src
)
185 gdb_assert (src
!= NULL
&& dst
!= NULL
);
186 gdb_assert (src
->tdesc
== dst
->tdesc
);
187 gdb_assert (src
!= dst
);
189 memcpy (dst
->registers
, src
->registers
, src
->tdesc
->registers_size
);
190 #ifndef IN_PROCESS_AGENT
191 if (dst
->register_status
!= NULL
&& src
->register_status
!= NULL
)
192 memcpy (dst
->register_status
, src
->register_status
,
193 src
->tdesc
->reg_defs
.size ());
195 dst
->registers_valid
= src
->registers_valid
;
198 /* Return a reference to the description of register N. */
200 static const struct gdb::reg
&
201 find_register_by_number (const struct target_desc
*tdesc
, int n
)
204 gdb_assert (n
< tdesc
->reg_defs
.size ());
206 return tdesc
->reg_defs
[n
];
209 #ifndef IN_PROCESS_AGENT
212 registers_to_string (struct regcache
*regcache
, char *buf
)
214 unsigned char *registers
= regcache
->registers
;
215 const struct target_desc
*tdesc
= regcache
->tdesc
;
217 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
219 if (regcache
->register_status
[i
] == REG_VALID
)
221 bin2hex (registers
, buf
, register_size (tdesc
, i
));
222 buf
+= register_size (tdesc
, i
) * 2;
226 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
227 buf
+= register_size (tdesc
, i
) * 2;
229 registers
+= register_size (tdesc
, i
);
235 registers_from_string (struct regcache
*regcache
, char *buf
)
237 int len
= strlen (buf
);
238 unsigned char *registers
= regcache
->registers
;
239 const struct target_desc
*tdesc
= regcache
->tdesc
;
241 if (len
!= tdesc
->registers_size
* 2)
243 warning ("Wrong sized register packet (expected %d bytes, got %d)",
244 2 * tdesc
->registers_size
, len
);
245 if (len
> tdesc
->registers_size
* 2)
246 len
= tdesc
->registers_size
* 2;
248 hex2bin (buf
, registers
, len
/ 2);
254 find_regno_no_throw (const struct target_desc
*tdesc
, const char *name
)
256 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
258 if (strcmp (name
, find_register_by_number (tdesc
, i
).name
) == 0)
265 find_regno (const struct target_desc
*tdesc
, const char *name
)
267 std::optional
<int> regnum
= find_regno_no_throw (tdesc
, name
);
269 if (regnum
.has_value ())
272 internal_error ("Unknown register %s requested", name
);
276 free_register_cache_thread (struct thread_info
*thread
)
278 struct regcache
*regcache
= thread_regcache_data (thread
);
280 if (regcache
!= NULL
)
282 regcache_invalidate_thread (thread
);
283 free_register_cache (regcache
);
284 set_thread_regcache_data (thread
, NULL
);
289 regcache_release (void)
291 /* Flush and release all pre-existing register caches. */
292 for_each_thread (free_register_cache_thread
);
297 register_cache_size (const struct target_desc
*tdesc
)
299 return tdesc
->registers_size
;
303 register_size (const struct target_desc
*tdesc
, int n
)
305 return find_register_by_number (tdesc
, n
).size
/ 8;
308 /* See gdbsupport/common-regcache.h. */
311 regcache_register_size (const reg_buffer_common
*regcache
, int n
)
314 (gdb::checked_static_cast
<const struct regcache
*> (regcache
)->tdesc
, n
);
317 static gdb::array_view
<gdb_byte
>
318 register_data (const struct regcache
*regcache
, int n
)
320 const gdb::reg
®
= find_register_by_number (regcache
->tdesc
, n
);
321 return gdb::make_array_view (regcache
->registers
+ reg
.offset
/ 8,
326 supply_register (struct regcache
*regcache
, int n
, const void *vbuf
)
328 const gdb::reg
®
= find_register_by_number (regcache
->tdesc
, n
);
329 const gdb_byte
*buf
= static_cast<const gdb_byte
*> (vbuf
);
330 return regcache
->raw_supply (n
, gdb::make_array_view (buf
, reg
.size
/ 8));
333 /* See gdbsupport/common-regcache.h. */
336 regcache::raw_supply (int n
, gdb::array_view
<const gdb_byte
> src
)
338 auto dst
= register_data (this, n
);
340 if (src
.data () != nullptr)
343 #ifndef IN_PROCESS_AGENT
344 if (register_status
!= NULL
)
345 register_status
[n
] = REG_VALID
;
350 memset (dst
.data (), 0, dst
.size ());
351 #ifndef IN_PROCESS_AGENT
352 if (register_status
!= NULL
)
353 register_status
[n
] = REG_UNAVAILABLE
;
358 /* Supply register N with value zero to REGCACHE. */
361 supply_register_zeroed (struct regcache
*regcache
, int n
)
363 auto dst
= register_data (regcache
, n
);
364 memset (dst
.data (), 0, dst
.size ());
365 #ifndef IN_PROCESS_AGENT
366 if (regcache
->register_status
!= NULL
)
367 regcache
->register_status
[n
] = REG_VALID
;
371 #ifndef IN_PROCESS_AGENT
373 /* Supply register called NAME with value zero to REGCACHE. */
376 supply_register_by_name_zeroed (struct regcache
*regcache
,
379 supply_register_zeroed (regcache
, find_regno (regcache
->tdesc
, name
));
384 /* Supply the whole register set whose contents are stored in BUF, to
385 REGCACHE. If BUF is NULL, all the registers' values are recorded
389 supply_regblock (struct regcache
*regcache
, const void *buf
)
393 const struct target_desc
*tdesc
= regcache
->tdesc
;
395 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
396 #ifndef IN_PROCESS_AGENT
400 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
401 regcache
->register_status
[i
] = REG_VALID
;
407 const struct target_desc
*tdesc
= regcache
->tdesc
;
409 memset (regcache
->registers
, 0, tdesc
->registers_size
);
410 #ifndef IN_PROCESS_AGENT
414 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
415 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
421 #ifndef IN_PROCESS_AGENT
424 supply_register_by_name (struct regcache
*regcache
,
425 const char *name
, const void *buf
)
427 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
433 collect_register (struct regcache
*regcache
, int n
, void *vbuf
)
435 const gdb::reg
®
= find_register_by_number (regcache
->tdesc
, n
);
436 gdb_byte
*buf
= static_cast<gdb_byte
*> (vbuf
);
437 regcache
->raw_collect (n
, gdb::make_array_view (buf
, reg
.size
/ 8));
440 /* See gdbsupport/common-regcache.h. */
443 regcache::raw_collect (int n
, gdb::array_view
<gdb_byte
> dst
) const
445 auto src
= register_data (this, n
);
450 regcache_raw_read_unsigned (reg_buffer_common
*reg_buf
, int regnum
,
454 regcache
*regcache
= gdb::checked_static_cast
<struct regcache
*> (reg_buf
);
456 gdb_assert (regcache
!= NULL
);
458 size
= register_size (regcache
->tdesc
, regnum
);
460 if (size
> (int) sizeof (ULONGEST
))
461 error (_("That operation is not available on integers of more than"
463 (int) sizeof (ULONGEST
));
466 collect_register (regcache
, regnum
, val
);
471 #ifndef IN_PROCESS_AGENT
473 /* See regcache.h. */
476 regcache_raw_get_unsigned_by_name (struct regcache
*regcache
,
479 return regcache_raw_get_unsigned (regcache
,
480 find_regno (regcache
->tdesc
, name
));
484 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
486 bin2hex (register_data (regcache
, n
), buf
);
490 collect_register_by_name (struct regcache
*regcache
,
491 const char *name
, void *buf
)
493 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
496 /* Special handling for register PC. */
499 regcache_read_pc (reg_buffer_common
*regcache
)
501 return the_target
->read_pc
502 (gdb::checked_static_cast
<struct regcache
*> (regcache
));
506 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
508 the_target
->write_pc (regcache
, pc
);
513 /* See gdbsupport/common-regcache.h. */
516 regcache::get_register_status (int regnum
) const
518 #ifndef IN_PROCESS_AGENT
519 gdb_assert (regnum
>= 0 && regnum
< tdesc
->reg_defs
.size ());
520 return (enum register_status
) (register_status
[regnum
]);
526 /* See gdbsupport/common-regcache.h. */
529 regcache::raw_compare (int regnum
, const void *buf
, int offset
) const
531 gdb_assert (buf
!= NULL
);
533 gdb::array_view
<const gdb_byte
> regbuf
= register_data (this, regnum
);
534 gdb_assert (offset
< regbuf
.size ());
535 regbuf
= regbuf
.slice (offset
);
537 return memcmp (buf
, regbuf
.data (), regbuf
.size ()) == 0;