| 1 |
/* Register support routines for the remote server for GDB. |
| 2 |
Copyright (C) 2001-2025 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is part of GDB. |
| 5 |
|
| 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. |
| 10 |
|
| 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. |
| 15 |
|
| 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/>. */ |
| 18 |
|
| 19 |
#include "regdef.h" |
| 20 |
#include "gdbthread.h" |
| 21 |
#include "tdesc.h" |
| 22 |
#include "gdbsupport/rsp-low.h" |
| 23 |
#include "gdbsupport/gdb-checked-static-cast.h" |
| 24 |
|
| 25 |
#ifndef IN_PROCESS_AGENT |
| 26 |
|
| 27 |
struct regcache * |
| 28 |
get_thread_regcache (thread_info *thread, bool fetch) |
| 29 |
{ |
| 30 |
regcache *regcache = thread->regcache (); |
| 31 |
|
| 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 |
| 38 |
have. */ |
| 39 |
if (regcache == NULL) |
| 40 |
{ |
| 41 |
process_info *proc = thread->process (); |
| 42 |
|
| 43 |
gdb_assert (proc->tdesc != NULL); |
| 44 |
|
| 45 |
thread->set_regcache (std::make_unique<struct regcache> (proc->tdesc)); |
| 46 |
regcache = thread->regcache (); |
| 47 |
} |
| 48 |
|
| 49 |
if (fetch && !regcache->registers_fetched) |
| 50 |
{ |
| 51 |
scoped_restore_current_thread restore_thread; |
| 52 |
|
| 53 |
switch_to_thread (thread); |
| 54 |
/* Invalidate all registers, to prevent stale left-overs. */ |
| 55 |
regcache->reset (REG_UNKNOWN); |
| 56 |
fetch_inferior_registers (regcache, -1); |
| 57 |
regcache->registers_fetched = true; |
| 58 |
} |
| 59 |
|
| 60 |
return regcache; |
| 61 |
} |
| 62 |
|
| 63 |
/* See gdbsupport/common-regcache.h. */ |
| 64 |
|
| 65 |
reg_buffer_common * |
| 66 |
get_thread_regcache_for_ptid (ptid_t ptid) |
| 67 |
{ |
| 68 |
return get_thread_regcache (find_thread_ptid (ptid)); |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
regcache_invalidate_thread (thread_info *thread) |
| 73 |
{ |
| 74 |
regcache *regcache = thread->regcache (); |
| 75 |
|
| 76 |
if (regcache == NULL) |
| 77 |
return; |
| 78 |
|
| 79 |
if (regcache->registers_fetched) |
| 80 |
{ |
| 81 |
scoped_restore_current_thread restore_thread; |
| 82 |
|
| 83 |
switch_to_thread (thread); |
| 84 |
store_inferior_registers (regcache, -1); |
| 85 |
} |
| 86 |
|
| 87 |
regcache->registers_fetched = false; |
| 88 |
} |
| 89 |
|
| 90 |
/* See regcache.h. */ |
| 91 |
|
| 92 |
void |
| 93 |
regcache_invalidate_pid (int pid) |
| 94 |
{ |
| 95 |
process_info *process = find_process_pid (pid); |
| 96 |
|
| 97 |
if (process != nullptr) |
| 98 |
process->for_each_thread (regcache_invalidate_thread); |
| 99 |
} |
| 100 |
|
| 101 |
/* See regcache.h. */ |
| 102 |
|
| 103 |
void |
| 104 |
regcache_invalidate (void) |
| 105 |
{ |
| 106 |
/* Only update the threads of the current process. */ |
| 107 |
int pid = current_thread->id.pid (); |
| 108 |
|
| 109 |
regcache_invalidate_pid (pid); |
| 110 |
} |
| 111 |
|
| 112 |
#endif |
| 113 |
|
| 114 |
regcache::regcache (const target_desc *tdesc, unsigned char *regbuf) |
| 115 |
: tdesc (tdesc), registers (regbuf) |
| 116 |
{ |
| 117 |
gdb_assert (regbuf != nullptr); |
| 118 |
} |
| 119 |
|
| 120 |
#ifndef IN_PROCESS_AGENT |
| 121 |
|
| 122 |
regcache::regcache (const target_desc *tdesc) |
| 123 |
: tdesc (tdesc), registers_owned (true) |
| 124 |
{ |
| 125 |
gdb_assert (tdesc->registers_size != 0); |
| 126 |
|
| 127 |
/* Make sure to zero-initialize the register cache when it is |
| 128 |
created, in case there are registers the target never |
| 129 |
fetches. This way they'll read as zero instead of |
| 130 |
garbage. */ |
| 131 |
this->registers |
| 132 |
= (unsigned char *) xmalloc (tdesc->registers_size); |
| 133 |
size_t num_regs = tdesc->reg_defs.size (); |
| 134 |
m_register_status.reset (new enum register_status[num_regs]); |
| 135 |
reset (REG_UNKNOWN); |
| 136 |
} |
| 137 |
|
| 138 |
regcache::~regcache () |
| 139 |
{ |
| 140 |
if (registers_owned) |
| 141 |
free (registers); |
| 142 |
} |
| 143 |
|
| 144 |
#endif |
| 145 |
|
| 146 |
void |
| 147 |
regcache::reset (enum register_status status) |
| 148 |
{ |
| 149 |
/* Zero-initialize the register cache, in case there are registers |
| 150 |
the target never fetches. This way they'll read as zero instead |
| 151 |
of garbage. */ |
| 152 |
memset (this->registers, 0, this->tdesc->registers_size); |
| 153 |
#ifndef IN_PROCESS_AGENT |
| 154 |
for (int i = 0; i < this->tdesc->reg_defs.size (); i++) |
| 155 |
set_register_status (i, status); |
| 156 |
#endif |
| 157 |
} |
| 158 |
|
| 159 |
void |
| 160 |
regcache::copy_from (regcache *src) |
| 161 |
{ |
| 162 |
gdb_assert (src != nullptr); |
| 163 |
gdb_assert (src->tdesc == this->tdesc); |
| 164 |
gdb_assert (src != this); |
| 165 |
|
| 166 |
memcpy (this->registers, src->registers, src->tdesc->registers_size); |
| 167 |
#ifndef IN_PROCESS_AGENT |
| 168 |
if (m_register_status != nullptr && src->m_register_status != nullptr) |
| 169 |
memcpy (m_register_status.get (), src->m_register_status.get (), |
| 170 |
src->tdesc->reg_defs.size ()); |
| 171 |
#endif |
| 172 |
this->registers_fetched = src->registers_fetched; |
| 173 |
} |
| 174 |
|
| 175 |
/* Return a reference to the description of register N. */ |
| 176 |
|
| 177 |
static const struct gdb::reg & |
| 178 |
find_register_by_number (const struct target_desc *tdesc, int n) |
| 179 |
{ |
| 180 |
gdb_assert (n >= 0); |
| 181 |
gdb_assert (n < tdesc->reg_defs.size ()); |
| 182 |
|
| 183 |
return tdesc->reg_defs[n]; |
| 184 |
} |
| 185 |
|
| 186 |
#ifndef IN_PROCESS_AGENT |
| 187 |
|
| 188 |
void |
| 189 |
registers_to_string (struct regcache *regcache, char *buf) |
| 190 |
{ |
| 191 |
const struct target_desc *tdesc = regcache->tdesc; |
| 192 |
|
| 193 |
for (int i = 0; i < tdesc->reg_defs.size (); ++i) |
| 194 |
{ |
| 195 |
collect_register_as_string (regcache, i, buf); |
| 196 |
buf += register_size (tdesc, i) * 2; |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
void |
| 201 |
registers_from_string (struct regcache *regcache, char *buf) |
| 202 |
{ |
| 203 |
int len = strlen (buf); |
| 204 |
unsigned char *registers = regcache->registers; |
| 205 |
const struct target_desc *tdesc = regcache->tdesc; |
| 206 |
|
| 207 |
if (len != tdesc->registers_size * 2) |
| 208 |
{ |
| 209 |
warning ("Wrong sized register packet (expected %d bytes, got %d)", |
| 210 |
2 * tdesc->registers_size, len); |
| 211 |
if (len > tdesc->registers_size * 2) |
| 212 |
len = tdesc->registers_size * 2; |
| 213 |
} |
| 214 |
hex2bin (buf, registers, len / 2); |
| 215 |
} |
| 216 |
|
| 217 |
/* See regcache.h */ |
| 218 |
|
| 219 |
std::optional<int> |
| 220 |
find_regno_no_throw (const struct target_desc *tdesc, const char *name) |
| 221 |
{ |
| 222 |
for (int i = 0; i < tdesc->reg_defs.size (); ++i) |
| 223 |
{ |
| 224 |
if (strcmp (name, find_register_by_number (tdesc, i).name) == 0) |
| 225 |
return i; |
| 226 |
} |
| 227 |
return {}; |
| 228 |
} |
| 229 |
|
| 230 |
int |
| 231 |
find_regno (const struct target_desc *tdesc, const char *name) |
| 232 |
{ |
| 233 |
std::optional<int> regnum = find_regno_no_throw (tdesc, name); |
| 234 |
|
| 235 |
if (regnum.has_value ()) |
| 236 |
return *regnum; |
| 237 |
|
| 238 |
internal_error ("Unknown register %s requested", name); |
| 239 |
} |
| 240 |
|
| 241 |
static void |
| 242 |
free_register_cache_thread (thread_info *thread) |
| 243 |
{ |
| 244 |
regcache *regcache = thread->regcache (); |
| 245 |
|
| 246 |
if (regcache != NULL) |
| 247 |
{ |
| 248 |
regcache_invalidate_thread (thread); |
| 249 |
thread->set_regcache (nullptr); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
void |
| 254 |
regcache_release (void) |
| 255 |
{ |
| 256 |
/* Flush and release all pre-existing register caches. */ |
| 257 |
for_each_thread (free_register_cache_thread); |
| 258 |
} |
| 259 |
#endif |
| 260 |
|
| 261 |
int |
| 262 |
register_cache_size (const struct target_desc *tdesc) |
| 263 |
{ |
| 264 |
return tdesc->registers_size; |
| 265 |
} |
| 266 |
|
| 267 |
int |
| 268 |
register_size (const struct target_desc *tdesc, int n) |
| 269 |
{ |
| 270 |
return find_register_by_number (tdesc, n).size / 8; |
| 271 |
} |
| 272 |
|
| 273 |
/* See gdbsupport/common-regcache.h. */ |
| 274 |
|
| 275 |
int |
| 276 |
regcache::register_size (int regnum) const |
| 277 |
{ |
| 278 |
return ::register_size (tdesc, regnum); |
| 279 |
} |
| 280 |
|
| 281 |
static gdb::array_view<gdb_byte> |
| 282 |
register_data (const struct regcache *regcache, int n) |
| 283 |
{ |
| 284 |
const gdb::reg ® = find_register_by_number (regcache->tdesc, n); |
| 285 |
return gdb::make_array_view (regcache->registers + reg.offset / 8, |
| 286 |
reg.size / 8); |
| 287 |
} |
| 288 |
|
| 289 |
void |
| 290 |
supply_register (struct regcache *regcache, int n, const void *vbuf) |
| 291 |
{ |
| 292 |
const gdb::reg ® = find_register_by_number (regcache->tdesc, n); |
| 293 |
const gdb_byte *buf = static_cast<const gdb_byte *> (vbuf); |
| 294 |
return regcache->raw_supply (n, gdb::make_array_view (buf, reg.size / 8)); |
| 295 |
} |
| 296 |
|
| 297 |
/* See gdbsupport/common-regcache.h. */ |
| 298 |
|
| 299 |
void |
| 300 |
regcache::raw_supply (int n, gdb::array_view<const gdb_byte> src) |
| 301 |
{ |
| 302 |
auto dst = register_data (this, n); |
| 303 |
|
| 304 |
if (src.data () != nullptr) |
| 305 |
{ |
| 306 |
copy (src, dst); |
| 307 |
#ifndef IN_PROCESS_AGENT |
| 308 |
set_register_status (n, REG_VALID); |
| 309 |
#endif |
| 310 |
} |
| 311 |
else |
| 312 |
{ |
| 313 |
memset (dst.data (), 0, dst.size ()); |
| 314 |
#ifndef IN_PROCESS_AGENT |
| 315 |
set_register_status (n, REG_UNAVAILABLE); |
| 316 |
#endif |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/* Supply register N with value zero to REGCACHE. */ |
| 321 |
|
| 322 |
void |
| 323 |
supply_register_zeroed (struct regcache *regcache, int n) |
| 324 |
{ |
| 325 |
auto dst = register_data (regcache, n); |
| 326 |
memset (dst.data (), 0, dst.size ()); |
| 327 |
#ifndef IN_PROCESS_AGENT |
| 328 |
regcache->set_register_status (n, REG_VALID); |
| 329 |
#endif |
| 330 |
} |
| 331 |
|
| 332 |
void |
| 333 |
regcache::raw_supply_part_zeroed (int regnum, int offset, size_t size) |
| 334 |
{ |
| 335 |
auto dst = register_data (this, regnum).slice (offset, size); |
| 336 |
memset (dst.data (), 0, dst.size ()); |
| 337 |
#ifndef IN_PROCESS_AGENT |
| 338 |
set_register_status (regnum, REG_VALID); |
| 339 |
#endif |
| 340 |
} |
| 341 |
|
| 342 |
#ifndef IN_PROCESS_AGENT |
| 343 |
|
| 344 |
/* Supply register called NAME with value zero to REGCACHE. */ |
| 345 |
|
| 346 |
void |
| 347 |
supply_register_by_name_zeroed (struct regcache *regcache, |
| 348 |
const char *name) |
| 349 |
{ |
| 350 |
supply_register_zeroed (regcache, find_regno (regcache->tdesc, name)); |
| 351 |
} |
| 352 |
|
| 353 |
#endif |
| 354 |
|
| 355 |
/* Supply the whole register set whose contents are stored in BUF, to |
| 356 |
REGCACHE. */ |
| 357 |
|
| 358 |
void |
| 359 |
supply_regblock (struct regcache *regcache, const void *buf) |
| 360 |
{ |
| 361 |
gdb_assert (buf != nullptr); |
| 362 |
const struct target_desc *tdesc = regcache->tdesc; |
| 363 |
|
| 364 |
memcpy (regcache->registers, buf, tdesc->registers_size); |
| 365 |
#ifndef IN_PROCESS_AGENT |
| 366 |
for (int i = 0; i < tdesc->reg_defs.size (); i++) |
| 367 |
regcache->set_register_status (i, REG_VALID); |
| 368 |
#endif |
| 369 |
} |
| 370 |
|
| 371 |
#ifndef IN_PROCESS_AGENT |
| 372 |
|
| 373 |
void |
| 374 |
supply_register_by_name (struct regcache *regcache, |
| 375 |
const char *name, const void *buf) |
| 376 |
{ |
| 377 |
supply_register (regcache, find_regno (regcache->tdesc, name), buf); |
| 378 |
} |
| 379 |
|
| 380 |
#endif |
| 381 |
|
| 382 |
void |
| 383 |
collect_register (struct regcache *regcache, int n, void *vbuf) |
| 384 |
{ |
| 385 |
const gdb::reg ® = find_register_by_number (regcache->tdesc, n); |
| 386 |
gdb_byte *buf = static_cast<gdb_byte *> (vbuf); |
| 387 |
regcache->raw_collect (n, gdb::make_array_view (buf, reg.size / 8)); |
| 388 |
} |
| 389 |
|
| 390 |
/* See gdbsupport/common-regcache.h. */ |
| 391 |
|
| 392 |
void |
| 393 |
regcache::raw_collect (int n, gdb::array_view<gdb_byte> dst) const |
| 394 |
{ |
| 395 |
auto src = register_data (this, n); |
| 396 |
copy (src, dst); |
| 397 |
} |
| 398 |
|
| 399 |
enum register_status |
| 400 |
regcache_raw_read_unsigned (reg_buffer_common *reg_buf, int regnum, |
| 401 |
ULONGEST *val) |
| 402 |
{ |
| 403 |
int size; |
| 404 |
regcache *regcache = gdb::checked_static_cast<struct regcache *> (reg_buf); |
| 405 |
|
| 406 |
gdb_assert (regcache != NULL); |
| 407 |
|
| 408 |
size = register_size (regcache->tdesc, regnum); |
| 409 |
|
| 410 |
if (size > (int) sizeof (ULONGEST)) |
| 411 |
error (_("That operation is not available on integers of more than" |
| 412 |
"%d bytes."), |
| 413 |
(int) sizeof (ULONGEST)); |
| 414 |
|
| 415 |
*val = 0; |
| 416 |
collect_register (regcache, regnum, val); |
| 417 |
|
| 418 |
return regcache->get_register_status (regnum); |
| 419 |
} |
| 420 |
|
| 421 |
#ifndef IN_PROCESS_AGENT |
| 422 |
|
| 423 |
/* See regcache.h. */ |
| 424 |
|
| 425 |
ULONGEST |
| 426 |
regcache_raw_get_unsigned_by_name (struct regcache *regcache, |
| 427 |
const char *name) |
| 428 |
{ |
| 429 |
return regcache_raw_get_unsigned (regcache, |
| 430 |
find_regno (regcache->tdesc, name)); |
| 431 |
} |
| 432 |
|
| 433 |
void |
| 434 |
collect_register_as_string (struct regcache *regcache, int n, char *buf) |
| 435 |
{ |
| 436 |
int reg_size = register_size (regcache->tdesc, n); |
| 437 |
|
| 438 |
if (regcache->get_register_status (n) == REG_VALID) |
| 439 |
bin2hex (register_data (regcache, n), buf); |
| 440 |
else |
| 441 |
memset (buf, 'x', reg_size * 2); |
| 442 |
|
| 443 |
buf += reg_size * 2; |
| 444 |
*buf = '\0'; |
| 445 |
} |
| 446 |
|
| 447 |
void |
| 448 |
collect_register_by_name (struct regcache *regcache, |
| 449 |
const char *name, void *buf) |
| 450 |
{ |
| 451 |
collect_register (regcache, find_regno (regcache->tdesc, name), buf); |
| 452 |
} |
| 453 |
|
| 454 |
/* Special handling for register PC. */ |
| 455 |
|
| 456 |
CORE_ADDR |
| 457 |
regcache_read_pc (reg_buffer_common *regcache) |
| 458 |
{ |
| 459 |
return the_target->read_pc |
| 460 |
(gdb::checked_static_cast<struct regcache *> (regcache)); |
| 461 |
} |
| 462 |
|
| 463 |
void |
| 464 |
regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
| 465 |
{ |
| 466 |
the_target->write_pc (regcache, pc); |
| 467 |
} |
| 468 |
|
| 469 |
#endif |
| 470 |
|
| 471 |
/* See gdbsupport/common-regcache.h. */ |
| 472 |
|
| 473 |
enum register_status |
| 474 |
regcache::get_register_status (int regnum) const |
| 475 |
{ |
| 476 |
#ifndef IN_PROCESS_AGENT |
| 477 |
gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ()); |
| 478 |
if (m_register_status != nullptr) |
| 479 |
return m_register_status[regnum]; |
| 480 |
else |
| 481 |
return REG_VALID; |
| 482 |
#else |
| 483 |
return REG_VALID; |
| 484 |
#endif |
| 485 |
} |
| 486 |
|
| 487 |
void |
| 488 |
regcache::set_register_status (int regnum, enum register_status status) |
| 489 |
{ |
| 490 |
#ifndef IN_PROCESS_AGENT |
| 491 |
gdb_assert (regnum >= 0 && regnum < tdesc->reg_defs.size ()); |
| 492 |
if (m_register_status != nullptr) |
| 493 |
m_register_status[regnum] = status; |
| 494 |
#endif |
| 495 |
} |
| 496 |
|
| 497 |
/* See gdbsupport/common-regcache.h. */ |
| 498 |
|
| 499 |
bool |
| 500 |
regcache::raw_compare (int regnum, const void *buf, int offset) const |
| 501 |
{ |
| 502 |
gdb_assert (buf != NULL); |
| 503 |
|
| 504 |
gdb::array_view<const gdb_byte> regbuf = register_data (this, regnum); |
| 505 |
gdb_assert (offset <= regbuf.size ()); |
| 506 |
regbuf = regbuf.slice (offset); |
| 507 |
|
| 508 |
return memcmp (buf, regbuf.data (), regbuf.size ()) == 0; |
| 509 |
} |