Add NaCl (NativeClient) specific classes Target_mips_nacl and
[external/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
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 /* Transfer a single or all registers belonging to a certain register
1071    set to or from a buffer.  This is the main worker function for
1072    regcache_supply_regset and regcache_collect_regset.  */
1073
1074 static void
1075 regcache_transfer_regset (const struct regset *regset,
1076                           const struct regcache *regcache,
1077                           struct regcache *out_regcache,
1078                           int regnum, const void *in_buf,
1079                           void *out_buf, size_t size)
1080 {
1081   const struct regcache_map_entry *map;
1082   int offs = 0, count;
1083
1084   for (map = regset->regmap; (count = map->count) != 0; map++)
1085     {
1086       int regno = map->regno;
1087       int slot_size = map->size;
1088
1089       if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
1090         slot_size = regcache->descr->sizeof_register[regno];
1091
1092       if (regno == REGCACHE_MAP_SKIP
1093           || (regnum != -1
1094               && (regnum < regno || regnum >= regno + count)))
1095           offs += count * slot_size;
1096
1097       else if (regnum == -1)
1098         for (; count--; regno++, offs += slot_size)
1099           {
1100             if (offs + slot_size > size)
1101               break;
1102
1103             if (out_buf)
1104               regcache_raw_collect (regcache, regno,
1105                                     (gdb_byte *) out_buf + offs);
1106             else
1107               regcache_raw_supply (out_regcache, regno, in_buf
1108                                    ? (const gdb_byte *) in_buf + offs
1109                                    : NULL);
1110           }
1111       else
1112         {
1113           /* Transfer a single register and return.  */
1114           offs += (regnum - regno) * slot_size;
1115           if (offs + slot_size > size)
1116             return;
1117
1118           if (out_buf)
1119             regcache_raw_collect (regcache, regnum,
1120                                   (gdb_byte *) out_buf + offs);
1121           else
1122             regcache_raw_supply (out_regcache, regnum, in_buf
1123                                  ? (const gdb_byte *) in_buf + offs
1124                                  : NULL);
1125           return;
1126         }
1127     }
1128 }
1129
1130 /* Supply register REGNUM from BUF to REGCACHE, using the register map
1131    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
1132    If BUF is NULL, set the register(s) to "unavailable" status. */
1133
1134 void
1135 regcache_supply_regset (const struct regset *regset,
1136                         struct regcache *regcache,
1137                         int regnum, const void *buf, size_t size)
1138 {
1139   regcache_transfer_regset (regset, regcache, regcache, regnum,
1140                             buf, NULL, size);
1141 }
1142
1143 /* Collect register REGNUM from REGCACHE to BUF, using the register
1144    map in REGSET.  If REGNUM is -1, do this for all registers in
1145    REGSET.  */
1146
1147 void
1148 regcache_collect_regset (const struct regset *regset,
1149                          const struct regcache *regcache,
1150                          int regnum, void *buf, size_t size)
1151 {
1152   regcache_transfer_regset (regset, regcache, NULL, regnum,
1153                             NULL, buf, size);
1154 }
1155
1156
1157 /* Special handling for register PC.  */
1158
1159 CORE_ADDR
1160 regcache_read_pc (struct regcache *regcache)
1161 {
1162   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1163
1164   CORE_ADDR pc_val;
1165
1166   if (gdbarch_read_pc_p (gdbarch))
1167     pc_val = gdbarch_read_pc (gdbarch, regcache);
1168   /* Else use per-frame method on get_current_frame.  */
1169   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1170     {
1171       ULONGEST raw_val;
1172
1173       if (regcache_cooked_read_unsigned (regcache,
1174                                          gdbarch_pc_regnum (gdbarch),
1175                                          &raw_val) == REG_UNAVAILABLE)
1176         throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));
1177
1178       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
1179     }
1180   else
1181     internal_error (__FILE__, __LINE__,
1182                     _("regcache_read_pc: Unable to find PC"));
1183   return pc_val;
1184 }
1185
1186 void
1187 regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
1188 {
1189   struct gdbarch *gdbarch = get_regcache_arch (regcache);
1190
1191   if (gdbarch_write_pc_p (gdbarch))
1192     gdbarch_write_pc (gdbarch, regcache, pc);
1193   else if (gdbarch_pc_regnum (gdbarch) >= 0)
1194     regcache_cooked_write_unsigned (regcache,
1195                                     gdbarch_pc_regnum (gdbarch), pc);
1196   else
1197     internal_error (__FILE__, __LINE__,
1198                     _("regcache_write_pc: Unable to update PC"));
1199
1200   /* Writing the PC (for instance, from "load") invalidates the
1201      current frame.  */
1202   reinit_frame_cache ();
1203 }
1204
1205
1206 static void
1207 reg_flush_command (char *command, int from_tty)
1208 {
1209   /* Force-flush the register cache.  */
1210   registers_changed ();
1211   if (from_tty)
1212     printf_filtered (_("Register cache flushed.\n"));
1213 }
1214
1215 enum regcache_dump_what
1216 {
1217   regcache_dump_none, regcache_dump_raw,
1218   regcache_dump_cooked, regcache_dump_groups,
1219   regcache_dump_remote
1220 };
1221
1222 static void
1223 regcache_dump (struct regcache *regcache, struct ui_file *file,
1224                enum regcache_dump_what what_to_dump)
1225 {
1226   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1227   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1228   int regnum;
1229   int footnote_nr = 0;
1230   int footnote_register_size = 0;
1231   int footnote_register_offset = 0;
1232   int footnote_register_type_name_null = 0;
1233   long register_offset = 0;
1234   gdb_byte buf[MAX_REGISTER_SIZE];
1235
1236 #if 0
1237   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1238                       regcache->descr->nr_raw_registers);
1239   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1240                       regcache->descr->nr_cooked_registers);
1241   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1242                       regcache->descr->sizeof_raw_registers);
1243   fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
1244                       regcache->descr->sizeof_raw_register_status);
1245   fprintf_unfiltered (file, "gdbarch_num_regs %d\n", 
1246                       gdbarch_num_regs (gdbarch));
1247   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
1248                       gdbarch_num_pseudo_regs (gdbarch));
1249 #endif
1250
1251   gdb_assert (regcache->descr->nr_cooked_registers
1252               == (gdbarch_num_regs (gdbarch)
1253                   + gdbarch_num_pseudo_regs (gdbarch)));
1254
1255   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1256     {
1257       /* Name.  */
1258       if (regnum < 0)
1259         fprintf_unfiltered (file, " %-10s", "Name");
1260       else
1261         {
1262           const char *p = gdbarch_register_name (gdbarch, regnum);
1263
1264           if (p == NULL)
1265             p = "";
1266           else if (p[0] == '\0')
1267             p = "''";
1268           fprintf_unfiltered (file, " %-10s", p);
1269         }
1270
1271       /* Number.  */
1272       if (regnum < 0)
1273         fprintf_unfiltered (file, " %4s", "Nr");
1274       else
1275         fprintf_unfiltered (file, " %4d", regnum);
1276
1277       /* Relative number.  */
1278       if (regnum < 0)
1279         fprintf_unfiltered (file, " %4s", "Rel");
1280       else if (regnum < gdbarch_num_regs (gdbarch))
1281         fprintf_unfiltered (file, " %4d", regnum);
1282       else
1283         fprintf_unfiltered (file, " %4d",
1284                             (regnum - gdbarch_num_regs (gdbarch)));
1285
1286       /* Offset.  */
1287       if (regnum < 0)
1288         fprintf_unfiltered (file, " %6s  ", "Offset");
1289       else
1290         {
1291           fprintf_unfiltered (file, " %6ld",
1292                               regcache->descr->register_offset[regnum]);
1293           if (register_offset != regcache->descr->register_offset[regnum]
1294               || (regnum > 0
1295                   && (regcache->descr->register_offset[regnum]
1296                       != (regcache->descr->register_offset[regnum - 1]
1297                           + regcache->descr->sizeof_register[regnum - 1])))
1298               )
1299             {
1300               if (!footnote_register_offset)
1301                 footnote_register_offset = ++footnote_nr;
1302               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1303             }
1304           else
1305             fprintf_unfiltered (file, "  ");
1306           register_offset = (regcache->descr->register_offset[regnum]
1307                              + regcache->descr->sizeof_register[regnum]);
1308         }
1309
1310       /* Size.  */
1311       if (regnum < 0)
1312         fprintf_unfiltered (file, " %5s ", "Size");
1313       else
1314         fprintf_unfiltered (file, " %5ld",
1315                             regcache->descr->sizeof_register[regnum]);
1316
1317       /* Type.  */
1318       {
1319         const char *t;
1320
1321         if (regnum < 0)
1322           t = "Type";
1323         else
1324           {
1325             static const char blt[] = "builtin_type";
1326
1327             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1328             if (t == NULL)
1329               {
1330                 char *n;
1331
1332                 if (!footnote_register_type_name_null)
1333                   footnote_register_type_name_null = ++footnote_nr;
1334                 n = xstrprintf ("*%d", footnote_register_type_name_null);
1335                 make_cleanup (xfree, n);
1336                 t = n;
1337               }
1338             /* Chop a leading builtin_type.  */
1339             if (strncmp (t, blt, strlen (blt)) == 0)
1340               t += strlen (blt);
1341           }
1342         fprintf_unfiltered (file, " %-15s", t);
1343       }
1344
1345       /* Leading space always present.  */
1346       fprintf_unfiltered (file, " ");
1347
1348       /* Value, raw.  */
1349       if (what_to_dump == regcache_dump_raw)
1350         {
1351           if (regnum < 0)
1352             fprintf_unfiltered (file, "Raw value");
1353           else if (regnum >= regcache->descr->nr_raw_registers)
1354             fprintf_unfiltered (file, "<cooked>");
1355           else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
1356             fprintf_unfiltered (file, "<invalid>");
1357           else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
1358             fprintf_unfiltered (file, "<unavailable>");
1359           else
1360             {
1361               regcache_raw_read (regcache, regnum, buf);
1362               print_hex_chars (file, buf,
1363                                regcache->descr->sizeof_register[regnum],
1364                                gdbarch_byte_order (gdbarch));
1365             }
1366         }
1367
1368       /* Value, cooked.  */
1369       if (what_to_dump == regcache_dump_cooked)
1370         {
1371           if (regnum < 0)
1372             fprintf_unfiltered (file, "Cooked value");
1373           else
1374             {
1375               enum register_status status;
1376
1377               status = regcache_cooked_read (regcache, regnum, buf);
1378               if (status == REG_UNKNOWN)
1379                 fprintf_unfiltered (file, "<invalid>");
1380               else if (status == REG_UNAVAILABLE)
1381                 fprintf_unfiltered (file, "<unavailable>");
1382               else
1383                 print_hex_chars (file, buf,
1384                                  regcache->descr->sizeof_register[regnum],
1385                                  gdbarch_byte_order (gdbarch));
1386             }
1387         }
1388
1389       /* Group members.  */
1390       if (what_to_dump == regcache_dump_groups)
1391         {
1392           if (regnum < 0)
1393             fprintf_unfiltered (file, "Groups");
1394           else
1395             {
1396               const char *sep = "";
1397               struct reggroup *group;
1398
1399               for (group = reggroup_next (gdbarch, NULL);
1400                    group != NULL;
1401                    group = reggroup_next (gdbarch, group))
1402                 {
1403                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1404                     {
1405                       fprintf_unfiltered (file,
1406                                           "%s%s", sep, reggroup_name (group));
1407                       sep = ",";
1408                     }
1409                 }
1410             }
1411         }
1412
1413       /* Remote packet configuration.  */
1414       if (what_to_dump == regcache_dump_remote)
1415         {
1416           if (regnum < 0)
1417             {
1418               fprintf_unfiltered (file, "Rmt Nr  g/G Offset");
1419             }
1420           else if (regnum < regcache->descr->nr_raw_registers)
1421             {
1422               int pnum, poffset;
1423
1424               if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
1425                                                      &pnum, &poffset))
1426                 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
1427             }
1428         }
1429
1430       fprintf_unfiltered (file, "\n");
1431     }
1432
1433   if (footnote_register_size)
1434     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1435                         footnote_register_size);
1436   if (footnote_register_offset)
1437     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1438                         footnote_register_offset);
1439   if (footnote_register_type_name_null)
1440     fprintf_unfiltered (file, 
1441                         "*%d: Register type's name NULL.\n",
1442                         footnote_register_type_name_null);
1443   do_cleanups (cleanups);
1444 }
1445
1446 static void
1447 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1448 {
1449   if (args == NULL)
1450     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
1451   else
1452     {
1453       struct cleanup *cleanups;
1454       struct ui_file *file = gdb_fopen (args, "w");
1455
1456       if (file == NULL)
1457         perror_with_name (_("maintenance print architecture"));
1458       cleanups = make_cleanup_ui_file_delete (file);
1459       regcache_dump (get_current_regcache (), file, what_to_dump);
1460       do_cleanups (cleanups);
1461     }
1462 }
1463
1464 static void
1465 maintenance_print_registers (char *args, int from_tty)
1466 {
1467   regcache_print (args, regcache_dump_none);
1468 }
1469
1470 static void
1471 maintenance_print_raw_registers (char *args, int from_tty)
1472 {
1473   regcache_print (args, regcache_dump_raw);
1474 }
1475
1476 static void
1477 maintenance_print_cooked_registers (char *args, int from_tty)
1478 {
1479   regcache_print (args, regcache_dump_cooked);
1480 }
1481
1482 static void
1483 maintenance_print_register_groups (char *args, int from_tty)
1484 {
1485   regcache_print (args, regcache_dump_groups);
1486 }
1487
1488 static void
1489 maintenance_print_remote_registers (char *args, int from_tty)
1490 {
1491   regcache_print (args, regcache_dump_remote);
1492 }
1493
1494 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1495
1496 void
1497 _initialize_regcache (void)
1498 {
1499   regcache_descr_handle
1500     = gdbarch_data_register_post_init (init_regcache_descr);
1501
1502   observer_attach_target_changed (regcache_observer_target_changed);
1503   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);
1504
1505   add_com ("flushregs", class_maintenance, reg_flush_command,
1506            _("Force gdb to flush its register cache (maintainer command)"));
1507
1508   add_cmd ("registers", class_maintenance, maintenance_print_registers,
1509            _("Print the internal register configuration.\n"
1510              "Takes an optional file parameter."), &maintenanceprintlist);
1511   add_cmd ("raw-registers", class_maintenance,
1512            maintenance_print_raw_registers,
1513            _("Print the internal register configuration "
1514              "including raw values.\n"
1515              "Takes an optional file parameter."), &maintenanceprintlist);
1516   add_cmd ("cooked-registers", class_maintenance,
1517            maintenance_print_cooked_registers,
1518            _("Print the internal register configuration "
1519              "including cooked values.\n"
1520              "Takes an optional file parameter."), &maintenanceprintlist);
1521   add_cmd ("register-groups", class_maintenance,
1522            maintenance_print_register_groups,
1523            _("Print the internal register configuration "
1524              "including each register's group.\n"
1525              "Takes an optional file parameter."),
1526            &maintenanceprintlist);
1527   add_cmd ("remote-registers", class_maintenance,
1528            maintenance_print_remote_registers, _("\
1529 Print the internal register configuration including each register's\n\
1530 remote register number and buffer offset in the g/G packets.\n\
1531 Takes an optional file parameter."),
1532            &maintenanceprintlist);
1533
1534 }