GDB (xrefs)
py-varobj.c
Go to the documentation of this file.
1 /* Copyright (C) 2013-2018 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 #include "py-ref.h"
21 
22 /* A dynamic varobj iterator "class" for python pretty-printed
23  varobjs. This inherits struct varobj_iter. */
24 
26 {
27  /* The 'base class'. */
28  struct varobj_iter base;
29 
30  /* The python iterator returned by the printer's 'children' method,
31  or NULL if not available. */
32  PyObject *iter;
33 };
34 
35 /* Implementation of the 'dtor' method of pretty-printed varobj
36  iterators. */
37 
38 static void
40 {
41  struct py_varobj_iter *dis = (struct py_varobj_iter *) self;
42  gdbpy_enter_varobj enter_py (self->var);
43  Py_XDECREF (dis->iter);
44 }
45 
46 /* Implementation of the 'next' method of pretty-printed varobj
47  iterators. */
48 
49 static varobj_item *
51 {
52  struct py_varobj_iter *t = (struct py_varobj_iter *) self;
53  PyObject *py_v;
54  varobj_item *vitem;
55  const char *name = NULL;
56 
58  return NULL;
59 
60  gdbpy_enter_varobj enter_py (self->var);
61 
62  gdbpy_ref<> item (PyIter_Next (t->iter));
63 
64  if (item == NULL)
65  {
66  /* Normal end of iteration. */
67  if (!PyErr_Occurred ())
68  return NULL;
69 
70  /* If we got a memory error, just use the text as the item. */
71  if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
72  {
73  PyObject *type, *value, *trace;
74 
75  PyErr_Fetch (&type, &value, &trace);
77  value_str (gdbpy_exception_to_string (type, value));
78  Py_XDECREF (type);
79  Py_XDECREF (value);
80  Py_XDECREF (trace);
81  if (value_str == NULL)
82  {
84  return NULL;
85  }
86 
87  std::string name_str = string_printf ("<error at %d>",
88  self->next_raw_index++);
89  item.reset (Py_BuildValue ("(ss)", name_str.c_str (),
90  value_str.get ()));
91  if (item == NULL)
92  {
94  return NULL;
95  }
96  }
97  else
98  {
99  /* Any other kind of error. */
101  return NULL;
102  }
103  }
104 
105  if (!PyArg_ParseTuple (item.get (), "sO", &name, &py_v))
106  {
108  error (_("Invalid item from the child list"));
109  }
110 
111  vitem = new varobj_item ();
112  vitem->value = convert_value_from_python (py_v);
113  if (vitem->value == NULL)
115  vitem->name = name;
116 
117  self->next_raw_index++;
118  return vitem;
119 }
120 
121 /* The 'vtable' of pretty-printed python varobj iterators. */
122 
123 static const struct varobj_iter_ops py_varobj_iter_ops =
124 {
127 };
128 
129 /* Constructor of pretty-printed varobj iterators. VAR is the varobj
130  whose children the iterator will be iterating over. PYITER is the
131  python iterator actually responsible for the iteration. */
132 
134 py_varobj_iter_ctor (struct py_varobj_iter *self,
135  struct varobj *var, PyObject *pyiter)
136 {
137  self->base.var = var;
138  self->base.ops = &py_varobj_iter_ops;
139  self->base.next_raw_index = 0;
140  self->iter = pyiter;
141 }
142 
143 /* Allocate and construct a pretty-printed varobj iterator. VAR is
144  the varobj whose children the iterator will be iterating over.
145  PYITER is the python iterator actually responsible for the
146  iteration. */
147 
149 py_varobj_iter_new (struct varobj *var, PyObject *pyiter)
150 {
151  struct py_varobj_iter *self;
152 
153  self = XNEW (struct py_varobj_iter);
154  py_varobj_iter_ctor (self, var, pyiter);
155  return self;
156 }
157 
158 /* Return a new pretty-printed varobj iterator suitable to iterate
159  over VAR's children. */
160 
161 struct varobj_iter *
162 py_varobj_get_iterator (struct varobj *var, PyObject *printer)
163 {
164  PyObject *iter;
165  struct py_varobj_iter *py_iter;
166 
167  gdbpy_enter_varobj enter_py (var);
168 
169  if (!PyObject_HasAttr (printer, gdbpy_children_cst))
170  return NULL;
171 
172  gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
173  NULL));
174  if (children == NULL)
175  {
177  error (_("Null value returned for children"));
178  }
179 
180  iter = PyObject_GetIter (children.get ());
181  if (iter == NULL)
182  {
184  error (_("Could not get children iterator"));
185  }
186 
187  py_iter = py_varobj_iter_new (var, iter);
188 
189  return &py_iter->base;
190 }
const char * string
Definition: signals.c:50
Definition: varobj.h:94
#define _(String)
Definition: gdb_locale.h:35
struct varobj_item varobj_item
#define XNEW(T)
Definition: poison.h:109
void reset(T *obj)
Definition: gdb_ref_ptr.h:121
const char *const name
Definition: aarch64-tdep.c:76
static const struct varobj_iter_ops py_varobj_iter_ops
Definition: py-varobj.c:123
std::unique_ptr< T, xfree_deleter< T > > unique_xmalloc_ptr
struct varobj_iter * py_varobj_get_iterator(struct varobj *var, PyObject *printer)
Definition: py-varobj.c:162
struct varobj_iter base
Definition: py-varobj.c:28
std::string name
Definition: varobj-iter.h:22
Definition: gdbtypes.h:749
static const char * type
Definition: language.c:113
static void CPYCHECKER_STEALS_REFERENCE_TO_ARG(3)
Definition: py-varobj.c:133
int gdb_python_initialized
Definition: python.c:108
Definition: value.c:169
struct value * value
Definition: varobj-iter.h:25
PyObject * gdbpy_children_cst
Definition: python.c:121
void gdbpy_print_stack(void)
Definition: python.c:1262
struct value * convert_value_from_python(PyObject *obj)
Definition: py-value.c:1615
struct varobj * var
Definition: varobj-iter.h:38
T * get() const
Definition: gdb_ref_ptr.h:130
PyObject * gdbpy_gdb_memory_error
Definition: python.c:134
std::string string_printf(const char *fmt,...)
Definition: common-utils.c:150
gdb::unique_xmalloc_ptr< char > gdbpy_exception_to_string(PyObject *ptype, PyObject *pvalue)
Definition: py-utils.c:212
PyObject * iter
Definition: py-varobj.c:32
static varobj_item * py_varobj_iter_next(struct varobj_iter *self)
Definition: py-varobj.c:50
static void py_varobj_iter_dtor(struct varobj_iter *self)
Definition: py-varobj.c:39
void error(const char *fmt,...)
Definition: errors.c:38