| 1 | /* Cache and manage the values of registers for GDB, the GNU debugger. |
| 2 | |
| 3 | Copyright (C) 1986-2018 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 | #include "defs.h" |
| 21 | #include "inferior.h" |
| 22 | #include "target.h" |
| 23 | #include "gdbarch.h" |
| 24 | #include "gdbcmd.h" |
| 25 | #include "regcache.h" |
| 26 | #include "reggroups.h" |
| 27 | #include "observer.h" |
| 28 | #include "remote.h" |
| 29 | #include "valprint.h" |
| 30 | #include "regset.h" |
| 31 | #include <forward_list> |
| 32 | |
| 33 | /* |
| 34 | * DATA STRUCTURE |
| 35 | * |
| 36 | * Here is the actual register cache. |
| 37 | */ |
| 38 | |
| 39 | /* Per-architecture object describing the layout of a register cache. |
| 40 | Computed once when the architecture is created. */ |
| 41 | |
| 42 | struct gdbarch_data *regcache_descr_handle; |
| 43 | |
| 44 | struct regcache_descr |
| 45 | { |
| 46 | /* The architecture this descriptor belongs to. */ |
| 47 | struct gdbarch *gdbarch; |
| 48 | |
| 49 | /* The raw register cache. Each raw (or hard) register is supplied |
| 50 | by the target interface. The raw cache should not contain |
| 51 | redundant information - if the PC is constructed from two |
| 52 | registers then those registers and not the PC lives in the raw |
| 53 | cache. */ |
| 54 | long sizeof_raw_registers; |
| 55 | |
| 56 | /* The cooked register space. Each cooked register in the range |
| 57 | [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw |
| 58 | register. The remaining [NR_RAW_REGISTERS |
| 59 | .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto |
| 60 | both raw registers and memory by the architecture methods |
| 61 | gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */ |
| 62 | int nr_cooked_registers; |
| 63 | long sizeof_cooked_registers; |
| 64 | |
| 65 | /* Offset and size (in 8 bit bytes), of each register in the |
| 66 | register cache. All registers (including those in the range |
| 67 | [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an |
| 68 | offset. */ |
| 69 | long *register_offset; |
| 70 | long *sizeof_register; |
| 71 | |
| 72 | /* Cached table containing the type of each register. */ |
| 73 | struct type **register_type; |
| 74 | }; |
| 75 | |
| 76 | static void * |
| 77 | init_regcache_descr (struct gdbarch *gdbarch) |
| 78 | { |
| 79 | int i; |
| 80 | struct regcache_descr *descr; |
| 81 | gdb_assert (gdbarch != NULL); |
| 82 | |
| 83 | /* Create an initial, zero filled, table. */ |
| 84 | descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr); |
| 85 | descr->gdbarch = gdbarch; |
| 86 | |
| 87 | /* Total size of the register space. The raw registers are mapped |
| 88 | directly onto the raw register cache while the pseudo's are |
| 89 | either mapped onto raw-registers or memory. */ |
| 90 | descr->nr_cooked_registers = gdbarch_num_regs (gdbarch) |
| 91 | + gdbarch_num_pseudo_regs (gdbarch); |
| 92 | |
| 93 | /* Fill in a table of register types. */ |
| 94 | descr->register_type |
| 95 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, |
| 96 | struct type *); |
| 97 | for (i = 0; i < descr->nr_cooked_registers; i++) |
| 98 | descr->register_type[i] = gdbarch_register_type (gdbarch, i); |
| 99 | |
| 100 | /* Construct a strictly RAW register cache. Don't allow pseudo's |
| 101 | into the register cache. */ |
| 102 | |
| 103 | /* Lay out the register cache. |
| 104 | |
| 105 | NOTE: cagney/2002-05-22: Only register_type() is used when |
| 106 | constructing the register cache. It is assumed that the |
| 107 | register's raw size, virtual size and type length are all the |
| 108 | same. */ |
| 109 | |
| 110 | { |
| 111 | long offset = 0; |
| 112 | |
| 113 | descr->sizeof_register |
| 114 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); |
| 115 | descr->register_offset |
| 116 | = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); |
| 117 | for (i = 0; i < gdbarch_num_regs (gdbarch); i++) |
| 118 | { |
| 119 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
| 120 | descr->register_offset[i] = offset; |
| 121 | offset += descr->sizeof_register[i]; |
| 122 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
| 123 | } |
| 124 | /* Set the real size of the raw register cache buffer. */ |
| 125 | descr->sizeof_raw_registers = offset; |
| 126 | |
| 127 | for (; i < descr->nr_cooked_registers; i++) |
| 128 | { |
| 129 | descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]); |
| 130 | descr->register_offset[i] = offset; |
| 131 | offset += descr->sizeof_register[i]; |
| 132 | gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]); |
| 133 | } |
| 134 | /* Set the real size of the readonly register cache buffer. */ |
| 135 | descr->sizeof_cooked_registers = offset; |
| 136 | } |
| 137 | |
| 138 | return descr; |
| 139 | } |
| 140 | |
| 141 | static struct regcache_descr * |
| 142 | regcache_descr (struct gdbarch *gdbarch) |
| 143 | { |
| 144 | return (struct regcache_descr *) gdbarch_data (gdbarch, |
| 145 | regcache_descr_handle); |
| 146 | } |
| 147 | |
| 148 | /* Utility functions returning useful register attributes stored in |
| 149 | the regcache descr. */ |
| 150 | |
| 151 | struct type * |
| 152 | register_type (struct gdbarch *gdbarch, int regnum) |
| 153 | { |
| 154 | struct regcache_descr *descr = regcache_descr (gdbarch); |
| 155 | |
| 156 | gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); |
| 157 | return descr->register_type[regnum]; |
| 158 | } |
| 159 | |
| 160 | /* Utility functions returning useful register attributes stored in |
| 161 | the regcache descr. */ |
| 162 | |
| 163 | int |
| 164 | register_size (struct gdbarch *gdbarch, int regnum) |
| 165 | { |
| 166 | struct regcache_descr *descr = regcache_descr (gdbarch); |
| 167 | int size; |
| 168 | |
| 169 | gdb_assert (regnum >= 0 |
| 170 | && regnum < (gdbarch_num_regs (gdbarch) |
| 171 | + gdbarch_num_pseudo_regs (gdbarch))); |
| 172 | size = descr->sizeof_register[regnum]; |
| 173 | return size; |
| 174 | } |
| 175 | |
| 176 | /* See common/common-regcache.h. */ |
| 177 | |
| 178 | int |
| 179 | regcache_register_size (const struct regcache *regcache, int n) |
| 180 | { |
| 181 | return register_size (regcache->arch (), n); |
| 182 | } |
| 183 | |
| 184 | reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo) |
| 185 | : m_has_pseudo (has_pseudo) |
| 186 | { |
| 187 | gdb_assert (gdbarch != NULL); |
| 188 | m_descr = regcache_descr (gdbarch); |
| 189 | |
| 190 | if (has_pseudo) |
| 191 | { |
| 192 | m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers); |
| 193 | m_register_status = XCNEWVEC (signed char, |
| 194 | m_descr->nr_cooked_registers); |
| 195 | } |
| 196 | else |
| 197 | { |
| 198 | m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers); |
| 199 | m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | regcache::regcache (gdbarch *gdbarch, const address_space *aspace_, |
| 204 | bool readonly_p_) |
| 205 | /* The register buffers. A read-only register cache can hold the |
| 206 | full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a |
| 207 | read/write register cache can only hold [0 .. gdbarch_num_regs). */ |
| 208 | : readable_regcache (gdbarch, readonly_p_), |
| 209 | m_aspace (aspace_), m_readonly_p (readonly_p_) |
| 210 | { |
| 211 | m_ptid = minus_one_ptid; |
| 212 | } |
| 213 | |
| 214 | static enum register_status |
| 215 | do_cooked_read (void *src, int regnum, gdb_byte *buf) |
| 216 | { |
| 217 | struct regcache *regcache = (struct regcache *) src; |
| 218 | |
| 219 | return regcache_cooked_read (regcache, regnum, buf); |
| 220 | } |
| 221 | |
| 222 | regcache::regcache (readonly_t, const regcache &src) |
| 223 | : regcache (src.arch (), nullptr, true) |
| 224 | { |
| 225 | gdb_assert (!src.m_readonly_p); |
| 226 | save (do_cooked_read, (void *) &src); |
| 227 | } |
| 228 | |
| 229 | gdbarch * |
| 230 | reg_buffer::arch () const |
| 231 | { |
| 232 | return m_descr->gdbarch; |
| 233 | } |
| 234 | |
| 235 | /* See regcache.h. */ |
| 236 | |
| 237 | ptid_t |
| 238 | regcache_get_ptid (const struct regcache *regcache) |
| 239 | { |
| 240 | gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid)); |
| 241 | |
| 242 | return regcache->ptid (); |
| 243 | } |
| 244 | |
| 245 | /* Cleanup class for invalidating a register. */ |
| 246 | |
| 247 | class regcache_invalidator |
| 248 | { |
| 249 | public: |
| 250 | |
| 251 | regcache_invalidator (struct regcache *regcache, int regnum) |
| 252 | : m_regcache (regcache), |
| 253 | m_regnum (regnum) |
| 254 | { |
| 255 | } |
| 256 | |
| 257 | ~regcache_invalidator () |
| 258 | { |
| 259 | if (m_regcache != nullptr) |
| 260 | regcache_invalidate (m_regcache, m_regnum); |
| 261 | } |
| 262 | |
| 263 | DISABLE_COPY_AND_ASSIGN (regcache_invalidator); |
| 264 | |
| 265 | void release () |
| 266 | { |
| 267 | m_regcache = nullptr; |
| 268 | } |
| 269 | |
| 270 | private: |
| 271 | |
| 272 | struct regcache *m_regcache; |
| 273 | int m_regnum; |
| 274 | }; |
| 275 | |
| 276 | /* Return a pointer to register REGNUM's buffer cache. */ |
| 277 | |
| 278 | gdb_byte * |
| 279 | reg_buffer::register_buffer (int regnum) const |
| 280 | { |
| 281 | return m_registers + m_descr->register_offset[regnum]; |
| 282 | } |
| 283 | |
| 284 | void |
| 285 | regcache::save (regcache_cooked_read_ftype *cooked_read, |
| 286 | void *src) |
| 287 | { |
| 288 | struct gdbarch *gdbarch = m_descr->gdbarch; |
| 289 | int regnum; |
| 290 | |
| 291 | /* The DST should be `read-only', if it wasn't then the save would |
| 292 | end up trying to write the register values back out to the |
| 293 | target. */ |
| 294 | gdb_assert (m_readonly_p); |
| 295 | /* Clear the dest. */ |
| 296 | memset (m_registers, 0, m_descr->sizeof_cooked_registers); |
| 297 | memset (m_register_status, 0, m_descr->nr_cooked_registers); |
| 298 | /* Copy over any registers (identified by their membership in the |
| 299 | save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + |
| 300 | gdbarch_num_pseudo_regs) range is checked since some architectures need |
| 301 | to save/restore `cooked' registers that live in memory. */ |
| 302 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
| 303 | { |
| 304 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) |
| 305 | { |
| 306 | gdb_byte *dst_buf = register_buffer (regnum); |
| 307 | enum register_status status = cooked_read (src, regnum, dst_buf); |
| 308 | |
| 309 | gdb_assert (status != REG_UNKNOWN); |
| 310 | |
| 311 | if (status != REG_VALID) |
| 312 | memset (dst_buf, 0, register_size (gdbarch, regnum)); |
| 313 | |
| 314 | m_register_status[regnum] = status; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void |
| 320 | regcache::restore (struct regcache *src) |
| 321 | { |
| 322 | struct gdbarch *gdbarch = m_descr->gdbarch; |
| 323 | int regnum; |
| 324 | |
| 325 | gdb_assert (src != NULL); |
| 326 | /* The dst had better not be read-only. If it is, the `restore' |
| 327 | doesn't make much sense. */ |
| 328 | gdb_assert (!m_readonly_p); |
| 329 | gdb_assert (src->m_readonly_p); |
| 330 | |
| 331 | gdb_assert (gdbarch == src->arch ()); |
| 332 | |
| 333 | /* Copy over any registers, being careful to only restore those that |
| 334 | were both saved and need to be restored. The full [0 .. gdbarch_num_regs |
| 335 | + gdbarch_num_pseudo_regs) range is checked since some architectures need |
| 336 | to save/restore `cooked' registers that live in memory. */ |
| 337 | for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) |
| 338 | { |
| 339 | if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) |
| 340 | { |
| 341 | if (src->m_register_status[regnum] == REG_VALID) |
| 342 | cooked_write (regnum, src->register_buffer (regnum)); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | struct regcache * |
| 348 | regcache_dup (struct regcache *src) |
| 349 | { |
| 350 | return new regcache (regcache::readonly, *src); |
| 351 | } |
| 352 | |
| 353 | enum register_status |
| 354 | regcache_register_status (const struct regcache *regcache, int regnum) |
| 355 | { |
| 356 | gdb_assert (regcache != NULL); |
| 357 | return regcache->get_register_status (regnum); |
| 358 | } |
| 359 | |
| 360 | enum register_status |
| 361 | regcache::get_register_status (int regnum) const |
| 362 | { |
| 363 | gdb_assert (regnum >= 0); |
| 364 | if (m_readonly_p) |
| 365 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
| 366 | else |
| 367 | gdb_assert (regnum < num_raw_registers ()); |
| 368 | |
| 369 | return (enum register_status) m_register_status[regnum]; |
| 370 | } |
| 371 | |
| 372 | void |
| 373 | regcache_invalidate (struct regcache *regcache, int regnum) |
| 374 | { |
| 375 | gdb_assert (regcache != NULL); |
| 376 | regcache->invalidate (regnum); |
| 377 | } |
| 378 | |
| 379 | void |
| 380 | regcache::invalidate (int regnum) |
| 381 | { |
| 382 | gdb_assert (!m_readonly_p); |
| 383 | assert_regnum (regnum); |
| 384 | m_register_status[regnum] = REG_UNKNOWN; |
| 385 | } |
| 386 | |
| 387 | void |
| 388 | reg_buffer::assert_regnum (int regnum) const |
| 389 | { |
| 390 | gdb_assert (regnum >= 0); |
| 391 | if (m_has_pseudo) |
| 392 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
| 393 | else |
| 394 | gdb_assert (regnum < gdbarch_num_regs (arch ())); |
| 395 | } |
| 396 | |
| 397 | /* Global structure containing the current regcache. */ |
| 398 | |
| 399 | /* NOTE: this is a write-through cache. There is no "dirty" bit for |
| 400 | recording if the register values have been changed (eg. by the |
| 401 | user). Therefore all registers must be written back to the |
| 402 | target when appropriate. */ |
| 403 | std::forward_list<regcache *> regcache::current_regcache; |
| 404 | |
| 405 | struct regcache * |
| 406 | get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch, |
| 407 | struct address_space *aspace) |
| 408 | { |
| 409 | for (const auto ®cache : regcache::current_regcache) |
| 410 | if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch) |
| 411 | return regcache; |
| 412 | |
| 413 | regcache *new_regcache = new regcache (gdbarch, aspace, false); |
| 414 | |
| 415 | regcache::current_regcache.push_front (new_regcache); |
| 416 | new_regcache->set_ptid (ptid); |
| 417 | |
| 418 | return new_regcache; |
| 419 | } |
| 420 | |
| 421 | struct regcache * |
| 422 | get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch) |
| 423 | { |
| 424 | address_space *aspace = target_thread_address_space (ptid); |
| 425 | |
| 426 | return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace); |
| 427 | } |
| 428 | |
| 429 | static ptid_t current_thread_ptid; |
| 430 | static struct gdbarch *current_thread_arch; |
| 431 | |
| 432 | struct regcache * |
| 433 | get_thread_regcache (ptid_t ptid) |
| 434 | { |
| 435 | if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid)) |
| 436 | { |
| 437 | current_thread_ptid = ptid; |
| 438 | current_thread_arch = target_thread_architecture (ptid); |
| 439 | } |
| 440 | |
| 441 | return get_thread_arch_regcache (ptid, current_thread_arch); |
| 442 | } |
| 443 | |
| 444 | struct regcache * |
| 445 | get_current_regcache (void) |
| 446 | { |
| 447 | return get_thread_regcache (inferior_ptid); |
| 448 | } |
| 449 | |
| 450 | /* See common/common-regcache.h. */ |
| 451 | |
| 452 | struct regcache * |
| 453 | get_thread_regcache_for_ptid (ptid_t ptid) |
| 454 | { |
| 455 | return get_thread_regcache (ptid); |
| 456 | } |
| 457 | |
| 458 | /* Observer for the target_changed event. */ |
| 459 | |
| 460 | static void |
| 461 | regcache_observer_target_changed (struct target_ops *target) |
| 462 | { |
| 463 | registers_changed (); |
| 464 | } |
| 465 | |
| 466 | /* Update global variables old ptids to hold NEW_PTID if they were |
| 467 | holding OLD_PTID. */ |
| 468 | void |
| 469 | regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid) |
| 470 | { |
| 471 | for (auto ®cache : regcache::current_regcache) |
| 472 | { |
| 473 | if (ptid_equal (regcache->ptid (), old_ptid)) |
| 474 | regcache->set_ptid (new_ptid); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /* Low level examining and depositing of registers. |
| 479 | |
| 480 | The caller is responsible for making sure that the inferior is |
| 481 | stopped before calling the fetching routines, or it will get |
| 482 | garbage. (a change from GDB version 3, in which the caller got the |
| 483 | value from the last stop). */ |
| 484 | |
| 485 | /* REGISTERS_CHANGED () |
| 486 | |
| 487 | Indicate that registers may have changed, so invalidate the cache. */ |
| 488 | |
| 489 | void |
| 490 | registers_changed_ptid (ptid_t ptid) |
| 491 | { |
| 492 | for (auto oit = regcache::current_regcache.before_begin (), |
| 493 | it = std::next (oit); |
| 494 | it != regcache::current_regcache.end (); |
| 495 | ) |
| 496 | { |
| 497 | if (ptid_match ((*it)->ptid (), ptid)) |
| 498 | { |
| 499 | delete *it; |
| 500 | it = regcache::current_regcache.erase_after (oit); |
| 501 | } |
| 502 | else |
| 503 | oit = it++; |
| 504 | } |
| 505 | |
| 506 | if (ptid_match (current_thread_ptid, ptid)) |
| 507 | { |
| 508 | current_thread_ptid = null_ptid; |
| 509 | current_thread_arch = NULL; |
| 510 | } |
| 511 | |
| 512 | if (ptid_match (inferior_ptid, ptid)) |
| 513 | { |
| 514 | /* We just deleted the regcache of the current thread. Need to |
| 515 | forget about any frames we have cached, too. */ |
| 516 | reinit_frame_cache (); |
| 517 | } |
| 518 | } |
| 519 | |
| 520 | void |
| 521 | registers_changed (void) |
| 522 | { |
| 523 | registers_changed_ptid (minus_one_ptid); |
| 524 | |
| 525 | /* Force cleanup of any alloca areas if using C alloca instead of |
| 526 | a builtin alloca. This particular call is used to clean up |
| 527 | areas allocated by low level target code which may build up |
| 528 | during lengthy interactions between gdb and the target before |
| 529 | gdb gives control to the user (ie watchpoints). */ |
| 530 | alloca (0); |
| 531 | } |
| 532 | |
| 533 | void |
| 534 | regcache_raw_update (struct regcache *regcache, int regnum) |
| 535 | { |
| 536 | gdb_assert (regcache != NULL); |
| 537 | |
| 538 | regcache->raw_update (regnum); |
| 539 | } |
| 540 | |
| 541 | void |
| 542 | regcache::raw_update (int regnum) |
| 543 | { |
| 544 | assert_regnum (regnum); |
| 545 | |
| 546 | /* Make certain that the register cache is up-to-date with respect |
| 547 | to the current thread. This switching shouldn't be necessary |
| 548 | only there is still only one target side register cache. Sigh! |
| 549 | On the bright side, at least there is a regcache object. */ |
| 550 | |
| 551 | if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN) |
| 552 | { |
| 553 | target_fetch_registers (this, regnum); |
| 554 | |
| 555 | /* A number of targets can't access the whole set of raw |
| 556 | registers (because the debug API provides no means to get at |
| 557 | them). */ |
| 558 | if (m_register_status[regnum] == REG_UNKNOWN) |
| 559 | m_register_status[regnum] = REG_UNAVAILABLE; |
| 560 | } |
| 561 | } |
| 562 | |
| 563 | enum register_status |
| 564 | regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
| 565 | { |
| 566 | return regcache->raw_read (regnum, buf); |
| 567 | } |
| 568 | |
| 569 | enum register_status |
| 570 | readable_regcache::raw_read (int regnum, gdb_byte *buf) |
| 571 | { |
| 572 | gdb_assert (buf != NULL); |
| 573 | raw_update (regnum); |
| 574 | |
| 575 | if (m_register_status[regnum] != REG_VALID) |
| 576 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
| 577 | else |
| 578 | memcpy (buf, register_buffer (regnum), |
| 579 | m_descr->sizeof_register[regnum]); |
| 580 | |
| 581 | return (enum register_status) m_register_status[regnum]; |
| 582 | } |
| 583 | |
| 584 | enum register_status |
| 585 | regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) |
| 586 | { |
| 587 | gdb_assert (regcache != NULL); |
| 588 | return regcache->raw_read (regnum, val); |
| 589 | } |
| 590 | |
| 591 | template<typename T, typename> |
| 592 | enum register_status |
| 593 | readable_regcache::raw_read (int regnum, T *val) |
| 594 | { |
| 595 | gdb_byte *buf; |
| 596 | enum register_status status; |
| 597 | |
| 598 | assert_regnum (regnum); |
| 599 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); |
| 600 | status = raw_read (regnum, buf); |
| 601 | if (status == REG_VALID) |
| 602 | *val = extract_integer<T> (buf, |
| 603 | m_descr->sizeof_register[regnum], |
| 604 | gdbarch_byte_order (m_descr->gdbarch)); |
| 605 | else |
| 606 | *val = 0; |
| 607 | return status; |
| 608 | } |
| 609 | |
| 610 | enum register_status |
| 611 | regcache_raw_read_unsigned (struct regcache *regcache, int regnum, |
| 612 | ULONGEST *val) |
| 613 | { |
| 614 | gdb_assert (regcache != NULL); |
| 615 | return regcache->raw_read (regnum, val); |
| 616 | } |
| 617 | |
| 618 | void |
| 619 | regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) |
| 620 | { |
| 621 | gdb_assert (regcache != NULL); |
| 622 | regcache->raw_write (regnum, val); |
| 623 | } |
| 624 | |
| 625 | template<typename T, typename> |
| 626 | void |
| 627 | regcache::raw_write (int regnum, T val) |
| 628 | { |
| 629 | gdb_byte *buf; |
| 630 | |
| 631 | assert_regnum (regnum); |
| 632 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); |
| 633 | store_integer (buf, m_descr->sizeof_register[regnum], |
| 634 | gdbarch_byte_order (m_descr->gdbarch), val); |
| 635 | raw_write (regnum, buf); |
| 636 | } |
| 637 | |
| 638 | void |
| 639 | regcache_raw_write_unsigned (struct regcache *regcache, int regnum, |
| 640 | ULONGEST val) |
| 641 | { |
| 642 | gdb_assert (regcache != NULL); |
| 643 | regcache->raw_write (regnum, val); |
| 644 | } |
| 645 | |
| 646 | LONGEST |
| 647 | regcache_raw_get_signed (struct regcache *regcache, int regnum) |
| 648 | { |
| 649 | LONGEST value; |
| 650 | enum register_status status; |
| 651 | |
| 652 | status = regcache_raw_read_signed (regcache, regnum, &value); |
| 653 | if (status == REG_UNAVAILABLE) |
| 654 | throw_error (NOT_AVAILABLE_ERROR, |
| 655 | _("Register %d is not available"), regnum); |
| 656 | return value; |
| 657 | } |
| 658 | |
| 659 | enum register_status |
| 660 | regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf) |
| 661 | { |
| 662 | return regcache->cooked_read (regnum, buf); |
| 663 | } |
| 664 | |
| 665 | enum register_status |
| 666 | readable_regcache::cooked_read (int regnum, gdb_byte *buf) |
| 667 | { |
| 668 | gdb_assert (regnum >= 0); |
| 669 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
| 670 | if (regnum < num_raw_registers ()) |
| 671 | return raw_read (regnum, buf); |
| 672 | else if (m_has_pseudo |
| 673 | && m_register_status[regnum] != REG_UNKNOWN) |
| 674 | { |
| 675 | if (m_register_status[regnum] == REG_VALID) |
| 676 | memcpy (buf, register_buffer (regnum), |
| 677 | m_descr->sizeof_register[regnum]); |
| 678 | else |
| 679 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
| 680 | |
| 681 | return (enum register_status) m_register_status[regnum]; |
| 682 | } |
| 683 | else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
| 684 | { |
| 685 | struct value *mark, *computed; |
| 686 | enum register_status result = REG_VALID; |
| 687 | |
| 688 | mark = value_mark (); |
| 689 | |
| 690 | computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch, |
| 691 | this, regnum); |
| 692 | if (value_entirely_available (computed)) |
| 693 | memcpy (buf, value_contents_raw (computed), |
| 694 | m_descr->sizeof_register[regnum]); |
| 695 | else |
| 696 | { |
| 697 | memset (buf, 0, m_descr->sizeof_register[regnum]); |
| 698 | result = REG_UNAVAILABLE; |
| 699 | } |
| 700 | |
| 701 | value_free_to_mark (mark); |
| 702 | |
| 703 | return result; |
| 704 | } |
| 705 | else |
| 706 | return gdbarch_pseudo_register_read (m_descr->gdbarch, this, |
| 707 | regnum, buf); |
| 708 | } |
| 709 | |
| 710 | struct value * |
| 711 | regcache_cooked_read_value (struct regcache *regcache, int regnum) |
| 712 | { |
| 713 | return regcache->cooked_read_value (regnum); |
| 714 | } |
| 715 | |
| 716 | struct value * |
| 717 | readable_regcache::cooked_read_value (int regnum) |
| 718 | { |
| 719 | gdb_assert (regnum >= 0); |
| 720 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
| 721 | |
| 722 | if (regnum < num_raw_registers () |
| 723 | || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN) |
| 724 | || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) |
| 725 | { |
| 726 | struct value *result; |
| 727 | |
| 728 | result = allocate_value (register_type (m_descr->gdbarch, regnum)); |
| 729 | VALUE_LVAL (result) = lval_register; |
| 730 | VALUE_REGNUM (result) = regnum; |
| 731 | |
| 732 | /* It is more efficient in general to do this delegation in this |
| 733 | direction than in the other one, even though the value-based |
| 734 | API is preferred. */ |
| 735 | if (cooked_read (regnum, |
| 736 | value_contents_raw (result)) == REG_UNAVAILABLE) |
| 737 | mark_value_bytes_unavailable (result, 0, |
| 738 | TYPE_LENGTH (value_type (result))); |
| 739 | |
| 740 | return result; |
| 741 | } |
| 742 | else |
| 743 | return gdbarch_pseudo_register_read_value (m_descr->gdbarch, |
| 744 | this, regnum); |
| 745 | } |
| 746 | |
| 747 | enum register_status |
| 748 | regcache_cooked_read_signed (struct regcache *regcache, int regnum, |
| 749 | LONGEST *val) |
| 750 | { |
| 751 | gdb_assert (regcache != NULL); |
| 752 | return regcache->cooked_read (regnum, val); |
| 753 | } |
| 754 | |
| 755 | template<typename T, typename> |
| 756 | enum register_status |
| 757 | readable_regcache::cooked_read (int regnum, T *val) |
| 758 | { |
| 759 | enum register_status status; |
| 760 | gdb_byte *buf; |
| 761 | |
| 762 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); |
| 763 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); |
| 764 | status = cooked_read (regnum, buf); |
| 765 | if (status == REG_VALID) |
| 766 | *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum], |
| 767 | gdbarch_byte_order (m_descr->gdbarch)); |
| 768 | else |
| 769 | *val = 0; |
| 770 | return status; |
| 771 | } |
| 772 | |
| 773 | enum register_status |
| 774 | regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, |
| 775 | ULONGEST *val) |
| 776 | { |
| 777 | gdb_assert (regcache != NULL); |
| 778 | return regcache->cooked_read (regnum, val); |
| 779 | } |
| 780 | |
| 781 | void |
| 782 | regcache_cooked_write_signed (struct regcache *regcache, int regnum, |
| 783 | LONGEST val) |
| 784 | { |
| 785 | gdb_assert (regcache != NULL); |
| 786 | regcache->cooked_write (regnum, val); |
| 787 | } |
| 788 | |
| 789 | template<typename T, typename> |
| 790 | void |
| 791 | regcache::cooked_write (int regnum, T val) |
| 792 | { |
| 793 | gdb_byte *buf; |
| 794 | |
| 795 | gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers); |
| 796 | buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]); |
| 797 | store_integer (buf, m_descr->sizeof_register[regnum], |
| 798 | gdbarch_byte_order (m_descr->gdbarch), val); |
| 799 | cooked_write (regnum, buf); |
| 800 | } |
| 801 | |
| 802 | void |
| 803 | regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, |
| 804 | ULONGEST val) |
| 805 | { |
| 806 | gdb_assert (regcache != NULL); |
| 807 | regcache->cooked_write (regnum, val); |
| 808 | } |
| 809 | |
| 810 | /* See regcache.h. */ |
| 811 | |
| 812 | void |
| 813 | regcache_raw_set_cached_value (struct regcache *regcache, int regnum, |
| 814 | const gdb_byte *buf) |
| 815 | { |
| 816 | regcache->raw_set_cached_value (regnum, buf); |
| 817 | } |
| 818 | |
| 819 | void |
| 820 | regcache::raw_set_cached_value (int regnum, const gdb_byte *buf) |
| 821 | { |
| 822 | memcpy (register_buffer (regnum), buf, |
| 823 | m_descr->sizeof_register[regnum]); |
| 824 | m_register_status[regnum] = REG_VALID; |
| 825 | } |
| 826 | |
| 827 | void |
| 828 | regcache_raw_write (struct regcache *regcache, int regnum, |
| 829 | const gdb_byte *buf) |
| 830 | { |
| 831 | gdb_assert (regcache != NULL && buf != NULL); |
| 832 | regcache->raw_write (regnum, buf); |
| 833 | } |
| 834 | |
| 835 | void |
| 836 | regcache::raw_write (int regnum, const gdb_byte *buf) |
| 837 | { |
| 838 | |
| 839 | gdb_assert (buf != NULL); |
| 840 | assert_regnum (regnum); |
| 841 | gdb_assert (!m_readonly_p); |
| 842 | |
| 843 | /* On the sparc, writing %g0 is a no-op, so we don't even want to |
| 844 | change the registers array if something writes to this register. */ |
| 845 | if (gdbarch_cannot_store_register (arch (), regnum)) |
| 846 | return; |
| 847 | |
| 848 | /* If we have a valid copy of the register, and new value == old |
| 849 | value, then don't bother doing the actual store. */ |
| 850 | if (get_register_status (regnum) == REG_VALID |
| 851 | && (memcmp (register_buffer (regnum), buf, |
| 852 | m_descr->sizeof_register[regnum]) == 0)) |
| 853 | return; |
| 854 | |
| 855 | target_prepare_to_store (this); |
| 856 | raw_set_cached_value (regnum, buf); |
| 857 | |
| 858 | /* Invalidate the register after it is written, in case of a |
| 859 | failure. */ |
| 860 | regcache_invalidator invalidator (this, regnum); |
| 861 | |
| 862 | target_store_registers (this, regnum); |
| 863 | |
| 864 | /* The target did not throw an error so we can discard invalidating |
| 865 | the register. */ |
| 866 | invalidator.release (); |
| 867 | } |
| 868 | |
| 869 | void |
| 870 | regcache_cooked_write (struct regcache *regcache, int regnum, |
| 871 | const gdb_byte *buf) |
| 872 | { |
| 873 | regcache->cooked_write (regnum, buf); |
| 874 | } |
| 875 | |
| 876 | void |
| 877 | regcache::cooked_write (int regnum, const gdb_byte *buf) |
| 878 | { |
| 879 | gdb_assert (regnum >= 0); |
| 880 | gdb_assert (regnum < m_descr->nr_cooked_registers); |
| 881 | if (regnum < num_raw_registers ()) |
| 882 | raw_write (regnum, buf); |
| 883 | else |
| 884 | gdbarch_pseudo_register_write (m_descr->gdbarch, this, |
| 885 | regnum, buf); |
| 886 | } |
| 887 | |
| 888 | /* Perform a partial register transfer using a read, modify, write |
| 889 | operation. */ |
| 890 | |
| 891 | typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum, |
| 892 | void *buf); |
| 893 | typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum, |
| 894 | const void *buf); |
| 895 | |
| 896 | enum register_status |
| 897 | readable_regcache::read_part (int regnum, int offset, int len, void *in, |
| 898 | bool is_raw) |
| 899 | { |
| 900 | struct gdbarch *gdbarch = arch (); |
| 901 | gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum)); |
| 902 | |
| 903 | gdb_assert (in != NULL); |
| 904 | gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]); |
| 905 | gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]); |
| 906 | /* Something to do? */ |
| 907 | if (offset + len == 0) |
| 908 | return REG_VALID; |
| 909 | /* Read (when needed) ... */ |
| 910 | enum register_status status; |
| 911 | |
| 912 | if (is_raw) |
| 913 | status = raw_read (regnum, reg); |
| 914 | else |
| 915 | status = cooked_read (regnum, reg); |
| 916 | if (status != REG_VALID) |
| 917 | return status; |
| 918 | |
| 919 | /* ... modify ... */ |
| 920 | memcpy (in, reg + offset, len); |
| 921 | |
| 922 | return REG_VALID; |
| 923 | } |
| 924 | |
| 925 | enum register_status |
| 926 | regcache::write_part (int regnum, int offset, int len, |
| 927 | const void *out, bool is_raw) |
| 928 | { |
| 929 | struct gdbarch *gdbarch = arch (); |
| 930 | gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum)); |
| 931 | |
| 932 | gdb_assert (out != NULL); |
| 933 | gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]); |
| 934 | gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]); |
| 935 | /* Something to do? */ |
| 936 | if (offset + len == 0) |
| 937 | return REG_VALID; |
| 938 | /* Read (when needed) ... */ |
| 939 | if (offset > 0 |
| 940 | || offset + len < m_descr->sizeof_register[regnum]) |
| 941 | { |
| 942 | enum register_status status; |
| 943 | |
| 944 | if (is_raw) |
| 945 | status = raw_read (regnum, reg); |
| 946 | else |
| 947 | status = cooked_read (regnum, reg); |
| 948 | if (status != REG_VALID) |
| 949 | return status; |
| 950 | } |
| 951 | |
| 952 | memcpy (reg + offset, out, len); |
| 953 | /* ... write (when needed). */ |
| 954 | if (is_raw) |
| 955 | raw_write (regnum, reg); |
| 956 | else |
| 957 | cooked_write (regnum, reg); |
| 958 | |
| 959 | return REG_VALID; |
| 960 | } |
| 961 | |
| 962 | enum register_status |
| 963 | regcache_raw_read_part (struct regcache *regcache, int regnum, |
| 964 | int offset, int len, gdb_byte *buf) |
| 965 | { |
| 966 | return regcache->raw_read_part (regnum, offset, len, buf); |
| 967 | } |
| 968 | |
| 969 | enum register_status |
| 970 | readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf) |
| 971 | { |
| 972 | assert_regnum (regnum); |
| 973 | return read_part (regnum, offset, len, buf, true); |
| 974 | } |
| 975 | |
| 976 | void |
| 977 | regcache_raw_write_part (struct regcache *regcache, int regnum, |
| 978 | int offset, int len, const gdb_byte *buf) |
| 979 | { |
| 980 | regcache->raw_write_part (regnum, offset, len, buf); |
| 981 | } |
| 982 | |
| 983 | void |
| 984 | regcache::raw_write_part (int regnum, int offset, int len, |
| 985 | const gdb_byte *buf) |
| 986 | { |
| 987 | assert_regnum (regnum); |
| 988 | write_part (regnum, offset, len, buf, true); |
| 989 | } |
| 990 | |
| 991 | enum register_status |
| 992 | regcache_cooked_read_part (struct regcache *regcache, int regnum, |
| 993 | int offset, int len, gdb_byte *buf) |
| 994 | { |
| 995 | return regcache->cooked_read_part (regnum, offset, len, buf); |
| 996 | } |
| 997 | |
| 998 | |
| 999 | enum register_status |
| 1000 | readable_regcache::cooked_read_part (int regnum, int offset, int len, |
| 1001 | gdb_byte *buf) |
| 1002 | { |
| 1003 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); |
| 1004 | return read_part (regnum, offset, len, buf, false); |
| 1005 | } |
| 1006 | |
| 1007 | void |
| 1008 | regcache_cooked_write_part (struct regcache *regcache, int regnum, |
| 1009 | int offset, int len, const gdb_byte *buf) |
| 1010 | { |
| 1011 | regcache->cooked_write_part (regnum, offset, len, buf); |
| 1012 | } |
| 1013 | |
| 1014 | void |
| 1015 | regcache::cooked_write_part (int regnum, int offset, int len, |
| 1016 | const gdb_byte *buf) |
| 1017 | { |
| 1018 | gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); |
| 1019 | write_part (regnum, offset, len, buf, false); |
| 1020 | } |
| 1021 | |
| 1022 | /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */ |
| 1023 | |
| 1024 | void |
| 1025 | regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf) |
| 1026 | { |
| 1027 | gdb_assert (regcache != NULL); |
| 1028 | regcache->raw_supply (regnum, buf); |
| 1029 | } |
| 1030 | |
| 1031 | void |
| 1032 | regcache::raw_supply (int regnum, const void *buf) |
| 1033 | { |
| 1034 | void *regbuf; |
| 1035 | size_t size; |
| 1036 | |
| 1037 | assert_regnum (regnum); |
| 1038 | gdb_assert (!m_readonly_p); |
| 1039 | |
| 1040 | regbuf = register_buffer (regnum); |
| 1041 | size = m_descr->sizeof_register[regnum]; |
| 1042 | |
| 1043 | if (buf) |
| 1044 | { |
| 1045 | memcpy (regbuf, buf, size); |
| 1046 | m_register_status[regnum] = REG_VALID; |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | /* This memset not strictly necessary, but better than garbage |
| 1051 | in case the register value manages to escape somewhere (due |
| 1052 | to a bug, no less). */ |
| 1053 | memset (regbuf, 0, size); |
| 1054 | m_register_status[regnum] = REG_UNAVAILABLE; |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at |
| 1059 | address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If |
| 1060 | the register size is greater than ADDR_LEN, then the integer will be sign or |
| 1061 | zero extended. If the register size is smaller than the integer, then the |
| 1062 | most significant bytes of the integer will be truncated. */ |
| 1063 | |
| 1064 | void |
| 1065 | regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len, |
| 1066 | bool is_signed) |
| 1067 | { |
| 1068 | enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); |
| 1069 | gdb_byte *regbuf; |
| 1070 | size_t regsize; |
| 1071 | |
| 1072 | assert_regnum (regnum); |
| 1073 | gdb_assert (!m_readonly_p); |
| 1074 | |
| 1075 | regbuf = register_buffer (regnum); |
| 1076 | regsize = m_descr->sizeof_register[regnum]; |
| 1077 | |
| 1078 | copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed, |
| 1079 | byte_order); |
| 1080 | m_register_status[regnum] = REG_VALID; |
| 1081 | } |
| 1082 | |
| 1083 | /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same |
| 1084 | as calling raw_supply with NULL (which will set the state to |
| 1085 | unavailable). */ |
| 1086 | |
| 1087 | void |
| 1088 | regcache::raw_supply_zeroed (int regnum) |
| 1089 | { |
| 1090 | void *regbuf; |
| 1091 | size_t size; |
| 1092 | |
| 1093 | assert_regnum (regnum); |
| 1094 | gdb_assert (!m_readonly_p); |
| 1095 | |
| 1096 | regbuf = register_buffer (regnum); |
| 1097 | size = m_descr->sizeof_register[regnum]; |
| 1098 | |
| 1099 | memset (regbuf, 0, size); |
| 1100 | m_register_status[regnum] = REG_VALID; |
| 1101 | } |
| 1102 | |
| 1103 | /* Collect register REGNUM from REGCACHE and store its contents in BUF. */ |
| 1104 | |
| 1105 | void |
| 1106 | regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf) |
| 1107 | { |
| 1108 | gdb_assert (regcache != NULL && buf != NULL); |
| 1109 | regcache->raw_collect (regnum, buf); |
| 1110 | } |
| 1111 | |
| 1112 | void |
| 1113 | regcache::raw_collect (int regnum, void *buf) const |
| 1114 | { |
| 1115 | const void *regbuf; |
| 1116 | size_t size; |
| 1117 | |
| 1118 | gdb_assert (buf != NULL); |
| 1119 | assert_regnum (regnum); |
| 1120 | |
| 1121 | regbuf = register_buffer (regnum); |
| 1122 | size = m_descr->sizeof_register[regnum]; |
| 1123 | memcpy (buf, regbuf, size); |
| 1124 | } |
| 1125 | |
| 1126 | /* Transfer a single or all registers belonging to a certain register |
| 1127 | set to or from a buffer. This is the main worker function for |
| 1128 | regcache_supply_regset and regcache_collect_regset. */ |
| 1129 | |
| 1130 | /* Collect register REGNUM from REGCACHE. Store collected value as an integer |
| 1131 | at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. |
| 1132 | If ADDR_LEN is greater than the register size, then the integer will be sign |
| 1133 | or zero extended. If ADDR_LEN is smaller than the register size, then the |
| 1134 | most significant bytes of the integer will be truncated. */ |
| 1135 | |
| 1136 | void |
| 1137 | regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len, |
| 1138 | bool is_signed) const |
| 1139 | { |
| 1140 | enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); |
| 1141 | const gdb_byte *regbuf; |
| 1142 | size_t regsize; |
| 1143 | |
| 1144 | assert_regnum (regnum); |
| 1145 | |
| 1146 | regbuf = register_buffer (regnum); |
| 1147 | regsize = m_descr->sizeof_register[regnum]; |
| 1148 | |
| 1149 | copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed, |
| 1150 | byte_order); |
| 1151 | } |
| 1152 | |
| 1153 | void |
| 1154 | regcache::transfer_regset (const struct regset *regset, |
| 1155 | struct regcache *out_regcache, |
| 1156 | int regnum, const void *in_buf, |
| 1157 | void *out_buf, size_t size) const |
| 1158 | { |
| 1159 | const struct regcache_map_entry *map; |
| 1160 | int offs = 0, count; |
| 1161 | |
| 1162 | for (map = (const struct regcache_map_entry *) regset->regmap; |
| 1163 | (count = map->count) != 0; |
| 1164 | map++) |
| 1165 | { |
| 1166 | int regno = map->regno; |
| 1167 | int slot_size = map->size; |
| 1168 | |
| 1169 | if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) |
| 1170 | slot_size = m_descr->sizeof_register[regno]; |
| 1171 | |
| 1172 | if (regno == REGCACHE_MAP_SKIP |
| 1173 | || (regnum != -1 |
| 1174 | && (regnum < regno || regnum >= regno + count))) |
| 1175 | offs += count * slot_size; |
| 1176 | |
| 1177 | else if (regnum == -1) |
| 1178 | for (; count--; regno++, offs += slot_size) |
| 1179 | { |
| 1180 | if (offs + slot_size > size) |
| 1181 | break; |
| 1182 | |
| 1183 | if (out_buf) |
| 1184 | raw_collect (regno, (gdb_byte *) out_buf + offs); |
| 1185 | else |
| 1186 | out_regcache->raw_supply (regno, in_buf |
| 1187 | ? (const gdb_byte *) in_buf + offs |
| 1188 | : NULL); |
| 1189 | } |
| 1190 | else |
| 1191 | { |
| 1192 | /* Transfer a single register and return. */ |
| 1193 | offs += (regnum - regno) * slot_size; |
| 1194 | if (offs + slot_size > size) |
| 1195 | return; |
| 1196 | |
| 1197 | if (out_buf) |
| 1198 | raw_collect (regnum, (gdb_byte *) out_buf + offs); |
| 1199 | else |
| 1200 | out_regcache->raw_supply (regnum, in_buf |
| 1201 | ? (const gdb_byte *) in_buf + offs |
| 1202 | : NULL); |
| 1203 | return; |
| 1204 | } |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | /* Supply register REGNUM from BUF to REGCACHE, using the register map |
| 1209 | in REGSET. If REGNUM is -1, do this for all registers in REGSET. |
| 1210 | If BUF is NULL, set the register(s) to "unavailable" status. */ |
| 1211 | |
| 1212 | void |
| 1213 | regcache_supply_regset (const struct regset *regset, |
| 1214 | struct regcache *regcache, |
| 1215 | int regnum, const void *buf, size_t size) |
| 1216 | { |
| 1217 | regcache->supply_regset (regset, regnum, buf, size); |
| 1218 | } |
| 1219 | |
| 1220 | void |
| 1221 | regcache::supply_regset (const struct regset *regset, |
| 1222 | int regnum, const void *buf, size_t size) |
| 1223 | { |
| 1224 | transfer_regset (regset, this, regnum, buf, NULL, size); |
| 1225 | } |
| 1226 | |
| 1227 | /* Collect register REGNUM from REGCACHE to BUF, using the register |
| 1228 | map in REGSET. If REGNUM is -1, do this for all registers in |
| 1229 | REGSET. */ |
| 1230 | |
| 1231 | void |
| 1232 | regcache_collect_regset (const struct regset *regset, |
| 1233 | const struct regcache *regcache, |
| 1234 | int regnum, void *buf, size_t size) |
| 1235 | { |
| 1236 | regcache->collect_regset (regset, regnum, buf, size); |
| 1237 | } |
| 1238 | |
| 1239 | void |
| 1240 | regcache::collect_regset (const struct regset *regset, |
| 1241 | int regnum, void *buf, size_t size) const |
| 1242 | { |
| 1243 | transfer_regset (regset, NULL, regnum, NULL, buf, size); |
| 1244 | } |
| 1245 | |
| 1246 | |
| 1247 | /* Special handling for register PC. */ |
| 1248 | |
| 1249 | CORE_ADDR |
| 1250 | regcache_read_pc (struct regcache *regcache) |
| 1251 | { |
| 1252 | struct gdbarch *gdbarch = regcache->arch (); |
| 1253 | |
| 1254 | CORE_ADDR pc_val; |
| 1255 | |
| 1256 | if (gdbarch_read_pc_p (gdbarch)) |
| 1257 | pc_val = gdbarch_read_pc (gdbarch, regcache); |
| 1258 | /* Else use per-frame method on get_current_frame. */ |
| 1259 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
| 1260 | { |
| 1261 | ULONGEST raw_val; |
| 1262 | |
| 1263 | if (regcache_cooked_read_unsigned (regcache, |
| 1264 | gdbarch_pc_regnum (gdbarch), |
| 1265 | &raw_val) == REG_UNAVAILABLE) |
| 1266 | throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); |
| 1267 | |
| 1268 | pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); |
| 1269 | } |
| 1270 | else |
| 1271 | internal_error (__FILE__, __LINE__, |
| 1272 | _("regcache_read_pc: Unable to find PC")); |
| 1273 | return pc_val; |
| 1274 | } |
| 1275 | |
| 1276 | void |
| 1277 | regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) |
| 1278 | { |
| 1279 | struct gdbarch *gdbarch = regcache->arch (); |
| 1280 | |
| 1281 | if (gdbarch_write_pc_p (gdbarch)) |
| 1282 | gdbarch_write_pc (gdbarch, regcache, pc); |
| 1283 | else if (gdbarch_pc_regnum (gdbarch) >= 0) |
| 1284 | regcache_cooked_write_unsigned (regcache, |
| 1285 | gdbarch_pc_regnum (gdbarch), pc); |
| 1286 | else |
| 1287 | internal_error (__FILE__, __LINE__, |
| 1288 | _("regcache_write_pc: Unable to update PC")); |
| 1289 | |
| 1290 | /* Writing the PC (for instance, from "load") invalidates the |
| 1291 | current frame. */ |
| 1292 | reinit_frame_cache (); |
| 1293 | } |
| 1294 | |
| 1295 | int |
| 1296 | reg_buffer::num_raw_registers () const |
| 1297 | { |
| 1298 | return gdbarch_num_regs (arch ()); |
| 1299 | } |
| 1300 | |
| 1301 | void |
| 1302 | regcache::debug_print_register (const char *func, int regno) |
| 1303 | { |
| 1304 | struct gdbarch *gdbarch = arch (); |
| 1305 | |
| 1306 | fprintf_unfiltered (gdb_stdlog, "%s ", func); |
| 1307 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch) |
| 1308 | && gdbarch_register_name (gdbarch, regno) != NULL |
| 1309 | && gdbarch_register_name (gdbarch, regno)[0] != '\0') |
| 1310 | fprintf_unfiltered (gdb_stdlog, "(%s)", |
| 1311 | gdbarch_register_name (gdbarch, regno)); |
| 1312 | else |
| 1313 | fprintf_unfiltered (gdb_stdlog, "(%d)", regno); |
| 1314 | if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)) |
| 1315 | { |
| 1316 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
| 1317 | int size = register_size (gdbarch, regno); |
| 1318 | gdb_byte *buf = register_buffer (regno); |
| 1319 | |
| 1320 | fprintf_unfiltered (gdb_stdlog, " = "); |
| 1321 | for (int i = 0; i < size; i++) |
| 1322 | { |
| 1323 | fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]); |
| 1324 | } |
| 1325 | if (size <= sizeof (LONGEST)) |
| 1326 | { |
| 1327 | ULONGEST val = extract_unsigned_integer (buf, size, byte_order); |
| 1328 | |
| 1329 | fprintf_unfiltered (gdb_stdlog, " %s %s", |
| 1330 | core_addr_to_string_nz (val), plongest (val)); |
| 1331 | } |
| 1332 | } |
| 1333 | fprintf_unfiltered (gdb_stdlog, "\n"); |
| 1334 | } |
| 1335 | |
| 1336 | static void |
| 1337 | reg_flush_command (const char *command, int from_tty) |
| 1338 | { |
| 1339 | /* Force-flush the register cache. */ |
| 1340 | registers_changed (); |
| 1341 | if (from_tty) |
| 1342 | printf_filtered (_("Register cache flushed.\n")); |
| 1343 | } |
| 1344 | |
| 1345 | void |
| 1346 | regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump) |
| 1347 | { |
| 1348 | struct gdbarch *gdbarch = m_descr->gdbarch; |
| 1349 | int regnum; |
| 1350 | int footnote_nr = 0; |
| 1351 | int footnote_register_offset = 0; |
| 1352 | int footnote_register_type_name_null = 0; |
| 1353 | long register_offset = 0; |
| 1354 | |
| 1355 | gdb_assert (m_descr->nr_cooked_registers |
| 1356 | == (gdbarch_num_regs (gdbarch) |
| 1357 | + gdbarch_num_pseudo_regs (gdbarch))); |
| 1358 | |
| 1359 | for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++) |
| 1360 | { |
| 1361 | /* Name. */ |
| 1362 | if (regnum < 0) |
| 1363 | fprintf_unfiltered (file, " %-10s", "Name"); |
| 1364 | else |
| 1365 | { |
| 1366 | const char *p = gdbarch_register_name (gdbarch, regnum); |
| 1367 | |
| 1368 | if (p == NULL) |
| 1369 | p = ""; |
| 1370 | else if (p[0] == '\0') |
| 1371 | p = "''"; |
| 1372 | fprintf_unfiltered (file, " %-10s", p); |
| 1373 | } |
| 1374 | |
| 1375 | /* Number. */ |
| 1376 | if (regnum < 0) |
| 1377 | fprintf_unfiltered (file, " %4s", "Nr"); |
| 1378 | else |
| 1379 | fprintf_unfiltered (file, " %4d", regnum); |
| 1380 | |
| 1381 | /* Relative number. */ |
| 1382 | if (regnum < 0) |
| 1383 | fprintf_unfiltered (file, " %4s", "Rel"); |
| 1384 | else if (regnum < gdbarch_num_regs (gdbarch)) |
| 1385 | fprintf_unfiltered (file, " %4d", regnum); |
| 1386 | else |
| 1387 | fprintf_unfiltered (file, " %4d", |
| 1388 | (regnum - gdbarch_num_regs (gdbarch))); |
| 1389 | |
| 1390 | /* Offset. */ |
| 1391 | if (regnum < 0) |
| 1392 | fprintf_unfiltered (file, " %6s ", "Offset"); |
| 1393 | else |
| 1394 | { |
| 1395 | fprintf_unfiltered (file, " %6ld", |
| 1396 | m_descr->register_offset[regnum]); |
| 1397 | if (register_offset != m_descr->register_offset[regnum] |
| 1398 | || (regnum > 0 |
| 1399 | && (m_descr->register_offset[regnum] |
| 1400 | != (m_descr->register_offset[regnum - 1] |
| 1401 | + m_descr->sizeof_register[regnum - 1]))) |
| 1402 | ) |
| 1403 | { |
| 1404 | if (!footnote_register_offset) |
| 1405 | footnote_register_offset = ++footnote_nr; |
| 1406 | fprintf_unfiltered (file, "*%d", footnote_register_offset); |
| 1407 | } |
| 1408 | else |
| 1409 | fprintf_unfiltered (file, " "); |
| 1410 | register_offset = (m_descr->register_offset[regnum] |
| 1411 | + m_descr->sizeof_register[regnum]); |
| 1412 | } |
| 1413 | |
| 1414 | /* Size. */ |
| 1415 | if (regnum < 0) |
| 1416 | fprintf_unfiltered (file, " %5s ", "Size"); |
| 1417 | else |
| 1418 | fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]); |
| 1419 | |
| 1420 | /* Type. */ |
| 1421 | { |
| 1422 | const char *t; |
| 1423 | std::string name_holder; |
| 1424 | |
| 1425 | if (regnum < 0) |
| 1426 | t = "Type"; |
| 1427 | else |
| 1428 | { |
| 1429 | static const char blt[] = "builtin_type"; |
| 1430 | |
| 1431 | t = TYPE_NAME (register_type (arch (), regnum)); |
| 1432 | if (t == NULL) |
| 1433 | { |
| 1434 | if (!footnote_register_type_name_null) |
| 1435 | footnote_register_type_name_null = ++footnote_nr; |
| 1436 | name_holder = string_printf ("*%d", |
| 1437 | footnote_register_type_name_null); |
| 1438 | t = name_holder.c_str (); |
| 1439 | } |
| 1440 | /* Chop a leading builtin_type. */ |
| 1441 | if (startswith (t, blt)) |
| 1442 | t += strlen (blt); |
| 1443 | } |
| 1444 | fprintf_unfiltered (file, " %-15s", t); |
| 1445 | } |
| 1446 | |
| 1447 | /* Leading space always present. */ |
| 1448 | fprintf_unfiltered (file, " "); |
| 1449 | |
| 1450 | /* Value, raw. */ |
| 1451 | if (what_to_dump == regcache_dump_raw) |
| 1452 | { |
| 1453 | if (regnum < 0) |
| 1454 | fprintf_unfiltered (file, "Raw value"); |
| 1455 | else if (regnum >= num_raw_registers ()) |
| 1456 | fprintf_unfiltered (file, "<cooked>"); |
| 1457 | else if (get_register_status (regnum) == REG_UNKNOWN) |
| 1458 | fprintf_unfiltered (file, "<invalid>"); |
| 1459 | else if (get_register_status (regnum) == REG_UNAVAILABLE) |
| 1460 | fprintf_unfiltered (file, "<unavailable>"); |
| 1461 | else |
| 1462 | { |
| 1463 | raw_update (regnum); |
| 1464 | print_hex_chars (file, register_buffer (regnum), |
| 1465 | m_descr->sizeof_register[regnum], |
| 1466 | gdbarch_byte_order (gdbarch), true); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | /* Value, cooked. */ |
| 1471 | if (what_to_dump == regcache_dump_cooked) |
| 1472 | { |
| 1473 | if (regnum < 0) |
| 1474 | fprintf_unfiltered (file, "Cooked value"); |
| 1475 | else |
| 1476 | { |
| 1477 | const gdb_byte *buf = NULL; |
| 1478 | enum register_status status; |
| 1479 | struct value *value = NULL; |
| 1480 | |
| 1481 | if (regnum < num_raw_registers ()) |
| 1482 | { |
| 1483 | raw_update (regnum); |
| 1484 | status = get_register_status (regnum); |
| 1485 | buf = register_buffer (regnum); |
| 1486 | } |
| 1487 | else |
| 1488 | { |
| 1489 | value = cooked_read_value (regnum); |
| 1490 | |
| 1491 | if (!value_optimized_out (value) |
| 1492 | && value_entirely_available (value)) |
| 1493 | { |
| 1494 | status = REG_VALID; |
| 1495 | buf = value_contents_all (value); |
| 1496 | } |
| 1497 | else |
| 1498 | status = REG_UNAVAILABLE; |
| 1499 | } |
| 1500 | |
| 1501 | if (status == REG_UNKNOWN) |
| 1502 | fprintf_unfiltered (file, "<invalid>"); |
| 1503 | else if (status == REG_UNAVAILABLE) |
| 1504 | fprintf_unfiltered (file, "<unavailable>"); |
| 1505 | else |
| 1506 | print_hex_chars (file, buf, |
| 1507 | m_descr->sizeof_register[regnum], |
| 1508 | gdbarch_byte_order (gdbarch), true); |
| 1509 | |
| 1510 | if (value != NULL) |
| 1511 | { |
| 1512 | release_value (value); |
| 1513 | value_free (value); |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 | |
| 1518 | /* Group members. */ |
| 1519 | if (what_to_dump == regcache_dump_groups) |
| 1520 | { |
| 1521 | if (regnum < 0) |
| 1522 | fprintf_unfiltered (file, "Groups"); |
| 1523 | else |
| 1524 | { |
| 1525 | const char *sep = ""; |
| 1526 | struct reggroup *group; |
| 1527 | |
| 1528 | for (group = reggroup_next (gdbarch, NULL); |
| 1529 | group != NULL; |
| 1530 | group = reggroup_next (gdbarch, group)) |
| 1531 | { |
| 1532 | if (gdbarch_register_reggroup_p (gdbarch, regnum, group)) |
| 1533 | { |
| 1534 | fprintf_unfiltered (file, |
| 1535 | "%s%s", sep, reggroup_name (group)); |
| 1536 | sep = ","; |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* Remote packet configuration. */ |
| 1543 | if (what_to_dump == regcache_dump_remote) |
| 1544 | { |
| 1545 | if (regnum < 0) |
| 1546 | { |
| 1547 | fprintf_unfiltered (file, "Rmt Nr g/G Offset"); |
| 1548 | } |
| 1549 | else if (regnum < num_raw_registers ()) |
| 1550 | { |
| 1551 | int pnum, poffset; |
| 1552 | |
| 1553 | if (remote_register_number_and_offset (arch (), regnum, |
| 1554 | &pnum, &poffset)) |
| 1555 | fprintf_unfiltered (file, "%7d %11d", pnum, poffset); |
| 1556 | } |
| 1557 | } |
| 1558 | |
| 1559 | fprintf_unfiltered (file, "\n"); |
| 1560 | } |
| 1561 | |
| 1562 | if (footnote_register_offset) |
| 1563 | fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n", |
| 1564 | footnote_register_offset); |
| 1565 | if (footnote_register_type_name_null) |
| 1566 | fprintf_unfiltered (file, |
| 1567 | "*%d: Register type's name NULL.\n", |
| 1568 | footnote_register_type_name_null); |
| 1569 | } |
| 1570 | |
| 1571 | static void |
| 1572 | regcache_print (const char *args, enum regcache_dump_what what_to_dump) |
| 1573 | { |
| 1574 | /* Where to send output. */ |
| 1575 | stdio_file file; |
| 1576 | ui_file *out; |
| 1577 | |
| 1578 | if (args == NULL) |
| 1579 | out = gdb_stdout; |
| 1580 | else |
| 1581 | { |
| 1582 | if (!file.open (args, "w")) |
| 1583 | perror_with_name (_("maintenance print architecture")); |
| 1584 | out = &file; |
| 1585 | } |
| 1586 | |
| 1587 | if (target_has_registers) |
| 1588 | get_current_regcache ()->dump (out, what_to_dump); |
| 1589 | else |
| 1590 | { |
| 1591 | /* For the benefit of "maint print registers" & co when |
| 1592 | debugging an executable, allow dumping a regcache even when |
| 1593 | there is no thread selected / no registers. */ |
| 1594 | regcache dummy_regs (target_gdbarch ()); |
| 1595 | dummy_regs.dump (out, what_to_dump); |
| 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | static void |
| 1600 | maintenance_print_registers (const char *args, int from_tty) |
| 1601 | { |
| 1602 | regcache_print (args, regcache_dump_none); |
| 1603 | } |
| 1604 | |
| 1605 | static void |
| 1606 | maintenance_print_raw_registers (const char *args, int from_tty) |
| 1607 | { |
| 1608 | regcache_print (args, regcache_dump_raw); |
| 1609 | } |
| 1610 | |
| 1611 | static void |
| 1612 | maintenance_print_cooked_registers (const char *args, int from_tty) |
| 1613 | { |
| 1614 | regcache_print (args, regcache_dump_cooked); |
| 1615 | } |
| 1616 | |
| 1617 | static void |
| 1618 | maintenance_print_register_groups (const char *args, int from_tty) |
| 1619 | { |
| 1620 | regcache_print (args, regcache_dump_groups); |
| 1621 | } |
| 1622 | |
| 1623 | static void |
| 1624 | maintenance_print_remote_registers (const char *args, int from_tty) |
| 1625 | { |
| 1626 | regcache_print (args, regcache_dump_remote); |
| 1627 | } |
| 1628 | |
| 1629 | #if GDB_SELF_TEST |
| 1630 | #include "selftest.h" |
| 1631 | #include "selftest-arch.h" |
| 1632 | #include "gdbthread.h" |
| 1633 | #include "target-float.h" |
| 1634 | |
| 1635 | namespace selftests { |
| 1636 | |
| 1637 | class regcache_access : public regcache |
| 1638 | { |
| 1639 | public: |
| 1640 | |
| 1641 | /* Return the number of elements in current_regcache. */ |
| 1642 | |
| 1643 | static size_t |
| 1644 | current_regcache_size () |
| 1645 | { |
| 1646 | return std::distance (regcache::current_regcache.begin (), |
| 1647 | regcache::current_regcache.end ()); |
| 1648 | } |
| 1649 | }; |
| 1650 | |
| 1651 | static void |
| 1652 | current_regcache_test (void) |
| 1653 | { |
| 1654 | /* It is empty at the start. */ |
| 1655 | SELF_CHECK (regcache_access::current_regcache_size () == 0); |
| 1656 | |
| 1657 | ptid_t ptid1 (1), ptid2 (2), ptid3 (3); |
| 1658 | |
| 1659 | /* Get regcache from ptid1, a new regcache is added to |
| 1660 | current_regcache. */ |
| 1661 | regcache *regcache = get_thread_arch_aspace_regcache (ptid1, |
| 1662 | target_gdbarch (), |
| 1663 | NULL); |
| 1664 | |
| 1665 | SELF_CHECK (regcache != NULL); |
| 1666 | SELF_CHECK (regcache->ptid () == ptid1); |
| 1667 | SELF_CHECK (regcache_access::current_regcache_size () == 1); |
| 1668 | |
| 1669 | /* Get regcache from ptid2, a new regcache is added to |
| 1670 | current_regcache. */ |
| 1671 | regcache = get_thread_arch_aspace_regcache (ptid2, |
| 1672 | target_gdbarch (), |
| 1673 | NULL); |
| 1674 | SELF_CHECK (regcache != NULL); |
| 1675 | SELF_CHECK (regcache->ptid () == ptid2); |
| 1676 | SELF_CHECK (regcache_access::current_regcache_size () == 2); |
| 1677 | |
| 1678 | /* Get regcache from ptid3, a new regcache is added to |
| 1679 | current_regcache. */ |
| 1680 | regcache = get_thread_arch_aspace_regcache (ptid3, |
| 1681 | target_gdbarch (), |
| 1682 | NULL); |
| 1683 | SELF_CHECK (regcache != NULL); |
| 1684 | SELF_CHECK (regcache->ptid () == ptid3); |
| 1685 | SELF_CHECK (regcache_access::current_regcache_size () == 3); |
| 1686 | |
| 1687 | /* Get regcache from ptid2 again, nothing is added to |
| 1688 | current_regcache. */ |
| 1689 | regcache = get_thread_arch_aspace_regcache (ptid2, |
| 1690 | target_gdbarch (), |
| 1691 | NULL); |
| 1692 | SELF_CHECK (regcache != NULL); |
| 1693 | SELF_CHECK (regcache->ptid () == ptid2); |
| 1694 | SELF_CHECK (regcache_access::current_regcache_size () == 3); |
| 1695 | |
| 1696 | /* Mark ptid2 is changed, so regcache of ptid2 should be removed from |
| 1697 | current_regcache. */ |
| 1698 | registers_changed_ptid (ptid2); |
| 1699 | SELF_CHECK (regcache_access::current_regcache_size () == 2); |
| 1700 | } |
| 1701 | |
| 1702 | static void test_target_fetch_registers (target_ops *self, regcache *regs, |
| 1703 | int regno); |
| 1704 | static void test_target_store_registers (target_ops *self, regcache *regs, |
| 1705 | int regno); |
| 1706 | static enum target_xfer_status |
| 1707 | test_target_xfer_partial (struct target_ops *ops, |
| 1708 | enum target_object object, |
| 1709 | const char *annex, gdb_byte *readbuf, |
| 1710 | const gdb_byte *writebuf, |
| 1711 | ULONGEST offset, ULONGEST len, |
| 1712 | ULONGEST *xfered_len); |
| 1713 | |
| 1714 | class target_ops_no_register : public test_target_ops |
| 1715 | { |
| 1716 | public: |
| 1717 | target_ops_no_register () |
| 1718 | : test_target_ops {} |
| 1719 | { |
| 1720 | to_fetch_registers = test_target_fetch_registers; |
| 1721 | to_store_registers = test_target_store_registers; |
| 1722 | to_xfer_partial = test_target_xfer_partial; |
| 1723 | |
| 1724 | to_data = this; |
| 1725 | } |
| 1726 | |
| 1727 | void reset () |
| 1728 | { |
| 1729 | fetch_registers_called = 0; |
| 1730 | store_registers_called = 0; |
| 1731 | xfer_partial_called = 0; |
| 1732 | } |
| 1733 | |
| 1734 | unsigned int fetch_registers_called = 0; |
| 1735 | unsigned int store_registers_called = 0; |
| 1736 | unsigned int xfer_partial_called = 0; |
| 1737 | }; |
| 1738 | |
| 1739 | static void |
| 1740 | test_target_fetch_registers (target_ops *self, regcache *regs, int regno) |
| 1741 | { |
| 1742 | auto ops = static_cast<target_ops_no_register *> (self->to_data); |
| 1743 | |
| 1744 | /* Mark register available. */ |
| 1745 | regs->raw_supply_zeroed (regno); |
| 1746 | ops->fetch_registers_called++; |
| 1747 | } |
| 1748 | |
| 1749 | static void |
| 1750 | test_target_store_registers (target_ops *self, regcache *regs, int regno) |
| 1751 | { |
| 1752 | auto ops = static_cast<target_ops_no_register *> (self->to_data); |
| 1753 | |
| 1754 | ops->store_registers_called++; |
| 1755 | } |
| 1756 | |
| 1757 | static enum target_xfer_status |
| 1758 | test_target_xfer_partial (struct target_ops *self, enum target_object object, |
| 1759 | const char *annex, gdb_byte *readbuf, |
| 1760 | const gdb_byte *writebuf, |
| 1761 | ULONGEST offset, ULONGEST len, ULONGEST *xfered_len) |
| 1762 | { |
| 1763 | auto ops = static_cast<target_ops_no_register *> (self->to_data); |
| 1764 | |
| 1765 | ops->xfer_partial_called++; |
| 1766 | |
| 1767 | *xfered_len = len; |
| 1768 | return TARGET_XFER_OK; |
| 1769 | } |
| 1770 | |
| 1771 | class readwrite_regcache : public regcache |
| 1772 | { |
| 1773 | public: |
| 1774 | readwrite_regcache (struct gdbarch *gdbarch) |
| 1775 | : regcache (gdbarch, nullptr, false) |
| 1776 | {} |
| 1777 | }; |
| 1778 | |
| 1779 | /* Test regcache::cooked_read gets registers from raw registers and |
| 1780 | memory instead of target to_{fetch,store}_registers. */ |
| 1781 | |
| 1782 | static void |
| 1783 | cooked_read_test (struct gdbarch *gdbarch) |
| 1784 | { |
| 1785 | /* Error out if debugging something, because we're going to push the |
| 1786 | test target, which would pop any existing target. */ |
| 1787 | if (current_target.to_stratum >= process_stratum) |
| 1788 | error (_("target already pushed")); |
| 1789 | |
| 1790 | /* Create a mock environment. An inferior with a thread, with a |
| 1791 | process_stratum target pushed. */ |
| 1792 | |
| 1793 | target_ops_no_register mock_target; |
| 1794 | ptid_t mock_ptid (1, 1); |
| 1795 | inferior mock_inferior (mock_ptid.pid ()); |
| 1796 | address_space mock_aspace {}; |
| 1797 | mock_inferior.gdbarch = gdbarch; |
| 1798 | mock_inferior.aspace = &mock_aspace; |
| 1799 | thread_info mock_thread (&mock_inferior, mock_ptid); |
| 1800 | |
| 1801 | scoped_restore restore_thread_list |
| 1802 | = make_scoped_restore (&thread_list, &mock_thread); |
| 1803 | |
| 1804 | /* Add the mock inferior to the inferior list so that look ups by |
| 1805 | target+ptid can find it. */ |
| 1806 | scoped_restore restore_inferior_list |
| 1807 | = make_scoped_restore (&inferior_list); |
| 1808 | inferior_list = &mock_inferior; |
| 1809 | |
| 1810 | /* Switch to the mock inferior. */ |
| 1811 | scoped_restore_current_inferior restore_current_inferior; |
| 1812 | set_current_inferior (&mock_inferior); |
| 1813 | |
| 1814 | /* Push the process_stratum target so we can mock accessing |
| 1815 | registers. */ |
| 1816 | push_target (&mock_target); |
| 1817 | |
| 1818 | /* Pop it again on exit (return/exception). */ |
| 1819 | struct on_exit |
| 1820 | { |
| 1821 | ~on_exit () |
| 1822 | { |
| 1823 | pop_all_targets_at_and_above (process_stratum); |
| 1824 | } |
| 1825 | } pop_targets; |
| 1826 | |
| 1827 | /* Switch to the mock thread. */ |
| 1828 | scoped_restore restore_inferior_ptid |
| 1829 | = make_scoped_restore (&inferior_ptid, mock_ptid); |
| 1830 | |
| 1831 | /* Test that read one raw register from regcache_no_target will go |
| 1832 | to the target layer. */ |
| 1833 | int regnum; |
| 1834 | |
| 1835 | /* Find a raw register which size isn't zero. */ |
| 1836 | for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++) |
| 1837 | { |
| 1838 | if (register_size (gdbarch, regnum) != 0) |
| 1839 | break; |
| 1840 | } |
| 1841 | |
| 1842 | readwrite_regcache readwrite (gdbarch); |
| 1843 | gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum)); |
| 1844 | |
| 1845 | readwrite.raw_read (regnum, buf.data ()); |
| 1846 | |
| 1847 | /* raw_read calls target_fetch_registers. */ |
| 1848 | SELF_CHECK (mock_target.fetch_registers_called > 0); |
| 1849 | mock_target.reset (); |
| 1850 | |
| 1851 | /* Mark all raw registers valid, so the following raw registers |
| 1852 | accesses won't go to target. */ |
| 1853 | for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++) |
| 1854 | readwrite.raw_update (i); |
| 1855 | |
| 1856 | mock_target.reset (); |
| 1857 | /* Then, read all raw and pseudo registers, and don't expect calling |
| 1858 | to_{fetch,store}_registers. */ |
| 1859 | for (int regnum = 0; |
| 1860 | regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); |
| 1861 | regnum++) |
| 1862 | { |
| 1863 | if (register_size (gdbarch, regnum) == 0) |
| 1864 | continue; |
| 1865 | |
| 1866 | gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum)); |
| 1867 | |
| 1868 | SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ())); |
| 1869 | |
| 1870 | SELF_CHECK (mock_target.fetch_registers_called == 0); |
| 1871 | SELF_CHECK (mock_target.store_registers_called == 0); |
| 1872 | |
| 1873 | /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */ |
| 1874 | if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu) |
| 1875 | SELF_CHECK (mock_target.xfer_partial_called == 0); |
| 1876 | |
| 1877 | mock_target.reset (); |
| 1878 | } |
| 1879 | |
| 1880 | regcache readonly (regcache::readonly, readwrite); |
| 1881 | |
| 1882 | /* GDB may go to target layer to fetch all registers and memory for |
| 1883 | readonly regcache. */ |
| 1884 | mock_target.reset (); |
| 1885 | |
| 1886 | for (int regnum = 0; |
| 1887 | regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch); |
| 1888 | regnum++) |
| 1889 | { |
| 1890 | if (register_size (gdbarch, regnum) == 0) |
| 1891 | continue; |
| 1892 | |
| 1893 | gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum)); |
| 1894 | enum register_status status = readonly.cooked_read (regnum, |
| 1895 | buf.data ()); |
| 1896 | |
| 1897 | if (regnum < gdbarch_num_regs (gdbarch)) |
| 1898 | { |
| 1899 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; |
| 1900 | |
| 1901 | if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300 |
| 1902 | || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh |
| 1903 | || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850 |
| 1904 | || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep |
| 1905 | || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850 |
| 1906 | || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300 |
| 1907 | || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score) |
| 1908 | { |
| 1909 | /* Raw registers. If raw registers are not in save_reggroup, |
| 1910 | their status are unknown. */ |
| 1911 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) |
| 1912 | SELF_CHECK (status == REG_VALID); |
| 1913 | else |
| 1914 | SELF_CHECK (status == REG_UNKNOWN); |
| 1915 | } |
| 1916 | else |
| 1917 | SELF_CHECK (status == REG_VALID); |
| 1918 | } |
| 1919 | else |
| 1920 | { |
| 1921 | if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) |
| 1922 | SELF_CHECK (status == REG_VALID); |
| 1923 | else |
| 1924 | { |
| 1925 | /* If pseudo registers are not in save_reggroup, some of |
| 1926 | them can be computed from saved raw registers, but some |
| 1927 | of them are unknown. */ |
| 1928 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; |
| 1929 | |
| 1930 | if (bfd_arch == bfd_arch_frv |
| 1931 | || bfd_arch == bfd_arch_m32c |
| 1932 | || bfd_arch == bfd_arch_mep |
| 1933 | || bfd_arch == bfd_arch_sh) |
| 1934 | SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN); |
| 1935 | else if (bfd_arch == bfd_arch_mips |
| 1936 | || bfd_arch == bfd_arch_h8300) |
| 1937 | SELF_CHECK (status == REG_UNKNOWN); |
| 1938 | else |
| 1939 | SELF_CHECK (status == REG_VALID); |
| 1940 | } |
| 1941 | } |
| 1942 | |
| 1943 | SELF_CHECK (mock_target.fetch_registers_called == 0); |
| 1944 | SELF_CHECK (mock_target.store_registers_called == 0); |
| 1945 | SELF_CHECK (mock_target.xfer_partial_called == 0); |
| 1946 | |
| 1947 | mock_target.reset (); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | /* Test regcache::cooked_write by writing some expected contents to |
| 1952 | registers, and checking that contents read from registers and the |
| 1953 | expected contents are the same. */ |
| 1954 | |
| 1955 | static void |
| 1956 | cooked_write_test (struct gdbarch *gdbarch) |
| 1957 | { |
| 1958 | /* Error out if debugging something, because we're going to push the |
| 1959 | test target, which would pop any existing target. */ |
| 1960 | if (current_target.to_stratum >= process_stratum) |
| 1961 | error (_("target already pushed")); |
| 1962 | |
| 1963 | /* Create a mock environment. A process_stratum target pushed. */ |
| 1964 | |
| 1965 | target_ops_no_register mock_target; |
| 1966 | |
| 1967 | /* Push the process_stratum target so we can mock accessing |
| 1968 | registers. */ |
| 1969 | push_target (&mock_target); |
| 1970 | |
| 1971 | /* Pop it again on exit (return/exception). */ |
| 1972 | struct on_exit |
| 1973 | { |
| 1974 | ~on_exit () |
| 1975 | { |
| 1976 | pop_all_targets_at_and_above (process_stratum); |
| 1977 | } |
| 1978 | } pop_targets; |
| 1979 | |
| 1980 | readwrite_regcache readwrite (gdbarch); |
| 1981 | |
| 1982 | const int num_regs = (gdbarch_num_regs (gdbarch) |
| 1983 | + gdbarch_num_pseudo_regs (gdbarch)); |
| 1984 | |
| 1985 | for (auto regnum = 0; regnum < num_regs; regnum++) |
| 1986 | { |
| 1987 | if (register_size (gdbarch, regnum) == 0 |
| 1988 | || gdbarch_cannot_store_register (gdbarch, regnum)) |
| 1989 | continue; |
| 1990 | |
| 1991 | auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; |
| 1992 | |
| 1993 | if ((bfd_arch == bfd_arch_sparc |
| 1994 | /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM, |
| 1995 | SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */ |
| 1996 | && gdbarch_ptr_bit (gdbarch) == 64 |
| 1997 | && (regnum >= gdbarch_num_regs (gdbarch) |
| 1998 | && regnum <= gdbarch_num_regs (gdbarch) + 4)) |
| 1999 | || (bfd_arch == bfd_arch_sh |
| 2000 | /* FPSCR_C_REGNUM in sh64 is hard to test. */ |
| 2001 | && gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh5 |
| 2002 | && regnum == 243) |
| 2003 | || (bfd_arch == bfd_arch_spu |
| 2004 | /* SPU pseudo registers except SPU_SP_REGNUM are got by |
| 2005 | TARGET_OBJECT_SPU. */ |
| 2006 | && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130)) |
| 2007 | continue; |
| 2008 | |
| 2009 | std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0); |
| 2010 | std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0); |
| 2011 | const auto type = register_type (gdbarch, regnum); |
| 2012 | |
| 2013 | if (TYPE_CODE (type) == TYPE_CODE_FLT |
| 2014 | || TYPE_CODE (type) == TYPE_CODE_DECFLOAT) |
| 2015 | { |
| 2016 | /* Generate valid float format. */ |
| 2017 | target_float_from_string (expected.data (), type, "1.25"); |
| 2018 | } |
| 2019 | else if (TYPE_CODE (type) == TYPE_CODE_INT |
| 2020 | || TYPE_CODE (type) == TYPE_CODE_ARRAY |
| 2021 | || TYPE_CODE (type) == TYPE_CODE_PTR |
| 2022 | || TYPE_CODE (type) == TYPE_CODE_UNION |
| 2023 | || TYPE_CODE (type) == TYPE_CODE_STRUCT) |
| 2024 | { |
| 2025 | if (bfd_arch == bfd_arch_ia64 |
| 2026 | || (regnum >= gdbarch_num_regs (gdbarch) |
| 2027 | && (bfd_arch == bfd_arch_xtensa |
| 2028 | || bfd_arch == bfd_arch_bfin |
| 2029 | || bfd_arch == bfd_arch_m32c |
| 2030 | /* m68hc11 pseudo registers are in memory. */ |
| 2031 | || bfd_arch == bfd_arch_m68hc11 |
| 2032 | || bfd_arch == bfd_arch_m68hc12 |
| 2033 | || bfd_arch == bfd_arch_s390)) |
| 2034 | || (bfd_arch == bfd_arch_frv |
| 2035 | /* FRV pseudo registers except iacc0. */ |
| 2036 | && regnum > gdbarch_num_regs (gdbarch))) |
| 2037 | { |
| 2038 | /* Skip setting the expected values for some architecture |
| 2039 | registers. */ |
| 2040 | } |
| 2041 | else if (bfd_arch == bfd_arch_rl78 && regnum == 40) |
| 2042 | { |
| 2043 | /* RL78_PC_REGNUM */ |
| 2044 | for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++) |
| 2045 | expected[j] = j; |
| 2046 | } |
| 2047 | else |
| 2048 | { |
| 2049 | for (auto j = 0; j < register_size (gdbarch, regnum); j++) |
| 2050 | expected[j] = j; |
| 2051 | } |
| 2052 | } |
| 2053 | else if (TYPE_CODE (type) == TYPE_CODE_FLAGS) |
| 2054 | { |
| 2055 | /* No idea how to test flags. */ |
| 2056 | continue; |
| 2057 | } |
| 2058 | else |
| 2059 | { |
| 2060 | /* If we don't know how to create the expected value for the |
| 2061 | this type, make it fail. */ |
| 2062 | SELF_CHECK (0); |
| 2063 | } |
| 2064 | |
| 2065 | readwrite.cooked_write (regnum, expected.data ()); |
| 2066 | |
| 2067 | SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID); |
| 2068 | SELF_CHECK (expected == buf); |
| 2069 | } |
| 2070 | } |
| 2071 | |
| 2072 | } // namespace selftests |
| 2073 | #endif /* GDB_SELF_TEST */ |
| 2074 | |
| 2075 | void |
| 2076 | _initialize_regcache (void) |
| 2077 | { |
| 2078 | regcache_descr_handle |
| 2079 | = gdbarch_data_register_post_init (init_regcache_descr); |
| 2080 | |
| 2081 | observer_attach_target_changed (regcache_observer_target_changed); |
| 2082 | observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed); |
| 2083 | |
| 2084 | add_com ("flushregs", class_maintenance, reg_flush_command, |
| 2085 | _("Force gdb to flush its register cache (maintainer command)")); |
| 2086 | |
| 2087 | add_cmd ("registers", class_maintenance, maintenance_print_registers, |
| 2088 | _("Print the internal register configuration.\n" |
| 2089 | "Takes an optional file parameter."), &maintenanceprintlist); |
| 2090 | add_cmd ("raw-registers", class_maintenance, |
| 2091 | maintenance_print_raw_registers, |
| 2092 | _("Print the internal register configuration " |
| 2093 | "including raw values.\n" |
| 2094 | "Takes an optional file parameter."), &maintenanceprintlist); |
| 2095 | add_cmd ("cooked-registers", class_maintenance, |
| 2096 | maintenance_print_cooked_registers, |
| 2097 | _("Print the internal register configuration " |
| 2098 | "including cooked values.\n" |
| 2099 | "Takes an optional file parameter."), &maintenanceprintlist); |
| 2100 | add_cmd ("register-groups", class_maintenance, |
| 2101 | maintenance_print_register_groups, |
| 2102 | _("Print the internal register configuration " |
| 2103 | "including each register's group.\n" |
| 2104 | "Takes an optional file parameter."), |
| 2105 | &maintenanceprintlist); |
| 2106 | add_cmd ("remote-registers", class_maintenance, |
| 2107 | maintenance_print_remote_registers, _("\ |
| 2108 | Print the internal register configuration including each register's\n\ |
| 2109 | remote register number and buffer offset in the g/G packets.\n\ |
| 2110 | Takes an optional file parameter."), |
| 2111 | &maintenanceprintlist); |
| 2112 | |
| 2113 | #if GDB_SELF_TEST |
| 2114 | selftests::register_test ("current_regcache", selftests::current_regcache_test); |
| 2115 | |
| 2116 | selftests::register_test_foreach_arch ("regcache::cooked_read_test", |
| 2117 | selftests::cooked_read_test); |
| 2118 | selftests::register_test_foreach_arch ("regcache::cooked_write_test", |
| 2119 | selftests::cooked_write_test); |
| 2120 | #endif |
| 2121 | } |