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