opcodes/
[binutils-gdb.git] / gdb / varobj.c
index 308649972108ad8a954abf511d3b7737c8b66ba3..d4fa6ba0cc03d08d0f343295c90d55e54cd2d0c1 100644 (file)
@@ -1,6 +1,6 @@
 /* Implementation of the GDB variable objects API.
 
-   Copyright (C) 1999-2012 Free Software Foundation, Inc.
+   Copyright (C) 1999-2013 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -49,7 +49,7 @@ typedef int PyObject;
 
 /* Non-zero if we want to see trace of varobj level stuff.  */
 
-int varobjdebug = 0;
+unsigned int varobjdebug = 0;
 static void
 show_varobjdebug (struct ui_file *file, int from_tty,
                  struct cmd_list_element *c, const char *value)
@@ -84,7 +84,7 @@ struct varobj_root
   struct expression *exp;
 
   /* Block for which this expression is valid.  */
-  struct block *valid_block;
+  const struct block *valid_block;
 
   /* The frame for this expression.  This field is set iff valid_block is
      not NULL.  */
@@ -620,10 +620,11 @@ varobj_create (char *objname,
       struct frame_info *fi;
       struct frame_id old_id = null_frame_id;
       struct block *block;
-      char *p;
+      const char *p;
       enum varobj_languages lang;
       struct value *value = NULL;
       volatile struct gdb_exception except;
+      CORE_ADDR pc;
 
       /* Parse and evaluate the expression, filling in as much of the
          variable's data as possible.  */
@@ -650,9 +651,13 @@ varobj_create (char *objname,
       if (type == USE_SELECTED_FRAME)
        var->root->floating = 1;
 
+      pc = 0;
       block = NULL;
       if (fi != NULL)
-       block = get_frame_block (fi, 0);
+       {
+         block = get_frame_block (fi, 0);
+         pc = get_frame_pc (fi);
+       }
 
       p = expression;
       innermost_block = NULL;
@@ -660,7 +665,7 @@ varobj_create (char *objname,
          return a sensible error.  */
       TRY_CATCH (except, RETURN_MASK_ERROR)
        {
-         var->root->exp = parse_exp_1 (&p, block, 0);
+         var->root->exp = parse_exp_1 (&p, pc, block, 0);
        }
 
       if (except.reason < 0)
@@ -670,7 +675,9 @@ varobj_create (char *objname,
        }
 
       /* Don't allow variables to be created for types.  */
-      if (var->root->exp->elts[0].opcode == OP_TYPE)
+      if (var->root->exp->elts[0].opcode == OP_TYPE
+         || var->root->exp->elts[0].opcode == OP_TYPEOF
+         || var->root->exp->elts[0].opcode == OP_DECLTYPE)
        {
          do_cleanups (old_chain);
          fprintf_unfiltered (gdb_stderr, "Attempt to use a type name"
@@ -928,7 +935,12 @@ varobj_get_display_hint (struct varobj *var)
   char *result = NULL;
 
 #if HAVE_PYTHON
-  struct cleanup *back_to = varobj_ensure_python_env (var);
+  struct cleanup *back_to;
+
+  if (!gdb_python_initialized)
+    return NULL;
+
+  back_to = varobj_ensure_python_env (var);
 
   if (var->pretty_printer)
     result = gdbpy_get_display_hint (var->pretty_printer);
@@ -1060,6 +1072,9 @@ dynamic_varobj_has_child_method (struct varobj *var)
   PyObject *printer = var->pretty_printer;
   int result;
 
+  if (!gdb_python_initialized)
+    return 0;
+
   back_to = varobj_ensure_python_env (var);
   result = PyObject_HasAttr (printer, gdbpy_children_cst);
   do_cleanups (back_to);
@@ -1085,6 +1100,9 @@ update_dynamic_varobj_children (struct varobj *var,
   int i;
   PyObject *printer = var->pretty_printer;
 
+  if (!gdb_python_initialized)
+    return 0;
+
   back_to = varobj_ensure_python_env (var);
 
   *cchanged = 0;
@@ -1107,9 +1125,6 @@ update_dynamic_varobj_children (struct varobj *var,
 
       make_cleanup_py_decref (children);
 
-      if (!PyIter_Check (children))
-       error (_("Returned value is not iterable"));
-
       Py_XDECREF (var->child_iter);
       var->child_iter = PyObject_GetIter (children);
       if (!var->child_iter)
@@ -1248,7 +1263,7 @@ update_dynamic_varobj_children (struct varobj *var,
 
   return 1;
 #else
-  gdb_assert (0 && "should never be called if Python is not enabled");
+  gdb_assert_not_reached ("should never be called if Python is not enabled");
 #endif
 }
 
@@ -1465,13 +1480,13 @@ varobj_set_value (struct varobj *var, char *expression)
   struct expression *exp;
   struct value *value = NULL; /* Initialize to keep gcc happy.  */
   int saved_input_radix = input_radix;
-  char *s = expression;
+  const char *s = expression;
   volatile struct gdb_exception except;
 
   gdb_assert (varobj_editable_p (var));
 
   input_radix = 10;            /* ALWAYS reset to decimal temporarily.  */
-  exp = parse_exp_1 (&s, 0, 0);
+  exp = parse_exp_1 (&s, 0, 0, 0);
   TRY_CATCH (except, RETURN_MASK_ERROR)
     {
       value = evaluate_expression (exp);
@@ -1619,6 +1634,9 @@ install_new_value_visualizer (struct varobj *var)
 #if HAVE_PYTHON
   /* If the constructor is None, then we want the raw value.  If VAR
      does not have a value, just skip this.  */
+  if (!gdb_python_initialized)
+    return;
+
   if (var->constructor != Py_None && var->value)
     {
       struct cleanup *cleanup;
@@ -1895,6 +1913,9 @@ varobj_set_visualizer (struct varobj *var, const char *visualizer)
   PyObject *mainmod, *globals, *constructor;
   struct cleanup *back_to;
 
+  if (!gdb_python_initialized)
+    return;
+
   back_to = varobj_ensure_python_env (var);
 
   mainmod = PyImport_AddModule ("__main__");
@@ -1965,7 +1986,6 @@ varobj_value_has_mutated (struct varobj *var, struct value *new_value,
 VEC(varobj_update_result) *
 varobj_update (struct varobj **varp, int explicit)
 {
-  int changed = 0;
   int type_changed = 0;
   int i;
   struct value *new;
@@ -2841,7 +2861,7 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
 {
   struct ui_file *stb;
   struct cleanup *old_chain;
-  gdb_byte *thevalue = NULL;
+  char *thevalue = NULL;
   struct value_print_options opts;
   struct type *type = NULL;
   long len = 0;
@@ -2859,94 +2879,93 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
 
   gdbarch = get_type_arch (value_type (value));
 #if HAVE_PYTHON
-  {
-    PyObject *value_formatter = var->pretty_printer;
+  if (gdb_python_initialized)
+    {
+      PyObject *value_formatter = var->pretty_printer;
 
-    varobj_ensure_python_env (var);
+      varobj_ensure_python_env (var);
 
-    if (value_formatter)
-      {
-       /* First check to see if we have any children at all.  If so,
-          we simply return {...}.  */
-       if (dynamic_varobj_has_child_method (var))
-         {
-           do_cleanups (old_chain);
-           return xstrdup ("{...}");
-         }
+      if (value_formatter)
+       {
+         /* First check to see if we have any children at all.  If so,
+            we simply return {...}.  */
+         if (dynamic_varobj_has_child_method (var))
+           {
+             do_cleanups (old_chain);
+             return xstrdup ("{...}");
+           }
 
-       if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
-         {
-           struct value *replacement;
-           PyObject *output = NULL;
+         if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
+           {
+             struct value *replacement;
+             PyObject *output = NULL;
 
-           output = apply_varobj_pretty_printer (value_formatter,
-                                                 &replacement,
-                                                 stb);
+             output = apply_varobj_pretty_printer (value_formatter,
+                                                   &replacement,
+                                                   stb);
 
-           /* If we have string like output ...  */
-           if (output)
-             {
-               make_cleanup_py_decref (output);
-
-               /* If this is a lazy string, extract it.  For lazy
-                  strings we always print as a string, so set
-                  string_print.  */
-               if (gdbpy_is_lazy_string (output))
-                 {
-                   gdbpy_extract_lazy_string (output, &str_addr, &type,
-                                              &len, &encoding);
-                   make_cleanup (free_current_contents, &encoding);
-                   string_print = 1;
-                 }
-               else
-                 {
-                   /* If it is a regular (non-lazy) string, extract
-                      it and copy the contents into THEVALUE.  If the
-                      hint says to print it as a string, set
-                      string_print.  Otherwise just return the extracted
-                      string as a value.  */
-
-                   PyObject *py_str
-                     = python_string_to_target_python_string (output);
-
-                   if (py_str)
-                     {
-                       char *s = PyString_AsString (py_str);
-                       char *hint;
-
-                       hint = gdbpy_get_display_hint (value_formatter);
-                       if (hint)
-                         {
-                           if (!strcmp (hint, "string"))
-                             string_print = 1;
-                           xfree (hint);
-                         }
-
-                       len = PyString_Size (py_str);
-                       thevalue = xmemdup (s, len + 1, len + 1);
-                       type = builtin_type (gdbarch)->builtin_char;
-                       Py_DECREF (py_str);
-
-                       if (!string_print)
-                         {
-                           do_cleanups (old_chain);
-                           return thevalue;
-                         }
-
-                       make_cleanup (xfree, thevalue);
-                     }
-                   else
-                     gdbpy_print_stack ();
-                 }
-             }
-           /* If the printer returned a replacement value, set VALUE
-              to REPLACEMENT.  If there is not a replacement value,
-              just use the value passed to this function.  */
-           if (replacement)
-             value = replacement;
-         }
-      }
-  }
+             /* If we have string like output ...  */
+             if (output)
+               {
+                 make_cleanup_py_decref (output);
+
+                 /* If this is a lazy string, extract it.  For lazy
+                    strings we always print as a string, so set
+                    string_print.  */
+                 if (gdbpy_is_lazy_string (output))
+                   {
+                     gdbpy_extract_lazy_string (output, &str_addr, &type,
+                                                &len, &encoding);
+                     make_cleanup (free_current_contents, &encoding);
+                     string_print = 1;
+                   }
+                 else
+                   {
+                     /* If it is a regular (non-lazy) string, extract
+                        it and copy the contents into THEVALUE.  If the
+                        hint says to print it as a string, set
+                        string_print.  Otherwise just return the extracted
+                        string as a value.  */
+
+                     char *s = python_string_to_target_string (output);
+
+                     if (s)
+                       {
+                         char *hint;
+
+                         hint = gdbpy_get_display_hint (value_formatter);
+                         if (hint)
+                           {
+                             if (!strcmp (hint, "string"))
+                               string_print = 1;
+                             xfree (hint);
+                           }
+
+                         len = strlen (s);
+                         thevalue = xmemdup (s, len + 1, len + 1);
+                         type = builtin_type (gdbarch)->builtin_char;
+                         xfree (s);
+
+                         if (!string_print)
+                           {
+                             do_cleanups (old_chain);
+                             return thevalue;
+                           }
+
+                         make_cleanup (xfree, thevalue);
+                       }
+                     else
+                       gdbpy_print_stack ();
+                   }
+               }
+             /* If the printer returned a replacement value, set VALUE
+                to REPLACEMENT.  If there is not a replacement value,
+                just use the value passed to this function.  */
+             if (replacement)
+               value = replacement;
+           }
+       }
+    }
 #endif
 
   get_formatted_print_options (&opts, format_code[(int) format]);
@@ -2955,7 +2974,7 @@ value_get_print_value (struct value *value, enum varobj_display_formats format,
 
   /* If the THEVALUE has contents, it is a regular string.  */
   if (thevalue)
-    LA_PRINT_STRING (stb, type, thevalue, len, encoding, 0, &opts);
+    LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue, len, encoding, 0, &opts);
   else if (string_print)
     /* Otherwise, if string_print is set, and it is not a regular
        string, it is a lazy string.  */
@@ -3455,13 +3474,11 @@ c_value_of_root (struct varobj **var_handle)
        {
          new_val = evaluate_expression (var->root->exp);
        }
-
-      return new_val;
     }
 
   do_cleanups (back_to);
 
-  return NULL;
+  return new_val;
 }
 
 static struct value *
@@ -4169,28 +4186,27 @@ _initialize_varobj (void)
   varobj_table = xmalloc (sizeof_table);
   memset (varobj_table, 0, sizeof_table);
 
-  add_setshow_zinteger_cmd ("debugvarobj", class_maintenance,
-                           &varobjdebug,
-                           _("Set varobj debugging."),
-                           _("Show varobj debugging."),
-                           _("When non-zero, varobj debugging is enabled."),
-                           NULL, show_varobjdebug,
-                           &setlist, &showlist);
+  add_setshow_zuinteger_cmd ("debugvarobj", class_maintenance,
+                            &varobjdebug,
+                            _("Set varobj debugging."),
+                            _("Show varobj debugging."),
+                            _("When non-zero, varobj debugging is enabled."),
+                            NULL, show_varobjdebug,
+                            &setlist, &showlist);
 }
 
 /* Invalidate varobj VAR if it is tied to locals and re-create it if it is
-   defined on globals.  It is a helper for varobj_invalidate.  */
+   defined on globals.  It is a helper for varobj_invalidate.
+
+   This function is called after changing the symbol file, in this case the
+   pointers to "struct type" stored by the varobj are no longer valid.  All
+   varobj must be either re-evaluated, or marked as invalid here.  */
 
 static void
 varobj_invalidate_iter (struct varobj *var, void *unused)
 {
-  /* Floating varobjs are reparsed on each stop, so we don't care if the
-     presently parsed expression refers to something that's gone.  */
-  if (var->root->floating)
-    return;
-
-  /* global var must be re-evaluated.  */     
-  if (var->root->valid_block == NULL)
+  /* global and floating var must be re-evaluated.  */
+  if (var->root->floating || var->root->valid_block == NULL)
     {
       struct varobj *tmp_var;