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