Commit | Line | Data |
---|---|---|
32d0add0 | 1 | /* Copyright (C) 2013-2015 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" | |
20 | ||
21 | /* A dynamic varobj iterator "class" for python pretty-printed | |
22 | varobjs. This inherits struct varobj_iter. */ | |
23 | ||
24 | struct py_varobj_iter | |
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 | |
38 | py_varobj_iter_dtor (struct varobj_iter *self) | |
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 * | |
52 | py_varobj_iter_next (struct varobj_iter *self) | |
53 | { | |
54 | struct py_varobj_iter *t = (struct py_varobj_iter *) self; | |
55 | struct cleanup *back_to; | |
56 | PyObject *item; | |
827f100c YQ |
57 | PyObject *py_v; |
58 | varobj_item *vitem; | |
59 | const char *name = NULL; | |
60 | ||
61 | if (!gdb_python_initialized) | |
62 | return NULL; | |
e5250216 YQ |
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 | { | |
87 | gdbpy_print_stack (); | |
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 | { | |
98 | gdbpy_print_stack (); | |
99 | return NULL; | |
100 | } | |
101 | } | |
102 | else | |
103 | { | |
104 | /* Any other kind of error. */ | |
105 | gdbpy_print_stack (); | |
106 | return NULL; | |
107 | } | |
108 | } | |
109 | ||
827f100c YQ |
110 | if (!PyArg_ParseTuple (item, "sO", &name, &py_v)) |
111 | { | |
112 | gdbpy_print_stack (); | |
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) | |
119 | gdbpy_print_stack (); | |
120 | vitem->name = xstrdup (name); | |
121 | ||
e5250216 YQ |
122 | self->next_raw_index++; |
123 | do_cleanups (back_to); | |
827f100c | 124 | return vitem; |
e5250216 YQ |
125 | } |
126 | ||
127 | /* The 'vtable' of pretty-printed python varobj iterators. */ | |
128 | ||
129 | static const struct varobj_iter_ops py_varobj_iter_ops = | |
130 | { | |
131 | py_varobj_iter_dtor, | |
132 | py_varobj_iter_next | |
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 | ||
139 | static void CPYCHECKER_STEALS_REFERENCE_TO_ARG (3) | |
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 | ||
154 | static struct py_varobj_iter * CPYCHECKER_STEALS_REFERENCE_TO_ARG (2) | |
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 | { | |
186 | gdbpy_print_stack (); | |
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 | { | |
195 | gdbpy_print_stack (); | |
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 | } |