* target.h (struct target_ops): Add REGCACHE parameter to
[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
DJ
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2004, 2007 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
10 the Free Software Foundation; either version 2 of the License, or
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
19 along with this program; if not, write to the Free Software
197e01b6
EZ
20 Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
32178cab
MS
22
23#include "defs.h"
32178cab
MS
24#include "inferior.h"
25#include "target.h"
26#include "gdbarch.h"
705152c5 27#include "gdbcmd.h"
4e052eda 28#include "regcache.h"
b59ff9d5 29#include "reggroups.h"
61a0eb5b 30#include "gdb_assert.h"
b66d6d2e 31#include "gdb_string.h"
af030b9a 32#include "gdbcmd.h" /* For maintenanceprintlist. */
f4c5303c 33#include "observer.h"
32178cab
MS
34
35/*
36 * DATA STRUCTURE
37 *
38 * Here is the actual register cache.
39 */
40
3fadccb3
AC
41/* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created */
43
44struct gdbarch_data *regcache_descr_handle;
45
46struct regcache_descr
47{
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch *gdbarch;
50
bb1db049
AC
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
d2f0b918 54 registers then those registers and not the PC lives in the raw
bb1db049 55 cache. */
3fadccb3
AC
56 int nr_raw_registers;
57 long sizeof_raw_registers;
58 long sizeof_raw_register_valid_p;
59
d138e37a
AC
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
02f60eae 63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
d138e37a 64 both raw registers and memory by the architecture methods
02f60eae 65 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
d138e37a 66 int nr_cooked_registers;
067df2e5
AC
67 long sizeof_cooked_registers;
68 long sizeof_cooked_register_valid_p;
d138e37a
AC
69
70 /* Offset and size (in 8 bit bytes), of reach register in the
71 register cache. All registers (including those in the range
72 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
73 Assigning all registers an offset makes it possible to keep
74 legacy code, such as that found in read_register_bytes() and
75 write_register_bytes() working. */
3fadccb3 76 long *register_offset;
3fadccb3 77 long *sizeof_register;
3fadccb3 78
bb425013
AC
79 /* Cached table containing the type of each register. */
80 struct type **register_type;
3fadccb3
AC
81};
82
3fadccb3
AC
83static void *
84init_regcache_descr (struct gdbarch *gdbarch)
85{
86 int i;
87 struct regcache_descr *descr;
88 gdb_assert (gdbarch != NULL);
89
bb425013 90 /* Create an initial, zero filled, table. */
116f06ea 91 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
3fadccb3 92 descr->gdbarch = gdbarch;
3fadccb3 93
d138e37a
AC
94 /* Total size of the register space. The raw registers are mapped
95 directly onto the raw register cache while the pseudo's are
3fadccb3 96 either mapped onto raw-registers or memory. */
d138e37a 97 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
067df2e5 98 descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
3fadccb3 99
bb425013 100 /* Fill in a table of register types. */
116f06ea
AC
101 descr->register_type
102 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, 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. */
108 descr->nr_raw_registers = NUM_REGS;
109
110 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111 array. This pretects GDB from erant code that accesses elements
112 of the global register_valid_p[] array in the range [NUM_REGS
113 .. NUM_REGS + NUM_PSEUDO_REGS). */
114 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
115
067df2e5 116 /* Lay out the register cache.
3fadccb3 117
bb425013
AC
118 NOTE: cagney/2002-05-22: Only register_type() is used when
119 constructing the register cache. It is assumed that the
120 register's raw size, virtual size and type length are all the
121 same. */
3fadccb3
AC
122
123 {
124 long offset = 0;
116f06ea
AC
125 descr->sizeof_register
126 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
127 descr->register_offset
128 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
d138e37a 129 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3 130 {
bb425013 131 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
3fadccb3
AC
132 descr->register_offset[i] = offset;
133 offset += descr->sizeof_register[i];
123a958e 134 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
3fadccb3
AC
135 }
136 /* Set the real size of the register cache buffer. */
067df2e5 137 descr->sizeof_cooked_registers = offset;
3fadccb3
AC
138 }
139
067df2e5 140 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
ce2826aa 141 the raw registers. Unfortunately some code still accesses the
067df2e5
AC
142 register array directly using the global registers[]. Until that
143 code has been purged, play safe and over allocating the register
144 buffer. Ulgh! */
145 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
146
3fadccb3
AC
147 return descr;
148}
149
150static struct regcache_descr *
151regcache_descr (struct gdbarch *gdbarch)
152{
153 return gdbarch_data (gdbarch, regcache_descr_handle);
154}
155
bb425013
AC
156/* Utility functions returning useful register attributes stored in
157 the regcache descr. */
158
159struct type *
160register_type (struct gdbarch *gdbarch, int regnum)
161{
162 struct regcache_descr *descr = regcache_descr (gdbarch);
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;
175 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
176 size = descr->sizeof_register[regnum];
08a617da
AC
177 return size;
178}
179
3fadccb3
AC
180/* The register cache for storing raw register values. */
181
182struct regcache
183{
184 struct regcache_descr *descr;
51b1fe4e
AC
185 /* The register buffers. A read-only register cache can hold the
186 full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
187 register cache can only hold [0 .. NUM_REGS). */
2d522557 188 gdb_byte *registers;
b05e64e5
FR
189 /* Register cache status:
190 register_valid_p[REG] == 0 if REG value is not in the cache
191 > 0 if REG value is in the cache
192 < 0 if REG value is permanently unavailable */
193 signed char *register_valid_p;
2d28509a
AC
194 /* Is this a read-only cache? A read-only cache is used for saving
195 the target's register state (e.g, across an inferior function
196 call or just before forcing a function return). A read-only
197 cache can only be updated via the methods regcache_dup() and
198 regcache_cpy(). The actual contents are determined by the
199 reggroup_save and reggroup_restore methods. */
200 int readonly_p;
3fadccb3
AC
201};
202
203struct regcache *
204regcache_xmalloc (struct gdbarch *gdbarch)
205{
206 struct regcache_descr *descr;
207 struct regcache *regcache;
208 gdb_assert (gdbarch != NULL);
209 descr = regcache_descr (gdbarch);
210 regcache = XMALLOC (struct regcache);
211 regcache->descr = descr;
51b1fe4e 212 regcache->registers
2d522557 213 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
51b1fe4e 214 regcache->register_valid_p
2d522557 215 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
2d28509a 216 regcache->readonly_p = 1;
3fadccb3
AC
217 return regcache;
218}
219
220void
221regcache_xfree (struct regcache *regcache)
222{
223 if (regcache == NULL)
224 return;
51b1fe4e
AC
225 xfree (regcache->registers);
226 xfree (regcache->register_valid_p);
3fadccb3
AC
227 xfree (regcache);
228}
229
b9362cc7 230static void
36160dc4
AC
231do_regcache_xfree (void *data)
232{
233 regcache_xfree (data);
234}
235
236struct cleanup *
237make_cleanup_regcache_xfree (struct regcache *regcache)
238{
239 return make_cleanup (do_regcache_xfree, regcache);
240}
241
41d35cb0
MK
242/* Return REGCACHE's architecture. */
243
244struct gdbarch *
245get_regcache_arch (const struct regcache *regcache)
246{
247 return regcache->descr->gdbarch;
248}
249
51b1fe4e
AC
250/* Return a pointer to register REGNUM's buffer cache. */
251
2d522557 252static gdb_byte *
9a661b68 253register_buffer (const struct regcache *regcache, int regnum)
51b1fe4e
AC
254{
255 return regcache->registers + regcache->descr->register_offset[regnum];
256}
257
2d28509a 258void
5602984a
AC
259regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
260 void *src)
2d28509a
AC
261{
262 struct gdbarch *gdbarch = dst->descr->gdbarch;
2d522557 263 gdb_byte buf[MAX_REGISTER_SIZE];
2d28509a 264 int regnum;
2d28509a 265 /* The DST should be `read-only', if it wasn't then the save would
5602984a 266 end up trying to write the register values back out to the
2d28509a 267 target. */
2d28509a
AC
268 gdb_assert (dst->readonly_p);
269 /* Clear the dest. */
270 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
271 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
272 /* Copy over any registers (identified by their membership in the
5602984a
AC
273 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
274 NUM_PSEUDO_REGS) range is checked since some architectures need
275 to save/restore `cooked' registers that live in memory. */
2d28509a
AC
276 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
277 {
278 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
279 {
5602984a
AC
280 int valid = cooked_read (src, regnum, buf);
281 if (valid)
282 {
283 memcpy (register_buffer (dst, regnum), buf,
284 register_size (gdbarch, regnum));
285 dst->register_valid_p[regnum] = 1;
286 }
2d28509a
AC
287 }
288 }
289}
290
291void
5602984a
AC
292regcache_restore (struct regcache *dst,
293 regcache_cooked_read_ftype *cooked_read,
2d522557 294 void *cooked_read_context)
2d28509a
AC
295{
296 struct gdbarch *gdbarch = dst->descr->gdbarch;
2d522557 297 gdb_byte buf[MAX_REGISTER_SIZE];
2d28509a 298 int regnum;
5602984a
AC
299 /* The dst had better not be read-only. If it is, the `restore'
300 doesn't make much sense. */
2d28509a 301 gdb_assert (!dst->readonly_p);
2d28509a 302 /* Copy over any registers, being careful to only restore those that
5602984a
AC
303 were both saved and need to be restored. The full [0 .. NUM_REGS
304 + NUM_PSEUDO_REGS) range is checked since some architectures need
305 to save/restore `cooked' registers that live in memory. */
306 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
2d28509a 307 {
5602984a 308 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
2d28509a 309 {
2d522557 310 int valid = cooked_read (cooked_read_context, regnum, buf);
5602984a
AC
311 if (valid)
312 regcache_cooked_write (dst, regnum, buf);
2d28509a
AC
313 }
314 }
315}
316
5602984a 317static int
2d522557 318do_cooked_read (void *src, int regnum, gdb_byte *buf)
5602984a
AC
319{
320 struct regcache *regcache = src;
6f4e5a41 321 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
5602984a
AC
322 /* Don't even think about fetching a register from a read-only
323 cache when the register isn't yet valid. There isn't a target
324 from which the register value can be fetched. */
325 return 0;
326 regcache_cooked_read (regcache, regnum, buf);
327 return 1;
328}
329
330
3fadccb3
AC
331void
332regcache_cpy (struct regcache *dst, struct regcache *src)
333{
334 int i;
2d522557 335 gdb_byte *buf;
3fadccb3
AC
336 gdb_assert (src != NULL && dst != NULL);
337 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
338 gdb_assert (src != dst);
2d28509a
AC
339 gdb_assert (src->readonly_p || dst->readonly_p);
340 if (!src->readonly_p)
5602984a 341 regcache_save (dst, do_cooked_read, src);
2d28509a 342 else if (!dst->readonly_p)
5602984a 343 regcache_restore (dst, do_cooked_read, src);
2d28509a
AC
344 else
345 regcache_cpy_no_passthrough (dst, src);
3fadccb3
AC
346}
347
348void
349regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
350{
351 int i;
352 gdb_assert (src != NULL && dst != NULL);
353 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
354 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
355 move of data into the current_regcache(). Doing this would be
9564ee9f 356 silly - it would mean that valid_p would be completely invalid. */
3fadccb3 357 gdb_assert (dst != current_regcache);
51b1fe4e
AC
358 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
359 memcpy (dst->register_valid_p, src->register_valid_p,
3fadccb3
AC
360 dst->descr->sizeof_raw_register_valid_p);
361}
362
363struct regcache *
364regcache_dup (struct regcache *src)
365{
366 struct regcache *newbuf;
367 gdb_assert (current_regcache != NULL);
368 newbuf = regcache_xmalloc (src->descr->gdbarch);
369 regcache_cpy (newbuf, src);
370 return newbuf;
371}
372
373struct regcache *
374regcache_dup_no_passthrough (struct regcache *src)
375{
376 struct regcache *newbuf;
377 gdb_assert (current_regcache != NULL);
378 newbuf = regcache_xmalloc (src->descr->gdbarch);
379 regcache_cpy_no_passthrough (newbuf, src);
380 return newbuf;
381}
382
383int
6ed7ea50 384regcache_valid_p (const struct regcache *regcache, int regnum)
3fadccb3
AC
385{
386 gdb_assert (regcache != NULL);
6ed7ea50
UW
387 gdb_assert (regnum >= 0);
388 if (regcache->readonly_p)
389 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
390 else
391 gdb_assert (regnum < regcache->descr->nr_raw_registers);
392
51b1fe4e 393 return regcache->register_valid_p[regnum];
3fadccb3
AC
394}
395
3fadccb3
AC
396/* Global structure containing the current regcache. */
397/* FIXME: cagney/2002-05-11: The two global arrays registers[] and
8262ee23 398 deprecated_register_valid[] currently point into this structure. */
3fadccb3
AC
399struct regcache *current_regcache;
400
5ebd2499 401/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
402 recording if the register values have been changed (eg. by the
403 user). Therefore all registers must be written back to the
404 target when appropriate. */
405
39f77062 406/* The thread/process associated with the current set of registers. */
32178cab 407
39f77062 408static ptid_t registers_ptid;
32178cab
MS
409
410/*
411 * FUNCTIONS:
412 */
413
414/* REGISTER_CACHED()
415
416 Returns 0 if the value is not in the cache (needs fetch).
417 >0 if the value is in the cache.
418 <0 if the value is permanently unavailable (don't ask again). */
419
420int
421register_cached (int regnum)
422{
8851ec7a 423 return current_regcache->register_valid_p[regnum];
32178cab
MS
424}
425
7302a204
ND
426/* Record that REGNUM's value is cached if STATE is >0, uncached but
427 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
428
429void
430set_register_cached (int regnum, int state)
431{
53826de9
AC
432 gdb_assert (regnum >= 0);
433 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
51b1fe4e 434 current_regcache->register_valid_p[regnum] = state;
7302a204
ND
435}
436
f4c5303c
OF
437/* Observer for the target_changed event. */
438
439void
440regcache_observer_target_changed (struct target_ops *target)
441{
442 registers_changed ();
443}
444
32178cab
MS
445/* Low level examining and depositing of registers.
446
447 The caller is responsible for making sure that the inferior is
448 stopped before calling the fetching routines, or it will get
449 garbage. (a change from GDB version 3, in which the caller got the
450 value from the last stop). */
451
452/* REGISTERS_CHANGED ()
453
454 Indicate that registers may have changed, so invalidate the cache. */
455
456void
457registers_changed (void)
458{
459 int i;
32178cab 460
39f77062 461 registers_ptid = pid_to_ptid (-1);
32178cab
MS
462
463 /* Force cleanup of any alloca areas if using C alloca instead of
464 a builtin alloca. This particular call is used to clean up
465 areas allocated by low level target code which may build up
466 during lengthy interactions between gdb and the target before
467 gdb gives control to the user (ie watchpoints). */
468 alloca (0);
469
53826de9 470 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
7302a204 471 set_register_cached (i, 0);
32178cab
MS
472}
473
2b9e5f3f 474/* DEPRECATED_REGISTERS_FETCHED ()
32178cab
MS
475
476 Indicate that all registers have been fetched, so mark them all valid. */
477
31e9866e
AC
478/* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
479 code was blatting the registers[] array and then calling this.
23a6d369 480 Since targets should only be using regcache_raw_supply() the need for
31e9866e 481 this function/hack is eliminated. */
32178cab
MS
482
483void
2b9e5f3f 484deprecated_registers_fetched (void)
32178cab
MS
485{
486 int i;
32178cab 487
a728f042 488 for (i = 0; i < NUM_REGS; i++)
7302a204 489 set_register_cached (i, 1);
fcdc5976 490 /* Do not assume that the pseudo-regs have also been fetched.
31e9866e 491 Fetching all real regs NEVER accounts for pseudo-regs. */
32178cab
MS
492}
493
61a0eb5b 494void
2d522557 495regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
61a0eb5b 496{
3fadccb3
AC
497 gdb_assert (regcache != NULL && buf != NULL);
498 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
3fadccb3
AC
499 /* Make certain that the register cache is up-to-date with respect
500 to the current thread. This switching shouldn't be necessary
501 only there is still only one target side register cache. Sigh!
502 On the bright side, at least there is a regcache object. */
2d28509a 503 if (!regcache->readonly_p)
3fadccb3
AC
504 {
505 gdb_assert (regcache == current_regcache);
506 if (! ptid_equal (registers_ptid, inferior_ptid))
507 {
508 registers_changed ();
509 registers_ptid = inferior_ptid;
510 }
56be3814
UW
511 if (!regcache_valid_p (regcache, regnum))
512 target_fetch_registers (regcache, regnum);
0a8146bf
AC
513#if 0
514 /* FIXME: cagney/2004-08-07: At present a number of targets
04c663e3
DA
515 forget (or didn't know that they needed) to set this leading to
516 panics. Also is the problem that targets need to indicate
0a8146bf
AC
517 that a register is in one of the possible states: valid,
518 undefined, unknown. The last of which isn't yet
519 possible. */
7ab3286f 520 gdb_assert (register_cached (regnum));
0a8146bf 521#endif
3fadccb3
AC
522 }
523 /* Copy the value directly into the register cache. */
51b1fe4e 524 memcpy (buf, register_buffer (regcache, regnum),
3fadccb3 525 regcache->descr->sizeof_register[regnum]);
61a0eb5b
AC
526}
527
28fc6740
AC
528void
529regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
530{
2d522557 531 gdb_byte *buf;
28fc6740
AC
532 gdb_assert (regcache != NULL);
533 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
534 buf = alloca (regcache->descr->sizeof_register[regnum]);
535 regcache_raw_read (regcache, regnum, buf);
536 (*val) = extract_signed_integer (buf,
537 regcache->descr->sizeof_register[regnum]);
538}
539
540void
541regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
542 ULONGEST *val)
543{
2d522557 544 gdb_byte *buf;
28fc6740
AC
545 gdb_assert (regcache != NULL);
546 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
547 buf = alloca (regcache->descr->sizeof_register[regnum]);
548 regcache_raw_read (regcache, regnum, buf);
549 (*val) = extract_unsigned_integer (buf,
550 regcache->descr->sizeof_register[regnum]);
551}
552
c00dcbe9
MK
553void
554regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
555{
556 void *buf;
557 gdb_assert (regcache != NULL);
558 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
559 buf = alloca (regcache->descr->sizeof_register[regnum]);
560 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
561 regcache_raw_write (regcache, regnum, buf);
562}
563
564void
565regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
566 ULONGEST val)
567{
568 void *buf;
569 gdb_assert (regcache != NULL);
570 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
571 buf = alloca (regcache->descr->sizeof_register[regnum]);
572 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
573 regcache_raw_write (regcache, regnum, buf);
574}
575
68365089 576void
2d522557 577regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
68365089 578{
d138e37a 579 gdb_assert (regnum >= 0);
68365089
AC
580 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
581 if (regnum < regcache->descr->nr_raw_registers)
582 regcache_raw_read (regcache, regnum, buf);
2d28509a
AC
583 else if (regcache->readonly_p
584 && regnum < regcache->descr->nr_cooked_registers
585 && regcache->register_valid_p[regnum])
b2fa5097 586 /* Read-only register cache, perhaps the cooked value was cached? */
2d28509a
AC
587 memcpy (buf, register_buffer (regcache, regnum),
588 regcache->descr->sizeof_register[regnum]);
d138e37a 589 else
68365089
AC
590 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
591 regnum, buf);
61a0eb5b
AC
592}
593
a378f419
AC
594void
595regcache_cooked_read_signed (struct regcache *regcache, int regnum,
596 LONGEST *val)
597{
2d522557 598 gdb_byte *buf;
a378f419 599 gdb_assert (regcache != NULL);
a66a9c23 600 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
601 buf = alloca (regcache->descr->sizeof_register[regnum]);
602 regcache_cooked_read (regcache, regnum, buf);
603 (*val) = extract_signed_integer (buf,
604 regcache->descr->sizeof_register[regnum]);
605}
606
607void
608regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
609 ULONGEST *val)
610{
2d522557 611 gdb_byte *buf;
a378f419 612 gdb_assert (regcache != NULL);
a66a9c23 613 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
614 buf = alloca (regcache->descr->sizeof_register[regnum]);
615 regcache_cooked_read (regcache, regnum, buf);
616 (*val) = extract_unsigned_integer (buf,
617 regcache->descr->sizeof_register[regnum]);
618}
619
a66a9c23
AC
620void
621regcache_cooked_write_signed (struct regcache *regcache, int regnum,
622 LONGEST val)
623{
624 void *buf;
625 gdb_assert (regcache != NULL);
626 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
627 buf = alloca (regcache->descr->sizeof_register[regnum]);
628 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
629 regcache_cooked_write (regcache, regnum, buf);
630}
631
632void
633regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
634 ULONGEST val)
635{
636 void *buf;
637 gdb_assert (regcache != NULL);
638 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
639 buf = alloca (regcache->descr->sizeof_register[regnum]);
640 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
641 regcache_cooked_write (regcache, regnum, buf);
642}
643
61a0eb5b 644void
2d522557
AC
645regcache_raw_write (struct regcache *regcache, int regnum,
646 const gdb_byte *buf)
61a0eb5b 647{
3fadccb3
AC
648 gdb_assert (regcache != NULL && buf != NULL);
649 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
2d28509a 650 gdb_assert (!regcache->readonly_p);
3fadccb3 651
3fadccb3
AC
652 /* On the sparc, writing %g0 is a no-op, so we don't even want to
653 change the registers array if something writes to this register. */
654 if (CANNOT_STORE_REGISTER (regnum))
655 return;
656
3fadccb3
AC
657 /* Make certain that the correct cache is selected. */
658 gdb_assert (regcache == current_regcache);
659 if (! ptid_equal (registers_ptid, inferior_ptid))
660 {
661 registers_changed ();
662 registers_ptid = inferior_ptid;
663 }
664
665 /* If we have a valid copy of the register, and new value == old
666 value, then don't bother doing the actual store. */
667 if (regcache_valid_p (regcache, regnum)
668 && (memcmp (register_buffer (regcache, regnum), buf,
669 regcache->descr->sizeof_register[regnum]) == 0))
670 return;
671
316f2060 672 target_prepare_to_store (regcache);
3fadccb3
AC
673 memcpy (register_buffer (regcache, regnum), buf,
674 regcache->descr->sizeof_register[regnum]);
51b1fe4e 675 regcache->register_valid_p[regnum] = 1;
56be3814 676 target_store_registers (regcache, regnum);
61a0eb5b
AC
677}
678
68365089 679void
2d522557
AC
680regcache_cooked_write (struct regcache *regcache, int regnum,
681 const gdb_byte *buf)
68365089 682{
d138e37a 683 gdb_assert (regnum >= 0);
68365089
AC
684 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
685 if (regnum < regcache->descr->nr_raw_registers)
686 regcache_raw_write (regcache, regnum, buf);
d138e37a 687 else
68365089 688 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
d8124050 689 regnum, buf);
61a0eb5b
AC
690}
691
06c0b04e
AC
692/* Perform a partial register transfer using a read, modify, write
693 operation. */
694
695typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
696 void *buf);
697typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
698 const void *buf);
699
b9362cc7 700static void
06c0b04e
AC
701regcache_xfer_part (struct regcache *regcache, int regnum,
702 int offset, int len, void *in, const void *out,
2d522557
AC
703 void (*read) (struct regcache *regcache, int regnum,
704 gdb_byte *buf),
705 void (*write) (struct regcache *regcache, int regnum,
706 const gdb_byte *buf))
06c0b04e
AC
707{
708 struct regcache_descr *descr = regcache->descr;
fc1a4b47 709 gdb_byte reg[MAX_REGISTER_SIZE];
06c0b04e
AC
710 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
711 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
712 /* Something to do? */
713 if (offset + len == 0)
714 return;
715 /* Read (when needed) ... */
716 if (in != NULL
717 || offset > 0
718 || offset + len < descr->sizeof_register[regnum])
719 {
720 gdb_assert (read != NULL);
721 read (regcache, regnum, reg);
722 }
723 /* ... modify ... */
724 if (in != NULL)
725 memcpy (in, reg + offset, len);
726 if (out != NULL)
727 memcpy (reg + offset, out, len);
728 /* ... write (when needed). */
729 if (out != NULL)
730 {
731 gdb_assert (write != NULL);
732 write (regcache, regnum, reg);
733 }
734}
735
736void
737regcache_raw_read_part (struct regcache *regcache, int regnum,
2d522557 738 int offset, int len, gdb_byte *buf)
06c0b04e
AC
739{
740 struct regcache_descr *descr = regcache->descr;
741 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
742 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
743 regcache_raw_read, regcache_raw_write);
744}
745
746void
747regcache_raw_write_part (struct regcache *regcache, int regnum,
2d522557 748 int offset, int len, const gdb_byte *buf)
06c0b04e
AC
749{
750 struct regcache_descr *descr = regcache->descr;
751 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
752 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
753 regcache_raw_read, regcache_raw_write);
754}
755
756void
757regcache_cooked_read_part (struct regcache *regcache, int regnum,
2d522557 758 int offset, int len, gdb_byte *buf)
06c0b04e
AC
759{
760 struct regcache_descr *descr = regcache->descr;
761 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
762 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
763 regcache_cooked_read, regcache_cooked_write);
764}
765
766void
767regcache_cooked_write_part (struct regcache *regcache, int regnum,
2d522557 768 int offset, int len, const gdb_byte *buf)
06c0b04e
AC
769{
770 struct regcache_descr *descr = regcache->descr;
771 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
772 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
773 regcache_cooked_read, regcache_cooked_write);
774}
32178cab 775
d3b22ed5
AC
776/* Hack to keep code that view the register buffer as raw bytes
777 working. */
778
779int
780register_offset_hack (struct gdbarch *gdbarch, int regnum)
781{
782 struct regcache_descr *descr = regcache_descr (gdbarch);
783 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
784 return descr->register_offset[regnum];
785}
786
5ebd2499 787/* Return the contents of register REGNUM as an unsigned integer. */
32178cab 788
173155e8 789ULONGEST
5ebd2499 790read_register (int regnum)
32178cab 791{
2d522557 792 gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
81c4a259
UW
793 gdb_assert (current_regcache != NULL);
794 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
795 regcache_cooked_read (current_regcache, regnum, buf);
3acba339 796 return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
32178cab
MS
797}
798
173155e8 799ULONGEST
39f77062 800read_register_pid (int regnum, ptid_t ptid)
32178cab 801{
39f77062 802 ptid_t save_ptid;
32178cab
MS
803 int save_pid;
804 CORE_ADDR retval;
805
39f77062 806 if (ptid_equal (ptid, inferior_ptid))
5ebd2499 807 return read_register (regnum);
32178cab 808
39f77062 809 save_ptid = inferior_ptid;
32178cab 810
39f77062 811 inferior_ptid = ptid;
32178cab 812
5ebd2499 813 retval = read_register (regnum);
32178cab 814
39f77062 815 inferior_ptid = save_ptid;
32178cab
MS
816
817 return retval;
818}
819
5ebd2499 820/* Store VALUE into the raw contents of register number REGNUM. */
32178cab
MS
821
822void
5ebd2499 823write_register (int regnum, LONGEST val)
32178cab 824{
61a0eb5b 825 void *buf;
32178cab 826 int size;
3acba339 827 size = register_size (current_gdbarch, regnum);
32178cab
MS
828 buf = alloca (size);
829 store_signed_integer (buf, size, (LONGEST) val);
81c4a259
UW
830 gdb_assert (current_regcache != NULL);
831 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
832 regcache_cooked_write (current_regcache, regnum, buf);
32178cab
MS
833}
834
835void
39f77062 836write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
32178cab 837{
39f77062 838 ptid_t save_ptid;
32178cab 839
39f77062 840 if (ptid_equal (ptid, inferior_ptid))
32178cab 841 {
5ebd2499 842 write_register (regnum, val);
32178cab
MS
843 return;
844 }
845
39f77062 846 save_ptid = inferior_ptid;
32178cab 847
39f77062 848 inferior_ptid = ptid;
32178cab 849
5ebd2499 850 write_register (regnum, val);
32178cab 851
39f77062 852 inferior_ptid = save_ptid;
32178cab
MS
853}
854
a16d75cc 855/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
9a661b68
MK
856
857void
6618125d 858regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
9a661b68
MK
859{
860 void *regbuf;
861 size_t size;
862
a16d75cc 863 gdb_assert (regcache != NULL);
9a661b68
MK
864 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
865 gdb_assert (!regcache->readonly_p);
866
867 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
868 CURRENT_REGCACHE specially here. */
869 if (regcache == current_regcache
870 && !ptid_equal (registers_ptid, inferior_ptid))
871 {
872 registers_changed ();
873 registers_ptid = inferior_ptid;
874 }
875
876 regbuf = register_buffer (regcache, regnum);
877 size = regcache->descr->sizeof_register[regnum];
878
879 if (buf)
880 memcpy (regbuf, buf, size);
881 else
882 memset (regbuf, 0, size);
883
884 /* Mark the register as cached. */
885 regcache->register_valid_p[regnum] = 1;
886}
887
888/* Collect register REGNUM from REGCACHE and store its contents in BUF. */
889
890void
6618125d 891regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
9a661b68
MK
892{
893 const void *regbuf;
894 size_t size;
895
896 gdb_assert (regcache != NULL && buf != NULL);
897 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
898
899 regbuf = register_buffer (regcache, regnum);
900 size = regcache->descr->sizeof_register[regnum];
901 memcpy (buf, regbuf, size);
902}
903
193cb69f 904
9c8dbfa9
AC
905/* read_pc, write_pc, read_sp, etc. Special handling for registers
906 PC, SP, and FP. */
32178cab 907
9c8dbfa9
AC
908/* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
909 read_sp(), will eventually be replaced by per-frame methods.
910 Instead of relying on the global INFERIOR_PTID, they will use the
911 contextual information provided by the FRAME. These functions do
912 not belong in the register cache. */
32178cab 913
cde9ea48 914/* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
9c8dbfa9
AC
915 write_pc_pid() and write_pc(), all need to be replaced by something
916 that does not rely on global state. But what? */
32178cab
MS
917
918CORE_ADDR
39f77062 919read_pc_pid (ptid_t ptid)
32178cab 920{
39f77062 921 ptid_t saved_inferior_ptid;
32178cab
MS
922 CORE_ADDR pc_val;
923
39f77062
KB
924 /* In case ptid != inferior_ptid. */
925 saved_inferior_ptid = inferior_ptid;
926 inferior_ptid = ptid;
32178cab 927
cde9ea48
AC
928 if (TARGET_READ_PC_P ())
929 pc_val = TARGET_READ_PC (ptid);
930 /* Else use per-frame method on get_current_frame. */
931 else if (PC_REGNUM >= 0)
932 {
933 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
6ba34a8d 934 pc_val = ADDR_BITS_REMOVE (raw_val);
cde9ea48
AC
935 }
936 else
e2e0b3e5 937 internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
32178cab 938
39f77062 939 inferior_ptid = saved_inferior_ptid;
32178cab
MS
940 return pc_val;
941}
942
943CORE_ADDR
944read_pc (void)
945{
39f77062 946 return read_pc_pid (inferior_ptid);
32178cab
MS
947}
948
32178cab 949void
39f77062 950generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
32178cab 951{
32178cab 952 if (PC_REGNUM >= 0)
39f77062 953 write_register_pid (PC_REGNUM, pc, ptid);
afb18d0f
AC
954 else
955 internal_error (__FILE__, __LINE__,
e2e0b3e5 956 _("generic_target_write_pc"));
32178cab
MS
957}
958
959void
39f77062 960write_pc_pid (CORE_ADDR pc, ptid_t ptid)
32178cab 961{
39f77062 962 ptid_t saved_inferior_ptid;
32178cab 963
39f77062
KB
964 /* In case ptid != inferior_ptid. */
965 saved_inferior_ptid = inferior_ptid;
966 inferior_ptid = ptid;
32178cab 967
39f77062 968 TARGET_WRITE_PC (pc, ptid);
32178cab 969
39f77062 970 inferior_ptid = saved_inferior_ptid;
32178cab
MS
971}
972
973void
974write_pc (CORE_ADDR pc)
975{
39f77062 976 write_pc_pid (pc, inferior_ptid);
32178cab
MS
977}
978
979/* Cope with strage ways of getting to the stack and frame pointers */
980
32178cab
MS
981CORE_ADDR
982read_sp (void)
983{
bd1ce8ba
AC
984 if (TARGET_READ_SP_P ())
985 return TARGET_READ_SP ();
a9e5fdc2
AC
986 else if (gdbarch_unwind_sp_p (current_gdbarch))
987 return get_frame_sp (get_current_frame ());
bd1ce8ba 988 else if (SP_REGNUM >= 0)
a9e5fdc2
AC
989 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
990 about the architecture so put it at the end. */
bd1ce8ba 991 return read_register (SP_REGNUM);
e2e0b3e5 992 internal_error (__FILE__, __LINE__, _("read_sp: Unable to find SP"));
32178cab
MS
993}
994
705152c5
MS
995static void
996reg_flush_command (char *command, int from_tty)
997{
998 /* Force-flush the register cache. */
999 registers_changed ();
1000 if (from_tty)
a3f17187 1001 printf_filtered (_("Register cache flushed.\n"));
705152c5
MS
1002}
1003
32178cab
MS
1004static void
1005build_regcache (void)
3fadccb3
AC
1006{
1007 current_regcache = regcache_xmalloc (current_gdbarch);
2d28509a 1008 current_regcache->readonly_p = 0;
3fadccb3
AC
1009}
1010
af030b9a
AC
1011static void
1012dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1013 const unsigned char *buf, long len)
1014{
1015 int i;
1016 switch (endian)
1017 {
1018 case BFD_ENDIAN_BIG:
1019 for (i = 0; i < len; i++)
1020 fprintf_unfiltered (file, "%02x", buf[i]);
1021 break;
1022 case BFD_ENDIAN_LITTLE:
1023 for (i = len - 1; i >= 0; i--)
1024 fprintf_unfiltered (file, "%02x", buf[i]);
1025 break;
1026 default:
e2e0b3e5 1027 internal_error (__FILE__, __LINE__, _("Bad switch"));
af030b9a
AC
1028 }
1029}
1030
1031enum regcache_dump_what
1032{
b59ff9d5 1033 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
af030b9a
AC
1034};
1035
1036static void
1037regcache_dump (struct regcache *regcache, struct ui_file *file,
1038 enum regcache_dump_what what_to_dump)
1039{
1040 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
b59ff9d5 1041 struct gdbarch *gdbarch = regcache->descr->gdbarch;
af030b9a
AC
1042 int regnum;
1043 int footnote_nr = 0;
1044 int footnote_register_size = 0;
1045 int footnote_register_offset = 0;
1046 int footnote_register_type_name_null = 0;
1047 long register_offset = 0;
123a958e 1048 unsigned char buf[MAX_REGISTER_SIZE];
af030b9a
AC
1049
1050#if 0
af030b9a
AC
1051 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1052 regcache->descr->nr_raw_registers);
1053 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1054 regcache->descr->nr_cooked_registers);
1055 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1056 regcache->descr->sizeof_raw_registers);
1057 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1058 regcache->descr->sizeof_raw_register_valid_p);
af030b9a
AC
1059 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1060 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1061#endif
1062
1063 gdb_assert (regcache->descr->nr_cooked_registers
1064 == (NUM_REGS + NUM_PSEUDO_REGS));
1065
1066 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1067 {
1068 /* Name. */
1069 if (regnum < 0)
1070 fprintf_unfiltered (file, " %-10s", "Name");
1071 else
1072 {
1073 const char *p = REGISTER_NAME (regnum);
1074 if (p == NULL)
1075 p = "";
1076 else if (p[0] == '\0')
1077 p = "''";
1078 fprintf_unfiltered (file, " %-10s", p);
1079 }
1080
1081 /* Number. */
1082 if (regnum < 0)
1083 fprintf_unfiltered (file, " %4s", "Nr");
1084 else
1085 fprintf_unfiltered (file, " %4d", regnum);
1086
1087 /* Relative number. */
1088 if (regnum < 0)
1089 fprintf_unfiltered (file, " %4s", "Rel");
1090 else if (regnum < NUM_REGS)
1091 fprintf_unfiltered (file, " %4d", regnum);
1092 else
1093 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1094
1095 /* Offset. */
1096 if (regnum < 0)
1097 fprintf_unfiltered (file, " %6s ", "Offset");
1098 else
1099 {
1100 fprintf_unfiltered (file, " %6ld",
1101 regcache->descr->register_offset[regnum]);
a7e3c2ad 1102 if (register_offset != regcache->descr->register_offset[regnum]
d3b22ed5
AC
1103 || (regnum > 0
1104 && (regcache->descr->register_offset[regnum]
1105 != (regcache->descr->register_offset[regnum - 1]
1106 + regcache->descr->sizeof_register[regnum - 1])))
1107 )
af030b9a
AC
1108 {
1109 if (!footnote_register_offset)
1110 footnote_register_offset = ++footnote_nr;
1111 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1112 }
1113 else
1114 fprintf_unfiltered (file, " ");
1115 register_offset = (regcache->descr->register_offset[regnum]
1116 + regcache->descr->sizeof_register[regnum]);
1117 }
1118
1119 /* Size. */
1120 if (regnum < 0)
1121 fprintf_unfiltered (file, " %5s ", "Size");
1122 else
01e1877c
AC
1123 fprintf_unfiltered (file, " %5ld",
1124 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1125
1126 /* Type. */
b59ff9d5
AC
1127 {
1128 const char *t;
1129 if (regnum < 0)
1130 t = "Type";
1131 else
1132 {
1133 static const char blt[] = "builtin_type";
1134 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1135 if (t == NULL)
1136 {
1137 char *n;
1138 if (!footnote_register_type_name_null)
1139 footnote_register_type_name_null = ++footnote_nr;
b435e160 1140 n = xstrprintf ("*%d", footnote_register_type_name_null);
b59ff9d5
AC
1141 make_cleanup (xfree, n);
1142 t = n;
1143 }
1144 /* Chop a leading builtin_type. */
1145 if (strncmp (t, blt, strlen (blt)) == 0)
1146 t += strlen (blt);
1147 }
1148 fprintf_unfiltered (file, " %-15s", t);
1149 }
1150
1151 /* Leading space always present. */
1152 fprintf_unfiltered (file, " ");
af030b9a
AC
1153
1154 /* Value, raw. */
1155 if (what_to_dump == regcache_dump_raw)
1156 {
1157 if (regnum < 0)
1158 fprintf_unfiltered (file, "Raw value");
1159 else if (regnum >= regcache->descr->nr_raw_registers)
1160 fprintf_unfiltered (file, "<cooked>");
1161 else if (!regcache_valid_p (regcache, regnum))
1162 fprintf_unfiltered (file, "<invalid>");
1163 else
1164 {
1165 regcache_raw_read (regcache, regnum, buf);
1166 fprintf_unfiltered (file, "0x");
1167 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
01e1877c 1168 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1169 }
1170 }
1171
1172 /* Value, cooked. */
1173 if (what_to_dump == regcache_dump_cooked)
1174 {
1175 if (regnum < 0)
1176 fprintf_unfiltered (file, "Cooked value");
1177 else
1178 {
1179 regcache_cooked_read (regcache, regnum, buf);
1180 fprintf_unfiltered (file, "0x");
1181 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
01e1877c 1182 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1183 }
1184 }
1185
b59ff9d5
AC
1186 /* Group members. */
1187 if (what_to_dump == regcache_dump_groups)
1188 {
1189 if (regnum < 0)
1190 fprintf_unfiltered (file, "Groups");
1191 else
1192 {
b59ff9d5 1193 const char *sep = "";
6c7d17ba
AC
1194 struct reggroup *group;
1195 for (group = reggroup_next (gdbarch, NULL);
1196 group != NULL;
1197 group = reggroup_next (gdbarch, group))
b59ff9d5 1198 {
6c7d17ba 1199 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
b59ff9d5 1200 {
6c7d17ba 1201 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
b59ff9d5
AC
1202 sep = ",";
1203 }
1204 }
1205 }
1206 }
1207
af030b9a
AC
1208 fprintf_unfiltered (file, "\n");
1209 }
1210
1211 if (footnote_register_size)
1212 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1213 footnote_register_size);
1214 if (footnote_register_offset)
1215 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1216 footnote_register_offset);
1217 if (footnote_register_type_name_null)
1218 fprintf_unfiltered (file,
1219 "*%d: Register type's name NULL.\n",
1220 footnote_register_type_name_null);
1221 do_cleanups (cleanups);
1222}
1223
1224static void
1225regcache_print (char *args, enum regcache_dump_what what_to_dump)
1226{
1227 if (args == NULL)
1228 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1229 else
1230 {
1231 struct ui_file *file = gdb_fopen (args, "w");
1232 if (file == NULL)
e2e0b3e5 1233 perror_with_name (_("maintenance print architecture"));
af030b9a
AC
1234 regcache_dump (current_regcache, file, what_to_dump);
1235 ui_file_delete (file);
1236 }
1237}
1238
1239static void
1240maintenance_print_registers (char *args, int from_tty)
1241{
1242 regcache_print (args, regcache_dump_none);
1243}
1244
1245static void
1246maintenance_print_raw_registers (char *args, int from_tty)
1247{
1248 regcache_print (args, regcache_dump_raw);
1249}
1250
1251static void
1252maintenance_print_cooked_registers (char *args, int from_tty)
1253{
1254 regcache_print (args, regcache_dump_cooked);
1255}
1256
b59ff9d5
AC
1257static void
1258maintenance_print_register_groups (char *args, int from_tty)
1259{
1260 regcache_print (args, regcache_dump_groups);
1261}
1262
b9362cc7
AC
1263extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1264
32178cab
MS
1265void
1266_initialize_regcache (void)
1267{
030f20e1 1268 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
046a4708 1269 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
046a4708 1270 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
705152c5 1271
f4c5303c
OF
1272 observer_attach_target_changed (regcache_observer_target_changed);
1273
705152c5 1274 add_com ("flushregs", class_maintenance, reg_flush_command,
1bedd215 1275 _("Force gdb to flush its register cache (maintainer command)"));
39f77062
KB
1276
1277 /* Initialize the thread/process associated with the current set of
1278 registers. For now, -1 is special, and means `no current process'. */
1279 registers_ptid = pid_to_ptid (-1);
af030b9a 1280
1a966eab
AC
1281 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1282Print the internal register configuration.\n\
1283Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 1284 add_cmd ("raw-registers", class_maintenance,
1a966eab
AC
1285 maintenance_print_raw_registers, _("\
1286Print the internal register configuration including raw values.\n\
1287Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 1288 add_cmd ("cooked-registers", class_maintenance,
1a966eab
AC
1289 maintenance_print_cooked_registers, _("\
1290Print the internal register configuration including cooked values.\n\
1291Takes an optional file parameter."), &maintenanceprintlist);
b59ff9d5 1292 add_cmd ("register-groups", class_maintenance,
1a966eab
AC
1293 maintenance_print_register_groups, _("\
1294Print the internal register configuration including each register's group.\n\
1295Takes an optional file parameter."),
af030b9a
AC
1296 &maintenanceprintlist);
1297
32178cab 1298}
This page took 0.817534 seconds and 4 git commands to generate.