506067fefacfb23ab702a535819a063d39b4f415
[platform/upstream/binutils.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3    Copyright (C) 1986-2014 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 <string.h>
28 #include "observer.h"
29 #include "exceptions.h"
30 #include "remote.h"
31 #include "valprint.h"
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 gdbarch_data (gdbarch, regcache_descr_handle);
152 }
153
154 /* Utility functions returning useful register attributes stored in
155    the regcache descr.  */
156
157 struct type *
158 register_type (struct gdbarch *gdbarch, int regnum)
159 {
160   struct regcache_descr *descr = regcache_descr (gdbarch);
161
162   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
163   return descr->register_type[regnum];
164 }
165
166 /* Utility functions returning useful register attributes stored in
167    the regcache descr.  */
168
169 int
170 register_size (struct gdbarch *gdbarch, int regnum)
171 {
172   struct regcache_descr *descr = regcache_descr (gdbarch);
173   int size;
174
175   gdb_assert (regnum >= 0
176               && regnum < (gdbarch_num_regs (gdbarch)
177                            + gdbarch_num_pseudo_regs (gdbarch)));
178   size = descr->sizeof_register[regnum];
179   return size;
180 }
181
182 /* The register cache for storing raw register values.  */
183
184 struct regcache
185 {
186   struct regcache_descr *descr;
187
188   /* The address space of this register cache (for registers where it
189      makes sense, like PC or SP).  */
190   struct address_space *aspace;
191
192   /* The register buffers.  A read-only register cache can hold the
193      full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
194      register cache can only hold [0 .. gdbarch_num_regs).  */
195   gdb_byte *registers;
196   /* Register cache status.  */
197   signed char *register_status;
198   /* Is this a read-only cache?  A read-only cache is used for saving
199      the target's register state (e.g, across an inferior function
200      call or just before forcing a function return).  A read-only
201      cache can only be updated via the methods regcache_dup() and
202      regcache_cpy().  The actual contents are determined by the
203      reggroup_save and reggroup_restore methods.  */
204   int readonly_p;
205   /* If this is a read-write cache, which thread's registers is
206      it connected to?  */
207   ptid_t ptid;
208 };
209
210 static struct regcache *
211 regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
212                     int readonly_p)
213 {
214   struct regcache_descr *descr;
215   struct regcache *regcache;
216
217   gdb_assert (gdbarch != NULL);
218   descr = regcache_descr (gdbarch);
219   regcache = XNEW (struct regcache);
220   regcache->descr = descr;
221   regcache->readonly_p = readonly_p;
222   if (readonly_p)
223     {
224       regcache->registers
225         = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
226       regcache->register_status
227         = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
228     }
229   else
230     {
231       regcache->registers
232         = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
233       regcache->register_status
234         = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
235     }
236   regcache->aspace = aspace;
237   regcache->ptid = minus_one_ptid;
238   return regcache;
239 }
240
241 struct regcache *
242 regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
243 {
244   return regcache_xmalloc_1 (gdbarch, aspace, 1);
245 }
246
247 void
248 regcache_xfree (struct regcache *regcache)
249 {
250   if (regcache == NULL)
251     return;
252   xfree (regcache->registers);
253   xfree (regcache->register_status);
254   xfree (regcache);
255 }
256
257 static void
258 do_regcache_xfree (void *data)
259 {
260   regcache_xfree (data);
261 }
262
263 struct cleanup *
264 make_cleanup_regcache_xfree (struct regcache *regcache)
265 {
266   return make_cleanup (do_regcache_xfree, regcache);
267 }
268
269 /* Cleanup routines for invalidating a register.  */
270
271 struct register_to_invalidate
272 {
273   struct regcache *regcache;
274   int regnum;
275 };
276
277 static void
278 do_regcache_invalidate (void *data)
279 {
280   struct register_to_invalidate *reg = data;
281
282   regcache_invalidate (reg->regcache, reg->regnum);
283 }
284
285 static struct cleanup *
286 make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
287 {
288   struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);
289
290   reg->regcache = regcache;
291   reg->regnum = regnum;
292   return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
293 }
294
295 /* Return REGCACHE's architecture.  */
296
297 struct gdbarch *
298 get_regcache_arch (const struct regcache *regcache)
299 {
300   return regcache->descr->gdbarch;
301 }
302
303 struct address_space *
304 get_regcache_aspace (const struct regcache *regcache)
305 {
306   return regcache->aspace;
307 }
308
309 /* Return  a pointer to register REGNUM's buffer cache.  */
310
311 static gdb_byte *
312 register_buffer (const struct regcache *regcache, int regnum)
313 {
314   return regcache->registers + regcache->descr->register_offset[regnum];
315 }
316
317 void
318 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
319                void *src)
320 {
321   struct gdbarch *gdbarch = dst->descr->gdbarch;
322   gdb_byte buf[MAX_REGISTER_SIZE];
323   int regnum;
324
325   /* The DST should be `read-only', if it wasn't then the save would
326      end up trying to write the register values back out to the
327      target.  */
328   gdb_assert (dst->readonly_p);
329   /* Clear the dest.  */
330   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
331   memset (dst->register_status, 0,
332           dst->descr->sizeof_cooked_register_status);
333   /* Copy over any registers (identified by their membership in the
334      save_reggroup) and mark them as valid.  The full [0 .. gdbarch_num_regs +
335      gdbarch_num_pseudo_regs) range is checked since some architectures need
336      to save/restore `cooked' registers that live in memory.  */
337   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
338     {
339       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
340         {
341           enum register_status status = cooked_read (src, regnum, buf);
342
343           if (status == REG_VALID)
344             memcpy (register_buffer (dst, regnum), buf,
345                     register_size (gdbarch, regnum));
346           else
347             {
348               gdb_assert (status != REG_UNKNOWN);
349
350               memset (register_buffer (dst, regnum), 0,
351                       register_size (gdbarch, regnum));
352             }
353           dst->register_status[regnum] = status;
354         }
355     }
356 }
357
358 static void
359 regcache_restore (struct regcache *dst,
360                   regcache_cooked_read_ftype *cooked_read,
361                   void *cooked_read_context)
362 {
363   struct gdbarch *gdbarch = dst->descr->gdbarch;
364   gdb_byte buf[MAX_REGISTER_SIZE];
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 (!dst->readonly_p);
370   /* Copy over any registers, being careful to only restore those that
371      were both saved and need to be restored.  The full [0 .. gdbarch_num_regs
372      + gdbarch_num_pseudo_regs) range is checked since some architectures need
373      to save/restore `cooked' registers that live in memory.  */
374   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
375     {
376       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
377         {
378           enum register_status status;
379
380           status = cooked_read (cooked_read_context, regnum, buf);
381           if (status == REG_VALID)
382             regcache_cooked_write (dst, regnum, buf);
383         }
384     }
385 }
386
387 static enum register_status
388 do_cooked_read (void *src, int regnum, gdb_byte *buf)
389 {
390   struct regcache *regcache = src;
391
392   return regcache_cooked_read (regcache, regnum, buf);
393 }
394
395 void
396 regcache_cpy (struct regcache *dst, struct regcache *src)
397 {
398   gdb_assert (src != NULL && dst != NULL);
399   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
400   gdb_assert (src != dst);
401   gdb_assert (src->readonly_p || dst->readonly_p);
402
403   if (!src->readonly_p)
404     regcache_save (dst, do_cooked_read, src);
405   else if (!dst->readonly_p)
406     regcache_restore (dst, do_cooked_read, src);
407   else
408     regcache_cpy_no_passthrough (dst, src);
409 }
410
411 void
412 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
413 {
414   gdb_assert (src != NULL && dst != NULL);
415   gdb_assert (src->descr->gdbarch == dst->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 (dst->readonly_p && src->readonly_p);
421
422   memcpy (dst->registers, src->registers,
423           dst->descr->sizeof_cooked_registers);
424   memcpy (dst->register_status, src->register_status,
425           dst->descr->sizeof_cooked_register_status);
426 }
427
428 struct regcache *
429 regcache_dup (struct regcache *src)
430 {
431   struct regcache *newbuf;
432
433   newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
434   regcache_cpy (newbuf, src);
435   return newbuf;
436 }
437
438 enum register_status
439 regcache_register_status (const struct regcache *regcache, int regnum)
440 {
441   gdb_assert (regcache != NULL);
442   gdb_assert (regnum >= 0);
443   if (regcache->readonly_p)
444     gdb_assert (regnum < regcache->descr->nr_cooked_registers);
445   else
446     gdb_assert (regnum < regcache->descr->nr_raw_registers);
447
448   return regcache->register_status[regnum];
449 }
450
451 void
452 regcache_invalidate (struct regcache *regcache, int regnum)
453 {
454   gdb_assert (regcache != NULL);
455   gdb_assert (regnum >= 0);
456   gdb_assert (!regcache->readonly_p);
457   gdb_assert (regnum < regcache->descr->nr_raw_registers);
458   regcache->register_status[regnum] = REG_UNKNOWN;
459 }
460
461
462 /* Global structure containing the current regcache.  */
463
464 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
465    recording if the register values have been changed (eg. by the
466    user).  Therefore all registers must be written back to the
467    target when appropriate.  */
468
469 struct regcache_list
470 {
471   struct regcache *regcache;
472   struct regcache_list *next;
473 };
474
475 static struct regcache_list *current_regcache;
476
477 struct regcache *
478 get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
479                                  struct address_space *aspace)
480 {
481   struct regcache_list *list;
482   struct regcache *new_regcache;
483
484   for (list = current_regcache; list; list = list->next)
485     if (ptid_equal (list->regcache->ptid, ptid)
486         && get_regcache_arch (list->regcache) == gdbarch)
487       return list->regcache;
488
489   new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
490   new_regcache->ptid = ptid;
491
492   list = xmalloc (sizeof (struct regcache_list));
493   list->regcache = new_regcache;
494   list->next = current_regcache;
495   current_regcache = list;
496
497   return new_regcache;
498 }
499
500 struct regcache *
501 get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
502 {
503   struct address_space *aspace;
504
505   /* For the benefit of "maint print registers" & co when debugging an
506      executable, allow dumping the regcache even when there is no
507      thread selected (target_thread_address_space internal-errors if
508      no address space is found).  Note that normal user commands will
509      fail higher up on the call stack due to no
510      target_has_registers.  */
511   aspace = (ptid_equal (null_ptid, ptid)
512             ? NULL
513             : target_thread_address_space (ptid));
514
515   return get_thread_arch_aspace_regcache  (ptid, gdbarch, aspace);
516 }
517
518 static ptid_t current_thread_ptid;
519 static struct gdbarch *current_thread_arch;
520
521 struct regcache *
522 get_thread_regcache (ptid_t ptid)
523 {
524   if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
525     {
526       current_thread_ptid = ptid;
527       current_thread_arch = target_thread_architecture (ptid);
528     }
529
530   return get_thread_arch_regcache (ptid, current_thread_arch);
531 }
532
533 struct regcache *
534 get_current_regcache (void)
535 {
536   return get_thread_regcache (inferior_ptid);
537 }
538
539
540 /* Observer for the target_changed event.  */
541
542 static void
543 regcache_observer_target_changed (struct target_ops *target)
544 {
545   registers_changed ();
546 }
547
548 /* Update global variables old ptids to hold NEW_PTID if they were
549    holding OLD_PTID.  */
550 static void
551 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
552 {
553   struct regcache_list *list;
554
555   for (list = current_regcache; list; list = list->next)
556     if (ptid_equal (list->regcache->ptid, old_ptid))
557       list->regcache->ptid = new_ptid;
558 }
559
560 /* Low level examining and depositing of registers.
561
562    The caller is responsible for making sure that the inferior is
563    stopped before calling the fetching routines, or it will get
564    garbage.  (a change from GDB version 3, in which the caller got the
565    value from the last stop).  */
566
567 /* REGISTERS_CHANGED ()
568
569    Indicate that registers may have changed, so invalidate the cache.  */
570
571 void
572 registers_changed_ptid (ptid_t ptid)
573 {
574   struct regcache_list *list, **list_link;
575
576   list = current_regcache;
577   list_link = &current_regcache;
578   while (list)
579     {
580       if (ptid_match (list->regcache->ptid, ptid))
581         {
582           struct regcache_list *dead = list;
583
584           *list_link = list->next;
585           regcache_xfree (list->regcache);
586           list = *list_link;
587           xfree (dead);
588           continue;
589         }
590
591       list_link = &list->next;
592       list = *list_link;
593     }
594
595   if (ptid_match (current_thread_ptid, ptid))
596     {
597       current_thread_ptid = null_ptid;
598       current_thread_arch = NULL;
599     }
600
601   if (ptid_match (inferior_ptid, ptid))
602     {
603       /* We just deleted the regcache of the current thread.  Need to
604          forget about any frames we have cached, too.  */
605       reinit_frame_cache ();
606     }
607 }
608
609 void
610 registers_changed (void)
611 {
612   registers_changed_ptid (minus_one_ptid);
613
614   /* Force cleanup of any alloca areas if using C alloca instead of
615      a builtin alloca.  This particular call is used to clean up
616      areas allocated by low level target code which may build up
617      during lengthy interactions between gdb and the target before
618      gdb gives control to the user (ie watchpoints).  */
619   alloca (0);
620 }
621
622 enum register_status
623 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
624 {
625   gdb_assert (regcache != NULL && buf != NULL);
626   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
627   /* Make certain that the register cache is up-to-date with respect
628      to the current thread.  This switching shouldn't be necessary
629      only there is still only one target side register cache.  Sigh!
630      On the bright side, at least there is a regcache object.  */
631   if (!regcache->readonly_p
632       && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
633     {
634       struct cleanup *old_chain = save_inferior_ptid ();
635
636       inferior_ptid = regcache->ptid;
637       target_fetch_registers (regcache, regnum);
638       do_cleanups (old_chain);
639
640       /* A number of targets can't access the whole set of raw
641          registers (because the debug API provides no means to get at
642          them).  */
643       if (regcache->register_status[regnum] == REG_UNKNOWN)
644         regcache->register_status[regnum] = REG_UNAVAILABLE;
645     }
646
647   if (regcache->register_status[regnum] != REG_VALID)
648     memset (buf, 0, regcache->descr->sizeof_register[regnum]);
649   else
650     memcpy (buf, register_buffer (regcache, regnum),
651             regcache->descr->sizeof_register[regnum]);
652
653   return regcache->register_status[regnum];
654 }
655
656 enum register_status
657 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
658 {
659   gdb_byte *buf;
660   enum register_status status;
661
662   gdb_assert (regcache != NULL);
663   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
664   buf = alloca (regcache->descr->sizeof_register[regnum]);
665   status = regcache_raw_read (regcache, regnum, buf);
666   if (status == REG_VALID)
667     *val = extract_signed_integer
668       (buf, regcache->descr->sizeof_register[regnum],
669        gdbarch_byte_order (regcache->descr->gdbarch));
670   else
671     *val = 0;
672   return status;
673 }
674
675 enum register_status
676 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
677                             ULONGEST *val)
678 {
679   gdb_byte *buf;
680   enum register_status status;
681
682   gdb_assert (regcache != NULL);
683   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
684   buf = alloca (regcache->descr->sizeof_register[regnum]);
685   status = regcache_raw_read (regcache, regnum, buf);
686   if (status == REG_VALID)
687     *val = extract_unsigned_integer
688       (buf, regcache->descr->sizeof_register[regnum],
689        gdbarch_byte_order (regcache->descr->gdbarch));
690   else
691     *val = 0;
692   return status;
693 }
694
695 void
696 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
697 {
698   void *buf;
699
700   gdb_assert (regcache != NULL);
701   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
702   buf = alloca (regcache->descr->sizeof_register[regnum]);
703   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
704                         gdbarch_byte_order (regcache->descr->gdbarch), val);
705   regcache_raw_write (regcache, regnum, buf);
706 }
707
708 void
709 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
710                              ULONGEST val)
711 {
712   void *buf;
713
714   gdb_assert (regcache != NULL);
715   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
716   buf = alloca (regcache->descr->sizeof_register[regnum]);
717   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
718                           gdbarch_byte_order (regcache->descr->gdbarch), val);
719   regcache_raw_write (regcache, regnum, buf);
720 }
721
722 enum register_status
723 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
724 {
725   gdb_assert (regnum >= 0);
726   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
727   if (regnum < regcache->descr->nr_raw_registers)
728     return regcache_raw_read (regcache, regnum, buf);
729   else if (regcache->readonly_p
730            && regcache->register_status[regnum] != REG_UNKNOWN)
731     {
732       /* Read-only register cache, perhaps the cooked value was
733          cached?  */
734       if (regcache->register_status[regnum] == REG_VALID)
735         memcpy (buf, register_buffer (regcache, regnum),
736                 regcache->descr->sizeof_register[regnum]);
737       else
738         memset (buf, 0, regcache->descr->sizeof_register[regnum]);
739
740       return regcache->register_status[regnum];
741     }
742   else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
743     {
744       struct value *mark, *computed;
745       enum register_status result = REG_VALID;
746
747       mark = value_mark ();
748
749       computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
750                                                      regcache, regnum);
751       if (value_entirely_available (computed))
752         memcpy (buf, value_contents_raw (computed),
753                 regcache->descr->sizeof_register[regnum]);
754       else
755         {
756           memset (buf, 0, regcache->descr->sizeof_register[regnum]);
757           result = REG_UNAVAILABLE;
758         }
759
760       value_free_to_mark (mark);
761
762       return result;
763     }
764   else
765     return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
766                                          regnum, buf);
767 }
768
769 struct value *
770 regcache_cooked_read_value (struct regcache *regcache, int regnum)
771 {
772   gdb_assert (regnum >= 0);
773   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
774
775   if (regnum < regcache->descr->nr_raw_registers
776       || (regcache->readonly_p
777           && regcache->register_status[regnum] != REG_UNKNOWN)
778       || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
779     {
780       struct value *result;
781
782       result = allocate_value (register_type (regcache->descr->gdbarch,
783                                               regnum));
784       VALUE_LVAL (result) = lval_register;
785       VALUE_REGNUM (result) = regnum;
786
787       /* It is more efficient in general to do this delegation in this
788          direction than in the other one, even though the value-based
789          API is preferred.  */
790       if (regcache_cooked_read (regcache, regnum,
791                                 value_contents_raw (result)) == REG_UNAVAILABLE)
792         mark_value_bytes_unavailable (result, 0,
793                                       TYPE_LENGTH (value_type (result)));
794
795       return result;
796     }
797   else
798     return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
799                                                regcache, regnum);
800 }
801
802 enum register_status
803 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
804                              LONGEST *val)
805 {
806   enum register_status status;
807   gdb_byte *buf;
808
809   gdb_assert (regcache != NULL);
810   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
811   buf = alloca (regcache->descr->sizeof_register[regnum]);
812   status = regcache_cooked_read (regcache, regnum, buf);
813   if (status == REG_VALID)
814     *val = extract_signed_integer
815       (buf, regcache->descr->sizeof_register[regnum],
816        gdbarch_byte_order (regcache->descr->gdbarch));
817   else
818     *val = 0;
819   return status;
820 }
821
822 enum register_status
823 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
824                                ULONGEST *val)
825 {
826   enum register_status status;
827   gdb_byte *buf;
828
829   gdb_assert (regcache != NULL);
830   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
831   buf = alloca (regcache->descr->sizeof_register[regnum]);
832   status = regcache_cooked_read (regcache, regnum, buf);
833   if (status == REG_VALID)
834     *val = extract_unsigned_integer
835       (buf, regcache->descr->sizeof_register[regnum],
836        gdbarch_byte_order (regcache->descr->gdbarch));
837   else
838     *val = 0;
839   return status;
840 }
841
842 void
843 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
844                               LONGEST val)
845 {
846   void *buf;
847
848   gdb_assert (regcache != NULL);
849   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
850   buf = alloca (regcache->descr->sizeof_register[regnum]);
851   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
852                         gdbarch_byte_order (regcache->descr->gdbarch), val);
853   regcache_cooked_write (regcache, regnum, buf);
854 }
855
856 void
857 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
858                                 ULONGEST val)
859 {
860   void *buf;
861
862   gdb_assert (regcache != NULL);
863   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
864   buf = alloca (regcache->descr->sizeof_register[regnum]);
865   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
866                           gdbarch_byte_order (regcache->descr->gdbarch), val);
867   regcache_cooked_write (regcache, regnum, buf);
868 }
869
870 void
871 regcache_raw_write (struct regcache *regcache, int regnum,
872                     const gdb_byte *buf)
873 {
874   struct cleanup *chain_before_save_inferior;
875   struct cleanup *chain_before_invalidate_register;
876
877   gdb_assert (regcache != NULL && buf != NULL);
878   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
879   gdb_assert (!regcache->readonly_p);
880
881   /* On the sparc, writing %g0 is a no-op, so we don't even want to
882      change the registers array if something writes to this register.  */
883   if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
884     return;
885
886   /* If we have a valid copy of the register, and new value == old
887      value, then don't bother doing the actual store.  */
888   if (regcache_register_status (regcache, regnum) == REG_VALID
889       && (memcmp (register_buffer (regcache, regnum), buf,
890                   regcache->descr->sizeof_register[regnum]) == 0))
891     return;
892
893   chain_before_save_inferior = save_inferior_ptid ();
894   inferior_ptid = regcache->ptid;
895
896   target_prepare_to_store (regcache);
897   memcpy (register_buffer (regcache, regnum), buf,
898           regcache->descr->sizeof_register[regnum]);
899   regcache->register_status[regnum] = REG_VALID;
900
901   /* Register a cleanup function for invalidating the register after it is
902      written, in case of a failure.  */
903   chain_before_invalidate_register
904     = make_cleanup_regcache_invalidate (regcache, regnum);
905
906   target_store_registers (regcache, regnum);
907
908   /* The target did not throw an error so we can discard invalidating the
909      register and restore the cleanup chain to what it was.  */
910   discard_cleanups (chain_before_invalidate_register);
911
912   do_cleanups (chain_before_save_inferior);
913 }
914
915 void
916 regcache_cooked_write (struct regcache *regcache, int regnum,
917                        const gdb_byte *buf)
918 {
919   gdb_assert (regnum >= 0);
920   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
921   if (regnum < regcache->descr->nr_raw_registers)
922     regcache_raw_write (regcache, regnum, buf);
923   else
924     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
925                                    regnum, buf);
926 }
927
928 /* Perform a partial register transfer using a read, modify, write
929    operation.  */
930
931 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
932                                     void *buf);
933 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
934                                      const void *buf);
935
936 static enum register_status
937 regcache_xfer_part (struct regcache *regcache, int regnum,
938                     int offset, int len, void *in, const void *out,
939                     enum register_status (*read) (struct regcache *regcache,
940                                                   int regnum,
941                                                   gdb_byte *buf),
942                     void (*write) (struct regcache *regcache, int regnum,
943                                    const gdb_byte *buf))
944 {
945   struct regcache_descr *descr = regcache->descr;
946   gdb_byte reg[MAX_REGISTER_SIZE];
947
948   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
949   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
950   /* Something to do?  */
951   if (offset + len == 0)
952     return REG_VALID;
953   /* Read (when needed) ...  */
954   if (in != NULL
955       || offset > 0
956       || offset + len < descr->sizeof_register[regnum])
957     {
958       enum register_status status;
959
960       gdb_assert (read != NULL);
961       status = read (regcache, regnum, reg);
962       if (status != REG_VALID)
963         return status;
964     }
965   /* ... modify ...  */
966   if (in != NULL)
967     memcpy (in, reg + offset, len);
968   if (out != NULL)
969     memcpy (reg + offset, out, len);
970   /* ... write (when needed).  */
971   if (out != NULL)
972     {
973       gdb_assert (write != NULL);
974       write (regcache, regnum, reg);
975     }
976
977   return REG_VALID;
978 }
979
980 enum register_status
981 regcache_raw_read_part (struct regcache *regcache, int regnum,
982                         int offset, int len, gdb_byte *buf)
983 {
984   struct regcache_descr *descr = regcache->descr;
985
986   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
987   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
988                              regcache_raw_read, regcache_raw_write);
989 }
990
991 void
992 regcache_raw_write_part (struct regcache *regcache, int regnum,
993                          int offset, int len, const gdb_byte *buf)
994 {
995   struct regcache_descr *descr = regcache->descr;
996
997   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
998   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
999                       regcache_raw_read, regcache_raw_write);
1000 }
1001
1002 enum register_status
1003 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1004                            int offset, int len, gdb_byte *buf)
1005 {
1006   struct regcache_descr *descr = regcache->descr;
1007
1008   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1009   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1010                              regcache_cooked_read, regcache_cooked_write);
1011 }
1012
1013 void
1014 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1015                             int offset, int len, const gdb_byte *buf)
1016 {
1017   struct regcache_descr *descr = regcache->descr;
1018
1019   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1020   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1021                       regcache_cooked_read, regcache_cooked_write);
1022 }
1023
1024 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
1025
1026 void
1027 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1028 {
1029   void *regbuf;
1030   size_t size;
1031
1032   gdb_assert (regcache != NULL);
1033   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1034   gdb_assert (!regcache->readonly_p);
1035
1036   regbuf = register_buffer (regcache, regnum);
1037   size = regcache->descr->sizeof_register[regnum];
1038
1039   if (buf)
1040     {
1041       memcpy (regbuf, buf, size);
1042       regcache->register_status[regnum] = REG_VALID;
1043     }
1044   else
1045     {
1046       /* This memset not strictly necessary, but better than garbage
1047          in case the register value manages to escape somewhere (due
1048          to a bug, no less).  */
1049       memset (regbuf, 0, size);
1050       regcache->register_status[regnum] = REG_UNAVAILABLE;
1051     }
1052 }
1053
1054 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
1055
1056 void
1057 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1058 {
1059   const void *regbuf;
1060   size_t size;
1061
1062   gdb_assert (regcache != NULL && buf != NULL);
1063   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1064
1065   regbuf = register_buffer (regcache, regnum);
1066   size = regcache->descr->sizeof_register[regnum];
1067   memcpy (buf, regbuf, size);
1068 }
1069
1070
1071 /* Special handling for register PC.  */
1072
1073 CORE_ADDR
1074 regcache_read_pc (struct regcache *regcache)
1075 {
1076   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1077
1078   CORE_ADDR pc_val;
1079
1080   if (gdbarch_read_pc_p (gdbarch))
1081     pc_val = gdbarch_read_pc (gdbarch, regcache);
1082   /* Else use per-frame method on get_current_frame.  */
1083   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1084     {
1085       ULONGEST raw_val;
1086
1087       if (regcache_cooked_read_unsigned (regcache,
1088                                          gdbarch_pc_regnum (gdbarch),
1089                                          &raw_val) == REG_UNAVAILABLE)
1090         throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1091
1092       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1093     }
1094   else
1095     internal_error (__FILE__, __LINE__,
1096                     _("regcache_read_pc: Unable to find PC"));
1097   return pc_val;
1098 }
1099
1100 void
1101 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1102 {
1103   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1104
1105   if (gdbarch_write_pc_p (gdbarch))
1106     gdbarch_write_pc (gdbarch, regcache, pc);
1107   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1108     regcache_cooked_write_unsigned (regcache,
1109                                     gdbarch_pc_regnum (gdbarch), pc);
1110   else
1111     internal_error (__FILE__, __LINE__,
1112                     _("regcache_write_pc: Unable to update PC"));
1113
1114   /* Writing the PC (for instance, from "load") invalidates the
1115      current frame.  */
1116   reinit_frame_cache ();
1117 }
1118
1119
1120 static void
1121 reg_flush_command (char *command, int from_tty)
1122 {
1123   /* Force-flush the register cache.  */
1124   registers_changed ();
1125   if (from_tty)
1126     printf_filtered (_("Register cache flushed.\n"));
1127 }
1128
1129 enum regcache_dump_what
1130 {
1131   regcache_dump_none, regcache_dump_raw,
1132   regcache_dump_cooked, regcache_dump_groups,
1133   regcache_dump_remote
1134 };
1135
1136 static void
1137 regcache_dump (struct regcache *regcache, struct ui_file *file,
1138                enum regcache_dump_what what_to_dump)
1139 {
1140   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1141   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1142   int regnum;
1143   int footnote_nr = 0;
1144   int footnote_register_size = 0;
1145   int footnote_register_offset = 0;
1146   int footnote_register_type_name_null = 0;
1147   long register_offset = 0;
1148   gdb_byte buf[MAX_REGISTER_SIZE];
1149
1150 #if 0
1151   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1152                       regcache->descr->nr_raw_registers);
1153   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1154                       regcache->descr->nr_cooked_registers);
1155   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1156                       regcache->descr->sizeof_raw_registers);
1157   fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1158                       regcache->descr->sizeof_raw_register_status);
1159   fprintf_unfiltered (file, "gdbarch_num_regs %d\n", 
1160                       gdbarch_num_regs (gdbarch));
1161   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1162                       gdbarch_num_pseudo_regs (gdbarch));
1163 #endif
1164
1165   gdb_assert (regcache->descr->nr_cooked_registers
1166               == (gdbarch_num_regs (gdbarch)
1167                   + gdbarch_num_pseudo_regs (gdbarch)));
1168
1169   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1170     {
1171       /* Name.  */
1172       if (regnum < 0)
1173         fprintf_unfiltered (file, " %-10s", "Name");
1174       else
1175         {
1176           const char *p = gdbarch_register_name (gdbarch, regnum);
1177
1178           if (p == NULL)
1179             p = "";
1180           else if (p[0] == '\0')
1181             p = "''";
1182           fprintf_unfiltered (file, " %-10s", p);
1183         }
1184
1185       /* Number.  */
1186       if (regnum < 0)
1187         fprintf_unfiltered (file, " %4s", "Nr");
1188       else
1189         fprintf_unfiltered (file, " %4d", regnum);
1190
1191       /* Relative number.  */
1192       if (regnum < 0)
1193         fprintf_unfiltered (file, " %4s", "Rel");
1194       else if (regnum < gdbarch_num_regs (gdbarch))
1195         fprintf_unfiltered (file, " %4d", regnum);
1196       else
1197         fprintf_unfiltered (file, " %4d",
1198                             (regnum - gdbarch_num_regs (gdbarch)));
1199
1200       /* Offset.  */
1201       if (regnum < 0)
1202         fprintf_unfiltered (file, " %6s  ", "Offset");
1203       else
1204         {
1205           fprintf_unfiltered (file, " %6ld",
1206                               regcache->descr->register_offset[regnum]);
1207           if (register_offset != regcache->descr->register_offset[regnum]
1208               || (regnum > 0
1209                   && (regcache->descr->register_offset[regnum]
1210                       != (regcache->descr->register_offset[regnum - 1]
1211                           + regcache->descr->sizeof_register[regnum - 1])))
1212               )
1213             {
1214               if (!footnote_register_offset)
1215                 footnote_register_offset = ++footnote_nr;
1216               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1217             }
1218           else
1219             fprintf_unfiltered (file, "  ");
1220           register_offset = (regcache->descr->register_offset[regnum]
1221                              + regcache->descr->sizeof_register[regnum]);
1222         }
1223
1224       /* Size.  */
1225       if (regnum < 0)
1226         fprintf_unfiltered (file, " %5s ", "Size");
1227       else
1228         fprintf_unfiltered (file, " %5ld",
1229                             regcache->descr->sizeof_register[regnum]);
1230
1231       /* Type.  */
1232       {
1233         const char *t;
1234
1235         if (regnum < 0)
1236           t = "Type";
1237         else
1238           {
1239             static const char blt[] = "builtin_type";
1240
1241             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1242             if (t == NULL)
1243               {
1244                 char *n;
1245
1246                 if (!footnote_register_type_name_null)
1247                   footnote_register_type_name_null = ++footnote_nr;
1248                 n = xstrprintf ("*%d", footnote_register_type_name_null);
1249                 make_cleanup (xfree, n);
1250                 t = n;
1251               }
1252             /* Chop a leading builtin_type.  */
1253             if (strncmp (t, blt, strlen (blt)) == 0)
1254               t += strlen (blt);
1255           }
1256         fprintf_unfiltered (file, " %-15s", t);
1257       }
1258
1259       /* Leading space always present.  */
1260       fprintf_unfiltered (file, " ");
1261
1262       /* Value, raw.  */
1263       if (what_to_dump == regcache_dump_raw)
1264         {
1265           if (regnum < 0)
1266             fprintf_unfiltered (file, "Raw value");
1267           else if (regnum >= regcache->descr->nr_raw_registers)
1268             fprintf_unfiltered (file, "<cooked>");
1269           else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1270             fprintf_unfiltered (file, "<invalid>");
1271           else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1272             fprintf_unfiltered (file, "<unavailable>");
1273           else
1274             {
1275               regcache_raw_read (regcache, regnum, buf);
1276               print_hex_chars (file, buf,
1277                                regcache->descr->sizeof_register[regnum],
1278                                gdbarch_byte_order (gdbarch));
1279             }
1280         }
1281
1282       /* Value, cooked.  */
1283       if (what_to_dump == regcache_dump_cooked)
1284         {
1285           if (regnum < 0)
1286             fprintf_unfiltered (file, "Cooked value");
1287           else
1288             {
1289               enum register_status status;
1290
1291               status = regcache_cooked_read (regcache, regnum, buf);
1292               if (status == REG_UNKNOWN)
1293                 fprintf_unfiltered (file, "<invalid>");
1294               else if (status == REG_UNAVAILABLE)
1295                 fprintf_unfiltered (file, "<unavailable>");
1296               else
1297                 print_hex_chars (file, buf,
1298                                  regcache->descr->sizeof_register[regnum],
1299                                  gdbarch_byte_order (gdbarch));
1300             }
1301         }
1302
1303       /* Group members.  */
1304       if (what_to_dump == regcache_dump_groups)
1305         {
1306           if (regnum < 0)
1307             fprintf_unfiltered (file, "Groups");
1308           else
1309             {
1310               const char *sep = "";
1311               struct reggroup *group;
1312
1313               for (group = reggroup_next (gdbarch, NULL);
1314                    group != NULL;
1315                    group = reggroup_next (gdbarch, group))
1316                 {
1317                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1318                     {
1319                       fprintf_unfiltered (file,
1320                                           "%s%s", sep, reggroup_name (group));
1321                       sep = ",";
1322                     }
1323                 }
1324             }
1325         }
1326
1327       /* Remote packet configuration.  */
1328       if (what_to_dump == regcache_dump_remote)
1329         {
1330           if (regnum < 0)
1331             {
1332               fprintf_unfiltered (file, "Rmt Nr  g/G Offset");
1333             }
1334           else if (regnum < regcache->descr->nr_raw_registers)
1335             {
1336               int pnum, poffset;
1337
1338               if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1339                                                      &pnum, &poffset))
1340                 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1341             }
1342         }
1343
1344       fprintf_unfiltered (file, "\n");
1345     }
1346
1347   if (footnote_register_size)
1348     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1349                         footnote_register_size);
1350   if (footnote_register_offset)
1351     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1352                         footnote_register_offset);
1353   if (footnote_register_type_name_null)
1354     fprintf_unfiltered (file, 
1355                         "*%d: Register type's name NULL.\n",
1356                         footnote_register_type_name_null);
1357   do_cleanups (cleanups);
1358 }
1359
1360 static void
1361 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1362 {
1363   if (args == NULL)
1364     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1365   else
1366     {
1367       struct cleanup *cleanups;
1368       struct ui_file *file = gdb_fopen (args, "w");
1369
1370       if (file == NULL)
1371         perror_with_name (_("maintenance print architecture"));
1372       cleanups = make_cleanup_ui_file_delete (file);
1373       regcache_dump (get_current_regcache (), file, what_to_dump);
1374       do_cleanups (cleanups);
1375     }
1376 }
1377
1378 static void
1379 maintenance_print_registers (char *args, int from_tty)
1380 {
1381   regcache_print (args, regcache_dump_none);
1382 }
1383
1384 static void
1385 maintenance_print_raw_registers (char *args, int from_tty)
1386 {
1387   regcache_print (args, regcache_dump_raw);
1388 }
1389
1390 static void
1391 maintenance_print_cooked_registers (char *args, int from_tty)
1392 {
1393   regcache_print (args, regcache_dump_cooked);
1394 }
1395
1396 static void
1397 maintenance_print_register_groups (char *args, int from_tty)
1398 {
1399   regcache_print (args, regcache_dump_groups);
1400 }
1401
1402 static void
1403 maintenance_print_remote_registers (char *args, int from_tty)
1404 {
1405   regcache_print (args, regcache_dump_remote);
1406 }
1407
1408 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1409
1410 void
1411 _initialize_regcache (void)
1412 {
1413   regcache_descr_handle
1414     = gdbarch_data_register_post_init (init_regcache_descr);
1415
1416   observer_attach_target_changed (regcache_observer_target_changed);
1417   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1418
1419   add_com ("flushregs", class_maintenance, reg_flush_command,
1420            _("Force gdb to flush its register cache (maintainer command)"));
1421
1422   add_cmd ("registers", class_maintenance, maintenance_print_registers,
1423            _("Print the internal register configuration.\n"
1424              "Takes an optional file parameter."), &maintenanceprintlist);
1425   add_cmd ("raw-registers", class_maintenance,
1426            maintenance_print_raw_registers,
1427            _("Print the internal register configuration "
1428              "including raw values.\n"
1429              "Takes an optional file parameter."), &maintenanceprintlist);
1430   add_cmd ("cooked-registers", class_maintenance,
1431            maintenance_print_cooked_registers,
1432            _("Print the internal register configuration "
1433              "including cooked values.\n"
1434              "Takes an optional file parameter."), &maintenanceprintlist);
1435   add_cmd ("register-groups", class_maintenance,
1436            maintenance_print_register_groups,
1437            _("Print the internal register configuration "
1438              "including each register's group.\n"
1439              "Takes an optional file parameter."),
1440            &maintenanceprintlist);
1441   add_cmd ("remote-registers", class_maintenance,
1442            maintenance_print_remote_registers, _("\
1443 Print the internal register configuration including each register's\n\
1444 remote register number and buffer offset in the g/G packets.\n\
1445 Takes an optional file parameter."),
1446            &maintenanceprintlist);
1447
1448 }