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