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