aboutsummaryrefslogtreecommitdiff
path: root/gdb/python/py-varobj.c
diff options
context:
space:
mode:
authorYao Qi <yao@codesourcery.com>2013-08-30 09:44:46 +0800
committerYao Qi <yao@codesourcery.com>2014-06-12 15:27:15 +0800
commite525021603958709381fc4dc296cc2586aaa5dd7 (patch)
tree329c330c7a6febccb5e29d93fd96e06fecea512f /gdb/python/py-varobj.c
parent5a2e0d6e89f69f0c3d013cf43f3bd3d97b5e628b (diff)
downloadgdb-e525021603958709381fc4dc296cc2586aaa5dd7.zip
gdb-e525021603958709381fc4dc296cc2586aaa5dd7.tar.gz
gdb-e525021603958709381fc4dc296cc2586aaa5dd7.tar.bz2
Generalize varobj iterator
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.
Diffstat (limited to 'gdb/python/py-varobj.c')
-rw-r--r--gdb/python/py-varobj.c186
1 files changed, 186 insertions, 0 deletions
diff --git a/gdb/python/py-varobj.c b/gdb/python/py-varobj.c
new file mode 100644
index 0000000..2b95348
--- /dev/null
+++ b/gdb/python/py-varobj.c
@@ -0,0 +1,186 @@
+/* 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;
+}