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