GDBserver self tests
[deliverable/binutils-gdb.git] / gdb / varobj.c
index 03edf0eb9692ab524926ae01289aa51e06b63993..7bd549d45c59d64eea6a3670e9b20066b94b2a2b 100644 (file)
@@ -1,6 +1,6 @@
 /* Implementation of the GDB variable objects API.
 
-   Copyright (C) 1999-2016 Free Software Foundation, Inc.
+   Copyright (C) 1999-2017 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
@@ -34,6 +34,7 @@
 #if HAVE_PYTHON
 #include "python/python.h"
 #include "python/python-internal.h"
+#include "python/py-ref.h"
 #else
 typedef int PyObject;
 #endif
@@ -49,7 +50,7 @@ show_varobjdebug (struct ui_file *file, int from_tty,
 }
 
 /* String representations of gdb's format codes.  */
-char *varobj_format_string[] =
+const char *varobj_format_string[] =
   { "natural", "binary", "decimal", "hexadecimal", "octal", "zero-hexadecimal" };
 
 /* True if we want to allow Python-based pretty-printing.  */
@@ -225,14 +226,13 @@ is_root_p (const struct varobj *var)
 }
 
 #ifdef HAVE_PYTHON
-/* Helper function to install a Python environment suitable for
-   use during operations on VAR.  */
-struct cleanup *
-varobj_ensure_python_env (const struct varobj *var)
+
+/* See python-internal.h.  */
+gdbpy_enter_varobj::gdbpy_enter_varobj (const struct varobj *var)
+: gdbpy_enter (var->root->exp->gdbarch, var->root->exp->language_defn)
 {
-  return ensure_python_env (var->root->exp->gdbarch,
-                           var->root->exp->language_defn);
 }
+
 #endif
 
 /* Return the full FRAME which corresponds to the given CORE_ADDR
@@ -563,17 +563,13 @@ varobj_get_display_hint (const struct varobj *var)
   gdb::unique_xmalloc_ptr<char> result;
 
 #if HAVE_PYTHON
-  struct cleanup *back_to;
-
   if (!gdb_python_initialized)
     return NULL;
 
-  back_to = varobj_ensure_python_env (var);
+  gdbpy_enter_varobj enter_py (var);
 
   if (var->dynamic->pretty_printer != NULL)
     result = gdbpy_get_display_hint (var->dynamic->pretty_printer);
-
-  do_cleanups (back_to);
 #endif
 
   return result;
@@ -695,17 +691,13 @@ install_dynamic_child (struct varobj *var,
 static int
 dynamic_varobj_has_child_method (const struct varobj *var)
 {
-  struct cleanup *back_to;
   PyObject *printer = var->dynamic->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);
-  return result;
+  gdbpy_enter_varobj enter_py (var);
+  return PyObject_HasAttr (printer, gdbpy_children_cst);
 }
 #endif
 
@@ -732,7 +724,7 @@ varobj_clear_saved_item (struct varobj_dynamic *var)
   if (var->saved_item != NULL)
     {
       value_free (var->saved_item->value);
-      xfree (var->saved_item);
+      delete var->saved_item;
       var->saved_item = NULL;
     }
 }
@@ -807,7 +799,7 @@ update_dynamic_varobj_children (struct varobj *var,
                                 can_mention ? cchanged : NULL, i,
                                 item);
 
-         xfree (item);
+         delete item;
        }
       else
        {
@@ -1213,16 +1205,12 @@ install_new_value_visualizer (struct varobj *var)
 
   if (var->dynamic->constructor != Py_None && var->value != NULL)
     {
-      struct cleanup *cleanup;
-
-      cleanup = varobj_ensure_python_env (var);
+      gdbpy_enter_varobj enter_py (var);
 
       if (var->dynamic->constructor == NULL)
        install_default_visualizer (var);
       else
        construct_visualizer (var, var->dynamic->constructor);
-
-      do_cleanups (cleanup);
     }
 #else
   /* Do nothing.  */
@@ -1482,35 +1470,31 @@ void
 varobj_set_visualizer (struct varobj *var, const char *visualizer)
 {
 #if HAVE_PYTHON
-  PyObject *mainmod, *globals, *constructor;
-  struct cleanup *back_to;
+  PyObject *mainmod;
 
   if (!gdb_python_initialized)
     return;
 
-  back_to = varobj_ensure_python_env (var);
+  gdbpy_enter_varobj enter_py (var);
 
   mainmod = PyImport_AddModule ("__main__");
-  globals = PyModule_GetDict (mainmod);
-  Py_INCREF (globals);
-  make_cleanup_py_decref (globals);
+  gdbpy_ref<> globals (PyModule_GetDict (mainmod));
+  Py_INCREF (globals.get ());
 
-  constructor = PyRun_String (visualizer, Py_eval_input, globals, globals);
+  gdbpy_ref<> constructor (PyRun_String (visualizer, Py_eval_input,
+                                        globals.get (), globals.get ()));
 
-  if (! constructor)
+  if (constructor == NULL)
     {
       gdbpy_print_stack ();
       error (_("Could not evaluate visualizer expression: %s"), visualizer);
     }
 
-  construct_visualizer (var, constructor);
-  Py_XDECREF (constructor);
+  construct_visualizer (var, constructor.get ());
 
   /* If there are any children now, wipe them.  */
   varobj_delete (var, 1 /* children only */);
   var->num_children = -1;
-
-  do_cleanups (back_to);
 #else
   error (_("Python support required"));
 #endif
@@ -2099,11 +2083,10 @@ free_variable (struct varobj *var)
 #if HAVE_PYTHON
   if (var->dynamic->pretty_printer != NULL)
     {
-      struct cleanup *cleanup = varobj_ensure_python_env (var);
+      gdbpy_enter_varobj enter_py (var);
 
       Py_XDECREF (var->dynamic->constructor);
       Py_XDECREF (var->dynamic->pretty_printer);
-      do_cleanups (cleanup);
     }
 #endif
 
@@ -2153,7 +2136,7 @@ varobj_get_value_type (const struct varobj *var)
 
   type = check_typedef (type);
 
-  if (TYPE_CODE (type) == TYPE_CODE_REF)
+  if (TYPE_IS_REFERENCE (type))
     type = get_target_type (type);
 
   type = check_typedef (type);
@@ -2235,14 +2218,13 @@ value_of_root_1 (struct varobj **var_handle)
   struct value *new_val = NULL;
   struct varobj *var = *var_handle;
   int within_scope = 0;
-  struct cleanup *back_to;
                                                                 
   /*  Only root variables can be updated...  */
   if (!is_root_p (var))
     /* Not a root var.  */
     return NULL;
 
-  back_to = make_cleanup_restore_current_thread ();
+  scoped_restore_current_thread restore_thread;
 
   /* Determine whether the variable is still around.  */
   if (var->root->valid_block == NULL || var->root->floating)
@@ -2281,8 +2263,6 @@ value_of_root_1 (struct varobj **var_handle)
       END_CATCH
     }
 
