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