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))
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)
296 if (PyLong_Check (result))
301 if (PyErr_Occurred () !=
nullptr)
312 if (
string ==
nullptr)
335 if (!PyDict_Check (result))
336 gdbpy_error (_(
"Result from invoke must be a dictionary"));
340 while (PyDict_Next (result, &pos, &key, &
value))
342 gdb::unique_xmalloc_ptr<char> key_string
360 if (parse->
argv ==
nullptr)
361 error (_(
"Problem parsing arguments: %s %s"), parse->
command.get (),
370 if (argobj ==
nullptr)
373 for (
int i = 0; i < parse->
argc; ++i)
376 strlen (parse->
argv[i]),
378 if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
382 gdb_assert (this->
m_pyobj !=
nullptr);
383 gdb_assert (PyErr_Occurred () ==
nullptr);
386 argobj.get (),
nullptr));
387 if (result ==
nullptr)
390 if (result != Py_None)
399 gdb_assert (cmd_obj !=
nullptr);
401 gdb_assert (cmd !=
nullptr);
403 gdb_assert (
name !=
nullptr);
406 gdb_assert (mi_cmd == cmd);
407 gdb_assert (cmd->
m_pyobj == cmd_obj);
437 gdb_assert (removed);
468 if (cmd !=
nullptr && cmd_py ==
nullptr)
472 PyErr_SetString (PyExc_RuntimeError,
473 _(
"unable to add command, name is already in use"));
477 if (cmd_py !=
nullptr)
507 static const char *keywords[] = {
"name",
nullptr };
515 const int name_len = strlen (
name);
518 PyErr_SetString (PyExc_ValueError, _(
"MI command name is empty."));
521 else if ((name_len < 2) || (
name[0] !=
'-') || !isalnum (
name[1]))
523 PyErr_SetString (PyExc_ValueError,
524 _(
"MI command name does not start with '-'"
525 " followed by at least one letter or digit."));
530 for (
int i = 2; i < name_len; i++)
532 if (!isalnum (
name[i]) &&
name[i] !=
'-')
536 _(
"MI command name contains invalid character: %c."),
563 _(
"can't reinitialize object with a different command name"));
609 Py_TYPE (obj)->tp_free (obj);
656 std::string name_str = string_printf (
"-%s", micmd_obj->
mi_command_name);
657 return PyUnicode_FromString (name_str.c_str ());
685 bool installed_p = PyObject_IsTrue (newvalue);
686 if (installed_p == (micmd_obj->
mi_command !=
nullptr))
700 "Is this command installed for use.",
nullptr },
707 PyVarObject_HEAD_INIT (
nullptr, 0)
"gdb.MICommand",
725 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
726 "GDB mi-command object",
751 _(
"Set Python micmd debugging."),
752 _(
"Show Python micmd debugging."),
753 _(
"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())
void field_signed(const char *fldname, LONGEST value)
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)
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
static void gdbpy_finalize_micommands()
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)
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)
static int CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION gdbpy_initialize_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)
#define CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION
#define GDBPY_INITIALIZE_FILE(INIT,...)
#define gdb_py_long_as_long_and_overflow
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
mi_command(const char *name, int *suppress_notification)
gdb::unique_xmalloc_ptr< char > command
PyObject_HEAD struct mi_command_py * mi_command
void gdb_printf(struct ui_file *stream, const char *format,...)