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