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