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