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