Destroy allocated values when exiting GDB
[deliverable/binutils-gdb.git] / gdb / value.c
index 6bb6b8eb61ebfe5509d9a423643d124772a1fb14..bcfc084e09092a187baa691856f7b3f940573073 100644 (file)
@@ -1,6 +1,6 @@
 /* Low level packing and unpacking of values for GDB, the GNU Debugger.
 
-   Copyright (C) 1986-2018 Free Software Foundation, Inc.
+   Copyright (C) 1986-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -41,7 +41,7 @@
 #include "user-regs.h"
 #include <algorithm>
 #include "completer.h"
-#include "selftest.h"
+#include "common/selftest.h"
 #include "common/array-view.h"
 
 /* Definition of a user function.  */
@@ -2274,20 +2274,20 @@ set_internalvar (struct internalvar *var, struct value *val)
 
     default:
       new_kind = INTERNALVAR_VALUE;
-      new_data.value = value_copy (val);
-      new_data.value->modifiable = 1;
+      struct value *copy = value_copy (val);
+      copy->modifiable = 1;
 
       /* Force the value to be fetched from the target now, to avoid problems
         later when this internalvar is referenced and the target is gone or
         has changed.  */
-      if (value_lazy (new_data.value))
-       value_fetch_lazy (new_data.value);
+      if (value_lazy (copy))
+       value_fetch_lazy (copy);
 
       /* Release the value from the value chain to prevent it from being
         deleted by free_all_values.  From here on this function should not
         call error () until new_data is installed into the var->u to avoid
         leaking memory.  */
-      release_value (new_data.value).release ();
+      new_data.value = release_value (copy).release ();
 
       /* Internal variables which are created from values with a dynamic
          location don't need the location property of the origin anymore.
@@ -2583,24 +2583,23 @@ value_from_xmethod (xmethod_worker_up &&worker)
 /* Return the type of the result of TYPE_CODE_XMETHOD value METHOD.  */
 
 struct type *
-result_type_of_xmethod (struct value *method, int argc, struct value **argv)
+result_type_of_xmethod (struct value *method, gdb::array_view<value *> argv)
 {
   gdb_assert (TYPE_CODE (value_type (method)) == TYPE_CODE_XMETHOD
-             && method->lval == lval_xcallable && argc > 0);
+             && method->lval == lval_xcallable && !argv.empty ());
 
-  return method->location.xm_worker->get_result_type
-    (argv[0], argv + 1, argc - 1);
+  return method->location.xm_worker->get_result_type (argv[0], argv.slice (1));
 }
 
 /* Call the xmethod corresponding to the TYPE_CODE_XMETHOD value METHOD.  */
 
 struct value *
-call_xmethod (struct value *method, int argc, struct value **argv)
+call_xmethod (struct value *method, gdb::array_view<value *> argv)
 {
   gdb_assert (TYPE_CODE (value_type (method)) == TYPE_CODE_XMETHOD
-             && method->lval == lval_xcallable && argc > 0);
+             && method->lval == lval_xcallable && !argv.empty ());
 
