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