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