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