Introduce gdbpy_enter_varobj and use it
[deliverable/binutils-gdb.git] / gdb / python / py-varobj.c
1 /* Copyright (C) 2013-2017 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
25 struct py_varobj_iter
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
39 py_varobj_iter_dtor (struct varobj_iter *self)
40 {
41 struct py_varobj_iter *dis = (struct py_varobj_iter *) self;
42 struct cleanup *back_to = varobj_ensure_python_env (self->var);
43
44 Py_XDECREF (dis->iter);
45
46 do_cleanups (back_to);
47 }
48
49 /* Implementation of the 'next' method of pretty-printed varobj
50 iterators. */
51
52 static varobj_item *
53 py_varobj_iter_next (struct varobj_iter *self)
54 {
55 struct py_varobj_iter *t = (struct py_varobj_iter *) self;
56 struct cleanup *back_to;
57 PyObject *item;
58 PyObject *py_v;
59 varobj_item *vitem;
60 const char *name = NULL;
61
62 if (!gdb_python_initialized)
63 return NULL;
64
65 back_to = varobj_ensure_python_env (self->var);
66
67 item = PyIter_Next (t->iter);
68
69 if (item == NULL)
70 {
71 /* Normal end of iteration. */
72 if (!PyErr_Occurred ())
73 return NULL;
74
75 /* If we got a memory error, just use the text as the item. */
76 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
77 {
78 PyObject *type, *value, *trace;
79 char *name_str;
80
81 PyErr_Fetch (&type, &value, &trace);
82 gdb::unique_xmalloc_ptr<char>
83 value_str (gdbpy_exception_to_string (type, value));
84 Py_XDECREF (type);
85 Py_XDECREF (value);
86 Py_XDECREF (trace);
87 if (value_str == NULL)
88 {
89 gdbpy_print_stack ();
90 return NULL;
91 }
92
93 name_str = xstrprintf ("<error at %d>",
94 self->next_raw_index++);
95 item = Py_BuildValue ("(ss)", name_str, value_str.get ());
96 xfree (name_str);
97 if (item == NULL)
98 {
99 gdbpy_print_stack ();
100 return NULL;
101 }
102 }
103 else
104 {
105 /* Any other kind of error. */
106 gdbpy_print_stack ();
107 return NULL;
108 }
109 }
110
111 if (!PyArg_ParseTuple (item, "sO", &name, &py_v))
112 {
113 gdbpy_print_stack ();
114 error (_("Invalid item from the child list"));
115 }
116
117 vitem = new varobj_item ();
118 vitem->value = convert_value_from_python (py_v);
119 if (vitem->value == NULL)
120 gdbpy_print_stack ();
121 vitem->name = name;
122
123 self->next_raw_index++;
124 do_cleanups (back_to);
125 return vitem;
126 }
127
128 /* The 'vtable' of pretty-printed python varobj iterators. */
129
130 static const struct varobj_iter_ops py_varobj_iter_ops =
131 {
132 py_varobj_iter_dtor,
133 py_varobj_iter_next
134 };
135
136 /* Constructor of pretty-printed varobj iterators. VAR is the varobj
137 whose children the iterator will be iterating over. PYITER is the
138 python iterator actually responsible for the iteration. */
139
140 static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3)
141 py_varobj_iter_ctor (struct py_varobj_iter *self,
142 struct varobj *var, PyObject *pyiter)
143 {
144 self->base.var = var;
145 self->base.ops = &py_varobj_iter_ops;
146 self->base.next_raw_index = 0;
147 self->iter = pyiter;
148 }
149
150 /* Allocate and construct a pretty-printed varobj iterator. VAR is
151 the varobj whose children the iterator will be iterating over.
152 PYITER is the python iterator actually responsible for the
153 iteration. */
154
155 static struct py_varobj_iter * CPYCHECKER_STEALS_REFERENCE_TO_ARG (2)
156 py_varobj_iter_new (struct varobj *var, PyObject *pyiter)
157 {
158 struct py_varobj_iter *self;
159
160 self = XNEW (struct py_varobj_iter);
161 py_varobj_iter_ctor (self, var, pyiter);
162 return self;
163 }
164
165 /* Return a new pretty-printed varobj iterator suitable to iterate
166 over VAR's children. */
167
168 struct varobj_iter *
169 py_varobj_get_iterator (struct varobj *var, PyObject *printer)
170 {
171 PyObject *iter;
172 struct py_varobj_iter *py_iter;
173
174 gdbpy_enter_varobj enter_py (var);
175
176 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
177 return NULL;
178
179 gdbpy_ref children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
180 NULL));
181 if (children == NULL)
182 {
183 gdbpy_print_stack ();
184 error (_("Null value returned for children"));
185 }
186
187 iter = PyObject_GetIter (children.get ());
188 if (iter == NULL)
189 {
190 gdbpy_print_stack ();
191 error (_("Could not get children iterator"));
192 }
193
194 py_iter = py_varobj_iter_new (var, iter);
195
196 return &py_iter->base;
197 }
This page took 0.033566 seconds and 4 git commands to generate.