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