* m32c/gdb-if.c (m32c_signal_to_host): Rename to
[gdb/SamB.git] / gdb / regcache.c
blob13b9db45c3491321276c15aa26b68162151c83c1
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
6 This file is part of GDB.
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
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "defs.h"
22 #include "inferior.h"
23 #include "target.h"
24 #include "gdbarch.h"
25 #include "gdbcmd.h"
26 #include "regcache.h"
27 #include "reggroups.h"
28 #include "gdb_assert.h"
29 #include "gdb_string.h"
30 #include "gdbcmd.h" /* For maintenanceprintlist. */
31 #include "observer.h"
34 * DATA STRUCTURE
36 * Here is the actual register cache.
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created */
42 struct gdbarch_data *regcache_descr_handle;
44 struct regcache_descr
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
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
52 registers then those registers and not the PC lives in the raw
53 cache. */
54 int nr_raw_registers;
55 long sizeof_raw_registers;
56 long sizeof_raw_register_valid_p;
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
61 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
62 both raw registers and memory by the architecture methods
63 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
64 int nr_cooked_registers;
65 long sizeof_cooked_registers;
66 long sizeof_cooked_register_valid_p;
68 /* Offset and size (in 8 bit bytes), of reach register in the
69 register cache. All registers (including those in the range
70 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
71 Assigning all registers an offset makes it possible to keep
72 legacy code, such as that found in read_register_bytes() and
73 write_register_bytes() working. */
74 long *register_offset;
75 long *sizeof_register;
77 /* Cached table containing the type of each register. */
78 struct type **register_type;
81 static void *
82 init_regcache_descr (struct gdbarch *gdbarch)
84 int i;
85 struct regcache_descr *descr;
86 gdb_assert (gdbarch != NULL);
88 /* Create an initial, zero filled, table. */
89 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
90 descr->gdbarch = gdbarch;
92 /* Total size of the register space. The raw registers are mapped
93 directly onto the raw register cache while the pseudo's are
94 either mapped onto raw-registers or memory. */
95 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
96 + gdbarch_num_pseudo_regs (gdbarch);
97 descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (gdbarch)
98 + gdbarch_num_pseudo_regs
99 (gdbarch);
101 /* Fill in a table of register types. */
102 descr->register_type
103 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
104 for (i = 0; i < descr->nr_cooked_registers; i++)
105 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
107 /* Construct a strictly RAW register cache. Don't allow pseudo's
108 into the register cache. */
109 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
111 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
112 array. This pretects GDB from erant code that accesses elements
113 of the global register_valid_p[] array in the range
114 [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
115 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
117 /* Lay out the register cache.
119 NOTE: cagney/2002-05-22: Only register_type() is used when
120 constructing the register cache. It is assumed that the
121 register's raw size, virtual size and type length are all the
122 same. */
125 long offset = 0;
126 descr->sizeof_register
127 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
128 descr->register_offset
129 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
130 for (i = 0; i < descr->nr_cooked_registers; i++)
132 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
133 descr->register_offset[i] = offset;
134 offset += descr->sizeof_register[i];
135 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
137 /* Set the real size of the register cache buffer. */
138 descr->sizeof_cooked_registers = offset;
141 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
142 the raw registers. Unfortunately some code still accesses the
143 register array directly using the global registers[]. Until that
144 code has been purged, play safe and over allocating the register
145 buffer. Ulgh! */
146 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
148 return descr;
151 static struct regcache_descr *
152 regcache_descr (struct gdbarch *gdbarch)
154 return gdbarch_data (gdbarch, regcache_descr_handle);
157 /* Utility functions returning useful register attributes stored in
158 the regcache descr. */
160 struct type *
161 register_type (struct gdbarch *gdbarch, int regnum)
163 struct regcache_descr *descr = regcache_descr (gdbarch);
164 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
165 return descr->register_type[regnum];
168 /* Utility functions returning useful register attributes stored in
169 the regcache descr. */
172 register_size (struct gdbarch *gdbarch, int regnum)
174 struct regcache_descr *descr = regcache_descr (gdbarch);
175 int size;
176 gdb_assert (regnum >= 0
177 && regnum < (gdbarch_num_regs (gdbarch)
178 + gdbarch_num_pseudo_regs (gdbarch)));
179 size = descr->sizeof_register[regnum];
180 return size;
183 /* The register cache for storing raw register values. */
185 struct regcache
187 struct regcache_descr *descr;
188 /* The register buffers. A read-only register cache can hold the
189 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
190 register cache can only hold [0 .. gdbarch_num_regs). */
191 gdb_byte *registers;
192 /* Register cache status:
193 register_valid_p[REG] == 0 if REG value is not in the cache
194 > 0 if REG value is in the cache
195 < 0 if REG value is permanently unavailable */
196 signed char *register_valid_p;
197 /* Is this a read-only cache? A read-only cache is used for saving
198 the target's register state (e.g, across an inferior function
199 call or just before forcing a function return). A read-only
200 cache can only be updated via the methods regcache_dup() and
201 regcache_cpy(). The actual contents are determined by the
202 reggroup_save and reggroup_restore methods. */
203 int readonly_p;
204 /* If this is a read-write cache, which thread's registers is
205 it connected to? */
206 ptid_t ptid;
209 struct regcache *
210 regcache_xmalloc (struct gdbarch *gdbarch)
212 struct regcache_descr *descr;
213 struct regcache *regcache;
214 gdb_assert (gdbarch != NULL);
215 descr = regcache_descr (gdbarch);
216 regcache = XMALLOC (struct regcache);
217 regcache->descr = descr;
218 regcache->registers
219 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
220 regcache->register_valid_p
221 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
222 regcache->readonly_p = 1;
223 regcache->ptid = minus_one_ptid;
224 return regcache;
227 void
228 regcache_xfree (struct regcache *regcache)
230 if (regcache == NULL)
231 return;
232 xfree (regcache->registers);
233 xfree (regcache->register_valid_p);
234 xfree (regcache);
237 static void
238 do_regcache_xfree (void *data)
240 regcache_xfree (data);
243 struct cleanup *
244 make_cleanup_regcache_xfree (struct regcache *regcache)
246 return make_cleanup (do_regcache_xfree, regcache);
249 /* Return REGCACHE's architecture. */
251 struct gdbarch *
252 get_regcache_arch (const struct regcache *regcache)
254 return regcache->descr->gdbarch;
257 /* Return a pointer to register REGNUM's buffer cache. */
259 static gdb_byte *
260 register_buffer (const struct regcache *regcache, int regnum)
262 return regcache->registers + regcache->descr->register_offset[regnum];
265 void
266 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
267 void *src)
269 struct gdbarch *gdbarch = dst->descr->gdbarch;
270 gdb_byte buf[MAX_REGISTER_SIZE];
271 int regnum;
272 /* The DST should be `read-only', if it wasn't then the save would
273 end up trying to write the register values back out to the
274 target. */
275 gdb_assert (dst->readonly_p);
276 /* Clear the dest. */
277 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
278 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
279 /* Copy over any registers (identified by their membership in the
280 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
281 gdbarch_num_pseudo_regs) range is checked since some architectures need
282 to save/restore `cooked' registers that live in memory. */
283 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
285 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
287 int valid = cooked_read (src, regnum, buf);
288 if (valid)
290 memcpy (register_buffer (dst, regnum), buf,
291 register_size (gdbarch, regnum));
292 dst->register_valid_p[regnum] = 1;
298 void
299 regcache_restore (struct regcache *dst,
300 regcache_cooked_read_ftype *cooked_read,
301 void *cooked_read_context)
303 struct gdbarch *gdbarch = dst->descr->gdbarch;
304 gdb_byte buf[MAX_REGISTER_SIZE];
305 int regnum;
306 /* The dst had better not be read-only. If it is, the `restore'
307 doesn't make much sense. */
308 gdb_assert (!dst->readonly_p);
309 /* Copy over any registers, being careful to only restore those that
310 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
311 + gdbarch_num_pseudo_regs) range is checked since some architectures need
312 to save/restore `cooked' registers that live in memory. */
313 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
315 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
317 int valid = cooked_read (cooked_read_context, regnum, buf);
318 if (valid)
319 regcache_cooked_write (dst, regnum, buf);
324 static int
325 do_cooked_read (void *src, int regnum, gdb_byte *buf)
327 struct regcache *regcache = src;
328 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
329 /* Don't even think about fetching a register from a read-only
330 cache when the register isn't yet valid. There isn't a target
331 from which the register value can be fetched. */
332 return 0;
333 regcache_cooked_read (regcache, regnum, buf);
334 return 1;
338 void
339 regcache_cpy (struct regcache *dst, struct regcache *src)
341 int i;
342 gdb_byte *buf;
343 gdb_assert (src != NULL && dst != NULL);
344 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
345 gdb_assert (src != dst);
346 gdb_assert (src->readonly_p || dst->readonly_p);
347 if (!src->readonly_p)
348 regcache_save (dst, do_cooked_read, src);
349 else if (!dst->readonly_p)
350 regcache_restore (dst, do_cooked_read, src);
351 else
352 regcache_cpy_no_passthrough (dst, src);
355 void
356 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
358 int i;
359 gdb_assert (src != NULL && dst != NULL);
360 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
361 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
362 move of data into the current regcache. Doing this would be
363 silly - it would mean that valid_p would be completely invalid. */
364 gdb_assert (dst->readonly_p);
365 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
366 memcpy (dst->register_valid_p, src->register_valid_p,
367 dst->descr->sizeof_raw_register_valid_p);
370 struct regcache *
371 regcache_dup (struct regcache *src)
373 struct regcache *newbuf;
374 newbuf = regcache_xmalloc (src->descr->gdbarch);
375 regcache_cpy (newbuf, src);
376 return newbuf;
379 struct regcache *
380 regcache_dup_no_passthrough (struct regcache *src)
382 struct regcache *newbuf;
383 newbuf = regcache_xmalloc (src->descr->gdbarch);
384 regcache_cpy_no_passthrough (newbuf, src);
385 return newbuf;
389 regcache_valid_p (const struct regcache *regcache, int regnum)
391 gdb_assert (regcache != NULL);
392 gdb_assert (regnum >= 0);
393 if (regcache->readonly_p)
394 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
395 else
396 gdb_assert (regnum < regcache->descr->nr_raw_registers);
398 return regcache->register_valid_p[regnum];
401 void
402 regcache_invalidate (struct regcache *regcache, int regnum)
404 gdb_assert (regcache != NULL);
405 gdb_assert (regnum >= 0);
406 gdb_assert (!regcache->readonly_p);
407 gdb_assert (regnum < regcache->descr->nr_raw_registers);
408 regcache->register_valid_p[regnum] = 0;
412 /* Global structure containing the current regcache. */
413 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
414 deprecated_register_valid[] currently point into this structure. */
415 static struct regcache *current_regcache;
417 /* NOTE: this is a write-through cache. There is no "dirty" bit for
418 recording if the register values have been changed (eg. by the
419 user). Therefore all registers must be written back to the
420 target when appropriate. */
422 struct regcache *get_thread_regcache (ptid_t ptid)
424 /* NOTE: uweigand/2007-05-05: We need to detect the thread's
425 current architecture at this point. */
426 struct gdbarch *thread_gdbarch = current_gdbarch;
428 if (current_regcache && ptid_equal (current_regcache->ptid, ptid)
429 && get_regcache_arch (current_regcache) == thread_gdbarch)
430 return current_regcache;
432 if (current_regcache)
433 regcache_xfree (current_regcache);
435 current_regcache = regcache_xmalloc (thread_gdbarch);
436 current_regcache->readonly_p = 0;
437 current_regcache->ptid = ptid;
439 return current_regcache;
442 struct regcache *get_current_regcache (void)
444 return get_thread_regcache (inferior_ptid);
448 /* Observer for the target_changed event. */
450 static void
451 regcache_observer_target_changed (struct target_ops *target)
453 registers_changed ();
456 /* Update global variables old ptids to hold NEW_PTID if they were
457 holding OLD_PTID. */
458 static void
459 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
461 if (current_regcache != NULL
462 && ptid_equal (current_regcache->ptid, old_ptid))
463 current_regcache->ptid = new_ptid;
466 /* Low level examining and depositing of registers.
468 The caller is responsible for making sure that the inferior is
469 stopped before calling the fetching routines, or it will get
470 garbage. (a change from GDB version 3, in which the caller got the
471 value from the last stop). */
473 /* REGISTERS_CHANGED ()
475 Indicate that registers may have changed, so invalidate the cache. */
477 void
478 registers_changed (void)
480 int i;
482 regcache_xfree (current_regcache);
483 current_regcache = NULL;
485 /* Need to forget about any frames we have cached, too. */
486 reinit_frame_cache ();
488 /* Force cleanup of any alloca areas if using C alloca instead of
489 a builtin alloca. This particular call is used to clean up
490 areas allocated by low level target code which may build up
491 during lengthy interactions between gdb and the target before
492 gdb gives control to the user (ie watchpoints). */
493 alloca (0);
497 void
498 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
500 gdb_assert (regcache != NULL && buf != NULL);
501 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
502 /* Make certain that the register cache is up-to-date with respect
503 to the current thread. This switching shouldn't be necessary
504 only there is still only one target side register cache. Sigh!
505 On the bright side, at least there is a regcache object. */
506 if (!regcache->readonly_p)
508 if (!regcache_valid_p (regcache, regnum))
510 struct cleanup *old_chain = save_inferior_ptid ();
511 inferior_ptid = regcache->ptid;
512 target_fetch_registers (regcache, regnum);
513 do_cleanups (old_chain);
515 #if 0
516 /* FIXME: cagney/2004-08-07: At present a number of targets
517 forget (or didn't know that they needed) to set this leading to
518 panics. Also is the problem that targets need to indicate
519 that a register is in one of the possible states: valid,
520 undefined, unknown. The last of which isn't yet
521 possible. */
522 gdb_assert (regcache_valid_p (regcache, regnum));
523 #endif
525 /* Copy the value directly into the register cache. */
526 memcpy (buf, register_buffer (regcache, regnum),
527 regcache->descr->sizeof_register[regnum]);
530 void
531 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
533 gdb_byte *buf;
534 gdb_assert (regcache != NULL);
535 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
536 buf = alloca (regcache->descr->sizeof_register[regnum]);
537 regcache_raw_read (regcache, regnum, buf);
538 (*val) = extract_signed_integer (buf,
539 regcache->descr->sizeof_register[regnum]);
542 void
543 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
544 ULONGEST *val)
546 gdb_byte *buf;
547 gdb_assert (regcache != NULL);
548 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
549 buf = alloca (regcache->descr->sizeof_register[regnum]);
550 regcache_raw_read (regcache, regnum, buf);
551 (*val) = extract_unsigned_integer (buf,
552 regcache->descr->sizeof_register[regnum]);
555 void
556 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
558 void *buf;
559 gdb_assert (regcache != NULL);
560 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
561 buf = alloca (regcache->descr->sizeof_register[regnum]);
562 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
563 regcache_raw_write (regcache, regnum, buf);
566 void
567 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
568 ULONGEST val)
570 void *buf;
571 gdb_assert (regcache != NULL);
572 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
573 buf = alloca (regcache->descr->sizeof_register[regnum]);
574 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
575 regcache_raw_write (regcache, regnum, buf);
578 void
579 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
581 gdb_assert (regnum >= 0);
582 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
583 if (regnum < regcache->descr->nr_raw_registers)
584 regcache_raw_read (regcache, regnum, buf);
585 else if (regcache->readonly_p
586 && regnum < regcache->descr->nr_cooked_registers
587 && regcache->register_valid_p[regnum])
588 /* Read-only register cache, perhaps the cooked value was cached? */
589 memcpy (buf, register_buffer (regcache, regnum),
590 regcache->descr->sizeof_register[regnum]);
591 else
592 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
593 regnum, buf);
596 void
597 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
598 LONGEST *val)
600 gdb_byte *buf;
601 gdb_assert (regcache != NULL);
602 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
603 buf = alloca (regcache->descr->sizeof_register[regnum]);
604 regcache_cooked_read (regcache, regnum, buf);
605 (*val) = extract_signed_integer (buf,
606 regcache->descr->sizeof_register[regnum]);
609 void
610 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
611 ULONGEST *val)
613 gdb_byte *buf;
614 gdb_assert (regcache != NULL);
615 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
616 buf = alloca (regcache->descr->sizeof_register[regnum]);
617 regcache_cooked_read (regcache, regnum, buf);
618 (*val) = extract_unsigned_integer (buf,
619 regcache->descr->sizeof_register[regnum]);
622 void
623 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
624 LONGEST val)
626 void *buf;
627 gdb_assert (regcache != NULL);
628 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
629 buf = alloca (regcache->descr->sizeof_register[regnum]);
630 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
631 regcache_cooked_write (regcache, regnum, buf);
634 void
635 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
636 ULONGEST val)
638 void *buf;
639 gdb_assert (regcache != NULL);
640 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
641 buf = alloca (regcache->descr->sizeof_register[regnum]);
642 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
643 regcache_cooked_write (regcache, regnum, buf);
646 void
647 regcache_raw_write (struct regcache *regcache, int regnum,
648 const gdb_byte *buf)
650 struct cleanup *old_chain;
652 gdb_assert (regcache != NULL && buf != NULL);
653 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
654 gdb_assert (!regcache->readonly_p);
656 /* On the sparc, writing %g0 is a no-op, so we don't even want to
657 change the registers array if something writes to this register. */
658 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
659 return;
661 /* If we have a valid copy of the register, and new value == old
662 value, then don't bother doing the actual store. */
663 if (regcache_valid_p (regcache, regnum)
664 && (memcmp (register_buffer (regcache, regnum), buf,
665 regcache->descr->sizeof_register[regnum]) == 0))
666 return;
668 old_chain = save_inferior_ptid ();
669 inferior_ptid = regcache->ptid;
671 target_prepare_to_store (regcache);
672 memcpy (register_buffer (regcache, regnum), buf,
673 regcache->descr->sizeof_register[regnum]);
674 regcache->register_valid_p[regnum] = 1;
675 target_store_registers (regcache, regnum);
677 do_cleanups (old_chain);
680 void
681 regcache_cooked_write (struct regcache *regcache, int regnum,
682 const gdb_byte *buf)
684 gdb_assert (regnum >= 0);
685 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
686 if (regnum < regcache->descr->nr_raw_registers)
687 regcache_raw_write (regcache, regnum, buf);
688 else
689 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
690 regnum, buf);
693 /* Perform a partial register transfer using a read, modify, write
694 operation. */
696 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
697 void *buf);
698 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
699 const void *buf);
701 static void
702 regcache_xfer_part (struct regcache *regcache, int regnum,
703 int offset, int len, void *in, const void *out,
704 void (*read) (struct regcache *regcache, int regnum,
705 gdb_byte *buf),
706 void (*write) (struct regcache *regcache, int regnum,
707 const gdb_byte *buf))
709 struct regcache_descr *descr = regcache->descr;
710 gdb_byte reg[MAX_REGISTER_SIZE];
711 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
712 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
713 /* Something to do? */
714 if (offset + len == 0)
715 return;
716 /* Read (when needed) ... */
717 if (in != NULL
718 || offset > 0
719 || offset + len < descr->sizeof_register[regnum])
721 gdb_assert (read != NULL);
722 read (regcache, regnum, reg);
724 /* ... modify ... */
725 if (in != NULL)
726 memcpy (in, reg + offset, len);
727 if (out != NULL)
728 memcpy (reg + offset, out, len);
729 /* ... write (when needed). */
730 if (out != NULL)
732 gdb_assert (write != NULL);
733 write (regcache, regnum, reg);
737 void
738 regcache_raw_read_part (struct regcache *regcache, int regnum,
739 int offset, int len, gdb_byte *buf)
741 struct regcache_descr *descr = regcache->descr;
742 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
743 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
744 regcache_raw_read, regcache_raw_write);
747 void
748 regcache_raw_write_part (struct regcache *regcache, int regnum,
749 int offset, int len, const gdb_byte *buf)
751 struct regcache_descr *descr = regcache->descr;
752 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
753 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
754 regcache_raw_read, regcache_raw_write);
757 void
758 regcache_cooked_read_part (struct regcache *regcache, int regnum,
759 int offset, int len, gdb_byte *buf)
761 struct regcache_descr *descr = regcache->descr;
762 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
763 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
764 regcache_cooked_read, regcache_cooked_write);
767 void
768 regcache_cooked_write_part (struct regcache *regcache, int regnum,
769 int offset, int len, const gdb_byte *buf)
771 struct regcache_descr *descr = regcache->descr;
772 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
773 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
774 regcache_cooked_read, regcache_cooked_write);
777 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
779 void
780 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
782 void *regbuf;
783 size_t size;
785 gdb_assert (regcache != NULL);
786 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
787 gdb_assert (!regcache->readonly_p);
789 regbuf = register_buffer (regcache, regnum);
790 size = regcache->descr->sizeof_register[regnum];
792 if (buf)
793 memcpy (regbuf, buf, size);
794 else
795 memset (regbuf, 0, size);
797 /* Mark the register as cached. */
798 regcache->register_valid_p[regnum] = 1;
801 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
803 void
804 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
806 const void *regbuf;
807 size_t size;
809 gdb_assert (regcache != NULL && buf != NULL);
810 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
812 regbuf = register_buffer (regcache, regnum);
813 size = regcache->descr->sizeof_register[regnum];
814 memcpy (buf, regbuf, size);
818 /* Special handling for register PC. */
820 CORE_ADDR
821 regcache_read_pc (struct regcache *regcache)
823 struct gdbarch *gdbarch = get_regcache_arch (regcache);
825 CORE_ADDR pc_val;
827 if (gdbarch_read_pc_p (gdbarch))
828 pc_val = gdbarch_read_pc (gdbarch, regcache);
829 /* Else use per-frame method on get_current_frame. */
830 else if (gdbarch_pc_regnum (gdbarch) >= 0)
832 ULONGEST raw_val;
833 regcache_cooked_read_unsigned (regcache,
834 gdbarch_pc_regnum (gdbarch),
835 &raw_val);
836 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
838 else
839 internal_error (__FILE__, __LINE__,
840 _("regcache_read_pc: Unable to find PC"));
841 return pc_val;
844 void
845 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
847 struct gdbarch *gdbarch = get_regcache_arch (regcache);
849 if (gdbarch_write_pc_p (gdbarch))
850 gdbarch_write_pc (gdbarch, regcache, pc);
851 else if (gdbarch_pc_regnum (gdbarch) >= 0)
852 regcache_cooked_write_unsigned (regcache,
853 gdbarch_pc_regnum (gdbarch), pc);
854 else
855 internal_error (__FILE__, __LINE__,
856 _("regcache_write_pc: Unable to update PC"));
860 static void
861 reg_flush_command (char *command, int from_tty)
863 /* Force-flush the register cache. */
864 registers_changed ();
865 if (from_tty)
866 printf_filtered (_("Register cache flushed.\n"));
869 static void
870 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
871 const unsigned char *buf, long len)
873 int i;
874 switch (endian)
876 case BFD_ENDIAN_BIG:
877 for (i = 0; i < len; i++)
878 fprintf_unfiltered (file, "%02x", buf[i]);
879 break;
880 case BFD_ENDIAN_LITTLE:
881 for (i = len - 1; i >= 0; i--)
882 fprintf_unfiltered (file, "%02x", buf[i]);
883 break;
884 default:
885 internal_error (__FILE__, __LINE__, _("Bad switch"));
889 enum regcache_dump_what
891 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
894 static void
895 regcache_dump (struct regcache *regcache, struct ui_file *file,
896 enum regcache_dump_what what_to_dump)
898 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
899 struct gdbarch *gdbarch = regcache->descr->gdbarch;
900 int regnum;
901 int footnote_nr = 0;
902 int footnote_register_size = 0;
903 int footnote_register_offset = 0;
904 int footnote_register_type_name_null = 0;
905 long register_offset = 0;
906 unsigned char buf[MAX_REGISTER_SIZE];
908 #if 0
909 fprintf_unfiltered (file, "nr_raw_registers %d\n",
910 regcache->descr->nr_raw_registers);
911 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
912 regcache->descr->nr_cooked_registers);
913 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
914 regcache->descr->sizeof_raw_registers);
915 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
916 regcache->descr->sizeof_raw_register_valid_p);
917 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
918 gdbarch_num_regs (gdbarch));
919 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
920 gdbarch_num_pseudo_regs (gdbarch));
921 #endif
923 gdb_assert (regcache->descr->nr_cooked_registers
924 == (gdbarch_num_regs (gdbarch)
925 + gdbarch_num_pseudo_regs (gdbarch)));
927 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
929 /* Name. */
930 if (regnum < 0)
931 fprintf_unfiltered (file, " %-10s", "Name");
932 else
934 const char *p = gdbarch_register_name (gdbarch, regnum);
935 if (p == NULL)
936 p = "";
937 else if (p[0] == '\0')
938 p = "''";
939 fprintf_unfiltered (file, " %-10s", p);
942 /* Number. */
943 if (regnum < 0)
944 fprintf_unfiltered (file, " %4s", "Nr");
945 else
946 fprintf_unfiltered (file, " %4d", regnum);
948 /* Relative number. */
949 if (regnum < 0)
950 fprintf_unfiltered (file, " %4s", "Rel");
951 else if (regnum < gdbarch_num_regs (gdbarch))
952 fprintf_unfiltered (file, " %4d", regnum);
953 else
954 fprintf_unfiltered (file, " %4d",
955 (regnum - gdbarch_num_regs (gdbarch)));
957 /* Offset. */
958 if (regnum < 0)
959 fprintf_unfiltered (file, " %6s ", "Offset");
960 else
962 fprintf_unfiltered (file, " %6ld",
963 regcache->descr->register_offset[regnum]);
964 if (register_offset != regcache->descr->register_offset[regnum]
965 || (regnum > 0
966 && (regcache->descr->register_offset[regnum]
967 != (regcache->descr->register_offset[regnum - 1]
968 + regcache->descr->sizeof_register[regnum - 1])))
971 if (!footnote_register_offset)
972 footnote_register_offset = ++footnote_nr;
973 fprintf_unfiltered (file, "*%d", footnote_register_offset);
975 else
976 fprintf_unfiltered (file, " ");
977 register_offset = (regcache->descr->register_offset[regnum]
978 + regcache->descr->sizeof_register[regnum]);
981 /* Size. */
982 if (regnum < 0)
983 fprintf_unfiltered (file, " %5s ", "Size");
984 else
985 fprintf_unfiltered (file, " %5ld",
986 regcache->descr->sizeof_register[regnum]);
988 /* Type. */
990 const char *t;
991 if (regnum < 0)
992 t = "Type";
993 else
995 static const char blt[] = "builtin_type";
996 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
997 if (t == NULL)
999 char *n;
1000 if (!footnote_register_type_name_null)
1001 footnote_register_type_name_null = ++footnote_nr;
1002 n = xstrprintf ("*%d", footnote_register_type_name_null);
1003 make_cleanup (xfree, n);
1004 t = n;
1006 /* Chop a leading builtin_type. */
1007 if (strncmp (t, blt, strlen (blt)) == 0)
1008 t += strlen (blt);
1010 fprintf_unfiltered (file, " %-15s", t);
1013 /* Leading space always present. */
1014 fprintf_unfiltered (file, " ");
1016 /* Value, raw. */
1017 if (what_to_dump == regcache_dump_raw)
1019 if (regnum < 0)
1020 fprintf_unfiltered (file, "Raw value");
1021 else if (regnum >= regcache->descr->nr_raw_registers)
1022 fprintf_unfiltered (file, "<cooked>");
1023 else if (!regcache_valid_p (regcache, regnum))
1024 fprintf_unfiltered (file, "<invalid>");
1025 else
1027 regcache_raw_read (regcache, regnum, buf);
1028 fprintf_unfiltered (file, "0x");
1029 dump_endian_bytes (file,
1030 gdbarch_byte_order (gdbarch), buf,
1031 regcache->descr->sizeof_register[regnum]);
1035 /* Value, cooked. */
1036 if (what_to_dump == regcache_dump_cooked)
1038 if (regnum < 0)
1039 fprintf_unfiltered (file, "Cooked value");
1040 else
1042 regcache_cooked_read (regcache, regnum, buf);
1043 fprintf_unfiltered (file, "0x");
1044 dump_endian_bytes (file,
1045 gdbarch_byte_order (gdbarch), buf,
1046 regcache->descr->sizeof_register[regnum]);
1050 /* Group members. */
1051 if (what_to_dump == regcache_dump_groups)
1053 if (regnum < 0)
1054 fprintf_unfiltered (file, "Groups");
1055 else
1057 const char *sep = "";
1058 struct reggroup *group;
1059 for (group = reggroup_next (gdbarch, NULL);
1060 group != NULL;
1061 group = reggroup_next (gdbarch, group))
1063 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1065 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1066 sep = ",";
1072 fprintf_unfiltered (file, "\n");
1075 if (footnote_register_size)
1076 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1077 footnote_register_size);
1078 if (footnote_register_offset)
1079 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1080 footnote_register_offset);
1081 if (footnote_register_type_name_null)
1082 fprintf_unfiltered (file,
1083 "*%d: Register type's name NULL.\n",
1084 footnote_register_type_name_null);
1085 do_cleanups (cleanups);
1088 static void
1089 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1091 if (args == NULL)
1092 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1093 else
1095 struct cleanup *cleanups;
1096 struct ui_file *file = gdb_fopen (args, "w");
1097 if (file == NULL)
1098 perror_with_name (_("maintenance print architecture"));
1099 cleanups = make_cleanup_ui_file_delete (file);
1100 regcache_dump (get_current_regcache (), file, what_to_dump);
1101 do_cleanups (cleanups);
1105 static void
1106 maintenance_print_registers (char *args, int from_tty)
1108 regcache_print (args, regcache_dump_none);
1111 static void
1112 maintenance_print_raw_registers (char *args, int from_tty)
1114 regcache_print (args, regcache_dump_raw);
1117 static void
1118 maintenance_print_cooked_registers (char *args, int from_tty)
1120 regcache_print (args, regcache_dump_cooked);
1123 static void
1124 maintenance_print_register_groups (char *args, int from_tty)
1126 regcache_print (args, regcache_dump_groups);
1129 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1131 void
1132 _initialize_regcache (void)
1134 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1136 observer_attach_target_changed (regcache_observer_target_changed);
1137 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1139 add_com ("flushregs", class_maintenance, reg_flush_command,
1140 _("Force gdb to flush its register cache (maintainer command)"));
1142 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1143 Print the internal register configuration.\n\
1144 Takes an optional file parameter."), &maintenanceprintlist);
1145 add_cmd ("raw-registers", class_maintenance,
1146 maintenance_print_raw_registers, _("\
1147 Print the internal register configuration including raw values.\n\
1148 Takes an optional file parameter."), &maintenanceprintlist);
1149 add_cmd ("cooked-registers", class_maintenance,
1150 maintenance_print_cooked_registers, _("\
1151 Print the internal register configuration including cooked values.\n\
1152 Takes an optional file parameter."), &maintenanceprintlist);
1153 add_cmd ("register-groups", class_maintenance,
1154 maintenance_print_register_groups, _("\
1155 Print the internal register configuration including each register's group.\n\
1156 Takes an optional file parameter."),
1157 &maintenanceprintlist);