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