Simplify regcache_restore
[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-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "target.h"
23 #include "gdbarch.h"
24 #include "gdbcmd.h"
25 #include "regcache.h"
26 #include "reggroups.h"
27 #include "observer.h"
28 #include "remote.h"
29 #include "valprint.h"
30 #include "regset.h"
31
32 /*
33 * DATA STRUCTURE
34 *
35 * Here is the actual register cache.
36 */
37
38 /* Per-architecture object describing the layout of a register cache.
39 Computed once when the architecture is created. */
40
41 struct gdbarch_data *regcache_descr_handle;
42
43 struct regcache_descr
44 {
45 /* The architecture this descriptor belongs to. */
46 struct gdbarch *gdbarch;
47
48 /* The raw register cache. Each raw (or hard) register is supplied
49 by the target interface. The raw cache should not contain
50 redundant information - if the PC is constructed from two
51 registers then those registers and not the PC lives in the raw
52 cache. */
53 int nr_raw_registers;
54 long sizeof_raw_registers;
55 long sizeof_raw_register_status;
56
57 /* The cooked register space. Each cooked register in the range
58 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
59 register. The remaining [NR_RAW_REGISTERS
60 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
61 both raw registers and memory by the architecture methods
62 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
63 int nr_cooked_registers;
64 long sizeof_cooked_registers;
65 long sizeof_cooked_register_status;
66
67 /* Offset and size (in 8 bit bytes), of each register in the
68 register cache. All registers (including those in the range
69 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
70 offset. */
71 long *register_offset;
72 long *sizeof_register;
73
74 /* Cached table containing the type of each register. */
75 struct type **register_type;
76 };
77
78 static void *
79 init_regcache_descr (struct gdbarch *gdbarch)
80 {
81 int i;
82 struct regcache_descr *descr;
83 gdb_assert (gdbarch != NULL);
84
85 /* Create an initial, zero filled, table. */
86 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
87 descr->gdbarch = gdbarch;
88
89 /* Total size of the register space. The raw registers are mapped
90 directly onto the raw register cache while the pseudo's are
91 either mapped onto raw-registers or memory. */
92 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
93 + gdbarch_num_pseudo_regs (gdbarch);
94 descr->sizeof_cooked_register_status
95 = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
96
97 /* Fill in a table of register types. */
98 descr->register_type
99 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
100 struct type *);
101 for (i = 0; i < descr->nr_cooked_registers; i++)
102 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
103
104 /* Construct a strictly RAW register cache. Don't allow pseudo's
105 into the register cache. */
106 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
107 descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
108
109 /* Lay out the register cache.
110
111 NOTE: cagney/2002-05-22: Only register_type() is used when
112 constructing the register cache. It is assumed that the
113 register's raw size, virtual size and type length are all the
114 same. */
115
116 {
117 long offset = 0;
118
119 descr->sizeof_register
120 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
121 descr->register_offset
122 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
123 for (i = 0; i < descr->nr_raw_registers; i++)
124 {
125 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
126 descr->register_offset[i] = offset;
127 offset += descr->sizeof_register[i];
128 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
129 }
130 /* Set the real size of the raw register cache buffer. */
131 descr->sizeof_raw_registers = offset;
132
133 for (; i < descr->nr_cooked_registers; i++)
134 {
135 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
136 descr->register_offset[i] = offset;
137 offset += descr->sizeof_register[i];
138 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
139 }
140 /* Set the real size of the readonly register cache buffer. */
141 descr->sizeof_cooked_registers = offset;
142 }
143
144 return descr;
145 }
146
147 static struct regcache_descr *
148 regcache_descr (struct gdbarch *gdbarch)
149 {
150 return (struct regcache_descr *) gdbarch_data (gdbarch,
151 regcache_descr_handle);
152 }
153
154 /* Utility functions returning useful register attributes stored in
155 the regcache descr. */
156
157 struct type *
158 register_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
169 int
170 register_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 /* See common/common-regcache.h. */
183
184 int
185 regcache_register_size (const struct regcache *regcache, int n)
186 {
187 return register_size (get_regcache_arch (regcache), n);
188 }
189
190 /* The register cache for storing raw register values. */
191
192 struct regcache
193 {
194 struct regcache_descr *descr;
195
196 /* The address space of this register cache (for registers where it
197 makes sense, like PC or SP). */
198 struct address_space *aspace;
199
200 /* The register buffers. A read-only register cache can hold the
201 full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
202 register cache can only hold [0 .. gdbarch_num_regs). */
203 gdb_byte *registers;
204 /* Register cache status. */
205 signed char *register_status;
206 /* Is this a read-only cache? A read-only cache is used for saving
207 the target's register state (e.g, across an inferior function
208 call or just before forcing a function return). A read-only
209 cache can only be updated via the methods regcache_dup() and
210 regcache_cpy(). The actual contents are determined by the
211 reggroup_save and reggroup_restore methods. */
212 int readonly_p;
213 /* If this is a read-write cache, which thread's registers is
214 it connected to? */
215 ptid_t ptid;
216 };
217
218 /* See regcache.h. */
219
220 ptid_t
221 regcache_get_ptid (const struct regcache *regcache)
222 {
223 gdb_assert (!ptid_equal (regcache->ptid, minus_one_ptid));
224
225 return regcache->ptid;
226 }
227
228 static struct regcache *
229 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
230 int readonly_p)
231 {
232 struct regcache_descr *descr;
233 struct regcache *regcache;
234
235 gdb_assert (gdbarch != NULL);
236 descr = regcache_descr (gdbarch);
237 regcache = XNEW (struct regcache);
238 regcache->descr = descr;
239 regcache->readonly_p = readonly_p;
240 if (readonly_p)
241 {
242 regcache->registers
243 = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
244 regcache->register_status
245 = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
246 }
247 else
248 {
249 regcache->registers
250 = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
251 regcache->register_status
252 = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
253 }
254 regcache->aspace = aspace;
255 regcache->ptid = minus_one_ptid;
256 return regcache;
257 }
258
259 struct regcache *
260 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
261 {
262 return regcache_xmalloc_1 (gdbarch, aspace, 1);
263 }
264
265 void
266 regcache_xfree (struct regcache *regcache)
267 {
268 if (regcache == NULL)
269 return;
270 xfree (regcache->registers);
271 xfree (regcache->register_status);
272 xfree (regcache);
273 }
274
275 static void
276 do_regcache_xfree (void *data)
277 {
278 regcache_xfree ((struct regcache *) data);
279 }
280
281 struct cleanup *
282 make_cleanup_regcache_xfree (struct regcache *regcache)
283 {
284 return make_cleanup (do_regcache_xfree, regcache);
285 }
286
287 /* Cleanup routines for invalidating a register. */
288
289 struct register_to_invalidate
290 {
291 struct regcache *regcache;
292 int regnum;
293 };
294
295 static void
296 do_regcache_invalidate (void *data)
297 {
298 struct register_to_invalidate *reg = (struct register_to_invalidate *) data;
299
300 regcache_invalidate (reg->regcache, reg->regnum);
301 }
302
303 static struct cleanup *
304 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
305 {
306 struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
307
308 reg->regcache = regcache;
309 reg->regnum = regnum;
310 return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
311 }
312
313 /* Return REGCACHE's architecture. */
314
315 struct gdbarch *
316 get_regcache_arch (const struct regcache *regcache)
317 {
318 return regcache->descr->gdbarch;
319 }
320
321 struct address_space *
322 get_regcache_aspace (const struct regcache *regcache)
323 {
324 return regcache->aspace;
325 }
326
327 /* Return a pointer to register REGNUM's buffer cache. */
328
329 static gdb_byte *
330 register_buffer (const struct regcache *regcache, int regnum)
331 {
332 return regcache->registers + regcache->descr->register_offset[regnum];
333 }
334
335 void
336 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
337 void *src)
338 {
339 struct gdbarch *gdbarch = dst->descr->gdbarch;
340 gdb_byte buf[MAX_REGISTER_SIZE];
341 int regnum;
342
343 /* The DST should be `read-only', if it wasn't then the save would
344 end up trying to write the register values back out to the
345 target. */
346 gdb_assert (dst->readonly_p);
347 /* Clear the dest. */
348 memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
349 memset (dst->register_status, 0,
350 dst->descr->sizeof_cooked_register_status);
351 /* Copy over any registers (identified by their membership in the
352 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
353 gdbarch_num_pseudo_regs) range is checked since some architectures need
354 to save/restore `cooked' registers that live in memory. */
355 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
356 {
357 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
358 {
359 enum register_status status = cooked_read (src, regnum, buf);
360
361 if (status == REG_VALID)
362 memcpy (register_buffer (dst, regnum), buf,
363 register_size (gdbarch, regnum));
364 else
365 {
366 gdb_assert (status != REG_UNKNOWN);
367
368 memset (register_buffer (dst, regnum), 0,
369 register_size (gdbarch, regnum));
370 }
371 dst->register_status[regnum] = status;
372 }
373 }
374 }
375
376 static void
377 regcache_restore (struct regcache *dst, struct regcache *src)
378 {
379 struct gdbarch *gdbarch = dst->descr->gdbarch;
380 int regnum;
381
382 /* The dst had better not be read-only. If it is, the `restore'
383 doesn't make much sense. */
384 gdb_assert (!dst->readonly_p);
385 gdb_assert (src->readonly_p);
386 /* Copy over any registers, being careful to only restore those that
387 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
388 + gdbarch_num_pseudo_regs) range is checked since some architectures need
389 to save/restore `cooked' registers that live in memory. */
390 for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
391 {
392 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
393 {
394 if (src->register_status[regnum] == REG_VALID)
395 regcache_cooked_write (dst, regnum, register_buffer (src, regnum));
396 }
397 }
398 }
399
400 static enum register_status
401 do_cooked_read (void *src, int regnum, gdb_byte *buf)
402 {
403 struct regcache *regcache = (struct regcache *) src;
404
405 return regcache_cooked_read (regcache, regnum, buf);
406 }
407
408 static void regcache_cpy_no_passthrough (struct regcache *dst,
409 struct regcache *src);
410
411 void
412 regcache_cpy (struct regcache *dst, struct regcache *src)
413 {
414 gdb_assert (src != NULL && dst != NULL);
415 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
416 gdb_assert (src != dst);
417 gdb_assert (src->readonly_p || dst->readonly_p);
418
419 if (!src->readonly_p)
420 regcache_save (dst, do_cooked_read, src);
421 else if (!dst->readonly_p)
422 regcache_restore (dst, src);
423 else
424 regcache_cpy_no_passthrough (dst, src);
425 }
426
427 /* Copy/duplicate the contents of a register cache. Unlike regcache_cpy,
428 which is pass-through, this does not go through to the target.
429 Only values values already in the cache are transferred. The SRC and DST
430 buffers must not overlap. */
431
432 static void
433 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
434 {
435 gdb_assert (src != NULL && dst != NULL);
436 gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
437 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
438 move of data into a thread's regcache. Doing this would be silly
439 - it would mean that regcache->register_status would be
440 completely invalid. */
441 gdb_assert (dst->readonly_p && src->readonly_p);
442
443 memcpy (dst->registers, src->registers,
444 dst->descr->sizeof_cooked_registers);
445 memcpy (dst->register_status, src->register_status,
446 dst->descr->sizeof_cooked_register_status);
447 }
448
449 struct regcache *
450 regcache_dup (struct regcache *src)
451 {
452 struct regcache *newbuf;
453
454 newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
455 regcache_cpy (newbuf, src);
456 return newbuf;
457 }
458
459 enum register_status
460 regcache_register_status (const struct regcache *regcache, int regnum)
461 {
462 gdb_assert (regcache != NULL);
463 gdb_assert (regnum >= 0);
464 if (regcache->readonly_p)
465 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
466 else
467 gdb_assert (regnum < regcache->descr->nr_raw_registers);
468
469 return (enum register_status) regcache->register_status[regnum];
470 }
471
472 void
473 regcache_invalidate (struct regcache *regcache, int regnum)
474 {
475 gdb_assert (regcache != NULL);
476 gdb_assert (regnum >= 0);
477 gdb_assert (!regcache->readonly_p);
478 gdb_assert (regnum < regcache->descr->nr_raw_registers);
479 regcache->register_status[regnum] = REG_UNKNOWN;
480 }
481
482
483 /* Global structure containing the current regcache. */
484
485 /* NOTE: this is a write-through cache. There is no "dirty" bit for
486 recording if the register values have been changed (eg. by the
487 user). Therefore all registers must be written back to the
488 target when appropriate. */
489
490 struct regcache_list
491 {
492 struct regcache *regcache;
493 struct regcache_list *next;
494 };
495
496 static struct regcache_list *current_regcache;
497
498 struct regcache *
499 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
500 struct address_space *aspace)
501 {
502 struct regcache_list *list;
503 struct regcache *new_regcache;
504
505 for (list = current_regcache; list; list = list->next)
506 if (ptid_equal (list->regcache->ptid, ptid)
507 && get_regcache_arch (list->regcache) == gdbarch)
508 return list->regcache;
509
510 new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
511 new_regcache->ptid = ptid;
512
513 list = XNEW (struct regcache_list);
514 list->regcache = new_regcache;
515 list->next = current_regcache;
516 current_regcache = list;
517
518 return new_regcache;
519 }
520
521 struct regcache *
522 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
523 {
524 struct address_space *aspace;
525
526 /* For the benefit of "maint print registers" & co when debugging an
527 executable, allow dumping the regcache even when there is no
528 thread selected (target_thread_address_space internal-errors if
529 no address space is found). Note that normal user commands will
530 fail higher up on the call stack due to no
531 target_has_registers. */
532 aspace = (ptid_equal (null_ptid, ptid)
533 ? NULL
534 : target_thread_address_space (ptid));
535
536 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
537 }
538
539 static ptid_t current_thread_ptid;
540 static struct gdbarch *current_thread_arch;
541
542 struct regcache *
543 get_thread_regcache (ptid_t ptid)
544 {
545 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
546 {
547 current_thread_ptid = ptid;
548 current_thread_arch = target_thread_architecture (ptid);
549 }
550
551 return get_thread_arch_regcache (ptid, current_thread_arch);
552 }
553
554 struct regcache *
555 get_current_regcache (void)
556 {
557 return get_thread_regcache (inferior_ptid);
558 }
559
560 /* See common/common-regcache.h. */
561
562 struct regcache *
563 get_thread_regcache_for_ptid (ptid_t ptid)
564 {
565 return get_thread_regcache (ptid);
566 }
567
568 /* Observer for the target_changed event. */
569
570 static void
571 regcache_observer_target_changed (struct target_ops *target)
572 {
573 registers_changed ();
574 }
575
576 /* Update global variables old ptids to hold NEW_PTID if they were
577 holding OLD_PTID. */
578 static void
579 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
580 {
581 struct regcache_list *list;
582
583 for (list = current_regcache; list; list = list->next)
584 if (ptid_equal (list->regcache->ptid, old_ptid))
585 list->regcache->ptid = new_ptid;
586 }
587
588 /* Low level examining and depositing of registers.
589
590 The caller is responsible for making sure that the inferior is
591 stopped before calling the fetching routines, or it will get
592 garbage. (a change from GDB version 3, in which the caller got the
593 value from the last stop). */
594
595 /* REGISTERS_CHANGED ()
596
597 Indicate that registers may have changed, so invalidate the cache. */
598
599 void
600 registers_changed_ptid (ptid_t ptid)
601 {
602 struct regcache_list *list, **list_link;
603
604 list = current_regcache;
605 list_link = &current_regcache;
606 while (list)
607 {
608 if (ptid_match (list->regcache->ptid, ptid))
609 {
610 struct regcache_list *dead = list;
611
612 *list_link = list->next;
613 regcache_xfree (list->regcache);
614 list = *list_link;
615 xfree (dead);
616 continue;
617 }
618
619 list_link = &list->next;
620 list = *list_link;
621 }
622
623 if (ptid_match (current_thread_ptid, ptid))
624 {
625 current_thread_ptid = null_ptid;
626 current_thread_arch = NULL;
627 }
628
629 if (ptid_match (inferior_ptid, ptid))
630 {
631 /* We just deleted the regcache of the current thread. Need to
632 forget about any frames we have cached, too. */
633 reinit_frame_cache ();
634 }
635 }
636
637 void
638 registers_changed (void)
639 {
640 registers_changed_ptid (minus_one_ptid);
641
642 /* Force cleanup of any alloca areas if using C alloca instead of
643 a builtin alloca. This particular call is used to clean up
644 areas allocated by low level target code which may build up
645 during lengthy interactions between gdb and the target before
646 gdb gives control to the user (ie watchpoints). */
647 alloca (0);
648 }
649
650 void
651 regcache_raw_update (struct regcache *regcache, int regnum)
652 {
653 gdb_assert (regcache != NULL);
654 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
655
656 /* Make certain that the register cache is up-to-date with respect
657 to the current thread. This switching shouldn't be necessary
658 only there is still only one target side register cache. Sigh!
659 On the bright side, at least there is a regcache object. */
660
661 if (!regcache->readonly_p
662 && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
663 {
664 target_fetch_registers (regcache, regnum);
665
666 /* A number of targets can't access the whole set of raw
667 registers (because the debug API provides no means to get at
668 them). */
669 if (regcache->register_status[regnum] == REG_UNKNOWN)
670 regcache->register_status[regnum] = REG_UNAVAILABLE;
671 }
672 }
673
674 enum register_status
675 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
676 {
677 gdb_assert (buf != NULL);
678 regcache_raw_update (regcache, regnum);
679
680 if (regcache->register_status[regnum] != REG_VALID)
681 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
682 else
683 memcpy (buf, register_buffer (regcache, regnum),
684 regcache->descr->sizeof_register[regnum]);
685
686 return (enum register_status) regcache->register_status[regnum];
687 }
688
689 enum register_status
690 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
691 {
692 gdb_byte *buf;
693 enum register_status status;
694
695 gdb_assert (regcache != NULL);
696 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
697 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
698 status = regcache_raw_read (regcache, regnum, buf);
699 if (status == REG_VALID)
700 *val = extract_signed_integer
701 (buf, regcache->descr->sizeof_register[regnum],
702 gdbarch_byte_order (regcache->descr->gdbarch));
703 else
704 *val = 0;
705 return status;
706 }
707
708 enum register_status
709 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
710 ULONGEST *val)
711 {
712 gdb_byte *buf;
713 enum register_status status;
714
715 gdb_assert (regcache != NULL);
716 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
717 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
718 status = regcache_raw_read (regcache, regnum, buf);
719 if (status == REG_VALID)
720 *val = extract_unsigned_integer
721 (buf, regcache->descr->sizeof_register[regnum],
722 gdbarch_byte_order (regcache->descr->gdbarch));
723 else
724 *val = 0;
725 return status;
726 }
727
728 void
729 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
730 {
731 gdb_byte *buf;
732
733 gdb_assert (regcache != NULL);
734 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
735 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
736 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
737 gdbarch_byte_order (regcache->descr->gdbarch), val);
738 regcache_raw_write (regcache, regnum, buf);
739 }
740
741 void
742 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
743 ULONGEST val)
744 {
745 gdb_byte *buf;
746
747 gdb_assert (regcache != NULL);
748 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
749 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
750 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
751 gdbarch_byte_order (regcache->descr->gdbarch), val);
752 regcache_raw_write (regcache, regnum, buf);
753 }
754
755 LONGEST
756 regcache_raw_get_signed (struct regcache *regcache, int regnum)
757 {
758 LONGEST value;
759 enum register_status status;
760
761 status = regcache_raw_read_signed (regcache, regnum, &value);
762 if (status == REG_UNAVAILABLE)
763 throw_error (NOT_AVAILABLE_ERROR,
764 _("Register %d is not available"), regnum);
765 return value;
766 }
767
768 enum register_status
769 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
770 {
771 gdb_assert (regnum >= 0);
772 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
773 if (regnum < regcache->descr->nr_raw_registers)
774 return regcache_raw_read (regcache, regnum, buf);
775 else if (regcache->readonly_p
776 && regcache->register_status[regnum] != REG_UNKNOWN)
777 {
778 /* Read-only register cache, perhaps the cooked value was
779 cached? */
780 if (regcache->register_status[regnum] == REG_VALID)
781 memcpy (buf, register_buffer (regcache, regnum),
782 regcache->descr->sizeof_register[regnum]);
783 else
784 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
785
786 return (enum register_status) regcache->register_status[regnum];
787 }
788 else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
789 {
790 struct value *mark, *computed;
791 enum register_status result = REG_VALID;
792
793 mark = value_mark ();
794
795 computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
796 regcache, regnum);
797 if (value_entirely_available (computed))
798 memcpy (buf, value_contents_raw (computed),
799 regcache->descr->sizeof_register[regnum]);
800 else
801 {
802 memset (buf, 0, regcache->descr->sizeof_register[regnum]);
803 result = REG_UNAVAILABLE;
804 }
805
806 value_free_to_mark (mark);
807
808 return result;
809 }
810 else
811 return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
812 regnum, buf);
813 }
814
815 struct value *
816 regcache_cooked_read_value (struct regcache *regcache, int regnum)
817 {
818 gdb_assert (regnum >= 0);
819 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
820
821 if (regnum < regcache->descr->nr_raw_registers
822 || (regcache->readonly_p
823 && regcache->register_status[regnum] != REG_UNKNOWN)
824 || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
825 {
826 struct value *result;
827
828 result = allocate_value (register_type (regcache->descr->gdbarch,
829 regnum));
830 VALUE_LVAL (result) = lval_register;
831 VALUE_REGNUM (result) = regnum;
832
833 /* It is more efficient in general to do this delegation in this
834 direction than in the other one, even though the value-based
835 API is preferred. */
836 if (regcache_cooked_read (regcache, regnum,
837 value_contents_raw (result)) == REG_UNAVAILABLE)
838 mark_value_bytes_unavailable (result, 0,
839 TYPE_LENGTH (value_type (result)));
840
841 return result;
842 }
843 else
844 return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
845 regcache, regnum);
846 }
847
848 enum register_status
849 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
850 LONGEST *val)
851 {
852 enum register_status status;
853 gdb_byte *buf;
854
855 gdb_assert (regcache != NULL);
856 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
857 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
858 status = regcache_cooked_read (regcache, regnum, buf);
859 if (status == REG_VALID)
860 *val = extract_signed_integer
861 (buf, regcache->descr->sizeof_register[regnum],
862 gdbarch_byte_order (regcache->descr->gdbarch));
863 else
864 *val = 0;
865 return status;
866 }
867
868 enum register_status
869 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
870 ULONGEST *val)
871 {
872 enum register_status status;
873 gdb_byte *buf;
874
875 gdb_assert (regcache != NULL);
876 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
877 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
878 status = regcache_cooked_read (regcache, regnum, buf);
879 if (status == REG_VALID)
880 *val = extract_unsigned_integer
881 (buf, regcache->descr->sizeof_register[regnum],
882 gdbarch_byte_order (regcache->descr->gdbarch));
883 else
884 *val = 0;
885 return status;
886 }
887
888 void
889 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
890 LONGEST val)
891 {
892 gdb_byte *buf;
893
894 gdb_assert (regcache != NULL);
895 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
896 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
897 store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
898 gdbarch_byte_order (regcache->descr->gdbarch), val);
899 regcache_cooked_write (regcache, regnum, buf);
900 }
901
902 void
903 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
904 ULONGEST val)
905 {
906 gdb_byte *buf;
907
908 gdb_assert (regcache != NULL);
909 gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
910 buf = (gdb_byte *) alloca (regcache->descr->sizeof_register[regnum]);
911 store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
912 gdbarch_byte_order (regcache->descr->gdbarch), val);
913 regcache_cooked_write (regcache, regnum, buf);
914 }
915
916 /* See regcache.h. */
917
918 void
919 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
920 const gdb_byte *buf)
921 {
922 memcpy (register_buffer (regcache, regnum), buf,
923 regcache->descr->sizeof_register[regnum]);
924 regcache->register_status[regnum] = REG_VALID;
925 }
926
927 void
928 regcache_raw_write (struct regcache *regcache, int regnum,
929 const gdb_byte *buf)
930 {
931 struct cleanup *old_chain;
932
933 gdb_assert (regcache != NULL && buf != NULL);
934 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
935 gdb_assert (!regcache->readonly_p);
936
937 /* On the sparc, writing %g0 is a no-op, so we don't even want to
938 change the registers array if something writes to this register. */
939 if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
940 return;
941
942 /* If we have a valid copy of the register, and new value == old
943 value, then don't bother doing the actual store. */
944 if (regcache_register_status (regcache, regnum) == REG_VALID
945 && (memcmp (register_buffer (regcache, regnum), buf,
946 regcache->descr->sizeof_register[regnum]) == 0))
947 return;
948
949 target_prepare_to_store (regcache);
950 regcache_raw_set_cached_value (regcache, regnum, buf);
951
952 /* Register a cleanup function for invalidating the register after it is
953 written, in case of a failure. */
954 old_chain = make_cleanup_regcache_invalidate (regcache, regnum);
955
956 target_store_registers (regcache, regnum);
957
958 /* The target did not throw an error so we can discard invalidating the
959 register and restore the cleanup chain to what it was. */
960 discard_cleanups (old_chain);
961 }
962
963 void
964 regcache_cooked_write (struct regcache *regcache, int regnum,
965 const gdb_byte *buf)
966 {
967 gdb_assert (regnum >= 0);
968 gdb_assert (regnum < regcache->descr->nr_cooked_registers);
969 if (regnum < regcache->descr->nr_raw_registers)
970 regcache_raw_write (regcache, regnum, buf);
971 else
972 gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
973 regnum, buf);
974 }
975
976 /* Perform a partial register transfer using a read, modify, write
977 operation. */
978
979 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
980 void *buf);
981 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
982 const void *buf);
983
984 static enum register_status
985 regcache_xfer_part (struct regcache *regcache, int regnum,
986 int offset, int len, void *in, const void *out,
987 enum register_status (*read) (struct regcache *regcache,
988 int regnum,
989 gdb_byte *buf),
990 void (*write) (struct regcache *regcache, int regnum,
991 const gdb_byte *buf))
992 {
993 struct regcache_descr *descr = regcache->descr;
994 struct gdbarch *gdbarch = get_regcache_arch (regcache);
995 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
996
997 gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
998 gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
999 /* Something to do? */
1000 if (offset + len == 0)
1001 return REG_VALID;
1002 /* Read (when needed) ... */
1003 if (in != NULL
1004 || offset > 0
1005 || offset + len < descr->sizeof_register[regnum])
1006 {
1007 enum register_status status;
1008
1009 gdb_assert (read != NULL);
1010 status = read (regcache, regnum, reg);
1011 if (status != REG_VALID)
1012 return status;
1013 }
1014 /* ... modify ... */
1015 if (in != NULL)
1016 memcpy (in, reg + offset, len);
1017 if (out != NULL)
1018 memcpy (reg + offset, out, len);
1019 /* ... write (when needed). */
1020 if (out != NULL)
1021 {
1022 gdb_assert (write != NULL);
1023 write (regcache, regnum, reg);
1024 }
1025
1026 return REG_VALID;
1027 }
1028
1029 enum register_status
1030 regcache_raw_read_part (struct regcache *regcache, int regnum,
1031 int offset, int len, gdb_byte *buf)
1032 {
1033 struct regcache_descr *descr = regcache->descr;
1034
1035 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1036 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1037 regcache_raw_read, regcache_raw_write);
1038 }
1039
1040 void
1041 regcache_raw_write_part (struct regcache *regcache, int regnum,
1042 int offset, int len, const gdb_byte *buf)
1043 {
1044 struct regcache_descr *descr = regcache->descr;
1045
1046 gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1047 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1048 regcache_raw_read, regcache_raw_write);
1049 }
1050
1051 enum register_status
1052 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1053 int offset, int len, gdb_byte *buf)
1054 {
1055 struct regcache_descr *descr = regcache->descr;
1056
1057 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1058 return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1059 regcache_cooked_read, regcache_cooked_write);
1060 }
1061
1062 void
1063 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1064 int offset, int len, const gdb_byte *buf)
1065 {
1066 struct regcache_descr *descr = regcache->descr;
1067
1068 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1069 regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1070 regcache_cooked_read, regcache_cooked_write);
1071 }
1072
1073 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1074
1075 void
1076 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1077 {
1078 void *regbuf;
1079 size_t size;
1080
1081 gdb_assert (regcache != NULL);
1082 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1083 gdb_assert (!regcache->readonly_p);
1084
1085 regbuf = register_buffer (regcache, regnum);
1086 size = regcache->descr->sizeof_register[regnum];
1087
1088 if (buf)
1089 {
1090 memcpy (regbuf, buf, size);
1091 regcache->register_status[regnum] = REG_VALID;
1092 }
1093 else
1094 {
1095 /* This memset not strictly necessary, but better than garbage
1096 in case the register value manages to escape somewhere (due
1097 to a bug, no less). */
1098 memset (regbuf, 0, size);
1099 regcache->register_status[regnum] = REG_UNAVAILABLE;
1100 }
1101 }
1102
1103 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1104
1105 void
1106 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1107 {
1108 const void *regbuf;
1109 size_t size;
1110
1111 gdb_assert (regcache != NULL && buf != NULL);
1112 gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1113
1114 regbuf = register_buffer (regcache, regnum);
1115 size = regcache->descr->sizeof_register[regnum];
1116 memcpy (buf, regbuf, size);
1117 }
1118
1119 /* Transfer a single or all registers belonging to a certain register
1120 set to or from a buffer. This is the main worker function for
1121 regcache_supply_regset and regcache_collect_regset. */
1122
1123 static void
1124 regcache_transfer_regset (const struct regset *regset,
1125 const struct regcache *regcache,
1126 struct regcache *out_regcache,
1127 int regnum, const void *in_buf,
1128 void *out_buf, size_t size)
1129 {
1130 const struct regcache_map_entry *map;
1131 int offs = 0, count;
1132
1133 for (map = (const struct regcache_map_entry *) regset->regmap;
1134 (count = map->count) != 0;
1135 map++)
1136 {
1137 int regno = map->regno;
1138 int slot_size = map->size;
1139
1140 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1141 slot_size = regcache->descr->sizeof_register[regno];
1142
1143 if (regno == REGCACHE_MAP_SKIP
1144 || (regnum != -1
1145 && (regnum < regno || regnum >= regno + count)))
1146 offs += count * slot_size;
1147
1148 else if (regnum == -1)
1149 for (; count--; regno++, offs += slot_size)
1150 {
1151 if (offs + slot_size > size)
1152 break;
1153
1154 if (out_buf)
1155 regcache_raw_collect (regcache, regno,
1156 (gdb_byte *) out_buf + offs);
1157 else
1158 regcache_raw_supply (out_regcache, regno, in_buf
1159 ? (const gdb_byte *) in_buf + offs
1160 : NULL);
1161 }
1162 else
1163 {
1164 /* Transfer a single register and return. */
1165 offs += (regnum - regno) * slot_size;
1166 if (offs + slot_size > size)
1167 return;
1168
1169 if (out_buf)
1170 regcache_raw_collect (regcache, regnum,
1171 (gdb_byte *) out_buf + offs);
1172 else
1173 regcache_raw_supply (out_regcache, regnum, in_buf
1174 ? (const gdb_byte *) in_buf + offs
1175 : NULL);
1176 return;
1177 }
1178 }
1179 }
1180
1181 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1182 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1183 If BUF is NULL, set the register(s) to "unavailable" status. */
1184
1185 void
1186 regcache_supply_regset (const struct regset *regset,
1187 struct regcache *regcache,
1188 int regnum, const void *buf, size_t size)
1189 {
1190 regcache_transfer_regset (regset, regcache, regcache, regnum,
1191 buf, NULL, size);
1192 }
1193
1194 /* Collect register REGNUM from REGCACHE to BUF, using the register
1195 map in REGSET. If REGNUM is -1, do this for all registers in
1196 REGSET. */
1197
1198 void
1199 regcache_collect_regset (const struct regset *regset,
1200 const struct regcache *regcache,
1201 int regnum, void *buf, size_t size)
1202 {
1203 regcache_transfer_regset (regset, regcache, NULL, regnum,
1204 NULL, buf, size);
1205 }
1206
1207
1208 /* Special handling for register PC. */
1209
1210 CORE_ADDR
1211 regcache_read_pc (struct regcache *regcache)
1212 {
1213 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1214
1215 CORE_ADDR pc_val;
1216
1217 if (gdbarch_read_pc_p (gdbarch))
1218 pc_val = gdbarch_read_pc (gdbarch, regcache);
1219 /* Else use per-frame method on get_current_frame. */
1220 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1221 {
1222 ULONGEST raw_val;
1223
1224 if (regcache_cooked_read_unsigned (regcache,
1225 gdbarch_pc_regnum (gdbarch),
1226 &raw_val) == REG_UNAVAILABLE)
1227 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1228
1229 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1230 }
1231 else
1232 internal_error (__FILE__, __LINE__,
1233 _("regcache_read_pc: Unable to find PC"));
1234 return pc_val;
1235 }
1236
1237 void
1238 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1239 {
1240 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1241
1242 if (gdbarch_write_pc_p (gdbarch))
1243 gdbarch_write_pc (gdbarch, regcache, pc);
1244 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1245 regcache_cooked_write_unsigned (regcache,
1246 gdbarch_pc_regnum (gdbarch), pc);
1247 else
1248 internal_error (__FILE__, __LINE__,
1249 _("regcache_write_pc: Unable to update PC"));
1250
1251 /* Writing the PC (for instance, from "load") invalidates the
1252 current frame. */
1253 reinit_frame_cache ();
1254 }
1255
1256 void
1257 regcache_debug_print_register (const char *func, struct regcache *regcache,
1258 int regno)
1259 {
1260 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1261
1262 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1263 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1264 && gdbarch_register_name (gdbarch, regno) != NULL
1265 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1266 fprintf_unfiltered (gdb_stdlog, "(%s)",
1267 gdbarch_register_name (gdbarch, regno));
1268 else
1269 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1270 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1271 {
1272 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1273 int size = register_size (gdbarch, regno);
1274 gdb_byte *buf = register_buffer (regcache, regno);
1275
1276 fprintf_unfiltered (gdb_stdlog, " = ");
1277 for (int i = 0; i < size; i++)
1278 {
1279 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1280 }
1281 if (size <= sizeof (LONGEST))
1282 {
1283 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1284
1285 fprintf_unfiltered (gdb_stdlog, " %s %s",
1286 core_addr_to_string_nz (val), plongest (val));
1287 }
1288 }
1289 fprintf_unfiltered (gdb_stdlog, "\n");
1290 }
1291
1292 static void
1293 reg_flush_command (char *command, int from_tty)
1294 {
1295 /* Force-flush the register cache. */
1296 registers_changed ();
1297 if (from_tty)
1298 printf_filtered (_("Register cache flushed.\n"));
1299 }
1300
1301 enum regcache_dump_what
1302 {
1303 regcache_dump_none, regcache_dump_raw,
1304 regcache_dump_cooked, regcache_dump_groups,
1305 regcache_dump_remote
1306 };
1307
1308 static void
1309 regcache_dump (struct regcache *regcache, struct ui_file *file,
1310 enum regcache_dump_what what_to_dump)
1311 {
1312 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1313 struct gdbarch *gdbarch = regcache->descr->gdbarch;
1314 int regnum;
1315 int footnote_nr = 0;
1316 int footnote_register_size = 0;
1317 int footnote_register_offset = 0;
1318 int footnote_register_type_name_null = 0;
1319 long register_offset = 0;
1320 gdb_byte buf[MAX_REGISTER_SIZE];
1321
1322 #if 0
1323 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1324 regcache->descr->nr_raw_registers);
1325 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1326 regcache->descr->nr_cooked_registers);
1327 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1328 regcache->descr->sizeof_raw_registers);
1329 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1330 regcache->descr->sizeof_raw_register_status);
1331 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1332 gdbarch_num_regs (gdbarch));
1333 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1334 gdbarch_num_pseudo_regs (gdbarch));
1335 #endif
1336
1337 gdb_assert (regcache->descr->nr_cooked_registers
1338 == (gdbarch_num_regs (gdbarch)
1339 + gdbarch_num_pseudo_regs (gdbarch)));
1340
1341 for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1342 {
1343 /* Name. */
1344 if (regnum < 0)
1345 fprintf_unfiltered (file, " %-10s", "Name");
1346 else
1347 {
1348 const char *p = gdbarch_register_name (gdbarch, regnum);
1349
1350 if (p == NULL)
1351 p = "";
1352 else if (p[0] == '\0')
1353 p = "''";
1354 fprintf_unfiltered (file, " %-10s", p);
1355 }
1356
1357 /* Number. */
1358 if (regnum < 0)
1359 fprintf_unfiltered (file, " %4s", "Nr");
1360 else
1361 fprintf_unfiltered (file, " %4d", regnum);
1362
1363 /* Relative number. */
1364 if (regnum < 0)
1365 fprintf_unfiltered (file, " %4s", "Rel");
1366 else if (regnum < gdbarch_num_regs (gdbarch))
1367 fprintf_unfiltered (file, " %4d", regnum);
1368 else
1369 fprintf_unfiltered (file, " %4d",
1370 (regnum - gdbarch_num_regs (gdbarch)));
1371
1372 /* Offset. */
1373 if (regnum < 0)
1374 fprintf_unfiltered (file, " %6s ", "Offset");
1375 else
1376 {
1377 fprintf_unfiltered (file, " %6ld",
1378 regcache->descr->register_offset[regnum]);
1379 if (register_offset != regcache->descr->register_offset[regnum]
1380 || (regnum > 0
1381 && (regcache->descr->register_offset[regnum]
1382 != (regcache->descr->register_offset[regnum - 1]
1383 + regcache->descr->sizeof_register[regnum - 1])))
1384 )
1385 {
1386 if (!footnote_register_offset)
1387 footnote_register_offset = ++footnote_nr;
1388 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1389 }
1390 else
1391 fprintf_unfiltered (file, " ");
1392 register_offset = (regcache->descr->register_offset[regnum]
1393 + regcache->descr->sizeof_register[regnum]);
1394 }
1395
1396 /* Size. */
1397 if (regnum < 0)
1398 fprintf_unfiltered (file, " %5s ", "Size");
1399 else
1400 fprintf_unfiltered (file, " %5ld",
1401 regcache->descr->sizeof_register[regnum]);
1402
1403 /* Type. */
1404 {
1405 const char *t;
1406
1407 if (regnum < 0)
1408 t = "Type";
1409 else
1410 {
1411 static const char blt[] = "builtin_type";
1412
1413 t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1414 if (t == NULL)
1415 {
1416 char *n;
1417
1418 if (!footnote_register_type_name_null)
1419 footnote_register_type_name_null = ++footnote_nr;
1420 n = xstrprintf ("*%d", footnote_register_type_name_null);
1421 make_cleanup (xfree, n);
1422 t = n;
1423 }
1424 /* Chop a leading builtin_type. */
1425 if (startswith (t, blt))
1426 t += strlen (blt);
1427 }
1428 fprintf_unfiltered (file, " %-15s", t);
1429 }
1430
1431 /* Leading space always present. */
1432 fprintf_unfiltered (file, " ");
1433
1434 /* Value, raw. */
1435 if (what_to_dump == regcache_dump_raw)
1436 {
1437 if (regnum < 0)
1438 fprintf_unfiltered (file, "Raw value");
1439 else if (regnum >= regcache->descr->nr_raw_registers)
1440 fprintf_unfiltered (file, "<cooked>");
1441 else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1442 fprintf_unfiltered (file, "<invalid>");
1443 else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1444 fprintf_unfiltered (file, "<unavailable>");
1445 else
1446 {
1447 regcache_raw_read (regcache, regnum, buf);
1448 print_hex_chars (file, buf,
1449 regcache->descr->sizeof_register[regnum],
1450 gdbarch_byte_order (gdbarch));
1451 }
1452 }
1453
1454 /* Value, cooked. */
1455 if (what_to_dump == regcache_dump_cooked)
1456 {
1457 if (regnum < 0)
1458 fprintf_unfiltered (file, "Cooked value");
1459 else
1460 {
1461 enum register_status status;
1462
1463 status = regcache_cooked_read (regcache, regnum, buf);
1464 if (status == REG_UNKNOWN)
1465 fprintf_unfiltered (file, "<invalid>");
1466 else if (status == REG_UNAVAILABLE)
1467 fprintf_unfiltered (file, "<unavailable>");
1468 else
1469 print_hex_chars (file, buf,
1470 regcache->descr->sizeof_register[regnum],
1471 gdbarch_byte_order (gdbarch));
1472 }
1473 }
1474
1475 /* Group members. */
1476 if (what_to_dump == regcache_dump_groups)
1477 {
1478 if (regnum < 0)
1479 fprintf_unfiltered (file, "Groups");
1480 else
1481 {
1482 const char *sep = "";
1483 struct reggroup *group;
1484
1485 for (group = reggroup_next (gdbarch, NULL);
1486 group != NULL;
1487 group = reggroup_next (gdbarch, group))
1488 {
1489 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1490 {
1491 fprintf_unfiltered (file,
1492 "%s%s", sep, reggroup_name (group));
1493 sep = ",";
1494 }
1495 }
1496 }
1497 }
1498
1499 /* Remote packet configuration. */
1500 if (what_to_dump == regcache_dump_remote)
1501 {
1502 if (regnum < 0)
1503 {
1504 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1505 }
1506 else if (regnum < regcache->descr->nr_raw_registers)
1507 {
1508 int pnum, poffset;
1509
1510 if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1511 &pnum, &poffset))
1512 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1513 }
1514 }
1515
1516 fprintf_unfiltered (file, "\n");
1517 }
1518
1519 if (footnote_register_size)
1520 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1521 footnote_register_size);
1522 if (footnote_register_offset)
1523 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1524 footnote_register_offset);
1525 if (footnote_register_type_name_null)
1526 fprintf_unfiltered (file,
1527 "*%d: Register type's name NULL.\n",
1528 footnote_register_type_name_null);
1529 do_cleanups (cleanups);
1530 }
1531
1532 static void
1533 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1534 {
1535 if (args == NULL)
1536 regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1537 else
1538 {
1539 stdio_file file;
1540
1541 if (!file.open (args, "w"))
1542 perror_with_name (_("maintenance print architecture"));
1543 regcache_dump (get_current_regcache (), &file, what_to_dump);
1544 }
1545 }
1546
1547 static void
1548 maintenance_print_registers (char *args, int from_tty)
1549 {
1550 regcache_print (args, regcache_dump_none);
1551 }
1552
1553 static void
1554 maintenance_print_raw_registers (char *args, int from_tty)
1555 {
1556 regcache_print (args, regcache_dump_raw);
1557 }
1558
1559 static void
1560 maintenance_print_cooked_registers (char *args, int from_tty)
1561 {
1562 regcache_print (args, regcache_dump_cooked);
1563 }
1564
1565 static void
1566 maintenance_print_register_groups (char *args, int from_tty)
1567 {
1568 regcache_print (args, regcache_dump_groups);
1569 }
1570
1571 static void
1572 maintenance_print_remote_registers (char *args, int from_tty)
1573 {
1574 regcache_print (args, regcache_dump_remote);
1575 }
1576
1577 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1578
1579 void
1580 _initialize_regcache (void)
1581 {
1582 regcache_descr_handle
1583 = gdbarch_data_register_post_init (init_regcache_descr);
1584
1585 observer_attach_target_changed (regcache_observer_target_changed);
1586 observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1587
1588 add_com ("flushregs", class_maintenance, reg_flush_command,
1589 _("Force gdb to flush its register cache (maintainer command)"));
1590
1591 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1592 _("Print the internal register configuration.\n"
1593 "Takes an optional file parameter."), &maintenanceprintlist);
1594 add_cmd ("raw-registers", class_maintenance,
1595 maintenance_print_raw_registers,
1596 _("Print the internal register configuration "
1597 "including raw values.\n"
1598 "Takes an optional file parameter."), &maintenanceprintlist);
1599 add_cmd ("cooked-registers", class_maintenance,
1600 maintenance_print_cooked_registers,
1601 _("Print the internal register configuration "
1602 "including cooked values.\n"
1603 "Takes an optional file parameter."), &maintenanceprintlist);
1604 add_cmd ("register-groups", class_maintenance,
1605 maintenance_print_register_groups,
1606 _("Print the internal register configuration "
1607 "including each register's group.\n"
1608 "Takes an optional file parameter."),
1609 &maintenanceprintlist);
1610 add_cmd ("remote-registers", class_maintenance,
1611 maintenance_print_remote_registers, _("\
1612 Print the internal register configuration including each register's\n\
1613 remote register number and buffer offset in the g/G packets.\n\
1614 Takes an optional file parameter."),
1615 &maintenanceprintlist);
1616
1617 }
This page took 0.065578 seconds and 5 git commands to generate.