]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/python/py-varobj.c
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / python / py-varobj.c
CommitLineData
213516ef 1/* Copyright (C) 2013-2023 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"
c4a3dbaf 20#include "valprint.h"
e5250216
YQ
21
22/* A dynamic varobj iterator "class" for python pretty-printed
23 varobjs. This inherits struct varobj_iter. */
24
54746ce3 25struct py_varobj_iter : public varobj_iter
e5250216 26{
c4a3dbaf
TT
27 py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter,
28 const value_print_options *opts);
54746ce3
TT
29 ~py_varobj_iter () override;
30
60ee72f6 31 std::unique_ptr<varobj_item> next () override;
54746ce3
TT
32
33private:
34
35 /* The varobj this iterator is listing children for. */
36 struct varobj *m_var;
37
38 /* The next raw index we will try to check is available. If it is
39 equal to number_of_children, then we've already iterated the
40 whole set. */
41 int m_next_raw_index = 0;
e5250216
YQ
42
43 /* The python iterator returned by the printer's 'children' method,
44 or NULL if not available. */
54746ce3 45 PyObject *m_iter;
c4a3dbaf
TT
46
47 /* The print options to use. */
48 value_print_options m_opts;
e5250216
YQ
49};
50
51/* Implementation of the 'dtor' method of pretty-printed varobj
52 iterators. */
53
54746ce3 54py_varobj_iter::~py_varobj_iter ()
e5250216 55{
54746ce3
TT
56 gdbpy_enter_varobj enter_py (m_var);
57 Py_XDECREF (m_iter);
e5250216
YQ
58}
59
60/* Implementation of the 'next' method of pretty-printed varobj
61 iterators. */
62
60ee72f6 63std::unique_ptr<varobj_item>
54746ce3 64py_varobj_iter::next ()
e5250216 65{
827f100c
YQ
66 PyObject *py_v;
67 varobj_item *vitem;
68 const char *name = NULL;
69
70 if (!gdb_python_initialized)
71 return NULL;
e5250216 72
54746ce3 73 gdbpy_enter_varobj enter_py (m_var);
e5250216 74
c4a3dbaf
TT
75 scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
76 &m_opts);
77
54746ce3 78 gdbpy_ref<> item (PyIter_Next (m_iter));
e5250216
YQ
79
80 if (item == NULL)
81 {
82 /* Normal end of iteration. */
83 if (!PyErr_Occurred ())
84 return NULL;
85
86 /* If we got a memory error, just use the text as the item. */
87 if (PyErr_ExceptionMatches (gdbpy_gdb_memory_error))
88 {
5c329e6a
TT
89 gdbpy_err_fetch fetched_error;
90 gdb::unique_xmalloc_ptr<char> value_str = fetched_error.to_string ();
e5250216
YQ
91 if (value_str == NULL)
92 {
93 gdbpy_print_stack ();
94 return NULL;
95 }
96
7f968c89 97 std::string name_str = string_printf ("<error at %d>",
54746ce3 98 m_next_raw_index++);
7f968c89
TT
99 item.reset (Py_BuildValue ("(ss)", name_str.c_str (),
100 value_str.get ()));
e5250216
YQ
101 if (item == NULL)
102 {
103 gdbpy_print_stack ();
104 return NULL;
105 }
106 }
107 else
108 {
109 /* Any other kind of error. */
110 gdbpy_print_stack ();
111 return NULL;
112 }
113 }
114
788f2586 115 if (!PyArg_ParseTuple (item.get (), "sO", &name, &py_v))
827f100c
YQ
116 {
117 gdbpy_print_stack ();
118 error (_("Invalid item from the child list"));
119 }
120
2f408ecb 121 vitem = new varobj_item ();
11106495 122 vitem->value = release_value (convert_value_from_python (py_v));
827f100c
YQ
123 if (vitem->value == NULL)
124 gdbpy_print_stack ();
2f408ecb 125 vitem->name = name;
827f100c 126
54746ce3 127 m_next_raw_index++;
60ee72f6 128 return std::unique_ptr<varobj_item> (vitem);
e5250216
YQ
129}
130
e5250216
YQ
131/* Constructor of pretty-printed varobj iterators. VAR is the varobj
132 whose children the iterator will be iterating over. PYITER is the
133 python iterator actually responsible for the iteration. */
134
c4a3dbaf
TT
135py_varobj_iter::py_varobj_iter (struct varobj *var, gdbpy_ref<> &&pyiter,
136 const value_print_options *opts)
54746ce3 137 : m_var (var),
c4a3dbaf
TT
138 m_iter (pyiter.release ()),
139 m_opts (*opts)
e5250216 140{
e5250216
YQ
141}
142
143/* Return a new pretty-printed varobj iterator suitable to iterate
144 over VAR's children. */
145
24fd95b4 146std::unique_ptr<varobj_iter>
c4a3dbaf
TT
147py_varobj_get_iterator (struct varobj *var, PyObject *printer,
148 const value_print_options *opts)
e5250216 149{
6cd67bea 150 gdbpy_enter_varobj enter_py (var);
e5250216
YQ
151
152 if (!PyObject_HasAttr (printer, gdbpy_children_cst))
6cd67bea 153 return NULL;
e5250216 154
c4a3dbaf
TT
155 scoped_restore set_options = make_scoped_restore (&gdbpy_current_print_options,
156 opts);
157
7780f186
TT
158 gdbpy_ref<> children (PyObject_CallMethodObjArgs (printer, gdbpy_children_cst,
159 NULL));
e5250216
YQ
160 if (children == NULL)
161 {
162 gdbpy_print_stack ();
163 error (_("Null value returned for children"));
164 }
165
1a338907 166 gdbpy_ref<> iter (PyObject_GetIter (children.get ()));
e5250216
YQ
167 if (iter == NULL)
168 {
169 gdbpy_print_stack ();
170 error (_("Could not get children iterator"));
171 }
172
24fd95b4 173 return std::unique_ptr<varobj_iter> (new py_varobj_iter (var,
c4a3dbaf
TT
174 std::move (iter),
175 opts));
e5250216 176}