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