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