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