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