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