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