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