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