GDB (xrefs)
py-varobj.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2015 Free Software Foundation, Inc.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; either version 3 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>. */
15 
16 #include "defs.h"
17 #include "python-internal.h"
18 #include "varobj.h"
19 #include "varobj-iter.h"
20 
21 /* A dynamic varobj iterator "class" for python pretty-printed
22  varobjs. This inherits struct varobj_iter. */
23 
25 {
26  /* The 'base class'. */
27  struct varobj_iter base;
28 
29  /* The python iterator returned by the printer's 'children' method,
30  or NULL if not available. */
31  PyObject *iter;
32 };
33 
34 /* Implementation of the 'dtor' method of pretty-printed varobj
35  iterators. */
36 
37 static void
39 {
40  struct py_varobj_iter *dis = (struct py_varobj_iter *) self;
41  struct cleanup *back_to = varobj_ensure_python_env (self->var);
42 
43  Py_XDECREF (dis->iter);
44 
45  do_cleanups (back_to);
46 }
47 
48 /* Implementation of the 'next' method of pretty-printed varobj
49  iterators. */
50 
51 static varobj_item *
53 {
54  struct py_varobj_iter *t = (struct py_varobj_iter *) self;
55  struct cleanup *back_to;
56  PyObject *item;
57  PyObject *py_v;
58  varobj_item *vitem;
59  const char *name = NULL;
60 
62  return NULL;
63 
64  back_to = varobj_ensure_python_env (self->var);
65 
66  item = PyIter_Next (t->iter);
67 
68  if (item == NULL)
69  {
70  /* Normal end of iteration. */
71  if (!PyErr_Occurred ())
72  return NULL;
73 
74  /* If we got a memory error, just use the text as the item. */
75  if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
76  {
77  PyObject *type, *value, *trace;
78  char *name_str, *value_str;
79 
80  PyErr_Fetch (&type, &value, &trace);
81  value_str = gdbpy_exception_to_string (type, value);
82  Py_XDECREF (type);
83  Py_XDECREF (value);
84  Py_XDECREF (trace);
85  if (value_str == NULL)
86  {
88  return NULL;
89  }
90 
91  name_str = xstrprintf ("<error at %d>",
92  self->next_raw_index++);
93  item = Py_BuildValue ("(ss)", name_str, value_str);
94  xfree (name_str);
95  xfree (value_str);
96  if (item == NULL)
97  {
99  return NULL;
100  }
101  }
102  else
103  {
104  /* Any other kind of error. */
106  return NULL;
107  }
108  }
109 
110  if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
111  {
113  error (_("Invalid item from the child list"));
114  }
115 
116  vitem = xmalloc (sizeof *vitem);
117  vitem->value = convert_value_from_python (py_v);
118  if (vitem->value == NULL)
120  vitem->name = xstrdup (name);
121 
122  self->next_raw_index++;
123  do_cleanups (back_to);
124  return vitem;
125 }
126 
127 /* The 'vtable' of pretty-printed python varobj iterators. */
128 
129 static const struct varobj_iter_ops py_varobj_iter_ops =
130 {
133 };
134 
135 /* Constructor of pretty-printed varobj iterators. VAR is the varobj
136  whose children the iterator will be iterating over. PYITER is the
137  python iterator actually responsible for the iteration. */
138 
140 py_varobj_iter_ctor (struct py_varobj_iter *self,
141  struct varobj *var, PyObject *pyiter)
142 {
143  self->base.var = var;
144  self->base.ops = &py_varobj_iter_ops;
145  self->base.next_raw_index = 0;
146  self->iter = pyiter;
147 }
148 
149 /* Allocate and construct a pretty-printed varobj iterator. VAR is
150  the varobj whose children the iterator will be iterating over.
151  PYITER is the python iterator actually responsible for the
152  iteration. */
153 
155 py_varobj_iter_new (struct varobj *var, PyObject *pyiter)
156 {
157  struct py_varobj_iter *self;
158 
159  self = XNEW (struct py_varobj_iter);
160  py_varobj_iter_ctor (self, var, pyiter);
161  return self;
162 }
163 
164 /* Return a new pretty-printed varobj iterator suitable to iterate
165  over VAR's children. */
166 
167 struct varobj_iter *
168 py_varobj_get_iterator (struct varobj *var, PyObject *printer)
169 {
170  PyObject *children;
171  int i;
172  PyObject *iter;
173  struct py_varobj_iter *py_iter;
174  struct cleanup *back_to = varobj_ensure_python_env (var);
175 
176  if (!PyObject_HasAttr (printer, gdbpy_children_cst))
177  {
178  do_cleanups (back_to);
179  return NULL;
180  }
181 
182  children = PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
183  NULL);
184  if (children == NULL)
185  {
187  error (_("Null value returned for children"));
188  }
189 
190  make_cleanup_py_decref (children);
191 
192  iter = PyObject_GetIter (children);
193  if (iter == NULL)
194  {
196  error (_("Could not get children iterator"));
197  }
198 
199  py_iter = py_varobj_iter_new (var, iter);
200 
201  do_cleanups (back_to);
202 
203  return &py_iter->base;
204 }
Definition: varobj.h:89
void xfree(void *)
Definition: common-utils.c:97
#define _(String)
Definition: gdb_locale.h:40
const char *const name
Definition: aarch64-tdep.c:68
static const struct varobj_iter_ops py_varobj_iter_ops
Definition: py-varobj.c:129
struct varobj_iter * py_varobj_get_iterator(struct varobj *var, PyObject *printer)
Definition: py-varobj.c:168
struct varobj_iter base
Definition: py-varobj.c:27
char * name
Definition: varobj-iter.h:22
static const char * type
Definition: language.c:103
static void CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
Definition: py-varobj.c:139
int gdb_python_initialized
Definition: python.c:104
char * xstrprintf(const char *format,...)
Definition: common-utils.c:107
void * xmalloc(YYSIZE_T)
Definition: value.c:172
struct value * value
Definition: varobj-iter.h:25
PyObject * gdbpy_children_cst
Definition: python.c:117
void gdbpy_print_stack(void)
Definition: python.c:1199
struct value * convert_value_from_python(PyObject *obj)
Definition: py-value.c:1559
struct varobj * var
Definition: varobj-iter.h:38
PyObject * gdbpy_gdb_memory_error
Definition: python.c:130
PyObject * iter
Definition: py-varobj.c:31
struct cleanup * varobj_ensure_python_env(const struct varobj *var)
Definition: varobj.c:242
static varobj_item * py_varobj_iter_next(struct varobj_iter *self)
Definition: py-varobj.c:52
struct cleanup * make_cleanup_py_decref(PyObject *py)
Definition: py-utils.c:41
static void py_varobj_iter_dtor(struct varobj_iter *self)
Definition: py-varobj.c:38
void error(const char *fmt,...)
Definition: errors.c:38
char * gdbpy_exception_to_string(PyObject *ptype, PyObject *pvalue)
Definition: py-utils.c:268
void do_cleanups(struct cleanup *old_chain)
Definition: cleanups.c:175