gdb: split regcaches management selftest
[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 <unordered_map>
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 /* Type to map a ptid to a list of regcaches (one thread may have multiple
317 regcaches, associated to different gdbarches). */
318
319 using ptid_regcache_map
320 = std::unordered_multimap<ptid_t, regcache_up, hash_ptid>;
321
322 /* Type to map a target to a ptid_regcache_map, holding the regcaches for the
323 threads defined by that target. */
324
325 using target_ptid_regcache_map
326 = std::unordered_map<process_stratum_target *, ptid_regcache_map>;
327
328 /* Global structure containing the existing regcaches. */
329
330 /* NOTE: this is a write-through cache. There is no "dirty" bit for
331 recording if the register values have been changed (eg. by the
332 user). Therefore all registers must be written back to the
333 target when appropriate. */
334 static target_ptid_regcache_map regcaches;
335
336 struct regcache *
337 get_thread_arch_aspace_regcache (process_stratum_target *target,
338 ptid_t ptid, gdbarch *arch,
339 struct address_space *aspace)
340 {
341 gdb_assert (target != nullptr);
342
343 /* Find the ptid -> regcache map for this target. */
344 auto &ptid_regc_map = regcaches[target];
345
346 /* Check first if a regcache for this arch already exists. */
347 auto range = ptid_regc_map.equal_range (ptid);
348 for (auto it = range.first; it != range.second; ++it)
349 {
350 if (it->second->arch () == arch)
351 return it->second.get ();
352 }
353
354 /* It does not exist, create it. */
355 regcache *new_regcache = new regcache (target, arch, aspace);
356 new_regcache->set_ptid (ptid);
357 /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
358 constructor explictly instead of implicitly. */
359 ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
360
361 return new_regcache;
362 }
363
364 struct regcache *
365 get_thread_arch_regcache (process_stratum_target *target, ptid_t ptid,
366 struct gdbarch *gdbarch)
367 {
368 scoped_restore_current_inferior restore_current_inferior;
369 set_current_inferior (find_inferior_ptid (target, ptid));
370 address_space *aspace = target_thread_address_space (ptid);
371
372 return get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
373 }
374
375 static process_stratum_target *current_thread_target;
376 static ptid_t current_thread_ptid;
377 static struct gdbarch *current_thread_arch;
378
379 struct regcache *
380 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
381 {
382 if (!current_thread_arch
383 || target != current_thread_target
384 || current_thread_ptid != ptid)
385 {
386 gdb_assert (ptid != null_ptid);
387
388 current_thread_ptid = ptid;
389 current_thread_target = target;
390
391 scoped_restore_current_inferior restore_current_inferior;
392 set_current_inferior (find_inferior_ptid (target, ptid));
393 current_thread_arch = target_thread_architecture (ptid);
394 }
395
396 return get_thread_arch_regcache (target, ptid, current_thread_arch);
397 }
398
399 /* See regcache.h. */
400
401 struct regcache *
402 get_thread_regcache (thread_info *thread)
403 {
404 return get_thread_regcache (thread->inf->process_target (),
405 thread->ptid);
406 }
407
408 struct regcache *
409 get_current_regcache (void)
410 {
411 return get_thread_regcache (inferior_thread ());
412 }
413
414 /* See gdbsupport/common-regcache.h. */
415
416 struct regcache *
417 get_thread_regcache_for_ptid (ptid_t ptid)
418 {
419 /* This function doesn't take a process_stratum_target parameter
420 because it's a gdbsupport/ routine implemented by both gdb and
421 gdbserver. It always refers to a ptid of the current target. */
422 process_stratum_target *proc_target = current_inferior ()->process_target ();
423 return get_thread_regcache (proc_target, ptid);
424 }
425
426 /* Observer for the target_changed event. */
427
428 static void
429 regcache_observer_target_changed (struct target_ops *target)
430 {
431 registers_changed ();
432 }
433
434 /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
435 static void
436 regcache_thread_ptid_changed (process_stratum_target *target,
437 ptid_t old_ptid, ptid_t new_ptid)
438 {
439 auto ptid_regc_map_it = regcaches.find (target);
440
441 if (ptid_regc_map_it == regcaches.end ())
442 return;
443
444 auto &ptid_regc_map = ptid_regc_map_it->second;
445 auto range = ptid_regc_map.equal_range (old_ptid);
446 for (auto it = range.first; it != range.second;)
447 {
448 regcache_up rc = std::move (it->second);
449 rc->set_ptid (new_ptid);
450
451 /* Remove old before inserting new, to avoid rehashing,
452 which would invalidate iterators. */
453 it = ptid_regc_map.erase (it);
454 ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
455 }
456 }
457
458 /* Low level examining and depositing of registers.
459
460 The caller is responsible for making sure that the inferior is
461 stopped before calling the fetching routines, or it will get
462 garbage. (a change from GDB version 3, in which the caller got the
463 value from the last stop). */
464
465 /* REGISTERS_CHANGED ()
466
467 Indicate that registers may have changed, so invalidate the cache. */
468
469 void
470 registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
471 {
472 if (target == nullptr)
473 {
474 /* Since there can be ptid clashes between targets, it's not valid to
475 pass a ptid without saying to which target it belongs. */
476 gdb_assert (ptid == minus_one_ptid);
477
478 /* Delete all the regcaches of all targets. */
479 regcaches.clear ();
480 }
481 else if (ptid != minus_one_ptid)
482 {
483 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
484 to this (TARGET, PTID). */
485 auto ptid_regc_map_it = regcaches.find (target);
486 if (ptid_regc_map_it != regcaches.end ())
487 {
488 auto &ptid_regc_map = ptid_regc_map_it->second;
489 ptid_regc_map.erase (ptid);
490 }
491 }
492 else
493 {
494 /* Non-NULL target and minus_one_ptid, delete all regcaches
495 associated to this target. */
496 regcaches.erase (target);
497 }
498
499 if ((target == nullptr || current_thread_target == target)
500 && current_thread_ptid.matches (ptid))
501 {
502 current_thread_target = NULL;
503 current_thread_ptid = null_ptid;
504 current_thread_arch = NULL;
505 }
506
507 if ((target == nullptr || current_inferior ()->process_target () == target)
508 && inferior_ptid.matches (ptid))
509 {
510 /* We just deleted the regcache of the current thread. Need to
511 forget about any frames we have cached, too. */
512 reinit_frame_cache ();
513 }
514 }
515
516 /* See regcache.h. */
517
518 void
519 registers_changed_thread (thread_info *thread)
520 {
521 registers_changed_ptid (thread->inf->process_target (), thread->ptid);
522 }
523
524 void
525 registers_changed (void)
526 {
527 registers_changed_ptid (nullptr, minus_one_ptid);
528 }
529
530 void
531 regcache::raw_update (int regnum)
532 {
533 assert_regnum (regnum);
534
535 /* Make certain that the register cache is up-to-date with respect
536 to the current thread. This switching shouldn't be necessary
537 only there is still only one target side register cache. Sigh!
538 On the bright side, at least there is a regcache object. */
539
540 if (get_register_status (regnum) == REG_UNKNOWN)
541 {
542 target_fetch_registers (this, regnum);
543
544 /* A number of targets can't access the whole set of raw
545 registers (because the debug API provides no means to get at
546 them). */
547 if (m_register_status[regnum] == REG_UNKNOWN)
548 m_register_status[regnum] = REG_UNAVAILABLE;
549 }
550 }
551
552 enum register_status
553 readable_regcache::raw_read (int regnum, gdb_byte *buf)
554 {
555 gdb_assert (buf != NULL);
556 raw_update (regnum);
557
558 if (m_register_status[regnum] != REG_VALID)
559 memset (buf, 0, m_descr->sizeof_register[regnum]);
560 else
561 memcpy (buf, register_buffer (regnum),
562 m_descr->sizeof_register[regnum]);
563
564 return m_register_status[regnum];
565 }
566
567 enum register_status
568 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
569 {
570 gdb_assert (regcache != NULL);
571 return regcache->raw_read (regnum, val);
572 }
573
574 template<typename T, typename>
575 enum register_status
576 readable_regcache::raw_read (int regnum, T *val)
577 {
578 gdb_byte *buf;
579 enum register_status status;
580
581 assert_regnum (regnum);
582 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
583 status = raw_read (regnum, buf);
584 if (status == REG_VALID)
585 *val = extract_integer<T> (buf,
586 m_descr->sizeof_register[regnum],
587 gdbarch_byte_order (m_descr->gdbarch));
588 else
589 *val = 0;
590 return status;
591 }
592
593 enum register_status
594 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
595 ULONGEST *val)
596 {
597 gdb_assert (regcache != NULL);
598 return regcache->raw_read (regnum, val);
599 }
600
601 void
602 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
603 {
604 gdb_assert (regcache != NULL);
605 regcache->raw_write (regnum, val);
606 }
607
608 template<typename T, typename>
609 void
610 regcache::raw_write (int regnum, T val)
611 {
612 gdb_byte *buf;
613
614 assert_regnum (regnum);
615 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
616 store_integer (buf, m_descr->sizeof_register[regnum],
617 gdbarch_byte_order (m_descr->gdbarch), val);
618 raw_write (regnum, buf);
619 }
620
621 void
622 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
623 ULONGEST val)
624 {
625 gdb_assert (regcache != NULL);
626 regcache->raw_write (regnum, val);
627 }
628
629 LONGEST
630 regcache_raw_get_signed (struct regcache *regcache, int regnum)
631 {
632 LONGEST value;
633 enum register_status status;
634
635 status = regcache_raw_read_signed (regcache, regnum, &value);
636 if (status == REG_UNAVAILABLE)
637 throw_error (NOT_AVAILABLE_ERROR,
638 _("Register %d is not available"), regnum);
639 return value;
640 }
641
642 enum register_status
643 readable_regcache::cooked_read (int regnum, gdb_byte *buf)
644 {
645 gdb_assert (regnum >= 0);
646 gdb_assert (regnum < m_descr->nr_cooked_registers);
647 if (regnum < num_raw_registers ())
648 return raw_read (regnum, buf);
649 else if (m_has_pseudo
650 && m_register_status[regnum] != REG_UNKNOWN)
651 {
652 if (m_register_status[regnum] == REG_VALID)
653 memcpy (buf, register_buffer (regnum),
654 m_descr->sizeof_register[regnum]);
655 else
656 memset (buf, 0, m_descr->sizeof_register[regnum]);
657
658 return m_register_status[regnum];
659 }
660 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
661 {
662 struct value *mark, *computed;
663 enum register_status result = REG_VALID;
664
665 mark = value_mark ();
666
667 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
668 this, regnum);
669 if (value_entirely_available (computed))
670 memcpy (buf, value_contents_raw (computed),
671 m_descr->sizeof_register[regnum]);
672 else
673 {
674 memset (buf, 0, m_descr->sizeof_register[regnum]);
675 result = REG_UNAVAILABLE;
676 }
677
678 value_free_to_mark (mark);
679
680 return result;
681 }
682 else
683 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
684 regnum, buf);
685 }
686
687 struct value *
688 readable_regcache::cooked_read_value (int regnum)
689 {
690 gdb_assert (regnum >= 0);
691 gdb_assert (regnum < m_descr->nr_cooked_registers);
692
693 if (regnum < num_raw_registers ()
694 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
695 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
696 {
697 struct value *result;
698
699 result = allocate_value (register_type (m_descr->gdbarch, regnum));
700 VALUE_LVAL (result) = lval_register;
701 VALUE_REGNUM (result) = regnum;
702
703 /* It is more efficient in general to do this delegation in this
704 direction than in the other one, even though the value-based
705 API is preferred. */
706 if (cooked_read (regnum,
707 value_contents_raw (result)) == REG_UNAVAILABLE)
708 mark_value_bytes_unavailable (result, 0,
709 TYPE_LENGTH (value_type (result)));
710
711 return result;
712 }
713 else
714 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
715 this, regnum);
716 }
717
718 enum register_status
719 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
720 LONGEST *val)
721 {
722 gdb_assert (regcache != NULL);
723 return regcache->cooked_read (regnum, val);
724 }
725
726 template<typename T, typename>
727 enum register_status
728 readable_regcache::cooked_read (int regnum, T *val)
729 {
730 enum register_status status;
731 gdb_byte *buf;
732
733 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
734 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
735 status = cooked_read (regnum, buf);
736 if (status == REG_VALID)
737 *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
738 gdbarch_byte_order (m_descr->gdbarch));
739 else
740 *val = 0;
741 return status;
742 }
743
744 enum register_status
745 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
746 ULONGEST *val)
747 {
748 gdb_assert (regcache != NULL);
749 return regcache->cooked_read (regnum, val);
750 }
751
752 void
753 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
754 LONGEST val)
755 {
756 gdb_assert (regcache != NULL);
757 regcache->cooked_write (regnum, val);
758 }
759
760 template<typename T, typename>
761 void
762 regcache::cooked_write (int regnum, T val)
763 {
764 gdb_byte *buf;
765
766 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
767 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
768 store_integer (buf, m_descr->sizeof_register[regnum],
769 gdbarch_byte_order (m_descr->gdbarch), val);
770 cooked_write (regnum, buf);
771 }
772
773 void
774 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
775 ULONGEST val)
776 {
777 gdb_assert (regcache != NULL);
778 regcache->cooked_write (regnum, val);
779 }
780
781 void
782 regcache::raw_write (int regnum, const gdb_byte *buf)
783 {
784
785 gdb_assert (buf != NULL);
786 assert_regnum (regnum);
787
788 /* On the sparc, writing %g0 is a no-op, so we don't even want to
789 change the registers array if something writes to this register. */
790 if (gdbarch_cannot_store_register (arch (), regnum))
791 return;
792
793 /* If we have a valid copy of the register, and new value == old
794 value, then don't bother doing the actual store. */
795 if (get_register_status (regnum) == REG_VALID
796 && (memcmp (register_buffer (regnum), buf,
797 m_descr->sizeof_register[regnum]) == 0))
798 return;
799
800 target_prepare_to_store (this);
801 raw_supply (regnum, buf);
802
803 /* Invalidate the register after it is written, in case of a
804 failure. */
805 auto invalidator
806 = make_scope_exit ([&] { this->invalidate (regnum); });
807
808 target_store_registers (this, regnum);
809
810 /* The target did not throw an error so we can discard invalidating
811 the register. */
812 invalidator.release ();
813 }
814
815 void
816 regcache::cooked_write (int regnum, const gdb_byte *buf)
817 {
818 gdb_assert (regnum >= 0);
819 gdb_assert (regnum < m_descr->nr_cooked_registers);
820 if (regnum < num_raw_registers ())
821 raw_write (regnum, buf);
822 else
823 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
824 regnum, buf);
825 }
826
827 /* See regcache.h. */
828
829 enum register_status
830 readable_regcache::read_part (int regnum, int offset, int len,
831 gdb_byte *out, bool is_raw)
832 {
833 int reg_size = register_size (arch (), regnum);
834
835 gdb_assert (out != NULL);
836 gdb_assert (offset >= 0 && offset <= reg_size);
837 gdb_assert (len >= 0 && offset + len <= reg_size);
838
839 if (offset == 0 && len == 0)
840 {
841 /* Nothing to do. */
842 return REG_VALID;
843 }
844
845 if (offset == 0 && len == reg_size)
846 {
847 /* Read the full register. */
848 return (is_raw) ? raw_read (regnum, out) : cooked_read (regnum, out);
849 }
850
851 enum register_status status;
852 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
853
854 /* Read full register to buffer. */
855 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
856 if (status != REG_VALID)
857 return status;
858
859 /* Copy out. */
860 memcpy (out, reg + offset, len);
861 return REG_VALID;
862 }
863
864 /* See regcache.h. */
865
866 void
867 reg_buffer::raw_collect_part (int regnum, int offset, int len,
868 gdb_byte *out) const
869 {
870 int reg_size = register_size (arch (), regnum);
871
872 gdb_assert (out != nullptr);
873 gdb_assert (offset >= 0 && offset <= reg_size);
874 gdb_assert (len >= 0 && offset + len <= reg_size);
875
876 if (offset == 0 && len == 0)
877 {
878 /* Nothing to do. */
879 return;
880 }
881
882 if (offset == 0 && len == reg_size)
883 {
884 /* Collect the full register. */
885 return raw_collect (regnum, out);
886 }
887
888 /* Read to buffer, then write out. */
889 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
890 raw_collect (regnum, reg);
891 memcpy (out, reg + offset, len);
892 }
893
894 /* See regcache.h. */
895
896 enum register_status
897 regcache::write_part (int regnum, int offset, int len,
898 const gdb_byte *in, bool is_raw)
899 {
900 int reg_size = register_size (arch (), regnum);
901
902 gdb_assert (in != NULL);
903 gdb_assert (offset >= 0 && offset <= reg_size);
904 gdb_assert (len >= 0 && offset + len <= reg_size);
905
906 if (offset == 0 && len == 0)
907 {
908 /* Nothing to do. */
909 return REG_VALID;
910 }
911
912 if (offset == 0 && len == reg_size)
913 {
914 /* Write the full register. */
915 (is_raw) ? raw_write (regnum, in) : cooked_write (regnum, in);
916 return REG_VALID;
917 }
918
919 enum register_status status;
920 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
921
922 /* Read existing register to buffer. */
923 status = (is_raw) ? raw_read (regnum, reg) : cooked_read (regnum, reg);
924 if (status != REG_VALID)
925 return status;
926
927 /* Update buffer, then write back to regcache. */
928 memcpy (reg + offset, in, len);
929 is_raw ? raw_write (regnum, reg) : cooked_write (regnum, reg);
930 return REG_VALID;
931 }
932
933 /* See regcache.h. */
934
935 void
936 reg_buffer::raw_supply_part (int regnum, int offset, int len,
937 const gdb_byte *in)
938 {
939 int reg_size = register_size (arch (), regnum);
940
941 gdb_assert (in != nullptr);
942 gdb_assert (offset >= 0 && offset <= reg_size);
943 gdb_assert (len >= 0 && offset + len <= reg_size);
944
945 if (offset == 0 && len == 0)
946 {
947 /* Nothing to do. */
948 return;
949 }
950
951 if (offset == 0 && len == reg_size)
952 {
953 /* Supply the full register. */
954 return raw_supply (regnum, in);
955 }
956
957 gdb_byte *reg = (gdb_byte *) alloca (reg_size);
958
959 /* Read existing value to buffer. */
960 raw_collect (regnum, reg);
961
962 /* Write to buffer, then write out. */
963 memcpy (reg + offset, in, len);
964 raw_supply (regnum, reg);
965 }
966
967 enum register_status
968 readable_regcache::raw_read_part (int regnum, int offset, int len,
969 gdb_byte *buf)
970 {
971 assert_regnum (regnum);
972 return read_part (regnum, offset, len, buf, true);
973 }
974
975 /* See regcache.h. */
976
977 void
978 regcache::raw_write_part (int regnum, int offset, int len,
979 const gdb_byte *buf)
980 {
981 assert_regnum (regnum);
982 write_part (regnum, offset, len, buf, true);
983 }
984
985 /* See regcache.h. */
986
987 enum register_status
988 readable_regcache::cooked_read_part (int regnum, int offset, int len,
989 gdb_byte *buf)
990 {
991 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
992 return read_part (regnum, offset, len, buf, false);
993 }
994
995 /* See regcache.h. */
996
997 void
998 regcache::cooked_write_part (int regnum, int offset, int len,
999 const gdb_byte *buf)
1000 {
1001 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1002 write_part (regnum, offset, len, buf, false);
1003 }
1004
1005 /* See gdbsupport/common-regcache.h. */
1006
1007 void
1008 reg_buffer::raw_supply (int regnum, const void *buf)
1009 {
1010 void *regbuf;
1011 size_t size;
1012
1013 assert_regnum (regnum);
1014
1015 regbuf = register_buffer (regnum);
1016 size = m_descr->sizeof_register[regnum];
1017
1018 if (buf)
1019 {
1020 memcpy (regbuf, buf, size);
1021 m_register_status[regnum] = REG_VALID;
1022 }
1023 else
1024 {
1025 /* This memset not strictly necessary, but better than garbage
1026 in case the register value manages to escape somewhere (due
1027 to a bug, no less). */
1028 memset (regbuf, 0, size);
1029 m_register_status[regnum] = REG_UNAVAILABLE;
1030 }
1031 }
1032
1033 /* See regcache.h. */
1034
1035 void
1036 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr,
1037 int addr_len, bool is_signed)
1038 {
1039 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1040 gdb_byte *regbuf;
1041 size_t regsize;
1042
1043 assert_regnum (regnum);
1044
1045 regbuf = register_buffer (regnum);
1046 regsize = m_descr->sizeof_register[regnum];
1047
1048 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1049 byte_order);
1050 m_register_status[regnum] = REG_VALID;
1051 }
1052
1053 /* See regcache.h. */
1054
1055 void
1056 reg_buffer::raw_supply_zeroed (int regnum)
1057 {
1058 void *regbuf;
1059 size_t size;
1060
1061 assert_regnum (regnum);
1062
1063 regbuf = register_buffer (regnum);
1064 size = m_descr->sizeof_register[regnum];
1065
1066 memset (regbuf, 0, size);
1067 m_register_status[regnum] = REG_VALID;
1068 }
1069
1070 /* See gdbsupport/common-regcache.h. */
1071
1072 void
1073 reg_buffer::raw_collect (int regnum, void *buf) const
1074 {
1075 const void *regbuf;
1076 size_t size;
1077
1078 gdb_assert (buf != NULL);
1079 assert_regnum (regnum);
1080
1081 regbuf = register_buffer (regnum);
1082 size = m_descr->sizeof_register[regnum];
1083 memcpy (buf, regbuf, size);
1084 }
1085
1086 /* See regcache.h. */
1087
1088 void
1089 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1090 bool is_signed) const
1091 {
1092 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1093 const gdb_byte *regbuf;
1094 size_t regsize;
1095
1096 assert_regnum (regnum);
1097
1098 regbuf = register_buffer (regnum);
1099 regsize = m_descr->sizeof_register[regnum];
1100
1101 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1102 byte_order);
1103 }
1104
1105 /* See regcache.h. */
1106
1107 void
1108 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1109 const gdb_byte *in_buf, gdb_byte *out_buf,
1110 int slot_size, int offs) const
1111 {
1112 struct gdbarch *gdbarch = arch ();
1113 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1114
1115 /* Use part versions and reg_size to prevent possible buffer overflows when
1116 accessing the regcache. */
1117
1118 if (out_buf != nullptr)
1119 {
1120 raw_collect_part (regnum, 0, reg_size, out_buf + offs);
1121
1122 /* Ensure any additional space is cleared. */
1123 if (slot_size > reg_size)
1124 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1125 }
1126 else if (in_buf != nullptr)
1127 out_regcache->raw_supply_part (regnum, 0, reg_size, in_buf + offs);
1128 else
1129 {
1130 /* Invalidate the register. */
1131 out_regcache->raw_supply (regnum, nullptr);
1132 }
1133 }
1134
1135 /* See regcache.h. */
1136
1137 void
1138 regcache::transfer_regset (const struct regset *regset,
1139 struct regcache *out_regcache,
1140 int regnum, const gdb_byte *in_buf,
1141 gdb_byte *out_buf, size_t size) const
1142 {
1143 const struct regcache_map_entry *map;
1144 int offs = 0, count;
1145
1146 for (map = (const struct regcache_map_entry *) regset->regmap;
1147 (count = map->count) != 0;
1148 map++)
1149 {
1150 int regno = map->regno;
1151 int slot_size = map->size;
1152
1153 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1154 slot_size = m_descr->sizeof_register[regno];
1155
1156 if (regno == REGCACHE_MAP_SKIP
1157 || (regnum != -1
1158 && (regnum < regno || regnum >= regno + count)))
1159 offs += count * slot_size;
1160
1161 else if (regnum == -1)
1162 for (; count--; regno++, offs += slot_size)
1163 {
1164 if (offs + slot_size > size)
1165 break;
1166
1167 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1168 slot_size, offs);
1169 }
1170 else
1171 {
1172 /* Transfer a single register and return. */
1173 offs += (regnum - regno) * slot_size;
1174 if (offs + slot_size > size)
1175 return;
1176
1177 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1178 slot_size, offs);
1179 return;
1180 }
1181 }
1182 }
1183
1184 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1185 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1186 If BUF is NULL, set the register(s) to "unavailable" status. */
1187
1188 void
1189 regcache_supply_regset (const struct regset *regset,
1190 struct regcache *regcache,
1191 int regnum, const void *buf, size_t size)
1192 {
1193 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1194 }
1195
1196 void
1197 regcache::supply_regset (const struct regset *regset,
1198 int regnum, const void *buf, size_t size)
1199 {
1200 transfer_regset (regset, this, regnum, (const gdb_byte *) buf, nullptr, size);
1201 }
1202
1203 /* Collect register REGNUM from REGCACHE to BUF, using the register
1204 map in REGSET. If REGNUM is -1, do this for all registers in
1205 REGSET. */
1206
1207 void
1208 regcache_collect_regset (const struct regset *regset,
1209 const struct regcache *regcache,
1210 int regnum, void *buf, size_t size)
1211 {
1212 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1213 }
1214
1215 void
1216 regcache::collect_regset (const struct regset *regset,
1217 int regnum, void *buf, size_t size) const
1218 {
1219 transfer_regset (regset, nullptr, regnum, nullptr, (gdb_byte *) buf, size);
1220 }
1221
1222 /* See gdbsupport/common-regcache.h. */
1223
1224 bool
1225 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1226 {
1227 gdb_assert (buf != NULL);
1228 assert_regnum (regnum);
1229
1230 const char *regbuf = (const char *) register_buffer (regnum);
1231 size_t size = m_descr->sizeof_register[regnum];
1232 gdb_assert (size >= offset);
1233
1234 return (memcmp (buf, regbuf + offset, size - offset) == 0);
1235 }
1236
1237 /* Special handling for register PC. */
1238
1239 CORE_ADDR
1240 regcache_read_pc (struct regcache *regcache)
1241 {
1242 struct gdbarch *gdbarch = regcache->arch ();
1243
1244 CORE_ADDR pc_val;
1245
1246 if (gdbarch_read_pc_p (gdbarch))
1247 pc_val = gdbarch_read_pc (gdbarch, regcache);
1248 /* Else use per-frame method on get_current_frame. */
1249 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1250 {
1251 ULONGEST raw_val;
1252
1253 if (regcache_cooked_read_unsigned (regcache,
1254 gdbarch_pc_regnum (gdbarch),
1255 &raw_val) == REG_UNAVAILABLE)
1256 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1257
1258 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1259 }
1260 else
1261 internal_error (__FILE__, __LINE__,
1262 _("regcache_read_pc: Unable to find PC"));
1263 return pc_val;
1264 }
1265
1266 /* See gdbsupport/common-regcache.h. */
1267
1268 CORE_ADDR
1269 regcache_read_pc_protected (regcache *regcache)
1270 {
1271 CORE_ADDR pc;
1272 try
1273 {
1274 pc = regcache_read_pc (regcache);
1275 }
1276 catch (const gdb_exception_error &ex)
1277 {
1278 pc = 0;
1279 }
1280
1281 return pc;
1282 }
1283
1284 void
1285 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1286 {
1287 struct gdbarch *gdbarch = regcache->arch ();
1288
1289 if (gdbarch_write_pc_p (gdbarch))
1290 gdbarch_write_pc (gdbarch, regcache, pc);
1291 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1292 regcache_cooked_write_unsigned (regcache,
1293 gdbarch_pc_regnum (gdbarch), pc);
1294 else
1295 internal_error (__FILE__, __LINE__,
1296 _("regcache_write_pc: Unable to update PC"));
1297
1298 /* Writing the PC (for instance, from "load") invalidates the
1299 current frame. */
1300 reinit_frame_cache ();
1301 }
1302
1303 int
1304 reg_buffer::num_raw_registers () const
1305 {
1306 return gdbarch_num_regs (arch ());
1307 }
1308
1309 void
1310 regcache::debug_print_register (const char *func, int regno)
1311 {
1312 struct gdbarch *gdbarch = arch ();
1313
1314 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1315 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1316 && gdbarch_register_name (gdbarch, regno) != NULL
1317 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1318 fprintf_unfiltered (gdb_stdlog, "(%s)",
1319 gdbarch_register_name (gdbarch, regno));
1320 else
1321 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1322 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1323 {
1324 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1325 int size = register_size (gdbarch, regno);
1326 gdb_byte *buf = register_buffer (regno);
1327
1328 fprintf_unfiltered (gdb_stdlog, " = ");
1329 for (int i = 0; i < size; i++)
1330 {
1331 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1332 }
1333 if (size <= sizeof (LONGEST))
1334 {
1335 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1336
1337 fprintf_unfiltered (gdb_stdlog, " %s %s",
1338 core_addr_to_string_nz (val), plongest (val));
1339 }
1340 }
1341 fprintf_unfiltered (gdb_stdlog, "\n");
1342 }
1343
1344 static void
1345 reg_flush_command (const char *command, int from_tty)
1346 {
1347 /* Force-flush the register cache. */
1348 registers_changed ();
1349 if (from_tty)
1350 printf_filtered (_("Register cache flushed.\n"));
1351 }
1352
1353 void
1354 register_dump::dump (ui_file *file)
1355 {
1356 auto descr = regcache_descr (m_gdbarch);
1357 int regnum;
1358 int footnote_nr = 0;
1359 int footnote_register_offset = 0;
1360 int footnote_register_type_name_null = 0;
1361 long register_offset = 0;
1362
1363 gdb_assert (descr->nr_cooked_registers
1364 == gdbarch_num_cooked_regs (m_gdbarch));
1365
1366 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1367 {
1368 /* Name. */
1369 if (regnum < 0)
1370 fprintf_unfiltered (file, " %-10s", "Name");
1371 else
1372 {
1373 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1374
1375 if (p == NULL)
1376 p = "";
1377 else if (p[0] == '\0')
1378 p = "''";
1379 fprintf_unfiltered (file, " %-10s", p);
1380 }
1381
1382 /* Number. */
1383 if (regnum < 0)
1384 fprintf_unfiltered (file, " %4s", "Nr");
1385 else
1386 fprintf_unfiltered (file, " %4d", regnum);
1387
1388 /* Relative number. */
1389 if (regnum < 0)
1390 fprintf_unfiltered (file, " %4s", "Rel");
1391 else if (regnum < gdbarch_num_regs (m_gdbarch))
1392 fprintf_unfiltered (file, " %4d", regnum);
1393 else
1394 fprintf_unfiltered (file, " %4d",
1395 (regnum - gdbarch_num_regs (m_gdbarch)));
1396
1397 /* Offset. */
1398 if (regnum < 0)
1399 fprintf_unfiltered (file, " %6s ", "Offset");
1400 else
1401 {
1402 fprintf_unfiltered (file, " %6ld",
1403 descr->register_offset[regnum]);
1404 if (register_offset != descr->register_offset[regnum]
1405 || (regnum > 0
1406 && (descr->register_offset[regnum]
1407 != (descr->register_offset[regnum - 1]
1408 + descr->sizeof_register[regnum - 1])))
1409 )
1410 {
1411 if (!footnote_register_offset)
1412 footnote_register_offset = ++footnote_nr;
1413 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1414 }
1415 else
1416 fprintf_unfiltered (file, " ");
1417 register_offset = (descr->register_offset[regnum]
1418 + descr->sizeof_register[regnum]);
1419 }
1420
1421 /* Size. */
1422 if (regnum < 0)
1423 fprintf_unfiltered (file, " %5s ", "Size");
1424 else
1425 fprintf_unfiltered (file, " %5ld", descr->sizeof_register[regnum]);
1426
1427 /* Type. */
1428 {
1429 const char *t;
1430 std::string name_holder;
1431
1432 if (regnum < 0)
1433 t = "Type";
1434 else
1435 {
1436 static const char blt[] = "builtin_type";
1437
1438 t = register_type (m_gdbarch, regnum)->name ();
1439 if (t == NULL)
1440 {
1441 if (!footnote_register_type_name_null)
1442 footnote_register_type_name_null = ++footnote_nr;
1443 name_holder = string_printf ("*%d",
1444 footnote_register_type_name_null);
1445 t = name_holder.c_str ();
1446 }
1447 /* Chop a leading builtin_type. */
1448 if (startswith (t, blt))
1449 t += strlen (blt);
1450 }
1451 fprintf_unfiltered (file, " %-15s", t);
1452 }
1453
1454 /* Leading space always present. */
1455 fprintf_unfiltered (file, " ");
1456
1457 dump_reg (file, regnum);
1458
1459 fprintf_unfiltered (file, "\n");
1460 }
1461
1462 if (footnote_register_offset)
1463 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1464 footnote_register_offset);
1465 if (footnote_register_type_name_null)
1466 fprintf_unfiltered (file,
1467 "*%d: Register type's name NULL.\n",
1468 footnote_register_type_name_null);
1469 }
1470
1471 #if GDB_SELF_TEST
1472 #include "gdbsupport/selftest.h"
1473 #include "selftest-arch.h"
1474 #include "target-float.h"
1475
1476 namespace selftests {
1477
1478 static size_t
1479 regcaches_size ()
1480 {
1481 size_t size = 0;
1482 for (auto it = regcaches.begin (); it != regcaches.end (); ++it)
1483 {
1484 auto &ptid_regc_map = it->second;
1485 size += ptid_regc_map.size ();
1486 }
1487
1488 return size;
1489 }
1490
1491 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1492
1493 static int
1494 regcache_count (process_stratum_target *target, ptid_t ptid)
1495 {
1496 auto ptid_regc_map_it = regcaches.find (target);
1497 if (ptid_regc_map_it != regcaches.end ())
1498 {
1499 auto &ptid_regc_map = ptid_regc_map_it->second;
1500 auto range = ptid_regc_map.equal_range (ptid);
1501
1502 return std::distance (range.first, range.second);
1503 }
1504
1505 return 0;
1506 };
1507
1508 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1509
1510 static void
1511 get_thread_arch_aspace_regcache_and_check (process_stratum_target *target,
1512 ptid_t ptid)
1513 {
1514 /* We currently only test with a single gdbarch. Any gdbarch will do, so use
1515 the current inferior's gdbarch. Also use the current inferior's address
1516 space. */
1517 gdbarch *arch = current_inferior ()->gdbarch;
1518 address_space *aspace = current_inferior ()->aspace;
1519 regcache *regcache
1520 = get_thread_arch_aspace_regcache (target, ptid, arch, aspace);
1521
1522 SELF_CHECK (regcache != NULL);
1523 SELF_CHECK (regcache->target () == target);
1524 SELF_CHECK (regcache->ptid () == ptid);
1525 SELF_CHECK (regcache->arch () == arch);
1526 SELF_CHECK (regcache->aspace () == aspace);
1527 }
1528
1529 /* The data that the regcaches selftests must hold onto for the duration of the
1530 test. */
1531
1532 struct regcache_test_data
1533 {
1534 regcache_test_data ()
1535 {
1536 /* Ensure the regcaches container is empty at the start. */
1537 registers_changed ();
1538 }
1539
1540 ~regcache_test_data ()
1541 {
1542 /* Make sure to leave the global regcaches container empty. */
1543 registers_changed ();
1544 }
1545
1546 test_target_ops test_target1;
1547 test_target_ops test_target2;
1548 };
1549
1550 using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
1551
1552 /* Set up a few regcaches from two different targets, for use in
1553 regcache-management tests.
1554
1555 Return a pointer, because the `regcache_test_data` type is not moveable. */
1556
1557 static regcache_test_data_up
1558 populate_regcaches_for_test ()
1559 {
1560 regcache_test_data_up data (new regcache_test_data);
1561 size_t expected_regcache_size = 0;
1562
1563 SELF_CHECK (regcaches_size () == 0);
1564
1565 /* Populate the regcache container with a few regcaches for the two test
1566 targets. */
1567 for (int pid : { 1, 2 })
1568 {
1569 for (long lwp : { 1, 2, 3 })
1570 {
1571 get_thread_arch_aspace_regcache_and_check
1572 (&data->test_target1, ptid_t (pid, lwp));
1573 expected_regcache_size++;
1574 SELF_CHECK (regcaches_size () == expected_regcache_size);
1575
1576 get_thread_arch_aspace_regcache_and_check
1577 (&data->test_target2, ptid_t (pid, lwp));
1578 expected_regcache_size++;
1579 SELF_CHECK (regcaches_size () == expected_regcache_size);
1580 }
1581 }
1582
1583 return data;
1584 }
1585
1586 static void
1587 get_thread_arch_aspace_regcache_test ()
1588 {
1589 /* populate_regcaches_for_test already tests most of the
1590 get_thread_arch_aspace_regcache functionality. */
1591 regcache_test_data_up data = populate_regcaches_for_test ();
1592 size_t regcaches_size_before = regcaches_size ();
1593
1594 /* Test that getting an existing regcache doesn't create a new one. */
1595 get_thread_arch_aspace_regcache_and_check (&data->test_target1, ptid_t (2, 2));
1596 SELF_CHECK (regcaches_size () == regcaches_size_before);
1597 }
1598
1599 /* Test marking all regcaches of all targets as changed. */
1600
1601 static void
1602 registers_changed_ptid_all_test ()
1603 {
1604 regcache_test_data_up data = populate_regcaches_for_test ();
1605
1606 registers_changed_ptid (nullptr, minus_one_ptid);
1607 SELF_CHECK (regcaches_size () == 0);
1608 }
1609
1610 /* Test marking regcaches of a specific target as changed. */
1611
1612 static void
1613 registers_changed_ptid_target_test ()
1614 {
1615 regcache_test_data_up data = populate_regcaches_for_test ();
1616
1617 registers_changed_ptid (&data->test_target1, minus_one_ptid);
1618 SELF_CHECK (regcaches_size () == 6);
1619
1620 /* Check that we deleted the regcache for the right target. */
1621 SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1622 SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1623 }
1624
1625 /* Test marking regcaches of a specific (target, ptid) as changed. */
1626
1627 static void
1628 registers_changed_ptid_target_ptid_test ()
1629 {
1630 regcache_test_data_up data = populate_regcaches_for_test ();
1631
1632 registers_changed_ptid (&data->test_target1, ptid_t (2, 2));
1633 SELF_CHECK (regcaches_size () == 11);
1634
1635 /* Check that we deleted the regcache for the right target. */
1636 SELF_CHECK (regcache_count (&data->test_target1, ptid_t (2, 2)) == 0);
1637 SELF_CHECK (regcache_count (&data->test_target2, ptid_t (2, 2)) == 1);
1638 }
1639
1640 class target_ops_no_register : public test_target_ops
1641 {
1642 public:
1643 target_ops_no_register ()
1644 : test_target_ops {}
1645 {}
1646
1647 void reset ()
1648 {
1649 fetch_registers_called = 0;
1650 store_registers_called = 0;
1651 xfer_partial_called = 0;
1652 }
1653
1654 void fetch_registers (regcache *regs, int regno) override;
1655 void store_registers (regcache *regs, int regno) override;
1656
1657 enum target_xfer_status xfer_partial (enum target_object object,
1658 const char *annex, gdb_byte *readbuf,
1659 const gdb_byte *writebuf,
1660 ULONGEST offset, ULONGEST len,
1661 ULONGEST *xfered_len) override;
1662
1663 unsigned int fetch_registers_called = 0;
1664 unsigned int store_registers_called = 0;
1665 unsigned int xfer_partial_called = 0;
1666 };
1667
1668 void
1669 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1670 {
1671 /* Mark register available. */
1672 regs->raw_supply_zeroed (regno);
1673 this->fetch_registers_called++;
1674 }
1675
1676 void
1677 target_ops_no_register::store_registers (regcache *regs, int regno)
1678 {
1679 this->store_registers_called++;
1680 }
1681
1682 enum target_xfer_status
1683 target_ops_no_register::xfer_partial (enum target_object object,
1684 const char *annex, gdb_byte *readbuf,
1685 const gdb_byte *writebuf,
1686 ULONGEST offset, ULONGEST len,
1687 ULONGEST *xfered_len)
1688 {
1689 this->xfer_partial_called++;
1690
1691 *xfered_len = len;
1692 return TARGET_XFER_OK;
1693 }
1694
1695 class readwrite_regcache : public regcache
1696 {
1697 public:
1698 readwrite_regcache (process_stratum_target *target,
1699 struct gdbarch *gdbarch)
1700 : regcache (target, gdbarch, nullptr)
1701 {}
1702 };
1703
1704 /* Test regcache::cooked_read gets registers from raw registers and
1705 memory instead of target to_{fetch,store}_registers. */
1706
1707 static void
1708 cooked_read_test (struct gdbarch *gdbarch)
1709 {
1710 scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1711
1712 /* Test that read one raw register from regcache_no_target will go
1713 to the target layer. */
1714
1715 /* Find a raw register which size isn't zero. */
1716 int nonzero_regnum;
1717 for (nonzero_regnum = 0;
1718 nonzero_regnum < gdbarch_num_regs (gdbarch);
1719 nonzero_regnum++)
1720 {
1721 if (register_size (gdbarch, nonzero_regnum) != 0)
1722 break;
1723 }
1724
1725 readwrite_regcache readwrite (&mockctx.mock_target, gdbarch);
1726 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1727
1728 readwrite.raw_read (nonzero_regnum, buf.data ());
1729
1730 /* raw_read calls target_fetch_registers. */
1731 SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1732 mockctx.mock_target.reset ();
1733
1734 /* Mark all raw registers valid, so the following raw registers
1735 accesses won't go to target. */
1736 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1737 readwrite.raw_update (i);
1738
1739 mockctx.mock_target.reset ();
1740 /* Then, read all raw and pseudo registers, and don't expect calling
1741 to_{fetch,store}_registers. */
1742 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1743 {
1744 if (register_size (gdbarch, regnum) == 0)
1745 continue;
1746
1747 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1748
1749 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1750 inner_buf.data ()));
1751
1752 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1753 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1754 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1755
1756 mockctx.mock_target.reset ();
1757 }
1758
1759 readonly_detached_regcache readonly (readwrite);
1760
1761 /* GDB may go to target layer to fetch all registers and memory for
1762 readonly regcache. */
1763 mockctx.mock_target.reset ();
1764
1765 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1766 {
1767 if (register_size (gdbarch, regnum) == 0)
1768 continue;
1769
1770 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1771 enum register_status status = readonly.cooked_read (regnum,
1772 inner_buf.data ());
1773
1774 if (regnum < gdbarch_num_regs (gdbarch))
1775 {
1776 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1777
1778 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1779 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1780 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1781 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1782 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1783 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1784 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1785 || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
1786 {
1787 /* Raw registers. If raw registers are not in save_reggroup,
1788 their status are unknown. */
1789 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1790 SELF_CHECK (status == REG_VALID);
1791 else
1792 SELF_CHECK (status == REG_UNKNOWN);
1793 }
1794 else
1795 SELF_CHECK (status == REG_VALID);
1796 }
1797 else
1798 {
1799 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1800 SELF_CHECK (status == REG_VALID);
1801 else
1802 {
1803 /* If pseudo registers are not in save_reggroup, some of
1804 them can be computed from saved raw registers, but some
1805 of them are unknown. */
1806 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1807
1808 if (bfd_arch == bfd_arch_frv
1809 || bfd_arch == bfd_arch_m32c
1810 || bfd_arch == bfd_arch_mep
1811 || bfd_arch == bfd_arch_sh)
1812 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1813 else if (bfd_arch == bfd_arch_mips
1814 || bfd_arch == bfd_arch_h8300)
1815 SELF_CHECK (status == REG_UNKNOWN);
1816 else
1817 SELF_CHECK (status == REG_VALID);
1818 }
1819 }
1820
1821 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1822 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1823 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1824
1825 mockctx.mock_target.reset ();
1826 }
1827 }
1828
1829 /* Test regcache::cooked_write by writing some expected contents to
1830 registers, and checking that contents read from registers and the
1831 expected contents are the same. */
1832
1833 static void
1834 cooked_write_test (struct gdbarch *gdbarch)
1835 {
1836 /* Error out if debugging something, because we're going to push the
1837 test target, which would pop any existing target. */
1838 if (current_top_target ()->stratum () >= process_stratum)
1839 error (_("target already pushed"));
1840
1841 /* Create a mock environment. A process_stratum target pushed. */
1842
1843 target_ops_no_register mock_target;
1844
1845 /* Push the process_stratum target so we can mock accessing
1846 registers. */
1847 push_target (&mock_target);
1848
1849 /* Pop it again on exit (return/exception). */
1850 struct on_exit
1851 {
1852 ~on_exit ()
1853 {
1854 pop_all_targets_at_and_above (process_stratum);
1855 }
1856 } pop_targets;
1857
1858 readwrite_regcache readwrite (&mock_target, gdbarch);
1859
1860 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1861
1862 for (auto regnum = 0; regnum < num_regs; regnum++)
1863 {
1864 if (register_size (gdbarch, regnum) == 0
1865 || gdbarch_cannot_store_register (gdbarch, regnum))
1866 continue;
1867
1868 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1869
1870 if (bfd_arch == bfd_arch_sparc
1871 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1872 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1873 && gdbarch_ptr_bit (gdbarch) == 64
1874 && (regnum >= gdbarch_num_regs (gdbarch)
1875 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1876 continue;
1877
1878 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1879 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1880 const auto type = register_type (gdbarch, regnum);
1881
1882 if (type->code () == TYPE_CODE_FLT
1883 || type->code () == TYPE_CODE_DECFLOAT)
1884 {
1885 /* Generate valid float format. */
1886 target_float_from_string (expected.data (), type, "1.25");
1887 }
1888 else if (type->code () == TYPE_CODE_INT
1889 || type->code () == TYPE_CODE_ARRAY
1890 || type->code () == TYPE_CODE_PTR
1891 || type->code () == TYPE_CODE_UNION
1892 || type->code () == TYPE_CODE_STRUCT)
1893 {
1894 if (bfd_arch == bfd_arch_ia64
1895 || (regnum >= gdbarch_num_regs (gdbarch)
1896 && (bfd_arch == bfd_arch_xtensa
1897 || bfd_arch == bfd_arch_bfin
1898 || bfd_arch == bfd_arch_m32c
1899 /* m68hc11 pseudo registers are in memory. */
1900 || bfd_arch == bfd_arch_m68hc11
1901 || bfd_arch == bfd_arch_m68hc12
1902 || bfd_arch == bfd_arch_s390))
1903 || (bfd_arch == bfd_arch_frv
1904 /* FRV pseudo registers except iacc0. */
1905 && regnum > gdbarch_num_regs (gdbarch)))
1906 {
1907 /* Skip setting the expected values for some architecture
1908 registers. */
1909 }
1910 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1911 {
1912 /* RL78_PC_REGNUM */
1913 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1914 expected[j] = j;
1915 }
1916 else
1917 {
1918 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1919 expected[j] = j;
1920 }
1921 }
1922 else if (type->code () == TYPE_CODE_FLAGS)
1923 {
1924 /* No idea how to test flags. */
1925 continue;
1926 }
1927 else
1928 {
1929 /* If we don't know how to create the expected value for the
1930 this type, make it fail. */
1931 SELF_CHECK (0);
1932 }
1933
1934 readwrite.cooked_write (regnum, expected.data ());
1935
1936 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1937 SELF_CHECK (expected == buf);
1938 }
1939 }
1940
1941 /* Verify that when two threads with the same ptid exist (from two different
1942 targets) and one of them changes ptid, we only update the appropriate
1943 regcaches. */
1944
1945 static void
1946 regcache_thread_ptid_changed ()
1947 {
1948 /* This test relies on the global regcache list to initially be empty. */
1949 registers_changed ();
1950
1951 /* Any arch will do. */
1952 gdbarch *arch = current_inferior ()->gdbarch;
1953
1954 /* Prepare two targets with one thread each, with the same ptid. */
1955 scoped_mock_context<test_target_ops> target1 (arch);
1956 scoped_mock_context<test_target_ops> target2 (arch);
1957 target2.mock_inferior.next = &target1.mock_inferior;
1958
1959 ptid_t old_ptid (111, 222);
1960 ptid_t new_ptid (111, 333);
1961
1962 target1.mock_inferior.pid = old_ptid.pid ();
1963 target1.mock_thread.ptid = old_ptid;
1964 target2.mock_inferior.pid = old_ptid.pid ();
1965 target2.mock_thread.ptid = old_ptid;
1966
1967 gdb_assert (regcaches.empty ());
1968
1969 /* Populate the regcaches container. */
1970 get_thread_arch_aspace_regcache (&target1.mock_target, old_ptid, arch,
1971 nullptr);
1972 get_thread_arch_aspace_regcache (&target2.mock_target, old_ptid, arch,
1973 nullptr);
1974
1975 gdb_assert (regcaches.size () == 2);
1976 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
1977 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
1978 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
1979 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
1980
1981 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
1982
1983 gdb_assert (regcaches.size () == 2);
1984 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
1985 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
1986 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
1987 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
1988
1989 /* Leave the regcache list empty. */
1990 registers_changed ();
1991 gdb_assert (regcaches.empty ());
1992 }
1993
1994 } // namespace selftests
1995 #endif /* GDB_SELF_TEST */
1996
1997 void _initialize_regcache ();
1998 void
1999 _initialize_regcache ()
2000 {
2001 regcache_descr_handle
2002 = gdbarch_data_register_post_init (init_regcache_descr);
2003
2004 gdb::observers::target_changed.attach (regcache_observer_target_changed);
2005 gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed);
2006
2007 add_com ("flushregs", class_maintenance, reg_flush_command,
2008 _("Force gdb to flush its register cache (maintainer command)."));
2009
2010 #if GDB_SELF_TEST
2011 selftests::register_test ("get_thread_arch_aspace_regcache",
2012 selftests::get_thread_arch_aspace_regcache_test);
2013 selftests::register_test ("registers_changed_ptid_all",
2014 selftests::registers_changed_ptid_all_test);
2015 selftests::register_test ("registers_changed_ptid_target_ptid",
2016 selftests::registers_changed_ptid_target_ptid_test);
2017 selftests::register_test ("registers_changed_ptid_target",
2018 selftests::registers_changed_ptid_target_test);
2019
2020 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2021 selftests::cooked_read_test);
2022 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2023 selftests::cooked_write_test);
2024 selftests::register_test ("regcache_thread_ptid_changed",
2025 selftests::regcache_thread_ptid_changed);
2026 #endif
2027 }