* regcache.h (get_regcache_arch): New prototype.
[platform/upstream/binutils.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3    Copyright 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000,
4    2001, 2002 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., 59 Temple Place - Suite 330,
21    Boston, MA 02111-1307, 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
34 /*
35  * DATA STRUCTURE
36  *
37  * Here is the actual register cache.
38  */
39
40 /* Per-architecture object describing the layout of a register cache.
41    Computed once when the architecture is created */
42
43 struct gdbarch_data *regcache_descr_handle;
44
45 struct regcache_descr
46 {
47   /* The architecture this descriptor belongs to.  */
48   struct gdbarch *gdbarch;
49
50   /* Is this a ``legacy'' register cache?  Such caches reserve space
51      for raw and pseudo registers and allow access to both.  */
52   int legacy_p;
53
54   /* The raw register cache.  Each raw (or hard) register is supplied
55      by the target interface.  The raw cache should not contain
56      redundant information - if the PC is constructed from two
57      registers then those regigisters and not the PC lives in the raw
58      cache.  */
59   int nr_raw_registers;
60   long sizeof_raw_registers;
61   long sizeof_raw_register_valid_p;
62
63   /* The cooked register space.  Each cooked register in the range
64      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
65      register.  The remaining [NR_RAW_REGISTERS
66      .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
67      both raw registers and memory by the architecture methods
68      gdbarch_register_read and gdbarch_register_write.  */
69   int nr_cooked_registers;
70   long sizeof_cooked_registers;
71   long sizeof_cooked_register_valid_p;
72
73   /* Offset and size (in 8 bit bytes), of reach register in the
74      register cache.  All registers (including those in the range
75      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
76      Assigning all registers an offset makes it possible to keep
77      legacy code, such as that found in read_register_bytes() and
78      write_register_bytes() working.  */
79   long *register_offset;
80   long *sizeof_register;
81
82   /* Cached table containing the type of each register.  */
83   struct type **register_type;
84 };
85
86 static void
87 init_legacy_regcache_descr (struct gdbarch *gdbarch,
88                             struct regcache_descr *descr)
89 {
90   int i;
91   /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
92      ``gdbarch'' as a parameter.  */
93   gdb_assert (gdbarch != NULL);
94
95   /* Compute the offset of each register.  Legacy architectures define
96      DEPRECATED_REGISTER_BYTE() so use that.  */
97   /* FIXME: cagney/2002-11-07: Instead of using
98      DEPRECATED_REGISTER_BYTE() this code should, as is done in
99      init_regcache_descr(), compute the offets at runtime.  This
100      currently isn't possible as some ISAs define overlapping register
101      regions - see the mess in read_register_bytes() and
102      write_register_bytes() registers.  */
103   descr->sizeof_register
104     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
105   descr->register_offset
106     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
107   for (i = 0; i < descr->nr_cooked_registers; i++)
108     {
109       /* FIXME: cagney/2001-12-04: This code shouldn't need to use
110          DEPRECATED_REGISTER_BYTE().  Unfortunatly, legacy code likes
111          to lay the buffer out so that certain registers just happen
112          to overlap.  Ulgh!  New targets use gdbarch's register
113          read/write and entirely avoid this uglyness.  */
114       descr->register_offset[i] = DEPRECATED_REGISTER_BYTE (i);
115       descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
116       gdb_assert (MAX_REGISTER_SIZE >= REGISTER_RAW_SIZE (i));
117       gdb_assert (MAX_REGISTER_SIZE >= REGISTER_VIRTUAL_SIZE (i));
118     }
119
120   /* Compute the real size of the register buffer.  Start out by
121      trusting DEPRECATED_REGISTER_BYTES, but then adjust it upwards
122      should that be found to not be sufficient.  */
123   /* FIXME: cagney/2002-11-05: Instead of using the macro
124      DEPRECATED_REGISTER_BYTES, this code should, as is done in
125      init_regcache_descr(), compute the total number of register bytes
126      using the accumulated offsets.  */
127   descr->sizeof_cooked_registers = DEPRECATED_REGISTER_BYTES; /* OK */
128   for (i = 0; i < descr->nr_cooked_registers; i++)
129     {
130       long regend;
131       /* Keep extending the buffer so that there is always enough
132          space for all registers.  The comparison is necessary since
133          legacy code is free to put registers in random places in the
134          buffer separated by holes.  Once DEPRECATED_REGISTER_BYTE()
135          is killed this can be greatly simplified.  */
136       regend = descr->register_offset[i] + descr->sizeof_register[i];
137       if (descr->sizeof_cooked_registers < regend)
138         descr->sizeof_cooked_registers = regend;
139     }
140   /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
141      in the register cache.  Unfortunatly some architectures still
142      rely on this and the pseudo_register_write() method.  */
143   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
144 }
145
146 static void *
147 init_regcache_descr (struct gdbarch *gdbarch)
148 {
149   int i;
150   struct regcache_descr *descr;
151   gdb_assert (gdbarch != NULL);
152
153   /* Create an initial, zero filled, table.  */
154   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
155   descr->gdbarch = gdbarch;
156
157   /* Total size of the register space.  The raw registers are mapped
158      directly onto the raw register cache while the pseudo's are
159      either mapped onto raw-registers or memory.  */
160   descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
161   descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
162
163   /* Fill in a table of register types.  */
164   descr->register_type
165     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
166   for (i = 0; i < descr->nr_cooked_registers; i++)
167     {
168       if (gdbarch_register_type_p (gdbarch))
169         {
170           gdb_assert (!REGISTER_VIRTUAL_TYPE_P ()); /* OK */
171           descr->register_type[i] = gdbarch_register_type (gdbarch, i);
172         }
173       else
174         descr->register_type[i] = REGISTER_VIRTUAL_TYPE (i); /* OK */
175     }
176
177   /* Construct a strictly RAW register cache.  Don't allow pseudo's
178      into the register cache.  */
179   descr->nr_raw_registers = NUM_REGS;
180
181   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
182      array.  This pretects GDB from erant code that accesses elements
183      of the global register_valid_p[] array in the range [NUM_REGS
184      .. NUM_REGS + NUM_PSEUDO_REGS).  */
185   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
186
187   /* If an old style architecture, fill in the remainder of the
188      register cache descriptor using the register macros.  */
189   /* NOTE: cagney/2003-06-29: If either of DEPRECATED_REGISTER_BYTE or
190      REGISTER_RAW_SIZE are still present, things are most likely
191      totally screwed.  Ex: an architecture with raw register sizes
192      smaller than what DEPRECATED_REGISTER_BYTE indicates; non
193      monotonic DEPRECATED_REGISTER_BYTE values.  For GDB 6 check for
194      these nasty methods and fall back to legacy code when present.
195      Sigh!  */
196   if ((!gdbarch_pseudo_register_read_p (gdbarch)
197        && !gdbarch_pseudo_register_write_p (gdbarch)
198        && !gdbarch_register_type_p (gdbarch))
199       || DEPRECATED_REGISTER_BYTE_P () || REGISTER_RAW_SIZE_P ())
200     {
201       descr->legacy_p = 1;
202       init_legacy_regcache_descr (gdbarch, descr);
203       return descr;
204     }
205
206   /* Lay out the register cache.
207
208      NOTE: cagney/2002-05-22: Only register_type() is used when
209      constructing the register cache.  It is assumed that the
210      register's raw size, virtual size and type length are all the
211      same.  */
212
213   {
214     long offset = 0;
215     descr->sizeof_register
216       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
217     descr->register_offset
218       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
219     for (i = 0; i < descr->nr_cooked_registers; i++)
220       {
221         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
222         descr->register_offset[i] = offset;
223         offset += descr->sizeof_register[i];
224         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
225       }
226     /* Set the real size of the register cache buffer.  */
227     descr->sizeof_cooked_registers = offset;
228   }
229
230   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
231      the raw registers.  Unfortunatly some code still accesses the
232      register array directly using the global registers[].  Until that
233      code has been purged, play safe and over allocating the register
234      buffer.  Ulgh!  */
235   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
236
237   /* Sanity check.  Confirm that there is agreement between the
238      regcache and the target's redundant DEPRECATED_REGISTER_BYTE (new
239      targets should not even be defining it).  */
240   for (i = 0; i < descr->nr_cooked_registers; i++)
241     {
242       if (DEPRECATED_REGISTER_BYTE_P ())
243         gdb_assert (descr->register_offset[i] == DEPRECATED_REGISTER_BYTE (i));
244 #if 0
245       gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
246       gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
247 #endif
248     }
249   /* gdb_assert (descr->sizeof_raw_registers == DEPRECATED_REGISTER_BYTES (i));  */
250
251   return descr;
252 }
253
254 static struct regcache_descr *
255 regcache_descr (struct gdbarch *gdbarch)
256 {
257   return gdbarch_data (gdbarch, regcache_descr_handle);
258 }
259
260 /* Utility functions returning useful register attributes stored in
261    the regcache descr.  */
262
263 struct type *
264 register_type (struct gdbarch *gdbarch, int regnum)
265 {
266   struct regcache_descr *descr = regcache_descr (gdbarch);
267   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
268   return descr->register_type[regnum];
269 }
270
271 /* Utility functions returning useful register attributes stored in
272    the regcache descr.  */
273
274 int
275 register_size (struct gdbarch *gdbarch, int regnum)
276 {
277   struct regcache_descr *descr = regcache_descr (gdbarch);
278   int size;
279   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
280   size = descr->sizeof_register[regnum];
281   /* NB: The deprecated REGISTER_RAW_SIZE, if not provided, defaults
282      to the size of the register's type.  */
283   gdb_assert (size == REGISTER_RAW_SIZE (regnum)); /* OK */
284   /* NB: Don't check the register's virtual size.  It, in say the case
285      of the MIPS, may not match the raw size!  */
286   return size;
287 }
288
289 /* The register cache for storing raw register values.  */
290
291 struct regcache
292 {
293   struct regcache_descr *descr;
294   /* The register buffers.  A read-only register cache can hold the
295      full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
296      register cache can only hold [0 .. NUM_REGS).  */
297   char *registers;
298   char *register_valid_p;
299   /* Is this a read-only cache?  A read-only cache is used for saving
300      the target's register state (e.g, across an inferior function
301      call or just before forcing a function return).  A read-only
302      cache can only be updated via the methods regcache_dup() and
303      regcache_cpy().  The actual contents are determined by the
304      reggroup_save and reggroup_restore methods.  */
305   int readonly_p;
306 };
307
308 struct regcache *
309 regcache_xmalloc (struct gdbarch *gdbarch)
310 {
311   struct regcache_descr *descr;
312   struct regcache *regcache;
313   gdb_assert (gdbarch != NULL);
314   descr = regcache_descr (gdbarch);
315   regcache = XMALLOC (struct regcache);
316   regcache->descr = descr;
317   regcache->registers
318     = XCALLOC (descr->sizeof_raw_registers, char);
319   regcache->register_valid_p
320     = XCALLOC (descr->sizeof_raw_register_valid_p, char);
321   regcache->readonly_p = 1;
322   return regcache;
323 }
324
325 void
326 regcache_xfree (struct regcache *regcache)
327 {
328   if (regcache == NULL)
329     return;
330   xfree (regcache->registers);
331   xfree (regcache->register_valid_p);
332   xfree (regcache);
333 }
334
335 static void
336 do_regcache_xfree (void *data)
337 {
338   regcache_xfree (data);
339 }
340
341 struct cleanup *
342 make_cleanup_regcache_xfree (struct regcache *regcache)
343 {
344   return make_cleanup (do_regcache_xfree, regcache);
345 }
346
347 /* Return REGCACHE's architecture.  */
348
349 struct gdbarch *
350 get_regcache_arch (const struct regcache *regcache)
351 {
352   return regcache->descr->gdbarch;
353 }
354
355 /* Return  a pointer to register REGNUM's buffer cache.  */
356
357 static char *
358 register_buffer (const struct regcache *regcache, int regnum)
359 {
360   return regcache->registers + regcache->descr->register_offset[regnum];
361 }
362
363 void
364 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
365                void *src)
366 {
367   struct gdbarch *gdbarch = dst->descr->gdbarch;
368   char buf[MAX_REGISTER_SIZE];
369   int regnum;
370   /* The DST should be `read-only', if it wasn't then the save would
371      end up trying to write the register values back out to the
372      target.  */
373   gdb_assert (dst->readonly_p);
374   /* Clear the dest.  */
375   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
376   memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
377   /* Copy over any registers (identified by their membership in the
378      save_reggroup) and mark them as valid.  The full [0 .. NUM_REGS +
379      NUM_PSEUDO_REGS) range is checked since some architectures need
380      to save/restore `cooked' registers that live in memory.  */
381   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
382     {
383       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
384         {
385           int valid = cooked_read (src, regnum, buf);
386           if (valid)
387             {
388               memcpy (register_buffer (dst, regnum), buf,
389                       register_size (gdbarch, regnum));
390               dst->register_valid_p[regnum] = 1;
391             }
392         }
393     }
394 }
395
396 void
397 regcache_restore (struct regcache *dst,
398                   regcache_cooked_read_ftype *cooked_read,
399                   void *src)
400 {
401   struct gdbarch *gdbarch = dst->descr->gdbarch;
402   char buf[MAX_REGISTER_SIZE];
403   int regnum;
404   /* The dst had better not be read-only.  If it is, the `restore'
405      doesn't make much sense.  */
406   gdb_assert (!dst->readonly_p);
407   /* Copy over any registers, being careful to only restore those that
408      were both saved and need to be restored.  The full [0 .. NUM_REGS
409      + NUM_PSEUDO_REGS) range is checked since some architectures need
410      to save/restore `cooked' registers that live in memory.  */
411   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
412     {
413       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
414         {
415           int valid = cooked_read (src, regnum, buf);
416           if (valid)
417             regcache_cooked_write (dst, regnum, buf);
418         }
419     }
420 }
421
422 static int
423 do_cooked_read (void *src, int regnum, void *buf)
424 {
425   struct regcache *regcache = src;
426   if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
427     /* Don't even think about fetching a register from a read-only
428        cache when the register isn't yet valid.  There isn't a target
429        from which the register value can be fetched.  */
430     return 0;
431   regcache_cooked_read (regcache, regnum, buf);
432   return 1;
433 }
434
435
436 void
437 regcache_cpy (struct regcache *dst, struct regcache *src)
438 {
439   int i;
440   char *buf;
441   gdb_assert (src != NULL && dst != NULL);
442   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
443   gdb_assert (src != dst);
444   gdb_assert (src->readonly_p || dst->readonly_p);
445   if (!src->readonly_p)
446     regcache_save (dst, do_cooked_read, src);
447   else if (!dst->readonly_p)
448     regcache_restore (dst, do_cooked_read, src);
449   else
450     regcache_cpy_no_passthrough (dst, src);
451 }
452
453 void
454 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
455 {
456   int i;
457   gdb_assert (src != NULL && dst != NULL);
458   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
459   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
460      move of data into the current_regcache().  Doing this would be
461      silly - it would mean that valid_p would be completly invalid.  */
462   gdb_assert (dst != current_regcache);
463   memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
464   memcpy (dst->register_valid_p, src->register_valid_p,
465           dst->descr->sizeof_raw_register_valid_p);
466 }
467
468 struct regcache *
469 regcache_dup (struct regcache *src)
470 {
471   struct regcache *newbuf;
472   gdb_assert (current_regcache != NULL);
473   newbuf = regcache_xmalloc (src->descr->gdbarch);
474   regcache_cpy (newbuf, src);
475   return newbuf;
476 }
477
478 struct regcache *
479 regcache_dup_no_passthrough (struct regcache *src)
480 {
481   struct regcache *newbuf;
482   gdb_assert (current_regcache != NULL);
483   newbuf = regcache_xmalloc (src->descr->gdbarch);
484   regcache_cpy_no_passthrough (newbuf, src);
485   return newbuf;
486 }
487
488 int
489 regcache_valid_p (struct regcache *regcache, int regnum)
490 {
491   gdb_assert (regcache != NULL);
492   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
493   return regcache->register_valid_p[regnum];
494 }
495
496 char *
497 deprecated_grub_regcache_for_registers (struct regcache *regcache)
498 {
499   return regcache->registers;
500 }
501
502 /* Global structure containing the current regcache.  */
503 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
504    deprecated_register_valid[] currently point into this structure.  */
505 struct regcache *current_regcache;
506
507 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
508    recording if the register values have been changed (eg. by the
509    user).  Therefore all registers must be written back to the
510    target when appropriate.  */
511
512 /* REGISTERS contains the cached register values (in target byte order). */
513
514 char *deprecated_registers;
515
516 /* DEPRECATED_REGISTER_VALID is 0 if the register needs to be fetched,
517                      1 if it has been fetched, and
518                     -1 if the register value was not available.  
519
520    "Not available" indicates that the target is not not able to supply
521    the register at this state.  The register may become available at a
522    later time (after the next resume).  This often occures when GDB is
523    manipulating a target that contains only a snapshot of the entire
524    system being debugged - some of the registers in such a system may
525    not have been saved.  */
526
527 signed char *deprecated_register_valid;
528
529 /* The thread/process associated with the current set of registers. */
530
531 static ptid_t registers_ptid;
532
533 /*
534  * FUNCTIONS:
535  */
536
537 /* REGISTER_CACHED()
538
539    Returns 0 if the value is not in the cache (needs fetch).
540           >0 if the value is in the cache.
541           <0 if the value is permanently unavailable (don't ask again).  */
542
543 int
544 register_cached (int regnum)
545 {
546   return deprecated_register_valid[regnum];
547 }
548
549 /* Record that REGNUM's value is cached if STATE is >0, uncached but
550    fetchable if STATE is 0, and uncached and unfetchable if STATE is <0.  */
551
552 void
553 set_register_cached (int regnum, int state)
554 {
555   gdb_assert (regnum >= 0);
556   gdb_assert (regnum < current_regcache->descr->nr_raw_registers);
557   current_regcache->register_valid_p[regnum] = state;
558 }
559
560 /* Return whether register REGNUM is a real register.  */
561
562 static int
563 real_register (int regnum)
564 {
565   return regnum >= 0 && regnum < NUM_REGS;
566 }
567
568 /* Low level examining and depositing of registers.
569
570    The caller is responsible for making sure that the inferior is
571    stopped before calling the fetching routines, or it will get
572    garbage.  (a change from GDB version 3, in which the caller got the
573    value from the last stop).  */
574
575 /* REGISTERS_CHANGED ()
576
577    Indicate that registers may have changed, so invalidate the cache.  */
578
579 void
580 registers_changed (void)
581 {
582   int i;
583
584   registers_ptid = pid_to_ptid (-1);
585
586   /* Force cleanup of any alloca areas if using C alloca instead of
587      a builtin alloca.  This particular call is used to clean up
588      areas allocated by low level target code which may build up
589      during lengthy interactions between gdb and the target before
590      gdb gives control to the user (ie watchpoints).  */
591   alloca (0);
592
593   for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
594     set_register_cached (i, 0);
595
596   if (registers_changed_hook)
597     registers_changed_hook ();
598 }
599
600 /* DEPRECATED_REGISTERS_FETCHED ()
601
602    Indicate that all registers have been fetched, so mark them all valid.  */
603
604 /* NOTE: cagney/2001-12-04: This function does not set valid on the
605    pseudo-register range since pseudo registers are always supplied
606    using supply_register().  */
607 /* FIXME: cagney/2001-12-04: This function is DEPRECATED.  The target
608    code was blatting the registers[] array and then calling this.
609    Since targets should only be using supply_register() the need for
610    this function/hack is eliminated.  */
611
612 void
613 deprecated_registers_fetched (void)
614 {
615   int i;
616
617   for (i = 0; i < NUM_REGS; i++)
618     set_register_cached (i, 1);
619   /* Do not assume that the pseudo-regs have also been fetched.
620      Fetching all real regs NEVER accounts for pseudo-regs.  */
621 }
622
623 /* deprecated_read_register_bytes and deprecated_write_register_bytes
624    are generally a *BAD* idea.  They are inefficient because they need
625    to check for partial updates, which can only be done by scanning
626    through all of the registers and seeing if the bytes that are being
627    read/written fall inside of an invalid register.  [The main reason
628    this is necessary is that register sizes can vary, so a simple
629    index won't suffice.]  It is far better to call read_register_gen
630    and write_register_gen if you want to get at the raw register
631    contents, as it only takes a regnum as an argument, and therefore
632    can't do a partial register update.
633
634    Prior to the recent fixes to check for partial updates, both read
635    and deprecated_write_register_bytes always checked to see if any
636    registers were stale, and then called target_fetch_registers (-1)
637    to update the whole set.  This caused really slowed things down for
638    remote targets.  */
639
640 /* Copy INLEN bytes of consecutive data from registers
641    starting with the INREGBYTE'th byte of register data
642    into memory at MYADDR.  */
643
644 void
645 deprecated_read_register_bytes (int in_start, char *in_buf, int in_len)
646 {
647   int in_end = in_start + in_len;
648   int regnum;
649   char reg_buf[MAX_REGISTER_SIZE];
650
651   /* See if we are trying to read bytes from out-of-date registers.  If so,
652      update just those registers.  */
653
654   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
655     {
656       int reg_start;
657       int reg_end;
658       int reg_len;
659       int start;
660       int end;
661       int byte;
662
663       reg_start = DEPRECATED_REGISTER_BYTE (regnum);
664       reg_len = REGISTER_RAW_SIZE (regnum);
665       reg_end = reg_start + reg_len;
666
667       if (reg_end <= in_start || in_end <= reg_start)
668         /* The range the user wants to read doesn't overlap with regnum.  */
669         continue;
670
671       if (REGISTER_NAME (regnum) != NULL && *REGISTER_NAME (regnum) != '\0')
672         /* Force the cache to fetch the entire register.  */
673         deprecated_read_register_gen (regnum, reg_buf);
674       else
675         /* Legacy note: even though this register is ``invalid'' we
676            still need to return something.  It would appear that some
677            code relies on apparent gaps in the register array also
678            being returned.  */
679         /* FIXME: cagney/2001-08-18: This is just silly.  It defeats
680            the entire register read/write flow of control.  Must
681            resist temptation to return 0xdeadbeef.  */
682         memcpy (reg_buf, &deprecated_registers[reg_start], reg_len);
683
684       /* Legacy note: This function, for some reason, allows a NULL
685          input buffer.  If the buffer is NULL, the registers are still
686          fetched, just the final transfer is skipped. */
687       if (in_buf == NULL)
688         continue;
689
690       /* start = max (reg_start, in_start) */
691       if (reg_start > in_start)
692         start = reg_start;
693       else
694         start = in_start;
695
696       /* end = min (reg_end, in_end) */
697       if (reg_end < in_end)
698         end = reg_end;
699       else
700         end = in_end;
701
702       /* Transfer just the bytes common to both IN_BUF and REG_BUF */
703       for (byte = start; byte < end; byte++)
704         {
705           in_buf[byte - in_start] = reg_buf[byte - reg_start];
706         }
707     }
708 }
709
710 /* Read register REGNUM into memory at MYADDR, which must be large
711    enough for REGISTER_RAW_BYTES (REGNUM).  Target byte-order.  If the
712    register is known to be the size of a CORE_ADDR or smaller,
713    read_register can be used instead.  */
714
715 static void
716 legacy_read_register_gen (int regnum, char *myaddr)
717 {
718   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
719   if (! ptid_equal (registers_ptid, inferior_ptid))
720     {
721       registers_changed ();
722       registers_ptid = inferior_ptid;
723     }
724
725   if (!register_cached (regnum))
726     target_fetch_registers (regnum);
727
728   memcpy (myaddr, register_buffer (current_regcache, regnum),
729           REGISTER_RAW_SIZE (regnum));
730 }
731
732 void
733 regcache_raw_read (struct regcache *regcache, int regnum, void *buf)
734 {
735   gdb_assert (regcache != NULL && buf != NULL);
736   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
737   if (regcache->descr->legacy_p
738       && !regcache->readonly_p)
739     {
740       gdb_assert (regcache == current_regcache);
741       /* For moment, just use underlying legacy code.  Ulgh!!! This
742          silently and very indirectly updates the regcache's regcache
743          via the global deprecated_register_valid[].  */
744       legacy_read_register_gen (regnum, buf);
745       return;
746     }
747   /* Make certain that the register cache is up-to-date with respect
748      to the current thread.  This switching shouldn't be necessary
749      only there is still only one target side register cache.  Sigh!
750      On the bright side, at least there is a regcache object.  */
751   if (!regcache->readonly_p)
752     {
753       gdb_assert (regcache == current_regcache);
754       if (! ptid_equal (registers_ptid, inferior_ptid))
755         {
756           registers_changed ();
757           registers_ptid = inferior_ptid;
758         }
759       if (!register_cached (regnum))
760         target_fetch_registers (regnum);
761     }
762   /* Copy the value directly into the register cache.  */
763   memcpy (buf, register_buffer (regcache, regnum),
764           regcache->descr->sizeof_register[regnum]);
765 }
766
767 void
768 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
769 {
770   char *buf;
771   gdb_assert (regcache != NULL);
772   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
773   buf = alloca (regcache->descr->sizeof_register[regnum]);
774   regcache_raw_read (regcache, regnum, buf);
775   (*val) = extract_signed_integer (buf,
776                                    regcache->descr->sizeof_register[regnum]);
777 }
778
779 void
780 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
781                             ULONGEST *val)
782 {
783   char *buf;
784   gdb_assert (regcache != NULL);
785   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
786   buf = alloca (regcache->descr->sizeof_register[regnum]);
787   regcache_raw_read (regcache, regnum, buf);
788   (*val) = extract_unsigned_integer (buf,
789                                      regcache->descr->sizeof_register[regnum]);
790 }
791
792 void
793 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
794 {
795   void *buf;
796   gdb_assert (regcache != NULL);
797   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
798   buf = alloca (regcache->descr->sizeof_register[regnum]);
799   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
800   regcache_raw_write (regcache, regnum, buf);
801 }
802
803 void
804 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
805                              ULONGEST val)
806 {
807   void *buf;
808   gdb_assert (regcache != NULL);
809   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
810   buf = alloca (regcache->descr->sizeof_register[regnum]);
811   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
812   regcache_raw_write (regcache, regnum, buf);
813 }
814
815 void
816 deprecated_read_register_gen (int regnum, char *buf)
817 {
818   gdb_assert (current_regcache != NULL);
819   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
820   if (current_regcache->descr->legacy_p)
821     {
822       legacy_read_register_gen (regnum, buf);
823       return;
824     }
825   regcache_cooked_read (current_regcache, regnum, buf);
826 }
827
828 void
829 regcache_cooked_read (struct regcache *regcache, int regnum, void *buf)
830 {
831   gdb_assert (regnum >= 0);
832   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
833   if (regnum < regcache->descr->nr_raw_registers)
834     regcache_raw_read (regcache, regnum, buf);
835   else if (regcache->readonly_p
836            && regnum < regcache->descr->nr_cooked_registers
837            && regcache->register_valid_p[regnum])
838     /* Read-only register cache, perhaphs the cooked value was cached?  */
839     memcpy (buf, register_buffer (regcache, regnum),
840             regcache->descr->sizeof_register[regnum]);
841   else
842     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
843                                   regnum, buf);
844 }
845
846 void
847 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
848                              LONGEST *val)
849 {
850   char *buf;
851   gdb_assert (regcache != NULL);
852   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
853   buf = alloca (regcache->descr->sizeof_register[regnum]);
854   regcache_cooked_read (regcache, regnum, buf);
855   (*val) = extract_signed_integer (buf,
856                                    regcache->descr->sizeof_register[regnum]);
857 }
858
859 void
860 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
861                                ULONGEST *val)
862 {
863   char *buf;
864   gdb_assert (regcache != NULL);
865   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
866   buf = alloca (regcache->descr->sizeof_register[regnum]);
867   regcache_cooked_read (regcache, regnum, buf);
868   (*val) = extract_unsigned_integer (buf,
869                                      regcache->descr->sizeof_register[regnum]);
870 }
871
872 void
873 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
874                               LONGEST val)
875 {
876   void *buf;
877   gdb_assert (regcache != NULL);
878   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
879   buf = alloca (regcache->descr->sizeof_register[regnum]);
880   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
881   regcache_cooked_write (regcache, regnum, buf);
882 }
883
884 void
885 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
886                                 ULONGEST val)
887 {
888   void *buf;
889   gdb_assert (regcache != NULL);
890   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
891   buf = alloca (regcache->descr->sizeof_register[regnum]);
892   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
893   regcache_cooked_write (regcache, regnum, buf);
894 }
895
896 /* Write register REGNUM at MYADDR to the target.  MYADDR points at
897    REGISTER_RAW_BYTES(REGNUM), which must be in target byte-order.  */
898
899 static void
900 legacy_write_register_gen (int regnum, const void *myaddr)
901 {
902   int size;
903   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
904
905   /* On the sparc, writing %g0 is a no-op, so we don't even want to
906      change the registers array if something writes to this register.  */
907   if (CANNOT_STORE_REGISTER (regnum))
908     return;
909
910   if (! ptid_equal (registers_ptid, inferior_ptid))
911     {
912       registers_changed ();
913       registers_ptid = inferior_ptid;
914     }
915
916   size = REGISTER_RAW_SIZE (regnum);
917
918   if (real_register (regnum))
919     {
920       /* If we have a valid copy of the register, and new value == old
921          value, then don't bother doing the actual store. */
922       if (register_cached (regnum)
923           && (memcmp (register_buffer (current_regcache, regnum), myaddr, size)
924               == 0))
925         return;
926       else
927         target_prepare_to_store ();
928     }
929
930   memcpy (register_buffer (current_regcache, regnum), myaddr, size);
931
932   set_register_cached (regnum, 1);
933   target_store_registers (regnum);
934 }
935
936 void
937 regcache_raw_write (struct regcache *regcache, int regnum, const void *buf)
938 {
939   gdb_assert (regcache != NULL && buf != NULL);
940   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
941   gdb_assert (!regcache->readonly_p);
942
943   if (regcache->descr->legacy_p)
944     {
945       /* For moment, just use underlying legacy code.  Ulgh!!! This
946          silently and very indirectly updates the regcache's buffers
947          via the globals deprecated_register_valid[] and registers[].  */
948       gdb_assert (regcache == current_regcache);
949       legacy_write_register_gen (regnum, buf);
950       return;
951     }
952
953   /* On the sparc, writing %g0 is a no-op, so we don't even want to
954      change the registers array if something writes to this register.  */
955   if (CANNOT_STORE_REGISTER (regnum))
956     return;
957
958   /* Make certain that the correct cache is selected.  */
959   gdb_assert (regcache == current_regcache);
960   if (! ptid_equal (registers_ptid, inferior_ptid))
961     {
962       registers_changed ();
963       registers_ptid = inferior_ptid;
964     }
965
966   /* If we have a valid copy of the register, and new value == old
967      value, then don't bother doing the actual store. */
968   if (regcache_valid_p (regcache, regnum)
969       && (memcmp (register_buffer (regcache, regnum), buf,
970                   regcache->descr->sizeof_register[regnum]) == 0))
971     return;
972
973   target_prepare_to_store ();
974   memcpy (register_buffer (regcache, regnum), buf,
975           regcache->descr->sizeof_register[regnum]);
976   regcache->register_valid_p[regnum] = 1;
977   target_store_registers (regnum);
978 }
979
980 void
981 deprecated_write_register_gen (int regnum, char *buf)
982 {
983   gdb_assert (current_regcache != NULL);
984   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
985   if (current_regcache->descr->legacy_p)
986     {
987       legacy_write_register_gen (regnum, buf);
988       return;
989     }
990   regcache_cooked_write (current_regcache, regnum, buf);
991 }
992
993 void
994 regcache_cooked_write (struct regcache *regcache, int regnum, const void *buf)
995 {
996   gdb_assert (regnum >= 0);
997   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
998   if (regnum < regcache->descr->nr_raw_registers)
999     regcache_raw_write (regcache, regnum, buf);
1000   else
1001     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
1002                                    regnum, buf);
1003 }
1004
1005 /* Copy INLEN bytes of consecutive data from memory at MYADDR
1006    into registers starting with the MYREGSTART'th byte of register data.  */
1007
1008 void
1009 deprecated_write_register_bytes (int myregstart, char *myaddr, int inlen)
1010 {
1011   int myregend = myregstart + inlen;
1012   int regnum;
1013
1014   target_prepare_to_store ();
1015
1016   /* Scan through the registers updating any that are covered by the
1017      range myregstart<=>myregend using write_register_gen, which does
1018      nice things like handling threads, and avoiding updates when the
1019      new and old contents are the same.  */
1020
1021   for (regnum = 0; regnum < NUM_REGS + NUM_PSEUDO_REGS; regnum++)
1022     {
1023       int regstart, regend;
1024
1025       regstart = DEPRECATED_REGISTER_BYTE (regnum);
1026       regend = regstart + REGISTER_RAW_SIZE (regnum);
1027
1028       /* Is this register completely outside the range the user is writing?  */
1029       if (myregend <= regstart || regend <= myregstart)
1030         /* do nothing */ ;              
1031
1032       /* Is this register completely within the range the user is writing?  */
1033       else if (myregstart <= regstart && regend <= myregend)
1034         deprecated_write_register_gen (regnum, myaddr + (regstart - myregstart));
1035
1036       /* The register partially overlaps the range being written.  */
1037       else
1038         {
1039           char regbuf[MAX_REGISTER_SIZE];
1040           /* What's the overlap between this register's bytes and
1041              those the caller wants to write?  */
1042           int overlapstart = max (regstart, myregstart);
1043           int overlapend   = min (regend,   myregend);
1044
1045           /* We may be doing a partial update of an invalid register.
1046              Update it from the target before scribbling on it.  */
1047           deprecated_read_register_gen (regnum, regbuf);
1048
1049           memcpy (&deprecated_registers[overlapstart],
1050                   myaddr + (overlapstart - myregstart),
1051                   overlapend - overlapstart);
1052
1053           target_store_registers (regnum);
1054         }
1055     }
1056 }
1057
1058 /* Perform a partial register transfer using a read, modify, write
1059    operation.  */
1060
1061 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
1062                                     void *buf);
1063 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
1064                                      const void *buf);
1065
1066 static void
1067 regcache_xfer_part (struct regcache *regcache, int regnum,
1068                     int offset, int len, void *in, const void *out,
1069                     regcache_read_ftype *read, regcache_write_ftype *write)
1070 {
1071   struct regcache_descr *descr = regcache->descr;
1072   bfd_byte reg[MAX_REGISTER_SIZE];
1073   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
1074   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
1075   /* Something to do?  */
1076   if (offset + len == 0)
1077     return;
1078   /* Read (when needed) ... */
1079   if (in != NULL
1080       || offset > 0
1081       || offset + len < descr->sizeof_register[regnum])
1082     {
1083       gdb_assert (read != NULL);
1084       read (regcache, regnum, reg);
1085     }
1086   /* ... modify ... */
1087   if (in != NULL)
1088     memcpy (in, reg + offset, len);
1089   if (out != NULL)
1090     memcpy (reg + offset, out, len);
1091   /* ... write (when needed).  */
1092   if (out != NULL)
1093     {
1094       gdb_assert (write != NULL);
1095       write (regcache, regnum, reg);
1096     }
1097 }
1098
1099 void
1100 regcache_raw_read_part (struct regcache *regcache, int regnum,
1101                         int offset, int len, void *buf)
1102 {
1103   struct regcache_descr *descr = regcache->descr;
1104   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1105   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1106                       regcache_raw_read, regcache_raw_write);
1107 }
1108
1109 void
1110 regcache_raw_write_part (struct regcache *regcache, int regnum,
1111                          int offset, int len, const void *buf)
1112 {
1113   struct regcache_descr *descr = regcache->descr;
1114   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
1115   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1116                       regcache_raw_read, regcache_raw_write);
1117 }
1118
1119 void
1120 regcache_cooked_read_part (struct regcache *regcache, int regnum,
1121                            int offset, int len, void *buf)
1122 {
1123   struct regcache_descr *descr = regcache->descr;
1124   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1125   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
1126                       regcache_cooked_read, regcache_cooked_write);
1127 }
1128
1129 void
1130 regcache_cooked_write_part (struct regcache *regcache, int regnum,
1131                             int offset, int len, const void *buf)
1132 {
1133   struct regcache_descr *descr = regcache->descr;
1134   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1135   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
1136                       regcache_cooked_read, regcache_cooked_write);
1137 }
1138
1139 /* Hack to keep code that view the register buffer as raw bytes
1140    working.  */
1141
1142 int
1143 register_offset_hack (struct gdbarch *gdbarch, int regnum)
1144 {
1145   struct regcache_descr *descr = regcache_descr (gdbarch);
1146   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
1147   return descr->register_offset[regnum];
1148 }
1149
1150 /* Return the contents of register REGNUM as an unsigned integer.  */
1151
1152 ULONGEST
1153 read_register (int regnum)
1154 {
1155   char *buf = alloca (REGISTER_RAW_SIZE (regnum));
1156   deprecated_read_register_gen (regnum, buf);
1157   return (extract_unsigned_integer (buf, REGISTER_RAW_SIZE (regnum)));
1158 }
1159
1160 ULONGEST
1161 read_register_pid (int regnum, ptid_t ptid)
1162 {
1163   ptid_t save_ptid;
1164   int save_pid;
1165   CORE_ADDR retval;
1166
1167   if (ptid_equal (ptid, inferior_ptid))
1168     return read_register (regnum);
1169
1170   save_ptid = inferior_ptid;
1171
1172   inferior_ptid = ptid;
1173
1174   retval = read_register (regnum);
1175
1176   inferior_ptid = save_ptid;
1177
1178   return retval;
1179 }
1180
1181 /* Store VALUE into the raw contents of register number REGNUM.  */
1182
1183 void
1184 write_register (int regnum, LONGEST val)
1185 {
1186   void *buf;
1187   int size;
1188   size = REGISTER_RAW_SIZE (regnum);
1189   buf = alloca (size);
1190   store_signed_integer (buf, size, (LONGEST) val);
1191   deprecated_write_register_gen (regnum, buf);
1192 }
1193
1194 void
1195 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
1196 {
1197   ptid_t save_ptid;
1198
1199   if (ptid_equal (ptid, inferior_ptid))
1200     {
1201       write_register (regnum, val);
1202       return;
1203     }
1204
1205   save_ptid = inferior_ptid;
1206
1207   inferior_ptid = ptid;
1208
1209   write_register (regnum, val);
1210
1211   inferior_ptid = save_ptid;
1212 }
1213
1214 /* FIXME: kettenis/20030828: We should get rid of supply_register and
1215    regcache_collect in favour of regcache_raw_supply and
1216    regcache_raw_collect.  */
1217
1218 /* SUPPLY_REGISTER()
1219
1220    Record that register REGNUM contains VAL.  This is used when the
1221    value is obtained from the inferior or core dump, so there is no
1222    need to store the value there.
1223
1224    If VAL is a NULL pointer, then it's probably an unsupported register.
1225    We just set its value to all zeros.  We might want to record this
1226    fact, and report it to the users of read_register and friends.  */
1227
1228 void
1229 supply_register (int regnum, const void *val)
1230 {
1231   regcache_raw_supply (current_regcache, regnum, val);
1232
1233   /* On some architectures, e.g. HPPA, there are a few stray bits in
1234      some registers, that the rest of the code would like to ignore.  */
1235
1236   /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1237      going to be deprecated.  Instead architectures will leave the raw
1238      register value as is and instead clean things up as they pass
1239      through the method gdbarch_pseudo_register_read() clean up the
1240      values. */
1241
1242 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1243   DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1244     (regnum, register_buffer (current_regcache, regnum));
1245 #endif
1246 }
1247
1248 void
1249 regcache_collect (int regnum, void *buf)
1250 {
1251   regcache_raw_collect (current_regcache, regnum, buf);
1252 }
1253
1254 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
1255
1256 void
1257 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
1258 {
1259   void *regbuf;
1260   size_t size;
1261
1262   gdb_assert (regcache != NULL);
1263   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1264   gdb_assert (!regcache->readonly_p);
1265
1266   /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
1267      CURRENT_REGCACHE specially here.  */
1268   if (regcache == current_regcache
1269       && !ptid_equal (registers_ptid, inferior_ptid))
1270     {
1271       registers_changed ();
1272       registers_ptid = inferior_ptid;
1273     }
1274
1275   regbuf = register_buffer (regcache, regnum);
1276   size = regcache->descr->sizeof_register[regnum];
1277
1278   if (buf)
1279     memcpy (regbuf, buf, size);
1280   else
1281     memset (regbuf, 0, size);
1282
1283   /* Mark the register as cached.  */
1284   regcache->register_valid_p[regnum] = 1;
1285 }
1286
1287 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
1288
1289 void
1290 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
1291 {
1292   const void *regbuf;
1293   size_t size;
1294
1295   gdb_assert (regcache != NULL && buf != NULL);
1296   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
1297
1298   regbuf = register_buffer (regcache, regnum);
1299   size = regcache->descr->sizeof_register[regnum];
1300   memcpy (buf, regbuf, size);
1301 }
1302
1303
1304 /* read_pc, write_pc, read_sp, deprecated_read_fp, etc.  Special
1305    handling for registers PC, SP, and FP.  */
1306
1307 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(),
1308    read_sp(), and deprecated_read_fp(), will eventually be replaced by
1309    per-frame methods.  Instead of relying on the global INFERIOR_PTID,
1310    they will use the contextual information provided by the FRAME.
1311    These functions do not belong in the register cache.  */
1312
1313 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1314    write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to
1315    be replaced by something that does not rely on global state.  But
1316    what?  */
1317
1318 CORE_ADDR
1319 read_pc_pid (ptid_t ptid)
1320 {
1321   ptid_t saved_inferior_ptid;
1322   CORE_ADDR pc_val;
1323
1324   /* In case ptid != inferior_ptid. */
1325   saved_inferior_ptid = inferior_ptid;
1326   inferior_ptid = ptid;
1327
1328   if (TARGET_READ_PC_P ())
1329     pc_val = TARGET_READ_PC (ptid);
1330   /* Else use per-frame method on get_current_frame.  */
1331   else if (PC_REGNUM >= 0)
1332     {
1333       CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1334       CORE_ADDR pc_val = ADDR_BITS_REMOVE (raw_val);
1335       return pc_val;
1336     }
1337   else
1338     internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
1339
1340   inferior_ptid = saved_inferior_ptid;
1341   return pc_val;
1342 }
1343
1344 CORE_ADDR
1345 read_pc (void)
1346 {
1347   return read_pc_pid (inferior_ptid);
1348 }
1349
1350 void
1351 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1352 {
1353 #ifdef PC_REGNUM
1354   if (PC_REGNUM >= 0)
1355     write_register_pid (PC_REGNUM, pc, ptid);
1356   if (DEPRECATED_NPC_REGNUM >= 0)
1357     write_register_pid (DEPRECATED_NPC_REGNUM, pc + 4, ptid);
1358 #else
1359   internal_error (__FILE__, __LINE__,
1360                   "generic_target_write_pc");
1361 #endif
1362 }
1363
1364 void
1365 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1366 {
1367   ptid_t saved_inferior_ptid;
1368
1369   /* In case ptid != inferior_ptid. */
1370   saved_inferior_ptid = inferior_ptid;
1371   inferior_ptid = ptid;
1372
1373   TARGET_WRITE_PC (pc, ptid);
1374
1375   inferior_ptid = saved_inferior_ptid;
1376 }
1377
1378 void
1379 write_pc (CORE_ADDR pc)
1380 {
1381   write_pc_pid (pc, inferior_ptid);
1382 }
1383
1384 /* Cope with strage ways of getting to the stack and frame pointers */
1385
1386 CORE_ADDR
1387 read_sp (void)
1388 {
1389   if (TARGET_READ_SP_P ())
1390     return TARGET_READ_SP ();
1391   else if (gdbarch_unwind_sp_p (current_gdbarch))
1392     return get_frame_sp (get_current_frame ());
1393   else if (SP_REGNUM >= 0)
1394     /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1395        about the architecture so put it at the end.  */
1396     return read_register (SP_REGNUM);
1397   internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
1398 }
1399
1400 void
1401 deprecated_write_sp (CORE_ADDR val)
1402 {
1403   gdb_assert (SP_REGNUM >= 0);
1404   write_register (SP_REGNUM, val);
1405 }
1406
1407 CORE_ADDR
1408 deprecated_read_fp (void)
1409 {
1410   if (DEPRECATED_TARGET_READ_FP_P ())
1411     return DEPRECATED_TARGET_READ_FP ();
1412   else if (DEPRECATED_FP_REGNUM >= 0)
1413     return read_register (DEPRECATED_FP_REGNUM);
1414   else
1415     internal_error (__FILE__, __LINE__, "deprecated_read_fp");
1416 }
1417
1418 static void
1419 reg_flush_command (char *command, int from_tty)
1420 {
1421   /* Force-flush the register cache.  */
1422   registers_changed ();
1423   if (from_tty)
1424     printf_filtered ("Register cache flushed.\n");
1425 }
1426
1427 static void
1428 build_regcache (void)
1429 {
1430   current_regcache = regcache_xmalloc (current_gdbarch);
1431   current_regcache->readonly_p = 0;
1432   deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
1433   deprecated_register_valid = current_regcache->register_valid_p;
1434 }
1435
1436 static void
1437 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1438                    const unsigned char *buf, long len)
1439 {
1440   int i;
1441   switch (endian)
1442     {
1443     case BFD_ENDIAN_BIG:
1444       for (i = 0; i < len; i++)
1445         fprintf_unfiltered (file, "%02x", buf[i]);
1446       break;
1447     case BFD_ENDIAN_LITTLE:
1448       for (i = len - 1; i >= 0; i--)
1449         fprintf_unfiltered (file, "%02x", buf[i]);
1450       break;
1451     default:
1452       internal_error (__FILE__, __LINE__, "Bad switch");
1453     }
1454 }
1455
1456 enum regcache_dump_what
1457 {
1458   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1459 };
1460
1461 static void
1462 regcache_dump (struct regcache *regcache, struct ui_file *file,
1463                enum regcache_dump_what what_to_dump)
1464 {
1465   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1466   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1467   int regnum;
1468   int footnote_nr = 0;
1469   int footnote_register_size = 0;
1470   int footnote_register_offset = 0;
1471   int footnote_register_type_name_null = 0;
1472   long register_offset = 0;
1473   unsigned char buf[MAX_REGISTER_SIZE];
1474
1475 #if 0
1476   fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1477   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1478                       regcache->descr->nr_raw_registers);
1479   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1480                       regcache->descr->nr_cooked_registers);
1481   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1482                       regcache->descr->sizeof_raw_registers);
1483   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1484                       regcache->descr->sizeof_raw_register_valid_p);
1485   fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1486   fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1487 #endif
1488
1489   gdb_assert (regcache->descr->nr_cooked_registers
1490               == (NUM_REGS + NUM_PSEUDO_REGS));
1491
1492   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1493     {
1494       /* Name.  */
1495       if (regnum < 0)
1496         fprintf_unfiltered (file, " %-10s", "Name");
1497       else
1498         {
1499           const char *p = REGISTER_NAME (regnum);
1500           if (p == NULL)
1501             p = "";
1502           else if (p[0] == '\0')
1503             p = "''";
1504           fprintf_unfiltered (file, " %-10s", p);
1505         }
1506
1507       /* Number.  */
1508       if (regnum < 0)
1509         fprintf_unfiltered (file, " %4s", "Nr");
1510       else
1511         fprintf_unfiltered (file, " %4d", regnum);
1512
1513       /* Relative number.  */
1514       if (regnum < 0)
1515         fprintf_unfiltered (file, " %4s", "Rel");
1516       else if (regnum < NUM_REGS)
1517         fprintf_unfiltered (file, " %4d", regnum);
1518       else
1519         fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1520
1521       /* Offset.  */
1522       if (regnum < 0)
1523         fprintf_unfiltered (file, " %6s  ", "Offset");
1524       else
1525         {
1526           fprintf_unfiltered (file, " %6ld",
1527                               regcache->descr->register_offset[regnum]);
1528           if (register_offset != regcache->descr->register_offset[regnum]
1529               || register_offset != DEPRECATED_REGISTER_BYTE (regnum)
1530               || (regnum > 0
1531                   && (regcache->descr->register_offset[regnum]
1532                       != (regcache->descr->register_offset[regnum - 1]
1533                           + regcache->descr->sizeof_register[regnum - 1])))
1534               )
1535             {
1536               if (!footnote_register_offset)
1537                 footnote_register_offset = ++footnote_nr;
1538               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1539             }
1540           else
1541             fprintf_unfiltered (file, "  ");
1542           register_offset = (regcache->descr->register_offset[regnum]
1543                              + regcache->descr->sizeof_register[regnum]);
1544         }
1545
1546       /* Size.  */
1547       if (regnum < 0)
1548         fprintf_unfiltered (file, " %5s ", "Size");
1549       else
1550         {
1551           fprintf_unfiltered (file, " %5ld",
1552                               regcache->descr->sizeof_register[regnum]);
1553           if ((regcache->descr->sizeof_register[regnum]
1554                != REGISTER_RAW_SIZE (regnum))
1555               || (regcache->descr->sizeof_register[regnum]
1556                   != REGISTER_VIRTUAL_SIZE (regnum))
1557               || (regcache->descr->sizeof_register[regnum]
1558                   != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1559                                                  regnum)))
1560               )
1561             {
1562               if (!footnote_register_size)
1563                 footnote_register_size = ++footnote_nr;
1564               fprintf_unfiltered (file, "*%d", footnote_register_size);
1565             }
1566           else
1567             fprintf_unfiltered (file, " ");
1568         }
1569
1570       /* Type.  */
1571       {
1572         const char *t;
1573         if (regnum < 0)
1574           t = "Type";
1575         else
1576           {
1577             static const char blt[] = "builtin_type";
1578             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1579             if (t == NULL)
1580               {
1581                 char *n;
1582                 if (!footnote_register_type_name_null)
1583                   footnote_register_type_name_null = ++footnote_nr;
1584                 xasprintf (&n, "*%d", footnote_register_type_name_null);
1585                 make_cleanup (xfree, n);
1586                 t = n;
1587               }
1588             /* Chop a leading builtin_type.  */
1589             if (strncmp (t, blt, strlen (blt)) == 0)
1590               t += strlen (blt);
1591           }
1592         fprintf_unfiltered (file, " %-15s", t);
1593       }
1594
1595       /* Leading space always present.  */
1596       fprintf_unfiltered (file, " ");
1597
1598       /* Value, raw.  */
1599       if (what_to_dump == regcache_dump_raw)
1600         {
1601           if (regnum < 0)
1602             fprintf_unfiltered (file, "Raw value");
1603           else if (regnum >= regcache->descr->nr_raw_registers)
1604             fprintf_unfiltered (file, "<cooked>");
1605           else if (!regcache_valid_p (regcache, regnum))
1606             fprintf_unfiltered (file, "<invalid>");
1607           else
1608             {
1609               regcache_raw_read (regcache, regnum, buf);
1610               fprintf_unfiltered (file, "0x");
1611               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1612                                  REGISTER_RAW_SIZE (regnum));
1613             }
1614         }
1615
1616       /* Value, cooked.  */
1617       if (what_to_dump == regcache_dump_cooked)
1618         {
1619           if (regnum < 0)
1620             fprintf_unfiltered (file, "Cooked value");
1621           else
1622             {
1623               regcache_cooked_read (regcache, regnum, buf);
1624               fprintf_unfiltered (file, "0x");
1625               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1626                                  REGISTER_VIRTUAL_SIZE (regnum));
1627             }
1628         }
1629
1630       /* Group members.  */
1631       if (what_to_dump == regcache_dump_groups)
1632         {
1633           if (regnum < 0)
1634             fprintf_unfiltered (file, "Groups");
1635           else
1636             {
1637               const char *sep = "";
1638               struct reggroup *group;
1639               for (group = reggroup_next (gdbarch, NULL);
1640                    group != NULL;
1641                    group = reggroup_next (gdbarch, group))
1642                 {
1643                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1644                     {
1645                       fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1646                       sep = ",";
1647                     }
1648                 }
1649             }
1650         }
1651
1652       fprintf_unfiltered (file, "\n");
1653     }
1654
1655   if (footnote_register_size)
1656     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1657                         footnote_register_size);
1658   if (footnote_register_offset)
1659     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1660                         footnote_register_offset);
1661   if (footnote_register_type_name_null)
1662     fprintf_unfiltered (file, 
1663                         "*%d: Register type's name NULL.\n",
1664                         footnote_register_type_name_null);
1665   do_cleanups (cleanups);
1666 }
1667
1668 static void
1669 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1670 {
1671   if (args == NULL)
1672     regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1673   else
1674     {
1675       struct ui_file *file = gdb_fopen (args, "w");
1676       if (file == NULL)
1677         perror_with_name ("maintenance print architecture");
1678       regcache_dump (current_regcache, file, what_to_dump);    
1679       ui_file_delete (file);
1680     }
1681 }
1682
1683 static void
1684 maintenance_print_registers (char *args, int from_tty)
1685 {
1686   regcache_print (args, regcache_dump_none);
1687 }
1688
1689 static void
1690 maintenance_print_raw_registers (char *args, int from_tty)
1691 {
1692   regcache_print (args, regcache_dump_raw);
1693 }
1694
1695 static void
1696 maintenance_print_cooked_registers (char *args, int from_tty)
1697 {
1698   regcache_print (args, regcache_dump_cooked);
1699 }
1700
1701 static void
1702 maintenance_print_register_groups (char *args, int from_tty)
1703 {
1704   regcache_print (args, regcache_dump_groups);
1705 }
1706
1707 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1708
1709 void
1710 _initialize_regcache (void)
1711 {
1712   regcache_descr_handle = register_gdbarch_data (init_regcache_descr);
1713   REGISTER_GDBARCH_SWAP (current_regcache);
1714   register_gdbarch_swap (&deprecated_registers, sizeof (deprecated_registers), NULL);
1715   register_gdbarch_swap (&deprecated_register_valid, sizeof (deprecated_register_valid), NULL);
1716   register_gdbarch_swap (NULL, 0, build_regcache);
1717
1718   add_com ("flushregs", class_maintenance, reg_flush_command,
1719            "Force gdb to flush its register cache (maintainer command)");
1720
1721    /* Initialize the thread/process associated with the current set of
1722       registers.  For now, -1 is special, and means `no current process'.  */
1723   registers_ptid = pid_to_ptid (-1);
1724
1725   add_cmd ("registers", class_maintenance,
1726            maintenance_print_registers,
1727            "Print the internal register configuration.\
1728 Takes an optional file parameter.",
1729            &maintenanceprintlist);
1730   add_cmd ("raw-registers", class_maintenance,
1731            maintenance_print_raw_registers,
1732            "Print the internal register configuration including raw values.\
1733 Takes an optional file parameter.",
1734            &maintenanceprintlist);
1735   add_cmd ("cooked-registers", class_maintenance,
1736            maintenance_print_cooked_registers,
1737            "Print the internal register configuration including cooked values.\
1738 Takes an optional file parameter.",
1739            &maintenanceprintlist);
1740   add_cmd ("register-groups", class_maintenance,
1741            maintenance_print_register_groups,
1742            "Print the internal register configuration including each register's group.\
1743 Takes an optional file parameter.",
1744            &maintenanceprintlist);
1745
1746 }