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