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