* tui-interp.c: Include "cli-out.h".
[deliverable/binutils-gdb.git] / gdb / regcache.c
CommitLineData
32178cab 1/* Cache and manage the values of registers for GDB, the GNU debugger.
3fadccb3
AC
2
3 Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4 2001, 2002 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
20 Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
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. */
32178cab
MS
33
34/*
35 * DATA STRUCTURE
36 *
37 * Here is the actual register cache.
38 */
39
3fadccb3
AC
40/* Per-architecture object describing the layout of a register cache.
41 Computed once when the architecture is created */
42
43struct gdbarch_data *regcache_descr_handle;
44
45struct regcache_descr
46{
47 /* The architecture this descriptor belongs to. */
48 struct gdbarch *gdbarch;
49
50 /* Is this a ``legacy'' register cache? Such caches reserve space
51 for raw and pseudo registers and allow access to both. */
52 int legacy_p;
53
bb1db049
AC
54 /* The raw register cache. Each raw (or hard) register is supplied
55 by the target interface. The raw cache should not contain
56 redundant information - if the PC is constructed from two
57 registers then those regigisters and not the PC lives in the raw
58 cache. */
3fadccb3
AC
59 int nr_raw_registers;
60 long sizeof_raw_registers;
61 long sizeof_raw_register_valid_p;
62
d138e37a
AC
63 /* The cooked register space. Each cooked register in the range
64 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
65 register. The remaining [NR_RAW_REGISTERS
66 .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
67 both raw registers and memory by the architecture methods
68 gdbarch_register_read and gdbarch_register_write. */
69 int nr_cooked_registers;
067df2e5
AC
70 long sizeof_cooked_registers;
71 long sizeof_cooked_register_valid_p;
d138e37a
AC
72
73 /* Offset and size (in 8 bit bytes), of reach register in the
74 register cache. All registers (including those in the range
75 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
76 Assigning all registers an offset makes it possible to keep
77 legacy code, such as that found in read_register_bytes() and
78 write_register_bytes() working. */
3fadccb3 79 long *register_offset;
3fadccb3 80 long *sizeof_register;
3fadccb3 81
bb425013
AC
82 /* Cached table containing the type of each register. */
83 struct type **register_type;
3fadccb3
AC
84};
85
b9362cc7 86static void
bb425013
AC
87init_legacy_regcache_descr (struct gdbarch *gdbarch,
88 struct regcache_descr *descr)
3fadccb3
AC
89{
90 int i;
3fadccb3
AC
91 /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
92 ``gdbarch'' as a parameter. */
93 gdb_assert (gdbarch != NULL);
94
067df2e5
AC
95 /* Compute the offset of each register. Legacy architectures define
96 REGISTER_BYTE() so use that. */
97 /* FIXME: cagney/2002-11-07: Instead of using REGISTER_BYTE() this
98 code should, as is done in init_regcache_descr(), compute the
99 offets at runtime. This currently isn't possible as some ISAs
100 define overlapping register regions - see the mess in
101 read_register_bytes() and write_register_bytes() registers. */
d138e37a
AC
102 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
103 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
d138e37a 104 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3 105 {
067df2e5
AC
106 /* FIXME: cagney/2001-12-04: This code shouldn't need to use
107 REGISTER_BYTE(). Unfortunatly, legacy code likes to lay the
108 buffer out so that certain registers just happen to overlap.
109 Ulgh! New targets use gdbarch's register read/write and
110 entirely avoid this uglyness. */
3fadccb3
AC
111 descr->register_offset[i] = REGISTER_BYTE (i);
112 descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
123a958e
AC
113 gdb_assert (MAX_REGISTER_SIZE >= REGISTER_RAW_SIZE (i));
114 gdb_assert (MAX_REGISTER_SIZE >= REGISTER_VIRTUAL_SIZE (i));
3fadccb3
AC
115 }
116
067df2e5 117 /* Compute the real size of the register buffer. Start out by
b8b527c5
AC
118 trusting DEPRECATED_REGISTER_BYTES, but then adjust it upwards
119 should that be found to not be sufficient. */
120 /* FIXME: cagney/2002-11-05: Instead of using the macro
121 DEPRECATED_REGISTER_BYTES, this code should, as is done in
122 init_regcache_descr(), compute the total number of register bytes
123 using the accumulated offsets. */
124 descr->sizeof_cooked_registers = DEPRECATED_REGISTER_BYTES; /* OK */
d138e37a 125 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3
AC
126 {
127 long regend;
128 /* Keep extending the buffer so that there is always enough
129 space for all registers. The comparison is necessary since
130 legacy code is free to put registers in random places in the
131 buffer separated by holes. Once REGISTER_BYTE() is killed
132 this can be greatly simplified. */
3fadccb3 133 regend = descr->register_offset[i] + descr->sizeof_register[i];
067df2e5
AC
134 if (descr->sizeof_cooked_registers < regend)
135 descr->sizeof_cooked_registers = regend;
3fadccb3 136 }
067df2e5
AC
137 /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
138 in the register cache. Unfortunatly some architectures still
139 rely on this and the pseudo_register_write() method. */
140 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
3fadccb3
AC
141}
142
143static void *
144init_regcache_descr (struct gdbarch *gdbarch)
145{
146 int i;
147 struct regcache_descr *descr;
148 gdb_assert (gdbarch != NULL);
149
bb425013
AC
150 /* Create an initial, zero filled, table. */
151 descr = XCALLOC (1, struct regcache_descr);
3fadccb3 152 descr->gdbarch = gdbarch;
3fadccb3 153
d138e37a
AC
154 /* Total size of the register space. The raw registers are mapped
155 directly onto the raw register cache while the pseudo's are
3fadccb3 156 either mapped onto raw-registers or memory. */
d138e37a 157 descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
067df2e5 158 descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
3fadccb3 159
bb425013
AC
160 /* Fill in a table of register types. */
161 descr->register_type = XCALLOC (descr->nr_cooked_registers,
162 struct type *);
163 for (i = 0; i < descr->nr_cooked_registers; i++)
164 {
35cac7cf
AC
165 if (gdbarch_register_type_p (gdbarch))
166 {
167 gdb_assert (!REGISTER_VIRTUAL_TYPE_P ()); /* OK */
168 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
169 }
170 else
171 descr->register_type[i] = REGISTER_VIRTUAL_TYPE (i); /* OK */
bb425013
AC
172 }
173
bb1db049
AC
174 /* Construct a strictly RAW register cache. Don't allow pseudo's
175 into the register cache. */
176 descr->nr_raw_registers = NUM_REGS;
177
178 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
179 array. This pretects GDB from erant code that accesses elements
180 of the global register_valid_p[] array in the range [NUM_REGS
181 .. NUM_REGS + NUM_PSEUDO_REGS). */
182 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
183
bb425013
AC
184 /* If an old style architecture, fill in the remainder of the
185 register cache descriptor using the register macros. */
dadd712e
AC
186 /* NOTE: cagney/2003-06-29: If either of REGISTER_BYTE or
187 REGISTER_RAW_SIZE are still present, things are most likely
188 totally screwed. Ex: an architecture with raw register sizes
189 smaller than what REGISTER_BYTE indicates; non monotonic
190 REGISTER_BYTE values. For GDB 6 check for these nasty methods
191 and fall back to legacy code when present. Sigh! */
192 if ((!gdbarch_pseudo_register_read_p (gdbarch)
193 && !gdbarch_pseudo_register_write_p (gdbarch)
194 && !gdbarch_register_type_p (gdbarch))
195 || REGISTER_BYTE_P () || REGISTER_RAW_SIZE_P ())
bb425013
AC
196 {
197 descr->legacy_p = 1;
198 init_legacy_regcache_descr (gdbarch, descr);
199 return descr;
200 }
201
067df2e5 202 /* Lay out the register cache.
3fadccb3 203
bb425013
AC
204 NOTE: cagney/2002-05-22: Only register_type() is used when
205 constructing the register cache. It is assumed that the
206 register's raw size, virtual size and type length are all the
207 same. */
3fadccb3
AC
208
209 {
210 long offset = 0;
d138e37a
AC
211 descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
212 descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
d138e37a 213 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3 214 {
bb425013 215 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
3fadccb3
AC
216 descr->register_offset[i] = offset;
217 offset += descr->sizeof_register[i];
123a958e 218 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
3fadccb3
AC
219 }
220 /* Set the real size of the register cache buffer. */
067df2e5 221 descr->sizeof_cooked_registers = offset;
3fadccb3
AC
222 }
223
067df2e5
AC
224 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
225 the raw registers. Unfortunatly some code still accesses the
226 register array directly using the global registers[]. Until that
227 code has been purged, play safe and over allocating the register
228 buffer. Ulgh! */
229 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
230
46654a5b
AC
231 /* Sanity check. Confirm that there is agreement between the
232 regcache and the target's redundant REGISTER_BYTE (new targets
233 should not even be defining it). */
d138e37a 234 for (i = 0; i < descr->nr_cooked_registers; i++)
3fadccb3 235 {
46654a5b
AC
236 if (REGISTER_BYTE_P ())
237 gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
238#if 0
3fadccb3
AC
239 gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
240 gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
46654a5b 241#endif
3fadccb3 242 }
b8b527c5 243 /* gdb_assert (descr->sizeof_raw_registers == DEPRECATED_REGISTER_BYTES (i)); */
3fadccb3
AC
244
245 return descr;
246}
247
248static struct regcache_descr *
249regcache_descr (struct gdbarch *gdbarch)
250{
251 return gdbarch_data (gdbarch, regcache_descr_handle);
252}
253
254static void
255xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
256{
257 struct regcache_descr *descr = ptr;
258 if (descr == NULL)
259 return;
260 xfree (descr->register_offset);
261 xfree (descr->sizeof_register);
262 descr->register_offset = NULL;
263 descr->sizeof_register = NULL;
264 xfree (descr);
265}
266
bb425013
AC
267/* Utility functions returning useful register attributes stored in
268 the regcache descr. */
269
270struct type *
271register_type (struct gdbarch *gdbarch, int regnum)
272{
273 struct regcache_descr *descr = regcache_descr (gdbarch);
274 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
275 return descr->register_type[regnum];
276}
277
0ed04cce
AC
278/* Utility functions returning useful register attributes stored in
279 the regcache descr. */
280
08a617da
AC
281int
282register_size (struct gdbarch *gdbarch, int regnum)
283{
284 struct regcache_descr *descr = regcache_descr (gdbarch);
285 int size;
286 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
287 size = descr->sizeof_register[regnum];
96a4ee76
AC
288 /* NB: The deprecated REGISTER_RAW_SIZE, if not provided, defaults
289 to the size of the register's type. */
08a617da 290 gdb_assert (size == REGISTER_RAW_SIZE (regnum)); /* OK */
96a4ee76
AC
291 /* NB: Don't check the register's virtual size. It, in say the case
292 of the MIPS, may not match the raw size! */
08a617da
AC
293 return size;
294}
295
3fadccb3
AC
296/* The register cache for storing raw register values. */
297
298struct regcache
299{
300 struct regcache_descr *descr;
51b1fe4e
AC
301 /* The register buffers. A read-only register cache can hold the
302 full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
303 register cache can only hold [0 .. NUM_REGS). */
304 char *registers;
305 char *register_valid_p;
2d28509a
AC
306 /* Is this a read-only cache? A read-only cache is used for saving
307 the target's register state (e.g, across an inferior function
308 call or just before forcing a function return). A read-only
309 cache can only be updated via the methods regcache_dup() and
310 regcache_cpy(). The actual contents are determined by the
311 reggroup_save and reggroup_restore methods. */
312 int readonly_p;
3fadccb3
AC
313};
314
315struct regcache *
316regcache_xmalloc (struct gdbarch *gdbarch)
317{
318 struct regcache_descr *descr;
319 struct regcache *regcache;
320 gdb_assert (gdbarch != NULL);
321 descr = regcache_descr (gdbarch);
322 regcache = XMALLOC (struct regcache);
323 regcache->descr = descr;
51b1fe4e 324 regcache->registers
3fadccb3 325 = XCALLOC (descr->sizeof_raw_registers, char);
51b1fe4e 326 regcache->register_valid_p
3fadccb3 327 = XCALLOC (descr->sizeof_raw_register_valid_p, char);
2d28509a 328 regcache->readonly_p = 1;
3fadccb3
AC
329 return regcache;
330}
331
332void
333regcache_xfree (struct regcache *regcache)
334{
335 if (regcache == NULL)
336 return;
51b1fe4e
AC
337 xfree (regcache->registers);
338 xfree (regcache->register_valid_p);
3fadccb3
AC
339 xfree (regcache);
340}
341
b9362cc7 342static void
36160dc4
AC
343do_regcache_xfree (void *data)
344{
345 regcache_xfree (data);
346}
347
348struct cleanup *
349make_cleanup_regcache_xfree (struct regcache *regcache)
350{
351 return make_cleanup (do_regcache_xfree, regcache);
352}
353
51b1fe4e
AC
354/* Return a pointer to register REGNUM's buffer cache. */
355
356static char *
357register_buffer (struct regcache *regcache, int regnum)
358{
359 return regcache->registers + regcache->descr->register_offset[regnum];
360}
361
2d28509a 362void
5602984a
AC
363regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
364 void *src)
2d28509a
AC
365{
366 struct gdbarch *gdbarch = dst->descr->gdbarch;
123a958e 367 char buf[MAX_REGISTER_SIZE];
2d28509a 368 int regnum;
2d28509a 369 /* The DST should be `read-only', if it wasn't then the save would
5602984a 370 end up trying to write the register values back out to the
2d28509a 371 target. */
2d28509a
AC
372 gdb_assert (dst->readonly_p);
373 /* Clear the dest. */
374 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
375 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
376 /* Copy over any registers (identified by their membership in the
5602984a
AC
377 save_reggroup) and mark them as valid. The full [0 .. NUM_REGS +
378 NUM_PSEUDO_REGS) range is checked since some architectures need
379 to save/restore `cooked' registers that live in memory. */
2d28509a
AC
380 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
381 {
382 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
383 {
5602984a
AC
384 int valid = cooked_read (src, regnum, buf);
385 if (valid)
386 {
387 memcpy (register_buffer (dst, regnum), buf,
388 register_size (gdbarch, regnum));
389 dst->register_valid_p[regnum] = 1;
390 }
2d28509a
AC
391 }
392 }
393}
394
395void
5602984a
AC
396regcache_restore (struct regcache *dst,
397 regcache_cooked_read_ftype *cooked_read,
398 void *src)
2d28509a
AC
399{
400 struct gdbarch *gdbarch = dst->descr->gdbarch;
123a958e 401 char buf[MAX_REGISTER_SIZE];
2d28509a 402 int regnum;
5602984a
AC
403 /* The dst had better not be read-only. If it is, the `restore'
404 doesn't make much sense. */
2d28509a 405 gdb_assert (!dst->readonly_p);
2d28509a 406 /* Copy over any registers, being careful to only restore those that
5602984a
AC
407 were both saved and need to be restored. The full [0 .. NUM_REGS
408 + NUM_PSEUDO_REGS) range is checked since some architectures need
409 to save/restore `cooked' registers that live in memory. */
410 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
2d28509a 411 {
5602984a 412 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
2d28509a 413 {
5602984a
AC
414 int valid = cooked_read (src, regnum, buf);
415 if (valid)
416 regcache_cooked_write (dst, regnum, buf);
2d28509a
AC
417 }
418 }
419}
420
5602984a
AC
421static int
422do_cooked_read (void *src, int regnum, void *buf)
423{
424 struct regcache *regcache = src;
6f4e5a41 425 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
5602984a
AC
426 /* Don't even think about fetching a register from a read-only
427 cache when the register isn't yet valid. There isn't a target
428 from which the register value can be fetched. */
429 return 0;
430 regcache_cooked_read (regcache, regnum, buf);
431 return 1;
432}
433
434
3fadccb3
AC
435void
436regcache_cpy (struct regcache *dst, struct regcache *src)
437{
438 int i;
439 char *buf;
440 gdb_assert (src != NULL && dst != NULL);
441 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
442 gdb_assert (src != dst);
2d28509a
AC
443 gdb_assert (src->readonly_p || dst->readonly_p);
444 if (!src->readonly_p)
5602984a 445 regcache_save (dst, do_cooked_read, src);
2d28509a 446 else if (!dst->readonly_p)
5602984a 447 regcache_restore (dst, do_cooked_read, src);
2d28509a
AC
448 else
449 regcache_cpy_no_passthrough (dst, src);
3fadccb3
AC
450}
451
452void
453regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
454{
455 int i;
456 gdb_assert (src != NULL && dst != NULL);
457 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
458 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
459 move of data into the current_regcache(). Doing this would be
460 silly - it would mean that valid_p would be completly invalid. */
461 gdb_assert (dst != current_regcache);
51b1fe4e
AC
462 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
463 memcpy (dst->register_valid_p, src->register_valid_p,
3fadccb3
AC
464 dst->descr->sizeof_raw_register_valid_p);
465}
466
467struct regcache *
468regcache_dup (struct regcache *src)
469{
470 struct regcache *newbuf;
471 gdb_assert (current_regcache != NULL);
472 newbuf = regcache_xmalloc (src->descr->gdbarch);
473 regcache_cpy (newbuf, src);
474 return newbuf;
475}
476
477struct regcache *
478regcache_dup_no_passthrough (struct regcache *src)
479{
480 struct regcache *newbuf;
481 gdb_assert (current_regcache != NULL);
482 newbuf = regcache_xmalloc (src->descr->gdbarch);
483 regcache_cpy_no_passthrough (newbuf, src);
484 return newbuf;
485}
486
487int
488regcache_valid_p (struct regcache *regcache, int regnum)
489{
490 gdb_assert (regcache != NULL);
491 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
51b1fe4e 492 return regcache->register_valid_p[regnum];
3fadccb3
AC
493}
494
3fadccb3
AC
495char *
496deprecated_grub_regcache_for_registers (struct regcache *regcache)
497{
51b1fe4e 498 return regcache->registers;
3fadccb3
AC
499}
500
3fadccb3
AC
501/* Global structure containing the current regcache. */
502/* FIXME: cagney/2002-05-11: The two global arrays registers[] and
8262ee23 503 deprecated_register_valid[] currently point into this structure. */
3fadccb3
AC
504struct regcache *current_regcache;
505
5ebd2499 506/* NOTE: this is a write-through cache. There is no "dirty" bit for
32178cab
MS
507 recording if the register values have been changed (eg. by the
508 user). Therefore all registers must be written back to the
509 target when appropriate. */
510
511/* REGISTERS contains the cached register values (in target byte order). */
512
524d7c18 513char *deprecated_registers;
32178cab 514
8262ee23 515/* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
32178cab
MS
516 1 if it has been fetched, and
517 -1 if the register value was not available.
c97dcfc7
AC
518
519 "Not available" indicates that the target is not not able to supply
520 the register at this state. The register may become available at a
521 later time (after the next resume). This often occures when GDB is
522 manipulating a target that contains only a snapshot of the entire
523 system being debugged - some of the registers in such a system may
524 not have been saved. */
32178cab 525
8262ee23 526signed char *deprecated_register_valid;
32178cab 527
39f77062 528/* The thread/process associated with the current set of registers. */
32178cab 529
39f77062 530static ptid_t registers_ptid;
32178cab
MS
531
532/*
533 * FUNCTIONS:
534 */
535
536/* REGISTER_CACHED()
537
538 Returns 0 if the value is not in the cache (needs fetch).
539 >0 if the value is in the cache.
540 <0 if the value is permanently unavailable (don't ask again). */
541
542int
543register_cached (int regnum)
544{
8262ee23 545 return deprecated_register_valid[regnum];
32178cab
MS
546}
547
7302a204
ND
548/* Record that REGNUM's value is cached if STATE is >0, uncached but
549 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
550
551void
552set_register_cached (int regnum, int state)
553{
53826de9
AC
554 gdb_assert (regnum >= 0);
555 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
51b1fe4e 556 current_regcache->register_valid_p[regnum] = state;
7302a204
ND
557}
558
559/* Return whether register REGNUM is a real register. */
560
561static int
562real_register (int regnum)
563{
564 return regnum >= 0 && regnum < NUM_REGS;
565}
566
32178cab
MS
567/* Low level examining and depositing of registers.
568
569 The caller is responsible for making sure that the inferior is
570 stopped before calling the fetching routines, or it will get
571 garbage. (a change from GDB version 3, in which the caller got the
572 value from the last stop). */
573
574/* REGISTERS_CHANGED ()
575
576 Indicate that registers may have changed, so invalidate the cache. */
577
578void
579registers_changed (void)
580{
581 int i;
32178cab 582
39f77062 583 registers_ptid = pid_to_ptid (-1);
32178cab
MS
584
585 /* Force cleanup of any alloca areas if using C alloca instead of
586 a builtin alloca. This particular call is used to clean up
587 areas allocated by low level target code which may build up
588 during lengthy interactions between gdb and the target before
589 gdb gives control to the user (ie watchpoints). */
590 alloca (0);
591
53826de9 592 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
7302a204 593 set_register_cached (i, 0);
32178cab
MS
594
595 if (registers_changed_hook)
596 registers_changed_hook ();
597}
598
2b9e5f3f 599/* DEPRECATED_REGISTERS_FETCHED ()
32178cab
MS
600
601 Indicate that all registers have been fetched, so mark them all valid. */
602
31e9866e
AC
603/* NOTE: cagney/2001-12-04: This function does not set valid on the
604 pseudo-register range since pseudo registers are always supplied
605 using supply_register(). */
606/* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
607 code was blatting the registers[] array and then calling this.
608 Since targets should only be using supply_register() the need for
609 this function/hack is eliminated. */
32178cab
MS
610
611void
2b9e5f3f 612deprecated_registers_fetched (void)
32178cab
MS
613{
614 int i;
32178cab 615
a728f042 616 for (i = 0; i < NUM_REGS; i++)
7302a204 617 set_register_cached (i, 1);
fcdc5976 618 /* Do not assume that the pseudo-regs have also been fetched.
31e9866e 619 Fetching all real regs NEVER accounts for pseudo-regs. */
32178cab
MS
620}
621
73937e03
AC
622/* deprecated_read_register_bytes and deprecated_write_register_bytes
623 are generally a *BAD* idea. They are inefficient because they need
624 to check for partial updates, which can only be done by scanning
625 through all of the registers and seeing if the bytes that are being
626 read/written fall inside of an invalid register. [The main reason
627 this is necessary is that register sizes can vary, so a simple
628 index won't suffice.] It is far better to call read_register_gen
629 and write_register_gen if you want to get at the raw register
630 contents, as it only takes a regnum as an argument, and therefore
631 can't do a partial register update.
32178cab
MS
632
633 Prior to the recent fixes to check for partial updates, both read
73937e03
AC
634 and deprecated_write_register_bytes always checked to see if any
635 registers were stale, and then called target_fetch_registers (-1)
636 to update the whole set. This caused really slowed things down for
637 remote targets. */
32178cab
MS
638
639/* Copy INLEN bytes of consecutive data from registers
640 starting with the INREGBYTE'th byte of register data
641 into memory at MYADDR. */
642
643void
73937e03 644deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
32178cab 645{
61a0eb5b 646 int in_end = in_start + in_len;
5ebd2499 647 int regnum;
d9d9c31f 648 char reg_buf[MAX_REGISTER_SIZE];
32178cab
MS
649
650 /* See if we are trying to read bytes from out-of-date registers. If so,
651 update just those registers. */
652
5ebd2499 653 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab 654 {
61a0eb5b
AC
655 int reg_start;
656 int reg_end;
657 int reg_len;
658 int start;
659 int end;
660 int byte;
32178cab 661
61a0eb5b
AC
662 reg_start = REGISTER_BYTE (regnum);
663 reg_len = REGISTER_RAW_SIZE (regnum);
664 reg_end = reg_start + reg_len;
32178cab 665
61a0eb5b 666 if (reg_end <= in_start || in_end <= reg_start)
5ebd2499 667 /* The range the user wants to read doesn't overlap with regnum. */
32178cab
MS
668 continue;
669
275f450c
AC
670 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
671 /* Force the cache to fetch the entire register. */
4caf0990 672 deprecated_read_register_gen (regnum, reg_buf);
275f450c
AC
673 else
674 /* Legacy note: even though this register is ``invalid'' we
675 still need to return something. It would appear that some
676 code relies on apparent gaps in the register array also
677 being returned. */
678 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
679 the entire register read/write flow of control. Must
680 resist temptation to return 0xdeadbeef. */
524d7c18 681 memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
32178cab 682
61a0eb5b
AC
683 /* Legacy note: This function, for some reason, allows a NULL
684 input buffer. If the buffer is NULL, the registers are still
685 fetched, just the final transfer is skipped. */
686 if (in_buf == NULL)
687 continue;
688
689 /* start = max (reg_start, in_start) */
690 if (reg_start > in_start)
691 start = reg_start;
692 else
693 start = in_start;
694
695 /* end = min (reg_end, in_end) */
696 if (reg_end < in_end)
697 end = reg_end;
698 else
699 end = in_end;
700
701 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
702 for (byte = start; byte < end; byte++)
165cd47f 703 {
61a0eb5b 704 in_buf[byte - in_start] = reg_buf[byte - reg_start];
165cd47f 705 }
32178cab 706 }
32178cab
MS
707}
708
5ebd2499
ND
709/* Read register REGNUM into memory at MYADDR, which must be large
710 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
32178cab
MS
711 register is known to be the size of a CORE_ADDR or smaller,
712 read_register can be used instead. */
713
61a0eb5b
AC
714static void
715legacy_read_register_gen (int regnum, char *myaddr)
32178cab 716{
61a0eb5b 717 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
39f77062 718 if (! ptid_equal (registers_ptid, inferior_ptid))
32178cab
MS
719 {
720 registers_changed ();
39f77062 721 registers_ptid = inferior_ptid;
32178cab
MS
722 }
723
7302a204 724 if (!register_cached (regnum))
5c27f28a 725 target_fetch_registers (regnum);
7302a204 726
3fadccb3 727 memcpy (myaddr, register_buffer (current_regcache, regnum),
5ebd2499 728 REGISTER_RAW_SIZE (regnum));
32178cab
MS
729}
730
61a0eb5b 731void
1aaa5f99 732regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
61a0eb5b 733{
3fadccb3
AC
734 gdb_assert (regcache != NULL && buf != NULL);
735 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
736 if (regcache->descr->legacy_p
2d28509a 737 && !regcache->readonly_p)
3fadccb3
AC
738 {
739 gdb_assert (regcache == current_regcache);
740 /* For moment, just use underlying legacy code. Ulgh!!! This
741 silently and very indirectly updates the regcache's regcache
8262ee23 742 via the global deprecated_register_valid[]. */
3fadccb3
AC
743 legacy_read_register_gen (regnum, buf);
744 return;
745 }
746 /* Make certain that the register cache is up-to-date with respect
747 to the current thread. This switching shouldn't be necessary
748 only there is still only one target side register cache. Sigh!
749 On the bright side, at least there is a regcache object. */
2d28509a 750 if (!regcache->readonly_p)
3fadccb3
AC
751 {
752 gdb_assert (regcache == current_regcache);
753 if (! ptid_equal (registers_ptid, inferior_ptid))
754 {
755 registers_changed ();
756 registers_ptid = inferior_ptid;
757 }
758 if (!register_cached (regnum))
5c27f28a 759 target_fetch_registers (regnum);
3fadccb3
AC
760 }
761 /* Copy the value directly into the register cache. */
51b1fe4e 762 memcpy (buf, register_buffer (regcache, regnum),
3fadccb3 763 regcache->descr->sizeof_register[regnum]);
61a0eb5b
AC
764}
765
28fc6740
AC
766void
767regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
768{
769 char *buf;
770 gdb_assert (regcache != NULL);
771 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
772 buf = alloca (regcache->descr->sizeof_register[regnum]);
773 regcache_raw_read (regcache, regnum, buf);
774 (*val) = extract_signed_integer (buf,
775 regcache->descr->sizeof_register[regnum]);
776}
777
778void
779regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
780 ULONGEST *val)
781{
782 char *buf;
783 gdb_assert (regcache != NULL);
784 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
785 buf = alloca (regcache->descr->sizeof_register[regnum]);
786 regcache_raw_read (regcache, regnum, buf);
787 (*val) = extract_unsigned_integer (buf,
788 regcache->descr->sizeof_register[regnum]);
789}
790
c00dcbe9
MK
791void
792regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
793{
794 void *buf;
795 gdb_assert (regcache != NULL);
796 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
797 buf = alloca (regcache->descr->sizeof_register[regnum]);
798 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
799 regcache_raw_write (regcache, regnum, buf);
800}
801
802void
803regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
804 ULONGEST val)
805{
806 void *buf;
807 gdb_assert (regcache != NULL);
808 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
809 buf = alloca (regcache->descr->sizeof_register[regnum]);
810 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
811 regcache_raw_write (regcache, regnum, buf);
812}
813
61a0eb5b 814void
4caf0990 815deprecated_read_register_gen (int regnum, char *buf)
61a0eb5b 816{
3fadccb3
AC
817 gdb_assert (current_regcache != NULL);
818 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
819 if (current_regcache->descr->legacy_p)
61a0eb5b
AC
820 {
821 legacy_read_register_gen (regnum, buf);
822 return;
823 }
68365089
AC
824 regcache_cooked_read (current_regcache, regnum, buf);
825}
826
827void
29e1842b 828regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
68365089 829{
d138e37a 830 gdb_assert (regnum >= 0);
68365089
AC
831 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
832 if (regnum < regcache->descr->nr_raw_registers)
833 regcache_raw_read (regcache, regnum, buf);
2d28509a
AC
834 else if (regcache->readonly_p
835 && regnum < regcache->descr->nr_cooked_registers
836 && regcache->register_valid_p[regnum])
837 /* Read-only register cache, perhaphs the cooked value was cached? */
838 memcpy (buf, register_buffer (regcache, regnum),
839 regcache->descr->sizeof_register[regnum]);
d138e37a 840 else
68365089
AC
841 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
842 regnum, buf);
61a0eb5b
AC
843}
844
a378f419
AC
845void
846regcache_cooked_read_signed (struct regcache *regcache, int regnum,
847 LONGEST *val)
848{
849 char *buf;
850 gdb_assert (regcache != NULL);
a66a9c23 851 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
852 buf = alloca (regcache->descr->sizeof_register[regnum]);
853 regcache_cooked_read (regcache, regnum, buf);
854 (*val) = extract_signed_integer (buf,
855 regcache->descr->sizeof_register[regnum]);
856}
857
858void
859regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
860 ULONGEST *val)
861{
862 char *buf;
863 gdb_assert (regcache != NULL);
a66a9c23 864 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
a378f419
AC
865 buf = alloca (regcache->descr->sizeof_register[regnum]);
866 regcache_cooked_read (regcache, regnum, buf);
867 (*val) = extract_unsigned_integer (buf,
868 regcache->descr->sizeof_register[regnum]);
869}
870
a66a9c23
AC
871void
872regcache_cooked_write_signed (struct regcache *regcache, int regnum,
873 LONGEST val)
874{
875 void *buf;
876 gdb_assert (regcache != NULL);
877 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
878 buf = alloca (regcache->descr->sizeof_register[regnum]);
879 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
880 regcache_cooked_write (regcache, regnum, buf);
881}
882
883void
884regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
885 ULONGEST val)
886{
887 void *buf;
888 gdb_assert (regcache != NULL);
889 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
890 buf = alloca (regcache->descr->sizeof_register[regnum]);
891 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
892 regcache_cooked_write (regcache, regnum, buf);
893}
894
5ebd2499
ND
895/* Write register REGNUM at MYADDR to the target. MYADDR points at
896 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
32178cab 897
61a0eb5b 898static void
1aaa5f99 899legacy_write_register_gen (int regnum, const void *myaddr)
32178cab
MS
900{
901 int size;
61a0eb5b 902 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
32178cab
MS
903
904 /* On the sparc, writing %g0 is a no-op, so we don't even want to
905 change the registers array if something writes to this register. */
5ebd2499 906 if (CANNOT_STORE_REGISTER (regnum))
32178cab
MS
907 return;
908
39f77062 909 if (! ptid_equal (registers_ptid, inferior_ptid))
32178cab
MS
910 {
911 registers_changed ();
39f77062 912 registers_ptid = inferior_ptid;
32178cab
MS
913 }
914
5ebd2499 915 size = REGISTER_RAW_SIZE (regnum);
32178cab 916
7302a204 917 if (real_register (regnum))
1297a2f0
MS
918 {
919 /* If we have a valid copy of the register, and new value == old
920 value, then don't bother doing the actual store. */
921 if (register_cached (regnum)
3fadccb3
AC
922 && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
923 == 0))
1297a2f0
MS
924 return;
925 else
926 target_prepare_to_store ();
927 }
32178cab 928
3fadccb3 929 memcpy (register_buffer (current_regcache, regnum), myaddr, size);
32178cab 930
7302a204 931 set_register_cached (regnum, 1);
5c27f28a 932 target_store_registers (regnum);
32178cab
MS
933}
934
61a0eb5b 935void
1aaa5f99 936regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
61a0eb5b 937{
3fadccb3
AC
938 gdb_assert (regcache != NULL && buf != NULL);
939 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
2d28509a 940 gdb_assert (!regcache->readonly_p);
3fadccb3 941
2d28509a 942 if (regcache->descr->legacy_p)
3fadccb3
AC
943 {
944 /* For moment, just use underlying legacy code. Ulgh!!! This
945 silently and very indirectly updates the regcache's buffers
8262ee23 946 via the globals deprecated_register_valid[] and registers[]. */
3fadccb3
AC
947 gdb_assert (regcache == current_regcache);
948 legacy_write_register_gen (regnum, buf);
949 return;
950 }
951
952 /* On the sparc, writing %g0 is a no-op, so we don't even want to
953 change the registers array if something writes to this register. */
954 if (CANNOT_STORE_REGISTER (regnum))
955 return;
956
3fadccb3
AC
957 /* Make certain that the correct cache is selected. */
958 gdb_assert (regcache == current_regcache);
959 if (! ptid_equal (registers_ptid, inferior_ptid))
960 {
961 registers_changed ();
962 registers_ptid = inferior_ptid;
963 }
964
965 /* If we have a valid copy of the register, and new value == old
966 value, then don't bother doing the actual store. */
967 if (regcache_valid_p (regcache, regnum)
968 && (memcmp (register_buffer (regcache, regnum), buf,
969 regcache->descr->sizeof_register[regnum]) == 0))
970 return;
971
972 target_prepare_to_store ();
973 memcpy (register_buffer (regcache, regnum), buf,
974 regcache->descr->sizeof_register[regnum]);
51b1fe4e 975 regcache->register_valid_p[regnum] = 1;
5c27f28a 976 target_store_registers (regnum);
61a0eb5b
AC
977}
978
979void
4caf0990 980deprecated_write_register_gen (int regnum, char *buf)
61a0eb5b 981{
3fadccb3
AC
982 gdb_assert (current_regcache != NULL);
983 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
984 if (current_regcache->descr->legacy_p)
61a0eb5b
AC
985 {
986 legacy_write_register_gen (regnum, buf);
987 return;
988 }
68365089
AC
989 regcache_cooked_write (current_regcache, regnum, buf);
990}
991
992void
29e1842b 993regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
68365089 994{
d138e37a 995 gdb_assert (regnum >= 0);
68365089
AC
996 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
997 if (regnum < regcache->descr->nr_raw_registers)
998 regcache_raw_write (regcache, regnum, buf);
d138e37a 999 else
68365089 1000 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
d8124050 1001 regnum, buf);
61a0eb5b
AC
1002}
1003
32178cab
MS
1004/* Copy INLEN bytes of consecutive data from memory at MYADDR
1005 into registers starting with the MYREGSTART'th byte of register data. */
1006
1007void
73937e03 1008deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
32178cab
MS
1009{
1010 int myregend = myregstart + inlen;
5ebd2499 1011 int regnum;
32178cab
MS
1012
1013 target_prepare_to_store ();
1014
1015 /* Scan through the registers updating any that are covered by the
1016 range myregstart<=>myregend using write_register_gen, which does
1017 nice things like handling threads, and avoiding updates when the
1018 new and old contents are the same. */
1019
5ebd2499 1020 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
32178cab
MS
1021 {
1022 int regstart, regend;
1023
5ebd2499
ND
1024 regstart = REGISTER_BYTE (regnum);
1025 regend = regstart + REGISTER_RAW_SIZE (regnum);
32178cab
MS
1026
1027 /* Is this register completely outside the range the user is writing? */
1028 if (myregend <= regstart || regend <= myregstart)
1029 /* do nothing */ ;
1030
1031 /* Is this register completely within the range the user is writing? */
1032 else if (myregstart <= regstart && regend <= myregend)
4caf0990 1033 deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
32178cab
MS
1034
1035 /* The register partially overlaps the range being written. */
1036 else
1037 {
d9d9c31f 1038 char regbuf[MAX_REGISTER_SIZE];
32178cab
MS
1039 /* What's the overlap between this register's bytes and
1040 those the caller wants to write? */
1041 int overlapstart = max (regstart, myregstart);
1042 int overlapend = min (regend, myregend);
1043
1044 /* We may be doing a partial update of an invalid register.
1045 Update it from the target before scribbling on it. */
4caf0990 1046 deprecated_read_register_gen (regnum, regbuf);
32178cab 1047
524d7c18 1048 memcpy (&deprecated_registers[overlapstart],
32178cab
MS
1049 myaddr + (overlapstart - myregstart),
1050 overlapend - overlapstart);
1051
5c27f28a 1052 target_store_registers (regnum);
32178cab
MS
1053 }
1054 }
1055}
1056
06c0b04e
AC
1057/* Perform a partial register transfer using a read, modify, write
1058 operation. */
1059
1060typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
1061 void *buf);
1062typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
1063 const void *buf);
1064
b9362cc7 1065static void
06c0b04e
AC
1066regcache_xfer_part (struct regcache *regcache, int regnum,
1067 int offset, int len, void *in, const void *out,
1068 regcache_read_ftype *read, regcache_write_ftype *write)
1069{
1070 struct regcache_descr *descr = regcache->descr;
123a958e 1071 bfd_byte reg[MAX_REGISTER_SIZE];
06c0b04e
AC
1072 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
1073 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
1074 /* Something to do? */
1075 if (offset + len == 0)
1076 return;
1077 /* Read (when needed) ... */
1078 if (in != NULL
1079 || offset > 0
1080 || offset + len < descr->sizeof_register[regnum])
1081 {
1082 gdb_assert (read != NULL);
1083 read (regcache, regnum, reg);
1084 }
1085 /* ... modify ... */
1086 if (in != NULL)
1087 memcpy (in, reg + offset, len);
1088 if (out != NULL)
1089 memcpy (reg + offset, out, len);
1090 /* ... write (when needed). */
1091 if (out != NULL)
1092 {
1093 gdb_assert (write != NULL);
1094 write (regcache, regnum, reg);
1095 }
1096}
1097
1098void
1099regcache_raw_read_part (struct regcache *regcache, int regnum,
1100 int offset, int len, void *buf)
1101{
1102 struct regcache_descr *descr = regcache->descr;
1103 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1104 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1105 regcache_raw_read, regcache_raw_write);
1106}
1107
1108void
1109regcache_raw_write_part (struct regcache *regcache, int regnum,
1110 int offset, int len, const void *buf)
1111{
1112 struct regcache_descr *descr = regcache->descr;
1113 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1114 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1115 regcache_raw_read, regcache_raw_write);
1116}
1117
1118void
1119regcache_cooked_read_part (struct regcache *regcache, int regnum,
1120 int offset, int len, void *buf)
1121{
1122 struct regcache_descr *descr = regcache->descr;
1123 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1124 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1125 regcache_cooked_read, regcache_cooked_write);
1126}
1127
1128void
1129regcache_cooked_write_part (struct regcache *regcache, int regnum,
1130 int offset, int len, const void *buf)
1131{
1132 struct regcache_descr *descr = regcache->descr;
1133 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1134 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1135 regcache_cooked_read, regcache_cooked_write);
1136}
32178cab 1137
d3b22ed5
AC
1138/* Hack to keep code that view the register buffer as raw bytes
1139 working. */
1140
1141int
1142register_offset_hack (struct gdbarch *gdbarch, int regnum)
1143{
1144 struct regcache_descr *descr = regcache_descr (gdbarch);
1145 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1146 return descr->register_offset[regnum];
1147}
1148
5ebd2499 1149/* Return the contents of register REGNUM as an unsigned integer. */
32178cab 1150
173155e8 1151ULONGEST
5ebd2499 1152read_register (int regnum)
32178cab 1153{
61a0eb5b 1154 char *buf = alloca (REGISTER_RAW_SIZE (regnum));
4caf0990 1155 deprecated_read_register_gen (regnum, buf);
61a0eb5b 1156 return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
32178cab
MS
1157}
1158
173155e8 1159ULONGEST
39f77062 1160read_register_pid (int regnum, ptid_t ptid)
32178cab 1161{
39f77062 1162 ptid_t save_ptid;
32178cab
MS
1163 int save_pid;
1164 CORE_ADDR retval;
1165
39f77062 1166 if (ptid_equal (ptid, inferior_ptid))
5ebd2499 1167 return read_register (regnum);
32178cab 1168
39f77062 1169 save_ptid = inferior_ptid;
32178cab 1170
39f77062 1171 inferior_ptid = ptid;
32178cab 1172
5ebd2499 1173 retval = read_register (regnum);
32178cab 1174
39f77062 1175 inferior_ptid = save_ptid;
32178cab
MS
1176
1177 return retval;
1178}
1179
5ebd2499 1180/* Store VALUE into the raw contents of register number REGNUM. */
32178cab
MS
1181
1182void
5ebd2499 1183write_register (int regnum, LONGEST val)
32178cab 1184{
61a0eb5b 1185 void *buf;
32178cab 1186 int size;
5ebd2499 1187 size = REGISTER_RAW_SIZE (regnum);
32178cab
MS
1188 buf = alloca (size);
1189 store_signed_integer (buf, size, (LONGEST) val);
4caf0990 1190 deprecated_write_register_gen (regnum, buf);
32178cab
MS
1191}
1192
1193void
39f77062 1194write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
32178cab 1195{
39f77062 1196 ptid_t save_ptid;
32178cab 1197
39f77062 1198 if (ptid_equal (ptid, inferior_ptid))
32178cab 1199 {
5ebd2499 1200 write_register (regnum, val);
32178cab
MS
1201 return;
1202 }
1203
39f77062 1204 save_ptid = inferior_ptid;
32178cab 1205
39f77062 1206 inferior_ptid = ptid;
32178cab 1207
5ebd2499 1208 write_register (regnum, val);
32178cab 1209
39f77062 1210 inferior_ptid = save_ptid;
32178cab
MS
1211}
1212
1213/* SUPPLY_REGISTER()
1214
5ebd2499 1215 Record that register REGNUM contains VAL. This is used when the
32178cab
MS
1216 value is obtained from the inferior or core dump, so there is no
1217 need to store the value there.
1218
1219 If VAL is a NULL pointer, then it's probably an unsupported register.
5ebd2499 1220 We just set its value to all zeros. We might want to record this
32178cab
MS
1221 fact, and report it to the users of read_register and friends. */
1222
1223void
1aaa5f99 1224supply_register (int regnum, const void *val)
32178cab
MS
1225{
1226#if 1
39f77062 1227 if (! ptid_equal (registers_ptid, inferior_ptid))
32178cab
MS
1228 {
1229 registers_changed ();
39f77062 1230 registers_ptid = inferior_ptid;
32178cab
MS
1231 }
1232#endif
1233
7302a204 1234 set_register_cached (regnum, 1);
32178cab 1235 if (val)
3fadccb3 1236 memcpy (register_buffer (current_regcache, regnum), val,
5ebd2499 1237 REGISTER_RAW_SIZE (regnum));
32178cab 1238 else
3fadccb3 1239 memset (register_buffer (current_regcache, regnum), '\000',
5ebd2499 1240 REGISTER_RAW_SIZE (regnum));
32178cab
MS
1241
1242 /* On some architectures, e.g. HPPA, there are a few stray bits in
1243 some registers, that the rest of the code would like to ignore. */
1244
61a0eb5b
AC
1245 /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1246 going to be deprecated. Instead architectures will leave the raw
1247 register value as is and instead clean things up as they pass
d8124050 1248 through the method gdbarch_pseudo_register_read() clean up the
61a0eb5b
AC
1249 values. */
1250
4ee3352d 1251#ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
0b434a00
AC
1252 DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1253 (regnum, register_buffer (current_regcache, regnum));
32178cab
MS
1254#endif
1255}
1256
193cb69f
AC
1257void
1258regcache_collect (int regnum, void *buf)
1259{
3fadccb3
AC
1260 memcpy (buf, register_buffer (current_regcache, regnum),
1261 REGISTER_RAW_SIZE (regnum));
193cb69f
AC
1262}
1263
1264
0ba6dca9
AC
1265/* read_pc, write_pc, read_sp, deprecated_read_fp, etc. Special
1266 handling for registers PC, SP, and FP. */
32178cab 1267
cde9ea48
AC
1268/* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(),
1269 read_sp(), and deprecated_read_fp(), will eventually be replaced by
1270 per-frame methods. Instead of relying on the global INFERIOR_PTID,
1271 they will use the contextual information provided by the FRAME.
1272 These functions do not belong in the register cache. */
32178cab 1273
cde9ea48
AC
1274/* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1275 write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to
1276 be replaced by something that does not rely on global state. But
1277 what? */
32178cab
MS
1278
1279CORE_ADDR
39f77062 1280read_pc_pid (ptid_t ptid)
32178cab 1281{
39f77062 1282 ptid_t saved_inferior_ptid;
32178cab
MS
1283 CORE_ADDR pc_val;
1284
39f77062
KB
1285 /* In case ptid != inferior_ptid. */
1286 saved_inferior_ptid = inferior_ptid;
1287 inferior_ptid = ptid;
32178cab 1288
cde9ea48
AC
1289 if (TARGET_READ_PC_P ())
1290 pc_val = TARGET_READ_PC (ptid);
1291 /* Else use per-frame method on get_current_frame. */
1292 else if (PC_REGNUM >= 0)
1293 {
1294 CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1295 CORE_ADDR pc_val = ADDR_BITS_REMOVE (raw_val);
1296 return pc_val;
1297 }
1298 else
1299 internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
32178cab 1300
39f77062 1301 inferior_ptid = saved_inferior_ptid;
32178cab
MS
1302 return pc_val;
1303}
1304
1305CORE_ADDR
1306read_pc (void)
1307{
39f77062 1308 return read_pc_pid (inferior_ptid);
32178cab
MS
1309}
1310
32178cab 1311void
39f77062 1312generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
32178cab
MS
1313{
1314#ifdef PC_REGNUM
1315 if (PC_REGNUM >= 0)
39f77062 1316 write_register_pid (PC_REGNUM, pc, ptid);
32178cab 1317 if (NPC_REGNUM >= 0)
39f77062 1318 write_register_pid (NPC_REGNUM, pc + 4, ptid);
32178cab 1319#else
8e65ff28
AC
1320 internal_error (__FILE__, __LINE__,
1321 "generic_target_write_pc");
32178cab
MS
1322#endif
1323}
1324
1325void
39f77062 1326write_pc_pid (CORE_ADDR pc, ptid_t ptid)
32178cab 1327{
39f77062 1328 ptid_t saved_inferior_ptid;
32178cab 1329
39f77062
KB
1330 /* In case ptid != inferior_ptid. */
1331 saved_inferior_ptid = inferior_ptid;
1332 inferior_ptid = ptid;
32178cab 1333
39f77062 1334 TARGET_WRITE_PC (pc, ptid);
32178cab 1335
39f77062 1336 inferior_ptid = saved_inferior_ptid;
32178cab
MS
1337}
1338
1339void
1340write_pc (CORE_ADDR pc)
1341{
39f77062 1342 write_pc_pid (pc, inferior_ptid);
32178cab
MS
1343}
1344
1345/* Cope with strage ways of getting to the stack and frame pointers */
1346
32178cab
MS
1347CORE_ADDR
1348read_sp (void)
1349{
bd1ce8ba
AC
1350 if (TARGET_READ_SP_P ())
1351 return TARGET_READ_SP ();
a9e5fdc2
AC
1352 else if (gdbarch_unwind_sp_p (current_gdbarch))
1353 return get_frame_sp (get_current_frame ());
bd1ce8ba 1354 else if (SP_REGNUM >= 0)
a9e5fdc2
AC
1355 /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1356 about the architecture so put it at the end. */
bd1ce8ba
AC
1357 return read_register (SP_REGNUM);
1358 internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
32178cab
MS
1359}
1360
32178cab 1361void
b46e02f6 1362deprecated_write_sp (CORE_ADDR val)
32178cab 1363{
b46e02f6
AC
1364 gdb_assert (SP_REGNUM >= 0);
1365 write_register (SP_REGNUM, val);
32178cab
MS
1366}
1367
32178cab 1368CORE_ADDR
0ba6dca9 1369deprecated_read_fp (void)
32178cab 1370{
0ba6dca9
AC
1371 if (DEPRECATED_TARGET_READ_FP_P ())
1372 return DEPRECATED_TARGET_READ_FP ();
1373 else if (DEPRECATED_FP_REGNUM >= 0)
1374 return read_register (DEPRECATED_FP_REGNUM);
1375 else
1376 internal_error (__FILE__, __LINE__, "deprecated_read_fp");
32178cab
MS
1377}
1378
705152c5
MS
1379/* ARGSUSED */
1380static void
1381reg_flush_command (char *command, int from_tty)
1382{
1383 /* Force-flush the register cache. */
1384 registers_changed ();
1385 if (from_tty)
1386 printf_filtered ("Register cache flushed.\n");
1387}
1388
32178cab
MS
1389static void
1390build_regcache (void)
3fadccb3
AC
1391{
1392 current_regcache = regcache_xmalloc (current_gdbarch);
2d28509a 1393 current_regcache->readonly_p = 0;
524d7c18 1394 deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
b923b08d 1395 deprecated_register_valid = current_regcache->register_valid_p;
3fadccb3
AC
1396}
1397
af030b9a
AC
1398static void
1399dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1400 const unsigned char *buf, long len)
1401{
1402 int i;
1403 switch (endian)
1404 {
1405 case BFD_ENDIAN_BIG:
1406 for (i = 0; i < len; i++)
1407 fprintf_unfiltered (file, "%02x", buf[i]);
1408 break;
1409 case BFD_ENDIAN_LITTLE:
1410 for (i = len - 1; i >= 0; i--)
1411 fprintf_unfiltered (file, "%02x", buf[i]);
1412 break;
1413 default:
1414 internal_error (__FILE__, __LINE__, "Bad switch");
1415 }
1416}
1417
1418enum regcache_dump_what
1419{
b59ff9d5 1420 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
af030b9a
AC
1421};
1422
1423static void
1424regcache_dump (struct regcache *regcache, struct ui_file *file,
1425 enum regcache_dump_what what_to_dump)
1426{
1427 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
b59ff9d5
AC
1428 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1429 struct reggroup *const *groups = reggroups (gdbarch);
af030b9a
AC
1430 int regnum;
1431 int footnote_nr = 0;
1432 int footnote_register_size = 0;
1433 int footnote_register_offset = 0;
1434 int footnote_register_type_name_null = 0;
1435 long register_offset = 0;
123a958e 1436 unsigned char buf[MAX_REGISTER_SIZE];
af030b9a
AC
1437
1438#if 0
1439 fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1440 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1441 regcache->descr->nr_raw_registers);
1442 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1443 regcache->descr->nr_cooked_registers);
1444 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1445 regcache->descr->sizeof_raw_registers);
1446 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1447 regcache->descr->sizeof_raw_register_valid_p);
af030b9a
AC
1448 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1449 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1450#endif
1451
1452 gdb_assert (regcache->descr->nr_cooked_registers
1453 == (NUM_REGS + NUM_PSEUDO_REGS));
1454
1455 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1456 {
1457 /* Name. */
1458 if (regnum < 0)
1459 fprintf_unfiltered (file, " %-10s", "Name");
1460 else
1461 {
1462 const char *p = REGISTER_NAME (regnum);
1463 if (p == NULL)
1464 p = "";
1465 else if (p[0] == '\0')
1466 p = "''";
1467 fprintf_unfiltered (file, " %-10s", p);
1468 }
1469
1470 /* Number. */
1471 if (regnum < 0)
1472 fprintf_unfiltered (file, " %4s", "Nr");
1473 else
1474 fprintf_unfiltered (file, " %4d", regnum);
1475
1476 /* Relative number. */
1477 if (regnum < 0)
1478 fprintf_unfiltered (file, " %4s", "Rel");
1479 else if (regnum < NUM_REGS)
1480 fprintf_unfiltered (file, " %4d", regnum);
1481 else
1482 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1483
1484 /* Offset. */
1485 if (regnum < 0)
1486 fprintf_unfiltered (file, " %6s ", "Offset");
1487 else
1488 {
1489 fprintf_unfiltered (file, " %6ld",
1490 regcache->descr->register_offset[regnum]);
a7e3c2ad 1491 if (register_offset != regcache->descr->register_offset[regnum]
d3b22ed5
AC
1492 || register_offset != REGISTER_BYTE (regnum)
1493 || (regnum > 0
1494 && (regcache->descr->register_offset[regnum]
1495 != (regcache->descr->register_offset[regnum - 1]
1496 + regcache->descr->sizeof_register[regnum - 1])))
1497 )
af030b9a
AC
1498 {
1499 if (!footnote_register_offset)
1500 footnote_register_offset = ++footnote_nr;
1501 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1502 }
1503 else
1504 fprintf_unfiltered (file, " ");
1505 register_offset = (regcache->descr->register_offset[regnum]
1506 + regcache->descr->sizeof_register[regnum]);
1507 }
1508
1509 /* Size. */
1510 if (regnum < 0)
1511 fprintf_unfiltered (file, " %5s ", "Size");
1512 else
1513 {
1514 fprintf_unfiltered (file, " %5ld",
1515 regcache->descr->sizeof_register[regnum]);
1516 if ((regcache->descr->sizeof_register[regnum]
1517 != REGISTER_RAW_SIZE (regnum))
1518 || (regcache->descr->sizeof_register[regnum]
1519 != REGISTER_VIRTUAL_SIZE (regnum))
1520 || (regcache->descr->sizeof_register[regnum]
bb425013
AC
1521 != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1522 regnum)))
af030b9a
AC
1523 )
1524 {
1525 if (!footnote_register_size)
1526 footnote_register_size = ++footnote_nr;
1527 fprintf_unfiltered (file, "*%d", footnote_register_size);
1528 }
1529 else
1530 fprintf_unfiltered (file, " ");
1531 }
1532
1533 /* Type. */
b59ff9d5
AC
1534 {
1535 const char *t;
1536 if (regnum < 0)
1537 t = "Type";
1538 else
1539 {
1540 static const char blt[] = "builtin_type";
1541 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1542 if (t == NULL)
1543 {
1544 char *n;
1545 if (!footnote_register_type_name_null)
1546 footnote_register_type_name_null = ++footnote_nr;
1547 xasprintf (&n, "*%d", footnote_register_type_name_null);
1548 make_cleanup (xfree, n);
1549 t = n;
1550 }
1551 /* Chop a leading builtin_type. */
1552 if (strncmp (t, blt, strlen (blt)) == 0)
1553 t += strlen (blt);
1554 }
1555 fprintf_unfiltered (file, " %-15s", t);
1556 }
1557
1558 /* Leading space always present. */
1559 fprintf_unfiltered (file, " ");
af030b9a
AC
1560
1561 /* Value, raw. */
1562 if (what_to_dump == regcache_dump_raw)
1563 {
1564 if (regnum < 0)
1565 fprintf_unfiltered (file, "Raw value");
1566 else if (regnum >= regcache->descr->nr_raw_registers)
1567 fprintf_unfiltered (file, "<cooked>");
1568 else if (!regcache_valid_p (regcache, regnum))
1569 fprintf_unfiltered (file, "<invalid>");
1570 else
1571 {
1572 regcache_raw_read (regcache, regnum, buf);
1573 fprintf_unfiltered (file, "0x");
1574 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1575 REGISTER_RAW_SIZE (regnum));
1576 }
1577 }
1578
1579 /* Value, cooked. */
1580 if (what_to_dump == regcache_dump_cooked)
1581 {
1582 if (regnum < 0)
1583 fprintf_unfiltered (file, "Cooked value");
1584 else
1585 {
1586 regcache_cooked_read (regcache, regnum, buf);
1587 fprintf_unfiltered (file, "0x");
1588 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1589 REGISTER_VIRTUAL_SIZE (regnum));
1590 }
1591 }
1592
b59ff9d5
AC
1593 /* Group members. */
1594 if (what_to_dump == regcache_dump_groups)
1595 {
1596 if (regnum < 0)
1597 fprintf_unfiltered (file, "Groups");
1598 else
1599 {
1600 int i;
1601 const char *sep = "";
1602 for (i = 0; groups[i] != NULL; i++)
1603 {
1604 if (gdbarch_register_reggroup_p (gdbarch, regnum, groups[i]))
1605 {
1606 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (groups[i]));
1607 sep = ",";
1608 }
1609 }
1610 }
1611 }
1612
af030b9a
AC
1613 fprintf_unfiltered (file, "\n");
1614 }
1615
1616 if (footnote_register_size)
1617 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1618 footnote_register_size);
1619 if (footnote_register_offset)
1620 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1621 footnote_register_offset);
1622 if (footnote_register_type_name_null)
1623 fprintf_unfiltered (file,
1624 "*%d: Register type's name NULL.\n",
1625 footnote_register_type_name_null);
1626 do_cleanups (cleanups);
1627}
1628
1629static void
1630regcache_print (char *args, enum regcache_dump_what what_to_dump)
1631{
1632 if (args == NULL)
1633 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1634 else
1635 {
1636 struct ui_file *file = gdb_fopen (args, "w");
1637 if (file == NULL)
1638 perror_with_name ("maintenance print architecture");
1639 regcache_dump (current_regcache, file, what_to_dump);
1640 ui_file_delete (file);
1641 }
1642}
1643
1644static void
1645maintenance_print_registers (char *args, int from_tty)
1646{
1647 regcache_print (args, regcache_dump_none);
1648}
1649
1650static void
1651maintenance_print_raw_registers (char *args, int from_tty)
1652{
1653 regcache_print (args, regcache_dump_raw);
1654}
1655
1656static void
1657maintenance_print_cooked_registers (char *args, int from_tty)
1658{
1659 regcache_print (args, regcache_dump_cooked);
1660}
1661
b59ff9d5
AC
1662static void
1663maintenance_print_register_groups (char *args, int from_tty)
1664{
1665 regcache_print (args, regcache_dump_groups);
1666}
1667
b9362cc7
AC
1668extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1669
32178cab
MS
1670void
1671_initialize_regcache (void)
1672{
3fadccb3
AC
1673 regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1674 xfree_regcache_descr);
1675 REGISTER_GDBARCH_SWAP (current_regcache);
524d7c18 1676 register_gdbarch_swap (&deprecated_registers, sizeof (deprecated_registers), NULL);
8262ee23 1677 register_gdbarch_swap (&deprecated_register_valid, sizeof (deprecated_register_valid), NULL);
32178cab 1678 register_gdbarch_swap (NULL, 0, build_regcache);
705152c5
MS
1679
1680 add_com ("flushregs", class_maintenance, reg_flush_command,
1681 "Force gdb to flush its register cache (maintainer command)");
39f77062
KB
1682
1683 /* Initialize the thread/process associated with the current set of
1684 registers. For now, -1 is special, and means `no current process'. */
1685 registers_ptid = pid_to_ptid (-1);
af030b9a
AC
1686
1687 add_cmd ("registers", class_maintenance,
1688 maintenance_print_registers,
1689 "Print the internal register configuration.\
1690Takes an optional file parameter.",
1691 &maintenanceprintlist);
1692 add_cmd ("raw-registers", class_maintenance,
1693 maintenance_print_raw_registers,
1694 "Print the internal register configuration including raw values.\
1695Takes an optional file parameter.",
1696 &maintenanceprintlist);
1697 add_cmd ("cooked-registers", class_maintenance,
1698 maintenance_print_cooked_registers,
1699 "Print the internal register configuration including cooked values.\
b59ff9d5
AC
1700Takes an optional file parameter.",
1701 &maintenanceprintlist);
1702 add_cmd ("register-groups", class_maintenance,
1703 maintenance_print_register_groups,
1704 "Print the internal register configuration including each register's group.\
af030b9a
AC
1705Takes an optional file parameter.",
1706 &maintenanceprintlist);
1707
32178cab 1708}
This page took 0.417641 seconds and 4 git commands to generate.