1 /* Python interface to MI commands
3 Copyright (C) 2023-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
20 #include "python-internal.h"
26 #include "mi/mi-parse.h"
27 #include "mi/mi-console.h"
28 #include "mi/mi-interp.h"
31 py_ui_out::add_field (const char *name
, const gdbpy_ref
<> &obj
)
39 object_desc
&desc
= current ();
40 if (desc
.type
== ui_out_type_list
)
42 if (PyList_Append (desc
.obj
.get (), obj
.get ()) < 0)
47 if (PyDict_SetItemString (desc
.obj
.get (), name
, obj
.get ()) < 0)
53 py_ui_out::do_begin (ui_out_type type
, const char *id
)
55 if (m_error
.has_value ())
58 gdbpy_ref
<> new_obj (type
== ui_out_type_list
61 if (new_obj
== nullptr)
69 new_desc
.field_name
= id
;
70 new_desc
.obj
= std::move (new_obj
);
73 m_objects
.push_back (std::move (new_desc
));
77 py_ui_out::do_end (ui_out_type type
)
79 if (m_error
.has_value ())
82 object_desc new_obj
= std::move (current ());
83 m_objects
.pop_back ();
84 add_field (new_obj
.field_name
.c_str (), new_obj
.obj
);
88 py_ui_out::do_field_signed (int fldno
, int width
, ui_align align
,
89 const char *fldname
, LONGEST value
,
90 const ui_file_style
&style
)
92 if (m_error
.has_value ())
95 gdbpy_ref
<> val
= gdb_py_object_from_longest (value
);
96 add_field (fldname
, val
);
100 py_ui_out::do_field_unsigned (int fldno
, int width
, ui_align align
,
101 const char *fldname
, ULONGEST value
)
103 if (m_error
.has_value ())
106 gdbpy_ref
<> val
= gdb_py_object_from_ulongest (value
);
107 add_field (fldname
, val
);
111 py_ui_out::do_field_string (int fldno
, int width
, ui_align align
,
112 const char *fldname
, const char *string
,
113 const ui_file_style
&style
)
115 if (m_error
.has_value ())
118 gdbpy_ref
<> val
= host_string_to_python_string (string
);
119 add_field (fldname
, val
);
123 py_ui_out::do_field_fmt (int fldno
, int width
, ui_align align
,
124 const char *fldname
, const ui_file_style
&style
,
125 const char *format
, va_list args
)
127 if (m_error
.has_value ())
130 std::string str
= string_vprintf (format
, args
);
131 do_field_string (fldno
, width
, align
, fldname
, str
.c_str (), style
);
134 /* Implementation of the gdb.execute_mi command. */
137 gdbpy_execute_mi_command (PyObject
*self
, PyObject
*args
, PyObject
*kw
)
139 gdb::unique_xmalloc_ptr
<char> mi_command
;
140 std::vector
<gdb::unique_xmalloc_ptr
<char>> arg_strings
;
142 Py_ssize_t n_args
= PyTuple_Size (args
);
148 PyErr_SetString (PyExc_TypeError
,
149 _("gdb.execute_mi requires command argument"));
153 for (Py_ssize_t i
= 0; i
< n_args
; ++i
)
155 /* Note this returns a borrowed reference. */
156 PyObject
*arg
= PyTuple_GetItem (args
, i
);
159 gdb::unique_xmalloc_ptr
<char> str
= python_string_to_host_string (arg
);
163 mi_command
= std::move (str
);
165 arg_strings
.push_back (std::move (str
));
172 scoped_restore save_uiout
= make_scoped_restore (¤t_uiout
, &uiout
);
173 auto parser
= std::make_unique
<mi_parse
> (std::move (mi_command
),
174 std::move (arg_strings
));
175 mi_execute_command (parser
.get ());
177 catch (const gdb_exception
&except
)
179 return gdbpy_handle_gdb_exception (nullptr, except
);
182 return uiout
.result ().release ();
185 /* Convert KEY_OBJ into a string that can be used as a field name in MI
186 output. KEY_OBJ must be a Python string object, and must only contain
187 characters suitable for use as an MI field name.
189 If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
190 then an error is thrown. Otherwise, KEY_OBJ is converted to a string
193 static gdb::unique_xmalloc_ptr
<char>
194 py_object_to_mi_key (PyObject
*key_obj
)
196 /* The key must be a string. */
197 if (!PyUnicode_Check (key_obj
))
199 gdbpy_ref
<> key_repr (PyObject_Repr (key_obj
));
200 gdb::unique_xmalloc_ptr
<char> key_repr_string
;
201 if (key_repr
!= nullptr)
202 key_repr_string
= python_string_to_target_string (key_repr
.get ());
203 if (key_repr_string
== nullptr)
204 gdbpy_handle_exception ();
206 gdbpy_error (_("non-string object used as key: %s"),
207 key_repr_string
.get ());
210 gdb::unique_xmalloc_ptr
<char> key_string
211 = python_string_to_target_string (key_obj
);
212 if (key_string
== nullptr)
213 gdbpy_handle_exception ();
215 /* Predicate function, returns true if NAME is a valid field name for use
216 in MI result output, otherwise, returns false. */
217 auto is_valid_key_name
= [] (const char *name
) -> bool
219 gdb_assert (name
!= nullptr);
221 if (*name
== '\0' || !isalpha (*name
))
224 for (; *name
!= '\0'; ++name
)
225 if (!isalnum (*name
) && *name
!= '_' && *name
!= '-')
231 if (!is_valid_key_name (key_string
.get ()))
233 if (*key_string
.get () == '\0')
234 gdbpy_error (_("Invalid empty key in MI result"));
236 gdbpy_error (_("Invalid key in MI result: %s"), key_string
.get ());
242 /* Serialize RESULT and print it in MI format to the current_uiout.
243 FIELD_NAME is used as the name of this result field.
245 RESULT can be a dictionary, a sequence, an iterator, or an object that
246 can be converted to a string, these are converted to the matching MI
247 output format (dictionaries as tuples, sequences and iterators as lists,
248 and strings as named fields).
250 If anything goes wrong while formatting the output then an error is
253 This function is the recursive inner core of serialize_mi_result, and
254 should only be called from that function. */
257 serialize_mi_result_1 (PyObject
*result
, const char *field_name
)
259 struct ui_out
*uiout
= current_uiout
;
261 if (PyDict_Check (result
))
263 PyObject
*key
, *value
;
265 ui_out_emit_tuple
tuple_emitter (uiout
, field_name
);
266 while (PyDict_Next (result
, &pos
, &key
, &value
))
268 gdb::unique_xmalloc_ptr
<char> key_string
269 (py_object_to_mi_key (key
));
270 serialize_mi_result_1 (value
, key_string
.get ());
273 else if (PySequence_Check (result
) && !PyUnicode_Check (result
))
275 ui_out_emit_list
list_emitter (uiout
, field_name
);
276 Py_ssize_t len
= PySequence_Size (result
);
278 gdbpy_handle_exception ();
279 for (Py_ssize_t i
= 0; i
< len
; ++i
)
281 gdbpy_ref
<> item (PySequence_ITEM (result
, i
));
283 gdbpy_handle_exception ();
284 serialize_mi_result_1 (item
.get (), nullptr);
287 else if (PyIter_Check (result
))
290 ui_out_emit_list
list_emitter (uiout
, field_name
);
293 item
.reset (PyIter_Next (result
));
296 if (PyErr_Occurred () != nullptr)
297 gdbpy_handle_exception ();
300 serialize_mi_result_1 (item
.get (), nullptr);
305 if (PyLong_Check (result
))
308 gdb_py_longest val
= gdb_py_long_as_long_and_overflow (result
,
310 if (PyErr_Occurred () != nullptr)
311 gdbpy_handle_exception ();
314 uiout
->field_signed (field_name
, val
);
317 /* Fall through to the string case on overflow. */
320 gdb::unique_xmalloc_ptr
<char> string (gdbpy_obj_to_string (result
));
321 if (string
== nullptr)
322 gdbpy_handle_exception ();
323 uiout
->field_string (field_name
, string
.get ());
327 /* See python-internal.h. */
330 serialize_mi_results (PyObject
*results
)
332 gdb_assert (PyDict_Check (results
));
334 PyObject
*key
, *value
;
336 while (PyDict_Next (results
, &pos
, &key
, &value
))
338 gdb::unique_xmalloc_ptr
<char> key_string
339 (py_object_to_mi_key (key
));
340 serialize_mi_result_1 (value
, key_string
.get ());
344 /* See python-internal.h. */
347 gdbpy_notify_mi (PyObject
*self
, PyObject
*args
, PyObject
*kwargs
)
349 static const char *keywords
[] = { "name", "data", nullptr };
350 char *name
= nullptr;
351 PyObject
*data
= Py_None
;
353 if (!gdb_PyArg_ParseTupleAndKeywords (args
, kwargs
, "s|O", keywords
,
357 /* Validate notification name. */
358 const int name_len
= strlen (name
);
361 PyErr_SetString (PyExc_ValueError
, _("MI notification name is empty."));
364 for (int i
= 0; i
< name_len
; i
++)
366 if (!isalnum (name
[i
]) && name
[i
] != '-')
370 _("MI notification name contains invalid character: %c."),
376 /* Validate additional data. */
377 if (!(data
== Py_None
|| PyDict_Check (data
)))
381 _("MI notification data must be either None or a dictionary, not %s"),
382 Py_TYPE (data
)->tp_name
);
386 SWITCH_THRU_ALL_UIS ()
388 struct mi_interp
*mi
= as_mi_interp (top_level_interpreter ());
393 target_terminal::scoped_restore_terminal_state term_state
;
394 target_terminal::ours_for_output ();
396 gdb_printf (mi
->event_channel
, "%s", name
);
399 ui_out
*mi_uiout
= mi
->interp_ui_out ();
400 ui_out_redirect_pop
redir (mi_uiout
, mi
->event_channel
);
401 scoped_restore restore_uiout
402 = make_scoped_restore (¤t_uiout
, mi_uiout
);
404 serialize_mi_results (data
);
406 gdb_flush (mi
->event_channel
);