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 (thread_info
*thread
, bool fetch
)
30 regcache
*regcache
= thread
->regcache ();
32 /* Threads' regcaches are created lazily, because biarch targets add
33 the main thread/lwp before seeing it stop for the first time, and
34 it is only after the target sees the thread stop for the first
35 time that the target has a chance of determining the process's
36 architecture. IOW, when we first add the process's main thread
37 we don't know which architecture/tdesc its regcache should
41 process_info
*proc
= thread
->process ();
43 gdb_assert (proc
->tdesc
!= NULL
);
45 regcache
= new_register_cache (proc
->tdesc
);
46 thread
->set_regcache (regcache
);
49 if (fetch
&& !regcache
->registers_fetched
)
51 scoped_restore_current_thread restore_thread
;
53 switch_to_thread (thread
);
54 /* Invalidate all registers, to prevent stale left-overs. */
55 memset (regcache
->register_status
, REG_UNAVAILABLE
,
56 regcache
->tdesc
->reg_defs
.size ());
57 fetch_inferior_registers (regcache
, -1);
58 regcache
->registers_fetched
= true;
64 /* See gdbsupport/common-regcache.h. */
67 get_thread_regcache_for_ptid (ptid_t ptid
)
69 return get_thread_regcache (find_thread_ptid (ptid
));
73 regcache_invalidate_thread (thread_info
*thread
)
75 regcache
*regcache
= thread
->regcache ();
80 if (regcache
->registers_fetched
)
82 scoped_restore_current_thread restore_thread
;
84 switch_to_thread (thread
);
85 store_inferior_registers (regcache
, -1);
88 regcache
->registers_fetched
= false;
94 regcache_invalidate_pid (int pid
)
96 process_info
*process
= find_process_pid (pid
);
98 if (process
!= nullptr)
99 process
->for_each_thread (regcache_invalidate_thread
);
102 /* See regcache.h. */
105 regcache_invalidate (void)
107 /* Only update the threads of the current process. */
108 int pid
= current_thread
->id
.pid ();
110 regcache_invalidate_pid (pid
);
116 init_register_cache (struct regcache
*regcache
,
117 const struct target_desc
*tdesc
,
118 unsigned char *regbuf
)
122 #ifndef IN_PROCESS_AGENT
123 /* Make sure to zero-initialize the register cache when it is
124 created, in case there are registers the target never
125 fetches. This way they'll read as zero instead of
127 regcache
->tdesc
= tdesc
;
129 = (unsigned char *) xcalloc (1, tdesc
->registers_size
);
130 regcache
->registers_owned
= true;
131 regcache
->register_status
132 = (unsigned char *) xmalloc (tdesc
->reg_defs
.size ());
133 memset ((void *) regcache
->register_status
, REG_UNAVAILABLE
,
134 tdesc
->reg_defs
.size ());
136 gdb_assert_not_reached ("can't allocate memory from the heap");
141 regcache
->tdesc
= tdesc
;
142 regcache
->registers
= regbuf
;
143 regcache
->registers_owned
= false;
144 #ifndef IN_PROCESS_AGENT
145 regcache
->register_status
= NULL
;
149 regcache
->registers_fetched
= false;
154 #ifndef IN_PROCESS_AGENT
157 new_register_cache (const struct target_desc
*tdesc
)
159 struct regcache
*regcache
= new struct regcache
;
161 gdb_assert (tdesc
->registers_size
!= 0);
163 return init_register_cache (regcache
, tdesc
, NULL
);
167 free_register_cache (struct regcache
*regcache
)
171 if (regcache
->registers_owned
)
172 free (regcache
->registers
);
173 free (regcache
->register_status
);
181 regcache::copy_from (regcache
*src
)
183 gdb_assert (src
!= nullptr);
184 gdb_assert (src
->tdesc
== this->tdesc
);
185 gdb_assert (src
!= this);
187 memcpy (this->registers
, src
->registers
, src
->tdesc
->registers_size
);
188 #ifndef IN_PROCESS_AGENT
189 if (this->register_status
!= nullptr && src
->register_status
!= nullptr)
190 memcpy (this->register_status
, src
->register_status
,
191 src
->tdesc
->reg_defs
.size ());
193 this->registers_fetched
= src
->registers_fetched
;
196 /* Return a reference to the description of register N. */
198 static const struct gdb::reg
&
199 find_register_by_number (const struct target_desc
*tdesc
, int n
)
202 gdb_assert (n
< tdesc
->reg_defs
.size ());
204 return tdesc
->reg_defs
[n
];
207 #ifndef IN_PROCESS_AGENT
210 registers_to_string (struct regcache
*regcache
, char *buf
)
212 unsigned char *registers
= regcache
->registers
;
213 const struct target_desc
*tdesc
= regcache
->tdesc
;
215 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
217 if (regcache
->register_status
[i
] == REG_VALID
)
219 bin2hex (registers
, buf
, register_size (tdesc
, i
));
220 buf
+= register_size (tdesc
, i
) * 2;
224 memset (buf
, 'x', register_size (tdesc
, i
) * 2);
225 buf
+= register_size (tdesc
, i
) * 2;
227 registers
+= register_size (tdesc
, i
);
233 registers_from_string (struct regcache
*regcache
, char *buf
)
235 int len
= strlen (buf
);
236 unsigned char *registers
= regcache
->registers
;
237 const struct target_desc
*tdesc
= regcache
->tdesc
;
239 if (len
!= tdesc
->registers_size
* 2)
241 warning ("Wrong sized register packet (expected %d bytes, got %d)",
242 2 * tdesc
->registers_size
, len
);
243 if (len
> tdesc
->registers_size
* 2)
244 len
= tdesc
->registers_size
* 2;
246 hex2bin (buf
, registers
, len
/ 2);
252 find_regno_no_throw (const struct target_desc
*tdesc
, const char *name
)
254 for (int i
= 0; i
< tdesc
->reg_defs
.size (); ++i
)
256 if (strcmp (name
, find_register_by_number (tdesc
, i
).name
) == 0)
263 find_regno (const struct target_desc
*tdesc
, const char *name
)
265 std::optional
<int> regnum
= find_regno_no_throw (tdesc
, name
);
267 if (regnum
.has_value ())
270 internal_error ("Unknown register %s requested", name
);
274 free_register_cache_thread (thread_info
*thread
)
276 regcache
*regcache
= thread
->regcache ();
278 if (regcache
!= NULL
)
280 regcache_invalidate_thread (thread
);
281 free_register_cache (regcache
);
282 thread
->set_regcache (nullptr);
287 regcache_release (void)
289 /* Flush and release all pre-existing register caches. */
290 for_each_thread (free_register_cache_thread
);
295 register_cache_size (const struct target_desc
*tdesc
)
297 return tdesc
->registers_size
;
301 register_size (const struct target_desc
*tdesc
, int n
)
303 return find_register_by_number (tdesc
, n
).size
/ 8;
306 /* See gdbsupport/common-regcache.h. */
309 regcache_register_size (const reg_buffer_common
*regcache
, int n
)
312 (gdb::checked_static_cast
<const struct regcache
*> (regcache
)->tdesc
, n
);
315 static gdb::array_view
<gdb_byte
>
316 register_data (const struct regcache
*regcache
, int n
)
318 const gdb::reg
®
= find_register_by_number (regcache
->tdesc
, n
);
319 return gdb::make_array_view (regcache
->registers
+ reg
.offset
/ 8,
324 supply_register (struct regcache
*regcache
, int n
, const void *vbuf
)
326 const gdb::reg
®
= find_register_by_number (regcache
->tdesc
, n
);
327 const gdb_byte
*buf
= static_cast<const gdb_byte
*> (vbuf
);
328 return regcache
->raw_supply (n
, gdb::make_array_view (buf
, reg
.size
/ 8));
331 /* See gdbsupport/common-regcache.h. */
334 regcache::raw_supply (int n
, gdb::array_view
<const gdb_byte
> src
)
336 auto dst
= register_data (this, n
);
338 if (src
.data () != nullptr)
341 #ifndef IN_PROCESS_AGENT
342 if (register_status
!= NULL
)
343 register_status
[n
] = REG_VALID
;
348 memset (dst
.data (), 0, dst
.size ());
349 #ifndef IN_PROCESS_AGENT
350 if (register_status
!= NULL
)
351 register_status
[n
] = REG_UNAVAILABLE
;
356 /* Supply register N with value zero to REGCACHE. */
359 supply_register_zeroed (struct regcache
*regcache
, int n
)
361 auto dst
= register_data (regcache
, n
);
362 memset (dst
.data (), 0, dst
.size ());
363 #ifndef IN_PROCESS_AGENT
364 if (regcache
->register_status
!= NULL
)
365 regcache
->register_status
[n
] = REG_VALID
;
370 regcache::raw_supply_part_zeroed (int regnum
, int offset
, size_t size
)
372 auto dst
= register_data (this, regnum
).slice (offset
, size
);
373 memset (dst
.data (), 0, dst
.size ());
374 #ifndef IN_PROCESS_AGENT
375 if (register_status
!= NULL
)
376 register_status
[regnum
] = REG_VALID
;
380 #ifndef IN_PROCESS_AGENT
382 /* Supply register called NAME with value zero to REGCACHE. */
385 supply_register_by_name_zeroed (struct regcache
*regcache
,
388 supply_register_zeroed (regcache
, find_regno (regcache
->tdesc
, name
));
393 /* Supply the whole register set whose contents are stored in BUF, to
394 REGCACHE. If BUF is NULL, all the registers' values are recorded
398 supply_regblock (struct regcache
*regcache
, const void *buf
)
402 const struct target_desc
*tdesc
= regcache
->tdesc
;
404 memcpy (regcache
->registers
, buf
, tdesc
->registers_size
);
405 #ifndef IN_PROCESS_AGENT
409 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
410 regcache
->register_status
[i
] = REG_VALID
;
416 const struct target_desc
*tdesc
= regcache
->tdesc
;
418 memset (regcache
->registers
, 0, tdesc
->registers_size
);
419 #ifndef IN_PROCESS_AGENT
423 for (i
= 0; i
< tdesc
->reg_defs
.size (); i
++)
424 regcache
->register_status
[i
] = REG_UNAVAILABLE
;
430 #ifndef IN_PROCESS_AGENT
433 supply_register_by_name (struct regcache
*regcache
,
434 const char *name
, const void *buf
)
436 supply_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
442 collect_register (struct regcache
*regcache
, int n
, void *vbuf
)
444 const gdb::reg
®
= find_register_by_number (regcache
->tdesc
, n
);
445 gdb_byte
*buf
= static_cast<gdb_byte
*> (vbuf
);
446 regcache
->raw_collect (n
, gdb::make_array_view (buf
, reg
.size
/ 8));
449 /* See gdbsupport/common-regcache.h. */
452 regcache::raw_collect (int n
, gdb::array_view
<gdb_byte
> dst
) const
454 auto src
= register_data (this, n
);
459 regcache_raw_read_unsigned (reg_buffer_common
*reg_buf
, int regnum
,
463 regcache
*regcache
= gdb::checked_static_cast
<struct regcache
*> (reg_buf
);
465 gdb_assert (regcache
!= NULL
);
467 size
= register_size (regcache
->tdesc
, regnum
);
469 if (size
> (int) sizeof (ULONGEST
))
470 error (_("That operation is not available on integers of more than"
472 (int) sizeof (ULONGEST
));
475 collect_register (regcache
, regnum
, val
);
477 return regcache
->get_register_status (regnum
);
480 #ifndef IN_PROCESS_AGENT
482 /* See regcache.h. */
485 regcache_raw_get_unsigned_by_name (struct regcache
*regcache
,
488 return regcache_raw_get_unsigned (regcache
,
489 find_regno (regcache
->tdesc
, name
));
493 collect_register_as_string (struct regcache
*regcache
, int n
, char *buf
)
495 bin2hex (register_data (regcache
, n
), buf
);
499 collect_register_by_name (struct regcache
*regcache
,
500 const char *name
, void *buf
)
502 collect_register (regcache
, find_regno (regcache
->tdesc
, name
), buf
);
505 /* Special handling for register PC. */
508 regcache_read_pc (reg_buffer_common
*regcache
)
510 return the_target
->read_pc
511 (gdb::checked_static_cast
<struct regcache
*> (regcache
));
515 regcache_write_pc (struct regcache
*regcache
, CORE_ADDR pc
)
517 the_target
->write_pc (regcache
, pc
);
522 /* See gdbsupport/common-regcache.h. */
525 regcache::get_register_status (int regnum
) const
527 #ifndef IN_PROCESS_AGENT
528 gdb_assert (regnum
>= 0 && regnum
< tdesc
->reg_defs
.size ());
529 if (register_status
!= nullptr)
530 return (enum register_status
) (register_status
[regnum
]);
538 /* See gdbsupport/common-regcache.h. */
541 regcache::raw_compare (int regnum
, const void *buf
, int offset
) const
543 gdb_assert (buf
!= NULL
);
545 gdb::array_view
<const gdb_byte
> regbuf
= register_data (this, regnum
);
546 gdb_assert (offset
< regbuf
.size ());
547 regbuf
= regbuf
.slice (offset
);
549 return memcmp (buf
, regbuf
.data (), regbuf
.size ()) == 0;