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