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