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