1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2024 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 "extract-store-integer.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 "regcache.h"
28 #include "reggroups.h"
29 #include "observable.h"
30 #include "regset.h"
31 #include <unordered_map>
32 #include "cli/cli-cmds.h"
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 regcache_descr
44 {
45 /* The architecture this descriptor belongs to. */
46 struct gdbarch *gdbarch = nullptr;
47
48 /* The raw register cache. Each raw (or hard) register is supplied
49 by the target interface. The raw cache should not contain
50 redundant information - if the PC is constructed from two
51 registers then those registers and not the PC lives in the raw
52 cache. */
53 long sizeof_raw_registers = 0;
54
55 /* The cooked register space. Each cooked register in the range
56 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
57 register. The remaining [NR_RAW_REGISTERS
58 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
59 both raw registers and memory by the architecture methods
60 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
61 int nr_cooked_registers = 0;
62 long sizeof_cooked_registers = 0;
63
64 /* Offset and size (in 8 bit bytes), of each register in the
65 register cache. All registers (including those in the range
66 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
67 offset. */
68 long *register_offset = nullptr;
69 long *sizeof_register = nullptr;
70
71 /* Cached table containing the type of each register. */
72 struct type **register_type = nullptr;
73 };
74
75 static const registry<gdbarch>::key<struct regcache_descr>
76 regcache_descr_handle;
77
78 static struct regcache_descr *
init_regcache_descr(struct gdbarch * gdbarch)79 init_regcache_descr (struct gdbarch *gdbarch)
80 {
81 int i;
82 struct regcache_descr *descr;
83 gdb_assert (gdbarch != NULL);
84
85 /* Create an initial, zero filled, table. */
86 descr = new struct regcache_descr;
87 descr->gdbarch = gdbarch;
88
89 /* Total size of the register space. The raw registers are mapped
90 directly onto the raw register cache while the pseudo's are
91 either mapped onto raw-registers or memory. */
92 descr->nr_cooked_registers = gdbarch_num_cooked_regs (gdbarch);
93
94 /* Fill in a table of register types. */
95 descr->register_type
96 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
97 struct type *);
98 for (i = 0; i < descr->nr_cooked_registers; i++)
99 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
100
101 /* Construct a strictly RAW register cache. Don't allow pseudo's
102 into the register cache. */
103
104 /* Lay out the register cache.
105
106 NOTE: cagney/2002-05-22: Only register_type () is used when
107 constructing the register cache. It is assumed that the
108 register's raw size, virtual size and type length are all the
109 same. */
110
111 {
112 long offset = 0;
113
114 descr->sizeof_register
115 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
116 descr->register_offset
117 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
118 for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
119 {
120 descr->sizeof_register[i] = descr->register_type[i]->length ();
121 descr->register_offset[i] = offset;
122 offset += descr->sizeof_register[i];
123 }
124 /* Set the real size of the raw register cache buffer. */
125 descr->sizeof_raw_registers = offset;
126
127 for (; i < descr->nr_cooked_registers; i++)
128 {
129 descr->sizeof_register[i] = descr->register_type[i]->length ();
130 descr->register_offset[i] = offset;
131 offset += descr->sizeof_register[i];
132 }
133 /* Set the real size of the readonly register cache buffer. */
134 descr->sizeof_cooked_registers = offset;
135 }
136
137 return descr;
138 }
139
140 static struct regcache_descr *
regcache_descr(struct gdbarch * gdbarch)141 regcache_descr (struct gdbarch *gdbarch)
142 {
143 struct regcache_descr *result = regcache_descr_handle.get (gdbarch);
144 if (result == nullptr)
145 {
146 result = init_regcache_descr (gdbarch);
147 regcache_descr_handle.set (gdbarch, result);
148 }
149
150 return result;
151 }
152
153 /* Utility functions returning useful register attributes stored in
154 the regcache descr. */
155
156 struct type *
register_type(struct gdbarch * gdbarch,int regnum)157 register_type (struct gdbarch *gdbarch, int regnum)
158 {
159 struct regcache_descr *descr = regcache_descr (gdbarch);
160
161 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
162 return descr->register_type[regnum];
163 }
164
165 /* Utility functions returning useful register attributes stored in
166 the regcache descr. */
167
168 int
register_size(struct gdbarch * gdbarch,int regnum)169 register_size (struct gdbarch *gdbarch, int regnum)
170 {
171 struct regcache_descr *descr = regcache_descr (gdbarch);
172 int size;
173
174 gdb_assert (regnum >= 0 && regnum < gdbarch_num_cooked_regs (gdbarch));
175 size = descr->sizeof_register[regnum];
176 return size;
177 }
178
179 /* See gdbsupport/common-regcache.h. */
180
181 int
regcache_register_size(const reg_buffer_common * regcache,int n)182 regcache_register_size (const reg_buffer_common *regcache, int n)
183 {
184 return register_size
185 (gdb::checked_static_cast<const struct regcache *> (regcache)->arch (), n);
186 }
187
reg_buffer(gdbarch * gdbarch,bool has_pseudo)188 reg_buffer::reg_buffer (gdbarch *gdbarch, bool has_pseudo)
189 : m_has_pseudo (has_pseudo)
190 {
191 gdb_assert (gdbarch != NULL);
192 m_descr = regcache_descr (gdbarch);
193
194 /* We don't zero-initialize the M_REGISTERS array, as the bytes it contains
195 aren't meaningful as long as the corresponding register status is not
196 REG_VALID. */
197 if (has_pseudo)
198 {
199 m_registers.reset (new gdb_byte[m_descr->sizeof_cooked_registers]);
200 m_register_status.reset
201 (new register_status[m_descr->nr_cooked_registers] ());
202 }
203 else
204 {
205 m_registers.reset (new gdb_byte[m_descr->sizeof_raw_registers]);
206 m_register_status.reset
207 (new register_status[gdbarch_num_regs (gdbarch)] ());
208 }
209 }
210
regcache(inferior * inf_for_target_calls,gdbarch * gdbarch)211 regcache::regcache (inferior *inf_for_target_calls, gdbarch *gdbarch)
212 /* The register buffers. A read/write register cache can only hold
213 [0 .. gdbarch_num_regs). */
214 : detached_regcache (gdbarch, false),
215 m_inf_for_target_calls (inf_for_target_calls)
216 {
217 m_ptid = minus_one_ptid;
218 }
219
readonly_detached_regcache(regcache & src)220 readonly_detached_regcache::readonly_detached_regcache (regcache &src)
221 : readonly_detached_regcache (src.arch (),
222 [&src] (int regnum,
223 gdb::array_view<gdb_byte> buf)
224 { return src.cooked_read (regnum, buf); })
225 {
226 }
227
228 gdbarch *
arch()229 reg_buffer::arch () const
230 {
231 return m_descr->gdbarch;
232 }
233
234 /* Helper for reg_buffer::register_buffer. */
235
236 template<typename ElemType>
237 gdb::array_view<ElemType>
register_buffer(int regnum)238 reg_buffer::register_buffer (int regnum) const
239 {
240 assert_regnum (regnum);
241 ElemType *start = &m_registers[m_descr->register_offset[regnum]];
242 int size = m_descr->sizeof_register[regnum];
243 return gdb::array_view<ElemType> (start, size);
244 }
245
246 /* See regcache.h. */
247
248 gdb::array_view<const gdb_byte>
register_buffer(int regnum)249 reg_buffer::register_buffer (int regnum) const
250 {
251 return register_buffer<const gdb_byte> (regnum);
252 }
253
254 /* See regcache.h. */
255
256 gdb::array_view<gdb_byte>
register_buffer(int regnum)257 reg_buffer::register_buffer (int regnum)
258 {
259 return register_buffer<gdb_byte> (regnum);
260 }
261
262 void
save(register_read_ftype cooked_read)263 reg_buffer::save (register_read_ftype cooked_read)
264 {
265 struct gdbarch *gdbarch = m_descr->gdbarch;
266
267 /* It should have pseudo registers. */
268 gdb_assert (m_has_pseudo);
269 /* Clear the dest. */
270 memset (m_registers.get (), 0, m_descr->sizeof_cooked_registers);
271 memset (m_register_status.get (), REG_UNKNOWN, m_descr->nr_cooked_registers);
272 /* Copy over any registers (identified by their membership in the
273 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
274 gdbarch_num_pseudo_regs) range is checked since some architectures need
275 to save/restore `cooked' registers that live in memory. */
276 for (int regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
277 {
278 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
279 {
280 gdb::array_view<gdb_byte> dst_buf = register_buffer (regnum);
281 register_status status = cooked_read (regnum, dst_buf);
282
283 gdb_assert (status != REG_UNKNOWN);
284
285 if (status != REG_VALID)
286 memset (dst_buf.data (), 0, dst_buf.size ());
287
288 m_register_status[regnum] = status;
289 }
290 }
291 }
292
293 void
restore(readonly_detached_regcache * src)294 regcache::restore (readonly_detached_regcache *src)
295 {
296 struct gdbarch *gdbarch = m_descr->gdbarch;
297 int regnum;
298
299 gdb_assert (src != NULL);
300 gdb_assert (src->m_has_pseudo);
301
302 gdb_assert (gdbarch == src->arch ());
303
304 /* Copy over any registers, being careful to only restore those that
305 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
306 + gdbarch_num_pseudo_regs) range is checked since some architectures need
307 to save/restore `cooked' registers that live in memory. */
308 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
309 {
310 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
311 {
312 if (src->m_register_status[regnum] == REG_VALID)
313 cooked_write (regnum, src->register_buffer (regnum));
314 }
315 }
316 }
317
318 /* See gdbsupport/common-regcache.h. */
319
320 enum register_status
get_register_status(int regnum)321 reg_buffer::get_register_status (int regnum) const
322 {
323 assert_regnum (regnum);
324
325 return m_register_status[regnum];
326 }
327
328 void
invalidate(int regnum)329 reg_buffer::invalidate (int regnum)
330 {
331 assert_regnum (regnum);
332 m_register_status[regnum] = REG_UNKNOWN;
333 }
334
335 void
assert_regnum(int regnum)336 reg_buffer::assert_regnum (int regnum) const
337 {
338 gdb_assert (regnum >= 0);
339 if (m_has_pseudo)
340 gdb_assert (regnum < m_descr->nr_cooked_registers);
341 else
342 gdb_assert (regnum < gdbarch_num_regs (arch ()));
343 }
344
345 /* Type to map a ptid to a list of regcaches (one thread may have multiple
346 regcaches, associated to different gdbarches). */
347
348 using ptid_regcache_map
349 = std::unordered_multimap<ptid_t, regcache_up>;
350
351 /* Type holding regcaches for a given pid. */
352
353 using pid_ptid_regcache_map = std::unordered_map<int, ptid_regcache_map>;
354
355 /* Type holding regcaches for a given target. */
356
357 using target_pid_ptid_regcache_map
358 = std::unordered_map<process_stratum_target *, pid_ptid_regcache_map>;
359
360 /* Global structure containing the existing regcaches. */
361
362 /* NOTE: this is a write-through cache. There is no "dirty" bit for
363 recording if the register values have been changed (eg. by the
364 user). Therefore all registers must be written back to the
365 target when appropriate. */
366 static target_pid_ptid_regcache_map regcaches;
367
368 regcache *
get_thread_arch_regcache(inferior * inf_for_target_calls,ptid_t ptid,gdbarch * arch)369 get_thread_arch_regcache (inferior *inf_for_target_calls, ptid_t ptid,
370 gdbarch *arch)
371 {
372 gdb_assert (inf_for_target_calls != nullptr);
373
374 process_stratum_target *proc_target = inf_for_target_calls->process_target ();
375 gdb_assert (proc_target != nullptr);
376
377 /* Find the map for this target. */
378 pid_ptid_regcache_map &pid_ptid_regc_map = regcaches[proc_target];
379
380 /* Find the map for this pid. */
381 ptid_regcache_map &ptid_regc_map = pid_ptid_regc_map[ptid.pid ()];
382
383 /* Check first if a regcache for this arch already exists. */
384 auto range = ptid_regc_map.equal_range (ptid);
385 for (auto it = range.first; it != range.second; ++it)
386 {
387 if (it->second->arch () == arch)
388 return it->second.get ();
389 }
390
391 /* It does not exist, create it. */
392 regcache *new_regcache = new regcache (inf_for_target_calls, arch);
393 new_regcache->set_ptid (ptid);
394 /* Work around a problem with g++ 4.8 (PR96537): Call the regcache_up
395 constructor explicitly instead of implicitly. */
396 ptid_regc_map.insert (std::make_pair (ptid, regcache_up (new_regcache)));
397
398 return new_regcache;
399 }
400
401 static process_stratum_target *current_thread_target;
402 static ptid_t current_thread_ptid;
403 static struct gdbarch *current_thread_arch;
404
405 struct regcache *
get_thread_regcache(process_stratum_target * target,ptid_t ptid)406 get_thread_regcache (process_stratum_target *target, ptid_t ptid)
407 {
408 inferior *inf = find_inferior_ptid (target, ptid);
409
410 if (!current_thread_arch
411 || target != current_thread_target
412 || current_thread_ptid != ptid)
413 {
414 gdb_assert (ptid != null_ptid);
415
416 current_thread_ptid = ptid;
417 current_thread_target = target;
418
419 scoped_restore_current_inferior restore_current_inferior;
420 set_current_inferior (inf);
421 current_thread_arch = target_thread_architecture (ptid);
422 }
423
424 return get_thread_arch_regcache (inf, ptid, current_thread_arch);
425 }
426
427 /* See regcache.h. */
428
429 struct regcache *
get_thread_regcache(thread_info * thread)430 get_thread_regcache (thread_info *thread)
431 {
432 gdb_assert (thread->state != THREAD_EXITED);
433
434 return get_thread_regcache (thread->inf->process_target (),
435 thread->ptid);
436 }
437
438 /* See gdbsupport/common-regcache.h. */
439
440 reg_buffer_common *
get_thread_regcache_for_ptid(ptid_t ptid)441 get_thread_regcache_for_ptid (ptid_t ptid)
442 {
443 /* This function doesn't take a process_stratum_target parameter
444 because it's a gdbsupport/ routine implemented by both gdb and
445 gdbserver. It always refers to a ptid of the current target. */
446 process_stratum_target *proc_target = current_inferior ()->process_target ();
447 return get_thread_regcache (proc_target, ptid);
448 }
449
450 /* Observer for the target_changed event. */
451
452 static void
regcache_observer_target_changed(struct target_ops * target)453 regcache_observer_target_changed (struct target_ops *target)
454 {
455 registers_changed ();
456 }
457
458 /* Update regcaches related to OLD_PTID to now use NEW_PTID. */
459 static void
regcache_thread_ptid_changed(process_stratum_target * target,ptid_t old_ptid,ptid_t new_ptid)460 regcache_thread_ptid_changed (process_stratum_target *target,
461 ptid_t old_ptid, ptid_t new_ptid)
462 {
463 /* Look up map for target. */
464 auto pid_ptid_regc_map_it = regcaches.find (target);
465 if (pid_ptid_regc_map_it == regcaches.end ())
466 return;
467
468 /* Look up map for pid. */
469 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
470 auto ptid_regc_map_it = pid_ptid_regc_map.find (old_ptid.pid ());
471 if (ptid_regc_map_it == pid_ptid_regc_map.end ())
472 return;
473
474 /* Update all regcaches belonging to old_ptid. */
475 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
476 auto range = ptid_regc_map.equal_range (old_ptid);
477 for (auto it = range.first; it != range.second;)
478 {
479 regcache_up rc = std::move (it->second);
480 rc->set_ptid (new_ptid);
481
482 /* Remove old before inserting new, to avoid rehashing,
483 which would invalidate iterators. */
484 it = ptid_regc_map.erase (it);
485 ptid_regc_map.insert (std::make_pair (new_ptid, std::move (rc)));
486 }
487 }
488
489 /* Low level examining and depositing of registers.
490
491 The caller is responsible for making sure that the inferior is
492 stopped before calling the fetching routines, or it will get
493 garbage. (a change from GDB version 3, in which the caller got the
494 value from the last stop). */
495
496 /* REGISTERS_CHANGED ()
497
498 Indicate that registers may have changed, so invalidate the cache. */
499
500 void
registers_changed_ptid(process_stratum_target * target,ptid_t ptid)501 registers_changed_ptid (process_stratum_target *target, ptid_t ptid)
502 {
503 if (target == nullptr)
504 {
505 /* Since there can be ptid clashes between targets, it's not valid to
506 pass a ptid without saying to which target it belongs. */
507 gdb_assert (ptid == minus_one_ptid);
508
509 /* Delete all the regcaches of all targets. */
510 regcaches.clear ();
511 }
512 else if (ptid.is_pid ())
513 {
514 /* Non-NULL target and pid ptid, delete all regcaches belonging
515 to this (TARGET, PID). */
516
517 /* Look up map for target. */
518 auto pid_ptid_regc_map_it = regcaches.find (target);
519 if (pid_ptid_regc_map_it != regcaches.end ())
520 {
521 pid_ptid_regcache_map &pid_ptid_regc_map
522 = pid_ptid_regc_map_it->second;
523
524 pid_ptid_regc_map.erase (ptid.pid ());
525 }
526 }
527 else if (ptid != minus_one_ptid)
528 {
529 /* Non-NULL target and non-minus_one_ptid, delete all regcaches belonging
530 to this (TARGET, PTID). */
531
532 /* Look up map for target. */
533 auto pid_ptid_regc_map_it = regcaches.find (target);
534 if (pid_ptid_regc_map_it != regcaches.end ())
535 {
536 pid_ptid_regcache_map &pid_ptid_regc_map
537 = pid_ptid_regc_map_it->second;
538
539 /* Look up map for pid. */
540 auto ptid_regc_map_it
541 = pid_ptid_regc_map.find (ptid.pid ());
542 if (ptid_regc_map_it != pid_ptid_regc_map.end ())
543 {
544 ptid_regcache_map &ptid_regc_map
545 = ptid_regc_map_it->second;
546
547 ptid_regc_map.erase (ptid);
548 }
549 }
550 }
551 else
552 {
553 /* Non-NULL target and minus_one_ptid, delete all regcaches
554 associated to this target. */
555 regcaches.erase (target);
556 }
557
558 if ((target == nullptr || current_thread_target == target)
559 && current_thread_ptid.matches (ptid))
560 {
561 current_thread_target = NULL;
562 current_thread_ptid = null_ptid;
563 current_thread_arch = NULL;
564 }
565
566 if ((target == nullptr || current_inferior ()->process_target () == target)
567 && inferior_ptid.matches (ptid))
568 {
569 /* We just deleted the regcache of the current thread. Need to
570 forget about any frames we have cached, too. */
571 reinit_frame_cache ();
572 }
573 }
574
575 /* See regcache.h. */
576
577 void
registers_changed_thread(thread_info * thread)578 registers_changed_thread (thread_info *thread)
579 {
580 registers_changed_ptid (thread->inf->process_target (), thread->ptid);
581 }
582
583 void
registers_changed(void)584 registers_changed (void)
585 {
586 registers_changed_ptid (nullptr, minus_one_ptid);
587 }
588
589 void
raw_update(int regnum)590 regcache::raw_update (int regnum)
591 {
592 assert_regnum (regnum);
593
594 /* Make certain that the register cache is up-to-date with respect
595 to the current thread. This switching shouldn't be necessary
596 only there is still only one target side register cache. Sigh!
597 On the bright side, at least there is a regcache object. */
598
599 if (get_register_status (regnum) == REG_UNKNOWN)
600 {
601 std::optional<scoped_restore_current_thread> maybe_restore_thread
602 = maybe_switch_inferior (m_inf_for_target_calls);
603
604 target_fetch_registers (this, regnum);
605
606 /* A number of targets can't access the whole set of raw
607 registers (because the debug API provides no means to get at
608 them). */
609 if (m_register_status[regnum] == REG_UNKNOWN)
610 m_register_status[regnum] = REG_UNAVAILABLE;
611 }
612 }
613
614 register_status
raw_read(int regnum,gdb::array_view<gdb_byte> dst)615 readable_regcache::raw_read (int regnum, gdb::array_view<gdb_byte> dst)
616 {
617 assert_regnum (regnum);
618 gdb_assert (dst.size () == m_descr->sizeof_register[regnum]);
619
620 raw_update (regnum);
621
622 if (m_register_status[regnum] != REG_VALID)
623 memset (dst.data (), 0, dst.size ());
624 else
625 copy (register_buffer (regnum), dst);
626
627 return m_register_status[regnum];
628 }
629
630 register_status
raw_read(int regnum,gdb_byte * dst)631 readable_regcache::raw_read (int regnum, gdb_byte *dst)
632 {
633 assert_regnum (regnum);
634 int size = m_descr->sizeof_register[regnum];
635 return raw_read (regnum, gdb::make_array_view (dst, size));
636 }
637
638 enum register_status
regcache_raw_read_signed(struct regcache * regcache,int regnum,LONGEST * val)639 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
640 {
641 gdb_assert (regcache != NULL);
642 return regcache->raw_read (regnum, val);
643 }
644
645 template<typename T, typename>
646 enum register_status
raw_read(int regnum,T * val)647 readable_regcache::raw_read (int regnum, T *val)
648 {
649 assert_regnum (regnum);
650 size_t size = m_descr->sizeof_register[regnum];
651 gdb_byte *buf = (gdb_byte *) alloca (size);
652 auto view = gdb::make_array_view (buf, size);
653 register_status status = raw_read (regnum, view);
654
655 if (status == REG_VALID)
656 *val = extract_integer<T> (view, gdbarch_byte_order (m_descr->gdbarch));
657 else
658 *val = 0;
659
660 return status;
661 }
662
663 enum register_status
regcache_raw_read_unsigned(reg_buffer_common * regcache,int regnum,ULONGEST * val)664 regcache_raw_read_unsigned (reg_buffer_common *regcache, int regnum,
665 ULONGEST *val)
666 {
667 gdb_assert (regcache != NULL);
668 return gdb::checked_static_cast<struct regcache *> (regcache)->raw_read
669 (regnum, val);
670 }
671
672 void
regcache_raw_write_signed(struct regcache * regcache,int regnum,LONGEST val)673 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
674 {
675 gdb_assert (regcache != NULL);
676 regcache->raw_write (regnum, val);
677 }
678
679 template<typename T, typename>
680 void
raw_write(int regnum,T val)681 regcache::raw_write (int regnum, T val)
682 {
683 assert_regnum (regnum);
684
685 int size = m_descr->sizeof_register[regnum];
686 gdb_byte *buf = (gdb_byte *) alloca (size);
687 auto view = gdb::make_array_view (buf, size);
688 store_integer (view, gdbarch_byte_order (m_descr->gdbarch), val);
689 raw_write (regnum, view);
690 }
691
692 void
regcache_raw_write_unsigned(struct regcache * regcache,int regnum,ULONGEST val)693 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
694 ULONGEST val)
695 {
696 gdb_assert (regcache != NULL);
697 regcache->raw_write (regnum, val);
698 }
699
700 LONGEST
regcache_raw_get_signed(struct regcache * regcache,int regnum)701 regcache_raw_get_signed (struct regcache *regcache, int regnum)
702 {
703 LONGEST value;
704 enum register_status status;
705
706 status = regcache_raw_read_signed (regcache, regnum, &value);
707 if (status == REG_UNAVAILABLE)
708 throw_error (NOT_AVAILABLE_ERROR,
709 _("Register %d is not available"), regnum);
710 return value;
711 }
712
713 /* See regcache.h. */
714
715 register_status
cooked_read(int regnum,gdb::array_view<gdb_byte> dst)716 readable_regcache::cooked_read (int regnum, gdb::array_view<gdb_byte> dst)
717 {
718 gdb_assert (regnum >= 0);
719 gdb_assert (regnum < m_descr->nr_cooked_registers);
720
721 if (regnum < num_raw_registers ())
722 return raw_read (regnum, dst);
723
724 gdb_assert (dst.size () == m_descr->sizeof_register[regnum]);
725
726 if (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
727 {
728 if (m_register_status[regnum] == REG_VALID)
729 copy (register_buffer (regnum), dst);
730 else
731 memset (dst.data (), 0, dst.size ());
732
733 return m_register_status[regnum];
734 }
735 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
736 {
737 register_status result = REG_VALID;
738 scoped_value_mark mark;
739 value *computed = gdbarch_pseudo_register_read_value
740 (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()),
741 regnum);
742
743 if (computed->entirely_available ())
744 copy (computed->contents_raw (), dst);
745 else
746 {
747 memset (dst.data (), 0, dst.size ());
748 result = REG_UNAVAILABLE;
749 }
750
751 return result;
752 }
753 else
754 return gdbarch_pseudo_register_read (m_descr->gdbarch, this, regnum,
755 dst.data ());
756 }
757
758 /* See regcache.h. */
759
760 register_status
cooked_read(int regnum,gdb_byte * dst)761 readable_regcache::cooked_read (int regnum, gdb_byte *dst)
762 {
763 gdb_assert (regnum >= 0);
764 gdb_assert (regnum < m_descr->nr_cooked_registers);
765
766 int size = m_descr->sizeof_register[regnum];
767 return cooked_read (regnum, gdb::make_array_view (dst, size));
768 }
769
770 struct value *
cooked_read_value(int regnum)771 readable_regcache::cooked_read_value (int regnum)
772 {
773 gdb_assert (regnum >= 0);
774 gdb_assert (regnum < m_descr->nr_cooked_registers);
775
776 if (regnum < num_raw_registers ()
777 || (m_has_pseudo && m_register_status[regnum] != REG_UNKNOWN)
778 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
779 {
780 value *result = value::allocate_register
781 (get_next_frame_sentinel_okay (get_current_frame ()), regnum);
782
783 /* It is more efficient in general to do this delegation in this
784 direction than in the other one, even though the value-based
785 API is preferred. */
786 if (cooked_read (regnum, result->contents_raw ()) == REG_UNAVAILABLE)
787 result->mark_bytes_unavailable (0,
788 result->type ()->length ());
789
790 return result;
791 }
792 else
793 return gdbarch_pseudo_register_read_value
794 (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()),
795 regnum);
796 }
797
798 enum register_status
regcache_cooked_read_signed(struct regcache * regcache,int regnum,LONGEST * val)799 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
800 LONGEST *val)
801 {
802 gdb_assert (regcache != NULL);
803 return regcache->cooked_read (regnum, val);
804 }
805
806 template<typename T, typename>
807 enum register_status
cooked_read(int regnum,T * val)808 readable_regcache::cooked_read (int regnum, T *val)
809 {
810 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
811 size_t size = m_descr->sizeof_register[regnum];
812 gdb_byte *buf = (gdb_byte *) alloca (size);
813 auto view = gdb::make_array_view (buf, size);
814 register_status status = cooked_read (regnum, view);
815 if (status == REG_VALID)
816 *val = extract_integer<T> (view, gdbarch_byte_order (m_descr->gdbarch));
817 else
818 *val = 0;
819 return status;
820 }
821
822 enum register_status
regcache_cooked_read_unsigned(struct regcache * regcache,int regnum,ULONGEST * val)823 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
824 ULONGEST *val)
825 {
826 gdb_assert (regcache != NULL);
827 return regcache->cooked_read (regnum, val);
828 }
829
830 void
regcache_cooked_write_signed(struct regcache * regcache,int regnum,LONGEST val)831 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
832 LONGEST val)
833 {
834 gdb_assert (regcache != NULL);
835 regcache->cooked_write (regnum, val);
836 }
837
838 template<typename T, typename>
839 void
cooked_write(int regnum,T val)840 regcache::cooked_write (int regnum, T val)
841 {
842 gdb_assert (regnum >= 0);
843 gdb_assert (regnum < m_descr->nr_cooked_registers);
844
845 int size = m_descr->sizeof_register[regnum];
846 gdb_byte *buf = (gdb_byte *) alloca (size);
847 auto view = gdb::make_array_view (buf, size);
848 store_integer (view, gdbarch_byte_order (m_descr->gdbarch), val);
849 cooked_write (regnum, view);
850 }
851
852 void
regcache_cooked_write_unsigned(struct regcache * regcache,int regnum,ULONGEST val)853 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
854 ULONGEST val)
855 {
856 gdb_assert (regcache != NULL);
857 regcache->cooked_write (regnum, val);
858 }
859
860 void
raw_write(int regnum,gdb::array_view<const gdb_byte> src)861 regcache::raw_write (int regnum, gdb::array_view<const gdb_byte> src)
862 {
863 assert_regnum (regnum);
864 gdb_assert (src.size () == m_descr->sizeof_register[regnum]);
865
866 /* On the sparc, writing %g0 is a no-op, so we don't even want to
867 change the registers array if something writes to this register. */
868 if (gdbarch_cannot_store_register (arch (), regnum))
869 return;
870
871 /* If we have a valid copy of the register, and new value == old
872 value, then don't bother doing the actual store. */
873 if (get_register_status (regnum) == REG_VALID
874 && (memcmp (register_buffer (regnum).data (), src.data (), src.size ())
875 == 0))
876 return;
877
878 std::optional<scoped_restore_current_thread> maybe_restore_thread
879 = maybe_switch_inferior (m_inf_for_target_calls);
880
881 target_prepare_to_store (this);
882 raw_supply (regnum, src);
883
884 /* Invalidate the register after it is written, in case of a
885 failure. */
886 auto invalidator
887 = make_scope_exit ([&] { this->invalidate (regnum); });
888
889 target_store_registers (this, regnum);
890
891 /* The target did not throw an error so we can discard invalidating
892 the register. */
893 invalidator.release ();
894 }
895
896 void
raw_write(int regnum,const gdb_byte * src)897 regcache::raw_write (int regnum, const gdb_byte *src)
898 {
899 assert_regnum (regnum);
900
901 int size = m_descr->sizeof_register[regnum];
902 raw_write (regnum, gdb::make_array_view (src, size));
903 }
904
905 /* See regcache.h. */
906
907 void
cooked_write(int regnum,gdb::array_view<const gdb_byte> src)908 regcache::cooked_write (int regnum, gdb::array_view<const gdb_byte> src)
909 {
910 gdb_assert (regnum >= 0);
911 gdb_assert (regnum < m_descr->nr_cooked_registers);
912
913 if (regnum < num_raw_registers ())
914 raw_write (regnum, src);
915 else if (gdbarch_pseudo_register_write_p (m_descr->gdbarch))
916 gdbarch_pseudo_register_write
917 (m_descr->gdbarch, get_next_frame_sentinel_okay (get_current_frame ()),
918 regnum, src);
919 else
920 gdbarch_deprecated_pseudo_register_write (m_descr->gdbarch, this, regnum,
921 src.data ());
922 }
923
924 /* See regcache.h. */
925
926 void
cooked_write(int regnum,const gdb_byte * src)927 regcache::cooked_write (int regnum, const gdb_byte *src)
928 {
929 gdb_assert (regnum >= 0);
930 gdb_assert (regnum < m_descr->nr_cooked_registers);
931
932 int size = m_descr->sizeof_register[regnum];
933 return cooked_write (regnum, gdb::make_array_view (src, size));
934 }
935
936 /* See regcache.h. */
937
938 register_status
read_part(int regnum,int offset,gdb::array_view<gdb_byte> dst,bool is_raw)939 readable_regcache::read_part (int regnum, int offset,
940 gdb::array_view<gdb_byte> dst, bool is_raw)
941 {
942 int reg_size = register_size (arch (), regnum);
943
944 gdb_assert (offset >= 0);
945 gdb_assert (offset + dst.size () <= reg_size);
946
947 if (dst.size () == 0)
948 {
949 /* Nothing to do. */
950 return REG_VALID;
951 }
952
953 if (dst.size () == reg_size)
954 {
955 /* Read the full register. */
956 if (is_raw)
957 return raw_read (regnum, dst);
958 else
959 return cooked_read (regnum, dst);
960 }
961
962 /* Read full register to buffer. */
963 register_status status;
964 gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size);
965 auto reg = gdb::make_array_view (reg_buf, reg_size);
966
967 if (is_raw)
968 status = raw_read (regnum, reg);
969 else
970 status = cooked_read (regnum, reg);
971
972 if (status != REG_VALID)
973 return status;
974
975 /* Copy out. */
976 copy (reg.slice (offset, dst.size ()), dst);
977 return REG_VALID;
978 }
979
980 /* See regcache.h. */
981
982 void
raw_collect_part(int regnum,int offset,gdb::array_view<gdb_byte> dst)983 reg_buffer::raw_collect_part (int regnum, int offset,
984 gdb::array_view<gdb_byte> dst) const
985 {
986 int reg_size = register_size (arch (), regnum);
987
988 gdb_assert (offset >= 0);
989 gdb_assert (offset + dst.size () <= reg_size);
990
991 if (dst.size () == 0)
992 {
993 /* Nothing to do. */
994 return;
995 }
996
997 if (dst.size () == reg_size)
998 {
999 /* Collect the full register. */
1000 return raw_collect (regnum, dst);
1001 }
1002
1003 /* Read to buffer, then write out. */
1004 gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size);
1005 auto reg = gdb::make_array_view (reg_buf, reg_size);
1006 raw_collect (regnum, reg);
1007 copy (reg.slice (offset, dst.size ()), dst);
1008 }
1009
1010 /* See regcache.h. */
1011
1012 register_status
write_part(int regnum,int offset,gdb::array_view<const gdb_byte> src,bool is_raw)1013 regcache::write_part (int regnum, int offset,
1014 gdb::array_view<const gdb_byte> src, bool is_raw)
1015 {
1016 int reg_size = register_size (arch (), regnum);
1017
1018 gdb_assert (offset >= 0);
1019 gdb_assert (offset + src.size () <= reg_size);
1020
1021 if (src.size () == 0)
1022 {
1023 /* Nothing to do. */
1024 return REG_VALID;
1025 }
1026
1027 if (src.size () == reg_size)
1028 {
1029 /* Write the full register. */
1030 if (is_raw)
1031 raw_write (regnum, src);
1032 else
1033 cooked_write (regnum, src);
1034
1035 return REG_VALID;
1036 }
1037
1038 /* Read existing register to buffer. */
1039 register_status status;
1040 gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size);
1041 auto reg = gdb::make_array_view (reg_buf, reg_size);
1042
1043 if (is_raw)
1044 status = raw_read (regnum, reg);
1045 else
1046 status = cooked_read (regnum, reg);
1047
1048 if (status != REG_VALID)
1049 return status;
1050
1051 /* Update buffer, then write back to regcache. */
1052 copy (src, reg.slice (offset, src.size ()));
1053
1054 if (is_raw)
1055 raw_write (regnum, reg);
1056 else
1057 cooked_write (regnum, reg);
1058
1059 return REG_VALID;
1060 }
1061
1062 /* See regcache.h. */
1063
1064 void
raw_supply_part(int regnum,int offset,gdb::array_view<const gdb_byte> src)1065 reg_buffer::raw_supply_part (int regnum, int offset,
1066 gdb::array_view<const gdb_byte> src)
1067 {
1068 int reg_size = register_size (arch (), regnum);
1069
1070 gdb_assert (offset >= 0);
1071 gdb_assert (offset + src.size () <= reg_size);
1072
1073 if (src.size () == 0)
1074 {
1075 /* Nothing to do. */
1076 return;
1077 }
1078
1079 if (src.size () == reg_size)
1080 {
1081 /* Supply the full register. */
1082 return raw_supply (regnum, src);
1083 }
1084
1085 /* Read existing value to buffer. */
1086 gdb_byte *reg_buf = (gdb_byte *) alloca (reg_size);
1087 auto reg = gdb::make_array_view (reg_buf, reg_size);
1088 raw_collect (regnum, reg);
1089
1090 /* Write to buffer, then write out. */
1091 copy (src, reg.slice (offset, src.size ()));
1092 raw_supply (regnum, reg);
1093 }
1094
1095 register_status
raw_read_part(int regnum,int offset,gdb::array_view<gdb_byte> dst)1096 readable_regcache::raw_read_part (int regnum, int offset,
1097 gdb::array_view<gdb_byte> dst)
1098 {
1099 assert_regnum (regnum);
1100 return read_part (regnum, offset, dst, true);
1101 }
1102
1103 /* See regcache.h. */
1104
1105 void
raw_write_part(int regnum,int offset,gdb::array_view<const gdb_byte> src)1106 regcache::raw_write_part (int regnum, int offset,
1107 gdb::array_view<const gdb_byte> src)
1108 {
1109 assert_regnum (regnum);
1110 write_part (regnum, offset, src, true);
1111 }
1112
1113 /* See regcache.h. */
1114
1115 register_status
cooked_read_part(int regnum,int offset,gdb::array_view<gdb_byte> dst)1116 readable_regcache::cooked_read_part (int regnum, int offset,
1117 gdb::array_view<gdb_byte> dst)
1118 {
1119 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1120 return read_part (regnum, offset, dst, false);
1121 }
1122
1123 /* See regcache.h. */
1124
1125 void
cooked_write_part(int regnum,int offset,gdb::array_view<const gdb_byte> src)1126 regcache::cooked_write_part (int regnum, int offset,
1127 gdb::array_view<const gdb_byte> src)
1128 {
1129 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1130 write_part (regnum, offset, src, false);
1131 }
1132
1133 /* See gdbsupport/common-regcache.h. */
1134
1135 void
raw_supply(int regnum,gdb::array_view<const gdb_byte> src)1136 reg_buffer::raw_supply (int regnum, gdb::array_view<const gdb_byte> src)
1137 {
1138 gdb::array_view<gdb_byte> dst = register_buffer (regnum);
1139
1140 if (src.data () != nullptr)
1141 {
1142 copy (src, dst);
1143 m_register_status[regnum] = REG_VALID;
1144 }
1145 else
1146 {
1147 /* This memset not strictly necessary, but better than garbage
1148 in case the register value manages to escape somewhere (due
1149 to a bug, no less). */
1150 memset (dst.data (), 0, dst.size ());
1151 m_register_status[regnum] = REG_UNAVAILABLE;
1152 }
1153 }
1154
1155 /* See regcache.h. */
1156
1157 void
raw_supply(int regnum,const void * src)1158 reg_buffer::raw_supply (int regnum, const void *src)
1159 {
1160 assert_regnum (regnum);
1161
1162 int size = m_descr->sizeof_register[regnum];
1163 raw_supply (regnum, gdb::make_array_view ((const gdb_byte *) src, size));
1164 }
1165
1166 /* See regcache.h. */
1167
1168 void
raw_supply_integer(int regnum,const gdb_byte * addr,int addr_len,bool is_signed)1169 reg_buffer::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1170 bool is_signed)
1171 {
1172 gdb::array_view<gdb_byte> dst = register_buffer (regnum);
1173 bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1174
1175 copy_integer_to_size (dst.data (), dst.size (), addr, addr_len, is_signed,
1176 byte_order);
1177 m_register_status[regnum] = REG_VALID;
1178 }
1179
1180 /* See regcache.h. */
1181
1182 void
raw_supply_zeroed(int regnum)1183 reg_buffer::raw_supply_zeroed (int regnum)
1184 {
1185 gdb::array_view<gdb_byte> dst = register_buffer (regnum);
1186 memset (dst.data (), 0, dst.size ());
1187 m_register_status[regnum] = REG_VALID;
1188 }
1189
1190 /* See gdbsupport/common-regcache.h. */
1191
1192 void
raw_collect(int regnum,gdb::array_view<gdb_byte> dst)1193 reg_buffer::raw_collect (int regnum, gdb::array_view<gdb_byte> dst) const
1194 {
1195 gdb::array_view<const gdb_byte> src = register_buffer (regnum);
1196 copy (src, dst);
1197 }
1198
1199 /* See regcache.h. */
1200
1201 void
raw_collect(int regnum,void * dst)1202 reg_buffer::raw_collect (int regnum, void *dst) const
1203 {
1204 assert_regnum (regnum);
1205
1206 int size = m_descr->sizeof_register[regnum];
1207 return raw_collect (regnum, gdb::make_array_view ((gdb_byte *) dst, size));
1208 }
1209
1210 /* See regcache.h. */
1211
1212 void
raw_collect_integer(int regnum,gdb_byte * addr,int addr_len,bool is_signed)1213 reg_buffer::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1214 bool is_signed) const
1215 {
1216 gdb::array_view<const gdb_byte> dst = register_buffer (regnum);
1217 bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1218 copy_integer_to_size (addr, addr_len, dst.data (), dst.size (), is_signed,
1219 byte_order);
1220 }
1221
1222 /* See regcache.h. */
1223
1224 void
transfer_regset_register(struct regcache * out_regcache,int regnum,const gdb_byte * in_buf,gdb_byte * out_buf,int slot_size,int offs)1225 regcache::transfer_regset_register (struct regcache *out_regcache, int regnum,
1226 const gdb_byte *in_buf, gdb_byte *out_buf,
1227 int slot_size, int offs) const
1228 {
1229 struct gdbarch *gdbarch = arch ();
1230 int reg_size = std::min (register_size (gdbarch, regnum), slot_size);
1231
1232 /* Use part versions and reg_size to prevent possible buffer overflows when
1233 accessing the regcache. */
1234
1235 if (out_buf != nullptr)
1236 {
1237 raw_collect_part (regnum, 0,
1238 gdb::make_array_view (out_buf + offs, reg_size));
1239
1240 /* Ensure any additional space is cleared. */
1241 if (slot_size > reg_size)
1242 memset (out_buf + offs + reg_size, 0, slot_size - reg_size);
1243 }
1244 else if (in_buf != nullptr)
1245 {
1246 /* Zero-extend the register value if the slot is smaller than the register. */
1247 if (slot_size < register_size (gdbarch, regnum))
1248 out_regcache->raw_supply_zeroed (regnum);
1249 out_regcache->raw_supply_part (regnum, 0,
1250 gdb::make_array_view (in_buf + offs,
1251 reg_size));
1252 }
1253 else
1254 {
1255 /* Invalidate the register. */
1256 out_regcache->raw_supply (regnum, {});
1257 }
1258 }
1259
1260 /* See regcache.h. */
1261
1262 void
transfer_regset(const struct regset * regset,int regbase,struct regcache * out_regcache,int regnum,const gdb_byte * in_buf,gdb_byte * out_buf,size_t size)1263 regcache::transfer_regset (const struct regset *regset, int regbase,
1264 struct regcache *out_regcache,
1265 int regnum, const gdb_byte *in_buf,
1266 gdb_byte *out_buf, size_t size) const
1267 {
1268 const struct regcache_map_entry *map;
1269 int offs = 0, count;
1270
1271 for (map = (const struct regcache_map_entry *) regset->regmap;
1272 (count = map->count) != 0;
1273 map++)
1274 {
1275 int regno = map->regno;
1276 int slot_size = map->size;
1277
1278 if (regno != REGCACHE_MAP_SKIP)
1279 regno += regbase;
1280
1281 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1282 slot_size = m_descr->sizeof_register[regno];
1283
1284 if (regno == REGCACHE_MAP_SKIP
1285 || (regnum != -1
1286 && (regnum < regno || regnum >= regno + count)))
1287 offs += count * slot_size;
1288
1289 else if (regnum == -1)
1290 for (; count--; regno++, offs += slot_size)
1291 {
1292 if (offs + slot_size > size)
1293 return;
1294
1295 transfer_regset_register (out_regcache, regno, in_buf, out_buf,
1296 slot_size, offs);
1297 }
1298 else
1299 {
1300 /* Transfer a single register and return. */
1301 offs += (regnum - regno) * slot_size;
1302 if (offs + slot_size > size)
1303 return;
1304
1305 transfer_regset_register (out_regcache, regnum, in_buf, out_buf,
1306 slot_size, offs);
1307 return;
1308 }
1309 }
1310 }
1311
1312 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1313 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1314 If BUF is NULL, set the register(s) to "unavailable" status. */
1315
1316 void
regcache_supply_regset(const struct regset * regset,struct regcache * regcache,int regnum,const void * buf,size_t size)1317 regcache_supply_regset (const struct regset *regset,
1318 struct regcache *regcache,
1319 int regnum, const void *buf, size_t size)
1320 {
1321 regcache->supply_regset (regset, regnum, (const gdb_byte *) buf, size);
1322 }
1323
1324 /* See regcache.h. */
1325
1326 void
supply_regset(const struct regset * regset,int regbase,int regnum,const void * buf,size_t size)1327 regcache::supply_regset (const struct regset *regset, int regbase,
1328 int regnum, const void *buf, size_t size)
1329 {
1330 transfer_regset (regset, regbase, this, regnum, (const gdb_byte *) buf,
1331 nullptr, size);
1332 }
1333
1334 /* Collect register REGNUM from REGCACHE to BUF, using the register
1335 map in REGSET. If REGNUM is -1, do this for all registers in
1336 REGSET. */
1337
1338 void
regcache_collect_regset(const struct regset * regset,const struct regcache * regcache,int regnum,void * buf,size_t size)1339 regcache_collect_regset (const struct regset *regset,
1340 const struct regcache *regcache,
1341 int regnum, void *buf, size_t size)
1342 {
1343 regcache->collect_regset (regset, regnum, (gdb_byte *) buf, size);
1344 }
1345
1346 /* See regcache.h */
1347
1348 void
collect_regset(const struct regset * regset,int regbase,int regnum,void * buf,size_t size)1349 regcache::collect_regset (const struct regset *regset, int regbase,
1350 int regnum, void *buf, size_t size) const
1351 {
1352 transfer_regset (regset, regbase, nullptr, regnum, nullptr, (gdb_byte *) buf,
1353 size);
1354 }
1355
1356 bool
regcache_map_supplies(const struct regcache_map_entry * map,int regnum,struct gdbarch * gdbarch,size_t size)1357 regcache_map_supplies (const struct regcache_map_entry *map, int regnum,
1358 struct gdbarch *gdbarch, size_t size)
1359 {
1360 int offs = 0, count;
1361
1362 for (; (count = map->count) != 0; map++)
1363 {
1364 int regno = map->regno;
1365 int slot_size = map->size;
1366
1367 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1368 slot_size = register_size (gdbarch, regno);
1369
1370 if (regno != REGCACHE_MAP_SKIP && regnum >= regno
1371 && regnum < regno + count)
1372 return offs + (regnum - regno + 1) * slot_size <= size;
1373
1374 offs += count * slot_size;
1375 if (offs >= size)
1376 return false;
1377 }
1378 return false;
1379 }
1380
1381 /* See gdbsupport/common-regcache.h. */
1382
1383 bool
raw_compare(int regnum,const void * buf,int offset)1384 reg_buffer::raw_compare (int regnum, const void *buf, int offset) const
1385 {
1386 gdb_assert (buf != NULL);
1387
1388 gdb::array_view<const gdb_byte> regbuf = register_buffer (regnum);
1389 gdb_assert (offset <= regbuf.size ());
1390 regbuf = regbuf.slice (offset);
1391
1392 return memcmp (buf, regbuf.data (), regbuf.size ()) == 0;
1393 }
1394
1395 /* Special handling for register PC. */
1396
1397 CORE_ADDR
regcache_read_pc(reg_buffer_common * reg_buf)1398 regcache_read_pc (reg_buffer_common *reg_buf)
1399 {
1400 regcache *regcache = gdb::checked_static_cast<struct regcache *> (reg_buf);
1401 struct gdbarch *gdbarch = regcache->arch ();
1402
1403 CORE_ADDR pc_val;
1404
1405 if (gdbarch_read_pc_p (gdbarch))
1406 pc_val = gdbarch_read_pc (gdbarch, regcache);
1407 /* Else use per-frame method on get_current_frame. */
1408 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1409 {
1410 ULONGEST raw_val;
1411
1412 if (regcache_cooked_read_unsigned (regcache,
1413 gdbarch_pc_regnum (gdbarch),
1414 &raw_val) == REG_UNAVAILABLE)
1415 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1416
1417 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1418 }
1419 else
1420 internal_error (_("regcache_read_pc: Unable to find PC"));
1421 return pc_val;
1422 }
1423
1424 /* See gdbsupport/common-regcache.h. */
1425
1426 CORE_ADDR
regcache_read_pc_protected(reg_buffer_common * regcache)1427 regcache_read_pc_protected (reg_buffer_common *regcache)
1428 {
1429 CORE_ADDR pc;
1430 try
1431 {
1432 pc = regcache_read_pc (regcache);
1433 }
1434 catch (const gdb_exception_error &ex)
1435 {
1436 pc = 0;
1437 }
1438
1439 return pc;
1440 }
1441
1442 void
regcache_write_pc(struct regcache * regcache,CORE_ADDR pc)1443 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1444 {
1445 struct gdbarch *gdbarch = regcache->arch ();
1446
1447 if (gdbarch_write_pc_p (gdbarch))
1448 gdbarch_write_pc (gdbarch, regcache, pc);
1449 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1450 regcache_cooked_write_unsigned (regcache,
1451 gdbarch_pc_regnum (gdbarch), pc);
1452 else
1453 internal_error (_("regcache_write_pc: Unable to update PC"));
1454
1455 /* Writing the PC (for instance, from "load") invalidates the
1456 current frame. */
1457 reinit_frame_cache ();
1458 }
1459
1460 int
num_raw_registers()1461 reg_buffer::num_raw_registers () const
1462 {
1463 return gdbarch_num_regs (arch ());
1464 }
1465
1466 std::string
register_debug_string(int regno)1467 regcache::register_debug_string (int regno)
1468 {
1469 struct gdbarch *gdbarch = arch ();
1470 std::string s;
1471
1472 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1473 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1474 string_appendf (s, "register %s:", gdbarch_register_name (gdbarch, regno));
1475 else
1476 string_appendf (s, "register %d:", regno);
1477
1478 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1479 {
1480 gdb::array_view<gdb_byte> buf = register_buffer (regno);
1481
1482 string_appendf (s, " = ");
1483
1484 for (gdb_byte byte : buf)
1485 string_appendf (s, "%02x", byte);
1486
1487 if (buf.size () <= sizeof (LONGEST))
1488 {
1489 ULONGEST val
1490 = extract_unsigned_integer (buf, gdbarch_byte_order (gdbarch));
1491
1492 string_appendf (s, " %s %s",
1493 core_addr_to_string_nz (val), plongest (val));
1494 }
1495 }
1496
1497 return s;
1498 }
1499
1500 /* Implement 'maint flush register-cache' command. */
1501
1502 static void
reg_flush_command(const char * command,int from_tty)1503 reg_flush_command (const char *command, int from_tty)
1504 {
1505 /* Force-flush the register cache. */
1506 registers_changed ();
1507 if (from_tty)
1508 gdb_printf (_("Register cache flushed.\n"));
1509 }
1510
1511 void
dump(ui_file * file)1512 register_dump::dump (ui_file *file)
1513 {
1514 auto descr = regcache_descr (m_gdbarch);
1515 int regnum;
1516 int footnote_nr = 0;
1517 int footnote_register_offset = 0;
1518 int footnote_register_type_name_null = 0;
1519 long register_offset = 0;
1520
1521 gdb_assert (descr->nr_cooked_registers
1522 == gdbarch_num_cooked_regs (m_gdbarch));
1523
1524 for (regnum = -1; regnum < descr->nr_cooked_registers; regnum++)
1525 {
1526 /* Name. */
1527 if (regnum < 0)
1528 gdb_printf (file, " %-10s", "Name");
1529 else
1530 {
1531 const char *p = gdbarch_register_name (m_gdbarch, regnum);
1532
1533 if (p[0] == '\0')
1534 p = "''";
1535 gdb_printf (file, " %-10s", p);
1536 }
1537
1538 /* Number. */
1539 if (regnum < 0)
1540 gdb_printf (file, " %4s", "Nr");
1541 else
1542 gdb_printf (file, " %4d", regnum);
1543
1544 /* Relative number. */
1545 if (regnum < 0)
1546 gdb_printf (file, " %4s", "Rel");
1547 else if (regnum < gdbarch_num_regs (m_gdbarch))
1548 gdb_printf (file, " %4d", regnum);
1549 else
1550 gdb_printf (file, " %4d",
1551 (regnum - gdbarch_num_regs (m_gdbarch)));
1552
1553 /* Offset. */
1554 if (regnum < 0)
1555 gdb_printf (file, " %6s ", "Offset");
1556 else
1557 {
1558 gdb_printf (file, " %6ld",
1559 descr->register_offset[regnum]);
1560 if (register_offset != descr->register_offset[regnum]
1561 || (regnum > 0
1562 && (descr->register_offset[regnum]
1563 != (descr->register_offset[regnum - 1]
1564 + descr->sizeof_register[regnum - 1])))
1565 )
1566 {
1567 if (!footnote_register_offset)
1568 footnote_register_offset = ++footnote_nr;
1569 gdb_printf (file, "*%d", footnote_register_offset);
1570 }
1571 else
1572 gdb_printf (file, " ");
1573 register_offset = (descr->register_offset[regnum]
1574 + descr->sizeof_register[regnum]);
1575 }
1576
1577 /* Size. */
1578 if (regnum < 0)
1579 gdb_printf (file, " %5s ", "Size");
1580 else
1581 gdb_printf (file, " %5ld", descr->sizeof_register[regnum]);
1582
1583 /* Type. */
1584 {
1585 const char *t;
1586 std::string name_holder;
1587
1588 if (regnum < 0)
1589 t = "Type";
1590 else
1591 {
1592 static const char blt[] = "builtin_type";
1593
1594 t = register_type (m_gdbarch, regnum)->name ();
1595 if (t == NULL)
1596 {
1597 if (!footnote_register_type_name_null)
1598 footnote_register_type_name_null = ++footnote_nr;
1599 name_holder = string_printf ("*%d",
1600 footnote_register_type_name_null);
1601 t = name_holder.c_str ();
1602 }
1603 /* Chop a leading builtin_type. */
1604 if (startswith (t, blt))
1605 t += strlen (blt);
1606 }
1607 gdb_printf (file, " %-15s", t);
1608 }
1609
1610 /* Leading space always present. */
1611 gdb_printf (file, " ");
1612
1613 dump_reg (file, regnum);
1614
1615 gdb_printf (file, "\n");
1616 }
1617
1618 if (footnote_register_offset)
1619 gdb_printf (file, "*%d: Inconsistent register offsets.\n",
1620 footnote_register_offset);
1621 if (footnote_register_type_name_null)
1622 gdb_printf (file,
1623 "*%d: Register type's name NULL.\n",
1624 footnote_register_type_name_null);
1625 }
1626
1627 #if GDB_SELF_TEST
1628 #include "gdbsupport/selftest.h"
1629 #include "selftest-arch.h"
1630 #include "target-float.h"
1631
1632 namespace selftests {
1633
1634 static size_t
regcaches_size()1635 regcaches_size ()
1636 {
1637 size_t size = 0;
1638
1639 for (auto pid_ptid_regc_map_it = regcaches.cbegin ();
1640 pid_ptid_regc_map_it != regcaches.cend ();
1641 ++pid_ptid_regc_map_it)
1642 {
1643 const pid_ptid_regcache_map &pid_ptid_regc_map
1644 = pid_ptid_regc_map_it->second;
1645
1646 for (auto ptid_regc_map_it = pid_ptid_regc_map.cbegin ();
1647 ptid_regc_map_it != pid_ptid_regc_map.cend ();
1648 ++ptid_regc_map_it)
1649 {
1650 const ptid_regcache_map &ptid_regc_map
1651 = ptid_regc_map_it->second;
1652
1653 size += ptid_regc_map.size ();
1654 }
1655 }
1656
1657 return size;
1658 }
1659
1660 /* Return the count of regcaches for (TARGET, PTID) in REGCACHES. */
1661
1662 static int
regcache_count(process_stratum_target * target,ptid_t ptid)1663 regcache_count (process_stratum_target *target, ptid_t ptid)
1664 {
1665 /* Look up map for target. */
1666 auto pid_ptid_regc_map_it = regcaches.find (target);
1667 if (pid_ptid_regc_map_it != regcaches.end ())
1668 {
1669 pid_ptid_regcache_map &pid_ptid_regc_map = pid_ptid_regc_map_it->second;
1670
1671 /* Look map for pid. */
1672 auto ptid_regc_map_it = pid_ptid_regc_map.find (ptid.pid ());
1673 if (ptid_regc_map_it != pid_ptid_regc_map.end ())
1674 {
1675 ptid_regcache_map &ptid_regc_map = ptid_regc_map_it->second;
1676 auto range = ptid_regc_map.equal_range (ptid);
1677
1678 return std::distance (range.first, range.second);
1679 }
1680 }
1681
1682 return 0;
1683 };
1684
1685 /* Wrapper around get_thread_arch_regcache that does some self checks. */
1686
1687 static void
get_thread_arch_regcache_and_check(inferior * inf_for_target_calls,ptid_t ptid)1688 get_thread_arch_regcache_and_check (inferior *inf_for_target_calls,
1689 ptid_t ptid)
1690 {
1691 /* We currently only test with a single gdbarch. Any gdbarch will do, so use
1692 the current inferior's gdbarch. Also use the current inferior's address
1693 space. */
1694 gdbarch *arch = inf_for_target_calls->arch ();
1695 regcache *regcache
1696 = get_thread_arch_regcache (inf_for_target_calls, ptid, arch);
1697
1698 SELF_CHECK (regcache != NULL);
1699 SELF_CHECK (regcache->ptid () == ptid);
1700 SELF_CHECK (regcache->arch () == arch);
1701 }
1702
1703 /* The data that the regcaches selftests must hold onto for the duration of the
1704 test. */
1705
1706 struct regcache_test_data
1707 {
regcache_test_dataregcache_test_data1708 regcache_test_data ()
1709 /* The specific arch doesn't matter. */
1710 : test_ctx_1 (current_inferior ()->arch ()),
1711 test_ctx_2 (current_inferior ()->arch ())
1712 {
1713 /* Ensure the regcaches container is empty at the start. */
1714 registers_changed ();
1715 }
1716
~regcache_test_dataregcache_test_data1717 ~regcache_test_data ()
1718 {
1719 /* Make sure to leave the global regcaches container empty. */
1720 registers_changed ();
1721 }
1722
1723 scoped_mock_context<test_target_ops> test_ctx_1;
1724 scoped_mock_context<test_target_ops> test_ctx_2;
1725 };
1726
1727 using regcache_test_data_up = std::unique_ptr<regcache_test_data>;
1728
1729 /* Set up a few regcaches from two different targets, for use in
1730 regcache-management tests.
1731
1732 Return a pointer, because the `regcache_test_data` type is not moveable. */
1733
1734 static regcache_test_data_up
populate_regcaches_for_test()1735 populate_regcaches_for_test ()
1736 {
1737 regcache_test_data_up data (new regcache_test_data);
1738 size_t expected_regcache_size = 0;
1739
1740 SELF_CHECK (regcaches_size () == 0);
1741
1742 /* Populate the regcache container with a few regcaches for the two test
1743 targets. */
1744 for (int pid : { 1, 2 })
1745 {
1746 for (long lwp : { 1, 2, 3 })
1747 {
1748 get_thread_arch_regcache_and_check
1749 (&data->test_ctx_1.mock_inferior, ptid_t (pid, lwp));
1750 expected_regcache_size++;
1751 SELF_CHECK (regcaches_size () == expected_regcache_size);
1752
1753 get_thread_arch_regcache_and_check
1754 (&data->test_ctx_2.mock_inferior, ptid_t (pid, lwp));
1755 expected_regcache_size++;
1756 SELF_CHECK (regcaches_size () == expected_regcache_size);
1757 }
1758 }
1759
1760 return data;
1761 }
1762
1763 static void
get_thread_arch_regcache_test()1764 get_thread_arch_regcache_test ()
1765 {
1766 /* populate_regcaches_for_test already tests most of the
1767 get_thread_arch_regcache functionality. */
1768 regcache_test_data_up data = populate_regcaches_for_test ();
1769 size_t regcaches_size_before = regcaches_size ();
1770
1771 /* Test that getting an existing regcache doesn't create a new one. */
1772 get_thread_arch_regcache_and_check (&data->test_ctx_1.mock_inferior,
1773 ptid_t (2, 2));
1774 SELF_CHECK (regcaches_size () == regcaches_size_before);
1775 }
1776
1777 /* Test marking all regcaches of all targets as changed. */
1778
1779 static void
registers_changed_ptid_all_test()1780 registers_changed_ptid_all_test ()
1781 {
1782 regcache_test_data_up data = populate_regcaches_for_test ();
1783
1784 registers_changed_ptid (nullptr, minus_one_ptid);
1785 SELF_CHECK (regcaches_size () == 0);
1786 }
1787
1788 /* Test marking regcaches of a specific target as changed. */
1789
1790 static void
registers_changed_ptid_target_test()1791 registers_changed_ptid_target_test ()
1792 {
1793 regcache_test_data_up data = populate_regcaches_for_test ();
1794
1795 registers_changed_ptid (&data->test_ctx_1.mock_target, minus_one_ptid);
1796 SELF_CHECK (regcaches_size () == 6);
1797
1798 /* Check that we deleted the regcache for the right target. */
1799 SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target,
1800 ptid_t (2, 2)) == 0);
1801 SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target,
1802 ptid_t (2, 2)) == 1);
1803 }
1804
1805 /* Test marking regcaches of a specific (target, pid) as changed. */
1806
1807 static void
registers_changed_ptid_target_pid_test()1808 registers_changed_ptid_target_pid_test ()
1809 {
1810 regcache_test_data_up data = populate_regcaches_for_test ();
1811
1812 registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2));
1813 SELF_CHECK (regcaches_size () == 9);
1814
1815 /* Regcaches from target1 should not exist, while regcaches from target2
1816 should exist. */
1817 SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target,
1818 ptid_t (2, 2)) == 0);
1819 SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target,
1820 ptid_t (2, 2)) == 1);
1821 }
1822
1823 /* Test marking regcaches of a specific (target, ptid) as changed. */
1824
1825 static void
registers_changed_ptid_target_ptid_test()1826 registers_changed_ptid_target_ptid_test ()
1827 {
1828 regcache_test_data_up data = populate_regcaches_for_test ();
1829
1830 registers_changed_ptid (&data->test_ctx_1.mock_target, ptid_t (2, 2));
1831 SELF_CHECK (regcaches_size () == 11);
1832
1833 /* Check that we deleted the regcache for the right target. */
1834 SELF_CHECK (regcache_count (&data->test_ctx_1.mock_target,
1835 ptid_t (2, 2)) == 0);
1836 SELF_CHECK (regcache_count (&data->test_ctx_2.mock_target,
1837 ptid_t (2, 2)) == 1);
1838 }
1839
1840 /* Test using reg_buffer::raw_compare with offset equal to the register size
1841 (thus comparing 0 bytes). */
1842
1843 static void
reg_buffer_raw_compare_zero_len_test()1844 reg_buffer_raw_compare_zero_len_test ()
1845 {
1846 regcache_test_data_up data = populate_regcaches_for_test ();
1847 inferior &inf = data->test_ctx_1.mock_inferior;
1848 const regcache *regcache
1849 = get_thread_arch_regcache (&inf, ptid_t (1, 1), inf.arch ());
1850
1851 /* The buffer address is irrelevant since we end up comparing 0 bytes, we just
1852 need to pass something. */
1853 gdb_byte buf;
1854 SELF_CHECK (regcache->raw_compare (0, &buf, register_size (inf.arch (), 0)));
1855 }
1856
1857 class target_ops_no_register : public test_target_ops
1858 {
1859 public:
target_ops_no_register()1860 target_ops_no_register ()
1861 : test_target_ops {}
1862 {}
1863
reset()1864 void reset ()
1865 {
1866 fetch_registers_called = 0;
1867 store_registers_called = 0;
1868 xfer_partial_called = 0;
1869 }
1870
1871 void fetch_registers (regcache *regs, int regno) override;
1872 void store_registers (regcache *regs, int regno) override;
1873
1874 enum target_xfer_status xfer_partial (enum target_object object,
1875 const char *annex, gdb_byte *readbuf,
1876 const gdb_byte *writebuf,
1877 ULONGEST offset, ULONGEST len,
1878 ULONGEST *xfered_len) override;
1879
1880 unsigned int fetch_registers_called = 0;
1881 unsigned int store_registers_called = 0;
1882 unsigned int xfer_partial_called = 0;
1883 };
1884
1885 void
fetch_registers(regcache * regs,int regno)1886 target_ops_no_register::fetch_registers (regcache *regs, int regno)
1887 {
1888 /* Mark register available. */
1889 regs->raw_supply_zeroed (regno);
1890 this->fetch_registers_called++;
1891 }
1892
1893 void
store_registers(regcache * regs,int regno)1894 target_ops_no_register::store_registers (regcache *regs, int regno)
1895 {
1896 this->store_registers_called++;
1897 }
1898
1899 enum target_xfer_status
xfer_partial(enum target_object object,const char * annex,gdb_byte * readbuf,const gdb_byte * writebuf,ULONGEST offset,ULONGEST len,ULONGEST * xfered_len)1900 target_ops_no_register::xfer_partial (enum target_object object,
1901 const char *annex, gdb_byte *readbuf,
1902 const gdb_byte *writebuf,
1903 ULONGEST offset, ULONGEST len,
1904 ULONGEST *xfered_len)
1905 {
1906 this->xfer_partial_called++;
1907
1908 *xfered_len = len;
1909 return TARGET_XFER_OK;
1910 }
1911
1912 class readwrite_regcache : public regcache
1913 {
1914 public:
readwrite_regcache(inferior * inf_for_target_calls,struct gdbarch * gdbarch)1915 readwrite_regcache (inferior *inf_for_target_calls,
1916 struct gdbarch *gdbarch)
1917 : regcache (inf_for_target_calls, gdbarch)
1918 {}
1919 };
1920
1921 /* Return true if regcache::cooked_{read,write}_test should be skipped for
1922 GDBARCH. */
1923
1924 static bool
selftest_skiparch(struct gdbarch * gdbarch)1925 selftest_skiparch (struct gdbarch *gdbarch)
1926 {
1927 const char *name = gdbarch_bfd_arch_info (gdbarch)->printable_name;
1928
1929 /* Avoid warning:
1930 Running selftest regcache::cooked_{read,write}_test::m68hc11.
1931 warning: No frame soft register found in the symbol table.
1932 Stack backtrace will not work.
1933 We could instead capture the output and then filter out the warning, but
1934 that seems more trouble than it's worth. */
1935 return (strcmp (name, "m68hc11") == 0
1936 || strcmp (name, "m68hc12") == 0
1937 || strcmp (name, "m68hc12:HCS12") == 0);
1938 }
1939
1940 /* Test regcache::cooked_read gets registers from raw registers and
1941 memory instead of target to_{fetch,store}_registers. */
1942
1943 static void
cooked_read_test(struct gdbarch * gdbarch)1944 cooked_read_test (struct gdbarch *gdbarch)
1945 {
1946 if (selftest_skiparch (gdbarch))
1947 return;
1948
1949 scoped_mock_context<target_ops_no_register> mockctx (gdbarch);
1950
1951 /* Test that read one raw register from regcache_no_target will go
1952 to the target layer. */
1953
1954 /* Find a raw register which size isn't zero. */
1955 int nonzero_regnum;
1956 for (nonzero_regnum = 0;
1957 nonzero_regnum < gdbarch_num_regs (gdbarch);
1958 nonzero_regnum++)
1959 {
1960 if (register_size (gdbarch, nonzero_regnum) != 0)
1961 break;
1962 }
1963
1964 /* Install this regcache in the regcaches global structure, so that. */
1965 pid_ptid_regcache_map &x = regcaches[&mockctx.mock_target];
1966 ptid_regcache_map &y = x[mockctx.mock_ptid.pid ()];
1967 regcache &readwrite
1968 = *y.emplace (std::make_pair (mockctx.mock_ptid,
1969 std::make_unique<readwrite_regcache> (
1970 &mockctx.mock_inferior, gdbarch)))
1971 ->second;
1972
1973 readwrite.set_ptid (mockctx.mock_ptid);
1974
1975 gdb::byte_vector buf (register_size (gdbarch, nonzero_regnum));
1976 readwrite.raw_read (nonzero_regnum, buf);
1977
1978 /* raw_read calls target_fetch_registers. */
1979 SELF_CHECK (mockctx.mock_target.fetch_registers_called > 0);
1980 mockctx.mock_target.reset ();
1981
1982 /* Mark all raw registers valid, so the following raw registers
1983 accesses won't go to target. */
1984 for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1985 readwrite.raw_update (i);
1986
1987 mockctx.mock_target.reset ();
1988 /* Then, read all raw and pseudo registers, and don't expect calling
1989 to_{fetch,store}_registers. */
1990 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
1991 {
1992 if (register_size (gdbarch, regnum) == 0)
1993 continue;
1994
1995 gdb::byte_vector inner_buf (register_size (gdbarch, regnum));
1996
1997 SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, inner_buf));
1998 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
1999 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
2000 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
2001
2002 mockctx.mock_target.reset ();
2003 }
2004
2005 readonly_detached_regcache readonly (readwrite);
2006
2007 /* GDB may go to target layer to fetch all registers and memory for
2008 readonly regcache. */
2009 mockctx.mock_target.reset ();
2010
2011 for (int regnum = 0; regnum < gdbarch_num_cooked_regs (gdbarch); regnum++)
2012 {
2013 if (register_size (gdbarch, regnum) == 0)
2014 continue;
2015
2016 gdb::byte_vector inner_buf (register_size (gdbarch, regnum));
2017 register_status status = readonly.cooked_read (regnum, inner_buf);
2018
2019 if (regnum < gdbarch_num_regs (gdbarch))
2020 {
2021 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2022
2023 if (bfd_arch == bfd_arch_amdgcn
2024 || bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
2025 || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
2026 || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
2027 || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
2028 || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
2029 || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
2030 || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score
2031 || bfd_arch == bfd_arch_riscv || bfd_arch == bfd_arch_csky)
2032 {
2033 /* Raw registers. If raw registers are not in save_reggroup,
2034 their status are unknown. */
2035 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
2036 SELF_CHECK (status == REG_VALID);
2037 else
2038 SELF_CHECK (status == REG_UNKNOWN);
2039 }
2040 else
2041 SELF_CHECK (status == REG_VALID);
2042 }
2043 else
2044 {
2045 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
2046 SELF_CHECK (status == REG_VALID);
2047 else
2048 {
2049 /* If pseudo registers are not in save_reggroup, some of
2050 them can be computed from saved raw registers, but some
2051 of them are unknown. */
2052 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2053
2054 if (bfd_arch == bfd_arch_frv
2055 || bfd_arch == bfd_arch_m32c
2056 || bfd_arch == bfd_arch_mep
2057 || bfd_arch == bfd_arch_sh)
2058 SELF_CHECK (status == REG_VALID || status == REG_UNKNOWN);
2059 else if (bfd_arch == bfd_arch_mips
2060 || bfd_arch == bfd_arch_h8300)
2061 SELF_CHECK (status == REG_UNKNOWN);
2062 else
2063 SELF_CHECK (status == REG_VALID);
2064 }
2065 }
2066
2067 SELF_CHECK (mockctx.mock_target.fetch_registers_called == 0);
2068 SELF_CHECK (mockctx.mock_target.store_registers_called == 0);
2069 SELF_CHECK (mockctx.mock_target.xfer_partial_called == 0);
2070
2071 mockctx.mock_target.reset ();
2072 }
2073
2074 regcaches.erase (&mockctx.mock_target);
2075 }
2076
2077 /* Test regcache::cooked_write by writing some expected contents to
2078 registers, and checking that contents read from registers and the
2079 expected contents are the same. */
2080
2081 static void
cooked_write_test(struct gdbarch * gdbarch)2082 cooked_write_test (struct gdbarch *gdbarch)
2083 {
2084 if (selftest_skiparch (gdbarch))
2085 return;
2086
2087 /* Create a mock environment. A process_stratum target pushed. */
2088 scoped_mock_context<target_ops_no_register> ctx (gdbarch);
2089
2090
2091 /* Install this regcache in the regcaches global structure, so that. */
2092 pid_ptid_regcache_map &x = regcaches[&ctx.mock_target];
2093 ptid_regcache_map &y = x[ctx.mock_ptid.pid ()];
2094 regcache &readwrite
2095 = *y.emplace (std::make_pair (ctx.mock_ptid,
2096 std::make_unique<readwrite_regcache> (
2097 &ctx.mock_inferior, gdbarch)))
2098 ->second;
2099
2100 readwrite.set_ptid (ctx.mock_ptid);
2101 const int num_regs = gdbarch_num_cooked_regs (gdbarch);
2102
2103 for (auto regnum = 0; regnum < num_regs; regnum++)
2104 {
2105 if (register_size (gdbarch, regnum) == 0
2106 || gdbarch_cannot_store_register (gdbarch, regnum))
2107 continue;
2108
2109 auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
2110
2111 if (bfd_arch == bfd_arch_sparc
2112 /* SPARC64_CWP_REGNUM, SPARC64_PSTATE_REGNUM,
2113 SPARC64_ASI_REGNUM and SPARC64_CCR_REGNUM are hard to test. */
2114 && gdbarch_ptr_bit (gdbarch) == 64
2115 && (regnum >= gdbarch_num_regs (gdbarch)
2116 && regnum <= gdbarch_num_regs (gdbarch) + 4))
2117 continue;
2118
2119 gdb::byte_vector expected (register_size (gdbarch, regnum), 0);
2120 gdb::byte_vector buf (register_size (gdbarch, regnum), 0);
2121 const auto type = register_type (gdbarch, regnum);
2122
2123 if (type->code () == TYPE_CODE_FLT
2124 || type->code () == TYPE_CODE_DECFLOAT)
2125 {
2126 /* Generate valid float format. */
2127 target_float_from_string (expected.data (), type, "1.25");
2128 }
2129 else if (type->code () == TYPE_CODE_INT
2130 || type->code () == TYPE_CODE_ARRAY
2131 || type->code () == TYPE_CODE_PTR
2132 || type->code () == TYPE_CODE_UNION
2133 || type->code () == TYPE_CODE_STRUCT)
2134 {
2135 if (bfd_arch == bfd_arch_ia64
2136 || (regnum >= gdbarch_num_regs (gdbarch)
2137 && (bfd_arch == bfd_arch_xtensa
2138 || bfd_arch == bfd_arch_bfin
2139 || bfd_arch == bfd_arch_m32c
2140 /* m68hc11 pseudo registers are in memory. */
2141 || bfd_arch == bfd_arch_m68hc11
2142 || bfd_arch == bfd_arch_m68hc12
2143 || bfd_arch == bfd_arch_s390))
2144 || (bfd_arch == bfd_arch_frv
2145 /* FRV pseudo registers except iacc0. */
2146 && regnum > gdbarch_num_regs (gdbarch)))
2147 {
2148 /* Skip setting the expected values for some architecture
2149 registers. */
2150 }
2151 else if (bfd_arch == bfd_arch_rl78 && regnum == 40)
2152 {
2153 /* RL78_PC_REGNUM */
2154 for (auto j = 0; j < register_size (gdbarch, regnum) - 1; j++)
2155 expected[j] = j;
2156 }
2157 else
2158 {
2159 for (auto j = 0; j < register_size (gdbarch, regnum); j++)
2160 expected[j] = j;
2161 }
2162 }
2163 else if (type->code () == TYPE_CODE_FLAGS)
2164 {
2165 /* No idea how to test flags. */
2166 continue;
2167 }
2168 else
2169 {
2170 /* If we don't know how to create the expected value for the
2171 this type, make it fail. */
2172 SELF_CHECK (0);
2173 }
2174
2175 readwrite.cooked_write (regnum, expected);
2176
2177 SELF_CHECK (readwrite.cooked_read (regnum, buf) == REG_VALID);
2178 SELF_CHECK (expected == buf);
2179 }
2180
2181 regcaches.erase (&ctx.mock_target);
2182 }
2183
2184 /* Verify that when two threads with the same ptid exist (from two different
2185 targets) and one of them changes ptid, we only update the appropriate
2186 regcaches. */
2187
2188 static void
regcache_thread_ptid_changed()2189 regcache_thread_ptid_changed ()
2190 {
2191 /* This test relies on the global regcache list to initially be empty. */
2192 registers_changed ();
2193
2194 /* Any arch will do. */
2195 gdbarch *arch = current_inferior ()->arch ();
2196
2197 /* Prepare two targets with one thread each, with the same ptid. */
2198 scoped_mock_context<test_target_ops> target1 (arch);
2199 scoped_mock_context<test_target_ops> target2 (arch);
2200
2201 ptid_t old_ptid (111, 222);
2202 ptid_t new_ptid (111, 333);
2203
2204 target1.mock_inferior.pid = old_ptid.pid ();
2205 target1.mock_thread.ptid = old_ptid;
2206 target1.mock_inferior.ptid_thread_map.clear ();
2207 target1.mock_inferior.ptid_thread_map[old_ptid] = &target1.mock_thread;
2208
2209 target2.mock_inferior.pid = old_ptid.pid ();
2210 target2.mock_thread.ptid = old_ptid;
2211 target2.mock_inferior.ptid_thread_map.clear ();
2212 target2.mock_inferior.ptid_thread_map[old_ptid] = &target2.mock_thread;
2213
2214 gdb_assert (regcaches.empty ());
2215
2216 /* Populate the regcaches container. */
2217 get_thread_arch_regcache (&target1.mock_inferior, old_ptid, arch);
2218 get_thread_arch_regcache (&target2.mock_inferior, old_ptid, arch);
2219
2220 gdb_assert (regcaches.size () == 2);
2221 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 1);
2222 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 0);
2223 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2224 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2225
2226 thread_change_ptid (&target1.mock_target, old_ptid, new_ptid);
2227
2228 gdb_assert (regcaches.size () == 2);
2229 gdb_assert (regcache_count (&target1.mock_target, old_ptid) == 0);
2230 gdb_assert (regcache_count (&target1.mock_target, new_ptid) == 1);
2231 gdb_assert (regcache_count (&target2.mock_target, old_ptid) == 1);
2232 gdb_assert (regcache_count (&target2.mock_target, new_ptid) == 0);
2233
2234 /* Leave the regcache list empty. */
2235 registers_changed ();
2236 gdb_assert (regcaches.empty ());
2237 }
2238
2239 } // namespace selftests
2240 #endif /* GDB_SELF_TEST */
2241
2242 void _initialize_regcache ();
2243 void
_initialize_regcache()2244 _initialize_regcache ()
2245 {
2246 struct cmd_list_element *c;
2247
2248 gdb::observers::target_changed.attach (regcache_observer_target_changed,
2249 "regcache");
2250 gdb::observers::thread_ptid_changed.attach (regcache_thread_ptid_changed,
2251 "regcache");
2252
2253 cmd_list_element *maintenance_flush_register_cache_cmd
2254 = add_cmd ("register-cache", class_maintenance, reg_flush_command,
2255 _("Force gdb to flush its register and frame cache."),
2256 &maintenanceflushlist);
2257 c = add_com_alias ("flushregs", maintenance_flush_register_cache_cmd,
2258 class_maintenance, 0);
2259 deprecate_cmd (c, "maintenance flush register-cache");
2260
2261 #if GDB_SELF_TEST
2262 selftests::register_test ("get_thread_arch_regcache",
2263 selftests::get_thread_arch_regcache_test);
2264 selftests::register_test ("registers_changed_ptid_all",
2265 selftests::registers_changed_ptid_all_test);
2266 selftests::register_test ("registers_changed_ptid_target",
2267 selftests::registers_changed_ptid_target_test);
2268 selftests::register_test ("registers_changed_ptid_target_pid",
2269 selftests::registers_changed_ptid_target_pid_test);
2270 selftests::register_test ("registers_changed_ptid_target_ptid",
2271 selftests::registers_changed_ptid_target_ptid_test);
2272 selftests::register_test ("reg_buffer_raw_compare_zero_len",
2273 selftests::reg_buffer_raw_compare_zero_len_test);
2274
2275 selftests::register_test_foreach_arch ("regcache::cooked_read_test",
2276 selftests::cooked_read_test);
2277 selftests::register_test_foreach_arch ("regcache::cooked_write_test",
2278 selftests::cooked_write_test);
2279 selftests::register_test ("regcache_thread_ptid_changed",
2280 selftests::regcache_thread_ptid_changed);
2281 #endif
2282 }
2283