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