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