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