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