Replace regcache::dump with class register_dump
[deliverable/binutils-gdb.git] / gdb / regcache.c
CommitLineData
32178cab 1/* Cache and manage the values of registers for GDB, the GNU debugger.
3fadccb3 2
e2882c85 3 Copyright (C) 1986-2018 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
MS
21#include "inferior.h"
22#include "target.h"
23#include "gdbarch.h"
705152c5 24#include "gdbcmd.h"
4e052eda 25#include "regcache.h"
b59ff9d5 26#include "reggroups.h"
f4c5303c 27#include "observer.h"
c21236dc 28#include "remote.h"
d3eaaf66 29#include "valprint.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. */
214e098a
UW
90 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
91 + gdbarch_num_pseudo_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
bb425013
AC
105 NOTE: cagney/2002-05-22: Only register_type() is used when
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];
122 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
123 }
124 /* Set the real size of the raw register cache buffer. */
125 descr->sizeof_raw_registers = offset;
126
127 for (; i < descr->nr_cooked_registers; i++)
3fadccb3 128 {
bb425013 129 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
3fadccb3
AC
130 descr->register_offset[i] = offset;
131 offset += descr->sizeof_register[i];
123a958e 132 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
3fadccb3 133 }
99e42fd8 134 /* Set the real size of the readonly register cache buffer. */
067df2e5 135 descr->sizeof_cooked_registers = offset;
3fadccb3
AC
136 }
137
3fadccb3
AC
138 return descr;
139}
140
141static struct regcache_descr *
142regcache_descr (struct gdbarch *gdbarch)
143{
19ba03f4
SM
144 return (struct regcache_descr *) gdbarch_data (gdbarch,
145 regcache_descr_handle);
3fadccb3
AC
146}
147
bb425013
AC
148/* Utility functions returning useful register attributes stored in
149 the regcache descr. */
150
151struct type *
152register_type (struct gdbarch *gdbarch, int regnum)
153{
154 struct regcache_descr *descr = regcache_descr (gdbarch);
123f5f96 155
bb425013
AC
156 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
157 return descr->register_type[regnum];
158}
159
0ed04cce
AC
160/* Utility functions returning useful register attributes stored in
161 the regcache descr. */
162
08a617da
AC
163int
164register_size (struct gdbarch *gdbarch, int regnum)
165{
166 struct regcache_descr *descr = regcache_descr (gdbarch);
167 int size;
123f5f96 168
f57d151a 169 gdb_assert (regnum >= 0
214e098a
UW
170 && regnum < (gdbarch_num_regs (gdbarch)
171 + gdbarch_num_pseudo_regs (gdbarch)));
08a617da 172 size = descr->sizeof_register[regnum];
08a617da
AC
173 return size;
174}
175
8d689ee5
YQ
176/* See common/common-regcache.h. */
177
178int
179regcache_register_size (const struct regcache *regcache, int n)
180{
ac7936df 181 return register_size (regcache->arch (), n);
8d689ee5
YQ
182}
183
31716595
YQ
184reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
185 : m_has_pseudo (has_pseudo)
3fadccb3 186{
ef79d9a3
YQ
187 gdb_assert (gdbarch != NULL);
188 m_descr = regcache_descr (gdbarch);
4621115f 189
31716595 190 if (has_pseudo)
4621115f 191 {
ef79d9a3
YQ
192 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
193 m_register_status = XCNEWVEC (signed char,
6c5218df 194 m_descr->nr_cooked_registers);
4621115f
YQ
195 }
196 else
197 {
ef79d9a3 198 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
d999647b 199 m_register_status = XCNEWVEC (signed char, gdbarch_num_regs (gdbarch));
4621115f 200 }
31716595
YQ
201}
202
203regcache::regcache (gdbarch *gdbarch, const address_space *aspace_,
204 bool readonly_p_)
205/* The register buffers. A read-only register cache can hold the
206 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a
207 read/write register cache can only hold [0 .. gdbarch_num_regs). */
c8ec2f33 208 : detached_regcache (gdbarch, readonly_p_),
31716595
YQ
209 m_aspace (aspace_), m_readonly_p (readonly_p_)
210{
ef79d9a3
YQ
211 m_ptid = minus_one_ptid;
212}
4621115f 213
deb1fa3e
YQ
214static enum register_status
215do_cooked_read (void *src, int regnum, gdb_byte *buf)
216{
217 struct regcache *regcache = (struct regcache *) src;
218
219 return regcache_cooked_read (regcache, regnum, buf);
220}
221
222regcache::regcache (readonly_t, const regcache &src)
f26ae15b 223 : regcache (src.arch (), nullptr, true)
deb1fa3e
YQ
224{
225 gdb_assert (!src.m_readonly_p);
226 save (do_cooked_read, (void *) &src);
227}
228
daf6667d
YQ
229readonly_detached_regcache::readonly_detached_regcache (const regcache &src)
230 : readonly_detached_regcache (src.arch (), do_cooked_read, (void *) &src)
231{
232}
233
ef79d9a3 234gdbarch *
31716595 235reg_buffer::arch () const
ef79d9a3
YQ
236{
237 return m_descr->gdbarch;
238}
3fadccb3 239
ddaaf0fb
SM
240/* See regcache.h. */
241
242ptid_t
243regcache_get_ptid (const struct regcache *regcache)
244{
ef79d9a3 245 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
ddaaf0fb 246
ef79d9a3 247 return regcache->ptid ();
ddaaf0fb
SM
248}
249
b292235f 250/* Cleanup class for invalidating a register. */
b94ade42 251
b292235f 252class regcache_invalidator
b94ade42 253{
b292235f 254public:
b94ade42 255
b292235f
TT
256 regcache_invalidator (struct regcache *regcache, int regnum)
257 : m_regcache (regcache),
258 m_regnum (regnum)
259 {
260 }
b94ade42 261
b292235f
TT
262 ~regcache_invalidator ()
263 {
264 if (m_regcache != nullptr)
265 regcache_invalidate (m_regcache, m_regnum);
266 }
b94ade42 267
b292235f 268 DISABLE_COPY_AND_ASSIGN (regcache_invalidator);
b94ade42 269
b292235f
TT
270 void release ()
271 {
272 m_regcache = nullptr;
273 }
274
275private:
276
277 struct regcache *m_regcache;
278 int m_regnum;
279};
b94ade42 280
51b1fe4e
AC
281/* Return a pointer to register REGNUM's buffer cache. */
282
ef79d9a3 283gdb_byte *
31716595 284reg_buffer::register_buffer (int regnum) const
51b1fe4e 285{
ef79d9a3 286 return m_registers + m_descr->register_offset[regnum];
51b1fe4e
AC
287}
288
ef79d9a3 289void
daf6667d
YQ
290reg_buffer::save (regcache_cooked_read_ftype *cooked_read,
291 void *src)
ef79d9a3
YQ
292{
293 struct gdbarch *gdbarch = m_descr->gdbarch;
2d28509a 294 int regnum;
123f5f96 295
daf6667d
YQ
296 /* It should have pseudo registers. */
297 gdb_assert (m_has_pseudo);
2d28509a 298 /* Clear the dest. */
ef79d9a3 299 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
6c5218df 300 memset (m_register_status, 0, m_descr->nr_cooked_registers);
2d28509a 301 /* Copy over any registers (identified by their membership in the
f57d151a
UW
302 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
303 gdbarch_num_pseudo_regs) range is checked since some architectures need
5602984a 304 to save/restore `cooked' registers that live in memory. */
ef79d9a3 305 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
2d28509a
AC
306 {
307 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
308 {
50d6adef
AH
309 gdb_byte *dst_buf = register_buffer (regnum);
310 enum register_status status = cooked_read (src, regnum, dst_buf);
123f5f96 311
50d6adef
AH
312 gdb_assert (status != REG_UNKNOWN);
313
314 if (status != REG_VALID)
315 memset (dst_buf, 0, register_size (gdbarch, regnum));
05d1431c 316
ef79d9a3 317 m_register_status[regnum] = status;
2d28509a
AC
318 }
319 }
320}
321
ef79d9a3 322void
daf6667d 323regcache::restore (readonly_detached_regcache *src)
2d28509a 324{
ef79d9a3 325 struct gdbarch *gdbarch = m_descr->gdbarch;
2d28509a 326 int regnum;
123f5f96 327
fc5b8736 328 gdb_assert (src != NULL);
ef79d9a3 329 gdb_assert (!m_readonly_p);
daf6667d 330 gdb_assert (src->m_has_pseudo);
fc5b8736
YQ
331
332 gdb_assert (gdbarch == src->arch ());
333
2d28509a 334 /* Copy over any registers, being careful to only restore those that
f57d151a
UW
335 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
336 + gdbarch_num_pseudo_regs) range is checked since some architectures need
5602984a 337 to save/restore `cooked' registers that live in memory. */
ef79d9a3 338 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
2d28509a 339 {
5602984a 340 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
2d28509a 341 {
ef79d9a3
YQ
342 if (src->m_register_status[regnum] == REG_VALID)
343 cooked_write (regnum, src->register_buffer (regnum));
2d28509a
AC
344 }
345 }
346}
347
39181896 348enum register_status
ee99023e 349regcache_register_status (const struct regcache *regcache, int regnum)
3fadccb3
AC
350{
351 gdb_assert (regcache != NULL);
ef79d9a3
YQ
352 return regcache->get_register_status (regnum);
353}
354
355enum register_status
c8ec2f33 356reg_buffer::get_register_status (int regnum) const
ef79d9a3 357{
c8ec2f33 358 assert_regnum (regnum);
6ed7ea50 359
ef79d9a3 360 return (enum register_status) m_register_status[regnum];
3fadccb3
AC
361}
362
9c5ea4d9
UW
363void
364regcache_invalidate (struct regcache *regcache, int regnum)
365{
366 gdb_assert (regcache != NULL);
ef79d9a3 367 regcache->invalidate (regnum);
9c5ea4d9
UW
368}
369
ef79d9a3
YQ
370void
371regcache::invalidate (int regnum)
372{
ef79d9a3 373 gdb_assert (!m_readonly_p);
4e888c28 374 assert_regnum (regnum);
ef79d9a3
YQ
375 m_register_status[regnum] = REG_UNKNOWN;
376}
9c5ea4d9 377
4e888c28 378void
31716595 379reg_buffer::assert_regnum (int regnum) const
4e888c28 380{
31716595
YQ
381 gdb_assert (regnum >= 0);
382 if (m_has_pseudo)
383 gdb_assert (regnum < m_descr->nr_cooked_registers);
384 else
385 gdb_assert (regnum < gdbarch_num_regs (arch ()));
4e888c28
YQ
386}
387
3fadccb3 388/* Global structure containing the current regcache. */
3fadccb3 389
5ebd2499 390/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
391 recording if the register values have been changed (eg. by the
392 user). Therefore all registers must be written back to the
393 target when appropriate. */
e521e87e 394std::forward_list<regcache *> regcache::current_regcache;
c2250ad1
UW
395
396struct regcache *
e2d96639
YQ
397get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
398 struct address_space *aspace)
c2250ad1 399{
e521e87e 400 for (const auto &regcache : regcache::current_regcache)
94bb8dfe
YQ
401 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
402 return regcache;
594f7785 403
94bb8dfe 404 regcache *new_regcache = new regcache (gdbarch, aspace, false);
594f7785 405
e521e87e 406 regcache::current_regcache.push_front (new_regcache);
ef79d9a3 407 new_regcache->set_ptid (ptid);
e2d96639 408
e2d96639
YQ
409 return new_regcache;
410}
411
412struct regcache *
413get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
414{
ed4227b7 415 address_space *aspace = target_thread_address_space (ptid);
b78974c3 416
e2d96639 417 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
594f7785
UW
418}
419
c2250ad1
UW
420static ptid_t current_thread_ptid;
421static struct gdbarch *current_thread_arch;
422
423struct regcache *
424get_thread_regcache (ptid_t ptid)
425{
426 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
427 {
428 current_thread_ptid = ptid;
429 current_thread_arch = target_thread_architecture (ptid);
430 }
431
432 return get_thread_arch_regcache (ptid, current_thread_arch);
433}
434
435struct regcache *
436get_current_regcache (void)
594f7785
UW
437{
438 return get_thread_regcache (inferior_ptid);
439}
32178cab 440
361c8ade
GB
441/* See common/common-regcache.h. */
442
443struct regcache *
444get_thread_regcache_for_ptid (ptid_t ptid)
445{
446 return get_thread_regcache (ptid);
447}
32178cab 448
f4c5303c
OF
449/* Observer for the target_changed event. */
450
2c0b251b 451static void
f4c5303c
OF
452regcache_observer_target_changed (struct target_ops *target)
453{
454 registers_changed ();
455}
456
5231c1fd
PA
457/* Update global variables old ptids to hold NEW_PTID if they were
458 holding OLD_PTID. */
e521e87e
YQ
459void
460regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
5231c1fd 461{
e521e87e 462 for (auto &regcache : regcache::current_regcache)
94bb8dfe
YQ
463 {
464 if (ptid_equal (regcache->ptid (), old_ptid))
465 regcache->set_ptid (new_ptid);
466 }
5231c1fd
PA
467}
468
32178cab
MS
469/* Low level examining and depositing of registers.
470
471 The caller is responsible for making sure that the inferior is
472 stopped before calling the fetching routines, or it will get
473 garbage. (a change from GDB version 3, in which the caller got the
474 value from the last stop). */
475
476/* REGISTERS_CHANGED ()
477
478 Indicate that registers may have changed, so invalidate the cache. */
479
480void
e66408ed 481registers_changed_ptid (ptid_t ptid)
32178cab 482{
e521e87e 483 for (auto oit = regcache::current_regcache.before_begin (),
94bb8dfe 484 it = std::next (oit);
e521e87e 485 it != regcache::current_regcache.end ();
94bb8dfe 486 )
c2250ad1 487 {
94bb8dfe 488 if (ptid_match ((*it)->ptid (), ptid))
e66408ed 489 {
94bb8dfe 490 delete *it;
e521e87e 491 it = regcache::current_regcache.erase_after (oit);
e66408ed 492 }
94bb8dfe
YQ
493 else
494 oit = it++;
c2250ad1 495 }
32178cab 496
c34fd852 497 if (ptid_match (current_thread_ptid, ptid))
041274d8
PA
498 {
499 current_thread_ptid = null_ptid;
500 current_thread_arch = NULL;
501 }
32178cab 502
c34fd852 503 if (ptid_match (inferior_ptid, ptid))
041274d8
PA
504 {
505 /* We just deleted the regcache of the current thread. Need to
506 forget about any frames we have cached, too. */
507 reinit_frame_cache ();
508 }
509}
c2250ad1 510
041274d8
PA
511void
512registers_changed (void)
513{
514 registers_changed_ptid (minus_one_ptid);
a5d9d57d 515
32178cab
MS
516 /* Force cleanup of any alloca areas if using C alloca instead of
517 a builtin alloca. This particular call is used to clean up
518 areas allocated by low level target code which may build up
519 during lengthy interactions between gdb and the target before
520 gdb gives control to the user (ie watchpoints). */
521 alloca (0);
32178cab
MS
522}
523
8e368124
AH
524void
525regcache_raw_update (struct regcache *regcache, int regnum)
61a0eb5b 526{
8e368124 527 gdb_assert (regcache != NULL);
ef79d9a3
YQ
528
529 regcache->raw_update (regnum);
530}
531
532void
533regcache::raw_update (int regnum)
534{
4e888c28 535 assert_regnum (regnum);
8e368124 536
3fadccb3
AC
537 /* Make certain that the register cache is up-to-date with respect
538 to the current thread. This switching shouldn't be necessary
539 only there is still only one target side register cache. Sigh!
540 On the bright side, at least there is a regcache object. */
8e368124 541
ef79d9a3 542 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
3fadccb3 543 {
ef79d9a3 544 target_fetch_registers (this, regnum);
788c8b10
PA
545
546 /* A number of targets can't access the whole set of raw
547 registers (because the debug API provides no means to get at
548 them). */
ef79d9a3
YQ
549 if (m_register_status[regnum] == REG_UNKNOWN)
550 m_register_status[regnum] = REG_UNAVAILABLE;
3fadccb3 551 }
8e368124
AH
552}
553
554enum register_status
555regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
ef79d9a3
YQ
556{
557 return regcache->raw_read (regnum, buf);
558}
559
560enum register_status
849d0ba8 561readable_regcache::raw_read (int regnum, gdb_byte *buf)
8e368124
AH
562{
563 gdb_assert (buf != NULL);
ef79d9a3 564 raw_update (regnum);
05d1431c 565
ef79d9a3
YQ
566 if (m_register_status[regnum] != REG_VALID)
567 memset (buf, 0, m_descr->sizeof_register[regnum]);
05d1431c 568 else
ef79d9a3
YQ
569 memcpy (buf, register_buffer (regnum),
570 m_descr->sizeof_register[regnum]);
05d1431c 571
ef79d9a3 572 return (enum register_status) m_register_status[regnum];
61a0eb5b
AC
573}
574
05d1431c 575enum register_status
28fc6740 576regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
ef79d9a3
YQ
577{
578 gdb_assert (regcache != NULL);
6f98355c 579 return regcache->raw_read (regnum, val);
ef79d9a3
YQ
580}
581
6f98355c 582template<typename T, typename>
ef79d9a3 583enum register_status
849d0ba8 584readable_regcache::raw_read (int regnum, T *val)
28fc6740 585{
2d522557 586 gdb_byte *buf;
05d1431c 587 enum register_status status;
123f5f96 588
4e888c28 589 assert_regnum (regnum);
ef79d9a3
YQ
590 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
591 status = raw_read (regnum, buf);
05d1431c 592 if (status == REG_VALID)
6f98355c
YQ
593 *val = extract_integer<T> (buf,
594 m_descr->sizeof_register[regnum],
595 gdbarch_byte_order (m_descr->gdbarch));
05d1431c
PA
596 else
597 *val = 0;
598 return status;
28fc6740
AC
599}
600
05d1431c 601enum register_status
28fc6740
AC
602regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
603 ULONGEST *val)
ef79d9a3
YQ
604{
605 gdb_assert (regcache != NULL);
6f98355c 606 return regcache->raw_read (regnum, val);
28fc6740
AC
607}
608
c00dcbe9
MK
609void
610regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
ef79d9a3
YQ
611{
612 gdb_assert (regcache != NULL);
6f98355c 613 regcache->raw_write (regnum, val);
ef79d9a3
YQ
614}
615
6f98355c 616template<typename T, typename>
ef79d9a3 617void
6f98355c 618regcache::raw_write (int regnum, T val)
c00dcbe9 619{
7c543f7b 620 gdb_byte *buf;
123f5f96 621
4e888c28 622 assert_regnum (regnum);
ef79d9a3 623 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
6f98355c
YQ
624 store_integer (buf, m_descr->sizeof_register[regnum],
625 gdbarch_byte_order (m_descr->gdbarch), val);
ef79d9a3 626 raw_write (regnum, buf);
c00dcbe9
MK
627}
628
629void
630regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
631 ULONGEST val)
ef79d9a3
YQ
632{
633 gdb_assert (regcache != NULL);
6f98355c 634 regcache->raw_write (regnum, val);
c00dcbe9
MK
635}
636
9fd15b2e
YQ
637LONGEST
638regcache_raw_get_signed (struct regcache *regcache, int regnum)
639{
640 LONGEST value;
641 enum register_status status;
642
643 status = regcache_raw_read_signed (regcache, regnum, &value);
644 if (status == REG_UNAVAILABLE)
645 throw_error (NOT_AVAILABLE_ERROR,
646 _("Register %d is not available"), regnum);
647 return value;
648}
649
05d1431c 650enum register_status
2d522557 651regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
ef79d9a3
YQ
652{
653 return regcache->cooked_read (regnum, buf);
654}
655
656enum register_status
849d0ba8 657readable_regcache::cooked_read (int regnum, gdb_byte *buf)
68365089 658{
d138e37a 659 gdb_assert (regnum >= 0);
ef79d9a3 660 gdb_assert (regnum < m_descr->nr_cooked_registers);
d999647b 661 if (regnum < num_raw_registers ())
ef79d9a3 662 return raw_read (regnum, buf);
849d0ba8 663 else if (m_has_pseudo
ef79d9a3 664 && m_register_status[regnum] != REG_UNKNOWN)
05d1431c 665 {
ef79d9a3
YQ
666 if (m_register_status[regnum] == REG_VALID)
667 memcpy (buf, register_buffer (regnum),
668 m_descr->sizeof_register[regnum]);
05d1431c 669 else
ef79d9a3 670 memset (buf, 0, m_descr->sizeof_register[regnum]);
05d1431c 671
ef79d9a3 672 return (enum register_status) m_register_status[regnum];
05d1431c 673 }
ef79d9a3 674 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
3543a589
TT
675 {
676 struct value *mark, *computed;
677 enum register_status result = REG_VALID;
678
679 mark = value_mark ();
680
ef79d9a3
YQ
681 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
682 this, regnum);
3543a589
TT
683 if (value_entirely_available (computed))
684 memcpy (buf, value_contents_raw (computed),
ef79d9a3 685 m_descr->sizeof_register[regnum]);
3543a589
TT
686 else
687 {
ef79d9a3 688 memset (buf, 0, m_descr->sizeof_register[regnum]);
3543a589
TT
689 result = REG_UNAVAILABLE;
690 }
691
692 value_free_to_mark (mark);
693
694 return result;
695 }
d138e37a 696 else
ef79d9a3 697 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
05d1431c 698 regnum, buf);
61a0eb5b
AC
699}
700
3543a589
TT
701struct value *
702regcache_cooked_read_value (struct regcache *regcache, int regnum)
ef79d9a3
YQ
703{
704 return regcache->cooked_read_value (regnum);
705}
706
707struct value *
849d0ba8 708readable_regcache::cooked_read_value (int regnum)
3543a589
TT
709{
710 gdb_assert (regnum >= 0);
ef79d9a3 711 gdb_assert (regnum < m_descr->nr_cooked_registers);
3543a589 712
d999647b 713 if (regnum < num_raw_registers ()
849d0ba8 714 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
ef79d9a3 715 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
3543a589
TT
716 {
717 struct value *result;
718
ef79d9a3 719 result = allocate_value (register_type (m_descr->gdbarch, regnum));
3543a589
TT
720 VALUE_LVAL (result) = lval_register;
721 VALUE_REGNUM (result) = regnum;
722
723 /* It is more efficient in general to do this delegation in this
724 direction than in the other one, even though the value-based
725 API is preferred. */
ef79d9a3
YQ
726 if (cooked_read (regnum,
727 value_contents_raw (result)) == REG_UNAVAILABLE)
3543a589
TT
728 mark_value_bytes_unavailable (result, 0,
729 TYPE_LENGTH (value_type (result)));
730
731 return result;
732 }
733 else
ef79d9a3
YQ
734 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
735 this, regnum);
3543a589
TT
736}
737
05d1431c 738enum register_status
a378f419
AC
739regcache_cooked_read_signed (struct regcache *regcache, int regnum,
740 LONGEST *val)
ef79d9a3
YQ
741{
742 gdb_assert (regcache != NULL);
6f98355c 743 return regcache->cooked_read (regnum, val);
ef79d9a3
YQ
744}
745
6f98355c 746template<typename T, typename>
ef79d9a3 747enum register_status
849d0ba8 748readable_regcache::cooked_read (int regnum, T *val)
a378f419 749{
05d1431c 750 enum register_status status;
2d522557 751 gdb_byte *buf;
123f5f96 752
ef79d9a3
YQ
753 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
754 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
755 status = cooked_read (regnum, buf);
05d1431c 756 if (status == REG_VALID)
6f98355c
YQ
757 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
758 gdbarch_byte_order (m_descr->gdbarch));
05d1431c
PA
759 else
760 *val = 0;
761 return status;
a378f419
AC
762}
763
05d1431c 764enum register_status
a378f419
AC
765regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
766 ULONGEST *val)
ef79d9a3
YQ
767{
768 gdb_assert (regcache != NULL);
6f98355c 769 return regcache->cooked_read (regnum, val);
a378f419
AC
770}
771
a66a9c23
AC
772void
773regcache_cooked_write_signed (struct regcache *regcache, int regnum,
774 LONGEST val)
ef79d9a3
YQ
775{
776 gdb_assert (regcache != NULL);
6f98355c 777 regcache->cooked_write (regnum, val);
ef79d9a3
YQ
778}
779
6f98355c 780template<typename T, typename>
ef79d9a3 781void
6f98355c 782regcache::cooked_write (int regnum, T val)
a66a9c23 783{
7c543f7b 784 gdb_byte *buf;
123f5f96 785
ef79d9a3
YQ
786 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
787 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
6f98355c
YQ
788 store_integer (buf, m_descr->sizeof_register[regnum],
789 gdbarch_byte_order (m_descr->gdbarch), val);
ef79d9a3 790 cooked_write (regnum, buf);
a66a9c23
AC
791}
792
793void
794regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
795 ULONGEST val)
ef79d9a3
YQ
796{
797 gdb_assert (regcache != NULL);
6f98355c 798 regcache->cooked_write (regnum, val);
a66a9c23
AC
799}
800
61a0eb5b 801void
2d522557
AC
802regcache_raw_write (struct regcache *regcache, int regnum,
803 const gdb_byte *buf)
ef79d9a3
YQ
804{
805 gdb_assert (regcache != NULL && buf != NULL);
806 regcache->raw_write (regnum, buf);
807}
808
809void
810regcache::raw_write (int regnum, const gdb_byte *buf)
61a0eb5b 811{
594f7785 812
ef79d9a3 813 gdb_assert (buf != NULL);
4e888c28 814 assert_regnum (regnum);
ef79d9a3 815 gdb_assert (!m_readonly_p);
3fadccb3 816
3fadccb3
AC
817 /* On the sparc, writing %g0 is a no-op, so we don't even want to
818 change the registers array if something writes to this register. */
ef79d9a3 819 if (gdbarch_cannot_store_register (arch (), regnum))
3fadccb3
AC
820 return;
821
3fadccb3 822 /* If we have a valid copy of the register, and new value == old
0df8b418 823 value, then don't bother doing the actual store. */
ef79d9a3
YQ
824 if (get_register_status (regnum) == REG_VALID
825 && (memcmp (register_buffer (regnum), buf,
826 m_descr->sizeof_register[regnum]) == 0))
3fadccb3
AC
827 return;
828
ef79d9a3 829 target_prepare_to_store (this);
c8ec2f33 830 raw_supply (regnum, buf);
b94ade42 831
b292235f
TT
832 /* Invalidate the register after it is written, in case of a
833 failure. */
834 regcache_invalidator invalidator (this, regnum);
b94ade42 835
ef79d9a3 836 target_store_registers (this, regnum);
594f7785 837
b292235f
TT
838 /* The target did not throw an error so we can discard invalidating
839 the register. */
840 invalidator.release ();
61a0eb5b
AC
841}
842
68365089 843void
2d522557
AC
844regcache_cooked_write (struct regcache *regcache, int regnum,
845 const gdb_byte *buf)
ef79d9a3
YQ
846{
847 regcache->cooked_write (regnum, buf);
848}
849
850void
851regcache::cooked_write (int regnum, const gdb_byte *buf)
68365089 852{
d138e37a 853 gdb_assert (regnum >= 0);
ef79d9a3 854 gdb_assert (regnum < m_descr->nr_cooked_registers);
d999647b 855 if (regnum < num_raw_registers ())
ef79d9a3 856 raw_write (regnum, buf);
d138e37a 857 else
ef79d9a3 858 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
d8124050 859 regnum, buf);
61a0eb5b
AC
860}
861
06c0b04e
AC
862/* Perform a partial register transfer using a read, modify, write
863 operation. */
864
865typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
866 void *buf);
867typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
868 const void *buf);
869
ef79d9a3 870enum register_status
849d0ba8
YQ
871readable_regcache::read_part (int regnum, int offset, int len, void *in,
872 bool is_raw)
873{
874 struct gdbarch *gdbarch = arch ();
875 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
876
877 gdb_assert (in != NULL);
878 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
879 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
880 /* Something to do? */
881 if (offset + len == 0)
882 return REG_VALID;
883 /* Read (when needed) ... */
884 enum register_status status;
885
886 if (is_raw)
887 status = raw_read (regnum, reg);
888 else
889 status = cooked_read (regnum, reg);
890 if (status != REG_VALID)
891 return status;
892
893 /* ... modify ... */
894 memcpy (in, reg + offset, len);
895
896 return REG_VALID;
897}
898
899enum register_status
900regcache::write_part (int regnum, int offset, int len,
d3037ba6 901 const void *out, bool is_raw)
ef79d9a3
YQ
902{
903 struct gdbarch *gdbarch = arch ();
9890e433 904 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
123f5f96 905
849d0ba8 906 gdb_assert (out != NULL);
ef79d9a3
YQ
907 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
908 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
06c0b04e
AC
909 /* Something to do? */
910 if (offset + len == 0)
05d1431c 911 return REG_VALID;
0df8b418 912 /* Read (when needed) ... */
849d0ba8 913 if (offset > 0
ef79d9a3 914 || offset + len < m_descr->sizeof_register[regnum])
06c0b04e 915 {
05d1431c
PA
916 enum register_status status;
917
d3037ba6
YQ
918 if (is_raw)
919 status = raw_read (regnum, reg);
920 else
921 status = cooked_read (regnum, reg);
05d1431c
PA
922 if (status != REG_VALID)
923 return status;
06c0b04e 924 }
849d0ba8
YQ
925
926 memcpy (reg + offset, out, len);
06c0b04e 927 /* ... write (when needed). */
849d0ba8
YQ
928 if (is_raw)
929 raw_write (regnum, reg);
930 else
931 cooked_write (regnum, reg);
05d1431c
PA
932
933 return REG_VALID;
06c0b04e
AC
934}
935
05d1431c 936enum register_status
06c0b04e 937regcache_raw_read_part (struct regcache *regcache, int regnum,
2d522557 938 int offset, int len, gdb_byte *buf)
06c0b04e 939{
ef79d9a3
YQ
940 return regcache->raw_read_part (regnum, offset, len, buf);
941}
123f5f96 942
ef79d9a3 943enum register_status
849d0ba8 944readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
ef79d9a3 945{
4e888c28 946 assert_regnum (regnum);
849d0ba8 947 return read_part (regnum, offset, len, buf, true);
06c0b04e
AC
948}
949
950void
951regcache_raw_write_part (struct regcache *regcache, int regnum,
2d522557 952 int offset, int len, const gdb_byte *buf)
06c0b04e 953{
ef79d9a3
YQ
954 regcache->raw_write_part (regnum, offset, len, buf);
955}
123f5f96 956
ef79d9a3
YQ
957void
958regcache::raw_write_part (int regnum, int offset, int len,
959 const gdb_byte *buf)
960{
4e888c28 961 assert_regnum (regnum);
849d0ba8 962 write_part (regnum, offset, len, buf, true);
06c0b04e
AC
963}
964
05d1431c 965enum register_status
06c0b04e 966regcache_cooked_read_part (struct regcache *regcache, int regnum,
2d522557 967 int offset, int len, gdb_byte *buf)
06c0b04e 968{
ef79d9a3
YQ
969 return regcache->cooked_read_part (regnum, offset, len, buf);
970}
123f5f96 971
ef79d9a3
YQ
972
973enum register_status
849d0ba8
YQ
974readable_regcache::cooked_read_part (int regnum, int offset, int len,
975 gdb_byte *buf)
ef79d9a3
YQ
976{
977 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
849d0ba8 978 return read_part (regnum, offset, len, buf, false);
06c0b04e
AC
979}
980
981void
982regcache_cooked_write_part (struct regcache *regcache, int regnum,
2d522557 983 int offset, int len, const gdb_byte *buf)
06c0b04e 984{
ef79d9a3
YQ
985 regcache->cooked_write_part (regnum, offset, len, buf);
986}
123f5f96 987
ef79d9a3
YQ
988void
989regcache::cooked_write_part (int regnum, int offset, int len,
990 const gdb_byte *buf)
991{
992 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
849d0ba8 993 write_part (regnum, offset, len, buf, false);
06c0b04e 994}
32178cab 995
a16d75cc 996/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
9a661b68
MK
997
998void
6618125d 999regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
ef79d9a3
YQ
1000{
1001 gdb_assert (regcache != NULL);
1002 regcache->raw_supply (regnum, buf);
1003}
1004
1005void
c8ec2f33 1006detached_regcache::raw_supply (int regnum, const void *buf)
9a661b68
MK
1007{
1008 void *regbuf;
1009 size_t size;
1010
4e888c28 1011 assert_regnum (regnum);
9a661b68 1012
ef79d9a3
YQ
1013 regbuf = register_buffer (regnum);
1014 size = m_descr->sizeof_register[regnum];
9a661b68
MK
1015
1016 if (buf)
ee99023e
PA
1017 {
1018 memcpy (regbuf, buf, size);
ef79d9a3 1019 m_register_status[regnum] = REG_VALID;
ee99023e 1020 }
9a661b68 1021 else
ee99023e
PA
1022 {
1023 /* This memset not strictly necessary, but better than garbage
1024 in case the register value manages to escape somewhere (due
1025 to a bug, no less). */
1026 memset (regbuf, 0, size);
ef79d9a3 1027 m_register_status[regnum] = REG_UNAVAILABLE;
ee99023e 1028 }
9a661b68
MK
1029}
1030
b057297a
AH
1031/* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1032 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1033 the register size is greater than ADDR_LEN, then the integer will be sign or
1034 zero extended. If the register size is smaller than the integer, then the
1035 most significant bytes of the integer will be truncated. */
1036
1037void
1038regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1039 bool is_signed)
1040{
1041 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1042 gdb_byte *regbuf;
1043 size_t regsize;
1044
4e888c28 1045 assert_regnum (regnum);
b057297a
AH
1046 gdb_assert (!m_readonly_p);
1047
1048 regbuf = register_buffer (regnum);
1049 regsize = m_descr->sizeof_register[regnum];
1050
1051 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1052 byte_order);
1053 m_register_status[regnum] = REG_VALID;
1054}
1055
f81fdd35
AH
1056/* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1057 as calling raw_supply with NULL (which will set the state to
1058 unavailable). */
1059
1060void
1061regcache::raw_supply_zeroed (int regnum)
1062{
1063 void *regbuf;
1064 size_t size;
1065
4e888c28 1066 assert_regnum (regnum);
f81fdd35
AH
1067 gdb_assert (!m_readonly_p);
1068
1069 regbuf = register_buffer (regnum);
1070 size = m_descr->sizeof_register[regnum];
1071
1072 memset (regbuf, 0, size);
1073 m_register_status[regnum] = REG_VALID;
1074}
1075
9a661b68
MK
1076/* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1077
1078void
6618125d 1079regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
ef79d9a3
YQ
1080{
1081 gdb_assert (regcache != NULL && buf != NULL);
1082 regcache->raw_collect (regnum, buf);
1083}
1084
1085void
1086regcache::raw_collect (int regnum, void *buf) const
9a661b68
MK
1087{
1088 const void *regbuf;
1089 size_t size;
1090
ef79d9a3 1091 gdb_assert (buf != NULL);
4e888c28 1092 assert_regnum (regnum);
9a661b68 1093
ef79d9a3
YQ
1094 regbuf = register_buffer (regnum);
1095 size = m_descr->sizeof_register[regnum];
9a661b68
MK
1096 memcpy (buf, regbuf, size);
1097}
1098
0b309272
AA
1099/* Transfer a single or all registers belonging to a certain register
1100 set to or from a buffer. This is the main worker function for
1101 regcache_supply_regset and regcache_collect_regset. */
1102
b057297a
AH
1103/* Collect register REGNUM from REGCACHE. Store collected value as an integer
1104 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1105 If ADDR_LEN is greater than the register size, then the integer will be sign
1106 or zero extended. If ADDR_LEN is smaller than the register size, then the
1107 most significant bytes of the integer will be truncated. */
1108
1109void
1110regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1111 bool is_signed) const
1112{
1113 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1114 const gdb_byte *regbuf;
1115 size_t regsize;
1116
4e888c28 1117 assert_regnum (regnum);
b057297a
AH
1118
1119 regbuf = register_buffer (regnum);
1120 regsize = m_descr->sizeof_register[regnum];
1121
1122 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1123 byte_order);
1124}
1125
ef79d9a3
YQ
1126void
1127regcache::transfer_regset (const struct regset *regset,
1128 struct regcache *out_regcache,
1129 int regnum, const void *in_buf,
1130 void *out_buf, size_t size) const
0b309272
AA
1131{
1132 const struct regcache_map_entry *map;
1133 int offs = 0, count;
1134
19ba03f4
SM
1135 for (map = (const struct regcache_map_entry *) regset->regmap;
1136 (count = map->count) != 0;
1137 map++)
0b309272
AA
1138 {
1139 int regno = map->regno;
1140 int slot_size = map->size;
1141
1142 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
ef79d9a3 1143 slot_size = m_descr->sizeof_register[regno];
0b309272
AA
1144
1145 if (regno == REGCACHE_MAP_SKIP
1146 || (regnum != -1
1147 && (regnum < regno || regnum >= regno + count)))
1148 offs += count * slot_size;
1149
1150 else if (regnum == -1)
1151 for (; count--; regno++, offs += slot_size)
1152 {
1153 if (offs + slot_size > size)
1154 break;
1155
1156 if (out_buf)
ef79d9a3 1157 raw_collect (regno, (gdb_byte *) out_buf + offs);
0b309272 1158 else
ef79d9a3
YQ
1159 out_regcache->raw_supply (regno, in_buf
1160 ? (const gdb_byte *) in_buf + offs
1161 : NULL);
0b309272
AA
1162 }
1163 else
1164 {
1165 /* Transfer a single register and return. */
1166 offs += (regnum - regno) * slot_size;
1167 if (offs + slot_size > size)
1168 return;
1169
1170 if (out_buf)
ef79d9a3 1171 raw_collect (regnum, (gdb_byte *) out_buf + offs);
0b309272 1172 else
ef79d9a3
YQ
1173 out_regcache->raw_supply (regnum, in_buf
1174 ? (const gdb_byte *) in_buf + offs
1175 : NULL);
0b309272
AA
1176 return;
1177 }
1178 }
1179}
1180
1181/* Supply register REGNUM from BUF to REGCACHE, using the register map
1182 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1183 If BUF is NULL, set the register(s) to "unavailable" status. */
1184
1185void
1186regcache_supply_regset (const struct regset *regset,
1187 struct regcache *regcache,
1188 int regnum, const void *buf, size_t size)
1189{
ef79d9a3
YQ
1190 regcache->supply_regset (regset, regnum, buf, size);
1191}
1192
1193void
1194regcache::supply_regset (const struct regset *regset,
1195 int regnum, const void *buf, size_t size)
1196{
1197 transfer_regset (regset, this, regnum, buf, NULL, size);
0b309272
AA
1198}
1199
1200/* Collect register REGNUM from REGCACHE to BUF, using the register
1201 map in REGSET. If REGNUM is -1, do this for all registers in
1202 REGSET. */
1203
1204void
1205regcache_collect_regset (const struct regset *regset,
1206 const struct regcache *regcache,
1207 int regnum, void *buf, size_t size)
1208{
ef79d9a3
YQ
1209 regcache->collect_regset (regset, regnum, buf, size);
1210}
1211
1212void
1213regcache::collect_regset (const struct regset *regset,
1214 int regnum, void *buf, size_t size) const
1215{
1216 transfer_regset (regset, NULL, regnum, NULL, buf, size);
0b309272
AA
1217}
1218
193cb69f 1219
515630c5 1220/* Special handling for register PC. */
32178cab
MS
1221
1222CORE_ADDR
515630c5 1223regcache_read_pc (struct regcache *regcache)
32178cab 1224{
ac7936df 1225 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 1226
32178cab
MS
1227 CORE_ADDR pc_val;
1228
61a1198a
UW
1229 if (gdbarch_read_pc_p (gdbarch))
1230 pc_val = gdbarch_read_pc (gdbarch, regcache);
cde9ea48 1231 /* Else use per-frame method on get_current_frame. */
214e098a 1232 else if (gdbarch_pc_regnum (gdbarch) >= 0)
cde9ea48 1233 {
61a1198a 1234 ULONGEST raw_val;
123f5f96 1235
05d1431c
PA
1236 if (regcache_cooked_read_unsigned (regcache,
1237 gdbarch_pc_regnum (gdbarch),
1238 &raw_val) == REG_UNAVAILABLE)
1239 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1240
214e098a 1241 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
cde9ea48
AC
1242 }
1243 else
515630c5
UW
1244 internal_error (__FILE__, __LINE__,
1245 _("regcache_read_pc: Unable to find PC"));
32178cab
MS
1246 return pc_val;
1247}
1248
32178cab 1249void
515630c5 1250regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
32178cab 1251{
ac7936df 1252 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 1253
61a1198a
UW
1254 if (gdbarch_write_pc_p (gdbarch))
1255 gdbarch_write_pc (gdbarch, regcache, pc);
214e098a 1256 else if (gdbarch_pc_regnum (gdbarch) >= 0)
3e8c568d 1257 regcache_cooked_write_unsigned (regcache,
214e098a 1258 gdbarch_pc_regnum (gdbarch), pc);
61a1198a
UW
1259 else
1260 internal_error (__FILE__, __LINE__,
515630c5 1261 _("regcache_write_pc: Unable to update PC"));
edb3359d
DJ
1262
1263 /* Writing the PC (for instance, from "load") invalidates the
1264 current frame. */
1265 reinit_frame_cache ();
32178cab
MS
1266}
1267
d999647b 1268int
31716595 1269reg_buffer::num_raw_registers () const
d999647b
YQ
1270{
1271 return gdbarch_num_regs (arch ());
1272}
1273
ed771251 1274void
ef79d9a3 1275regcache::debug_print_register (const char *func, int regno)
ed771251 1276{
ef79d9a3 1277 struct gdbarch *gdbarch = arch ();
ed771251
AH
1278
1279 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1280 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1281 && gdbarch_register_name (gdbarch, regno) != NULL
1282 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1283 fprintf_unfiltered (gdb_stdlog, "(%s)",
1284 gdbarch_register_name (gdbarch, regno));
1285 else
1286 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1287 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1288 {
1289 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1290 int size = register_size (gdbarch, regno);
ef79d9a3 1291 gdb_byte *buf = register_buffer (regno);
ed771251
AH
1292
1293 fprintf_unfiltered (gdb_stdlog, " = ");
1294 for (int i = 0; i < size; i++)
1295 {
1296 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1297 }
1298 if (size <= sizeof (LONGEST))
1299 {
1300 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1301
1302 fprintf_unfiltered (gdb_stdlog, " %s %s",
1303 core_addr_to_string_nz (val), plongest (val));
1304 }
1305 }
1306 fprintf_unfiltered (gdb_stdlog, "\n");
1307}
32178cab 1308
705152c5 1309static void
0b39b52e 1310reg_flush_command (const char *command, int from_tty)
705152c5
MS
1311{
1312 /* Force-flush the register cache. */
1313 registers_changed ();
1314 if (from_tty)
a3f17187 1315 printf_filtered (_("Register cache flushed.\n"));
705152c5
MS
1316}
1317
f3384e66
YQ
1318/* An abstract base class for register dump. */
1319
1320class register_dump
af030b9a 1321{
f3384e66
YQ
1322public:
1323 void dump (ui_file *file)
1324 {
1325 auto descr = regcache_descr (m_gdbarch);
1326 int regnum;
1327 int footnote_nr = 0;
1328 int footnote_register_offset = 0;
1329 int footnote_register_type_name_null = 0;
1330 long register_offset = 0;
1331
1332 gdb_assert (descr->nr_cooked_registers
1333 == (gdbarch_num_regs (m_gdbarch)
1334 + gdbarch_num_pseudo_regs (m_gdbarch)));
1335
1336 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1337 {
1338 /* Name. */
1339 if (regnum < 0)
1340 fprintf_unfiltered (file, " %-10s", "Name");
1341 else
1342 {
1343 const char *p = gdbarch_register_name (m_gdbarch, regnum);
af030b9a 1344
f3384e66
YQ
1345 if (p == NULL)
1346 p = "";
1347 else if (p[0] == '\0')
1348 p = "''";
1349 fprintf_unfiltered (file, " %-10s", p);
1350 }
af030b9a 1351
f3384e66
YQ
1352 /* Number. */
1353 if (regnum < 0)
1354 fprintf_unfiltered (file, " %4s", "Nr");
1355 else
1356 fprintf_unfiltered (file, " %4d", regnum);
123f5f96 1357
f3384e66
YQ
1358 /* Relative number. */
1359 if (regnum < 0)
1360 fprintf_unfiltered (file, " %4s", "Rel");
1361 else if (regnum < gdbarch_num_regs (m_gdbarch))
1362 fprintf_unfiltered (file, " %4d", regnum);
1363 else
1364 fprintf_unfiltered (file, " %4d",
1365 (regnum - gdbarch_num_regs (m_gdbarch)));
af030b9a 1366
f3384e66
YQ
1367 /* Offset. */
1368 if (regnum < 0)
1369 fprintf_unfiltered (file, " %6s ", "Offset");
1370 else
1371 {
1372 fprintf_unfiltered (file, " %6ld",
1373 descr->register_offset[regnum]);
1374 if (register_offset != descr->register_offset[regnum]
1375 || (regnum > 0
1376 && (descr->register_offset[regnum]
1377 != (descr->register_offset[regnum - 1]
1378 + descr->sizeof_register[regnum - 1])))
1379 )
1380 {
1381 if (!footnote_register_offset)
1382 footnote_register_offset = ++footnote_nr;
1383 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1384 }
1385 else
1386 fprintf_unfiltered (file, " ");
1387 register_offset = (descr->register_offset[regnum]
1388 + descr->sizeof_register[regnum]);
1389 }
af030b9a 1390
f3384e66
YQ
1391 /* Size. */
1392 if (regnum < 0)
1393 fprintf_unfiltered (file, " %5s ", "Size");
1394 else
1395 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
af030b9a 1396
f3384e66 1397 /* Type. */
af030b9a 1398 {
f3384e66
YQ
1399 const char *t;
1400 std::string name_holder;
1401
1402 if (regnum < 0)
1403 t = "Type";
1404 else
af030b9a 1405 {
f3384e66
YQ
1406 static const char blt[] = "builtin_type";
1407
1408 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1409 if (t == NULL)
1410 {
1411 if (!footnote_register_type_name_null)
1412 footnote_register_type_name_null = ++footnote_nr;
1413 name_holder = string_printf ("*%d",
1414 footnote_register_type_name_null);
1415 t = name_holder.c_str ();
1416 }
1417 /* Chop a leading builtin_type. */
1418 if (startswith (t, blt))
1419 t += strlen (blt);
af030b9a 1420 }
f3384e66 1421 fprintf_unfiltered (file, " %-15s", t);
af030b9a
AC
1422 }
1423
f3384e66
YQ
1424 /* Leading space always present. */
1425 fprintf_unfiltered (file, " ");
af030b9a 1426
f3384e66 1427 dump_reg (file, regnum);
123f5f96 1428
f3384e66
YQ
1429 fprintf_unfiltered (file, "\n");
1430 }
1431
1432 if (footnote_register_offset)
1433 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1434 footnote_register_offset);
1435 if (footnote_register_type_name_null)
1436 fprintf_unfiltered (file,
1437 "*%d: Register type's name NULL.\n",
1438 footnote_register_type_name_null);
1439 }
1440
1441 virtual ~register_dump () {};
1442
1443protected:
1444 register_dump (gdbarch *arch)
1445 : m_gdbarch (arch)
1446 {}
1447
1448 /* Dump the register REGNUM contents. If REGNUM is -1, print the
1449 header. */
1450 virtual void dump_reg (ui_file *file, int regnum) = 0;
1451
1452 gdbarch *m_gdbarch;
1453};
1454
1455/* Dump registers from regcache, used for dump raw registers and
1456 cooked registers. */
1457
1458class register_dump_regcache : public register_dump
1459{
1460public:
1461 register_dump_regcache (regcache *regcache, bool dump_pseudo)
1462 : register_dump (regcache->arch ()), m_regcache (regcache),
1463 m_dump_pseudo (dump_pseudo)
1464 {
1465 }
1466
1467protected:
1468 void dump_reg (ui_file *file, int regnum) override
1469 {
1470 if (regnum < 0)
1471 {
1472 if (m_dump_pseudo)
1473 fprintf_unfiltered (file, "Cooked value");
b59ff9d5 1474 else
f3384e66
YQ
1475 fprintf_unfiltered (file, "Raw value");
1476 }
1477 else
1478 {
1479 if (regnum < gdbarch_num_regs (m_gdbarch) || m_dump_pseudo)
b59ff9d5 1480 {
f3384e66 1481 auto size = register_size (m_gdbarch, regnum);
123f5f96 1482
f3384e66
YQ
1483 if (size == 0)
1484 return;
1485
1486 gdb::def_vector<gdb_byte> buf (size);
1487 auto status = m_regcache->cooked_read (regnum, buf.data ());
1488
1489 if (status == REG_UNKNOWN)
1490 fprintf_unfiltered (file, "<invalid>");
1491 else if (status == REG_UNAVAILABLE)
1492 fprintf_unfiltered (file, "<unavailable>");
1493 else
b59ff9d5 1494 {
f3384e66
YQ
1495 print_hex_chars (file, buf.data (), size,
1496 gdbarch_byte_order (m_gdbarch), true);
b59ff9d5 1497 }
b59ff9d5 1498 }
f3384e66
YQ
1499 else
1500 {
1501 /* Just print "<cooked>" for pseudo register when
1502 regcache_dump_raw. */
1503 fprintf_unfiltered (file, "<cooked>");
1504 }
b59ff9d5 1505 }
f3384e66 1506 }
b59ff9d5 1507
f3384e66
YQ
1508private:
1509 regcache *m_regcache;
af030b9a 1510
f3384e66
YQ
1511 /* Dump pseudo registers or not. */
1512 const bool m_dump_pseudo;
1513};
af030b9a 1514
f3384e66 1515/* For "maint print registers". */
50d6adef 1516
f3384e66
YQ
1517class register_dump_none : public register_dump
1518{
1519public:
1520 register_dump_none (gdbarch *arch)
1521 : register_dump (arch)
1522 {}
05d1431c 1523
f3384e66
YQ
1524protected:
1525 void dump_reg (ui_file *file, int regnum) override
1526 {}
1527};
50d6adef 1528
f3384e66 1529/* For "maint print remote-registers". */
af030b9a 1530
f3384e66
YQ
1531class register_dump_remote : public register_dump
1532{
1533public:
1534 register_dump_remote (gdbarch *arch)
1535 : register_dump (arch)
1536 {}
123f5f96 1537
f3384e66
YQ
1538protected:
1539 void dump_reg (ui_file *file, int regnum) override
1540 {
1541 if (regnum < 0)
1542 {
1543 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1544 }
1545 else if (regnum < gdbarch_num_regs (m_gdbarch))
1546 {
1547 int pnum, poffset;
b59ff9d5 1548
f3384e66
YQ
1549 if (remote_register_number_and_offset (m_gdbarch, regnum,
1550 &pnum, &poffset))
1551 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1552 }
1553 }
1554};
c21236dc 1555
f3384e66 1556/* For "maint print register-groups". */
c21236dc 1557
f3384e66
YQ
1558class register_dump_groups : public register_dump
1559{
1560public:
1561 register_dump_groups (gdbarch *arch)
1562 : register_dump (arch)
1563 {}
af030b9a 1564
f3384e66
YQ
1565protected:
1566 void dump_reg (ui_file *file, int regnum) override
1567 {
1568 if (regnum < 0)
1569 fprintf_unfiltered (file, "Groups");
1570 else
1571 {
1572 const char *sep = "";
1573 struct reggroup *group;
1574
1575 for (group = reggroup_next (m_gdbarch, NULL);
1576 group != NULL;
1577 group = reggroup_next (m_gdbarch, group))
1578 {
1579 if (gdbarch_register_reggroup_p (m_gdbarch, regnum, group))
1580 {
1581 fprintf_unfiltered (file,
1582 "%s%s", sep, reggroup_name (group));
1583 sep = ",";
1584 }
1585 }
1586 }
1587 }
1588};
1589
1590enum regcache_dump_what
1591{
1592 regcache_dump_none, regcache_dump_raw,
1593 regcache_dump_cooked, regcache_dump_groups,
1594 regcache_dump_remote
1595};
af030b9a
AC
1596
1597static void
4e001312 1598regcache_print (const char *args, enum regcache_dump_what what_to_dump)
af030b9a 1599{
ed4227b7
PA
1600 /* Where to send output. */
1601 stdio_file file;
1602 ui_file *out;
1603
af030b9a 1604 if (args == NULL)
ed4227b7 1605 out = gdb_stdout;
af030b9a
AC
1606 else
1607 {
d7e74731 1608 if (!file.open (args, "w"))
e2e0b3e5 1609 perror_with_name (_("maintenance print architecture"));
ed4227b7
PA
1610 out = &file;
1611 }
1612
f3384e66
YQ
1613 std::unique_ptr<register_dump> dump;
1614 std::unique_ptr<regcache> regs;
1615 gdbarch *gdbarch;
1616
ed4227b7 1617 if (target_has_registers)
f3384e66 1618 gdbarch = get_current_regcache ()->arch ();
ed4227b7 1619 else
f3384e66
YQ
1620 gdbarch = target_gdbarch ();
1621
1622 switch (what_to_dump)
ed4227b7 1623 {
f3384e66
YQ
1624 case regcache_dump_none:
1625 dump.reset (new register_dump_none (gdbarch));
1626 break;
1627 case regcache_dump_remote:
1628 dump.reset (new register_dump_remote (gdbarch));
1629 break;
1630 case regcache_dump_groups:
1631 dump.reset (new register_dump_groups (gdbarch));
1632 break;
1633 case regcache_dump_raw:
1634 case regcache_dump_cooked:
1635 {
1636 regcache *reg;
1637
1638 if (target_has_registers)
1639 reg = get_current_regcache ();
1640 else
1641 {
1642 /* For the benefit of "maint print registers" & co when
1643 debugging an executable, allow dumping a regcache even when
1644 there is no thread selected / no registers. */
1645 reg = new regcache (target_gdbarch ());
1646 regs.reset (reg);
1647 }
1648
1649 auto dump_pseudo = (what_to_dump == regcache_dump_cooked);
1650
1651 dump.reset (new register_dump_regcache (reg, dump_pseudo));
1652 }
1653 break;
af030b9a 1654 }
f3384e66
YQ
1655
1656 dump->dump (out);
af030b9a
AC
1657}
1658
1659static void
4e001312 1660maintenance_print_registers (const char *args, int from_tty)
af030b9a
AC
1661{
1662 regcache_print (args, regcache_dump_none);
1663}
1664
1665static void
4e001312 1666maintenance_print_raw_registers (const char *args, int from_tty)
af030b9a
AC
1667{
1668 regcache_print (args, regcache_dump_raw);
1669}
1670
1671static void
4e001312 1672maintenance_print_cooked_registers (const char *args, int from_tty)
af030b9a
AC
1673{
1674 regcache_print (args, regcache_dump_cooked);
1675}
1676
b59ff9d5 1677static void
4e001312 1678maintenance_print_register_groups (const char *args, int from_tty)
b59ff9d5
AC
1679{
1680 regcache_print (args, regcache_dump_groups);
1681}
1682
c21236dc 1683static void
4e001312 1684maintenance_print_remote_registers (const char *args, int from_tty)
c21236dc
PA
1685{
1686 regcache_print (args, regcache_dump_remote);
1687}
1688
8248946c
YQ
1689#if GDB_SELF_TEST
1690#include "selftest.h"
1b30aaa5
YQ
1691#include "selftest-arch.h"
1692#include "gdbthread.h"
ec7a5fcb 1693#include "target-float.h"
8248946c
YQ
1694
1695namespace selftests {
1696
e521e87e 1697class regcache_access : public regcache
8248946c 1698{
e521e87e
YQ
1699public:
1700
1701 /* Return the number of elements in current_regcache. */
1702
1703 static size_t
1704 current_regcache_size ()
1705 {
1706 return std::distance (regcache::current_regcache.begin (),
1707 regcache::current_regcache.end ());
1708 }
1709};
8248946c
YQ
1710
1711static void
1712current_regcache_test (void)
1713{
1714 /* It is empty at the start. */
e521e87e 1715 SELF_CHECK (regcache_access::current_regcache_size () == 0);
8248946c
YQ
1716
1717 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1718
1719 /* Get regcache from ptid1, a new regcache is added to
1720 current_regcache. */
1721 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1722 target_gdbarch (),
1723 NULL);
1724
1725 SELF_CHECK (regcache != NULL);
1726 SELF_CHECK (regcache->ptid () == ptid1);
e521e87e 1727 SELF_CHECK (regcache_access::current_regcache_size () == 1);
8248946c
YQ
1728
1729 /* Get regcache from ptid2, a new regcache is added to
1730 current_regcache. */
1731 regcache = get_thread_arch_aspace_regcache (ptid2,
1732 target_gdbarch (),
1733 NULL);
1734 SELF_CHECK (regcache != NULL);
1735 SELF_CHECK (regcache->ptid () == ptid2);
e521e87e 1736 SELF_CHECK (regcache_access::current_regcache_size () == 2);
8248946c
YQ
1737
1738 /* Get regcache from ptid3, a new regcache is added to
1739 current_regcache. */
1740 regcache = get_thread_arch_aspace_regcache (ptid3,
1741 target_gdbarch (),
1742 NULL);
1743 SELF_CHECK (regcache != NULL);
1744 SELF_CHECK (regcache->ptid () == ptid3);
e521e87e 1745 SELF_CHECK (regcache_access::current_regcache_size () == 3);
8248946c
YQ
1746
1747 /* Get regcache from ptid2 again, nothing is added to
1748 current_regcache. */
1749 regcache = get_thread_arch_aspace_regcache (ptid2,
1750 target_gdbarch (),
1751 NULL);
1752 SELF_CHECK (regcache != NULL);
1753 SELF_CHECK (regcache->ptid () == ptid2);
e521e87e 1754 SELF_CHECK (regcache_access::current_regcache_size () == 3);
8248946c
YQ
1755
1756 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1757 current_regcache. */
1758 registers_changed_ptid (ptid2);
e521e87e 1759 SELF_CHECK (regcache_access::current_regcache_size () == 2);
8248946c
YQ
1760}
1761
1b30aaa5
YQ
1762static void test_target_fetch_registers (target_ops *self, regcache *regs,
1763 int regno);
1764static void test_target_store_registers (target_ops *self, regcache *regs,
1765 int regno);
1766static enum target_xfer_status
1767 test_target_xfer_partial (struct target_ops *ops,
1768 enum target_object object,
1769 const char *annex, gdb_byte *readbuf,
1770 const gdb_byte *writebuf,
1771 ULONGEST offset, ULONGEST len,
1772 ULONGEST *xfered_len);
1773
1774class target_ops_no_register : public test_target_ops
1775{
1776public:
1777 target_ops_no_register ()
1778 : test_target_ops {}
1779 {
1780 to_fetch_registers = test_target_fetch_registers;
1781 to_store_registers = test_target_store_registers;
1782 to_xfer_partial = test_target_xfer_partial;
1783
1784 to_data = this;
1785 }
1786
1787 void reset ()
1788 {
1789 fetch_registers_called = 0;
1790 store_registers_called = 0;
1791 xfer_partial_called = 0;
1792 }
1793
1794 unsigned int fetch_registers_called = 0;
1795 unsigned int store_registers_called = 0;
1796 unsigned int xfer_partial_called = 0;
1797};
1798
1799static void
1800test_target_fetch_registers (target_ops *self, regcache *regs, int regno)
1801{
1802 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1803
1804 /* Mark register available. */
1805 regs->raw_supply_zeroed (regno);
1806 ops->fetch_registers_called++;
1807}
1808
1809static void
1810test_target_store_registers (target_ops *self, regcache *regs, int regno)
1811{
1812 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1813
1814 ops->store_registers_called++;
1815}
1816
1817static enum target_xfer_status
1818test_target_xfer_partial (struct target_ops *self, enum target_object object,
1819 const char *annex, gdb_byte *readbuf,
1820 const gdb_byte *writebuf,
1821 ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1822{
1823 auto ops = static_cast<target_ops_no_register *> (self->to_data);
1824
1825 ops->xfer_partial_called++;
1826
1827 *xfered_len = len;
1828 return TARGET_XFER_OK;
1829}
1830
1831class readwrite_regcache : public regcache
1832{
1833public:
1834 readwrite_regcache (struct gdbarch *gdbarch)
1835 : regcache (gdbarch, nullptr, false)
1836 {}
1837};
1838
1839/* Test regcache::cooked_read gets registers from raw registers and
1840 memory instead of target to_{fetch,store}_registers. */
1841
1842static void
1843cooked_read_test (struct gdbarch *gdbarch)
1844{
1845 /* Error out if debugging something, because we're going to push the
1846 test target, which would pop any existing target. */
1847 if (current_target.to_stratum >= process_stratum)
1848 error (_("target already pushed"));
1849
1850 /* Create a mock environment. An inferior with a thread, with a
1851 process_stratum target pushed. */
1852
1853 target_ops_no_register mock_target;
1854 ptid_t mock_ptid (1, 1);
1855 inferior mock_inferior (mock_ptid.pid ());
1856 address_space mock_aspace {};
1857 mock_inferior.gdbarch = gdbarch;
1858 mock_inferior.aspace = &mock_aspace;
1859 thread_info mock_thread (&mock_inferior, mock_ptid);
1860
1861 scoped_restore restore_thread_list
1862 = make_scoped_restore (&thread_list, &mock_thread);
1863
1864 /* Add the mock inferior to the inferior list so that look ups by
1865 target+ptid can find it. */
1866 scoped_restore restore_inferior_list
1867 = make_scoped_restore (&inferior_list);
1868 inferior_list = &mock_inferior;
1869
1870 /* Switch to the mock inferior. */
1871 scoped_restore_current_inferior restore_current_inferior;
1872 set_current_inferior (&mock_inferior);
1873
1874 /* Push the process_stratum target so we can mock accessing
1875 registers. */
1876 push_target (&mock_target);
1877
1878 /* Pop it again on exit (return/exception). */
1879 struct on_exit
1880 {
1881 ~on_exit ()
1882 {
1883 pop_all_targets_at_and_above (process_stratum);
1884 }
1885 } pop_targets;
1886
1887 /* Switch to the mock thread. */
1888 scoped_restore restore_inferior_ptid
1889 = make_scoped_restore (&inferior_ptid, mock_ptid);
1890
1891 /* Test that read one raw register from regcache_no_target will go
1892 to the target layer. */
1893 int regnum;
1894
1895 /* Find a raw register which size isn't zero. */
1896 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1897 {
1898 if (register_size (gdbarch, regnum) != 0)
1899 break;
1900 }
1901
1902 readwrite_regcache readwrite (gdbarch);
1903 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1904
1905 readwrite.raw_read (regnum, buf.data ());
1906
1907 /* raw_read calls target_fetch_registers. */
1908 SELF_CHECK (mock_target.fetch_registers_called > 0);
1909 mock_target.reset ();
1910
1911 /* Mark all raw registers valid, so the following raw registers
1912 accesses won't go to target. */
1913 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1914 readwrite.raw_update (i);
1915
1916 mock_target.reset ();
1917 /* Then, read all raw and pseudo registers, and don't expect calling
1918 to_{fetch,store}_registers. */
1919 for (int regnum = 0;
1920 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1921 regnum++)
1922 {
1923 if (register_size (gdbarch, regnum) == 0)
1924 continue;
1925
1926 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1927
1928 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1929
dc711524
YQ
1930 SELF_CHECK (mock_target.fetch_registers_called == 0);
1931 SELF_CHECK (mock_target.store_registers_called == 0);
1b30aaa5
YQ
1932
1933 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1934 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1935 SELF_CHECK (mock_target.xfer_partial_called == 0);
1936
1937 mock_target.reset ();
1938 }
a63f2d2f
YQ
1939
1940 regcache readonly (regcache::readonly, readwrite);
1941
1942 /* GDB may go to target layer to fetch all registers and memory for
1943 readonly regcache. */
1944 mock_target.reset ();
1945
1946 for (int regnum = 0;
1947 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1948 regnum++)
1949 {
a63f2d2f
YQ
1950 if (register_size (gdbarch, regnum) == 0)
1951 continue;
1952
1953 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1954 enum register_status status = readonly.cooked_read (regnum,
1955 buf.data ());
1956
1957 if (regnum < gdbarch_num_regs (gdbarch))
1958 {
1959 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1960
1961 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1962 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1963 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1964 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1965 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1966 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1967 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score)
1968 {
1969 /* Raw registers. If raw registers are not in save_reggroup,
1970 their status are unknown. */
1971 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1972 SELF_CHECK (status == REG_VALID);
1973 else
1974 SELF_CHECK (status == REG_UNKNOWN);
1975 }
1976 else
1977 SELF_CHECK (status == REG_VALID);
1978 }
1979 else
1980 {
1981 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1982 SELF_CHECK (status == REG_VALID);
1983 else
1984 {
1985 /* If pseudo registers are not in save_reggroup, some of
1986 them can be computed from saved raw registers, but some
1987 of them are unknown. */
1988 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1989
1990 if (bfd_arch == bfd_arch_frv
1991 || bfd_arch == bfd_arch_m32c
1992 || bfd_arch == bfd_arch_mep
1993 || bfd_arch == bfd_arch_sh)
1994 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1995 else if (bfd_arch == bfd_arch_mips
1996 || bfd_arch == bfd_arch_h8300)
1997 SELF_CHECK (status == REG_UNKNOWN);
1998 else
1999 SELF_CHECK (status == REG_VALID);
2000 }
2001 }
2002
2003 SELF_CHECK (mock_target.fetch_registers_called == 0);
2004 SELF_CHECK (mock_target.store_registers_called == 0);
2005 SELF_CHECK (mock_target.xfer_partial_called == 0);
2006
2007 mock_target.reset ();
2008 }
1b30aaa5
YQ
2009}
2010
ec7a5fcb
YQ
2011/* Test regcache::cooked_write by writing some expected contents to
2012 registers, and checking that contents read from registers and the
2013 expected contents are the same. */
2014
2015static void
2016cooked_write_test (struct gdbarch *gdbarch)
2017{
2018 /* Error out if debugging something, because we're going to push the
2019 test target, which would pop any existing target. */
2020 if (current_target.to_stratum >= process_stratum)
2021 error (_("target already pushed"));
2022
2023 /* Create a mock environment. A process_stratum target pushed. */
2024
2025 target_ops_no_register mock_target;
2026
2027 /* Push the process_stratum target so we can mock accessing
2028 registers. */
2029 push_target (&mock_target);
2030
2031 /* Pop it again on exit (return/exception). */
2032 struct on_exit
2033 {
2034 ~on_exit ()
2035 {
2036 pop_all_targets_at_and_above (process_stratum);
2037 }
2038 } pop_targets;
2039
2040 readwrite_regcache readwrite (gdbarch);
2041
2042 const int num_regs = (gdbarch_num_regs (gdbarch)
2043 + gdbarch_num_pseudo_regs (gdbarch));
2044
2045 for (auto regnum = 0; regnum < num_regs; regnum++)
2046 {
2047 if (register_size (gdbarch, regnum) == 0
2048 || gdbarch_cannot_store_register (gdbarch, regnum))
2049 continue;
2050
2051 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2052
2053 if ((bfd_arch == bfd_arch_sparc
2054 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
2055 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
2056 && gdbarch_ptr_bit (gdbarch) == 64
2057 && (regnum >= gdbarch_num_regs (gdbarch)
2058 && regnum <= gdbarch_num_regs (gdbarch) + 4))
2059 || (bfd_arch == bfd_arch_sh
2060 /* FPSCR_C_REGNUM in sh64 is hard to test. */
2061 && gdbarch_bfd_arch_info (gdbarch)->mach == bfd_mach_sh5
2062 && regnum == 243)
2063 || (bfd_arch == bfd_arch_spu
2064 /* SPU pseudo registers except SPU_SP_REGNUM are got by
2065 TARGET_OBJECT_SPU. */
2066 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
2067 continue;
2068
2069 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
2070 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
2071 const auto type = register_type (gdbarch, regnum);
2072
2073 if (TYPE_CODE (type) == TYPE_CODE_FLT
2074 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
2075 {
2076 /* Generate valid float format. */
2077 target_float_from_string (expected.data (), type, "1.25");
2078 }
2079 else if (TYPE_CODE (type) == TYPE_CODE_INT
2080 || TYPE_CODE (type) == TYPE_CODE_ARRAY
2081 || TYPE_CODE (type) == TYPE_CODE_PTR
2082 || TYPE_CODE (type) == TYPE_CODE_UNION
2083 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
2084 {
2085 if (bfd_arch == bfd_arch_ia64
2086 || (regnum >= gdbarch_num_regs (gdbarch)
2087 && (bfd_arch == bfd_arch_xtensa
2088 || bfd_arch == bfd_arch_bfin
2089 || bfd_arch == bfd_arch_m32c
2090 /* m68hc11 pseudo registers are in memory. */
2091 || bfd_arch == bfd_arch_m68hc11
2092 || bfd_arch == bfd_arch_m68hc12
2093 || bfd_arch == bfd_arch_s390))
2094 || (bfd_arch == bfd_arch_frv
2095 /* FRV pseudo registers except iacc0. */
2096 && regnum > gdbarch_num_regs (gdbarch)))
2097 {
2098 /* Skip setting the expected values for some architecture
2099 registers. */
2100 }
2101 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
2102 {
2103 /* RL78_PC_REGNUM */
2104 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
2105 expected[j] = j;
2106 }
2107 else
2108 {
2109 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
2110 expected[j] = j;
2111 }
2112 }
2113 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
2114 {
2115 /* No idea how to test flags. */
2116 continue;
2117 }
2118 else
2119 {
2120 /* If we don't know how to create the expected value for the
2121 this type, make it fail. */
2122 SELF_CHECK (0);
2123 }
2124
2125 readwrite.cooked_write (regnum, expected.data ());
2126
2127 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
2128 SELF_CHECK (expected == buf);
2129 }
2130}
2131
8248946c
YQ
2132} // namespace selftests
2133#endif /* GDB_SELF_TEST */
2134
32178cab
MS
2135void
2136_initialize_regcache (void)
2137{
3e43a32a
MS
2138 regcache_descr_handle
2139 = gdbarch_data_register_post_init (init_regcache_descr);
705152c5 2140
f4c5303c 2141 observer_attach_target_changed (regcache_observer_target_changed);
e521e87e 2142 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
f4c5303c 2143
705152c5 2144 add_com ("flushregs", class_maintenance, reg_flush_command,
1bedd215 2145 _("Force gdb to flush its register cache (maintainer command)"));
39f77062 2146
3e43a32a
MS
2147 add_cmd ("registers", class_maintenance, maintenance_print_registers,
2148 _("Print the internal register configuration.\n"
2149 "Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 2150 add_cmd ("raw-registers", class_maintenance,
3e43a32a
MS
2151 maintenance_print_raw_registers,
2152 _("Print the internal register configuration "
2153 "including raw values.\n"
2154 "Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 2155 add_cmd ("cooked-registers", class_maintenance,
3e43a32a
MS
2156 maintenance_print_cooked_registers,
2157 _("Print the internal register configuration "
2158 "including cooked values.\n"
2159 "Takes an optional file parameter."), &maintenanceprintlist);
b59ff9d5 2160 add_cmd ("register-groups", class_maintenance,
3e43a32a
MS
2161 maintenance_print_register_groups,
2162 _("Print the internal register configuration "
2163 "including each register's group.\n"
2164 "Takes an optional file parameter."),
af030b9a 2165 &maintenanceprintlist);
c21236dc
PA
2166 add_cmd ("remote-registers", class_maintenance,
2167 maintenance_print_remote_registers, _("\
2168Print the internal register configuration including each register's\n\
2169remote register number and buffer offset in the g/G packets.\n\
2170Takes an optional file parameter."),
2171 &maintenanceprintlist);
1526853e 2172
8248946c 2173#if GDB_SELF_TEST
1526853e 2174 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1b30aaa5
YQ
2175
2176 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2177 selftests::cooked_read_test);
ec7a5fcb
YQ
2178 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2179 selftests::cooked_write_test);
8248946c 2180#endif
32178cab 2181}
This page took 1.756528 seconds and 4 git commands to generate.