[gdb/build] Fix missing implicit constructor call with gcc 4.8
[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 /* Wrapper around get_thread_arch_aspace_regcache that does some self checks. */
1492
1493 static void
1494 test_get_thread_arch_aspace_regcache (process_stratum_target *target,
1495 ptid_t ptid, struct gdbarch *gdbarch,
1496 address_space *aspace)
1497 {
1498 struct regcache *regcache
1499 = get_thread_arch_aspace_regcache (target, ptid, gdbarch, aspace);
1500 SELF_CHECK (regcache != NULL);
1501 SELF_CHECK (regcache->target () == target);
1502 SELF_CHECK (regcache->ptid () == ptid);
1503 SELF_CHECK (regcache->aspace () == aspace);
1504 }
1505
1506 static void
1507 regcaches_test ()
1508 {
1509 /* It is empty at the start. */
1510 SELF_CHECK (regcaches_size () == 0);
1511
1512 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1513
1514 test_target_ops test_target1;
1515 test_target_ops test_target2;
1516
1517 /* Get regcache from (target1,ptid1), a new regcache is added to
1518 REGCACHES. */
1519 test_get_thread_arch_aspace_regcache (&test_target1, ptid1,
1520 target_gdbarch (),
1521 NULL);
1522 SELF_CHECK (regcaches_size () == 1);
1523
1524 /* Get regcache from (target1,ptid2), a new regcache is added to
1525 REGCACHES. */
1526 test_get_thread_arch_aspace_regcache (&test_target1, ptid2,
1527 target_gdbarch (),
1528 NULL);
1529 SELF_CHECK (regcaches_size () == 2);
1530
1531 /* Get regcache from (target1,ptid3), a new regcache is added to
1532 REGCACHES. */
1533 test_get_thread_arch_aspace_regcache (&test_target1, ptid3,
1534 target_gdbarch (),
1535 NULL);
1536 SELF_CHECK (regcaches_size () == 3);
1537
1538 /* Get regcache from (target1,ptid2) again, nothing is added to
1539 REGCACHES. */
1540 test_get_thread_arch_aspace_regcache (&test_target1, ptid2,
1541 target_gdbarch (),
1542 NULL);
1543 SELF_CHECK (regcaches_size () == 3);
1544
1545 /* Get regcache from (target2,ptid2), a new regcache is added to
1546 REGCACHES, since this time we're using a different target. */
1547 test_get_thread_arch_aspace_regcache (&test_target2, ptid2,
1548 target_gdbarch (),
1549 NULL);
1550 SELF_CHECK (regcaches_size () == 4);
1551
1552 /* Mark that (target1,ptid2) changed. The regcache of (target1,
1553 ptid2) should be removed from REGCACHES. */
1554 registers_changed_ptid (&test_target1, ptid2);
1555 SELF_CHECK (regcaches_size () == 3);
1556
1557 /* Get the regcache from (target2,ptid2) again, confirming the
1558 registers_changed_ptid call above did not delete it. */
1559 test_get_thread_arch_aspace_regcache (&test_target2, ptid2,
1560 target_gdbarch (),
1561 NULL);
1562 SELF_CHECK (regcaches_size () == 3);
1563
1564 /* Confirm that marking all regcaches of all targets as changed
1565 clears REGCACHES. */
1566 registers_changed_ptid (nullptr, minus_one_ptid);
1567 SELF_CHECK (regcaches_size () == 0);
1568 }
1569
1570 class target_ops_no_register : public test_target_ops
1571 {
1572 public:
1573 target_ops_no_register ()
1574 : test_target_ops {}
1575 {}
1576
1577 void reset ()
1578 {
1579 fetch_registers_called = 0;
1580 store_registers_called = 0;
1581 xfer_partial_called = 0;
1582 }
1583
1584 void fetch_registers (regcache *regs, int regno) override;
1585 void store_registers (regcache *regs, int regno) override;
1586
1587 enum target_xfer_status xfer_partial (enum target_object object,
1588 const char *annex, gdb_byte *readbuf,
1589 const gdb_byte *writebuf,
1590 ULONGEST offset, ULONGEST len,
1591 ULONGEST *xfered_len) override;
1592
1593 unsigned int fetch_registers_called = 0;
1594 unsigned int store_registers_called = 0;
1595 unsigned int xfer_partial_called = 0;
1596 };
1597
1598 void
1599 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1600 {
1601 /* Mark register available. */
1602 regs->raw_supply_zeroed (regno);
1603 this->fetch_registers_called++;
1604 }
1605
1606 void
1607 target_ops_no_register::store_registers (regcache *regs, int regno)
1608 {
1609 this->store_registers_called++;
1610 }
1611
1612 enum target_xfer_status
1613 target_ops_no_register::xfer_partial (enum target_object object,
1614 const char *annex, gdb_byte *readbuf,
1615 const gdb_byte *writebuf,
1616 ULONGEST offset, ULONGEST len,
1617 ULONGEST *xfered_len)
1618 {
1619 this->xfer_partial_called++;
1620
1621 *xfered_len = len;
1622 return TARGET_XFER_OK;
1623 }
1624
1625 class readwrite_regcache : public regcache
1626 {
1627 public:
1628 readwrite_regcache (process_stratum_target *target,
1629 struct gdbarch *gdbarch)
1630 : regcache (target, gdbarch, nullptr)
1631 {}
1632 };
1633
1634 /* Test regcache::cooked_read gets registers from raw registers and
1635 memory instead of target to_{fetch,store}_registers. */
1636
1637 static void
1638 cooked_read_test (struct gdbarch *gdbarch)
1639 {
1640 scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1641
1642 /* Test that read one raw register from regcache_no_target will go
1643 to the target layer. */
1644
1645 /* Find a raw register which size isn't zero. */
1646 int nonzero_regnum;
1647 for (nonzero_regnum = 0;
1648 nonzero_regnum < gdbarch_num_regs (gdbarch);
1649 nonzero_regnum++)
1650 {
1651 if (register_size (gdbarch, nonzero_regnum) != 0)
1652 break;
1653 }
1654
1655 readwrite_regcache readwrite (&mockctx.mock_target, gdbarch);
1656 gdb::def_vector<gdb_byte> buf (register_size (gdbarch, nonzero_regnum));
1657
1658 readwrite.raw_read (nonzero_regnum, buf.data ());
1659
1660 /* raw_read calls target_fetch_registers. */
1661 SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1662 mockctx.mock_target.reset ();
1663
1664 /* Mark all raw registers valid, so the following raw registers
1665 accesses won't go to target. */
1666 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1667 readwrite.raw_update (i);
1668
1669 mockctx.mock_target.reset ();
1670 /* Then, read all raw and pseudo registers, and don't expect calling
1671 to_{fetch,store}_registers. */
1672 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1673 {
1674 if (register_size (gdbarch, regnum) == 0)
1675 continue;
1676
1677 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1678
1679 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum,
1680 inner_buf.data ()));
1681
1682 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1683 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1684 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1685
1686 mockctx.mock_target.reset ();
1687 }
1688
1689 readonly_detached_regcache readonly (readwrite);
1690
1691 /* GDB may go to target layer to fetch all registers and memory for
1692 readonly regcache. */
1693 mockctx.mock_target.reset ();
1694
1695 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1696 {
1697 if (register_size (gdbarch, regnum) == 0)
1698 continue;
1699
1700 gdb::def_vector<gdb_byte> inner_buf (register_size (gdbarch, regnum));
1701 enum register_status status = readonly.cooked_read (regnum,
1702 inner_buf.data ());
1703
1704 if (regnum < gdbarch_num_regs (gdbarch))
1705 {
1706 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1707
1708 if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1709 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1710 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1711 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1712 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1713 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1714 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
1715 || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
1716 {
1717 /* Raw registers. If raw registers are not in save_reggroup,
1718 their status are unknown. */
1719 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1720 SELF_CHECK (status == REG_VALID);
1721 else
1722 SELF_CHECK (status == REG_UNKNOWN);
1723 }
1724 else
1725 SELF_CHECK (status == REG_VALID);
1726 }
1727 else
1728 {
1729 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
1730 SELF_CHECK (status == REG_VALID);
1731 else
1732 {
1733 /* If pseudo registers are not in save_reggroup, some of
1734 them can be computed from saved raw registers, but some
1735 of them are unknown. */
1736 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1737
1738 if (bfd_arch == bfd_arch_frv
1739 || bfd_arch == bfd_arch_m32c
1740 || bfd_arch == bfd_arch_mep
1741 || bfd_arch == bfd_arch_sh)
1742 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
1743 else if (bfd_arch == bfd_arch_mips
1744 || bfd_arch == bfd_arch_h8300)
1745 SELF_CHECK (status == REG_UNKNOWN);
1746 else
1747 SELF_CHECK (status == REG_VALID);
1748 }
1749 }
1750
1751 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1752 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
1753 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
1754
1755 mockctx.mock_target.reset ();
1756 }
1757 }
1758
1759 /* Test regcache::cooked_write by writing some expected contents to
1760 registers, and checking that contents read from registers and the
1761 expected contents are the same. */
1762
1763 static void
1764 cooked_write_test (struct gdbarch *gdbarch)
1765 {
1766 /* Error out if debugging something, because we're going to push the
1767 test target, which would pop any existing target. */
1768 if (current_top_target ()->stratum () >= process_stratum)
1769 error (_("target already pushed"));
1770
1771 /* Create a mock environment. A process_stratum target pushed. */
1772
1773 target_ops_no_register mock_target;
1774
1775 /* Push the process_stratum target so we can mock accessing
1776 registers. */
1777 push_target (&mock_target);
1778
1779 /* Pop it again on exit (return/exception). */
1780 struct on_exit
1781 {
1782 ~on_exit ()
1783 {
1784 pop_all_targets_at_and_above (process_stratum);
1785 }
1786 } pop_targets;
1787
1788 readwrite_regcache readwrite (&mock_target, gdbarch);
1789
1790 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
1791
1792 for (auto regnum = 0; regnum < num_regs; regnum++)
1793 {
1794 if (register_size (gdbarch, regnum) == 0
1795 || gdbarch_cannot_store_register (gdbarch, regnum))
1796 continue;
1797
1798 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1799
1800 if (bfd_arch == bfd_arch_sparc
1801 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
1802 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
1803 && gdbarch_ptr_bit (gdbarch) == 64
1804 && (regnum >= gdbarch_num_regs (gdbarch)
1805 && regnum <= gdbarch_num_regs (gdbarch) + 4))
1806 continue;
1807
1808 std::vector<gdb_byte> expected (register_size (gdbarch, regnum), 0);
1809 std::vector<gdb_byte> buf (register_size (gdbarch, regnum), 0);
1810 const auto type = register_type (gdbarch, regnum);
1811
1812 if (type->code () == TYPE_CODE_FLT
1813 || type->code () == TYPE_CODE_DECFLOAT)
1814 {
1815 /* Generate valid float format. */
1816 target_float_from_string (expected.data (), type, "1.25");
1817 }
1818 else if (type->code () == TYPE_CODE_INT
1819 || type->code () == TYPE_CODE_ARRAY
1820 || type->code () == TYPE_CODE_PTR
1821 || type->code () == TYPE_CODE_UNION
1822 || type->code () == TYPE_CODE_STRUCT)
1823 {
1824 if (bfd_arch == bfd_arch_ia64
1825 || (regnum >= gdbarch_num_regs (gdbarch)
1826 && (bfd_arch == bfd_arch_xtensa
1827 || bfd_arch == bfd_arch_bfin
1828 || bfd_arch == bfd_arch_m32c
1829 /* m68hc11 pseudo registers are in memory. */
1830 || bfd_arch == bfd_arch_m68hc11
1831 || bfd_arch == bfd_arch_m68hc12
1832 || bfd_arch == bfd_arch_s390))
1833 || (bfd_arch == bfd_arch_frv
1834 /* FRV pseudo registers except iacc0. */
1835 && regnum > gdbarch_num_regs (gdbarch)))
1836 {
1837 /* Skip setting the expected values for some architecture
1838 registers. */
1839 }
1840 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
1841 {
1842 /* RL78_PC_REGNUM */
1843 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
1844 expected[j] = j;
1845 }
1846 else
1847 {
1848 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
1849 expected[j] = j;
1850 }
1851 }
1852 else if (type->code () == TYPE_CODE_FLAGS)
1853 {
1854 /* No idea how to test flags. */
1855 continue;
1856 }
1857 else
1858 {
1859 /* If we don't know how to create the expected value for the
1860 this type, make it fail. */
1861 SELF_CHECK (0);
1862 }
1863
1864 readwrite.cooked_write (regnum, expected.data ());
1865
1866 SELF_CHECK (readwrite.cooked_read (regnum, buf.data ()) == REG_VALID);
1867 SELF_CHECK (expected == buf);
1868 }
1869 }
1870
1871 /* Verify that when two threads with the same ptid exist (from two different
1872 targets) and one of them changes ptid, we only update the appropriate
1873 regcaches. */
1874
1875 static void
1876 regcache_thread_ptid_changed ()
1877 {
1878 /* This test relies on the global regcache list to initially be empty. */
1879 registers_changed ();
1880
1881 /* Any arch will do. */
1882 gdbarch *arch = current_inferior ()->gdbarch;
1883
1884 /* Prepare two targets with one thread each, with the same ptid. */
1885 scoped_mock_context<test_target_ops> target1 (arch);
1886 scoped_mock_context<test_target_ops> target2 (arch);
1887 target2.mock_inferior.next = &target1.mock_inferior;
1888
1889 ptid_t old_ptid (111, 222);
1890 ptid_t new_ptid (111, 333);
1891
1892 target1.mock_inferior.pid = old_ptid.pid ();
1893 target1.mock_thread.ptid = old_ptid;
1894 target2.mock_inferior.pid = old_ptid.pid ();
1895 target2.mock_thread.ptid = old_ptid;
1896
1897 gdb_assert (regcaches.empty ());
1898
1899 /* Populate the regcaches container. */
1900 get_thread_arch_aspace_regcache (&target1.mock_target, old_ptid, arch,
1901 nullptr);
1902 get_thread_arch_aspace_regcache (&target2.mock_target, old_ptid, arch,
1903 nullptr);
1904
1905 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1906 auto regcache_count = [] (process_stratum_target *target, ptid_t ptid)
1907 -> int
1908 {
1909 auto ptid_regc_map_it = regcaches.find (target);
1910 if (ptid_regc_map_it != regcaches.end ())
1911 {
1912 auto &ptid_regc_map = ptid_regc_map_it->second;
1913 auto range = ptid_regc_map.equal_range (ptid);
1914 return std::distance (range.first, range.second);
1915 }
1916 return 0;
1917 };
1918
1919 gdb_assert (regcaches.size () == 2);
1920 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
1921 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
1922 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
1923 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
1924
1925 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
1926
1927 gdb_assert (regcaches.size () == 2);
1928 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
1929 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
1930 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
1931 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
1932
1933 /* Leave the regcache list empty. */
1934 registers_changed ();
1935 gdb_assert (regcaches.empty ());
1936 }
1937
1938 } // namespace selftests
1939 #endif /* GDB_SELF_TEST */
1940
1941 void _initialize_regcache ();
1942 void
1943 _initialize_regcache ()
1944 {
1945 regcache_descr_handle
1946 = gdbarch_data_register_post_init (init_regcache_descr);
1947
1948 gdb::observers::target_changed.attach (regcache_observer_target_changed);
1949 gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed);
1950
1951 add_com ("flushregs", class_maintenance, reg_flush_command,
1952 _("Force gdb to flush its register cache (maintainer command)."));
1953
1954 #if GDB_SELF_TEST
1955 selftests::register_test ("regcaches", selftests::regcaches_test);
1956
1957 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1958 selftests::cooked_read_test);
1959 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
1960 selftests::cooked_write_test);
1961 selftests::register_test ("regcache_thread_ptid_changed",
1962 selftests::regcache_thread_ptid_changed);
1963 #endif
1964 }