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