GDB (xrefs)
Loading...
Searching...
No Matches
py-mi.c
Go to the documentation of this file.
1/* Python interface to MI commands
2
3 Copyright (C) 2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20#include "defs.h"
21#include "python-internal.h"
22#include "ui-out.h"
23#include "mi/mi-parse.h"
24
25/* A ui_out subclass that creates a Python object based on the data
26 that is passed in. */
27
28class py_ui_out : public ui_out
29{
30public:
31
38
39 bool can_emit_style_escape () const override
40 { return false; }
41
42 bool do_is_mi_like_p () const override
43 { return true; }
44
45 /* Return the Python object that was created. If a Python error
46 occurred during the processing, set the Python error and return
47 nullptr. */
49 {
50 if (m_error.has_value ())
51 {
52 m_error->restore ();
53 return nullptr;
54 }
55 return current ().obj.release ();
56 }
57
58protected:
59
60 void do_progress_end () override { }
61 void do_progress_start () override { }
62 void do_progress_notify (const std::string &, const char *, double, double)
63 override
64 { }
65
66 void do_table_begin (int nbrofcols, int nr_rows, const char *tblid) override
67 {
69 }
70 void do_table_body () override
71 { }
72 void do_table_end () override
73 {
75 }
76 void do_table_header (int width, ui_align align,
77 const std::string &col_name,
78 const std::string &col_hdr) override
79 { }
80
81 void do_begin (ui_out_type type, const char *id) override;
82 void do_end (ui_out_type type) override;
83
84 void do_field_signed (int fldno, int width, ui_align align,
85 const char *fldname, LONGEST value) override;
86 void do_field_unsigned (int fldno, int width, ui_align align,
87 const char *fldname, ULONGEST value) override;
88
89 void do_field_skip (int fldno, int width, ui_align align,
90 const char *fldname) override
91 { }
92
93 void do_field_string (int fldno, int width, ui_align align,
94 const char *fldname, const char *string,
95 const ui_file_style &style) override;
96 void do_field_fmt (int fldno, int width, ui_align align,
97 const char *fldname, const ui_file_style &style,
98 const char *format, va_list args) override
99 ATTRIBUTE_PRINTF (7, 0);
100
101 void do_spaces (int numspaces) override
102 { }
103
104 void do_text (const char *string) override
105 { }
106
107 void do_message (const ui_file_style &style,
108 const char *format, va_list args)
109 override ATTRIBUTE_PRINTF (3,0)
110 { }
111
112 void do_wrap_hint (int indent) override
113 { }
114
115 void do_flush () override
116 { }
117
118 void do_redirect (struct ui_file *outstream) override
119 { }
120
121private:
122
123 /* When constructing Python objects, this class keeps a stack of
124 objects being constructed. Each such object has this type. */
125 struct object_desc
126 {
127 /* Name of the field (or empty for lists) that this object will
128 eventually become. */
129 std::string field_name;
130 /* The object under construction. */
131 gdbpy_ref<> obj;
132 /* The type of structure being created. Note that tables are
133 treated as lists here. */
135 };
136
137 /* The stack of objects being created. */
138 std::vector<object_desc> m_objects;
139
140 /* If an error occurred, this holds the exception information for
141 use by the 'release' method. */
142 gdb::optional<gdbpy_err_fetch> m_error;
143
144 /* Return a reference to the object under construction. */
145 object_desc &current ()
146 { return m_objects.back (); }
147
148 /* Add a new field to the current object under construction. */
149 void add_field (const char *name, const gdbpy_ref<> &obj);
150};
151
152void
153py_ui_out::add_field (const char *name, const gdbpy_ref<> &obj)
154{
155 if (obj == nullptr)
156 {
157 m_error.emplace ();
158 return;
159 }
160
161 object_desc &desc = current ();
162 if (desc.type == ui_out_type_list)
163 {
164 if (PyList_Append (desc.obj.get (), obj.get ()) < 0)
165 m_error.emplace ();
166 }
167 else
168 {
169 if (PyDict_SetItemString (desc.obj.get (), name, obj.get ()) < 0)
170 m_error.emplace ();
171 }
172}
173
174void
176{
177 if (m_error.has_value ())
178 return;
179
181 ? PyList_New (0)
182 : PyDict_New ());
183 if (new_obj == nullptr)
184 {
185 m_error.emplace ();
186 return;
187 }
188
189 object_desc new_desc;
190 if (id != nullptr)
191 new_desc.field_name = id;
192 new_desc.obj = std::move (new_obj);
193 new_desc.type = type;
194
195 m_objects.push_back (std::move (new_desc));
196}
197
198void
200{
201 if (m_error.has_value ())
202 return;
203
204 object_desc new_obj = std::move (current ());
205 m_objects.pop_back ();
206 add_field (new_obj.field_name.c_str (), new_obj.obj);
207}
208
209void
210py_ui_out::do_field_signed (int fldno, int width, ui_align align,
211 const char *fldname, LONGEST value)
212{
213 if (m_error.has_value ())
214 return;
215
217 add_field (fldname, val);
218}
219
220void
221py_ui_out::do_field_unsigned (int fldno, int width, ui_align align,
222 const char *fldname, ULONGEST value)
223{
224 if (m_error.has_value ())
225 return;
226
228 add_field (fldname, val);
229}
230
231void
232py_ui_out::do_field_string (int fldno, int width, ui_align align,
233 const char *fldname, const char *string,
234 const ui_file_style &style)
235{
236 if (m_error.has_value ())
237 return;
238
240 add_field (fldname, val);
241}
242
243void
244py_ui_out::do_field_fmt (int fldno, int width, ui_align align,
245 const char *fldname, const ui_file_style &style,
246 const char *format, va_list args)
247{
248 if (m_error.has_value ())
249 return;
250
251 std::string str = string_vprintf (format, args);
252 do_field_string (fldno, width, align, fldname, str.c_str (), style);
253}
254
255/* Implementation of the gdb.execute_mi command. */
256
257PyObject *
259{
260 gdb::unique_xmalloc_ptr<char> mi_command;
261 std::vector<gdb::unique_xmalloc_ptr<char>> arg_strings;
262
263 Py_ssize_t n_args = PyTuple_Size (args);
264 if (n_args < 0)
265 return nullptr;
266
267 for (Py_ssize_t i = 0; i < n_args; ++i)
268 {
269 /* Note this returns a borrowed reference. */
270 PyObject *arg = PyTuple_GetItem (args, i);
271 if (arg == nullptr)
272 return nullptr;
273 gdb::unique_xmalloc_ptr<char> str = python_string_to_host_string (arg);
274 if (str == nullptr)
275 return nullptr;
276 if (i == 0)
277 mi_command = std::move (str);
278 else
279 arg_strings.push_back (std::move (str));
280 }
281
282 py_ui_out uiout;
283
284 try
285 {
286 scoped_restore save_uiout = make_scoped_restore (&current_uiout, &uiout);
287 auto parser = gdb::make_unique<mi_parse> (std::move (mi_command),
288 std::move (arg_strings));
289 mi_execute_command (parser.get ());
290 }
291 catch (const gdb_exception &except)
292 {
294 return nullptr;
295 }
296
297 return uiout.result ();
298}
const char *const name
bool do_is_mi_like_p() const override
Definition py-mi.c:42
void do_progress_end() override
Definition py-mi.c:60
bool can_emit_style_escape() const override
Definition py-mi.c:39
void add_field(const char *name, const gdbpy_ref<> &obj)
Definition py-mi.c:153
gdb::optional< gdbpy_err_fetch > m_error
Definition py-mi.c:142
void do_field_string(int fldno, int width, ui_align align, const char *fldname, const char *string, const ui_file_style &style) override
Definition py-mi.c:232
py_ui_out()
Definition py-mi.c:32
void do_table_header(int width, ui_align align, const std::string &col_name, const std::string &col_hdr) override
Definition py-mi.c:76
void do_progress_notify(const std::string &, const char *, double, double) override
Definition py-mi.c:62
void do_field_fmt(int fldno, int width, ui_align align, const char *fldname, const ui_file_style &style, const char *format, va_list args) override ATTRIBUTE_PRINTF(7
Definition py-mi.c:244
void do_table_end() override
Definition py-mi.c:72
void do_message(const ui_file_style &style, const char *format, va_list args) override ATTRIBUTE_PRINTF(3
void do_text(const char *string) override
Definition py-mi.c:104
void do_field_signed(int fldno, int width, ui_align align, const char *fldname, LONGEST value) override
Definition py-mi.c:210
PyObject * result()
Definition py-mi.c:48
void do_table_begin(int nbrofcols, int nr_rows, const char *tblid) override
Definition py-mi.c:66
void do_begin(ui_out_type type, const char *id) override
Definition py-mi.c:175
void std::vector< object_desc > m_objects
Definition py-mi.c:138
object_desc & current()
Definition py-mi.c:145
void do_end(ui_out_type type) override
Definition py-mi.c:199
void void do_spaces(int numspaces) override
Definition py-mi.c:101
void do_progress_start() override
Definition py-mi.c:61
void do_table_body() override
Definition py-mi.c:70
void do_field_skip(int fldno, int width, ui_align align, const char *fldname) override
Definition py-mi.c:89
void do_field_unsigned(int fldno, int width, ui_align align, const char *fldname, ULONGEST value) override
Definition py-mi.c:221
virtual void virtual void do_wrap_hint(int indent)=0
virtual void do_flush()=0
virtual void do_redirect(struct ui_file *outstream)=0
static void ATTRIBUTE_PRINTF(1, 0)
Definition gdb_bfd.c:1154
void mi_execute_command(const char *cmd, int from_tty)
Definition mi-main.c:1920
PyObject * gdbpy_execute_mi_command(PyObject *self, PyObject *args, PyObject *kw)
Definition py-mi.c:258
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
Definition py-ref.h:42
static gdbpy_ref field_name(struct type *type, int field)
Definition py-type.c:234
gdbpy_ref host_string_to_python_string(const char *str)
Definition py-utils.c:154
gdbpy_ref gdb_py_object_from_longest(LONGEST l)
Definition py-utils.c:282
void gdbpy_convert_exception(const struct gdb_exception &exception)
Definition py-utils.c:217
gdb::unique_xmalloc_ptr< char > python_string_to_host_string(PyObject *obj)
Definition py-utils.c:142
gdbpy_ref gdb_py_object_from_ulongest(ULONGEST l)
Definition py-utils.c:293
enum var_types type
Definition scm-param.c:142
Definition value.h:130
ui_out_type
Definition ui-out.h:70
@ ui_out_type_list
Definition ui-out.h:72
@ ui_out_type_tuple
Definition ui-out.h:71
ui_align
Definition ui-out.h:44
@ fix_multi_location_breakpoint_output
Definition ui-out.h:55
@ fix_breakpoint_script_output
Definition ui-out.h:59
#define current_uiout
Definition ui-out.h:40
int PyObject
Definition varobj.c:41