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