gdb/python/py-varobj.c - gdb

Global variables defined

Data types defined

Functions defined

Source code

  1. /* Copyright (C) 2013-2015 Free Software Foundation, Inc.

  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 3 of the License, or
  5.    (at your option) any later version.

  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.

  10.    You should have received a copy of the GNU General Public License
  11.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  12. #include "defs.h"
  13. #include "python-internal.h"
  14. #include "varobj.h"
  15. #include "varobj-iter.h"

  16. /* A dynamic varobj iterator "class" for python pretty-printed
  17.    varobjs.  This inherits struct varobj_iter.  */

  18. struct py_varobj_iter
  19. {
  20.   /* The 'base class'.  */
  21.   struct varobj_iter base;

  22.   /* The python iterator returned by the printer's 'children' method,
  23.      or NULL if not available.  */
  24.   PyObject *iter;
  25. };

  26. /* Implementation of the 'dtor' method of pretty-printed varobj
  27.    iterators.  */

  28. static void
  29. py_varobj_iter_dtor (struct varobj_iter *self)
  30. {
  31.   struct py_varobj_iter *dis = (struct py_varobj_iter *) self;
  32.   struct cleanup *back_to = varobj_ensure_python_env (self->var);

  33.   Py_XDECREF (dis->iter);

  34.   do_cleanups (back_to);
  35. }

  36. /* Implementation of the 'next' method of pretty-printed varobj
  37.    iterators.  */

  38. static varobj_item *
  39. py_varobj_iter_next (struct varobj_iter *self)
  40. {
  41.   struct py_varobj_iter *t = (struct py_varobj_iter *) self;
  42.   struct cleanup *back_to;
  43.   PyObject *item;
  44.   PyObject *py_v;
  45.   varobj_item *vitem;
  46.   const char *name = NULL;

  47.   if (!gdb_python_initialized)
  48.     return NULL;

  49.   back_to = varobj_ensure_python_env (self->var);

  50.   item = PyIter_Next (t->iter);

  51.   if (item == NULL)
  52.     {
  53.       /* Normal end of iteration.  */
  54.       if (!PyErr_Occurred ())
  55.         return NULL;

  56.       /* If we got a memory error, just use the text as the item.  */
  57.       if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
  58.         {
  59.           PyObject *type, *value, *trace;
  60.           char *name_str, *value_str;

  61.           PyErr_Fetch (&type, &value, &trace);
  62.           value_str = gdbpy_exception_to_string (type, value);
  63.           Py_XDECREF (type);
  64.           Py_XDECREF (value);
  65.           Py_XDECREF (trace);
  66.           if (value_str == NULL)
  67.             {
  68.               gdbpy_print_stack ();
  69.               return NULL;
  70.             }

  71.           name_str = xstrprintf ("<error at %d>",
  72.                                  self->next_raw_index++);
  73.           item = Py_BuildValue ("(ss)", name_str, value_str);
  74.           xfree (name_str);
  75.           xfree (value_str);
  76.           if (item == NULL)
  77.             {
  78.               gdbpy_print_stack ();
  79.               return NULL;
  80.             }
  81.         }
  82.       else
  83.         {
  84.           /* Any other kind of error.  */
  85.           gdbpy_print_stack ();
  86.           return NULL;
  87.         }
  88.     }

  89.   if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
  90.     {
  91.       gdbpy_print_stack ();
  92.       error (_("Invalid item from the child list"));
  93.     }

  94.   vitem = xmalloc (sizeof *vitem);
  95.   vitem->value = convert_value_from_python (py_v);
  96.   if (vitem->value == NULL)
  97.     gdbpy_print_stack ();
  98.   vitem->name = xstrdup (name);

  99.   self->next_raw_index++;
  100.   do_cleanups (back_to);
  101.   return vitem;
  102. }

  103. /* The 'vtable' of pretty-printed python varobj iterators.  */

  104. static const struct varobj_iter_ops py_varobj_iter_ops =
  105. {
  106.   py_varobj_iter_dtor,
  107.   py_varobj_iter_next
  108. };

  109. /* Constructor of pretty-printed varobj iterators.  VAR is the varobj
  110.    whose children the iterator will be iterating over.  PYITER is the
  111.    python iterator actually responsible for the iteration.  */

  112. static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3)
  113. py_varobj_iter_ctor (struct py_varobj_iter *self,
  114.                       struct varobj *var, PyObject *pyiter)
  115. {
  116.   self->base.var = var;
  117.   self->base.ops = &py_varobj_iter_ops;
  118.   self->base.next_raw_index = 0;
  119.   self->iter = pyiter;
  120. }

  121. /* Allocate and construct a pretty-printed varobj iterator.  VAR is
  122.    the varobj whose children the iterator will be iterating over.
  123.    PYITER is the python iterator actually responsible for the
  124.    iteration.  */

  125. static struct py_varobj_iter * CPYCHECKER_STEALS_REFERENCE_TO_ARG (2)
  126. py_varobj_iter_new (struct varobj *var, PyObject *pyiter)
  127. {
  128.   struct py_varobj_iter *self;

  129.   self = XNEW (struct py_varobj_iter);
  130.   py_varobj_iter_ctor (self, var, pyiter);
  131.   return self;
  132. }

  133. /* Return a new pretty-printed varobj iterator suitable to iterate
  134.    over VAR's children.  */

  135. struct varobj_iter *
  136. py_varobj_get_iterator (struct varobj *var, PyObject *printer)
  137. {
  138.   PyObject *children;
  139.   int i;
  140.   PyObject *iter;
  141.   struct py_varobj_iter *py_iter;
  142.   struct cleanup *back_to = varobj_ensure_python_env (var);

  143.   if (!PyObject_HasAttr (printer, gdbpy_children_cst))
  144.     {
  145.       do_cleanups (back_to);
  146.       return NULL;
  147.     }

  148.   children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
  149.                                          NULL);
  150.   if (children == NULL)
  151.     {
  152.       gdbpy_print_stack ();
  153.       error (_("Null value returned for children"));
  154.     }

  155.   make_cleanup_py_decref (children);

  156.   iter = PyObject_GetIter (children);
  157.   if (iter == NULL)
  158.     {
  159.       gdbpy_print_stack ();
  160.       error (_("Could not get children iterator"));
  161.     }

  162.   py_iter = py_varobj_iter_new (var, iter);

  163.   do_cleanups (back_to);

  164.   return &py_iter->base;
  165. }