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