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