Add regcache raw_supply_integer and raw_collect_integer.
[deliverable/binutils-gdb.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3 Copyright (C) 1986-2017 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "inferior.h"
22 #include "target.h"
23 #include "gdbarch.h"
24 #include "gdbcmd.h"
25 #include "regcache.h"
26 #include "reggroups.h"
27 #include "observer.h"
28 #include "remote.h"
29 #include "valprint.h"
30 #include "regset.h"
31 #include <forward_list>
32
33 /*
34 * DATA STRUCTURE
35 *
36 * Here is the actual register cache.
37 */
38
39 /* Per-architecture object describing the layout of a register cache.
40 Computed once when the architecture is created. */
41
42 struct gdbarch_data *regcache_descr_handle;
43
44 struct regcache_descr
45 {
46 /* The architecture this descriptor belongs to. */
47 struct gdbarch *gdbarch;
48
49 /* The raw register cache. Each raw (or hard) register is supplied
50 by the target interface. The raw cache should not contain
51 redundant information - if the PC is constructed from two
52 registers then those registers and not the PC lives in the raw
53 cache. */
54 int nr_raw_registers;
55 long sizeof_raw_registers;
56 long sizeof_raw_register_status;
57
58 /* The cooked register space. Each cooked register in the range
59 [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
60 register. The remaining [NR_RAW_REGISTERS
61 .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
62 both raw registers and memory by the architecture methods
63 gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
64 int nr_cooked_registers;
65 long sizeof_cooked_registers;
66 long sizeof_cooked_register_status;
67
68 /* Offset and size (in 8 bit bytes), of each register in the
69 register cache. All registers (including those in the range
70 [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
71 offset. */
72 long *register_offset;
73 long *sizeof_register;
74
75 /* Cached table containing the type of each register. */
76 struct type **register_type;
77 };
78
79 static void *
80 init_regcache_descr (struct gdbarch *gdbarch)
81 {
82 int i;
83 struct regcache_descr *descr;
84 gdb_assert (gdbarch != NULL);
85
86 /* Create an initial, zero filled, table. */
87 descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
88 descr->gdbarch = gdbarch;
89
90 /* Total size of the register space. The raw registers are mapped
91 directly onto the raw register cache while the pseudo's are
92 either mapped onto raw-registers or memory. */
93 descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
94 + gdbarch_num_pseudo_regs (gdbarch);
95 descr->sizeof_cooked_register_status
96 = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);
97
98 /* Fill in a table of register types. */
99 descr->register_type
100 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
101 struct type *);
102 for (i = 0; i < descr->nr_cooked_registers; i++)
103 descr->register_type[i] = gdbarch_register_type (gdbarch, i);
104
105 /* Construct a strictly RAW register cache. Don't allow pseudo's
106 into the register cache. */
107 descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
108 descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);
109
110 /* Lay out the register cache.
111
112 NOTE: cagney/2002-05-22: Only register_type() is used when
113 constructing the register cache. It is assumed that the
114 register's raw size, virtual size and type length are all the
115 same. */
116
117 {
118 long offset = 0;
119
120 descr->sizeof_register
121 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
122 descr->register_offset
123 = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
124 for (i = 0; i < descr->nr_raw_registers; i++)
125 {
126 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
127 descr->register_offset[i] = offset;
128 offset += descr->sizeof_register[i];
129 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
130 }
131 /* Set the real size of the raw register cache buffer. */
132 descr->sizeof_raw_registers = offset;
133
134 for (; i < descr->nr_cooked_registers; i++)
135 {
136 descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
137 descr->register_offset[i] = offset;
138 offset += descr->sizeof_register[i];
139 gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
140 }
141 /* Set the real size of the readonly register cache buffer. */
142 descr->sizeof_cooked_registers = offset;
143 }
144
145 return descr;
146 }
147
148 static struct regcache_descr *
149 regcache_descr (struct gdbarch *gdbarch)
150 {
151 return (struct regcache_descr *) gdbarch_data (gdbarch,
152 regcache_descr_handle);
153 }
154
155 /* Utility functions returning useful register attributes stored in
156 the regcache descr. */
157
158 struct type *
159 register_type (struct gdbarch *gdbarch, int regnum)
160 {
161 struct regcache_descr *descr = regcache_descr (gdbarch);
162
163 gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164 return descr->register_type[regnum];
165 }
166
167 /* Utility functions returning useful register attributes stored in
168 the regcache descr. */
169
170 int
171 register_size (struct gdbarch *gdbarch, int regnum)
172 {
173 struct regcache_descr *descr = regcache_descr (gdbarch);
174 int size;
175
176 gdb_assert (regnum >= 0
177 && regnum < (gdbarch_num_regs (gdbarch)
178 + gdbarch_num_pseudo_regs (gdbarch)));
179 size = descr->sizeof_register[regnum];
180 return size;
181 }
182
183 /* See common/common-regcache.h. */
184
185 int
186 regcache_register_size (const struct regcache *regcache, int n)
187 {
188 return register_size (get_regcache_arch (regcache), n);
189 }
190
191 regcache::regcache (gdbarch *gdbarch, address_space *aspace_,
192 bool readonly_p_)
193 : m_aspace (aspace_), m_readonly_p (readonly_p_)
194 {
195 gdb_assert (gdbarch != NULL);
196 m_descr = regcache_descr (gdbarch);
197
198 if (m_readonly_p)
199 {
200 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_cooked_registers);
201 m_register_status = XCNEWVEC (signed char,
202 m_descr->sizeof_cooked_register_status);
203 }
204 else
205 {
206 m_registers = XCNEWVEC (gdb_byte, m_descr->sizeof_raw_registers);
207 m_register_status = XCNEWVEC (signed char,
208 m_descr->sizeof_raw_register_status);
209 }
210 m_ptid = minus_one_ptid;
211 }
212
213 static enum register_status
214 do_cooked_read (void *src, int regnum, gdb_byte *buf)
215 {
216 struct regcache *regcache = (struct regcache *) src;
217
218 return regcache_cooked_read (regcache, regnum, buf);
219 }
220
221 regcache::regcache (readonly_t, const regcache &src)
222 : regcache (src.arch (), src.aspace (), true)
223 {
224 gdb_assert (!src.m_readonly_p);
225 save (do_cooked_read, (void *) &src);
226 }
227
228 gdbarch *
229 regcache::arch () const
230 {
231 return m_descr->gdbarch;
232 }
233
234 /* See regcache.h. */
235
236 ptid_t
237 regcache_get_ptid (const struct regcache *regcache)
238 {
239 gdb_assert (!ptid_equal (regcache->ptid (), minus_one_ptid));
240
241 return regcache->ptid ();
242 }
243
244 struct regcache *
245 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
246 {
247 return new regcache (gdbarch, aspace);
248 }
249
250 void
251 regcache_xfree (struct regcache *regcache)
252 {
253 if (regcache == NULL)
254 return;
255
256 delete regcache;
257 }
258
259 static void
260 do_regcache_xfree (void *data)
261 {
262 regcache_xfree ((struct regcache *) data);
263 }
264
265 struct cleanup *
266 make_cleanup_regcache_xfree (struct regcache *regcache)
267 {
268 return make_cleanup (do_regcache_xfree, regcache);
269 }
270
271 /* Cleanup routines for invalidating a register. */
272
273 struct register_to_invalidate
274 {
275 struct regcache *regcache;
276 int regnum;
277 };
278
279 static void
280 do_regcache_invalidate (void *data)
281 {
282 struct register_to_invalidate *reg = (struct register_to_invalidate *) data;
283
284 regcache_invalidate (reg->regcache, reg->regnum);
285 }
286
287 static struct cleanup *
288 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
289 {
290 struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
291
292 reg->regcache = regcache;
293 reg->regnum = regnum;
294 return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
295 }
296
297 /* Return REGCACHE's architecture. */
298
299 struct gdbarch *
300 get_regcache_arch (const struct regcache *regcache)
301 {
302 return regcache->arch ();
303 }
304
305 struct address_space *
306 get_regcache_aspace (const struct regcache *regcache)
307 {
308 return regcache->aspace ();
309 }
310
311 /* Return a pointer to register REGNUM's buffer cache. */
312
313 gdb_byte *
314 regcache::register_buffer (int regnum) const
315 {
316 return m_registers + m_descr->register_offset[regnum];
317 }
318
319 void
320 regcache_save (struct regcache *regcache,
321 regcache_cooked_read_ftype *cooked_read, void *src)
322 {
323 regcache->save (cooked_read, src);
324 }
325
326 void
327 regcache::save (regcache_cooked_read_ftype *cooked_read,
328 void *src)
329 {
330 struct gdbarch *gdbarch = m_descr->gdbarch;
331 gdb_byte buf[MAX_REGISTER_SIZE];
332 int regnum;
333
334 /* The DST should be `read-only', if it wasn't then the save would
335 end up trying to write the register values back out to the
336 target. */
337 gdb_assert (m_readonly_p);
338 /* Clear the dest. */
339 memset (m_registers, 0, m_descr->sizeof_cooked_registers);
340 memset (m_register_status, 0, m_descr->sizeof_cooked_register_status);
341 /* Copy over any registers (identified by their membership in the
342 save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
343 gdbarch_num_pseudo_regs) range is checked since some architectures need
344 to save/restore `cooked' registers that live in memory. */
345 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
346 {
347 if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
348 {
349 enum register_status status = cooked_read (src, regnum, buf);
350
351 if (status == REG_VALID)
352 memcpy (register_buffer (regnum), buf,
353 register_size (gdbarch, regnum));
354 else
355 {
356 gdb_assert (status != REG_UNKNOWN);
357
358 memset (register_buffer (regnum), 0,
359 register_size (gdbarch, regnum));
360 }
361 m_register_status[regnum] = status;
362 }
363 }
364 }
365
366 void
367 regcache::restore (struct regcache *src)
368 {
369 struct gdbarch *gdbarch = m_descr->gdbarch;
370 int regnum;
371
372 /* The dst had better not be read-only. If it is, the `restore'
373 doesn't make much sense. */
374 gdb_assert (!m_readonly_p);
375 gdb_assert (src->m_readonly_p);
376 /* Copy over any registers, being careful to only restore those that
377 were both saved and need to be restored. The full [0 .. gdbarch_num_regs
378 + gdbarch_num_pseudo_regs) range is checked since some architectures need
379 to save/restore `cooked' registers that live in memory. */
380 for (regnum = 0; regnum < m_descr->nr_cooked_registers; regnum++)
381 {
382 if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
383 {
384 if (src->m_register_status[regnum] == REG_VALID)
385 cooked_write (regnum, src->register_buffer (regnum));
386 }
387 }
388 }
389
390 void
391 regcache_cpy (struct regcache *dst, struct regcache *src)
392 {
393 gdb_assert (src != NULL && dst != NULL);
394 gdb_assert (src->m_descr->gdbarch == dst->m_descr->gdbarch);
395 gdb_assert (src != dst);
396 gdb_assert (src->m_readonly_p || dst->m_readonly_p);
397
398 if (!src->m_readonly_p)
399 regcache_save (dst, do_cooked_read, src);
400 else if (!dst->m_readonly_p)
401 dst->restore (src);
402 else
403 dst->cpy_no_passthrough (src);
404 }
405
406 /* Copy/duplicate the contents of a register cache. Unlike regcache_cpy,
407 which is pass-through, this does not go through to the target.
408 Only values values already in the cache are transferred. The SRC and DST
409 buffers must not overlap. */
410
411 void
412 regcache::cpy_no_passthrough (struct regcache *src)
413 {
414 gdb_assert (src != NULL);
415 gdb_assert (src->m_descr->gdbarch == m_descr->gdbarch);
416 /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
417 move of data into a thread's regcache. Doing this would be silly
418 - it would mean that regcache->register_status would be
419 completely invalid. */
420 gdb_assert (m_readonly_p && src->m_readonly_p);
421
422 memcpy (m_registers, src->m_registers,
423 m_descr->sizeof_cooked_registers);
424 memcpy (m_register_status, src->m_register_status,
425 m_descr->sizeof_cooked_register_status);
426 }
427
428 struct regcache *
429 regcache_dup (struct regcache *src)
430 {
431 return new regcache (regcache::readonly, *src);
432 }
433
434 enum register_status
435 regcache_register_status (const struct regcache *regcache, int regnum)
436 {
437 gdb_assert (regcache != NULL);
438 return regcache->get_register_status (regnum);
439 }
440
441 enum register_status
442 regcache::get_register_status (int regnum) const
443 {
444 gdb_assert (regnum >= 0);
445 if (m_readonly_p)
446 gdb_assert (regnum < m_descr->nr_cooked_registers);
447 else
448 gdb_assert (regnum < m_descr->nr_raw_registers);
449
450 return (enum register_status) m_register_status[regnum];
451 }
452
453 void
454 regcache_invalidate (struct regcache *regcache, int regnum)
455 {
456 gdb_assert (regcache != NULL);
457 regcache->invalidate (regnum);
458 }
459
460 void
461 regcache::invalidate (int regnum)
462 {
463 gdb_assert (regnum >= 0);
464 gdb_assert (!m_readonly_p);
465 gdb_assert (regnum < m_descr->nr_raw_registers);
466 m_register_status[regnum] = REG_UNKNOWN;
467 }
468
469 /* Global structure containing the current regcache. */
470
471 /* NOTE: this is a write-through cache. There is no "dirty" bit for
472 recording if the register values have been changed (eg. by the
473 user). Therefore all registers must be written back to the
474 target when appropriate. */
475 std::forward_list<regcache *> regcache::current_regcache;
476
477 struct regcache *
478 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
479 struct address_space *aspace)
480 {
481 for (const auto &regcache : regcache::current_regcache)
482 if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
483 return regcache;
484
485 regcache *new_regcache = new regcache (gdbarch, aspace, false);
486
487 regcache::current_regcache.push_front (new_regcache);
488 new_regcache->set_ptid (ptid);
489
490 return new_regcache;
491 }
492
493 struct regcache *
494 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
495 {
496 struct address_space *aspace;
497
498 /* For the benefit of "maint print registers" & co when debugging an
499 executable, allow dumping the regcache even when there is no
500 thread selected (target_thread_address_space internal-errors if
501 no address space is found). Note that normal user commands will
502 fail higher up on the call stack due to no
503 target_has_registers. */
504 aspace = (ptid_equal (null_ptid, ptid)
505 ? NULL
506 : target_thread_address_space (ptid));
507
508 return get_thread_arch_aspace_regcache (ptid, gdbarch, aspace);
509 }
510
511 static ptid_t current_thread_ptid;
512 static struct gdbarch *current_thread_arch;
513
514 struct regcache *
515 get_thread_regcache (ptid_t ptid)
516 {
517 if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
518 {
519 current_thread_ptid = ptid;
520 current_thread_arch = target_thread_architecture (ptid);
521 }
522
523 return get_thread_arch_regcache (ptid, current_thread_arch);
524 }
525
526 struct regcache *
527 get_current_regcache (void)
528 {
529 return get_thread_regcache (inferior_ptid);
530 }
531
532 /* See common/common-regcache.h. */
533
534 struct regcache *
535 get_thread_regcache_for_ptid (ptid_t ptid)
536 {
537 return get_thread_regcache (ptid);
538 }
539
540 /* Observer for the target_changed event. */
541
542 static void
543 regcache_observer_target_changed (struct target_ops *target)
544 {
545 registers_changed ();
546 }
547
548 /* Update global variables old ptids to hold NEW_PTID if they were
549 holding OLD_PTID. */
550 void
551 regcache::regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
552 {
553 for (auto &regcache : regcache::current_regcache)
554 {
555 if (ptid_equal (regcache->ptid (), old_ptid))
556 regcache->set_ptid (new_ptid);
557 }
558 }
559
560 /* Low level examining and depositing of registers.
561
562 The caller is responsible for making sure that the inferior is
563 stopped before calling the fetching routines, or it will get
564 garbage. (a change from GDB version 3, in which the caller got the
565 value from the last stop). */
566
567 /* REGISTERS_CHANGED ()
568
569 Indicate that registers may have changed, so invalidate the cache. */
570
571 void
572 registers_changed_ptid (ptid_t ptid)
573 {
574 for (auto oit = regcache::current_regcache.before_begin (),
575 it = std::next (oit);
576 it != regcache::current_regcache.end ();
577 )
578 {
579 if (ptid_match ((*it)->ptid (), ptid))
580 {
581 delete *it;
582 it = regcache::current_regcache.erase_after (oit);
583 }
584 else
585 oit = it++;
586 }
587
588 if (ptid_match (current_thread_ptid, ptid))
589 {
590 current_thread_ptid = null_ptid;
591 current_thread_arch = NULL;
592 }
593
594 if (ptid_match (inferior_ptid, ptid))
595 {
596 /* We just deleted the regcache of the current thread. Need to
597 forget about any frames we have cached, too. */
598 reinit_frame_cache ();
599 }
600 }
601
602 void
603 registers_changed (void)
604 {
605 registers_changed_ptid (minus_one_ptid);
606
607 /* Force cleanup of any alloca areas if using C alloca instead of
608 a builtin alloca. This particular call is used to clean up
609 areas allocated by low level target code which may build up
610 during lengthy interactions between gdb and the target before
611 gdb gives control to the user (ie watchpoints). */
612 alloca (0);
613 }
614
615 void
616 regcache_raw_update (struct regcache *regcache, int regnum)
617 {
618 gdb_assert (regcache != NULL);
619
620 regcache->raw_update (regnum);
621 }
622
623 void
624 regcache::raw_update (int regnum)
625 {
626 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
627
628 /* Make certain that the register cache is up-to-date with respect
629 to the current thread. This switching shouldn't be necessary
630 only there is still only one target side register cache. Sigh!
631 On the bright side, at least there is a regcache object. */
632
633 if (!m_readonly_p && get_register_status (regnum) == REG_UNKNOWN)
634 {
635 target_fetch_registers (this, regnum);
636
637 /* A number of targets can't access the whole set of raw
638 registers (because the debug API provides no means to get at
639 them). */
640 if (m_register_status[regnum] == REG_UNKNOWN)
641 m_register_status[regnum] = REG_UNAVAILABLE;
642 }
643 }
644
645 enum register_status
646 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
647 {
648 return regcache->raw_read (regnum, buf);
649 }
650
651 enum register_status
652 regcache::raw_read (int regnum, gdb_byte *buf)
653 {
654 gdb_assert (buf != NULL);
655 raw_update (regnum);
656
657 if (m_register_status[regnum] != REG_VALID)
658 memset (buf, 0, m_descr->sizeof_register[regnum]);
659 else
660 memcpy (buf, register_buffer (regnum),
661 m_descr->sizeof_register[regnum]);
662
663 return (enum register_status) m_register_status[regnum];
664 }
665
666 enum register_status
667 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
668 {
669 gdb_assert (regcache != NULL);
670 return regcache->raw_read_signed (regnum, val);
671 }
672
673 enum register_status
674 regcache::raw_read_signed (int regnum, LONGEST *val)
675 {
676 gdb_byte *buf;
677 enum register_status status;
678
679 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
680 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
681 status = raw_read (regnum, buf);
682 if (status == REG_VALID)
683 *val = extract_signed_integer
684 (buf, m_descr->sizeof_register[regnum],
685 gdbarch_byte_order (m_descr->gdbarch));
686 else
687 *val = 0;
688 return status;
689 }
690
691 enum register_status
692 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
693 ULONGEST *val)
694 {
695 gdb_assert (regcache != NULL);
696 return regcache->raw_read_unsigned (regnum, val);
697 }
698
699
700 enum register_status
701 regcache::raw_read_unsigned (int regnum, ULONGEST *val)
702 {
703 gdb_byte *buf;
704 enum register_status status;
705
706 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
707 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
708 status = raw_read (regnum, buf);
709 if (status == REG_VALID)
710 *val = extract_unsigned_integer
711 (buf, m_descr->sizeof_register[regnum],
712 gdbarch_byte_order (m_descr->gdbarch));
713 else
714 *val = 0;
715 return status;
716 }
717
718 void
719 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
720 {
721 gdb_assert (regcache != NULL);
722 regcache->raw_write_signed (regnum, val);
723 }
724
725 void
726 regcache::raw_write_signed (int regnum, LONGEST val)
727 {
728 gdb_byte *buf;
729
730 gdb_assert (regnum >=0 && regnum < m_descr->nr_raw_registers);
731 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
732 store_signed_integer (buf, m_descr->sizeof_register[regnum],
733 gdbarch_byte_order (m_descr->gdbarch), val);
734 raw_write (regnum, buf);
735 }
736
737 void
738 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
739 ULONGEST val)
740 {
741 gdb_assert (regcache != NULL);
742 regcache->raw_write_unsigned (regnum, val);
743 }
744
745 void
746 regcache::raw_write_unsigned (int regnum, ULONGEST val)
747 {
748 gdb_byte *buf;
749
750 gdb_assert (regnum >=0 && regnum < m_descr->nr_raw_registers);
751 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
752 store_unsigned_integer (buf, m_descr->sizeof_register[regnum],
753 gdbarch_byte_order (m_descr->gdbarch), val);
754 raw_write (regnum, buf);
755 }
756
757 LONGEST
758 regcache_raw_get_signed (struct regcache *regcache, int regnum)
759 {
760 LONGEST value;
761 enum register_status status;
762
763 status = regcache_raw_read_signed (regcache, regnum, &value);
764 if (status == REG_UNAVAILABLE)
765 throw_error (NOT_AVAILABLE_ERROR,
766 _("Register %d is not available"), regnum);
767 return value;
768 }
769
770 enum register_status
771 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
772 {
773 return regcache->cooked_read (regnum, buf);
774 }
775
776 enum register_status
777 regcache::cooked_read (int regnum, gdb_byte *buf)
778 {
779 gdb_assert (regnum >= 0);
780 gdb_assert (regnum < m_descr->nr_cooked_registers);
781 if (regnum < m_descr->nr_raw_registers)
782 return raw_read (regnum, buf);
783 else if (m_readonly_p
784 && m_register_status[regnum] != REG_UNKNOWN)
785 {
786 /* Read-only register cache, perhaps the cooked value was
787 cached? */
788 if (m_register_status[regnum] == REG_VALID)
789 memcpy (buf, register_buffer (regnum),
790 m_descr->sizeof_register[regnum]);
791 else
792 memset (buf, 0, m_descr->sizeof_register[regnum]);
793
794 return (enum register_status) m_register_status[regnum];
795 }
796 else if (gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
797 {
798 struct value *mark, *computed;
799 enum register_status result = REG_VALID;
800
801 mark = value_mark ();
802
803 computed = gdbarch_pseudo_register_read_value (m_descr->gdbarch,
804 this, regnum);
805 if (value_entirely_available (computed))
806 memcpy (buf, value_contents_raw (computed),
807 m_descr->sizeof_register[regnum]);
808 else
809 {
810 memset (buf, 0, m_descr->sizeof_register[regnum]);
811 result = REG_UNAVAILABLE;
812 }
813
814 value_free_to_mark (mark);
815
816 return result;
817 }
818 else
819 return gdbarch_pseudo_register_read (m_descr->gdbarch, this,
820 regnum, buf);
821 }
822
823 struct value *
824 regcache_cooked_read_value (struct regcache *regcache, int regnum)
825 {
826 return regcache->cooked_read_value (regnum);
827 }
828
829 struct value *
830 regcache::cooked_read_value (int regnum)
831 {
832 gdb_assert (regnum >= 0);
833 gdb_assert (regnum < m_descr->nr_cooked_registers);
834
835 if (regnum < m_descr->nr_raw_registers
836 || (m_readonly_p && m_register_status[regnum] != REG_UNKNOWN)
837 || !gdbarch_pseudo_register_read_value_p (m_descr->gdbarch))
838 {
839 struct value *result;
840
841 result = allocate_value (register_type (m_descr->gdbarch, regnum));
842 VALUE_LVAL (result) = lval_register;
843 VALUE_REGNUM (result) = regnum;
844
845 /* It is more efficient in general to do this delegation in this
846 direction than in the other one, even though the value-based
847 API is preferred. */
848 if (cooked_read (regnum,
849 value_contents_raw (result)) == REG_UNAVAILABLE)
850 mark_value_bytes_unavailable (result, 0,
851 TYPE_LENGTH (value_type (result)));
852
853 return result;
854 }
855 else
856 return gdbarch_pseudo_register_read_value (m_descr->gdbarch,
857 this, regnum);
858 }
859
860 enum register_status
861 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
862 LONGEST *val)
863 {
864 gdb_assert (regcache != NULL);
865 return regcache->cooked_read_signed (regnum, val);
866 }
867
868 enum register_status
869 regcache::cooked_read_signed (int regnum, LONGEST *val)
870 {
871 enum register_status status;
872 gdb_byte *buf;
873
874 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
875 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
876 status = cooked_read (regnum, buf);
877 if (status == REG_VALID)
878 *val = extract_signed_integer
879 (buf, m_descr->sizeof_register[regnum],
880 gdbarch_byte_order (m_descr->gdbarch));
881 else
882 *val = 0;
883 return status;
884 }
885
886 enum register_status
887 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
888 ULONGEST *val)
889 {
890 gdb_assert (regcache != NULL);
891 return regcache->cooked_read_unsigned (regnum, val);
892 }
893
894 enum register_status
895 regcache::cooked_read_unsigned (int regnum, ULONGEST *val)
896 {
897 enum register_status status;
898 gdb_byte *buf;
899
900 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
901 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
902 status = cooked_read (regnum, buf);
903 if (status == REG_VALID)
904 *val = extract_unsigned_integer
905 (buf, m_descr->sizeof_register[regnum],
906 gdbarch_byte_order (m_descr->gdbarch));
907 else
908 *val = 0;
909 return status;
910 }
911
912 void
913 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
914 LONGEST val)
915 {
916 gdb_assert (regcache != NULL);
917 regcache->cooked_write_signed (regnum, val);
918 }
919
920 void
921 regcache::cooked_write_signed (int regnum, LONGEST val)
922 {
923 gdb_byte *buf;
924
925 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
926 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
927 store_signed_integer (buf, m_descr->sizeof_register[regnum],
928 gdbarch_byte_order (m_descr->gdbarch), val);
929 cooked_write (regnum, buf);
930 }
931
932 void
933 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
934 ULONGEST val)
935 {
936 gdb_assert (regcache != NULL);
937 regcache->cooked_write_unsigned (regnum, val);
938 }
939
940 void
941 regcache::cooked_write_unsigned (int regnum, ULONGEST val)
942 {
943 gdb_byte *buf;
944
945 gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
946 buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
947 store_unsigned_integer (buf, m_descr->sizeof_register[regnum],
948 gdbarch_byte_order (m_descr->gdbarch), val);
949 cooked_write (regnum, buf);
950 }
951
952 /* See regcache.h. */
953
954 void
955 regcache_raw_set_cached_value (struct regcache *regcache, int regnum,
956 const gdb_byte *buf)
957 {
958 regcache->raw_set_cached_value (regnum, buf);
959 }
960
961 void
962 regcache::raw_set_cached_value (int regnum, const gdb_byte *buf)
963 {
964 memcpy (register_buffer (regnum), buf,
965 m_descr->sizeof_register[regnum]);
966 m_register_status[regnum] = REG_VALID;
967 }
968
969 void
970 regcache_raw_write (struct regcache *regcache, int regnum,
971 const gdb_byte *buf)
972 {
973 gdb_assert (regcache != NULL && buf != NULL);
974 regcache->raw_write (regnum, buf);
975 }
976
977 void
978 regcache::raw_write (int regnum, const gdb_byte *buf)
979 {
980 struct cleanup *old_chain;
981
982 gdb_assert (buf != NULL);
983 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
984 gdb_assert (!m_readonly_p);
985
986 /* On the sparc, writing %g0 is a no-op, so we don't even want to
987 change the registers array if something writes to this register. */
988 if (gdbarch_cannot_store_register (arch (), regnum))
989 return;
990
991 /* If we have a valid copy of the register, and new value == old
992 value, then don't bother doing the actual store. */
993 if (get_register_status (regnum) == REG_VALID
994 && (memcmp (register_buffer (regnum), buf,
995 m_descr->sizeof_register[regnum]) == 0))
996 return;
997
998 target_prepare_to_store (this);
999 raw_set_cached_value (regnum, buf);
1000
1001 /* Register a cleanup function for invalidating the register after it is
1002 written, in case of a failure. */
1003 old_chain = make_cleanup_regcache_invalidate (this, regnum);
1004
1005 target_store_registers (this, regnum);
1006
1007 /* The target did not throw an error so we can discard invalidating the
1008 register and restore the cleanup chain to what it was. */
1009 discard_cleanups (old_chain);
1010 }
1011
1012 void
1013 regcache_cooked_write (struct regcache *regcache, int regnum,
1014 const gdb_byte *buf)
1015 {
1016 regcache->cooked_write (regnum, buf);
1017 }
1018
1019 void
1020 regcache::cooked_write (int regnum, const gdb_byte *buf)
1021 {
1022 gdb_assert (regnum >= 0);
1023 gdb_assert (regnum < m_descr->nr_cooked_registers);
1024 if (regnum < m_descr->nr_raw_registers)
1025 raw_write (regnum, buf);
1026 else
1027 gdbarch_pseudo_register_write (m_descr->gdbarch, this,
1028 regnum, buf);
1029 }
1030
1031 /* Perform a partial register transfer using a read, modify, write
1032 operation. */
1033
1034 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
1035 void *buf);
1036 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
1037 const void *buf);
1038
1039 enum register_status
1040 regcache::xfer_part (int regnum, int offset, int len, void *in,
1041 const void *out,
1042 enum register_status (*read) (struct regcache *regcache,
1043 int regnum,
1044 gdb_byte *buf),
1045 void (*write) (struct regcache *regcache, int regnum,
1046 const gdb_byte *buf))
1047 {
1048 struct gdbarch *gdbarch = arch ();
1049 gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
1050
1051 gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
1052 gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
1053 /* Something to do? */
1054 if (offset + len == 0)
1055 return REG_VALID;
1056 /* Read (when needed) ... */
1057 if (in != NULL
1058 || offset > 0
1059 || offset + len < m_descr->sizeof_register[regnum])
1060 {
1061 enum register_status status;
1062
1063 gdb_assert (read != NULL);
1064 status = read (this, regnum, reg);
1065 if (status != REG_VALID)
1066 return status;
1067 }
1068 /* ... modify ... */
1069 if (in != NULL)
1070 memcpy (in, reg + offset, len);
1071 if (out != NULL)
1072 memcpy (reg + offset, out, len);
1073 /* ... write (when needed). */
1074 if (out != NULL)
1075 {
1076 gdb_assert (write != NULL);
1077 write (this, regnum, reg);
1078 }
1079
1080 return REG_VALID;
1081 }
1082
1083 enum register_status
1084 regcache_raw_read_part (struct regcache *regcache, int regnum,
1085 int offset, int len, gdb_byte *buf)
1086 {
1087 return regcache->raw_read_part (regnum, offset, len, buf);
1088 }
1089
1090 enum register_status
1091 regcache::raw_read_part (int regnum, int offset, int len, gdb_byte *buf)
1092 {
1093 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1094 return xfer_part (regnum, offset, len, buf, NULL,
1095 regcache_raw_read, regcache_raw_write);
1096 }
1097
1098 void
1099 regcache_raw_write_part (struct regcache *regcache, int regnum,
1100 int offset, int len, const gdb_byte *buf)
1101 {
1102 regcache->raw_write_part (regnum, offset, len, buf);
1103 }
1104
1105 void
1106 regcache::raw_write_part (int regnum, int offset, int len,
1107 const gdb_byte *buf)
1108 {
1109 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1110 xfer_part (regnum, offset, len, NULL, buf, regcache_raw_read,
1111 regcache_raw_write);
1112 }
1113
1114 enum register_status
1115 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1116 int offset, int len, gdb_byte *buf)
1117 {
1118 return regcache->cooked_read_part (regnum, offset, len, buf);
1119 }
1120
1121
1122 enum register_status
1123 regcache::cooked_read_part (int regnum, int offset, int len, gdb_byte *buf)
1124 {
1125 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1126 return xfer_part (regnum, offset, len, buf, NULL,
1127 regcache_cooked_read, regcache_cooked_write);
1128 }
1129
1130 void
1131 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1132 int offset, int len, const gdb_byte *buf)
1133 {
1134 regcache->cooked_write_part (regnum, offset, len, buf);
1135 }
1136
1137 void
1138 regcache::cooked_write_part (int regnum, int offset, int len,
1139 const gdb_byte *buf)
1140 {
1141 gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
1142 xfer_part (regnum, offset, len, NULL, buf,
1143 regcache_cooked_read, regcache_cooked_write);
1144 }
1145
1146 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1147
1148 void
1149 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1150 {
1151 gdb_assert (regcache != NULL);
1152 regcache->raw_supply (regnum, buf);
1153 }
1154
1155 void
1156 regcache::raw_supply (int regnum, const void *buf)
1157 {
1158 void *regbuf;
1159 size_t size;
1160
1161 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1162 gdb_assert (!m_readonly_p);
1163
1164 regbuf = register_buffer (regnum);
1165 size = m_descr->sizeof_register[regnum];
1166
1167 if (buf)
1168 {
1169 memcpy (regbuf, buf, size);
1170 m_register_status[regnum] = REG_VALID;
1171 }
1172 else
1173 {
1174 /* This memset not strictly necessary, but better than garbage
1175 in case the register value manages to escape somewhere (due
1176 to a bug, no less). */
1177 memset (regbuf, 0, size);
1178 m_register_status[regnum] = REG_UNAVAILABLE;
1179 }
1180 }
1181
1182 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1183 address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1184 the register size is greater than ADDR_LEN, then the integer will be sign or
1185 zero extended. If the register size is smaller than the integer, then the
1186 most significant bytes of the integer will be truncated. */
1187
1188 void
1189 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1190 bool is_signed)
1191 {
1192 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1193 gdb_byte *regbuf;
1194 size_t regsize;
1195
1196 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1197 gdb_assert (!m_readonly_p);
1198
1199 regbuf = register_buffer (regnum);
1200 regsize = m_descr->sizeof_register[regnum];
1201
1202 copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1203 byte_order);
1204 m_register_status[regnum] = REG_VALID;
1205 }
1206
1207 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1208 as calling raw_supply with NULL (which will set the state to
1209 unavailable). */
1210
1211 void
1212 regcache::raw_supply_zeroed (int regnum)
1213 {
1214 void *regbuf;
1215 size_t size;
1216
1217 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1218 gdb_assert (!m_readonly_p);
1219
1220 regbuf = register_buffer (regnum);
1221 size = m_descr->sizeof_register[regnum];
1222
1223 memset (regbuf, 0, size);
1224 m_register_status[regnum] = REG_VALID;
1225 }
1226
1227 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1228
1229 void
1230 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1231 {
1232 gdb_assert (regcache != NULL && buf != NULL);
1233 regcache->raw_collect (regnum, buf);
1234 }
1235
1236 void
1237 regcache::raw_collect (int regnum, void *buf) const
1238 {
1239 const void *regbuf;
1240 size_t size;
1241
1242 gdb_assert (buf != NULL);
1243 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1244
1245 regbuf = register_buffer (regnum);
1246 size = m_descr->sizeof_register[regnum];
1247 memcpy (buf, regbuf, size);
1248 }
1249
1250 /* Transfer a single or all registers belonging to a certain register
1251 set to or from a buffer. This is the main worker function for
1252 regcache_supply_regset and regcache_collect_regset. */
1253
1254 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1255 at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1256 If ADDR_LEN is greater than the register size, then the integer will be sign
1257 or zero extended. If ADDR_LEN is smaller than the register size, then the
1258 most significant bytes of the integer will be truncated. */
1259
1260 void
1261 regcache::raw_collect_integer (int regnum, gdb_byte *addr, int addr_len,
1262 bool is_signed) const
1263 {
1264 enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1265 const gdb_byte *regbuf;
1266 size_t regsize;
1267
1268 gdb_assert (regnum >= 0 && regnum < m_descr->nr_raw_registers);
1269
1270 regbuf = register_buffer (regnum);
1271 regsize = m_descr->sizeof_register[regnum];
1272
1273 copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1274 byte_order);
1275 }
1276
1277 void
1278 regcache::transfer_regset (const struct regset *regset,
1279 struct regcache *out_regcache,
1280 int regnum, const void *in_buf,
1281 void *out_buf, size_t size) const
1282 {
1283 const struct regcache_map_entry *map;
1284 int offs = 0, count;
1285
1286 for (map = (const struct regcache_map_entry *) regset->regmap;
1287 (count = map->count) != 0;
1288 map++)
1289 {
1290 int regno = map->regno;
1291 int slot_size = map->size;
1292
1293 if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1294 slot_size = m_descr->sizeof_register[regno];
1295
1296 if (regno == REGCACHE_MAP_SKIP
1297 || (regnum != -1
1298 && (regnum < regno || regnum >= regno + count)))
1299 offs += count * slot_size;
1300
1301 else if (regnum == -1)
1302 for (; count--; regno++, offs += slot_size)
1303 {
1304 if (offs + slot_size > size)
1305 break;
1306
1307 if (out_buf)
1308 raw_collect (regno, (gdb_byte *) out_buf + offs);
1309 else
1310 out_regcache->raw_supply (regno, in_buf
1311 ? (const gdb_byte *) in_buf + offs
1312 : NULL);
1313 }
1314 else
1315 {
1316 /* Transfer a single register and return. */
1317 offs += (regnum - regno) * slot_size;
1318 if (offs + slot_size > size)
1319 return;
1320
1321 if (out_buf)
1322 raw_collect (regnum, (gdb_byte *) out_buf + offs);
1323 else
1324 out_regcache->raw_supply (regnum, in_buf
1325 ? (const gdb_byte *) in_buf + offs
1326 : NULL);
1327 return;
1328 }
1329 }
1330 }
1331
1332 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1333 in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1334 If BUF is NULL, set the register(s) to "unavailable" status. */
1335
1336 void
1337 regcache_supply_regset (const struct regset *regset,
1338 struct regcache *regcache,
1339 int regnum, const void *buf, size_t size)
1340 {
1341 regcache->supply_regset (regset, regnum, buf, size);
1342 }
1343
1344 void
1345 regcache::supply_regset (const struct regset *regset,
1346 int regnum, const void *buf, size_t size)
1347 {
1348 transfer_regset (regset, this, regnum, buf, NULL, size);
1349 }
1350
1351 /* Collect register REGNUM from REGCACHE to BUF, using the register
1352 map in REGSET. If REGNUM is -1, do this for all registers in
1353 REGSET. */
1354
1355 void
1356 regcache_collect_regset (const struct regset *regset,
1357 const struct regcache *regcache,
1358 int regnum, void *buf, size_t size)
1359 {
1360 regcache->collect_regset (regset, regnum, buf, size);
1361 }
1362
1363 void
1364 regcache::collect_regset (const struct regset *regset,
1365 int regnum, void *buf, size_t size) const
1366 {
1367 transfer_regset (regset, NULL, regnum, NULL, buf, size);
1368 }
1369
1370
1371 /* Special handling for register PC. */
1372
1373 CORE_ADDR
1374 regcache_read_pc (struct regcache *regcache)
1375 {
1376 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1377
1378 CORE_ADDR pc_val;
1379
1380 if (gdbarch_read_pc_p (gdbarch))
1381 pc_val = gdbarch_read_pc (gdbarch, regcache);
1382 /* Else use per-frame method on get_current_frame. */
1383 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1384 {
1385 ULONGEST raw_val;
1386
1387 if (regcache_cooked_read_unsigned (regcache,
1388 gdbarch_pc_regnum (gdbarch),
1389 &raw_val) == REG_UNAVAILABLE)
1390 throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1391
1392 pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1393 }
1394 else
1395 internal_error (__FILE__, __LINE__,
1396 _("regcache_read_pc: Unable to find PC"));
1397 return pc_val;
1398 }
1399
1400 void
1401 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1402 {
1403 struct gdbarch *gdbarch = get_regcache_arch (regcache);
1404
1405 if (gdbarch_write_pc_p (gdbarch))
1406 gdbarch_write_pc (gdbarch, regcache, pc);
1407 else if (gdbarch_pc_regnum (gdbarch) >= 0)
1408 regcache_cooked_write_unsigned (regcache,
1409 gdbarch_pc_regnum (gdbarch), pc);
1410 else
1411 internal_error (__FILE__, __LINE__,
1412 _("regcache_write_pc: Unable to update PC"));
1413
1414 /* Writing the PC (for instance, from "load") invalidates the
1415 current frame. */
1416 reinit_frame_cache ();
1417 }
1418
1419 void
1420 regcache::debug_print_register (const char *func, int regno)
1421 {
1422 struct gdbarch *gdbarch = arch ();
1423
1424 fprintf_unfiltered (gdb_stdlog, "%s ", func);
1425 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1426 && gdbarch_register_name (gdbarch, regno) != NULL
1427 && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1428 fprintf_unfiltered (gdb_stdlog, "(%s)",
1429 gdbarch_register_name (gdbarch, regno));
1430 else
1431 fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1432 if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1433 {
1434 enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1435 int size = register_size (gdbarch, regno);
1436 gdb_byte *buf = register_buffer (regno);
1437
1438 fprintf_unfiltered (gdb_stdlog, " = ");
1439 for (int i = 0; i < size; i++)
1440 {
1441 fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1442 }
1443 if (size <= sizeof (LONGEST))
1444 {
1445 ULONGEST val = extract_unsigned_integer (buf, size, byte_order);
1446
1447 fprintf_unfiltered (gdb_stdlog, " %s %s",
1448 core_addr_to_string_nz (val), plongest (val));
1449 }
1450 }
1451 fprintf_unfiltered (gdb_stdlog, "\n");
1452 }
1453
1454 static void
1455 reg_flush_command (char *command, int from_tty)
1456 {
1457 /* Force-flush the register cache. */
1458 registers_changed ();
1459 if (from_tty)
1460 printf_filtered (_("Register cache flushed.\n"));
1461 }
1462
1463 void
1464 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1465 {
1466 struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1467 struct gdbarch *gdbarch = m_descr->gdbarch;
1468 int regnum;
1469 int footnote_nr = 0;
1470 int footnote_register_size = 0;
1471 int footnote_register_offset = 0;
1472 int footnote_register_type_name_null = 0;
1473 long register_offset = 0;
1474 gdb_byte buf[MAX_REGISTER_SIZE];
1475
1476 #if 0
1477 fprintf_unfiltered (file, "nr_raw_registers %d\n",
1478 m_descr->nr_raw_registers);
1479 fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1480 m_descr->nr_cooked_registers);
1481 fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1482 m_descr->sizeof_raw_registers);
1483 fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1484 m_descr->sizeof_raw_register_status);
1485 fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
1486 gdbarch_num_regs (gdbarch));
1487 fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1488 gdbarch_num_pseudo_regs (gdbarch));
1489 #endif
1490
1491 gdb_assert (m_descr->nr_cooked_registers
1492 == (gdbarch_num_regs (gdbarch)
1493 + gdbarch_num_pseudo_regs (gdbarch)));
1494
1495 for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1496 {
1497 /* Name. */
1498 if (regnum < 0)
1499 fprintf_unfiltered (file, " %-10s", "Name");
1500 else
1501 {
1502 const char *p = gdbarch_register_name (gdbarch, regnum);
1503
1504 if (p == NULL)
1505 p = "";
1506 else if (p[0] == '\0')
1507 p = "''";
1508 fprintf_unfiltered (file, " %-10s", p);
1509 }
1510
1511 /* Number. */
1512 if (regnum < 0)
1513 fprintf_unfiltered (file, " %4s", "Nr");
1514 else
1515 fprintf_unfiltered (file, " %4d", regnum);
1516
1517 /* Relative number. */
1518 if (regnum < 0)
1519 fprintf_unfiltered (file, " %4s", "Rel");
1520 else if (regnum < gdbarch_num_regs (gdbarch))
1521 fprintf_unfiltered (file, " %4d", regnum);
1522 else
1523 fprintf_unfiltered (file, " %4d",
1524 (regnum - gdbarch_num_regs (gdbarch)));
1525
1526 /* Offset. */
1527 if (regnum < 0)
1528 fprintf_unfiltered (file, " %6s ", "Offset");
1529 else
1530 {
1531 fprintf_unfiltered (file, " %6ld",
1532 m_descr->register_offset[regnum]);
1533 if (register_offset != m_descr->register_offset[regnum]
1534 || (regnum > 0
1535 && (m_descr->register_offset[regnum]
1536 != (m_descr->register_offset[regnum - 1]
1537 + m_descr->sizeof_register[regnum - 1])))
1538 )
1539 {
1540 if (!footnote_register_offset)
1541 footnote_register_offset = ++footnote_nr;
1542 fprintf_unfiltered (file, "*%d", footnote_register_offset);
1543 }
1544 else
1545 fprintf_unfiltered (file, " ");
1546 register_offset = (m_descr->register_offset[regnum]
1547 + m_descr->sizeof_register[regnum]);
1548 }
1549
1550 /* Size. */
1551 if (regnum < 0)
1552 fprintf_unfiltered (file, " %5s ", "Size");
1553 else
1554 fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1555
1556 /* Type. */
1557 {
1558 const char *t;
1559
1560 if (regnum < 0)
1561 t = "Type";
1562 else
1563 {
1564 static const char blt[] = "builtin_type";
1565
1566 t = TYPE_NAME (register_type (arch (), regnum));
1567 if (t == NULL)
1568 {
1569 char *n;
1570
1571 if (!footnote_register_type_name_null)
1572 footnote_register_type_name_null = ++footnote_nr;
1573 n = xstrprintf ("*%d", footnote_register_type_name_null);
1574 make_cleanup (xfree, n);
1575 t = n;
1576 }
1577 /* Chop a leading builtin_type. */
1578 if (startswith (t, blt))
1579 t += strlen (blt);
1580 }
1581 fprintf_unfiltered (file, " %-15s", t);
1582 }
1583
1584 /* Leading space always present. */
1585 fprintf_unfiltered (file, " ");
1586
1587 /* Value, raw. */
1588 if (what_to_dump == regcache_dump_raw)
1589 {
1590 if (regnum < 0)
1591 fprintf_unfiltered (file, "Raw value");
1592 else if (regnum >= m_descr->nr_raw_registers)
1593 fprintf_unfiltered (file, "<cooked>");
1594 else if (get_register_status (regnum) == REG_UNKNOWN)
1595 fprintf_unfiltered (file, "<invalid>");
1596 else if (get_register_status (regnum) == REG_UNAVAILABLE)
1597 fprintf_unfiltered (file, "<unavailable>");
1598 else
1599 {
1600 raw_read (regnum, buf);
1601 print_hex_chars (file, buf,
1602 m_descr->sizeof_register[regnum],
1603 gdbarch_byte_order (gdbarch));
1604 }
1605 }
1606
1607 /* Value, cooked. */
1608 if (what_to_dump == regcache_dump_cooked)
1609 {
1610 if (regnum < 0)
1611 fprintf_unfiltered (file, "Cooked value");
1612 else
1613 {
1614 enum register_status status;
1615
1616 status = cooked_read (regnum, buf);
1617 if (status == REG_UNKNOWN)
1618 fprintf_unfiltered (file, "<invalid>");
1619 else if (status == REG_UNAVAILABLE)
1620 fprintf_unfiltered (file, "<unavailable>");
1621 else
1622 print_hex_chars (file, buf,
1623 m_descr->sizeof_register[regnum],
1624 gdbarch_byte_order (gdbarch));
1625 }
1626 }
1627
1628 /* Group members. */
1629 if (what_to_dump == regcache_dump_groups)
1630 {
1631 if (regnum < 0)
1632 fprintf_unfiltered (file, "Groups");
1633 else
1634 {
1635 const char *sep = "";
1636 struct reggroup *group;
1637
1638 for (group = reggroup_next (gdbarch, NULL);
1639 group != NULL;
1640 group = reggroup_next (gdbarch, group))
1641 {
1642 if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1643 {
1644 fprintf_unfiltered (file,
1645 "%s%s", sep, reggroup_name (group));
1646 sep = ",";
1647 }
1648 }
1649 }
1650 }
1651
1652 /* Remote packet configuration. */
1653 if (what_to_dump == regcache_dump_remote)
1654 {
1655 if (regnum < 0)
1656 {
1657 fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1658 }
1659 else if (regnum < m_descr->nr_raw_registers)
1660 {
1661 int pnum, poffset;
1662
1663 if (remote_register_number_and_offset (arch (), regnum,
1664 &pnum, &poffset))
1665 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1666 }
1667 }
1668
1669 fprintf_unfiltered (file, "\n");
1670 }
1671
1672 if (footnote_register_size)
1673 fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1674 footnote_register_size);
1675 if (footnote_register_offset)
1676 fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1677 footnote_register_offset);
1678 if (footnote_register_type_name_null)
1679 fprintf_unfiltered (file,
1680 "*%d: Register type's name NULL.\n",
1681 footnote_register_type_name_null);
1682 do_cleanups (cleanups);
1683 }
1684
1685 static void
1686 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1687 {
1688 if (args == NULL)
1689 get_current_regcache ()->dump (gdb_stdout, what_to_dump);
1690 else
1691 {
1692 stdio_file file;
1693
1694 if (!file.open (args, "w"))
1695 perror_with_name (_("maintenance print architecture"));
1696 get_current_regcache ()->dump (&file, what_to_dump);
1697 }
1698 }
1699
1700 static void
1701 maintenance_print_registers (char *args, int from_tty)
1702 {
1703 regcache_print (args, regcache_dump_none);
1704 }
1705
1706 static void
1707 maintenance_print_raw_registers (char *args, int from_tty)
1708 {
1709 regcache_print (args, regcache_dump_raw);
1710 }
1711
1712 static void
1713 maintenance_print_cooked_registers (char *args, int from_tty)
1714 {
1715 regcache_print (args, regcache_dump_cooked);
1716 }
1717
1718 static void
1719 maintenance_print_register_groups (char *args, int from_tty)
1720 {
1721 regcache_print (args, regcache_dump_groups);
1722 }
1723
1724 static void
1725 maintenance_print_remote_registers (char *args, int from_tty)
1726 {
1727 regcache_print (args, regcache_dump_remote);
1728 }
1729
1730 #if GDB_SELF_TEST
1731 #include "selftest.h"
1732
1733 namespace selftests {
1734
1735 class regcache_access : public regcache
1736 {
1737 public:
1738
1739 /* Return the number of elements in current_regcache. */
1740
1741 static size_t
1742 current_regcache_size ()
1743 {
1744 return std::distance (regcache::current_regcache.begin (),
1745 regcache::current_regcache.end ());
1746 }
1747 };
1748
1749 static void
1750 current_regcache_test (void)
1751 {
1752 /* It is empty at the start. */
1753 SELF_CHECK (regcache_access::current_regcache_size () == 0);
1754
1755 ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1756
1757 /* Get regcache from ptid1, a new regcache is added to
1758 current_regcache. */
1759 regcache *regcache = get_thread_arch_aspace_regcache (ptid1,
1760 target_gdbarch (),
1761 NULL);
1762
1763 SELF_CHECK (regcache != NULL);
1764 SELF_CHECK (regcache->ptid () == ptid1);
1765 SELF_CHECK (regcache_access::current_regcache_size () == 1);
1766
1767 /* Get regcache from ptid2, a new regcache is added to
1768 current_regcache. */
1769 regcache = get_thread_arch_aspace_regcache (ptid2,
1770 target_gdbarch (),
1771 NULL);
1772 SELF_CHECK (regcache != NULL);
1773 SELF_CHECK (regcache->ptid () == ptid2);
1774 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1775
1776 /* Get regcache from ptid3, a new regcache is added to
1777 current_regcache. */
1778 regcache = get_thread_arch_aspace_regcache (ptid3,
1779 target_gdbarch (),
1780 NULL);
1781 SELF_CHECK (regcache != NULL);
1782 SELF_CHECK (regcache->ptid () == ptid3);
1783 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1784
1785 /* Get regcache from ptid2 again, nothing is added to
1786 current_regcache. */
1787 regcache = get_thread_arch_aspace_regcache (ptid2,
1788 target_gdbarch (),
1789 NULL);
1790 SELF_CHECK (regcache != NULL);
1791 SELF_CHECK (regcache->ptid () == ptid2);
1792 SELF_CHECK (regcache_access::current_regcache_size () == 3);
1793
1794 /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1795 current_regcache. */
1796 registers_changed_ptid (ptid2);
1797 SELF_CHECK (regcache_access::current_regcache_size () == 2);
1798 }
1799
1800 } // namespace selftests
1801 #endif /* GDB_SELF_TEST */
1802
1803 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1804
1805 void
1806 _initialize_regcache (void)
1807 {
1808 regcache_descr_handle
1809 = gdbarch_data_register_post_init (init_regcache_descr);
1810
1811 observer_attach_target_changed (regcache_observer_target_changed);
1812 observer_attach_thread_ptid_changed (regcache::regcache_thread_ptid_changed);
1813
1814 add_com ("flushregs", class_maintenance, reg_flush_command,
1815 _("Force gdb to flush its register cache (maintainer command)"));
1816
1817 add_cmd ("registers", class_maintenance, maintenance_print_registers,
1818 _("Print the internal register configuration.\n"
1819 "Takes an optional file parameter."), &maintenanceprintlist);
1820 add_cmd ("raw-registers", class_maintenance,
1821 maintenance_print_raw_registers,
1822 _("Print the internal register configuration "
1823 "including raw values.\n"
1824 "Takes an optional file parameter."), &maintenanceprintlist);
1825 add_cmd ("cooked-registers", class_maintenance,
1826 maintenance_print_cooked_registers,
1827 _("Print the internal register configuration "
1828 "including cooked values.\n"
1829 "Takes an optional file parameter."), &maintenanceprintlist);
1830 add_cmd ("register-groups", class_maintenance,
1831 maintenance_print_register_groups,
1832 _("Print the internal register configuration "
1833 "including each register's group.\n"
1834 "Takes an optional file parameter."),
1835 &maintenanceprintlist);
1836 add_cmd ("remote-registers", class_maintenance,
1837 maintenance_print_remote_registers, _("\
1838 Print the internal register configuration including each register's\n\
1839 remote register number and buffer offset in the g/G packets.\n\
1840 Takes an optional file parameter."),
1841 &maintenanceprintlist);
1842 #if GDB_SELF_TEST
1843 register_self_test (selftests::current_regcache_test);
1844 #endif
1845 }
This page took 0.096202 seconds and 5 git commands to generate.