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