-  return method->location.xm_worker->invoke (argv[0], argv + 1, argc - 1);
+  return method->location.xm_worker->invoke (argv[0], argv.slice (1));
 }
 \f
 /* Extract a value as a C number (either long or double).
@@ -2777,7 +2776,6 @@ unpack_long (struct type *type, const gdb_byte *valaddr)
     default:
       error (_("Value can't be converted to integer."));
     }
-  return 0;                    /* Placate lint.  */
 }
 
 /* Unpack raw data (copied from debugee, target byte order) at VALADDR
@@ -3051,7 +3049,7 @@ value_fn_field (struct value **arg1p, struct fn_field *f,
   VALUE_LVAL (v) = lval_memory;
   if (sym)
     {
-      set_value_address (v, BLOCK_START (SYMBOL_BLOCK_VALUE (sym)));
+      set_value_address (v, BLOCK_ENTRY_PC (SYMBOL_BLOCK_VALUE (sym)));
     }
   else
     {
@@ -3062,7 +3060,7 @@ value_fn_field (struct value **arg1p, struct fn_field *f,
 
       set_value_address (v,
        gdbarch_convert_from_func_ptr_addr
-          (gdbarch, BMSYMBOL_VALUE_ADDRESS (msym), target_stack));
+          (gdbarch, BMSYMBOL_VALUE_ADDRESS (msym), current_top_target ()));
     }
 
   if (arg1p)
@@ -3432,6 +3430,19 @@ value_from_pointer (struct type *type, CORE_ADDR addr)
   return val;
 }
 
+/* Create and return a value object of TYPE containing the value D.  The
+   TYPE must be of TYPE_CODE_FLT, and must be large enough to hold D once
+   it is converted to target format.  */
+
+struct value *
+value_from_host_double (struct type *type, double d)
+{
+  struct value *value = allocate_value (type);
+  gdb_assert (TYPE_CODE (type) == TYPE_CODE_FLT);
+  target_float_from_host_double (value_contents_raw (value),
+                                value_type (value), d);
+  return value;
+}
 
 /* Create a value of type TYPE whose contents come from VALADDR, if it
    is non-null, and whose memory address (in the inferior) is
@@ -3714,6 +3725,164 @@ value_initialized (const struct value *val)
   return val->initialized;
 }
 
+/* Helper for value_fetch_lazy when the value is a bitfield.  */
+
+static void
+value_fetch_lazy_bitfield (struct value *val)
+{
+  gdb_assert (value_bitsize (val) != 0);
+
+  /* To read a lazy bitfield, read the entire enclosing value.  This
+     prevents reading the same block of (possibly volatile) memory once
+     per bitfield.  It would be even better to read only the containing
+     word, but we have no way to record that just specific bits of a
+     value have been fetched.  */
+  struct value *parent = value_parent (val);
+
+  if (value_lazy (parent))
+    value_fetch_lazy (parent);
+
+  unpack_value_bitfield (val, value_bitpos (val), value_bitsize (val),
+                        value_contents_for_printing (parent),
+                        value_offset (val), parent);
+}
+
+/* Helper for value_fetch_lazy when the value is in memory.  */
+
+static void
+value_fetch_lazy_memory (struct value *val)
+{
+  gdb_assert (VALUE_LVAL (val) == lval_memory);
+
+  CORE_ADDR addr = value_address (val);
+  struct type *type = check_typedef (value_enclosing_type (val));
+
+  if (TYPE_LENGTH (type))
+      read_value_memory (val, 0, value_stack (val),
+                        addr, value_contents_all_raw (val),
+                        type_length_units (type));
+}
+
+/* Helper for value_fetch_lazy when the value is in a register.  */
+
+static void
+value_fetch_lazy_register (struct value *val)
+{
+  struct frame_info *next_frame;
+  int regnum;
+  struct type *type = check_typedef (value_type (val));
+  struct value *new_val = val, *mark = value_mark ();
+
+  /* Offsets are not supported here; lazy register values must
+     refer to the entire register.  */
+  gdb_assert (value_offset (val) == 0);
+
+  while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
+    {
+      struct frame_id next_frame_id = VALUE_NEXT_FRAME_ID (new_val);
+
+      next_frame = frame_find_by_id (next_frame_id);
+      regnum = VALUE_REGNUM (new_val);
+
+      gdb_assert (next_frame != NULL);
+
+      /* Convertible register routines are used for multi-register
+        values and for interpretation in different types
+        (e.g. float or int from a double register).  Lazy
+        register values should have the register's natural type,
+        so they do not apply.  */
+      gdb_assert (!gdbarch_convert_register_p (get_frame_arch (next_frame),
+                                              regnum, type));
+
+      /* FRAME was obtained, above, via VALUE_NEXT_FRAME_ID.
+        Since a "->next" operation was performed when setting
+        this field, we do not need to perform a "next" operation
+        again when unwinding the register.  That's why
+        frame_unwind_register_value() is called here instead of
+        get_frame_register_value().  */
+      new_val = frame_unwind_register_value (next_frame, regnum);
+
+      /* If we get another lazy lval_register value, it means the
+        register is found by reading it from NEXT_FRAME's next frame.
+        frame_unwind_register_value should never return a value with
+        the frame id pointing to NEXT_FRAME.  If it does, it means we
+        either have two consecutive frames with the same frame id
+        in the frame chain, or some code is trying to unwind
+        behind get_prev_frame's back (e.g., a frame unwind
+        sniffer trying to unwind), bypassing its validations.  In
+        any case, it should always be an internal error to end up
+        in this situation.  */
+      if (VALUE_LVAL (new_val) == lval_register
+         && value_lazy (new_val)
+         && frame_id_eq (VALUE_NEXT_FRAME_ID (new_val), next_frame_id))
+       internal_error (__FILE__, __LINE__,
+                       _("infinite loop while fetching a register"));
+    }
+
+  /* If it's still lazy (for instance, a saved register on the
+     stack), fetch it.  */
+  if (value_lazy (new_val))
+    value_fetch_lazy (new_val);
+
+  /* Copy the contents and the unavailability/optimized-out
+     meta-data from NEW_VAL to VAL.  */
+  set_value_lazy (val, 0);
+  value_contents_copy (val, value_embedded_offset (val),
+                      new_val, value_embedded_offset (new_val),
+                      type_length_units (type));
+
+  if (frame_debug)
+    {
+      struct gdbarch *gdbarch;
+      struct frame_info *frame;
+      /* VALUE_FRAME_ID is used here, instead of VALUE_NEXT_FRAME_ID,
+        so that the frame level will be shown correctly.  */
+      frame = frame_find_by_id (VALUE_FRAME_ID (val));
+      regnum = VALUE_REGNUM (val);
+      gdbarch = get_frame_arch (frame);
+
+      fprintf_unfiltered (gdb_stdlog,
+                         "{ value_fetch_lazy "
+                         "(frame=%d,regnum=%d(%s),...) ",
+                         frame_relative_level (frame), regnum,
+                         user_reg_map_regnum_to_name (gdbarch, regnum));
+
+      fprintf_unfiltered (gdb_stdlog, "->");
+      if (value_optimized_out (new_val))
+       {
+         fprintf_unfiltered (gdb_stdlog, " ");
+         val_print_optimized_out (new_val, gdb_stdlog);
+       }
+      else
+       {
+         int i;
+         const gdb_byte *buf = value_contents (new_val);
+
+         if (VALUE_LVAL (new_val) == lval_register)
+           fprintf_unfiltered (gdb_stdlog, " register=%d",
+                               VALUE_REGNUM (new_val));
+         else if (VALUE_LVAL (new_val) == lval_memory)
+           fprintf_unfiltered (gdb_stdlog, " address=%s",
+                               paddress (gdbarch,
+                                         value_address (new_val)));
+         else
+           fprintf_unfiltered (gdb_stdlog, " computed");
+
+         fprintf_unfiltered (gdb_stdlog, " bytes=");
+         fprintf_unfiltered (gdb_stdlog, "[");
+         for (i = 0; i < register_size (gdbarch, regnum); i++)
+           fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
+         fprintf_unfiltered (gdb_stdlog, "]");
+       }
+
+      fprintf_unfiltered (gdb_stdlog, " }\n");
+    }
+
+  /* Dispose of the intermediate values.  This prevents
+     watchpoints from trying to watch the saved frame pointer.  */
+  value_free_to_mark (mark);
+}
+
 /* Load the actual content of a lazy value.  Fetch the data from the
    user's process and clear the lazy flag to indicate that the data in
    the buffer is valid.
@@ -3733,149 +3902,11 @@ value_fetch_lazy (struct value *val)
   gdb_assert (val->optimized_out.empty ());
   gdb_assert (val->unavailable.empty ());
   if (value_bitsize (val))
-    {
-      /* To read a lazy bitfield, read the entire enclosing value.  This
-        prevents reading the same block of (possibly volatile) memory once
-         per bitfield.  It would be even better to read only the containing
-         word, but we have no way to record that just specific bits of a
-         value have been fetched.  */
-      struct type *type = check_typedef (value_type (val));
-      struct value *parent = value_parent (val);
-
-      if (value_lazy (parent))
-       value_fetch_lazy (parent);
-
-      unpack_value_bitfield (val,
-                            value_bitpos (val), value_bitsize (val),
-                            value_contents_for_printing (parent),
-                            value_offset (val), parent);
-    }
+    value_fetch_lazy_bitfield (val);
   else if (VALUE_LVAL (val) == lval_memory)
