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