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