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