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