1 1.1 christos /* Cache and manage the values of registers for GDB, the GNU debugger. 2 1.1 christos 3 1.11 christos Copyright (C) 1986-2024 Free Software Foundation, Inc. 4 1.1 christos 5 1.1 christos This file is part of GDB. 6 1.1 christos 7 1.1 christos This program is free software; you can redistribute it and/or modify 8 1.1 christos it under the terms of the GNU General Public License as published by 9 1.1 christos the Free Software Foundation; either version 3 of the License, or 10 1.1 christos (at your option) any later version. 11 1.1 christos 12 1.1 christos This program is distributed in the hope that it will be useful, 13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 1.1 christos GNU General Public License for more details. 16 1.1 christos 17 1.1 christos You should have received a copy of the GNU General Public License 18 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 19 1.1 christos 20 1.11 christos #include "extract-store-integer.h" 21 1.1 christos #include "inferior.h" 22 1.8 christos #include "gdbthread.h" 23 1.1 christos #include "target.h" 24 1.8 christos #include "test-target.h" 25 1.9 christos #include "scoped-mock-context.h" 26 1.1 christos #include "gdbarch.h" 27 1.1 christos #include "regcache.h" 28 1.1 christos #include "reggroups.h" 29 1.8 christos #include "observable.h" 30 1.3 christos #include "regset.h" 31 1.9 christos #include <unordered_map> 32 1.10 christos #include "cli/cli-cmds.h" 33 1.1 christos 34 1.1 christos /* 35 1.1 christos * DATA STRUCTURE 36 1.1 christos * 37 1.1 christos * Here is the actual register cache. 38 1.1 christos */ 39 1.1 christos 40 1.1 christos /* Per-architecture object describing the layout of a register cache. 41 1.1 christos Computed once when the architecture is created. */ 42 1.1 christos 43 1.1 christos struct regcache_descr 44 1.1 christos { 45 1.1 christos /* The architecture this descriptor belongs to. */ 46 1.10 christos struct gdbarch *gdbarch = nullptr; 47 1.1 christos 48 1.1 christos /* The raw register cache. Each raw (or hard) register is supplied 49 1.1 christos by the target interface. The raw cache should not contain 50 1.1 christos redundant information - if the PC is constructed from two 51 1.1 christos registers then those registers and not the PC lives in the raw 52 1.1 christos cache. */ 53 1.10 christos long sizeof_raw_registers = 0; 54 1.1 christos 55 1.1 christos /* The cooked register space. Each cooked register in the range 56 1.1 christos [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw 57 1.1 christos register. The remaining [NR_RAW_REGISTERS 58 1.1 christos .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto 59 1.1 christos both raw registers and memory by the architecture methods 60 1.1 christos gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */ 61 1.10 christos int nr_cooked_registers = 0; 62 1.10 christos long sizeof_cooked_registers = 0; 63 1.1 christos 64 1.1 christos /* Offset and size (in 8 bit bytes), of each register in the 65 1.1 christos register cache. All registers (including those in the range 66 1.1 christos [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an 67 1.1 christos offset. */ 68 1.10 christos long *register_offset = nullptr; 69 1.10 christos long *sizeof_register = nullptr; 70 1.1 christos 71 1.1 christos /* Cached table containing the type of each register. */ 72 1.10 christos struct type **register_type = nullptr; 73 1.1 christos }; 74 1.1 christos 75 1.10 christos static const registry<gdbarch>::key<struct regcache_descr> 76 1.10 christos regcache_descr_handle; 77 1.10 christos 78 1.10 christos static struct regcache_descr * 79 1.1 christos init_regcache_descr (struct gdbarch *gdbarch) 80 1.1 christos { 81 1.1 christos int i; 82 1.1 christos struct regcache_descr *descr; 83 1.1 christos gdb_assert (gdbarch != NULL); 84 1.1 christos 85 1.1 christos /* Create an initial, zero filled, table. */ 86 1.10 christos descr = new struct regcache_descr; 87 1.1 christos descr->gdbarch = gdbarch; 88 1.1 christos 89 1.1 christos /* Total size of the register space. The raw registers are mapped 90 1.1 christos directly onto the raw register cache while the pseudo's are 91 1.1 christos either mapped onto raw-registers or memory. */ 92 1.8 christos descr->nr_cooked_registers = gdbarch_num_cooked_regs (gdbarch); 93 1.1 christos 94 1.1 christos /* Fill in a table of register types. */ 95 1.1 christos descr->register_type 96 1.1 christos = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, 97 1.1 christos struct type *); 98 1.1 christos for (i = 0; i < descr->nr_cooked_registers; i++) 99 1.1 christos descr->register_type[i] = gdbarch_register_type (gdbarch, i); 100 1.1 christos 101 1.1 christos /* Construct a strictly RAW register cache. Don't allow pseudo's 102 1.1 christos into the register cache. */ 103 1.1 christos 104 1.1 christos /* Lay out the register cache. 105 1.1 christos 106 1.9 christos NOTE: cagney/2002-05-22: Only register_type () is used when 107 1.1 christos constructing the register cache. It is assumed that the 108 1.1 christos register's raw size, virtual size and type length are all the 109 1.1 christos same. */ 110 1.1 christos 111 1.1 christos { 112 1.1 christos long offset = 0; 113 1.1 christos 114 1.1 christos descr->sizeof_register 115 1.1 christos = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); 116 1.1 christos descr->register_offset 117 1.1 christos = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long); 118 1.8 christos for (i = 0; i < gdbarch_num_regs (gdbarch); i++) 119 1.1 christos { 120 1.10 christos descr->sizeof_register[i] = descr->register_type[i]->length (); 121 1.1 christos descr->register_offset[i] = offset; 122 1.1 christos offset += descr->sizeof_register[i]; 123 1.1 christos } 124 1.1 christos /* Set the real size of the raw register cache buffer. */ 125 1.1 christos descr->sizeof_raw_registers = offset; 126 1.1 christos 127 1.1 christos for (; i < descr->nr_cooked_registers; i++) 128 1.1 christos { 129 1.10 christos descr->sizeof_register[i] = descr->register_type[i]->length (); 130 1.1 christos descr->register_offset[i] = offset; 131 1.1 christos offset += descr->sizeof_register[i]; 132 1.1 christos } 133 1.1 christos /* Set the real size of the readonly register cache buffer. */ 134 1.1 christos descr->sizeof_cooked_registers = offset; 135 1.1 christos } 136 1.1 christos 137 1.1 christos return descr; 138 1.1 christos } 139 1.1 christos 140 1.1 christos static struct regcache_descr * 141 1.1 christos regcache_descr (struct gdbarch *gdbarch) 142 1.1 christos { 143 1.10 christos struct regcache_descr *result = regcache_descr_handle.get (gdbarch); 144 1.10 christos if (result == nullptr) 145 1.10 christos { 146 1.10 christos result = init_regcache_descr (gdbarch); 147 1.10 christos regcache_descr_handle.set (gdbarch, result); 148 1.10 christos } 149 1.10 christos 150 1.10 christos return result; 151 1.1 christos } 152 1.1 christos 153 1.1 christos /* Utility functions returning useful register attributes stored in 154 1.1 christos the regcache descr. */ 155 1.1 christos 156 1.1 christos struct type * 157 1.1 christos register_type (struct gdbarch *gdbarch, int regnum) 158 1.1 christos { 159 1.1 christos struct regcache_descr *descr = regcache_descr (gdbarch); 160 1.1 christos 161 1.1 christos gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers); 162 1.1 christos return descr->register_type[regnum]; 163 1.1 christos } 164 1.1 christos 165 1.1 christos /* Utility functions returning useful register attributes stored in 166 1.1 christos the regcache descr. */ 167 1.1 christos 168 1.1 christos int 169 1.1 christos register_size (struct gdbarch *gdbarch, int regnum) 170 1.1 christos { 171 1.1 christos struct regcache_descr *descr = regcache_descr (gdbarch); 172 1.1 christos int size; 173 1.1 christos 174 1.8 christos gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch)); 175 1.1 christos size = descr->sizeof_register[regnum]; 176 1.1 christos return size; 177 1.1 christos } 178 1.1 christos 179 1.9 christos /* See gdbsupport/common-regcache.h. */ 180 1.6 christos 181 1.6 christos int 182 1.11 christos regcache_register_size (const reg_buffer_common *regcache, int n) 183 1.6 christos { 184 1.11 christos return register_size 185 1.11 christos (gdb::checked_static_cast<const struct regcache *> (regcache)->arch (), n); 186 1.6 christos } 187 1.6 christos 188 1.8 christos reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo) 189 1.8 christos : m_has_pseudo (has_pseudo) 190 1.1 christos { 191 1.8 christos gdb_assert (gdbarch != NULL); 192 1.8 christos m_descr = regcache_descr (gdbarch); 193 1.1 christos 194 1.10 christos /* We don't zero-initialize the M_REGISTERS array, as the bytes it contains 195 1.10 christos aren't meaningful as long as the corresponding register status is not 196 1.10 christos REG_VALID. */ 197 1.8 christos if (has_pseudo) 198 1.8 christos { 199 1.10 christos m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers]); 200 1.8 christos m_register_status.reset 201 1.8 christos (new register_status[m_descr->nr_cooked_registers] ()); 202 1.1 christos } 203 1.1 christos else 204 1.1 christos { 205 1.10 christos m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers]); 206 1.8 christos m_register_status.reset 207 1.8 christos (new register_status[gdbarch_num_regs (gdbarch)] ()); 208 1.8 christos } 209 1.1 christos } 210 1.1 christos 211 1.11 christos regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch) 212 1.8 christos /* The register buffers. A read/write register cache can only hold 213 1.8 christos [0 .. gdbarch_num_regs). */ 214 1.11 christos : detached_regcache (gdbarch, false), 215 1.11 christos m_inf_for_target_calls (inf_for_target_calls) 216 1.1 christos { 217 1.8 christos m_ptid = minus_one_ptid; 218 1.1 christos } 219 1.1 christos 220 1.8 christos readonly_detached_regcache::readonly_detached_regcache (regcache &src) 221 1.8 christos : readonly_detached_regcache (src.arch (), 222 1.11 christos [&src] (int regnum, 223 1.11 christos gdb::array_view<gdb_byte> buf) 224 1.11 christos { return src.cooked_read (regnum, buf); }) 225 1.1 christos { 226 1.1 christos } 227 1.1 christos 228 1.8 christos gdbarch * 229 1.8 christos reg_buffer::arch () const 230 1.1 christos { 231 1.8 christos return m_descr->gdbarch; 232 1.1 christos } 233 1.1 christos 234 1.11 christos /* Helper for reg_buffer::register_buffer. */ 235 1.1 christos 236 1.11 christos template<typename ElemType> 237 1.11 christos gdb::array_view<ElemType> 238 1.8 christos reg_buffer::register_buffer (int regnum) const 239 1.1 christos { 240 1.11 christos assert_regnum (regnum); 241 1.11 christos ElemType *start = &m_registers[m_descr->register_offset[regnum]]; 242 1.11 christos int size = m_descr->sizeof_register[regnum]; 243 1.11 christos return gdb::array_view<ElemType> (start, size); 244 1.11 christos } 245 1.11 christos 246 1.11 christos /* See regcache.h. */ 247 1.11 christos 248 1.11 christos gdb::array_view<const gdb_byte> 249 1.11 christos reg_buffer::register_buffer (int regnum) const 250 1.11 christos { 251 1.11 christos return register_buffer<const gdb_byte> (regnum); 252 1.11 christos } 253 1.11 christos 254 1.11 christos /* See regcache.h. */ 255 1.11 christos 256 1.11 christos gdb::array_view<gdb_byte> 257 1.11 christos reg_buffer::register_buffer (int regnum) 258 1.11 christos { 259 1.11 christos return register_buffer<gdb_byte> (regnum); 260 1.1 christos } 261 1.1 christos 262 1.1 christos void 263 1.8 christos reg_buffer::save (register_read_ftype cooked_read) 264 1.1 christos { 265 1.8 christos struct gdbarch *gdbarch = m_descr->gdbarch; 266 1.1 christos 267 1.8 christos /* It should have pseudo registers. */ 268 1.8 christos gdb_assert (m_has_pseudo); 269 1.1 christos /* Clear the dest. */ 270 1.8 christos memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers); 271 1.8 christos memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers); 272 1.1 christos /* Copy over any registers (identified by their membership in the 273 1.1 christos save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs + 274 1.1 christos gdbarch_num_pseudo_regs) range is checked since some architectures need 275 1.1 christos to save/restore `cooked' registers that live in memory. */ 276 1.11 christos for (int regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) 277 1.1 christos { 278 1.1 christos if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) 279 1.1 christos { 280 1.11 christos gdb::array_view<gdb_byte> dst_buf = register_buffer (regnum); 281 1.11 christos register_status status = cooked_read (regnum, dst_buf); 282 1.8 christos 283 1.8 christos gdb_assert (status != REG_UNKNOWN); 284 1.1 christos 285 1.8 christos if (status != REG_VALID) 286 1.11 christos memset (dst_buf.data (), 0, dst_buf.size ()); 287 1.1 christos 288 1.8 christos m_register_status[regnum] = status; 289 1.1 christos } 290 1.1 christos } 291 1.1 christos } 292 1.1 christos 293 1.8 christos void 294 1.8 christos regcache::restore (readonly_detached_regcache *src) 295 1.1 christos { 296 1.8 christos struct gdbarch *gdbarch = m_descr->gdbarch; 297 1.1 christos int regnum; 298 1.1 christos 299 1.8 christos gdb_assert (src != NULL); 300 1.8 christos gdb_assert (src->m_has_pseudo); 301 1.8 christos 302 1.8 christos gdb_assert (gdbarch == src->arch ()); 303 1.8 christos 304 1.1 christos /* Copy over any registers, being careful to only restore those that 305 1.1 christos were both saved and need to be restored. The full [0 .. gdbarch_num_regs 306 1.1 christos + gdbarch_num_pseudo_regs) range is checked since some architectures need 307 1.1 christos to save/restore `cooked' registers that live in memory. */ 308 1.8 christos for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++) 309 1.1 christos { 310 1.1 christos if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)) 311 1.1 christos { 312 1.8 christos if (src->m_register_status[regnum] == REG_VALID) 313 1.8 christos cooked_write (regnum, src->register_buffer (regnum)); 314 1.1 christos } 315 1.1 christos } 316 1.1 christos } 317 1.1 christos 318 1.9 christos /* See gdbsupport/common-regcache.h. */ 319 1.8 christos 320 1.8 christos enum register_status 321 1.8 christos reg_buffer::get_register_status (int regnum) const 322 1.1 christos { 323 1.8 christos assert_regnum (regnum); 324 1.1 christos 325 1.8 christos return m_register_status[regnum]; 326 1.1 christos } 327 1.1 christos 328 1.1 christos void 329 1.8 christos reg_buffer::invalidate (int regnum) 330 1.1 christos { 331 1.8 christos assert_regnum (regnum); 332 1.8 christos m_register_status[regnum] = REG_UNKNOWN; 333 1.1 christos } 334 1.1 christos 335 1.8 christos void 336 1.8 christos reg_buffer::assert_regnum (int regnum) const 337 1.1 christos { 338 1.1 christos gdb_assert (regnum >= 0); 339 1.8 christos if (m_has_pseudo) 340 1.8 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 341 1.1 christos else 342 1.8 christos gdb_assert (regnum < gdbarch_num_regs (arch ())); 343 1.1 christos } 344 1.1 christos 345 1.9 christos /* Type to map a ptid to a list of regcaches (one thread may have multiple 346 1.9 christos regcaches, associated to different gdbarches). */ 347 1.9 christos 348 1.9 christos using ptid_regcache_map 349 1.11 christos = std::unordered_multimap<ptid_t, regcache_up>; 350 1.9 christos 351 1.9 christos /* Type holding regcaches for a given pid. */ 352 1.9 christos 353 1.9 christos using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>; 354 1.9 christos 355 1.9 christos /* Type holding regcaches for a given target. */ 356 1.9 christos 357 1.9 christos using target_pid_ptid_regcache_map 358 1.9 christos = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>; 359 1.9 christos 360 1.9 christos /* Global structure containing the existing regcaches. */ 361 1.1 christos 362 1.1 christos /* NOTE: this is a write-through cache. There is no "dirty" bit for 363 1.1 christos recording if the register values have been changed (eg. by the 364 1.1 christos user). Therefore all registers must be written back to the 365 1.1 christos target when appropriate. */ 366 1.9 christos static target_pid_ptid_regcache_map regcaches; 367 1.1 christos 368 1.11 christos regcache * 369 1.11 christos get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid, 370 1.11 christos gdbarch *arch) 371 1.1 christos { 372 1.11 christos gdb_assert (inf_for_target_calls != nullptr); 373 1.11 christos 374 1.11 christos process_stratum_target *proc_target = inf_for_target_calls->process_target (); 375 1.11 christos gdb_assert (proc_target != nullptr); 376 1.9 christos 377 1.9 christos /* Find the map for this target. */ 378 1.11 christos pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[proc_target]; 379 1.9 christos 380 1.9 christos /* Find the map for this pid. */ 381 1.9 christos ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()]; 382 1.8 christos 383 1.9 christos /* Check first if a regcache for this arch already exists. */ 384 1.9 christos auto range = ptid_regc_map.equal_range (ptid); 385 1.9 christos for (auto it = range.first; it != range.second; ++it) 386 1.9 christos { 387 1.9 christos if (it->second->arch () == arch) 388 1.9 christos return it->second.get (); 389 1.9 christos } 390 1.1 christos 391 1.9 christos /* It does not exist, create it. */ 392 1.11 christos regcache *new_regcache = new regcache (inf_for_target_calls, arch); 393 1.8 christos new_regcache->set_ptid (ptid); 394 1.9 christos /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up 395 1.11 christos constructor explicitly instead of implicitly. */ 396 1.9 christos ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache))); 397 1.1 christos 398 1.1 christos return new_regcache; 399 1.1 christos } 400 1.1 christos 401 1.9 christos static process_stratum_target *current_thread_target; 402 1.1 christos static ptid_t current_thread_ptid; 403 1.1 christos static struct gdbarch *current_thread_arch; 404 1.1 christos 405 1.1 christos struct regcache * 406 1.9 christos get_thread_regcache (process_stratum_target *target, ptid_t ptid) 407 1.1 christos { 408 1.11 christos inferior *inf = find_inferior_ptid (target, ptid); 409 1.11 christos 410 1.9 christos if (!current_thread_arch 411 1.9 christos || target != current_thread_target 412 1.9 christos || current_thread_ptid != ptid) 413 1.1 christos { 414 1.9 christos gdb_assert (ptid != null_ptid); 415 1.9 christos 416 1.1 christos current_thread_ptid = ptid; 417 1.9 christos current_thread_target = target; 418 1.9 christos 419 1.9 christos scoped_restore_current_inferior restore_current_inferior; 420 1.11 christos set_current_inferior (inf); 421 1.1 christos current_thread_arch = target_thread_architecture (ptid); 422 1.1 christos } 423 1.1 christos 424 1.11 christos return get_thread_arch_regcache (inf, ptid, current_thread_arch); 425 1.1 christos } 426 1.1 christos 427 1.8 christos /* See regcache.h. */ 428 1.8 christos 429 1.8 christos struct regcache * 430 1.8 christos get_thread_regcache (thread_info *thread) 431 1.8 christos { 432 1.11 christos gdb_assert (thread->state != THREAD_EXITED); 433 1.11 christos 434 1.9 christos return get_thread_regcache (thread->inf->process_target (), 435 1.9 christos thread->ptid); 436 1.8 christos } 437 1.8 christos 438 1.9 christos /* See gdbsupport/common-regcache.h. */ 439 1.3 christos 440 1.11 christos reg_buffer_common * 441 1.3 christos get_thread_regcache_for_ptid (ptid_t ptid) 442 1.3 christos { 443 1.9 christos /* This function doesn't take a process_stratum_target parameter 444 1.9 christos because it's a gdbsupport/ routine implemented by both gdb and 445 1.9 christos gdbserver. It always refers to a ptid of the current target. */ 446 1.9 christos process_stratum_target *proc_target = current_inferior ()->process_target (); 447 1.9 christos return get_thread_regcache (proc_target, ptid); 448 1.3 christos } 449 1.1 christos 450 1.1 christos /* Observer for the target_changed event. */ 451 1.1 christos 452 1.1 christos static void 453 1.1 christos regcache_observer_target_changed (struct target_ops *target) 454 1.1 christos { 455 1.1 christos registers_changed (); 456 1.1 christos } 457 1.1 christos 458 1.9 christos /* Update regcaches related to OLD_PTID to now use NEW_PTID. */ 459 1.9 christos static void 460 1.9 christos regcache_thread_ptid_changed (process_stratum_target *target, 461 1.9 christos ptid_t old_ptid, ptid_t new_ptid) 462 1.1 christos { 463 1.9 christos /* Look up map for target. */ 464 1.9 christos auto pid_ptid_regc_map_it = regcaches.find (target); 465 1.9 christos if (pid_ptid_regc_map_it == regcaches.end ()) 466 1.9 christos return; 467 1.9 christos 468 1.9 christos /* Look up map for pid. */ 469 1.9 christos pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second; 470 1.9 christos auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ()); 471 1.9 christos if (ptid_regc_map_it == pid_ptid_regc_map.end ()) 472 1.9 christos return; 473 1.9 christos 474 1.9 christos /* Update all regcaches belonging to old_ptid. */ 475 1.9 christos ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second; 476 1.9 christos auto range = ptid_regc_map.equal_range (old_ptid); 477 1.9 christos for (auto it = range.first; it != range.second;) 478 1.9 christos { 479 1.9 christos regcache_up rc = std::move (it->second); 480 1.9 christos rc->set_ptid (new_ptid); 481 1.9 christos 482 1.9 christos /* Remove old before inserting new, to avoid rehashing, 483 1.9 christos which would invalidate iterators. */ 484 1.9 christos it = ptid_regc_map.erase (it); 485 1.9 christos ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc))); 486 1.8 christos } 487 1.1 christos } 488 1.1 christos 489 1.1 christos /* Low level examining and depositing of registers. 490 1.1 christos 491 1.1 christos The caller is responsible for making sure that the inferior is 492 1.1 christos stopped before calling the fetching routines, or it will get 493 1.1 christos garbage. (a change from GDB version 3, in which the caller got the 494 1.1 christos value from the last stop). */ 495 1.1 christos 496 1.1 christos /* REGISTERS_CHANGED () 497 1.1 christos 498 1.1 christos Indicate that registers may have changed, so invalidate the cache. */ 499 1.1 christos 500 1.1 christos void 501 1.9 christos registers_changed_ptid (process_stratum_target *target, ptid_t ptid) 502 1.1 christos { 503 1.9 christos if (target == nullptr) 504 1.1 christos { 505 1.9 christos /* Since there can be ptid clashes between targets, it's not valid to 506 1.9 christos pass a ptid without saying to which target it belongs. */ 507 1.9 christos gdb_assert (ptid == minus_one_ptid); 508 1.9 christos 509 1.9 christos /* Delete all the regcaches of all targets. */ 510 1.9 christos regcaches.clear (); 511 1.9 christos } 512 1.9 christos else if (ptid.is_pid ()) 513 1.9 christos { 514 1.9 christos /* Non-NULL target and pid ptid, delete all regcaches belonging 515 1.9 christos to this (TARGET, PID). */ 516 1.9 christos 517 1.9 christos /* Look up map for target. */ 518 1.9 christos auto pid_ptid_regc_map_it = regcaches.find (target); 519 1.9 christos if (pid_ptid_regc_map_it != regcaches.end ()) 520 1.1 christos { 521 1.9 christos pid_ptid_regcache_map &pid_ptid_regc_map 522 1.9 christos = pid_ptid_regc_map_it->second; 523 1.9 christos 524 1.9 christos pid_ptid_regc_map.erase (ptid.pid ()); 525 1.1 christos } 526 1.9 christos } 527 1.9 christos else if (ptid != minus_one_ptid) 528 1.9 christos { 529 1.9 christos /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging 530 1.9 christos to this (TARGET, PTID). */ 531 1.9 christos 532 1.9 christos /* Look up map for target. */ 533 1.9 christos auto pid_ptid_regc_map_it = regcaches.find (target); 534 1.9 christos if (pid_ptid_regc_map_it != regcaches.end ()) 535 1.9 christos { 536 1.9 christos pid_ptid_regcache_map &pid_ptid_regc_map 537 1.9 christos = pid_ptid_regc_map_it->second; 538 1.9 christos 539 1.9 christos /* Look up map for pid. */ 540 1.9 christos auto ptid_regc_map_it 541 1.9 christos = pid_ptid_regc_map.find (ptid.pid ()); 542 1.9 christos if (ptid_regc_map_it != pid_ptid_regc_map.end ()) 543 1.9 christos { 544 1.9 christos ptid_regcache_map &ptid_regc_map 545 1.9 christos = ptid_regc_map_it->second; 546 1.9 christos 547 1.9 christos ptid_regc_map.erase (ptid); 548 1.9 christos } 549 1.9 christos } 550 1.9 christos } 551 1.9 christos else 552 1.9 christos { 553 1.9 christos /* Non-NULL target and minus_one_ptid, delete all regcaches 554 1.9 christos associated to this target. */ 555 1.9 christos regcaches.erase (target); 556 1.1 christos } 557 1.1 christos 558 1.9 christos if ((target == nullptr || current_thread_target == target) 559 1.9 christos && current_thread_ptid.matches (ptid)) 560 1.1 christos { 561 1.9 christos current_thread_target = NULL; 562 1.1 christos current_thread_ptid = null_ptid; 563 1.1 christos current_thread_arch = NULL; 564 1.1 christos } 565 1.1 christos 566 1.9 christos if ((target == nullptr || current_inferior ()->process_target () == target) 567 1.9 christos && inferior_ptid.matches (ptid)) 568 1.1 christos { 569 1.1 christos /* We just deleted the regcache of the current thread. Need to 570 1.1 christos forget about any frames we have cached, too. */ 571 1.1 christos reinit_frame_cache (); 572 1.1 christos } 573 1.1 christos } 574 1.1 christos 575 1.8 christos /* See regcache.h. */ 576 1.8 christos 577 1.8 christos void 578 1.8 christos registers_changed_thread (thread_info *thread) 579 1.8 christos { 580 1.9 christos registers_changed_ptid (thread->inf->process_target (), thread->ptid); 581 1.8 christos } 582 1.8 christos 583 1.1 christos void 584 1.1 christos registers_changed (void) 585 1.1 christos { 586 1.9 christos registers_changed_ptid (nullptr, minus_one_ptid); 587 1.1 christos } 588 1.1 christos 589 1.7 christos void 590 1.8 christos regcache::raw_update (int regnum) 591 1.1 christos { 592 1.8 christos assert_regnum (regnum); 593 1.7 christos 594 1.1 christos /* Make certain that the register cache is up-to-date with respect 595 1.1 christos to the current thread. This switching shouldn't be necessary 596 1.1 christos only there is still only one target side register cache. Sigh! 597 1.1 christos On the bright side, at least there is a regcache object. */ 598 1.7 christos 599 1.8 christos if (get_register_status (regnum) == REG_UNKNOWN) 600 1.1 christos { 601 1.11 christos std::optional<scoped_restore_current_thread> maybe_restore_thread 602 1.11 christos = maybe_switch_inferior (m_inf_for_target_calls); 603 1.11 christos 604 1.8 christos target_fetch_registers (this, regnum); 605 1.1 christos 606 1.1 christos /* A number of targets can't access the whole set of raw 607 1.1 christos registers (because the debug API provides no means to get at 608 1.1 christos them). */ 609 1.8 christos if (m_register_status[regnum] == REG_UNKNOWN) 610 1.8 christos m_register_status[regnum] = REG_UNAVAILABLE; 611 1.1 christos } 612 1.7 christos } 613 1.7 christos 614 1.11 christos register_status 615 1.11 christos readable_regcache::raw_read (int regnum, gdb::array_view<gdb_byte> dst) 616 1.7 christos { 617 1.11 christos assert_regnum (regnum); 618 1.11 christos gdb_assert (dst.size () == m_descr->sizeof_register[regnum]); 619 1.11 christos 620 1.8 christos raw_update (regnum); 621 1.1 christos 622 1.8 christos if (m_register_status[regnum] != REG_VALID) 623 1.11 christos memset (dst.data (), 0, dst.size ()); 624 1.1 christos else 625 1.11 christos copy (register_buffer (regnum), dst); 626 1.1 christos 627 1.8 christos return m_register_status[regnum]; 628 1.1 christos } 629 1.1 christos 630 1.11 christos register_status 631 1.11 christos readable_regcache::raw_read (int regnum, gdb_byte *dst) 632 1.11 christos { 633 1.11 christos assert_regnum (regnum); 634 1.11 christos int size = m_descr->sizeof_register[regnum]; 635 1.11 christos return raw_read (regnum, gdb::make_array_view (dst, size)); 636 1.11 christos } 637 1.11 christos 638 1.1 christos enum register_status 639 1.1 christos regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val) 640 1.1 christos { 641 1.8 christos gdb_assert (regcache != NULL); 642 1.8 christos return regcache->raw_read (regnum, val); 643 1.8 christos } 644 1.8 christos 645 1.8 christos template<typename T, typename> 646 1.8 christos enum register_status 647 1.8 christos readable_regcache::raw_read (int regnum, T *val) 648 1.8 christos { 649 1.8 christos assert_regnum (regnum); 650 1.11 christos size_t size = m_descr->sizeof_register[regnum]; 651 1.11 christos gdb_byte *buf = (gdb_byte *) alloca (size); 652 1.11 christos auto view = gdb::make_array_view (buf, size); 653 1.11 christos register_status status = raw_read (regnum, view); 654 1.11 christos 655 1.1 christos if (status == REG_VALID) 656 1.11 christos *val = extract_integer<T> (view, gdbarch_byte_order (m_descr->gdbarch)); 657 1.1 christos else 658 1.1 christos *val = 0; 659 1.11 christos 660 1.1 christos return status; 661 1.1 christos } 662 1.1 christos 663 1.1 christos enum register_status 664 1.11 christos regcache_raw_read_unsigned (reg_buffer_common *regcache, int regnum, 665 1.1 christos ULONGEST *val) 666 1.1 christos { 667 1.8 christos gdb_assert (regcache != NULL); 668 1.11 christos return gdb::checked_static_cast<struct regcache *> (regcache)->raw_read 669 1.11 christos (regnum, val); 670 1.8 christos } 671 1.1 christos 672 1.8 christos void 673 1.8 christos regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val) 674 1.8 christos { 675 1.1 christos gdb_assert (regcache != NULL); 676 1.8 christos regcache->raw_write (regnum, val); 677 1.1 christos } 678 1.1 christos 679 1.8 christos template<typename T, typename> 680 1.1 christos void 681 1.8 christos regcache::raw_write (int regnum, T val) 682 1.1 christos { 683 1.11 christos assert_regnum (regnum); 684 1.1 christos 685 1.11 christos int size = m_descr->sizeof_register[regnum]; 686 1.11 christos gdb_byte *buf = (gdb_byte *) alloca (size); 687 1.11 christos auto view = gdb::make_array_view (buf, size); 688 1.11 christos store_integer (view, gdbarch_byte_order (m_descr->gdbarch), val); 689 1.11 christos raw_write (regnum, view); 690 1.1 christos } 691 1.1 christos 692 1.1 christos void 693 1.1 christos regcache_raw_write_unsigned (struct regcache *regcache, int regnum, 694 1.1 christos ULONGEST val) 695 1.1 christos { 696 1.1 christos gdb_assert (regcache != NULL); 697 1.8 christos regcache->raw_write (regnum, val); 698 1.1 christos } 699 1.1 christos 700 1.7 christos LONGEST 701 1.7 christos regcache_raw_get_signed (struct regcache *regcache, int regnum) 702 1.7 christos { 703 1.7 christos LONGEST value; 704 1.7 christos enum register_status status; 705 1.7 christos 706 1.7 christos status = regcache_raw_read_signed (regcache, regnum, &value); 707 1.7 christos if (status == REG_UNAVAILABLE) 708 1.7 christos throw_error (NOT_AVAILABLE_ERROR, 709 1.7 christos _("Register %d is not available"), regnum); 710 1.7 christos return value; 711 1.7 christos } 712 1.7 christos 713 1.11 christos /* See regcache.h. */ 714 1.11 christos 715 1.11 christos register_status 716 1.11 christos readable_regcache::cooked_read (int regnum, gdb::array_view<gdb_byte> dst) 717 1.1 christos { 718 1.1 christos gdb_assert (regnum >= 0); 719 1.8 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 720 1.11 christos 721 1.8 christos if (regnum < num_raw_registers ()) 722 1.11 christos return raw_read (regnum, dst); 723 1.11 christos 724 1.11 christos gdb_assert (dst.size () == m_descr->sizeof_register[regnum]); 725 1.11 christos 726 1.11 christos if (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN) 727 1.8 christos { 728 1.8 christos if (m_register_status[regnum] == REG_VALID) 729 1.11 christos copy (register_buffer (regnum), dst); 730 1.1 christos else 731 1.11 christos memset (dst.data (), 0, dst.size ()); 732 1.1 christos 733 1.8 christos return m_register_status[regnum]; 734 1.1 christos } 735 1.8 christos else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) 736 1.1 christos { 737 1.11 christos register_status result = REG_VALID; 738 1.10 christos scoped_value_mark mark; 739 1.11 christos value *computed = gdbarch_pseudo_register_read_value 740 1.11 christos (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()), 741 1.11 christos regnum); 742 1.1 christos 743 1.11 christos if (computed->entirely_available ()) 744 1.11 christos copy (computed->contents_raw (), dst); 745 1.1 christos else 746 1.1 christos { 747 1.11 christos memset (dst.data (), 0, dst.size ()); 748 1.1 christos result = REG_UNAVAILABLE; 749 1.1 christos } 750 1.1 christos 751 1.1 christos return result; 752 1.1 christos } 753 1.1 christos else 754 1.11 christos return gdbarch_pseudo_register_read (m_descr->gdbarch, this, regnum, 755 1.11 christos dst.data ()); 756 1.11 christos } 757 1.11 christos 758 1.11 christos /* See regcache.h. */ 759 1.11 christos 760 1.11 christos register_status 761 1.11 christos readable_regcache::cooked_read (int regnum, gdb_byte *dst) 762 1.11 christos { 763 1.11 christos gdb_assert (regnum >= 0); 764 1.11 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 765 1.11 christos 766 1.11 christos int size = m_descr->sizeof_register[regnum]; 767 1.11 christos return cooked_read (regnum, gdb::make_array_view (dst, size)); 768 1.1 christos } 769 1.1 christos 770 1.1 christos struct value * 771 1.8 christos readable_regcache::cooked_read_value (int regnum) 772 1.1 christos { 773 1.1 christos gdb_assert (regnum >= 0); 774 1.8 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 775 1.1 christos 776 1.8 christos if (regnum < num_raw_registers () 777 1.8 christos || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN) 778 1.8 christos || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch)) 779 1.1 christos { 780 1.11 christos value *result = value::allocate_register 781 1.11 christos (get_next_frame_sentinel_okay (get_current_frame ()), regnum); 782 1.1 christos 783 1.1 christos /* It is more efficient in general to do this delegation in this 784 1.1 christos direction than in the other one, even though the value-based 785 1.1 christos API is preferred. */ 786 1.11 christos if (cooked_read (regnum, result->contents_raw ()) == REG_UNAVAILABLE) 787 1.11 christos result->mark_bytes_unavailable (0, 788 1.11 christos result->type ()->length ()); 789 1.1 christos 790 1.1 christos return result; 791 1.1 christos } 792 1.1 christos else 793 1.11 christos return gdbarch_pseudo_register_read_value 794 1.11 christos (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()), 795 1.11 christos regnum); 796 1.1 christos } 797 1.1 christos 798 1.1 christos enum register_status 799 1.1 christos regcache_cooked_read_signed (struct regcache *regcache, int regnum, 800 1.1 christos LONGEST *val) 801 1.1 christos { 802 1.8 christos gdb_assert (regcache != NULL); 803 1.8 christos return regcache->cooked_read (regnum, val); 804 1.8 christos } 805 1.8 christos 806 1.8 christos template<typename T, typename> 807 1.8 christos enum register_status 808 1.8 christos readable_regcache::cooked_read (int regnum, T *val) 809 1.8 christos { 810 1.8 christos gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); 811 1.11 christos size_t size = m_descr->sizeof_register[regnum]; 812 1.11 christos gdb_byte *buf = (gdb_byte *) alloca (size); 813 1.11 christos auto view = gdb::make_array_view (buf, size); 814 1.11 christos register_status status = cooked_read (regnum, view); 815 1.1 christos if (status == REG_VALID) 816 1.11 christos *val = extract_integer<T> (view, gdbarch_byte_order (m_descr->gdbarch)); 817 1.1 christos else 818 1.1 christos *val = 0; 819 1.1 christos return status; 820 1.1 christos } 821 1.1 christos 822 1.1 christos enum register_status 823 1.1 christos regcache_cooked_read_unsigned (struct regcache *regcache, int regnum, 824 1.1 christos ULONGEST *val) 825 1.1 christos { 826 1.1 christos gdb_assert (regcache != NULL); 827 1.8 christos return regcache->cooked_read (regnum, val); 828 1.1 christos } 829 1.1 christos 830 1.1 christos void 831 1.1 christos regcache_cooked_write_signed (struct regcache *regcache, int regnum, 832 1.1 christos LONGEST val) 833 1.1 christos { 834 1.1 christos gdb_assert (regcache != NULL); 835 1.8 christos regcache->cooked_write (regnum, val); 836 1.1 christos } 837 1.1 christos 838 1.8 christos template<typename T, typename> 839 1.1 christos void 840 1.8 christos regcache::cooked_write (int regnum, T val) 841 1.1 christos { 842 1.11 christos gdb_assert (regnum >= 0); 843 1.11 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 844 1.1 christos 845 1.11 christos int size = m_descr->sizeof_register[regnum]; 846 1.11 christos gdb_byte *buf = (gdb_byte *) alloca (size); 847 1.11 christos auto view = gdb::make_array_view (buf, size); 848 1.11 christos store_integer (view, gdbarch_byte_order (m_descr->gdbarch), val); 849 1.11 christos cooked_write (regnum, view); 850 1.1 christos } 851 1.1 christos 852 1.6 christos void 853 1.8 christos regcache_cooked_write_unsigned (struct regcache *regcache, int regnum, 854 1.8 christos ULONGEST val) 855 1.6 christos { 856 1.8 christos gdb_assert (regcache != NULL); 857 1.8 christos regcache->cooked_write (regnum, val); 858 1.6 christos } 859 1.6 christos 860 1.1 christos void 861 1.11 christos regcache::raw_write (int regnum, gdb::array_view<const gdb_byte> src) 862 1.1 christos { 863 1.8 christos assert_regnum (regnum); 864 1.11 christos gdb_assert (src.size () == m_descr->sizeof_register[regnum]); 865 1.1 christos 866 1.1 christos /* On the sparc, writing %g0 is a no-op, so we don't even want to 867 1.1 christos change the registers array if something writes to this register. */ 868 1.8 christos if (gdbarch_cannot_store_register (arch (), regnum)) 869 1.1 christos return; 870 1.1 christos 871 1.1 christos /* If we have a valid copy of the register, and new value == old 872 1.1 christos value, then don't bother doing the actual store. */ 873 1.8 christos if (get_register_status (regnum) == REG_VALID 874 1.11 christos && (memcmp (register_buffer (regnum).data (), src.data (), src.size ()) 875 1.11 christos == 0)) 876 1.1 christos return; 877 1.1 christos 878 1.11 christos std::optional<scoped_restore_current_thread> maybe_restore_thread 879 1.11 christos = maybe_switch_inferior (m_inf_for_target_calls); 880 1.11 christos 881 1.8 christos target_prepare_to_store (this); 882 1.11 christos raw_supply (regnum, src); 883 1.3 christos 884 1.8 christos /* Invalidate the register after it is written, in case of a 885 1.8 christos failure. */ 886 1.8 christos auto invalidator 887 1.8 christos = make_scope_exit ([&] { this->invalidate (regnum); }); 888 1.3 christos 889 1.8 christos target_store_registers (this, regnum); 890 1.1 christos 891 1.8 christos /* The target did not throw an error so we can discard invalidating 892 1.8 christos the register. */ 893 1.8 christos invalidator.release (); 894 1.1 christos } 895 1.1 christos 896 1.1 christos void 897 1.11 christos regcache::raw_write (int regnum, const gdb_byte *src) 898 1.11 christos { 899 1.11 christos assert_regnum (regnum); 900 1.11 christos 901 1.11 christos int size = m_descr->sizeof_register[regnum]; 902 1.11 christos raw_write (regnum, gdb::make_array_view (src, size)); 903 1.11 christos } 904 1.11 christos 905 1.11 christos /* See regcache.h. */ 906 1.11 christos 907 1.11 christos void 908 1.11 christos regcache::cooked_write (int regnum, gdb::array_view<const gdb_byte> src) 909 1.1 christos { 910 1.1 christos gdb_assert (regnum >= 0); 911 1.8 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 912 1.11 christos 913 1.8 christos if (regnum < num_raw_registers ()) 914 1.11 christos raw_write (regnum, src); 915 1.11 christos else if (gdbarch_pseudo_register_write_p (m_descr->gdbarch)) 916 1.11 christos gdbarch_pseudo_register_write 917 1.11 christos (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()), 918 1.11 christos regnum, src); 919 1.1 christos else 920 1.11 christos gdbarch_deprecated_pseudo_register_write (m_descr->gdbarch, this, regnum, 921 1.11 christos src.data ()); 922 1.11 christos } 923 1.11 christos 924 1.11 christos /* See regcache.h. */ 925 1.11 christos 926 1.11 christos void 927 1.11 christos regcache::cooked_write (int regnum, const gdb_byte *src) 928 1.11 christos { 929 1.11 christos gdb_assert (regnum >= 0); 930 1.11 christos gdb_assert (regnum < m_descr->nr_cooked_registers); 931 1.11 christos 932 1.11 christos int size = m_descr->sizeof_register[regnum]; 933 1.11 christos return cooked_write (regnum, gdb::make_array_view (src, size)); 934 1.1 christos } 935 1.1 christos 936 1.8 christos /* See regcache.h. */ 937 1.1 christos 938 1.11 christos register_status 939 1.11 christos readable_regcache::read_part (int regnum, int offset, 940 1.11 christos gdb::array_view<gdb_byte> dst, bool is_raw) 941 1.8 christos { 942 1.8 christos int reg_size = register_size (arch (), regnum); 943 1.8 christos 944 1.11 christos gdb_assert (offset >= 0); 945 1.11 christos gdb_assert (offset + dst.size () <= reg_size); 946 1.8 christos 947 1.11 christos if (dst.size () == 0) 948 1.8 christos { 949 1.8 christos /* Nothing to do. */ 950 1.8 christos return REG_VALID; 951 1.8 christos } 952 1.8 christos 953 1.11 christos if (dst.size () == reg_size) 954 1.1 christos { 955 1.8 christos /* Read the full register. */ 956 1.11 christos if (is_raw) 957 1.11 christos return raw_read (regnum, dst); 958 1.11 christos else 959 1.11 christos return cooked_read (regnum, dst); 960 1.1 christos } 961 1.1 christos 962 1.11 christos /* Read full register to buffer. */ 963 1.11 christos register_status status; 964 1.11 christos gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); 965 1.11 christos auto reg = gdb::make_array_view (reg_buf, reg_size); 966 1.11 christos 967 1.11 christos if (is_raw) 968 1.11 christos status = raw_read (regnum, reg); 969 1.11 christos else 970 1.11 christos status = cooked_read (regnum, reg); 971 1.8 christos 972 1.8 christos if (status != REG_VALID) 973 1.8 christos return status; 974 1.8 christos 975 1.8 christos /* Copy out. */ 976 1.11 christos copy (reg.slice (offset, dst.size ()), dst); 977 1.1 christos return REG_VALID; 978 1.1 christos } 979 1.1 christos 980 1.8 christos /* See regcache.h. */ 981 1.8 christos 982 1.8 christos void 983 1.11 christos reg_buffer::raw_collect_part (int regnum, int offset, 984 1.11 christos gdb::array_view<gdb_byte> dst) const 985 1.8 christos { 986 1.8 christos int reg_size = register_size (arch (), regnum); 987 1.8 christos 988 1.11 christos gdb_assert (offset >= 0); 989 1.11 christos gdb_assert (offset + dst.size () <= reg_size); 990 1.8 christos 991 1.11 christos if (dst.size () == 0) 992 1.8 christos { 993 1.8 christos /* Nothing to do. */ 994 1.8 christos return; 995 1.8 christos } 996 1.8 christos 997 1.11 christos if (dst.size () == reg_size) 998 1.8 christos { 999 1.8 christos /* Collect the full register. */ 1000 1.11 christos return raw_collect (regnum, dst); 1001 1.8 christos } 1002 1.8 christos 1003 1.8 christos /* Read to buffer, then write out. */ 1004 1.11 christos gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); 1005 1.11 christos auto reg = gdb::make_array_view (reg_buf, reg_size); 1006 1.8 christos raw_collect (regnum, reg); 1007 1.11 christos copy (reg.slice (offset, dst.size ()), dst); 1008 1.8 christos } 1009 1.8 christos 1010 1.8 christos /* See regcache.h. */ 1011 1.8 christos 1012 1.11 christos register_status 1013 1.11 christos regcache::write_part (int regnum, int offset, 1014 1.11 christos gdb::array_view<const gdb_byte> src, bool is_raw) 1015 1.1 christos { 1016 1.8 christos int reg_size = register_size (arch (), regnum); 1017 1.8 christos 1018 1.11 christos gdb_assert (offset >= 0); 1019 1.11 christos gdb_assert (offset + src.size () <= reg_size); 1020 1.8 christos 1021 1.11 christos if (src.size () == 0) 1022 1.8 christos { 1023 1.8 christos /* Nothing to do. */ 1024 1.8 christos return REG_VALID; 1025 1.8 christos } 1026 1.8 christos 1027 1.11 christos if (src.size () == reg_size) 1028 1.8 christos { 1029 1.8 christos /* Write the full register. */ 1030 1.11 christos if (is_raw) 1031 1.11 christos raw_write (regnum, src); 1032 1.11 christos else 1033 1.11 christos cooked_write (regnum, src); 1034 1.11 christos 1035 1.8 christos return REG_VALID; 1036 1.8 christos } 1037 1.8 christos 1038 1.11 christos /* Read existing register to buffer. */ 1039 1.11 christos register_status status; 1040 1.11 christos gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); 1041 1.11 christos auto reg = gdb::make_array_view (reg_buf, reg_size); 1042 1.11 christos 1043 1.11 christos if (is_raw) 1044 1.11 christos status = raw_read (regnum, reg); 1045 1.11 christos else 1046 1.11 christos status = cooked_read (regnum, reg); 1047 1.1 christos 1048 1.8 christos if (status != REG_VALID) 1049 1.8 christos return status; 1050 1.8 christos 1051 1.8 christos /* Update buffer, then write back to regcache. */ 1052 1.11 christos copy (src, reg.slice (offset, src.size ())); 1053 1.11 christos 1054 1.11 christos if (is_raw) 1055 1.11 christos raw_write (regnum, reg); 1056 1.11 christos else 1057 1.11 christos cooked_write (regnum, reg); 1058 1.11 christos 1059 1.8 christos return REG_VALID; 1060 1.1 christos } 1061 1.1 christos 1062 1.8 christos /* See regcache.h. */ 1063 1.8 christos 1064 1.1 christos void 1065 1.11 christos reg_buffer::raw_supply_part (int regnum, int offset, 1066 1.11 christos gdb::array_view<const gdb_byte> src) 1067 1.1 christos { 1068 1.8 christos int reg_size = register_size (arch (), regnum); 1069 1.8 christos 1070 1.11 christos gdb_assert (offset >= 0); 1071 1.11 christos gdb_assert (offset + src.size () <= reg_size); 1072 1.8 christos 1073 1.11 christos if (src.size () == 0) 1074 1.8 christos { 1075 1.8 christos /* Nothing to do. */ 1076 1.8 christos return; 1077 1.8 christos } 1078 1.1 christos 1079 1.11 christos if (src.size () == reg_size) 1080 1.8 christos { 1081 1.8 christos /* Supply the full register. */ 1082 1.11 christos return raw_supply (regnum, src); 1083 1.8 christos } 1084 1.8 christos 1085 1.8 christos /* Read existing value to buffer. */ 1086 1.11 christos gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size); 1087 1.11 christos auto reg = gdb::make_array_view (reg_buf, reg_size); 1088 1.8 christos raw_collect (regnum, reg); 1089 1.8 christos 1090 1.8 christos /* Write to buffer, then write out. */ 1091 1.11 christos copy (src, reg.slice (offset, src.size ())); 1092 1.8 christos raw_supply (regnum, reg); 1093 1.1 christos } 1094 1.1 christos 1095 1.11 christos register_status 1096 1.11 christos readable_regcache::raw_read_part (int regnum, int offset, 1097 1.11 christos gdb::array_view<gdb_byte> dst) 1098 1.1 christos { 1099 1.8 christos assert_regnum (regnum); 1100 1.11 christos return read_part (regnum, offset, dst, true); 1101 1.8 christos } 1102 1.1 christos 1103 1.8 christos /* See regcache.h. */ 1104 1.8 christos 1105 1.8 christos void 1106 1.11 christos regcache::raw_write_part (int regnum, int offset, 1107 1.11 christos gdb::array_view<const gdb_byte> src) 1108 1.8 christos { 1109 1.8 christos assert_regnum (regnum); 1110 1.11 christos write_part (regnum, offset, src, true); 1111 1.8 christos } 1112 1.8 christos 1113 1.8 christos /* See regcache.h. */ 1114 1.8 christos 1115 1.11 christos register_status 1116 1.11 christos readable_regcache::cooked_read_part (int regnum, int offset, 1117 1.11 christos gdb::array_view<gdb_byte> dst) 1118 1.8 christos { 1119 1.8 christos gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); 1120 1.11 christos return read_part (regnum, offset, dst, false); 1121 1.1 christos } 1122 1.1 christos 1123 1.8 christos /* See regcache.h. */ 1124 1.8 christos 1125 1.1 christos void 1126 1.11 christos regcache::cooked_write_part (int regnum, int offset, 1127 1.11 christos gdb::array_view<const gdb_byte> src) 1128 1.1 christos { 1129 1.8 christos gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers); 1130 1.11 christos write_part (regnum, offset, src, false); 1131 1.1 christos } 1132 1.1 christos 1133 1.9 christos /* See gdbsupport/common-regcache.h. */ 1134 1.1 christos 1135 1.1 christos void 1136 1.11 christos reg_buffer::raw_supply (int regnum, gdb::array_view<const gdb_byte> src) 1137 1.1 christos { 1138 1.11 christos gdb::array_view<gdb_byte> dst = register_buffer (regnum); 1139 1.1 christos 1140 1.11 christos if (src.data () != nullptr) 1141 1.1 christos { 1142 1.11 christos copy (src, dst); 1143 1.8 christos m_register_status[regnum] = REG_VALID; 1144 1.1 christos } 1145 1.1 christos else 1146 1.1 christos { 1147 1.1 christos /* This memset not strictly necessary, but better than garbage 1148 1.1 christos in case the register value manages to escape somewhere (due 1149 1.1 christos to a bug, no less). */ 1150 1.11 christos memset (dst.data (), 0, dst.size ()); 1151 1.8 christos m_register_status[regnum] = REG_UNAVAILABLE; 1152 1.1 christos } 1153 1.1 christos } 1154 1.1 christos 1155 1.8 christos /* See regcache.h. */ 1156 1.8 christos 1157 1.8 christos void 1158 1.11 christos reg_buffer::raw_supply (int regnum, const void *src) 1159 1.8 christos { 1160 1.11 christos assert_regnum (regnum); 1161 1.8 christos 1162 1.11 christos int size = m_descr->sizeof_register[regnum]; 1163 1.11 christos raw_supply (regnum, gdb::make_array_view ((const gdb_byte *) src, size)); 1164 1.11 christos } 1165 1.8 christos 1166 1.11 christos /* See regcache.h. */ 1167 1.8 christos 1168 1.11 christos void 1169 1.11 christos reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len, 1170 1.11 christos bool is_signed) 1171 1.11 christos { 1172 1.11 christos gdb::array_view<gdb_byte> dst = register_buffer (regnum); 1173 1.11 christos bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); 1174 1.11 christos 1175 1.11 christos copy_integer_to_size (dst.data (), dst.size (), addr, addr_len, is_signed, 1176 1.8 christos byte_order); 1177 1.8 christos m_register_status[regnum] = REG_VALID; 1178 1.8 christos } 1179 1.8 christos 1180 1.8 christos /* See regcache.h. */ 1181 1.8 christos 1182 1.8 christos void 1183 1.8 christos reg_buffer::raw_supply_zeroed (int regnum) 1184 1.8 christos { 1185 1.11 christos gdb::array_view<gdb_byte> dst = register_buffer (regnum); 1186 1.11 christos memset (dst.data (), 0, dst.size ()); 1187 1.8 christos m_register_status[regnum] = REG_VALID; 1188 1.8 christos } 1189 1.8 christos 1190 1.9 christos /* See gdbsupport/common-regcache.h. */ 1191 1.1 christos 1192 1.1 christos void 1193 1.11 christos reg_buffer::raw_collect (int regnum, gdb::array_view<gdb_byte> dst) const 1194 1.1 christos { 1195 1.11 christos gdb::array_view<const gdb_byte> src = register_buffer (regnum); 1196 1.11 christos copy (src, dst); 1197 1.11 christos } 1198 1.11 christos 1199 1.11 christos /* See regcache.h. */ 1200 1.1 christos 1201 1.11 christos void 1202 1.11 christos reg_buffer::raw_collect (int regnum, void *dst) const 1203 1.11 christos { 1204 1.8 christos assert_regnum (regnum); 1205 1.1 christos 1206 1.11 christos int size = m_descr->sizeof_register[regnum]; 1207 1.11 christos return raw_collect (regnum, gdb::make_array_view ((gdb_byte *) dst, size)); 1208 1.1 christos } 1209 1.1 christos 1210 1.8 christos /* See regcache.h. */ 1211 1.3 christos 1212 1.8 christos void 1213 1.8 christos reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len, 1214 1.8 christos bool is_signed) const 1215 1.8 christos { 1216 1.11 christos gdb::array_view<const gdb_byte> dst = register_buffer (regnum); 1217 1.11 christos bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch); 1218 1.11 christos copy_integer_to_size (addr, addr_len, dst.data (), dst.size (), is_signed, 1219 1.8 christos byte_order); 1220 1.8 christos } 1221 1.8 christos 1222 1.8 christos /* See regcache.h. */ 1223 1.8 christos 1224 1.8 christos void 1225 1.8 christos regcache::transfer_regset_register (struct regcache *out_regcache, int regnum, 1226 1.8 christos const gdb_byte *in_buf, gdb_byte *out_buf, 1227 1.8 christos int slot_size, int offs) const 1228 1.8 christos { 1229 1.8 christos struct gdbarch *gdbarch = arch (); 1230 1.8 christos int reg_size = std::min (register_size (gdbarch, regnum), slot_size); 1231 1.8 christos 1232 1.8 christos /* Use part versions and reg_size to prevent possible buffer overflows when 1233 1.8 christos accessing the regcache. */ 1234 1.8 christos 1235 1.8 christos if (out_buf != nullptr) 1236 1.8 christos { 1237 1.11 christos raw_collect_part (regnum, 0, 1238 1.11 christos gdb::make_array_view (out_buf + offs, reg_size)); 1239 1.8 christos 1240 1.8 christos /* Ensure any additional space is cleared. */ 1241 1.8 christos if (slot_size > reg_size) 1242 1.8 christos memset (out_buf + offs + reg_size, 0, slot_size - reg_size); 1243 1.8 christos } 1244 1.8 christos else if (in_buf != nullptr) 1245 1.10 christos { 1246 1.10 christos /* Zero-extend the register value if the slot is smaller than the register. */ 1247 1.10 christos if (slot_size < register_size (gdbarch, regnum)) 1248 1.10 christos out_regcache->raw_supply_zeroed (regnum); 1249 1.11 christos out_regcache->raw_supply_part (regnum, 0, 1250 1.11 christos gdb::make_array_view (in_buf + offs, 1251 1.11 christos reg_size)); 1252 1.10 christos } 1253 1.8 christos else 1254 1.8 christos { 1255 1.8 christos /* Invalidate the register. */ 1256 1.11 christos out_regcache->raw_supply (regnum, {}); 1257 1.8 christos } 1258 1.8 christos } 1259 1.8 christos 1260 1.8 christos /* See regcache.h. */ 1261 1.8 christos 1262 1.8 christos void 1263 1.10 christos regcache::transfer_regset (const struct regset *regset, int regbase, 1264 1.8 christos struct regcache *out_regcache, 1265 1.8 christos int regnum, const gdb_byte *in_buf, 1266 1.8 christos gdb_byte *out_buf, size_t size) const 1267 1.3 christos { 1268 1.3 christos const struct regcache_map_entry *map; 1269 1.3 christos int offs = 0, count; 1270 1.3 christos 1271 1.6 christos for (map = (const struct regcache_map_entry *) regset->regmap; 1272 1.6 christos (count = map->count) != 0; 1273 1.6 christos map++) 1274 1.3 christos { 1275 1.3 christos int regno = map->regno; 1276 1.3 christos int slot_size = map->size; 1277 1.3 christos 1278 1.10 christos if (regno != REGCACHE_MAP_SKIP) 1279 1.10 christos regno += regbase; 1280 1.10 christos 1281 1.3 christos if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) 1282 1.8 christos slot_size = m_descr->sizeof_register[regno]; 1283 1.3 christos 1284 1.3 christos if (regno == REGCACHE_MAP_SKIP 1285 1.3 christos || (regnum != -1 1286 1.3 christos && (regnum < regno || regnum >= regno + count))) 1287 1.3 christos offs += count * slot_size; 1288 1.3 christos 1289 1.3 christos else if (regnum == -1) 1290 1.3 christos for (; count--; regno++, offs += slot_size) 1291 1.3 christos { 1292 1.3 christos if (offs + slot_size > size) 1293 1.11 christos return; 1294 1.3 christos 1295 1.8 christos transfer_regset_register (out_regcache, regno, in_buf, out_buf, 1296 1.8 christos slot_size, offs); 1297 1.3 christos } 1298 1.3 christos else 1299 1.3 christos { 1300 1.3 christos /* Transfer a single register and return. */ 1301 1.3 christos offs += (regnum - regno) * slot_size; 1302 1.3 christos if (offs + slot_size > size) 1303 1.3 christos return; 1304 1.3 christos 1305 1.8 christos transfer_regset_register (out_regcache, regnum, in_buf, out_buf, 1306 1.8 christos slot_size, offs); 1307 1.3 christos return; 1308 1.3 christos } 1309 1.3 christos } 1310 1.3 christos } 1311 1.3 christos 1312 1.3 christos /* Supply register REGNUM from BUF to REGCACHE, using the register map 1313 1.3 christos in REGSET. If REGNUM is -1, do this for all registers in REGSET. 1314 1.3 christos If BUF is NULL, set the register(s) to "unavailable" status. */ 1315 1.3 christos 1316 1.3 christos void 1317 1.3 christos regcache_supply_regset (const struct regset *regset, 1318 1.3 christos struct regcache *regcache, 1319 1.3 christos int regnum, const void *buf, size_t size) 1320 1.3 christos { 1321 1.8 christos regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size); 1322 1.8 christos } 1323 1.8 christos 1324 1.10 christos /* See regcache.h. */ 1325 1.10 christos 1326 1.8 christos void 1327 1.10 christos regcache::supply_regset (const struct regset *regset, int regbase, 1328 1.8 christos int regnum, const void *buf, size_t size) 1329 1.8 christos { 1330 1.10 christos transfer_regset (regset, regbase, this, regnum, (const gdb_byte *) buf, 1331 1.10 christos nullptr, size); 1332 1.3 christos } 1333 1.3 christos 1334 1.3 christos /* Collect register REGNUM from REGCACHE to BUF, using the register 1335 1.3 christos map in REGSET. If REGNUM is -1, do this for all registers in 1336 1.3 christos REGSET. */ 1337 1.3 christos 1338 1.3 christos void 1339 1.3 christos regcache_collect_regset (const struct regset *regset, 1340 1.3 christos const struct regcache *regcache, 1341 1.3 christos int regnum, void *buf, size_t size) 1342 1.3 christos { 1343 1.8 christos regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size); 1344 1.3 christos } 1345 1.3 christos 1346 1.10 christos /* See regcache.h */ 1347 1.10 christos 1348 1.8 christos void 1349 1.10 christos regcache::collect_regset (const struct regset *regset, int regbase, 1350 1.8 christos int regnum, void *buf, size_t size) const 1351 1.8 christos { 1352 1.10 christos transfer_regset (regset, regbase, nullptr, regnum, nullptr, (gdb_byte *) buf, 1353 1.10 christos size); 1354 1.10 christos } 1355 1.10 christos 1356 1.10 christos bool 1357 1.10 christos regcache_map_supplies (const struct regcache_map_entry *map, int regnum, 1358 1.10 christos struct gdbarch *gdbarch, size_t size) 1359 1.10 christos { 1360 1.10 christos int offs = 0, count; 1361 1.10 christos 1362 1.10 christos for (; (count = map->count) != 0; map++) 1363 1.10 christos { 1364 1.10 christos int regno = map->regno; 1365 1.10 christos int slot_size = map->size; 1366 1.10 christos 1367 1.10 christos if (slot_size == 0 && regno != REGCACHE_MAP_SKIP) 1368 1.10 christos slot_size = register_size (gdbarch, regno); 1369 1.10 christos 1370 1.10 christos if (regno != REGCACHE_MAP_SKIP && regnum >= regno 1371 1.10 christos && regnum < regno + count) 1372 1.10 christos return offs + (regnum - regno + 1) * slot_size <= size; 1373 1.10 christos 1374 1.10 christos offs += count * slot_size; 1375 1.10 christos if (offs >= size) 1376 1.10 christos return false; 1377 1.10 christos } 1378 1.10 christos return false; 1379 1.8 christos } 1380 1.8 christos 1381 1.9 christos /* See gdbsupport/common-regcache.h. */ 1382 1.8 christos 1383 1.8 christos bool 1384 1.8 christos reg_buffer::raw_compare (int regnum, const void *buf, int offset) const 1385 1.8 christos { 1386 1.8 christos gdb_assert (buf != NULL); 1387 1.8 christos 1388 1.11 christos gdb::array_view<const gdb_byte> regbuf = register_buffer (regnum); 1389 1.11 christos gdb_assert (offset <= regbuf.size ()); 1390 1.11 christos regbuf = regbuf.slice (offset); 1391 1.8 christos 1392 1.11 christos return memcmp (buf, regbuf.data (), regbuf.size ()) == 0; 1393 1.8 christos } 1394 1.1 christos 1395 1.1 christos /* Special handling for register PC. */ 1396 1.1 christos 1397 1.1 christos CORE_ADDR 1398 1.11 christos regcache_read_pc (reg_buffer_common *reg_buf) 1399 1.1 christos { 1400 1.11 christos regcache *regcache = gdb::checked_static_cast<struct regcache *> (reg_buf); 1401 1.8 christos struct gdbarch *gdbarch = regcache->arch (); 1402 1.1 christos 1403 1.1 christos CORE_ADDR pc_val; 1404 1.1 christos 1405 1.1 christos if (gdbarch_read_pc_p (gdbarch)) 1406 1.1 christos pc_val = gdbarch_read_pc (gdbarch, regcache); 1407 1.1 christos /* Else use per-frame method on get_current_frame. */ 1408 1.1 christos else if (gdbarch_pc_regnum (gdbarch) >= 0) 1409 1.1 christos { 1410 1.1 christos ULONGEST raw_val; 1411 1.1 christos 1412 1.1 christos if (regcache_cooked_read_unsigned (regcache, 1413 1.1 christos gdbarch_pc_regnum (gdbarch), 1414 1.1 christos &raw_val) == REG_UNAVAILABLE) 1415 1.1 christos throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available")); 1416 1.1 christos 1417 1.1 christos pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val); 1418 1.1 christos } 1419 1.1 christos else 1420 1.10 christos internal_error (_("regcache_read_pc: Unable to find PC")); 1421 1.1 christos return pc_val; 1422 1.1 christos } 1423 1.1 christos 1424 1.9 christos /* See gdbsupport/common-regcache.h. */ 1425 1.9 christos 1426 1.9 christos CORE_ADDR 1427 1.11 christos regcache_read_pc_protected (reg_buffer_common *regcache) 1428 1.9 christos { 1429 1.9 christos CORE_ADDR pc; 1430 1.9 christos try 1431 1.9 christos { 1432 1.9 christos pc = regcache_read_pc (regcache); 1433 1.9 christos } 1434 1.9 christos catch (const gdb_exception_error &ex) 1435 1.9 christos { 1436 1.9 christos pc = 0; 1437 1.9 christos } 1438 1.9 christos 1439 1.9 christos return pc; 1440 1.9 christos } 1441 1.9 christos 1442 1.1 christos void 1443 1.1 christos regcache_write_pc (struct regcache *regcache, CORE_ADDR pc) 1444 1.1 christos { 1445 1.8 christos struct gdbarch *gdbarch = regcache->arch (); 1446 1.1 christos 1447 1.1 christos if (gdbarch_write_pc_p (gdbarch)) 1448 1.1 christos gdbarch_write_pc (gdbarch, regcache, pc); 1449 1.1 christos else if (gdbarch_pc_regnum (gdbarch) >= 0) 1450 1.1 christos regcache_cooked_write_unsigned (regcache, 1451 1.1 christos gdbarch_pc_regnum (gdbarch), pc); 1452 1.1 christos else 1453 1.10 christos internal_error (_("regcache_write_pc: Unable to update PC")); 1454 1.1 christos 1455 1.1 christos /* Writing the PC (for instance, from "load") invalidates the 1456 1.1 christos current frame. */ 1457 1.1 christos reinit_frame_cache (); 1458 1.1 christos } 1459 1.1 christos 1460 1.8 christos int 1461 1.8 christos reg_buffer::num_raw_registers () const 1462 1.8 christos { 1463 1.8 christos return gdbarch_num_regs (arch ()); 1464 1.8 christos } 1465 1.8 christos 1466 1.11 christos std::string 1467 1.11 christos regcache::register_debug_string (int regno) 1468 1.7 christos { 1469 1.8 christos struct gdbarch *gdbarch = arch (); 1470 1.11 christos std::string s; 1471 1.7 christos 1472 1.7 christos if (regno >= 0 && regno < gdbarch_num_regs (gdbarch) 1473 1.7 christos && gdbarch_register_name (gdbarch, regno)[0] != '\0') 1474 1.11 christos string_appendf (s, "register %s:", gdbarch_register_name (gdbarch, regno)); 1475 1.7 christos else 1476 1.11 christos string_appendf (s, "register %d:", regno); 1477 1.11 christos 1478 1.7 christos if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)) 1479 1.7 christos { 1480 1.11 christos gdb::array_view<gdb_byte> buf = register_buffer (regno); 1481 1.7 christos 1482 1.11 christos string_appendf (s, " = "); 1483 1.11 christos 1484 1.11 christos for (gdb_byte byte : buf) 1485 1.11 christos string_appendf (s, "%02x", byte); 1486 1.11 christos 1487 1.11 christos if (buf.size () <= sizeof (LONGEST)) 1488 1.7 christos { 1489 1.11 christos ULONGEST val 1490 1.11 christos = extract_unsigned_integer (buf, gdbarch_byte_order (gdbarch)); 1491 1.7 christos 1492 1.11 christos string_appendf (s, " %s %s", 1493 1.11 christos core_addr_to_string_nz (val), plongest (val)); 1494 1.7 christos } 1495 1.7 christos } 1496 1.11 christos 1497 1.11 christos return s; 1498 1.7 christos } 1499 1.1 christos 1500 1.10 christos /* Implement 'maint flush register-cache' command. */ 1501 1.10 christos 1502 1.1 christos static void 1503 1.8 christos reg_flush_command (const char *command, int from_tty) 1504 1.1 christos { 1505 1.1 christos /* Force-flush the register cache. */ 1506 1.1 christos registers_changed (); 1507 1.1 christos if (from_tty) 1508 1.10 christos gdb_printf (_("Register cache flushed.\n")); 1509 1.1 christos } 1510 1.1 christos 1511 1.8 christos void 1512 1.8 christos register_dump::dump (ui_file *file) 1513 1.1 christos { 1514 1.8 christos auto descr = regcache_descr (m_gdbarch); 1515 1.1 christos int regnum; 1516 1.1 christos int footnote_nr = 0; 1517 1.1 christos int footnote_register_offset = 0; 1518 1.1 christos int footnote_register_type_name_null = 0; 1519 1.1 christos long register_offset = 0; 1520 1.1 christos 1521 1.8 christos gdb_assert (descr->nr_cooked_registers 1522 1.8 christos == gdbarch_num_cooked_regs (m_gdbarch)); 1523 1.1 christos 1524 1.8 christos for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++) 1525 1.1 christos { 1526 1.1 christos /* Name. */ 1527 1.1 christos if (regnum < 0) 1528 1.10 christos gdb_printf (file, " %-10s", "Name"); 1529 1.1 christos else 1530 1.1 christos { 1531 1.8 christos const char *p = gdbarch_register_name (m_gdbarch, regnum); 1532 1.1 christos 1533 1.10 christos if (p[0] == '\0') 1534 1.1 christos p = "''"; 1535 1.10 christos gdb_printf (file, " %-10s", p); 1536 1.1 christos } 1537 1.1 christos 1538 1.1 christos /* Number. */ 1539 1.1 christos if (regnum < 0) 1540 1.10 christos gdb_printf (file, " %4s", "Nr"); 1541 1.1 christos else 1542 1.10 christos gdb_printf (file, " %4d", regnum); 1543 1.1 christos 1544 1.1 christos /* Relative number. */ 1545 1.1 christos if (regnum < 0) 1546 1.10 christos gdb_printf (file, " %4s", "Rel"); 1547 1.8 christos else if (regnum < gdbarch_num_regs (m_gdbarch)) 1548 1.10 christos gdb_printf (file, " %4d", regnum); 1549 1.1 christos else 1550 1.10 christos gdb_printf (file, " %4d", 1551 1.10 christos (regnum - gdbarch_num_regs (m_gdbarch))); 1552 1.1 christos 1553 1.1 christos /* Offset. */ 1554 1.1 christos if (regnum < 0) 1555 1.10 christos gdb_printf (file, " %6s ", "Offset"); 1556 1.1 christos else 1557 1.1 christos { 1558 1.10 christos gdb_printf (file, " %6ld", 1559 1.10 christos descr->register_offset[regnum]); 1560 1.8 christos if (register_offset != descr->register_offset[regnum] 1561 1.1 christos || (regnum > 0 1562 1.8 christos && (descr->register_offset[regnum] 1563 1.8 christos != (descr->register_offset[regnum - 1] 1564 1.8 christos + descr->sizeof_register[regnum - 1]))) 1565 1.1 christos ) 1566 1.1 christos { 1567 1.1 christos if (!footnote_register_offset) 1568 1.1 christos footnote_register_offset = ++footnote_nr; 1569 1.10 christos gdb_printf (file, "*%d", footnote_register_offset); 1570 1.1 christos } 1571 1.1 christos else 1572 1.10 christos gdb_printf (file, " "); 1573 1.8 christos register_offset = (descr->register_offset[regnum] 1574 1.8 christos + descr->sizeof_register[regnum]); 1575 1.1 christos } 1576 1.1 christos 1577 1.1 christos /* Size. */ 1578 1.1 christos if (regnum < 0) 1579 1.10 christos gdb_printf (file, " %5s ", "Size"); 1580 1.1 christos else 1581 1.10 christos gdb_printf (file, " %5ld", descr->sizeof_register[regnum]); 1582 1.1 christos 1583 1.1 christos /* Type. */ 1584 1.1 christos { 1585 1.1 christos const char *t; 1586 1.8 christos std::string name_holder; 1587 1.1 christos 1588 1.1 christos if (regnum < 0) 1589 1.1 christos t = "Type"; 1590 1.1 christos else 1591 1.1 christos { 1592 1.1 christos static const char blt[] = "builtin_type"; 1593 1.1 christos 1594 1.9 christos t = register_type (m_gdbarch, regnum)->name (); 1595 1.1 christos if (t == NULL) 1596 1.1 christos { 1597 1.1 christos if (!footnote_register_type_name_null) 1598 1.1 christos footnote_register_type_name_null = ++footnote_nr; 1599 1.8 christos name_holder = string_printf ("*%d", 1600 1.8 christos footnote_register_type_name_null); 1601 1.8 christos t = name_holder.c_str (); 1602 1.1 christos } 1603 1.1 christos /* Chop a leading builtin_type. */ 1604 1.5 christos if (startswith (t, blt)) 1605 1.1 christos t += strlen (blt); 1606 1.1 christos } 1607 1.10 christos gdb_printf (file, " %-15s", t); 1608 1.1 christos } 1609 1.1 christos 1610 1.1 christos /* Leading space always present. */ 1611 1.10 christos gdb_printf (file, " "); 1612 1.1 christos 1613 1.8 christos dump_reg (file, regnum); 1614 1.1 christos 1615 1.10 christos gdb_printf (file, "\n"); 1616 1.1 christos } 1617 1.1 christos 1618 1.1 christos if (footnote_register_offset) 1619 1.10 christos gdb_printf (file, "*%d: Inconsistent register offsets.\n", 1620 1.10 christos footnote_register_offset); 1621 1.1 christos if (footnote_register_type_name_null) 1622 1.10 christos gdb_printf (file, 1623 1.10 christos "*%d: Register type's name NULL.\n", 1624 1.10 christos footnote_register_type_name_null); 1625 1.1 christos } 1626 1.1 christos 1627 1.8 christos #if GDB_SELF_TEST 1628 1.9 christos #include "gdbsupport/selftest.h" 1629 1.8 christos #include "selftest-arch.h" 1630 1.8 christos #include "target-float.h" 1631 1.8 christos 1632 1.8 christos namespace selftests { 1633 1.8 christos 1634 1.9 christos static size_t 1635 1.9 christos regcaches_size () 1636 1.8 christos { 1637 1.9 christos size_t size = 0; 1638 1.9 christos 1639 1.9 christos for (auto pid_ptid_regc_map_it = regcaches.cbegin (); 1640 1.9 christos pid_ptid_regc_map_it != regcaches.cend (); 1641 1.9 christos ++pid_ptid_regc_map_it) 1642 1.9 christos { 1643 1.9 christos const pid_ptid_regcache_map &pid_ptid_regc_map 1644 1.9 christos = pid_ptid_regc_map_it->second; 1645 1.9 christos 1646 1.9 christos for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin (); 1647 1.9 christos ptid_regc_map_it != pid_ptid_regc_map.cend (); 1648 1.9 christos ++ptid_regc_map_it) 1649 1.9 christos { 1650 1.9 christos const ptid_regcache_map &ptid_regc_map 1651 1.9 christos = ptid_regc_map_it->second; 1652 1.9 christos 1653 1.9 christos size += ptid_regc_map.size (); 1654 1.9 christos } 1655 1.9 christos } 1656 1.9 christos 1657 1.9 christos return size; 1658 1.9 christos } 1659 1.9 christos 1660 1.9 christos /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */ 1661 1.9 christos 1662 1.9 christos static int 1663 1.9 christos regcache_count (process_stratum_target *target, ptid_t ptid) 1664 1.9 christos { 1665 1.9 christos /* Look up map for target. */ 1666 1.9 christos auto pid_ptid_regc_map_it = regcaches.find (target); 1667 1.9 christos if (pid_ptid_regc_map_it != regcaches.end ()) 1668 1.9 christos { 1669 1.9 christos pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second; 1670 1.9 christos 1671 1.9 christos /* Look map for pid. */ 1672 1.9 christos auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ()); 1673 1.9 christos if (ptid_regc_map_it != pid_ptid_regc_map.end ()) 1674 1.9 christos { 1675 1.9 christos ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second; 1676 1.9 christos auto range = ptid_regc_map.equal_range (ptid); 1677 1.9 christos 1678 1.9 christos return std::distance (range.first, range.second); 1679 1.9 christos } 1680 1.9 christos } 1681 1.9 christos 1682 1.9 christos return 0; 1683 1.9 christos }; 1684 1.9 christos 1685 1.11 christos /* Wrapper around get_thread_arch_regcache that does some self checks. */ 1686 1.9 christos 1687 1.9 christos static void 1688 1.11 christos get_thread_arch_regcache_and_check (inferior *inf_for_target_calls, 1689 1.11 christos ptid_t ptid) 1690 1.9 christos { 1691 1.9 christos /* We currently only test with a single gdbarch. Any gdbarch will do, so use 1692 1.9 christos the current inferior's gdbarch. Also use the current inferior's address 1693 1.9 christos space. */ 1694 1.11 christos gdbarch *arch = inf_for_target_calls->arch (); 1695 1.9 christos regcache *regcache 1696 1.11 christos = get_thread_arch_regcache (inf_for_target_calls, ptid, arch); 1697 1.9 christos 1698 1.9 christos SELF_CHECK (regcache != NULL); 1699 1.9 christos SELF_CHECK (regcache->ptid () == ptid); 1700 1.9 christos SELF_CHECK (regcache->arch () == arch); 1701 1.9 christos } 1702 1.9 christos 1703 1.9 christos /* The data that the regcaches selftests must hold onto for the duration of the 1704 1.9 christos test. */ 1705 1.8 christos 1706 1.9 christos struct regcache_test_data 1707 1.9 christos { 1708 1.9 christos regcache_test_data () 1709 1.11 christos /* The specific arch doesn't matter. */ 1710 1.11 christos : test_ctx_1 (current_inferior ()->arch ()), 1711 1.11 christos test_ctx_2 (current_inferior ()->arch ()) 1712 1.9 christos { 1713 1.9 christos /* Ensure the regcaches container is empty at the start. */ 1714 1.9 christos registers_changed (); 1715 1.9 christos } 1716 1.8 christos 1717 1.9 christos ~regcache_test_data () 1718 1.8 christos { 1719 1.9 christos /* Make sure to leave the global regcaches container empty. */ 1720 1.9 christos registers_changed (); 1721 1.8 christos } 1722 1.9 christos 1723 1.11 christos scoped_mock_context<test_target_ops> test_ctx_1; 1724 1.11 christos scoped_mock_context<test_target_ops> test_ctx_2; 1725 1.8 christos }; 1726 1.8 christos 1727 1.9 christos using regcache_test_data_up = std::unique_ptr<regcache_test_data>; 1728 1.9 christos 1729 1.9 christos /* Set up a few regcaches from two different targets, for use in 1730 1.9 christos regcache-management tests. 1731 1.9 christos 1732 1.9 christos Return a pointer, because the `regcache_test_data` type is not moveable. */ 1733 1.9 christos 1734 1.9 christos static regcache_test_data_up 1735 1.9 christos populate_regcaches_for_test () 1736 1.9 christos { 1737 1.9 christos regcache_test_data_up data (new regcache_test_data); 1738 1.9 christos size_t expected_regcache_size = 0; 1739 1.9 christos 1740 1.9 christos SELF_CHECK (regcaches_size () == 0); 1741 1.9 christos 1742 1.9 christos /* Populate the regcache container with a few regcaches for the two test 1743 1.9 christos targets. */ 1744 1.9 christos for (int pid : { 1, 2 }) 1745 1.9 christos { 1746 1.9 christos for (long lwp : { 1, 2, 3 }) 1747 1.9 christos { 1748 1.11 christos get_thread_arch_regcache_and_check 1749 1.11 christos (&data->test_ctx_1.mock_inferior, ptid_t (pid, lwp)); 1750 1.9 christos expected_regcache_size++; 1751 1.9 christos SELF_CHECK (regcaches_size () == expected_regcache_size); 1752 1.9 christos 1753 1.11 christos get_thread_arch_regcache_and_check 1754 1.11 christos (&data->test_ctx_2.mock_inferior, ptid_t (pid, lwp)); 1755 1.9 christos expected_regcache_size++; 1756 1.9 christos SELF_CHECK (regcaches_size () == expected_regcache_size); 1757 1.9 christos } 1758 1.9 christos } 1759 1.9 christos 1760 1.9 christos return data; 1761 1.9 christos } 1762 1.9 christos 1763 1.1 christos static void 1764 1.11 christos get_thread_arch_regcache_test () 1765 1.1 christos { 1766 1.9 christos /* populate_regcaches_for_test already tests most of the 1767 1.11 christos get_thread_arch_regcache functionality. */ 1768 1.9 christos regcache_test_data_up data = populate_regcaches_for_test (); 1769 1.9 christos size_t regcaches_size_before = regcaches_size (); 1770 1.8 christos 1771 1.9 christos /* Test that getting an existing regcache doesn't create a new one. */ 1772 1.11 christos get_thread_arch_regcache_and_check (&data->test_ctx_1.mock_inferior, 1773 1.11 christos ptid_t (2, 2)); 1774 1.9 christos SELF_CHECK (regcaches_size () == regcaches_size_before); 1775 1.9 christos } 1776 1.8 christos 1777 1.9 christos /* Test marking all regcaches of all targets as changed. */ 1778 1.8 christos 1779 1.9 christos static void 1780 1.9 christos registers_changed_ptid_all_test () 1781 1.9 christos { 1782 1.9 christos regcache_test_data_up data = populate_regcaches_for_test (); 1783 1.9 christos 1784 1.9 christos registers_changed_ptid (nullptr, minus_one_ptid); 1785 1.9 christos SELF_CHECK (regcaches_size () == 0); 1786 1.9 christos } 1787 1.9 christos 1788 1.9 christos /* Test marking regcaches of a specific target as changed. */ 1789 1.9 christos 1790 1.9 christos static void 1791 1.9 christos registers_changed_ptid_target_test () 1792 1.9 christos { 1793 1.9 christos regcache_test_data_up data = populate_regcaches_for_test (); 1794 1.9 christos 1795 1.11 christos registers_changed_ptid (&data->test_ctx_1.mock_target, minus_one_ptid); 1796 1.9 christos SELF_CHECK (regcaches_size () == 6); 1797 1.9 christos 1798 1.9 christos /* Check that we deleted the regcache for the right target. */ 1799 1.11 christos SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target, 1800 1.11 christos ptid_t (2, 2)) == 0); 1801 1.11 christos SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target, 1802 1.11 christos ptid_t (2, 2)) == 1); 1803 1.9 christos } 1804 1.9 christos 1805 1.9 christos /* Test marking regcaches of a specific (target, pid) as changed. */ 1806 1.9 christos 1807 1.9 christos static void 1808 1.9 christos registers_changed_ptid_target_pid_test () 1809 1.9 christos { 1810 1.9 christos regcache_test_data_up data = populate_regcaches_for_test (); 1811 1.9 christos 1812 1.11 christos registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2)); 1813 1.9 christos SELF_CHECK (regcaches_size () == 9); 1814 1.9 christos 1815 1.9 christos /* Regcaches from target1 should not exist, while regcaches from target2 1816 1.9 christos should exist. */ 1817 1.11 christos SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target, 1818 1.11 christos ptid_t (2, 2)) == 0); 1819 1.11 christos SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target, 1820 1.11 christos ptid_t (2, 2)) == 1); 1821 1.9 christos } 1822 1.8 christos 1823 1.9 christos /* Test marking regcaches of a specific (target, ptid) as changed. */ 1824 1.8 christos 1825 1.9 christos static void 1826 1.9 christos registers_changed_ptid_target_ptid_test () 1827 1.9 christos { 1828 1.9 christos regcache_test_data_up data = populate_regcaches_for_test (); 1829 1.8 christos 1830 1.11 christos registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2, 2)); 1831 1.9 christos SELF_CHECK (regcaches_size () == 11); 1832 1.8 christos 1833 1.9 christos /* Check that we deleted the regcache for the right target. */ 1834 1.11 christos SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target, 1835 1.11 christos ptid_t (2, 2)) == 0); 1836 1.11 christos SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target, 1837 1.11 christos ptid_t (2, 2)) == 1); 1838 1.11 christos } 1839 1.11 christos 1840 1.11 christos /* Test using reg_buffer::raw_compare with offset equal to the register size 1841 1.11 christos (thus comparing 0 bytes). */ 1842 1.11 christos 1843 1.11 christos static void 1844 1.11 christos reg_buffer_raw_compare_zero_len_test () 1845 1.11 christos { 1846 1.11 christos regcache_test_data_up data = populate_regcaches_for_test (); 1847 1.11 christos inferior &inf = data->test_ctx_1.mock_inferior; 1848 1.11 christos const regcache *regcache 1849 1.11 christos = get_thread_arch_regcache (&inf, ptid_t (1, 1), inf.arch ()); 1850 1.11 christos 1851 1.11 christos /* The buffer address is irrelevant since we end up comparing 0 bytes, we just 1852 1.11 christos need to pass something. */ 1853 1.11 christos gdb_byte buf; 1854 1.11 christos SELF_CHECK (regcache->raw_compare (0, &buf, register_size (inf.arch (), 0))); 1855 1.8 christos } 1856 1.8 christos 1857 1.8 christos class target_ops_no_register : public test_target_ops 1858 1.8 christos { 1859 1.8 christos public: 1860 1.8 christos target_ops_no_register () 1861 1.8 christos : test_target_ops {} 1862 1.8 christos {} 1863 1.8 christos 1864 1.8 christos void reset () 1865 1.8 christos { 1866 1.8 christos fetch_registers_called = 0; 1867 1.8 christos store_registers_called = 0; 1868 1.8 christos xfer_partial_called = 0; 1869 1.8 christos } 1870 1.8 christos 1871 1.8 christos void fetch_registers (regcache *regs, int regno) override; 1872 1.8 christos void store_registers (regcache *regs, int regno) override; 1873 1.8 christos 1874 1.8 christos enum target_xfer_status xfer_partial (enum target_object object, 1875 1.8 christos const char *annex, gdb_byte *readbuf, 1876 1.8 christos const gdb_byte *writebuf, 1877 1.8 christos ULONGEST offset, ULONGEST len, 1878 1.8 christos ULONGEST *xfered_len) override; 1879 1.8 christos 1880 1.8 christos unsigned int fetch_registers_called = 0; 1881 1.8 christos unsigned int store_registers_called = 0; 1882 1.8 christos unsigned int xfer_partial_called = 0; 1883 1.8 christos }; 1884 1.1 christos 1885 1.8 christos void 1886 1.8 christos target_ops_no_register::fetch_registers (regcache *regs, int regno) 1887 1.8 christos { 1888 1.8 christos /* Mark register available. */ 1889 1.8 christos regs->raw_supply_zeroed (regno); 1890 1.8 christos this->fetch_registers_called++; 1891 1.1 christos } 1892 1.1 christos 1893 1.8 christos void 1894 1.8 christos target_ops_no_register::store_registers (regcache *regs, int regno) 1895 1.1 christos { 1896 1.8 christos this->store_registers_called++; 1897 1.1 christos } 1898 1.1 christos 1899 1.8 christos enum target_xfer_status 1900 1.8 christos target_ops_no_register::xfer_partial (enum target_object object, 1901 1.8 christos const char *annex, gdb_byte *readbuf, 1902 1.8 christos const gdb_byte *writebuf, 1903 1.8 christos ULONGEST offset, ULONGEST len, 1904 1.8 christos ULONGEST *xfered_len) 1905 1.1 christos { 1906 1.8 christos this->xfer_partial_called++; 1907 1.8 christos 1908 1.8 christos *xfered_len = len; 1909 1.8 christos return TARGET_XFER_OK; 1910 1.1 christos } 1911 1.1 christos 1912 1.8 christos class readwrite_regcache : public regcache 1913 1.1 christos { 1914 1.8 christos public: 1915 1.11 christos readwrite_regcache (inferior *inf_for_target_calls, 1916 1.9 christos struct gdbarch *gdbarch) 1917 1.11 christos : regcache (inf_for_target_calls, gdbarch) 1918 1.8 christos {} 1919 1.8 christos }; 1920 1.8 christos 1921 1.10 christos /* Return true if regcache::cooked_{read,write}_test should be skipped for 1922 1.10 christos GDBARCH. */ 1923 1.10 christos 1924 1.10 christos static bool 1925 1.10 christos selftest_skiparch (struct gdbarch *gdbarch) 1926 1.10 christos { 1927 1.10 christos const char *name = gdbarch_bfd_arch_info (gdbarch)->printable_name; 1928 1.10 christos 1929 1.10 christos /* Avoid warning: 1930 1.10 christos Running selftest regcache::cooked_{read,write}_test::m68hc11. 1931 1.10 christos warning: No frame soft register found in the symbol table. 1932 1.10 christos Stack backtrace will not work. 1933 1.10 christos We could instead capture the output and then filter out the warning, but 1934 1.10 christos that seems more trouble than it's worth. */ 1935 1.10 christos return (strcmp (name, "m68hc11") == 0 1936 1.10 christos || strcmp (name, "m68hc12") == 0 1937 1.10 christos || strcmp (name, "m68hc12:HCS12") == 0); 1938 1.10 christos } 1939 1.10 christos 1940 1.8 christos /* Test regcache::cooked_read gets registers from raw registers and 1941 1.8 christos memory instead of target to_{fetch,store}_registers. */ 1942 1.1 christos 1943 1.1 christos static void 1944 1.8 christos cooked_read_test (struct gdbarch *gdbarch) 1945 1.1 christos { 1946 1.10 christos if (selftest_skiparch (gdbarch)) 1947 1.10 christos return; 1948 1.10 christos 1949 1.9 christos scoped_mock_context<target_ops_no_register> mockctx (gdbarch); 1950 1.8 christos 1951 1.8 christos /* Test that read one raw register from regcache_no_target will go 1952 1.8 christos to the target layer. */ 1953 1.8 christos 1954 1.8 christos /* Find a raw register which size isn't zero. */ 1955 1.8 christos int nonzero_regnum; 1956 1.8 christos for (nonzero_regnum = 0; 1957 1.8 christos nonzero_regnum < gdbarch_num_regs (gdbarch); 1958 1.8 christos nonzero_regnum++) 1959 1.8 christos { 1960 1.8 christos if (register_size (gdbarch, nonzero_regnum) != 0) 1961 1.8 christos break; 1962 1.8 christos } 1963 1.8 christos 1964 1.11 christos /* Install this regcache in the regcaches global structure, so that. */ 1965 1.11 christos pid_ptid_regcache_map &x = regcaches[&mockctx.mock_target]; 1966 1.11 christos ptid_regcache_map &y = x[mockctx.mock_ptid.pid ()]; 1967 1.11 christos regcache &readwrite 1968 1.11 christos = *y.emplace (std::make_pair (mockctx.mock_ptid, 1969 1.11 christos std::make_unique<readwrite_regcache> ( 1970 1.11 christos &mockctx.mock_inferior, gdbarch))) 1971 1.11 christos ->second; 1972 1.11 christos 1973 1.11 christos readwrite.set_ptid (mockctx.mock_ptid); 1974 1.8 christos 1975 1.11 christos gdb::byte_vector buf (register_size (gdbarch, nonzero_regnum)); 1976 1.11 christos readwrite.raw_read (nonzero_regnum, buf); 1977 1.8 christos 1978 1.8 christos /* raw_read calls target_fetch_registers. */ 1979 1.9 christos SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0); 1980 1.9 christos mockctx.mock_target.reset (); 1981 1.8 christos 1982 1.8 christos /* Mark all raw registers valid, so the following raw registers 1983 1.8 christos accesses won't go to target. */ 1984 1.8 christos for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++) 1985 1.8 christos readwrite.raw_update (i); 1986 1.8 christos 1987 1.9 christos mockctx.mock_target.reset (); 1988 1.8 christos /* Then, read all raw and pseudo registers, and don't expect calling 1989 1.8 christos to_{fetch,store}_registers. */ 1990 1.8 christos for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) 1991 1.8 christos { 1992 1.8 christos if (register_size (gdbarch, regnum) == 0) 1993 1.8 christos continue; 1994 1.8 christos 1995 1.11 christos gdb::byte_vector inner_buf (register_size (gdbarch, regnum)); 1996 1.8 christos 1997 1.11 christos SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, inner_buf)); 1998 1.9 christos SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0); 1999 1.9 christos SELF_CHECK (mockctx.mock_target.store_registers_called == 0); 2000 1.9 christos SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0); 2001 1.8 christos 2002 1.9 christos mockctx.mock_target.reset (); 2003 1.8 christos } 2004 1.8 christos 2005 1.8 christos readonly_detached_regcache readonly (readwrite); 2006 1.8 christos 2007 1.8 christos /* GDB may go to target layer to fetch all registers and memory for 2008 1.8 christos readonly regcache. */ 2009 1.9 christos mockctx.mock_target.reset (); 2010 1.8 christos 2011 1.8 christos for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++) 2012 1.8 christos { 2013 1.8 christos if (register_size (gdbarch, regnum) == 0) 2014 1.8 christos continue; 2015 1.8 christos 2016 1.11 christos gdb::byte_vector inner_buf (register_size (gdbarch, regnum)); 2017 1.11 christos register_status status = readonly.cooked_read (regnum, inner_buf); 2018 1.8 christos 2019 1.8 christos if (regnum < gdbarch_num_regs (gdbarch)) 2020 1.8 christos { 2021 1.8 christos auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; 2022 1.8 christos 2023 1.11 christos if (bfd_arch == bfd_arch_amdgcn 2024 1.11 christos || bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300 2025 1.8 christos || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh 2026 1.8 christos || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850 2027 1.8 christos || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep 2028 1.8 christos || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850 2029 1.8 christos || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300 2030 1.8 christos || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score 2031 1.8 christos || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky) 2032 1.8 christos { 2033 1.8 christos /* Raw registers. If raw registers are not in save_reggroup, 2034 1.8 christos their status are unknown. */ 2035 1.8 christos if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) 2036 1.8 christos SELF_CHECK (status == REG_VALID); 2037 1.8 christos else 2038 1.8 christos SELF_CHECK (status == REG_UNKNOWN); 2039 1.8 christos } 2040 1.8 christos else 2041 1.8 christos SELF_CHECK (status == REG_VALID); 2042 1.8 christos } 2043 1.8 christos else 2044 1.8 christos { 2045 1.8 christos if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup)) 2046 1.8 christos SELF_CHECK (status == REG_VALID); 2047 1.8 christos else 2048 1.8 christos { 2049 1.8 christos /* If pseudo registers are not in save_reggroup, some of 2050 1.8 christos them can be computed from saved raw registers, but some 2051 1.8 christos of them are unknown. */ 2052 1.8 christos auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; 2053 1.8 christos 2054 1.8 christos if (bfd_arch == bfd_arch_frv 2055 1.8 christos || bfd_arch == bfd_arch_m32c 2056 1.8 christos || bfd_arch == bfd_arch_mep 2057 1.8 christos || bfd_arch == bfd_arch_sh) 2058 1.8 christos SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN); 2059 1.8 christos else if (bfd_arch == bfd_arch_mips 2060 1.8 christos || bfd_arch == bfd_arch_h8300) 2061 1.8 christos SELF_CHECK (status == REG_UNKNOWN); 2062 1.8 christos else 2063 1.8 christos SELF_CHECK (status == REG_VALID); 2064 1.8 christos } 2065 1.8 christos } 2066 1.8 christos 2067 1.9 christos SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0); 2068 1.9 christos SELF_CHECK (mockctx.mock_target.store_registers_called == 0); 2069 1.9 christos SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0); 2070 1.8 christos 2071 1.9 christos mockctx.mock_target.reset (); 2072 1.8 christos } 2073 1.11 christos 2074 1.11 christos regcaches.erase (&mockctx.mock_target); 2075 1.1 christos } 2076 1.1 christos 2077 1.8 christos /* Test regcache::cooked_write by writing some expected contents to 2078 1.8 christos registers, and checking that contents read from registers and the 2079 1.8 christos expected contents are the same. */ 2080 1.8 christos 2081 1.1 christos static void 2082 1.8 christos cooked_write_test (struct gdbarch *gdbarch) 2083 1.1 christos { 2084 1.10 christos if (selftest_skiparch (gdbarch)) 2085 1.10 christos return; 2086 1.8 christos 2087 1.8 christos /* Create a mock environment. A process_stratum target pushed. */ 2088 1.10 christos scoped_mock_context<target_ops_no_register> ctx (gdbarch); 2089 1.11 christos 2090 1.11 christos 2091 1.11 christos /* Install this regcache in the regcaches global structure, so that. */ 2092 1.11 christos pid_ptid_regcache_map &x = regcaches[&ctx.mock_target]; 2093 1.11 christos ptid_regcache_map &y = x[ctx.mock_ptid.pid ()]; 2094 1.11 christos regcache &readwrite 2095 1.11 christos = *y.emplace (std::make_pair (ctx.mock_ptid, 2096 1.11 christos std::make_unique<readwrite_regcache> ( 2097 1.11 christos &ctx.mock_inferior, gdbarch))) 2098 1.11 christos ->second; 2099 1.11 christos 2100 1.11 christos readwrite.set_ptid (ctx.mock_ptid); 2101 1.8 christos const int num_regs = gdbarch_num_cooked_regs (gdbarch); 2102 1.8 christos 2103 1.8 christos for (auto regnum = 0; regnum < num_regs; regnum++) 2104 1.8 christos { 2105 1.8 christos if (register_size (gdbarch, regnum) == 0 2106 1.8 christos || gdbarch_cannot_store_register (gdbarch, regnum)) 2107 1.8 christos continue; 2108 1.8 christos 2109 1.8 christos auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch; 2110 1.8 christos 2111 1.9 christos if (bfd_arch == bfd_arch_sparc 2112 1.9 christos /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM, 2113 1.9 christos SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */ 2114 1.9 christos && gdbarch_ptr_bit (gdbarch) == 64 2115 1.9 christos && (regnum >= gdbarch_num_regs (gdbarch) 2116 1.9 christos && regnum <= gdbarch_num_regs (gdbarch) + 4)) 2117 1.8 christos continue; 2118 1.8 christos 2119 1.11 christos gdb::byte_vector expected (register_size (gdbarch, regnum), 0); 2120 1.11 christos gdb::byte_vector buf (register_size (gdbarch, regnum), 0); 2121 1.8 christos const auto type = register_type (gdbarch, regnum); 2122 1.8 christos 2123 1.9 christos if (type->code () == TYPE_CODE_FLT 2124 1.9 christos || type->code () == TYPE_CODE_DECFLOAT) 2125 1.8 christos { 2126 1.8 christos /* Generate valid float format. */ 2127 1.8 christos target_float_from_string (expected.data (), type, "1.25"); 2128 1.8 christos } 2129 1.9 christos else if (type->code () == TYPE_CODE_INT 2130 1.9 christos || type->code () == TYPE_CODE_ARRAY 2131 1.9 christos || type->code () == TYPE_CODE_PTR 2132 1.9 christos || type->code () == TYPE_CODE_UNION 2133 1.9 christos || type->code () == TYPE_CODE_STRUCT) 2134 1.8 christos { 2135 1.8 christos if (bfd_arch == bfd_arch_ia64 2136 1.8 christos || (regnum >= gdbarch_num_regs (gdbarch) 2137 1.8 christos && (bfd_arch == bfd_arch_xtensa 2138 1.8 christos || bfd_arch == bfd_arch_bfin 2139 1.8 christos || bfd_arch == bfd_arch_m32c 2140 1.8 christos /* m68hc11 pseudo registers are in memory. */ 2141 1.8 christos || bfd_arch == bfd_arch_m68hc11 2142 1.8 christos || bfd_arch == bfd_arch_m68hc12 2143 1.8 christos || bfd_arch == bfd_arch_s390)) 2144 1.8 christos || (bfd_arch == bfd_arch_frv 2145 1.8 christos /* FRV pseudo registers except iacc0. */ 2146 1.8 christos && regnum > gdbarch_num_regs (gdbarch))) 2147 1.8 christos { 2148 1.8 christos /* Skip setting the expected values for some architecture 2149 1.8 christos registers. */ 2150 1.8 christos } 2151 1.8 christos else if (bfd_arch == bfd_arch_rl78 && regnum == 40) 2152 1.8 christos { 2153 1.8 christos /* RL78_PC_REGNUM */ 2154 1.8 christos for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++) 2155 1.8 christos expected[j] = j; 2156 1.8 christos } 2157 1.8 christos else 2158 1.8 christos { 2159 1.8 christos for (auto j = 0; j < register_size (gdbarch, regnum); j++) 2160 1.8 christos expected[j] = j; 2161 1.8 christos } 2162 1.8 christos } 2163 1.9 christos else if (type->code () == TYPE_CODE_FLAGS) 2164 1.8 christos { 2165 1.8 christos /* No idea how to test flags. */ 2166 1.8 christos continue; 2167 1.8 christos } 2168 1.8 christos else 2169 1.8 christos { 2170 1.8 christos /* If we don't know how to create the expected value for the 2171 1.8 christos this type, make it fail. */ 2172 1.8 christos SELF_CHECK (0); 2173 1.8 christos } 2174 1.8 christos 2175 1.11 christos readwrite.cooked_write (regnum, expected); 2176 1.8 christos 2177 1.11 christos SELF_CHECK (readwrite.cooked_read (regnum, buf) == REG_VALID); 2178 1.8 christos SELF_CHECK (expected == buf); 2179 1.8 christos } 2180 1.11 christos 2181 1.11 christos regcaches.erase (&ctx.mock_target); 2182 1.1 christos } 2183 1.1 christos 2184 1.9 christos /* Verify that when two threads with the same ptid exist (from two different 2185 1.9 christos targets) and one of them changes ptid, we only update the appropriate 2186 1.9 christos regcaches. */ 2187 1.9 christos 2188 1.9 christos static void 2189 1.9 christos regcache_thread_ptid_changed () 2190 1.9 christos { 2191 1.9 christos /* This test relies on the global regcache list to initially be empty. */ 2192 1.9 christos registers_changed (); 2193 1.9 christos 2194 1.9 christos /* Any arch will do. */ 2195 1.11 christos gdbarch *arch = current_inferior ()->arch (); 2196 1.9 christos 2197 1.9 christos /* Prepare two targets with one thread each, with the same ptid. */ 2198 1.9 christos scoped_mock_context<test_target_ops> target1 (arch); 2199 1.9 christos scoped_mock_context<test_target_ops> target2 (arch); 2200 1.9 christos 2201 1.9 christos ptid_t old_ptid (111, 222); 2202 1.9 christos ptid_t new_ptid (111, 333); 2203 1.9 christos 2204 1.9 christos target1.mock_inferior.pid = old_ptid.pid (); 2205 1.9 christos target1.mock_thread.ptid = old_ptid; 2206 1.10 christos target1.mock_inferior.ptid_thread_map.clear (); 2207 1.10 christos target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread; 2208 1.10 christos 2209 1.9 christos target2.mock_inferior.pid = old_ptid.pid (); 2210 1.9 christos target2.mock_thread.ptid = old_ptid; 2211 1.10 christos target2.mock_inferior.ptid_thread_map.clear (); 2212 1.10 christos target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread; 2213 1.9 christos 2214 1.9 christos gdb_assert (regcaches.empty ()); 2215 1.9 christos 2216 1.9 christos /* Populate the regcaches container. */ 2217 1.11 christos get_thread_arch_regcache (&target1.mock_inferior, old_ptid, arch); 2218 1.11 christos get_thread_arch_regcache (&target2.mock_inferior, old_ptid, arch); 2219 1.9 christos 2220 1.9 christos gdb_assert (regcaches.size () == 2); 2221 1.9 christos gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1); 2222 1.9 christos gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0); 2223 1.9 christos gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1); 2224 1.9 christos gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0); 2225 1.9 christos 2226 1.9 christos thread_change_ptid (&target1.mock_target, old_ptid, new_ptid); 2227 1.9 christos 2228 1.9 christos gdb_assert (regcaches.size () == 2); 2229 1.9 christos gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0); 2230 1.9 christos gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1); 2231 1.9 christos gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1); 2232 1.9 christos gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0); 2233 1.9 christos 2234 1.9 christos /* Leave the regcache list empty. */ 2235 1.9 christos registers_changed (); 2236 1.9 christos gdb_assert (regcaches.empty ()); 2237 1.9 christos } 2238 1.9 christos 2239 1.8 christos } // namespace selftests 2240 1.8 christos #endif /* GDB_SELF_TEST */ 2241 1.1 christos 2242 1.9 christos void _initialize_regcache (); 2243 1.1 christos void 2244 1.9 christos _initialize_regcache () 2245 1.1 christos { 2246 1.10 christos struct cmd_list_element *c; 2247 1.1 christos 2248 1.10 christos gdb::observers::target_changed.attach (regcache_observer_target_changed, 2249 1.10 christos "regcache"); 2250 1.10 christos gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed, 2251 1.10 christos "regcache"); 2252 1.10 christos 2253 1.10 christos cmd_list_element *maintenance_flush_register_cache_cmd 2254 1.10 christos = add_cmd ("register-cache", class_maintenance, reg_flush_command, 2255 1.10 christos _("Force gdb to flush its register and frame cache."), 2256 1.10 christos &maintenanceflushlist); 2257 1.10 christos c = add_com_alias ("flushregs", maintenance_flush_register_cache_cmd, 2258 1.10 christos class_maintenance, 0); 2259 1.10 christos deprecate_cmd (c, "maintenance flush register-cache"); 2260 1.1 christos 2261 1.8 christos #if GDB_SELF_TEST 2262 1.11 christos selftests::register_test ("get_thread_arch_regcache", 2263 1.11 christos selftests::get_thread_arch_regcache_test); 2264 1.9 christos selftests::register_test ("registers_changed_ptid_all", 2265 1.9 christos selftests::registers_changed_ptid_all_test); 2266 1.9 christos selftests::register_test ("registers_changed_ptid_target", 2267 1.10 christos selftests::registers_changed_ptid_target_test); 2268 1.9 christos selftests::register_test ("registers_changed_ptid_target_pid", 2269 1.10 christos selftests::registers_changed_ptid_target_pid_test); 2270 1.9 christos selftests::register_test ("registers_changed_ptid_target_ptid", 2271 1.9 christos selftests::registers_changed_ptid_target_ptid_test); 2272 1.11 christos selftests::register_test ("reg_buffer_raw_compare_zero_len", 2273 1.11 christos selftests::reg_buffer_raw_compare_zero_len_test); 2274 1.1 christos 2275 1.8 christos selftests::register_test_foreach_arch ("regcache::cooked_read_test", 2276 1.8 christos selftests::cooked_read_test); 2277 1.8 christos selftests::register_test_foreach_arch ("regcache::cooked_write_test", 2278 1.8 christos selftests::cooked_write_test); 2279 1.9 christos selftests::register_test ("regcache_thread_ptid_changed", 2280 1.9 christos selftests::regcache_thread_ptid_changed); 2281 1.8 christos #endif 2282 1.1 christos } 2283