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