* dwarf2read.c (dwarf2_ranges_read): Skip empty range entries.
[deliverable/binutils-gdb.git] / gdb / regcache.c
CommitLineData
32178cab 1/* Cache and manage the values of registers for GDB, the GNU debugger.
3fadccb3 2
6aba47ca 3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
7b6bb8da 4 2002, 2004, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
32178cab
MS
5
6 This file is part of GDB.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
a9762ec7 10 the Free Software Foundation; either version 3 of the License, or
32178cab
MS
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
a9762ec7 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
32178cab
MS
20
21#include "defs.h"
32178cab
MS
22#include "inferior.h"
23#include "target.h"
24#include "gdbarch.h"
705152c5 25#include "gdbcmd.h"
4e052eda 26#include "regcache.h"
b59ff9d5 27#include "reggroups.h"
61a0eb5b 28#include "gdb_assert.h"
b66d6d2e 29#include "gdb_string.h"
af030b9a 30#include "gdbcmd.h" /* For maintenanceprintlist. */
f4c5303c 31#include "observer.h"
32178cab
MS
32
33/*
34 * DATA STRUCTURE
35 *
36 * Here is the actual register cache.
37 */
38
3fadccb3 39/* Per-architecture object describing the layout of a register cache.
0df8b418 40 Computed once when the architecture is created. */
3fadccb3
AC
41
42struct gdbarch_data *regcache_descr_handle;
43
44struct regcache_descr
45{
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
48
bb1db049
AC
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
d2f0b918 52 registers then those registers and not the PC lives in the raw
bb1db049 53 cache. */
3fadccb3
AC
54 int nr_raw_registers;
55 long sizeof_raw_registers;
ee99023e 56 long sizeof_raw_register_status;
3fadccb3 57
d138e37a
AC
58 /* The cooked register space. Each cooked register in the range
59 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
60 register. The remaining [NR_RAW_REGISTERS
02f60eae 61 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
d138e37a 62 both raw registers and memory by the architecture methods
02f60eae 63 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
d138e37a 64 int nr_cooked_registers;
067df2e5 65 long sizeof_cooked_registers;
ee99023e 66 long sizeof_cooked_register_status;
d138e37a
AC
67
68 /* Offset and size (in 8 bit bytes), of reach register in the
69 register cache. All registers (including those in the range
99e42fd8
PA
70 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
71 offset. */
3fadccb3 72 long *register_offset;
3fadccb3 73 long *sizeof_register;
3fadccb3 74
bb425013
AC
75 /* Cached table containing the type of each register. */
76 struct type **register_type;
3fadccb3
AC
77};
78
3fadccb3
AC
79static void *
80init_regcache_descr (struct gdbarch *gdbarch)
81{
82 int i;
83 struct regcache_descr *descr;
84 gdb_assert (gdbarch != NULL);
85
bb425013 86 /* Create an initial, zero filled, table. */
116f06ea 87 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
3fadccb3 88 descr->gdbarch = gdbarch;
3fadccb3 89
d138e37a
AC
90 /* Total size of the register space. The raw registers are mapped
91 directly onto the raw register cache while the pseudo's are
3fadccb3 92 either mapped onto raw-registers or memory. */
214e098a
UW
93 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
94 + gdbarch_num_pseudo_regs (gdbarch);
ee99023e
PA
95 descr->sizeof_cooked_register_status
96 = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
3fadccb3 97
bb425013 98 /* Fill in a table of register types. */
116f06ea 99 descr->register_type
3e43a32a
MS
100 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
101 struct type *);
bb425013 102 for (i = 0; i < descr->nr_cooked_registers; i++)
336a3131 103 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
bb425013 104
bb1db049
AC
105 /* Construct a strictly RAW register cache. Don't allow pseudo's
106 into the register cache. */
214e098a 107 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
ee99023e 108 descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
bb1db049 109
067df2e5 110 /* Lay out the register cache.
3fadccb3 111
bb425013
AC
112 NOTE: cagney/2002-05-22: Only register_type() is used when
113 constructing the register cache. It is assumed that the
114 register's raw size, virtual size and type length are all the
115 same. */
3fadccb3
AC
116
117 {
118 long offset = 0;
123f5f96 119
116f06ea
AC
120 descr->sizeof_register
121 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
122 descr->register_offset
123 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
99e42fd8
PA
124 for (i = 0; i < descr->nr_raw_registers; i++)
125 {
126 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
127 descr->register_offset[i] = offset;
128 offset += descr->sizeof_register[i];
129 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
130 }
131 /* Set the real size of the raw register cache buffer. */
132 descr->sizeof_raw_registers = offset;
133
134 for (; i < descr->nr_cooked_registers; i++)
3fadccb3 135 {
bb425013 136 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
3fadccb3
AC
137 descr->register_offset[i] = offset;
138 offset += descr->sizeof_register[i];
123a958e 139 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
3fadccb3 140 }
99e42fd8 141 /* Set the real size of the readonly register cache buffer. */
067df2e5 142 descr->sizeof_cooked_registers = offset;
3fadccb3
AC
143 }
144
3fadccb3
AC
145 return descr;
146}
147
148static struct regcache_descr *
149regcache_descr (struct gdbarch *gdbarch)
150{
151 return gdbarch_data (gdbarch, regcache_descr_handle);
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);
219 regcache = XMALLOC (struct regcache);
220 regcache->descr = descr;
99e42fd8
PA
221 regcache->readonly_p = readonly_p;
222 if (readonly_p)
223 {
224 regcache->registers
225 = XCALLOC (descr->sizeof_cooked_registers, gdb_byte);
ee99023e
PA
226 regcache->register_status
227 = XCALLOC (descr->sizeof_cooked_register_status, gdb_byte);
99e42fd8
PA
228 }
229 else
230 {
231 regcache->registers
232 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
ee99023e
PA
233 regcache->register_status
234 = XCALLOC (descr->sizeof_raw_register_status, gdb_byte);
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{
260 regcache_xfree (data);
261}
262
263struct cleanup *
264make_cleanup_regcache_xfree (struct regcache *regcache)
265{
266 return make_cleanup (do_regcache_xfree, regcache);
267}
268
41d35cb0
MK
269/* Return REGCACHE's architecture. */
270
271struct gdbarch *
272get_regcache_arch (const struct regcache *regcache)
273{
274 return regcache->descr->gdbarch;
275}
276
6c95b8df
PA
277struct address_space *
278get_regcache_aspace (const struct regcache *regcache)
279{
280 return regcache->aspace;
281}
282
51b1fe4e
AC
283/* Return a pointer to register REGNUM's buffer cache. */
284
2d522557 285static gdb_byte *
9a661b68 286register_buffer (const struct regcache *regcache, int regnum)
51b1fe4e
AC
287{
288 return regcache->registers + regcache->descr->register_offset[regnum];
289}
290
2d28509a 291void
5602984a
AC
292regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
293 void *src)
2d28509a
AC
294{
295 struct gdbarch *gdbarch = dst->descr->gdbarch;
2d522557 296 gdb_byte buf[MAX_REGISTER_SIZE];
2d28509a 297 int regnum;
123f5f96 298
2d28509a 299 /* The DST should be `read-only', if it wasn't then the save would
5602984a 300 end up trying to write the register values back out to the
2d28509a 301 target. */
2d28509a
AC
302 gdb_assert (dst->readonly_p);
303 /* Clear the dest. */
304 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
ee99023e
PA
305 memset (dst->register_status, 0,
306 dst->descr->sizeof_cooked_register_status);
2d28509a 307 /* Copy over any registers (identified by their membership in the
f57d151a
UW
308 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
309 gdbarch_num_pseudo_regs) range is checked since some architectures need
5602984a 310 to save/restore `cooked' registers that live in memory. */
2d28509a
AC
311 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
312 {
313 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
314 {
5602984a 315 int valid = cooked_read (src, regnum, buf);
123f5f96 316
5602984a
AC
317 if (valid)
318 {
319 memcpy (register_buffer (dst, regnum), buf,
320 register_size (gdbarch, regnum));
ee99023e 321 dst->register_status[regnum] = REG_VALID;
5602984a 322 }
2d28509a
AC
323 }
324 }
325}
326
327void
5602984a
AC
328regcache_restore (struct regcache *dst,
329 regcache_cooked_read_ftype *cooked_read,
2d522557 330 void *cooked_read_context)
2d28509a
AC
331{
332 struct gdbarch *gdbarch = dst->descr->gdbarch;
2d522557 333 gdb_byte buf[MAX_REGISTER_SIZE];
2d28509a 334 int regnum;
123f5f96 335
5602984a
AC
336 /* The dst had better not be read-only. If it is, the `restore'
337 doesn't make much sense. */
2d28509a 338 gdb_assert (!dst->readonly_p);
2d28509a 339 /* Copy over any registers, being careful to only restore those that
f57d151a
UW
340 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
341 + gdbarch_num_pseudo_regs) range is checked since some architectures need
5602984a
AC
342 to save/restore `cooked' registers that live in memory. */
343 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
2d28509a 344 {
5602984a 345 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
2d28509a 346 {
2d522557 347 int valid = cooked_read (cooked_read_context, regnum, buf);
123f5f96 348
5602984a
AC
349 if (valid)
350 regcache_cooked_write (dst, regnum, buf);
2d28509a
AC
351 }
352 }
353}
354
5602984a 355static int
2d522557 356do_cooked_read (void *src, int regnum, gdb_byte *buf)
5602984a
AC
357{
358 struct regcache *regcache = src;
123f5f96 359
ee99023e 360 if (regcache->register_status[regnum] == REG_UNKNOWN && regcache->readonly_p)
5602984a
AC
361 /* Don't even think about fetching a register from a read-only
362 cache when the register isn't yet valid. There isn't a target
363 from which the register value can be fetched. */
364 return 0;
365 regcache_cooked_read (regcache, regnum, buf);
366 return 1;
367}
368
369
3fadccb3
AC
370void
371regcache_cpy (struct regcache *dst, struct regcache *src)
372{
3fadccb3
AC
373 gdb_assert (src != NULL && dst != NULL);
374 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
375 gdb_assert (src != dst);
2d28509a 376 gdb_assert (src->readonly_p || dst->readonly_p);
6c95b8df 377
2d28509a 378 if (!src->readonly_p)
5602984a 379 regcache_save (dst, do_cooked_read, src);
2d28509a 380 else if (!dst->readonly_p)
5602984a 381 regcache_restore (dst, do_cooked_read, src);
2d28509a
AC
382 else
383 regcache_cpy_no_passthrough (dst, src);
3fadccb3
AC
384}
385
386void
387regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
388{
3fadccb3
AC
389 gdb_assert (src != NULL && dst != NULL);
390 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
391 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
ee99023e
PA
392 move of data into a thread's regcache. Doing this would be silly
393 - it would mean that regcache->register_status would be
394 completely invalid. */
99e42fd8 395 gdb_assert (dst->readonly_p && src->readonly_p);
6c95b8df 396
99e42fd8
PA
397 memcpy (dst->registers, src->registers,
398 dst->descr->sizeof_cooked_registers);
ee99023e
PA
399 memcpy (dst->register_status, src->register_status,
400 dst->descr->sizeof_cooked_register_status);
3fadccb3
AC
401}
402
403struct regcache *
404regcache_dup (struct regcache *src)
405{
406 struct regcache *newbuf;
123f5f96 407
d37346f0 408 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
3fadccb3
AC
409 regcache_cpy (newbuf, src);
410 return newbuf;
411}
412
3fadccb3 413int
ee99023e 414regcache_register_status (const struct regcache *regcache, int regnum)
3fadccb3
AC
415{
416 gdb_assert (regcache != NULL);
6ed7ea50
UW
417 gdb_assert (regnum >= 0);
418 if (regcache->readonly_p)
419 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
420 else
421 gdb_assert (regnum < regcache->descr->nr_raw_registers);
422
ee99023e 423 return regcache->register_status[regnum];
3fadccb3
AC
424}
425
9c5ea4d9
UW
426void
427regcache_invalidate (struct regcache *regcache, int regnum)
428{
429 gdb_assert (regcache != NULL);
430 gdb_assert (regnum >= 0);
431 gdb_assert (!regcache->readonly_p);
432 gdb_assert (regnum < regcache->descr->nr_raw_registers);
ee99023e 433 regcache->register_status[regnum] = REG_UNKNOWN;
9c5ea4d9
UW
434}
435
436
3fadccb3 437/* Global structure containing the current regcache. */
3fadccb3 438
5ebd2499 439/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
440 recording if the register values have been changed (eg. by the
441 user). Therefore all registers must be written back to the
442 target when appropriate. */
443
c2250ad1 444struct regcache_list
594f7785 445{
c2250ad1
UW
446 struct regcache *regcache;
447 struct regcache_list *next;
448};
449
450static struct regcache_list *current_regcache;
451
452struct regcache *
453get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
454{
455 struct regcache_list *list;
456 struct regcache *new_regcache;
594f7785 457
c2250ad1
UW
458 for (list = current_regcache; list; list = list->next)
459 if (ptid_equal (list->regcache->ptid, ptid)
460 && get_regcache_arch (list->regcache) == gdbarch)
461 return list->regcache;
594f7785 462
99e42fd8
PA
463 new_regcache = regcache_xmalloc_1 (gdbarch,
464 target_thread_address_space (ptid), 0);
c2250ad1 465 new_regcache->ptid = ptid;
6c95b8df 466 gdb_assert (new_regcache->aspace != NULL);
594f7785 467
c2250ad1
UW
468 list = xmalloc (sizeof (struct regcache_list));
469 list->regcache = new_regcache;
470 list->next = current_regcache;
471 current_regcache = list;
594f7785 472
c2250ad1 473 return new_regcache;
594f7785
UW
474}
475
c2250ad1
UW
476static ptid_t current_thread_ptid;
477static struct gdbarch *current_thread_arch;
478
479struct regcache *
480get_thread_regcache (ptid_t ptid)
481{
482 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
483 {
484 current_thread_ptid = ptid;
485 current_thread_arch = target_thread_architecture (ptid);
486 }
487
488 return get_thread_arch_regcache (ptid, current_thread_arch);
489}
490
491struct regcache *
492get_current_regcache (void)
594f7785
UW
493{
494 return get_thread_regcache (inferior_ptid);
495}
32178cab 496
32178cab 497
f4c5303c
OF
498/* Observer for the target_changed event. */
499
2c0b251b 500static void
f4c5303c
OF
501regcache_observer_target_changed (struct target_ops *target)
502{
503 registers_changed ();
504}
505
5231c1fd
PA
506/* Update global variables old ptids to hold NEW_PTID if they were
507 holding OLD_PTID. */
508static void
509regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
510{
c2250ad1
UW
511 struct regcache_list *list;
512
513 for (list = current_regcache; list; list = list->next)
514 if (ptid_equal (list->regcache->ptid, old_ptid))
515 list->regcache->ptid = new_ptid;
5231c1fd
PA
516}
517
32178cab
MS
518/* Low level examining and depositing of registers.
519
520 The caller is responsible for making sure that the inferior is
521 stopped before calling the fetching routines, or it will get
522 garbage. (a change from GDB version 3, in which the caller got the
523 value from the last stop). */
524
525/* REGISTERS_CHANGED ()
526
527 Indicate that registers may have changed, so invalidate the cache. */
528
529void
e66408ed 530registers_changed_ptid (ptid_t ptid)
32178cab 531{
e66408ed 532 struct regcache_list *list, **list_link;
041274d8 533 int wildcard = ptid_equal (ptid, minus_one_ptid);
c2250ad1 534
e66408ed
PA
535 list = current_regcache;
536 list_link = &current_regcache;
537 while (list)
c2250ad1 538 {
e66408ed
PA
539 if (ptid_match (list->regcache->ptid, ptid))
540 {
541 struct regcache_list *dead = list;
542
543 *list_link = list->next;
544 regcache_xfree (list->regcache);
545 list = *list_link;
546 xfree (dead);
547 continue;
548 }
549
550 list_link = &list->next;
551 list = *list_link;
c2250ad1 552 }
32178cab 553
041274d8
PA
554 if (wildcard || ptid_equal (ptid, current_thread_ptid))
555 {
556 current_thread_ptid = null_ptid;
557 current_thread_arch = NULL;
558 }
32178cab 559
041274d8
PA
560 if (wildcard || ptid_equal (ptid, inferior_ptid))
561 {
562 /* We just deleted the regcache of the current thread. Need to
563 forget about any frames we have cached, too. */
564 reinit_frame_cache ();
565 }
566}
c2250ad1 567
041274d8
PA
568void
569registers_changed (void)
570{
571 registers_changed_ptid (minus_one_ptid);
a5d9d57d 572
32178cab
MS
573 /* Force cleanup of any alloca areas if using C alloca instead of
574 a builtin alloca. This particular call is used to clean up
575 areas allocated by low level target code which may build up
576 during lengthy interactions between gdb and the target before
577 gdb gives control to the user (ie watchpoints). */
578 alloca (0);
32178cab
MS
579}
580
61a0eb5b 581void
2d522557 582regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
61a0eb5b 583{
3fadccb3
AC
584 gdb_assert (regcache != NULL && buf != NULL);
585 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
3fadccb3
AC
586 /* Make certain that the register cache is up-to-date with respect
587 to the current thread. This switching shouldn't be necessary
588 only there is still only one target side register cache. Sigh!
589 On the bright side, at least there is a regcache object. */
2d28509a 590 if (!regcache->readonly_p)
3fadccb3 591 {
ee99023e 592 if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
3fadccb3 593 {
594f7785 594 struct cleanup *old_chain = save_inferior_ptid ();
123f5f96 595
594f7785
UW
596 inferior_ptid = regcache->ptid;
597 target_fetch_registers (regcache, regnum);
598 do_cleanups (old_chain);
3fadccb3 599 }
0a8146bf
AC
600#if 0
601 /* FIXME: cagney/2004-08-07: At present a number of targets
04c663e3
DA
602 forget (or didn't know that they needed) to set this leading to
603 panics. Also is the problem that targets need to indicate
0a8146bf
AC
604 that a register is in one of the possible states: valid,
605 undefined, unknown. The last of which isn't yet
606 possible. */
ee99023e 607 gdb_assert (regcache_register_status (regcache, regnum) == REG_VALID);
0a8146bf 608#endif
3fadccb3
AC
609 }
610 /* Copy the value directly into the register cache. */
51b1fe4e 611 memcpy (buf, register_buffer (regcache, regnum),
3fadccb3 612 regcache->descr->sizeof_register[regnum]);
61a0eb5b
AC
613}
614
28fc6740
AC
615void
616regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
617{
2d522557 618 gdb_byte *buf;
123f5f96 619
28fc6740
AC
620 gdb_assert (regcache != NULL);
621 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
622 buf = alloca (regcache->descr->sizeof_register[regnum]);
623 regcache_raw_read (regcache, regnum, buf);
e17a4113
UW
624 (*val) = extract_signed_integer
625 (buf, regcache->descr->sizeof_register[regnum],
626 gdbarch_byte_order (regcache->descr->gdbarch));
28fc6740
AC
627}
628
629void
630regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
631 ULONGEST *val)
632{
2d522557 633 gdb_byte *buf;
123f5f96 634
28fc6740
AC
635 gdb_assert (regcache != NULL);
636 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
637 buf = alloca (regcache->descr->sizeof_register[regnum]);
638 regcache_raw_read (regcache, regnum, buf);
e17a4113
UW
639 (*val) = extract_unsigned_integer
640 (buf, regcache->descr->sizeof_register[regnum],
641 gdbarch_byte_order (regcache->descr->gdbarch));
28fc6740
AC
642}
643
c00dcbe9
MK
644void
645regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
646{
647 void *buf;
123f5f96 648
c00dcbe9
MK
649 gdb_assert (regcache != NULL);
650 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
651 buf = alloca (regcache->descr->sizeof_register[regnum]);
e17a4113
UW
652 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
653 gdbarch_byte_order (regcache->descr->gdbarch), val);
c00dcbe9
MK
654 regcache_raw_write (regcache, regnum, buf);
655}
656
657void
658regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
659 ULONGEST val)
660{
661 void *buf;
123f5f96 662
c00dcbe9
MK
663 gdb_assert (regcache != NULL);
664 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
665 buf = alloca (regcache->descr->sizeof_register[regnum]);
e17a4113
UW
666 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
667 gdbarch_byte_order (regcache->descr->gdbarch), val);
c00dcbe9
MK
668 regcache_raw_write (regcache, regnum, buf);
669}
670
68365089 671void
2d522557 672regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
68365089 673{
d138e37a 674 gdb_assert (regnum >= 0);
68365089
AC
675 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
676 if (regnum < regcache->descr->nr_raw_registers)
677 regcache_raw_read (regcache, regnum, buf);
2d28509a
AC
678 else if (regcache->readonly_p
679 && regnum < regcache->descr->nr_cooked_registers
ee99023e
PA
680 && regcache->register_status[regnum] == REG_VALID)
681 /* Read-only register cache, and the cooked value was cached. */
2d28509a
AC
682 memcpy (buf, register_buffer (regcache, regnum),
683 regcache->descr->sizeof_register[regnum]);
d138e37a 684 else
68365089
AC
685 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
686 regnum, buf);
61a0eb5b
AC
687}
688
a378f419
AC
689void
690regcache_cooked_read_signed (struct regcache *regcache, int regnum,
691 LONGEST *val)
692{
2d522557 693 gdb_byte *buf;
123f5f96 694
a378f419 695 gdb_assert (regcache != NULL);
a66a9c23 696 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
697 buf = alloca (regcache->descr->sizeof_register[regnum]);
698 regcache_cooked_read (regcache, regnum, buf);
e17a4113
UW
699 (*val) = extract_signed_integer
700 (buf, regcache->descr->sizeof_register[regnum],
701 gdbarch_byte_order (regcache->descr->gdbarch));
a378f419
AC
702}
703
704void
705regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
706 ULONGEST *val)
707{
2d522557 708 gdb_byte *buf;
123f5f96 709
a378f419 710 gdb_assert (regcache != NULL);
a66a9c23 711 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
712 buf = alloca (regcache->descr->sizeof_register[regnum]);
713 regcache_cooked_read (regcache, regnum, buf);
e17a4113
UW
714 (*val) = extract_unsigned_integer
715 (buf, regcache->descr->sizeof_register[regnum],
716 gdbarch_byte_order (regcache->descr->gdbarch));
a378f419
AC
717}
718
a66a9c23
AC
719void
720regcache_cooked_write_signed (struct regcache *regcache, int regnum,
721 LONGEST val)
722{
723 void *buf;
123f5f96 724
a66a9c23
AC
725 gdb_assert (regcache != NULL);
726 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
727 buf = alloca (regcache->descr->sizeof_register[regnum]);
e17a4113
UW
728 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
729 gdbarch_byte_order (regcache->descr->gdbarch), val);
a66a9c23
AC
730 regcache_cooked_write (regcache, regnum, buf);
731}
732
733void
734regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
735 ULONGEST val)
736{
737 void *buf;
123f5f96 738
a66a9c23
AC
739 gdb_assert (regcache != NULL);
740 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
741 buf = alloca (regcache->descr->sizeof_register[regnum]);
e17a4113
UW
742 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
743 gdbarch_byte_order (regcache->descr->gdbarch), val);
a66a9c23
AC
744 regcache_cooked_write (regcache, regnum, buf);
745}
746
61a0eb5b 747void
2d522557
AC
748regcache_raw_write (struct regcache *regcache, int regnum,
749 const gdb_byte *buf)
61a0eb5b 750{
594f7785
UW
751 struct cleanup *old_chain;
752
3fadccb3
AC
753 gdb_assert (regcache != NULL && buf != NULL);
754 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
2d28509a 755 gdb_assert (!regcache->readonly_p);
3fadccb3 756
3fadccb3
AC
757 /* On the sparc, writing %g0 is a no-op, so we don't even want to
758 change the registers array if something writes to this register. */
214e098a 759 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
3fadccb3
AC
760 return;
761
3fadccb3 762 /* If we have a valid copy of the register, and new value == old
0df8b418 763 value, then don't bother doing the actual store. */
ee99023e 764 if (regcache_register_status (regcache, regnum) == REG_VALID
3fadccb3
AC
765 && (memcmp (register_buffer (regcache, regnum), buf,
766 regcache->descr->sizeof_register[regnum]) == 0))
767 return;
768
594f7785
UW
769 old_chain = save_inferior_ptid ();
770 inferior_ptid = regcache->ptid;
771
316f2060 772 target_prepare_to_store (regcache);
3fadccb3
AC
773 memcpy (register_buffer (regcache, regnum), buf,
774 regcache->descr->sizeof_register[regnum]);
ee99023e 775 regcache->register_status[regnum] = REG_VALID;
56be3814 776 target_store_registers (regcache, regnum);
594f7785
UW
777
778 do_cleanups (old_chain);
61a0eb5b
AC
779}
780
68365089 781void
2d522557
AC
782regcache_cooked_write (struct regcache *regcache, int regnum,
783 const gdb_byte *buf)
68365089 784{
d138e37a 785 gdb_assert (regnum >= 0);
68365089
AC
786 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
787 if (regnum < regcache->descr->nr_raw_registers)
788 regcache_raw_write (regcache, regnum, buf);
d138e37a 789 else
68365089 790 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
d8124050 791 regnum, buf);
61a0eb5b
AC
792}
793
06c0b04e
AC
794/* Perform a partial register transfer using a read, modify, write
795 operation. */
796
797typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
798 void *buf);
799typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
800 const void *buf);
801
b9362cc7 802static void
06c0b04e
AC
803regcache_xfer_part (struct regcache *regcache, int regnum,
804 int offset, int len, void *in, const void *out,
2d522557
AC
805 void (*read) (struct regcache *regcache, int regnum,
806 gdb_byte *buf),
807 void (*write) (struct regcache *regcache, int regnum,
808 const gdb_byte *buf))
06c0b04e
AC
809{
810 struct regcache_descr *descr = regcache->descr;
fc1a4b47 811 gdb_byte reg[MAX_REGISTER_SIZE];
123f5f96 812
06c0b04e
AC
813 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
814 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
815 /* Something to do? */
816 if (offset + len == 0)
817 return;
0df8b418 818 /* Read (when needed) ... */
06c0b04e
AC
819 if (in != NULL
820 || offset > 0
821 || offset + len < descr->sizeof_register[regnum])
822 {
823 gdb_assert (read != NULL);
824 read (regcache, regnum, reg);
825 }
0df8b418 826 /* ... modify ... */
06c0b04e
AC
827 if (in != NULL)
828 memcpy (in, reg + offset, len);
829 if (out != NULL)
830 memcpy (reg + offset, out, len);
831 /* ... write (when needed). */
832 if (out != NULL)
833 {
834 gdb_assert (write != NULL);
835 write (regcache, regnum, reg);
836 }
837}
838
839void
840regcache_raw_read_part (struct regcache *regcache, int regnum,
2d522557 841 int offset, int len, gdb_byte *buf)
06c0b04e
AC
842{
843 struct regcache_descr *descr = regcache->descr;
123f5f96 844
06c0b04e
AC
845 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
846 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
847 regcache_raw_read, regcache_raw_write);
848}
849
850void
851regcache_raw_write_part (struct regcache *regcache, int regnum,
2d522557 852 int offset, int len, const gdb_byte *buf)
06c0b04e
AC
853{
854 struct regcache_descr *descr = regcache->descr;
123f5f96 855
06c0b04e
AC
856 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
857 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
858 regcache_raw_read, regcache_raw_write);
859}
860
861void
862regcache_cooked_read_part (struct regcache *regcache, int regnum,
2d522557 863 int offset, int len, gdb_byte *buf)
06c0b04e
AC
864{
865 struct regcache_descr *descr = regcache->descr;
123f5f96 866
06c0b04e
AC
867 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
868 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
869 regcache_cooked_read, regcache_cooked_write);
870}
871
872void
873regcache_cooked_write_part (struct regcache *regcache, int regnum,
2d522557 874 int offset, int len, const gdb_byte *buf)
06c0b04e
AC
875{
876 struct regcache_descr *descr = regcache->descr;
123f5f96 877
06c0b04e
AC
878 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
879 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
880 regcache_cooked_read, regcache_cooked_write);
881}
32178cab 882
a16d75cc 883/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
9a661b68
MK
884
885void
6618125d 886regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
9a661b68
MK
887{
888 void *regbuf;
889 size_t size;
890
a16d75cc 891 gdb_assert (regcache != NULL);
9a661b68
MK
892 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
893 gdb_assert (!regcache->readonly_p);
894
9a661b68
MK
895 regbuf = register_buffer (regcache, regnum);
896 size = regcache->descr->sizeof_register[regnum];
897
898 if (buf)
ee99023e
PA
899 {
900 memcpy (regbuf, buf, size);
901 regcache->register_status[regnum] = REG_VALID;
902 }
9a661b68 903 else
ee99023e
PA
904 {
905 /* This memset not strictly necessary, but better than garbage
906 in case the register value manages to escape somewhere (due
907 to a bug, no less). */
908 memset (regbuf, 0, size);
909 regcache->register_status[regnum] = REG_UNAVAILABLE;
910 }
9a661b68
MK
911}
912
913/* Collect register REGNUM from REGCACHE and store its contents in BUF. */
914
915void
6618125d 916regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
9a661b68
MK
917{
918 const void *regbuf;
919 size_t size;
920
921 gdb_assert (regcache != NULL && buf != NULL);
922 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
923
924 regbuf = register_buffer (regcache, regnum);
925 size = regcache->descr->sizeof_register[regnum];
926 memcpy (buf, regbuf, size);
927}
928
193cb69f 929
515630c5 930/* Special handling for register PC. */
32178cab
MS
931
932CORE_ADDR
515630c5 933regcache_read_pc (struct regcache *regcache)
32178cab 934{
61a1198a
UW
935 struct gdbarch *gdbarch = get_regcache_arch (regcache);
936
32178cab
MS
937 CORE_ADDR pc_val;
938
61a1198a
UW
939 if (gdbarch_read_pc_p (gdbarch))
940 pc_val = gdbarch_read_pc (gdbarch, regcache);
cde9ea48 941 /* Else use per-frame method on get_current_frame. */
214e098a 942 else if (gdbarch_pc_regnum (gdbarch) >= 0)
cde9ea48 943 {
61a1198a 944 ULONGEST raw_val;
123f5f96 945
3e8c568d 946 regcache_cooked_read_unsigned (regcache,
214e098a 947 gdbarch_pc_regnum (gdbarch),
3e8c568d 948 &raw_val);
214e098a 949 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
cde9ea48
AC
950 }
951 else
515630c5
UW
952 internal_error (__FILE__, __LINE__,
953 _("regcache_read_pc: Unable to find PC"));
32178cab
MS
954 return pc_val;
955}
956
32178cab 957void
515630c5 958regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
32178cab 959{
61a1198a
UW
960 struct gdbarch *gdbarch = get_regcache_arch (regcache);
961
61a1198a
UW
962 if (gdbarch_write_pc_p (gdbarch))
963 gdbarch_write_pc (gdbarch, regcache, pc);
214e098a 964 else if (gdbarch_pc_regnum (gdbarch) >= 0)
3e8c568d 965 regcache_cooked_write_unsigned (regcache,
214e098a 966 gdbarch_pc_regnum (gdbarch), pc);
61a1198a
UW
967 else
968 internal_error (__FILE__, __LINE__,
515630c5 969 _("regcache_write_pc: Unable to update PC"));
edb3359d
DJ
970
971 /* Writing the PC (for instance, from "load") invalidates the
972 current frame. */
973 reinit_frame_cache ();
32178cab
MS
974}
975
32178cab 976
705152c5
MS
977static void
978reg_flush_command (char *command, int from_tty)
979{
980 /* Force-flush the register cache. */
981 registers_changed ();
982 if (from_tty)
a3f17187 983 printf_filtered (_("Register cache flushed.\n"));
705152c5
MS
984}
985
af030b9a
AC
986static void
987dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
988 const unsigned char *buf, long len)
989{
990 int i;
123f5f96 991
af030b9a
AC
992 switch (endian)
993 {
994 case BFD_ENDIAN_BIG:
995 for (i = 0; i < len; i++)
996 fprintf_unfiltered (file, "%02x", buf[i]);
997 break;
998 case BFD_ENDIAN_LITTLE:
999 for (i = len - 1; i >= 0; i--)
1000 fprintf_unfiltered (file, "%02x", buf[i]);
1001 break;
1002 default:
e2e0b3e5 1003 internal_error (__FILE__, __LINE__, _("Bad switch"));
af030b9a
AC
1004 }
1005}
1006
1007enum regcache_dump_what
1008{
3e43a32a
MS
1009 regcache_dump_none, regcache_dump_raw,
1010 regcache_dump_cooked, regcache_dump_groups
af030b9a
AC
1011};
1012
1013static void
1014regcache_dump (struct regcache *regcache, struct ui_file *file,
1015 enum regcache_dump_what what_to_dump)
1016{
1017 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
b59ff9d5 1018 struct gdbarch *gdbarch = regcache->descr->gdbarch;
af030b9a
AC
1019 int regnum;
1020 int footnote_nr = 0;
1021 int footnote_register_size = 0;
1022 int footnote_register_offset = 0;
1023 int footnote_register_type_name_null = 0;
1024 long register_offset = 0;
123a958e 1025 unsigned char buf[MAX_REGISTER_SIZE];
af030b9a
AC
1026
1027#if 0
af030b9a
AC
1028 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1029 regcache->descr->nr_raw_registers);
1030 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1031 regcache->descr->nr_cooked_registers);
1032 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1033 regcache->descr->sizeof_raw_registers);
ee99023e
PA
1034 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1035 regcache->descr->sizeof_raw_register_status);
f57d151a 1036 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
214e098a 1037 gdbarch_num_regs (gdbarch));
f57d151a 1038 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
214e098a 1039 gdbarch_num_pseudo_regs (gdbarch));
af030b9a
AC
1040#endif
1041
1042 gdb_assert (regcache->descr->nr_cooked_registers
214e098a
UW
1043 == (gdbarch_num_regs (gdbarch)
1044 + gdbarch_num_pseudo_regs (gdbarch)));
af030b9a
AC
1045
1046 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1047 {
1048 /* Name. */
1049 if (regnum < 0)
1050 fprintf_unfiltered (file, " %-10s", "Name");
1051 else
1052 {
214e098a 1053 const char *p = gdbarch_register_name (gdbarch, regnum);
123f5f96 1054
af030b9a
AC
1055 if (p == NULL)
1056 p = "";
1057 else if (p[0] == '\0')
1058 p = "''";
1059 fprintf_unfiltered (file, " %-10s", p);
1060 }
1061
1062 /* Number. */
1063 if (regnum < 0)
1064 fprintf_unfiltered (file, " %4s", "Nr");
1065 else
1066 fprintf_unfiltered (file, " %4d", regnum);
1067
1068 /* Relative number. */
1069 if (regnum < 0)
1070 fprintf_unfiltered (file, " %4s", "Rel");
214e098a 1071 else if (regnum < gdbarch_num_regs (gdbarch))
af030b9a
AC
1072 fprintf_unfiltered (file, " %4d", regnum);
1073 else
f57d151a 1074 fprintf_unfiltered (file, " %4d",
214e098a 1075 (regnum - gdbarch_num_regs (gdbarch)));
af030b9a
AC
1076
1077 /* Offset. */
1078 if (regnum < 0)
1079 fprintf_unfiltered (file, " %6s ", "Offset");
1080 else
1081 {
1082 fprintf_unfiltered (file, " %6ld",
1083 regcache->descr->register_offset[regnum]);
a7e3c2ad 1084 if (register_offset != regcache->descr->register_offset[regnum]
d3b22ed5
AC
1085 || (regnum > 0
1086 && (regcache->descr->register_offset[regnum]
1087 != (regcache->descr->register_offset[regnum - 1]
1088 + regcache->descr->sizeof_register[regnum - 1])))
1089 )
af030b9a
AC
1090 {
1091 if (!footnote_register_offset)
1092 footnote_register_offset = ++footnote_nr;
1093 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1094 }
1095 else
1096 fprintf_unfiltered (file, " ");
1097 register_offset = (regcache->descr->register_offset[regnum]
1098 + regcache->descr->sizeof_register[regnum]);
1099 }
1100
1101 /* Size. */
1102 if (regnum < 0)
1103 fprintf_unfiltered (file, " %5s ", "Size");
1104 else
01e1877c
AC
1105 fprintf_unfiltered (file, " %5ld",
1106 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1107
1108 /* Type. */
b59ff9d5
AC
1109 {
1110 const char *t;
123f5f96 1111
b59ff9d5
AC
1112 if (regnum < 0)
1113 t = "Type";
1114 else
1115 {
1116 static const char blt[] = "builtin_type";
123f5f96 1117
b59ff9d5
AC
1118 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1119 if (t == NULL)
1120 {
1121 char *n;
123f5f96 1122
b59ff9d5
AC
1123 if (!footnote_register_type_name_null)
1124 footnote_register_type_name_null = ++footnote_nr;
b435e160 1125 n = xstrprintf ("*%d", footnote_register_type_name_null);
b59ff9d5
AC
1126 make_cleanup (xfree, n);
1127 t = n;
1128 }
1129 /* Chop a leading builtin_type. */
1130 if (strncmp (t, blt, strlen (blt)) == 0)
1131 t += strlen (blt);
1132 }
1133 fprintf_unfiltered (file, " %-15s", t);
1134 }
1135
1136 /* Leading space always present. */
1137 fprintf_unfiltered (file, " ");
af030b9a
AC
1138
1139 /* Value, raw. */
1140 if (what_to_dump == regcache_dump_raw)
1141 {
1142 if (regnum < 0)
1143 fprintf_unfiltered (file, "Raw value");
1144 else if (regnum >= regcache->descr->nr_raw_registers)
1145 fprintf_unfiltered (file, "<cooked>");
ee99023e 1146 else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
af030b9a 1147 fprintf_unfiltered (file, "<invalid>");
ee99023e
PA
1148 else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1149 fprintf_unfiltered (file, "<unavailable>");
af030b9a
AC
1150 else
1151 {
1152 regcache_raw_read (regcache, regnum, buf);
1153 fprintf_unfiltered (file, "0x");
0d20ae72 1154 dump_endian_bytes (file,
214e098a 1155 gdbarch_byte_order (gdbarch), buf,
01e1877c 1156 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1157 }
1158 }
1159
1160 /* Value, cooked. */
1161 if (what_to_dump == regcache_dump_cooked)
1162 {
1163 if (regnum < 0)
1164 fprintf_unfiltered (file, "Cooked value");
1165 else
1166 {
ee99023e
PA
1167 /* FIXME: no way for cooked reads to signal unavailable
1168 yet. */
af030b9a
AC
1169 regcache_cooked_read (regcache, regnum, buf);
1170 fprintf_unfiltered (file, "0x");
0d20ae72 1171 dump_endian_bytes (file,
214e098a 1172 gdbarch_byte_order (gdbarch), buf,
01e1877c 1173 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1174 }
1175 }
1176
b59ff9d5
AC
1177 /* Group members. */
1178 if (what_to_dump == regcache_dump_groups)
1179 {
1180 if (regnum < 0)
1181 fprintf_unfiltered (file, "Groups");
1182 else
1183 {
b59ff9d5 1184 const char *sep = "";
6c7d17ba 1185 struct reggroup *group;
123f5f96 1186
6c7d17ba
AC
1187 for (group = reggroup_next (gdbarch, NULL);
1188 group != NULL;
1189 group = reggroup_next (gdbarch, group))
b59ff9d5 1190 {
6c7d17ba 1191 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
b59ff9d5 1192 {
3e43a32a
MS
1193 fprintf_unfiltered (file,
1194 "%s%s", sep, reggroup_name (group));
b59ff9d5
AC
1195 sep = ",";
1196 }
1197 }
1198 }
1199 }
1200
af030b9a
AC
1201 fprintf_unfiltered (file, "\n");
1202 }
1203
1204 if (footnote_register_size)
1205 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1206 footnote_register_size);
1207 if (footnote_register_offset)
1208 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1209 footnote_register_offset);
1210 if (footnote_register_type_name_null)
1211 fprintf_unfiltered (file,
1212 "*%d: Register type's name NULL.\n",
1213 footnote_register_type_name_null);
1214 do_cleanups (cleanups);
1215}
1216
1217static void
1218regcache_print (char *args, enum regcache_dump_what what_to_dump)
1219{
1220 if (args == NULL)
28c38f10 1221 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
af030b9a
AC
1222 else
1223 {
724b958c 1224 struct cleanup *cleanups;
af030b9a 1225 struct ui_file *file = gdb_fopen (args, "w");
123f5f96 1226
af030b9a 1227 if (file == NULL)
e2e0b3e5 1228 perror_with_name (_("maintenance print architecture"));
724b958c 1229 cleanups = make_cleanup_ui_file_delete (file);
28c38f10 1230 regcache_dump (get_current_regcache (), file, what_to_dump);
724b958c 1231 do_cleanups (cleanups);
af030b9a
AC
1232 }
1233}
1234
1235static void
1236maintenance_print_registers (char *args, int from_tty)
1237{
1238 regcache_print (args, regcache_dump_none);
1239}
1240
1241static void
1242maintenance_print_raw_registers (char *args, int from_tty)
1243{
1244 regcache_print (args, regcache_dump_raw);
1245}
1246
1247static void
1248maintenance_print_cooked_registers (char *args, int from_tty)
1249{
1250 regcache_print (args, regcache_dump_cooked);
1251}
1252
b59ff9d5
AC
1253static void
1254maintenance_print_register_groups (char *args, int from_tty)
1255{
1256 regcache_print (args, regcache_dump_groups);
1257}
1258
b9362cc7
AC
1259extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1260
32178cab
MS
1261void
1262_initialize_regcache (void)
1263{
3e43a32a
MS
1264 regcache_descr_handle
1265 = gdbarch_data_register_post_init (init_regcache_descr);
705152c5 1266
f4c5303c 1267 observer_attach_target_changed (regcache_observer_target_changed);
5231c1fd 1268 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
f4c5303c 1269
705152c5 1270 add_com ("flushregs", class_maintenance, reg_flush_command,
1bedd215 1271 _("Force gdb to flush its register cache (maintainer command)"));
39f77062 1272
3e43a32a
MS
1273 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1274 _("Print the internal register configuration.\n"
1275 "Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 1276 add_cmd ("raw-registers", class_maintenance,
3e43a32a
MS
1277 maintenance_print_raw_registers,
1278 _("Print the internal register configuration "
1279 "including raw values.\n"
1280 "Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 1281 add_cmd ("cooked-registers", class_maintenance,
3e43a32a
MS
1282 maintenance_print_cooked_registers,
1283 _("Print the internal register configuration "
1284 "including cooked values.\n"
1285 "Takes an optional file parameter."), &maintenanceprintlist);
b59ff9d5 1286 add_cmd ("register-groups", class_maintenance,
3e43a32a
MS
1287 maintenance_print_register_groups,
1288 _("Print the internal register configuration "
1289 "including each register's group.\n"
1290 "Takes an optional file parameter."),
af030b9a
AC
1291 &maintenanceprintlist);
1292
32178cab 1293}
This page took 0.986195 seconds and 4 git commands to generate.