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