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