gdb/regcache.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Cache and manage the values of registers for GDB, the GNU debugger.

  2.    Copyright (C) 1986-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "inferior.h"
  16. #include "target.h"
  17. #include "gdbarch.h"
  18. #include "gdbcmd.h"
  19. #include "regcache.h"
  20. #include "reggroups.h"
  21. #include "observer.h"
  22. #include "remote.h"
  23. #include "valprint.h"
  24. #include "regset.h"

  25. /*
  26. * DATA STRUCTURE
  27. *
  28. * Here is the actual register cache.
  29. */

  30. /* Per-architecture object describing the layout of a register cache.
  31.    Computed once when the architecture is created.  */

  32. struct gdbarch_data *regcache_descr_handle;

  33. struct regcache_descr
  34. {
  35.   /* The architecture this descriptor belongs to.  */
  36.   struct gdbarch *gdbarch;

  37.   /* The raw register cache.  Each raw (or hard) register is supplied
  38.      by the target interface.  The raw cache should not contain
  39.      redundant information - if the PC is constructed from two
  40.      registers then those registers and not the PC lives in the raw
  41.      cache.  */
  42.   int nr_raw_registers;
  43.   long sizeof_raw_registers;
  44.   long sizeof_raw_register_status;

  45.   /* The cooked register space.  Each cooked register in the range
  46.      [0..NR_RAW_REGISTERS) is direct-mapped onto the corresponding raw
  47.      register.  The remaining [NR_RAW_REGISTERS
  48.      .. NR_COOKED_REGISTERS) (a.k.a. pseudo registers) are mapped onto
  49.      both raw registers and memory by the architecture methods
  50.      gdbarch_pseudo_register_read and gdbarch_pseudo_register_write.  */
  51.   int nr_cooked_registers;
  52.   long sizeof_cooked_registers;
  53.   long sizeof_cooked_register_status;

  54.   /* Offset and size (in 8 bit bytes), of each register in the
  55.      register cache.  All registers (including those in the range
  56.      [NR_RAW_REGISTERS .. NR_COOKED_REGISTERS) are given an
  57.      offset.  */
  58.   long *register_offset;
  59.   long *sizeof_register;

  60.   /* Cached table containing the type of each register.  */
  61.   struct type **register_type;
  62. };

  63. static void *
  64. init_regcache_descr (struct gdbarch *gdbarch)
  65. {
  66.   int i;
  67.   struct regcache_descr *descr;
  68.   gdb_assert (gdbarch != NULL);

  69.   /* Create an initial, zero filled, table.  */
  70.   descr = GDBARCH_OBSTACK_ZALLOC (gdbarch, struct regcache_descr);
  71.   descr->gdbarch = gdbarch;

  72.   /* Total size of the register space.  The raw registers are mapped
  73.      directly onto the raw register cache while the pseudo's are
  74.      either mapped onto raw-registers or memory.  */
  75.   descr->nr_cooked_registers = gdbarch_num_regs (gdbarch)
  76.                                + gdbarch_num_pseudo_regs (gdbarch);
  77.   descr->sizeof_cooked_register_status
  78.     = gdbarch_num_regs (gdbarch) + gdbarch_num_pseudo_regs (gdbarch);

  79.   /* Fill in a table of register types.  */
  80.   descr->register_type
  81.     = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers,
  82.                               struct type *);
  83.   for (i = 0; i < descr->nr_cooked_registers; i++)
  84.     descr->register_type[i] = gdbarch_register_type (gdbarch, i);

  85.   /* Construct a strictly RAW register cache.  Don't allow pseudo's
  86.      into the register cache.  */
  87.   descr->nr_raw_registers = gdbarch_num_regs (gdbarch);
  88.   descr->sizeof_raw_register_status = gdbarch_num_regs (gdbarch);

  89.   /* Lay out the register cache.

  90.      NOTE: cagney/2002-05-22: Only register_type() is used when
  91.      constructing the register cache.  It is assumed that the
  92.      register's raw size, virtual size and type length are all the
  93.      same.  */

  94.   {
  95.     long offset = 0;

  96.     descr->sizeof_register
  97.       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
  98.     descr->register_offset
  99.       = GDBARCH_OBSTACK_CALLOC (gdbarch, descr->nr_cooked_registers, long);
  100.     for (i = 0; i < descr->nr_raw_registers; i++)
  101.       {
  102.         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
  103.         descr->register_offset[i] = offset;
  104.         offset += descr->sizeof_register[i];
  105.         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
  106.       }
  107.     /* Set the real size of the raw register cache buffer.  */
  108.     descr->sizeof_raw_registers = offset;

  109.     for (; i < descr->nr_cooked_registers; i++)
  110.       {
  111.         descr->sizeof_register[i] = TYPE_LENGTH (descr->register_type[i]);
  112.         descr->register_offset[i] = offset;
  113.         offset += descr->sizeof_register[i];
  114.         gdb_assert (MAX_REGISTER_SIZE >= descr->sizeof_register[i]);
  115.       }
  116.     /* Set the real size of the readonly register cache buffer.  */
  117.     descr->sizeof_cooked_registers = offset;
  118.   }

  119.   return descr;
  120. }

  121. static struct regcache_descr *
  122. regcache_descr (struct gdbarch *gdbarch)
  123. {
  124.   return gdbarch_data (gdbarch, regcache_descr_handle);
  125. }

  126. /* Utility functions returning useful register attributes stored in
  127.    the regcache descr.  */

  128. struct type *
  129. register_type (struct gdbarch *gdbarch, int regnum)
  130. {
  131.   struct regcache_descr *descr = regcache_descr (gdbarch);

  132.   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
  133.   return descr->register_type[regnum];
  134. }

  135. /* Utility functions returning useful register attributes stored in
  136.    the regcache descr.  */

  137. int
  138. register_size (struct gdbarch *gdbarch, int regnum)
  139. {
  140.   struct regcache_descr *descr = regcache_descr (gdbarch);
  141.   int size;

  142.   gdb_assert (regnum >= 0
  143.               && regnum < (gdbarch_num_regs (gdbarch)
  144.                            + gdbarch_num_pseudo_regs (gdbarch)));
  145.   size = descr->sizeof_register[regnum];
  146.   return size;
  147. }

  148. /* The register cache for storing raw register values.  */

  149. struct regcache
  150. {
  151.   struct regcache_descr *descr;

  152.   /* The address space of this register cache (for registers where it
  153.      makes sense, like PC or SP).  */
  154.   struct address_space *aspace;

  155.   /* The register buffers.  A read-only register cache can hold the
  156.      full [0 .. gdbarch_num_regs + gdbarch_num_pseudo_regs) while a read/write
  157.      register cache can only hold [0 .. gdbarch_num_regs).  */
  158.   gdb_byte *registers;
  159.   /* Register cache status.  */
  160.   signed char *register_status;
  161.   /* Is this a read-only cache?  A read-only cache is used for saving
  162.      the target's register state (e.g, across an inferior function
  163.      call or just before forcing a function return).  A read-only
  164.      cache can only be updated via the methods regcache_dup() and
  165.      regcache_cpy().  The actual contents are determined by the
  166.      reggroup_save and reggroup_restore methods.  */
  167.   int readonly_p;
  168.   /* If this is a read-write cache, which thread's registers is
  169.      it connected to?  */
  170.   ptid_t ptid;
  171. };

  172. static struct regcache *
  173. regcache_xmalloc_1 (struct gdbarch *gdbarch, struct address_space *aspace,
  174.                     int readonly_p)
  175. {
  176.   struct regcache_descr *descr;
  177.   struct regcache *regcache;

  178.   gdb_assert (gdbarch != NULL);
  179.   descr = regcache_descr (gdbarch);
  180.   regcache = XNEW (struct regcache);
  181.   regcache->descr = descr;
  182.   regcache->readonly_p = readonly_p;
  183.   if (readonly_p)
  184.     {
  185.       regcache->registers
  186.         = XCNEWVEC (gdb_byte, descr->sizeof_cooked_registers);
  187.       regcache->register_status
  188.         = XCNEWVEC (signed char, descr->sizeof_cooked_register_status);
  189.     }
  190.   else
  191.     {
  192.       regcache->registers
  193.         = XCNEWVEC (gdb_byte, descr->sizeof_raw_registers);
  194.       regcache->register_status
  195.         = XCNEWVEC (signed char, descr->sizeof_raw_register_status);
  196.     }
  197.   regcache->aspace = aspace;
  198.   regcache->ptid = minus_one_ptid;
  199.   return regcache;
  200. }

  201. struct regcache *
  202. regcache_xmalloc (struct gdbarch *gdbarch, struct address_space *aspace)
  203. {
  204.   return regcache_xmalloc_1 (gdbarch, aspace, 1);
  205. }

  206. void
  207. regcache_xfree (struct regcache *regcache)
  208. {
  209.   if (regcache == NULL)
  210.     return;
  211.   xfree (regcache->registers);
  212.   xfree (regcache->register_status);
  213.   xfree (regcache);
  214. }

  215. static void
  216. do_regcache_xfree (void *data)
  217. {
  218.   regcache_xfree (data);
  219. }

  220. struct cleanup *
  221. make_cleanup_regcache_xfree (struct regcache *regcache)
  222. {
  223.   return make_cleanup (do_regcache_xfree, regcache);
  224. }

  225. /* Cleanup routines for invalidating a register.  */

  226. struct register_to_invalidate
  227. {
  228.   struct regcache *regcache;
  229.   int regnum;
  230. };

  231. static void
  232. do_regcache_invalidate (void *data)
  233. {
  234.   struct register_to_invalidate *reg = data;

  235.   regcache_invalidate (reg->regcache, reg->regnum);
  236. }

  237. static struct cleanup *
  238. make_cleanup_regcache_invalidate (struct regcache *regcache, int regnum)
  239. {
  240.   struct register_to_invalidate* reg = XNEW (struct register_to_invalidate);

  241.   reg->regcache = regcache;
  242.   reg->regnum = regnum;
  243.   return make_cleanup_dtor (do_regcache_invalidate, (void *) reg, xfree);
  244. }

  245. /* Return REGCACHE's architecture.  */

  246. struct gdbarch *
  247. get_regcache_arch (const struct regcache *regcache)
  248. {
  249.   return regcache->descr->gdbarch;
  250. }

  251. struct address_space *
  252. get_regcache_aspace (const struct regcache *regcache)
  253. {
  254.   return regcache->aspace;
  255. }

  256. /* Return  a pointer to register REGNUM's buffer cache.  */

  257. static gdb_byte *
  258. register_buffer (const struct regcache *regcache, int regnum)
  259. {
  260.   return regcache->registers + regcache->descr->register_offset[regnum];
  261. }

  262. void
  263. regcache_save (struct regcache *dst, regcache_cooked_read_ftype *cooked_read,
  264.                void *src)
  265. {
  266.   struct gdbarch *gdbarch = dst->descr->gdbarch;
  267.   gdb_byte buf[MAX_REGISTER_SIZE];
  268.   int regnum;

  269.   /* The DST should be `read-only', if it wasn't then the save would
  270.      end up trying to write the register values back out to the
  271.      target.  */
  272.   gdb_assert (dst->readonly_p);
  273.   /* Clear the dest.  */
  274.   memset (dst->registers, 0, dst->descr->sizeof_cooked_registers);
  275.   memset (dst->register_status, 0,
  276.           dst->descr->sizeof_cooked_register_status);
  277.   /* Copy over any registers (identified by their membership in the
  278.      save_reggroup) and mark them as valid.  The full [0 .. gdbarch_num_regs +
  279.      gdbarch_num_pseudo_regs) range is checked since some architectures need
  280.      to save/restore `cooked' registers that live in memory.  */
  281.   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
  282.     {
  283.       if (gdbarch_register_reggroup_p (gdbarch, regnum, save_reggroup))
  284.         {
  285.           enum register_status status = cooked_read (src, regnum, buf);

  286.           if (status == REG_VALID)
  287.             memcpy (register_buffer (dst, regnum), buf,
  288.                     register_size (gdbarch, regnum));
  289.           else
  290.             {
  291.               gdb_assert (status != REG_UNKNOWN);

  292.               memset (register_buffer (dst, regnum), 0,
  293.                       register_size (gdbarch, regnum));
  294.             }
  295.           dst->register_status[regnum] = status;
  296.         }
  297.     }
  298. }

  299. static void
  300. regcache_restore (struct regcache *dst,
  301.                   regcache_cooked_read_ftype *cooked_read,
  302.                   void *cooked_read_context)
  303. {
  304.   struct gdbarch *gdbarch = dst->descr->gdbarch;
  305.   gdb_byte buf[MAX_REGISTER_SIZE];
  306.   int regnum;

  307.   /* The dst had better not be read-only.  If it is, the `restore'
  308.      doesn't make much sense.  */
  309.   gdb_assert (!dst->readonly_p);
  310.   /* Copy over any registers, being careful to only restore those that
  311.      were both saved and need to be restored.  The full [0 .. gdbarch_num_regs
  312.      + gdbarch_num_pseudo_regs) range is checked since some architectures need
  313.      to save/restore `cooked' registers that live in memory.  */
  314.   for (regnum = 0; regnum < dst->descr->nr_cooked_registers; regnum++)
  315.     {
  316.       if (gdbarch_register_reggroup_p (gdbarch, regnum, restore_reggroup))
  317.         {
  318.           enum register_status status;

  319.           status = cooked_read (cooked_read_context, regnum, buf);
  320.           if (status == REG_VALID)
  321.             regcache_cooked_write (dst, regnum, buf);
  322.         }
  323.     }
  324. }

  325. static enum register_status
  326. do_cooked_read (void *src, int regnum, gdb_byte *buf)
  327. {
  328.   struct regcache *regcache = src;

  329.   return regcache_cooked_read (regcache, regnum, buf);
  330. }

  331. void
  332. regcache_cpy (struct regcache *dst, struct regcache *src)
  333. {
  334.   gdb_assert (src != NULL && dst != NULL);
  335.   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
  336.   gdb_assert (src != dst);
  337.   gdb_assert (src->readonly_p || dst->readonly_p);

  338.   if (!src->readonly_p)
  339.     regcache_save (dst, do_cooked_read, src);
  340.   else if (!dst->readonly_p)
  341.     regcache_restore (dst, do_cooked_read, src);
  342.   else
  343.     regcache_cpy_no_passthrough (dst, src);
  344. }

  345. void
  346. regcache_cpy_no_passthrough (struct regcache *dst, struct regcache *src)
  347. {
  348.   gdb_assert (src != NULL && dst != NULL);
  349.   gdb_assert (src->descr->gdbarch == dst->descr->gdbarch);
  350.   /* NOTE: cagney/2002-05-17: Don't let the caller do a no-passthrough
  351.      move of data into a thread's regcache.  Doing this would be silly
  352.      - it would mean that regcache->register_status would be
  353.      completely invalid.  */
  354.   gdb_assert (dst->readonly_p && src->readonly_p);

  355.   memcpy (dst->registers, src->registers,
  356.           dst->descr->sizeof_cooked_registers);
  357.   memcpy (dst->register_status, src->register_status,
  358.           dst->descr->sizeof_cooked_register_status);
  359. }

  360. struct regcache *
  361. regcache_dup (struct regcache *src)
  362. {
  363.   struct regcache *newbuf;

  364.   newbuf = regcache_xmalloc (src->descr->gdbarch, get_regcache_aspace (src));
  365.   regcache_cpy (newbuf, src);
  366.   return newbuf;
  367. }

  368. enum register_status
  369. regcache_register_status (const struct regcache *regcache, int regnum)
  370. {
  371.   gdb_assert (regcache != NULL);
  372.   gdb_assert (regnum >= 0);
  373.   if (regcache->readonly_p)
  374.     gdb_assert (regnum < regcache->descr->nr_cooked_registers);
  375.   else
  376.     gdb_assert (regnum < regcache->descr->nr_raw_registers);

  377.   return regcache->register_status[regnum];
  378. }

  379. void
  380. regcache_invalidate (struct regcache *regcache, int regnum)
  381. {
  382.   gdb_assert (regcache != NULL);
  383.   gdb_assert (regnum >= 0);
  384.   gdb_assert (!regcache->readonly_p);
  385.   gdb_assert (regnum < regcache->descr->nr_raw_registers);
  386.   regcache->register_status[regnum] = REG_UNKNOWN;
  387. }


  388. /* Global structure containing the current regcache.  */

  389. /* NOTE: this is a write-through cache.  There is no "dirty" bit for
  390.    recording if the register values have been changed (eg. by the
  391.    user).  Therefore all registers must be written back to the
  392.    target when appropriate.  */

  393. struct regcache_list
  394. {
  395.   struct regcache *regcache;
  396.   struct regcache_list *next;
  397. };

  398. static struct regcache_list *current_regcache;

  399. struct regcache *
  400. get_thread_arch_aspace_regcache (ptid_t ptid, struct gdbarch *gdbarch,
  401.                                  struct address_space *aspace)
  402. {
  403.   struct regcache_list *list;
  404.   struct regcache *new_regcache;

  405.   for (list = current_regcache; list; list = list->next)
  406.     if (ptid_equal (list->regcache->ptid, ptid)
  407.         && get_regcache_arch (list->regcache) == gdbarch)
  408.       return list->regcache;

  409.   new_regcache = regcache_xmalloc_1 (gdbarch, aspace, 0);
  410.   new_regcache->ptid = ptid;

  411.   list = xmalloc (sizeof (struct regcache_list));
  412.   list->regcache = new_regcache;
  413.   list->next = current_regcache;
  414.   current_regcache = list;

  415.   return new_regcache;
  416. }

  417. struct regcache *
  418. get_thread_arch_regcache (ptid_t ptid, struct gdbarch *gdbarch)
  419. {
  420.   struct address_space *aspace;

  421.   /* For the benefit of "maint print registers" & co when debugging an
  422.      executable, allow dumping the regcache even when there is no
  423.      thread selected (target_thread_address_space internal-errors if
  424.      no address space is found).  Note that normal user commands will
  425.      fail higher up on the call stack due to no
  426.      target_has_registers.  */
  427.   aspace = (ptid_equal (null_ptid, ptid)
  428.             ? NULL
  429.             : target_thread_address_space (ptid));

  430.   return get_thread_arch_aspace_regcache  (ptid, gdbarch, aspace);
  431. }

  432. static ptid_t current_thread_ptid;
  433. static struct gdbarch *current_thread_arch;

  434. struct regcache *
  435. get_thread_regcache (ptid_t ptid)
  436. {
  437.   if (!current_thread_arch || !ptid_equal (current_thread_ptid, ptid))
  438.     {
  439.       current_thread_ptid = ptid;
  440.       current_thread_arch = target_thread_architecture (ptid);
  441.     }

  442.   return get_thread_arch_regcache (ptid, current_thread_arch);
  443. }

  444. struct regcache *
  445. get_current_regcache (void)
  446. {
  447.   return get_thread_regcache (inferior_ptid);
  448. }

  449. /* See common/common-regcache.h.  */

  450. struct regcache *
  451. get_thread_regcache_for_ptid (ptid_t ptid)
  452. {
  453.   return get_thread_regcache (ptid);
  454. }

  455. /* Observer for the target_changed event.  */

  456. static void
  457. regcache_observer_target_changed (struct target_ops *target)
  458. {
  459.   registers_changed ();
  460. }

  461. /* Update global variables old ptids to hold NEW_PTID if they were
  462.    holding OLD_PTID.  */
  463. static void
  464. regcache_thread_ptid_changed (ptid_t old_ptid, ptid_t new_ptid)
  465. {
  466.   struct regcache_list *list;

  467.   for (list = current_regcache; list; list = list->next)
  468.     if (ptid_equal (list->regcache->ptid, old_ptid))
  469.       list->regcache->ptid = new_ptid;
  470. }

  471. /* Low level examining and depositing of registers.

  472.    The caller is responsible for making sure that the inferior is
  473.    stopped before calling the fetching routines, or it will get
  474.    garbage.  (a change from GDB version 3, in which the caller got the
  475.    value from the last stop).  */

  476. /* REGISTERS_CHANGED ()

  477.    Indicate that registers may have changed, so invalidate the cache.  */

  478. void
  479. registers_changed_ptid (ptid_t ptid)
  480. {
  481.   struct regcache_list *list, **list_link;

  482.   list = current_regcache;
  483.   list_link = &current_regcache;
  484.   while (list)
  485.     {
  486.       if (ptid_match (list->regcache->ptid, ptid))
  487.         {
  488.           struct regcache_list *dead = list;

  489.           *list_link = list->next;
  490.           regcache_xfree (list->regcache);
  491.           list = *list_link;
  492.           xfree (dead);
  493.           continue;
  494.         }

  495.       list_link = &list->next;
  496.       list = *list_link;
  497.     }

  498.   if (ptid_match (current_thread_ptid, ptid))
  499.     {
  500.       current_thread_ptid = null_ptid;
  501.       current_thread_arch = NULL;
  502.     }

  503.   if (ptid_match (inferior_ptid, ptid))
  504.     {
  505.       /* We just deleted the regcache of the current thread.  Need to
  506.          forget about any frames we have cached, too.  */
  507.       reinit_frame_cache ();
  508.     }
  509. }

  510. void
  511. registers_changed (void)
  512. {
  513.   registers_changed_ptid (minus_one_ptid);

  514.   /* Force cleanup of any alloca areas if using C alloca instead of
  515.      a builtin alloca.  This particular call is used to clean up
  516.      areas allocated by low level target code which may build up
  517.      during lengthy interactions between gdb and the target before
  518.      gdb gives control to the user (ie watchpoints).  */
  519.   alloca (0);
  520. }

  521. enum register_status
  522. regcache_raw_read (struct regcache *regcache, int regnum, gdb_byte *buf)
  523. {
  524.   gdb_assert (regcache != NULL && buf != NULL);
  525.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
  526.   /* Make certain that the register cache is up-to-date with respect
  527.      to the current thread.  This switching shouldn't be necessary
  528.      only there is still only one target side register cache.  Sigh!
  529.      On the bright side, at least there is a regcache object.  */
  530.   if (!regcache->readonly_p
  531.       && regcache_register_status (regcache, regnum) == REG_UNKNOWN)
  532.     {
  533.       struct cleanup *old_chain = save_inferior_ptid ();

  534.       inferior_ptid = regcache->ptid;
  535.       target_fetch_registers (regcache, regnum);
  536.       do_cleanups (old_chain);

  537.       /* A number of targets can't access the whole set of raw
  538.          registers (because the debug API provides no means to get at
  539.          them).  */
  540.       if (regcache->register_status[regnum] == REG_UNKNOWN)
  541.         regcache->register_status[regnum] = REG_UNAVAILABLE;
  542.     }

  543.   if (regcache->register_status[regnum] != REG_VALID)
  544.     memset (buf, 0, regcache->descr->sizeof_register[regnum]);
  545.   else
  546.     memcpy (buf, register_buffer (regcache, regnum),
  547.             regcache->descr->sizeof_register[regnum]);

  548.   return regcache->register_status[regnum];
  549. }

  550. enum register_status
  551. regcache_raw_read_signed (struct regcache *regcache, int regnum, LONGEST *val)
  552. {
  553.   gdb_byte *buf;
  554.   enum register_status status;

  555.   gdb_assert (regcache != NULL);
  556.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
  557.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  558.   status = regcache_raw_read (regcache, regnum, buf);
  559.   if (status == REG_VALID)
  560.     *val = extract_signed_integer
  561.       (buf, regcache->descr->sizeof_register[regnum],
  562.        gdbarch_byte_order (regcache->descr->gdbarch));
  563.   else
  564.     *val = 0;
  565.   return status;
  566. }

  567. enum register_status
  568. regcache_raw_read_unsigned (struct regcache *regcache, int regnum,
  569.                             ULONGEST *val)
  570. {
  571.   gdb_byte *buf;
  572.   enum register_status status;

  573.   gdb_assert (regcache != NULL);
  574.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
  575.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  576.   status = regcache_raw_read (regcache, regnum, buf);
  577.   if (status == REG_VALID)
  578.     *val = extract_unsigned_integer
  579.       (buf, regcache->descr->sizeof_register[regnum],
  580.        gdbarch_byte_order (regcache->descr->gdbarch));
  581.   else
  582.     *val = 0;
  583.   return status;
  584. }

  585. void
  586. regcache_raw_write_signed (struct regcache *regcache, int regnum, LONGEST val)
  587. {
  588.   void *buf;

  589.   gdb_assert (regcache != NULL);
  590.   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
  591.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  592.   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
  593.                         gdbarch_byte_order (regcache->descr->gdbarch), val);
  594.   regcache_raw_write (regcache, regnum, buf);
  595. }

  596. void
  597. regcache_raw_write_unsigned (struct regcache *regcache, int regnum,
  598.                              ULONGEST val)
  599. {
  600.   void *buf;

  601.   gdb_assert (regcache != NULL);
  602.   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_raw_registers);
  603.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  604.   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
  605.                           gdbarch_byte_order (regcache->descr->gdbarch), val);
  606.   regcache_raw_write (regcache, regnum, buf);
  607. }

  608. enum register_status
  609. regcache_cooked_read (struct regcache *regcache, int regnum, gdb_byte *buf)
  610. {
  611.   gdb_assert (regnum >= 0);
  612.   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
  613.   if (regnum < regcache->descr->nr_raw_registers)
  614.     return regcache_raw_read (regcache, regnum, buf);
  615.   else if (regcache->readonly_p
  616.            && regcache->register_status[regnum] != REG_UNKNOWN)
  617.     {
  618.       /* Read-only register cache, perhaps the cooked value was
  619.          cached?  */
  620.       if (regcache->register_status[regnum] == REG_VALID)
  621.         memcpy (buf, register_buffer (regcache, regnum),
  622.                 regcache->descr->sizeof_register[regnum]);
  623.       else
  624.         memset (buf, 0, regcache->descr->sizeof_register[regnum]);

  625.       return regcache->register_status[regnum];
  626.     }
  627.   else if (gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
  628.     {
  629.       struct value *mark, *computed;
  630.       enum register_status result = REG_VALID;

  631.       mark = value_mark ();

  632.       computed = gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
  633.                                                      regcache, regnum);
  634.       if (value_entirely_available (computed))
  635.         memcpy (buf, value_contents_raw (computed),
  636.                 regcache->descr->sizeof_register[regnum]);
  637.       else
  638.         {
  639.           memset (buf, 0, regcache->descr->sizeof_register[regnum]);
  640.           result = REG_UNAVAILABLE;
  641.         }

  642.       value_free_to_mark (mark);

  643.       return result;
  644.     }
  645.   else
  646.     return gdbarch_pseudo_register_read (regcache->descr->gdbarch, regcache,
  647.                                          regnum, buf);
  648. }

  649. struct value *
  650. regcache_cooked_read_value (struct regcache *regcache, int regnum)
  651. {
  652.   gdb_assert (regnum >= 0);
  653.   gdb_assert (regnum < regcache->descr->nr_cooked_registers);

  654.   if (regnum < regcache->descr->nr_raw_registers
  655.       || (regcache->readonly_p
  656.           && regcache->register_status[regnum] != REG_UNKNOWN)
  657.       || !gdbarch_pseudo_register_read_value_p (regcache->descr->gdbarch))
  658.     {
  659.       struct value *result;

  660.       result = allocate_value (register_type (regcache->descr->gdbarch,
  661.                                               regnum));
  662.       VALUE_LVAL (result) = lval_register;
  663.       VALUE_REGNUM (result) = regnum;

  664.       /* It is more efficient in general to do this delegation in this
  665.          direction than in the other one, even though the value-based
  666.          API is preferred.  */
  667.       if (regcache_cooked_read (regcache, regnum,
  668.                                 value_contents_raw (result)) == REG_UNAVAILABLE)
  669.         mark_value_bytes_unavailable (result, 0,
  670.                                       TYPE_LENGTH (value_type (result)));

  671.       return result;
  672.     }
  673.   else
  674.     return gdbarch_pseudo_register_read_value (regcache->descr->gdbarch,
  675.                                                regcache, regnum);
  676. }

  677. enum register_status
  678. regcache_cooked_read_signed (struct regcache *regcache, int regnum,
  679.                              LONGEST *val)
  680. {
  681.   enum register_status status;
  682.   gdb_byte *buf;

  683.   gdb_assert (regcache != NULL);
  684.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
  685.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  686.   status = regcache_cooked_read (regcache, regnum, buf);
  687.   if (status == REG_VALID)
  688.     *val = extract_signed_integer
  689.       (buf, regcache->descr->sizeof_register[regnum],
  690.        gdbarch_byte_order (regcache->descr->gdbarch));
  691.   else
  692.     *val = 0;
  693.   return status;
  694. }

  695. enum register_status
  696. regcache_cooked_read_unsigned (struct regcache *regcache, int regnum,
  697.                                ULONGEST *val)
  698. {
  699.   enum register_status status;
  700.   gdb_byte *buf;

  701.   gdb_assert (regcache != NULL);
  702.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_cooked_registers);
  703.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  704.   status = regcache_cooked_read (regcache, regnum, buf);
  705.   if (status == REG_VALID)
  706.     *val = extract_unsigned_integer
  707.       (buf, regcache->descr->sizeof_register[regnum],
  708.        gdbarch_byte_order (regcache->descr->gdbarch));
  709.   else
  710.     *val = 0;
  711.   return status;
  712. }

  713. void
  714. regcache_cooked_write_signed (struct regcache *regcache, int regnum,
  715.                               LONGEST val)
  716. {
  717.   void *buf;

  718.   gdb_assert (regcache != NULL);
  719.   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
  720.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  721.   store_signed_integer (buf, regcache->descr->sizeof_register[regnum],
  722.                         gdbarch_byte_order (regcache->descr->gdbarch), val);
  723.   regcache_cooked_write (regcache, regnum, buf);
  724. }

  725. void
  726. regcache_cooked_write_unsigned (struct regcache *regcache, int regnum,
  727.                                 ULONGEST val)
  728. {
  729.   void *buf;

  730.   gdb_assert (regcache != NULL);
  731.   gdb_assert (regnum >=0 && regnum < regcache->descr->nr_cooked_registers);
  732.   buf = alloca (regcache->descr->sizeof_register[regnum]);
  733.   store_unsigned_integer (buf, regcache->descr->sizeof_register[regnum],
  734.                           gdbarch_byte_order (regcache->descr->gdbarch), val);
  735.   regcache_cooked_write (regcache, regnum, buf);
  736. }

  737. void
  738. regcache_raw_write (struct regcache *regcache, int regnum,
  739.                     const gdb_byte *buf)
  740. {
  741.   struct cleanup *chain_before_save_inferior;
  742.   struct cleanup *chain_before_invalidate_register;

  743.   gdb_assert (regcache != NULL && buf != NULL);
  744.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
  745.   gdb_assert (!regcache->readonly_p);

  746.   /* On the sparc, writing %g0 is a no-op, so we don't even want to
  747.      change the registers array if something writes to this register.  */
  748.   if (gdbarch_cannot_store_register (get_regcache_arch (regcache), regnum))
  749.     return;

  750.   /* If we have a valid copy of the register, and new value == old
  751.      value, then don't bother doing the actual store.  */
  752.   if (regcache_register_status (regcache, regnum) == REG_VALID
  753.       && (memcmp (register_buffer (regcache, regnum), buf,
  754.                   regcache->descr->sizeof_register[regnum]) == 0))
  755.     return;

  756.   chain_before_save_inferior = save_inferior_ptid ();
  757.   inferior_ptid = regcache->ptid;

  758.   target_prepare_to_store (regcache);
  759.   memcpy (register_buffer (regcache, regnum), buf,
  760.           regcache->descr->sizeof_register[regnum]);
  761.   regcache->register_status[regnum] = REG_VALID;

  762.   /* Register a cleanup function for invalidating the register after it is
  763.      written, in case of a failure.  */
  764.   chain_before_invalidate_register
  765.     = make_cleanup_regcache_invalidate (regcache, regnum);

  766.   target_store_registers (regcache, regnum);

  767.   /* The target did not throw an error so we can discard invalidating the
  768.      register and restore the cleanup chain to what it was.  */
  769.   discard_cleanups (chain_before_invalidate_register);

  770.   do_cleanups (chain_before_save_inferior);
  771. }

  772. void
  773. regcache_cooked_write (struct regcache *regcache, int regnum,
  774.                        const gdb_byte *buf)
  775. {
  776.   gdb_assert (regnum >= 0);
  777.   gdb_assert (regnum < regcache->descr->nr_cooked_registers);
  778.   if (regnum < regcache->descr->nr_raw_registers)
  779.     regcache_raw_write (regcache, regnum, buf);
  780.   else
  781.     gdbarch_pseudo_register_write (regcache->descr->gdbarch, regcache,
  782.                                    regnum, buf);
  783. }

  784. /* Perform a partial register transfer using a read, modify, write
  785.    operation.  */

  786. typedef void (regcache_read_ftype) (struct regcache *regcache, int regnum,
  787.                                     void *buf);
  788. typedef void (regcache_write_ftype) (struct regcache *regcache, int regnum,
  789.                                      const void *buf);

  790. static enum register_status
  791. regcache_xfer_part (struct regcache *regcache, int regnum,
  792.                     int offset, int len, void *in, const void *out,
  793.                     enum register_status (*read) (struct regcache *regcache,
  794.                                                   int regnum,
  795.                                                   gdb_byte *buf),
  796.                     void (*write) (struct regcache *regcache, int regnum,
  797.                                    const gdb_byte *buf))
  798. {
  799.   struct regcache_descr *descr = regcache->descr;
  800.   gdb_byte reg[MAX_REGISTER_SIZE];

  801.   gdb_assert (offset >= 0 && offset <= descr->sizeof_register[regnum]);
  802.   gdb_assert (len >= 0 && offset + len <= descr->sizeof_register[regnum]);
  803.   /* Something to do?  */
  804.   if (offset + len == 0)
  805.     return REG_VALID;
  806.   /* Read (when needed) ...  */
  807.   if (in != NULL
  808.       || offset > 0
  809.       || offset + len < descr->sizeof_register[regnum])
  810.     {
  811.       enum register_status status;

  812.       gdb_assert (read != NULL);
  813.       status = read (regcache, regnum, reg);
  814.       if (status != REG_VALID)
  815.         return status;
  816.     }
  817.   /* ... modify ...  */
  818.   if (in != NULL)
  819.     memcpy (in, reg + offset, len);
  820.   if (out != NULL)
  821.     memcpy (reg + offset, out, len);
  822.   /* ... write (when needed).  */
  823.   if (out != NULL)
  824.     {
  825.       gdb_assert (write != NULL);
  826.       write (regcache, regnum, reg);
  827.     }

  828.   return REG_VALID;
  829. }

  830. enum register_status
  831. regcache_raw_read_part (struct regcache *regcache, int regnum,
  832.                         int offset, int len, gdb_byte *buf)
  833. {
  834.   struct regcache_descr *descr = regcache->descr;

  835.   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
  836.   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
  837.                              regcache_raw_read, regcache_raw_write);
  838. }

  839. void
  840. regcache_raw_write_part (struct regcache *regcache, int regnum,
  841.                          int offset, int len, const gdb_byte *buf)
  842. {
  843.   struct regcache_descr *descr = regcache->descr;

  844.   gdb_assert (regnum >= 0 && regnum < descr->nr_raw_registers);
  845.   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
  846.                       regcache_raw_read, regcache_raw_write);
  847. }

  848. enum register_status
  849. regcache_cooked_read_part (struct regcache *regcache, int regnum,
  850.                            int offset, int len, gdb_byte *buf)
  851. {
  852.   struct regcache_descr *descr = regcache->descr;

  853.   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
  854.   return regcache_xfer_part (regcache, regnum, offset, len, buf, NULL,
  855.                              regcache_cooked_read, regcache_cooked_write);
  856. }

  857. void
  858. regcache_cooked_write_part (struct regcache *regcache, int regnum,
  859.                             int offset, int len, const gdb_byte *buf)
  860. {
  861.   struct regcache_descr *descr = regcache->descr;

  862.   gdb_assert (regnum >= 0 && regnum < descr->nr_cooked_registers);
  863.   regcache_xfer_part (regcache, regnum, offset, len, NULL, buf,
  864.                       regcache_cooked_read, regcache_cooked_write);
  865. }

  866. /* Supply register REGNUM, whose contents are stored in BUF, to REGCACHE.  */

  867. void
  868. regcache_raw_supply (struct regcache *regcache, int regnum, const void *buf)
  869. {
  870.   void *regbuf;
  871.   size_t size;

  872.   gdb_assert (regcache != NULL);
  873.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);
  874.   gdb_assert (!regcache->readonly_p);

  875.   regbuf = register_buffer (regcache, regnum);
  876.   size = regcache->descr->sizeof_register[regnum];

  877.   if (buf)
  878.     {
  879.       memcpy (regbuf, buf, size);
  880.       regcache->register_status[regnum] = REG_VALID;
  881.     }
  882.   else
  883.     {
  884.       /* This memset not strictly necessary, but better than garbage
  885.          in case the register value manages to escape somewhere (due
  886.          to a bug, no less).  */
  887.       memset (regbuf, 0, size);
  888.       regcache->register_status[regnum] = REG_UNAVAILABLE;
  889.     }
  890. }

  891. /* Collect register REGNUM from REGCACHE and store its contents in BUF.  */

  892. void
  893. regcache_raw_collect (const struct regcache *regcache, int regnum, void *buf)
  894. {
  895.   const void *regbuf;
  896.   size_t size;

  897.   gdb_assert (regcache != NULL && buf != NULL);
  898.   gdb_assert (regnum >= 0 && regnum < regcache->descr->nr_raw_registers);

  899.   regbuf = register_buffer (regcache, regnum);
  900.   size = regcache->descr->sizeof_register[regnum];
  901.   memcpy (buf, regbuf, size);
  902. }

  903. /* Transfer a single or all registers belonging to a certain register
  904.    set to or from a buffer.  This is the main worker function for
  905.    regcache_supply_regset and regcache_collect_regset.  */

  906. static void
  907. regcache_transfer_regset (const struct regset *regset,
  908.                           const struct regcache *regcache,
  909.                           struct regcache *out_regcache,
  910.                           int regnum, const void *in_buf,
  911.                           void *out_buf, size_t size)
  912. {
  913.   const struct regcache_map_entry *map;
  914.   int offs = 0, count;

  915.   for (map = regset->regmap; (count = map->count) != 0; map++)
  916.     {
  917.       int regno = map->regno;
  918.       int slot_size = map->size;

  919.       if (slot_size == 0 && regno != REGCACHE_MAP_SKIP)
  920.         slot_size = regcache->descr->sizeof_register[regno];

  921.       if (regno == REGCACHE_MAP_SKIP
  922.           || (regnum != -1
  923.               && (regnum < regno || regnum >= regno + count)))
  924.           offs += count * slot_size;

  925.       else if (regnum == -1)
  926.         for (; count--; regno++, offs += slot_size)
  927.           {
  928.             if (offs + slot_size > size)
  929.               break;

  930.             if (out_buf)
  931.               regcache_raw_collect (regcache, regno,
  932.                                     (gdb_byte *) out_buf + offs);
  933.             else
  934.               regcache_raw_supply (out_regcache, regno, in_buf
  935.                                    ? (const gdb_byte *) in_buf + offs
  936.                                    : NULL);
  937.           }
  938.       else
  939.         {
  940.           /* Transfer a single register and return.  */
  941.           offs += (regnum - regno) * slot_size;
  942.           if (offs + slot_size > size)
  943.             return;

  944.           if (out_buf)
  945.             regcache_raw_collect (regcache, regnum,
  946.                                   (gdb_byte *) out_buf + offs);
  947.           else
  948.             regcache_raw_supply (out_regcache, regnum, in_buf
  949.                                  ? (const gdb_byte *) in_buf + offs
  950.                                  : NULL);
  951.           return;
  952.         }
  953.     }
  954. }

  955. /* Supply register REGNUM from BUF to REGCACHE, using the register map
  956.    in REGSET.  If REGNUM is -1, do this for all registers in REGSET.
  957.    If BUF is NULL, set the register(s) to "unavailable" status. */

  958. void
  959. regcache_supply_regset (const struct regset *regset,
  960.                         struct regcache *regcache,
  961.                         int regnum, const void *buf, size_t size)
  962. {
  963.   regcache_transfer_regset (regset, regcache, regcache, regnum,
  964.                             buf, NULL, size);
  965. }

  966. /* Collect register REGNUM from REGCACHE to BUF, using the register
  967.    map in REGSET.  If REGNUM is -1, do this for all registers in
  968.    REGSET.  */

  969. void
  970. regcache_collect_regset (const struct regset *regset,
  971.                          const struct regcache *regcache,
  972.                          int regnum, void *buf, size_t size)
  973. {
  974.   regcache_transfer_regset (regset, regcache, NULL, regnum,
  975.                             NULL, buf, size);
  976. }


  977. /* Special handling for register PC.  */

  978. CORE_ADDR
  979. regcache_read_pc (struct regcache *regcache)
  980. {
  981.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  982.   CORE_ADDR pc_val;

  983.   if (gdbarch_read_pc_p (gdbarch))
  984.     pc_val = gdbarch_read_pc (gdbarch, regcache);
  985.   /* Else use per-frame method on get_current_frame.  */
  986.   else if (gdbarch_pc_regnum (gdbarch) >= 0)
  987.     {
  988.       ULONGEST raw_val;

  989.       if (regcache_cooked_read_unsigned (regcache,
  990.                                          gdbarch_pc_regnum (gdbarch),
  991.                                          &raw_val) == REG_UNAVAILABLE)
  992.         throw_error (NOT_AVAILABLE_ERROR, _("PC register is not available"));

  993.       pc_val = gdbarch_addr_bits_remove (gdbarch, raw_val);
  994.     }
  995.   else
  996.     internal_error (__FILE__, __LINE__,
  997.                     _("regcache_read_pc: Unable to find PC"));
  998.   return pc_val;
  999. }

  1000. void
  1001. regcache_write_pc (struct regcache *regcache, CORE_ADDR pc)
  1002. {
  1003.   struct gdbarch *gdbarch = get_regcache_arch (regcache);

  1004.   if (gdbarch_write_pc_p (gdbarch))
  1005.     gdbarch_write_pc (gdbarch, regcache, pc);
  1006.   else if (gdbarch_pc_regnum (gdbarch) >= 0)
  1007.     regcache_cooked_write_unsigned (regcache,
  1008.                                     gdbarch_pc_regnum (gdbarch), pc);
  1009.   else
  1010.     internal_error (__FILE__, __LINE__,
  1011.                     _("regcache_write_pc: Unable to update PC"));

  1012.   /* Writing the PC (for instance, from "load") invalidates the
  1013.      current frame.  */
  1014.   reinit_frame_cache ();
  1015. }


  1016. static void
  1017. reg_flush_command (char *command, int from_tty)
  1018. {
  1019.   /* Force-flush the register cache.  */
  1020.   registers_changed ();
  1021.   if (from_tty)
  1022.     printf_filtered (_("Register cache flushed.\n"));
  1023. }

  1024. enum regcache_dump_what
  1025. {
  1026.   regcache_dump_none, regcache_dump_raw,
  1027.   regcache_dump_cooked, regcache_dump_groups,
  1028.   regcache_dump_remote
  1029. };

  1030. static void
  1031. regcache_dump (struct regcache *regcache, struct ui_file *file,
  1032.                enum regcache_dump_what what_to_dump)
  1033. {
  1034.   struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
  1035.   struct gdbarch *gdbarch = regcache->descr->gdbarch;
  1036.   int regnum;
  1037.   int footnote_nr = 0;
  1038.   int footnote_register_size = 0;
  1039.   int footnote_register_offset = 0;
  1040.   int footnote_register_type_name_null = 0;
  1041.   long register_offset = 0;
  1042.   gdb_byte buf[MAX_REGISTER_SIZE];

  1043. #if 0
  1044.   fprintf_unfiltered (file, "nr_raw_registers %d\n",
  1045.                       regcache->descr->nr_raw_registers);
  1046.   fprintf_unfiltered (file, "nr_cooked_registers %d\n",
  1047.                       regcache->descr->nr_cooked_registers);
  1048.   fprintf_unfiltered (file, "sizeof_raw_registers %ld\n",
  1049.                       regcache->descr->sizeof_raw_registers);
  1050.   fprintf_unfiltered (file, "sizeof_raw_register_status %ld\n",
  1051.                       regcache->descr->sizeof_raw_register_status);
  1052.   fprintf_unfiltered (file, "gdbarch_num_regs %d\n",
  1053.                       gdbarch_num_regs (gdbarch));
  1054.   fprintf_unfiltered (file, "gdbarch_num_pseudo_regs %d\n",
  1055.                       gdbarch_num_pseudo_regs (gdbarch));
  1056. #endif

  1057.   gdb_assert (regcache->descr->nr_cooked_registers
  1058.               == (gdbarch_num_regs (gdbarch)
  1059.                   + gdbarch_num_pseudo_regs (gdbarch)));

  1060.   for (regnum = -1; regnum < regcache->descr->nr_cooked_registers; regnum++)
  1061.     {
  1062.       /* Name.  */
  1063.       if (regnum < 0)
  1064.         fprintf_unfiltered (file, " %-10s", "Name");
  1065.       else
  1066.         {
  1067.           const char *p = gdbarch_register_name (gdbarch, regnum);

  1068.           if (p == NULL)
  1069.             p = "";
  1070.           else if (p[0] == '\0')
  1071.             p = "''";
  1072.           fprintf_unfiltered (file, " %-10s", p);
  1073.         }

  1074.       /* Number.  */
  1075.       if (regnum < 0)
  1076.         fprintf_unfiltered (file, " %4s", "Nr");
  1077.       else
  1078.         fprintf_unfiltered (file, " %4d", regnum);

  1079.       /* Relative number.  */
  1080.       if (regnum < 0)
  1081.         fprintf_unfiltered (file, " %4s", "Rel");
  1082.       else if (regnum < gdbarch_num_regs (gdbarch))
  1083.         fprintf_unfiltered (file, " %4d", regnum);
  1084.       else
  1085.         fprintf_unfiltered (file, " %4d",
  1086.                             (regnum - gdbarch_num_regs (gdbarch)));

  1087.       /* Offset.  */
  1088.       if (regnum < 0)
  1089.         fprintf_unfiltered (file, " %6s  ", "Offset");
  1090.       else
  1091.         {
  1092.           fprintf_unfiltered (file, " %6ld",
  1093.                               regcache->descr->register_offset[regnum]);
  1094.           if (register_offset != regcache->descr->register_offset[regnum]
  1095.               || (regnum > 0
  1096.                   && (regcache->descr->register_offset[regnum]
  1097.                       != (regcache->descr->register_offset[regnum - 1]
  1098.                           + regcache->descr->sizeof_register[regnum - 1])))
  1099.               )
  1100.             {
  1101.               if (!footnote_register_offset)
  1102.                 footnote_register_offset = ++footnote_nr;
  1103.               fprintf_unfiltered (file, "*%d", footnote_register_offset);
  1104.             }
  1105.           else
  1106.             fprintf_unfiltered (file, "  ");
  1107.           register_offset = (regcache->descr->register_offset[regnum]
  1108.                              + regcache->descr->sizeof_register[regnum]);
  1109.         }

  1110.       /* Size.  */
  1111.       if (regnum < 0)
  1112.         fprintf_unfiltered (file, " %5s ", "Size");
  1113.       else
  1114.         fprintf_unfiltered (file, " %5ld",
  1115.                             regcache->descr->sizeof_register[regnum]);

  1116.       /* Type.  */
  1117.       {
  1118.         const char *t;

  1119.         if (regnum < 0)
  1120.           t = "Type";
  1121.         else
  1122.           {
  1123.             static const char blt[] = "builtin_type";

  1124.             t = TYPE_NAME (register_type (regcache->descr->gdbarch, regnum));
  1125.             if (t == NULL)
  1126.               {
  1127.                 char *n;

  1128.                 if (!footnote_register_type_name_null)
  1129.                   footnote_register_type_name_null = ++footnote_nr;
  1130.                 n = xstrprintf ("*%d", footnote_register_type_name_null);
  1131.                 make_cleanup (xfree, n);
  1132.                 t = n;
  1133.               }
  1134.             /* Chop a leading builtin_type.  */
  1135.             if (strncmp (t, blt, strlen (blt)) == 0)
  1136.               t += strlen (blt);
  1137.           }
  1138.         fprintf_unfiltered (file, " %-15s", t);
  1139.       }

  1140.       /* Leading space always present.  */
  1141.       fprintf_unfiltered (file, " ");

  1142.       /* Value, raw.  */
  1143.       if (what_to_dump == regcache_dump_raw)
  1144.         {
  1145.           if (regnum < 0)
  1146.             fprintf_unfiltered (file, "Raw value");
  1147.           else if (regnum >= regcache->descr->nr_raw_registers)
  1148.             fprintf_unfiltered (file, "<cooked>");
  1149.           else if (regcache_register_status (regcache, regnum) == REG_UNKNOWN)
  1150.             fprintf_unfiltered (file, "<invalid>");
  1151.           else if (regcache_register_status (regcache, regnum) == REG_UNAVAILABLE)
  1152.             fprintf_unfiltered (file, "<unavailable>");
  1153.           else
  1154.             {
  1155.               regcache_raw_read (regcache, regnum, buf);
  1156.               print_hex_chars (file, buf,
  1157.                                regcache->descr->sizeof_register[regnum],
  1158.                                gdbarch_byte_order (gdbarch));
  1159.             }
  1160.         }

  1161.       /* Value, cooked.  */
  1162.       if (what_to_dump == regcache_dump_cooked)
  1163.         {
  1164.           if (regnum < 0)
  1165.             fprintf_unfiltered (file, "Cooked value");
  1166.           else
  1167.             {
  1168.               enum register_status status;

  1169.               status = regcache_cooked_read (regcache, regnum, buf);
  1170.               if (status == REG_UNKNOWN)
  1171.                 fprintf_unfiltered (file, "<invalid>");
  1172.               else if (status == REG_UNAVAILABLE)
  1173.                 fprintf_unfiltered (file, "<unavailable>");
  1174.               else
  1175.                 print_hex_chars (file, buf,
  1176.                                  regcache->descr->sizeof_register[regnum],
  1177.                                  gdbarch_byte_order (gdbarch));
  1178.             }
  1179.         }

  1180.       /* Group members.  */
  1181.       if (what_to_dump == regcache_dump_groups)
  1182.         {
  1183.           if (regnum < 0)
  1184.             fprintf_unfiltered (file, "Groups");
  1185.           else
  1186.             {
  1187.               const char *sep = "";
  1188.               struct reggroup *group;

  1189.               for (group = reggroup_next (gdbarch, NULL);
  1190.                    group != NULL;
  1191.                    group = reggroup_next (gdbarch, group))
  1192.                 {
  1193.                   if (gdbarch_register_reggroup_p (gdbarch, regnum, group))
  1194.                     {
  1195.                       fprintf_unfiltered (file,
  1196.                                           "%s%s", sep, reggroup_name (group));
  1197.                       sep = ",";
  1198.                     }
  1199.                 }
  1200.             }
  1201.         }

  1202.       /* Remote packet configuration.  */
  1203.       if (what_to_dump == regcache_dump_remote)
  1204.         {
  1205.           if (regnum < 0)
  1206.             {
  1207.               fprintf_unfiltered (file, "Rmt Nr  g/G Offset");
  1208.             }
  1209.           else if (regnum < regcache->descr->nr_raw_registers)
  1210.             {
  1211.               int pnum, poffset;

  1212.               if (remote_register_number_and_offset (get_regcache_arch (regcache), regnum,
  1213.                                                      &pnum, &poffset))
  1214.                 fprintf_unfiltered (file, "%7d %11d", pnum, poffset);
  1215.             }
  1216.         }

  1217.       fprintf_unfiltered (file, "\n");
  1218.     }

  1219.   if (footnote_register_size)
  1220.     fprintf_unfiltered (file, "*%d: Inconsistent register sizes.\n",
  1221.                         footnote_register_size);
  1222.   if (footnote_register_offset)
  1223.     fprintf_unfiltered (file, "*%d: Inconsistent register offsets.\n",
  1224.                         footnote_register_offset);
  1225.   if (footnote_register_type_name_null)
  1226.     fprintf_unfiltered (file,
  1227.                         "*%d: Register type's name NULL.\n",
  1228.                         footnote_register_type_name_null);
  1229.   do_cleanups (cleanups);
  1230. }

  1231. static void
  1232. regcache_print (char *args, enum regcache_dump_what what_to_dump)
  1233. {
  1234.   if (args == NULL)
  1235.     regcache_dump (get_current_regcache (), gdb_stdout, what_to_dump);
  1236.   else
  1237.     {
  1238.       struct cleanup *cleanups;
  1239.       struct ui_file *file = gdb_fopen (args, "w");

  1240.       if (file == NULL)
  1241.         perror_with_name (_("maintenance print architecture"));
  1242.       cleanups = make_cleanup_ui_file_delete (file);
  1243.       regcache_dump (get_current_regcache (), file, what_to_dump);
  1244.       do_cleanups (cleanups);
  1245.     }
  1246. }

  1247. static void
  1248. maintenance_print_registers (char *args, int from_tty)
  1249. {
  1250.   regcache_print (args, regcache_dump_none);
  1251. }

  1252. static void
  1253. maintenance_print_raw_registers (char *args, int from_tty)
  1254. {
  1255.   regcache_print (args, regcache_dump_raw);
  1256. }

  1257. static void
  1258. maintenance_print_cooked_registers (char *args, int from_tty)
  1259. {
  1260.   regcache_print (args, regcache_dump_cooked);
  1261. }

  1262. static void
  1263. maintenance_print_register_groups (char *args, int from_tty)
  1264. {
  1265.   regcache_print (args, regcache_dump_groups);
  1266. }

  1267. static void
  1268. maintenance_print_remote_registers (char *args, int from_tty)
  1269. {
  1270.   regcache_print (args, regcache_dump_remote);
  1271. }

  1272. extern initialize_file_ftype _initialize_regcache; /* -Wmissing-prototype */

  1273. void
  1274. _initialize_regcache (void)
  1275. {
  1276.   regcache_descr_handle
  1277.     = gdbarch_data_register_post_init (init_regcache_descr);

  1278.   observer_attach_target_changed (regcache_observer_target_changed);
  1279.   observer_attach_thread_ptid_changed (regcache_thread_ptid_changed);

  1280.   add_com ("flushregs", class_maintenance, reg_flush_command,
  1281.            _("Force gdb to flush its register cache (maintainer command)"));

  1282.   add_cmd ("registers", class_maintenance, maintenance_print_registers,
  1283.            _("Print the internal register configuration.\n"
  1284.              "Takes an optional file parameter."), &maintenanceprintlist);
  1285.   add_cmd ("raw-registers", class_maintenance,
  1286.            maintenance_print_raw_registers,
  1287.            _("Print the internal register configuration "
  1288.              "including raw values.\n"
  1289.              "Takes an optional file parameter."), &maintenanceprintlist);
  1290.   add_cmd ("cooked-registers", class_maintenance,
  1291.            maintenance_print_cooked_registers,
  1292.            _("Print the internal register configuration "
  1293.              "including cooked values.\n"
  1294.              "Takes an optional file parameter."), &maintenanceprintlist);
  1295.   add_cmd ("register-groups", class_maintenance,
  1296.            maintenance_print_register_groups,
  1297.            _("Print the internal register configuration "
  1298.              "including each register's group.\n"
  1299.              "Takes an optional file parameter."),
  1300.            &maintenanceprintlist);
  1301.   add_cmd ("remote-registers", class_maintenance,
  1302.            maintenance_print_remote_registers, _("\
  1303. Print the internal register configuration including each register's\n\
  1304. remote register number and buffer offset in the g/G packets.\n\
  1305. Takes an optional file parameter."),
  1306.            &maintenanceprintlist);

  1307. }