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