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