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