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