-    {
-      CORE_ADDR addr = value_address (val);
-      struct type *type = check_typedef (value_enclosing_type (val));
-
-      if (TYPE_LENGTH (type))
-       read_value_memory (val, 0, value_stack (val),
-                          addr, value_contents_all_raw (val),
-                          type_length_units (type));
-    }
+    value_fetch_lazy_memory (val);
   else if (VALUE_LVAL (val) == lval_register)
-    {
-      struct frame_info *next_frame;
-      int regnum;
-      struct type *type = check_typedef (value_type (val));
-      struct value *new_val = val, *mark = value_mark ();
-
-      /* Offsets are not supported here; lazy register values must
-        refer to the entire register.  */
-      gdb_assert (value_offset (val) == 0);
-
-      while (VALUE_LVAL (new_val) == lval_register && value_lazy (new_val))
-       {
-         struct frame_id next_frame_id = VALUE_NEXT_FRAME_ID (new_val);
-
-         next_frame = frame_find_by_id (next_frame_id);
-         regnum = VALUE_REGNUM (new_val);
-
-         gdb_assert (next_frame != NULL);
-
-         /* Convertible register routines are used for multi-register
-            values and for interpretation in different types
-            (e.g. float or int from a double register).  Lazy
-            register values should have the register's natural type,
-            so they do not apply.  */
-         gdb_assert (!gdbarch_convert_register_p (get_frame_arch (next_frame),
-                                                  regnum, type));
-
-         /* FRAME was obtained, above, via VALUE_NEXT_FRAME_ID. 
-            Since a "->next" operation was performed when setting
-            this field, we do not need to perform a "next" operation
-            again when unwinding the register.  That's why
-            frame_unwind_register_value() is called here instead of
-            get_frame_register_value().  */
-         new_val = frame_unwind_register_value (next_frame, regnum);
-
-         /* If we get another lazy lval_register value, it means the
-            register is found by reading it from NEXT_FRAME's next frame.
-            frame_unwind_register_value should never return a value with
-            the frame id pointing to NEXT_FRAME.  If it does, it means we
-            either have two consecutive frames with the same frame id
-            in the frame chain, or some code is trying to unwind
-            behind get_prev_frame's back (e.g., a frame unwind
-            sniffer trying to unwind), bypassing its validations.  In
-            any case, it should always be an internal error to end up
-            in this situation.  */
-         if (VALUE_LVAL (new_val) == lval_register
-             && value_lazy (new_val)
-             && frame_id_eq (VALUE_NEXT_FRAME_ID (new_val), next_frame_id))
-           internal_error (__FILE__, __LINE__,
-                           _("infinite loop while fetching a register"));
-       }
-
-      /* If it's still lazy (for instance, a saved register on the
-        stack), fetch it.  */
-      if (value_lazy (new_val))
-       value_fetch_lazy (new_val);
-
-      /* Copy the contents and the unavailability/optimized-out
-        meta-data from NEW_VAL to VAL.  */
-      set_value_lazy (val, 0);
-      value_contents_copy (val, value_embedded_offset (val),
-                          new_val, value_embedded_offset (new_val),
-                          type_length_units (type));
-
-      if (frame_debug)
-       {
-         struct gdbarch *gdbarch;
-         struct frame_info *frame;
-         /* VALUE_FRAME_ID is used here, instead of VALUE_NEXT_FRAME_ID,
-            so that the frame level will be shown correctly.  */
-         frame = frame_find_by_id (VALUE_FRAME_ID (val));
-         regnum = VALUE_REGNUM (val);
-         gdbarch = get_frame_arch (frame);
-
-         fprintf_unfiltered (gdb_stdlog,
-                             "{ value_fetch_lazy "
-                             "(frame=%d,regnum=%d(%s),...) ",
-                             frame_relative_level (frame), regnum,
-                             user_reg_map_regnum_to_name (gdbarch, regnum));
-
-         fprintf_unfiltered (gdb_stdlog, "->");
-         if (value_optimized_out (new_val))
-           {
-             fprintf_unfiltered (gdb_stdlog, " ");
-             val_print_optimized_out (new_val, gdb_stdlog);
-           }
-         else
-           {
-             int i;
-             const gdb_byte *buf = value_contents (new_val);
-
-             if (VALUE_LVAL (new_val) == lval_register)
-               fprintf_unfiltered (gdb_stdlog, " register=%d",
-                                   VALUE_REGNUM (new_val));
-             else if (VALUE_LVAL (new_val) == lval_memory)
-               fprintf_unfiltered (gdb_stdlog, " address=%s",
-                                   paddress (gdbarch,
-                                             value_address (new_val)));
-             else
-               fprintf_unfiltered (gdb_stdlog, " computed");
-
-             fprintf_unfiltered (gdb_stdlog, " bytes=");
-             fprintf_unfiltered (gdb_stdlog, "[");
-             for (i = 0; i < register_size (gdbarch, regnum); i++)
-               fprintf_unfiltered (gdb_stdlog, "%02x", buf[i]);
-             fprintf_unfiltered (gdb_stdlog, "]");
-           }
-
-         fprintf_unfiltered (gdb_stdlog, " }\n");
-       }
-
-      /* Dispose of the intermediate values.  This prevents
-        watchpoints from trying to watch the saved frame pointer.  */
-      value_free_to_mark (mark);
-    }
+    value_fetch_lazy_register (val);
   else if (VALUE_LVAL (val) == lval_computed
           && value_computed_funcs (val)->read != NULL)
     value_computed_funcs (val)->read (val);
@@ -4101,3 +4132,11 @@ prevents future values, larger than this size, from being allocated."),
                            selftests::test_insert_into_bit_range_vector);
 #endif
 }
+
+/* See value.h.  */
+
+void
+finalize_values ()
+{
+  all_values.clear ();
+}
This page took 0.030611 seconds and 4 git commands to generate.