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