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