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