GDB (xrefs)
/tmp/gdb-8.1/gdb/regcache.c
Go to the documentation of this file.
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2 
3  Copyright (C) 1986-2018 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 
43 
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. */
55 
56  /* The cooked register space. Each cooked register in the range
57  [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
58  register. The remaining [NR_RAW_REGISTERS
59  .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
60  both raw registers and memory by the architecture methods
61  gdbarch_pseudo_register_read and gdbarch_pseudo_register_write. */
64 
65  /* Offset and size (in 8 bit bytes), of each register in the
66  register cache. All registers (including those in the range
67  [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
68  offset. */
71 
72  /* Cached table containing the type of each register. */
73  struct type **register_type;
74 };
75 
76 static void *
78 {
79  int i;
80  struct regcache_descr *descr;
81  gdb_assert (gdbarch != NULL);
82 
83  /* Create an initial, zero filled, table. */
85  descr->gdbarch = gdbarch;
86 
87  /* Total size of the register space. The raw registers are mapped
88  directly onto the raw register cache while the pseudo's are
89  either mapped onto raw-registers or memory. */
92 
93  /* Fill in a table of register types. */
94  descr->register_type
96  struct type *);
97  for (i = 0; i < descr->nr_cooked_registers; i++)
99 
100  /* Construct a strictly RAW register cache. Don't allow pseudo's
101  into the register cache. */
102 
103  /* Lay out the register cache.
104 
105  NOTE: cagney/2002-05-22: Only register_type() is used when
106  constructing the register cache. It is assumed that the
107  register's raw size, virtual size and type length are all the
108  same. */
109 
110  {
111  long offset = 0;
112 
113  descr->sizeof_register
115  descr->register_offset
117  for (i = 0; i < gdbarch_num_regs (gdbarch); i++)
118  {
119  descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
120  descr->register_offset[i] = offset;
121  offset += descr->sizeof_register[i];
123  }
124  /* Set the real size of the raw register cache buffer. */
125  descr->sizeof_raw_registers = offset;
126 
127  for (; i < descr->nr_cooked_registers; i++)
128  {
129  descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
130  descr->register_offset[i] = offset;
131  offset += descr->sizeof_register[i];
133  }
134  /* Set the real size of the readonly register cache buffer. */
136  }
137 
138  return descr;
139 }
140 
141 static struct regcache_descr *
143 {
144  return (struct regcache_descr *) gdbarch_data (gdbarch,
146 }
147 
148 /* Utility functions returning useful register attributes stored in
149  the regcache descr. */
150 
151 struct type *
153 {
154  struct regcache_descr *descr = regcache_descr (gdbarch);
155 
156  gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
157  return descr->register_type[regnum];
158 }
159 
160 /* Utility functions returning useful register attributes stored in
161  the regcache descr. */
162 
163 int
165 {
166  struct regcache_descr *descr = regcache_descr (gdbarch);
167  int size;
168 
169  gdb_assert (regnum >= 0
172  size = descr->sizeof_register[regnum];
173  return size;
174 }
175 
176 /* See common/common-regcache.h. */
177 
178 int
180 {
181  return register_size (regcache->arch (), n);
182 }
183 
185  bool readonly_p_)
186  : m_aspace (aspace_), m_readonly_p (readonly_p_)
187 {
188  gdb_assert (gdbarch != NULL);
190 
191  if (m_readonly_p)
192  {
194  m_register_status = XCNEWVEC (signed char,
196  }
197  else
198  {
201  }
203 }
204 
205 static enum register_status
206 do_cooked_read (void *src, int regnum, gdb_byte *buf)
207 {
208  struct regcache *regcache = (struct regcache *) src;
209 
210  return regcache_cooked_read (regcache, regnum, buf);
211 }
212 
214  : regcache (src.arch (), nullptr, true)
215 {
216  gdb_assert (!src.m_readonly_p);
217  save (do_cooked_read, (void *) &src);
218 }
219 
220 gdbarch *
222 {
223  return m_descr->gdbarch;
224 }
225 
226 /* See regcache.h. */
227 
228 ptid_t
230 {
232 
233  return regcache->ptid ();
234 }
235 
236 /* Cleanup class for invalidating a register. */
237 
239 {
240 public:
241 
243  : m_regcache (regcache),
244  m_regnum (regnum)
245  {
246  }
247 
249  {
250  if (m_regcache != nullptr)
252  }
253 
255 
256  void release ()
257  {
258  m_regcache = nullptr;
259  }
260 
261 private:
262 
264  int m_regnum;
265 };
266 
267 /* Return a pointer to register REGNUM's buffer cache. */
268 
269 gdb_byte *
271 {
273 }
274 
275 void
278 {
279  regcache->save (cooked_read, src);
280 }
281 
282 void
284  void *src)
285 {
286  struct gdbarch *gdbarch = m_descr->gdbarch;
287  int regnum;
288 
289  /* The DST should be `read-only', if it wasn't then the save would
290  end up trying to write the register values back out to the
291  target. */
293  /* Clear the dest. */
296  /* Copy over any registers (identified by their membership in the
297  save_reggroup) and mark them as valid. The full [0 .. gdbarch_num_regs +
298  gdbarch_num_pseudo_regs) range is checked since some architectures need
299  to save/restore `cooked' registers that live in memory. */
301  {
303  {
304  gdb_byte *dst_buf = register_buffer (regnum);
305  enum register_status status = cooked_read (src, regnum, dst_buf);
306 
308 
309  if (status != REG_VALID)
310  memset (dst_buf, 0, register_size (gdbarch, regnum));
311 
313  }
314  }
315 }
316 
317 void
319 {
320  struct gdbarch *gdbarch = m_descr->gdbarch;
321  int regnum;
322 
323  /* The dst had better not be read-only. If it is, the `restore'
324  doesn't make much sense. */
326  gdb_assert (src->m_readonly_p);
327  /* Copy over any registers, being careful to only restore those that
328  were both saved and need to be restored. The full [0 .. gdbarch_num_regs
329  + gdbarch_num_pseudo_regs) range is checked since some architectures need
330  to save/restore `cooked' registers that live in memory. */
332  {
334  {
335  if (src->m_register_status[regnum] == REG_VALID)
337  }
338  }
339 }
340 
341 void
342 regcache_cpy (struct regcache *dst, struct regcache *src)
343 {
344  gdb_assert (src != NULL && dst != NULL);
345  gdb_assert (src->m_descr->gdbarch == dst->m_descr->gdbarch);
346  gdb_assert (src != dst);
347  gdb_assert (src->m_readonly_p && !dst->m_readonly_p);
348 
349  dst->restore (src);
350 }
351 
352 struct regcache *
353 regcache_dup (struct regcache *src)
354 {
355  return new regcache (regcache::readonly, *src);
356 }
357 
358 enum register_status
360 {
361  gdb_assert (regcache != NULL);
363 }
364 
365 enum register_status
367 {
368  gdb_assert (regnum >= 0);
369  if (m_readonly_p)
370  gdb_assert (regnum < m_descr->nr_cooked_registers);
371  else
373 
375 }
376 
377 void
379 {
380  gdb_assert (regcache != NULL);
382 }
383 
384 void
386 {
390 }
391 
392 void
394 {
395  gdb_assert (regnum >= 0 && regnum < gdbarch_num_regs (arch ()));
396 }
397 
398 /* Global structure containing the current regcache. */
399 
400 /* NOTE: this is a write-through cache. There is no "dirty" bit for
401  recording if the register values have been changed (eg. by the
402  user). Therefore all registers must be written back to the
403  target when appropriate. */
404 std::forward_list<regcache *> regcache::current_regcache;
405 
406 struct regcache *
408  struct address_space *aspace)
409 {
410  for (const auto &regcache : regcache::current_regcache)
411  if (ptid_equal (regcache->ptid (), ptid) && regcache->arch () == gdbarch)
412  return regcache;
413 
414  regcache *new_regcache = new regcache (gdbarch, aspace, false);
415 
416  regcache::current_regcache.push_front (new_regcache);
417  new_regcache->set_ptid (ptid);
418 
419  return new_regcache;
420 }
421 
422 struct regcache *
424 {
426 
428 }
429 
432 
433 struct regcache *
435 {
437  {
440  }
441 
443 }
444 
445 struct regcache *
447 {
449 }
450 
451 /* See common/common-regcache.h. */
452 
453 struct regcache *
455 {
456  return get_thread_regcache (ptid);
457 }
458 
459 /* Observer for the target_changed event. */
460 
461 static void
463 {
465 }
466 
467 /* Update global variables old ptids to hold NEW_PTID if they were
468  holding OLD_PTID. */
469 void
471 {
473  {
474  if (ptid_equal (regcache->ptid (), old_ptid))
475  regcache->set_ptid (new_ptid);
476  }
477 }
478 
479 /* Low level examining and depositing of registers.
480 
481  The caller is responsible for making sure that the inferior is
482  stopped before calling the fetching routines, or it will get
483  garbage. (a change from GDB version 3, in which the caller got the
484  value from the last stop). */
485 
486 /* REGISTERS_CHANGED ()
487 
488  Indicate that registers may have changed, so invalidate the cache. */
489 
490 void
492 {
493  for (auto oit = regcache::current_regcache.before_begin (),
494  it = std::next (oit);
495  it != regcache::current_regcache.end ();
496  )
497  {
498  if (ptid_match ((*it)->ptid (), ptid))
499  {
500  delete *it;
501  it = regcache::current_regcache.erase_after (oit);
502  }
503  else
504  oit = it++;
505  }
506 
508  {
510  current_thread_arch = NULL;
511  }
512 
514  {
515  /* We just deleted the regcache of the current thread. Need to
516  forget about any frames we have cached, too. */
518  }
519 }
520 
521 void
523 {
525 
526  /* Force cleanup of any alloca areas if using C alloca instead of
527  a builtin alloca. This particular call is used to clean up
528  areas allocated by low level target code which may build up
529  during lengthy interactions between gdb and the target before
530  gdb gives control to the user (ie watchpoints). */
531  alloca (0);
532 }
533 
534 void
536 {
537  gdb_assert (regcache != NULL);
538 
540 }
541 
542 void
544 {
546 
547  /* Make certain that the register cache is up-to-date with respect
548  to the current thread. This switching shouldn't be necessary
549  only there is still only one target side register cache. Sigh!
550  On the bright side, at least there is a regcache object. */
551 
553  {
555 
556  /* A number of targets can't access the whole set of raw
557  registers (because the debug API provides no means to get at
558  them). */
561  }
562 }
563 
564 enum register_status
566 {
567  return regcache->raw_read (regnum, buf);
568 }
569 
570 enum register_status
572 {
573  gdb_assert (buf != NULL);
574  raw_update (regnum);
575 
577  memset (buf, 0, m_descr->sizeof_register[regnum]);
578  else
579  memcpy (buf, register_buffer (regnum),
581 
583 }
584 
585 enum register_status
587 {
588  gdb_assert (regcache != NULL);
589  return regcache->raw_read (regnum, val);
590 }
591 
592 template<typename T, typename>
593 enum register_status
595 {
596  gdb_byte *buf;
597  enum register_status status;
598 
600  buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
601  status = raw_read (regnum, buf);
602  if (status == REG_VALID)
603  *val = extract_integer<T> (buf,
606  else
607  *val = 0;
608  return status;
609 }
610 
611 enum register_status
613  ULONGEST *val)
614 {
615  gdb_assert (regcache != NULL);
616  return regcache->raw_read (regnum, val);
617 }
618 
619 void
621 {
622  gdb_assert (regcache != NULL);
623  regcache->raw_write (regnum, val);
624 }
625 
626 template<typename T, typename>
627 void
629 {
630  gdb_byte *buf;
631 
633  buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
636  raw_write (regnum, buf);
637 }
638 
639 void
641  ULONGEST val)
642 {
643  gdb_assert (regcache != NULL);
644  regcache->raw_write (regnum, val);
645 }
646 
647 LONGEST
649 {
650  LONGEST value;
651  enum register_status status;
652 
654  if (status == REG_UNAVAILABLE)
656  _("Register %d is not available"), regnum);
657  return value;
658 }
659 
660 enum register_status
662 {
663  return regcache->cooked_read (regnum, buf);
664 }
665 
666 enum register_status
668 {
669  gdb_assert (regnum >= 0);
670  gdb_assert (regnum < m_descr->nr_cooked_registers);
671  if (regnum < num_raw_registers ())
672  return raw_read (regnum, buf);
673  else if (m_readonly_p
675  {
676  /* Read-only register cache, perhaps the cooked value was
677  cached? */
679  memcpy (buf, register_buffer (regnum),
681  else
682  memset (buf, 0, m_descr->sizeof_register[regnum]);
683 
685  }
687  {
688  struct value *mark, *computed;
689  enum register_status result = REG_VALID;
690 
691  mark = value_mark ();
692 
694  this, regnum);
696  memcpy (buf, value_contents_raw (computed),
698  else
699  {
700  memset (buf, 0, m_descr->sizeof_register[regnum]);
701  result = REG_UNAVAILABLE;
702  }
703 
704  value_free_to_mark (mark);
705 
706  return result;
707  }
708  else
710  regnum, buf);
711 }
712 
713 struct value *
715 {
717 }
718 
719 struct value *
721 {
722  gdb_assert (regnum >= 0);
723  gdb_assert (regnum < m_descr->nr_cooked_registers);
724 
725  if (regnum < num_raw_registers ()
728  {
729  struct value *result;
730 
732  VALUE_LVAL (result) = lval_register;
733  VALUE_REGNUM (result) = regnum;
734 
735  /* It is more efficient in general to do this delegation in this
736  direction than in the other one, even though the value-based
737  API is preferred. */
738  if (cooked_read (regnum,
739  value_contents_raw (result)) == REG_UNAVAILABLE)
740  mark_value_bytes_unavailable (result, 0,
741  TYPE_LENGTH (value_type (result)));
742 
743  return result;
744  }
745  else
747  this, regnum);
748 }
749 
750 enum register_status
752  LONGEST *val)
753 {
754  gdb_assert (regcache != NULL);
755  return regcache->cooked_read (regnum, val);
756 }
757 
758 template<typename T, typename>
759 enum register_status
761 {
762  enum register_status status;
763  gdb_byte *buf;
764 
765  gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
766  buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
767  status = cooked_read (regnum, buf);
768  if (status == REG_VALID)
769  *val = extract_integer<T> (buf, m_descr->sizeof_register[regnum],
771  else
772  *val = 0;
773  return status;
774 }
775 
776 enum register_status
778  ULONGEST *val)
779 {
780  gdb_assert (regcache != NULL);
781  return regcache->cooked_read (regnum, val);
782 }
783 
784 void
786  LONGEST val)
787 {
788  gdb_assert (regcache != NULL);
789  regcache->cooked_write (regnum, val);
790 }
791 
792 template<typename T, typename>
793 void
795 {
796  gdb_byte *buf;
797 
798  gdb_assert (regnum >=0 && regnum < m_descr->nr_cooked_registers);
799  buf = (gdb_byte *) alloca (m_descr->sizeof_register[regnum]);
802  cooked_write (regnum, buf);
803 }
804 
805 void
807  ULONGEST val)
808 {
809  gdb_assert (regcache != NULL);
810  regcache->cooked_write (regnum, val);
811 }
812 
813 /* See regcache.h. */
814 
815 void
817  const gdb_byte *buf)
818 {
820 }
821 
822 void
824 {
825  memcpy (register_buffer (regnum), buf,
828 }
829 
830 void
832  const gdb_byte *buf)
833 {
834  gdb_assert (regcache != NULL && buf != NULL);
835  regcache->raw_write (regnum, buf);
836 }
837 
838 void
840 {
841 
842  gdb_assert (buf != NULL);
845 
846  /* On the sparc, writing %g0 is a no-op, so we don't even want to
847  change the registers array if something writes to this register. */
849  return;
850 
851  /* If we have a valid copy of the register, and new value == old
852  value, then don't bother doing the actual store. */
854  && (memcmp (register_buffer (regnum), buf,
855  m_descr->sizeof_register[regnum]) == 0))
856  return;
857 
860 
861  /* Invalidate the register after it is written, in case of a
862  failure. */
863  regcache_invalidator invalidator (this, regnum);
864 
866 
867  /* The target did not throw an error so we can discard invalidating
868  the register. */
869  invalidator.release ();
870 }
871 
872 void
874  const gdb_byte *buf)
875 {
876  regcache->cooked_write (regnum, buf);
877 }
878 
879 void
881 {
882  gdb_assert (regnum >= 0);
883  gdb_assert (regnum < m_descr->nr_cooked_registers);
884  if (regnum < num_raw_registers ())
885  raw_write (regnum, buf);
886  else
888  regnum, buf);
889 }
890 
891 /* Perform a partial register transfer using a read, modify, write
892  operation. */
893 
894 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
895  void *buf);
896 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
897  const void *buf);
898 
899 enum register_status
900 regcache::xfer_part (int regnum, int offset, int len, void *in,
901  const void *out, bool is_raw)
902 {
903  struct gdbarch *gdbarch = arch ();
904  gdb_byte *reg = (gdb_byte *) alloca (register_size (gdbarch, regnum));
905 
906  gdb_assert (offset >= 0 && offset <= m_descr->sizeof_register[regnum]);
907  gdb_assert (len >= 0 && offset + len <= m_descr->sizeof_register[regnum]);
908  /* Something to do? */
909  if (offset + len == 0)
910  return REG_VALID;
911  /* Read (when needed) ... */
912  if (in != NULL
913  || offset > 0
914  || offset + len < m_descr->sizeof_register[regnum])
915  {
916  enum register_status status;
917 
918  if (is_raw)
919  status = raw_read (regnum, reg);
920  else
922  if (status != REG_VALID)
923  return status;
924  }
925  /* ... modify ... */
926  if (in != NULL)
927  memcpy (in, reg + offset, len);
928  if (out != NULL)
929  memcpy (reg + offset, out, len);
930  /* ... write (when needed). */
931  if (out != NULL)
932  {
933  if (is_raw)
934  raw_write (regnum, reg);
935  else
937  }
938 
939  return REG_VALID;
940 }
941 
942 enum register_status
944  int offset, int len, gdb_byte *buf)
945 {
946  return regcache->raw_read_part (regnum, offset, len, buf);
947 }
948 
949 enum register_status
951 {
953  return xfer_part (regnum, offset, len, buf, NULL, true);
954 }
955 
956 void
958  int offset, int len, const gdb_byte *buf)
959 {
960  regcache->raw_write_part (regnum, offset, len, buf);
961 }
962 
963 void
965  const gdb_byte *buf)
966 {
968  xfer_part (regnum, offset, len, NULL, buf, true);
969 }
970 
971 enum register_status
973  int offset, int len, gdb_byte *buf)
974 {
975  return regcache->cooked_read_part (regnum, offset, len, buf);
976 }
977 
978 
979 enum register_status
981 {
982  gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
983  return xfer_part (regnum, offset, len, buf, NULL, false);
984 }
985 
986 void
988  int offset, int len, const gdb_byte *buf)
989 {
990  regcache->cooked_write_part (regnum, offset, len, buf);
991 }
992 
993 void
995  const gdb_byte *buf)
996 {
997  gdb_assert (regnum >= 0 && regnum < m_descr->nr_cooked_registers);
998  xfer_part (regnum, offset, len, NULL, buf, false);
999 }
1000 
1001 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE. */
1002 
1003 void
1004 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1005 {
1006  gdb_assert (regcache != NULL);
1007  regcache->raw_supply (regnum, buf);
1008 }
1009 
1010 void
1011 regcache::raw_supply (int regnum, const void *buf)
1012 {
1013  void *regbuf;
1014  size_t size;
1015 
1018 
1019  regbuf = register_buffer (regnum);
1021 
1022  if (buf)
1023  {
1024  memcpy (regbuf, buf, size);
1026  }
1027  else
1028  {
1029  /* This memset not strictly necessary, but better than garbage
1030  in case the register value manages to escape somewhere (due
1031  to a bug, no less). */
1032  memset (regbuf, 0, size);
1034  }
1035 }
1036 
1037 /* Supply register REGNUM to REGCACHE. Value to supply is an integer stored at
1038  address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED. If
1039  the register size is greater than ADDR_LEN, then the integer will be sign or
1040  zero extended. If the register size is smaller than the integer, then the
1041  most significant bytes of the integer will be truncated. */
1042 
1043 void
1044 regcache::raw_supply_integer (int regnum, const gdb_byte *addr, int addr_len,
1045  bool is_signed)
1046 {
1047  enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1048  gdb_byte *regbuf;
1049  size_t regsize;
1050 
1053 
1054  regbuf = register_buffer (regnum);
1055  regsize = m_descr->sizeof_register[regnum];
1056 
1057  copy_integer_to_size (regbuf, regsize, addr, addr_len, is_signed,
1058  byte_order);
1060 }
1061 
1062 /* Supply register REGNUM with zeroed value to REGCACHE. This is not the same
1063  as calling raw_supply with NULL (which will set the state to
1064  unavailable). */
1065 
1066 void
1068 {
1069  void *regbuf;
1070  size_t size;
1071 
1074 
1075  regbuf = register_buffer (regnum);
1077 
1078  memset (regbuf, 0, size);
1080 }
1081 
1082 /* Collect register REGNUM from REGCACHE and store its contents in BUF. */
1083 
1084 void
1085 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1086 {
1087  gdb_assert (regcache != NULL && buf != NULL);
1088  regcache->raw_collect (regnum, buf);
1089 }
1090 
1091 void
1092 regcache::raw_collect (int regnum, void *buf) const
1093 {
1094  const void *regbuf;
1095  size_t size;
1096 
1097  gdb_assert (buf != NULL);
1099 
1100  regbuf = register_buffer (regnum);
1102  memcpy (buf, regbuf, size);
1103 }
1104 
1105 /* Transfer a single or all registers belonging to a certain register
1106  set to or from a buffer. This is the main worker function for
1107  regcache_supply_regset and regcache_collect_regset. */
1108 
1109 /* Collect register REGNUM from REGCACHE. Store collected value as an integer
1110  at address ADDR, in target endian, with length ADDR_LEN and sign IS_SIGNED.
1111  If ADDR_LEN is greater than the register size, then the integer will be sign
1112  or zero extended. If ADDR_LEN is smaller than the register size, then the
1113  most significant bytes of the integer will be truncated. */
1114 
1115 void
1117  bool is_signed) const
1118 {
1119  enum bfd_endian byte_order = gdbarch_byte_order (m_descr->gdbarch);
1120  const gdb_byte *regbuf;
1121  size_t regsize;
1122 
1124 
1125  regbuf = register_buffer (regnum);
1126  regsize = m_descr->sizeof_register[regnum];
1127 
1128  copy_integer_to_size (addr, addr_len, regbuf, regsize, is_signed,
1129  byte_order);
1130 }
1131 
1132 void
1134  struct regcache *out_regcache,
1135  int regnum, const void *in_buf,
1136  void *out_buf, size_t size) const
1137 {
1138  const struct regcache_map_entry *map;
1139  int offs = 0, count;
1140 
1141  for (map = (const struct regcache_map_entry *) regset->regmap;
1142  (count = map->count) != 0;
1143  map++)
1144  {
1145  int regno = map->regno;
1146  int slot_size = map->size;
1147 
1148  if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1149  slot_size = m_descr->sizeof_register[regno];
1150 
1151  if (regno == REGCACHE_MAP_SKIP
1152  || (regnum != -1
1153  && (regnum < regno || regnum >= regno + count)))
1154  offs += count * slot_size;
1155 
1156  else if (regnum == -1)
1157  for (; count--; regno++, offs += slot_size)
1158  {
1159  if (offs + slot_size > size)
1160  break;
1161 
1162  if (out_buf)
1163  raw_collect (regno, (gdb_byte *) out_buf + offs);
1164  else
1165  out_regcache->raw_supply (regno, in_buf
1166  ? (const gdb_byte *) in_buf + offs
1167  : NULL);
1168  }
1169  else
1170  {
1171  /* Transfer a single register and return. */
1172  offs += (regnum - regno) * slot_size;
1173  if (offs + slot_size > size)
1174  return;
1175 
1176  if (out_buf)
1177  raw_collect (regnum, (gdb_byte *) out_buf + offs);
1178  else
1179  out_regcache->raw_supply (regnum, in_buf
1180  ? (const gdb_byte *) in_buf + offs
1181  : NULL);
1182  return;
1183  }
1184  }
1185 }
1186 
1187 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1188  in REGSET. If REGNUM is -1, do this for all registers in REGSET.
1189  If BUF is NULL, set the register(s) to "unavailable" status. */
1190 
1191 void
1193  struct regcache *regcache,
1194  int regnum, const void *buf, size_t size)
1195 {
1197 }
1198 
1199 void
1201  int regnum, const void *buf, size_t size)
1202 {
1203  transfer_regset (regset, this, regnum, buf, NULL, size);
1204 }
1205 
1206 /* Collect register REGNUM from REGCACHE to BUF, using the register
1207  map in REGSET. If REGNUM is -1, do this for all registers in
1208  REGSET. */
1209 
1210 void
1212  const struct regcache *regcache,
1213  int regnum, void *buf, size_t size)
1214 {
1216 }
1217 
1218 void
1220  int regnum, void *buf, size_t size) const
1221 {
1222  transfer_regset (regset, NULL, regnum, NULL, buf, size);
1223 }
1224 
1225 
1226 /* Special handling for register PC. */
1227 
1228 CORE_ADDR
1230 {
1231  struct gdbarch *gdbarch = regcache->arch ();
1232 
1233  CORE_ADDR pc_val;
1234 
1235  if (gdbarch_read_pc_p (gdbarch))
1236  pc_val = gdbarch_read_pc (gdbarch, regcache);
1237  /* Else use per-frame method on get_current_frame. */
1238  else if (gdbarch_pc_regnum (gdbarch) >= 0)
1239  {
1240  ULONGEST raw_val;
1241 
1244  &raw_val) == REG_UNAVAILABLE)
1245  throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1246 
1247  pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1248  }
1249  else
1250  internal_error (__FILE__, __LINE__,
1251  _("regcache_read_pc: Unable to find PC"));
1252  return pc_val;
1253 }
1254 
1255 void
1257 {
1258  struct gdbarch *gdbarch = regcache->arch ();
1259 
1262  else if (gdbarch_pc_regnum (gdbarch) >= 0)
1264  gdbarch_pc_regnum (gdbarch), pc);
1265  else
1266  internal_error (__FILE__, __LINE__,
1267  _("regcache_write_pc: Unable to update PC"));
1268 
1269  /* Writing the PC (for instance, from "load") invalidates the
1270  current frame. */
1271  reinit_frame_cache ();
1272 }
1273 
1274 int
1276 {
1277  return gdbarch_num_regs (arch ());
1278 }
1279 
1280 void
1281 regcache::debug_print_register (const char *func, int regno)
1282 {
1283  struct gdbarch *gdbarch = arch ();
1284 
1286  if (regno >= 0 && regno < gdbarch_num_regs (gdbarch)
1287  && gdbarch_register_name (gdbarch, regno) != NULL
1288  && gdbarch_register_name (gdbarch, regno)[0] != '\0')
1289  fprintf_unfiltered (gdb_stdlog, "(%s)",
1290  gdbarch_register_name (gdbarch, regno));
1291  else
1292  fprintf_unfiltered (gdb_stdlog, "(%d)", regno);
1293  if (regno >= 0 && regno < gdbarch_num_regs (gdbarch))
1294  {
1295  enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
1296  int size = register_size (gdbarch, regno);
1297  gdb_byte *buf = register_buffer (regno);
1298 
1299  fprintf_unfiltered (gdb_stdlog, " = ");
1300  for (int i = 0; i < size; i++)
1301  {
1302  fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
1303  }
1304  if (size <= sizeof (LONGEST))
1305  {
1307 
1308  fprintf_unfiltered (gdb_stdlog, " %s %s",
1309  core_addr_to_string_nz (val), plongest (val));
1310  }
1311  }
1313 }
1314 
1315 static void
1316 reg_flush_command (const char *command, int from_tty)
1317 {
1318  /* Force-flush the register cache. */
1319  registers_changed ();
1320  if (from_tty)
1321  printf_filtered (_("Register cache flushed.\n"));
1322 }
1323 
1324 void
1325 regcache::dump (ui_file *file, enum regcache_dump_what what_to_dump)
1326 {
1327  struct gdbarch *gdbarch = m_descr->gdbarch;
1328  int regnum;
1329  int footnote_nr = 0;
1330  int footnote_register_offset = 0;
1331  int footnote_register_type_name_null = 0;
1332  long register_offset = 0;
1333 
1337 
1338  for (regnum = -1; regnum < m_descr->nr_cooked_registers; regnum++)
1339  {
1340  /* Name. */
1341  if (regnum < 0)
1342  fprintf_unfiltered (file, " %-10s", "Name");
1343  else
1344  {
1345  const char *p = gdbarch_register_name (gdbarch, regnum);
1346 
1347  if (p == NULL)
1348  p = "";
1349  else if (p[0] == '\0')
1350  p = "''";
1351  fprintf_unfiltered (file, " %-10s", p);
1352  }
1353 
1354  /* Number. */
1355  if (regnum < 0)
1356  fprintf_unfiltered (file, " %4s", "Nr");
1357  else
1358  fprintf_unfiltered (file, " %4d", regnum);
1359 
1360  /* Relative number. */
1361  if (regnum < 0)
1362  fprintf_unfiltered (file, " %4s", "Rel");
1363  else if (regnum < gdbarch_num_regs (gdbarch))
1364  fprintf_unfiltered (file, " %4d", regnum);
1365  else
1366  fprintf_unfiltered (file, " %4d",
1368 
1369  /* Offset. */
1370  if (regnum < 0)
1371  fprintf_unfiltered (file, " %6s ", "Offset");
1372  else
1373  {
1374  fprintf_unfiltered (file, " %6ld",
1376  if (register_offset != m_descr->register_offset[regnum]
1377  || (regnum > 0
1379  != (m_descr->register_offset[regnum - 1]
1380  + m_descr->sizeof_register[regnum - 1])))
1381  )
1382  {
1383  if (!footnote_register_offset)
1384  footnote_register_offset = ++footnote_nr;
1385  fprintf_unfiltered (file, "*%d", footnote_register_offset);
1386  }
1387  else
1388  fprintf_unfiltered (file, " ");
1389  register_offset = (m_descr->register_offset[regnum]
1391  }
1392 
1393  /* Size. */
1394  if (regnum < 0)
1395  fprintf_unfiltered (file, " %5s ", "Size");
1396  else
1397  fprintf_unfiltered (file, " %5ld", m_descr->sizeof_register[regnum]);
1398 
1399  /* Type. */
1400  {
1401  const char *t;
1402  std::string name_holder;
1403 
1404  if (regnum < 0)
1405  t = "Type";
1406  else
1407  {
1408  static const char blt[] = "builtin_type";
1409 
1410  t = TYPE_NAME (register_type (arch (), regnum));
1411  if (t == NULL)
1412  {
1413  if (!footnote_register_type_name_null)
1414  footnote_register_type_name_null = ++footnote_nr;
1415  name_holder = string_printf ("*%d",
1416  footnote_register_type_name_null);
1417  t = name_holder.c_str ();
1418  }
1419  /* Chop a leading builtin_type. */
1420  if (startswith (t, blt))
1421  t += strlen (blt);
1422  }
1423  fprintf_unfiltered (file, " %-15s", t);
1424  }
1425 
1426  /* Leading space always present. */
1427  fprintf_unfiltered (file, " ");
1428 
1429  /* Value, raw. */
1430  if (what_to_dump == regcache_dump_raw)
1431  {
1432  if (regnum < 0)
1433  fprintf_unfiltered (file, "Raw value");
1434  else if (regnum >= num_raw_registers ())
1435  fprintf_unfiltered (file, "<cooked>");
1436  else if (get_register_status (regnum) == REG_UNKNOWN)
1437  fprintf_unfiltered (file, "<invalid>");
1439  fprintf_unfiltered (file, "<unavailable>");
1440  else
1441  {
1442  raw_update (regnum);
1445  gdbarch_byte_order (gdbarch), true);
1446  }
1447  }
1448 
1449  /* Value, cooked. */
1450  if (what_to_dump == regcache_dump_cooked)
1451  {
1452  if (regnum < 0)
1453  fprintf_unfiltered (file, "Cooked value");
1454  else
1455  {
1456  const gdb_byte *buf = NULL;
1457  enum register_status status;
1458  struct value *value = NULL;
1459 
1460  if (regnum < num_raw_registers ())
1461  {
1462  raw_update (regnum);
1464  buf = register_buffer (regnum);
1465  }
1466  else
1467  {
1469 
1470  if (!value_optimized_out (value)
1472  {
1473  status = REG_VALID;
1474  buf = value_contents_all (value);
1475  }
1476  else
1478  }
1479 
1480  if (status == REG_UNKNOWN)
1481  fprintf_unfiltered (file, "<invalid>");
1482  else if (status == REG_UNAVAILABLE)
1483  fprintf_unfiltered (file, "<unavailable>");
1484  else
1485  print_hex_chars (file, buf,
1487  gdbarch_byte_order (gdbarch), true);
1488 
1489  if (value != NULL)
1490  {
1491  release_value (value);
1492  value_free (value);
1493  }
1494  }
1495  }
1496 
1497  /* Group members. */
1498  if (what_to_dump == regcache_dump_groups)
1499  {
1500  if (regnum < 0)
1501  fprintf_unfiltered (file, "Groups");
1502  else
1503  {
1504  const char *sep = "";
1505  struct reggroup *group;
1506 
1507  for (group = reggroup_next (gdbarch, NULL);
1508  group != NULL;
1509  group = reggroup_next (gdbarch, group))
1510  {
1512  {
1513  fprintf_unfiltered (file,
1514  "%s%s", sep, reggroup_name (group));
1515  sep = ",";
1516  }
1517  }
1518  }
1519  }
1520 
1521  /* Remote packet configuration. */
1522  if (what_to_dump == regcache_dump_remote)
1523  {
1524  if (regnum < 0)
1525  {
1526  fprintf_unfiltered (file, "Rmt Nr g/G Offset");
1527  }
1528  else if (regnum < num_raw_registers ())
1529  {
1530  int pnum, poffset;
1531 
1533  &pnum, &poffset))
1534  fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1535  }
1536  }
1537 
1538  fprintf_unfiltered (file, "\n");
1539  }
1540 
1541  if (footnote_register_offset)
1542  fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1543  footnote_register_offset);
1544  if (footnote_register_type_name_null)
1545  fprintf_unfiltered (file,
1546  "*%d: Register type's name NULL.\n",
1547  footnote_register_type_name_null);
1548 }
1549 
1550 static void
1551 regcache_print (const char *args, enum regcache_dump_what what_to_dump)
1552 {
1553  /* Where to send output. */
1554  stdio_file file;
1555  ui_file *out;
1556 
1557  if (args == NULL)
1558  out = gdb_stdout;
1559  else
1560  {
1561  if (!file.open (args, "w"))
1562  perror_with_name (_("maintenance print architecture"));
1563  out = &file;
1564  }
1565 
1567  get_current_regcache ()->dump (out, what_to_dump);
1568  else
1569  {
1570  /* For the benefit of "maint print registers" & co when
1571  debugging an executable, allow dumping a regcache even when
1572  there is no thread selected / no registers. */
1573  regcache dummy_regs (target_gdbarch ());
1574  dummy_regs.dump (out, what_to_dump);
1575  }
1576 }
1577 
1578 static void
1579 maintenance_print_registers (const char *args, int from_tty)
1580 {
1582 }
1583 
1584 static void
1585 maintenance_print_raw_registers (const char *args, int from_tty)
1586 {
1588 }
1589 
1590 static void
1591 maintenance_print_cooked_registers (const char *args, int from_tty)
1592 {
1594 }
1595 
1596 static void
1597 maintenance_print_register_groups (const char *args, int from_tty)
1598 {
1600 }
1601 
1602 static void
1603 maintenance_print_remote_registers (const char *args, int from_tty)
1604 {
1606 }
1607 
1608 #if GDB_SELF_TEST
1609 #include "selftest.h"
1610 #include "selftest-arch.h"
1611 #include "gdbthread.h"
1612 
1613 namespace selftests {
1614 
1615 class regcache_access : public regcache
1616 {
1617 public:
1618 
1619  /* Return the number of elements in current_regcache. */
1620 
1621  static size_t
1622  current_regcache_size ()
1623  {
1624  return std::distance (regcache::current_regcache.begin (),
1626  }
1627 };
1628 
1629 static void
1630 current_regcache_test (void)
1631 {
1632  /* It is empty at the start. */
1633  SELF_CHECK (regcache_access::current_regcache_size () == 0);
1634 
1635  ptid_t ptid1 (1), ptid2 (2), ptid3 (3);
1636 
1637  /* Get regcache from ptid1, a new regcache is added to
1638  current_regcache. */
1640  target_gdbarch (),
1641  NULL);
1642 
1643  SELF_CHECK (regcache != NULL);
1644  SELF_CHECK (regcache->ptid () == ptid1);
1645  SELF_CHECK (regcache_access::current_regcache_size () == 1);
1646 
1647  /* Get regcache from ptid2, a new regcache is added to
1648  current_regcache. */
1650  target_gdbarch (),
1651  NULL);
1652  SELF_CHECK (regcache != NULL);
1653  SELF_CHECK (regcache->ptid () == ptid2);
1654  SELF_CHECK (regcache_access::current_regcache_size () == 2);
1655 
1656  /* Get regcache from ptid3, a new regcache is added to
1657  current_regcache. */
1659  target_gdbarch (),
1660  NULL);
1661  SELF_CHECK (regcache != NULL);
1662  SELF_CHECK (regcache->ptid () == ptid3);
1663  SELF_CHECK (regcache_access::current_regcache_size () == 3);
1664 
1665  /* Get regcache from ptid2 again, nothing is added to
1666  current_regcache. */
1668  target_gdbarch (),
1669  NULL);
1670  SELF_CHECK (regcache != NULL);
1671  SELF_CHECK (regcache->ptid () == ptid2);
1672  SELF_CHECK (regcache_access::current_regcache_size () == 3);
1673 
1674  /* Mark ptid2 is changed, so regcache of ptid2 should be removed from
1675  current_regcache. */
1676  registers_changed_ptid (ptid2);
1677  SELF_CHECK (regcache_access::current_regcache_size () == 2);
1678 }
1679 
1680 static void test_target_fetch_registers (target_ops *self, regcache *regs,
1681  int regno);
1682 static void test_target_store_registers (target_ops *self, regcache *regs,
1683  int regno);
1684 static enum target_xfer_status
1685  test_target_xfer_partial (struct target_ops *ops,
1686  enum target_object object,
1687  const char *annex, gdb_byte *readbuf,
1688  const gdb_byte *writebuf,
1689  ULONGEST offset, ULONGEST len,
1690  ULONGEST *xfered_len);
1691 
1692 class target_ops_no_register : public test_target_ops
1693 {
1694 public:
1695  target_ops_no_register ()
1696  : test_target_ops {}
1697  {
1698  to_fetch_registers = test_target_fetch_registers;
1699  to_store_registers = test_target_store_registers;
1700  to_xfer_partial = test_target_xfer_partial;
1701 
1702  to_data = this;
1703  }
1704 
1705  void reset ()
1706  {
1707  fetch_registers_called = 0;
1708  store_registers_called = 0;
1709  xfer_partial_called = 0;
1710  }
1711 
1712  unsigned int fetch_registers_called = 0;
1713  unsigned int store_registers_called = 0;
1714  unsigned int xfer_partial_called = 0;
1715 };
1716 
1717 static void
1718 test_target_fetch_registers (target_ops *self, regcache *regs, int regno)
1719 {
1720  auto ops = static_cast<target_ops_no_register *> (self->to_data);
1721 
1722  /* Mark register available. */
1723  regs->raw_supply_zeroed (regno);
1724  ops->fetch_registers_called++;
1725 }
1726 
1727 static void
1728 test_target_store_registers (target_ops *self, regcache *regs, int regno)
1729 {
1730  auto ops = static_cast<target_ops_no_register *> (self->to_data);
1731 
1732  ops->store_registers_called++;
1733 }
1734 
1735 static enum target_xfer_status
1736 test_target_xfer_partial (struct target_ops *self, enum target_object object,
1737  const char *annex, gdb_byte *readbuf,
1738  const gdb_byte *writebuf,
1739  ULONGEST offset, ULONGEST len, ULONGEST *xfered_len)
1740 {
1741  auto ops = static_cast<target_ops_no_register *> (self->to_data);
1742 
1743  ops->xfer_partial_called++;
1744 
1745  *xfered_len = len;
1746  return TARGET_XFER_OK;
1747 }
1748 
1749 class readwrite_regcache : public regcache
1750 {
1751 public:
1752  readwrite_regcache (struct gdbarch *gdbarch)
1753  : regcache (gdbarch, nullptr, false)
1754  {}
1755 };
1756 
1757 /* Test regcache::cooked_read gets registers from raw registers and
1758  memory instead of target to_{fetch,store}_registers. */
1759 
1760 static void
1761 cooked_read_test (struct gdbarch *gdbarch)
1762 {
1763  /* Error out if debugging something, because we're going to push the
1764  test target, which would pop any existing target. */
1766  error (_("target already pushed"));
1767 
1768  /* Create a mock environment. An inferior with a thread, with a
1769  process_stratum target pushed. */
1770 
1771  target_ops_no_register mock_target;
1772  ptid_t mock_ptid (1, 1);
1773  inferior mock_inferior (mock_ptid.pid ());
1774  address_space mock_aspace {};
1775  mock_inferior.gdbarch = gdbarch;
1776  mock_inferior.aspace = &mock_aspace;
1777  thread_info mock_thread (&mock_inferior, mock_ptid);
1778 
1779  scoped_restore restore_thread_list
1780  = make_scoped_restore (&thread_list, &mock_thread);
1781 
1782  /* Add the mock inferior to the inferior list so that look ups by
1783  target+ptid can find it. */
1784  scoped_restore restore_inferior_list
1786  inferior_list = &mock_inferior;
1787 
1788  /* Switch to the mock inferior. */
1789  scoped_restore_current_inferior restore_current_inferior;
1790  set_current_inferior (&mock_inferior);
1791 
1792  /* Push the process_stratum target so we can mock accessing
1793  registers. */
1794  push_target (&mock_target);
1795 
1796  /* Pop it again on exit (return/exception). */
1797  struct on_exit
1798  {
1799  ~on_exit ()
1800  {
1802  }
1803  } pop_targets;
1804 
1805  /* Switch to the mock thread. */
1806  scoped_restore restore_inferior_ptid
1807  = make_scoped_restore (&inferior_ptid, mock_ptid);
1808 
1809  /* Test that read one raw register from regcache_no_target will go
1810  to the target layer. */
1811  int regnum;
1812 
1813  /* Find a raw register which size isn't zero. */
1814  for (regnum = 0; regnum < gdbarch_num_regs (gdbarch); regnum++)
1815  {
1816  if (register_size (gdbarch, regnum) != 0)
1817  break;
1818  }
1819 
1820  readwrite_regcache readwrite (gdbarch);
1822 
1823  readwrite.raw_read (regnum, buf.data ());
1824 
1825  /* raw_read calls target_fetch_registers. */
1826  SELF_CHECK (mock_target.fetch_registers_called > 0);
1827  mock_target.reset ();
1828 
1829  /* Mark all raw registers valid, so the following raw registers
1830  accesses won't go to target. */
1831  for (auto i = 0; i < gdbarch_num_regs (gdbarch); i++)
1832  readwrite.raw_update (i);
1833 
1834  mock_target.reset ();
1835  /* Then, read all raw and pseudo registers, and don't expect calling
1836  to_{fetch,store}_registers. */
1837  for (int regnum = 0;
1839  regnum++)
1840  {
1841  if (register_size (gdbarch, regnum) == 0)
1842  continue;
1843 
1845 
1846  SELF_CHECK (REG_VALID == readwrite.cooked_read (regnum, buf.data ()));
1847 
1848  if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_mt)
1849  {
1850  /* MT pseudo registers are banked, and different banks are
1851  selected by a raw registers, so GDB needs to write to
1852  that raw register to get different banked pseudo registers.
1853  See mt_select_coprocessor. */
1854  SELF_CHECK (mock_target.fetch_registers_called == 0);
1855  SELF_CHECK (mock_target.store_registers_called == 0);
1856  }
1857 
1858  /* Some SPU pseudo registers are got via TARGET_OBJECT_SPU. */
1859  if (gdbarch_bfd_arch_info (gdbarch)->arch != bfd_arch_spu)
1860  SELF_CHECK (mock_target.xfer_partial_called == 0);
1861 
1862  mock_target.reset ();
1863  }
1864 
1865  regcache readonly (regcache::readonly, readwrite);
1866 
1867  /* GDB may go to target layer to fetch all registers and memory for
1868  readonly regcache. */
1869  mock_target.reset ();
1870 
1871  for (int regnum = 0;
1873  regnum++)
1874  {
1875  if (gdbarch_bfd_arch_info (gdbarch)->arch == bfd_arch_mt)
1876  {
1877  /* Trigger an internal error otherwise. */
1878  continue;
1879  }
1880 
1881  if (register_size (gdbarch, regnum) == 0)
1882  continue;
1883 
1885  enum register_status status = readonly.cooked_read (regnum,
1886  buf.data ());
1887 
1889  {
1890  auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1891 
1892  if (bfd_arch == bfd_arch_frv || bfd_arch == bfd_arch_h8300
1893  || bfd_arch == bfd_arch_m32c || bfd_arch == bfd_arch_sh
1894  || bfd_arch == bfd_arch_alpha || bfd_arch == bfd_arch_v850
1895  || bfd_arch == bfd_arch_msp430 || bfd_arch == bfd_arch_mep
1896  || bfd_arch == bfd_arch_mips || bfd_arch == bfd_arch_v850_rh850
1897  || bfd_arch == bfd_arch_tic6x || bfd_arch == bfd_arch_mn10300
1898  || bfd_arch == bfd_arch_rl78 || bfd_arch == bfd_arch_score)
1899  {
1900  /* Raw registers. If raw registers are not in save_reggroup,
1901  their status are unknown. */
1904  else
1906  }
1907  else
1909  }
1910  else
1911  {
1914  else
1915  {
1916  /* If pseudo registers are not in save_reggroup, some of
1917  them can be computed from saved raw registers, but some
1918  of them are unknown. */
1919  auto bfd_arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1920 
1921  if (bfd_arch == bfd_arch_frv
1922  || bfd_arch == bfd_arch_m32c
1923  || bfd_arch == bfd_arch_mep
1924  || bfd_arch == bfd_arch_sh)
1926  else if (bfd_arch == bfd_arch_mips
1927  || bfd_arch == bfd_arch_h8300)
1929  else
1931  }
1932  }
1933 
1934  SELF_CHECK (mock_target.fetch_registers_called == 0);
1935  SELF_CHECK (mock_target.store_registers_called == 0);
1936  SELF_CHECK (mock_target.xfer_partial_called == 0);
1937 
1938  mock_target.reset ();
1939  }
1940 }
1941 
1942 } // namespace selftests
1943 #endif /* GDB_SELF_TEST */
1944 
1945 void
1947 {
1950 
1953 
1955  _("Force gdb to flush its register cache (maintainer command)"));
1956 
1958  _("Print the internal register configuration.\n"
1959  "Takes an optional file parameter."), &maintenanceprintlist);
1960  add_cmd ("raw-registers", class_maintenance,
1962  _("Print the internal register configuration "
1963  "including raw values.\n"
1964  "Takes an optional file parameter."), &maintenanceprintlist);
1965  add_cmd ("cooked-registers", class_maintenance,
1967  _("Print the internal register configuration "
1968  "including cooked values.\n"
1969  "Takes an optional file parameter."), &maintenanceprintlist);
1970  add_cmd ("register-groups", class_maintenance,
1972  _("Print the internal register configuration "
1973  "including each register's group.\n"
1974  "Takes an optional file parameter."),
1976  add_cmd ("remote-registers", class_maintenance,
1978 Print the internal register configuration including each register's\n\
1979 remote register number and buffer offset in the g/G packets.\n\
1980 Takes an optional file parameter."),
1982 
1983 #if GDB_SELF_TEST
1984  selftests::register_test ("current_regcache", selftests::current_regcache_test);
1985 
1986  selftests::register_test_foreach_arch ("regcache::cooked_read_test",
1987  selftests::cooked_read_test);
1988 #endif
1989 }
struct gdbarch * target_gdbarch(void)
Definition: gdbarch.c:5467
static void maintenance_print_raw_registers(const char *args, int from_tty)
Definition: regcache.c:1585
const char * string
Definition: signals.c:50
void reset()
struct value * value_mark(void)
Definition: value.c:1589
#define target_has_registers
Definition: target.h:1740
int gdbarch_pseudo_register_read_value_p(struct gdbarch *gdbarch)
Definition: gdbarch.c:1991
static void maintenance_print_registers(const char *args, int from_tty)
Definition: regcache.c:1579
enum register_status cooked_read(int regnum, gdb_byte *buf)
Definition: regcache.c:667
int num_raw_registers() const
Definition: regcache.c:1275
regcache_dump_what
Definition: regcache.h:215
enum register_status raw_read(int regnum, gdb_byte *buf)
Definition: regcache.c:571
static void regcache_observer_target_changed(struct target_ops *target)
Definition: regcache.c:462
static struct regcache_descr * regcache_descr(struct gdbarch *gdbarch)
Definition: regcache.c:142
bfd_vma CORE_ADDR
Definition: common-types.h:41
void cooked_write_part(int regnum, int offset, int len, const gdb_byte *buf)
Definition: regcache.c:994
void gdbarch_write_pc(struct gdbarch *gdbarch, struct regcache *regcache, CORE_ADDR val)
Definition: gdbarch.c:1933
long sizeof_raw_registers
Definition: regcache.c:54
struct regcache * get_thread_regcache(ptid_t ptid)
Definition: regcache.c:434
#define GDBARCH_OBSTACK_CALLOC(GDBARCH, NR, TYPE)
Definition: gdbarch.h:1708
struct value * cooked_read_value(int regnum)
Definition: regcache.c:720
static void regcache_thread_ptid_changed(ptid_t old_ptid, ptid_t new_ptid)
Definition: regcache.c:470
CORE_ADDR regcache_read_pc(struct regcache *regcache)
Definition: regcache.c:1229
void(* func)(char *)
void supply_regset(const struct regset *regset, int regnum, const void *buf, size_t size)
Definition: regcache.c:1200
regcache(gdbarch *gdbarch)
Definition: regcache.h:235
struct type ** register_type
Definition: regcache.c:73
#define TYPE_NAME(thistype)
Definition: gdbtypes.h:1224
regcache_invalidator(struct regcache *regcache, int regnum)
Definition: regcache.c:242
void push_target(struct target_ops *t)
Definition: target.c:642
void * gdbarch_data(struct gdbarch *gdbarch, struct gdbarch_data *data)
Definition: gdbarch.c:5169
void regcache_cooked_write_signed(struct regcache *regcache, int regnum, LONGEST val)
Definition: regcache.c:785
std::vector< T, gdb::default_init_allocator< T > > def_vector
Definition: def-vector.h:32
void mark_value_bytes_unavailable(struct value *value, LONGEST offset, LONGEST length)
Definition: value.c:601
ptid_t regcache_get_ptid(const struct regcache *regcache)
Definition: regcache.c:229
void raw_write(int regnum, const gdb_byte *buf)
Definition: regcache.c:839
void * memset(T *s, int c, size_t n)=delete
void internal_error(const char *file, int line, const char *fmt,...)
Definition: errors.c:50
void regcache_supply_regset(const struct regset *regset, struct regcache *regcache, int regnum, const void *buf, size_t size)
Definition: regcache.c:1192
struct address_space * target_thread_address_space(ptid_t ptid)
Definition: target.c:2689
void value_free(struct value *val)
Definition: value.c:1608
struct cmd_list_element * add_cmd(const char *name, enum command_class theclass, const char *doc, struct cmd_list_element **list)
Definition: cli-decode.c:262
void regcache_cooked_write_part(struct regcache *regcache, int regnum, int offset, int len, const gdb_byte *buf)
Definition: regcache.c:987
enum register_status raw_read_part(int regnum, int offset, int len, gdb_byte *buf)
Definition: regcache.c:950
int regcache_register_size(const struct regcache *regcache, int n)
Definition: regcache.c:179
void save(regcache_cooked_read_ftype *cooked_read, void *src)
Definition: regcache.c:283
gdb_byte * register_buffer(int regnum) const
Definition: regcache.c:270
register_status
void copy_integer_to_size(gdb_byte *dest, int dest_size, const gdb_byte *source, int source_size, bool is_signed, enum bfd_endian byte_order)
Definition: findvar.c:224
int gdbarch_write_pc_p(struct gdbarch *gdbarch)
Definition: gdbarch.c:1926
struct reggroup *const restore_reggroup
Definition: reggroups.c:320
int gdbarch_num_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:2039
void target_fetch_registers(struct regcache *regcache, int regno)
Definition: target.c:3437
void assert_regnum(int regnum) const
Definition: regcache.c:393
#define _(String)
Definition: gdb_locale.h:35
static void regcache_print(const char *args, enum regcache_dump_what what_to_dump)
Definition: regcache.c:1551
void register_test_foreach_arch(const std::string &name, self_test_foreach_arch_function *function)
void regcache_save(struct regcache *regcache, regcache_cooked_read_ftype *cooked_read, void *src)
Definition: regcache.c:276
void regcache_write_pc(struct regcache *regcache, CORE_ADDR pc)
Definition: regcache.c:1256
void set_ptid(const ptid_t ptid)
Definition: regcache.h:330
#define VALUE_LVAL(val)
Definition: value.h:414
struct value * allocate_value(struct type *type)
Definition: value.c:1036
int count
Definition: regcache.h:159
struct regcache * get_current_regcache(void)
Definition: regcache.c:446
static constexpr readonly_t readonly
Definition: regcache.h:240
void printf_filtered(const char *format,...)
Definition: utils.c:2045
#define GDBARCH_OBSTACK_ZALLOC(GDBARCH, TYPE)
Definition: gdbarch.h:1709
void raw_supply_integer(int regnum, const gdb_byte *addr, int addr_len, bool is_signed)
Definition: regcache.c:1044
struct regcache * regcache_dup(struct regcache *src)
Definition: regcache.c:353
struct regcache_descr * m_descr
Definition: regcache.h:363
void regcache_raw_write_signed(struct regcache *regcache, int regnum, LONGEST val)
Definition: regcache.c:620
const bool m_readonly_p
Definition: regcache.h:381
enum register_status xfer_part(int regnum, int offset, int len, void *in, const void *out, bool is_raw)
Definition: regcache.c:900
int gdbarch_read_pc_p(struct gdbarch *gdbarch)
Definition: gdbarch.c:1902
scoped_restore_tmpl< T > make_scoped_restore(T *var)
Definition: regset.h:34
int gdbarch_num_pseudo_regs(struct gdbarch *gdbarch)
Definition: gdbarch.c:2057
#define XCNEWVEC(T, N)
Definition: poison.h:157
void restore(struct regcache *src)
Definition: regcache.c:318
struct observer * observer_attach_target_changed(observer_target_changed_ftype *f)
void _initialize_regcache(void)
Definition: regcache.c:1946
LONGEST regcache_raw_get_signed(struct regcache *regcache, int regnum)
Definition: regcache.c:648
CORE_ADDR gdbarch_read_pc(struct gdbarch *gdbarch, struct regcache *regcache)
Definition: gdbarch.c:1909
void raw_collect(int regnum, void *buf) const
Definition: regcache.c:1092
struct value::@186::@188 computed
enum register_status regcache_raw_read_part(struct regcache *regcache, int regnum, int offset, int len, gdb_byte *buf)
Definition: regcache.c:943
#define target_prepare_to_store(regcache)
Definition: target.h:1398
void raw_supply(int regnum, const void *buf)
Definition: regcache.c:1011
struct target_ops current_target
CORE_ADDR gdbarch_addr_bits_remove(struct gdbarch *gdbarch, CORE_ADDR addr)
Definition: gdbarch.c:3208
static void * init_regcache_descr(struct gdbarch *gdbarch)
Definition: regcache.c:77
void raw_write_part(int regnum, int offset, int len, const gdb_byte *buf)
Definition: regcache.c:964
static ULONGEST extract_unsigned_integer(const gdb_byte *addr, int len, enum bfd_endian byte_order)
Definition: defs.h:577
enum register_status regcache_cooked_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition: regcache.c:777
void print_hex_chars(struct ui_file *stream, const gdb_byte *valaddr, unsigned len, enum bfd_endian byte_order, bool zero_pad)
Definition: valprint.c:1784
Definition: ptid.h:35
struct type * register_type(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:152
void fprintf_unfiltered(struct ui_file *stream, const char *format,...)
Definition: utils.c:2018
int gdbarch_cannot_store_register(struct gdbarch *gdbarch, int regnum)
Definition: gdbarch.c:2538
static void maintenance_print_cooked_registers(const char *args, int from_tty)
Definition: regcache.c:1591
void regcache_raw_update(struct regcache *regcache, int regnum)
Definition: regcache.c:535
const gdb_byte * value_contents_all(struct value *value)
Definition: value.c:1265
long sizeof_cooked_registers
Definition: regcache.c:63
int size
Definition: regcache.h:161
enum bfd_endian gdbarch_byte_order(struct gdbarch *gdbarch)
Definition: gdbarch.c:1509
target_xfer_status
Definition: target.h:206
void transfer_regset(const struct regset *regset, struct regcache *out_regcache, int regnum, const void *in_buf, void *out_buf, size_t size) const
Definition: regcache.c:1133
void raw_collect_integer(int regnum, gdb_byte *addr, int addr_len, bool is_signed) const
Definition: regcache.c:1116
Definition: gdbtypes.h:749
#define VALUE_REGNUM(val)
Definition: value.h:451
void regcache_collect_regset(const struct regset *regset, const struct regcache *regcache, int regnum, void *buf, size_t size)
Definition: regcache.c:1211
struct inferior * inferior_list
Definition: inferior.c:45
struct gdbarch * gdbarch
Definition: regcache.c:47
void regcache_cpy(struct regcache *dst, struct regcache *src)
Definition: regcache.c:342
void raw_set_cached_value(int regnum, const gdb_byte *buf)
Definition: regcache.c:823
const char * gdbarch_register_name(struct gdbarch *gdbarch, int regnr)
Definition: gdbarch.c:2282
mach_port_t mach_port_t name mach_port_t mach_port_t name kern_return_t int status
Definition: gnu-nat.c:1822
static int startswith(const char *string, const char *pattern)
Definition: common-utils.h:107
enum register_status regcache_raw_read_unsigned(struct regcache *regcache, int regnum, ULONGEST *val)
Definition: regcache.c:612
long * sizeof_register
Definition: regcache.c:70
void raw_supply_zeroed(int regnum)
Definition: regcache.c:1067
int regnum
Definition: aarch64-tdep.c:77
static enum register_status do_cooked_read(void *src, int regnum, gdb_byte *buf)
Definition: regcache.c:206
void collect_regset(const struct regset *regset, int regnum, void *buf, size_t size) const
Definition: regcache.c:1219
struct value * gdbarch_pseudo_register_read_value(struct gdbarch *gdbarch, struct regcache *regcache, int cookednum)
Definition: gdbarch.c:1998
struct observer * observer_attach_thread_ptid_changed(observer_thread_ptid_changed_ftype *f)
struct cmd_list_element * maintenanceprintlist
Definition: cli-cmds.c:143
target_object
Definition: target.h:132
const address_space * aspace() const
Definition: regcache.h:257
static std::forward_list< regcache * > current_regcache
Definition: regcache.h:345
Definition: regdef.h:22
struct type * gdbarch_register_type(struct gdbarch *gdbarch, int reg_nr)
Definition: gdbarch.c:2306
#define gdb_assert(expr)
Definition: gdb_assert.h:32
Definition: value.c:169
static void reg_flush_command(const char *command, int from_tty)
Definition: regcache.c:1316
void regcache_invalidate(struct regcache *regcache, int regnum)
Definition: regcache.c:378
enum register_status get_register_status(int regnum) const
Definition: regcache.c:366
void regcache_raw_write_part(struct regcache *regcache, int regnum, int offset, int len, const gdb_byte *buf)
Definition: regcache.c:957
bfd_byte gdb_byte
Definition: common-types.h:38
void store_integer(gdb_byte *addr, int len, enum bfd_endian byte_order, T val)
Definition: findvar.c:168
int value_entirely_available(struct value *value)
Definition: value.c:374
void regcache_raw_set_cached_value(struct regcache *regcache, int regnum, const gdb_byte *buf)
Definition: regcache.c:816
int value_optimized_out(struct value *value)
Definition: value.c:1424
ptid_t null_ptid
Definition: ptid.c:25
static void maintenance_print_register_groups(const char *args, int from_tty)
Definition: regcache.c:1597
void void void void void void void void void perror_with_name(const char *string) ATTRIBUTE_NORETURN
Definition: utils.c:692
int gdbarch_register_reggroup_p(struct gdbarch *gdbarch, int regnum, struct reggroup *reggroup)
Definition: gdbarch.c:3589
struct value * regcache_cooked_read_value(struct regcache *regcache, int regnum)
Definition: regcache.c:714
enum register_status regcache_register_status(const struct regcache *regcache, int regnum)
Definition: regcache.c:359
enum register_status gdbarch_pseudo_register_read(struct gdbarch *gdbarch, struct regcache *regcache, int cookednum, gdb_byte *buf)
Definition: gdbarch.c:1974
void registers_changed_ptid(ptid_t ptid)
Definition: regcache.c:491
enum register_status regcache_raw_read(struct regcache *regcache, int regnum, gdb_byte *buf)
Definition: regcache.c:565
void regcache_cooked_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:806
void debug_print_register(const char *func, int regno)
Definition: regcache.c:1281
bool open(const char *name, const char *mode)
Definition: ui-file.c:171
ptid_t inferior_ptid
Definition: infcmd.c:94
enum strata to_stratum
Definition: target.h:656
struct regcache * m_regcache
Definition: regcache.c:263
int offset
Definition: agent.c:65
void registers_changed(void)
Definition: regcache.c:522
void regcache_raw_write_unsigned(struct regcache *regcache, int regnum, ULONGEST val)
Definition: regcache.c:640
enum register_status cooked_read_part(int regnum, int offset, int len, gdb_byte *buf)
Definition: regcache.c:980
gdb_byte * m_registers
Definition: regcache.h:372
gdbarch * arch() const
Definition: regcache.c:221
enum register_status regcache_cooked_read(struct regcache *regcache, int regnum, gdb_byte *buf)
Definition: regcache.c:661
struct regcache * get_thread_arch_aspace_regcache(ptid_t ptid, struct gdbarch *gdbarch, struct address_space *aspace)
Definition: regcache.c:407
void cooked_write(int regnum, const gdb_byte *buf)
Definition: regcache.c:880
enum register_status() regcache_cooked_read_ftype(void *src, int regnum, gdb_byte *buf)
std::string string_printf(const char *fmt,...)
Definition: common-utils.c:150
void value_free_to_mark(const struct value *mark)
Definition: value.c:1641
static struct gdbarch * current_thread_arch
Definition: regcache.c:431
struct thread_info * thread_list
Definition: thread.c:53
void gdbarch_pseudo_register_write(struct gdbarch *gdbarch, struct regcache *regcache, int cookednum, const gdb_byte *buf)
Definition: gdbarch.c:2022
void dump(ui_file *file, enum regcache_dump_what what_to_dump)
Definition: regcache.c:1325
int ptid_equal(const ptid_t &ptid1, const ptid_t &ptid2)
Definition: ptid.c:71
void regcache_raw_supply(struct regcache *regcache, int regnum, const void *buf)
Definition: regcache.c:1004
int ptid_match(const ptid_t &ptid, const ptid_t &filter)
Definition: ptid.c:103
const void * regmap
Definition: regset.h:39
unsigned long long ULONGEST
Definition: common-types.h:53
void target_store_registers(struct regcache *regcache, int regno)
Definition: target.c:3445
void release_value(struct value *val)
Definition: value.c:1693
int register_size(struct gdbarch *gdbarch, int regnum)
Definition: regcache.c:164
#define gdb_stdlog
Definition: utils.h:349
struct type * value_type(const struct value *value)
Definition: value.c:1095
enum register_status regcache_cooked_read_part(struct regcache *regcache, int regnum, int offset, int len, gdb_byte *buf)
Definition: regcache.c:972
const struct bfd_arch_info * gdbarch_bfd_arch_info(struct gdbarch *gdbarch)
Definition: gdbarch.c:1500
void regcache_raw_collect(const struct regcache *regcache, int regnum, void *buf)
Definition: regcache.c:1085
int gdbarch_pc_regnum(struct gdbarch *gdbarch)
Definition: gdbarch.c:2163
DISABLE_COPY_AND_ASSIGN(regcache_invalidator)
int remote_register_number_and_offset(struct gdbarch *gdbarch, int regnum, int *pnum, int *poffset)
Definition: remote.c:794
int regno
Definition: regcache.h:160
gdb_byte * value_contents_raw(struct value *value)
Definition: value.c:1158
long * register_offset
Definition: regcache.c:69
struct reggroup * reggroup_next(struct gdbarch *gdbarch, struct reggroup *last)
Definition: reggroups.c:133
struct reggroup *const save_reggroup
Definition: reggroups.c:319
#define TYPE_LENGTH(thistype)
Definition: gdbtypes.h:1235
void raw_update(int regnum)
Definition: regcache.c:543
static ptid_t current_thread_ptid
Definition: regcache.c:430
void reinit_frame_cache(void)
Definition: frame.c:1809
ptid_t m_ptid
Definition: regcache.h:384
ptid_t ptid() const
Definition: regcache.h:325
struct cmd_list_element * add_com(const char *name, enum command_class theclass, cmd_const_cfunc_ftype *fun, const char *doc)
Definition: cli-decode.c:902
void() regcache_write_ftype(struct regcache *regcache, int regnum, const void *buf)
Definition: regcache.c:896
struct regcache * get_thread_regcache_for_ptid(ptid_t ptid)
Definition: regcache.c:454
#define target_thread_architecture(ptid)
Definition: target.h:1844
signed char * m_register_status
Definition: regcache.h:374
void() regcache_read_ftype(struct regcache *regcache, int regnum, void *buf)
Definition: regcache.c:894
enum bfd_endian byte_order
Definition: gdbarch.c:137
void invalidate(int regnum)
Definition: regcache.c:385
void register_test(const std::string &name, selftest *test)
Definition: selftest.c:52
void pop_all_targets_at_and_above(enum strata stratum)
Definition: target.c:752
void error(const char *fmt,...)
Definition: errors.c:38
size_t size
Definition: go32-nat.c:242
struct gdbarch_data * gdbarch_data_register_post_init(gdbarch_data_post_init_ftype *post_init)
Definition: gdbarch.c:5136
ptid_t minus_one_ptid
Definition: ptid.c:26
enum register_status regcache_cooked_read_signed(struct regcache *regcache, int regnum, LONGEST *val)
Definition: regcache.c:751
void set_current_inferior(struct inferior *inf)
Definition: inferior.c:64
void throw_error(enum errors error, const char *fmt,...)
long long LONGEST
Definition: common-types.h:52
const char * reggroup_name(struct reggroup *group)
Definition: reggroups.c:66
static void maintenance_print_remote_registers(const char *args, int from_tty)
Definition: regcache.c:1603
Definition: regcache.h:157
int nr_cooked_registers
Definition: regcache.c:62
void regcache_cooked_write(struct regcache *regcache, int regnum, const gdb_byte *buf)
Definition: regcache.c:873
struct regcache * get_thread_arch_regcache(ptid_t ptid, struct gdbarch *gdbarch)
Definition: regcache.c:423
#define SELF_CHECK(VALUE)
Definition: selftest.h:67
#define gdb_stdout
Definition: utils.h:340
void regcache_raw_write(struct regcache *regcache, int regnum, const gdb_byte *buf)
Definition: regcache.c:831
struct gdbarch_data * regcache_descr_handle
Definition: regcache.c:42
enum register_status regcache_raw_read_signed(struct regcache *regcache, int regnum, LONGEST *val)
Definition: regcache.c:586