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