OSDN Git Service

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