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