-  do_cleanups (back_to);
-
   return new_val;
 }
 
@@ -2417,12 +2397,10 @@ varobj_value_get_print_value (struct value *value,
                              enum varobj_display_formats format,
                              const struct varobj *var)
 {
-  struct ui_file *stb;
-  struct cleanup *old_chain;
   struct value_print_options opts;
   struct type *type = NULL;
   long len = 0;
-  char *encoding = NULL;
+  gdb::unique_xmalloc_ptr<char> encoding;
   /* Initialize it just to avoid a GCC false warning.  */
   CORE_ADDR str_addr = 0;
   int string_print = 0;
@@ -2430,9 +2408,7 @@ varobj_value_get_print_value (struct value *value,
   if (value == NULL)
     return std::string ();
 
-  stb = mem_fileopen ();
-  old_chain = make_cleanup_ui_file_delete (stb);
-
+  string_file stb;
   std::string thevalue;
 
 #if HAVE_PYTHON
@@ -2440,40 +2416,33 @@ varobj_value_get_print_value (struct value *value,
     {
       PyObject *value_formatter =  var->dynamic->pretty_printer;
 
-      varobj_ensure_python_env (var);
+      gdbpy_enter_varobj enter_py (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 ("{...}");
-           }
+           return "{...}";
 
          if (PyObject_HasAttr (value_formatter, gdbpy_to_string_cst))
            {
              struct value *replacement;
-             PyObject *output = NULL;
 
-             output = apply_varobj_pretty_printer (value_formatter,
-                                                   &replacement,
-                                                   stb);
+             gdbpy_ref<> output (apply_varobj_pretty_printer (value_formatter,
+                                                              &replacement,
+                                                              &stb));
 
              /* If we have string like output ...  */
-             if (output)
+             if (output != NULL)
                {
-                 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))
+                 if (gdbpy_is_lazy_string (output.get ()))
                    {
-                     gdbpy_extract_lazy_string (output, &str_addr, &type,
-                                                &len, &encoding);
-                     make_cleanup (free_current_contents, &encoding);
+                     gdbpy_extract_lazy_string (output.get (), &str_addr,
+                                                &type, &len, &encoding);
                      string_print = 1;
                    }
                  else
@@ -2485,7 +2454,7 @@ varobj_value_get_print_value (struct value *value,
                         string as a value.  */
 
                      gdb::unique_xmalloc_ptr<char> s
-                       = python_string_to_target_string (output);
+                       = python_string_to_target_string (output.get ());
 
                      if (s)
                        {
@@ -2505,10 +2474,7 @@ varobj_value_get_print_value (struct value *value,
                          type = builtin_type (gdbarch)->builtin_char;
 
                          if (!string_print)
-                           {
-                             do_cleanups (old_chain);
-                             return thevalue;
-                           }
+                           return thevalue;
                        }
                      else
                        gdbpy_print_stack ();
@@ -2528,20 +2494,17 @@ varobj_value_get_print_value (struct value *value,
 
   /* If the THEVALUE has contents, it is a regular string.  */
   if (!thevalue.empty ())
-    LA_PRINT_STRING (stb, type, (gdb_byte *) thevalue.c_str (),
-                    len, encoding, 0, &opts);
+    LA_PRINT_STRING (&stb, type, (gdb_byte *) thevalue.c_str (),
+                    len, encoding.get (), 0, &opts);
   else if (string_print)
     /* Otherwise, if string_print is set, and it is not a regular
        string, it is a lazy string.  */
-    val_print_string (type, encoding, str_addr, len, stb, &opts);
+    val_print_string (type, encoding.get (), str_addr, len, &stb, &opts);
   else
     /* All other cases.  */
-    common_val_print (value, stb, 0, &opts, current_language);
-
-  thevalue = ui_file_as_string (stb);
+    common_val_print (value, &stb, 0, &opts, current_language);
 
-  do_cleanups (old_chain);
-  return thevalue;
+  return std::move (stb.string ());
 }
 
 int
This page took 0.028062 seconds and 4 git commands to generate.