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