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