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