gdb/findvar.c - gdb

Data types defined

Functions defined

Source code

  1. /* Find a variable's value in memory, 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 "symtab.h"
  16. #include "gdbtypes.h"
  17. #include "frame.h"
  18. #include "value.h"
  19. #include "gdbcore.h"
  20. #include "inferior.h"
  21. #include "target.h"
  22. #include "floatformat.h"
  23. #include "symfile.h"                /* for overlay functions */
  24. #include "regcache.h"
  25. #include "user-regs.h"
  26. #include "block.h"
  27. #include "objfiles.h"
  28. #include "language.h"

  29. /* Basic byte-swapping routines.  All 'extract' functions return a
  30.    host-format integer from a target-format integer at ADDR which is
  31.    LEN bytes long.  */

  32. #if TARGET_CHAR_BIT != 8 || HOST_CHAR_BIT != 8
  33.   /* 8 bit characters are a pretty safe assumption these days, so we
  34.      assume it throughout all these swapping routines.  If we had to deal with
  35.      9 bit characters, we would need to make len be in bits and would have
  36.      to re-write these routines...  */
  37. you lose
  38. #endif

  39. LONGEST
  40. extract_signed_integer (const gdb_byte *addr, int len,
  41.                         enum bfd_endian byte_order)
  42. {
  43.   LONGEST retval;
  44.   const unsigned char *p;
  45.   const unsigned char *startaddr = addr;
  46.   const unsigned char *endaddr = startaddr + len;

  47.   if (len > (int) sizeof (LONGEST))
  48.     error (_("\
  49. That operation is not available on integers of more than %d bytes."),
  50.            (int) sizeof (LONGEST));

  51.   /* Start at the most significant end of the integer, and work towards
  52.      the least significant.  */
  53.   if (byte_order == BFD_ENDIAN_BIG)
  54.     {
  55.       p = startaddr;
  56.       /* Do the sign extension once at the start.  */
  57.       retval = ((LONGEST) * p ^ 0x80) - 0x80;
  58.       for (++p; p < endaddr; ++p)
  59.         retval = (retval << 8) | *p;
  60.     }
  61.   else
  62.     {
  63.       p = endaddr - 1;
  64.       /* Do the sign extension once at the start.  */
  65.       retval = ((LONGEST) * p ^ 0x80) - 0x80;
  66.       for (--p; p >= startaddr; --p)
  67.         retval = (retval << 8) | *p;
  68.     }
  69.   return retval;
  70. }

  71. ULONGEST
  72. extract_unsigned_integer (const gdb_byte *addr, int len,
  73.                           enum bfd_endian byte_order)
  74. {
  75.   ULONGEST retval;
  76.   const unsigned char *p;
  77.   const unsigned char *startaddr = addr;
  78.   const unsigned char *endaddr = startaddr + len;

  79.   if (len > (int) sizeof (ULONGEST))
  80.     error (_("\
  81. That operation is not available on integers of more than %d bytes."),
  82.            (int) sizeof (ULONGEST));

  83.   /* Start at the most significant end of the integer, and work towards
  84.      the least significant.  */
  85.   retval = 0;
  86.   if (byte_order == BFD_ENDIAN_BIG)
  87.     {
  88.       for (p = startaddr; p < endaddr; ++p)
  89.         retval = (retval << 8) | *p;
  90.     }
  91.   else
  92.     {
  93.       for (p = endaddr - 1; p >= startaddr; --p)
  94.         retval = (retval << 8) | *p;
  95.     }
  96.   return retval;
  97. }

  98. /* Sometimes a long long unsigned integer can be extracted as a
  99.    LONGEST value.  This is done so that we can print these values
  100.    better.  If this integer can be converted to a LONGEST, this
  101.    function returns 1 and sets *PVAL.  Otherwise it returns 0.  */

  102. int
  103. extract_long_unsigned_integer (const gdb_byte *addr, int orig_len,
  104.                                enum bfd_endian byte_order, LONGEST *pval)
  105. {
  106.   const gdb_byte *p;
  107.   const gdb_byte *first_addr;
  108.   int len;

  109.   len = orig_len;
  110.   if (byte_order == BFD_ENDIAN_BIG)
  111.     {
  112.       for (p = addr;
  113.            len > (int) sizeof (LONGEST) && p < addr + orig_len;
  114.            p++)
  115.         {
  116.           if (*p == 0)
  117.             len--;
  118.           else
  119.             break;
  120.         }
  121.       first_addr = p;
  122.     }
  123.   else
  124.     {
  125.       first_addr = addr;
  126.       for (p = addr + orig_len - 1;
  127.            len > (int) sizeof (LONGEST) && p >= addr;
  128.            p--)
  129.         {
  130.           if (*p == 0)
  131.             len--;
  132.           else
  133.             break;
  134.         }
  135.     }

  136.   if (len <= (int) sizeof (LONGEST))
  137.     {
  138.       *pval = (LONGEST) extract_unsigned_integer (first_addr,
  139.                                                   sizeof (LONGEST),
  140.                                                   byte_order);
  141.       return 1;
  142.     }

  143.   return 0;
  144. }


  145. /* Treat the bytes at BUF as a pointer of type TYPE, and return the
  146.    address it represents.  */
  147. CORE_ADDR
  148. extract_typed_address (const gdb_byte *buf, struct type *type)
  149. {
  150.   if (TYPE_CODE (type) != TYPE_CODE_PTR
  151.       && TYPE_CODE (type) != TYPE_CODE_REF)
  152.     internal_error (__FILE__, __LINE__,
  153.                     _("extract_typed_address: "
  154.                     "type is not a pointer or reference"));

  155.   return gdbarch_pointer_to_address (get_type_arch (type), type, buf);
  156. }

  157. /* All 'store' functions accept a host-format integer and store a
  158.    target-format integer at ADDR which is LEN bytes long.  */

  159. void
  160. store_signed_integer (gdb_byte *addr, int len,
  161.                       enum bfd_endian byte_order, LONGEST val)
  162. {
  163.   gdb_byte *p;
  164.   gdb_byte *startaddr = addr;
  165.   gdb_byte *endaddr = startaddr + len;

  166.   /* Start at the least significant end of the integer, and work towards
  167.      the most significant.  */
  168.   if (byte_order == BFD_ENDIAN_BIG)
  169.     {
  170.       for (p = endaddr - 1; p >= startaddr; --p)
  171.         {
  172.           *p = val & 0xff;
  173.           val >>= 8;
  174.         }
  175.     }
  176.   else
  177.     {
  178.       for (p = startaddr; p < endaddr; ++p)
  179.         {
  180.           *p = val & 0xff;
  181.           val >>= 8;
  182.         }
  183.     }
  184. }

  185. void
  186. store_unsigned_integer (gdb_byte *addr, int len,
  187.                         enum bfd_endian byte_order, ULONGEST val)
  188. {
  189.   unsigned char *p;
  190.   unsigned char *startaddr = (unsigned char *) addr;
  191.   unsigned char *endaddr = startaddr + len;

  192.   /* Start at the least significant end of the integer, and work towards
  193.      the most significant.  */
  194.   if (byte_order == BFD_ENDIAN_BIG)
  195.     {
  196.       for (p = endaddr - 1; p >= startaddr; --p)
  197.         {
  198.           *p = val & 0xff;
  199.           val >>= 8;
  200.         }
  201.     }
  202.   else
  203.     {
  204.       for (p = startaddr; p < endaddr; ++p)
  205.         {
  206.           *p = val & 0xff;
  207.           val >>= 8;
  208.         }
  209.     }
  210. }

  211. /* Store the address ADDR as a pointer of type TYPE at BUF, in target
  212.    form.  */
  213. void
  214. store_typed_address (gdb_byte *buf, struct type *type, CORE_ADDR addr)
  215. {
  216.   if (TYPE_CODE (type) != TYPE_CODE_PTR
  217.       && TYPE_CODE (type) != TYPE_CODE_REF)
  218.     internal_error (__FILE__, __LINE__,
  219.                     _("store_typed_address: "
  220.                     "type is not a pointer or reference"));

  221.   gdbarch_address_to_pointer (get_type_arch (type), type, buf, addr);
  222. }



  223. /* Return a `value' with the contents of (virtual or cooked) register
  224.    REGNUM as found in the specified FRAME.  The register's type is
  225.    determined by register_type().  */

  226. struct value *
  227. value_of_register (int regnum, struct frame_info *frame)
  228. {
  229.   struct gdbarch *gdbarch = get_frame_arch (frame);
  230.   struct value *reg_val;

  231.   /* User registers lie completely outside of the range of normal
  232.      registers.  Catch them early so that the target never sees them.  */
  233.   if (regnum >= gdbarch_num_regs (gdbarch)
  234.                 + gdbarch_num_pseudo_regs (gdbarch))
  235.     return value_of_user_reg (regnum, frame);

  236.   reg_val = value_of_register_lazy (frame, regnum);
  237.   value_fetch_lazy (reg_val);
  238.   return reg_val;
  239. }

  240. /* Return a `value' with the contents of (virtual or cooked) register
  241.    REGNUM as found in the specified FRAME.  The register's type is
  242.    determined by register_type().  The value is not fetched.  */

  243. struct value *
  244. value_of_register_lazy (struct frame_info *frame, int regnum)
  245. {
  246.   struct gdbarch *gdbarch = get_frame_arch (frame);
  247.   struct value *reg_val;

  248.   gdb_assert (regnum < (gdbarch_num_regs (gdbarch)
  249.                         + gdbarch_num_pseudo_regs (gdbarch)));

  250.   /* We should have a valid (i.e. non-sentinel) frame.  */
  251.   gdb_assert (frame_id_p (get_frame_id (frame)));

  252.   reg_val = allocate_value_lazy (register_type (gdbarch, regnum));
  253.   VALUE_LVAL (reg_val) = lval_register;
  254.   VALUE_REGNUM (reg_val) = regnum;
  255.   VALUE_FRAME_ID (reg_val) = get_frame_id (frame);
  256.   return reg_val;
  257. }

  258. /* Given a pointer of type TYPE in target form in BUF, return the
  259.    address it represents.  */
  260. CORE_ADDR
  261. unsigned_pointer_to_address (struct gdbarch *gdbarch,
  262.                              struct type *type, const gdb_byte *buf)
  263. {
  264.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  265.   return extract_unsigned_integer (buf, TYPE_LENGTH (type), byte_order);
  266. }

  267. CORE_ADDR
  268. signed_pointer_to_address (struct gdbarch *gdbarch,
  269.                            struct type *type, const gdb_byte *buf)
  270. {
  271.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  272.   return extract_signed_integer (buf, TYPE_LENGTH (type), byte_order);
  273. }

  274. /* Given an address, store it as a pointer of type TYPE in target
  275.    format in BUF.  */
  276. void
  277. unsigned_address_to_pointer (struct gdbarch *gdbarch, struct type *type,
  278.                              gdb_byte *buf, CORE_ADDR addr)
  279. {
  280.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  281.   store_unsigned_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  282. }

  283. void
  284. address_to_signed_pointer (struct gdbarch *gdbarch, struct type *type,
  285.                            gdb_byte *buf, CORE_ADDR addr)
  286. {
  287.   enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);

  288.   store_signed_integer (buf, TYPE_LENGTH (type), byte_order, addr);
  289. }

  290. /* Will calling read_var_value or locate_var_value on SYM end
  291.    up caring what frame it is being evaluated relative to?  SYM must
  292.    be non-NULL.  */
  293. int
  294. symbol_read_needs_frame (struct symbol *sym)
  295. {
  296.   if (SYMBOL_COMPUTED_OPS (sym) != NULL)
  297.     return SYMBOL_COMPUTED_OPS (sym)->read_needs_frame (sym);

  298.   switch (SYMBOL_CLASS (sym))
  299.     {
  300.       /* All cases listed explicitly so that gcc -Wall will detect it if
  301.          we failed to consider one.  */
  302.     case LOC_COMPUTED:
  303.       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));

  304.     case LOC_REGISTER:
  305.     case LOC_ARG:
  306.     case LOC_REF_ARG:
  307.     case LOC_REGPARM_ADDR:
  308.     case LOC_LOCAL:
  309.       return 1;

  310.     case LOC_UNDEF:
  311.     case LOC_CONST:
  312.     case LOC_STATIC:
  313.     case LOC_TYPEDEF:

  314.     case LOC_LABEL:
  315.       /* Getting the address of a label can be done independently of the block,
  316.          even if some *uses* of that address wouldn't work so well without
  317.          the right frame.  */

  318.     case LOC_BLOCK:
  319.     case LOC_CONST_BYTES:
  320.     case LOC_UNRESOLVED:
  321.     case LOC_OPTIMIZED_OUT:
  322.       return 0;
  323.     }
  324.   return 1;
  325. }

  326. /* Private data to be used with minsym_lookup_iterator_cb.  */

  327. struct minsym_lookup_data
  328. {
  329.   /* The name of the minimal symbol we are searching for.  */
  330.   const char *name;

  331.   /* The field where the callback should store the minimal symbol
  332.      if found.  It should be initialized to NULL before the search
  333.      is started.  */
  334.   struct bound_minimal_symbol result;
  335. };

  336. /* A callback function for gdbarch_iterate_over_objfiles_in_search_order.
  337.    It searches by name for a minimal symbol within the given OBJFILE.
  338.    The arguments are passed via CB_DATA, which in reality is a pointer
  339.    to struct minsym_lookup_data.  */

  340. static int
  341. minsym_lookup_iterator_cb (struct objfile *objfile, void *cb_data)
  342. {
  343.   struct minsym_lookup_data *data = (struct minsym_lookup_data *) cb_data;

  344.   gdb_assert (data->result.minsym == NULL);

  345.   data->result = lookup_minimal_symbol (data->name, NULL, objfile);

  346.   /* The iterator should stop iff a match was found.  */
  347.   return (data->result.minsym != NULL);
  348. }

  349. /* A default implementation for the "la_read_var_value" hook in
  350.    the language vector which should work in most situations.  */

  351. struct value *
  352. default_read_var_value (struct symbol *var, struct frame_info *frame)
  353. {
  354.   struct value *v;
  355.   struct type *type = SYMBOL_TYPE (var);
  356.   CORE_ADDR addr;

  357.   /* Call check_typedef on our type to make sure that, if TYPE is
  358.      a TYPE_CODE_TYPEDEF, its length is set to the length of the target type
  359.      instead of zero.  However, we do not replace the typedef type by the
  360.      target type, because we want to keep the typedef in order to be able to
  361.      set the returned value type description correctly.  */
  362.   check_typedef (type);

  363.   if (symbol_read_needs_frame (var))
  364.     gdb_assert (frame);

  365.   if (SYMBOL_COMPUTED_OPS (var) != NULL)
  366.     return SYMBOL_COMPUTED_OPS (var)->read_variable (var, frame);

  367.   switch (SYMBOL_CLASS (var))
  368.     {
  369.     case LOC_CONST:
  370.       if (is_dynamic_type (type))
  371.         {
  372.           /* Value is a constant byte-sequence and needs no memory access.  */
  373.           type = resolve_dynamic_type (type, /* Unused address.  */ 0);
  374.         }
  375.       /* Put the constant back in target format. */
  376.       v = allocate_value (type);
  377.       store_signed_integer (value_contents_raw (v), TYPE_LENGTH (type),
  378.                             gdbarch_byte_order (get_type_arch (type)),
  379.                             (LONGEST) SYMBOL_VALUE (var));
  380.       VALUE_LVAL (v) = not_lval;
  381.       return v;

  382.     case LOC_LABEL:
  383.       /* Put the constant back in target format.  */
  384.       v = allocate_value (type);
  385.       if (overlay_debugging)
  386.         {
  387.           CORE_ADDR addr
  388.             = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
  389.                                         SYMBOL_OBJ_SECTION (symbol_objfile (var),
  390.                                                             var));

  391.           store_typed_address (value_contents_raw (v), type, addr);
  392.         }
  393.       else
  394.         store_typed_address (value_contents_raw (v), type,
  395.                               SYMBOL_VALUE_ADDRESS (var));
  396.       VALUE_LVAL (v) = not_lval;
  397.       return v;

  398.     case LOC_CONST_BYTES:
  399.       if (is_dynamic_type (type))
  400.         {
  401.           /* Value is a constant byte-sequence and needs no memory access.  */
  402.           type = resolve_dynamic_type (type, /* Unused address.  */ 0);
  403.         }
  404.       v = allocate_value (type);
  405.       memcpy (value_contents_raw (v), SYMBOL_VALUE_BYTES (var),
  406.               TYPE_LENGTH (type));
  407.       VALUE_LVAL (v) = not_lval;
  408.       return v;

  409.     case LOC_STATIC:
  410.       if (overlay_debugging)
  411.         addr = symbol_overlayed_address (SYMBOL_VALUE_ADDRESS (var),
  412.                                          SYMBOL_OBJ_SECTION (symbol_objfile (var),
  413.                                                              var));
  414.       else
  415.         addr = SYMBOL_VALUE_ADDRESS (var);
  416.       break;

  417.     case LOC_ARG:
  418.       addr = get_frame_args_address (frame);
  419.       if (!addr)
  420.         error (_("Unknown argument list address for `%s'."),
  421.                SYMBOL_PRINT_NAME (var));
  422.       addr += SYMBOL_VALUE (var);
  423.       break;

  424.     case LOC_REF_ARG:
  425.       {
  426.         struct value *ref;
  427.         CORE_ADDR argref;

  428.         argref = get_frame_args_address (frame);
  429.         if (!argref)
  430.           error (_("Unknown argument list address for `%s'."),
  431.                  SYMBOL_PRINT_NAME (var));
  432.         argref += SYMBOL_VALUE (var);
  433.         ref = value_at (lookup_pointer_type (type), argref);
  434.         addr = value_as_address (ref);
  435.         break;
  436.       }

  437.     case LOC_LOCAL:
  438.       addr = get_frame_locals_address (frame);
  439.       addr += SYMBOL_VALUE (var);
  440.       break;

  441.     case LOC_TYPEDEF:
  442.       error (_("Cannot look up value of a typedef `%s'."),
  443.              SYMBOL_PRINT_NAME (var));
  444.       break;

  445.     case LOC_BLOCK:
  446.       if (overlay_debugging)
  447.         addr = symbol_overlayed_address
  448.           (BLOCK_START (SYMBOL_BLOCK_VALUE (var)),
  449.            SYMBOL_OBJ_SECTION (symbol_objfile (var), var));
  450.       else
  451.         addr = BLOCK_START (SYMBOL_BLOCK_VALUE (var));
  452.       break;

  453.     case LOC_REGISTER:
  454.     case LOC_REGPARM_ADDR:
  455.       {
  456.         int regno = SYMBOL_REGISTER_OPS (var)
  457.                       ->register_number (var, get_frame_arch (frame));
  458.         struct value *regval;

  459.         if (SYMBOL_CLASS (var) == LOC_REGPARM_ADDR)
  460.           {
  461.             regval = value_from_register (lookup_pointer_type (type),
  462.                                           regno,
  463.                                           frame);

  464.             if (regval == NULL)
  465.               error (_("Value of register variable not available for `%s'."),
  466.                      SYMBOL_PRINT_NAME (var));

  467.             addr = value_as_address (regval);
  468.           }
  469.         else
  470.           {
  471.             regval = value_from_register (type, regno, frame);

  472.             if (regval == NULL)
  473.               error (_("Value of register variable not available for `%s'."),
  474.                      SYMBOL_PRINT_NAME (var));
  475.             return regval;
  476.           }
  477.       }
  478.       break;

  479.     case LOC_COMPUTED:
  480.       gdb_assert_not_reached (_("LOC_COMPUTED variable missing a method"));

  481.     case LOC_UNRESOLVED:
  482.       {
  483.         struct minsym_lookup_data lookup_data;
  484.         struct minimal_symbol *msym;
  485.         struct obj_section *obj_section;

  486.         memset (&lookup_data, 0, sizeof (lookup_data));
  487.         lookup_data.name = SYMBOL_LINKAGE_NAME (var);

  488.         gdbarch_iterate_over_objfiles_in_search_order
  489.           (symbol_arch (var),
  490.            minsym_lookup_iterator_cb, &lookup_data,
  491.            symbol_objfile (var));
  492.         msym = lookup_data.result.minsym;

  493.         if (msym == NULL)
  494.           error (_("No global symbol \"%s\"."), SYMBOL_LINKAGE_NAME (var));
  495.         if (overlay_debugging)
  496.           addr = symbol_overlayed_address (BMSYMBOL_VALUE_ADDRESS (lookup_data.result),
  497.                                            MSYMBOL_OBJ_SECTION (lookup_data.result.objfile,
  498.                                                                 msym));
  499.         else
  500.           addr = BMSYMBOL_VALUE_ADDRESS (lookup_data.result);

  501.         obj_section = MSYMBOL_OBJ_SECTION (lookup_data.result.objfile, msym);
  502.         if (obj_section
  503.             && (obj_section->the_bfd_section->flags & SEC_THREAD_LOCAL) != 0)
  504.           addr = target_translate_tls_address (obj_section->objfile, addr);
  505.       }
  506.       break;

  507.     case LOC_OPTIMIZED_OUT:
  508.       return allocate_optimized_out_value (type);

  509.     default:
  510.       error (_("Cannot look up value of a botched symbol `%s'."),
  511.              SYMBOL_PRINT_NAME (var));
  512.       break;
  513.     }

  514.   v = value_at_lazy (type, addr);
  515.   return v;
  516. }

  517. /* Calls VAR's language la_read_var_value hook with the given arguments.  */

  518. struct value *
  519. read_var_value (struct symbol *var, struct frame_info *frame)
  520. {
  521.   const struct language_defn *lang = language_def (SYMBOL_LANGUAGE (var));

  522.   gdb_assert (lang != NULL);
  523.   gdb_assert (lang->la_read_var_value != NULL);

  524.   return lang->la_read_var_value (var, frame);
  525. }

  526. /* Install default attributes for register values.  */

  527. struct value *
  528. default_value_from_register (struct gdbarch *gdbarch, struct type *type,
  529.                              int regnum, struct frame_id frame_id)
  530. {
  531.   int len = TYPE_LENGTH (type);
  532.   struct value *value = allocate_value (type);

  533.   VALUE_LVAL (value) = lval_register;
  534.   VALUE_FRAME_ID (value) = frame_id;
  535.   VALUE_REGNUM (value) = regnum;

  536.   /* Any structure stored in more than one register will always be
  537.      an integral number of registers.  Otherwise, you need to do
  538.      some fiddling with the last register copied here for little
  539.      endian machines.  */
  540.   if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG
  541.       && len < register_size (gdbarch, regnum))
  542.     /* Big-endian, and we want less than full size.  */
  543.     set_value_offset (value, register_size (gdbarch, regnum) - len);
  544.   else
  545.     set_value_offset (value, 0);

  546.   return value;
  547. }

  548. /* VALUE must be an lval_register value.  If regnum is the value's
  549.    associated register number, and len the length of the values type,
  550.    read one or more registers in FRAME, starting with register REGNUM,
  551.    until we've read LEN bytes.

  552.    If any of the registers we try to read are optimized out, then mark the
  553.    complete resulting value as optimized out.  */

  554. void
  555. read_frame_register_value (struct value *value, struct frame_info *frame)
  556. {
  557.   struct gdbarch *gdbarch = get_frame_arch (frame);
  558.   int offset = 0;
  559.   int reg_offset = value_offset (value);
  560.   int regnum = VALUE_REGNUM (value);
  561.   int len = TYPE_LENGTH (check_typedef (value_type (value)));

  562.   gdb_assert (VALUE_LVAL (value) == lval_register);

  563.   /* Skip registers wholly inside of REG_OFFSET.  */
  564.   while (reg_offset >= register_size (gdbarch, regnum))
  565.     {
  566.       reg_offset -= register_size (gdbarch, regnum);
  567.       regnum++;
  568.     }

  569.   /* Copy the data.  */
  570.   while (len > 0)
  571.     {
  572.       struct value *regval = get_frame_register_value (frame, regnum);
  573.       int reg_len = TYPE_LENGTH (value_type (regval)) - reg_offset;

  574.       /* If the register length is larger than the number of bytes
  575.          remaining to copy, then only copy the appropriate bytes.  */
  576.       if (reg_len > len)
  577.         reg_len = len;

  578.       value_contents_copy (value, offset, regval, reg_offset, reg_len);

  579.       offset += reg_len;
  580.       len -= reg_len;
  581.       reg_offset = 0;
  582.       regnum++;
  583.     }
  584. }

  585. /* Return a value of type TYPE, stored in register REGNUM, in frame FRAME.  */

  586. struct value *
  587. value_from_register (struct type *type, int regnum, struct frame_info *frame)
  588. {
  589.   struct gdbarch *gdbarch = get_frame_arch (frame);
  590.   struct type *type1 = check_typedef (type);
  591.   struct value *v;

  592.   if (gdbarch_convert_register_p (gdbarch, regnum, type1))
  593.     {
  594.       int optim, unavail, ok;

  595.       /* The ISA/ABI need to something weird when obtaining the
  596.          specified value from this register.  It might need to
  597.          re-order non-adjacent, starting with REGNUM (see MIPS and
  598.          i386).  It might need to convert the [float] register into
  599.          the corresponding [integer] type (see Alpha).  The assumption
  600.          is that gdbarch_register_to_value populates the entire value
  601.          including the location.  */
  602.       v = allocate_value (type);
  603.       VALUE_LVAL (v) = lval_register;
  604.       VALUE_FRAME_ID (v) = get_frame_id (frame);
  605.       VALUE_REGNUM (v) = regnum;
  606.       ok = gdbarch_register_to_value (gdbarch, frame, regnum, type1,
  607.                                       value_contents_raw (v), &optim,
  608.                                       &unavail);

  609.       if (!ok)
  610.         {
  611.           if (optim)
  612.             mark_value_bytes_optimized_out (v, 0, TYPE_LENGTH (type));
  613.           if (unavail)
  614.             mark_value_bytes_unavailable (v, 0, TYPE_LENGTH (type));
  615.         }
  616.     }
  617.   else
  618.     {
  619.       /* Construct the value.  */
  620.       v = gdbarch_value_from_register (gdbarch, type,
  621.                                        regnum, get_frame_id (frame));

  622.       /* Get the data.  */
  623.       read_frame_register_value (v, frame);
  624.     }

  625.   return v;
  626. }

  627. /* Return contents of register REGNUM in frame FRAME as address.
  628.    Will abort if register value is not available.  */

  629. CORE_ADDR
  630. address_from_register (int regnum, struct frame_info *frame)
  631. {
  632.   struct gdbarch *gdbarch = get_frame_arch (frame);
  633.   struct type *type = builtin_type (gdbarch)->builtin_data_ptr;
  634.   struct value *value;
  635.   CORE_ADDR result;

  636.   /* This routine may be called during early unwinding, at a time
  637.      where the ID of FRAME is not yet known.  Calling value_from_register
  638.      would therefore abort in get_frame_id.  However, since we only need
  639.      a temporary value that is never used as lvalue, we actually do not
  640.      really need to set its VALUE_FRAME_ID.  Therefore, we re-implement
  641.      the core of value_from_register, but use the null_frame_id.  */

  642.   /* Some targets require a special conversion routine even for plain
  643.      pointer types.  Avoid constructing a value object in those cases.  */
  644.   if (gdbarch_convert_register_p (gdbarch, regnum, type))
  645.     {
  646.       gdb_byte *buf = alloca (TYPE_LENGTH (type));
  647.       int optim, unavail, ok;

  648.       ok = gdbarch_register_to_value (gdbarch, frame, regnum, type,
  649.                                       buf, &optim, &unavail);
  650.       if (!ok)
  651.         {
  652.           /* This function is used while computing a location expression.
  653.              Complain about the value being optimized out, rather than
  654.              letting value_as_address complain about some random register
  655.              the expression depends on not being saved.  */
  656.           error_value_optimized_out ();
  657.         }

  658.       return unpack_long (type, buf);
  659.     }

  660.   value = gdbarch_value_from_register (gdbarch, type, regnum, null_frame_id);
  661.   read_frame_register_value (value, frame);

  662.   if (value_optimized_out (value))
  663.     {
  664.       /* This function is used while computing a location expression.
  665.          Complain about the value being optimized out, rather than
  666.          letting value_as_address complain about some random register
  667.          the expression depends on not being saved.  */
  668.       error_value_optimized_out ();
  669.     }

  670.   result = value_as_address (value);
  671.   release_value (value);
  672.   value_free (value);

  673.   return result;
  674. }