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