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