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