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