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