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