2003-06-16 Andrew Cagney <cagney@redhat.com>
[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.  This should contain just [0
55      .. NUM_RAW_REGISTERS).  However, for older targets, it contains
56      space for the full [0 .. NUM_RAW_REGISTERS +
57      NUM_PSEUDO_REGISTERS).  */
58   int nr_raw_registers;
59   long sizeof_raw_registers;
60   long sizeof_raw_register_valid_p;
61
62   /* The cooked register space.  Each cooked register in the range
63      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
64      register.  The remaining [NR_RAW_REGISTERS
65      .. NR_COOKED_REGISTERS) (a.k.a. pseudo regiters) are mapped onto
66      both raw registers and memory by the architecture methods
67      gdbarch_register_read and gdbarch_register_write.  */
68   int nr_cooked_registers;
69   long sizeof_cooked_registers;
70   long sizeof_cooked_register_valid_p;
71
72   /* Offset and size (in 8 bit bytes), of reach register in the
73      register cache.  All registers (including those in the range
74      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an offset.
75      Assigning all registers an offset makes it possible to keep
76      legacy code, such as that found in read_register_bytes() and
77      write_register_bytes() working.  */
78   long *register_offset;
79   long *sizeof_register;
80
81   /* Cached table containing the type of each register.  */
82   struct type **register_type;
83 };
84
85 static void
86 init_legacy_regcache_descr (struct gdbarch *gdbarch,
87                             struct regcache_descr *descr)
88 {
89   int i;
90   /* FIXME: cagney/2002-05-11: gdbarch_data() should take that
91      ``gdbarch'' as a parameter.  */
92   gdb_assert (gdbarch != NULL);
93
94   /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
95      in the register cache.  Unfortunatly some architectures still
96      rely on this and the pseudo_register_write() method.  */
97   descr->nr_raw_registers = descr->nr_cooked_registers;
98   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
99
100   /* Compute the offset of each register.  Legacy architectures define
101      REGISTER_BYTE() so use that.  */
102   /* FIXME: cagney/2002-11-07: Instead of using REGISTER_BYTE() this
103      code should, as is done in init_regcache_descr(), compute the
104      offets at runtime.  This currently isn't possible as some ISAs
105      define overlapping register regions - see the mess in
106      read_register_bytes() and write_register_bytes() registers.  */
107   descr->sizeof_register = XCALLOC (descr->nr_cooked_registers, long);
108   descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
109   for (i = 0; i < descr->nr_cooked_registers; i++)
110     {
111       /* FIXME: cagney/2001-12-04: This code shouldn't need to use
112          REGISTER_BYTE().  Unfortunatly, legacy code likes to lay the
113          buffer out so that certain registers just happen to overlap.
114          Ulgh!  New targets use gdbarch's register read/write and
115          entirely avoid this uglyness.  */
116       descr->register_offset[i] = REGISTER_BYTE (i);
117       descr->sizeof_register[i] = REGISTER_RAW_SIZE (i);
118       gdb_assert (MAX_REGISTER_SIZE >= REGISTER_RAW_SIZE (i));
119       gdb_assert (MAX_REGISTER_SIZE >= REGISTER_VIRTUAL_SIZE (i));
120     }
121
122   /* Compute the real size of the register buffer.  Start out by
123      trusting DEPRECATED_REGISTER_BYTES, but then adjust it upwards
124      should that be found to not be sufficient.  */
125   /* FIXME: cagney/2002-11-05: Instead of using the macro
126      DEPRECATED_REGISTER_BYTES, this code should, as is done in
127      init_regcache_descr(), compute the total number of register bytes
128      using the accumulated offsets.  */
129   descr->sizeof_cooked_registers = DEPRECATED_REGISTER_BYTES; /* OK */
130   for (i = 0; i < descr->nr_cooked_registers; i++)
131     {
132       long regend;
133       /* Keep extending the buffer so that there is always enough
134          space for all registers.  The comparison is necessary since
135          legacy code is free to put registers in random places in the
136          buffer separated by holes.  Once REGISTER_BYTE() is killed
137          this can be greatly simplified.  */
138       regend = descr->register_offset[i] + descr->sizeof_register[i];
139       if (descr->sizeof_cooked_registers < regend)
140         descr->sizeof_cooked_registers = regend;
141     }
142   /* FIXME: cagney/2002-05-11: Shouldn't be including pseudo-registers
143      in the register cache.  Unfortunatly some architectures still
144      rely on this and the pseudo_register_write() method.  */
145   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
146 }
147
148 static void *
149 init_regcache_descr (struct gdbarch *gdbarch)
150 {
151   int i;
152   struct regcache_descr *descr;
153   gdb_assert (gdbarch != NULL);
154
155   /* Create an initial, zero filled, table.  */
156   descr = XCALLOC (1, struct regcache_descr);
157   descr->gdbarch = gdbarch;
158
159   /* Total size of the register space.  The raw registers are mapped
160      directly onto the raw register cache while the pseudo's are
161      either mapped onto raw-registers or memory.  */
162   descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
163   descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
164
165   /* Fill in a table of register types.  */
166   descr->register_type = XCALLOC (descr->nr_cooked_registers,
167                                   struct type *);
168   for (i = 0; i < descr->nr_cooked_registers; i++)
169     {
170       if (gdbarch_register_type_p (gdbarch))
171         {
172           gdb_assert (!REGISTER_VIRTUAL_TYPE_P ()); /* OK */
173           descr->register_type[i] = gdbarch_register_type (gdbarch, i);
174         }
175       else
176         descr->register_type[i] = REGISTER_VIRTUAL_TYPE (i); /* OK */
177     }
178
179   /* If an old style architecture, fill in the remainder of the
180      register cache descriptor using the register macros.  */
181   if (!gdbarch_pseudo_register_read_p (gdbarch)
182       && !gdbarch_pseudo_register_write_p (gdbarch)
183       && !gdbarch_register_type_p (gdbarch))
184     {
185       /* NOTE: cagney/2003-05-02: Don't add a test for REGISTER_BYTE_P
186          to the above.  Doing that would cause all the existing
187          architectures to revert back to the legacy regcache
188          mechanisms, and that is not a good thing.  Instead just,
189          later, check that the register cache's layout is consistent
190          with REGISTER_BYTE.  */
191       descr->legacy_p = 1;
192       init_legacy_regcache_descr (gdbarch, descr);
193       return descr;
194     }
195
196   /* Construct a strictly RAW register cache.  Don't allow pseudo's
197      into the register cache.  */
198   descr->nr_raw_registers = NUM_REGS;
199
200   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
201      array.  This pretects GDB from erant code that accesses elements
202      of the global register_valid_p[] array in the range [NUM_REGS
203      .. NUM_REGS + NUM_PSEUDO_REGS).  */
204   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
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 = XCALLOC (descr->nr_cooked_registers, long);
216     descr->register_offset = XCALLOC (descr->nr_cooked_registers, long);
217     for (i = 0; i < descr->nr_cooked_registers; i++)
218       {
219         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
220         descr->register_offset[i] = offset;
221         offset += descr->sizeof_register[i];
222         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
223       }
224     /* Set the real size of the register cache buffer.  */
225     descr->sizeof_cooked_registers = offset;
226   }
227
228   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
229      the raw registers.  Unfortunatly some code still accesses the
230      register array directly using the global registers[].  Until that
231      code has been purged, play safe and over allocating the register
232      buffer.  Ulgh!  */
233   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
234
235   /* Sanity check.  Confirm that there is agreement between the
236      regcache and the target's redundant REGISTER_BYTE (new targets
237      should not even be defining it).  */
238   for (i = 0; i < descr->nr_cooked_registers; i++)
239     {
240       if (REGISTER_BYTE_P ())
241         gdb_assert (descr->register_offset[i] == REGISTER_BYTE (i));
242 #if 0
243       gdb_assert (descr->sizeof_register[i] == REGISTER_RAW_SIZE (i));
244       gdb_assert (descr->sizeof_register[i] == REGISTER_VIRTUAL_SIZE (i));
245 #endif
246     }
247   /* gdb_assert (descr->sizeof_raw_registers == DEPRECATED_REGISTER_BYTES (i));  */
248
249   return descr;
250 }
251
252 static struct regcache_descr *
253 regcache_descr (struct gdbarch *gdbarch)
254 {
255   return gdbarch_data (gdbarch, regcache_descr_handle);
256 }
257
258 static void
259 xfree_regcache_descr (struct gdbarch *gdbarch, void *ptr)
260 {
261   struct regcache_descr *descr = ptr;
262   if (descr == NULL)
263     return;
264   xfree (descr->register_offset);
265   xfree (descr->sizeof_register);
266   descr->register_offset = NULL;
267   descr->sizeof_register = NULL;
268   xfree (descr);
269 }
270
271 /* Utility functions returning useful register attributes stored in
272    the regcache descr.  */
273
274 struct type *
275 register_type (struct gdbarch *gdbarch, int regnum)
276 {
277   struct regcache_descr *descr = regcache_descr (gdbarch);
278   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
279   return descr->register_type[regnum];
280 }
281
282 /* Utility functions returning useful register attributes stored in
283    the regcache descr.  */
284
285 int
286 register_size (struct gdbarch *gdbarch, int regnum)
287 {
288   struct regcache_descr *descr = regcache_descr (gdbarch);
289   int size;
290   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
291   size = descr->sizeof_register[regnum];
292   gdb_assert (size == REGISTER_RAW_SIZE (regnum)); /* OK */
293   gdb_assert (size == REGISTER_RAW_SIZE (regnum)); /* OK */
294   return size;
295 }
296
297 /* The register cache for storing raw register values.  */
298
299 struct regcache
300 {
301   struct regcache_descr *descr;
302   /* The register buffers.  A read-only register cache can hold the
303      full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
304      register cache can only hold [0 .. NUM_REGS).  */
305   char *registers;
306   char *register_valid_p;
307   /* Is this a read-only cache?  A read-only cache is used for saving
308      the target's register state (e.g, across an inferior function
309      call or just before forcing a function return).  A read-only
310      cache can only be updated via the methods regcache_dup() and
311      regcache_cpy().  The actual contents are determined by the
312      reggroup_save and reggroup_restore methods.  */
313   int readonly_p;
314 };
315
316 struct regcache *
317 regcache_xmalloc (struct gdbarch *gdbarch)
318 {
319   struct regcache_descr *descr;
320   struct regcache *regcache;
321   gdb_assert (gdbarch != NULL);
322   descr = regcache_descr (gdbarch);
323   regcache = XMALLOC (struct regcache);
324   regcache->descr = descr;
325   regcache->registers
326     = XCALLOC (descr->sizeof_raw_registers, char);
327   regcache->register_valid_p
328     = XCALLOC (descr->sizeof_raw_register_valid_p, char);
329   regcache->readonly_p = 1;
330   return regcache;
331 }
332
333 void
334 regcache_xfree (struct regcache *regcache)
335 {
336   if (regcache == NULL)
337     return;
338   xfree (regcache->registers);
339   xfree (regcache->register_valid_p);
340   xfree (regcache);
341 }
342
343 static void
344 do_regcache_xfree (void *data)
345 {
346   regcache_xfree (data);
347 }
348
349 struct cleanup *
350 make_cleanup_regcache_xfree (struct regcache *regcache)
351 {
352   return make_cleanup (do_regcache_xfree, regcache);
353 }
354
355 /* Return  a pointer to register REGNUM's buffer cache.  */
356
357 static char *
358 register_buffer (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 = 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 = 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 /* SUPPLY_REGISTER()
1215
1216    Record that register REGNUM contains VAL.  This is used when the
1217    value is obtained from the inferior or core dump, so there is no
1218    need to store the value there.
1219
1220    If VAL is a NULL pointer, then it's probably an unsupported register.
1221    We just set its value to all zeros.  We might want to record this
1222    fact, and report it to the users of read_register and friends.  */
1223
1224 void
1225 supply_register (int regnum, const void *val)
1226 {
1227 #if 1
1228   if (! ptid_equal (registers_ptid, inferior_ptid))
1229     {
1230       registers_changed ();
1231       registers_ptid = inferior_ptid;
1232     }
1233 #endif
1234
1235   set_register_cached (regnum, 1);
1236   if (val)
1237     memcpy (register_buffer (current_regcache, regnum), val, 
1238             REGISTER_RAW_SIZE (regnum));
1239   else
1240     memset (register_buffer (current_regcache, regnum), '\000', 
1241             REGISTER_RAW_SIZE (regnum));
1242
1243   /* On some architectures, e.g. HPPA, there are a few stray bits in
1244      some registers, that the rest of the code would like to ignore.  */
1245
1246   /* NOTE: cagney/2001-03-16: The macro CLEAN_UP_REGISTER_VALUE is
1247      going to be deprecated.  Instead architectures will leave the raw
1248      register value as is and instead clean things up as they pass
1249      through the method gdbarch_pseudo_register_read() clean up the
1250      values. */
1251
1252 #ifdef DEPRECATED_CLEAN_UP_REGISTER_VALUE
1253   DEPRECATED_CLEAN_UP_REGISTER_VALUE \
1254     (regnum, register_buffer (current_regcache, regnum));
1255 #endif
1256 }
1257
1258 void
1259 regcache_collect (int regnum, void *buf)
1260 {
1261   memcpy (buf, register_buffer (current_regcache, regnum),
1262           REGISTER_RAW_SIZE (regnum));
1263 }
1264
1265
1266 /* read_pc, write_pc, read_sp, deprecated_read_fp, etc.  Special
1267    handling for registers PC, SP, and FP.  */
1268
1269 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc(),
1270    read_sp(), and deprecated_read_fp(), will eventually be replaced by
1271    per-frame methods.  Instead of relying on the global INFERIOR_PTID,
1272    they will use the contextual information provided by the FRAME.
1273    These functions do not belong in the register cache.  */
1274
1275 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
1276    write_pc_pid(), write_pc(), and deprecated_read_fp(), all need to
1277    be replaced by something that does not rely on global state.  But
1278    what?  */
1279
1280 CORE_ADDR
1281 read_pc_pid (ptid_t ptid)
1282 {
1283   ptid_t saved_inferior_ptid;
1284   CORE_ADDR pc_val;
1285
1286   /* In case ptid != inferior_ptid. */
1287   saved_inferior_ptid = inferior_ptid;
1288   inferior_ptid = ptid;
1289
1290   if (TARGET_READ_PC_P ())
1291     pc_val = TARGET_READ_PC (ptid);
1292   /* Else use per-frame method on get_current_frame.  */
1293   else if (PC_REGNUM >= 0)
1294     {
1295       CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
1296       CORE_ADDR pc_val = ADDR_BITS_REMOVE (raw_val);
1297       return pc_val;
1298     }
1299   else
1300     internal_error (__FILE__, __LINE__, "read_pc_pid: Unable to find PC");
1301
1302   inferior_ptid = saved_inferior_ptid;
1303   return pc_val;
1304 }
1305
1306 CORE_ADDR
1307 read_pc (void)
1308 {
1309   return read_pc_pid (inferior_ptid);
1310 }
1311
1312 void
1313 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
1314 {
1315 #ifdef PC_REGNUM
1316   if (PC_REGNUM >= 0)
1317     write_register_pid (PC_REGNUM, pc, ptid);
1318   if (NPC_REGNUM >= 0)
1319     write_register_pid (NPC_REGNUM, pc + 4, ptid);
1320 #else
1321   internal_error (__FILE__, __LINE__,
1322                   "generic_target_write_pc");
1323 #endif
1324 }
1325
1326 void
1327 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
1328 {
1329   ptid_t saved_inferior_ptid;
1330
1331   /* In case ptid != inferior_ptid. */
1332   saved_inferior_ptid = inferior_ptid;
1333   inferior_ptid = ptid;
1334
1335   TARGET_WRITE_PC (pc, ptid);
1336
1337   inferior_ptid = saved_inferior_ptid;
1338 }
1339
1340 void
1341 write_pc (CORE_ADDR pc)
1342 {
1343   write_pc_pid (pc, inferior_ptid);
1344 }
1345
1346 /* Cope with strage ways of getting to the stack and frame pointers */
1347
1348 CORE_ADDR
1349 read_sp (void)
1350 {
1351   if (TARGET_READ_SP_P ())
1352     return TARGET_READ_SP ();
1353   else if (gdbarch_unwind_sp_p (current_gdbarch))
1354     return get_frame_sp (get_current_frame ());
1355   else if (SP_REGNUM >= 0)
1356     /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
1357        about the architecture so put it at the end.  */
1358     return read_register (SP_REGNUM);
1359   internal_error (__FILE__, __LINE__, "read_sp: Unable to find SP");
1360 }
1361
1362 void
1363 deprecated_write_sp (CORE_ADDR val)
1364 {
1365   gdb_assert (SP_REGNUM >= 0);
1366   write_register (SP_REGNUM, val);
1367 }
1368
1369 CORE_ADDR
1370 deprecated_read_fp (void)
1371 {
1372   if (DEPRECATED_TARGET_READ_FP_P ())
1373     return DEPRECATED_TARGET_READ_FP ();
1374   else if (DEPRECATED_FP_REGNUM >= 0)
1375     return read_register (DEPRECATED_FP_REGNUM);
1376   else
1377     internal_error (__FILE__, __LINE__, "deprecated_read_fp");
1378 }
1379
1380 /* ARGSUSED */
1381 static void
1382 reg_flush_command (char *command, int from_tty)
1383 {
1384   /* Force-flush the register cache.  */
1385   registers_changed ();
1386   if (from_tty)
1387     printf_filtered ("Register cache flushed.\n");
1388 }
1389
1390 static void
1391 build_regcache (void)
1392 {
1393   current_regcache = regcache_xmalloc (current_gdbarch);
1394   current_regcache->readonly_p = 0;
1395   deprecated_registers = deprecated_grub_regcache_for_registers (current_regcache);
1396   deprecated_register_valid = current_regcache->register_valid_p;
1397 }
1398
1399 static void
1400 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
1401                    const unsigned char *buf, long len)
1402 {
1403   int i;
1404   switch (endian)
1405     {
1406     case BFD_ENDIAN_BIG:
1407       for (i = 0; i < len; i++)
1408         fprintf_unfiltered (file, "%02x", buf[i]);
1409       break;
1410     case BFD_ENDIAN_LITTLE:
1411       for (i = len - 1; i >= 0; i--)
1412         fprintf_unfiltered (file, "%02x", buf[i]);
1413       break;
1414     default:
1415       internal_error (__FILE__, __LINE__, "Bad switch");
1416     }
1417 }
1418
1419 enum regcache_dump_what
1420 {
1421   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
1422 };
1423
1424 static void
1425 regcache_dump (struct regcache *regcache, struct ui_file *file,
1426                enum regcache_dump_what what_to_dump)
1427 {
1428   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1429   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1430   struct reggroup *const *groups = reggroups (gdbarch);
1431   int regnum;
1432   int footnote_nr = 0;
1433   int footnote_register_size = 0;
1434   int footnote_register_offset = 0;
1435   int footnote_register_type_name_null = 0;
1436   long register_offset = 0;
1437   unsigned char buf[MAX_REGISTER_SIZE];
1438
1439 #if 0
1440   fprintf_unfiltered (file, "legacy_p %d\n", regcache->descr->legacy_p);
1441   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1442                       regcache->descr->nr_raw_registers);
1443   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1444                       regcache->descr->nr_cooked_registers);
1445   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1446                       regcache->descr->sizeof_raw_registers);
1447   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1448                       regcache->descr->sizeof_raw_register_valid_p);
1449   fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1450   fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1451 #endif
1452
1453   gdb_assert (regcache->descr->nr_cooked_registers
1454               == (NUM_REGS + NUM_PSEUDO_REGS));
1455
1456   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1457     {
1458       /* Name.  */
1459       if (regnum < 0)
1460         fprintf_unfiltered (file, " %-10s", "Name");
1461       else
1462         {
1463           const char *p = REGISTER_NAME (regnum);
1464           if (p == NULL)
1465             p = "";
1466           else if (p[0] == '\0')
1467             p = "''";
1468           fprintf_unfiltered (file, " %-10s", p);
1469         }
1470
1471       /* Number.  */
1472       if (regnum < 0)
1473         fprintf_unfiltered (file, " %4s", "Nr");
1474       else
1475         fprintf_unfiltered (file, " %4d", regnum);
1476
1477       /* Relative number.  */
1478       if (regnum < 0)
1479         fprintf_unfiltered (file, " %4s", "Rel");
1480       else if (regnum < NUM_REGS)
1481         fprintf_unfiltered (file, " %4d", regnum);
1482       else
1483         fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1484
1485       /* Offset.  */
1486       if (regnum < 0)
1487         fprintf_unfiltered (file, " %6s  ", "Offset");
1488       else
1489         {
1490           fprintf_unfiltered (file, " %6ld",
1491                               regcache->descr->register_offset[regnum]);
1492           if (register_offset != regcache->descr->register_offset[regnum]
1493               || register_offset != REGISTER_BYTE (regnum)
1494               || (regnum > 0
1495                   && (regcache->descr->register_offset[regnum]
1496                       != (regcache->descr->register_offset[regnum - 1]
1497                           + regcache->descr->sizeof_register[regnum - 1])))
1498               )
1499             {
1500               if (!footnote_register_offset)
1501                 footnote_register_offset = ++footnote_nr;
1502               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1503             }
1504           else
1505             fprintf_unfiltered (file, "  ");
1506           register_offset = (regcache->descr->register_offset[regnum]
1507                              + regcache->descr->sizeof_register[regnum]);
1508         }
1509
1510       /* Size.  */
1511       if (regnum < 0)
1512         fprintf_unfiltered (file, " %5s ", "Size");
1513       else
1514         {
1515           fprintf_unfiltered (file, " %5ld",
1516                               regcache->descr->sizeof_register[regnum]);
1517           if ((regcache->descr->sizeof_register[regnum]
1518                != REGISTER_RAW_SIZE (regnum))
1519               || (regcache->descr->sizeof_register[regnum]
1520                   != REGISTER_VIRTUAL_SIZE (regnum))
1521               || (regcache->descr->sizeof_register[regnum]
1522                   != TYPE_LENGTH (register_type (regcache->descr->gdbarch,
1523                                                  regnum)))
1524               )
1525             {
1526               if (!footnote_register_size)
1527                 footnote_register_size = ++footnote_nr;
1528               fprintf_unfiltered (file, "*%d", footnote_register_size);
1529             }
1530           else
1531             fprintf_unfiltered (file, " ");
1532         }
1533
1534       /* Type.  */
1535       {
1536         const char *t;
1537         if (regnum < 0)
1538           t = "Type";
1539         else
1540           {
1541             static const char blt[] = "builtin_type";
1542             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1543             if (t == NULL)
1544               {
1545                 char *n;
1546                 if (!footnote_register_type_name_null)
1547                   footnote_register_type_name_null = ++footnote_nr;
1548                 xasprintf (&n, "*%d", footnote_register_type_name_null);
1549                 make_cleanup (xfree, n);
1550                 t = n;
1551               }
1552             /* Chop a leading builtin_type.  */
1553             if (strncmp (t, blt, strlen (blt)) == 0)
1554               t += strlen (blt);
1555           }
1556         fprintf_unfiltered (file, " %-15s", t);
1557       }
1558
1559       /* Leading space always present.  */
1560       fprintf_unfiltered (file, " ");
1561
1562       /* Value, raw.  */
1563       if (what_to_dump == regcache_dump_raw)
1564         {
1565           if (regnum < 0)
1566             fprintf_unfiltered (file, "Raw value");
1567           else if (regnum >= regcache->descr->nr_raw_registers)
1568             fprintf_unfiltered (file, "<cooked>");
1569           else if (!regcache_valid_p (regcache, regnum))
1570             fprintf_unfiltered (file, "<invalid>");
1571           else
1572             {
1573               regcache_raw_read (regcache, regnum, buf);
1574               fprintf_unfiltered (file, "0x");
1575               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1576                                  REGISTER_RAW_SIZE (regnum));
1577             }
1578         }
1579
1580       /* Value, cooked.  */
1581       if (what_to_dump == regcache_dump_cooked)
1582         {
1583           if (regnum < 0)
1584             fprintf_unfiltered (file, "Cooked value");
1585           else
1586             {
1587               regcache_cooked_read (regcache, regnum, buf);
1588               fprintf_unfiltered (file, "0x");
1589               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1590                                  REGISTER_VIRTUAL_SIZE (regnum));
1591             }
1592         }
1593
1594       /* Group members.  */
1595       if (what_to_dump == regcache_dump_groups)
1596         {
1597           if (regnum < 0)
1598             fprintf_unfiltered (file, "Groups");
1599           else
1600             {
1601               int i;
1602               const char *sep = "";
1603               for (i = 0; groups[i] != NULL; i++)
1604                 {
1605                   if (gdbarch_register_reggroup_p (gdbarch, regnum, groups[i]))
1606                     {
1607                       fprintf_unfiltered (file, "%s%s", sep, reggroup_name (groups[i]));
1608                       sep = ",";
1609                     }
1610                 }
1611             }
1612         }
1613
1614       fprintf_unfiltered (file, "\n");
1615     }
1616
1617   if (footnote_register_size)
1618     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1619                         footnote_register_size);
1620   if (footnote_register_offset)
1621     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1622                         footnote_register_offset);
1623   if (footnote_register_type_name_null)
1624     fprintf_unfiltered (file, 
1625                         "*%d: Register type's name NULL.\n",
1626                         footnote_register_type_name_null);
1627   do_cleanups (cleanups);
1628 }
1629
1630 static void
1631 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1632 {
1633   if (args == NULL)
1634     regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1635   else
1636     {
1637       struct ui_file *file = gdb_fopen (args, "w");
1638       if (file == NULL)
1639         perror_with_name ("maintenance print architecture");
1640       regcache_dump (current_regcache, file, what_to_dump);    
1641       ui_file_delete (file);
1642     }
1643 }
1644
1645 static void
1646 maintenance_print_registers (char *args, int from_tty)
1647 {
1648   regcache_print (args, regcache_dump_none);
1649 }
1650
1651 static void
1652 maintenance_print_raw_registers (char *args, int from_tty)
1653 {
1654   regcache_print (args, regcache_dump_raw);
1655 }
1656
1657 static void
1658 maintenance_print_cooked_registers (char *args, int from_tty)
1659 {
1660   regcache_print (args, regcache_dump_cooked);
1661 }
1662
1663 static void
1664 maintenance_print_register_groups (char *args, int from_tty)
1665 {
1666   regcache_print (args, regcache_dump_groups);
1667 }
1668
1669 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1670
1671 void
1672 _initialize_regcache (void)
1673 {
1674   regcache_descr_handle = register_gdbarch_data (init_regcache_descr,
1675                                                  xfree_regcache_descr);
1676   REGISTER_GDBARCH_SWAP (current_regcache);
1677   register_gdbarch_swap (&deprecated_registers, sizeof (deprecated_registers), NULL);
1678   register_gdbarch_swap (&deprecated_register_valid, sizeof (deprecated_register_valid), NULL);
1679   register_gdbarch_swap (NULL, 0, build_regcache);
1680
1681   add_com ("flushregs", class_maintenance, reg_flush_command,
1682            "Force gdb to flush its register cache (maintainer command)");
1683
1684    /* Initialize the thread/process associated with the current set of
1685       registers.  For now, -1 is special, and means `no current process'.  */
1686   registers_ptid = pid_to_ptid (-1);
1687
1688   add_cmd ("registers", class_maintenance,
1689            maintenance_print_registers,
1690            "Print the internal register configuration.\
1691 Takes an optional file parameter.",
1692            &maintenanceprintlist);
1693   add_cmd ("raw-registers", class_maintenance,
1694            maintenance_print_raw_registers,
1695            "Print the internal register configuration including raw values.\
1696 Takes an optional file parameter.",
1697            &maintenanceprintlist);
1698   add_cmd ("cooked-registers", class_maintenance,
1699            maintenance_print_cooked_registers,
1700            "Print the internal register configuration including cooked values.\
1701 Takes an optional file parameter.",
1702            &maintenanceprintlist);
1703   add_cmd ("register-groups", class_maintenance,
1704            maintenance_print_register_groups,
1705            "Print the internal register configuration including each register's group.\
1706 Takes an optional file parameter.",
1707            &maintenanceprintlist);
1708
1709 }