PR python/13598 - add before_prompt event
[deliverable/binutils-gdb.git] / gdb / python / py-varobj.c
CommitLineData
61baf725 1/* Copyright (C) 2013-2017 Free Software Foundation, Inc.
e5250216
YQ
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"
6cd67bea 20#include "py-ref.h"
e5250216
YQ
21
22/* A dynamic varobj iterator "class" for python pretty-printed
23 varobjs. This inherits struct varobj_iter. */
24
25struct 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
38static void
39py_varobj_iter_dtor (struct varobj_iter *self)
40{
41 struct py_varobj_iter *dis = (struct py_varobj_iter *) self;
788f2586 42 gdbpy_enter_varobj enter_py (self->var);
e5250216 43 Py_XDECREF (dis->iter);
e5250216
YQ
44}
45
46/* Implementation of the 'next' method of pretty-printed varobj
47 iterators. */
48
49static varobj_item *
50py_varobj_iter_next (struct varobj_iter *self)
51{
52 struct py_varobj_iter *t = (struct py_varobj_iter *) self;
827f100c
YQ
53 PyObject *py_v;
54 varobj_item *vitem;
55 const char *name = NULL;
56
57 if (!gdb_python_initialized)
58 return NULL;
e5250216 59
788f2586 60 gdbpy_enter_varobj enter_py (self->var);
e5250216 61
7780f186 62 gdbpy_ref<> item (PyIter_Next (t->iter));
e5250216
YQ
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;
9b972014 74 char *name_str;
e5250216
YQ
75
76 PyErr_Fetch (&type, &value, &trace);
9b972014
TT
77 gdb::unique_xmalloc_ptr<char>
78 value_str (gdbpy_exception_to_string (type, value));
e5250216
YQ
79 Py_XDECREF (type);
80 Py_XDECREF (value);
81 Py_XDECREF (trace);
82 if (value_str == NULL)
83 {
84 gdbpy_print_stack ();
85 return NULL;
86 }
87
88 name_str = xstrprintf ("<error at %d>",
89 self->next_raw_index++);
788f2586 90 item.reset (Py_BuildValue ("(ss)", name_str, value_str.get ()));
e5250216 91 xfree (name_str);
e5250216
YQ
92 if (item == NULL)
93 {
94 gdbpy_print_stack ();
95 return NULL;
96 }
97 }
98 else
99 {
100 /* Any other kind of error. */
101 gdbpy_print_stack ();
102 return NULL;
103 }
104 }
105
788f2586 106 if (!PyArg_ParseTuple (item.get (), "sO", &name, &py_v))
827f100c
YQ
107 {
108 gdbpy_print_stack ();
109 error (_("Invalid item from the child list"));
110 }
111
2f408ecb 112 vitem = new varobj_item ();
827f100c
YQ
113 vitem->value = convert_value_from_python (py_v);
114 if (vitem->value == NULL)
115 gdbpy_print_stack ();
2f408ecb 116 vitem->name = name;
827f100c 117
e5250216 118 self->next_raw_index++;
827f100c 119 return vitem;
e5250216
YQ
120}
121
122/* The 'vtable' of pretty-printed python varobj iterators. */
123
124static const struct varobj_iter_ops py_varobj_iter_ops =
125{
126 py_varobj_iter_dtor,
127 py_varobj_iter_next
128};
129
130/* Constructor of pretty-printed varobj iterators. VAR is the varobj
131 whose children the iterator will be iterating over. PYITER is the
132 python iterator actually responsible for the iteration. */
133
134static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3)
135py_varobj_iter_ctor (struct py_varobj_iter *self,
136 struct varobj *var, PyObject *pyiter)
137{
138 self->base.var = var;
139 self->base.ops = &py_varobj_iter_ops;
140 self->base.next_raw_index = 0;
141 self->iter = pyiter;
142}
143
144/* Allocate and construct a pretty-printed varobj iterator. VAR is
145 the varobj whose children the iterator will be iterating over.
146 PYITER is the python iterator actually responsible for the
147 iteration. */
148
149static struct py_varobj_iter * CPYCHECKER_STEALS_REFERENCE_TO_ARG (2)
150py_varobj_iter_new (struct varobj *var, PyObject *pyiter)
151{
152 struct py_varobj_iter *self;
153
154 self = XNEW (struct py_varobj_iter);
155 py_varobj_iter_ctor (self, var, pyiter);
156 return self;
157}
158
159/* Return a new pretty-printed varobj iterator suitable to iterate
160 over VAR's children. */
161
162struct varobj_iter *
163py_varobj_get_iterator (struct varobj *var, PyObject *printer)
164{
e5250216
YQ
165 PyObject *iter;
166 struct py_varobj_iter *py_iter;
6cd67bea
TT
167
168 gdbpy_enter_varobj enter_py (var);
e5250216
YQ
169
170 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
6cd67bea 171 return NULL;
e5250216 172
7780f186
TT
173 gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
174 NULL));
e5250216
YQ
175 if (children == NULL)
176 {
177 gdbpy_print_stack ();
178 error (_("Null value returned for children"));
179 }
180
6cd67bea 181 iter = PyObject_GetIter (children.get ());
e5250216
YQ
182 if (iter == NULL)
183 {
184 gdbpy_print_stack ();
185 error (_("Could not get children iterator"));
186 }
187
188 py_iter = py_varobj_iter_new (var, iter);
189
e5250216
YQ
190 return &py_iter->base;
191}
This page took 0.269255 seconds and 4 git commands to generate.