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