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