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