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