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