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