Remove regcache_raw_read
[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
ef79d9a3
YQ
491void
492regcache::raw_update (int regnum)
493{
4e888c28 494 assert_regnum (regnum);
8e368124 495
3fadccb3
AC
496 /* Make certain that the register cache is up-to-date with respect
497 to the current thread. This switching shouldn't be necessary
498 only there is still only one target side register cache. Sigh!
499 On the bright side, at least there is a regcache object. */
8e368124 500
796bb026 501 if (get_register_status (regnum) == REG_UNKNOWN)
3fadccb3 502 {
ef79d9a3 503 target_fetch_registers (this, regnum);
788c8b10
PA
504
505 /* A number of targets can't access the whole set of raw
506 registers (because the debug API provides no means to get at
507 them). */
ef79d9a3
YQ
508 if (m_register_status[regnum] == REG_UNKNOWN)
509 m_register_status[regnum] = REG_UNAVAILABLE;
3fadccb3 510 }
8e368124
AH
511}
512
ef79d9a3 513enum register_status
849d0ba8 514readable_regcache::raw_read (int regnum, gdb_byte *buf)
8e368124
AH
515{
516 gdb_assert (buf != NULL);
ef79d9a3 517 raw_update (regnum);
05d1431c 518
ef79d9a3
YQ
519 if (m_register_status[regnum] != REG_VALID)
520 memset (buf, 0, m_descr->sizeof_register[regnum]);
05d1431c 521 else
ef79d9a3
YQ
522 memcpy (buf, register_buffer (regnum),
523 m_descr->sizeof_register[regnum]);
05d1431c 524
ef79d9a3 525 return (enum register_status) m_register_status[regnum];
61a0eb5b
AC
526}
527
05d1431c 528enum register_status
28fc6740 529regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
ef79d9a3
YQ
530{
531 gdb_assert (regcache != NULL);
6f98355c 532 return regcache->raw_read (regnum, val);
ef79d9a3
YQ
533}
534
6f98355c 535template<typename T, typename>
ef79d9a3 536enum register_status
849d0ba8 537readable_regcache::raw_read (int regnum, T *val)
28fc6740 538{
2d522557 539 gdb_byte *buf;
05d1431c 540 enum register_status status;
123f5f96 541
4e888c28 542 assert_regnum (regnum);
ef79d9a3
YQ
543 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
544 status = raw_read (regnum, buf);
05d1431c 545 if (status == REG_VALID)
6f98355c
YQ
546 *val = extract_integer<T> (buf,
547 m_descr->sizeof_register[regnum],
548 gdbarch_byte_order (m_descr->gdbarch));
05d1431c
PA
549 else
550 *val = 0;
551 return status;
28fc6740
AC
552}
553
05d1431c 554enum register_status
28fc6740
AC
555regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
556 ULONGEST *val)
ef79d9a3
YQ
557{
558 gdb_assert (regcache != NULL);
6f98355c 559 return regcache->raw_read (regnum, val);
28fc6740
AC
560}
561
c00dcbe9
MK
562void
563regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
ef79d9a3
YQ
564{
565 gdb_assert (regcache != NULL);
6f98355c 566 regcache->raw_write (regnum, val);
ef79d9a3
YQ
567}
568
6f98355c 569template<typename T, typename>
ef79d9a3 570void
6f98355c 571regcache::raw_write (int regnum, T val)
c00dcbe9 572{
7c543f7b 573 gdb_byte *buf;
123f5f96 574
4e888c28 575 assert_regnum (regnum);
ef79d9a3 576 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
6f98355c
YQ
577 store_integer (buf, m_descr->sizeof_register[regnum],
578 gdbarch_byte_order (m_descr->gdbarch), val);
ef79d9a3 579 raw_write (regnum, buf);
c00dcbe9
MK
580}
581
582void
583regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
584 ULONGEST val)
ef79d9a3
YQ
585{
586 gdb_assert (regcache != NULL);
6f98355c 587 regcache->raw_write (regnum, val);
c00dcbe9
MK
588}
589
9fd15b2e
YQ
590LONGEST
591regcache_raw_get_signed (struct regcache *regcache, int regnum)
592{
593 LONGEST value;
594 enum register_status status;
595
596 status = regcache_raw_read_signed (regcache, regnum, &value);
597 if (status == REG_UNAVAILABLE)
598 throw_error (NOT_AVAILABLE_ERROR,
599 _("Register %d is not available"), regnum);
600 return value;
601}
602
05d1431c 603enum register_status
2d522557 604regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
ef79d9a3
YQ
605{
606 return regcache->cooked_read (regnum, buf);
607}
608
609enum register_status
849d0ba8 610readable_regcache::cooked_read (int regnum, gdb_byte *buf)
68365089 611{
d138e37a 612 gdb_assert (regnum >= 0);
ef79d9a3 613 gdb_assert (regnum < m_descr->nr_cooked_registers);
d999647b 614 if (regnum < num_raw_registers ())
ef79d9a3 615 return raw_read (regnum, buf);
849d0ba8 616 else if (m_has_pseudo
ef79d9a3 617 && m_register_status[regnum] != REG_UNKNOWN)
05d1431c 618 {
ef79d9a3
YQ
619 if (m_register_status[regnum] == REG_VALID)
620 memcpy (buf, register_buffer (regnum),
621 m_descr->sizeof_register[regnum]);
05d1431c 622 else
ef79d9a3 623 memset (buf, 0, m_descr->sizeof_register[regnum]);
05d1431c 624
ef79d9a3 625 return (enum register_status) m_register_status[regnum];
05d1431c 626 }
ef79d9a3 627 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
3543a589
TT
628 {
629 struct value *mark, *computed;
630 enum register_status result = REG_VALID;
631
632 mark = value_mark ();
633
ef79d9a3
YQ
634 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
635 this, regnum);
3543a589
TT
636 if (value_entirely_available (computed))
637 memcpy (buf, value_contents_raw (computed),
ef79d9a3 638 m_descr->sizeof_register[regnum]);
3543a589
TT
639 else
640 {
ef79d9a3 641 memset (buf, 0, m_descr->sizeof_register[regnum]);
3543a589
TT
642 result = REG_UNAVAILABLE;
643 }
644
645 value_free_to_mark (mark);
646
647 return result;
648 }
d138e37a 649 else
ef79d9a3 650 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
05d1431c 651 regnum, buf);
61a0eb5b
AC
652}
653
3543a589
TT
654struct value *
655regcache_cooked_read_value (struct regcache *regcache, int regnum)
ef79d9a3
YQ
656{
657 return regcache->cooked_read_value (regnum);
658}
659
660struct value *
849d0ba8 661readable_regcache::cooked_read_value (int regnum)
3543a589
TT
662{
663 gdb_assert (regnum >= 0);
ef79d9a3 664 gdb_assert (regnum < m_descr->nr_cooked_registers);
3543a589 665
d999647b 666 if (regnum < num_raw_registers ()
849d0ba8 667 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
ef79d9a3 668 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
3543a589
TT
669 {
670 struct value *result;
671
ef79d9a3 672 result = allocate_value (register_type (m_descr->gdbarch, regnum));
3543a589
TT
673 VALUE_LVAL (result) = lval_register;
674 VALUE_REGNUM (result) = regnum;
675
676 /* It is more efficient in general to do this delegation in this
677 direction than in the other one, even though the value-based
678 API is preferred. */
ef79d9a3
YQ
679 if (cooked_read (regnum,
680 value_contents_raw (result)) == REG_UNAVAILABLE)
3543a589
TT
681 mark_value_bytes_unavailable (result, 0,
682 TYPE_LENGTH (value_type (result)));
683
684 return result;
685 }
686 else
ef79d9a3
YQ
687 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
688 this, regnum);
3543a589
TT
689}
690
05d1431c 691enum register_status
a378f419
AC
692regcache_cooked_read_signed (struct regcache *regcache, int regnum,
693 LONGEST *val)
ef79d9a3
YQ
694{
695 gdb_assert (regcache != NULL);
6f98355c 696 return regcache->cooked_read (regnum, val);
ef79d9a3
YQ
697}
698
6f98355c 699template<typename T, typename>
ef79d9a3 700enum register_status
849d0ba8 701readable_regcache::cooked_read (int regnum, T *val)
a378f419 702{
05d1431c 703 enum register_status status;
2d522557 704 gdb_byte *buf;
123f5f96 705
ef79d9a3
YQ
706 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
707 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
708 status = cooked_read (regnum, buf);
05d1431c 709 if (status == REG_VALID)
6f98355c
YQ
710 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
711 gdbarch_byte_order (m_descr->gdbarch));
05d1431c
PA
712 else
713 *val = 0;
714 return status;
a378f419
AC
715}
716
05d1431c 717enum register_status
a378f419
AC
718regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
719 ULONGEST *val)
ef79d9a3
YQ
720{
721 gdb_assert (regcache != NULL);
6f98355c 722 return regcache->cooked_read (regnum, val);
a378f419
AC
723}
724
a66a9c23
AC
725void
726regcache_cooked_write_signed (struct regcache *regcache, int regnum,
727 LONGEST val)
ef79d9a3
YQ
728{
729 gdb_assert (regcache != NULL);
6f98355c 730 regcache->cooked_write (regnum, val);
ef79d9a3
YQ
731}
732
6f98355c 733template<typename T, typename>
ef79d9a3 734void
6f98355c 735regcache::cooked_write (int regnum, T val)
a66a9c23 736{
7c543f7b 737 gdb_byte *buf;
123f5f96 738
ef79d9a3
YQ
739 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
740 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
6f98355c
YQ
741 store_integer (buf, m_descr->sizeof_register[regnum],
742 gdbarch_byte_order (m_descr->gdbarch), val);
ef79d9a3 743 cooked_write (regnum, buf);
a66a9c23
AC
744}
745
746void
747regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
748 ULONGEST val)
ef79d9a3
YQ
749{
750 gdb_assert (regcache != NULL);
6f98355c 751 regcache->cooked_write (regnum, val);
a66a9c23
AC
752}
753
61a0eb5b 754void
2d522557
AC
755regcache_raw_write (struct regcache *regcache, int regnum,
756 const gdb_byte *buf)
ef79d9a3
YQ
757{
758 gdb_assert (regcache != NULL && buf != NULL);
759 regcache->raw_write (regnum, buf);
760}
761
762void
763regcache::raw_write (int regnum, const gdb_byte *buf)
61a0eb5b 764{
594f7785 765
ef79d9a3 766 gdb_assert (buf != NULL);
4e888c28 767 assert_regnum (regnum);
3fadccb3 768
3fadccb3
AC
769 /* On the sparc, writing %g0 is a no-op, so we don't even want to
770 change the registers array if something writes to this register. */
ef79d9a3 771 if (gdbarch_cannot_store_register (arch (), regnum))
3fadccb3
AC
772 return;
773
3fadccb3 774 /* If we have a valid copy of the register, and new value == old
0df8b418 775 value, then don't bother doing the actual store. */
ef79d9a3
YQ
776 if (get_register_status (regnum) == REG_VALID
777 && (memcmp (register_buffer (regnum), buf,
778 m_descr->sizeof_register[regnum]) == 0))
3fadccb3
AC
779 return;
780
ef79d9a3 781 target_prepare_to_store (this);
c8ec2f33 782 raw_supply (regnum, buf);
b94ade42 783
b292235f
TT
784 /* Invalidate the register after it is written, in case of a
785 failure. */
786 regcache_invalidator invalidator (this, regnum);
b94ade42 787
ef79d9a3 788 target_store_registers (this, regnum);
594f7785 789
b292235f
TT
790 /* The target did not throw an error so we can discard invalidating
791 the register. */
792 invalidator.release ();
61a0eb5b
AC
793}
794
68365089 795void
2d522557
AC
796regcache_cooked_write (struct regcache *regcache, int regnum,
797 const gdb_byte *buf)
ef79d9a3
YQ
798{
799 regcache->cooked_write (regnum, buf);
800}
801
802void
803regcache::cooked_write (int regnum, const gdb_byte *buf)
68365089 804{
d138e37a 805 gdb_assert (regnum >= 0);
ef79d9a3 806 gdb_assert (regnum < m_descr->nr_cooked_registers);
d999647b 807 if (regnum < num_raw_registers ())
ef79d9a3 808 raw_write (regnum, buf);
d138e37a 809 else
ef79d9a3 810 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
d8124050 811 regnum, buf);
61a0eb5b
AC
812}
813
06c0b04e
AC
814/* Perform a partial register transfer using a read, modify, write
815 operation. */
816
ef79d9a3 817enum register_status
849d0ba8
YQ
818readable_regcache::read_part (int regnum, int offset, int len, void *in,
819 bool is_raw)
820{
821 struct gdbarch *gdbarch = arch ();
822 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
823
824 gdb_assert (in != NULL);
825 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
826 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
827 /* Something to do? */
828 if (offset + len == 0)
829 return REG_VALID;
830 /* Read (when needed) ... */
831 enum register_status status;
832
833 if (is_raw)
834 status = raw_read (regnum, reg);
835 else
836 status = cooked_read (regnum, reg);
837 if (status != REG_VALID)
838 return status;
839
840 /* ... modify ... */
841 memcpy (in, reg + offset, len);
842
843 return REG_VALID;
844}
845
846enum register_status
847regcache::write_part (int regnum, int offset, int len,
d3037ba6 848 const void *out, bool is_raw)
ef79d9a3
YQ
849{
850 struct gdbarch *gdbarch = arch ();
9890e433 851 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
123f5f96 852
849d0ba8 853 gdb_assert (out != NULL);
ef79d9a3
YQ
854 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
855 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
06c0b04e
AC
856 /* Something to do? */
857 if (offset + len == 0)
05d1431c 858 return REG_VALID;
0df8b418 859 /* Read (when needed) ... */
849d0ba8 860 if (offset > 0
ef79d9a3 861 || offset + len < m_descr->sizeof_register[regnum])
06c0b04e 862 {
05d1431c
PA
863 enum register_status status;
864
d3037ba6
YQ
865 if (is_raw)
866 status = raw_read (regnum, reg);
867 else
868 status = cooked_read (regnum, reg);
05d1431c
PA
869 if (status != REG_VALID)
870 return status;
06c0b04e 871 }
849d0ba8
YQ
872
873 memcpy (reg + offset, out, len);
06c0b04e 874 /* ... write (when needed). */
849d0ba8
YQ
875 if (is_raw)
876 raw_write (regnum, reg);
877 else
878 cooked_write (regnum, reg);
05d1431c
PA
879
880 return REG_VALID;
06c0b04e
AC
881}
882
05d1431c 883enum register_status
06c0b04e 884regcache_raw_read_part (struct regcache *regcache, int regnum,
2d522557 885 int offset, int len, gdb_byte *buf)
06c0b04e 886{
ef79d9a3
YQ
887 return regcache->raw_read_part (regnum, offset, len, buf);
888}
123f5f96 889
ef79d9a3 890enum register_status
849d0ba8 891readable_regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
ef79d9a3 892{
4e888c28 893 assert_regnum (regnum);
849d0ba8 894 return read_part (regnum, offset, len, buf, true);
06c0b04e
AC
895}
896
897void
898regcache_raw_write_part (struct regcache *regcache, int regnum,
2d522557 899 int offset, int len, const gdb_byte *buf)
06c0b04e 900{
ef79d9a3
YQ
901 regcache->raw_write_part (regnum, offset, len, buf);
902}
123f5f96 903
ef79d9a3
YQ
904void
905regcache::raw_write_part (int regnum, int offset, int len,
906 const gdb_byte *buf)
907{
4e888c28 908 assert_regnum (regnum);
849d0ba8 909 write_part (regnum, offset, len, buf, true);
06c0b04e
AC
910}
911
05d1431c 912enum register_status
06c0b04e 913regcache_cooked_read_part (struct regcache *regcache, int regnum,
2d522557 914 int offset, int len, gdb_byte *buf)
06c0b04e 915{
ef79d9a3
YQ
916 return regcache->cooked_read_part (regnum, offset, len, buf);
917}
123f5f96 918
ef79d9a3
YQ
919
920enum register_status
849d0ba8
YQ
921readable_regcache::cooked_read_part (int regnum, int offset, int len,
922 gdb_byte *buf)
ef79d9a3
YQ
923{
924 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
849d0ba8 925 return read_part (regnum, offset, len, buf, false);
06c0b04e
AC
926}
927
928void
929regcache_cooked_write_part (struct regcache *regcache, int regnum,
2d522557 930 int offset, int len, const gdb_byte *buf)
06c0b04e 931{
ef79d9a3
YQ
932 regcache->cooked_write_part (regnum, offset, len, buf);
933}
123f5f96 934
ef79d9a3
YQ
935void
936regcache::cooked_write_part (int regnum, int offset, int len,
937 const gdb_byte *buf)
938{
939 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
849d0ba8 940 write_part (regnum, offset, len, buf, false);
06c0b04e 941}
32178cab 942
a16d75cc 943/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
9a661b68
MK
944
945void
6618125d 946regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
ef79d9a3
YQ
947{
948 gdb_assert (regcache != NULL);
949 regcache->raw_supply (regnum, buf);
950}
951
952void
c8ec2f33 953detached_regcache::raw_supply (int regnum, const void *buf)
9a661b68
MK
954{
955 void *regbuf;
956 size_t size;
957
4e888c28 958 assert_regnum (regnum);
9a661b68 959
ef79d9a3
YQ
960 regbuf = register_buffer (regnum);
961 size = m_descr->sizeof_register[regnum];
9a661b68
MK
962
963 if (buf)
ee99023e
PA
964 {
965 memcpy (regbuf, buf, size);
ef79d9a3 966 m_register_status[regnum] = REG_VALID;
ee99023e 967 }
9a661b68 968 else
ee99023e
PA
969 {
970 /* This memset not strictly necessary, but better than garbage
971 in case the register value manages to escape somewhere (due
972 to a bug, no less). */
973 memset (regbuf, 0, size);
ef79d9a3 974 m_register_status[regnum] = REG_UNAVAILABLE;
ee99023e 975 }
9a661b68
MK
976}
977
b057297a
AH
978/* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
979 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
980 the register size is greater than ADDR_LEN, then the integer will be sign or
981 zero extended. If the register size is smaller than the integer, then the
982 most significant bytes of the integer will be truncated. */
983
984void
796bb026
YQ
985detached_regcache::raw_supply_integer (int regnum, const gdb_byte *addr,
986 int addr_len, bool is_signed)
b057297a
AH
987{
988 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
989 gdb_byte *regbuf;
990 size_t regsize;
991
4e888c28 992 assert_regnum (regnum);
b057297a
AH
993
994 regbuf = register_buffer (regnum);
995 regsize = m_descr->sizeof_register[regnum];
996
997 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
998 byte_order);
999 m_register_status[regnum] = REG_VALID;
1000}
1001
f81fdd35
AH
1002/* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1003 as calling raw_supply with NULL (which will set the state to
1004 unavailable). */
1005
1006void
796bb026 1007detached_regcache::raw_supply_zeroed (int regnum)
f81fdd35
AH
1008{
1009 void *regbuf;
1010 size_t size;
1011
4e888c28 1012 assert_regnum (regnum);
f81fdd35
AH
1013
1014 regbuf = register_buffer (regnum);
1015 size = m_descr->sizeof_register[regnum];
1016
1017 memset (regbuf, 0, size);
1018 m_register_status[regnum] = REG_VALID;
1019}
1020
9a661b68
MK
1021/* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1022
1023void
6618125d 1024regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
ef79d9a3
YQ
1025{
1026 gdb_assert (regcache != NULL && buf != NULL);
1027 regcache->raw_collect (regnum, buf);
1028}
1029
1030void
1031regcache::raw_collect (int regnum, void *buf) const
9a661b68
MK
1032{
1033 const void *regbuf;
1034 size_t size;
1035
ef79d9a3 1036 gdb_assert (buf != NULL);
4e888c28 1037 assert_regnum (regnum);
9a661b68 1038
ef79d9a3
YQ
1039 regbuf = register_buffer (regnum);
1040 size = m_descr->sizeof_register[regnum];
9a661b68
MK
1041 memcpy (buf, regbuf, size);
1042}
1043
0b309272
AA
1044/* Transfer a single or all registers belonging to a certain register
1045 set to or from a buffer. This is the main worker function for
1046 regcache_supply_regset and regcache_collect_regset. */
1047
b057297a
AH
1048/* Collect register REGNUM from REGCACHE. Store collected value as an integer
1049 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1050 If ADDR_LEN is greater than the register size, then the integer will be sign
1051 or zero extended. If ADDR_LEN is smaller than the register size, then the
1052 most significant bytes of the integer will be truncated. */
1053
1054void
1055regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1056 bool is_signed) const
1057{
1058 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1059 const gdb_byte *regbuf;
1060 size_t regsize;
1061
4e888c28 1062 assert_regnum (regnum);
b057297a
AH
1063
1064 regbuf = register_buffer (regnum);
1065 regsize = m_descr->sizeof_register[regnum];
1066
1067 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1068 byte_order);
1069}
1070
ef79d9a3
YQ
1071void
1072regcache::transfer_regset (const struct regset *regset,
1073 struct regcache *out_regcache,
1074 int regnum, const void *in_buf,
1075 void *out_buf, size_t size) const
0b309272
AA
1076{
1077 const struct regcache_map_entry *map;
1078 int offs = 0, count;
1079
19ba03f4
SM
1080 for (map = (const struct regcache_map_entry *) regset->regmap;
1081 (count = map->count) != 0;
1082 map++)
0b309272
AA
1083 {
1084 int regno = map->regno;
1085 int slot_size = map->size;
1086
1087 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
ef79d9a3 1088 slot_size = m_descr->sizeof_register[regno];
0b309272
AA
1089
1090 if (regno == REGCACHE_MAP_SKIP
1091 || (regnum != -1
1092 && (regnum < regno || regnum >= regno + count)))
1093 offs += count * slot_size;
1094
1095 else if (regnum == -1)
1096 for (; count--; regno++, offs += slot_size)
1097 {
1098 if (offs + slot_size > size)
1099 break;
1100
1101 if (out_buf)
ef79d9a3 1102 raw_collect (regno, (gdb_byte *) out_buf + offs);
0b309272 1103 else
ef79d9a3
YQ
1104 out_regcache->raw_supply (regno, in_buf
1105 ? (const gdb_byte *) in_buf + offs
1106 : NULL);
0b309272
AA
1107 }
1108 else
1109 {
1110 /* Transfer a single register and return. */
1111 offs += (regnum - regno) * slot_size;
1112 if (offs + slot_size > size)
1113 return;
1114
1115 if (out_buf)
ef79d9a3 1116 raw_collect (regnum, (gdb_byte *) out_buf + offs);
0b309272 1117 else
ef79d9a3
YQ
1118 out_regcache->raw_supply (regnum, in_buf
1119 ? (const gdb_byte *) in_buf + offs
1120 : NULL);
0b309272
AA
1121 return;
1122 }
1123 }
1124}
1125
1126/* Supply register REGNUM from BUF to REGCACHE, using the register map
1127 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1128 If BUF is NULL, set the register(s) to "unavailable" status. */
1129
1130void
1131regcache_supply_regset (const struct regset *regset,
1132 struct regcache *regcache,
1133 int regnum, const void *buf, size_t size)
1134{
ef79d9a3
YQ
1135 regcache->supply_regset (regset, regnum, buf, size);
1136}
1137
1138void
1139regcache::supply_regset (const struct regset *regset,
1140 int regnum, const void *buf, size_t size)
1141{
1142 transfer_regset (regset, this, regnum, buf, NULL, size);
0b309272
AA
1143}
1144
1145/* Collect register REGNUM from REGCACHE to BUF, using the register
1146 map in REGSET. If REGNUM is -1, do this for all registers in
1147 REGSET. */
1148
1149void
1150regcache_collect_regset (const struct regset *regset,
1151 const struct regcache *regcache,
1152 int regnum, void *buf, size_t size)
1153{
ef79d9a3
YQ
1154 regcache->collect_regset (regset, regnum, buf, size);
1155}
1156
1157void
1158regcache::collect_regset (const struct regset *regset,
1159 int regnum, void *buf, size_t size) const
1160{
1161 transfer_regset (regset, NULL, regnum, NULL, buf, size);
0b309272
AA
1162}
1163
193cb69f 1164
515630c5 1165/* Special handling for register PC. */
32178cab
MS
1166
1167CORE_ADDR
515630c5 1168regcache_read_pc (struct regcache *regcache)
32178cab 1169{
ac7936df 1170 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 1171
32178cab
MS
1172 CORE_ADDR pc_val;
1173
61a1198a
UW
1174 if (gdbarch_read_pc_p (gdbarch))
1175 pc_val = gdbarch_read_pc (gdbarch, regcache);
cde9ea48 1176 /* Else use per-frame method on get_current_frame. */
214e098a 1177 else if (gdbarch_pc_regnum (gdbarch) >= 0)
cde9ea48 1178 {
61a1198a 1179 ULONGEST raw_val;
123f5f96 1180
05d1431c
PA
1181 if (regcache_cooked_read_unsigned (regcache,
1182 gdbarch_pc_regnum (gdbarch),
1183 &raw_val) == REG_UNAVAILABLE)
1184 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1185
214e098a 1186 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
cde9ea48
AC
1187 }
1188 else
515630c5
UW
1189 internal_error (__FILE__, __LINE__,
1190 _("regcache_read_pc: Unable to find PC"));
32178cab
MS
1191 return pc_val;
1192}
1193
32178cab 1194void
515630c5 1195regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
32178cab 1196{
ac7936df 1197 struct gdbarch *gdbarch = regcache->arch ();
61a1198a 1198
61a1198a
UW
1199 if (gdbarch_write_pc_p (gdbarch))
1200 gdbarch_write_pc (gdbarch, regcache, pc);
214e098a 1201 else if (gdbarch_pc_regnum (gdbarch) >= 0)
3e8c568d 1202 regcache_cooked_write_unsigned (regcache,
214e098a 1203 gdbarch_pc_regnum (gdbarch), pc);
61a1198a
UW
1204 else
1205 internal_error (__FILE__, __LINE__,
515630c5 1206 _("regcache_write_pc: Unable to update PC"));
edb3359d
DJ
1207
1208 /* Writing the PC (for instance, from "load") invalidates the
1209 current frame. */
1210 reinit_frame_cache ();
32178cab
MS
1211}
1212
d999647b 1213int
31716595 1214reg_buffer::num_raw_registers () const
d999647b
YQ
1215{
1216 return gdbarch_num_regs (arch ());
1217}
1218
ed771251 1219void
ef79d9a3 1220regcache::debug_print_register (const char *func, int regno)
ed771251 1221{
ef79d9a3 1222 struct gdbarch *gdbarch = arch ();
ed771251
AH
1223
1224 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1225 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1226 && gdbarch_register_name (gdbarch, regno) != NULL
1227 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1228 fprintf_unfiltered (gdb_stdlog, "(%s)",
1229 gdbarch_register_name (gdbarch, regno));
1230 else
1231 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1232 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1233 {
1234 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1235 int size = register_size (gdbarch, regno);
ef79d9a3 1236 gdb_byte *buf = register_buffer (regno);
ed771251
AH
1237
1238 fprintf_unfiltered (gdb_stdlog, " = ");
1239 for (int i = 0; i < size; i++)
1240 {
1241 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1242 }
1243 if (size <= sizeof (LONGEST))
1244 {
1245 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1246
1247 fprintf_unfiltered (gdb_stdlog, " %s %s",
1248 core_addr_to_string_nz (val), plongest (val));
1249 }
1250 }
1251 fprintf_unfiltered (gdb_stdlog, "\n");
1252}
32178cab 1253
705152c5 1254static void
0b39b52e 1255reg_flush_command (const char *command, int from_tty)
705152c5
MS
1256{
1257 /* Force-flush the register cache. */
1258 registers_changed ();
1259 if (from_tty)
a3f17187 1260 printf_filtered (_("Register cache flushed.\n"));
705152c5
MS
1261}
1262
4c74fe6b
YQ
1263void
1264register_dump::dump (ui_file *file)
af030b9a 1265{
4c74fe6b
YQ
1266 auto descr = regcache_descr (m_gdbarch);
1267 int regnum;
1268 int footnote_nr = 0;
1269 int footnote_register_offset = 0;
1270 int footnote_register_type_name_null = 0;
1271 long register_offset = 0;
af030b9a 1272
4c74fe6b
YQ
1273 gdb_assert (descr->nr_cooked_registers
1274 == (gdbarch_num_regs (m_gdbarch)
1275 + gdbarch_num_pseudo_regs (m_gdbarch)));
af030b9a 1276
4c74fe6b
YQ
1277 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1278 {
1279 /* Name. */
1280 if (regnum < 0)
1281 fprintf_unfiltered (file, " %-10s", "Name");
1282 else
1283 {
1284 const char *p = gdbarch_register_name (m_gdbarch, regnum);
123f5f96 1285
4c74fe6b
YQ
1286 if (p == NULL)
1287 p = "";
1288 else if (p[0] == '\0')
1289 p = "''";
1290 fprintf_unfiltered (file, " %-10s", p);
1291 }
af030b9a 1292
4c74fe6b
YQ
1293 /* Number. */
1294 if (regnum < 0)
1295 fprintf_unfiltered (file, " %4s", "Nr");
1296 else
1297 fprintf_unfiltered (file, " %4d", regnum);
af030b9a 1298
4c74fe6b
YQ
1299 /* Relative number. */
1300 if (regnum < 0)
1301 fprintf_unfiltered (file, " %4s", "Rel");
1302 else if (regnum < gdbarch_num_regs (m_gdbarch))
1303 fprintf_unfiltered (file, " %4d", regnum);
1304 else
1305 fprintf_unfiltered (file, " %4d",
1306 (regnum - gdbarch_num_regs (m_gdbarch)));
af030b9a 1307
4c74fe6b
YQ
1308 /* Offset. */
1309 if (regnum < 0)
1310 fprintf_unfiltered (file, " %6s ", "Offset");
1311 else
af030b9a 1312 {
4c74fe6b
YQ
1313 fprintf_unfiltered (file, " %6ld",
1314 descr->register_offset[regnum]);
1315 if (register_offset != descr->register_offset[regnum]
1316 || (regnum > 0
1317 && (descr->register_offset[regnum]
1318 != (descr->register_offset[regnum - 1]
1319 + descr->sizeof_register[regnum - 1])))
1320 )
af030b9a 1321 {
4c74fe6b
YQ
1322 if (!footnote_register_offset)
1323 footnote_register_offset = ++footnote_nr;
1324 fprintf_unfiltered (file, "*%d", footnote_register_offset);
af030b9a 1325 }
4c74fe6b
YQ
1326 else
1327 fprintf_unfiltered (file, " ");
1328 register_offset = (descr->register_offset[regnum]
1329 + descr->sizeof_register[regnum]);
af030b9a
AC
1330 }
1331
4c74fe6b
YQ
1332 /* Size. */
1333 if (regnum < 0)
1334 fprintf_unfiltered (file, " %5s ", "Size");
1335 else
1336 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
f3384e66 1337
4c74fe6b 1338 /* Type. */
f3384e66 1339 {
4c74fe6b
YQ
1340 const char *t;
1341 std::string name_holder;
b59ff9d5 1342
4c74fe6b
YQ
1343 if (regnum < 0)
1344 t = "Type";
215c69dc
YQ
1345 else
1346 {
4c74fe6b 1347 static const char blt[] = "builtin_type";
123f5f96 1348
4c74fe6b
YQ
1349 t = TYPE_NAME (register_type (m_gdbarch, regnum));
1350 if (t == NULL)
f3384e66 1351 {
4c74fe6b
YQ
1352 if (!footnote_register_type_name_null)
1353 footnote_register_type_name_null = ++footnote_nr;
1354 name_holder = string_printf ("*%d",
1355 footnote_register_type_name_null);
1356 t = name_holder.c_str ();
f3384e66 1357 }
4c74fe6b
YQ
1358 /* Chop a leading builtin_type. */
1359 if (startswith (t, blt))
1360 t += strlen (blt);
f3384e66 1361 }
4c74fe6b 1362 fprintf_unfiltered (file, " %-15s", t);
f3384e66 1363 }
f3384e66 1364
4c74fe6b
YQ
1365 /* Leading space always present. */
1366 fprintf_unfiltered (file, " ");
af030b9a 1367
4c74fe6b 1368 dump_reg (file, regnum);
ed4227b7 1369
4c74fe6b 1370 fprintf_unfiltered (file, "\n");
ed4227b7
PA
1371 }
1372
4c74fe6b
YQ
1373 if (footnote_register_offset)
1374 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1375 footnote_register_offset);
1376 if (footnote_register_type_name_null)
1377 fprintf_unfiltered (file,
1378 "*%d: Register type's name NULL.\n",
1379 footnote_register_type_name_null);
c21236dc
PA
1380}
1381
8248946c
YQ
1382#if GDB_SELF_TEST
1383#include "selftest.h"
1b30aaa5
YQ
1384#include "selftest-arch.h"
1385#include "gdbthread.h"
ec7a5fcb 1386#include "target-float.h"
8248946c
YQ
1387
1388namespace selftests {
1389
e521e87e 1390class regcache_access : public regcache
8248946c 1391{
e521e87e
YQ
1392public:
1393
1394 /* Return the number of elements in current_regcache. */
1395
1396 static size_t
1397 current_regcache_size ()
1398 {
1399 return std::distance (regcache::current_regcache.begin (),
1400 regcache::current_regcache.end ());
1401 }
1402};
8248946c
YQ
1403
1404static void
1405current_regcache_test (void)
1406{
1407 /* It is empty at the start. */
e521e87e 1408 SELF_CHECK (regcache_access::current_regcache_size () == 0);
8248946c
YQ
1409
1410 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1411
1412 /* Get regcache from ptid1, a new regcache is added to
1413 current_regcache. */
1414 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1415 target_gdbarch (),
1416 NULL);
1417
1418 SELF_CHECK (regcache != NULL);
1419 SELF_CHECK (regcache->ptid () == ptid1);
e521e87e 1420 SELF_CHECK (regcache_access::current_regcache_size () == 1);
8248946c
YQ
1421
1422 /* Get regcache from ptid2, a new regcache is added to
1423 current_regcache. */
1424 regcache = get_thread_arch_aspace_regcache (ptid2,
1425 target_gdbarch (),
1426 NULL);
1427 SELF_CHECK (regcache != NULL);
1428 SELF_CHECK (regcache->ptid () == ptid2);
e521e87e 1429 SELF_CHECK (regcache_access::current_regcache_size () == 2);
8248946c
YQ
1430
1431 /* Get regcache from ptid3, a new regcache is added to
1432 current_regcache. */
1433 regcache = get_thread_arch_aspace_regcache (ptid3,
1434 target_gdbarch (),
1435 NULL);
1436 SELF_CHECK (regcache != NULL);
1437 SELF_CHECK (regcache->ptid () == ptid3);
e521e87e 1438 SELF_CHECK (regcache_access::current_regcache_size () == 3);
8248946c
YQ
1439
1440 /* Get regcache from ptid2 again, nothing is added to
1441 current_regcache. */
1442 regcache = get_thread_arch_aspace_regcache (ptid2,
1443 target_gdbarch (),
1444 NULL);
1445 SELF_CHECK (regcache != NULL);
1446 SELF_CHECK (regcache->ptid () == ptid2);
e521e87e 1447 SELF_CHECK (regcache_access::current_regcache_size () == 3);
8248946c
YQ
1448
1449 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1450 current_regcache. */
1451 registers_changed_ptid (ptid2);
e521e87e 1452 SELF_CHECK (regcache_access::current_regcache_size () == 2);
8248946c
YQ
1453}
1454
1b30aaa5
YQ
1455class target_ops_no_register : public test_target_ops
1456{
1457public:
1458 target_ops_no_register ()
1459 : test_target_ops {}
f6ac5f3d 1460 {}
1b30aaa5
YQ
1461
1462 void reset ()
1463 {
1464 fetch_registers_called = 0;
1465 store_registers_called = 0;
1466 xfer_partial_called = 0;
1467 }
1468
f6ac5f3d
PA
1469 void fetch_registers (regcache *regs, int regno) override;
1470 void store_registers (regcache *regs, int regno) override;
1471
1472 enum target_xfer_status xfer_partial (enum target_object object,
1473 const char *annex, gdb_byte *readbuf,
1474 const gdb_byte *writebuf,
1475 ULONGEST offset, ULONGEST len,
1476 ULONGEST *xfered_len) override;
1477
1b30aaa5
YQ
1478 unsigned int fetch_registers_called = 0;
1479 unsigned int store_registers_called = 0;
1480 unsigned int xfer_partial_called = 0;
1481};
1482
f6ac5f3d
PA
1483void
1484target_ops_no_register::fetch_registers (regcache *regs, int regno)
1b30aaa5 1485{
1b30aaa5
YQ
1486 /* Mark register available. */
1487 regs->raw_supply_zeroed (regno);
f6ac5f3d 1488 this->fetch_registers_called++;
1b30aaa5
YQ
1489}
1490
f6ac5f3d
PA
1491void
1492target_ops_no_register::store_registers (regcache *regs, int regno)
1b30aaa5 1493{
f6ac5f3d 1494 this->store_registers_called++;
1b30aaa5
YQ
1495}
1496
f6ac5f3d
PA
1497enum target_xfer_status
1498target_ops_no_register::xfer_partial (enum target_object object,
1499 const char *annex, gdb_byte *readbuf,
1500 const gdb_byte *writebuf,
1501 ULONGEST offset, ULONGEST len,
1502 ULONGEST *xfered_len)
1b30aaa5 1503{
f6ac5f3d 1504 this->xfer_partial_called++;
1b30aaa5
YQ
1505
1506 *xfered_len = len;
1507 return TARGET_XFER_OK;
1508}
1509
1510class readwrite_regcache : public regcache
1511{
1512public:
1513 readwrite_regcache (struct gdbarch *gdbarch)
796bb026 1514 : regcache (gdbarch, nullptr)
1b30aaa5
YQ
1515 {}
1516};
1517
1518/* Test regcache::cooked_read gets registers from raw registers and
1519 memory instead of target to_{fetch,store}_registers. */
1520
1521static void
1522cooked_read_test (struct gdbarch *gdbarch)
1523{
1524 /* Error out if debugging something, because we're going to push the
1525 test target, which would pop any existing target. */
f6ac5f3d 1526 if (target_stack->to_stratum >= process_stratum)
1b30aaa5
YQ
1527 error (_("target already pushed"));
1528
1529 /* Create a mock environment. An inferior with a thread, with a
1530 process_stratum target pushed. */
1531
1532 target_ops_no_register mock_target;
1533 ptid_t mock_ptid (1, 1);
1534 inferior mock_inferior (mock_ptid.pid ());
1535 address_space mock_aspace {};
1536 mock_inferior.gdbarch = gdbarch;
1537 mock_inferior.aspace = &mock_aspace;
1538 thread_info mock_thread (&mock_inferior, mock_ptid);
1539
1540 scoped_restore restore_thread_list
1541 = make_scoped_restore (&thread_list, &mock_thread);
1542
1543 /* Add the mock inferior to the inferior list so that look ups by
1544 target+ptid can find it. */
1545 scoped_restore restore_inferior_list
1546 = make_scoped_restore (&inferior_list);
1547 inferior_list = &mock_inferior;
1548
1549 /* Switch to the mock inferior. */
1550 scoped_restore_current_inferior restore_current_inferior;
1551 set_current_inferior (&mock_inferior);
1552
1553 /* Push the process_stratum target so we can mock accessing
1554 registers. */
1555 push_target (&mock_target);
1556
1557 /* Pop it again on exit (return/exception). */
1558 struct on_exit
1559 {
1560 ~on_exit ()
1561 {
1562 pop_all_targets_at_and_above (process_stratum);
1563 }
1564 } pop_targets;
1565
1566 /* Switch to the mock thread. */
1567 scoped_restore restore_inferior_ptid
1568 = make_scoped_restore (&inferior_ptid, mock_ptid);
1569
1570 /* Test that read one raw register from regcache_no_target will go
1571 to the target layer. */
1572 int regnum;
1573
1574 /* Find a raw register which size isn't zero. */
1575 for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1576 {
1577 if (register_size (gdbarch, regnum) != 0)
1578 break;
1579 }
1580
1581 readwrite_regcache readwrite (gdbarch);
1582 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1583
1584 readwrite.raw_read (regnum, buf.data ());
1585
1586 /* raw_read calls target_fetch_registers. */
1587 SELF_CHECK (mock_target.fetch_registers_called > 0);
1588 mock_target.reset ();
1589
1590 /* Mark all raw registers valid, so the following raw registers
1591 accesses won't go to target. */
1592 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1593 readwrite.raw_update (i);
1594
1595 mock_target.reset ();
1596 /* Then, read all raw and pseudo registers, and don't expect calling
1597 to_{fetch,store}_registers. */
1598 for (int regnum = 0;
1599 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1600 regnum++)
1601 {
1602 if (register_size (gdbarch, regnum) == 0)
1603 continue;
1604
1605 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1606
1607 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1608
dc711524
YQ
1609 SELF_CHECK (mock_target.fetch_registers_called == 0);
1610 SELF_CHECK (mock_target.store_registers_called == 0);
1b30aaa5
YQ
1611
1612 /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1613 if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1614 SELF_CHECK (mock_target.xfer_partial_called == 0);
1615
1616 mock_target.reset ();
1617 }
a63f2d2f 1618
215c69dc 1619 readonly_detached_regcache readonly (readwrite);
a63f2d2f
YQ
1620
1621 /* GDB may go to target layer to fetch all registers and memory for
1622 readonly regcache. */
1623 mock_target.reset ();
1624
1625 for (int regnum = 0;
1626 regnum < gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
1627 regnum++)
1628 {
a63f2d2f
YQ
1629 if (register_size (gdbarch, regnum) == 0)
1630 continue;
1631
1632 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, regnum));
1633 enum register_status status = readonly.cooked_read (regnum,
1634 buf.data ());
1635
1636 if (regnum < gdbarch_num_regs (gdbarch))
1637 {
1638 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1639
1640 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1641 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1642 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1643 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1644 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1645 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
ea005f31
AB
1646 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1647 || bfd_arch == bfd_arch_riscv)
a63f2d2f
YQ
1648 {
1649 /* Raw registers. If raw registers are not in save_reggroup,
1650 their status are unknown. */
1651 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1652 SELF_CHECK (status == REG_VALID);
1653 else
1654 SELF_CHECK (status == REG_UNKNOWN);
1655 }
1656 else
1657 SELF_CHECK (status == REG_VALID);
1658 }
1659 else
1660 {
1661 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1662 SELF_CHECK (status == REG_VALID);
1663 else
1664 {
1665 /* If pseudo registers are not in save_reggroup, some of
1666 them can be computed from saved raw registers, but some
1667 of them are unknown. */
1668 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1669
1670 if (bfd_arch == bfd_arch_frv
1671 || bfd_arch == bfd_arch_m32c
1672 || bfd_arch == bfd_arch_mep
1673 || bfd_arch == bfd_arch_sh)
1674 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1675 else if (bfd_arch == bfd_arch_mips
1676 || bfd_arch == bfd_arch_h8300)
1677 SELF_CHECK (status == REG_UNKNOWN);
1678 else
1679 SELF_CHECK (status == REG_VALID);
1680 }
1681 }
1682
1683 SELF_CHECK (mock_target.fetch_registers_called == 0);
1684 SELF_CHECK (mock_target.store_registers_called == 0);
1685 SELF_CHECK (mock_target.xfer_partial_called == 0);
1686
1687 mock_target.reset ();
1688 }
1b30aaa5
YQ
1689}
1690
ec7a5fcb
YQ
1691/* Test regcache::cooked_write by writing some expected contents to
1692 registers, and checking that contents read from registers and the
1693 expected contents are the same. */
1694
1695static void
1696cooked_write_test (struct gdbarch *gdbarch)
1697{
1698 /* Error out if debugging something, because we're going to push the
1699 test target, which would pop any existing target. */
f6ac5f3d 1700 if (target_stack->to_stratum >= process_stratum)
ec7a5fcb
YQ
1701 error (_("target already pushed"));
1702
1703 /* Create a mock environment. A process_stratum target pushed. */
1704
1705 target_ops_no_register mock_target;
1706
1707 /* Push the process_stratum target so we can mock accessing
1708 registers. */
1709 push_target (&mock_target);
1710
1711 /* Pop it again on exit (return/exception). */
1712 struct on_exit
1713 {
1714 ~on_exit ()
1715 {
1716 pop_all_targets_at_and_above (process_stratum);
1717 }
1718 } pop_targets;
1719
1720 readwrite_regcache readwrite (gdbarch);
1721
1722 const int num_regs = (gdbarch_num_regs (gdbarch)
1723 + gdbarch_num_pseudo_regs (gdbarch));
1724
1725 for (auto regnum = 0; regnum < num_regs; regnum++)
1726 {
1727 if (register_size (gdbarch, regnum) == 0
1728 || gdbarch_cannot_store_register (gdbarch, regnum))
1729 continue;
1730
1731 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1732
1733 if ((bfd_arch == bfd_arch_sparc
1734 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1735 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1736 && gdbarch_ptr_bit (gdbarch) == 64
1737 && (regnum >= gdbarch_num_regs (gdbarch)
1738 && regnum <= gdbarch_num_regs (gdbarch) + 4))
ec7a5fcb
YQ
1739 || (bfd_arch == bfd_arch_spu
1740 /* SPU pseudo registers except SPU_SP_REGNUM are got by
1741 TARGET_OBJECT_SPU. */
1742 && regnum >= gdbarch_num_regs (gdbarch) && regnum != 130))
1743 continue;
1744
1745 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1746 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1747 const auto type = register_type (gdbarch, regnum);
1748
1749 if (TYPE_CODE (type) == TYPE_CODE_FLT
1750 || TYPE_CODE (type) == TYPE_CODE_DECFLOAT)
1751 {
1752 /* Generate valid float format. */
1753 target_float_from_string (expected.data (), type, "1.25");
1754 }
1755 else if (TYPE_CODE (type) == TYPE_CODE_INT
1756 || TYPE_CODE (type) == TYPE_CODE_ARRAY
1757 || TYPE_CODE (type) == TYPE_CODE_PTR
1758 || TYPE_CODE (type) == TYPE_CODE_UNION
1759 || TYPE_CODE (type) == TYPE_CODE_STRUCT)
1760 {
1761 if (bfd_arch == bfd_arch_ia64
1762 || (regnum >= gdbarch_num_regs (gdbarch)
1763 && (bfd_arch == bfd_arch_xtensa
1764 || bfd_arch == bfd_arch_bfin
1765 || bfd_arch == bfd_arch_m32c
1766 /* m68hc11 pseudo registers are in memory. */
1767 || bfd_arch == bfd_arch_m68hc11
1768 || bfd_arch == bfd_arch_m68hc12
1769 || bfd_arch == bfd_arch_s390))
1770 || (bfd_arch == bfd_arch_frv
1771 /* FRV pseudo registers except iacc0. */
1772 && regnum > gdbarch_num_regs (gdbarch)))
1773 {
1774 /* Skip setting the expected values for some architecture
1775 registers. */
1776 }
1777 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1778 {
1779 /* RL78_PC_REGNUM */
1780 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1781 expected[j] = j;
1782 }
1783 else
1784 {
1785 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1786 expected[j] = j;
1787 }
1788 }
1789 else if (TYPE_CODE (type) == TYPE_CODE_FLAGS)
1790 {
1791 /* No idea how to test flags. */
1792 continue;
1793 }
1794 else
1795 {
1796 /* If we don't know how to create the expected value for the
1797 this type, make it fail. */
1798 SELF_CHECK (0);
1799 }
1800
1801 readwrite.cooked_write (regnum, expected.data ());
1802
1803 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1804 SELF_CHECK (expected == buf);
1805 }
1806}
1807
8248946c
YQ
1808} // namespace selftests
1809#endif /* GDB_SELF_TEST */
1810
32178cab
MS
1811void
1812_initialize_regcache (void)
1813{
3e43a32a
MS
1814 regcache_descr_handle
1815 = gdbarch_data_register_post_init (init_regcache_descr);
705152c5 1816
76727919
TT
1817 gdb::observers::target_changed.attach (regcache_observer_target_changed);
1818 gdb::observers::thread_ptid_changed.attach
1819 (regcache::regcache_thread_ptid_changed);
f4c5303c 1820
705152c5 1821 add_com ("flushregs", class_maintenance, reg_flush_command,
1bedd215 1822 _("Force gdb to flush its register cache (maintainer command)"));
39f77062 1823
8248946c 1824#if GDB_SELF_TEST
1526853e 1825 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1b30aaa5
YQ
1826
1827 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1828 selftests::cooked_read_test);
ec7a5fcb
YQ
1829 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1830 selftests::cooked_write_test);
8248946c 1831#endif
32178cab 1832}
This page took 1.828845 seconds and 4 git commands to generate.