IA64: Migrate from 'regset_from_core_section' to 'iterate_over_regset_sections'
[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 "observer.h"
28 #include "exceptions.h"
29 #include "remote.h"
30 #include "valprint.h"
31 #include "regset.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 /* See common/common-regcache.h.  */
540
541 struct regcache *
542 get_thread_regcache_for_ptid (ptid_t ptid)
543 {
544   return get_thread_regcache (ptid);
545 }
546
547 /* Observer for the target_changed event.  */
548
549 static void
550 regcache_observer_target_changed (struct target_ops *target)
551 {
552   registers_changed ();
553 }
554
555 /* Update global variables old ptids to hold NEW_PTID if they were
556    holding OLD_PTID.  */
557 static void
558 regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
559 {
560   struct regcache_list *list;
561
562   for (list = current_regcache; list; list = list->next)
563     if (ptid_equal (list->regcache->ptid, old_ptid))
564       list->regcache->ptid = new_ptid;
565 }
566
567 /* Low level examining and depositing of registers.
568
569    The caller is responsible for making sure that the inferior is
570    stopped before calling the fetching routines, or it will get
571    garbage.  (a change from GDB version 3, in which the caller got the
572    value from the last stop).  */
573
574 /* REGISTERS_CHANGED ()
575
576    Indicate that registers may have changed, so invalidate the cache.  */
577
578 void
579 registers_changed_ptid (ptid_t ptid)
580 {
581   struct regcache_list *list, **list_link;
582
583   list = current_regcache;
584   list_link = &current_regcache;
585   while (list)
586     {
587       if (ptid_match (list->regcache->ptid, ptid))
588         {
589           struct regcache_list *dead = list;
590
591           *list_link = list->next;
592           regcache_xfree (list->regcache);
593           list = *list_link;
594           xfree (dead);
595           continue;
596         }
597
598       list_link = &list->next;
599       list = *list_link;
600     }
601
602   if (ptid_match (current_thread_ptid, ptid))
603     {
604       current_thread_ptid = null_ptid;
605       current_thread_arch = NULL;
606     }
607
608   if (ptid_match (inferior_ptid, ptid))
609     {
610       /* We just deleted the regcache of the current thread.  Need to
611          forget about any frames we have cached, too.  */
612       reinit_frame_cache ();
613     }
614 }
615
616 void
617 registers_changed (void)
618 {
619   registers_changed_ptid (minus_one_ptid);
620
621   /* Force cleanup of any alloca areas if using C alloca instead of
622      a builtin alloca.  This particular call is used to clean up
623      areas allocated by low level target code which may build up
624      during lengthy interactions between gdb and the target before
625      gdb gives control to the user (ie watchpoints).  */
626   alloca (0);
627 }
628
629 enum register_status
630 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
631 {
632   gdb_assert (regcache != NULL && buf != NULL);
633   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
634   /* Make certain that the register cache is up-to-date with respect
635      to the current thread.  This switching shouldn't be necessary
636      only there is still only one target side register cache.  Sigh!
637      On the bright side, at least there is a regcache object.  */
638   if (!regcache->readonly_p
639       && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
640     {
641       struct cleanup *old_chain = save_inferior_ptid ();
642
643       inferior_ptid = regcache->ptid;
644       target_fetch_registers (regcache, regnum);
645       do_cleanups (old_chain);
646
647       /* A number of targets can't access the whole set of raw
648          registers (because the debug API provides no means to get at
649          them).  */
650       if (regcache->register_status[regnum] == REG_UNKNOWN)
651         regcache->register_status[regnum] = REG_UNAVAILABLE;
652     }
653
654   if (regcache->register_status[regnum] != REG_VALID)
655     memset (buf, 0, regcache->descr->sizeof_register[regnum]);
656   else
657     memcpy (buf, register_buffer (regcache, regnum),
658             regcache->descr->sizeof_register[regnum]);
659
660   return regcache->register_status[regnum];
661 }
662
663 enum register_status
664 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
665 {
666   gdb_byte *buf;
667   enum register_status status;
668
669   gdb_assert (regcache != NULL);
670   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
671   buf = alloca (regcache->descr->sizeof_register[regnum]);
672   status = regcache_raw_read (regcache, regnum, buf);
673   if (status == REG_VALID)
674     *val = extract_signed_integer
675       (buf, regcache->descr->sizeof_register[regnum],
676        gdbarch_byte_order (regcache->descr->gdbarch));
677   else
678     *val = 0;
679   return status;
680 }
681
682 enum register_status
683 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
684                             ULONGEST *val)
685 {
686   gdb_byte *buf;
687   enum register_status status;
688
689   gdb_assert (regcache != NULL);
690   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
691   buf = alloca (regcache->descr->sizeof_register[regnum]);
692   status = regcache_raw_read (regcache, regnum, buf);
693   if (status == REG_VALID)
694     *val = extract_unsigned_integer
695       (buf, regcache->descr->sizeof_register[regnum],
696        gdbarch_byte_order (regcache->descr->gdbarch));
697   else
698     *val = 0;
699   return status;
700 }
701
702 void
703 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
704 {
705   void *buf;
706
707   gdb_assert (regcache != NULL);
708   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
709   buf = alloca (regcache->descr->sizeof_register[regnum]);
710   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
711                         gdbarch_byte_order (regcache->descr->gdbarch), val);
712   regcache_raw_write (regcache, regnum, buf);
713 }
714
715 void
716 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
717                              ULONGEST val)
718 {
719   void *buf;
720
721   gdb_assert (regcache != NULL);
722   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
723   buf = alloca (regcache->descr->sizeof_register[regnum]);
724   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
725                           gdbarch_byte_order (regcache->descr->gdbarch), val);
726   regcache_raw_write (regcache, regnum, buf);
727 }
728
729 enum register_status
730 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
731 {
732   gdb_assert (regnum >= 0);
733   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
734   if (regnum < regcache->descr->nr_raw_registers)
735     return regcache_raw_read (regcache, regnum, buf);
736   else if (regcache->readonly_p
737            && regcache->register_status[regnum] != REG_UNKNOWN)
738     {
739       /* Read-only register cache, perhaps the cooked value was
740          cached?  */
741       if (regcache->register_status[regnum] == REG_VALID)
742         memcpy (buf, register_buffer (regcache, regnum),
743                 regcache->descr->sizeof_register[regnum]);
744       else
745         memset (buf, 0, regcache->descr->sizeof_register[regnum]);
746
747       return regcache->register_status[regnum];
748     }
749   else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
750     {
751       struct value *mark, *computed;
752       enum register_status result = REG_VALID;
753
754       mark = value_mark ();
755
756       computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
757                                                      regcache, regnum);
758       if (value_entirely_available (computed))
759         memcpy (buf, value_contents_raw (computed),
760                 regcache->descr->sizeof_register[regnum]);
761       else
762         {
763           memset (buf, 0, regcache->descr->sizeof_register[regnum]);
764           result = REG_UNAVAILABLE;
765         }
766
767       value_free_to_mark (mark);
768
769       return result;
770     }
771   else
772     return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
773                                          regnum, buf);
774 }
775
776 struct value *
777 regcache_cooked_read_value (struct regcache *regcache, int regnum)
778 {
779   gdb_assert (regnum >= 0);
780   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
781
782   if (regnum < regcache->descr->nr_raw_registers
783       || (regcache->readonly_p
784           && regcache->register_status[regnum] != REG_UNKNOWN)
785       || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
786     {
787       struct value *result;
788
789       result = allocate_value (register_type (regcache->descr->gdbarch,
790                                               regnum));
791       VALUE_LVAL (result) = lval_register;
792       VALUE_REGNUM (result) = regnum;
793
794       /* It is more efficient in general to do this delegation in this
795          direction than in the other one, even though the value-based
796          API is preferred.  */
797       if (regcache_cooked_read (regcache, regnum,
798                                 value_contents_raw (result)) == REG_UNAVAILABLE)
799         mark_value_bytes_unavailable (result, 0,
800                                       TYPE_LENGTH (value_type (result)));
801
802       return result;
803     }
804   else
805     return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
806                                                regcache, regnum);
807 }
808
809 enum register_status
810 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
811                              LONGEST *val)
812 {
813   enum register_status status;
814   gdb_byte *buf;
815
816   gdb_assert (regcache != NULL);
817   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
818   buf = alloca (regcache->descr->sizeof_register[regnum]);
819   status = regcache_cooked_read (regcache, regnum, buf);
820   if (status == REG_VALID)
821     *val = extract_signed_integer
822       (buf, regcache->descr->sizeof_register[regnum],
823        gdbarch_byte_order (regcache->descr->gdbarch));
824   else
825     *val = 0;
826   return status;
827 }
828
829 enum register_status
830 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
831                                ULONGEST *val)
832 {
833   enum register_status status;
834   gdb_byte *buf;
835
836   gdb_assert (regcache != NULL);
837   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
838   buf = alloca (regcache->descr->sizeof_register[regnum]);
839   status = regcache_cooked_read (regcache, regnum, buf);
840   if (status == REG_VALID)
841     *val = extract_unsigned_integer
842       (buf, regcache->descr->sizeof_register[regnum],
843        gdbarch_byte_order (regcache->descr->gdbarch));
844   else
845     *val = 0;
846   return status;
847 }
848
849 void
850 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
851                               LONGEST val)
852 {
853   void *buf;
854
855   gdb_assert (regcache != NULL);
856   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
857   buf = alloca (regcache->descr->sizeof_register[regnum]);
858   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
859                         gdbarch_byte_order (regcache->descr->gdbarch), val);
860   regcache_cooked_write (regcache, regnum, buf);
861 }
862
863 void
864 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
865                                 ULONGEST val)
866 {
867   void *buf;
868
869   gdb_assert (regcache != NULL);
870   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
871   buf = alloca (regcache->descr->sizeof_register[regnum]);
872   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
873                           gdbarch_byte_order (regcache->descr->gdbarch), val);
874   regcache_cooked_write (regcache, regnum, buf);
875 }
876
877 void
878 regcache_raw_write (struct regcache *regcache, int regnum,
879                     const gdb_byte *buf)
880 {
881   struct cleanup *chain_before_save_inferior;
882   struct cleanup *chain_before_invalidate_register;
883
884   gdb_assert (regcache != NULL && buf != NULL);
885   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
886   gdb_assert (!regcache->readonly_p);
887
888   /* On the sparc, writing %g0 is a no-op, so we don't even want to
889      change the registers array if something writes to this register.  */
890   if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
891     return;
892
893   /* If we have a valid copy of the register, and new value == old
894      value, then don't bother doing the actual store.  */
895   if (regcache_register_status (regcache, regnum) == REG_VALID
896       && (memcmp (register_buffer (regcache, regnum), buf,
897                   regcache->descr->sizeof_register[regnum]) == 0))
898     return;
899
900   chain_before_save_inferior = save_inferior_ptid ();
901   inferior_ptid = regcache->ptid;
902
903   target_prepare_to_store (regcache);
904   memcpy (register_buffer (regcache, regnum), buf,
905           regcache->descr->sizeof_register[regnum]);
906   regcache->register_status[regnum] = REG_VALID;
907
908   /* Register a cleanup function for invalidating the register after it is
909      written, in case of a failure.  */
910   chain_before_invalidate_register
911     = make_cleanup_regcache_invalidate (regcache, regnum);
912
913   target_store_registers (regcache, regnum);
914
915   /* The target did not throw an error so we can discard invalidating the
916      register and restore the cleanup chain to what it was.  */
917   discard_cleanups (chain_before_invalidate_register);
918
919   do_cleanups (chain_before_save_inferior);
920 }
921
922 void
923 regcache_cooked_write (struct regcache *regcache, int regnum,
924                        const gdb_byte *buf)
925 {
926   gdb_assert (regnum >= 0);
927   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
928   if (regnum < regcache->descr->nr_raw_registers)
929     regcache_raw_write (regcache, regnum, buf);
930   else
931     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
932                                    regnum, buf);
933 }
934
935 /* Perform a partial register transfer using a read, modify, write
936    operation.  */
937
938 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
939                                     void *buf);
940 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
941                                      const void *buf);
942
943 static enum register_status
944 regcache_xfer_part (struct regcache *regcache, int regnum,
945                     int offset, int len, void *in, const void *out,
946                     enum register_status (*read) (struct regcache *regcache,
947                                                   int regnum,
948                                                   gdb_byte *buf),
949                     void (*write) (struct regcache *regcache, int regnum,
950                                    const gdb_byte *buf))
951 {
952   struct regcache_descr *descr = regcache->descr;
953   gdb_byte reg[MAX_REGISTER_SIZE];
954
955   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
956   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
957   /* Something to do?  */
958   if (offset + len == 0)
959     return REG_VALID;
960   /* Read (when needed) ...  */
961   if (in != NULL
962       || offset > 0
963       || offset + len < descr->sizeof_register[regnum])
964     {
965       enum register_status status;
966
967       gdb_assert (read != NULL);
968       status = read (regcache, regnum, reg);
969       if (status != REG_VALID)
970         return status;
971     }
972   /* ... modify ...  */
973   if (in != NULL)
974     memcpy (in, reg + offset, len);
975   if (out != NULL)
976     memcpy (reg + offset, out, len);
977   /* ... write (when needed).  */
978   if (out != NULL)
979     {
980       gdb_assert (write != NULL);
981       write (regcache, regnum, reg);
982     }
983
984   return REG_VALID;
985 }
986
987 enum register_status
988 regcache_raw_read_part (struct regcache *regcache, int regnum,
989                         int offset, int len, gdb_byte *buf)
990 {
991   struct regcache_descr *descr = regcache->descr;
992
993   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
994   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
995                              regcache_raw_read, regcache_raw_write);
996 }
997
998 void
999 regcache_raw_write_part (struct regcache *regcache, int regnum,
1000                          int offset, int len, const gdb_byte *buf)
1001 {
1002   struct regcache_descr *descr = regcache->descr;
1003
1004   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1005   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1006                       regcache_raw_read, regcache_raw_write);
1007 }
1008
1009 enum register_status
1010 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1011                            int offset, int len, gdb_byte *buf)
1012 {
1013   struct regcache_descr *descr = regcache->descr;
1014
1015   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1016   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1017                              regcache_cooked_read, regcache_cooked_write);
1018 }
1019
1020 void
1021 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1022                             int offset, int len, const gdb_byte *buf)
1023 {
1024   struct regcache_descr *descr = regcache->descr;
1025
1026   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1027   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1028                       regcache_cooked_read, regcache_cooked_write);
1029 }
1030
1031 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
1032
1033 void
1034 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1035 {
1036   void *regbuf;
1037   size_t size;
1038
1039   gdb_assert (regcache != NULL);
1040   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1041   gdb_assert (!regcache->readonly_p);
1042
1043   regbuf = register_buffer (regcache, regnum);
1044   size = regcache->descr->sizeof_register[regnum];
1045
1046   if (buf)
1047     {
1048       memcpy (regbuf, buf, size);
1049       regcache->register_status[regnum] = REG_VALID;
1050     }
1051   else
1052     {
1053       /* This memset not strictly necessary, but better than garbage
1054          in case the register value manages to escape somewhere (due
1055          to a bug, no less).  */
1056       memset (regbuf, 0, size);
1057       regcache->register_status[regnum] = REG_UNAVAILABLE;
1058     }
1059 }
1060
1061 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
1062
1063 void
1064 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1065 {
1066   const void *regbuf;
1067   size_t size;
1068
1069   gdb_assert (regcache != NULL && buf != NULL);
1070   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1071
1072   regbuf = register_buffer (regcache, regnum);
1073   size = regcache->descr->sizeof_register[regnum];
1074   memcpy (buf, regbuf, size);
1075 }
1076
1077 /* Transfer a single or all registers belonging to a certain register
1078    set to or from a buffer.  This is the main worker function for
1079    regcache_supply_regset and regcache_collect_regset.  */
1080
1081 static void
1082 regcache_transfer_regset (const struct regset *regset,
1083                           const struct regcache *regcache,
1084                           struct regcache *out_regcache,
1085                           int regnum, const void *in_buf,
1086                           void *out_buf, size_t size)
1087 {
1088   const struct regcache_map_entry *map;
1089   int offs = 0, count;
1090
1091   for (map = regset->regmap; (count = map->count) != 0; map++)
1092     {
1093       int regno = map->regno;
1094       int slot_size = map->size;
1095
1096       if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1097         slot_size = regcache->descr->sizeof_register[regno];
1098
1099       if (regno == REGCACHE_MAP_SKIP
1100           || (regnum != -1
1101               && (regnum < regno || regnum >= regno + count)))
1102           offs += count * slot_size;
1103
1104       else if (regnum == -1)
1105         for (; count--; regno++, offs += slot_size)
1106           {
1107             if (offs + slot_size > size)
1108               break;
1109
1110             if (out_buf)
1111               regcache_raw_collect (regcache, regno,
1112                                     (gdb_byte *) out_buf + offs);
1113             else
1114               regcache_raw_supply (out_regcache, regno, in_buf
1115                                    ? (const gdb_byte *) in_buf + offs
1116                                    : NULL);
1117           }
1118       else
1119         {
1120           /* Transfer a single register and return.  */
1121           offs += (regnum - regno) * slot_size;
1122           if (offs + slot_size > size)
1123             return;
1124
1125           if (out_buf)
1126             regcache_raw_collect (regcache, regnum,
1127                                   (gdb_byte *) out_buf + offs);
1128           else
1129             regcache_raw_supply (out_regcache, regnum, in_buf
1130                                  ? (const gdb_byte *) in_buf + offs
1131                                  : NULL);
1132           return;
1133         }
1134     }
1135 }
1136
1137 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1138    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1139    If BUF is NULL, set the register(s) to "unavailable" status. */
1140
1141 void
1142 regcache_supply_regset (const struct regset *regset,
1143                         struct regcache *regcache,
1144                         int regnum, const void *buf, size_t size)
1145 {
1146   regcache_transfer_regset (regset, regcache, regcache, regnum,
1147                             buf, NULL, size);
1148 }
1149
1150 /* Collect register REGNUM from REGCACHE to BUF, using the register
1151    map in REGSET.  If REGNUM is -1, do this for all registers in
1152    REGSET.  */
1153
1154 void
1155 regcache_collect_regset (const struct regset *regset,
1156                          const struct regcache *regcache,
1157                          int regnum, void *buf, size_t size)
1158 {
1159   regcache_transfer_regset (regset, regcache, NULL, regnum,
1160                             NULL, buf, size);
1161 }
1162
1163
1164 /* Special handling for register PC.  */
1165
1166 CORE_ADDR
1167 regcache_read_pc (struct regcache *regcache)
1168 {
1169   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1170
1171   CORE_ADDR pc_val;
1172
1173   if (gdbarch_read_pc_p (gdbarch))
1174     pc_val = gdbarch_read_pc (gdbarch, regcache);
1175   /* Else use per-frame method on get_current_frame.  */
1176   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1177     {
1178       ULONGEST raw_val;
1179
1180       if (regcache_cooked_read_unsigned (regcache,
1181                                          gdbarch_pc_regnum (gdbarch),
1182                                          &raw_val) == REG_UNAVAILABLE)
1183         throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1184
1185       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1186     }
1187   else
1188     internal_error (__FILE__, __LINE__,
1189                     _("regcache_read_pc: Unable to find PC"));
1190   return pc_val;
1191 }
1192
1193 void
1194 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1195 {
1196   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1197
1198   if (gdbarch_write_pc_p (gdbarch))
1199     gdbarch_write_pc (gdbarch, regcache, pc);
1200   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1201     regcache_cooked_write_unsigned (regcache,
1202                                     gdbarch_pc_regnum (gdbarch), pc);
1203   else
1204     internal_error (__FILE__, __LINE__,
1205                     _("regcache_write_pc: Unable to update PC"));
1206
1207   /* Writing the PC (for instance, from "load") invalidates the
1208      current frame.  */
1209   reinit_frame_cache ();
1210 }
1211
1212
1213 static void
1214 reg_flush_command (char *command, int from_tty)
1215 {
1216   /* Force-flush the register cache.  */
1217   registers_changed ();
1218   if (from_tty)
1219     printf_filtered (_("Register cache flushed.\n"));
1220 }
1221
1222 enum regcache_dump_what
1223 {
1224   regcache_dump_none, regcache_dump_raw,
1225   regcache_dump_cooked, regcache_dump_groups,
1226   regcache_dump_remote
1227 };
1228
1229 static void
1230 regcache_dump (struct regcache *regcache, struct ui_file *file,
1231                enum regcache_dump_what what_to_dump)
1232 {
1233   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1234   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1235   int regnum;
1236   int footnote_nr = 0;
1237   int footnote_register_size = 0;
1238   int footnote_register_offset = 0;
1239   int footnote_register_type_name_null = 0;
1240   long register_offset = 0;
1241   gdb_byte buf[MAX_REGISTER_SIZE];
1242
1243 #if 0
1244   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1245                       regcache->descr->nr_raw_registers);
1246   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1247                       regcache->descr->nr_cooked_registers);
1248   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1249                       regcache->descr->sizeof_raw_registers);
1250   fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1251                       regcache->descr->sizeof_raw_register_status);
1252   fprintf_unfiltered (file, "gdbarch_num_regs %d\n", 
1253                       gdbarch_num_regs (gdbarch));
1254   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1255                       gdbarch_num_pseudo_regs (gdbarch));
1256 #endif
1257
1258   gdb_assert (regcache->descr->nr_cooked_registers
1259               == (gdbarch_num_regs (gdbarch)
1260                   + gdbarch_num_pseudo_regs (gdbarch)));
1261
1262   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1263     {
1264       /* Name.  */
1265       if (regnum < 0)
1266         fprintf_unfiltered (file, " %-10s", "Name");
1267       else
1268         {
1269           const char *p = gdbarch_register_name (gdbarch, regnum);
1270
1271           if (p == NULL)
1272             p = "";
1273           else if (p[0] == '\0')
1274             p = "''";
1275           fprintf_unfiltered (file, " %-10s", p);
1276         }
1277
1278       /* Number.  */
1279       if (regnum < 0)
1280         fprintf_unfiltered (file, " %4s", "Nr");
1281       else
1282         fprintf_unfiltered (file, " %4d", regnum);
1283
1284       /* Relative number.  */
1285       if (regnum < 0)
1286         fprintf_unfiltered (file, " %4s", "Rel");
1287       else if (regnum < gdbarch_num_regs (gdbarch))
1288         fprintf_unfiltered (file, " %4d", regnum);
1289       else
1290         fprintf_unfiltered (file, " %4d",
1291                             (regnum - gdbarch_num_regs (gdbarch)));
1292
1293       /* Offset.  */
1294       if (regnum < 0)
1295         fprintf_unfiltered (file, " %6s  ", "Offset");
1296       else
1297         {
1298           fprintf_unfiltered (file, " %6ld",
1299                               regcache->descr->register_offset[regnum]);
1300           if (register_offset != regcache->descr->register_offset[regnum]
1301               || (regnum > 0
1302                   && (regcache->descr->register_offset[regnum]
1303                       != (regcache->descr->register_offset[regnum - 1]
1304                           + regcache->descr->sizeof_register[regnum - 1])))
1305               )
1306             {
1307               if (!footnote_register_offset)
1308                 footnote_register_offset = ++footnote_nr;
1309               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1310             }
1311           else
1312             fprintf_unfiltered (file, "  ");
1313           register_offset = (regcache->descr->register_offset[regnum]
1314                              + regcache->descr->sizeof_register[regnum]);
1315         }
1316
1317       /* Size.  */
1318       if (regnum < 0)
1319         fprintf_unfiltered (file, " %5s ", "Size");
1320       else
1321         fprintf_unfiltered (file, " %5ld",
1322                             regcache->descr->sizeof_register[regnum]);
1323
1324       /* Type.  */
1325       {
1326         const char *t;
1327
1328         if (regnum < 0)
1329           t = "Type";
1330         else
1331           {
1332             static const char blt[] = "builtin_type";
1333
1334             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1335             if (t == NULL)
1336               {
1337                 char *n;
1338
1339                 if (!footnote_register_type_name_null)
1340                   footnote_register_type_name_null = ++footnote_nr;
1341                 n = xstrprintf ("*%d", footnote_register_type_name_null);
1342                 make_cleanup (xfree, n);
1343                 t = n;
1344               }
1345             /* Chop a leading builtin_type.  */
1346             if (strncmp (t, blt, strlen (blt)) == 0)
1347               t += strlen (blt);
1348           }
1349         fprintf_unfiltered (file, " %-15s", t);
1350       }
1351
1352       /* Leading space always present.  */
1353       fprintf_unfiltered (file, " ");
1354
1355       /* Value, raw.  */
1356       if (what_to_dump == regcache_dump_raw)
1357         {
1358           if (regnum < 0)
1359             fprintf_unfiltered (file, "Raw value");
1360           else if (regnum >= regcache->descr->nr_raw_registers)
1361             fprintf_unfiltered (file, "<cooked>");
1362           else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1363             fprintf_unfiltered (file, "<invalid>");
1364           else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1365             fprintf_unfiltered (file, "<unavailable>");
1366           else
1367             {
1368               regcache_raw_read (regcache, regnum, buf);
1369               print_hex_chars (file, buf,
1370                                regcache->descr->sizeof_register[regnum],
1371                                gdbarch_byte_order (gdbarch));
1372             }
1373         }
1374
1375       /* Value, cooked.  */
1376       if (what_to_dump == regcache_dump_cooked)
1377         {
1378           if (regnum < 0)
1379             fprintf_unfiltered (file, "Cooked value");
1380           else
1381             {
1382               enum register_status status;
1383
1384               status = regcache_cooked_read (regcache, regnum, buf);
1385               if (status == REG_UNKNOWN)
1386                 fprintf_unfiltered (file, "<invalid>");
1387               else if (status == REG_UNAVAILABLE)
1388                 fprintf_unfiltered (file, "<unavailable>");
1389               else
1390                 print_hex_chars (file, buf,
1391                                  regcache->descr->sizeof_register[regnum],
1392                                  gdbarch_byte_order (gdbarch));
1393             }
1394         }
1395
1396       /* Group members.  */
1397       if (what_to_dump == regcache_dump_groups)
1398         {
1399           if (regnum < 0)
1400             fprintf_unfiltered (file, "Groups");
1401           else
1402             {
1403               const char *sep = "";
1404               struct reggroup *group;
1405
1406               for (group = reggroup_next (gdbarch, NULL);
1407                    group != NULL;
1408                    group = reggroup_next (gdbarch, group))
1409                 {
1410                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1411                     {
1412                       fprintf_unfiltered (file,
1413                                           "%s%s", sep, reggroup_name (group));
1414                       sep = ",";
1415                     }
1416                 }
1417             }
1418         }
1419
1420       /* Remote packet configuration.  */
1421       if (what_to_dump == regcache_dump_remote)
1422         {
1423           if (regnum < 0)
1424             {
1425               fprintf_unfiltered (file, "Rmt Nr  g/G Offset");
1426             }
1427           else if (regnum < regcache->descr->nr_raw_registers)
1428             {
1429               int pnum, poffset;
1430
1431               if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1432                                                      &pnum, &poffset))
1433                 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1434             }
1435         }
1436
1437       fprintf_unfiltered (file, "\n");
1438     }
1439
1440   if (footnote_register_size)
1441     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1442                         footnote_register_size);
1443   if (footnote_register_offset)
1444     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1445                         footnote_register_offset);
1446   if (footnote_register_type_name_null)
1447     fprintf_unfiltered (file, 
1448                         "*%d: Register type's name NULL.\n",
1449                         footnote_register_type_name_null);
1450   do_cleanups (cleanups);
1451 }
1452
1453 static void
1454 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1455 {
1456   if (args == NULL)
1457     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1458   else
1459     {
1460       struct cleanup *cleanups;
1461       struct ui_file *file = gdb_fopen (args, "w");
1462
1463       if (file == NULL)
1464         perror_with_name (_("maintenance print architecture"));
1465       cleanups = make_cleanup_ui_file_delete (file);
1466       regcache_dump (get_current_regcache (), file, what_to_dump);
1467       do_cleanups (cleanups);
1468     }
1469 }
1470
1471 static void
1472 maintenance_print_registers (char *args, int from_tty)
1473 {
1474   regcache_print (args, regcache_dump_none);
1475 }
1476
1477 static void
1478 maintenance_print_raw_registers (char *args, int from_tty)
1479 {
1480   regcache_print (args, regcache_dump_raw);
1481 }
1482
1483 static void
1484 maintenance_print_cooked_registers (char *args, int from_tty)
1485 {
1486   regcache_print (args, regcache_dump_cooked);
1487 }
1488
1489 static void
1490 maintenance_print_register_groups (char *args, int from_tty)
1491 {
1492   regcache_print (args, regcache_dump_groups);
1493 }
1494
1495 static void
1496 maintenance_print_remote_registers (char *args, int from_tty)
1497 {
1498   regcache_print (args, regcache_dump_remote);
1499 }
1500
1501 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1502
1503 void
1504 _initialize_regcache (void)
1505 {
1506   regcache_descr_handle
1507     = gdbarch_data_register_post_init (init_regcache_descr);
1508
1509   observer_attach_target_changed (regcache_observer_target_changed);
1510   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1511
1512   add_com ("flushregs", class_maintenance, reg_flush_command,
1513            _("Force gdb to flush its register cache (maintainer command)"));
1514
1515   add_cmd ("registers", class_maintenance, maintenance_print_registers,
1516            _("Print the internal register configuration.\n"
1517              "Takes an optional file parameter."), &maintenanceprintlist);
1518   add_cmd ("raw-registers", class_maintenance,
1519            maintenance_print_raw_registers,
1520            _("Print the internal register configuration "
1521              "including raw values.\n"
1522              "Takes an optional file parameter."), &maintenanceprintlist);
1523   add_cmd ("cooked-registers", class_maintenance,
1524            maintenance_print_cooked_registers,
1525            _("Print the internal register configuration "
1526              "including cooked values.\n"
1527              "Takes an optional file parameter."), &maintenanceprintlist);
1528   add_cmd ("register-groups", class_maintenance,
1529            maintenance_print_register_groups,
1530            _("Print the internal register configuration "
1531              "including each register's group.\n"
1532              "Takes an optional file parameter."),
1533            &maintenanceprintlist);
1534   add_cmd ("remote-registers", class_maintenance,
1535            maintenance_print_remote_registers, _("\
1536 Print the internal register configuration including each register's\n\
1537 remote register number and buffer offset in the g/G packets.\n\
1538 Takes an optional file parameter."),
1539            &maintenanceprintlist);
1540
1541 }