*** empty log message ***
[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
384regcache_valid_p (struct regcache *regcache, int regnum)
385{
386 gdb_assert (regcache != NULL);
387 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
51b1fe4e 388 return regcache->register_valid_p[regnum];
3fadccb3
AC
389}
390
3fadccb3
AC
391/* Global structure containing the current regcache. */
392/* FIXME: cagney/2002-05-11: The two global arrays registers[] and
8262ee23 393 deprecated_register_valid[] currently point into this structure. */
3fadccb3
AC
394struct regcache *current_regcache;
395
5ebd2499 396/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
397 recording if the register values have been changed (eg. by the
398 user). Therefore all registers must be written back to the
399 target when appropriate. */
400
39f77062 401/* The thread/process associated with the current set of registers. */
32178cab 402
39f77062 403static ptid_t registers_ptid;
32178cab
MS
404
405/*
406 * FUNCTIONS:
407 */
408
409/* REGISTER_CACHED()
410
411 Returns 0 if the value is not in the cache (needs fetch).
412 >0 if the value is in the cache.
413 <0 if the value is permanently unavailable (don't ask again). */
414
415int
416register_cached (int regnum)
417{
8851ec7a 418 return current_regcache->register_valid_p[regnum];
32178cab
MS
419}
420
7302a204
ND
421/* Record that REGNUM's value is cached if STATE is >0, uncached but
422 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
423
424void
425set_register_cached (int regnum, int state)
426{
53826de9
AC
427 gdb_assert (regnum >= 0);
428 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
51b1fe4e 429 current_regcache->register_valid_p[regnum] = state;
7302a204
ND
430}
431
f4c5303c
OF
432/* Observer for the target_changed event. */
433
434void
435regcache_observer_target_changed (struct target_ops *target)
436{
437 registers_changed ();
438}
439
32178cab
MS
440/* Low level examining and depositing of registers.
441
442 The caller is responsible for making sure that the inferior is
443 stopped before calling the fetching routines, or it will get
444 garbage. (a change from GDB version 3, in which the caller got the
445 value from the last stop). */
446
447/* REGISTERS_CHANGED ()
448
449 Indicate that registers may have changed, so invalidate the cache. */
450
451void
452registers_changed (void)
453{
454 int i;
32178cab 455
39f77062 456 registers_ptid = pid_to_ptid (-1);
32178cab
MS
457
458 /* Force cleanup of any alloca areas if using C alloca instead of
459 a builtin alloca. This particular call is used to clean up
460 areas allocated by low level target code which may build up
461 during lengthy interactions between gdb and the target before
462 gdb gives control to the user (ie watchpoints). */
463 alloca (0);
464
53826de9 465 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
7302a204 466 set_register_cached (i, 0);
32178cab 467
9a4105ab
AC
468 if (deprecated_registers_changed_hook)
469 deprecated_registers_changed_hook ();
32178cab
MS
470}
471
2b9e5f3f 472/* DEPRECATED_REGISTERS_FETCHED ()
32178cab
MS
473
474 Indicate that all registers have been fetched, so mark them all valid. */
475
31e9866e
AC
476/* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
477 code was blatting the registers[] array and then calling this.
23a6d369 478 Since targets should only be using regcache_raw_supply() the need for
31e9866e 479 this function/hack is eliminated. */
32178cab
MS
480
481void
2b9e5f3f 482deprecated_registers_fetched (void)
32178cab
MS
483{
484 int i;
32178cab 485
a728f042 486 for (i = 0; i < NUM_REGS; i++)
7302a204 487 set_register_cached (i, 1);
fcdc5976 488 /* Do not assume that the pseudo-regs have also been fetched.
31e9866e 489 Fetching all real regs NEVER accounts for pseudo-regs. */
32178cab
MS
490}
491
61a0eb5b 492void
2d522557 493regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
61a0eb5b 494{
3fadccb3
AC
495 gdb_assert (regcache != NULL && buf != NULL);
496 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
3fadccb3
AC
497 /* Make certain that the register cache is up-to-date with respect
498 to the current thread. This switching shouldn't be necessary
499 only there is still only one target side register cache. Sigh!
500 On the bright side, at least there is a regcache object. */
2d28509a 501 if (!regcache->readonly_p)
3fadccb3
AC
502 {
503 gdb_assert (regcache == current_regcache);
504 if (! ptid_equal (registers_ptid, inferior_ptid))
505 {
506 registers_changed ();
507 registers_ptid = inferior_ptid;
508 }
509 if (!register_cached (regnum))
5c27f28a 510 target_fetch_registers (regnum);
0a8146bf
AC
511#if 0
512 /* FIXME: cagney/2004-08-07: At present a number of targets
04c663e3
DA
513 forget (or didn't know that they needed) to set this leading to
514 panics. Also is the problem that targets need to indicate
0a8146bf
AC
515 that a register is in one of the possible states: valid,
516 undefined, unknown. The last of which isn't yet
517 possible. */
7ab3286f 518 gdb_assert (register_cached (regnum));
0a8146bf 519#endif
3fadccb3
AC
520 }
521 /* Copy the value directly into the register cache. */
51b1fe4e 522 memcpy (buf, register_buffer (regcache, regnum),
3fadccb3 523 regcache->descr->sizeof_register[regnum]);
61a0eb5b
AC
524}
525
28fc6740
AC
526void
527regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
528{
2d522557 529 gdb_byte *buf;
28fc6740
AC
530 gdb_assert (regcache != NULL);
531 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
532 buf = alloca (regcache->descr->sizeof_register[regnum]);
533 regcache_raw_read (regcache, regnum, buf);
534 (*val) = extract_signed_integer (buf,
535 regcache->descr->sizeof_register[regnum]);
536}
537
538void
539regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
540 ULONGEST *val)
541{
2d522557 542 gdb_byte *buf;
28fc6740
AC
543 gdb_assert (regcache != NULL);
544 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
545 buf = alloca (regcache->descr->sizeof_register[regnum]);
546 regcache_raw_read (regcache, regnum, buf);
547 (*val) = extract_unsigned_integer (buf,
548 regcache->descr->sizeof_register[regnum]);
549}
550
c00dcbe9
MK
551void
552regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
553{
554 void *buf;
555 gdb_assert (regcache != NULL);
556 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
557 buf = alloca (regcache->descr->sizeof_register[regnum]);
558 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
559 regcache_raw_write (regcache, regnum, buf);
560}
561
562void
563regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
564 ULONGEST val)
565{
566 void *buf;
567 gdb_assert (regcache != NULL);
568 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
569 buf = alloca (regcache->descr->sizeof_register[regnum]);
570 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
571 regcache_raw_write (regcache, regnum, buf);
572}
573
61a0eb5b 574void
2d522557 575deprecated_read_register_gen (int regnum, gdb_byte *buf)
61a0eb5b 576{
3fadccb3
AC
577 gdb_assert (current_regcache != NULL);
578 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
68365089
AC
579 regcache_cooked_read (current_regcache, regnum, buf);
580}
581
582void
2d522557 583regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
68365089 584{
d138e37a 585 gdb_assert (regnum >= 0);
68365089
AC
586 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
587 if (regnum < regcache->descr->nr_raw_registers)
588 regcache_raw_read (regcache, regnum, buf);
2d28509a
AC
589 else if (regcache->readonly_p
590 && regnum < regcache->descr->nr_cooked_registers
591 && regcache->register_valid_p[regnum])
b2fa5097 592 /* Read-only register cache, perhaps the cooked value was cached? */
2d28509a
AC
593 memcpy (buf, register_buffer (regcache, regnum),
594 regcache->descr->sizeof_register[regnum]);
d138e37a 595 else
68365089
AC
596 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
597 regnum, buf);
61a0eb5b
AC
598}
599
a378f419
AC
600void
601regcache_cooked_read_signed (struct regcache *regcache, int regnum,
602 LONGEST *val)
603{
2d522557 604 gdb_byte *buf;
a378f419 605 gdb_assert (regcache != NULL);
a66a9c23 606 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
607 buf = alloca (regcache->descr->sizeof_register[regnum]);
608 regcache_cooked_read (regcache, regnum, buf);
609 (*val) = extract_signed_integer (buf,
610 regcache->descr->sizeof_register[regnum]);
611}
612
613void
614regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
615 ULONGEST *val)
616{
2d522557 617 gdb_byte *buf;
a378f419 618 gdb_assert (regcache != NULL);
a66a9c23 619 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
620 buf = alloca (regcache->descr->sizeof_register[regnum]);
621 regcache_cooked_read (regcache, regnum, buf);
622 (*val) = extract_unsigned_integer (buf,
623 regcache->descr->sizeof_register[regnum]);
624}
625
a66a9c23
AC
626void
627regcache_cooked_write_signed (struct regcache *regcache, int regnum,
628 LONGEST val)
629{
630 void *buf;
631 gdb_assert (regcache != NULL);
632 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
633 buf = alloca (regcache->descr->sizeof_register[regnum]);
634 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
635 regcache_cooked_write (regcache, regnum, buf);
636}
637
638void
639regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
640 ULONGEST val)
641{
642 void *buf;
643 gdb_assert (regcache != NULL);
644 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
645 buf = alloca (regcache->descr->sizeof_register[regnum]);
646 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
647 regcache_cooked_write (regcache, regnum, buf);
648}
649
61a0eb5b 650void
2d522557
AC
651regcache_raw_write (struct regcache *regcache, int regnum,
652 const gdb_byte *buf)
61a0eb5b 653{
3fadccb3
AC
654 gdb_assert (regcache != NULL && buf != NULL);
655 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
2d28509a 656 gdb_assert (!regcache->readonly_p);
3fadccb3 657
3fadccb3
AC
658 /* On the sparc, writing %g0 is a no-op, so we don't even want to
659 change the registers array if something writes to this register. */
660 if (CANNOT_STORE_REGISTER (regnum))
661 return;
662
3fadccb3
AC
663 /* Make certain that the correct cache is selected. */
664 gdb_assert (regcache == current_regcache);
665 if (! ptid_equal (registers_ptid, inferior_ptid))
666 {
667 registers_changed ();
668 registers_ptid = inferior_ptid;
669 }
670
671 /* If we have a valid copy of the register, and new value == old
672 value, then don't bother doing the actual store. */
673 if (regcache_valid_p (regcache, regnum)
674 && (memcmp (register_buffer (regcache, regnum), buf,
675 regcache->descr->sizeof_register[regnum]) == 0))
676 return;
677
678 target_prepare_to_store ();
679 memcpy (register_buffer (regcache, regnum), buf,
680 regcache->descr->sizeof_register[regnum]);
51b1fe4e 681 regcache->register_valid_p[regnum] = 1;
5c27f28a 682 target_store_registers (regnum);
61a0eb5b
AC
683}
684
685void
2d522557 686deprecated_write_register_gen (int regnum, gdb_byte *buf)
61a0eb5b 687{
3fadccb3
AC
688 gdb_assert (current_regcache != NULL);
689 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
68365089
AC
690 regcache_cooked_write (current_regcache, regnum, buf);
691}
692
693void
2d522557
AC
694regcache_cooked_write (struct regcache *regcache, int regnum,
695 const gdb_byte *buf)
68365089 696{
d138e37a 697 gdb_assert (regnum >= 0);
68365089
AC
698 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
699 if (regnum < regcache->descr->nr_raw_registers)
700 regcache_raw_write (regcache, regnum, buf);
d138e37a 701 else
68365089 702 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
d8124050 703 regnum, buf);
61a0eb5b
AC
704}
705
06c0b04e
AC
706/* Perform a partial register transfer using a read, modify, write
707 operation. */
708
709typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
710 void *buf);
711typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
712 const void *buf);
713
b9362cc7 714static void
06c0b04e
AC
715regcache_xfer_part (struct regcache *regcache, int regnum,
716 int offset, int len, void *in, const void *out,
2d522557
AC
717 void (*read) (struct regcache *regcache, int regnum,
718 gdb_byte *buf),
719 void (*write) (struct regcache *regcache, int regnum,
720 const gdb_byte *buf))
06c0b04e
AC
721{
722 struct regcache_descr *descr = regcache->descr;
fc1a4b47 723 gdb_byte reg[MAX_REGISTER_SIZE];
06c0b04e
AC
724 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
725 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
726 /* Something to do? */
727 if (offset + len == 0)
728 return;
729 /* Read (when needed) ... */
730 if (in != NULL
731 || offset > 0
732 || offset + len < descr->sizeof_register[regnum])
733 {
734 gdb_assert (read != NULL);
735 read (regcache, regnum, reg);
736 }
737 /* ... modify ... */
738 if (in != NULL)
739 memcpy (in, reg + offset, len);
740 if (out != NULL)
741 memcpy (reg + offset, out, len);
742 /* ... write (when needed). */
743 if (out != NULL)
744 {
745 gdb_assert (write != NULL);
746 write (regcache, regnum, reg);
747 }
748}
749
750void
751regcache_raw_read_part (struct regcache *regcache, int regnum,
2d522557 752 int offset, int len, gdb_byte *buf)
06c0b04e
AC
753{
754 struct regcache_descr *descr = regcache->descr;
755 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
756 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
757 regcache_raw_read, regcache_raw_write);
758}
759
760void
761regcache_raw_write_part (struct regcache *regcache, int regnum,
2d522557 762 int offset, int len, const gdb_byte *buf)
06c0b04e
AC
763{
764 struct regcache_descr *descr = regcache->descr;
765 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
766 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
767 regcache_raw_read, regcache_raw_write);
768}
769
770void
771regcache_cooked_read_part (struct regcache *regcache, int regnum,
2d522557 772 int offset, int len, gdb_byte *buf)
06c0b04e
AC
773{
774 struct regcache_descr *descr = regcache->descr;
775 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
776 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
777 regcache_cooked_read, regcache_cooked_write);
778}
779
780void
781regcache_cooked_write_part (struct regcache *regcache, int regnum,
2d522557 782 int offset, int len, const gdb_byte *buf)
06c0b04e
AC
783{
784 struct regcache_descr *descr = regcache->descr;
785 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
786 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
787 regcache_cooked_read, regcache_cooked_write);
788}
32178cab 789
d3b22ed5
AC
790/* Hack to keep code that view the register buffer as raw bytes
791 working. */
792
793int
794register_offset_hack (struct gdbarch *gdbarch, int regnum)
795{
796 struct regcache_descr *descr = regcache_descr (gdbarch);
797 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
798 return descr->register_offset[regnum];
799}
800
5ebd2499 801/* Return the contents of register REGNUM as an unsigned integer. */
32178cab 802
173155e8 803ULONGEST
5ebd2499 804read_register (int regnum)
32178cab 805{
2d522557 806 gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
4caf0990 807 deprecated_read_register_gen (regnum, buf);
3acba339 808 return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
32178cab
MS
809}
810
173155e8 811ULONGEST
39f77062 812read_register_pid (int regnum, ptid_t ptid)
32178cab 813{
39f77062 814 ptid_t save_ptid;
32178cab
MS
815 int save_pid;
816 CORE_ADDR retval;
817
39f77062 818 if (ptid_equal (ptid, inferior_ptid))
5ebd2499 819 return read_register (regnum);
32178cab 820
39f77062 821 save_ptid = inferior_ptid;
32178cab 822
39f77062 823 inferior_ptid = ptid;
32178cab 824
5ebd2499 825 retval = read_register (regnum);
32178cab 826
39f77062 827 inferior_ptid = save_ptid;
32178cab
MS
828
829 return retval;
830}
831
5ebd2499 832/* Store VALUE into the raw contents of register number REGNUM. */
32178cab
MS
833
834void
5ebd2499 835write_register (int regnum, LONGEST val)
32178cab 836{
61a0eb5b 837 void *buf;
32178cab 838 int size;
3acba339 839 size = register_size (current_gdbarch, regnum);
32178cab
MS
840 buf = alloca (size);
841 store_signed_integer (buf, size, (LONGEST) val);
4caf0990 842 deprecated_write_register_gen (regnum, buf);
32178cab
MS
843}
844
845void
39f77062 846write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
32178cab 847{
39f77062 848 ptid_t save_ptid;
32178cab 849
39f77062 850 if (ptid_equal (ptid, inferior_ptid))
32178cab 851 {
5ebd2499 852 write_register (regnum, val);
32178cab
MS
853 return;
854 }
855
39f77062 856 save_ptid = inferior_ptid;
32178cab 857
39f77062 858 inferior_ptid = ptid;
32178cab 859
5ebd2499 860 write_register (regnum, val);
32178cab 861
39f77062 862 inferior_ptid = save_ptid;
32178cab
MS
863}
864
a16d75cc 865/* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
9a661b68
MK
866
867void
6618125d 868regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
9a661b68
MK
869{
870 void *regbuf;
871 size_t size;
872
a16d75cc 873 gdb_assert (regcache != NULL);
9a661b68
MK
874 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
875 gdb_assert (!regcache->readonly_p);
876
877 /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
878 CURRENT_REGCACHE specially here. */
879 if (regcache == current_regcache
880 && !ptid_equal (registers_ptid, inferior_ptid))
881 {
882 registers_changed ();
883 registers_ptid = inferior_ptid;
884 }
885
886 regbuf = register_buffer (regcache, regnum);
887 size = regcache->descr->sizeof_register[regnum];
888
889 if (buf)
890 memcpy (regbuf, buf, size);
891 else
892 memset (regbuf, 0, size);
893
894 /* Mark the register as cached. */
895 regcache->register_valid_p[regnum] = 1;
896}
897
898/* Collect register REGNUM from REGCACHE and store its contents in BUF. */
899
900void
6618125d 901regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
9a661b68
MK
902{
903 const void *regbuf;
904 size_t size;
905
906 gdb_assert (regcache != NULL && buf != NULL);
907 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
908
909 regbuf = register_buffer (regcache, regnum);
910 size = regcache->descr->sizeof_register[regnum];
911 memcpy (buf, regbuf, size);
912}
913
193cb69f 914
9c8dbfa9
AC
915/* read_pc, write_pc, read_sp, etc. Special handling for registers
916 PC, SP, and FP. */
32178cab 917
9c8dbfa9
AC
918/* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
919 read_sp(), will eventually be replaced by per-frame methods.
920 Instead of relying on the global INFERIOR_PTID, they will use the
921 contextual information provided by the FRAME. These functions do
922 not belong in the register cache. */
32178cab 923
cde9ea48 924/* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
9c8dbfa9
AC
925 write_pc_pid() and write_pc(), all need to be replaced by something
926 that does not rely on global state. But what? */
32178cab
MS
927
928CORE_ADDR
39f77062 929read_pc_pid (ptid_t ptid)
32178cab 930{
39f77062 931 ptid_t saved_inferior_ptid;
32178cab
MS
932 CORE_ADDR pc_val;
933
39f77062
KB
934 /* In case ptid != inferior_ptid. */
935 saved_inferior_ptid = inferior_ptid;
936 inferior_ptid = ptid;
32178cab 937
cde9ea48
AC
938 if (TARGET_READ_PC_P ())
939 pc_val = TARGET_READ_PC (ptid);
940 /* Else use per-frame method on get_current_frame. */
941 else if (PC_REGNUM >= 0)
942 {
943 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
6ba34a8d 944 pc_val = ADDR_BITS_REMOVE (raw_val);
cde9ea48
AC
945 }
946 else
e2e0b3e5 947 internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
32178cab 948
39f77062 949 inferior_ptid = saved_inferior_ptid;
32178cab
MS
950 return pc_val;
951}
952
953CORE_ADDR
954read_pc (void)
955{
39f77062 956 return read_pc_pid (inferior_ptid);
32178cab
MS
957}
958
32178cab 959void
39f77062 960generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
32178cab 961{
32178cab 962 if (PC_REGNUM >= 0)
39f77062 963 write_register_pid (PC_REGNUM, pc, ptid);
afb18d0f
AC
964 else
965 internal_error (__FILE__, __LINE__,
e2e0b3e5 966 _("generic_target_write_pc"));
32178cab
MS
967}
968
969void
39f77062 970write_pc_pid (CORE_ADDR pc, ptid_t ptid)
32178cab 971{
39f77062 972 ptid_t saved_inferior_ptid;
32178cab 973
39f77062
KB
974 /* In case ptid != inferior_ptid. */
975 saved_inferior_ptid = inferior_ptid;
976 inferior_ptid = ptid;
32178cab 977
39f77062 978 TARGET_WRITE_PC (pc, ptid);
32178cab 979
39f77062 980 inferior_ptid = saved_inferior_ptid;
32178cab
MS
981}
982
983void
984write_pc (CORE_ADDR pc)
985{
39f77062 986 write_pc_pid (pc, inferior_ptid);
32178cab
MS
987}
988
989/* Cope with strage ways of getting to the stack and frame pointers */
990
32178cab
MS
991CORE_ADDR
992read_sp (void)
993{
bd1ce8ba
AC
994 if (TARGET_READ_SP_P ())
995 return TARGET_READ_SP ();
a9e5fdc2
AC
996 else if (gdbarch_unwind_sp_p (current_gdbarch))
997 return get_frame_sp (get_current_frame ());
bd1ce8ba 998 else if (SP_REGNUM >= 0)
a9e5fdc2
AC
999 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1000 about the architecture so put it at the end. */
bd1ce8ba 1001 return read_register (SP_REGNUM);
e2e0b3e5 1002 internal_error (__FILE__, __LINE__, _("read_sp: Unable to find SP"));
32178cab
MS
1003}
1004
705152c5
MS
1005static void
1006reg_flush_command (char *command, int from_tty)
1007{
1008 /* Force-flush the register cache. */
1009 registers_changed ();
1010 if (from_tty)
a3f17187 1011 printf_filtered (_("Register cache flushed.\n"));
705152c5
MS
1012}
1013
32178cab
MS
1014static void
1015build_regcache (void)
3fadccb3
AC
1016{
1017 current_regcache = regcache_xmalloc (current_gdbarch);
2d28509a 1018 current_regcache->readonly_p = 0;
3fadccb3
AC
1019}
1020
af030b9a
AC
1021static void
1022dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1023 const unsigned char *buf, long len)
1024{
1025 int i;
1026 switch (endian)
1027 {
1028 case BFD_ENDIAN_BIG:
1029 for (i = 0; i < len; i++)
1030 fprintf_unfiltered (file, "%02x", buf[i]);
1031 break;
1032 case BFD_ENDIAN_LITTLE:
1033 for (i = len - 1; i >= 0; i--)
1034 fprintf_unfiltered (file, "%02x", buf[i]);
1035 break;
1036 default:
e2e0b3e5 1037 internal_error (__FILE__, __LINE__, _("Bad switch"));
af030b9a
AC
1038 }
1039}
1040
1041enum regcache_dump_what
1042{
b59ff9d5 1043 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
af030b9a
AC
1044};
1045
1046static void
1047regcache_dump (struct regcache *regcache, struct ui_file *file,
1048 enum regcache_dump_what what_to_dump)
1049{
1050 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
b59ff9d5 1051 struct gdbarch *gdbarch = regcache->descr->gdbarch;
af030b9a
AC
1052 int regnum;
1053 int footnote_nr = 0;
1054 int footnote_register_size = 0;
1055 int footnote_register_offset = 0;
1056 int footnote_register_type_name_null = 0;
1057 long register_offset = 0;
123a958e 1058 unsigned char buf[MAX_REGISTER_SIZE];
af030b9a
AC
1059
1060#if 0
af030b9a
AC
1061 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1062 regcache->descr->nr_raw_registers);
1063 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1064 regcache->descr->nr_cooked_registers);
1065 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1066 regcache->descr->sizeof_raw_registers);
1067 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1068 regcache->descr->sizeof_raw_register_valid_p);
af030b9a
AC
1069 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1070 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1071#endif
1072
1073 gdb_assert (regcache->descr->nr_cooked_registers
1074 == (NUM_REGS + NUM_PSEUDO_REGS));
1075
1076 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1077 {
1078 /* Name. */
1079 if (regnum < 0)
1080 fprintf_unfiltered (file, " %-10s", "Name");
1081 else
1082 {
1083 const char *p = REGISTER_NAME (regnum);
1084 if (p == NULL)
1085 p = "";
1086 else if (p[0] == '\0')
1087 p = "''";
1088 fprintf_unfiltered (file, " %-10s", p);
1089 }
1090
1091 /* Number. */
1092 if (regnum < 0)
1093 fprintf_unfiltered (file, " %4s", "Nr");
1094 else
1095 fprintf_unfiltered (file, " %4d", regnum);
1096
1097 /* Relative number. */
1098 if (regnum < 0)
1099 fprintf_unfiltered (file, " %4s", "Rel");
1100 else if (regnum < NUM_REGS)
1101 fprintf_unfiltered (file, " %4d", regnum);
1102 else
1103 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1104
1105 /* Offset. */
1106 if (regnum < 0)
1107 fprintf_unfiltered (file, " %6s ", "Offset");
1108 else
1109 {
1110 fprintf_unfiltered (file, " %6ld",
1111 regcache->descr->register_offset[regnum]);
a7e3c2ad 1112 if (register_offset != regcache->descr->register_offset[regnum]
62700349 1113 || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
d3b22ed5
AC
1114 || (regnum > 0
1115 && (regcache->descr->register_offset[regnum]
1116 != (regcache->descr->register_offset[regnum - 1]
1117 + regcache->descr->sizeof_register[regnum - 1])))
1118 )
af030b9a
AC
1119 {
1120 if (!footnote_register_offset)
1121 footnote_register_offset = ++footnote_nr;
1122 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1123 }
1124 else
1125 fprintf_unfiltered (file, " ");
1126 register_offset = (regcache->descr->register_offset[regnum]
1127 + regcache->descr->sizeof_register[regnum]);
1128 }
1129
1130 /* Size. */
1131 if (regnum < 0)
1132 fprintf_unfiltered (file, " %5s ", "Size");
1133 else
01e1877c
AC
1134 fprintf_unfiltered (file, " %5ld",
1135 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1136
1137 /* Type. */
b59ff9d5
AC
1138 {
1139 const char *t;
1140 if (regnum < 0)
1141 t = "Type";
1142 else
1143 {
1144 static const char blt[] = "builtin_type";
1145 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1146 if (t == NULL)
1147 {
1148 char *n;
1149 if (!footnote_register_type_name_null)
1150 footnote_register_type_name_null = ++footnote_nr;
b435e160 1151 n = xstrprintf ("*%d", footnote_register_type_name_null);
b59ff9d5
AC
1152 make_cleanup (xfree, n);
1153 t = n;
1154 }
1155 /* Chop a leading builtin_type. */
1156 if (strncmp (t, blt, strlen (blt)) == 0)
1157 t += strlen (blt);
1158 }
1159 fprintf_unfiltered (file, " %-15s", t);
1160 }
1161
1162 /* Leading space always present. */
1163 fprintf_unfiltered (file, " ");
af030b9a
AC
1164
1165 /* Value, raw. */
1166 if (what_to_dump == regcache_dump_raw)
1167 {
1168 if (regnum < 0)
1169 fprintf_unfiltered (file, "Raw value");
1170 else if (regnum >= regcache->descr->nr_raw_registers)
1171 fprintf_unfiltered (file, "<cooked>");
1172 else if (!regcache_valid_p (regcache, regnum))
1173 fprintf_unfiltered (file, "<invalid>");
1174 else
1175 {
1176 regcache_raw_read (regcache, regnum, buf);
1177 fprintf_unfiltered (file, "0x");
1178 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
01e1877c 1179 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1180 }
1181 }
1182
1183 /* Value, cooked. */
1184 if (what_to_dump == regcache_dump_cooked)
1185 {
1186 if (regnum < 0)
1187 fprintf_unfiltered (file, "Cooked value");
1188 else
1189 {
1190 regcache_cooked_read (regcache, regnum, buf);
1191 fprintf_unfiltered (file, "0x");
1192 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
01e1877c 1193 regcache->descr->sizeof_register[regnum]);
af030b9a
AC
1194 }
1195 }
1196
b59ff9d5
AC
1197 /* Group members. */
1198 if (what_to_dump == regcache_dump_groups)
1199 {
1200 if (regnum < 0)
1201 fprintf_unfiltered (file, "Groups");
1202 else
1203 {
b59ff9d5 1204 const char *sep = "";
6c7d17ba
AC
1205 struct reggroup *group;
1206 for (group = reggroup_next (gdbarch, NULL);
1207 group != NULL;
1208 group = reggroup_next (gdbarch, group))
b59ff9d5 1209 {
6c7d17ba 1210 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
b59ff9d5 1211 {
6c7d17ba 1212 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
b59ff9d5
AC
1213 sep = ",";
1214 }
1215 }
1216 }
1217 }
1218
af030b9a
AC
1219 fprintf_unfiltered (file, "\n");
1220 }
1221
1222 if (footnote_register_size)
1223 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1224 footnote_register_size);
1225 if (footnote_register_offset)
1226 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1227 footnote_register_offset);
1228 if (footnote_register_type_name_null)
1229 fprintf_unfiltered (file,
1230 "*%d: Register type's name NULL.\n",
1231 footnote_register_type_name_null);
1232 do_cleanups (cleanups);
1233}
1234
1235static void
1236regcache_print (char *args, enum regcache_dump_what what_to_dump)
1237{
1238 if (args == NULL)
1239 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1240 else
1241 {
1242 struct ui_file *file = gdb_fopen (args, "w");
1243 if (file == NULL)
e2e0b3e5 1244 perror_with_name (_("maintenance print architecture"));
af030b9a
AC
1245 regcache_dump (current_regcache, file, what_to_dump);
1246 ui_file_delete (file);
1247 }
1248}
1249
1250static void
1251maintenance_print_registers (char *args, int from_tty)
1252{
1253 regcache_print (args, regcache_dump_none);
1254}
1255
1256static void
1257maintenance_print_raw_registers (char *args, int from_tty)
1258{
1259 regcache_print (args, regcache_dump_raw);
1260}
1261
1262static void
1263maintenance_print_cooked_registers (char *args, int from_tty)
1264{
1265 regcache_print (args, regcache_dump_cooked);
1266}
1267
b59ff9d5
AC
1268static void
1269maintenance_print_register_groups (char *args, int from_tty)
1270{
1271 regcache_print (args, regcache_dump_groups);
1272}
1273
b9362cc7
AC
1274extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1275
32178cab
MS
1276void
1277_initialize_regcache (void)
1278{
030f20e1 1279 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
046a4708 1280 DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
046a4708 1281 deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
705152c5 1282
f4c5303c
OF
1283 observer_attach_target_changed (regcache_observer_target_changed);
1284
705152c5 1285 add_com ("flushregs", class_maintenance, reg_flush_command,
1bedd215 1286 _("Force gdb to flush its register cache (maintainer command)"));
39f77062
KB
1287
1288 /* Initialize the thread/process associated with the current set of
1289 registers. For now, -1 is special, and means `no current process'. */
1290 registers_ptid = pid_to_ptid (-1);
af030b9a 1291
1a966eab
AC
1292 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1293Print the internal register configuration.\n\
1294Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 1295 add_cmd ("raw-registers", class_maintenance,
1a966eab
AC
1296 maintenance_print_raw_registers, _("\
1297Print the internal register configuration including raw values.\n\
1298Takes an optional file parameter."), &maintenanceprintlist);
af030b9a 1299 add_cmd ("cooked-registers", class_maintenance,
1a966eab
AC
1300 maintenance_print_cooked_registers, _("\
1301Print the internal register configuration including cooked values.\n\
1302Takes an optional file parameter."), &maintenanceprintlist);
b59ff9d5 1303 add_cmd ("register-groups", class_maintenance,
1a966eab
AC
1304 maintenance_print_register_groups, _("\
1305Print the internal register configuration including each register's group.\n\
1306Takes an optional file parameter."),
af030b9a
AC
1307 &maintenanceprintlist);
1308
32178cab 1309}
This page took 0.943759 seconds and 4 git commands to generate.