* regcache.c (regcache_invalidate): New function.
[platform/upstream/binutils.git] / gdb / regcache.c
1 /* Cache and manage the values of registers for GDB, the GNU debugger.
2
3    Copyright (C) 1986, 1987, 1989, 1991, 1994, 1995, 1996, 1998, 2000, 2001,
4    2002, 2004, 2007 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., 51 Franklin Street, Fifth Floor,
21    Boston, MA 02110-1301, 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 #include "observer.h"
34
35 /*
36  * DATA STRUCTURE
37  *
38  * Here is the actual register cache.
39  */
40
41 /* Per-architecture object describing the layout of a register cache.
42    Computed once when the architecture is created */
43
44 struct gdbarch_data *regcache_descr_handle;
45
46 struct regcache_descr
47 {
48   /* The architecture this descriptor belongs to.  */
49   struct gdbarch *gdbarch;
50
51   /* The raw register cache.  Each raw (or hard) register is supplied
52      by the target interface.  The raw cache should not contain
53      redundant information - if the PC is constructed from two
54      registers then those registers and not the PC lives in the raw
55      cache.  */
56   int nr_raw_registers;
57   long sizeof_raw_registers;
58   long sizeof_raw_register_valid_p;
59
60   /* The cooked register space.  Each cooked register in the range
61      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
62      register.  The remaining [NR_RAW_REGISTERS
63      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
64      both raw registers and memory by the architecture methods
65      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
66   int nr_cooked_registers;
67   long sizeof_cooked_registers;
68   long sizeof_cooked_register_valid_p;
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   /* Cached table containing the type of each register.  */
80   struct type **register_type;
81 };
82
83 static void *
84 init_regcache_descr (struct gdbarch *gdbarch)
85 {
86   int i;
87   struct regcache_descr *descr;
88   gdb_assert (gdbarch != NULL);
89
90   /* Create an initial, zero filled, table.  */
91   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
92   descr->gdbarch = gdbarch;
93
94   /* Total size of the register space.  The raw registers are mapped
95      directly onto the raw register cache while the pseudo's are
96      either mapped onto raw-registers or memory.  */
97   descr->nr_cooked_registers = NUM_REGS + NUM_PSEUDO_REGS;
98   descr->sizeof_cooked_register_valid_p = NUM_REGS + NUM_PSEUDO_REGS;
99
100   /* Fill in a table of register types.  */
101   descr->register_type
102     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, struct type *);
103   for (i = 0; i < descr->nr_cooked_registers; i++)
104     descr->register_type[i] = gdbarch_register_type (gdbarch, i);
105
106   /* Construct a strictly RAW register cache.  Don't allow pseudo's
107      into the register cache.  */
108   descr->nr_raw_registers = NUM_REGS;
109
110   /* FIXME: cagney/2002-08-13: Overallocate the register_valid_p
111      array.  This pretects GDB from erant code that accesses elements
112      of the global register_valid_p[] array in the range [NUM_REGS
113      .. NUM_REGS + NUM_PSEUDO_REGS).  */
114   descr->sizeof_raw_register_valid_p = descr->sizeof_cooked_register_valid_p;
115
116   /* Lay out the register cache.
117
118      NOTE: cagney/2002-05-22: Only register_type() is used when
119      constructing the register cache.  It is assumed that the
120      register's raw size, virtual size and type length are all the
121      same.  */
122
123   {
124     long offset = 0;
125     descr->sizeof_register
126       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
127     descr->register_offset
128       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
129     for (i = 0; i < descr->nr_cooked_registers; i++)
130       {
131         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
132         descr->register_offset[i] = offset;
133         offset += descr->sizeof_register[i];
134         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
135       }
136     /* Set the real size of the register cache buffer.  */
137     descr->sizeof_cooked_registers = offset;
138   }
139
140   /* FIXME: cagney/2002-05-22: Should only need to allocate space for
141      the raw registers.  Unfortunately some code still accesses the
142      register array directly using the global registers[].  Until that
143      code has been purged, play safe and over allocating the register
144      buffer.  Ulgh!  */
145   descr->sizeof_raw_registers = descr->sizeof_cooked_registers;
146
147   return descr;
148 }
149
150 static struct regcache_descr *
151 regcache_descr (struct gdbarch *gdbarch)
152 {
153   return gdbarch_data (gdbarch, regcache_descr_handle);
154 }
155
156 /* Utility functions returning useful register attributes stored in
157    the regcache descr.  */
158
159 struct type *
160 register_type (struct gdbarch *gdbarch, int regnum)
161 {
162   struct regcache_descr *descr = regcache_descr (gdbarch);
163   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
164   return descr->register_type[regnum];
165 }
166
167 /* Utility functions returning useful register attributes stored in
168    the regcache descr.  */
169
170 int
171 register_size (struct gdbarch *gdbarch, int regnum)
172 {
173   struct regcache_descr *descr = regcache_descr (gdbarch);
174   int size;
175   gdb_assert (regnum >= 0 && regnum < (NUM_REGS + NUM_PSEUDO_REGS));
176   size = descr->sizeof_register[regnum];
177   return size;
178 }
179
180 /* The register cache for storing raw register values.  */
181
182 struct regcache
183 {
184   struct regcache_descr *descr;
185   /* The register buffers.  A read-only register cache can hold the
186      full [0 .. NUM_REGS + NUM_PSEUDO_REGS) while a read/write
187      register cache can only hold [0 .. NUM_REGS).  */
188   gdb_byte *registers;
189   /* Register cache status:
190      register_valid_p[REG] == 0 if REG value is not in the cache
191                             > 0 if REG value is in the cache
192                             < 0 if REG value is permanently unavailable */
193   signed char *register_valid_p;
194   /* Is this a read-only cache?  A read-only cache is used for saving
195      the target's register state (e.g, across an inferior function
196      call or just before forcing a function return).  A read-only
197      cache can only be updated via the methods regcache_dup() and
198      regcache_cpy().  The actual contents are determined by the
199      reggroup_save and reggroup_restore methods.  */
200   int readonly_p;
201 };
202
203 struct regcache *
204 regcache_xmalloc (struct gdbarch *gdbarch)
205 {
206   struct regcache_descr *descr;
207   struct regcache *regcache;
208   gdb_assert (gdbarch != NULL);
209   descr = regcache_descr (gdbarch);
210   regcache = XMALLOC (struct regcache);
211   regcache->descr = descr;
212   regcache->registers
213     = XCALLOC (descr->sizeof_raw_registers, gdb_byte);
214   regcache->register_valid_p
215     = XCALLOC (descr->sizeof_raw_register_valid_p, gdb_byte);
216   regcache->readonly_p = 1;
217   return regcache;
218 }
219
220 void
221 regcache_xfree (struct regcache *regcache)
222 {
223   if (regcache == NULL)
224     return;
225   xfree (regcache->registers);
226   xfree (regcache->register_valid_p);
227   xfree (regcache);
228 }
229
230 static void
231 do_regcache_xfree (void *data)
232 {
233   regcache_xfree (data);
234 }
235
236 struct cleanup *
237 make_cleanup_regcache_xfree (struct regcache *regcache)
238 {
239   return make_cleanup (do_regcache_xfree, regcache);
240 }
241
242 /* Return REGCACHE's architecture.  */
243
244 struct gdbarch *
245 get_regcache_arch (const struct regcache *regcache)
246 {
247   return regcache->descr->gdbarch;
248 }
249
250 /* Return  a pointer to register REGNUM's buffer cache.  */
251
252 static gdb_byte *
253 register_buffer (const struct regcache *regcache, int regnum)
254 {
255   return regcache->registers + regcache->descr->register_offset[regnum];
256 }
257
258 void
259 regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
260                void *src)
261 {
262   struct gdbarch *gdbarch = dst->descr->gdbarch;
263   gdb_byte buf[MAX_REGISTER_SIZE];
264   int regnum;
265   /* The DST should be `read-only', if it wasn't then the save would
266      end up trying to write the register values back out to the
267      target.  */
268   gdb_assert (dst->readonly_p);
269   /* Clear the dest.  */
270   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
271   memset (dst->register_valid_p, 0, dst->descr->sizeof_cooked_register_valid_p);
272   /* Copy over any registers (identified by their membership in the
273      save_reggroup) and mark them as valid.  The full [0 .. NUM_REGS +
274      NUM_PSEUDO_REGS) range is checked since some architectures need
275      to save/restore `cooked' registers that live in memory.  */
276   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
277     {
278       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
279         {
280           int valid = cooked_read (src, regnum, buf);
281           if (valid)
282             {
283               memcpy (register_buffer (dst, regnum), buf,
284                       register_size (gdbarch, regnum));
285               dst->register_valid_p[regnum] = 1;
286             }
287         }
288     }
289 }
290
291 void
292 regcache_restore (struct regcache *dst,
293                   regcache_cooked_read_ftype *cooked_read,
294                   void *cooked_read_context)
295 {
296   struct gdbarch *gdbarch = dst->descr->gdbarch;
297   gdb_byte buf[MAX_REGISTER_SIZE];
298   int regnum;
299   /* The dst had better not be read-only.  If it is, the `restore'
300      doesn't make much sense.  */
301   gdb_assert (!dst->readonly_p);
302   /* Copy over any registers, being careful to only restore those that
303      were both saved and need to be restored.  The full [0 .. NUM_REGS
304      + NUM_PSEUDO_REGS) range is checked since some architectures need
305      to save/restore `cooked' registers that live in memory.  */
306   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
307     {
308       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
309         {
310           int valid = cooked_read (cooked_read_context, regnum, buf);
311           if (valid)
312             regcache_cooked_write (dst, regnum, buf);
313         }
314     }
315 }
316
317 static int
318 do_cooked_read (void *src, int regnum, gdb_byte *buf)
319 {
320   struct regcache *regcache = src;
321   if (!regcache->register_valid_p[regnum] && regcache->readonly_p)
322     /* Don't even think about fetching a register from a read-only
323        cache when the register isn't yet valid.  There isn't a target
324        from which the register value can be fetched.  */
325     return 0;
326   regcache_cooked_read (regcache, regnum, buf);
327   return 1;
328 }
329
330
331 void
332 regcache_cpy (struct regcache *dst, struct regcache *src)
333 {
334   int i;
335   gdb_byte *buf;
336   gdb_assert (src != NULL && dst != NULL);
337   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
338   gdb_assert (src != dst);
339   gdb_assert (src->readonly_p || dst->readonly_p);
340   if (!src->readonly_p)
341     regcache_save (dst, do_cooked_read, src);
342   else if (!dst->readonly_p)
343     regcache_restore (dst, do_cooked_read, src);
344   else
345     regcache_cpy_no_passthrough (dst, src);
346 }
347
348 void
349 regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
350 {
351   int i;
352   gdb_assert (src != NULL && dst != NULL);
353   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
354   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
355      move of data into the current_regcache().  Doing this would be
356      silly - it would mean that valid_p would be completely invalid.  */
357   gdb_assert (dst != current_regcache);
358   memcpy (dst->registers, src->registers, dst->descr->sizeof_raw_registers);
359   memcpy (dst->register_valid_p, src->register_valid_p,
360           dst->descr->sizeof_raw_register_valid_p);
361 }
362
363 struct regcache *
364 regcache_dup (struct regcache *src)
365 {
366   struct regcache *newbuf;
367   gdb_assert (current_regcache != NULL);
368   newbuf = regcache_xmalloc (src->descr->gdbarch);
369   regcache_cpy (newbuf, src);
370   return newbuf;
371 }
372
373 struct regcache *
374 regcache_dup_no_passthrough (struct regcache *src)
375 {
376   struct regcache *newbuf;
377   gdb_assert (current_regcache != NULL);
378   newbuf = regcache_xmalloc (src->descr->gdbarch);
379   regcache_cpy_no_passthrough (newbuf, src);
380   return newbuf;
381 }
382
383 int
384 regcache_valid_p (const struct regcache *regcache, int regnum)
385 {
386   gdb_assert (regcache != NULL);
387   gdb_assert (regnum >= 0);
388   if (regcache->readonly_p)
389     gdb_assert (regnum < regcache->descr->nr_cooked_registers);
390   else
391     gdb_assert (regnum < regcache->descr->nr_raw_registers);
392
393   return regcache->register_valid_p[regnum];
394 }
395
396 void
397 regcache_invalidate (struct regcache *regcache, int regnum)
398 {
399   gdb_assert (regcache != NULL);
400   gdb_assert (regnum >= 0);
401   gdb_assert (!regcache->readonly_p);
402   gdb_assert (regnum < regcache->descr->nr_raw_registers);
403   regcache->register_valid_p[regnum] = 0;
404 }
405
406
407 /* Global structure containing the current regcache.  */
408 /* FIXME: cagney/2002-05-11: The two global arrays registers[] and
409    deprecated_register_valid[] currently point into this structure.  */
410 struct regcache *current_regcache;
411
412 /* NOTE: this is a write-through cache.  There is no "dirty" bit for
413    recording if the register values have been changed (eg. by the
414    user).  Therefore all registers must be written back to the
415    target when appropriate.  */
416
417 /* The thread/process associated with the current set of registers. */
418
419 static ptid_t registers_ptid;
420
421 /* Observer for the target_changed event.  */
422
423 void
424 regcache_observer_target_changed (struct target_ops *target)
425 {
426   registers_changed ();
427 }
428
429 /* Low level examining and depositing of registers.
430
431    The caller is responsible for making sure that the inferior is
432    stopped before calling the fetching routines, or it will get
433    garbage.  (a change from GDB version 3, in which the caller got the
434    value from the last stop).  */
435
436 /* REGISTERS_CHANGED ()
437
438    Indicate that registers may have changed, so invalidate the cache.  */
439
440 void
441 registers_changed (void)
442 {
443   int i;
444
445   registers_ptid = pid_to_ptid (-1);
446
447   /* Force cleanup of any alloca areas if using C alloca instead of
448      a builtin alloca.  This particular call is used to clean up
449      areas allocated by low level target code which may build up
450      during lengthy interactions between gdb and the target before
451      gdb gives control to the user (ie watchpoints).  */
452   alloca (0);
453
454   for (i = 0; i < current_regcache->descr->nr_raw_registers; i++)
455     regcache_invalidate (current_regcache, i);
456 }
457
458
459 void
460 regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
461 {
462   gdb_assert (regcache != NULL && buf != NULL);
463   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
464   /* Make certain that the register cache is up-to-date with respect
465      to the current thread.  This switching shouldn't be necessary
466      only there is still only one target side register cache.  Sigh!
467      On the bright side, at least there is a regcache object.  */
468   if (!regcache->readonly_p)
469     {
470       gdb_assert (regcache == current_regcache);
471       if (! ptid_equal (registers_ptid, inferior_ptid))
472         {
473           registers_changed ();
474           registers_ptid = inferior_ptid;
475         }
476       if (!regcache_valid_p (regcache, regnum))
477         target_fetch_registers (regcache, regnum);
478 #if 0
479       /* FIXME: cagney/2004-08-07: At present a number of targets
480          forget (or didn't know that they needed) to set this leading to
481          panics.  Also is the problem that targets need to indicate
482          that a register is in one of the possible states: valid,
483          undefined, unknown.  The last of which isn't yet
484          possible.  */
485       gdb_assert (regcache_valid_p (regcache, regnum));
486 #endif
487     }
488   /* Copy the value directly into the register cache.  */
489   memcpy (buf, register_buffer (regcache, regnum),
490           regcache->descr->sizeof_register[regnum]);
491 }
492
493 void
494 regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
495 {
496   gdb_byte *buf;
497   gdb_assert (regcache != NULL);
498   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
499   buf = alloca (regcache->descr->sizeof_register[regnum]);
500   regcache_raw_read (regcache, regnum, buf);
501   (*val) = extract_signed_integer (buf,
502                                    regcache->descr->sizeof_register[regnum]);
503 }
504
505 void
506 regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
507                             ULONGEST *val)
508 {
509   gdb_byte *buf;
510   gdb_assert (regcache != NULL);
511   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
512   buf = alloca (regcache->descr->sizeof_register[regnum]);
513   regcache_raw_read (regcache, regnum, buf);
514   (*val) = extract_unsigned_integer (buf,
515                                      regcache->descr->sizeof_register[regnum]);
516 }
517
518 void
519 regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
520 {
521   void *buf;
522   gdb_assert (regcache != NULL);
523   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
524   buf = alloca (regcache->descr->sizeof_register[regnum]);
525   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
526   regcache_raw_write (regcache, regnum, buf);
527 }
528
529 void
530 regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
531                              ULONGEST val)
532 {
533   void *buf;
534   gdb_assert (regcache != NULL);
535   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
536   buf = alloca (regcache->descr->sizeof_register[regnum]);
537   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
538   regcache_raw_write (regcache, regnum, buf);
539 }
540
541 void
542 regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
543 {
544   gdb_assert (regnum >= 0);
545   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
546   if (regnum < regcache->descr->nr_raw_registers)
547     regcache_raw_read (regcache, regnum, buf);
548   else if (regcache->readonly_p
549            && regnum < regcache->descr->nr_cooked_registers
550            && regcache->register_valid_p[regnum])
551     /* Read-only register cache, perhaps the cooked value was cached?  */
552     memcpy (buf, register_buffer (regcache, regnum),
553             regcache->descr->sizeof_register[regnum]);
554   else
555     gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
556                                   regnum, buf);
557 }
558
559 void
560 regcache_cooked_read_signed (struct regcache *regcache, int regnum,
561                              LONGEST *val)
562 {
563   gdb_byte *buf;
564   gdb_assert (regcache != NULL);
565   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
566   buf = alloca (regcache->descr->sizeof_register[regnum]);
567   regcache_cooked_read (regcache, regnum, buf);
568   (*val) = extract_signed_integer (buf,
569                                    regcache->descr->sizeof_register[regnum]);
570 }
571
572 void
573 regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
574                                ULONGEST *val)
575 {
576   gdb_byte *buf;
577   gdb_assert (regcache != NULL);
578   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
579   buf = alloca (regcache->descr->sizeof_register[regnum]);
580   regcache_cooked_read (regcache, regnum, buf);
581   (*val) = extract_unsigned_integer (buf,
582                                      regcache->descr->sizeof_register[regnum]);
583 }
584
585 void
586 regcache_cooked_write_signed (struct regcache *regcache, int regnum,
587                               LONGEST val)
588 {
589   void *buf;
590   gdb_assert (regcache != NULL);
591   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
592   buf = alloca (regcache->descr->sizeof_register[regnum]);
593   store_signed_integer (buf, regcache->descr->sizeof_register[regnum], val);
594   regcache_cooked_write (regcache, regnum, buf);
595 }
596
597 void
598 regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
599                                 ULONGEST val)
600 {
601   void *buf;
602   gdb_assert (regcache != NULL);
603   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
604   buf = alloca (regcache->descr->sizeof_register[regnum]);
605   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum], val);
606   regcache_cooked_write (regcache, regnum, buf);
607 }
608
609 void
610 regcache_raw_write (struct regcache *regcache, int regnum,
611                     const gdb_byte *buf)
612 {
613   gdb_assert (regcache != NULL && buf != NULL);
614   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
615   gdb_assert (!regcache->readonly_p);
616
617   /* On the sparc, writing %g0 is a no-op, so we don't even want to
618      change the registers array if something writes to this register.  */
619   if (CANNOT_STORE_REGISTER (regnum))
620     return;
621
622   /* Make certain that the correct cache is selected.  */
623   gdb_assert (regcache == current_regcache);
624   if (! ptid_equal (registers_ptid, inferior_ptid))
625     {
626       registers_changed ();
627       registers_ptid = inferior_ptid;
628     }
629
630   /* If we have a valid copy of the register, and new value == old
631      value, then don't bother doing the actual store. */
632   if (regcache_valid_p (regcache, regnum)
633       && (memcmp (register_buffer (regcache, regnum), buf,
634                   regcache->descr->sizeof_register[regnum]) == 0))
635     return;
636
637   target_prepare_to_store (regcache);
638   memcpy (register_buffer (regcache, regnum), buf,
639           regcache->descr->sizeof_register[regnum]);
640   regcache->register_valid_p[regnum] = 1;
641   target_store_registers (regcache, regnum);
642 }
643
644 void
645 regcache_cooked_write (struct regcache *regcache, int regnum,
646                        const gdb_byte *buf)
647 {
648   gdb_assert (regnum >= 0);
649   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
650   if (regnum < regcache->descr->nr_raw_registers)
651     regcache_raw_write (regcache, regnum, buf);
652   else
653     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
654                                    regnum, buf);
655 }
656
657 /* Perform a partial register transfer using a read, modify, write
658    operation.  */
659
660 typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
661                                     void *buf);
662 typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
663                                      const void *buf);
664
665 static void
666 regcache_xfer_part (struct regcache *regcache, int regnum,
667                     int offset, int len, void *in, const void *out,
668                     void (*read) (struct regcache *regcache, int regnum,
669                                   gdb_byte *buf),
670                     void (*write) (struct regcache *regcache, int regnum,
671                                    const gdb_byte *buf))
672 {
673   struct regcache_descr *descr = regcache->descr;
674   gdb_byte reg[MAX_REGISTER_SIZE];
675   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
676   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
677   /* Something to do?  */
678   if (offset + len == 0)
679     return;
680   /* Read (when needed) ... */
681   if (in != NULL
682       || offset > 0
683       || offset + len < descr->sizeof_register[regnum])
684     {
685       gdb_assert (read != NULL);
686       read (regcache, regnum, reg);
687     }
688   /* ... modify ... */
689   if (in != NULL)
690     memcpy (in, reg + offset, len);
691   if (out != NULL)
692     memcpy (reg + offset, out, len);
693   /* ... write (when needed).  */
694   if (out != NULL)
695     {
696       gdb_assert (write != NULL);
697       write (regcache, regnum, reg);
698     }
699 }
700
701 void
702 regcache_raw_read_part (struct regcache *regcache, int regnum,
703                         int offset, int len, gdb_byte *buf)
704 {
705   struct regcache_descr *descr = regcache->descr;
706   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
707   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
708                       regcache_raw_read, regcache_raw_write);
709 }
710
711 void
712 regcache_raw_write_part (struct regcache *regcache, int regnum,
713                          int offset, int len, const gdb_byte *buf)
714 {
715   struct regcache_descr *descr = regcache->descr;
716   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
717   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
718                       regcache_raw_read, regcache_raw_write);
719 }
720
721 void
722 regcache_cooked_read_part (struct regcache *regcache, int regnum,
723                            int offset, int len, gdb_byte *buf)
724 {
725   struct regcache_descr *descr = regcache->descr;
726   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
727   regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
728                       regcache_cooked_read, regcache_cooked_write);
729 }
730
731 void
732 regcache_cooked_write_part (struct regcache *regcache, int regnum,
733                             int offset, int len, const gdb_byte *buf)
734 {
735   struct regcache_descr *descr = regcache->descr;
736   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
737   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
738                       regcache_cooked_read, regcache_cooked_write);
739 }
740
741 /* Hack to keep code that view the register buffer as raw bytes
742    working.  */
743
744 int
745 register_offset_hack (struct gdbarch *gdbarch, int regnum)
746 {
747   struct regcache_descr *descr = regcache_descr (gdbarch);
748   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
749   return descr->register_offset[regnum];
750 }
751
752 /* Return the contents of register REGNUM as an unsigned integer.  */
753
754 ULONGEST
755 read_register (int regnum)
756 {
757   gdb_byte *buf = alloca (register_size (current_gdbarch, regnum));
758   gdb_assert (current_regcache != NULL);
759   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
760   regcache_cooked_read (current_regcache, regnum, buf);
761   return (extract_unsigned_integer (buf, register_size (current_gdbarch, regnum)));
762 }
763
764 ULONGEST
765 read_register_pid (int regnum, ptid_t ptid)
766 {
767   ptid_t save_ptid;
768   int save_pid;
769   CORE_ADDR retval;
770
771   if (ptid_equal (ptid, inferior_ptid))
772     return read_register (regnum);
773
774   save_ptid = inferior_ptid;
775
776   inferior_ptid = ptid;
777
778   retval = read_register (regnum);
779
780   inferior_ptid = save_ptid;
781
782   return retval;
783 }
784
785 /* Store VALUE into the raw contents of register number REGNUM.  */
786
787 void
788 write_register (int regnum, LONGEST val)
789 {
790   void *buf;
791   int size;
792   size = register_size (current_gdbarch, regnum);
793   buf = alloca (size);
794   store_signed_integer (buf, size, (LONGEST) val);
795   gdb_assert (current_regcache != NULL);
796   gdb_assert (current_regcache->descr->gdbarch == current_gdbarch);
797   regcache_cooked_write (current_regcache, regnum, buf);
798 }
799
800 void
801 write_register_pid (int regnum, CORE_ADDR val, ptid_t ptid)
802 {
803   ptid_t save_ptid;
804
805   if (ptid_equal (ptid, inferior_ptid))
806     {
807       write_register (regnum, val);
808       return;
809     }
810
811   save_ptid = inferior_ptid;
812
813   inferior_ptid = ptid;
814
815   write_register (regnum, val);
816
817   inferior_ptid = save_ptid;
818 }
819
820 /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */
821
822 void
823 regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
824 {
825   void *regbuf;
826   size_t size;
827
828   gdb_assert (regcache != NULL);
829   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
830   gdb_assert (!regcache->readonly_p);
831
832   /* FIXME: kettenis/20030828: It shouldn't be necessary to handle
833      CURRENT_REGCACHE specially here.  */
834   if (regcache == current_regcache
835       && !ptid_equal (registers_ptid, inferior_ptid))
836     {
837       registers_changed ();
838       registers_ptid = inferior_ptid;
839     }
840
841   regbuf = register_buffer (regcache, regnum);
842   size = regcache->descr->sizeof_register[regnum];
843
844   if (buf)
845     memcpy (regbuf, buf, size);
846   else
847     memset (regbuf, 0, size);
848
849   /* Mark the register as cached.  */
850   regcache->register_valid_p[regnum] = 1;
851 }
852
853 /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */
854
855 void
856 regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
857 {
858   const void *regbuf;
859   size_t size;
860
861   gdb_assert (regcache != NULL && buf != NULL);
862   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
863
864   regbuf = register_buffer (regcache, regnum);
865   size = regcache->descr->sizeof_register[regnum];
866   memcpy (buf, regbuf, size);
867 }
868
869
870 /* read_pc, write_pc, read_sp, etc.  Special handling for registers
871    PC, SP, and FP.  */
872
873 /* NOTE: cagney/2001-02-18: The functions read_pc_pid(), read_pc() and
874    read_sp(), will eventually be replaced by per-frame methods.
875    Instead of relying on the global INFERIOR_PTID, they will use the
876    contextual information provided by the FRAME.  These functions do
877    not belong in the register cache.  */
878
879 /* NOTE: cagney/2003-06-07: The functions generic_target_write_pc(),
880    write_pc_pid() and write_pc(), all need to be replaced by something
881    that does not rely on global state.  But what?  */
882
883 CORE_ADDR
884 read_pc_pid (ptid_t ptid)
885 {
886   ptid_t saved_inferior_ptid;
887   CORE_ADDR pc_val;
888
889   /* In case ptid != inferior_ptid. */
890   saved_inferior_ptid = inferior_ptid;
891   inferior_ptid = ptid;
892
893   if (TARGET_READ_PC_P ())
894     pc_val = TARGET_READ_PC (ptid);
895   /* Else use per-frame method on get_current_frame.  */
896   else if (PC_REGNUM >= 0)
897     {
898       CORE_ADDR raw_val = read_register_pid (PC_REGNUM, ptid);
899       pc_val = ADDR_BITS_REMOVE (raw_val);
900     }
901   else
902     internal_error (__FILE__, __LINE__, _("read_pc_pid: Unable to find PC"));
903
904   inferior_ptid = saved_inferior_ptid;
905   return pc_val;
906 }
907
908 CORE_ADDR
909 read_pc (void)
910 {
911   return read_pc_pid (inferior_ptid);
912 }
913
914 void
915 generic_target_write_pc (CORE_ADDR pc, ptid_t ptid)
916 {
917   if (PC_REGNUM >= 0)
918     write_register_pid (PC_REGNUM, pc, ptid);
919   else
920     internal_error (__FILE__, __LINE__,
921                     _("generic_target_write_pc"));
922 }
923
924 void
925 write_pc_pid (CORE_ADDR pc, ptid_t ptid)
926 {
927   ptid_t saved_inferior_ptid;
928
929   /* In case ptid != inferior_ptid. */
930   saved_inferior_ptid = inferior_ptid;
931   inferior_ptid = ptid;
932
933   TARGET_WRITE_PC (pc, ptid);
934
935   inferior_ptid = saved_inferior_ptid;
936 }
937
938 void
939 write_pc (CORE_ADDR pc)
940 {
941   write_pc_pid (pc, inferior_ptid);
942 }
943
944 /* Cope with strage ways of getting to the stack and frame pointers */
945
946 CORE_ADDR
947 read_sp (void)
948 {
949   if (TARGET_READ_SP_P ())
950     return TARGET_READ_SP ();
951   else if (gdbarch_unwind_sp_p (current_gdbarch))
952     return get_frame_sp (get_current_frame ());
953   else if (SP_REGNUM >= 0)
954     /* Try SP_REGNUM last: this makes all sorts of [wrong] assumptions
955        about the architecture so put it at the end.  */
956     return read_register (SP_REGNUM);
957   internal_error (__FILE__, __LINE__, _("read_sp: Unable to find SP"));
958 }
959
960 static void
961 reg_flush_command (char *command, int from_tty)
962 {
963   /* Force-flush the register cache.  */
964   registers_changed ();
965   if (from_tty)
966     printf_filtered (_("Register cache flushed.\n"));
967 }
968
969 static void
970 build_regcache (void)
971 {
972   current_regcache = regcache_xmalloc (current_gdbarch);
973   current_regcache->readonly_p = 0;
974 }
975
976 static void
977 dump_endian_bytes (struct ui_file *file, enum bfd_endian endian,
978                    const unsigned char *buf, long len)
979 {
980   int i;
981   switch (endian)
982     {
983     case BFD_ENDIAN_BIG:
984       for (i = 0; i < len; i++)
985         fprintf_unfiltered (file, "%02x", buf[i]);
986       break;
987     case BFD_ENDIAN_LITTLE:
988       for (i = len - 1; i >= 0; i--)
989         fprintf_unfiltered (file, "%02x", buf[i]);
990       break;
991     default:
992       internal_error (__FILE__, __LINE__, _("Bad switch"));
993     }
994 }
995
996 enum regcache_dump_what
997 {
998   regcache_dump_none, regcache_dump_raw, regcache_dump_cooked, regcache_dump_groups
999 };
1000
1001 static void
1002 regcache_dump (struct regcache *regcache, struct ui_file *file,
1003                enum regcache_dump_what what_to_dump)
1004 {
1005   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
1006   struct gdbarch *gdbarch = regcache->descr->gdbarch;
1007   int regnum;
1008   int footnote_nr = 0;
1009   int footnote_register_size = 0;
1010   int footnote_register_offset = 0;
1011   int footnote_register_type_name_null = 0;
1012   long register_offset = 0;
1013   unsigned char buf[MAX_REGISTER_SIZE];
1014
1015 #if 0
1016   fprintf_unfiltered (file, "nr_raw_registers %d\n",
1017                       regcache->descr->nr_raw_registers);
1018   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
1019                       regcache->descr->nr_cooked_registers);
1020   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
1021                       regcache->descr->sizeof_raw_registers);
1022   fprintf_unfiltered (file, "sizeof_raw_register_valid_p %ld\n",
1023                       regcache->descr->sizeof_raw_register_valid_p);
1024   fprintf_unfiltered (file, "NUM_REGS %d\n", NUM_REGS);
1025   fprintf_unfiltered (file, "NUM_PSEUDO_REGS %d\n", NUM_PSEUDO_REGS);
1026 #endif
1027
1028   gdb_assert (regcache->descr->nr_cooked_registers
1029               == (NUM_REGS + NUM_PSEUDO_REGS));
1030
1031   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
1032     {
1033       /* Name.  */
1034       if (regnum < 0)
1035         fprintf_unfiltered (file, " %-10s", "Name");
1036       else
1037         {
1038           const char *p = REGISTER_NAME (regnum);
1039           if (p == NULL)
1040             p = "";
1041           else if (p[0] == '\0')
1042             p = "''";
1043           fprintf_unfiltered (file, " %-10s", p);
1044         }
1045
1046       /* Number.  */
1047       if (regnum < 0)
1048         fprintf_unfiltered (file, " %4s", "Nr");
1049       else
1050         fprintf_unfiltered (file, " %4d", regnum);
1051
1052       /* Relative number.  */
1053       if (regnum < 0)
1054         fprintf_unfiltered (file, " %4s", "Rel");
1055       else if (regnum < NUM_REGS)
1056         fprintf_unfiltered (file, " %4d", regnum);
1057       else
1058         fprintf_unfiltered (file, " %4d", (regnum - NUM_REGS));
1059
1060       /* Offset.  */
1061       if (regnum < 0)
1062         fprintf_unfiltered (file, " %6s  ", "Offset");
1063       else
1064         {
1065           fprintf_unfiltered (file, " %6ld",
1066                               regcache->descr->register_offset[regnum]);
1067           if (register_offset != regcache->descr->register_offset[regnum]
1068               || (regnum > 0
1069                   && (regcache->descr->register_offset[regnum]
1070                       != (regcache->descr->register_offset[regnum - 1]
1071                           + regcache->descr->sizeof_register[regnum - 1])))
1072               )
1073             {
1074               if (!footnote_register_offset)
1075                 footnote_register_offset = ++footnote_nr;
1076               fprintf_unfiltered (file, "*%d", footnote_register_offset);
1077             }
1078           else
1079             fprintf_unfiltered (file, "  ");
1080           register_offset = (regcache->descr->register_offset[regnum]
1081                              + regcache->descr->sizeof_register[regnum]);
1082         }
1083
1084       /* Size.  */
1085       if (regnum < 0)
1086         fprintf_unfiltered (file, " %5s ", "Size");
1087       else
1088         fprintf_unfiltered (file, " %5ld",
1089                             regcache->descr->sizeof_register[regnum]);
1090
1091       /* Type.  */
1092       {
1093         const char *t;
1094         if (regnum < 0)
1095           t = "Type";
1096         else
1097           {
1098             static const char blt[] = "builtin_type";
1099             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
1100             if (t == NULL)
1101               {
1102                 char *n;
1103                 if (!footnote_register_type_name_null)
1104                   footnote_register_type_name_null = ++footnote_nr;
1105                 n = xstrprintf ("*%d", footnote_register_type_name_null);
1106                 make_cleanup (xfree, n);
1107                 t = n;
1108               }
1109             /* Chop a leading builtin_type.  */
1110             if (strncmp (t, blt, strlen (blt)) == 0)
1111               t += strlen (blt);
1112           }
1113         fprintf_unfiltered (file, " %-15s", t);
1114       }
1115
1116       /* Leading space always present.  */
1117       fprintf_unfiltered (file, " ");
1118
1119       /* Value, raw.  */
1120       if (what_to_dump == regcache_dump_raw)
1121         {
1122           if (regnum < 0)
1123             fprintf_unfiltered (file, "Raw value");
1124           else if (regnum >= regcache->descr->nr_raw_registers)
1125             fprintf_unfiltered (file, "<cooked>");
1126           else if (!regcache_valid_p (regcache, regnum))
1127             fprintf_unfiltered (file, "<invalid>");
1128           else
1129             {
1130               regcache_raw_read (regcache, regnum, buf);
1131               fprintf_unfiltered (file, "0x");
1132               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1133                                  regcache->descr->sizeof_register[regnum]);
1134             }
1135         }
1136
1137       /* Value, cooked.  */
1138       if (what_to_dump == regcache_dump_cooked)
1139         {
1140           if (regnum < 0)
1141             fprintf_unfiltered (file, "Cooked value");
1142           else
1143             {
1144               regcache_cooked_read (regcache, regnum, buf);
1145               fprintf_unfiltered (file, "0x");
1146               dump_endian_bytes (file, TARGET_BYTE_ORDER, buf,
1147                                  regcache->descr->sizeof_register[regnum]);
1148             }
1149         }
1150
1151       /* Group members.  */
1152       if (what_to_dump == regcache_dump_groups)
1153         {
1154           if (regnum < 0)
1155             fprintf_unfiltered (file, "Groups");
1156           else
1157             {
1158               const char *sep = "";
1159               struct reggroup *group;
1160               for (group = reggroup_next (gdbarch, NULL);
1161                    group != NULL;
1162                    group = reggroup_next (gdbarch, group))
1163                 {
1164                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
1165                     {
1166                       fprintf_unfiltered (file, "%s%s", sep, reggroup_name (group));
1167                       sep = ",";
1168                     }
1169                 }
1170             }
1171         }
1172
1173       fprintf_unfiltered (file, "\n");
1174     }
1175
1176   if (footnote_register_size)
1177     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
1178                         footnote_register_size);
1179   if (footnote_register_offset)
1180     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
1181                         footnote_register_offset);
1182   if (footnote_register_type_name_null)
1183     fprintf_unfiltered (file, 
1184                         "*%d: Register type's name NULL.\n",
1185                         footnote_register_type_name_null);
1186   do_cleanups (cleanups);
1187 }
1188
1189 static void
1190 regcache_print (char *args, enum regcache_dump_what what_to_dump)
1191 {
1192   if (args == NULL)
1193     regcache_dump (current_regcache, gdb_stdout, what_to_dump);
1194   else
1195     {
1196       struct ui_file *file = gdb_fopen (args, "w");
1197       if (file == NULL)
1198         perror_with_name (_("maintenance print architecture"));
1199       regcache_dump (current_regcache, file, what_to_dump);    
1200       ui_file_delete (file);
1201     }
1202 }
1203
1204 static void
1205 maintenance_print_registers (char *args, int from_tty)
1206 {
1207   regcache_print (args, regcache_dump_none);
1208 }
1209
1210 static void
1211 maintenance_print_raw_registers (char *args, int from_tty)
1212 {
1213   regcache_print (args, regcache_dump_raw);
1214 }
1215
1216 static void
1217 maintenance_print_cooked_registers (char *args, int from_tty)
1218 {
1219   regcache_print (args, regcache_dump_cooked);
1220 }
1221
1222 static void
1223 maintenance_print_register_groups (char *args, int from_tty)
1224 {
1225   regcache_print (args, regcache_dump_groups);
1226 }
1227
1228 extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */
1229
1230 void
1231 _initialize_regcache (void)
1232 {
1233   regcache_descr_handle = gdbarch_data_register_post_init (init_regcache_descr);
1234   DEPRECATED_REGISTER_GDBARCH_SWAP (current_regcache);
1235   deprecated_register_gdbarch_swap (NULL, 0, build_regcache);
1236
1237   observer_attach_target_changed (regcache_observer_target_changed);
1238
1239   add_com ("flushregs", class_maintenance, reg_flush_command,
1240            _("Force gdb to flush its register cache (maintainer command)"));
1241
1242    /* Initialize the thread/process associated with the current set of
1243       registers.  For now, -1 is special, and means `no current process'.  */
1244   registers_ptid = pid_to_ptid (-1);
1245
1246   add_cmd ("registers", class_maintenance, maintenance_print_registers, _("\
1247 Print the internal register configuration.\n\
1248 Takes an optional file parameter."), &maintenanceprintlist);
1249   add_cmd ("raw-registers", class_maintenance,
1250            maintenance_print_raw_registers, _("\
1251 Print the internal register configuration including raw values.\n\
1252 Takes an optional file parameter."), &maintenanceprintlist);
1253   add_cmd ("cooked-registers", class_maintenance,
1254            maintenance_print_cooked_registers, _("\
1255 Print the internal register configuration including cooked values.\n\
1256 Takes an optional file parameter."), &maintenanceprintlist);
1257   add_cmd ("register-groups", class_maintenance,
1258            maintenance_print_register_groups, _("\
1259 Print the internal register configuration including each register's group.\n\
1260 Takes an optional file parameter."),
1261            &maintenanceprintlist);
1262
1263 }