6c0fca82acaf0a8c8b2a626970babbc4413862ff
[deliverable/binutils-gdb.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3 Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4 2002, 2004, 2007 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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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 #include "observer.h"
34
35 /*
36 * DATA STRUCTURE
37 *
38 * Here is the actual register cache.
39 */
40
41 /* Per-architecture object describing the layout of a register cache.
42 Computed once when the architecture is created */
43
44 struct gdbarch_data *regcache_descr_handle;
45
46 struct regcache_descr
47 {
48 /* The architecture this descriptor belongs to. */
49 struct gdbarch *gdbarch;
50
51 /* The raw register cache. Each raw (or hard) register is supplied
52 by the target interface. The raw cache should not contain
53 redundant information - if the PC is constructed from two
54 registers then those registers and not the PC lives in the raw
55 cache. */
56 int nr_raw_registers;
57 long sizeof_raw_registers;
58 long sizeof_raw_register_valid_p;
59
60 /* The cooked register space. Each cooked register in the range
61 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62 register. The remaining [NR_RAW_REGISTERS
63 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
64 both raw registers and memory by the architecture methods
65 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
66 int nr_cooked_registers;
67 long sizeof_cooked_registers;
68 long sizeof_cooked_register_valid_p;
69
70 /* Offset and size (in 8 bit bytes), of reach register in the
71 register cache. All registers (including those in the range
72 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
73 Assigning all registers an offset makes it possible to keep
74 legacy code, such as that found in read_register_bytes() and
75 write_register_bytes() working. */
76 long *register_offset;
77 long *sizeof_register;
78
79 /* Cached table containing the type of each register. */
80 struct type **register_type;
81 };
82
83 static void *
84 init_regcache_descr (struct gdbarch *gdbarch)
85 {
86 int i;
87 struct regcache_descr *descr;
88 gdb_assert (gdbarch != NULL);
89
90 /* Create an initial, zero filled, table. */
91 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
92 descr->gdbarch = gdbarch;
93
94 /* Total size of the register space. The raw registers are mapped
95 directly onto the raw register cache while the pseudo's are
96 either mapped onto raw-registers or memory. */
97 descr->nr_cooked_registers = gdbarch_num_regs (current_gdbarch)
98 + gdbarch_num_pseudo_regs (current_gdbarch);
99 descr->sizeof_cooked_register_valid_p = gdbarch_num_regs (current_gdbarch)
100 + gdbarch_num_pseudo_regs
101 (current_gdbarch);
102
103 /* Fill in a table of register types. */
104 descr->register_type
105 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
106 for (i = 0; i < descr->nr_cooked_registers; i++)
107 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
108
109 /* Construct a strictly RAW register cache. Don't allow pseudo's
110 into the register cache. */
111 descr->nr_raw_registers = gdbarch_num_regs (current_gdbarch);
112
113 /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
114 array. This pretects GDB from erant code that accesses elements
115 of the global register_valid_p[] array in the range
116 [gdbarch_num_regs .. gdbarch_num_regs + gdbarch_num_pseudo_regs). */
117 descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
118
119 /* Lay out the register cache.
120
121 NOTE: cagney/2002-05-22: Only register_type() is used when
122 constructing the register cache. It is assumed that the
123 register's raw size, virtual size and type length are all the
124 same. */
125
126 {
127 long offset = 0;
128 descr->sizeof_register
129 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
130 descr->register_offset
131 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
132 for (i = 0; i < descr->nr_cooked_registers; i++)
133 {
134 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
135 descr->register_offset[i] = offset;
136 offset += descr->sizeof_register[i];
137 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
138 }
139 /* Set the real size of the register cache buffer. */
140 descr->sizeof_cooked_registers = offset;
141 }
142
143 /* FIXME: cagney/2002-05-22: Should only need to allocate space for
144 the raw registers. Unfortunately some code still accesses the
145 register array directly using the global registers[]. Until that
146 code has been purged, play safe and over allocating the register
147 buffer. Ulgh! */
148 descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
149
150 return descr;
151 }
152
153 static struct regcache_descr *
154 regcache_descr (struct gdbarch *gdbarch)
155 {
156 return gdbarch_data (gdbarch, regcache_descr_handle);
157 }
158
159 /* Utility functions returning useful register attributes stored in
160 the regcache descr. */
161
162 struct type *
163 register_type (struct gdbarch *gdbarch, int regnum)
164 {
165 struct regcache_descr *descr = regcache_descr (gdbarch);
166 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
167 return descr->register_type[regnum];
168 }
169
170 /* Utility functions returning useful register attributes stored in
171 the regcache descr. */
172
173 int
174 register_size (struct gdbarch *gdbarch, int regnum)
175 {
176 struct regcache_descr *descr = regcache_descr (gdbarch);
177 int size;
178 gdb_assert (regnum >= 0
179 && regnum < (gdbarch_num_regs (current_gdbarch)
180 + gdbarch_num_pseudo_regs (current_gdbarch)));
181 size = descr->sizeof_register[regnum];
182 return size;
183 }
184
185 /* The register cache for storing raw register values. */
186
187 struct regcache
188 {
189 struct regcache_descr *descr;
190 /* The register buffers. A read-only register cache can hold the
191 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
192 register cache can only hold [0 .. gdbarch_num_regs). */
193 gdb_byte *registers;
194 /* Register cache status:
195 register_valid_p[REG] == 0 if REG value is not in the cache
196 > 0 if REG value is in the cache
197 < 0 if REG value is permanently unavailable */
198 signed char *register_valid_p;
199 /* Is this a read-only cache? A read-only cache is used for saving
200 the target's register state (e.g, across an inferior function
201 call or just before forcing a function return). A read-only
202 cache can only be updated via the methods regcache_dup() and
203 regcache_cpy(). The actual contents are determined by the
204 reggroup_save and reggroup_restore methods. */
205 int readonly_p;
206 /* If this is a read-write cache, which thread's registers is
207 it connected to? */
208 ptid_t ptid;
209 };
210
211 struct regcache *
212 regcache_xmalloc (struct gdbarch *gdbarch)
213 {
214 struct regcache_descr *descr;
215 struct regcache *regcache;
216 gdb_assert (gdbarch != NULL);
217 descr = regcache_descr (gdbarch);
218 regcache = XMALLOC (struct regcache);
219 regcache->descr = descr;
220 regcache->registers
221 = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
222 regcache->register_valid_p
223 = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
224 regcache->readonly_p = 1;
225 regcache->ptid = minus_one_ptid;
226 return regcache;
227 }
228
229 void
230 regcache_xfree (struct regcache *regcache)
231 {
232 if (regcache == NULL)
233 return;
234 xfree (regcache->registers);
235 xfree (regcache->register_valid_p);
236 xfree (regcache);
237 }
238
239 static void
240 do_regcache_xfree (void *data)
241 {
242 regcache_xfree (data);
243 }
244
245 struct cleanup *
246 make_cleanup_regcache_xfree (struct regcache *regcache)
247 {
248 return make_cleanup (do_regcache_xfree, regcache);
249 }
250
251 /* Return REGCACHE's architecture. */
252
253 struct gdbarch *
254 get_regcache_arch (const struct regcache *regcache)
255 {
256 return regcache->descr->gdbarch;
257 }
258
259 /* Return a pointer to register REGNUM's buffer cache. */
260
261 static gdb_byte *
262 register_buffer (const struct regcache *regcache, int regnum)
263 {
264 return regcache->registers + regcache->descr->register_offset[regnum];
265 }
266
267 void
268 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
269 void *src)
270 {
271 struct gdbarch *gdbarch = dst->descr->gdbarch;
272 gdb_byte buf[MAX_REGISTER_SIZE];
273 int regnum;
274 /* The DST should be `read-only', if it wasn't then the save would
275 end up trying to write the register values back out to the
276 target. */
277 gdb_assert (dst->readonly_p);
278 /* Clear the dest. */
279 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
280 memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
281 /* Copy over any registers (identified by their membership in the
282 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
283 gdbarch_num_pseudo_regs) range is checked since some architectures need
284 to save/restore `cooked' registers that live in memory. */
285 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
286 {
287 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
288 {
289 int valid = cooked_read (src, regnum, buf);
290 if (valid)
291 {
292 memcpy (register_buffer (dst, regnum), buf,
293 register_size (gdbarch, regnum));
294 dst->register_valid_p[regnum] = 1;
295 }
296 }
297 }
298 }
299
300 void
301 regcache_restore (struct regcache *dst,
302 regcache_cooked_read_ftype *cooked_read,
303 void *cooked_read_context)
304 {
305 struct gdbarch *gdbarch = dst->descr->gdbarch;
306 gdb_byte buf[MAX_REGISTER_SIZE];
307 int regnum;
308 /* The dst had better not be read-only. If it is, the `restore'
309 doesn't make much sense. */
310 gdb_assert (!dst->readonly_p);
311 /* Copy over any registers, being careful to only restore those that
312 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
313 + gdbarch_num_pseudo_regs) range is checked since some architectures need
314 to save/restore `cooked' registers that live in memory. */
315 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
316 {
317 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
318 {
319 int valid = cooked_read (cooked_read_context, regnum, buf);
320 if (valid)
321 regcache_cooked_write (dst, regnum, buf);
322 }
323 }
324 }
325
326 static int
327 do_cooked_read (void *src, int regnum, gdb_byte *buf)
328 {
329 struct regcache *regcache = src;
330 if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
331 /* Don't even think about fetching a register from a read-only
332 cache when the register isn't yet valid. There isn't a target
333 from which the register value can be fetched. */
334 return 0;
335 regcache_cooked_read (regcache, regnum, buf);
336 return 1;
337 }
338
339
340 void
341 regcache_cpy (struct regcache *dst, struct regcache *src)
342 {
343 int i;
344 gdb_byte *buf;
345 gdb_assert (src != NULL && dst != NULL);
346 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
347 gdb_assert (src != dst);
348 gdb_assert (src->readonly_p || dst->readonly_p);
349 if (!src->readonly_p)
350 regcache_save (dst, do_cooked_read, src);
351 else if (!dst->readonly_p)
352 regcache_restore (dst, do_cooked_read, src);
353 else
354 regcache_cpy_no_passthrough (dst, src);
355 }
356
357 void
358 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
359 {
360 int i;
361 gdb_assert (src != NULL && dst != NULL);
362 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
363 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
364 move of data into the current regcache. Doing this would be
365 silly - it would mean that valid_p would be completely invalid. */
366 gdb_assert (dst->readonly_p);
367 memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
368 memcpy (dst->register_valid_p, src->register_valid_p,
369 dst->descr->sizeof_raw_register_valid_p);
370 }
371
372 struct regcache *
373 regcache_dup (struct regcache *src)
374 {
375 struct regcache *newbuf;
376 newbuf = regcache_xmalloc (src->descr->gdbarch);
377 regcache_cpy (newbuf, src);
378 return newbuf;
379 }
380
381 struct regcache *
382 regcache_dup_no_passthrough (struct regcache *src)
383 {
384 struct regcache *newbuf;
385 newbuf = regcache_xmalloc (src->descr->gdbarch);
386 regcache_cpy_no_passthrough (newbuf, src);
387 return newbuf;
388 }
389
390 int
391 regcache_valid_p (const struct regcache *regcache, int regnum)
392 {
393 gdb_assert (regcache != NULL);
394 gdb_assert (regnum >= 0);
395 if (regcache->readonly_p)
396 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
397 else
398 gdb_assert (regnum < regcache->descr->nr_raw_registers);
399
400 return regcache->register_valid_p[regnum];
401 }
402
403 void
404 regcache_invalidate (struct regcache *regcache, int regnum)
405 {
406 gdb_assert (regcache != NULL);
407 gdb_assert (regnum >= 0);
408 gdb_assert (!regcache->readonly_p);
409 gdb_assert (regnum < regcache->descr->nr_raw_registers);
410 regcache->register_valid_p[regnum] = 0;
411 }
412
413
414 /* Global structure containing the current regcache. */
415 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
416 deprecated_register_valid[] currently point into this structure. */
417 static struct regcache *current_regcache;
418
419 /* NOTE: this is a write-through cache. There is no "dirty" bit for
420 recording if the register values have been changed (eg. by the
421 user). Therefore all registers must be written back to the
422 target when appropriate. */
423
424 struct regcache *get_thread_regcache (ptid_t ptid)
425 {
426 /* NOTE: uweigand/2007-05-05: We need to detect the thread's
427 current architecture at this point. */
428 struct gdbarch *thread_gdbarch = current_gdbarch;
429
430 if (current_regcache && ptid_equal (current_regcache->ptid, ptid)
431 && get_regcache_arch (current_regcache) == thread_gdbarch)
432 return current_regcache;
433
434 if (current_regcache)
435 regcache_xfree (current_regcache);
436
437 current_regcache = regcache_xmalloc (thread_gdbarch);
438 current_regcache->readonly_p = 0;
439 current_regcache->ptid = ptid;
440
441 return current_regcache;
442 }
443
444 struct regcache *get_current_regcache (void)
445 {
446 return get_thread_regcache (inferior_ptid);
447 }
448
449
450 /* Observer for the target_changed event. */
451
452 void
453 regcache_observer_target_changed (struct target_ops *target)
454 {
455 registers_changed ();
456 }
457
458 /* Low level examining and depositing of registers.
459
460 The caller is responsible for making sure that the inferior is
461 stopped before calling the fetching routines, or it will get
462 garbage. (a change from GDB version 3, in which the caller got the
463 value from the last stop). */
464
465 /* REGISTERS_CHANGED ()
466
467 Indicate that registers may have changed, so invalidate the cache. */
468
469 void
470 registers_changed (void)
471 {
472 int i;
473
474 regcache_xfree (current_regcache);
475 current_regcache = NULL;
476
477 /* Force cleanup of any alloca areas if using C alloca instead of
478 a builtin alloca. This particular call is used to clean up
479 areas allocated by low level target code which may build up
480 during lengthy interactions between gdb and the target before
481 gdb gives control to the user (ie watchpoints). */
482 alloca (0);
483 }
484
485
486 void
487 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
488 {
489 gdb_assert (regcache != NULL && buf != NULL);
490 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
491 /* Make certain that the register cache is up-to-date with respect
492 to the current thread. This switching shouldn't be necessary
493 only there is still only one target side register cache. Sigh!
494 On the bright side, at least there is a regcache object. */
495 if (!regcache->readonly_p)
496 {
497 if (!regcache_valid_p (regcache, regnum))
498 {
499 struct cleanup *old_chain = save_inferior_ptid ();
500 inferior_ptid = regcache->ptid;
501 target_fetch_registers (regcache, regnum);
502 do_cleanups (old_chain);
503 }
504 #if 0
505 /* FIXME: cagney/2004-08-07: At present a number of targets
506 forget (or didn't know that they needed) to set this leading to
507 panics. Also is the problem that targets need to indicate
508 that a register is in one of the possible states: valid,
509 undefined, unknown. The last of which isn't yet
510 possible. */
511 gdb_assert (regcache_valid_p (regcache, regnum));
512 #endif
513 }
514 /* Copy the value directly into the register cache. */
515 memcpy (buf, register_buffer (regcache, regnum),
516 regcache->descr->sizeof_register[regnum]);
517 }
518
519 void
520 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
521 {
522 gdb_byte *buf;
523 gdb_assert (regcache != NULL);
524 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
525 buf = alloca (regcache->descr->sizeof_register[regnum]);
526 regcache_raw_read (regcache, regnum, buf);
527 (*val) = extract_signed_integer (buf,
528 regcache->descr->sizeof_register[regnum]);
529 }
530
531 void
532 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
533 ULONGEST *val)
534 {
535 gdb_byte *buf;
536 gdb_assert (regcache != NULL);
537 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
538 buf = alloca (regcache->descr->sizeof_register[regnum]);
539 regcache_raw_read (regcache, regnum, buf);
540 (*val) = extract_unsigned_integer (buf,
541 regcache->descr->sizeof_register[regnum]);
542 }
543
544 void
545 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
546 {
547 void *buf;
548 gdb_assert (regcache != NULL);
549 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
550 buf = alloca (regcache->descr->sizeof_register[regnum]);
551 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
552 regcache_raw_write (regcache, regnum, buf);
553 }
554
555 void
556 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
557 ULONGEST val)
558 {
559 void *buf;
560 gdb_assert (regcache != NULL);
561 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
562 buf = alloca (regcache->descr->sizeof_register[regnum]);
563 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
564 regcache_raw_write (regcache, regnum, buf);
565 }
566
567 void
568 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
569 {
570 gdb_assert (regnum >= 0);
571 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
572 if (regnum < regcache->descr->nr_raw_registers)
573 regcache_raw_read (regcache, regnum, buf);
574 else if (regcache->readonly_p
575 && regnum < regcache->descr->nr_cooked_registers
576 && regcache->register_valid_p[regnum])
577 /* Read-only register cache, perhaps the cooked value was cached? */
578 memcpy (buf, register_buffer (regcache, regnum),
579 regcache->descr->sizeof_register[regnum]);
580 else
581 gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
582 regnum, buf);
583 }
584
585 void
586 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
587 LONGEST *val)
588 {
589 gdb_byte *buf;
590 gdb_assert (regcache != NULL);
591 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
592 buf = alloca (regcache->descr->sizeof_register[regnum]);
593 regcache_cooked_read (regcache, regnum, buf);
594 (*val) = extract_signed_integer (buf,
595 regcache->descr->sizeof_register[regnum]);
596 }
597
598 void
599 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
600 ULONGEST *val)
601 {
602 gdb_byte *buf;
603 gdb_assert (regcache != NULL);
604 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
605 buf = alloca (regcache->descr->sizeof_register[regnum]);
606 regcache_cooked_read (regcache, regnum, buf);
607 (*val) = extract_unsigned_integer (buf,
608 regcache->descr->sizeof_register[regnum]);
609 }
610
611 void
612 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
613 LONGEST val)
614 {
615 void *buf;
616 gdb_assert (regcache != NULL);
617 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
618 buf = alloca (regcache->descr->sizeof_register[regnum]);
619 store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
620 regcache_cooked_write (regcache, regnum, buf);
621 }
622
623 void
624 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
625 ULONGEST val)
626 {
627 void *buf;
628 gdb_assert (regcache != NULL);
629 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
630 buf = alloca (regcache->descr->sizeof_register[regnum]);
631 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
632 regcache_cooked_write (regcache, regnum, buf);
633 }
634
635 void
636 regcache_raw_write (struct regcache *regcache, int regnum,
637 const gdb_byte *buf)
638 {
639 struct cleanup *old_chain;
640
641 gdb_assert (regcache != NULL && buf != NULL);
642 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
643 gdb_assert (!regcache->readonly_p);
644
645 /* On the sparc, writing %g0 is a no-op, so we don't even want to
646 change the registers array if something writes to this register. */
647 if (gdbarch_cannot_store_register (current_gdbarch, regnum))
648 return;
649
650 /* If we have a valid copy of the register, and new value == old
651 value, then don't bother doing the actual store. */
652 if (regcache_valid_p (regcache, regnum)
653 && (memcmp (register_buffer (regcache, regnum), buf,
654 regcache->descr->sizeof_register[regnum]) == 0))
655 return;
656
657 old_chain = save_inferior_ptid ();
658 inferior_ptid = regcache->ptid;
659
660 target_prepare_to_store (regcache);
661 memcpy (register_buffer (regcache, regnum), buf,
662 regcache->descr->sizeof_register[regnum]);
663 regcache->register_valid_p[regnum] = 1;
664 target_store_registers (regcache, regnum);
665
666 do_cleanups (old_chain);
667 }
668
669 void
670 regcache_cooked_write (struct regcache *regcache, int regnum,
671 const gdb_byte *buf)
672 {
673 gdb_assert (regnum >= 0);
674 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
675 if (regnum < regcache->descr->nr_raw_registers)
676 regcache_raw_write (regcache, regnum, buf);
677 else
678 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
679 regnum, buf);
680 }
681
682 /* Perform a partial register transfer using a read, modify, write
683 operation. */
684
685 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
686 void *buf);
687 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
688 const void *buf);
689
690 static void
691 regcache_xfer_part (struct regcache *regcache, int regnum,
692 int offset, int len, void *in, const void *out,
693 void (*read) (struct regcache *regcache, int regnum,
694 gdb_byte *buf),
695 void (*write) (struct regcache *regcache, int regnum,
696 const gdb_byte *buf))
697 {
698 struct regcache_descr *descr = regcache->descr;
699 gdb_byte reg[MAX_REGISTER_SIZE];
700 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
701 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
702 /* Something to do? */
703 if (offset + len == 0)
704 return;
705 /* Read (when needed) ... */
706 if (in != NULL
707 || offset > 0
708 || offset + len < descr->sizeof_register[regnum])
709 {
710 gdb_assert (read != NULL);
711 read (regcache, regnum, reg);
712 }
713 /* ... modify ... */
714 if (in != NULL)
715 memcpy (in, reg + offset, len);
716 if (out != NULL)
717 memcpy (reg + offset, out, len);
718 /* ... write (when needed). */
719 if (out != NULL)
720 {
721 gdb_assert (write != NULL);
722 write (regcache, regnum, reg);
723 }
724 }
725
726 void
727 regcache_raw_read_part (struct regcache *regcache, int regnum,
728 int offset, int len, gdb_byte *buf)
729 {
730 struct regcache_descr *descr = regcache->descr;
731 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
732 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
733 regcache_raw_read, regcache_raw_write);
734 }
735
736 void
737 regcache_raw_write_part (struct regcache *regcache, int regnum,
738 int offset, int len, const gdb_byte *buf)
739 {
740 struct regcache_descr *descr = regcache->descr;
741 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
742 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
743 regcache_raw_read, regcache_raw_write);
744 }
745
746 void
747 regcache_cooked_read_part (struct regcache *regcache, int regnum,
748 int offset, int len, gdb_byte *buf)
749 {
750 struct regcache_descr *descr = regcache->descr;
751 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
752 regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
753 regcache_cooked_read, regcache_cooked_write);
754 }
755
756 void
757 regcache_cooked_write_part (struct regcache *regcache, int regnum,
758 int offset, int len, const gdb_byte *buf)
759 {
760 struct regcache_descr *descr = regcache->descr;
761 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
762 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
763 regcache_cooked_read, regcache_cooked_write);
764 }
765
766 /* Hack to keep code that view the register buffer as raw bytes
767 working. */
768
769 int
770 register_offset_hack (struct gdbarch *gdbarch, int regnum)
771 {
772 struct regcache_descr *descr = regcache_descr (gdbarch);
773 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
774 return descr->register_offset[regnum];
775 }
776
777
778 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
779
780 void
781 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
782 {
783 void *regbuf;
784 size_t size;
785
786 gdb_assert (regcache != NULL);
787 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
788 gdb_assert (!regcache->readonly_p);
789
790 regbuf = register_buffer (regcache, regnum);
791 size = regcache->descr->sizeof_register[regnum];
792
793 if (buf)
794 memcpy (regbuf, buf, size);
795 else
796 memset (regbuf, 0, size);
797
798 /* Mark the register as cached. */
799 regcache->register_valid_p[regnum] = 1;
800 }
801
802 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
803
804 void
805 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
806 {
807 const void *regbuf;
808 size_t size;
809
810 gdb_assert (regcache != NULL && buf != NULL);
811 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
812
813 regbuf = register_buffer (regcache, regnum);
814 size = regcache->descr->sizeof_register[regnum];
815 memcpy (buf, regbuf, size);
816 }
817
818
819 /* read_pc, write_pc, etc. Special handling for register PC. */
820
821 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
822 read_sp(), will eventually be replaced by per-frame methods.
823 Instead of relying on the global INFERIOR_PTID, they will use the
824 contextual information provided by the FRAME. These functions do
825 not belong in the register cache. */
826
827 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
828 write_pc_pid() and write_pc(), all need to be replaced by something
829 that does not rely on global state. But what? */
830
831 CORE_ADDR
832 read_pc_pid (ptid_t ptid)
833 {
834 struct regcache *regcache = get_thread_regcache (ptid);
835 struct gdbarch *gdbarch = get_regcache_arch (regcache);
836
837 CORE_ADDR pc_val;
838
839 if (gdbarch_read_pc_p (gdbarch))
840 pc_val = gdbarch_read_pc (gdbarch, regcache);
841 /* Else use per-frame method on get_current_frame. */
842 else if (PC_REGNUM >= 0)
843 {
844 ULONGEST raw_val;
845 regcache_cooked_read_unsigned (regcache, PC_REGNUM, &raw_val);
846 pc_val = gdbarch_addr_bits_remove (current_gdbarch, raw_val);
847 }
848 else
849 internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
850
851 return pc_val;
852 }
853
854 CORE_ADDR
855 read_pc (void)
856 {
857 return read_pc_pid (inferior_ptid);
858 }
859
860 void
861 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
862 {
863 struct regcache *regcache = get_thread_regcache (ptid);
864 struct gdbarch *gdbarch = get_regcache_arch (regcache);
865
866 if (gdbarch_write_pc_p (gdbarch))
867 gdbarch_write_pc (gdbarch, regcache, pc);
868 else if (PC_REGNUM >= 0)
869 regcache_cooked_write_unsigned (regcache, PC_REGNUM, pc);
870 else
871 internal_error (__FILE__, __LINE__,
872 _("write_pc_pid: Unable to update PC"));
873 }
874
875 void
876 write_pc (CORE_ADDR pc)
877 {
878 write_pc_pid (pc, inferior_ptid);
879 }
880
881
882 static void
883 reg_flush_command (char *command, int from_tty)
884 {
885 /* Force-flush the register cache. */
886 registers_changed ();
887 if (from_tty)
888 printf_filtered (_("Register cache flushed.\n"));
889 }
890
891 static void
892 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
893 const unsigned char *buf, long len)
894 {
895 int i;
896 switch (endian)
897 {
898 case BFD_ENDIAN_BIG:
899 for (i = 0; i < len; i++)
900 fprintf_unfiltered (file, "%02x", buf[i]);
901 break;
902 case BFD_ENDIAN_LITTLE:
903 for (i = len - 1; i >= 0; i--)
904 fprintf_unfiltered (file, "%02x", buf[i]);
905 break;
906 default:
907 internal_error (__FILE__, __LINE__, _("Bad switch"));
908 }
909 }
910
911 enum regcache_dump_what
912 {
913 regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
914 };
915
916 static void
917 regcache_dump (struct regcache *regcache, struct ui_file *file,
918 enum regcache_dump_what what_to_dump)
919 {
920 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
921 struct gdbarch *gdbarch = regcache->descr->gdbarch;
922 int regnum;
923 int footnote_nr = 0;
924 int footnote_register_size = 0;
925 int footnote_register_offset = 0;
926 int footnote_register_type_name_null = 0;
927 long register_offset = 0;
928 unsigned char buf[MAX_REGISTER_SIZE];
929
930 #if 0
931 fprintf_unfiltered (file, "nr_raw_registers %d\n",
932 regcache->descr->nr_raw_registers);
933 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
934 regcache->descr->nr_cooked_registers);
935 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
936 regcache->descr->sizeof_raw_registers);
937 fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
938 regcache->descr->sizeof_raw_register_valid_p);
939 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
940 gdbarch_num_regs (current_gdbarch));
941 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
942 gdbarch_num_pseudo_regs (current_gdbarch));
943 #endif
944
945 gdb_assert (regcache->descr->nr_cooked_registers
946 == (gdbarch_num_regs (current_gdbarch)
947 + gdbarch_num_pseudo_regs (current_gdbarch)));
948
949 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
950 {
951 /* Name. */
952 if (regnum < 0)
953 fprintf_unfiltered (file, " %-10s", "Name");
954 else
955 {
956 const char *p = gdbarch_register_name (current_gdbarch, regnum);
957 if (p == NULL)
958 p = "";
959 else if (p[0] == '\0')
960 p = "''";
961 fprintf_unfiltered (file, " %-10s", p);
962 }
963
964 /* Number. */
965 if (regnum < 0)
966 fprintf_unfiltered (file, " %4s", "Nr");
967 else
968 fprintf_unfiltered (file, " %4d", regnum);
969
970 /* Relative number. */
971 if (regnum < 0)
972 fprintf_unfiltered (file, " %4s", "Rel");
973 else if (regnum < gdbarch_num_regs (current_gdbarch))
974 fprintf_unfiltered (file, " %4d", regnum);
975 else
976 fprintf_unfiltered (file, " %4d",
977 (regnum - gdbarch_num_regs (current_gdbarch)));
978
979 /* Offset. */
980 if (regnum < 0)
981 fprintf_unfiltered (file, " %6s ", "Offset");
982 else
983 {
984 fprintf_unfiltered (file, " %6ld",
985 regcache->descr->register_offset[regnum]);
986 if (register_offset != regcache->descr->register_offset[regnum]
987 || (regnum > 0
988 && (regcache->descr->register_offset[regnum]
989 != (regcache->descr->register_offset[regnum - 1]
990 + regcache->descr->sizeof_register[regnum - 1])))
991 )
992 {
993 if (!footnote_register_offset)
994 footnote_register_offset = ++footnote_nr;
995 fprintf_unfiltered (file, "*%d", footnote_register_offset);
996 }
997 else
998 fprintf_unfiltered (file, " ");
999 register_offset = (regcache->descr->register_offset[regnum]
1000 + regcache->descr->sizeof_register[regnum]);
1001 }
1002
1003 /* Size. */
1004 if (regnum < 0)
1005 fprintf_unfiltered (file, " %5s ", "Size");
1006 else
1007 fprintf_unfiltered (file, " %5ld",
1008 regcache->descr->sizeof_register[regnum]);
1009
1010 /* Type. */
1011 {
1012 const char *t;
1013 if (regnum < 0)
1014 t = "Type";
1015 else
1016 {
1017 static const char blt[] = "builtin_type";
1018 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1019 if (t == NULL)
1020 {
1021 char *n;
1022 if (!footnote_register_type_name_null)
1023 footnote_register_type_name_null = ++footnote_nr;
1024 n = xstrprintf ("*%d", footnote_register_type_name_null);
1025 make_cleanup (xfree, n);
1026 t = n;
1027 }
1028 /* Chop a leading builtin_type. */
1029 if (strncmp (t, blt, strlen (blt)) == 0)
1030 t += strlen (blt);
1031 }
1032 fprintf_unfiltered (file, " %-15s", t);
1033 }
1034
1035 /* Leading space always present. */
1036 fprintf_unfiltered (file, " ");
1037
1038 /* Value, raw. */
1039 if (what_to_dump == regcache_dump_raw)
1040 {
1041 if (regnum < 0)
1042 fprintf_unfiltered (file, "Raw value");
1043 else if (regnum >= regcache->descr->nr_raw_registers)
1044 fprintf_unfiltered (file, "<cooked>");
1045 else if (!regcache_valid_p (regcache, regnum))
1046 fprintf_unfiltered (file, "<invalid>");
1047 else
1048 {
1049 regcache_raw_read (regcache, regnum, buf);
1050 fprintf_unfiltered (file, "0x");
1051 dump_endian_bytes (file,
1052 gdbarch_byte_order (current_gdbarch), buf,
1053 regcache->descr->sizeof_register[regnum]);
1054 }
1055 }
1056
1057 /* Value, cooked. */
1058 if (what_to_dump == regcache_dump_cooked)
1059 {
1060 if (regnum < 0)
1061 fprintf_unfiltered (file, "Cooked value");
1062 else
1063 {
1064 regcache_cooked_read (regcache, regnum, buf);
1065 fprintf_unfiltered (file, "0x");
1066 dump_endian_bytes (file,
1067 gdbarch_byte_order (current_gdbarch), buf,
1068 regcache->descr->sizeof_register[regnum]);
1069 }
1070 }
1071
1072 /* Group members. */
1073 if (what_to_dump == regcache_dump_groups)
1074 {
1075 if (regnum < 0)
1076 fprintf_unfiltered (file, "Groups");
1077 else
1078 {
1079 const char *sep = "";
1080 struct reggroup *group;
1081 for (group = reggroup_next (gdbarch, NULL);
1082 group != NULL;
1083 group = reggroup_next (gdbarch, group))
1084 {
1085 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1086 {
1087 fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1088 sep = ",";
1089 }
1090 }
1091 }
1092 }
1093
1094 fprintf_unfiltered (file, "\n");
1095 }
1096
1097 if (footnote_register_size)
1098 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1099 footnote_register_size);
1100 if (footnote_register_offset)
1101 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1102 footnote_register_offset);
1103 if (footnote_register_type_name_null)
1104 fprintf_unfiltered (file,
1105 "*%d: Register type's name NULL.\n",
1106 footnote_register_type_name_null);
1107 do_cleanups (cleanups);
1108 }
1109
1110 static void
1111 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1112 {
1113 if (args == NULL)
1114 regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1115 else
1116 {
1117 struct ui_file *file = gdb_fopen (args, "w");
1118 if (file == NULL)
1119 perror_with_name (_("maintenance print architecture"));
1120 regcache_dump (current_regcache, file, what_to_dump);
1121 ui_file_delete (file);
1122 }
1123 }
1124
1125 static void
1126 maintenance_print_registers (char *args, int from_tty)
1127 {
1128 regcache_print (args, regcache_dump_none);
1129 }
1130
1131 static void
1132 maintenance_print_raw_registers (char *args, int from_tty)
1133 {
1134 regcache_print (args, regcache_dump_raw);
1135 }
1136
1137 static void
1138 maintenance_print_cooked_registers (char *args, int from_tty)
1139 {
1140 regcache_print (args, regcache_dump_cooked);
1141 }
1142
1143 static void
1144 maintenance_print_register_groups (char *args, int from_tty)
1145 {
1146 regcache_print (args, regcache_dump_groups);
1147 }
1148
1149 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1150
1151 void
1152 _initialize_regcache (void)
1153 {
1154 regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1155
1156 observer_attach_target_changed (regcache_observer_target_changed);
1157
1158 add_com ("flushregs", class_maintenance, reg_flush_command,
1159 _("Force gdb to flush its register cache (maintainer command)"));
1160
1161 add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1162 Print the internal register configuration.\n\
1163 Takes an optional file parameter."), &maintenanceprintlist);
1164 add_cmd ("raw-registers", class_maintenance,
1165 maintenance_print_raw_registers, _("\
1166 Print the internal register configuration including raw values.\n\
1167 Takes an optional file parameter."), &maintenanceprintlist);
1168 add_cmd ("cooked-registers", class_maintenance,
1169 maintenance_print_cooked_registers, _("\
1170 Print the internal register configuration including cooked values.\n\
1171 Takes an optional file parameter."), &maintenanceprintlist);
1172 add_cmd ("register-groups", class_maintenance,
1173 maintenance_print_register_groups, _("\
1174 Print the internal register configuration including each register's group.\n\
1175 Takes an optional file parameter."),
1176 &maintenanceprintlist);
1177
1178 }
This page took 0.05264 seconds and 4 git commands to generate.