This patch generalizes varobj iterator, in a python-independent way.
Note varobj_item is still a typedef of PyObject, we can only focus on
API changes, and leave the data type changes to the next patch. As a
result, we include "varobj-iter.h" after the typedef of PyObject in
varobj.c, but it is an intermediate state. Finally, varobj-iter.h is
independent of PyObject.
This change is helpful to move some python-related code out of
varobj.c.
V2:
- Fix a missing cleanup.
- Fix typos.
- Use XNEW.
- Check against NULL explicitly.
- Update copyright year for new added files.
V3:
- Call PyGILState_Ensure before Py_XDECREF.
- Use CPYCHECKER_STEALS_REFERENCE_TO_ARG.
- Code indentation.
V4:
- use varobj_ensure_python_env instead of PyGILState_Ensure.
gdb:
2014-06-12 Pedro Alves <pedro@codesourcery.com>
Yao Qi <yao@codesourcery.com>
* Makefile.in (SUBDIR_PYTHON_OBS): Add "py-varobj.o".
(SUBDIR_PYTHON_SRCS): Add "python/py-varobj.c".
(HFILES_NO_SRCDIR): Add "varobj-iter.h".
(py-varobj.o): New rule.
* python/py-varobj.c: New file.
* python/python-internal.h (py_varobj_get_iterator): Declare.
* varobj-iter.h: New file.
* varobj.c: Include "varobj-iter.h"
(struct varobj) <child_iter>: Change its type from "PyObject *"
to "struct varobj_iter *".
<saved_item>: Likewise.
[HAVE_PYTHON] (varobj_ensure_python_env): Make it extern.
[HAVE_PYTHON] (varobj_get_iterator): New function.
(update_dynamic_varobj_children) [HAVE_PYTHON]: Move
python-specific code to python/py-varobj.c.
(install_visualizer): Call varobj_iter_delete instead of
Py_XDECREF.
* varobj.h (varobj_ensure_python_env): Declare.
+2014-06-12 Pedro Alves <pedro@codesourcery.com>
+ Yao Qi <yao@codesourcery.com>
+
+ * Makefile.in (SUBDIR_PYTHON_OBS): Add "py-varobj.o".
+ (SUBDIR_PYTHON_SRCS): Add "python/py-varobj.c".
+ (HFILES_NO_SRCDIR): Add "varobj-iter.h".
+ (py-varobj.o): New rule.
+ * python/py-varobj.c: New file.
+ * python/python-internal.h (py_varobj_get_iterator): Declare.
+ * varobj-iter.h: New file.
+ * varobj.c: Include "varobj-iter.h"
+ (struct varobj) <child_iter>: Change its type from "PyObject *"
+ to "struct varobj_iter *".
+ <saved_item>: Likewise.
+ [HAVE_PYTHON] (varobj_ensure_python_env): Make it extern.
+ [HAVE_PYTHON] (varobj_get_iterator): New function.
+ (update_dynamic_varobj_children) [HAVE_PYTHON]: Move
+ python-specific code to python/py-varobj.c.
+ (install_visualizer): Call varobj_iter_delete instead of
+ Py_XDECREF.
+ * varobj.h (varobj_ensure_python_env): Declare.
+
2014-06-12 Yao Qi <yao@codesourcery.com>
* varobj.c (struct varobj_item): New structure.
py-threadevent.o \
py-type.o \
py-utils.o \
- py-value.o
+ py-value.o \
+ py-varobj.o
SUBDIR_PYTHON_SRCS = \
python/python.c \
python/py-threadevent.c \
python/py-type.c \
python/py-utils.c \
- python/py-value.c
+ python/py-value.c \
+ python/py-varobj.c
SUBDIR_PYTHON_DEPS =
SUBDIR_PYTHON_LDFLAGS=
SUBDIR_PYTHON_CFLAGS=
ppcnbsd-tdep.h cli-out.h gdb_expat.h breakpoint.h infcall.h obsd-tdep.h \
exec.h m32r-tdep.h osabi.h gdbcore.h solib-som.h amd64bsd-nat.h \
i386bsd-nat.h xml-support.h xml-tdesc.h alphabsd-tdep.h gdb_obstack.h \
-ia64-tdep.h ada-lang.h varobj.h frv-tdep.h nto-tdep.h serial.h \
+ia64-tdep.h ada-lang.h ada-varobj.h varobj.h varobj-iter.h frv-tdep.h \
+nto-tdep.h serial.h \
c-lang.h d-lang.h go-lang.h frame.h event-loop.h block.h cli/cli-setshow.h \
cli/cli-decode.h cli/cli-cmds.h cli/cli-utils.h \
cli/cli-script.h macrotab.h symtab.h common/version.h \
$(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-value.c
$(POSTCOMPILE)
+py-varobj.o: $(srcdir)/python/py-varobj.c
+ $(COMPILE) $(PYTHON_CFLAGS) $(srcdir)/python/py-varobj.c
+ $(POSTCOMPILE)
+
#
# Dependency tracking. Most of this is conditional on GNU Make being
# found by configure; if GNU Make is not found, we fall back to a
--- /dev/null
+/* Copyright (C) 2013-2014 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
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "varobj.h"
+#include "varobj-iter.h"
+
+/* A dynamic varobj iterator "class" for python pretty-printed
+ varobjs. This inherits struct varobj_iter. */
+
+struct py_varobj_iter
+{
+ /* The 'base class'. */
+ struct varobj_iter base;
+
+ /* The python iterator returned by the printer's 'children' method,
+ or NULL if not available. */
+ PyObject *iter;
+};
+
+/* Implementation of the 'dtor' method of pretty-printed varobj
+ iterators. */
+
+static void
+py_varobj_iter_dtor (struct varobj_iter *self)
+{
+ struct py_varobj_iter *dis = (struct py_varobj_iter *) self;
+ struct cleanup *back_to = varobj_ensure_python_env (self->var);
+
+ Py_XDECREF (dis->iter);
+
+ do_cleanups (back_to);
+}
+
+/* Implementation of the 'next' method of pretty-printed varobj
+ iterators. */
+
+static varobj_item *
+py_varobj_iter_next (struct varobj_iter *self)
+{
+ struct py_varobj_iter *t = (struct py_varobj_iter *) self;
+ struct cleanup *back_to;
+ PyObject *item;
+
+ back_to = varobj_ensure_python_env (self->var);
+
+ item = PyIter_Next (t->iter);
+
+ if (item == NULL)
+ {
+ /* Normal end of iteration. */
+ if (!PyErr_Occurred ())
+ return NULL;
+
+ /* If we got a memory error, just use the text as the item. */
+ if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
+ {
+ PyObject *type, *value, *trace;
+ char *name_str, *value_str;
+
+ PyErr_Fetch (&type, &value, &trace);
+ value_str = gdbpy_exception_to_string (type, value);
+ Py_XDECREF (type);
+ Py_XDECREF (value);
+ Py_XDECREF (trace);
+ if (value_str == NULL)
+ {
+ gdbpy_print_stack ();
+ return NULL;
+ }
+
+ name_str = xstrprintf ("<error at %d>",
+ self->next_raw_index++);
+ item = Py_BuildValue ("(ss)", name_str, value_str);
+ xfree (name_str);
+ xfree (value_str);
+ if (item == NULL)
+ {
+ gdbpy_print_stack ();
+ return NULL;
+ }
+ }
+ else
+ {
+ /* Any other kind of error. */
+ gdbpy_print_stack ();
+ return NULL;
+ }
+ }
+
+ self->next_raw_index++;
+ do_cleanups (back_to);
+ return item;
+}
+
+/* The 'vtable' of pretty-printed python varobj iterators. */
+
+static const struct varobj_iter_ops py_varobj_iter_ops =
+{
+ py_varobj_iter_dtor,
+ py_varobj_iter_next
+};
+
+/* Constructor of pretty-printed varobj iterators. VAR is the varobj
+ whose children the iterator will be iterating over. PYITER is the
+ python iterator actually responsible for the iteration. */
+
+static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3)
+py_varobj_iter_ctor (struct py_varobj_iter *self,
+ struct varobj *var, PyObject *pyiter)
+{
+ self->base.var = var;
+ self->base.ops = &py_varobj_iter_ops;
+ self->base.next_raw_index = 0;
+ self->iter = pyiter;
+}
+
+/* Allocate and construct a pretty-printed varobj iterator. VAR is
+ the varobj whose children the iterator will be iterating over.
+ PYITER is the python iterator actually responsible for the
+ iteration. */
+
+static struct py_varobj_iter * CPYCHECKER_STEALS_REFERENCE_TO_ARG (2)
+py_varobj_iter_new (struct varobj *var, PyObject *pyiter)
+{
+ struct py_varobj_iter *self;
+
+ self = XNEW (struct py_varobj_iter);
+ py_varobj_iter_ctor (self, var, pyiter);
+ return self;
+}
+
+/* Return a new pretty-printed varobj iterator suitable to iterate
+ over VAR's children. */
+
+struct varobj_iter *
+py_varobj_get_iterator (struct varobj *var, PyObject *printer)
+{
+ PyObject *children;
+ int i;
+ PyObject *iter;
+ struct py_varobj_iter *py_iter;
+ struct cleanup *back_to = varobj_ensure_python_env (var);
+
+ if (!PyObject_HasAttr (printer, gdbpy_children_cst))
+ {
+ do_cleanups (back_to);
+ return NULL;
+ }
+
+ children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
+ NULL);
+ if (children == NULL)
+ {
+ gdbpy_print_stack ();
+ error (_("Null value returned for children"));
+ }
+
+ make_cleanup_py_decref (children);
+
+ iter = PyObject_GetIter (children);
+ if (iter == NULL)
+ {
+ gdbpy_print_stack ();
+ error (_("Could not get children iterator"));
+ }
+
+ py_iter = py_varobj_iter_new (var, iter);
+
+ do_cleanups (back_to);
+
+ return &py_iter->base;
+}
PyObject *object)
CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+struct varobj_iter;
+struct varobj;
+struct varobj_iter *py_varobj_get_iterator (struct varobj *var,
+ PyObject *printer);
+
#endif /* GDB_PYTHON_INTERNAL_H */
--- /dev/null
+/* Iterator of varobj.
+ Copyright (C) 2013-2014 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
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+struct varobj_iter_ops;
+
+typedef PyObject varobj_item;
+
+/* A dynamic varobj iterator "class". */
+
+struct varobj_iter
+{
+ /* The 'vtable'. */
+ const struct varobj_iter_ops *ops;
+
+ /* The varobj this iterator is listing children for. */
+ struct varobj *var;
+
+ /* The next raw index we will try to check is available. If it is
+ equal to number_of_children, then we've already iterated the
+ whole set. */
+ int next_raw_index;
+};
+
+/* The vtable of the varobj iterator class. */
+
+struct varobj_iter_ops
+{
+ /* Destructor. Releases everything from SELF (but not SELF
+ itself). */
+ void (*dtor) (struct varobj_iter *self);
+
+ /* Returns the next object or NULL if it has reached the end. */
+ varobj_item *(*next) (struct varobj_iter *self);
+};
+
+/* Returns the next varobj or NULL if it has reached the end. */
+
+#define varobj_iter_next(ITER) (ITER)->ops->next (ITER)
+
+/* Delete a varobj_iter object. */
+
+#define varobj_iter_delete(ITER) \
+ do \
+ { \
+ if ((ITER) != NULL) \
+ { \
+ (ITER)->ops->dtor (ITER); \
+ xfree (ITER); \
+ } \
+ } while (0)
typedef int PyObject;
#endif
+#include "varobj-iter.h"
+
/* Non-zero if we want to see trace of varobj level stuff. */
unsigned int varobjdebug = 0;
/* The iterator returned by the printer's 'children' method, or NULL
if not available. */
- PyObject *child_iter;
+ struct varobj_iter *child_iter;
/* We request one extra item from the iterator, so that we can
report to the caller whether there are more items than we have
already reported. However, we don't want to install this value
when we read it, because that will mess up future updates. So,
we stash it here instead. */
- PyObject *saved_item;
+ varobj_item *saved_item;
};
struct cpstack
#ifdef HAVE_PYTHON
/* Helper function to install a Python environment suitable for
use during operations on VAR. */
-static struct cleanup *
+struct cleanup *
varobj_ensure_python_env (struct varobj *var)
{
return ensure_python_env (var->root->exp->gdbarch,
return result;
}
+/* A factory for creating dynamic varobj's iterators. Returns an
+ iterator object suitable for iterating over VAR's children. */
+
+static struct varobj_iter *
+varobj_get_iterator (struct varobj *var)
+{
+ if (var->dynamic->pretty_printer)
+ return py_varobj_get_iterator (var, var->dynamic->pretty_printer);
+
+ gdb_assert_not_reached (_("\
+requested an iterator from a non-dynamic varobj"));
+}
+
#endif
static int
{
#if HAVE_PYTHON
struct cleanup *back_to;
- PyObject *children;
int i;
- PyObject *printer = var->dynamic->pretty_printer;
if (!gdb_python_initialized)
return 0;
back_to = varobj_ensure_python_env (var);
*cchanged = 0;
- if (!PyObject_HasAttr (printer, gdbpy_children_cst))
- {
- do_cleanups (back_to);
- return 0;
- }
if (update_children || var->dynamic->child_iter == NULL)
{
- children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
- NULL);
+ varobj_iter_delete (var->dynamic->child_iter);
+ var->dynamic->child_iter = varobj_get_iterator (var);
- if (!children)
- {
- gdbpy_print_stack ();
- error (_("Null value returned for children"));
- }
+ Py_XDECREF (var->dynamic->saved_item);
+ var->dynamic->saved_item = NULL;
- make_cleanup_py_decref (children);
+ i = 0;
- Py_XDECREF (var->dynamic->child_iter);
- var->dynamic->child_iter = PyObject_GetIter (children);
if (var->dynamic->child_iter == NULL)
{
- gdbpy_print_stack ();
- error (_("Could not get children iterator"));
+ do_cleanups (back_to);
+ return 0;
}
-
- Py_XDECREF (var->dynamic->saved_item);
- var->dynamic->saved_item = NULL;
-
- i = 0;
}
else
i = VEC_length (varobj_p, var->children);
for (; to < 0 || i < to + 1; ++i)
{
PyObject *item;
- int force_done = 0;
/* See if there was a leftover from last time. */
if (var->dynamic->saved_item)
var->dynamic->saved_item = NULL;
}
else
- item = PyIter_Next (var->dynamic->child_iter);
-
- if (!item)
{
- /* Normal end of iteration. */
- if (!PyErr_Occurred ())
- break;
-
- /* If we got a memory error, just use the text as the
- item. */
- if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
- {
- PyObject *type, *value, *trace;
- char *name_str, *value_str;
-
- PyErr_Fetch (&type, &value, &trace);
- value_str = gdbpy_exception_to_string (type, value);
- Py_XDECREF (type);
- Py_XDECREF (value);
- Py_XDECREF (trace);
- if (!value_str)
- {
- gdbpy_print_stack ();
- break;
- }
-
- name_str = xstrprintf ("<error at %d>", i);
- item = Py_BuildValue ("(ss)", name_str, value_str);
- xfree (name_str);
- xfree (value_str);
- if (!item)
- {
- gdbpy_print_stack ();
- break;
- }
-
- force_done = 1;
- }
- else
- {
- /* Any other kind of error. */
- gdbpy_print_stack ();
- break;
- }
+ item = varobj_iter_next (var->dynamic->child_iter);
}
+ if (item == NULL)
+ {
+ /* Iteration is done. Remove iterator from VAR. */
+ varobj_iter_delete (var->dynamic->child_iter);
+ var->dynamic->child_iter = NULL;
+ break;
+ }
/* We don't want to push the extra child on any report list. */
if (to < 0 || i < to)
{
element. */
break;
}
-
- if (force_done)
- break;
}
if (i < VEC_length (varobj_p, var->children))
*cchanged = 1;
var->num_children = VEC_length (varobj_p, var->children);
-
- do_cleanups (back_to);
+ do_cleanups (back_to);
return 1;
#else
gdb_assert_not_reached ("should never be called if Python is not enabled");
Py_XDECREF (var->pretty_printer);
var->pretty_printer = visualizer;
- Py_XDECREF (var->child_iter);
+ varobj_iter_delete (var->child_iter);
var->child_iter = NULL;
}
extern int varobj_pretty_printed_p (struct varobj *var);
+extern struct cleanup *varobj_ensure_python_env (struct varobj *var);
+
extern int varobj_default_value_is_changeable_p (struct varobj *var);
extern int varobj_value_is_changeable_p (struct varobj *var);