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