47#define pymicmd_debug_printf(fmt, ...) \
48 debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
52#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
53 scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
133 gdb_assert (
m_pyobj->mi_command !=
nullptr);
134 gdb_assert (new_pyobj->
mi_command ==
nullptr);
140 gdb_assert (
m_pyobj->mi_command_name !=
nullptr);
144 gdb_assert (
m_pyobj->mi_command_name == this->name ());
151 m_pyobj->mi_command_name) == 0);
184static gdb::unique_xmalloc_ptr<char>
188 if (!PyUnicode_Check (key_obj))
191 gdb::unique_xmalloc_ptr<char> key_repr_string;
192 if (key_repr !=
nullptr)
194 if (key_repr_string ==
nullptr)
197 gdbpy_error (_(
"non-string object used as key: %s"),
198 key_repr_string.get ());
201 gdb::unique_xmalloc_ptr<char> key_string
203 if (key_string ==
nullptr)
208 auto is_valid_key_name = [] (
const char *
name) ->
bool
210 gdb_assert (
name !=
nullptr);
212 if (*
name ==
'\0' || !isalpha (*
name))
222 if (!is_valid_key_name (key_string.get ()))
224 if (*key_string.get () ==
'\0')
227 gdbpy_error (_(
"Invalid key in MI result: %s"), key_string.get ());
252 if (PyDict_Check (result))
254 PyObject *key, *
value;
257 while (PyDict_Next (result, &pos, &key, &
value))
259 gdb::unique_xmalloc_ptr<char> key_string
264 else if (PySequence_Check (result) && !PyUnicode_Check (result))
267 Py_ssize_t len = PySequence_Size (result);
270 for (Py_ssize_t i = 0; i < len; ++i)
278 else if (PyIter_Check (result))
284 item.reset (PyIter_Next (result));
287 if (PyErr_Occurred () !=
nullptr)
297 if (
string ==
nullptr)
320 if (!PyDict_Check (result))
321 gdbpy_error (_(
"Result from invoke must be a dictionary"));
323 PyObject *key, *
value;
325 while (PyDict_Next (result, &pos, &key, &
value))
327 gdb::unique_xmalloc_ptr<char> key_string
345 if (parse->
argv ==
nullptr)
346 error (_(
"Problem parsing arguments: %s %s"), parse->
command, parse->
args);
354 if (argobj ==
nullptr)
357 for (
int i = 0; i < parse->
argc; ++i)
360 strlen (parse->
argv[i]),
362 if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
366 gdb_assert (this->
m_pyobj !=
nullptr);
367 gdb_assert (PyErr_Occurred () ==
nullptr);
370 argobj.get (),
nullptr));
371 if (result ==
nullptr)
374 if (result != Py_None)
383 gdb_assert (cmd_obj !=
nullptr);
385 gdb_assert (cmd !=
nullptr);
387 gdb_assert (
name !=
nullptr);
390 gdb_assert (mi_cmd == cmd);
391 gdb_assert (cmd->
m_pyobj == cmd_obj);
421 gdb_assert (removed);
452 if (cmd !=
nullptr && cmd_py ==
nullptr)
456 PyErr_SetString (PyExc_RuntimeError,
457 _(
"unable to add command, name is already in use"));
461 if (cmd_py !=
nullptr)
491 static const char *keywords[] = {
"name",
nullptr };
499 const int name_len = strlen (
name);
502 PyErr_SetString (PyExc_ValueError, _(
"MI command name is empty."));
505 else if ((name_len < 2) || (
name[0] !=
'-') || !isalnum (
name[1]))
507 PyErr_SetString (PyExc_ValueError,
508 _(
"MI command name does not start with '-'"
509 " followed by at least one letter or digit."));
514 for (
int i = 2; i < name_len; i++)
516 if (!isalnum (
name[i]) &&
name[i] !=
'-')
520 _(
"MI command name contains invalid character: %c."),
547 _(
"can't reinitialize object with a different command name"));
593 Py_TYPE (obj)->tp_free (obj);
638 std::string name_str = string_printf (
"-%s", micmd_obj->
mi_command_name);
639 return PyUnicode_FromString (name_str.c_str ());
667 bool installed_p = PyObject_IsTrue (newvalue);
668 if (installed_p == (micmd_obj->
mi_command !=
nullptr))
682 "Is this command installed for use.",
nullptr },
689 PyVarObject_HEAD_INIT (
nullptr, 0)
"gdb.MICommand",
707 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
708 "GDB mi-command object",
733 _(
"Set Python micmd debugging."),
734 _(
"Show Python micmd debugging."),
735 _(
"When on, Python micmd debugging is enabled."),
constexpr string_view get()
const char * host_charset(void)
void field_string(const char *fldname, const char *string, const ui_file_style &style=ui_file_style())
struct cmd_list_element * showdebuglist
struct cmd_list_element * setdebuglist
set_show_commands add_setshow_boolean_cmd(const char *name, enum command_class theclass, bool *var, const char *set_doc, const char *show_doc, const char *help_doc, cmd_func_ftype *set_func, show_value_ftype *show_func, struct cmd_list_element **set_list, struct cmd_list_element **show_list)
mi_command * mi_cmd_lookup(const char *command)
void remove_mi_cmd_entries(remove_mi_cmd_entries_ftype callback)
bool remove_mi_cmd_entry(const std::string &name)
bool insert_mi_cmd_entry(mi_command_up command)
void mi_parse_argv(const char *args, struct mi_parse *parse)
static void show_pymicmd_debug(struct ui_file *file, int from_tty, struct cmd_list_element *c, const char *value)
static int micmdpy_init(PyObject *self, PyObject *args, PyObject *kwargs)
static void micmdpy_dealloc(PyObject *obj)
static int micmdpy_install_command(micmdpy_object *obj)
static void serialize_mi_result(PyObject *result)
void _initialize_py_micmd()
PyTypeObject micmdpy_object_type
#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT
static PyObject * micmdpy_get_installed(PyObject *self, void *closure)
static int micmdpy_set_installed(PyObject *self, PyObject *newvalue, void *closure)
#define pymicmd_debug_printf(fmt,...)
static PyObject * invoke_cst
std::unique_ptr< mi_command_py > mi_command_py_up
static gdb_PyGetSetDef micmdpy_object_getset[]
static PyObject * micmdpy_get_name(PyObject *self, void *closure)
int gdbpy_initialize_micommands()
static void serialize_mi_result_1(PyObject *result, const char *field_name)
static gdb::unique_xmalloc_ptr< char > py_object_to_mi_key(PyObject *key_obj)
static mi_command_py * as_mi_command_py(mi_command *cmd)
static bool pymicmd_debug
static int micmdpy_uninstall_command(micmdpy_object *obj)
void gdbpy_finalize_micommands()
gdb::ref_ptr< T, gdbpy_ref_policy< T > > gdbpy_ref
static gdbpy_ref field_name(struct type *type, int field)
gdb::unique_xmalloc_ptr< char > python_string_to_target_string(PyObject *obj)
void gdbpy_error(const char *fmt,...)
gdb::unique_xmalloc_ptr< char > gdbpy_obj_to_string(PyObject *obj)
int gdb_pymodule_addobject(PyObject *module, const char *name, PyObject *object)
void gdbpy_handle_exception()
#define CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF(ARG)
static int gdb_PyArg_ParseTupleAndKeywords(PyObject *args, PyObject *kw, const char *format, const char **keywords,...)
void swap_python_object(micmdpy_object *new_pyobj)
gdbpy_ref< micmdpy_object > m_pyobj
virtual void invoke(struct mi_parse *parse) const override
mi_command_py(const char *name, micmdpy_object *object)
static void validate_installation(micmdpy_object *cmd_obj)
const char * name() const
PyObject_HEAD struct mi_command_py * mi_command
void gdb_printf(struct ui_file *stream, const char *format,...)