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