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