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