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