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