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