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