PR22220, BFD linker wrongly marks symbols as PREVAILING_DEF_IRONLY
[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 struct address_space *aspace;
443
444 /* For the benefit of "maint print registers" & co when debugging an
445 executable, allow dumping the regcache even when there is no
446 thread selected (target_thread_address_space internal-errors if
447 no address space is found). Note that normal user commands will
448 fail higher up on the call stack due to no
449 target_has_registers. */
450 aspace = (ptid_equal (null_ptid, ptid)
451 ? NULL
452 : target_thread_address_space (ptid));
453
454 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
455 }
456
457 static ptid_t current_thread_ptid;
458 static struct gdbarch *current_thread_arch;
459
460 struct regcache *
461 get_thread_regcache (ptid_t ptid)
462 {
463 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
464 {
465 current_thread_ptid = ptid;
466 current_thread_arch = target_thread_architecture (ptid);
467 }
468
469 return get_thread_arch_regcache (ptid, current_thread_arch);
470 }
471
472 struct regcache *
473 get_current_regcache (void)
474 {
475 return get_thread_regcache (inferior_ptid);
476 }
477
478 /* See common/common-regcache.h. */
479
480 struct regcache *
481 get_thread_regcache_for_ptid (ptid_t ptid)
482 {
483 return get_thread_regcache (ptid);
484 }
485
486 /* Observer for the target_changed event. */
487
488 static void
489 regcache_observer_target_changed (struct target_ops *target)
490 {
491 registers_changed ();
492 }
493
494 /* Update global variables old ptids to hold NEW_PTID if they were
495 holding OLD_PTID. */
496 void
497 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
498 {
499 for (auto &regcache : regcache::current_regcache)
500 {
501 if (ptid_equal (regcache->ptid (), old_ptid))
502 regcache->set_ptid (new_ptid);
503 }
504 }
505
506 /* Low level examining and depositing of registers.
507
508 The caller is responsible for making sure that the inferior is
509 stopped before calling the fetching routines, or it will get
510 garbage. (a change from GDB version 3, in which the caller got the
511 value from the last stop). */
512
513 /* REGISTERS_CHANGED ()
514
515 Indicate that registers may have changed, so invalidate the cache. */
516
517 void
518 registers_changed_ptid (ptid_t ptid)
519 {
520 for (auto oit = regcache::current_regcache.before_begin (),
521 it = std::next (oit);
522 it != regcache::current_regcache.end ();
523 )
524 {
525 if (ptid_match ((*it)->ptid (), ptid))
526 {
527 delete *it;
528 it = regcache::current_regcache.erase_after (oit);
529 }
530 else
531 oit = it++;
532 }
533
534 if (ptid_match (current_thread_ptid, ptid))
535 {
536 current_thread_ptid = null_ptid;
537 current_thread_arch = NULL;
538 }
539
540 if (ptid_match (inferior_ptid, ptid))
541 {
542 /* We just deleted the regcache of the current thread. Need to
543 forget about any frames we have cached, too. */
544 reinit_frame_cache ();
545 }
546 }
547
548 void
549 registers_changed (void)
550 {
551 registers_changed_ptid (minus_one_ptid);
552
553 /* Force cleanup of any alloca areas if using C alloca instead of
554 a builtin alloca. This particular call is used to clean up
555 areas allocated by low level target code which may build up
556 during lengthy interactions between gdb and the target before
557 gdb gives control to the user (ie watchpoints). */
558 alloca (0);
559 }
560
561 void
562 regcache_raw_update (struct regcache *regcache, int regnum)
563 {
564 gdb_assert (regcache != NULL);
565
566 regcache->raw_update (regnum);
567 }
568
569 void
570 regcache::raw_update (int regnum)
571 {
572 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
573
574 /* Make certain that the register cache is up-to-date with respect
575 to the current thread. This switching shouldn't be necessary
576 only there is still only one target side register cache. Sigh!
577 On the bright side, at least there is a regcache object. */
578
579 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
580 {
581 target_fetch_registers (this, regnum);
582
583 /* A number of targets can't access the whole set of raw
584 registers (because the debug API provides no means to get at
585 them). */
586 if (m_register_status[regnum] == REG_UNKNOWN)
587 m_register_status[regnum] = REG_UNAVAILABLE;
588 }
589 }
590
591 enum register_status
592 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
593 {
594 return regcache->raw_read (regnum, buf);
595 }
596
597 enum register_status
598 regcache::raw_read (int regnum, gdb_byte *buf)
599 {
600 gdb_assert (buf != NULL);
601 raw_update (regnum);
602
603 if (m_register_status[regnum] != REG_VALID)
604 memset (buf, 0, m_descr->sizeof_register[regnum]);
605 else
606 memcpy (buf, register_buffer (regnum),
607 m_descr->sizeof_register[regnum]);
608
609 return (enum register_status) m_register_status[regnum];
610 }
611
612 enum register_status
613 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
614 {
615 gdb_assert (regcache != NULL);
616 return regcache->raw_read (regnum, val);
617 }
618
619 template<typename T, typename>
620 enum register_status
621 regcache::raw_read (int regnum, T *val)
622 {
623 gdb_byte *buf;
624 enum register_status status;
625
626 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
627 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
628 status = raw_read (regnum, buf);
629 if (status == REG_VALID)
630 *val = extract_integer<T> (buf,
631 m_descr->sizeof_register[regnum],
632 gdbarch_byte_order (m_descr->gdbarch));
633 else
634 *val = 0;
635 return status;
636 }
637
638 enum register_status
639 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
640 ULONGEST *val)
641 {
642 gdb_assert (regcache != NULL);
643 return regcache->raw_read (regnum, val);
644 }
645
646 void
647 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
648 {
649 gdb_assert (regcache != NULL);
650 regcache->raw_write (regnum, val);
651 }
652
653 template<typename T, typename>
654 void
655 regcache::raw_write (int regnum, T val)
656 {
657 gdb_byte *buf;
658
659 gdb_assert (regnum >=0 && regnum < m_descr->nr_raw_registers);
660 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
661 store_integer (buf, m_descr->sizeof_register[regnum],
662 gdbarch_byte_order (m_descr->gdbarch), val);
663 raw_write (regnum, buf);
664 }
665
666 void
667 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
668 ULONGEST val)
669 {
670 gdb_assert (regcache != NULL);
671 regcache->raw_write (regnum, val);
672 }
673
674 LONGEST
675 regcache_raw_get_signed (struct regcache *regcache, int regnum)
676 {
677 LONGEST value;
678 enum register_status status;
679
680 status = regcache_raw_read_signed (regcache, regnum, &value);
681 if (status == REG_UNAVAILABLE)
682 throw_error (NOT_AVAILABLE_ERROR,
683 _("Register %d is not available"), regnum);
684 return value;
685 }
686
687 enum register_status
688 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
689 {
690 return regcache->cooked_read (regnum, buf);
691 }
692
693 enum register_status
694 regcache::cooked_read (int regnum, gdb_byte *buf)
695 {
696 gdb_assert (regnum >= 0);
697 gdb_assert (regnum < m_descr->nr_cooked_registers);
698 if (regnum < m_descr->nr_raw_registers)
699 return raw_read (regnum, buf);
700 else if (m_readonly_p
701 && m_register_status[regnum] != REG_UNKNOWN)
702 {
703 /* Read-only register cache, perhaps the cooked value was
704 cached? */
705 if (m_register_status[regnum] == REG_VALID)
706 memcpy (buf, register_buffer (regnum),
707 m_descr->sizeof_register[regnum]);
708 else
709 memset (buf, 0, m_descr->sizeof_register[regnum]);
710
711 return (enum register_status) m_register_status[regnum];
712 }
713 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
714 {
715 struct value *mark, *computed;
716 enum register_status result = REG_VALID;
717
718 mark = value_mark ();
719
720 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
721 this, regnum);
722 if (value_entirely_available (computed))
723 memcpy (buf, value_contents_raw (computed),
724 m_descr->sizeof_register[regnum]);
725 else
726 {
727 memset (buf, 0, m_descr->sizeof_register[regnum]);
728 result = REG_UNAVAILABLE;
729 }
730
731 value_free_to_mark (mark);
732
733 return result;
734 }
735 else
736 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
737 regnum, buf);
738 }
739
740 struct value *
741 regcache_cooked_read_value (struct regcache *regcache, int regnum)
742 {
743 return regcache->cooked_read_value (regnum);
744 }
745
746 struct value *
747 regcache::cooked_read_value (int regnum)
748 {
749 gdb_assert (regnum >= 0);
750 gdb_assert (regnum < m_descr->nr_cooked_registers);
751
752 if (regnum < m_descr->nr_raw_registers
753 || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN)
754 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
755 {
756 struct value *result;
757
758 result = allocate_value (register_type (m_descr->gdbarch, regnum));
759 VALUE_LVAL (result) = lval_register;
760 VALUE_REGNUM (result) = regnum;
761
762 /* It is more efficient in general to do this delegation in this
763 direction than in the other one, even though the value-based
764 API is preferred. */
765 if (cooked_read (regnum,
766 value_contents_raw (result)) == REG_UNAVAILABLE)
767 mark_value_bytes_unavailable (result, 0,
768 TYPE_LENGTH (value_type (result)));
769
770 return result;
771 }
772 else
773 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
774 this, regnum);
775 }
776
777 enum register_status
778 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
779 LONGEST *val)
780 {
781 gdb_assert (regcache != NULL);
782 return regcache->cooked_read (regnum, val);
783 }
784
785 template<typename T, typename>
786 enum register_status
787 regcache::cooked_read (int regnum, T *val)
788 {
789 enum register_status status;
790 gdb_byte *buf;
791
792 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
793 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
794 status = cooked_read (regnum, buf);
795 if (status == REG_VALID)
796 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
797 gdbarch_byte_order (m_descr->gdbarch));
798 else
799 *val = 0;
800 return status;
801 }
802
803 enum register_status
804 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
805 ULONGEST *val)
806 {
807 gdb_assert (regcache != NULL);
808 return regcache->cooked_read (regnum, val);
809 }
810
811 void
812 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
813 LONGEST val)
814 {
815 gdb_assert (regcache != NULL);
816 regcache->cooked_write (regnum, val);
817 }
818
819 template<typename T, typename>
820 void
821 regcache::cooked_write (int regnum, T val)
822 {
823 gdb_byte *buf;
824
825 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
826 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
827 store_integer (buf, m_descr->sizeof_register[regnum],
828 gdbarch_byte_order (m_descr->gdbarch), val);
829 cooked_write (regnum, buf);
830 }
831
832 void
833 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
834 ULONGEST val)
835 {
836 gdb_assert (regcache != NULL);
837 regcache->cooked_write (regnum, val);
838 }
839
840 /* See regcache.h. */
841
842 void
843 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
844 const gdb_byte *buf)
845 {
846 regcache->raw_set_cached_value (regnum, buf);
847 }
848
849 void
850 regcache::raw_set_cached_value (int regnum, const gdb_byte *buf)
851 {
852 memcpy (register_buffer (regnum), buf,
853 m_descr->sizeof_register[regnum]);
854 m_register_status[regnum] = REG_VALID;
855 }
856
857 void
858 regcache_raw_write (struct regcache *regcache, int regnum,
859 const gdb_byte *buf)
860 {
861 gdb_assert (regcache != NULL && buf != NULL);
862 regcache->raw_write (regnum, buf);
863 }
864
865 void
866 regcache::raw_write (int regnum, const gdb_byte *buf)
867 {
868
869 gdb_assert (buf != NULL);
870 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
871 gdb_assert (!m_readonly_p);
872
873 /* On the sparc, writing %g0 is a no-op, so we don't even want to
874 change the registers array if something writes to this register. */
875 if (gdbarch_cannot_store_register (arch (), regnum))
876 return;
877
878 /* If we have a valid copy of the register, and new value == old
879 value, then don't bother doing the actual store. */
880 if (get_register_status (regnum) == REG_VALID
881 && (memcmp (register_buffer (regnum), buf,
882 m_descr->sizeof_register[regnum]) == 0))
883 return;
884
885 target_prepare_to_store (this);
886 raw_set_cached_value (regnum, buf);
887
888 /* Invalidate the register after it is written, in case of a
889 failure. */
890 regcache_invalidator invalidator (this, regnum);
891
892 target_store_registers (this, regnum);
893
894 /* The target did not throw an error so we can discard invalidating
895 the register. */
896 invalidator.release ();
897 }
898
899 void
900 regcache_cooked_write (struct regcache *regcache, int regnum,
901 const gdb_byte *buf)
902 {
903 regcache->cooked_write (regnum, buf);
904 }
905
906 void
907 regcache::cooked_write (int regnum, const gdb_byte *buf)
908 {
909 gdb_assert (regnum >= 0);
910 gdb_assert (regnum < m_descr->nr_cooked_registers);
911 if (regnum < m_descr->nr_raw_registers)
912 raw_write (regnum, buf);
913 else
914 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
915 regnum, buf);
916 }
917
918 /* Perform a partial register transfer using a read, modify, write
919 operation. */
920
921 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
922 void *buf);
923 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
924 const void *buf);
925
926 enum register_status
927 regcache::xfer_part (int regnum, int offset, int len, void *in,
928 const void *out,
929 enum register_status (*read) (struct regcache *regcache,
930 int regnum,
931 gdb_byte *buf),
932 void (*write) (struct regcache *regcache, int regnum,
933 const gdb_byte *buf))
934 {
935 struct gdbarch *gdbarch = arch ();
936 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
937
938 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
939 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
940 /* Something to do? */
941 if (offset + len == 0)
942 return REG_VALID;
943 /* Read (when needed) ... */
944 if (in != NULL
945 || offset > 0
946 || offset + len < m_descr->sizeof_register[regnum])
947 {
948 enum register_status status;
949
950 gdb_assert (read != NULL);
951 status = read (this, regnum, reg);
952 if (status != REG_VALID)
953 return status;
954 }
955 /* ... modify ... */
956 if (in != NULL)
957 memcpy (in, reg + offset, len);
958 if (out != NULL)
959 memcpy (reg + offset, out, len);
960 /* ... write (when needed). */
961 if (out != NULL)
962 {
963 gdb_assert (write != NULL);
964 write (this, regnum, reg);
965 }
966
967 return REG_VALID;
968 }
969
970 enum register_status
971 regcache_raw_read_part (struct regcache *regcache, int regnum,
972 int offset, int len, gdb_byte *buf)
973 {
974 return regcache->raw_read_part (regnum, offset, len, buf);
975 }
976
977 enum register_status
978 regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
979 {
980 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
981 return xfer_part (regnum, offset, len, buf, NULL,
982 regcache_raw_read, regcache_raw_write);
983 }
984
985 void
986 regcache_raw_write_part (struct regcache *regcache, int regnum,
987 int offset, int len, const gdb_byte *buf)
988 {
989 regcache->raw_write_part (regnum, offset, len, buf);
990 }
991
992 void
993 regcache::raw_write_part (int regnum, int offset, int len,
994 const gdb_byte *buf)
995 {
996 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
997 xfer_part (regnum, offset, len, NULL, buf, regcache_raw_read,
998 regcache_raw_write);
999 }
1000
1001 enum register_status
1002 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1003 int offset, int len, gdb_byte *buf)
1004 {
1005 return regcache->cooked_read_part (regnum, offset, len, buf);
1006 }
1007
1008
1009 enum register_status
1010 regcache::cooked_read_part (int regnum, int offset, int len, gdb_byte *buf)
1011 {
1012 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1013 return xfer_part (regnum, offset, len, buf, NULL,
1014 regcache_cooked_read, regcache_cooked_write);
1015 }
1016
1017 void
1018 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1019 int offset, int len, const gdb_byte *buf)
1020 {
1021 regcache->cooked_write_part (regnum, offset, len, buf);
1022 }
1023
1024 void
1025 regcache::cooked_write_part (int regnum, int offset, int len,
1026 const gdb_byte *buf)
1027 {
1028 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1029 xfer_part (regnum, offset, len, NULL, buf,
1030 regcache_cooked_read, regcache_cooked_write);
1031 }
1032
1033 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1034
1035 void
1036 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1037 {
1038 gdb_assert (regcache != NULL);
1039 regcache->raw_supply (regnum, buf);
1040 }
1041
1042 void
1043 regcache::raw_supply (int regnum, const void *buf)
1044 {
1045 void *regbuf;
1046 size_t size;
1047
1048 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1049 gdb_assert (!m_readonly_p);
1050
1051 regbuf = register_buffer (regnum);
1052 size = m_descr->sizeof_register[regnum];
1053
1054 if (buf)
1055 {
1056 memcpy (regbuf, buf, size);
1057 m_register_status[regnum] = REG_VALID;
1058 }
1059 else
1060 {
1061 /* This memset not strictly necessary, but better than garbage
1062 in case the register value manages to escape somewhere (due
1063 to a bug, no less). */
1064 memset (regbuf, 0, size);
1065 m_register_status[regnum] = REG_UNAVAILABLE;
1066 }
1067 }
1068
1069 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1070 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1071 the register size is greater than ADDR_LEN, then the integer will be sign or
1072 zero extended. If the register size is smaller than the integer, then the
1073 most significant bytes of the integer will be truncated. */
1074
1075 void
1076 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1077 bool is_signed)
1078 {
1079 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1080 gdb_byte *regbuf;
1081 size_t regsize;
1082
1083 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1084 gdb_assert (!m_readonly_p);
1085
1086 regbuf = register_buffer (regnum);
1087 regsize = m_descr->sizeof_register[regnum];
1088
1089 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1090 byte_order);
1091 m_register_status[regnum] = REG_VALID;
1092 }
1093
1094 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1095 as calling raw_supply with NULL (which will set the state to
1096 unavailable). */
1097
1098 void
1099 regcache::raw_supply_zeroed (int regnum)
1100 {
1101 void *regbuf;
1102 size_t size;
1103
1104 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1105 gdb_assert (!m_readonly_p);
1106
1107 regbuf = register_buffer (regnum);
1108 size = m_descr->sizeof_register[regnum];
1109
1110 memset (regbuf, 0, size);
1111 m_register_status[regnum] = REG_VALID;
1112 }
1113
1114 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1115
1116 void
1117 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1118 {
1119 gdb_assert (regcache != NULL && buf != NULL);
1120 regcache->raw_collect (regnum, buf);
1121 }
1122
1123 void
1124 regcache::raw_collect (int regnum, void *buf) const
1125 {
1126 const void *regbuf;
1127 size_t size;
1128
1129 gdb_assert (buf != NULL);
1130 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1131
1132 regbuf = register_buffer (regnum);
1133 size = m_descr->sizeof_register[regnum];
1134 memcpy (buf, regbuf, size);
1135 }
1136
1137 /* Transfer a single or all registers belonging to a certain register
1138 set to or from a buffer. This is the main worker function for
1139 regcache_supply_regset and regcache_collect_regset. */
1140
1141 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1142 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1143 If ADDR_LEN is greater than the register size, then the integer will be sign
1144 or zero extended. If ADDR_LEN is smaller than the register size, then the
1145 most significant bytes of the integer will be truncated. */
1146
1147 void
1148 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1149 bool is_signed) const
1150 {
1151 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1152 const gdb_byte *regbuf;
1153 size_t regsize;
1154
1155 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1156
1157 regbuf = register_buffer (regnum);
1158 regsize = m_descr->sizeof_register[regnum];
1159
1160 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1161 byte_order);
1162 }
1163
1164 void
1165 regcache::transfer_regset (const struct regset *regset,
1166 struct regcache *out_regcache,
1167 int regnum, const void *in_buf,
1168 void *out_buf, size_t size) const
1169 {
1170 const struct regcache_map_entry *map;
1171 int offs = 0, count;
1172
1173 for (map = (const struct regcache_map_entry *) regset->regmap;
1174 (count = map->count) != 0;
1175 map++)
1176 {
1177 int regno = map->regno;
1178 int slot_size = map->size;
1179
1180 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1181 slot_size = m_descr->sizeof_register[regno];
1182
1183 if (regno == REGCACHE_MAP_SKIP
1184 || (regnum != -1
1185 && (regnum < regno || regnum >= regno + count)))
1186 offs += count * slot_size;
1187
1188 else if (regnum == -1)
1189 for (; count--; regno++, offs += slot_size)
1190 {
1191 if (offs + slot_size > size)
1192 break;
1193
1194 if (out_buf)
1195 raw_collect (regno, (gdb_byte *) out_buf + offs);
1196 else
1197 out_regcache->raw_supply (regno, in_buf
1198 ? (const gdb_byte *) in_buf + offs
1199 : NULL);
1200 }
1201 else
1202 {
1203 /* Transfer a single register and return. */
1204 offs += (regnum - regno) * slot_size;
1205 if (offs + slot_size > size)
1206 return;
1207
1208 if (out_buf)
1209 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1210 else
1211 out_regcache->raw_supply (regnum, in_buf
1212 ? (const gdb_byte *) in_buf + offs
1213 : NULL);
1214 return;
1215 }
1216 }
1217 }
1218
1219 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1220 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1221 If BUF is NULL, set the register(s) to "unavailable" status. */
1222
1223 void
1224 regcache_supply_regset (const struct regset *regset,
1225 struct regcache *regcache,
1226 int regnum, const void *buf, size_t size)
1227 {
1228 regcache->supply_regset (regset, regnum, buf, size);
1229 }
1230
1231 void
1232 regcache::supply_regset (const struct regset *regset,
1233 int regnum, const void *buf, size_t size)
1234 {
1235 transfer_regset (regset, this, regnum, buf, NULL, size);
1236 }
1237
1238 /* Collect register REGNUM from REGCACHE to BUF, using the register
1239 map in REGSET. If REGNUM is -1, do this for all registers in
1240 REGSET. */
1241
1242 void
1243 regcache_collect_regset (const struct regset *regset,
1244 const struct regcache *regcache,
1245 int regnum, void *buf, size_t size)
1246 {
1247 regcache->collect_regset (regset, regnum, buf, size);
1248 }
1249
1250 void
1251 regcache::collect_regset (const struct regset *regset,
1252 int regnum, void *buf, size_t size) const
1253 {
1254 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1255 }
1256
1257
1258 /* Special handling for register PC. */
1259
1260 CORE_ADDR
1261 regcache_read_pc (struct regcache *regcache)
1262 {
1263 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1264
1265 CORE_ADDR pc_val;
1266
1267 if (gdbarch_read_pc_p (gdbarch))
1268 pc_val = gdbarch_read_pc (gdbarch, regcache);
1269 /* Else use per-frame method on get_current_frame. */
1270 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1271 {
1272 ULONGEST raw_val;
1273
1274 if (regcache_cooked_read_unsigned (regcache,
1275 gdbarch_pc_regnum (gdbarch),
1276 &raw_val) == REG_UNAVAILABLE)
1277 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1278
1279 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1280 }
1281 else
1282 internal_error (__FILE__, __LINE__,
1283 _("regcache_read_pc: Unable to find PC"));
1284 return pc_val;
1285 }
1286
1287 void
1288 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1289 {
1290 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1291
1292 if (gdbarch_write_pc_p (gdbarch))
1293 gdbarch_write_pc (gdbarch, regcache, pc);
1294 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1295 regcache_cooked_write_unsigned (regcache,
1296 gdbarch_pc_regnum (gdbarch), pc);
1297 else
1298 internal_error (__FILE__, __LINE__,
1299 _("regcache_write_pc: Unable to update PC"));
1300
1301 /* Writing the PC (for instance, from "load") invalidates the
1302 current frame. */
1303 reinit_frame_cache ();
1304 }
1305
1306 void
1307 regcache::debug_print_register (const char *func, int regno)
1308 {
1309 struct gdbarch *gdbarch = arch ();
1310
1311 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1312 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1313 && gdbarch_register_name (gdbarch, regno) != NULL
1314 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1315 fprintf_unfiltered (gdb_stdlog, "(%s)",
1316 gdbarch_register_name (gdbarch, regno));
1317 else
1318 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1319 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1320 {
1321 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1322 int size = register_size (gdbarch, regno);
1323 gdb_byte *buf = register_buffer (regno);
1324
1325 fprintf_unfiltered (gdb_stdlog, " = ");
1326 for (int i = 0; i < size; i++)
1327 {
1328 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1329 }
1330 if (size <= sizeof (LONGEST))
1331 {
1332 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1333
1334 fprintf_unfiltered (gdb_stdlog, " %s %s",
1335 core_addr_to_string_nz (val), plongest (val));
1336 }
1337 }
1338 fprintf_unfiltered (gdb_stdlog, "\n");
1339 }
1340
1341 static void
1342 reg_flush_command (char *command, int from_tty)
1343 {
1344 /* Force-flush the register cache. */
1345 registers_changed ();
1346 if (from_tty)
1347 printf_filtered (_("Register cache flushed.\n"));
1348 }
1349
1350 void
1351 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1352 {
1353 struct gdbarch *gdbarch = m_descr->gdbarch;
1354 int regnum;
1355 int footnote_nr = 0;
1356 int footnote_register_size = 0;
1357 int footnote_register_offset = 0;
1358 int footnote_register_type_name_null = 0;
1359 long register_offset = 0;
1360
1361 #if 0
1362 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1363 m_descr->nr_raw_registers);
1364 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1365 m_descr->nr_cooked_registers);
1366 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1367 m_descr->sizeof_raw_registers);
1368 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1369 m_descr->sizeof_raw_register_status);
1370 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1371 gdbarch_num_regs (gdbarch));
1372 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1373 gdbarch_num_pseudo_regs (gdbarch));
1374 #endif
1375
1376 gdb_assert (m_descr->nr_cooked_registers
1377 == (gdbarch_num_regs (gdbarch)
1378 + gdbarch_num_pseudo_regs (gdbarch)));
1379
1380 for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1381 {
1382 /* Name. */
1383 if (regnum < 0)
1384 fprintf_unfiltered (file, " %-10s", "Name");
1385 else
1386 {
1387 const char *p = gdbarch_register_name (gdbarch, regnum);
1388
1389 if (p == NULL)
1390 p = "";
1391 else if (p[0] == '\0')
1392 p = "''";
1393 fprintf_unfiltered (file, " %-10s", p);
1394 }
1395
1396 /* Number. */
1397 if (regnum < 0)
1398 fprintf_unfiltered (file, " %4s", "Nr");
1399 else
1400 fprintf_unfiltered (file, " %4d", regnum);
1401
1402 /* Relative number. */
1403 if (regnum < 0)
1404 fprintf_unfiltered (file, " %4s", "Rel");
1405 else if (regnum < gdbarch_num_regs (gdbarch))
1406 fprintf_unfiltered (file, " %4d", regnum);
1407 else
1408 fprintf_unfiltered (file, " %4d",
1409 (regnum - gdbarch_num_regs (gdbarch)));
1410
1411 /* Offset. */
1412 if (regnum < 0)
1413 fprintf_unfiltered (file, " %6s ", "Offset");
1414 else
1415 {
1416 fprintf_unfiltered (file, " %6ld",
1417 m_descr->register_offset[regnum]);
1418 if (register_offset != m_descr->register_offset[regnum]
1419 || (regnum > 0
1420 && (m_descr->register_offset[regnum]
1421 != (m_descr->register_offset[regnum - 1]
1422 + m_descr->sizeof_register[regnum - 1])))
1423 )
1424 {
1425 if (!footnote_register_offset)
1426 footnote_register_offset = ++footnote_nr;
1427 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1428 }
1429 else
1430 fprintf_unfiltered (file, " ");
1431 register_offset = (m_descr->register_offset[regnum]
1432 + m_descr->sizeof_register[regnum]);
1433 }
1434
1435 /* Size. */
1436 if (regnum < 0)
1437 fprintf_unfiltered (file, " %5s ", "Size");
1438 else
1439 fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1440
1441 /* Type. */
1442 {
1443 const char *t;
1444 std::string name_holder;
1445
1446 if (regnum < 0)
1447 t = "Type";
1448 else
1449 {
1450 static const char blt[] = "builtin_type";
1451
1452 t = TYPE_NAME (register_type (arch (), regnum));
1453 if (t == NULL)
1454 {
1455 if (!footnote_register_type_name_null)
1456 footnote_register_type_name_null = ++footnote_nr;
1457 name_holder = string_printf ("*%d",
1458 footnote_register_type_name_null);
1459 t = name_holder.c_str ();
1460 }
1461 /* Chop a leading builtin_type. */
1462 if (startswith (t, blt))
1463 t += strlen (blt);
1464 }
1465 fprintf_unfiltered (file, " %-15s", t);
1466 }
1467
1468 /* Leading space always present. */
1469 fprintf_unfiltered (file, " ");
1470
1471 /* Value, raw. */
1472 if (what_to_dump == regcache_dump_raw)
1473 {
1474 if (regnum < 0)
1475 fprintf_unfiltered (file, "Raw value");
1476 else if (regnum >= m_descr->nr_raw_registers)
1477 fprintf_unfiltered (file, "<cooked>");
1478 else if (get_register_status (regnum) == REG_UNKNOWN)
1479 fprintf_unfiltered (file, "<invalid>");
1480 else if (get_register_status (regnum) == REG_UNAVAILABLE)
1481 fprintf_unfiltered (file, "<unavailable>");
1482 else
1483 {
1484 raw_update (regnum);
1485 print_hex_chars (file, register_buffer (regnum),
1486 m_descr->sizeof_register[regnum],
1487 gdbarch_byte_order (gdbarch), true);
1488 }
1489 }
1490
1491 /* Value, cooked. */
1492 if (what_to_dump == regcache_dump_cooked)
1493 {
1494 if (regnum < 0)
1495 fprintf_unfiltered (file, "Cooked value");
1496 else
1497 {
1498 const gdb_byte *buf = NULL;
1499 enum register_status status;
1500 struct value *value = NULL;
1501
1502 if (regnum < m_descr->nr_raw_registers)
1503 {
1504 raw_update (regnum);
1505 status = get_register_status (regnum);
1506 buf = register_buffer (regnum);
1507 }
1508 else
1509 {
1510 value = cooked_read_value (regnum);
1511
1512 if (!value_optimized_out (value)
1513 && value_entirely_available (value))
1514 {
1515 status = REG_VALID;
1516 buf = value_contents_all (value);
1517 }
1518 else
1519 status = REG_UNAVAILABLE;
1520 }
1521
1522 if (status == REG_UNKNOWN)
1523 fprintf_unfiltered (file, "<invalid>");
1524 else if (status == REG_UNAVAILABLE)
1525 fprintf_unfiltered (file, "<unavailable>");
1526 else
1527 print_hex_chars (file, buf,
1528 m_descr->sizeof_register[regnum],
1529 gdbarch_byte_order (gdbarch), true);
1530
1531 if (value != NULL)
1532 {
1533 release_value (value);
1534 value_free (value);
1535 }
1536 }
1537 }
1538
1539 /* Group members. */
1540 if (what_to_dump == regcache_dump_groups)
1541 {
1542 if (regnum < 0)
1543 fprintf_unfiltered (file, "Groups");
1544 else
1545 {
1546 const char *sep = "";
1547 struct reggroup *group;
1548
1549 for (group = reggroup_next (gdbarch, NULL);
1550 group != NULL;
1551 group = reggroup_next (gdbarch, group))
1552 {
1553 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1554 {
1555 fprintf_unfiltered (file,
1556 "%s%s", sep, reggroup_name (group));
1557 sep = ",";
1558 }
1559 }
1560 }
1561 }
1562
1563 /* Remote packet configuration. */
1564 if (what_to_dump == regcache_dump_remote)
1565 {
1566 if (regnum < 0)
1567 {
1568 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1569 }
1570 else if (regnum < m_descr->nr_raw_registers)
1571 {
1572 int pnum, poffset;
1573
1574 if (remote_register_number_and_offset (arch (), regnum,
1575 &pnum, &poffset))
1576 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1577 }
1578 }
1579
1580 fprintf_unfiltered (file, "\n");
1581 }
1582
1583 if (footnote_register_size)
1584 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1585 footnote_register_size);
1586 if (footnote_register_offset)
1587 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1588 footnote_register_offset);
1589 if (footnote_register_type_name_null)
1590 fprintf_unfiltered (file,
1591 "*%d: Register type's name NULL.\n",
1592 footnote_register_type_name_null);
1593 }
1594
1595 static void
1596 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
1597 {
1598 if (args == NULL)
1599 get_current_regcache ()->dump (gdb_stdout, what_to_dump);
1600 else
1601 {
1602 stdio_file file;
1603
1604 if (!file.open (args, "w"))
1605 perror_with_name (_("maintenance print architecture"));
1606 get_current_regcache ()->dump (&file, what_to_dump);
1607 }
1608 }
1609
1610 static void
1611 maintenance_print_registers (const char *args, int from_tty)
1612 {
1613 regcache_print (args, regcache_dump_none);
1614 }
1615
1616 static void
1617 maintenance_print_raw_registers (const char *args, int from_tty)
1618 {
1619 regcache_print (args, regcache_dump_raw);
1620 }
1621
1622 static void
1623 maintenance_print_cooked_registers (const char *args, int from_tty)
1624 {
1625 regcache_print (args, regcache_dump_cooked);
1626 }
1627
1628 static void
1629 maintenance_print_register_groups (const char *args, int from_tty)
1630 {
1631 regcache_print (args, regcache_dump_groups);
1632 }
1633
1634 static void
1635 maintenance_print_remote_registers (const char *args, int from_tty)
1636 {
1637 regcache_print (args, regcache_dump_remote);
1638 }
1639
1640 #if GDB_SELF_TEST
1641 #include "selftest.h"
1642
1643 namespace selftests {
1644
1645 class regcache_access : public regcache
1646 {
1647 public:
1648
1649 /* Return the number of elements in current_regcache. */
1650
1651 static size_t
1652 current_regcache_size ()
1653 {
1654 return std::distance (regcache::current_regcache.begin (),
1655 regcache::current_regcache.end ());
1656 }
1657 };
1658
1659 static void
1660 current_regcache_test (void)
1661 {
1662 /* It is empty at the start. */
1663 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1664
1665 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1666
1667 /* Get regcache from ptid1, a new regcache is added to
1668 current_regcache. */
1669 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1670 target_gdbarch (),
1671 NULL);
1672
1673 SELF_CHECK (regcache != NULL);
1674 SELF_CHECK (regcache->ptid () == ptid1);
1675 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1676
1677 /* Get regcache from ptid2, a new regcache is added to
1678 current_regcache. */
1679 regcache = get_thread_arch_aspace_regcache (ptid2,
1680 target_gdbarch (),
1681 NULL);
1682 SELF_CHECK (regcache != NULL);
1683 SELF_CHECK (regcache->ptid () == ptid2);
1684 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1685
1686 /* Get regcache from ptid3, a new regcache is added to
1687 current_regcache. */
1688 regcache = get_thread_arch_aspace_regcache (ptid3,
1689 target_gdbarch (),
1690 NULL);
1691 SELF_CHECK (regcache != NULL);
1692 SELF_CHECK (regcache->ptid () == ptid3);
1693 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1694
1695 /* Get regcache from ptid2 again, nothing is added to
1696 current_regcache. */
1697 regcache = get_thread_arch_aspace_regcache (ptid2,
1698 target_gdbarch (),
1699 NULL);
1700 SELF_CHECK (regcache != NULL);
1701 SELF_CHECK (regcache->ptid () == ptid2);
1702 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1703
1704 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1705 current_regcache. */
1706 registers_changed_ptid (ptid2);
1707 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1708 }
1709
1710 } // namespace selftests
1711 #endif /* GDB_SELF_TEST */
1712
1713 void
1714 _initialize_regcache (void)
1715 {
1716 regcache_descr_handle
1717 = gdbarch_data_register_post_init (init_regcache_descr);
1718
1719 observer_attach_target_changed (regcache_observer_target_changed);
1720 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
1721
1722 add_com ("flushregs", class_maintenance, reg_flush_command,
1723 _("Force gdb to flush its register cache (maintainer command)"));
1724
1725 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1726 _("Print the internal register configuration.\n"
1727 "Takes an optional file parameter."), &maintenanceprintlist);
1728 add_cmd ("raw-registers", class_maintenance,
1729 maintenance_print_raw_registers,
1730 _("Print the internal register configuration "
1731 "including raw values.\n"
1732 "Takes an optional file parameter."), &maintenanceprintlist);
1733 add_cmd ("cooked-registers", class_maintenance,
1734 maintenance_print_cooked_registers,
1735 _("Print the internal register configuration "
1736 "including cooked values.\n"
1737 "Takes an optional file parameter."), &maintenanceprintlist);
1738 add_cmd ("register-groups", class_maintenance,
1739 maintenance_print_register_groups,
1740 _("Print the internal register configuration "
1741 "including each register's group.\n"
1742 "Takes an optional file parameter."),
1743 &maintenanceprintlist);
1744 add_cmd ("remote-registers", class_maintenance,
1745 maintenance_print_remote_registers, _("\
1746 Print the internal register configuration including each register's\n\
1747 remote register number and buffer offset in the g/G packets.\n\
1748 Takes an optional file parameter."),
1749 &maintenanceprintlist);
1750
1751 #if GDB_SELF_TEST
1752 selftests::register_test ("current_regcache", selftests::current_regcache_test);
1753 #endif
1754 }