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