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