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