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