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