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