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