2003-03-04 Daniel Jacobowitz <drow@mvista.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, struct regcache *src)
393 {
394 struct gdbarch *gdbarch = dst->descr->gdbarch;
395 int regnum;
396 /* The SRC and DST register caches had better belong to the same
397 architecture. */
398 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
399 /* The DST should be `read-only', if it wasn't then the save would
400 end up trying to write the register values out through to the
401 target. */
402 gdb_assert (!src->readonly_p);
403 gdb_assert (dst->readonly_p);
404 /* Clear the dest. */
405 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
406 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
407 /* Copy over any registers (identified by their membership in the
408 save_reggroup) and mark them as valid. The full [0
409 .. NUM_REGS+NUM_PSEUDO_REGS) range is checked since some
410 architectures need to save/restore `cooked' registers that live
411 in memory. */
412 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
413 {
414 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
415 {
416 regcache_cooked_read (src, regnum, register_buffer (dst, regnum));
417 dst->register_valid_p[regnum] = 1;
418 }
419 }
420 }
421
422 void
423 regcache_restore (struct regcache *dst, struct regcache *src)
424 {
425 struct gdbarch *gdbarch = dst->descr->gdbarch;
426 int regnum;
427 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
428 gdb_assert (!dst->readonly_p);
429 gdb_assert (src->readonly_p);
430 /* Copy over any registers, being careful to only restore those that
431 were both saved and need to be restored. The full [0
432 .. NUM_REGS+NUM_PSEUDO_REGS) range is checked since some
433 architectures need to save/restore `cooked' registers that live
434 in memory. */
435 for (regnum = 0; regnum < src->descr->nr_cooked_registers; regnum++)
436 {
437 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup)
438 && src->register_valid_p[regnum])
439 {
440 regcache_cooked_write (dst, regnum, register_buffer (src, regnum));
441 }
442 }
443 }
444
445 void
446 regcache_cpy (struct regcache *dst, struct regcache *src)
447 {
448 int i;
449 char *buf;
450 gdb_assert (src != NULL && dst != NULL);
451 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
452 gdb_assert (src != dst);
453 gdb_assert (src->readonly_p || dst->readonly_p);
454 if (!src->readonly_p)
455 regcache_save (dst, src);
456 else if (!dst->readonly_p)
457 regcache_restore (dst, src);
458 else
459 regcache_cpy_no_passthrough (dst, src);
460 }
461
462 void
463 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
464 {
465 int i;
466 gdb_assert (src != NULL && dst != NULL);
467 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
468 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
469 move of data into the current_regcache(). Doing this would be
470 silly - it would mean that valid_p would be completly invalid. */
471 gdb_assert (dst != current_regcache);
472 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
473 memcpy (dst->register_valid_p, src->register_valid_p,
474 dst->descr->sizeof_raw_register_valid_p);
475 }
476
477 struct regcache *
478 regcache_dup (struct regcache *src)
479 {
480 struct regcache *newbuf;
481 gdb_assert (current_regcache != NULL);
482 newbuf = regcache_xmalloc (src->descr->gdbarch);
483 regcache_cpy (newbuf, src);
484 return newbuf;
485 }
486
487 struct regcache *
488 regcache_dup_no_passthrough (struct regcache *src)
489 {
490 struct regcache *newbuf;
491 gdb_assert (current_regcache != NULL);
492 newbuf = regcache_xmalloc (src->descr->gdbarch);
493 regcache_cpy_no_passthrough (newbuf, src);
494 return newbuf;
495 }
496
497 int
498 regcache_valid_p (struct regcache *regcache, int regnum)
499 {
500 gdb_assert (regcache != NULL);
501 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
502 return regcache->register_valid_p[regnum];
503 }
504
505 char *
506 deprecated_grub_regcache_for_registers (struct regcache *regcache)
507 {
508 return regcache->registers;
509 }
510
511 char *
512 deprecated_grub_regcache_for_register_valid (struct regcache *regcache)
513 {
514 return regcache->register_valid_p;
515 }
516
517 /* Global structure containing the current regcache. */
518 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
519 deprecated_register_valid[] currently point into this structure. */
520 struct regcache *current_regcache;
521
522 /* NOTE: this is a write-through cache. There is no "dirty" bit for
523 recording if the register values have been changed (eg. by the
524 user). Therefore all registers must be written back to the
525 target when appropriate. */
526
527 /* REGISTERS contains the cached register values (in target byte order). */
528
529 char *deprecated_registers;
530
531 /* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
532 1 if it has been fetched, and
533 -1 if the register value was not available.
534
535 "Not available" indicates that the target is not not able to supply
536 the register at this state. The register may become available at a
537 later time (after the next resume). This often occures when GDB is
538 manipulating a target that contains only a snapshot of the entire
539 system being debugged - some of the registers in such a system may
540 not have been saved. */
541
542 signed char *deprecated_register_valid;
543
544 /* The thread/process associated with the current set of registers. */
545
546 static ptid_t registers_ptid;
547
548 /*
549 * FUNCTIONS:
550 */
551
552 /* REGISTER_CACHED()
553
554 Returns 0 if the value is not in the cache (needs fetch).
555 >0 if the value is in the cache.
556 <0 if the value is permanently unavailable (don't ask again). */
557
558 int
559 register_cached (int regnum)
560 {
561 return deprecated_register_valid[regnum];
562 }
563
564 /* Record that REGNUM's value is cached if STATE is >0, uncached but
565 fetchable if STATE is 0, and uncached and unfetchable if STATE is <0. */
566
567 void
568 set_register_cached (int regnum, int state)
569 {
570 gdb_assert (regnum >= 0);
571 gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
572 current_regcache->register_valid_p[regnum] = state;
573 }
574
575 /* Return whether register REGNUM is a real register. */
576
577 static int
578 real_register (int regnum)
579 {
580 return regnum >= 0 && regnum < NUM_REGS;
581 }
582
583 /* Low level examining and depositing of registers.
584
585 The caller is responsible for making sure that the inferior is
586 stopped before calling the fetching routines, or it will get
587 garbage. (a change from GDB version 3, in which the caller got the
588 value from the last stop). */
589
590 /* REGISTERS_CHANGED ()
591
592 Indicate that registers may have changed, so invalidate the cache. */
593
594 void
595 registers_changed (void)
596 {
597 int i;
598
599 registers_ptid = pid_to_ptid (-1);
600
601 /* Force cleanup of any alloca areas if using C alloca instead of
602 a builtin alloca. This particular call is used to clean up
603 areas allocated by low level target code which may build up
604 during lengthy interactions between gdb and the target before
605 gdb gives control to the user (ie watchpoints). */
606 alloca (0);
607
608 for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
609 set_register_cached (i, 0);
610
611 if (registers_changed_hook)
612 registers_changed_hook ();
613 }
614
615 /* DEPRECATED_REGISTERS_FETCHED ()
616
617 Indicate that all registers have been fetched, so mark them all valid. */
618
619 /* NOTE: cagney/2001-12-04: This function does not set valid on the
620 pseudo-register range since pseudo registers are always supplied
621 using supply_register(). */
622 /* FIXME: cagney/2001-12-04: This function is DEPRECATED. The target
623 code was blatting the registers[] array and then calling this.
624 Since targets should only be using supply_register() the need for
625 this function/hack is eliminated. */
626
627 void
628 deprecated_registers_fetched (void)
629 {
630 int i;
631
632 for (i = 0; i < NUM_REGS; i++)
633 set_register_cached (i, 1);
634 /* Do not assume that the pseudo-regs have also been fetched.
635 Fetching all real regs NEVER accounts for pseudo-regs. */
636 }
637
638 /* deprecated_read_register_bytes and deprecated_write_register_bytes
639 are generally a *BAD* idea. They are inefficient because they need
640 to check for partial updates, which can only be done by scanning
641 through all of the registers and seeing if the bytes that are being
642 read/written fall inside of an invalid register. [The main reason
643 this is necessary is that register sizes can vary, so a simple
644 index won't suffice.] It is far better to call read_register_gen
645 and write_register_gen if you want to get at the raw register
646 contents, as it only takes a regnum as an argument, and therefore
647 can't do a partial register update.
648
649 Prior to the recent fixes to check for partial updates, both read
650 and deprecated_write_register_bytes always checked to see if any
651 registers were stale, and then called target_fetch_registers (-1)
652 to update the whole set. This caused really slowed things down for
653 remote targets. */
654
655 /* Copy INLEN bytes of consecutive data from registers
656 starting with the INREGBYTE'th byte of register data
657 into memory at MYADDR. */
658
659 void
660 deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
661 {
662 int in_end = in_start + in_len;
663 int regnum;
664 char *reg_buf = alloca (MAX_REGISTER_RAW_SIZE);
665
666 /* See if we are trying to read bytes from out-of-date registers. If so,
667 update just those registers. */
668
669 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
670 {
671 int reg_start;
672 int reg_end;
673 int reg_len;
674 int start;
675 int end;
676 int byte;
677
678 reg_start = REGISTER_BYTE (regnum);
679 reg_len = REGISTER_RAW_SIZE (regnum);
680 reg_end = reg_start + reg_len;
681
682 if (reg_end <= in_start || in_end <= reg_start)
683 /* The range the user wants to read doesn't overlap with regnum. */
684 continue;
685
686 if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
687 /* Force the cache to fetch the entire register. */
688 deprecated_read_register_gen (regnum, reg_buf);
689 else
690 /* Legacy note: even though this register is ``invalid'' we
691 still need to return something. It would appear that some
692 code relies on apparent gaps in the register array also
693 being returned. */
694 /* FIXME: cagney/2001-08-18: This is just silly. It defeats
695 the entire register read/write flow of control. Must
696 resist temptation to return 0xdeadbeef. */
697 memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
698
699 /* Legacy note: This function, for some reason, allows a NULL
700 input buffer. If the buffer is NULL, the registers are still
701 fetched, just the final transfer is skipped. */
702 if (in_buf == NULL)
703 continue;
704
705 /* start = max (reg_start, in_start) */
706 if (reg_start > in_start)
707 start = reg_start;
708 else
709 start = in_start;
710
711 /* end = min (reg_end, in_end) */
712 if (reg_end < in_end)
713 end = reg_end;
714 else
715 end = in_end;
716
717 /* Transfer just the bytes common to both IN_BUF and REG_BUF */
718 for (byte = start; byte < end; byte++)
719 {
720 in_buf[byte - in_start] = reg_buf[byte - reg_start];
721 }
722 }
723 }
724
725 /* Read register REGNUM into memory at MYADDR, which must be large
726 enough for REGISTER_RAW_BYTES (REGNUM). Target byte-order. If the
727 register is known to be the size of a CORE_ADDR or smaller,
728 read_register can be used instead. */
729
730 static void
731 legacy_read_register_gen (int regnum, char *myaddr)
732 {
733 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
734 if (! ptid_equal (registers_ptid, inferior_ptid))
735 {
736 registers_changed ();
737 registers_ptid = inferior_ptid;
738 }
739
740 if (!register_cached (regnum))
741 target_fetch_registers (regnum);
742
743 memcpy (myaddr, register_buffer (current_regcache, regnum),
744 REGISTER_RAW_SIZE (regnum));
745 }
746
747 void
748 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
749 {
750 gdb_assert (regcache != NULL && buf != NULL);
751 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
752 if (regcache->descr->legacy_p
753 && !regcache->readonly_p)
754 {
755 gdb_assert (regcache == current_regcache);
756 /* For moment, just use underlying legacy code. Ulgh!!! This
757 silently and very indirectly updates the regcache's regcache
758 via the global deprecated_register_valid[]. */
759 legacy_read_register_gen (regnum, buf);
760 return;
761 }
762 /* Make certain that the register cache is up-to-date with respect
763 to the current thread. This switching shouldn't be necessary
764 only there is still only one target side register cache. Sigh!
765 On the bright side, at least there is a regcache object. */
766 if (!regcache->readonly_p)
767 {
768 gdb_assert (regcache == current_regcache);
769 if (! ptid_equal (registers_ptid, inferior_ptid))
770 {
771 registers_changed ();
772 registers_ptid = inferior_ptid;
773 }
774 if (!register_cached (regnum))
775 target_fetch_registers (regnum);
776 }
777 /* Copy the value directly into the register cache. */
778 memcpy (buf, register_buffer (regcache, regnum),
779 regcache->descr->sizeof_register[regnum]);
780 }
781
782 void
783 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
784 {
785 char *buf;
786 gdb_assert (regcache != NULL);
787 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
788 buf = alloca (regcache->descr->sizeof_register[regnum]);
789 regcache_raw_read (regcache, regnum, buf);
790 (*val) = extract_signed_integer (buf,
791 regcache->descr->sizeof_register[regnum]);
792 }
793
794 void
795 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
796 ULONGEST *val)
797 {
798 char *buf;
799 gdb_assert (regcache != NULL);
800 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
801 buf = alloca (regcache->descr->sizeof_register[regnum]);
802 regcache_raw_read (regcache, regnum, buf);
803 (*val) = extract_unsigned_integer (buf,
804 regcache->descr->sizeof_register[regnum]);
805 }
806
807 void
808 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
809 {
810 void *buf;
811 gdb_assert (regcache != NULL);
812 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
813 buf = alloca (regcache->descr->sizeof_register[regnum]);
814 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
815 regcache_raw_write (regcache, regnum, buf);
816 }
817
818 void
819 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
820 ULONGEST val)
821 {
822 void *buf;
823 gdb_assert (regcache != NULL);
824 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
825 buf = alloca (regcache->descr->sizeof_register[regnum]);
826 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
827 regcache_raw_write (regcache, regnum, buf);
828 }
829
830 void
831 deprecated_read_register_gen (int regnum, char *buf)
832 {
833 gdb_assert (current_regcache != NULL);
834 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
835 if (current_regcache->descr->legacy_p)
836 {
837 legacy_read_register_gen (regnum, buf);
838 return;
839 }
840 regcache_cooked_read (current_regcache, regnum, buf);
841 }
842
843 void
844 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
845 {
846 gdb_assert (regnum >= 0);
847 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
848 if (regnum < regcache->descr->nr_raw_registers)
849 regcache_raw_read (regcache, regnum, buf);
850 else if (regcache->readonly_p
851 && regnum < regcache->descr->nr_cooked_registers
852 && regcache->register_valid_p[regnum])
853 /* Read-only register cache, perhaphs the cooked value was cached? */
854 memcpy (buf, register_buffer (regcache, regnum),
855 regcache->descr->sizeof_register[regnum]);
856 else
857 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
858 regnum, buf);
859 }
860
861 void
862 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
863 LONGEST *val)
864 {
865 char *buf;
866 gdb_assert (regcache != NULL);
867 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
868 buf = alloca (regcache->descr->sizeof_register[regnum]);
869 regcache_cooked_read (regcache, regnum, buf);
870 (*val) = extract_signed_integer (buf,
871 regcache->descr->sizeof_register[regnum]);
872 }
873
874 void
875 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
876 ULONGEST *val)
877 {
878 char *buf;
879 gdb_assert (regcache != NULL);
880 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
881 buf = alloca (regcache->descr->sizeof_register[regnum]);
882 regcache_cooked_read (regcache, regnum, buf);
883 (*val) = extract_unsigned_integer (buf,
884 regcache->descr->sizeof_register[regnum]);
885 }
886
887 /* Write register REGNUM at MYADDR to the target. MYADDR points at
888 REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order. */
889
890 static void
891 legacy_write_register_gen (int regnum, const void *myaddr)
892 {
893 int size;
894 gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
895
896 /* On the sparc, writing %g0 is a no-op, so we don't even want to
897 change the registers array if something writes to this register. */
898 if (CANNOT_STORE_REGISTER (regnum))
899 return;
900
901 if (! ptid_equal (registers_ptid, inferior_ptid))
902 {
903 registers_changed ();
904 registers_ptid = inferior_ptid;
905 }
906
907 size = REGISTER_RAW_SIZE (regnum);
908
909 if (real_register (regnum))
910 {
911 /* If we have a valid copy of the register, and new value == old
912 value, then don't bother doing the actual store. */
913 if (register_cached (regnum)
914 && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
915 == 0))
916 return;
917 else
918 target_prepare_to_store ();
919 }
920
921 memcpy (register_buffer (current_regcache, regnum), myaddr, size);
922
923 set_register_cached (regnum, 1);
924 target_store_registers (regnum);
925 }
926
927 void
928 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
929 {
930 gdb_assert (regcache != NULL && buf != NULL);
931 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
932 gdb_assert (!regcache->readonly_p);
933
934 if (regcache->descr->legacy_p)
935 {
936 /* For moment, just use underlying legacy code. Ulgh!!! This
937 silently and very indirectly updates the regcache's buffers
938 via the globals deprecated_register_valid[] and registers[]. */
939 gdb_assert (regcache == current_regcache);
940 legacy_write_register_gen (regnum, buf);
941 return;
942 }
943
944 /* On the sparc, writing %g0 is a no-op, so we don't even want to
945 change the registers array if something writes to this register. */
946 if (CANNOT_STORE_REGISTER (regnum))
947 return;
948
949 /* Make certain that the correct cache is selected. */
950 gdb_assert (regcache == current_regcache);
951 if (! ptid_equal (registers_ptid, inferior_ptid))
952 {
953 registers_changed ();
954 registers_ptid = inferior_ptid;
955 }
956
957 /* If we have a valid copy of the register, and new value == old
958 value, then don't bother doing the actual store. */
959 if (regcache_valid_p (regcache, regnum)
960 && (memcmp (register_buffer (regcache, regnum), buf,
961 regcache->descr->sizeof_register[regnum]) == 0))
962 return;
963
964 target_prepare_to_store ();
965 memcpy (register_buffer (regcache, regnum), buf,
966 regcache->descr->sizeof_register[regnum]);
967 regcache->register_valid_p[regnum] = 1;
968 target_store_registers (regnum);
969 }
970
971 void
972 deprecated_write_register_gen (int regnum, char *buf)
973 {
974 gdb_assert (current_regcache != NULL);
975 gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
976 if (current_regcache->descr->legacy_p)
977 {
978 legacy_write_register_gen (regnum, buf);
979 return;
980 }
981 regcache_cooked_write (current_regcache, regnum, buf);
982 }
983
984 void
985 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
986 {
987 gdb_assert (regnum >= 0);
988 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
989 if (regnum < regcache->descr->nr_raw_registers)
990 regcache_raw_write (regcache, regnum, buf);
991 else
992 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
993 regnum, buf);
994 }
995
996 /* Copy INLEN bytes of consecutive data from memory at MYADDR
997 into registers starting with the MYREGSTART'th byte of register data. */
998
999 void
1000 deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
1001 {
1002 int myregend = myregstart + inlen;
1003 int regnum;
1004
1005 target_prepare_to_store ();
1006
1007 /* Scan through the registers updating any that are covered by the
1008 range myregstart<=>myregend using write_register_gen, which does
1009 nice things like handling threads, and avoiding updates when the
1010 new and old contents are the same. */
1011
1012 for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
1013 {
1014 int regstart, regend;
1015
1016 regstart = REGISTER_BYTE (regnum);
1017 regend = regstart + REGISTER_RAW_SIZE (regnum);
1018
1019 /* Is this register completely outside the range the user is writing? */
1020 if (myregend <= regstart || regend <= myregstart)
1021 /* do nothing */ ;
1022
1023 /* Is this register completely within the range the user is writing? */
1024 else if (myregstart <= regstart && regend <= myregend)
1025 deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
1026
1027 /* The register partially overlaps the range being written. */
1028 else
1029 {
1030 char *regbuf = (char*) alloca (MAX_REGISTER_RAW_SIZE);
1031 /* What's the overlap between this register's bytes and
1032 those the caller wants to write? */
1033 int overlapstart = max (regstart, myregstart);
1034 int overlapend = min (regend, myregend);
1035
1036 /* We may be doing a partial update of an invalid register.
1037 Update it from the target before scribbling on it. */
1038 deprecated_read_register_gen (regnum, regbuf);
1039
1040 memcpy (&deprecated_registers[overlapstart],
1041 myaddr + (overlapstart - myregstart),
1042 overlapend - overlapstart);
1043
1044 target_store_registers (regnum);
1045 }
1046 }
1047 }
1048
1049 /* Perform a partial register transfer using a read, modify, write
1050 operation. */
1051
1052 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
1053 void *buf);
1054 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
1055 const void *buf);
1056
1057 void
1058 regcache_xfer_part (struct regcache *regcache, int regnum,
1059 int offset, int len, void *in, const void *out,
1060 regcache_read_ftype *read, regcache_write_ftype *write)
1061 {
1062 struct regcache_descr *descr = regcache->descr;
1063 bfd_byte *reg = alloca (descr->max_register_size);
1064 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
1065 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
1066 /* Something to do? */
1067 if (offset + len == 0)
1068 return;
1069 /* Read (when needed) ... */
1070 if (in != NULL
1071 || offset > 0
1072 || offset + len < descr->sizeof_register[regnum])
1073 {
1074 gdb_assert (read != NULL);
1075 read (regcache, regnum, reg);
1076 }
1077 /* ... modify ... */
1078 if (in != NULL)
1079 memcpy (in, reg + offset, len);
1080 if (out != NULL)
1081 memcpy (reg + offset, out, len);
1082 /* ... write (when needed). */
1083 if (out != NULL)
1084 {
1085 gdb_assert (write != NULL);
1086 write (regcache, regnum, reg);
1087 }
1088 }
1089
1090 void
1091 regcache_raw_read_part (struct regcache *regcache, int regnum,
1092 int offset, int len, void *buf)
1093 {
1094 struct regcache_descr *descr = regcache->descr;
1095 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1096 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1097 regcache_raw_read, regcache_raw_write);
1098 }
1099
1100 void
1101 regcache_raw_write_part (struct regcache *regcache, int regnum,
1102 int offset, int len, const void *buf)
1103 {
1104 struct regcache_descr *descr = regcache->descr;
1105 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1106 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1107 regcache_raw_read, regcache_raw_write);
1108 }
1109
1110 void
1111 regcache_cooked_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_cooked_registers);
1116 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1117 regcache_cooked_read, regcache_cooked_write);
1118 }
1119
1120 void
1121 regcache_cooked_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_cooked_registers);
1126 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1127 regcache_cooked_read, regcache_cooked_write);
1128 }
1129
1130 /* Hack to keep code that view the register buffer as raw bytes
1131 working. */
1132
1133 int
1134 register_offset_hack (struct gdbarch *gdbarch, int regnum)
1135 {
1136 struct regcache_descr *descr = regcache_descr (gdbarch);
1137 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1138 return descr->register_offset[regnum];
1139 }
1140
1141 /* Return the contents of register REGNUM as an unsigned integer. */
1142
1143 ULONGEST
1144 read_register (int regnum)
1145 {
1146 char *buf = alloca (REGISTER_RAW_SIZE (regnum));
1147 deprecated_read_register_gen (regnum, buf);
1148 return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
1149 }
1150
1151 ULONGEST
1152 read_register_pid (int regnum, ptid_t ptid)
1153 {
1154 ptid_t save_ptid;
1155 int save_pid;
1156 CORE_ADDR retval;
1157
1158 if (ptid_equal (ptid, inferior_ptid))
1159 return read_register (regnum);
1160
1161 save_ptid = inferior_ptid;
1162
1163 inferior_ptid = ptid;
1164
1165 retval = read_register (regnum);
1166
1167 inferior_ptid = save_ptid;
1168
1169 return retval;
1170 }
1171
1172 /* Return the contents of register REGNUM as a signed integer. */
1173
1174 LONGEST
1175 read_signed_register (int regnum)
1176 {
1177 void *buf = alloca (REGISTER_RAW_SIZE (regnum));
1178 deprecated_read_register_gen (regnum, buf);
1179 return (extract_signed_integer (buf, REGISTER_RAW_SIZE (regnum)));
1180 }
1181
1182 LONGEST
1183 read_signed_register_pid (int regnum, ptid_t ptid)
1184 {
1185 ptid_t save_ptid;
1186 LONGEST retval;
1187
1188 if (ptid_equal (ptid, inferior_ptid))
1189 return read_signed_register (regnum);
1190
1191 save_ptid = inferior_ptid;
1192
1193 inferior_ptid = ptid;
1194
1195 retval = read_signed_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, write_sp, 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(), write_sp(), generic_target_read_fp() and
1294 read_fp(), will eventually be moved out of the reg-cache into
1295 either frame.[hc] or to the multi-arch framework. The are not part
1296 of the raw register 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 void
1416 write_sp (CORE_ADDR val)
1417 {
1418 TARGET_WRITE_SP (val);
1419 }
1420
1421 CORE_ADDR
1422 generic_target_read_fp (void)
1423 {
1424 #ifdef FP_REGNUM
1425 if (FP_REGNUM >= 0)
1426 return read_register (FP_REGNUM);
1427 #endif
1428 internal_error (__FILE__, __LINE__,
1429 "generic_target_read_fp");
1430 }
1431
1432 CORE_ADDR
1433 read_fp (void)
1434 {
1435 return TARGET_READ_FP ();
1436 }
1437
1438 /* ARGSUSED */
1439 static void
1440 reg_flush_command (char *command, int from_tty)
1441 {
1442 /* Force-flush the register cache. */
1443 registers_changed ();
1444 if (from_tty)
1445 printf_filtered ("Register cache flushed.\n");
1446 }
1447
1448 static void
1449 build_regcache (void)
1450 {
1451 current_regcache = regcache_xmalloc (current_gdbarch);
1452 current_regcache->readonly_p = 0;
1453 deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
1454 deprecated_register_valid = deprecated_grub_regcache_for_register_valid (current_regcache);
1455 }
1456
1457 static void
1458 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1459 const unsigned char *buf, long len)
1460 {
1461 int i;
1462 switch (endian)
1463 {
1464 case BFD_ENDIAN_BIG:
1465 for (i = 0; i < len; i++)
1466 fprintf_unfiltered (file, "%02x", buf[i]);
1467 break;
1468 case BFD_ENDIAN_LITTLE:
1469 for (i = len - 1; i >= 0; i--)
1470 fprintf_unfiltered (file, "%02x", buf[i]);
1471 break;
1472 default:
1473 internal_error (__FILE__, __LINE__, "Bad switch");
1474 }
1475 }
1476
1477 enum regcache_dump_what
1478 {
1479 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1480 };
1481
1482 static void
1483 regcache_dump (struct regcache *regcache, struct ui_file *file,
1484 enum regcache_dump_what what_to_dump)
1485 {
1486 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1487 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1488 struct reggroup *const *groups = reggroups (gdbarch);
1489 int regnum;
1490 int footnote_nr = 0;
1491 int footnote_register_size = 0;
1492 int footnote_register_offset = 0;
1493 int footnote_register_type_name_null = 0;
1494 long register_offset = 0;
1495 unsigned char *buf = alloca (regcache->descr->max_register_size);
1496
1497 #if 0
1498 fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1499 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1500 regcache->descr->nr_raw_registers);
1501 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1502 regcache->descr->nr_cooked_registers);
1503 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1504 regcache->descr->sizeof_raw_registers);
1505 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1506 regcache->descr->sizeof_raw_register_valid_p);
1507 fprintf_unfiltered (file, "max_register_size %ld\n",
1508 regcache->descr->max_register_size);
1509 fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1510 fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1511 #endif
1512
1513 gdb_assert (regcache->descr->nr_cooked_registers
1514 == (NUM_REGS + NUM_PSEUDO_REGS));
1515
1516 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1517 {
1518 /* Name. */
1519 if (regnum < 0)
1520 fprintf_unfiltered (file, " %-10s", "Name");
1521 else
1522 {
1523 const char *p = REGISTER_NAME (regnum);
1524 if (p == NULL)
1525 p = "";
1526 else if (p[0] == '\0')
1527 p = "''";
1528 fprintf_unfiltered (file, " %-10s", p);
1529 }
1530
1531 /* Number. */
1532 if (regnum < 0)
1533 fprintf_unfiltered (file, " %4s", "Nr");
1534 else
1535 fprintf_unfiltered (file, " %4d", regnum);
1536
1537 /* Relative number. */
1538 if (regnum < 0)
1539 fprintf_unfiltered (file, " %4s", "Rel");
1540 else if (regnum < NUM_REGS)
1541 fprintf_unfiltered (file, " %4d", regnum);
1542 else
1543 fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1544
1545 /* Offset. */
1546 if (regnum < 0)
1547 fprintf_unfiltered (file, " %6s ", "Offset");
1548 else
1549 {
1550 fprintf_unfiltered (file, " %6ld",
1551 regcache->descr->register_offset[regnum]);
1552 if (register_offset != regcache->descr->register_offset[regnum]
1553 || register_offset != REGISTER_BYTE (regnum)
1554 || (regnum > 0
1555 && (regcache->descr->register_offset[regnum]
1556 != (regcache->descr->register_offset[regnum - 1]
1557 + regcache->descr->sizeof_register[regnum - 1])))
1558 )
1559 {
1560 if (!footnote_register_offset)
1561 footnote_register_offset = ++footnote_nr;
1562 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1563 }
1564 else
1565 fprintf_unfiltered (file, " ");
1566 register_offset = (regcache->descr->register_offset[regnum]
1567 + regcache->descr->sizeof_register[regnum]);
1568 }
1569
1570 /* Size. */
1571 if (regnum < 0)
1572 fprintf_unfiltered (file, " %5s ", "Size");
1573 else
1574 {
1575 fprintf_unfiltered (file, " %5ld",
1576 regcache->descr->sizeof_register[regnum]);
1577 if ((regcache->descr->sizeof_register[regnum]
1578 != REGISTER_RAW_SIZE (regnum))
1579 || (regcache->descr->sizeof_register[regnum]
1580 != REGISTER_VIRTUAL_SIZE (regnum))
1581 || (regcache->descr->sizeof_register[regnum]
1582 != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1583 regnum)))
1584 )
1585 {
1586 if (!footnote_register_size)
1587 footnote_register_size = ++footnote_nr;
1588 fprintf_unfiltered (file, "*%d", footnote_register_size);
1589 }
1590 else
1591 fprintf_unfiltered (file, " ");
1592 }
1593
1594 /* Type. */
1595 {
1596 const char *t;
1597 if (regnum < 0)
1598 t = "Type";
1599 else
1600 {
1601 static const char blt[] = "builtin_type";
1602 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1603 if (t == NULL)
1604 {
1605 char *n;
1606 if (!footnote_register_type_name_null)
1607 footnote_register_type_name_null = ++footnote_nr;
1608 xasprintf (&n, "*%d", footnote_register_type_name_null);
1609 make_cleanup (xfree, n);
1610 t = n;
1611 }
1612 /* Chop a leading builtin_type. */
1613 if (strncmp (t, blt, strlen (blt)) == 0)
1614 t += strlen (blt);
1615 }
1616 fprintf_unfiltered (file, " %-15s", t);
1617 }
1618
1619 /* Leading space always present. */
1620 fprintf_unfiltered (file, " ");
1621
1622 /* Value, raw. */
1623 if (what_to_dump == regcache_dump_raw)
1624 {
1625 if (regnum < 0)
1626 fprintf_unfiltered (file, "Raw value");
1627 else if (regnum >= regcache->descr->nr_raw_registers)
1628 fprintf_unfiltered (file, "<cooked>");
1629 else if (!regcache_valid_p (regcache, regnum))
1630 fprintf_unfiltered (file, "<invalid>");
1631 else
1632 {
1633 regcache_raw_read (regcache, regnum, buf);
1634 fprintf_unfiltered (file, "0x");
1635 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1636 REGISTER_RAW_SIZE (regnum));
1637 }
1638 }
1639
1640 /* Value, cooked. */
1641 if (what_to_dump == regcache_dump_cooked)
1642 {
1643 if (regnum < 0)
1644 fprintf_unfiltered (file, "Cooked value");
1645 else
1646 {
1647 regcache_cooked_read (regcache, regnum, buf);
1648 fprintf_unfiltered (file, "0x");
1649 dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1650 REGISTER_VIRTUAL_SIZE (regnum));
1651 }
1652 }
1653
1654 /* Group members. */
1655 if (what_to_dump == regcache_dump_groups)
1656 {
1657 if (regnum < 0)
1658 fprintf_unfiltered (file, "Groups");
1659 else
1660 {
1661 int i;
1662 const char *sep = "";
1663 for (i = 0; groups[i] != NULL; i++)
1664 {
1665 if (gdbarch_register_reggroup_p (gdbarch, regnum, groups[i]))
1666 {
1667 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (groups[i]));
1668 sep = ",";
1669 }
1670 }
1671 }
1672 }
1673
1674 fprintf_unfiltered (file, "\n");
1675 }
1676
1677 if (footnote_register_size)
1678 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1679 footnote_register_size);
1680 if (footnote_register_offset)
1681 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1682 footnote_register_offset);
1683 if (footnote_register_type_name_null)
1684 fprintf_unfiltered (file,
1685 "*%d: Register type's name NULL.\n",
1686 footnote_register_type_name_null);
1687 do_cleanups (cleanups);
1688 }
1689
1690 static void
1691 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1692 {
1693 if (args == NULL)
1694 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1695 else
1696 {
1697 struct ui_file *file = gdb_fopen (args, "w");
1698 if (file == NULL)
1699 perror_with_name ("maintenance print architecture");
1700 regcache_dump (current_regcache, file, what_to_dump);
1701 ui_file_delete (file);
1702 }
1703 }
1704
1705 static void
1706 maintenance_print_registers (char *args, int from_tty)
1707 {
1708 regcache_print (args, regcache_dump_none);
1709 }
1710
1711 static void
1712 maintenance_print_raw_registers (char *args, int from_tty)
1713 {
1714 regcache_print (args, regcache_dump_raw);
1715 }
1716
1717 static void
1718 maintenance_print_cooked_registers (char *args, int from_tty)
1719 {
1720 regcache_print (args, regcache_dump_cooked);
1721 }
1722
1723 static void
1724 maintenance_print_register_groups (char *args, int from_tty)
1725 {
1726 regcache_print (args, regcache_dump_groups);
1727 }
1728
1729 void
1730 _initialize_regcache (void)
1731 {
1732 regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1733 xfree_regcache_descr);
1734 REGISTER_GDBARCH_SWAP (current_regcache);
1735 register_gdbarch_swap (&deprecated_registers, sizeof (deprecated_registers), NULL);
1736 register_gdbarch_swap (&deprecated_register_valid, sizeof (deprecated_register_valid), NULL);
1737 register_gdbarch_swap (NULL, 0, build_regcache);
1738
1739 add_com ("flushregs", class_maintenance, reg_flush_command,
1740 "Force gdb to flush its register cache (maintainer command)");
1741
1742 /* Initialize the thread/process associated with the current set of
1743 registers. For now, -1 is special, and means `no current process'. */
1744 registers_ptid = pid_to_ptid (-1);
1745
1746 add_cmd ("registers", class_maintenance,
1747 maintenance_print_registers,
1748 "Print the internal register configuration.\
1749 Takes an optional file parameter.",
1750 &maintenanceprintlist);
1751 add_cmd ("raw-registers", class_maintenance,
1752 maintenance_print_raw_registers,
1753 "Print the internal register configuration including raw values.\
1754 Takes an optional file parameter.",
1755 &maintenanceprintlist);
1756 add_cmd ("cooked-registers", class_maintenance,
1757 maintenance_print_cooked_registers,
1758 "Print the internal register configuration including cooked values.\
1759 Takes an optional file parameter.",
1760 &maintenanceprintlist);
1761 add_cmd ("register-groups", class_maintenance,
1762 maintenance_print_register_groups,
1763 "Print the internal register configuration including each register's group.\
1764 Takes an optional file parameter.",
1765 &maintenanceprintlist);
1766
1767 }