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