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