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