public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/5] create GDB/MI commands using python
@ 2022-01-17 12:44 Jan Vrany
  2022-01-17 12:44 ` [PATCH 1/5] gdb/mi: introduce new class mi_command_builtin Jan Vrany
                   ` (5 more replies)
  0 siblings, 6 replies; 41+ messages in thread
From: Jan Vrany @ 2022-01-17 12:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

This is a restart of an earlier attempts to allow custom
GDB/MI commands written in Python.

I went thought comments made on earlier attempts and changed
code as suggested except in few cases - see my comments below.

====
gdb/python/py-micmd.c

> > +
> > +extern PyTypeObject
> > +  micmdpy_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
>
> Is there a reason this is "extern"?  I don't see this used anywhere
> outside py-micmd.c.

I think it is. micmdpy_object_type is needed in gdbpy_initialize_micommands() and then
it is statically initialized at the bottom of the file when all functions are defined.
The extern allows this. Same thing is done in py-cmd.c for CLI commands.


> > +
> > +/* If the command invoked returns a list, this function parses it and create an
> > +   appropriate MI out output.
> > +
> > +   The returned values must be Python string,
>
> "values ... string" doesn't parse correctly for me.
>
> Did you mean

I have rewritten the comment and renamed the function to emit_py_result()
which seems to me a better name (now).


> > +         }
> > +     }

> There's aspace vs tabs mixup above, and in other parts
> of the file too.  Please fix that throughout.

Changed to use spaces consistenly.


> > +
> > +PyTypeObject micmdpy_object_type = {
>
> Can this be static?

I don't think so, see above.
=====

Jan Vrany (5):
  gdb/mi: introduce new class mi_command_builtin
  gdb/python: create GDB/MI commands using python.
  gdb/python: allow redefinition of python GDB/MI commands
  gdb/testsuite: add tests for python-defined MI commands
  gdb/python: document GDB/MI commands in Python

 gdb/Makefile.in                        |   1 +
 gdb/NEWS                               |   2 +
 gdb/doc/python.texi                    |  80 ++++++-
 gdb/mi/mi-cmds.c                       |  60 +++--
 gdb/mi/mi-cmds.h                       |  32 ++-
 gdb/python/py-micmd.c                  | 312 +++++++++++++++++++++++++
 gdb/python/py-micmd.h                  |  66 ++++++
 gdb/python/python-internal.h           |   2 +
 gdb/python/python.c                    |  13 +-
 gdb/testsuite/gdb.python/py-mi-cmd.exp | 133 +++++++++++
 gdb/testsuite/gdb.python/py-mi-cmd.py  |  68 ++++++
 11 files changed, 733 insertions(+), 36 deletions(-)
 create mode 100644 gdb/python/py-micmd.c
 create mode 100644 gdb/python/py-micmd.h
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.exp
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.py

-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH 1/5] gdb/mi: introduce new class mi_command_builtin
  2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
@ 2022-01-17 12:44 ` Jan Vrany
  2022-01-17 12:44 ` [PATCH 2/5] gdb/python: create GDB/MI commands using python Jan Vrany
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 41+ messages in thread
From: Jan Vrany @ 2022-01-17 12:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

The motivation for this commit is that GDB/MI commands have their names
statically allocated whereas Python-based commands (that will be
introduced in later) will have their name dynamically allocated.

To support this, this commit introduces new abstract class
`mi_command_builtin` that allows for its name to be statically allocated.
Future Python-based commands will hold onto dynamically allocated
std::string.
---
 gdb/mi/mi-cmds.c | 43 ++++++++++++++++++++++++++++++-------------
 gdb/mi/mi-cmds.h | 14 +++++---------
 2 files changed, 35 insertions(+), 22 deletions(-)

diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..57fe32c1cc6 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -34,16 +34,42 @@ using mi_command_up = std::unique_ptr<struct mi_command>;
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
 
+/* The abstract base class for all built-in MI command types.  */
+
+struct mi_command_builtin : public mi_command
+{
+  /* Constructor.  NAME is the name of this MI command, excluding any
+     leading dash, that is the initial string the user will enter to run
+     this command.  For SUPPRESS_NOTIFICATION see mi_command
+     constructor, FUNC is the function called from do_invoke, which
+     implements this MI command.  */
+  mi_command_builtin (const char *name, int *suppress_notification)
+    : mi_command (suppress_notification),
+      m_name (name)
+  {
+    gdb_assert (m_name != nullptr && m_name[0] != '\0' && m_name[0] != '-');
+  }
+
+  virtual const char *name () const override
+  {
+    return m_name;
+  }
+
+private:
+  /* The name of the command.  */
+  const char *m_name;
+};
+
 /* MI command with a pure MI implementation.  */
 
-struct mi_command_mi : public mi_command
+struct mi_command_mi : public mi_command_builtin
 {
   /* Constructor.  For NAME and SUPPRESS_NOTIFICATION see mi_command
      constructor, FUNC is the function called from do_invoke, which
      implements this MI command.  */
   mi_command_mi (const char *name, mi_cmd_argv_ftype func,
                  int *suppress_notification)
-    : mi_command (name, suppress_notification),
+    : mi_command_builtin (name, suppress_notification),
       m_argv_function (func)
   {
     gdb_assert (func != nullptr);
@@ -72,7 +98,7 @@ struct mi_command_mi : public mi_command
 
 /* MI command implemented on top of a CLI command.  */
 
-struct mi_command_cli : public mi_command
+struct mi_command_cli : public mi_command_builtin
 {
   /* Constructor.  For NAME and SUPPRESS_NOTIFICATION see mi_command
      constructor, CLI_NAME is the name of a CLI command that should be
@@ -82,7 +108,7 @@ struct mi_command_cli : public mi_command
      false, nullptr is send to CLI_NAME as its argument string.  */
   mi_command_cli (const char *name, const char *cli_name, bool args_p,
                   int *suppress_notification)
-    : mi_command (name, suppress_notification),
+    : mi_command_builtin (name, suppress_notification),
       m_cli_name (cli_name),
       m_args_p (args_p)
   { /* Nothing.  */ }
@@ -159,15 +185,6 @@ add_mi_cmd_cli (const char *name, const char *cli_name, int args_p,
 
 /* See mi-cmds.h.  */
 
-mi_command::mi_command (const char *name, int *suppress_notification)
-  : m_name (name),
-    m_suppress_notification (suppress_notification)
-{
-  gdb_assert (m_name != nullptr && m_name[0] != '\0');
-}
-
-/* See mi-cmds.h.  */
-
 void
 mi_command::invoke (struct mi_parse *parse) const
 {
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..94ecb271d48 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -143,12 +143,12 @@ extern mi_cmd_argv_ftype mi_cmd_complete;
 
 struct mi_command
 {
-  /* Constructor.  NAME is the name of this MI command, excluding any
-     leading dash, that is the initial string the user will enter to run
-     this command.  The SUPPRESS_NOTIFICATION pointer is a flag which will
+  /* Constructor.  The SUPPRESS_NOTIFICATION pointer is a flag which will
      be set to 1 when this command is invoked, and reset to its previous
      value once the command invocation has completed.  */
-  mi_command (const char *name, int *suppress_notification);
+  mi_command (int *suppress_notification)
+    : m_suppress_notification (suppress_notification)
+  {}
 
   /* Destructor.  */
   virtual ~mi_command () = default;
@@ -156,8 +156,7 @@ struct mi_command
   /* Return the name of this command.  This is the command that the user
      will actually type in, without any arguments, and without the leading
      dash.  */
-  const char *name () const
-  { return m_name; }
+  virtual const char *name () const = 0;
 
   /* Execute the MI command.  Can throw an exception if something goes
      wrong.  */
@@ -180,9 +179,6 @@ struct mi_command
      then this function returns an empty gdb::optional.  */
   gdb::optional<scoped_restore_tmpl<int>> do_suppress_notification () const;
 
-  /* The name of the command.  */
-  const char *m_name;
-
   /* Pointer to integer to set during command's invocation.  */
   int *m_suppress_notification;
 };
-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH 2/5] gdb/python: create GDB/MI commands using python.
  2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
  2022-01-17 12:44 ` [PATCH 1/5] gdb/mi: introduce new class mi_command_builtin Jan Vrany
@ 2022-01-17 12:44 ` Jan Vrany
  2022-02-06 16:52   ` Lancelot SIX
  2022-01-17 12:44 ` [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands Jan Vrany
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 41+ messages in thread
From: Jan Vrany @ 2022-01-17 12:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

This commit allows an user to create custom MI commands using Python
similarly to what is possible for Python CLI commands.

A new subclass of mi_command is defined for Python MI commands,
mi_command_py. A new file, py-micmd.c contains the logic for Python
MI commands.
---
 gdb/Makefile.in              |   1 +
 gdb/mi/mi-cmds.c             |  13 +-
 gdb/mi/mi-cmds.h             |  14 ++
 gdb/python/py-micmd.c        | 300 +++++++++++++++++++++++++++++++++++
 gdb/python/py-micmd.h        |  63 ++++++++
 gdb/python/python-internal.h |   2 +
 gdb/python/python.c          |  13 +-
 7 files changed, 396 insertions(+), 10 deletions(-)
 create mode 100644 gdb/python/py-micmd.c
 create mode 100644 gdb/python/py-micmd.h

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index d0db5fbdee1..5cb428459c3 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index 57fe32c1cc6..c82bc4810df 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,13 +26,11 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
+/* See mi-cmds.h.  */
 
-using mi_command_up = std::unique_ptr<struct mi_command>;
+std::map<std::string, mi_command_up> mi_cmd_table;
 
-/* MI command table (built at run time). */
 
-static std::map<std::string, mi_command_up> mi_cmd_table;
 
 /* The abstract base class for all built-in MI command types.  */
 
@@ -134,12 +132,9 @@ struct mi_command_cli : public mi_command_builtin
   bool m_args_p;
 };
 
-/* Insert COMMAND into the global mi_cmd_table.  Return false if
-   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
-   not have been added to mi_cmd_table.  Otherwise, return true, and
-   COMMAND was added to mi_cmd_table.  */
+/* See mi-cmds.h.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 94ecb271d48..75c5927e3cb 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -22,6 +22,7 @@
 #ifndef MI_MI_CMDS_H
 #define MI_MI_CMDS_H
 
+#include <map>
 #include "gdbsupport/gdb_optional.h"
 
 enum print_values {
@@ -183,6 +184,14 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the MI_CMD_TABLE.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
+/* MI command table (built at run time). */
+
+extern std::map<std::string, mi_command_up> mi_cmd_table;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -190,4 +199,9 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert a new mi-command into the command table.  Return true if
+   insertion was successful.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..b94b748b25c
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,300 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* gdb MI commands implemented in Python  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "python/py-micmd.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+
+#include <string>
+
+static PyObject *invoke_cst;
+
+extern PyTypeObject
+  micmdpy_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* If the command invoked returns a list, this function parses it and create an
+   appropriate MI out output.
+
+   The returned values must be Python string, and can be contained within Python
+   lists and dictionaries. It is possible to have a multiple levels of lists
+   and/or dictionaries.  */
+
+static void
+parse_mi_result (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  if (!PyString_Check (key))
+	    {
+	      gdbpy_ref<> key_repr (PyObject_Repr (key));
+	      if (PyErr_Occurred () != NULL)
+                {
+                  gdbpy_err_fetch ex;
+                  gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
+
+                  if (ex_msg == NULL || *ex_msg == '\0')
+                    error (_("Non-string object used as key."));
+                  else
+                    error (_("Non-string object used as key: %s."),
+                           ex_msg.get ());
+                }
+              else
+	        {
+	          auto key_repr_string
+	                 = python_string_to_target_string (key_repr.get ());
+	          error (_("Non-string object used as key: %s."),
+                         key_repr_string.get ());
+	        }
+	    }
+
+	  auto key_string = python_string_to_target_string (key);
+	  parse_mi_result (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      for (Py_ssize_t i = 0; i < PySequence_Size (result); ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  parse_mi_result (item.get (), NULL);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (item.reset (PyIter_Next (result)), item != nullptr)
+	parse_mi_result (item.get (), NULL);
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Object initializer; sets up gdb-side structures for MI command.
+
+   Use: __init__(NAME).
+
+   NAME is the name of the MI command to register.  It must start with a dash
+   as traditional MI commands do.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
+{
+  const char *name;
+  gdbpy_ref<> self_ref = gdbpy_ref<>::new_reference (self);
+
+  if (!PyArg_ParseTuple (args, "s", &name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      error (_("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      error (_("MI command name does not start with '-'"
+               " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    for (int i = 2; i < name_len; i++)
+      {
+	if (!isalnum (name[i]) && name[i] != '-')
+	  {
+	    error (_("MI command name contains invalid character: %c."),
+                   name[i]);
+	    return -1;
+	  }
+      }
+
+  if (!PyObject_HasAttr (self_ref.get (), invoke_cst))
+      error (_("-%s: Python command object missing 'invoke' method."), name);
+
+  try
+    {
+      mi_command_up micommand = mi_command_up(new mi_command_py (name + 1, self_ref));
+
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+
+      if (!result)
+	{
+	  error (_("Unable to insert command."
+                   "The name might already be in use."));
+	  return -1;
+	}
+    }
+  catch (const gdb_exception &except)
+    {
+      GDB_PY_SET_HANDLE_EXCEPTION (except);
+    }
+
+  return 0;
+}
+
+mi_command_py::mi_command_py (const char *name, gdbpy_ref<> object)
+  : mi_command (NULL),
+    pyobj (object)
+{
+  gdb_assert (name != nullptr && name[0] != '\0' && name[0] != '-');
+  m_name = name;
+}
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == NULL)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = this->pyobj.get ();
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  gdb_assert (obj != nullptr);
+
+  if (!PyObject_HasAttr (obj, invoke_cst))
+      error (_("-%s: Python command object missing 'invoke' method."),
+	     name ());
+
+
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    {
+      gdbpy_print_stack ();
+      error (_("-%s: failed to create the Python arguments list."),
+	     name ());
+    }
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i], strlen (parse->argv[i]),
+					 host_charset (), NULL));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) != 0)
+	{
+	  error (_("-%s: failed to create the Python arguments list."),
+		 name ());
+	}
+    }
+
+  gdb_assert (PyErr_Occurred () == NULL);
+  gdbpy_ref<> result (
+    PyObject_CallMethodObjArgs (obj, invoke_cst, argobj.get (), NULL));
+  if (PyErr_Occurred () != NULL)
+    {
+      gdbpy_err_fetch ex;
+      gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
+
+      if (ex_msg == NULL || *ex_msg == '\0')
+	error (_("-%s: failed to execute command"), name ());
+      else
+	error (_("-%s: %s"), name (), ex_msg.get ());
+    }
+  else
+    {
+      if (Py_None != result)
+	parse_mi_result (result.get (), "result");
+    }
+}
+
+void mi_command_py::finalize ()
+{
+  this->pyobj.reset (nullptr);
+}
+
+/* Initialize the MI command object.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == NULL)
+    return -1;
+
+  return 0;
+}
+
+static PyMethodDef micmdpy_object_methods[] = {{0}};
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (NULL, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  0,						   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  micmdpy_object_methods,			   /* tp_methods */
+  0,						   /* tp_members */
+  0,						   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
diff --git a/gdb/python/py-micmd.h b/gdb/python/py-micmd.h
new file mode 100644
index 00000000000..b6b25351ac7
--- /dev/null
+++ b/gdb/python/py-micmd.h
@@ -0,0 +1,63 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+#ifndef PY_MICMDS_H
+#define PY_MICMDS_H
+
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "python-internal.h"
+#include "python/py-ref.h"
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+};
+
+typedef struct micmdpy_object micmdpy_object;
+
+/* MI command implemented in Python.  */
+
+class mi_command_py : public mi_command
+{
+  public:
+    /* Constructs a new mi_command_py object.  NAME is command name without
+       leading dash.  OBJECT is a reference to a Python object implementing
+       the command.  This object should inherit from gdb.MICommand and should
+       implement method invoke (args). */
+    mi_command_py (const char *name, gdbpy_ref<> object);
+
+
+    /* This is called just before shutting down a Python interpreter
+       to release python object implementing the command. */
+    void finalize ();
+
+    virtual const char *name () const override
+    { return m_name.c_str(); }
+
+  protected:
+    virtual void do_invoke(struct mi_parse *parse) const override;
+
+  private:
+    std::string m_name;
+
+    gdbpy_ref<> pyobj;
+};
+
+#endif
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 583989c5a6d..d5a0b4a4a91 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -561,6 +561,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4dcda53d9ab..6e2ed32bf90 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -37,6 +37,8 @@
 #include "run-on-main-thread.h"
 #include "gdbsupport/selftest.h"
 #include "observable.h"
+#include "mi/mi-cmds.h"
+#include "py-micmd.h"
 
 /* Declared constants and enum for python stack printing.  */
 static const char python_excp_none[] = "none";
@@ -1699,6 +1701,14 @@ finalize_python (void *ignore)
   python_gdbarch = target_gdbarch ();
   python_language = current_language;
 
+  for (const auto& name_and_cmd : mi_cmd_table)
+    {
+      mi_command *cmd = name_and_cmd.second.get ();
+      mi_command_py *cmd_py = dynamic_cast<mi_command_py*> (cmd);
+      if (cmd_py != nullptr)
+        cmd_py->finalize ();
+    }
+
   Py_Finalize ();
 
   gdb_python_initialized = false;
@@ -1887,7 +1897,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands
  2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
  2022-01-17 12:44 ` [PATCH 1/5] gdb/mi: introduce new class mi_command_builtin Jan Vrany
  2022-01-17 12:44 ` [PATCH 2/5] gdb/python: create GDB/MI commands using python Jan Vrany
@ 2022-01-17 12:44 ` Jan Vrany
  2022-02-06 17:13   ` Lancelot SIX
  2022-01-17 12:44 ` [PATCH 4/5] gdb/testsuite: add tests for python-defined MI commands Jan Vrany
                   ` (2 subsequent siblings)
  5 siblings, 1 reply; 41+ messages in thread
From: Jan Vrany @ 2022-01-17 12:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

Redefining python MI commands is especially useful when developing
them.
---
 gdb/mi/mi-cmds.c      |  10 +-
 gdb/mi/mi-cmds.h      |   4 +
 gdb/python/py-micmd.c | 236 ++++++++++++++++++++++--------------------
 gdb/python/py-micmd.h |  41 ++++----
 gdb/python/python.c   |   2 +-
 5 files changed, 160 insertions(+), 133 deletions(-)

diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index c82bc4810df..c2ec733ce06 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -141,7 +141,8 @@ insert_mi_cmd_entry (mi_command_up command)
 
   const std::string &name = command->name ();
 
-  if (mi_cmd_table.find (name) != mi_cmd_table.end ())
+  auto existing = mi_cmd_table.find (name);
+  if (existing != mi_cmd_table.end () && !existing->second->can_be_redefined ())
     return false;
 
   mi_cmd_table[name] = std::move (command);
@@ -180,6 +181,13 @@ add_mi_cmd_cli (const char *name, const char *cli_name, int args_p,
 
 /* See mi-cmds.h.  */
 
+
+bool
+mi_command::can_be_redefined ()
+{
+  return false;
+}
+
 void
 mi_command::invoke (struct mi_parse *parse) const
 {
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 75c5927e3cb..7d7aa52d4ae 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -23,6 +23,7 @@
 #define MI_MI_CMDS_H
 
 #include <map>
+#include <string>
 #include "gdbsupport/gdb_optional.h"
 
 enum print_values {
@@ -163,6 +164,9 @@ struct mi_command
      wrong.  */
   void invoke (struct mi_parse *parse) const;
 
+  /* Return TRUE if the command can be redefined, FALSE otherwise.  */
+  virtual bool can_be_redefined ();
+
 protected:
 
   /* The core of command invocation, this needs to be overridden in each
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
index b94b748b25c..59bf534b5b9 100644
--- a/gdb/python/py-micmd.c
+++ b/gdb/python/py-micmd.c
@@ -1,6 +1,6 @@
 /* MI Command Set for GDB, the GNU debugger.
 
-   Copyright (C) 2019 Free Software Foundation, Inc.
+   Copyright (C) 2019-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -28,34 +28,40 @@
 
 #include <string>
 
+/* Reference to Python string "invoke".  Used by this module when obtaining
+   invoke() method of Python MI command.  */
+
 static PyObject *invoke_cst;
 
 extern PyTypeObject
   micmdpy_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
 
-/* If the command invoked returns a list, this function parses it and create an
-   appropriate MI out output.
+/* Output Python OBJECT to MI channel as a "result" [+] named FIELD_NAME.  Various
+   kinds of Python objects are supported:
+
+   - Python 'dict' is emitted as a MI tuple.  Only string keys are supported.
+   - Python sequence or iterator is emitted as MI list.
+   - Any other Python object converted to string using Python's str() and emitted
+     as MI string.
 
-   The returned values must be Python string, and can be contained within Python
-   lists and dictionaries. It is possible to have a multiple levels of lists
-   and/or dictionaries.  */
+   [+] "result" refers to GDB/MI output syntax rule "result".  */
 
 static void
-parse_mi_result (PyObject *result, const char *field_name)
+emit_py_result (PyObject *object, const char *field_name)
 {
   struct ui_out *uiout = current_uiout;
 
-  if (PyDict_Check (result))
+  if (PyDict_Check (object))
     {
       PyObject *key, *value;
       Py_ssize_t pos = 0;
       ui_out_emit_tuple tuple_emitter (uiout, field_name);
-      while (PyDict_Next (result, &pos, &key, &value))
-	{
-	  if (!PyString_Check (key))
-	    {
-	      gdbpy_ref<> key_repr (PyObject_Repr (key));
-	      if (PyErr_Occurred () != NULL)
+      while (PyDict_Next (object, &pos, &key, &value))
+        {
+          if (!PyString_Check (key))
+            {
+              gdbpy_ref<> key_repr (PyObject_Repr (key));
+              if (PyErr_Occurred () != NULL)
                 {
                   gdbpy_err_fetch ex;
                   gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
@@ -67,42 +73,42 @@ parse_mi_result (PyObject *result, const char *field_name)
                            ex_msg.get ());
                 }
               else
-	        {
-	          auto key_repr_string
-	                 = python_string_to_target_string (key_repr.get ());
-	          error (_("Non-string object used as key: %s."),
+                {
+                  auto key_repr_string
+                         = python_string_to_target_string (key_repr.get ());
+                  error (_("Non-string object used as key: %s."),
                          key_repr_string.get ());
-	        }
-	    }
+                }
+            }
 
-	  auto key_string = python_string_to_target_string (key);
-	  parse_mi_result (value, key_string.get ());
-	}
+          auto key_string = python_string_to_target_string (key);
+          emit_py_result (value, key_string.get ());
+        }
     }
-  else if (PySequence_Check (result) && !PyString_Check (result))
+  else if (PySequence_Check (object) && !PyString_Check (object))
     {
       ui_out_emit_list list_emitter (uiout, field_name);
-      for (Py_ssize_t i = 0; i < PySequence_Size (result); ++i)
-	{
-          gdbpy_ref<> item (PySequence_ITEM (result, i));
-	  parse_mi_result (item.get (), NULL);
-	}
+      for (Py_ssize_t i = 0; i < PySequence_Size (object); ++i)
+        {
+          gdbpy_ref<> item (PySequence_ITEM (object, i));
+          emit_py_result (item.get (), NULL);
+        }
     }
-  else if (PyIter_Check (result))
+  else if (PyIter_Check (object))
     {
       gdbpy_ref<> item;
       ui_out_emit_list list_emitter (uiout, field_name);
-      while (item.reset (PyIter_Next (result)), item != nullptr)
-	parse_mi_result (item.get (), NULL);
+      while (item.reset (PyIter_Next (object)), item != nullptr)
+        emit_py_result (item.get (), NULL);
     }
   else
     {
-      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (object));
       uiout->field_string (field_name, string.get ());
     }
 }
 
-/* Object initializer; sets up gdb-side structures for MI command.
+/* Object initializer; sets up gdb-side structures for an MI command.
 
    Use: __init__(NAME).
 
@@ -118,45 +124,34 @@ micmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
   if (!PyArg_ParseTuple (args, "s", &name))
     return -1;
 
-  /* Validate command name */
+  /* Validate command name.  */
   const int name_len = strlen (name);
   if (name_len == 0)
+    error (_("MI command name is empty."));
+
+  if (name_len == 1 || name[0] != '-' || !isalnum (name[1]))
+    error (_("MI command name does not start with '-'"
+             " followed by at least one letter or digit."));
+
+  for (int i = 2; i < name_len; i++)
     {
-      error (_("MI command name is empty."));
-      return -1;
-    }
-  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
-    {
-      error (_("MI command name does not start with '-'"
-               " followed by at least one letter or digit."));
-      return -1;
+      if (!isalnum (name[i]) && name[i] != '-')
+        error (_("MI command name contains invalid character: %c."), name[i]);
     }
-  else
-    for (int i = 2; i < name_len; i++)
-      {
-	if (!isalnum (name[i]) && name[i] != '-')
-	  {
-	    error (_("MI command name contains invalid character: %c."),
-                   name[i]);
-	    return -1;
-	  }
-      }
 
   if (!PyObject_HasAttr (self_ref.get (), invoke_cst))
-      error (_("-%s: Python command object missing 'invoke' method."), name);
+    error (_("-%s: Python command object missing 'invoke' method."), name);
 
   try
     {
-      mi_command_up micommand = mi_command_up(new mi_command_py (name + 1, self_ref));
+      mi_command_up micommand
+        = mi_command_up(new mi_command_py (name + 1, self_ref));
 
       bool result = insert_mi_cmd_entry (std::move (micommand));
 
       if (!result)
-	{
-	  error (_("Unable to insert command."
-                   "The name might already be in use."));
-	  return -1;
-	}
+        error (_("Unable to insert command. "
+                 "The name might already be in use."));
     }
   catch (const gdb_exception &except)
     {
@@ -168,12 +163,18 @@ micmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
 
 mi_command_py::mi_command_py (const char *name, gdbpy_ref<> object)
   : mi_command (NULL),
-    pyobj (object)
+    m_pyobj (object)
 {
   gdb_assert (name != nullptr && name[0] != '\0' && name[0] != '-');
   m_name = name;
 }
 
+bool
+mi_command_py::can_be_redefined()
+{
+  return true;
+}
+
 void
 mi_command_py::do_invoke (struct mi_parse *parse) const
 {
@@ -182,7 +183,12 @@ mi_command_py::do_invoke (struct mi_parse *parse) const
   if (parse->argv == NULL)
     error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
 
-  PyObject *obj = this->pyobj.get ();
+  /* Must save a copy of the name because the Python code may
+     replace the very same command that is currently executing.
+     See further below.  */
+  std::string name = this->name ();
+
+  PyObject *obj = this->m_pyobj.get ();
 
   gdbpy_enter enter_py (get_current_arch (), current_language);
 
@@ -190,29 +196,33 @@ mi_command_py::do_invoke (struct mi_parse *parse) const
 
   if (!PyObject_HasAttr (obj, invoke_cst))
       error (_("-%s: Python command object missing 'invoke' method."),
-	     name ());
-
+               name.c_str ());
 
   gdbpy_ref<> argobj (PyList_New (parse->argc));
   if (argobj == nullptr)
     {
       gdbpy_print_stack ();
       error (_("-%s: failed to create the Python arguments list."),
-	     name ());
+               name.c_str ());
     }
 
   for (int i = 0; i < parse->argc; ++i)
     {
       gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i], strlen (parse->argv[i]),
-					 host_charset (), NULL));
+                                         host_charset (), NULL));
       if (PyList_SetItem (argobj.get (), i, str.release ()) != 0)
-	{
-	  error (_("-%s: failed to create the Python arguments list."),
-		 name ());
-	}
+        {
+          error (_("-%s: failed to create the Python arguments list."),
+                   name.c_str ());
+        }
     }
 
   gdb_assert (PyErr_Occurred () == NULL);
+
+  /* From this point on, THIS must not be used since Python code may replace
+     the very same command that is currently executing.  This in turn leads
+     to destruction of THIS making it invalid.  See insert_mi_cmd_entry.  */
+
   gdbpy_ref<> result (
     PyObject_CallMethodObjArgs (obj, invoke_cst, argobj.get (), NULL));
   if (PyErr_Occurred () != NULL)
@@ -221,20 +231,21 @@ mi_command_py::do_invoke (struct mi_parse *parse) const
       gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
 
       if (ex_msg == NULL || *ex_msg == '\0')
-	error (_("-%s: failed to execute command"), name ());
+        error (_("-%s: failed to execute command"), name.c_str ());
       else
-	error (_("-%s: %s"), name (), ex_msg.get ());
+        error (_("-%s: %s"), name.c_str (), ex_msg.get ());
     }
   else
     {
       if (Py_None != result)
-	parse_mi_result (result.get (), "result");
+        emit_py_result (result.get (), "result");
     }
 }
 
-void mi_command_py::finalize ()
+void
+mi_command_py::finalize ()
 {
-  this->pyobj.reset (nullptr);
+  this->m_pyobj.reset (nullptr);
 }
 
 /* Initialize the MI command object.  */
@@ -247,7 +258,7 @@ gdbpy_initialize_micommands ()
     return -1;
 
   if (gdb_pymodule_addobject (gdb_module, "MICommand",
-			      (PyObject *) &micmdpy_object_type)
+                              (PyObject *) &micmdpy_object_type)
       < 0)
     return -1;
 
@@ -261,40 +272,41 @@ gdbpy_initialize_micommands ()
 static PyMethodDef micmdpy_object_methods[] = {{0}};
 
 PyTypeObject micmdpy_object_type = {
-  PyVarObject_HEAD_INIT (NULL, 0) "gdb.MICommand", /*tp_name */
-  sizeof (micmdpy_object),			   /*tp_basicsize */
-  0,						   /*tp_itemsize */
-  0,						   /*tp_dealloc */
-  0,						   /*tp_print */
-  0,						   /*tp_getattr */
-  0,						   /*tp_setattr */
-  0,						   /*tp_compare */
-  0,						   /*tp_repr */
-  0,						   /*tp_as_number */
-  0,						   /*tp_as_sequence */
-  0,						   /*tp_as_mapping */
-  0,						   /*tp_hash */
-  0,						   /*tp_call */
-  0,						   /*tp_str */
-  0,						   /*tp_getattro */
-  0,						   /*tp_setattro */
-  0,						   /*tp_as_buffer */
-  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
-  "GDB mi-command object",			   /* tp_doc */
-  0,						   /* tp_traverse */
-  0,						   /* tp_clear */
-  0,						   /* tp_richcompare */
-  0,						   /* tp_weaklistoffset */
-  0,						   /* tp_iter */
-  0,						   /* tp_iternext */
-  micmdpy_object_methods,			   /* tp_methods */
-  0,						   /* tp_members */
-  0,						   /* tp_getset */
-  0,						   /* tp_base */
-  0,						   /* tp_dict */
-  0,						   /* tp_descr_get */
-  0,						   /* tp_descr_set */
-  0,						   /* tp_dictoffset */
-  micmdpy_init,					   /* tp_init */
-  0,						   /* tp_alloc */
+  PyVarObject_HEAD_INIT (NULL, 0)
+  "gdb.MICommand",                                 /*tp_name */
+  sizeof (micmdpy_object),                         /*tp_basicsize */
+  0,                                               /*tp_itemsize */
+  0,                                               /*tp_dealloc */
+  0,                                               /*tp_print */
+  0,                                               /*tp_getattr */
+  0,                                               /*tp_setattr */
+  0,                                               /*tp_compare */
+  0,                                               /*tp_repr */
+  0,                                               /*tp_as_number */
+  0,                                               /*tp_as_sequence */
+  0,                                               /*tp_as_mapping */
+  0,                                               /*tp_hash */
+  0,                                               /*tp_call */
+  0,                                               /*tp_str */
+  0,                                               /*tp_getattro */
+  0,                                               /*tp_setattro */
+  0,                                               /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,     /*tp_flags */
+  "GDB mi-command object",                         /* tp_doc */
+  0,                                               /* tp_traverse */
+  0,                                               /* tp_clear */
+  0,                                               /* tp_richcompare */
+  0,                                               /* tp_weaklistoffset */
+  0,                                               /* tp_iter */
+  0,                                               /* tp_iternext */
+  micmdpy_object_methods,                          /* tp_methods */
+  0,                                               /* tp_members */
+  0,                                               /* tp_getset */
+  0,                                               /* tp_base */
+  0,                                               /* tp_dict */
+  0,                                               /* tp_descr_get */
+  0,                                               /* tp_descr_set */
+  0,                                               /* tp_dictoffset */
+  micmdpy_init,                                    /* tp_init */
+  0,                                               /* tp_alloc */
 };
diff --git a/gdb/python/py-micmd.h b/gdb/python/py-micmd.h
index b6b25351ac7..1c675a7c1fb 100644
--- a/gdb/python/py-micmd.h
+++ b/gdb/python/py-micmd.h
@@ -1,6 +1,6 @@
 /* MI Command Set for GDB, the GNU debugger.
 
-   Copyright (C) 2019 Free Software Foundation, Inc.
+   Copyright (C) 2019-2021 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -30,34 +30,37 @@ struct micmdpy_object
   PyObject_HEAD
 };
 
-typedef struct micmdpy_object micmdpy_object;
-
 /* MI command implemented in Python.  */
 
 class mi_command_py : public mi_command
 {
-  public:
-    /* Constructs a new mi_command_py object.  NAME is command name without
-       leading dash.  OBJECT is a reference to a Python object implementing
-       the command.  This object should inherit from gdb.MICommand and should
-       implement method invoke (args). */
-    mi_command_py (const char *name, gdbpy_ref<> object);
+public:
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object should inherit from gdb.MICommand and should
+     implement method invoke (args).  */
+  mi_command_py (const char *name, gdbpy_ref<> object);
 
+  bool can_be_redefined () override;
 
-    /* This is called just before shutting down a Python interpreter
-       to release python object implementing the command. */
-    void finalize ();
+  /* This is called just before shutting down the Python interpreter
+     to release python object implementing the command.  */
+  void finalize ();
 
-    virtual const char *name () const override
-    { return m_name.c_str(); }
+  virtual const char *name () const override
+  {
+    return m_name.c_str();
+  }
 
-  protected:
-    virtual void do_invoke(struct mi_parse *parse) const override;
+protected:
+  virtual void do_invoke(struct mi_parse *parse) const override;
 
-  private:
-    std::string m_name;
+private:
+  /* The name of the command.  */
+  std::string m_name;
 
-    gdbpy_ref<> pyobj;
+  /* Reference to Python object implementing the command.  */
+  gdbpy_ref<> m_pyobj;
 };
 
 #endif
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 6e2ed32bf90..f8af27704cd 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1701,7 +1701,7 @@ finalize_python (void *ignore)
   python_gdbarch = target_gdbarch ();
   python_language = current_language;
 
-  for (const auto& name_and_cmd : mi_cmd_table)
+  for (const auto &name_and_cmd : mi_cmd_table)
     {
       mi_command *cmd = name_and_cmd.second.get ();
       mi_command_py *cmd_py = dynamic_cast<mi_command_py*> (cmd);
-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH 4/5] gdb/testsuite: add tests for python-defined MI commands
  2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
                   ` (2 preceding siblings ...)
  2022-01-17 12:44 ` [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands Jan Vrany
@ 2022-01-17 12:44 ` Jan Vrany
  2022-01-17 12:44 ` [PATCH 5/5] gdb/python: document GDB/MI commands in Python Jan Vrany
  2022-01-18 13:55 ` [PATCH 0/5] create GDB/MI commands using python Andrew Burgess
  5 siblings, 0 replies; 41+ messages in thread
From: Jan Vrany @ 2022-01-17 12:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

---
 gdb/testsuite/gdb.python/py-mi-cmd.exp | 133 +++++++++++++++++++++++++
 gdb/testsuite/gdb.python/py-mi-cmd.py  |  68 +++++++++++++
 2 files changed, 201 insertions(+)
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.exp
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.py

diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..78baf5e97ff
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,133 @@
+# Copyright (C) 2019-2021 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi2"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+#
+# Start here
+#
+
+
+mi_gdb_test "set python print-stack full" \
+  ".*\\^done" \
+  "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+  ".*\\^done" \
+  "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+  ".*\\^done" \
+  "define -pycmd MI command"
+
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Hello world!\"" \
+  "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+  "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+  "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+  "\\^done,result={hello=\"world\",times=\"42\"}" \
+  "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+  "\\^error,msg=\"Non-string object used as key: Bad Key\\.\"" \
+  "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+  "\\^error,msg=\"Non-string object used as key: 1\\.\"" \
+  "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+  "\\^error,msg=\"Non-string object used as key: __repr__ returned non-string .*" \
+  "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+  "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+  "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+  "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+  "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+  "\\^done" \
+  "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+  "\\^done,result=\\\[\"None\"\\\]" \
+  "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: bogus\"" \
+  "-pycmd bogus"
+
+mi_gdb_test "-pycmd exp" \
+  "\\^error,msg=\"-pycmd: failed to execute command\"" \
+  "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+  ".*\\^done" \
+  "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Ciao!\"" \
+  "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: int\"" \
+  "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"-pycmd: Command redefined but we failing anyway\"" \
+  "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int - redefined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+  ".*\\^error,msg=\"MI command name is empty.\"" \
+  "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+  ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit\\.\"" \
+  "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+  ".*\\^error,msg=\"MI command name contains invalid character: @\\.\"" \
+  "invalid character in MI command name"
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..2f6ba2f8037
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2019-2021 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'int':
+            return 42
+        elif argv[0] == 'str':
+            return "Hello world!"
+        elif argv[0] == 'ary':
+            return [ 'Hello', 42 ]
+        elif argv[0] == "dct":
+            return { 'hello' : 'world', 'times' : 42}
+        elif argv[0] == "bk1":
+            return { BadKey() : 'world' }
+        elif argv[0] == "bk2":
+            return { 1 : 'world' }
+        elif argv[0] == "bk3":
+            return { ReallyBadKey() : 'world' }
+        elif argv[0] == 'tpl':
+            return ( 42 , 'Hello' )
+        elif argv[0] == 'itr':
+            return iter([1,2,3])
+        elif argv[0] == 'nn1':
+            return None
+        elif argv[0] == 'nn2':
+            return [ None ]
+        elif argv[0] == 'red':
+            pycmd2('-pycmd')
+            return None
+        elif argv[0] == 'exp':
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'str':
+            return "Ciao!"
+        elif argv[0] == 'red':
+            pycmd1('-pycmd')
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCH 5/5] gdb/python: document GDB/MI commands in Python
  2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
                   ` (3 preceding siblings ...)
  2022-01-17 12:44 ` [PATCH 4/5] gdb/testsuite: add tests for python-defined MI commands Jan Vrany
@ 2022-01-17 12:44 ` Jan Vrany
  2022-01-17 13:15   ` Eli Zaretskii
  2022-01-17 13:20   ` Eli Zaretskii
  2022-01-18 13:55 ` [PATCH 0/5] create GDB/MI commands using python Andrew Burgess
  5 siblings, 2 replies; 41+ messages in thread
From: Jan Vrany @ 2022-01-17 12:44 UTC (permalink / raw)
  To: gdb-patches; +Cc: Jan Vrany

---
 gdb/NEWS            |  2 ++
 gdb/doc/python.texi | 80 +++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 76 insertions(+), 6 deletions(-)

diff --git a/gdb/NEWS b/gdb/NEWS
index 8c13cefb22f..60f157b9c82 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -146,6 +146,8 @@ show debug lin-lwp
   ** New function gdb.host_charset(), returns a string, which is the
      name of the current host charset.
 
+  ** New GDB/MI commands can now be written in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 38fce5b38e3..1c17fc1fe2d 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -204,7 +204,8 @@ optional arguments while skipping others.  Example:
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -388,7 +389,8 @@ the current language, evaluate it, and return the result as a
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2105,7 +2107,7 @@ must contain the frames that are being elided wrapped in a suitable
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3809,9 +3811,10 @@ def countrange (filename, linerange):
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
+@cindex CLI commands in python
 @cindex commands in python
 @cindex python commands
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
@@ -4092,6 +4095,71 @@ registration of the command with @value{GDBN}.  Depending on how the
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python
+@cindex python commands
+You can implement new @sc{GDB/MI} (@pxref{GDB/MI}) commands in Python.  A @sc{GDB/MI}
+command is implemented using an instance of the @code{gdb.MICommand}
+class, most commonly using a subclass.
+
+@defun Command.__init__ (name)
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid @sc{GDB/MI}
+command and must start with hyphen (-).
+@end defun
+
+@defun Command.invoke (arguments)
+This method is called by @value{GDBN} when this command is invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method throws an exception, it is turned into a @sc{GDB/MI}
+@code{^error} response.  Otherwise, the return value is converted
+to @sc{GDB/MI} value (@pxref{GDB/MI Output Syntax}) as follows:
+
+@itemize
+@item If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @emph{list} with elements converted recursively.
+
+@item If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @emph{tuple}.  Keys in that dictionary must be strings.
+Values are converted recursively.
+
+@item Otherwise, value is first converted to Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @emph{const}.
+@end itemize
+
+@end defun
+
+The following code snippet shows how a trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo parameters"""
+
+    def __init__ (self):
+        super (MIEcho, self).__init__ ("-echo")
+
+    def invoke (self, argv):
+        return @{'argv' : argv @}
+
+MIEcho ()
+@end smallexample
+
+The last line instantiates the class, and is necessary to trigger the
+registration of the command with @value{GDBN}.  Depending on how the
+Python code is read into @value{GDBN}, you may need to import the
+@code{gdb} module explicitly.
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4129,7 +4197,7 @@ If @var{name} consists of multiple words, and no prefix parameter group
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
-- 
2.30.2


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 5/5] gdb/python: document GDB/MI commands in Python
  2022-01-17 12:44 ` [PATCH 5/5] gdb/python: document GDB/MI commands in Python Jan Vrany
@ 2022-01-17 13:15   ` Eli Zaretskii
  2022-01-17 13:20   ` Eli Zaretskii
  1 sibling, 0 replies; 41+ messages in thread
From: Eli Zaretskii @ 2022-01-17 13:15 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

> Date: Mon, 17 Jan 2022 12:44:25 +0000
> From: Jan Vrany via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Jan Vrany <jan.vrany@labware.com>
> 
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -146,6 +146,8 @@ show debug lin-lwp
>    ** New function gdb.host_charset(), returns a string, which is the
>       name of the current host charset.
>  
> +  ** New GDB/MI commands can now be written in Python.

The "new" part here took me by surprise.  Why do we need it?

Or how about this rewording:

  ** It is now possible to add GDB/MI commands implemented in Python.

> +* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
                                                    ^^^^^^^^^^^
The argument of @sc should be in lower case.

> +@cindex CLI commands in python
>  @cindex commands in python
>  @cindex python commands
>  You can implement new @value{GDBN} CLI commands in Python.  A CLI
> @@ -4092,6 +4095,71 @@ registration of the command with @value{GDBN}.  Depending on how the
>  Python code is read into @value{GDBN}, you may need to import the
>  @code{gdb} module explicitly.
>  
> +@node GDB/MI Commands In Python
> +@subsubsection @sc{GDB/MI} Commands In Python
> +
> +@cindex MI commands in python
> +@cindex commands in python
> +@cindex python commands

You've added a second copy of the last two index entries, without any
qualifier.  This will cause makeinfo to generate index entries
"commands in python" and "commands is python<2>", without giving the
reader any clue what is the difference between these two instances.

It is much better to qualify each instance with some unique
qualifier.  Here, I'd use ", CLI" and ", GDB/MI" as the qualifiers.

> +You can implement new @sc{GDB/MI} (@pxref{GDB/MI}) commands in Python.

Same comment as for the NEWS entry.  With a possibly similar solution.

> +@var{name} is the name of the command.  It must be a valid @sc{GDB/MI}
> +command and must start with hyphen (-).

What does "It must be a valid @sc{GDB/MI} command" mean here?  Did you
mean to say "It must be a valid @sc{GDB/MI} command name" instead?  If
so, I suggest

  It must be a valid name of a @sc{GDB/MI} command, and in particular
  must start with a hyphen (

> +@end defun
> +
> +@defun Command.invoke (arguments)
> +This method is called by @value{GDBN} when this command is invoked.
> +
> +@var{arguments} is a list of strings.  Note, that @code{--thread}
> +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
> +they do not show up in @code{arguments}.
> +
> +If this method throws an exception, it is turned into a @sc{GDB/MI}
> +@code{^error} response.  Otherwise, the return value is converted
> +to @sc{GDB/MI} value (@pxref{GDB/MI Output Syntax}) as follows:
> +
> +@itemize
> +@item If the value is Python sequence or iterator, it is converted to
> +@sc{GDB/MI} @emph{list} with elements converted recursively.
> +
> +@item If the value is Python dictionary, it is converted to
> +@sc{GDB/MI} @emph{tuple}.  Keys in that dictionary must be strings.
> +Values are converted recursively.
> +
> +@item Otherwise, value is first converted to Python string using
> +@code{str ()} and then converted to @sc{GDB/MI} @emph{const}.
> +@end itemize
> +
> +@end defun
> +
> +The following code snippet shows how a trivial MI command can be
> +implemented in Python:
> +
> +@smallexample
> +class MIEcho(gdb.MICommand):
> +    """Echo parameters"""
> +
> +    def __init__ (self):
> +        super (MIEcho, self).__init__ ("-echo")
> +
> +    def invoke (self, argv):
> +        return @{'argv' : argv @}
> +
> +MIEcho ()
> +@end smallexample
> +
> +The last line instantiates the class, and is necessary to trigger the
> +registration of the command with @value{GDBN}.  Depending on how the
> +Python code is read into @value{GDBN}, you may need to import the
> +@code{gdb} module explicitly.
> +
>  @node Parameters In Python
>  @subsubsection Parameters In Python
>  
> @@ -4129,7 +4197,7 @@ If @var{name} consists of multiple words, and no prefix parameter group
>  can be found, an exception is raised.
>  
>  @var{command-class} should be one of the @samp{COMMAND_} constants
> -(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
> +(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
>  categorize the new parameter in the help system.
>  
>  @var{parameter-class} should be one of the @samp{PARAM_} constants
> -- 
> 2.30.2
> 
> 

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 5/5] gdb/python: document GDB/MI commands in Python
  2022-01-17 12:44 ` [PATCH 5/5] gdb/python: document GDB/MI commands in Python Jan Vrany
  2022-01-17 13:15   ` Eli Zaretskii
@ 2022-01-17 13:20   ` Eli Zaretskii
  2022-01-18 12:34     ` Jan Vrany
  1 sibling, 1 reply; 41+ messages in thread
From: Eli Zaretskii @ 2022-01-17 13:20 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

[Please disregard the previous email, which was mistakenly sent too early.]

> Date: Mon, 17 Jan 2022 12:44:25 +0000
> From: Jan Vrany via Gdb-patches <gdb-patches@sourceware.org>
> Cc: Jan Vrany <jan.vrany@labware.com>
> 
> --- a/gdb/NEWS
> +++ b/gdb/NEWS
> @@ -146,6 +146,8 @@ show debug lin-lwp
>    ** New function gdb.host_charset(), returns a string, which is the
>       name of the current host charset.
>  
> +  ** New GDB/MI commands can now be written in Python.

The "new" part here took me by surprise.  Why do we need it?

Or how about this rewording:

  ** It is now possible to add GDB/MI commands implemented in Python.

> +* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
                                                    ^^^^^^^^^^^
The argument of @sc should be in lower case.

> +@cindex CLI commands in python
>  @cindex commands in python
>  @cindex python commands
>  You can implement new @value{GDBN} CLI commands in Python.  A CLI
> @@ -4092,6 +4095,71 @@ registration of the command with @value{GDBN}.  Depending on how the
>  Python code is read into @value{GDBN}, you may need to import the
>  @code{gdb} module explicitly.
>  
> +@node GDB/MI Commands In Python
> +@subsubsection @sc{GDB/MI} Commands In Python
> +
> +@cindex MI commands in python
> +@cindex commands in python
> +@cindex python commands

You've added a second copy of the last two index entries, without any
qualifier.  This will cause makeinfo to generate index entries
"commands in python" and "commands is python<2>", without giving the
reader any clue what is the difference between these two instances.

It is much better to qualify each instance with some unique
qualifier.  Here, I'd use ", CLI" and ", GDB/MI" as the qualifiers.

> +You can implement new @sc{GDB/MI} (@pxref{GDB/MI}) commands in Python.

Same comment as for the NEWS entry.  With a possibly similar solution.

> +@var{name} is the name of the command.  It must be a valid @sc{GDB/MI}
> +command and must start with hyphen (-).

What does "It must be a valid @sc{GDB/MI} command" mean here?  Did you
mean to say "It must be a valid @sc{GDB/MI} command name" instead?  If
so, I suggest

  It must be a valid name of a @sc{GDB/MI} command, and in particular
  must start with a hyphen (@samp{-}).

> +@var{arguments} is a list of strings.  Note, that @code{--thread}
> +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
> +they do not show up in @code{arguments}.                       ^

Comma missing there.

> +If this method throws an exception, it is turned into a @sc{GDB/MI}

"it" here is ambiguous: does it mean the exception or does it mean the
method?  Suggest to be more explicit:

  If this method throws an exception, the exception is turned into a
  @sc{GDB/MI} @code{^error} response.

> +@itemize
> +@item If the value is Python sequence or iterator, it is converted to
> +@sc{GDB/MI} @emph{list} with elements converted recursively.

In an @itemize list, eacg @item should be alone on its line (unlike in
a @table).

> +@item If the value is Python dictionary, it is converted to
> +@sc{GDB/MI} @emph{tuple}.

I don't think using @emph here is a good idea.  I think @code is
better.  @mph will look odd in Info.

Thanks.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 5/5] gdb/python: document GDB/MI commands in Python
  2022-01-17 13:20   ` Eli Zaretskii
@ 2022-01-18 12:34     ` Jan Vrany
  2022-01-18 15:09       ` Eli Zaretskii
  0 siblings, 1 reply; 41+ messages in thread
From: Jan Vrany @ 2022-01-18 12:34 UTC (permalink / raw)
  To: gdb-patches

Hi, 
> 
> > Date: Mon, 17 Jan 2022 12:44:25 +0000
> > From: Jan Vrany via Gdb-patches <gdb-patches@sourceware.org>
> > Cc: Jan Vrany <jan.vrany@labware.com>
> > 
> > --- a/gdb/NEWS
> > +++ b/gdb/NEWS
> > @@ -146,6 +146,8 @@ show debug lin-lwp
> >    ** New function gdb.host_charset(), returns a string, which is the
> >       name of the current host charset.
> >  
> > 
> > 
> > 
> > +  ** New GDB/MI commands can now be written in Python.
> 
> The "new" part here took me by surprise.  Why do we need it?
> 
> Or how about this rewording:
> 
>   ** It is now possible to add GDB/MI commands implemented in Python.
> 
> > +* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
>                                                     ^^^^^^^^^^^
> The argument of @sc should be in lower case.
> 
> > +@cindex CLI commands in python
> >  @cindex commands in python
> >  @cindex python commands
> >  You can implement new @value{GDBN} CLI commands in Python.  A CLI
> > @@ -4092,6 +4095,71 @@ registration of the command with @value{GDBN}.  Depending on how the
> >  Python code is read into @value{GDBN}, you may need to import the
> >  @code{gdb} module explicitly.
> >  
> > 
> > 
> > 
> > 
> > 
> > 
> > 
> > +@node GDB/MI Commands In Python
> > +@subsubsection @sc{GDB/MI} Commands In Python
> > +
> > +@cindex MI commands in python
> > +@cindex commands in python
> > +@cindex python commands
> 
> You've added a second copy of the last two index entries, without any
> qualifier.  This will cause makeinfo to generate index entries
> "commands in python" and "commands is python<2>", without giving the
> reader any clue what is the difference between these two instances.
> 
> It is much better to qualify each instance with some unique
> qualifier.  Here, I'd use ", CLI" and ", GDB/MI" as the qualifiers.

I see. However I'm not sure what do you mean by "qualifiers". 
In new version (which I'll submit  once I get review on
other patches in this series) I changed index entries to 

@node CLI Commands In Python
@subsubsection CLI Commands In Python

@cindex commands in python @subentry CLI
@cindex python commands @subentry CLI

and then 

@node GDB/MI Commands In Python
@subsubsection @sc{gdb/mi} Commands In Python

@cindex commands in python @subentry @sc{gdb/mi}
@cindex python commands @subentry @sc{gdb/mi}

It looks good to me in HTML version. 

Is that what you meant? It seems that this is the first use
of @subentry in @cindex in GDB manual so perhaps you meant
something different? 


> 
> > +You can implement new @sc{GDB/MI} (@pxref{GDB/MI}) commands in Python.
> 
> Same comment as for the NEWS entry.  With a possibly similar solution.

This phrasing with "new" (both here and in NEWS) was taken from entries
for CLI commands. For NEWS, I have changed it as you suggested, it's better.

But for manual it makes less sense to me. Wording "It is now possible to add GDB/MI 
commands implemented in Python." sounds to me as to suggest this is some
new feature. It makes sense for NEWS but perhaps might look bit weird after
5 years (given that this series gets in) when this is no new feature. 

Maybe I'm misunderstanding something...

> 
> > +@var{name} is the name of the command.  It must be a valid @sc{GDB/MI}
> > +command and must start with hyphen (-).
> 
> What does "It must be a valid @sc{GDB/MI} command" mean here?  Did you
> mean to say "It must be a valid @sc{GDB/MI} command name" instead?  If
> so, I suggest
> 
>   It must be a valid name of a @sc{GDB/MI} command, and in particular
>   must start with a hyphen (@samp{-}).
> 
> > +@var{arguments} is a list of strings.  Note, that @code{--thread}
> > +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
> > +they do not show up in @code{arguments}.                       ^
> 
> Comma missing there.
> 
> > +If this method throws an exception, it is turned into a @sc{GDB/MI}
> 
> "it" here is ambiguous: does it mean the exception or does it mean the
> method?  Suggest to be more explicit:
> 
>   If this method throws an exception, the exception is turned into a
>   @sc{GDB/MI} @code{^error} response.
> 
> > +@itemize
> > +@item If the value is Python sequence or iterator, it is converted to
> > +@sc{GDB/MI} @emph{list} with elements converted recursively.
> 
> In an @itemize list, eacg @item should be alone on its line (unlike in
> a @table).
> 
> > +@item If the value is Python dictionary, it is converted to
> > +@sc{GDB/MI} @emph{tuple}.
> 
> I don't think using @emph here is a good idea.  I think @code is
> better.  @mph will look odd in Info.
> 
> 

I changed all the rest as suggested.

Thanks! Jan


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 0/5] create GDB/MI commands using python
  2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
                   ` (4 preceding siblings ...)
  2022-01-17 12:44 ` [PATCH 5/5] gdb/python: document GDB/MI commands in Python Jan Vrany
@ 2022-01-18 13:55 ` Andrew Burgess
  2022-01-18 15:13   ` Jan Vrany
  5 siblings, 1 reply; 41+ messages in thread
From: Andrew Burgess @ 2022-01-18 13:55 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

* Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-17 12:44:20 +0000]:

> This is a restart of an earlier attempts to allow custom
> GDB/MI commands written in Python.

Thanks for continuing to work on this feature.

I too had been looking at getting the remaining patches from your
series upstream, and I'd like to discuss some of the differences
between the approaches we took.

At the end of this mail you'll find my current work-in-progress
patch, it definitely needs the docs and NEWS entries adding, as well
as a few extra tests.  However, functionality wise I think its mostly
there.

My patch includes two sets of tests, gdb.python/py-mi-cmd.exp and
gdb.python/py-mi-cmd-orig.exp.  The former is based on your tests, but
with some tweaks based on changes I made.  The latter set is your
tests taken from this m/l thread.

When running the gdb.python/py-mi-cmd-orig.exp tests there should be
just 2 failures, both related to the same feature which is not present
in my version, that is, the ability of a command to redefine itself,
like this:

      class my_command(gdb.MICommand):
        def invoke(self, args):
          my_command("-blah")
          return None

       my_command("-blah")

this works with your version, but not with mine, this is because I'm
using python's own reference counting to track when a command can be
redefined or not, and, when you are within a commands invoke method
the reference count of the containing object is incremented, and this
prevents gdb from deleting the command.

My question then, is how important is this feature, and what use case
do you see for this?  Or, was support for this just a happy side
effect of the implementation approach you chose?

Thanks,
Andrew

---

commit 0ef6ec4fbf237a89697c3e6a16888d57f4e8b777
Author: Jan Vrany <jan.vrany@labware.com>
Date:   Tue Jun 23 14:45:38 2020 +0100

    [WIP] gdb: create MI commands using python
    
    This commit allows an user to create custom MI commands using Python
    similarly to what is possible for Python CLI commands.
    
    A new subclass of mi_command is defined for Python MI commands,
    mi_command_py. A new file, py-micmd.c contains the logic for Python MI
    commands.
    
    This commit is based on work linked too from this mailing list thread:
    
      https://sourceware.org/pipermail/gdb/2021-November/049774.html
    
    Which has also been previously posted to the mailing list here:
    
      https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html
    
    And was recently reposted here:
    
      https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html
    
    I've made some adjustments to how this feature is implemented though.
    
    One notable change is how the lifetime of the Python gdb.MICommand
    objects is managed.  In the original patch, these object were kept
    alive by an owned reference within the mi_command_py object.  As such,
    the Python object would not be deleted until the mi_command_py object
    itself was deleted.
    
    This caused a problem, the mi_command_py were held in the global mi
    command table (in mi/mi-cmds.c), which, as a global, was not cleared
    until program shutdown.  By this point the Python interpreter has
    already been shutdown.  Attempting to delete the mi_command_py object
    at this point was cause GDB to try and invoke Python code after
    finalising the Python interpreter, and we would crash.
    
    To work around this problem, the original patch added code in
    python/python.c that would search the mi command table, and delete the
    mi_command_py objects before the Python environment was finalised.
    
    I didn't like this, it means making the mi command table global, and
    having the python.c code understand about mi commands.
    
    In my version, I store the mi commands in a dictionary in the gdb
    module.  It is this dictionary that holds the reference to the Python
    object.  When the Python environment is finalised the dictionary is
    deleted, reducing the reference count on the Python objects within,
    this in turn allows the mi command objects to be deleted.
    
    I then make it so that when a Python MICommand object is deleted, the
    corresponding mi command is automatically deleted from the mi command
    table, removing that mi command.
    
    A second change I made is to handle the case where an object is
    reinitialised, consider this code:
    
      class my_command(gdb.MICommand):
        def invoke(self, args):
          # ...
          return None
    
      cmd = my_command("-my-command")
      cmd.__init__("-changed-the-name")
    
    Though strange, this is perfectly valid Python code, this is now
    handled correctly, the old `-my-command` will be removed, and the new
    `-changed-the-name` command will be added.
    
    A third change is how command redefinition is handled.  The old series
    required special support to allow command redefinition.  Now we get
    almost the same functionality, naturally.
    
    Consider:
    
      class command_one(gdb.MICommand):
        def invoke(self, args):
          # ...
          return None
    
      class command_two(gdb.MICommand):
        def invoke(self, args):
          # ...
          return None
    
      command_one("-my-command")
      command_two("-my-command")
    
    This will now do what (I hope) you'd expect.  There will be a single
    mi command `-my-command` defined, that calls `command_two.invoke`.
    
    However, if using the same class definitions, we try this instead:
    
      c1 = command_one("-my-command")
      c2 = command_two("-my-command")
    
    The second line will now fail.  The reason here is that `c1` is still
    holding a reference to the `command_one` object, and this prevents
    `c2` from taking that command name.  If we do this:
    
      c1 = command_one("-my-command")
      c1 = None
      c2 = command_two("-my-command")
    
    Now we are again fine, and c2 will be correctly initialised.  This
    feels like a bit of an edge case, but it's something that needs
    figuring out.  A different possibility would be to always allow `c2`
    to be created.  In the first case, creating `c2` would invalidate
    `c1`.  We would then have an "is_valid" method on gdb.MICommand
    object, so we could write something like:
    
      c1 = command_one("-my-command")
      c2 = command_two("-my-command")
      if c1.is_valid ():
        c1.invoke([])
    
    I considered this, but rejected it.  I believe that in most cases
    users will not be assigning the created MICommand objects to a
    variable, as it doesn't really serve any purpose.
    
    A fourth change is how the memory is handled for the name of the
    python mi command.  In the recently posted series the mi_command class
    has the name removed, and the name accessor is now virtual.  It is
    then up to the sub-classes to correctly handle the memory.  Instead of
    this, I just made the Python object manage the memory.  As the Python
    object always outlives the mi_command object the mi_command object can
    simply refer to the memory within the Python object.  This avoids
    having to add more complexity to the mi_command class hierarchy.
    
    I kept the test set from the original patch, and, almost all of the
    original tests pass with my new series.  There was however, one
    feature from the original series that I decided to drop, as supporting
    it within the new scheme would have added significant complexity, for
    what I consider, very little benefit.  That feature was for allowing
    MI commands to redefine themselves, something like:
    
      class my_command(gdb.MICommand):
        def invoke(self, args):
          my_command("-blah")
          return None
    
       my_command("-blah")
    
    This would work under the old scheme, and was tested for, but, will no
    longer work.  The reason this fails is that while invoking a command,
    the invoke method itself holds a reference to the command object.  As
    a result, this turns out to be similar to the first c1/c2 case posted
    above, the my_command object is referenced from both the global
    dictionary (in the gdb module), and from the running invoke method.
    When attempting to redefine the command GDB understands that it can
    reduce the reference count by removing the entry from the dictionary
    in the gdb module, but the extra reference prevents GDB from deleting
    the previous my_command object.
    
    todo:
    =====
    
    I'm still not 100% sure if I was correct to reject the is_valid method
    approach, maybe that would be a more natural way for the API to work?

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index d0db5fbdee1..5cb428459c3 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..dd0243e5bfe 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -113,12 +109,12 @@ struct mi_command_cli : public mi_command
    not have been added to mi_cmd_table.  Otherwise, return true, and
    COMMAND was added to mi_cmd_table.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
 
-  const std::string &name = command->name ();
+  const std::string name (command->name ());
 
   if (mi_cmd_table.find (name) != mi_cmd_table.end ())
     return false;
@@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+bool
+remove_mi_cmd_entry (mi_command *command)
+{
+  gdb_assert (command != nullptr);
+
+  const std::string name (command->name ());
+
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..3f4fb854d68 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the MI_CMD_TABLE.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,15 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert a new mi-command into the command table.  Return true if
+   insertion was successful.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove a mi-command from the command table.  Return true if the removal
+   was success, otherwise return false.  */
+
+extern bool remove_mi_cmd_entry (mi_command *command);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 11a1b444bfd..a9197eb4ffe 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -81,6 +81,9 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Hash containing all user created MI commands, the key is the command
+# name, and the value is the gdb.MICommand object.
+mi_commands = {}
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..042ba624f06
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,445 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* gdb MI commands implemented in Python  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+
+#include <string>
+
+struct mi_command_py;
+
+/* Representation of a python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the MI command table.  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing this command name.  This string is referenced
+     from the mi_command_py object, and must be free'd once the
+     mi_command_py object is no longer needed.  */
+  char *command_name;
+};
+
+/* MI command implemented in Python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object should inherit from gdb.MICommand and should
+     implement method invoke (args). */
+
+  mi_command_py (const char *name, PyObject *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  { /* Nothing.  */ }
+
+  ~mi_command_py () = default;
+
+protected:
+  /* Called when the mi command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The python object representing this mi command.  */
+  PyObject *m_pyobj;
+};
+
+static PyObject *invoke_cst;
+
+extern PyTypeObject micmdpy_object_type
+    CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* If the command invoked returns a list, this function parses it and create an
+   appropriate MI out output.
+
+   The returned values must be Python string, and can be contained within Python
+   lists and dictionaries. It is possible to have a multiple levels of lists
+   and/or dictionaries.  */
+
+static void
+parse_mi_result (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  if (!PyString_Check (key))
+	    {
+	      gdbpy_ref<> key_repr (PyObject_Repr (key));
+	      if (PyErr_Occurred () != NULL)
+                {
+                  gdbpy_err_fetch ex;
+                  gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
+
+                  if (ex_msg == NULL || *ex_msg == '\0')
+                    error (_("Non-string object used as key."));
+                  else
+                    error (_("Non-string object used as key: %s."),
+                           ex_msg.get ());
+                }
+              else
+	        {
+	          auto key_repr_string
+	                 = python_string_to_target_string (key_repr.get ());
+	          error (_("Non-string object used as key: %s."),
+                         key_repr_string.get ());
+	        }
+	    }
+
+	  auto key_string = python_string_to_target_string (key);
+	  parse_mi_result (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      for (Py_ssize_t i = 0; i < PySequence_Size (result); ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  parse_mi_result (item.get (), NULL);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (item.reset (PyIter_Next (result)), item != nullptr)
+	parse_mi_result (item.get (), NULL);
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Object initializer; sets up gdb-side structures for MI command.
+
+   Use: __init__(NAME).
+
+   NAME is the name of the MI command to register.  It must start with a dash
+   as traditional MI commands do.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
+{
+  const char *name;
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  if (!PyArg_ParseTuple (args, "s", &name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      error (_("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      error (_("MI command name does not start with '-'"
+               " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    for (int i = 2; i < name_len; i++)
+      {
+	if (!isalnum (name[i]) && name[i] != '-')
+	  {
+	    error (_("MI command name contains invalid character: %c."),
+                   name[i]);
+	    return -1;
+	  }
+      }
+
+  if (!PyObject_HasAttr (self, invoke_cst))
+    error (_("-%s: Python command object missing 'invoke' method."), name);
+
+  /* Now insert the object into the dictionary that lives in the gdb module.  */
+  if (gdb_python_module == nullptr
+      || ! PyObject_HasAttrString (gdb_python_module, "mi_commands"))
+    error (_("unable to find gdb.mi_commands dictionary"));
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "mi_commands"));
+  if (mi_cmd_dict == nullptr || !PyDict_Check (mi_cmd_dict.get ()))
+    error (_("unable to fetch gdb.mi_commands dictionary"));
+
+  /* Is the user re-initialising an existing gdb.MICommand object?  */
+  if (cmd->mi_command != nullptr)
+    {
+      /* Grab the old name for this command.  */
+      mi_command *old_cmd = cmd->mi_command;
+      gdbpy_ref<> old_name_obj
+	= host_string_to_python_string (old_cmd->name ());
+
+      /* Lookup the gdb.MICommand object in the dictionary of all python mi
+	 commands, this is gdb.mi_command, and remove it.  */
+      PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+						old_name_obj.get ());
+      if (curr == nullptr && PyErr_Occurred ())
+	return -1;
+      if (curr != nullptr)
+	{
+	  /* The old command is in gdb.mi_commands, so remove it.  */
+	  if (PyDict_DelItem (mi_cmd_dict.get (), old_name_obj.get ()) < 0)
+	    return -1;
+	}
+
+      /* Now remove the old command from the gdb internal table of mi
+	 commands.  */
+      remove_mi_cmd_entry (cmd->mi_command);
+
+      /* And reset this object back to a default state.  */
+      cmd->mi_command = nullptr;
+      xfree (cmd->command_name);
+      cmd->command_name = nullptr;
+    }
+
+  /* Create the gdb internal object to represent the mi command.  */
+  gdb::unique_xmalloc_ptr<char> name_up = make_unique_xstrdup (name + 1);
+  mi_command_py *tmp_cmd = new mi_command_py (name_up.get (), self);
+  cmd->mi_command = tmp_cmd;
+  cmd->command_name = name_up.release ();
+  mi_command_up micommand (tmp_cmd);
+
+  /* Look up this command name in the gdb.mi_commands dictionary, a command
+     with this name may already exist.  */
+  gdbpy_ref<> name_obj = host_string_to_python_string (cmd->command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb.mi_commands
+	 dictionary.  If the reference in the dictionary is the only
+	 reference, then we allow the user to replace this with a new
+	 command.  If, however, there are other references to this mi
+	 command object, then the user will get an error.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb.mi_commands dictionary"));
+	  return -1;
+	}
+
+      if (Py_REFCNT (curr) > 1)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  /* Add the command to the gdb internal mi command table.  */
+  bool result = insert_mi_cmd_entry (std::move (micommand));
+  if (!result)
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("unable to add command, name may already be in use"));
+      return -1;
+    }
+
+  /* And add the python object to the gdb.mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), self) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+  mi_command *py_cmd = cmd->mi_command;
+
+  /* There might not be an mi_command object if the object failed to
+     initialize correctly for some reason.  If there is though, then now is
+     the time we remove the object from the gdb internal command table.  */
+  if (py_cmd != nullptr)
+    remove_mi_cmd_entry (py_cmd);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->command_name);
+
+  /* Finally, free the memory for this python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Called when the mi command is invoked.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == NULL)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = this->m_pyobj;
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  gdb_assert (obj != nullptr);
+
+  if (!PyObject_HasAttr (obj, invoke_cst))
+      error (_("-%s: Python command object missing 'invoke' method."),
+	     name ());
+
+
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    {
+      gdbpy_print_stack ();
+      error (_("-%s: failed to create the Python arguments list."),
+	     name ());
+    }
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i], strlen (parse->argv[i]),
+					 host_charset (), NULL));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) != 0)
+	{
+	  error (_("-%s: failed to create the Python arguments list."),
+		 name ());
+	}
+    }
+
+  gdb_assert (PyErr_Occurred () == NULL);
+  gdbpy_ref<> result (
+    PyObject_CallMethodObjArgs (obj, invoke_cst, argobj.get (), NULL));
+  if (PyErr_Occurred () != NULL)
+    {
+      gdbpy_err_fetch ex;
+      gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
+
+      if (ex_msg == NULL || *ex_msg == '\0')
+	error (_("-%s: failed to execute command"), name ());
+      else
+	error (_("-%s: %s"), name (), ex_msg.get ());
+    }
+  else
+    {
+      if (Py_None != result)
+	parse_mi_result (result.get (), "result");
+    }
+}
+
+/* Initialize the MI command object.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == NULL)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.name attribute, return a string, the name of
+   this MI command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->command_name != nullptr);
+  return PyString_FromString (micmd_obj->command_name);
+}
+
+/* gdb.MICommand attributes.   */
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (NULL, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 583989c5a6d..d5a0b4a4a91 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -561,6 +561,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4dcda53d9ab..f2b09916374 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1887,7 +1887,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp b/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp
new file mode 100644
index 00000000000..78baf5e97ff
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp
@@ -0,0 +1,133 @@
+# Copyright (C) 2019-2021 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi2"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+#
+# Start here
+#
+
+
+mi_gdb_test "set python print-stack full" \
+  ".*\\^done" \
+  "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+  ".*\\^done" \
+  "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+  ".*\\^done" \
+  "define -pycmd MI command"
+
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Hello world!\"" \
+  "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+  "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+  "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+  "\\^done,result={hello=\"world\",times=\"42\"}" \
+  "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+  "\\^error,msg=\"Non-string object used as key: Bad Key\\.\"" \
+  "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+  "\\^error,msg=\"Non-string object used as key: 1\\.\"" \
+  "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+  "\\^error,msg=\"Non-string object used as key: __repr__ returned non-string .*" \
+  "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+  "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+  "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+  "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+  "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+  "\\^done" \
+  "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+  "\\^done,result=\\\[\"None\"\\\]" \
+  "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: bogus\"" \
+  "-pycmd bogus"
+
+mi_gdb_test "-pycmd exp" \
+  "\\^error,msg=\"-pycmd: failed to execute command\"" \
+  "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+  ".*\\^done" \
+  "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Ciao!\"" \
+  "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: int\"" \
+  "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"-pycmd: Command redefined but we failing anyway\"" \
+  "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int - redefined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+  ".*\\^error,msg=\"MI command name is empty.\"" \
+  "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+  ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit\\.\"" \
+  "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+  ".*\\^error,msg=\"MI command name contains invalid character: @\\.\"" \
+  "invalid character in MI command name"
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd-orig.py b/gdb/testsuite/gdb.python/py-mi-cmd-orig.py
new file mode 100644
index 00000000000..2f6ba2f8037
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd-orig.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2019-2021 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'int':
+            return 42
+        elif argv[0] == 'str':
+            return "Hello world!"
+        elif argv[0] == 'ary':
+            return [ 'Hello', 42 ]
+        elif argv[0] == "dct":
+            return { 'hello' : 'world', 'times' : 42}
+        elif argv[0] == "bk1":
+            return { BadKey() : 'world' }
+        elif argv[0] == "bk2":
+            return { 1 : 'world' }
+        elif argv[0] == "bk3":
+            return { ReallyBadKey() : 'world' }
+        elif argv[0] == 'tpl':
+            return ( 42 , 'Hello' )
+        elif argv[0] == 'itr':
+            return iter([1,2,3])
+        elif argv[0] == 'nn1':
+            return None
+        elif argv[0] == 'nn2':
+            return [ None ]
+        elif argv[0] == 'red':
+            pycmd2('-pycmd')
+            return None
+        elif argv[0] == 'exp':
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'str':
+            return "Ciao!"
+        elif argv[0] == 'red':
+            pycmd1('-pycmd')
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..55a4d821351
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,136 @@
+# Copyright (C) 2018 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi2"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+#
+# Start here
+#
+
+
+mi_gdb_test "set python print-stack full" \
+  ".*\\^done" \
+  "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+  ".*\\^done" \
+  "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+  ".*\\^done" \
+  "Define -pycmd MI command"
+
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Hello world!\"" \
+  "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+  "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+  "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+  "\\^done,result={hello=\"world\",times=\"42\"}" \
+  "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+  "\\^error,msg=\"Non-string object used as key: Bad Kay.\"" \
+  "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+  "\\^error,msg=\"Non-string object used as key: 1.\"" \
+  "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+  "\\^error,msg=\"Non-string object used as key: __repr__ returned non-string .*" \
+  "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+  "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+  "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+  "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+  "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+  "\\^done" \
+  "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+  "\\^done,result=\\\[\"None\"\\\]" \
+  "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: bogus\"" \
+  "-pycmd bogus"
+
+mi_gdb_test "-pycmd exp" \
+  "\\^error,msg=\"-pycmd: failed to execute command\"" \
+  "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+  ".*\\^done" \
+  "Redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Ciao!\"" \
+  "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: int\"" \
+  "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"-pycmd: unable to add command, name may already be in use\"" \
+  "Redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+  "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd-new int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int - redefined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+  ".*\\^error,msg=\"MI command name is empty.\"" \
+  "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+  ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit.\"" \
+  "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+  ".*\\^error,msg=\"MI command name contains invalid character: @.\"" \
+  "invalid character in MI command name"
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..fd09d8dca00
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,57 @@
+import gdb
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Kay"
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'int':
+            return 42
+        elif argv[0] == 'str':
+            return "Hello world!"
+        elif argv[0] == 'ary':
+            return [ 'Hello', 42 ]
+        elif argv[0] == "dct":
+            return { 'hello' : 'world', 'times' : 42}
+        elif argv[0] == "bk1":
+            return { BadKey() : 'world' }
+        elif argv[0] == "bk2":
+            return { 1 : 'world' }
+        elif argv[0] == "bk3":
+            return { ReallyBadKey() : 'world' }
+        elif argv[0] == 'tpl':
+            return ( 42 , 'Hello' )
+        elif argv[0] == 'itr':
+            return iter([1,2,3])
+        elif argv[0] == 'nn1':
+            return None
+        elif argv[0] == 'nn2':
+            return [ None ]
+        elif argv[0] == 'red':
+            pycmd2('-pycmd')
+            return None
+        elif argv[0] == 'exp':
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'str':
+            return "Ciao!"
+        elif argv[0] == 'red':
+            pycmd1('-pycmd')
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == 'new':
+            pycmd1('-pycmd-new')
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 5/5] gdb/python: document GDB/MI commands in Python
  2022-01-18 12:34     ` Jan Vrany
@ 2022-01-18 15:09       ` Eli Zaretskii
  0 siblings, 0 replies; 41+ messages in thread
From: Eli Zaretskii @ 2022-01-18 15:09 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

> From: Jan Vrany <jan.vrany@labware.com>
> Cc: Eli Zaretskii <eliz@gnu.org>
> Date: Tue, 18 Jan 2022 12:34:00 +0000
> 
> > You've added a second copy of the last two index entries, without any
> > qualifier.  This will cause makeinfo to generate index entries
> > "commands in python" and "commands is python<2>", without giving the
> > reader any clue what is the difference between these two instances.
> > 
> > It is much better to qualify each instance with some unique
> > qualifier.  Here, I'd use ", CLI" and ", GDB/MI" as the qualifiers.
> 
> I see. However I'm not sure what do you mean by "qualifiers". 
> In new version (which I'll submit  once I get review on
> other patches in this series) I changed index entries to 
> 
> @node CLI Commands In Python
> @subsubsection CLI Commands In Python
> 
> @cindex commands in python @subentry CLI
> @cindex python commands @subentry CLI

@subentry is a very new feature, so I'd prefer not to use it yet, as
that would force everyone who builds GDB to upgrade.

Something much simpler, like this, will do:

  @cindex commands in python, in CLI
  @cindex commands in python, in GDB/MI

> > > +You can implement new @sc{GDB/MI} (@pxref{GDB/MI}) commands in Python.
> > 
> > Same comment as for the NEWS entry.  With a possibly similar solution.
> 
> This phrasing with "new" (both here and in NEWS) was taken from entries
> for CLI commands. For NEWS, I have changed it as you suggested, it's better.
> 
> But for manual it makes less sense to me. Wording "It is now possible to add GDB/MI 
> commands implemented in Python." sounds to me as to suggest this is some
> new feature.

Yes, of course: you need to drop the "now" part in the manual.  It is
only appropriate in NEWS.  But the rest reads well here.  What
problems do you have with

  It is possible to add GDB/MI commands implemented in Python.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 0/5] create GDB/MI commands using python
  2022-01-18 13:55 ` [PATCH 0/5] create GDB/MI commands using python Andrew Burgess
@ 2022-01-18 15:13   ` Jan Vrany
  2022-01-21 15:22     ` Andrew Burgess
  0 siblings, 1 reply; 41+ messages in thread
From: Jan Vrany @ 2022-01-18 15:13 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On Tue, 2022-01-18 at 13:55 +0000, Andrew Burgess wrote:
> * Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-17 12:44:20 +0000]:
> 
> > This is a restart of an earlier attempts to allow custom
> > GDB/MI commands written in Python.
> 
> Thanks for continuing to work on this feature.
> 
> I too had been looking at getting the remaining patches from your
> series upstream, and I'd like to discuss some of the differences
> between the approaches we took.

Great, thanks a lot!

> 
> At the end of this mail you'll find my current work-in-progress
> patch, it definitely needs the docs and NEWS entries adding, as well
> as a few extra tests.  However, functionality wise I think its mostly
> there.
> 
> My patch includes two sets of tests, gdb.python/py-mi-cmd.exp and
> gdb.python/py-mi-cmd-orig.exp.  The former is based on your tests, but
> with some tweaks based on changes I made.  The latter set is your
> tests taken from this m/l thread.
> 
> When running the gdb.python/py-mi-cmd-orig.exp tests there should be
> just 2 failures, both related to the same feature which is not present
> in my version, that is, the ability of a command to redefine itself,
> like this:
> 
>       class my_command(gdb.MICommand):
>         def invoke(self, args):
>           my_command("-blah")
>           return None
> 
>        my_command("-blah")
> 
> this works with your version, but not with mine, this is because I'm
> using python's own reference counting to track when a command can be
> redefined or not, and, when you are within a commands invoke method
> the reference count of the containing object is incremented, and this
> prevents gdb from deleting the command.
> 
> My question then, is how important is this feature, and what use case
> do you see for this?  Or, was support for this just a happy side
> effect of the implementation approach you chose?

Initially I did not think of it and it was - IIRC - pointed out in
some review. I thought to be a corner case, but it turned out to be
(potentiality very useful feature. 

While developing custom commands, pretty printers and so on, it is useful
to be able to "reload" Python code into running GDB without need to restart 
it. To support that, I created a "pr" command which reloads all custom python 
code (well, tries to at least), see 

https://swing.fit.cvut.cz/hg/jv-vdb/file/tip/python/vdb/cli.py#l20

So ultimately, this custom "pr" command's invoke() causes redefinition
of the "pr" command itself. I have not done it yet, but having 
menu item/icon in (my) GDB frontend using something like -python-reload`
seems desirable from UX POV. 

Now, I have quickly read comment on the below patch (not yet the code)
and generally I like your changes.

As for: 


> I considered this, but rejected it.  I believe that in most cases
> users will not be assigning the created MICommand objects to a
> variable, as it doesn't really serve any purpose.

Actually, I do this, see for example: 

https://github.com/powerlang/powerlang/blob/master/debug/powerlang/cli.py#L61

But again, not for MI commands yet. The reason is that sometimes (when developing 
scripts often) I work in python interactive shell (entered by `pi` GDB CLI) and I 
quite often  find myself in need of executing (custom) commands from there without 
need to leave python interpreter. In scripts, I do:

do = __DumpObjectCmd('do', gdb.COMMAND_DATA)

which allows me to use

(gdb) do 0xCAFECAFE

or 

>>> do(0xCAFECAFE)

depending where I'm at the moment. I personally find it useful. 
Together with reloading, the "do" python variable gets reassigned
with (possibly) new version of the "do" command. 

Let me have a look at your code in more details and think of it.
Maybe the way to go is to move more stuff to python so we 
won't have mi_command_py at all. From GDB we'd just call Python
functions like "gdb.mi.lookup_and_execute('-some-command', args)" 
and implement the rest in entirely in Python... Just a wild idea
I had just now, have not tried. 

Jan
> 
> Thanks,
> Andrew
> 
> ---
> 
> commit 0ef6ec4fbf237a89697c3e6a16888d57f4e8b777
> Author: Jan Vrany <jan.vrany@labware.com>
> Date:   Tue Jun 23 14:45:38 2020 +0100
> 
>     [WIP] gdb: create MI commands using python
>     
> 
> 
> 
>     This commit allows an user to create custom MI commands using Python
>     similarly to what is possible for Python CLI commands.
>     
> 
> 
> 
>     A new subclass of mi_command is defined for Python MI commands,
>     mi_command_py. A new file, py-micmd.c contains the logic for Python MI
>     commands.
>     
> 
> 
> 
>     This commit is based on work linked too from this mailing list thread:
>     
> 
> 
> 
>       https://urldefense.proofpoint.com/v2/url?u=https-3A__sourceware.org_pipermail_gdb_2021-2DNovember_049774.html&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=9z1zfN087arljFQe7-fUP-WfWyBh2N_DHykrk4OCQuc&e= 
>     
> 
> 
> 
>     Which has also been previously posted to the mailing list here:
>     
> 
> 
> 
>       https://urldefense.proofpoint.com/v2/url?u=https-3A__sourceware.org_pipermail_gdb-2Dpatches_2019-2DMay_158010.html&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=qdexVTgQHVJrkGweQ7tlGtjAc50w9IxFkXXwWQAq2I8&e= 
>     
> 
> 
> 
>     And was recently reposted here:
>     
> 
> 
> 
>       https://urldefense.proofpoint.com/v2/url?u=https-3A__sourceware.org_pipermail_gdb-2Dpatches_2022-2DJanuary_185190.html&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=dlYMq29D2Xgvdkabn3HdUyDb9S33JfsQgfcEBptmvKc&e= 
>     
> 
> 
> 
>     I've made some adjustments to how this feature is implemented though.
>     
> 
> 
> 
>     One notable change is how the lifetime of the Python gdb.MICommand
>     objects is managed.  In the original patch, these object were kept
>     alive by an owned reference within the mi_command_py object.  As such,
>     the Python object would not be deleted until the mi_command_py object
>     itself was deleted.
>     
> 
> 
> 
>     This caused a problem, the mi_command_py were held in the global mi
>     command table (in mi/mi-cmds.c), which, as a global, was not cleared
>     until program shutdown.  By this point the Python interpreter has
>     already been shutdown.  Attempting to delete the mi_command_py object
>     at this point was cause GDB to try and invoke Python code after
>     finalising the Python interpreter, and we would crash.
>     
> 
> 
> 
>     To work around this problem, the original patch added code in
>     python/python.c that would search the mi command table, and delete the
>     mi_command_py objects before the Python environment was finalised.
>     
> 
> 
> 
>     I didn't like this, it means making the mi command table global, and
>     having the python.c code understand about mi commands.
>     
> 
> 
> 
>     In my version, I store the mi commands in a dictionary in the gdb
>     module.  It is this dictionary that holds the reference to the Python
>     object.  When the Python environment is finalised the dictionary is
>     deleted, reducing the reference count on the Python objects within,
>     this in turn allows the mi command objects to be deleted.
>     
> 
> 
> 
>     I then make it so that when a Python MICommand object is deleted, the
>     corresponding mi command is automatically deleted from the mi command
>     table, removing that mi command.
>     
> 
> 
> 
>     A second change I made is to handle the case where an object is
>     reinitialised, consider this code:
>     
> 
> 
> 
>       class my_command(gdb.MICommand):
>         def invoke(self, args):
>           # ...
>           return None
>     
> 
> 
> 
>       cmd = my_command("-my-command")
>       cmd.__init__("-changed-the-name")
>     
> 
> 
> 
>     Though strange, this is perfectly valid Python code, this is now
>     handled correctly, the old `-my-command` will be removed, and the new
>     `-changed-the-name` command will be added.
>     
> 
> 
> 
>     A third change is how command redefinition is handled.  The old series
>     required special support to allow command redefinition.  Now we get
>     almost the same functionality, naturally.
>     
> 
> 
> 
>     Consider:
>     
> 
> 
> 
>       class command_one(gdb.MICommand):
>         def invoke(self, args):
>           # ...
>           return None
>     
> 
> 
> 
>       class command_two(gdb.MICommand):
>         def invoke(self, args):
>           # ...
>           return None
>     
> 
> 
> 
>       command_one("-my-command")
>       command_two("-my-command")
>     
> 
> 
> 
>     This will now do what (I hope) you'd expect.  There will be a single
>     mi command `-my-command` defined, that calls `command_two.invoke`.
>     
> 
> 
> 
>     However, if using the same class definitions, we try this instead:
>     
> 
> 
> 
>       c1 = command_one("-my-command")
>       c2 = command_two("-my-command")
>     
> 
> 
> 
>     The second line will now fail.  The reason here is that `c1` is still
>     holding a reference to the `command_one` object, and this prevents
>     `c2` from taking that command name.  If we do this:
>     
> 
> 
> 
>       c1 = command_one("-my-command")
>       c1 = None
>       c2 = command_two("-my-command")
>     
> 
> 
> 
>     Now we are again fine, and c2 will be correctly initialised.  This
>     feels like a bit of an edge case, but it's something that needs
>     figuring out.  A different possibility would be to always allow `c2`
>     to be created.  In the first case, creating `c2` would invalidate
>     `c1`.  We would then have an "is_valid" method on gdb.MICommand
>     object, so we could write something like:
>     
> 
> 
> 
>       c1 = command_one("-my-command")
>       c2 = command_two("-my-command")
>       if c1.is_valid ():
>         c1.invoke([])
>     
> 
> 
> 
>     I considered this, but rejected it.  I believe that in most cases
>     users will not be assigning the created MICommand objects to a
>     variable, as it doesn't really serve any purpose.
>     
> 
> 
> 
>     A fourth change is how the memory is handled for the name of the
>     python mi command.  In the recently posted series the mi_command class
>     has the name removed, and the name accessor is now virtual.  It is
>     then up to the sub-classes to correctly handle the memory.  Instead of
>     this, I just made the Python object manage the memory.  As the Python
>     object always outlives the mi_command object the mi_command object can
>     simply refer to the memory within the Python object.  This avoids
>     having to add more complexity to the mi_command class hierarchy.
>     
> 
> 
> 
>     I kept the test set from the original patch, and, almost all of the
>     original tests pass with my new series.  There was however, one
>     feature from the original series that I decided to drop, as supporting
>     it within the new scheme would have added significant complexity, for
>     what I consider, very little benefit.  That feature was for allowing
>     MI commands to redefine themselves, something like:
>     
> 
> 
> 
>       class my_command(gdb.MICommand):
>         def invoke(self, args):
>           my_command("-blah")
>           return None
>     
> 
> 
> 
>        my_command("-blah")
>     
> 
> 
> 
>     This would work under the old scheme, and was tested for, but, will no
>     longer work.  The reason this fails is that while invoking a command,
>     the invoke method itself holds a reference to the command object.  As
>     a result, this turns out to be similar to the first c1/c2 case posted
>     above, the my_command object is referenced from both the global
>     dictionary (in the gdb module), and from the running invoke method.
>     When attempting to redefine the command GDB understands that it can
>     reduce the reference count by removing the entry from the dictionary
>     in the gdb module, but the extra reference prevents GDB from deleting
>     the previous my_command object.
>     
> 
> 
> 
>     todo:
>     =====
>     
> 
> 
> 
>     I'm still not 100% sure if I was correct to reject the is_valid method
>     approach, maybe that would be a more natural way for the API to work?
> 
> diff --git a/gdb/Makefile.in b/gdb/Makefile.in
> index d0db5fbdee1..5cb428459c3 100644
> --- a/gdb/Makefile.in
> +++ b/gdb/Makefile.in
> @@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
>  	python/py-lazy-string.c \
>  	python/py-linetable.c \
>  	python/py-membuf.c \
> +	python/py-micmd.c \
>  	python/py-newobjfileevent.c \
>  	python/py-objfile.c \
>  	python/py-param.c \
> diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
> index cd7cabdda9b..dd0243e5bfe 100644
> --- a/gdb/mi/mi-cmds.c
> +++ b/gdb/mi/mi-cmds.c
> @@ -26,10 +26,6 @@
>  #include <map>
>  #include <string>
>  
> 
> 
> 
> -/* A command held in the MI_CMD_TABLE.  */
> -
> -using mi_command_up = std::unique_ptr<struct mi_command>;
> -
>  /* MI command table (built at run time). */
>  
> 
> 
> 
>  static std::map<std::string, mi_command_up> mi_cmd_table;
> @@ -113,12 +109,12 @@ struct mi_command_cli : public mi_command
>     not have been added to mi_cmd_table.  Otherwise, return true, and
>     COMMAND was added to mi_cmd_table.  */
>  
> 
> 
> 
> -static bool
> +bool
>  insert_mi_cmd_entry (mi_command_up command)
>  {
>    gdb_assert (command != nullptr);
>  
> 
> 
> 
> -  const std::string &name = command->name ();
> +  const std::string name (command->name ());
>  
> 
> 
> 
>    if (mi_cmd_table.find (name) != mi_cmd_table.end ())
>      return false;
> @@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
>    return true;
>  }
>  
> 
> 
> 
> +bool
> +remove_mi_cmd_entry (mi_command *command)
> +{
> +  gdb_assert (command != nullptr);
> +
> +  const std::string name (command->name ());
> +
> +  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
> +    return false;
> +
> +  mi_cmd_table.erase (name);
> +  return true;
> +}
> +
>  /* Create and register a new MI command with an MI specific implementation.
>     NAME must name an MI command that does not already exist, otherwise an
>     assertion will trigger.  */
> diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
> index 2a93a9f5476..3f4fb854d68 100644
> --- a/gdb/mi/mi-cmds.h
> +++ b/gdb/mi/mi-cmds.h
> @@ -187,6 +187,10 @@ struct mi_command
>    int *m_suppress_notification;
>  };
>  
> 
> 
> 
> +/* A command held in the MI_CMD_TABLE.  */
> +
> +using mi_command_up = std::unique_ptr<struct mi_command>;
> +
>  /* Lookup a command in the MI command table, returns nullptr if COMMAND is
>     not found.  */
>  
> 
> 
> 
> @@ -194,4 +198,15 @@ extern mi_command *mi_cmd_lookup (const char *command);
>  
> 
> 
> 
>  extern void mi_execute_command (const char *cmd, int from_tty);
>  
> 
> 
> 
> +/* Insert a new mi-command into the command table.  Return true if
> +   insertion was successful.  */
> +
> +extern bool insert_mi_cmd_entry (mi_command_up command);
> +
> +/* Remove a mi-command from the command table.  Return true if the removal
> +   was success, otherwise return false.  */
> +
> +extern bool remove_mi_cmd_entry (mi_command *command);
> +
> +
>  #endif /* MI_MI_CMDS_H */
> diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
> index 11a1b444bfd..a9197eb4ffe 100644
> --- a/gdb/python/lib/gdb/__init__.py
> +++ b/gdb/python/lib/gdb/__init__.py
> @@ -81,6 +81,9 @@ frame_filters = {}
>  # Initial frame unwinders.
>  frame_unwinders = []
>  
> 
> 
> 
> +# Hash containing all user created MI commands, the key is the command
> +# name, and the value is the gdb.MICommand object.
> +mi_commands = {}
>  
> 
> 
> 
>  def _execute_unwinders(pending_frame):
>      """Internal function called from GDB to execute all unwinders.
> diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
> new file mode 100644
> index 00000000000..042ba624f06
> --- /dev/null
> +++ b/gdb/python/py-micmd.c
> @@ -0,0 +1,445 @@
> +/* MI Command Set for GDB, the GNU debugger.
> +
> +   Copyright (C) 2019 Free Software Foundation, Inc.
> +
> +   This file is part of GDB.
> +
> +   This program is free software; you can redistribute it and/or modify
> +   it under the terms of the GNU General Public License as published by
> +   the Free Software Foundation; either version 3 of the License, or
> +   (at your option) any later version.
> +
> +   This program is distributed in the hope that it will be useful,
> +   but WITHOUT ANY WARRANTY; without even the implied warranty of
> +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +   GNU General Public License for more details.
> +
> +   You should have received a copy of the GNU General Public License
> +   along with this program.  If not, see <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.gnu.org_licenses_&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=EDRROTsO763r9g0Rhl-o_E2lysOIy7ftShmwR1bl7Ic&e= >.  */
> +
> +/* gdb MI commands implemented in Python  */
> +
> +#include "defs.h"
> +#include "python-internal.h"
> +#include "arch-utils.h"
> +#include "charset.h"
> +#include "language.h"
> +#include "mi/mi-cmds.h"
> +#include "mi/mi-parse.h"
> +
> +#include <string>
> +
> +struct mi_command_py;
> +
> +/* Representation of a python gdb.MICommand object.  */
> +
> +struct micmdpy_object
> +{
> +  PyObject_HEAD
> +
> +  /* The object representing this command in the MI command table.  */
> +  struct mi_command_py *mi_command;
> +
> +  /* The string representing this command name.  This string is referenced
> +     from the mi_command_py object, and must be free'd once the
> +     mi_command_py object is no longer needed.  */
> +  char *command_name;
> +};
> +
> +/* MI command implemented in Python.  */
> +
> +struct mi_command_py : public mi_command
> +{
> +  /* Constructs a new mi_command_py object.  NAME is command name without
> +     leading dash.  OBJECT is a reference to a Python object implementing
> +     the command.  This object should inherit from gdb.MICommand and should
> +     implement method invoke (args). */
> +
> +  mi_command_py (const char *name, PyObject *object)
> +    : mi_command (name, nullptr),
> +      m_pyobj (object)
> +  { /* Nothing.  */ }
> +
> +  ~mi_command_py () = default;
> +
> +protected:
> +  /* Called when the mi command is invoked.  */
> +  virtual void do_invoke(struct mi_parse *parse) const override;
> +
> +private:
> +  /* The python object representing this mi command.  */
> +  PyObject *m_pyobj;
> +};
> +
> +static PyObject *invoke_cst;
> +
> +extern PyTypeObject micmdpy_object_type
> +    CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
> +
> +/* If the command invoked returns a list, this function parses it and create an
> +   appropriate MI out output.
> +
> +   The returned values must be Python string, and can be contained within Python
> +   lists and dictionaries. It is possible to have a multiple levels of lists
> +   and/or dictionaries.  */
> +
> +static void
> +parse_mi_result (PyObject *result, const char *field_name)
> +{
> +  struct ui_out *uiout = current_uiout;
> +
> +  if (PyDict_Check (result))
> +    {
> +      PyObject *key, *value;
> +      Py_ssize_t pos = 0;
> +      ui_out_emit_tuple tuple_emitter (uiout, field_name);
> +      while (PyDict_Next (result, &pos, &key, &value))
> +	{
> +	  if (!PyString_Check (key))
> +	    {
> +	      gdbpy_ref<> key_repr (PyObject_Repr (key));
> +	      if (PyErr_Occurred () != NULL)
> +                {
> +                  gdbpy_err_fetch ex;
> +                  gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
> +
> +                  if (ex_msg == NULL || *ex_msg == '\0')
> +                    error (_("Non-string object used as key."));
> +                  else
> +                    error (_("Non-string object used as key: %s."),
> +                           ex_msg.get ());
> +                }
> +              else
> +	        {
> +	          auto key_repr_string
> +	                 = python_string_to_target_string (key_repr.get ());
> +	          error (_("Non-string object used as key: %s."),
> +                         key_repr_string.get ());
> +	        }
> +	    }
> +
> +	  auto key_string = python_string_to_target_string (key);
> +	  parse_mi_result (value, key_string.get ());
> +	}
> +    }
> +  else if (PySequence_Check (result) && !PyString_Check (result))
> +    {
> +      ui_out_emit_list list_emitter (uiout, field_name);
> +      for (Py_ssize_t i = 0; i < PySequence_Size (result); ++i)
> +	{
> +          gdbpy_ref<> item (PySequence_ITEM (result, i));
> +	  parse_mi_result (item.get (), NULL);
> +	}
> +    }
> +  else if (PyIter_Check (result))
> +    {
> +      gdbpy_ref<> item;
> +      ui_out_emit_list list_emitter (uiout, field_name);
> +      while (item.reset (PyIter_Next (result)), item != nullptr)
> +	parse_mi_result (item.get (), NULL);
> +    }
> +  else
> +    {
> +      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
> +      uiout->field_string (field_name, string.get ());
> +    }
> +}
> +
> +/* Object initializer; sets up gdb-side structures for MI command.
> +
> +   Use: __init__(NAME).
> +
> +   NAME is the name of the MI command to register.  It must start with a dash
> +   as traditional MI commands do.  */
> +
> +static int
> +micmdpy_init (PyObject *self, PyObject *args, PyObject *kw)
> +{
> +  const char *name;
> +  micmdpy_object *cmd = (micmdpy_object *) self;
> +
> +  if (!PyArg_ParseTuple (args, "s", &name))
> +    return -1;
> +
> +  /* Validate command name */
> +  const int name_len = strlen (name);
> +  if (name_len == 0)
> +    {
> +      error (_("MI command name is empty."));
> +      return -1;
> +    }
> +  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
> +    {
> +      error (_("MI command name does not start with '-'"
> +               " followed by at least one letter or digit."));
> +      return -1;
> +    }
> +  else
> +    for (int i = 2; i < name_len; i++)
> +      {
> +	if (!isalnum (name[i]) && name[i] != '-')
> +	  {
> +	    error (_("MI command name contains invalid character: %c."),
> +                   name[i]);
> +	    return -1;
> +	  }
> +      }
> +
> +  if (!PyObject_HasAttr (self, invoke_cst))
> +    error (_("-%s: Python command object missing 'invoke' method."), name);
> +
> +  /* Now insert the object into the dictionary that lives in the gdb module.  */
> +  if (gdb_python_module == nullptr
> +      || ! PyObject_HasAttrString (gdb_python_module, "mi_commands"))
> +    error (_("unable to find gdb.mi_commands dictionary"));
> +
> +  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
> +						   "mi_commands"));
> +  if (mi_cmd_dict == nullptr || !PyDict_Check (mi_cmd_dict.get ()))
> +    error (_("unable to fetch gdb.mi_commands dictionary"));
> +
> +  /* Is the user re-initialising an existing gdb.MICommand object?  */
> +  if (cmd->mi_command != nullptr)
> +    {
> +      /* Grab the old name for this command.  */
> +      mi_command *old_cmd = cmd->mi_command;
> +      gdbpy_ref<> old_name_obj
> +	= host_string_to_python_string (old_cmd->name ());
> +
> +      /* Lookup the gdb.MICommand object in the dictionary of all python mi
> +	 commands, this is gdb.mi_command, and remove it.  */
> +      PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
> +						old_name_obj.get ());
> +      if (curr == nullptr && PyErr_Occurred ())
> +	return -1;
> +      if (curr != nullptr)
> +	{
> +	  /* The old command is in gdb.mi_commands, so remove it.  */
> +	  if (PyDict_DelItem (mi_cmd_dict.get (), old_name_obj.get ()) < 0)
> +	    return -1;
> +	}
> +
> +      /* Now remove the old command from the gdb internal table of mi
> +	 commands.  */
> +      remove_mi_cmd_entry (cmd->mi_command);
> +
> +      /* And reset this object back to a default state.  */
> +      cmd->mi_command = nullptr;
> +      xfree (cmd->command_name);
> +      cmd->command_name = nullptr;
> +    }
> +
> +  /* Create the gdb internal object to represent the mi command.  */
> +  gdb::unique_xmalloc_ptr<char> name_up = make_unique_xstrdup (name + 1);
> +  mi_command_py *tmp_cmd = new mi_command_py (name_up.get (), self);
> +  cmd->mi_command = tmp_cmd;
> +  cmd->command_name = name_up.release ();
> +  mi_command_up micommand (tmp_cmd);
> +
> +  /* Look up this command name in the gdb.mi_commands dictionary, a command
> +     with this name may already exist.  */
> +  gdbpy_ref<> name_obj = host_string_to_python_string (cmd->command_name);
> +
> +  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
> +					    name_obj.get ());
> +  if (curr == nullptr && PyErr_Occurred ())
> +    return -1;
> +  if (curr != nullptr)
> +    {
> +      /* There is a command with this name already in the gdb.mi_commands
> +	 dictionary.  If the reference in the dictionary is the only
> +	 reference, then we allow the user to replace this with a new
> +	 command.  If, however, there are other references to this mi
> +	 command object, then the user will get an error.  */
> +      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
> +	{
> +	  PyErr_SetString (PyExc_RuntimeError,
> +			   _("unexpected object in gdb.mi_commands dictionary"));
> +	  return -1;
> +	}
> +
> +      if (Py_REFCNT (curr) > 1)
> +	{
> +	  PyErr_SetString (PyExc_RuntimeError,
> +			   _("unable to add command, name may already be in use"));
> +	  return -1;
> +	}
> +
> +      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
> +	return -1;
> +    }
> +
> +  /* Add the command to the gdb internal mi command table.  */
> +  bool result = insert_mi_cmd_entry (std::move (micommand));
> +  if (!result)
> +    {
> +      PyErr_SetString (PyExc_RuntimeError,
> +		       _("unable to add command, name may already be in use"));
> +      return -1;
> +    }
> +
> +  /* And add the python object to the gdb.mi_commands dictionary.  */
> +  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), self) < 0)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +/* Called when a gdb.MICommand object is deallocated.  */
> +
> +static void
> +micmdpy_dealloc (PyObject *obj)
> +{
> +  micmdpy_object *cmd = (micmdpy_object *) obj;
> +  mi_command *py_cmd = cmd->mi_command;
> +
> +  /* There might not be an mi_command object if the object failed to
> +     initialize correctly for some reason.  If there is though, then now is
> +     the time we remove the object from the gdb internal command table.  */
> +  if (py_cmd != nullptr)
> +    remove_mi_cmd_entry (py_cmd);
> +
> +  /* Free the memory that holds the command name.  */
> +  xfree (cmd->command_name);
> +
> +  /* Finally, free the memory for this python object.  */
> +  Py_TYPE (obj)->tp_free (obj);
> +}
> +
> +/* Called when the mi command is invoked.  */
> +
> +void
> +mi_command_py::do_invoke (struct mi_parse *parse) const
> +{
> +  mi_parse_argv (parse->args, parse);
> +
> +  if (parse->argv == NULL)
> +    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
> +
> +  PyObject *obj = this->m_pyobj;
> +
> +  gdbpy_enter enter_py (get_current_arch (), current_language);
> +
> +  gdb_assert (obj != nullptr);
> +
> +  if (!PyObject_HasAttr (obj, invoke_cst))
> +      error (_("-%s: Python command object missing 'invoke' method."),
> +	     name ());
> +
> +
> +  gdbpy_ref<> argobj (PyList_New (parse->argc));
> +  if (argobj == nullptr)
> +    {
> +      gdbpy_print_stack ();
> +      error (_("-%s: failed to create the Python arguments list."),
> +	     name ());
> +    }
> +
> +  for (int i = 0; i < parse->argc; ++i)
> +    {
> +      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i], strlen (parse->argv[i]),
> +					 host_charset (), NULL));
> +      if (PyList_SetItem (argobj.get (), i, str.release ()) != 0)
> +	{
> +	  error (_("-%s: failed to create the Python arguments list."),
> +		 name ());
> +	}
> +    }
> +
> +  gdb_assert (PyErr_Occurred () == NULL);
> +  gdbpy_ref<> result (
> +    PyObject_CallMethodObjArgs (obj, invoke_cst, argobj.get (), NULL));
> +  if (PyErr_Occurred () != NULL)
> +    {
> +      gdbpy_err_fetch ex;
> +      gdb::unique_xmalloc_ptr<char> ex_msg (ex.to_string ());
> +
> +      if (ex_msg == NULL || *ex_msg == '\0')
> +	error (_("-%s: failed to execute command"), name ());
> +      else
> +	error (_("-%s: %s"), name (), ex_msg.get ());
> +    }
> +  else
> +    {
> +      if (Py_None != result)
> +	parse_mi_result (result.get (), "result");
> +    }
> +}
> +
> +/* Initialize the MI command object.  */
> +
> +int
> +gdbpy_initialize_micommands ()
> +{
> +  micmdpy_object_type.tp_new = PyType_GenericNew;
> +  if (PyType_Ready (&micmdpy_object_type) < 0)
> +    return -1;
> +
> +  if (gdb_pymodule_addobject (gdb_module, "MICommand",
> +			      (PyObject *) &micmdpy_object_type)
> +      < 0)
> +    return -1;
> +
> +  invoke_cst = PyString_FromString ("invoke");
> +  if (invoke_cst == NULL)
> +    return -1;
> +
> +  return 0;
> +}
> +
> +/* Implement gdb.MICommand.name attribute, return a string, the name of
> +   this MI command.  */
> +
> +static PyObject *
> +micmdpy_get_name (PyObject *self, void *closure)
> +{
> +  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
> +
> +  gdb_assert (micmd_obj->command_name != nullptr);
> +  return PyString_FromString (micmd_obj->command_name);
> +}
> +
> +/* gdb.MICommand attributes.   */
> +static gdb_PyGetSetDef micmdpy_object_getset[] = {
> +  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
> +  { nullptr }	/* Sentinel.  */
> +};
> +
> +PyTypeObject micmdpy_object_type = {
> +  PyVarObject_HEAD_INIT (NULL, 0) "gdb.MICommand", /*tp_name */
> +  sizeof (micmdpy_object),			   /*tp_basicsize */
> +  0,						   /*tp_itemsize */
> +  micmdpy_dealloc,				   /*tp_dealloc */
> +  0,						   /*tp_print */
> +  0,						   /*tp_getattr */
> +  0,						   /*tp_setattr */
> +  0,						   /*tp_compare */
> +  0,						   /*tp_repr */
> +  0,						   /*tp_as_number */
> +  0,						   /*tp_as_sequence */
> +  0,						   /*tp_as_mapping */
> +  0,						   /*tp_hash */
> +  0,						   /*tp_call */
> +  0,						   /*tp_str */
> +  0,						   /*tp_getattro */
> +  0,						   /*tp_setattro */
> +  0,						   /*tp_as_buffer */
> +  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
> +  "GDB mi-command object",			   /* tp_doc */
> +  0,						   /* tp_traverse */
> +  0,						   /* tp_clear */
> +  0,						   /* tp_richcompare */
> +  0,						   /* tp_weaklistoffset */
> +  0,						   /* tp_iter */
> +  0,						   /* tp_iternext */
> +  0,						   /* tp_methods */
> +  0,						   /* tp_members */
> +  micmdpy_object_getset,			   /* tp_getset */
> +  0,						   /* tp_base */
> +  0,						   /* tp_dict */
> +  0,						   /* tp_descr_get */
> +  0,						   /* tp_descr_set */
> +  0,						   /* tp_dictoffset */
> +  micmdpy_init,					   /* tp_init */
> +  0,						   /* tp_alloc */
> +};
> diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
> index 583989c5a6d..d5a0b4a4a91 100644
> --- a/gdb/python/python-internal.h
> +++ b/gdb/python/python-internal.h
> @@ -561,6 +561,8 @@ int gdbpy_initialize_membuf ()
>    CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
>  int gdbpy_initialize_connection ()
>    CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
> +int gdbpy_initialize_micommands (void)
> +  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
>  
> 
> 
> 
>  /* A wrapper for PyErr_Fetch that handles reference counting for the
>     caller.  */
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 4dcda53d9ab..f2b09916374 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -1887,7 +1887,8 @@ do_start_initialization ()
>        || gdbpy_initialize_unwind () < 0
>        || gdbpy_initialize_membuf () < 0
>        || gdbpy_initialize_connection () < 0
> -      || gdbpy_initialize_tui () < 0)
> +      || gdbpy_initialize_tui () < 0
> +      || gdbpy_initialize_micommands () < 0)
>      return false;
>  
> 
> 
> 
>  #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
> diff --git a/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp b/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp
> new file mode 100644
> index 00000000000..78baf5e97ff
> --- /dev/null
> +++ b/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp
> @@ -0,0 +1,133 @@
> +# Copyright (C) 2019-2021 Free Software Foundation, Inc.
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.gnu.org_licenses_&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=EDRROTsO763r9g0Rhl-o_E2lysOIy7ftShmwR1bl7Ic&e= >.
> +
> +# Test custom MI commands implemented in Python.
> +
> +load_lib gdb-python.exp
> +load_lib mi-support.exp
> +set MIFLAGS "-i=mi2"
> +
> +gdb_exit
> +if {[mi_gdb_start]} {
> +    continue
> +}
> +
> +if {[lsearch -exact [mi_get_features] python] < 0} {
> +    unsupported "python support is disabled"
> +    return -1
> +}
> +
> +standard_testfile
> +
> +#
> +# Start here
> +#
> +
> +
> +mi_gdb_test "set python print-stack full" \
> +  ".*\\^done" \
> +  "set python print-stack full"
> +
> +mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
> +  ".*\\^done" \
> +  "load python file"
> +
> +mi_gdb_test "python pycmd1('-pycmd')" \
> +  ".*\\^done" \
> +  "define -pycmd MI command"
> +
> +
> +mi_gdb_test "-pycmd int" \
> +  "\\^done,result=\"42\"" \
> +  "-pycmd int"
> +
> +mi_gdb_test "-pycmd str" \
> +  "\\^done,result=\"Hello world!\"" \
> +  "-pycmd str"
> +
> +mi_gdb_test "-pycmd ary" \
> +  "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
> +  "-pycmd ary"
> +
> +mi_gdb_test "-pycmd dct" \
> +  "\\^done,result={hello=\"world\",times=\"42\"}" \
> +  "-pycmd dct"
> +
> +mi_gdb_test "-pycmd bk1" \
> +  "\\^error,msg=\"Non-string object used as key: Bad Key\\.\"" \
> +  "-pycmd bk1"
> +
> +mi_gdb_test "-pycmd bk2" \
> +  "\\^error,msg=\"Non-string object used as key: 1\\.\"" \
> +  "-pycmd bk2"
> +
> +mi_gdb_test "-pycmd bk3" \
> +  "\\^error,msg=\"Non-string object used as key: __repr__ returned non-string .*" \
> +  "-pycmd bk3"
> +
> +mi_gdb_test "-pycmd tpl" \
> +  "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
> +  "-pycmd tpl"
> +
> +mi_gdb_test "-pycmd itr" \
> +  "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
> +  "-pycmd itr"
> +
> +mi_gdb_test "-pycmd nn1" \
> +  "\\^done" \
> +  "-pycmd nn1"
> +
> +mi_gdb_test "-pycmd nn2" \
> +  "\\^done,result=\\\[\"None\"\\\]" \
> +  "-pycmd nn2"
> +
> +mi_gdb_test "-pycmd bogus" \
> +  "\\^error,msg=\"-pycmd: Invalid parameter: bogus\"" \
> +  "-pycmd bogus"
> +
> +mi_gdb_test "-pycmd exp" \
> +  "\\^error,msg=\"-pycmd: failed to execute command\"" \
> +  "-pycmd exp"
> +
> +mi_gdb_test "python pycmd2('-pycmd')" \
> +  ".*\\^done" \
> +  "redefine -pycmd MI command from CLI command"
> +
> +mi_gdb_test "-pycmd str" \
> +  "\\^done,result=\"Ciao!\"" \
> +  "-pycmd str - redefined from CLI"
> +
> +mi_gdb_test "-pycmd int" \
> +  "\\^error,msg=\"-pycmd: Invalid parameter: int\"" \
> +  "-pycmd int - redefined from CLI"
> +
> +mi_gdb_test "-pycmd red" \
> +    "\\^error,msg=\"-pycmd: Command redefined but we failing anyway\"" \
> +  "redefine -pycmd MI command from Python MI command"
> +
> +mi_gdb_test "-pycmd int" \
> +  "\\^done,result=\"42\"" \
> +  "-pycmd int - redefined from MI"
> +
> +mi_gdb_test "python pycmd1('')" \
> +  ".*\\^error,msg=\"MI command name is empty.\"" \
> +  "empty MI command name"
> +
> +mi_gdb_test "python pycmd1('-')" \
> +  ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit\\.\"" \
> +  "invalid MI command name"
> +
> +mi_gdb_test "python pycmd1('-bad-character-@')" \
> +  ".*\\^error,msg=\"MI command name contains invalid character: @\\.\"" \
> +  "invalid character in MI command name"
> diff --git a/gdb/testsuite/gdb.python/py-mi-cmd-orig.py b/gdb/testsuite/gdb.python/py-mi-cmd-orig.py
> new file mode 100644
> index 00000000000..2f6ba2f8037
> --- /dev/null
> +++ b/gdb/testsuite/gdb.python/py-mi-cmd-orig.py
> @@ -0,0 +1,68 @@
> +# Copyright (C) 2019-2021 Free Software Foundation, Inc.
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.gnu.org_licenses_&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=EDRROTsO763r9g0Rhl-o_E2lysOIy7ftShmwR1bl7Ic&e= >.
> +
> +import gdb
> +
> +class BadKey:
> +    def __repr__(self):
> +        return "Bad Key"
> +
> +class ReallyBadKey:
> +    def __repr__(self):
> +        return BadKey()
> +
> +
> +class pycmd1(gdb.MICommand):
> +    def invoke(self, argv):
> +        if argv[0] == 'int':
> +            return 42
> +        elif argv[0] == 'str':
> +            return "Hello world!"
> +        elif argv[0] == 'ary':
> +            return [ 'Hello', 42 ]
> +        elif argv[0] == "dct":
> +            return { 'hello' : 'world', 'times' : 42}
> +        elif argv[0] == "bk1":
> +            return { BadKey() : 'world' }
> +        elif argv[0] == "bk2":
> +            return { 1 : 'world' }
> +        elif argv[0] == "bk3":
> +            return { ReallyBadKey() : 'world' }
> +        elif argv[0] == 'tpl':
> +            return ( 42 , 'Hello' )
> +        elif argv[0] == 'itr':
> +            return iter([1,2,3])
> +        elif argv[0] == 'nn1':
> +            return None
> +        elif argv[0] == 'nn2':
> +            return [ None ]
> +        elif argv[0] == 'red':
> +            pycmd2('-pycmd')
> +            return None
> +        elif argv[0] == 'exp':
> +            raise gdb.GdbError()
> +        else:
> +            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
> +
> +
> +class pycmd2(gdb.MICommand):
> +    def invoke(self, argv):
> +        if argv[0] == 'str':
> +            return "Ciao!"
> +        elif argv[0] == 'red':
> +            pycmd1('-pycmd')
> +            raise gdb.GdbError("Command redefined but we failing anyway")
> +        else:
> +            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
> +
> diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
> new file mode 100644
> index 00000000000..55a4d821351
> --- /dev/null
> +++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
> @@ -0,0 +1,136 @@
> +# Copyright (C) 2018 Free Software Foundation, Inc.
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <https://urldefense.proofpoint.com/v2/url?u=http-3A__www.gnu.org_licenses_&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=ZWcfs9DJzGKjOijHZvo43ko6T60zCH7uATK-dCOQm1k&s=EDRROTsO763r9g0Rhl-o_E2lysOIy7ftShmwR1bl7Ic&e= >.
> +
> +# Test custom MI commands implemented in Python.
> +
> +load_lib gdb-python.exp
> +load_lib mi-support.exp
> +set MIFLAGS "-i=mi2"
> +
> +gdb_exit
> +if {[mi_gdb_start]} {
> +    continue
> +}
> +
> +if {[lsearch -exact [mi_get_features] python] < 0} {
> +    unsupported "python support is disabled"
> +    return -1
> +}
> +
> +standard_testfile
> +#
> +# Start here
> +#
> +
> +
> +mi_gdb_test "set python print-stack full" \
> +  ".*\\^done" \
> +  "set python print-stack full"
> +
> +mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
> +  ".*\\^done" \
> +  "load python file"
> +
> +mi_gdb_test "python pycmd1('-pycmd')" \
> +  ".*\\^done" \
> +  "Define -pycmd MI command"
> +
> +
> +mi_gdb_test "-pycmd int" \
> +  "\\^done,result=\"42\"" \
> +  "-pycmd int"
> +
> +mi_gdb_test "-pycmd str" \
> +  "\\^done,result=\"Hello world!\"" \
> +  "-pycmd str"
> +
> +mi_gdb_test "-pycmd ary" \
> +  "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
> +  "-pycmd ary"
> +
> +mi_gdb_test "-pycmd dct" \
> +  "\\^done,result={hello=\"world\",times=\"42\"}" \
> +  "-pycmd dct"
> +
> +mi_gdb_test "-pycmd bk1" \
> +  "\\^error,msg=\"Non-string object used as key: Bad Kay.\"" \
> +  "-pycmd bk1"
> +
> +mi_gdb_test "-pycmd bk2" \
> +  "\\^error,msg=\"Non-string object used as key: 1.\"" \
> +  "-pycmd bk2"
> +
> +mi_gdb_test "-pycmd bk3" \
> +  "\\^error,msg=\"Non-string object used as key: __repr__ returned non-string .*" \
> +  "-pycmd bk3"
> +
> +mi_gdb_test "-pycmd tpl" \
> +  "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
> +  "-pycmd tpl"
> +
> +mi_gdb_test "-pycmd itr" \
> +  "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
> +  "-pycmd itr"
> +
> +mi_gdb_test "-pycmd nn1" \
> +  "\\^done" \
> +  "-pycmd nn1"
> +
> +mi_gdb_test "-pycmd nn2" \
> +  "\\^done,result=\\\[\"None\"\\\]" \
> +  "-pycmd nn2"
> +
> +mi_gdb_test "-pycmd bogus" \
> +  "\\^error,msg=\"-pycmd: Invalid parameter: bogus\"" \
> +  "-pycmd bogus"
> +
> +mi_gdb_test "-pycmd exp" \
> +  "\\^error,msg=\"-pycmd: failed to execute command\"" \
> +  "-pycmd exp"
> +
> +mi_gdb_test "python pycmd2('-pycmd')" \
> +  ".*\\^done" \
> +  "Redefine -pycmd MI command from CLI command"
> +
> +mi_gdb_test "-pycmd str" \
> +  "\\^done,result=\"Ciao!\"" \
> +  "-pycmd str - redefined from CLI"
> +
> +mi_gdb_test "-pycmd int" \
> +  "\\^error,msg=\"-pycmd: Invalid parameter: int\"" \
> +  "-pycmd int - redefined from CLI"
> +
> +mi_gdb_test "-pycmd red" \
> +    "\\^error,msg=\"-pycmd: unable to add command, name may already be in use\"" \
> +  "Redefine -pycmd MI command from Python MI command"
> +
> +mi_gdb_test "-pycmd new" \
> +    "\\^done" \
> +  "Define new command -pycmd-new MI command from Python MI command"
> +
> +mi_gdb_test "-pycmd-new int" \
> +  "\\^done,result=\"42\"" \
> +  "-pycmd int - redefined from MI"
> +
> +mi_gdb_test "python pycmd1('')" \
> +  ".*\\^error,msg=\"MI command name is empty.\"" \
> +  "empty MI command name"
> +
> +mi_gdb_test "python pycmd1('-')" \
> +  ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit.\"" \
> +  "invalid MI command name"
> +
> +mi_gdb_test "python pycmd1('-bad-character-@')" \
> +  ".*\\^error,msg=\"MI command name contains invalid character: @.\"" \
> +  "invalid character in MI command name"
> diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
> new file mode 100644
> index 00000000000..fd09d8dca00
> --- /dev/null
> +++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
> @@ -0,0 +1,57 @@
> +import gdb
> +
> +class BadKey:
> +    def __repr__(self):
> +        return "Bad Kay"
> +
> +class ReallyBadKey:
> +    def __repr__(self):
> +        return BadKey()
> +
> +
> +class pycmd1(gdb.MICommand):
> +    def invoke(self, argv):
> +        if argv[0] == 'int':
> +            return 42
> +        elif argv[0] == 'str':
> +            return "Hello world!"
> +        elif argv[0] == 'ary':
> +            return [ 'Hello', 42 ]
> +        elif argv[0] == "dct":
> +            return { 'hello' : 'world', 'times' : 42}
> +        elif argv[0] == "bk1":
> +            return { BadKey() : 'world' }
> +        elif argv[0] == "bk2":
> +            return { 1 : 'world' }
> +        elif argv[0] == "bk3":
> +            return { ReallyBadKey() : 'world' }
> +        elif argv[0] == 'tpl':
> +            return ( 42 , 'Hello' )
> +        elif argv[0] == 'itr':
> +            return iter([1,2,3])
> +        elif argv[0] == 'nn1':
> +            return None
> +        elif argv[0] == 'nn2':
> +            return [ None ]
> +        elif argv[0] == 'red':
> +            pycmd2('-pycmd')
> +            return None
> +        elif argv[0] == 'exp':
> +            raise gdb.GdbError()
> +        else:
> +            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
> +
> +
> +class pycmd2(gdb.MICommand):
> +    def invoke(self, argv):
> +        if argv[0] == 'str':
> +            return "Ciao!"
> +        elif argv[0] == 'red':
> +            pycmd1('-pycmd')
> +            raise gdb.GdbError("Command redefined but we failing anyway")
> +        elif argv[0] == 'new':
> +            pycmd1('-pycmd-new')
> +            return None
> +        else:
> +            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
> +
> 



^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 0/5] create GDB/MI commands using python
  2022-01-18 15:13   ` Jan Vrany
@ 2022-01-21 15:22     ` Andrew Burgess
  2022-01-24 12:59       ` Jan Vrany
  2022-02-06 21:16       ` Simon Marchi
  0 siblings, 2 replies; 41+ messages in thread
From: Andrew Burgess @ 2022-01-21 15:22 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

* Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-18 15:13:01 +0000]:

> On Tue, 2022-01-18 at 13:55 +0000, Andrew Burgess wrote:
> > * Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-17 12:44:20 +0000]:
> > 
> > > This is a restart of an earlier attempts to allow custom
> > > GDB/MI commands written in Python.
> > 
> > Thanks for continuing to work on this feature.
> > 
> > I too had been looking at getting the remaining patches from your
> > series upstream, and I'd like to discuss some of the differences
> > between the approaches we took.
> 
> Great, thanks a lot!
> 
> > 
> > At the end of this mail you'll find my current work-in-progress
> > patch, it definitely needs the docs and NEWS entries adding, as well
> > as a few extra tests.  However, functionality wise I think its mostly
> > there.
> > 
> > My patch includes two sets of tests, gdb.python/py-mi-cmd.exp and
> > gdb.python/py-mi-cmd-orig.exp.  The former is based on your tests, but
> > with some tweaks based on changes I made.  The latter set is your
> > tests taken from this m/l thread.
> > 
> > When running the gdb.python/py-mi-cmd-orig.exp tests there should be
> > just 2 failures, both related to the same feature which is not present
> > in my version, that is, the ability of a command to redefine itself,
> > like this:
> > 
> >       class my_command(gdb.MICommand):
> >         def invoke(self, args):
> >           my_command("-blah")
> >           return None
> > 
> >        my_command("-blah")
> > 
> > this works with your version, but not with mine, this is because I'm
> > using python's own reference counting to track when a command can be
> > redefined or not, and, when you are within a commands invoke method
> > the reference count of the containing object is incremented, and this
> > prevents gdb from deleting the command.
> > 
> > My question then, is how important is this feature, and what use case
> > do you see for this?  Or, was support for this just a happy side
> > effect of the implementation approach you chose?
> 
> Initially I did not think of it and it was - IIRC - pointed out in
> some review. I thought to be a corner case, but it turned out to be
> (potentiality very useful feature. 
> 
> While developing custom commands, pretty printers and so on, it is useful
> to be able to "reload" Python code into running GDB without need to restart 
> it. To support that, I created a "pr" command which reloads all custom python 
> code (well, tries to at least), see 
> 
> https://swing.fit.cvut.cz/hg/jv-vdb/file/tip/python/vdb/cli.py#l20
> 
> So ultimately, this custom "pr" command's invoke() causes redefinition
> of the "pr" command itself. I have not done it yet, but having 
> menu item/icon in (my) GDB frontend using something like -python-reload`
> seems desirable from UX POV.

Thanks, that's not an unreasonable use case.  I've tweaked things so
that this case is now supported.

I've gone through that patch and ported over your NEWS and doc
changes, with some updates based on Eli's feedback, as well as some
new content to cover some of the changes in my patch.

For the testing, I have, for now, kept gdb.python/py-mi-cmd-orig.exp,
which is your test file taken from your patch series.  I added some
'setup_kfail' markers to 7 tests that now fail due to changes in error
strings, hopefully, this will allow you to review what changed.
However, except for the differences in error string, everything your
original series supported is now supported by this patch.

From a user's point of view, I think the only differences with your
patch are:

 1. New .name attribute, that returns the name of the command as a
    string, here's an example session:

    (gdb) python
    >class MyCommand(gdb.MICommand):
    >  def __init__(self):
    >    super(MyCommand, self).__init__("-my-command")
    >  def invoke(self, args):
    >    return None
    >
    >end
    (gdb) python cmd = MyCommand()
    (gdb) python print(cmd.name)
    -my-command
    (gdb)

 2. New .installed attribute, that allows a command to be installed or
    removed from the mi command table, here's an example session:

    (gdb) python
    >class MyCommand(gdb.MICommand):
    >  def __init__(self, name, message):
    >    self._message = message
    >    super(MyCommand, self).__init__(name)
    >
    >  def invoke(self, args):
    >    return self._message
    >
    >end
    (gdb) python cmd1 = MyCommand("-my-command", "cmd1")
    (gdb) python print(cmd1.installed)
    True
    (gdb) interpreter-exec mi "-my-command"
    ^done,result="cmd1"
    (gdb) python cmd2 = MyCommand("-my-command", "cmd2")
    (gdb) python print(cmd2.installed)
    True
    (gdb) python print(cmd1.installed)
    False
    (gdb) interpreter-exec mi "-my-command"
    ^done,result="cmd2"
    (gdb) python cmd1.installed = True
    (gdb) python print(cmd2.installed)
    False
    (gdb) python print(cmd1.installed)
    True
    (gdb) interpreter-exec mi "-my-command"
    ^done,result="cmd1"
    (gdb) python cmd1.installed = False
    (gdb) interpreter-exec mi "-my-command"
    ^error,msg="Undefined MI command: my-command",code="undefined-command"
    (gdb)

 3. The top level result name can be changed from 'result' to anything
    the user wants, here's an example session:

    (gdb) python
    >class MyCommand(gdb.MICommand):
    >  def __init__(self):
    >    super(MyCommand, self).__init__("-my-command", "greeting")
    >  def invoke(self, args):
    >    return "Hello World"
    >
    >end
    (gdb) python cmd = MyCommand()
    (gdb) interpreter-exec mi "-my-command"
    ^done,greeting="Hello World"
    (gdb)

I'd love to hear your feedback on this iteration.

Thanks,
Andrew

---

commit 965c93b835c7abc64b0e1baa1b9e194a037fe565
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Tue Jun 23 14:45:38 2020 +0100

    [WIP] gdb: create MI commands using python
    
    This commit allows an user to create custom MI commands using Python
    similarly to what is possible for Python CLI commands.
    
    A new subclass of mi_command is defined for Python MI commands,
    mi_command_py. A new file, py-micmd.c contains the logic for Python MI
    commands.
    
    This commit is based on work linked too from this mailing list thread:
    
      https://sourceware.org/pipermail/gdb/2021-November/049774.html
    
    Which has also been previously posted to the mailing list here:
    
      https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html
    
    And was recently reposted here:
    
      https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html
    
    This patch takes some core code from the previous posted patches, but
    also has some significant differences.
    
    In this patch, I have changed how the lifetime of the Python
    gdb.MICommand objects is managed.  In the original patch, these object
    were kept alive by an owned reference within the mi_command_py object.
    As such, the Python object would not be deleted until the
    mi_command_py object itself was deleted.
    
    This caused a problem, the mi_command_py were held in the global mi
    command table (in mi/mi-cmds.c), which, as a global, was not cleared
    until program shutdown.  By this point the Python interpreter has
    already been shutdown.  Attempting to delete the mi_command_py object
    at this point was causing GDB to try and invoke Python code after
    finalising the Python interpreter, and we would crash.
    
    To work around this problem, the original patch added code in
    python/python.c that would search the mi command table, and delete the
    mi_command_py objects before the Python environment was finalised.
    
    In contrast, in this patch, I have added a new global dictionary to
    the gdb module, gdb.mi_commands.  We already have several such global
    data stores related to pretty printers, and frame unwinders.
    
    The MICommand objects are placed into the new gdb.mi_commands
    dictionary, and it is this reference that keeps the objects alive.
    When GDB's Python interpreter is shut down gdb.mi_commands is deleted,
    and any MICommand objects within it are deleted at this point.
    
    This change avoids having to make the mi_cmd_table global, and walk
    over it from within GDB's python related code.
    
    This patch handles command redefinition entirely within GDB's python
    code, though this does impose one small restriction which is not
    present in the original code (detailed below), I don't think this is a
    big issue.  However, the original patch relied on being able to
    finish executing the mi_command::do_invoke member function after the
    mi_command object had been deleted.  Though continuing to execute a
    member function after an object is deleted is well defined, it is
    also (IMHO) risky, its too easy for someone to later add a use of the
    object without realising that the object might sometimes, have been
    deleted.  The new patch avoids this issue.
    
    The one restriction that is added to avoid this, is that an MICommand
    object can't be reinitialised with a different command name, so:
    
      (gdb) python cmd = MyMICommand("-abc")
      (gdb) python cmd.__init__("-def")
      can't reinitialize object with a different command name
    
    This feels like a pretty weird edge case, and I'm happy to live with
    this restriction.
    
    I have also changed how the memory is managed for the command name.
    In the most recently posted patch series, the command name is moved
    into a subclass of mi_command, the python mi_command_py, which
    inherits from mi_command is then free to use a smart pointer to manage
    the memory for the name.
    
    In this patch, I leave the mi_command class unchanged, and instead
    hold the memory for the name within the Python object, as the lifetime
    of the Python object always exceeds the c++ object stored in the
    mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
    leaves the mi_command class nice and simple.
    
    Next, this patch adds some extra functionality, there's a
    MICommand.name read-only attribute containing the name of the command,
    and a read-write MICommand.installed attribute that can be used to
    install (make the command available for use) and uninstall (remove the
    command from the mi_cmd_table so it can't be used) the command.  This
    attribute will be automatically updated if a second command replaces
    an earlier command.
    
    This patch adds additional error handling, and makes more use the
    gdbpy_handle_exception function.
    
    The gdb.python/py-mi-cmd.exp test script is the official tests for
    this patch, which is based on the original tests from the latest patch
    posted by Jan to the mailing list.
    
    While this patch is in development I have also included a second test
    script gdb.python/py-mi-cmd-orig.exp, this is Jan's tests almost
    unmodified, so I could compare functionality.
    
    I say almost unmodified, there are a few tests in the original test
    script that now fail, these I have marked with setup_kfail.  All of
    these failures are due to changes in error message text.
    
    Co-Authored-By: Jan Vrany <jan.vrany@labware.com>
    
    f#      new file:   gdb/testsuite/gdb.python/py-mi-cmd-orig.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 3efd2227698..4962178fa9b 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index 8c13cefb22f..31189cd07dd 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -146,6 +146,8 @@ show debug lin-lwp
   ** New function gdb.host_charset(), returns a string, which is the
      name of the current host charset.
 
+  ** It is now possible to add GDB/MI commands implemented in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index 6730de05143..7c148bcd0a1 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -204,7 +204,8 @@
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -388,7 +389,8 @@
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2105,7 +2107,7 @@
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3809,11 +3811,12 @@
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
-@cindex commands in python
-@cindex python commands
+@cindex CLI commands in python
+@cindex commands in python, CLI
+@cindex python commands, CLI
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
 command is implemented using an instance of the @code{gdb.Command}
 class, most commonly using a subclass.
@@ -4092,6 +4095,141 @@
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python, GDB/MI
+@cindex python commands, GDB/MI
+It is possible to add @sc{GDB/MI} (@pxref{GDB/MI}) commands
+implemented in Python.  A @sc{GDB/MI} command is implemented using an
+instance of the @code{gdb.MICommand} class, most commonly using a
+subclass.
+
+@defun MICommand.__init__ (name @r{[}, toplevel@r{]})
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid name of a
+@sc{GDB/MI} command, and in particular must start with a hyphen
+(@code{-}).  Reusing the name of a built-in @sc{GDB/MI} is not
+allowed, and will @code{RuntimeError} will be raised.  Using the name
+of an @sc{GDB/MI} command previously defined in Python is allowed, the
+previous command will be replaced with the new command.
+
+@var{toplevel} specifies the toplevel name used for the result in the
+output of this MI command.  If this command doesn't return a result
+then any value passed for @var{toplevel} is ignored.  If
+@var{toplevel} is @code{None} then a default value of @code{result} is
+used, otherwise, @var{toplevel} should be a string.
+@end defun
+
+@defun MICommand.invoke (arguments)
+This method is called by @value{GDBN} when this command is invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method throws a @code{gdb.GdbError} exception, it is turned
+into a @sc{GDB/MI} @code{^error} response.  If this method returns
+@code{None}, then the @sc{GDB/MI} command will return a @code{^done}
+response with no additional value.  Otherwise, the return value is
+converted to a @sc{GDB/MI} value (@pxref{GDB/MI Output Syntax}) as
+follows:
+
+@itemize
+@item If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @emph{list} with elements converted recursively.
+
+@item If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @emph{tuple}.  Keys in that dictionary must be strings.
+Values are converted recursively.
+
+@item Otherwise, value is first converted to Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @emph{const}.
+@end itemize
+
+@end defun
+
+An instance of @code{MICommand} has the following attributes:
+
+@defvar MICommand.name
+A string, the name of this @sc{GDB/MI} command, as was passed to the
+@code{__init__} method.  This attribute is read-only.
+@end defvar
+
+@defvar MICommand.installed
+A boolean value indicating if this command is installed ready for a
+user to call from the command line.  Commands are automatically
+installed when they are instantiated, after which this attribute will
+be @code{True}.
+
+If later, a new command is created with the same name, then the
+original command will become uninstalled, and this attribute will be
+@code{False}.
+
+This attribute can be is read-write, setting this attribute to
+@code{False} will uninstall the command, removing it from the set of
+available commands.  Setting this attribute to @code{True} will
+install the command for use.  If there is already a Python command
+with this name installed, the currently installed command will be
+uninstalled, and this command installed in its place.
+@end defvar
+
+The following code snippet shows how a two trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo arguments passed to the command."""
+
+    def __init__(self, name, mode, toplevel = None):
+        self._mode = mode
+        super(MIEcho, self).__init__(name, toplevel)
+
+    def invoke(self, argv):
+        if self._mode == 'dict':
+            return @{ 'argv' : argv @}
+        elif self._mode == 'list':
+            return argv
+        else:
+            return ", ".join(argv)
+
+
+MIEcho("-echo-dict", "dict")
+MIEcho("-echo-list", "list")
+MIEcho("-echo-string", "string", "argv")
+@end smallexample
+
+The last three lines instantiate the class three times, creating three
+new @sc{GDB/MI} commands @code{-echo-dict}, @code{-echo-list}, and
+@code{-echo-string}.  Each time a subclass of @code{gdb.MICommand} is
+instantiated, the new command is automatically registered with
+@value{GDBN}.
+
+Depending on how the Python code is read into @value{GDBN}, you may
+need to import the @code{gdb} module explicitly.
+
+The following example shows a @value{GDBN} session in which the above
+commands have been added:
+
+@smallexample
+-echo-dict abc def ghi
+^done,result=@{argv=["abc","def","ghi"]@}
+(@value{GDBP})
+-echo-list abc def ghi
+^done,result=["abc","def","ghi"]
+(@value{GDBP})
+-echo-list abc def ghi
+^done,argv=["abc","def","ghi"]
+(@value{GDBP})
+@end smallexample
+
+Notice that for the @code{-echo-string} command the toplevel result
+name was changed from @samp{result} to @samp{argv}.
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4129,7 +4267,7 @@
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..dd0243e5bfe 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -113,12 +109,12 @@ struct mi_command_cli : public mi_command
    not have been added to mi_cmd_table.  Otherwise, return true, and
    COMMAND was added to mi_cmd_table.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
 
-  const std::string &name = command->name ();
+  const std::string name (command->name ());
 
   if (mi_cmd_table.find (name) != mi_cmd_table.end ())
     return false;
@@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+bool
+remove_mi_cmd_entry (mi_command *command)
+{
+  gdb_assert (command != nullptr);
+
+  const std::string name (command->name ());
+
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..3f4fb854d68 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the MI_CMD_TABLE.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,15 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert a new mi-command into the command table.  Return true if
+   insertion was successful.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove a mi-command from the command table.  Return true if the removal
+   was success, otherwise return false.  */
+
+extern bool remove_mi_cmd_entry (mi_command *command);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 11a1b444bfd..a9197eb4ffe 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -81,6 +81,9 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Hash containing all user created MI commands, the key is the command
+# name, and the value is the gdb.MICommand object.
+mi_commands = {}
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..2c3c9fe267a
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,767 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* gdb MI commands implemented in Python  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "cli/cli-cmds.h"
+
+#include <string>
+
+/* Debugging of Python mi commands.  */
+
+static bool pymicmd_debug;
+
+/* Implementation of "show debug py-micmd".  */
+
+static void
+show_pymicmd_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Python mi command debugging is %s.\n"), value);
+}
+
+/* Print a "py-micmd" debug statement.  */
+
+#define pymicmd_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
+
+/* Print a "py-micmd" enter/exit debug statements.  */
+
+#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
+  scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
+
+struct mi_command_py;
+
+/* Representation of a python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the mi command table.  This
+     pointer can be nullptr if the command is not currently installed into
+     the mi command table (see gdb.MICommand.installed property).  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing the name of this command, without the leading
+     dash.  This string is never nullptr once the python object has been
+     initialised.
+
+     The memory for this string was allocated with malloc, and needs to be
+     deallocated with free when the python object is deallocated.
+
+     When the MI_COMMAND variable is not nullptr, then the mi_command_py
+     object's name will point back to this string.  */
+  char *mi_command_name;
+
+  /* The name used for the toplevel result.  This can be nullptr, in which
+     case the default value should be used.  If this is not nullptr, then
+     the string was allocated with malloc, and needs to be freed when the
+     python object is deallocated.  */
+  char *mi_result_name;
+};
+
+/* The mi command implemented in python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object should inherit from gdb.MICommand and should
+     implement method invoke (args). */
+
+  mi_command_py (const char *name, micmdpy_object *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  {
+    pymicmd_debug_printf ("this = %p", this);
+  }
+
+  ~mi_command_py ()
+  {
+    /* The python object representing a mi command contains a pointer back
+       to this c++ object.  We can safely set this pointer back to nullptr
+       now, to indicate the python object no longer references a valid c++
+       object.
+
+       However, the python object also holds the storage for our name
+       string.  We can't clear that here as our parent's destructor might
+       still want to reference that string.  Instead we rely on the python
+       object deallocator to free that memory, and reset the pointer.  */
+    m_pyobj->mi_command = nullptr;
+
+    pymicmd_debug_printf ("this = %p", this);
+  };
+
+  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the mi
+     command table correctly.  This function looks up the command in the mi
+     command table and checks that the object we get back references
+     CMD_OBJ.  This function is only intended for calling within a
+     gdb_assert.  This function performs many assertions internally, and
+     then always returns true.  */
+  static bool validate_installation (micmdpy_object *cmd_obj);
+
+  /* Update m_pyobj to NEW_PYOBJ.  The pointer from M_PYOBJ that points
+     back to this object is swapped with the pointer in NEW_PYOBJ, which
+     must be nullptr, so that NEW_PYOBJ now points back to this object.
+     Additionally our parent's name string is stored in m_pyobj, so we
+     swap the name string with NEW_PYOBJ.
+
+     Before this call m_pyobj is the python object representing this mi
+     command object.  After this call has completed, NEW_PYOBJ now
+     represents this mi command object.  */
+  void swap_python_object (micmdpy_object *new_pyobj)
+  {
+    gdb_assert (new_pyobj->mi_command == nullptr);
+    std::swap (new_pyobj->mi_command, m_pyobj->mi_command);
+    std::swap (new_pyobj->mi_command_name, m_pyobj->mi_command_name);
+    m_pyobj = new_pyobj;
+  }
+
+protected:
+  /* Called when the mi command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The python object representing this mi command.  */
+  micmdpy_object *m_pyobj;
+};
+
+extern PyTypeObject micmdpy_object_type
+	CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* Holds a python object containing the string 'invoke'.  */
+
+static PyObject *invoke_cst;
+
+/* Parse RESULT and print it in mi format to the current_uiout.  FIELD_NAME
+   is used as the name of this result field.
+
+   RESULT can be a dictionary, a sequence, an iterator, or an object that
+   can be converted to a string, these are converted to the matching mi
+   output format (dictionaries as tuples, sequences and iterators as lists,
+   and strings as named fields).
+
+   If anything goes wrong while formatting the output then an error is
+   thrown.  */
+
+static void
+parse_mi_result (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  if (!PyString_Check (key))
+	    {
+	      gdbpy_ref<> key_repr (PyObject_Repr (key));
+	      gdb::unique_xmalloc_ptr<char> key_repr_string;
+	      if (key_repr != nullptr)
+		key_repr_string
+		  = python_string_to_target_string (key_repr.get ());
+
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+              else
+		error (_("non-string object used as key: %s"),
+		       key_repr_string.get ());
+	    }
+
+	  gdb::unique_xmalloc_ptr<char> key_string
+	    = python_string_to_target_string (key);
+	  if (key_string == nullptr)
+	    gdbpy_handle_exception ();
+	  parse_mi_result (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      Py_ssize_t len = PySequence_Size (result);
+      if (len == -1)
+	gdbpy_handle_exception ();
+      for (Py_ssize_t i = 0; i < len; ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  if (item == nullptr)
+	    gdbpy_handle_exception ();
+	  parse_mi_result (item.get (), nullptr);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (true)
+	{
+	  item.reset (PyIter_Next (result));
+	  if (item == nullptr)
+	    {
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+	      break;
+	    }
+	  parse_mi_result (item.get (), nullptr);
+	}
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      if (string == nullptr)
+	gdbpy_handle_exception ();
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Called when the mi command is invoked.  PARSE contains the parsed
+   command line arguments from the user.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == nullptr)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = (PyObject *) this->m_pyobj;
+  gdb_assert (obj != nullptr);
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  if (!PyObject_HasAttr (obj, invoke_cst))
+      error (_("-%s: Python command object missing 'invoke' method."),
+	     name ());
+
+  /* Place all the arguments into a list which we pass as a single
+     argument to the mi command's invoke method.  */
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    {
+      gdbpy_print_stack ();
+      error (_("-%s: failed to create the Python arguments list."),
+	     name ());
+    }
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i],
+					 strlen (parse->argv[i]),
+					 host_charset (), nullptr));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
+	{
+	  gdbpy_print_stack ();
+	  error (_("-%s: failed to create the Python arguments list."),
+		 name ());
+	}
+    }
+
+  gdb_assert (PyErr_Occurred () == nullptr);
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, invoke_cst,
+						  argobj.get (), nullptr));
+  if (result == nullptr)
+    gdbpy_handle_exception ();
+
+  if (result != Py_None)
+    {
+      micmdpy_object *cmd = (micmdpy_object *) obj;
+      const char *result_name
+	= (cmd->mi_result_name == nullptr ? "result" : cmd->mi_result_name);
+      parse_mi_result (result.get (), result_name);
+    }
+}
+
+/* See declaration above.  */
+
+bool
+mi_command_py::validate_installation (micmdpy_object *cmd_obj)
+{
+  gdb_assert (cmd_obj != nullptr);
+  mi_command_py *cmd = cmd_obj->mi_command;
+  gdb_assert (cmd != nullptr);
+  const char *name = cmd_obj->mi_command_name;
+  gdb_assert (name != nullptr);
+  gdb_assert (name == cmd->name ());
+  mi_command *mi_cmd = mi_cmd_lookup (name);
+  gdb_assert (mi_cmd == cmd);
+  gdb_assert (cmd->m_pyobj == cmd_obj);
+
+  return true;
+}
+
+/* Return a reference to the gdb.mi_commands dictionary.  */
+
+static gdbpy_ref<>
+micmdpy_global_command_dictionary ()
+{
+  if (gdb_python_module == nullptr
+      || ! PyObject_HasAttrString (gdb_python_module, "mi_commands"))
+    error (_("unable to find gdb.mi_commands dictionary"));
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "mi_commands"));
+  if (mi_cmd_dict == nullptr || !PyDict_Check (mi_cmd_dict.get ()))
+    error (_("unable to fetch gdb.mi_commands dictionary"));
+
+  return mi_cmd_dict;
+}
+
+/* Uninstall OBJ, making the mi command represented by OBJ unavailable for
+   use by the user.  On success 0 is returned, otherwise -1 is returned
+   and a python exception will be set.  */
+
+static int
+micmdpy_uninstall_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command != nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  /* Remove the command from the internal mi table of commands, this will
+     cause the c++ object to be deleted, which will clear the mi_command
+     member variable within the python object.  */
+  remove_mi_cmd_entry (obj->mi_command);
+  gdb_assert (obj->mi_command == nullptr);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+
+  /* Grab the name for this command.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+  if (name_obj == nullptr)
+    return -1;
+
+  /* Lookup the gdb.MICommand object in the dictionary of all python mi
+     commands, this is gdb.mi_command, and remove it.  */
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+
+  /* Did we encounter an error?  Failing to find the object in the
+     dictionary isn't an error, that's fine.  */
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+
+  /* Did we find this command in the gdb.mi_commands dictionary?  If so,
+     then remove it.  */
+  if (curr != nullptr)
+    {
+      /* Yes we did!  Remove it.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  return 0;
+}
+
+/* Install OBJ as a usable mi command.  Return 0 on success, and -1 on
+   error, in which case, a python error will have been set.
+
+   After successful completion the command name associated with OBJ will
+   be installed in the mi command table (so it can be found if the user
+   enters that command name), additionally, OBJ will have been added to
+   the gdb.mi_commands dictionary (using the command name as its key),
+   this will ensure that OBJ remains live even if the user gives up all
+   references.  */
+
+static int
+micmdpy_install_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command == nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+
+  /* Look up this command name in the gdb.mi_commands dictionary, a command
+     with this name may already exist.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb.mi_commands
+	 dictionary.  First, validate that the object in the dictionary is
+	 of the expected type, just in case something weird has happened.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb.mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* To get to this function OBJ should not be installed, which should
+	 mean OBJ is not in the gdb.mi_commands dictionary.  If we find
+	 that OBJ is the thing in the dictionary, then something weird is
+	 going on, we should throw an error.  */
+      micmdpy_object *other = (micmdpy_object *) curr;
+      if (other == obj || other->mi_command == nullptr)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("uninstalled command found in gdb.mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* All python mi command object should always have a name set.  */
+      gdb_assert (other->mi_command_name != nullptr);
+
+      /* We always insert commands into the gdb.mi_commands dictionary
+	 using their name as a key, if this check fails then the dictionary
+	 is in some weird state.  */
+      if (other->mi_command_name != other->mi_command->name ()
+	  || strcmp (other->mi_command_name, obj->mi_command_name) != 0)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("gdb.mi_commands dictionary is corrupted"));
+	  return -1;
+	}
+
+      /* Switch the state of the c++ object held in the mi command table
+	 so that it now references OBJ.  After this action the old python
+	 object that used to be referenced from the mi command table will
+	 now show as uninstalled, while the new python object will show as
+	 installed.  */
+      other->mi_command->swap_python_object (obj);
+
+      gdb_assert (other->mi_command == nullptr);
+      gdb_assert (obj->mi_command != nullptr);
+      gdb_assert (obj->mi_command->name () == obj->mi_command_name);
+
+      /* Remove the previous python object from the gdb.mi_commands
+	 dictionary, we'll install the new object below.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+       return -1;
+    }
+  else
+    {
+      /* There's no python object for this command name in the
+	 gdb.mi_commands dictionary from which we can steal an existing
+	 object already held in the mi commands table, and so, we now
+	 create a new c++ object, and install it into the mi table.  */
+      obj->mi_command = new mi_command_py (obj->mi_command_name, obj);
+      mi_command_up micommand (obj->mi_command);
+
+      /* Add the command to the gdb internal mi command table.  */
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+      if (!result)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+    }
+
+  /* Finally, add the python object to the gdb.mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), (PyObject *) obj) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.__init__.  The init method takes the name of
+   the mi command as the first argument, which must be a string, starting
+   with a single dash.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  static const char *keywords[] = { "name", "toplevel", nullptr };
+  const char *name;
+  const char *toplevel = nullptr;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s|z", keywords,
+					&name, &toplevel))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      error (_("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      error (_("MI command name does not start with '-'"
+               " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    {
+      for (int i = 2; i < name_len; i++)
+	{
+	  if (!isalnum (name[i]) && name[i] != '-')
+	    {
+	      error (_("MI command name contains invalid character: %c."),
+		     name[i]);
+	      return -1;
+	    }
+	}
+
+      /* Skip over the leading dash.  For the rest of this function the
+	 dash is not important.  */
+      ++name;
+    }
+
+  /* Validate the requested name for the toplevel result.  Also, if the
+     user has provided the default then we don't need to store that as a
+     separate string.  */
+  if (toplevel != nullptr && *toplevel == '\0')
+    error (_("-%s: Invalid name for toplevel result."), name);
+  if (toplevel != nullptr && strcmp (toplevel, "result") == 0)
+    toplevel = nullptr;
+
+  /* This command may have been previously initialised.  If it has, discard
+     the previous toplevel result name, and store the new name.  */
+  xfree (cmd->mi_result_name);
+  if (toplevel != nullptr)
+    cmd->mi_result_name = xstrdup (toplevel);
+  else
+    cmd->mi_result_name = nullptr;
+
+  /* Check that there's an 'invoke' method.  */
+  if (!PyObject_HasAttr (self, invoke_cst))
+    error (_("-%s: Python command object missing 'invoke' method."), name);
+
+  /* If this object already has a name set, then this object has been
+     initialized before.  We handle this case a little differently.  */
+  if (cmd->mi_command_name != nullptr)
+    {
+      /* First, we don't allow the user to change the mi command name.
+	 Supporting this would be tricky as we would need to delete the
+	 mi_command_py from the mi command table, however, the user might
+	 be trying to perform this reinitialization from within the very
+	 command we're about to delete... it all gets very messy.
+
+	 So, for now at least, we don't allow this.  This doesn't seem like
+	 an excessive restriction.  */
+      if (strcmp (cmd->mi_command_name, name) != 0)
+	error (_("can't reinitialize object with a different command name"));
+
+      /* If there's already an object registered with the mi command table,
+	 then we're done.  That object must be a mi_command_py, which
+	 should reference back to this micmdpy_object.  */
+      if (cmd->mi_command != nullptr)
+	{
+	  gdb_assert (mi_command_py::validate_installation (cmd));
+	  return 0;
+	}
+    }
+  else
+    cmd->mi_command_name = xstrdup (name);
+
+  /* Now we can install this mi_command_py in the mi command table.  */
+  return micmdpy_install_command (cmd);
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+
+  /* If the python object failed to initialize, then the name field might
+     be nullptr.  */
+  pymicmd_debug_printf ("obj = %p, name = %s", cmd,
+			(cmd->mi_command_name == nullptr
+			 ? "(null)" : cmd->mi_command_name));
+
+  /* Remove the command from the mi command table if needed.  This will
+     cause the mi_command_py object to be deleted, which, in turn, will
+     clear the cmd->mi_command member variable, hence the assert.  */
+  if (cmd->mi_command != nullptr)
+    remove_mi_cmd_entry (cmd->mi_command);
+  gdb_assert (cmd->mi_command == nullptr);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->mi_command_name);
+  cmd->mi_command_name = nullptr;
+
+  /* Free the toplevel result name.  */
+  xfree (cmd->mi_result_name);
+  cmd->mi_result_name = nullptr;
+
+  /* Finally, free the memory for this python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Python initialization for the mi commands components.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == nullptr)
+    return -1;
+
+  return 0;
+}
+
+/* Get the gdb.MICommand.name attribute, returns a string, the name of this
+   mi command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->mi_command_name != nullptr);
+  std::string name_str = string_printf ("-%s", micmd_obj->mi_command_name);
+  return PyString_FromString (name_str.c_str ());
+}
+
+/* Get the gdb.MICommand.installed property.  Returns true if this mi
+   command is installed into the mi command table, otherwise returns
+   false.  */
+
+static PyObject *
+micmdpy_get_installed (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  if (micmd_obj->mi_command == nullptr)
+    Py_RETURN_FALSE;
+  Py_RETURN_TRUE;
+}
+
+/* Set the gdb.MICommand.installed property.  The property can be set to
+   either true or false.  Setting the property to true will cause the
+   command to be installed into the mi command table (if it isn't
+   already), while setting this property to false will cause the command
+   to be removed from the mi command table (if it is present).  */
+
+static int
+micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  bool installed_p = PyObject_IsTrue (newvalue);
+  if (installed_p == (micmd_obj->mi_command != nullptr))
+    return 0;
+
+  if (installed_p)
+    return micmdpy_install_command (micmd_obj);
+  else
+    return micmdpy_uninstall_command (micmd_obj);
+}
+
+/* The gdb.MICommand properties.   */
+
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { "installed", micmdpy_get_installed, micmdpy_set_installed,
+    "Is this command installed for use.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+/* The gdb.MICommand descriptor.  */
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (nullptr, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
+
+void _initialize_py_micmd ();
+void
+_initialize_py_micmd ()
+{
+  add_setshow_boolean_cmd
+    ("py-micmd", class_maintenance, &pymicmd_debug,
+     _("Set Python micmd debugging."),
+     _("Show Python micmd debugging."),
+     _("When on, Python micmd debugging is enabled."),
+     nullptr,
+     show_pymicmd_debug,
+     &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 583989c5a6d..d5a0b4a4a91 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -561,6 +561,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 4dcda53d9ab..f2b09916374 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1887,7 +1887,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp b/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp
new file mode 100644
index 00000000000..d8ea8bbfdab
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd-orig.exp
@@ -0,0 +1,140 @@
+# Copyright (C) 2019-2021 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi2"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+#
+# Start here
+#
+
+
+mi_gdb_test "set python print-stack full" \
+  ".*\\^done" \
+  "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+  ".*\\^done" \
+  "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+  ".*\\^done" \
+  "define -pycmd MI command"
+
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Hello world!\"" \
+  "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+  "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+  "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+  "\\^done,result={hello=\"world\",times=\"42\"}" \
+  "-pycmd dct"
+
+setup_kfail "small change to error message format" *-*-*
+mi_gdb_test "-pycmd bk1" \
+  "\\^error,msg=\"Non-string object used as key: Bad Key\\.\"" \
+  "-pycmd bk1"
+
+setup_kfail "small change to error message format" *-*-*
+mi_gdb_test "-pycmd bk2" \
+  "\\^error,msg=\"Non-string object used as key: 1\\.\"" \
+  "-pycmd bk2"
+
+setup_kfail "bigger change to error message format" *-*-*
+mi_gdb_test "-pycmd bk3" \
+  "\\^error,msg=\"Non-string object used as key: __repr__ returned non-string .*" \
+  "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+  "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+  "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+  "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+  "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+  "\\^done" \
+  "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+  "\\^done,result=\\\[\"None\"\\\]" \
+  "-pycmd nn2"
+
+setup_kfail "no prefix on error messages" *-*-*
+mi_gdb_test "-pycmd bogus" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: bogus\"" \
+  "-pycmd bogus"
+
+setup_kfail "no prefix on error messages" *-*-*
+mi_gdb_test "-pycmd exp" \
+  "\\^error,msg=\"-pycmd: failed to execute command\"" \
+  "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+  ".*\\^done" \
+  "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+  "\\^done,result=\"Ciao!\"" \
+  "-pycmd str - redefined from CLI"
+
+setup_kfail "no prefix on error messages" *-*-*
+mi_gdb_test "-pycmd int" \
+  "\\^error,msg=\"-pycmd: Invalid parameter: int\"" \
+  "-pycmd int - redefined from CLI"
+
+setup_kfail "no prefix on error messages" *-*-*
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"-pycmd: Command redefined but we failing anyway\"" \
+  "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+  "\\^done,result=\"42\"" \
+  "-pycmd int - redefined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+  ".*\\^error,msg=\"MI command name is empty.\"" \
+  "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+  ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit\\.\"" \
+  "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+  ".*\\^error,msg=\"MI command name contains invalid character: @\\.\"" \
+  "invalid character in MI command name"
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd-orig.py b/gdb/testsuite/gdb.python/py-mi-cmd-orig.py
new file mode 100644
index 00000000000..2f6ba2f8037
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd-orig.py
@@ -0,0 +1,68 @@
+# Copyright (C) 2019-2021 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'int':
+            return 42
+        elif argv[0] == 'str':
+            return "Hello world!"
+        elif argv[0] == 'ary':
+            return [ 'Hello', 42 ]
+        elif argv[0] == "dct":
+            return { 'hello' : 'world', 'times' : 42}
+        elif argv[0] == "bk1":
+            return { BadKey() : 'world' }
+        elif argv[0] == "bk2":
+            return { 1 : 'world' }
+        elif argv[0] == "bk3":
+            return { ReallyBadKey() : 'world' }
+        elif argv[0] == 'tpl':
+            return ( 42 , 'Hello' )
+        elif argv[0] == 'itr':
+            return iter([1,2,3])
+        elif argv[0] == 'nn1':
+            return None
+        elif argv[0] == 'nn2':
+            return [ None ]
+        elif argv[0] == 'red':
+            pycmd2('-pycmd')
+            return None
+        elif argv[0] == 'exp':
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'str':
+            return "Ciao!"
+        elif argv[0] == 'red':
+            pycmd1('-pycmd')
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..d680aaf7adb
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,258 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi2"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+#
+# Start here
+#
+
+
+mi_gdb_test "set python print-stack full" \
+    ".*\\^done" \
+    "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+    ".*\\^done" \
+    "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+    ".*\\^done" \
+    "define -pycmd MI command"
+
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+    "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+    "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+    "\\^done,result={hello=\"world\",times=\"42\"}" \
+    "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+    "\\^error,msg=\"non-string object used as key: Bad Key\"" \
+    "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+    "\\^error,msg=\"non-string object used as key: 1\"" \
+    "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+    [multi_line \
+	 "&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
+	 "\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
+    "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+    "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+    "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+    "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+    "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+    "\\^done" \
+    "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+    "\\^done,result=\\\[\"None\"\\\]" \
+    "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+    "\\^error,msg=\"Invalid parameter: bogus\"" \
+    "-pycmd bogus"
+
+# With this argument the command raises a gdb.GdbError with no message
+# string.  GDB considers this a bug in the user program, so prints a
+# backtrace, and a generic error message.
+mi_gdb_test "-pycmd exp" \
+    [multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
+	 "&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
+	 "&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
+	 "&\"gdb.GdbError..\"" \
+	 "\\^error,msg=\"Error occurred in Python\\.\""] \
+    "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+    ".*\\^done" \
+    "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+    "\\^error,msg=\"Invalid parameter: int\"" \
+    "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+    "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"Command redefined but we failing anyway\"" \
+    "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int - redefined from MI"
+
+mi_gdb_test "-pycmd-new int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd-new int - defined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+    ".*\\^error,msg=\"MI command name is empty.\"" \
+    "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+    ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit\\.\"" \
+    "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+    ".*\\^error,msg=\"MI command name contains invalid character: @\\.\"" \
+    "invalid character in MI command name"
+
+mi_gdb_test "python cmd=pycmd1('-abc')" \
+    ".*\\^done" \
+    "create command -abc, stored in a python variable"
+
+mi_gdb_test "python print(cmd.name)" \
+    ".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
+    "print the name of the stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str"
+
+mi_gdb_test "python cmd.installed = False" \
+    ".*\\^done" \
+    "uninstall the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
+    "-abc str, but now the command is gone"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the command is back again"
+
+mi_gdb_test "python other=pycmd2('-abc')" \
+    ".*\\^done" \
+    "create another command called -abc, stored in a separate python variable"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the other stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "print the installed status of the original stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-abc str, when the other command is in place"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the original mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the original command is back again"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "the other command is now not installed"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "the original command is now installed"
+
+mi_gdb_test "python aa=pycmd3('-aa', 'message one')" \
+    ".*\\^done" \
+    "created a new -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message one\"}" \
+    "call the -aa command"
+
+mi_gdb_test "python aa.__init__('-aa', 'message two')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message two\"}" \
+    "call the -aa command, get the new message"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three')" \
+    ".*\\^error,msg=\"can't reinitialize object with a different command name\"" \
+    "attempt to reinitialise aa variable to a new command name"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message two\"}" \
+    "check the aa object has not changed after failed initialization"
+
+mi_gdb_test "python aa.installed = False" \
+    ".*\\^done" \
+    "uninstall the -aa command"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three')" \
+    ".*\\^error,msg=\"can't reinitialize object with a different command name\"" \
+    "attempt to reinitialise aa variable to a new command name while uninstalled"
+
+mi_gdb_test "python aa.__init__('-aa', 'message three')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message while uninstalled"
+
+mi_gdb_test "python aa.installed = True" \
+    ".*\\^done" \
+    "install the -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message three\"}" \
+    "call the -aa command looking for message three"
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..e47034ae4b5
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,82 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'int':
+            return 42
+        elif argv[0] == 'str':
+            return "Hello world!"
+        elif argv[0] == 'ary':
+            return [ 'Hello', 42 ]
+        elif argv[0] == "dct":
+            return { 'hello' : 'world', 'times' : 42}
+        elif argv[0] == "bk1":
+            return { BadKey() : 'world' }
+        elif argv[0] == "bk2":
+            return { 1 : 'world' }
+        elif argv[0] == "bk3":
+            return { ReallyBadKey() : 'world' }
+        elif argv[0] == 'tpl':
+            return ( 42 , 'Hello' )
+        elif argv[0] == 'itr':
+            return iter([1,2,3])
+        elif argv[0] == 'nn1':
+            return None
+        elif argv[0] == 'nn2':
+            return [ None ]
+        elif argv[0] == 'red':
+            pycmd2('-pycmd')
+            return None
+        elif argv[0] == 'exp':
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'str':
+            return "Ciao!"
+        elif argv[0] == 'red':
+            pycmd1('-pycmd')
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == 'new':
+            pycmd1('-pycmd-new')
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+# This class creates a command that returns a string, which is passed
+# when the command is created.  This class also makes use of a custom
+# toplevel result name, 'xxx' in this case.
+class pycmd3(gdb.MICommand):
+    def __init__(self, name, msg):
+        super(pycmd3, self).__init__(name, 'xxx')
+        self._msg = msg
+
+    def invoke(self, args):
+        return { 'msg' : self._msg }


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 0/5] create GDB/MI commands using python
  2022-01-21 15:22     ` Andrew Burgess
@ 2022-01-24 12:59       ` Jan Vrany
  2022-02-02 16:57         ` Andrew Burgess
  2022-02-06 21:16       ` Simon Marchi
  1 sibling, 1 reply; 41+ messages in thread
From: Jan Vrany @ 2022-01-24 12:59 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches

On Fri, 2022-01-21 at 15:22 +0000, Andrew Burgess wrote:
> * Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-18 15:13:01 +0000]:
> 
> > On Tue, 2022-01-18 at 13:55 +0000, Andrew Burgess wrote:
> > > * Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-17 12:44:20 +0000]:
> > > 
> > > > This is a restart of an earlier attempts to allow custom
> > > > GDB/MI commands written in Python.
> > > 
> > > Thanks for continuing to work on this feature.
> > > 
> > > I too had been looking at getting the remaining patches from your
> > > series upstream, and I'd like to discuss some of the differences
> > > between the approaches we took.
> > 
> > Great, thanks a lot!
> > 
> > > 
> > > At the end of this mail you'll find my current work-in-progress
> > > patch, it definitely needs the docs and NEWS entries adding, as well
> > > as a few extra tests.  However, functionality wise I think its mostly
> > > there.
> > > 
> > > My patch includes two sets of tests, gdb.python/py-mi-cmd.exp and
> > > gdb.python/py-mi-cmd-orig.exp.  The former is based on your tests, but
> > > with some tweaks based on changes I made.  The latter set is your
> > > tests taken from this m/l thread.
> > > 
> > > When running the gdb.python/py-mi-cmd-orig.exp tests there should be
> > > just 2 failures, both related to the same feature which is not present
> > > in my version, that is, the ability of a command to redefine itself,
> > > like this:
> > > 
> > >       class my_command(gdb.MICommand):
> > >         def invoke(self, args):
> > >           my_command("-blah")
> > >           return None
> > > 
> > >        my_command("-blah")
> > > 
> > > this works with your version, but not with mine, this is because I'm
> > > using python's own reference counting to track when a command can be
> > > redefined or not, and, when you are within a commands invoke method
> > > the reference count of the containing object is incremented, and this
> > > prevents gdb from deleting the command.
> > > 
> > > My question then, is how important is this feature, and what use case
> > > do you see for this?  Or, was support for this just a happy side
> > > effect of the implementation approach you chose?
> > 
> > Initially I did not think of it and it was - IIRC - pointed out in
> > some review. I thought to be a corner case, but it turned out to be
> > (potentiality very useful feature. 
> > 
> > While developing custom commands, pretty printers and so on, it is useful
> > to be able to "reload" Python code into running GDB without need to restart 
> > it. To support that, I created a "pr" command which reloads all custom python 
> > code (well, tries to at least), see 
> > 
> > https://urldefense.proofpoint.com/v2/url?u=https-3A__swing.fit.cvut.cz_hg_jv-2Dvdb_file_tip_python_vdb_cli.py-23l20&d=DwIFAw&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=aunQh9rOF7oPv3b6WEJbNxADRSv4-8Dy2BMsx38ToFU&s=jnwDUKw3EN3ruyHZoFfwtAJykINwXr92bHBvZHlvn1k&e= 
> > 
> > So ultimately, this custom "pr" command's invoke() causes redefinition
> > of the "pr" command itself. I have not done it yet, but having 
> > menu item/icon in (my) GDB frontend using something like -python-reload`
> > seems desirable from UX POV.
> 
> Thanks, that's not an unreasonable use case.  I've tweaked things so
> that this case is now supported.
> 
> I've gone through that patch and ported over your NEWS and doc
> changes, with some updates based on Eli's feedback, as well as some
> new content to cover some of the changes in my patch.
> 
> For the testing, I have, for now, kept gdb.python/py-mi-cmd-orig.exp,
> which is your test file taken from your patch series.  I added some
> 'setup_kfail' markers to 7 tests that now fail due to changes in error
> strings, hopefully, this will allow you to review what changed.
> However, except for the differences in error string, everything your
> original series supported is now supported by this patch.
> 
> From a user's point of view, I think the only differences with your
> patch are:
> 
>  1. New .name attribute, that returns the name of the command as a
>     string, here's an example session:
> 
>     (gdb) python
>     >class MyCommand(gdb.MICommand):
>     >  def __init__(self):
>     >    super(MyCommand, self).__init__("-my-command")
>     >  def invoke(self, args):
>     >    return None
>     >
>     >end
>     (gdb) python cmd = MyCommand()
>     (gdb) python print(cmd.name)
>     -my-command
>     (gdb)
> 
>  2. New .installed attribute, that allows a command to be installed or
>     removed from the mi command table, here's an example session:
> 
>     (gdb) python
>     >class MyCommand(gdb.MICommand):
>     >  def __init__(self, name, message):
>     >    self._message = message
>     >    super(MyCommand, self).__init__(name)
>     >
>     >  def invoke(self, args):
>     >    return self._message
>     >
>     >end
>     (gdb) python cmd1 = MyCommand("-my-command", "cmd1")
>     (gdb) python print(cmd1.installed)
>     True
>     (gdb) interpreter-exec mi "-my-command"
>     ^done,result="cmd1"
>     (gdb) python cmd2 = MyCommand("-my-command", "cmd2")
>     (gdb) python print(cmd2.installed)
>     True
>     (gdb) python print(cmd1.installed)
>     False
>     (gdb) interpreter-exec mi "-my-command"
>     ^done,result="cmd2"
>     (gdb) python cmd1.installed = True
>     (gdb) python print(cmd2.installed)
>     False
>     (gdb) python print(cmd1.installed)
>     True
>     (gdb) interpreter-exec mi "-my-command"
>     ^done,result="cmd1"
>     (gdb) python cmd1.installed = False
>     (gdb) interpreter-exec mi "-my-command"
>     ^error,msg="Undefined MI command: my-command",code="undefined-command"
>     (gdb)
> 
>  3. The top level result name can be changed from 'result' to anything
>     the user wants, here's an example session:
> 
>     (gdb) python
>     >class MyCommand(gdb.MICommand):
>     >  def __init__(self):
>     >    super(MyCommand, self).__init__("-my-command", "greeting")
>     >  def invoke(self, args):
>     >    return "Hello World"
>     >
>     >end
>     (gdb) python cmd = MyCommand()
>     (gdb) interpreter-exec mi "-my-command"
>     ^done,greeting="Hello World"
>     (gdb)
> 
> I'd love to hear your feedback on this iteration.

I'm personally happy with this iteration. I like your improvements 
to the Python API! I also tested it briefly with my Python code & 
frontend just to see and it worked just fine. 

Perhaps, I'd rename mi_commands module variable to _mi_commands to 
convey the fact it is "internal" to the implementation and should not
be touched (?).

Thanks!

Jan


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 0/5] create GDB/MI commands using python
  2022-01-24 12:59       ` Jan Vrany
@ 2022-02-02 16:57         ` Andrew Burgess
  0 siblings, 0 replies; 41+ messages in thread
From: Andrew Burgess @ 2022-02-02 16:57 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> writes:

> On Fri, 2022-01-21 at 15:22 +0000, Andrew Burgess wrote:
>> * Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-18 15:13:01 +0000]:
>> 
>> > On Tue, 2022-01-18 at 13:55 +0000, Andrew Burgess wrote:
>> > > * Jan Vrany via Gdb-patches <gdb-patches@sourceware.org> [2022-01-17 12:44:20 +0000]:
>> > > 
>> > > > This is a restart of an earlier attempts to allow custom
>> > > > GDB/MI commands written in Python.
>> > > 
>> > > Thanks for continuing to work on this feature.
>> > > 
>> > > I too had been looking at getting the remaining patches from your
>> > > series upstream, and I'd like to discuss some of the differences
>> > > between the approaches we took.
>> > 
>> > Great, thanks a lot!
>> > 
>> > > 
>> > > At the end of this mail you'll find my current work-in-progress
>> > > patch, it definitely needs the docs and NEWS entries adding, as well
>> > > as a few extra tests.  However, functionality wise I think its mostly
>> > > there.
>> > > 
>> > > My patch includes two sets of tests, gdb.python/py-mi-cmd.exp and
>> > > gdb.python/py-mi-cmd-orig.exp.  The former is based on your tests, but
>> > > with some tweaks based on changes I made.  The latter set is your
>> > > tests taken from this m/l thread.
>> > > 
>> > > When running the gdb.python/py-mi-cmd-orig.exp tests there should be
>> > > just 2 failures, both related to the same feature which is not present
>> > > in my version, that is, the ability of a command to redefine itself,
>> > > like this:
>> > > 
>> > >       class my_command(gdb.MICommand):
>> > >         def invoke(self, args):
>> > >           my_command("-blah")
>> > >           return None
>> > > 
>> > >        my_command("-blah")
>> > > 
>> > > this works with your version, but not with mine, this is because I'm
>> > > using python's own reference counting to track when a command can be
>> > > redefined or not, and, when you are within a commands invoke method
>> > > the reference count of the containing object is incremented, and this
>> > > prevents gdb from deleting the command.
>> > > 
>> > > My question then, is how important is this feature, and what use case
>> > > do you see for this?  Or, was support for this just a happy side
>> > > effect of the implementation approach you chose?
>> > 
>> > Initially I did not think of it and it was - IIRC - pointed out in
>> > some review. I thought to be a corner case, but it turned out to be
>> > (potentiality very useful feature. 
>> > 
>> > While developing custom commands, pretty printers and so on, it is useful
>> > to be able to "reload" Python code into running GDB without need to restart 
>> > it. To support that, I created a "pr" command which reloads all custom python 
>> > code (well, tries to at least), see 
>> > 
>> > https://urldefense.proofpoint.com/v2/url?u=https-3A__swing.fit.cvut.cz_hg_jv-2Dvdb_file_tip_python_vdb_cli.py-23l20&d=DwIFAw&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=aunQh9rOF7oPv3b6WEJbNxADRSv4-8Dy2BMsx38ToFU&s=jnwDUKw3EN3ruyHZoFfwtAJykINwXr92bHBvZHlvn1k&e= 
>> > 
>> > So ultimately, this custom "pr" command's invoke() causes redefinition
>> > of the "pr" command itself. I have not done it yet, but having 
>> > menu item/icon in (my) GDB frontend using something like -python-reload`
>> > seems desirable from UX POV.
>> 
>> Thanks, that's not an unreasonable use case.  I've tweaked things so
>> that this case is now supported.
>> 
>> I've gone through that patch and ported over your NEWS and doc
>> changes, with some updates based on Eli's feedback, as well as some
>> new content to cover some of the changes in my patch.
>> 
>> For the testing, I have, for now, kept gdb.python/py-mi-cmd-orig.exp,
>> which is your test file taken from your patch series.  I added some
>> 'setup_kfail' markers to 7 tests that now fail due to changes in error
>> strings, hopefully, this will allow you to review what changed.
>> However, except for the differences in error string, everything your
>> original series supported is now supported by this patch.
>> 
>> From a user's point of view, I think the only differences with your
>> patch are:
>> 
>>  1. New .name attribute, that returns the name of the command as a
>>     string, here's an example session:
>> 
>>     (gdb) python
>>     >class MyCommand(gdb.MICommand):
>>     >  def __init__(self):
>>     >    super(MyCommand, self).__init__("-my-command")
>>     >  def invoke(self, args):
>>     >    return None
>>     >
>>     >end
>>     (gdb) python cmd = MyCommand()
>>     (gdb) python print(cmd.name)
>>     -my-command
>>     (gdb)
>> 
>>  2. New .installed attribute, that allows a command to be installed or
>>     removed from the mi command table, here's an example session:
>> 
>>     (gdb) python
>>     >class MyCommand(gdb.MICommand):
>>     >  def __init__(self, name, message):
>>     >    self._message = message
>>     >    super(MyCommand, self).__init__(name)
>>     >
>>     >  def invoke(self, args):
>>     >    return self._message
>>     >
>>     >end
>>     (gdb) python cmd1 = MyCommand("-my-command", "cmd1")
>>     (gdb) python print(cmd1.installed)
>>     True
>>     (gdb) interpreter-exec mi "-my-command"
>>     ^done,result="cmd1"
>>     (gdb) python cmd2 = MyCommand("-my-command", "cmd2")
>>     (gdb) python print(cmd2.installed)
>>     True
>>     (gdb) python print(cmd1.installed)
>>     False
>>     (gdb) interpreter-exec mi "-my-command"
>>     ^done,result="cmd2"
>>     (gdb) python cmd1.installed = True
>>     (gdb) python print(cmd2.installed)
>>     False
>>     (gdb) python print(cmd1.installed)
>>     True
>>     (gdb) interpreter-exec mi "-my-command"
>>     ^done,result="cmd1"
>>     (gdb) python cmd1.installed = False
>>     (gdb) interpreter-exec mi "-my-command"
>>     ^error,msg="Undefined MI command: my-command",code="undefined-command"
>>     (gdb)
>> 
>>  3. The top level result name can be changed from 'result' to anything
>>     the user wants, here's an example session:
>> 
>>     (gdb) python
>>     >class MyCommand(gdb.MICommand):
>>     >  def __init__(self):
>>     >    super(MyCommand, self).__init__("-my-command", "greeting")
>>     >  def invoke(self, args):
>>     >    return "Hello World"
>>     >
>>     >end
>>     (gdb) python cmd = MyCommand()
>>     (gdb) interpreter-exec mi "-my-command"
>>     ^done,greeting="Hello World"
>>     (gdb)
>> 
>> I'd love to hear your feedback on this iteration.
>
> I'm personally happy with this iteration. I like your improvements 
> to the Python API! I also tested it briefly with my Python code & 
> frontend just to see and it worked just fine.

That's great news.

>
> Perhaps, I'd rename mi_commands module variable to _mi_commands to 
> convey the fact it is "internal" to the implementation and should not
> be touched (?).

Thanks, using the '_' prefix was mentioned for another patch I was
working on; I'll definitely make this change.

I'll give this a little more time, hopefully we'll get some more reviews
in.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 2/5] gdb/python: create GDB/MI commands using python.
  2022-01-17 12:44 ` [PATCH 2/5] gdb/python: create GDB/MI commands using python Jan Vrany
@ 2022-02-06 16:52   ` Lancelot SIX
  0 siblings, 0 replies; 41+ messages in thread
From: Lancelot SIX @ 2022-02-06 16:52 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

Hi,

I just have minor comments.  See below.

> +#include "defs.h"
> +#include "python-internal.h"
> +#include "python/py-micmd.h"
> +#include "arch-utils.h"
> +#include "charset.h"
> +#include "language.h"
> +
> +#include <string>
> +
> +static PyObject *invoke_cst;
> +
> +extern PyTypeObject
> +  micmdpy_object_type CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
> +
> +/* If the command invoked returns a list, this function parses it and create an
                                                                               ^

creates ?

> +   appropriate MI out output.
> +
> +   The returned values must be Python string, and can be contained within Python
> +   lists and dictionaries. It is possible to have a multiple levels of lists
> +   and/or dictionaries.  */
> +
> +static void
> +parse_mi_result (PyObject *result, const char *field_name)
> +{
> +  struct ui_out *uiout = current_uiout;
> +
> +  if (PyDict_Check (result))
> +    {
> +      PyObject *key, *value;
> +      Py_ssize_t pos = 0;
> +      ui_out_emit_tuple tuple_emitter (uiout, field_name);
> +      while (PyDict_Next (result, &pos, &key, &value))
> +	{
> +	  if (!PyString_Check (key))
> +	    {
> +	      gdbpy_ref<> key_repr (PyObject_Repr (key));
> +	      if (PyErr_Occurred () != NULL)

Ideally, s/NULL/nullptr/ on the patch.  You use nullptr in few places
already.

> +  try
> +    {
> +      mi_command_up micommand = mi_command_up(new mi_command_py (name + 1, self_ref));
> +
> +      bool result = insert_mi_cmd_entry (std::move (micommand));
> +
> +      if (!result)
> +	{
> +	  error (_("Unable to insert command."
> +                   "The name might already be in use."));

The two strings will be concatenated.  You might want to ensure a space
is added between the "." and "The" here.

For the rest, I am not very familiar with the python bindings, so I
mainly skimmed through the patch.

Best,
Lancelot.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands
  2022-01-17 12:44 ` [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands Jan Vrany
@ 2022-02-06 17:13   ` Lancelot SIX
  2022-02-06 20:33     ` Simon Marchi
  0 siblings, 1 reply; 41+ messages in thread
From: Lancelot SIX @ 2022-02-06 17:13 UTC (permalink / raw)
  To: Jan Vrany; +Cc: gdb-patches

Hi,

> @@ -163,6 +164,9 @@ struct mi_command
>       wrong.  */
>    void invoke (struct mi_parse *parse) const;
>  
> +  /* Return TRUE if the command can be redefined, FALSE otherwise.  */
> +  virtual bool can_be_redefined ();

It looks like that this method can be made const I think.

I also did ont pay too close attention in the previous patch, but it
looks like you use space indentation.  I think GNU standard[1] and GDB
codebase uses tab indentation (tab size of 8):

    The rest of this section gives our recommendations for other aspects of
    C formatting style, which is also the default style of the indent
    program in version 1.2 and newer. It corresponds to the options

    -nbad -bap -nbc -bbo -bl -bli2 -bls -ncdb -nce -cp1 -cs -di2
    -ndj -nfc1 -nfca -hnl -i2 -ip5 -lp -pcs -psl -nsc -nsob

This way to tell things is not super informative in itself.  Depending
on the text editor you use, there might be a configuration option to
help you automate this.

I will not point it out in every place in the patch, but there are
multiple instances where your changes lead to a indentation change (i.e.
replace 1 tab with 8 spaces).  This should be avoided.

> --- a/gdb/python/py-micmd.c
> +++ b/gdb/python/py-micmd.c
> @@ -1,6 +1,6 @@
>  /* MI Command Set for GDB, the GNU debugger.
>  
> -   Copyright (C) 2019 Free Software Foundation, Inc.
> +   Copyright (C) 2019-2021 Free Software Foundation, Inc.

2021 will most probably need to become 2022 (and same goes for other new
files as well).


>  class mi_command_py : public mi_command
>  {
> -  public:
> -    /* Constructs a new mi_command_py object.  NAME is command name without
> -       leading dash.  OBJECT is a reference to a Python object implementing
> -       the command.  This object should inherit from gdb.MICommand and should
> -       implement method invoke (args). */
> -    mi_command_py (const char *name, gdbpy_ref<> object);
> +public:
> +  /* Constructs a new mi_command_py object.  NAME is command name without
> +     leading dash.  OBJECT is a reference to a Python object implementing
> +     the command.  This object should inherit from gdb.MICommand and should
> +     implement method invoke (args).  */
> +  mi_command_py (const char *name, gdbpy_ref<> object);

Here you change the indentation from the previous patch.  This change
should probably be moved to it.

I understand that this is tedious to change and am sorry for that.

Best,
Lancelot.

[1] https://www.gnu.org/prep/standards/html_node/Formatting.html

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands
  2022-02-06 17:13   ` Lancelot SIX
@ 2022-02-06 20:33     ` Simon Marchi
  2022-02-06 20:44       ` Jan Vrany
  0 siblings, 1 reply; 41+ messages in thread
From: Simon Marchi @ 2022-02-06 20:33 UTC (permalink / raw)
  To: Lancelot SIX, Jan Vrany; +Cc: gdb-patches

> Here you change the indentation from the previous patch.  This change
> should probably be moved to it.
> 
> I understand that this is tedious to change and am sorry for that.

Yeah, it looks like there is a good number of changes in this patch that
adjust things added in the previous patch.  These changes should indeed
be folded in the previous patch.  I was going to comment on some of them
only to find out they get changed here, for no particular reason.

FYI, with the right tools it's not too tedious.  Here's how I do this
particular change:

1. "git config diff.tool meld", to set meld as my diff tool of choice
2. use "git rebase -i" to go to patch 2
3. "git difftool --dir-diff <sha1-of-patch-3>"

That opens Meld, showing the diff between patches 2 and 3.  That lets
you bring in the changes you want from patch 3 to 2.  Once you're done,
amend the commit, finish the rebase.

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands
  2022-02-06 20:33     ` Simon Marchi
@ 2022-02-06 20:44       ` Jan Vrany
  2022-02-06 20:46         ` Simon Marchi
  2022-02-07  9:46         ` Lancelot SIX
  0 siblings, 2 replies; 41+ messages in thread
From: Jan Vrany @ 2022-02-06 20:44 UTC (permalink / raw)
  To: Simon Marchi, Lancelot SIX; +Cc: gdb-patches

Hi, 

thanks! I'd just like to mention that my understanding is that
in the end we'd go with Andrew's rework of this patch series, 
see the thread: 

https://sourceware.org/pipermail/gdb-patches/2022-January/185236.html

and

https://sourceware.org/pipermail/gdb-patches/2022-February/185662.html

Jan


On Sun, 2022-02-06 at 15:33 -0500, Simon Marchi wrote:
> > Here you change the indentation from the previous patch.  This change
> > should probably be moved to it.
> > 
> > I understand that this is tedious to change and am sorry for that.
> 
> Yeah, it looks like there is a good number of changes in this patch that
> adjust things added in the previous patch.  These changes should indeed
> be folded in the previous patch.  I was going to comment on some of them
> only to find out they get changed here, for no particular reason.
> 
> FYI, with the right tools it's not too tedious.  Here's how I do this
> particular change:
> 
> 1. "git config diff.tool meld", to set meld as my diff tool of choice
> 2. use "git rebase -i" to go to patch 2
> 3. "git difftool --dir-diff <sha1-of-patch-3>"
> 
> That opens Meld, showing the diff between patches 2 and 3.  That lets
> you bring in the changes you want from patch 3 to 2.  Once you're done,
> amend the commit, finish the rebase.
> 
> Simon



^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands
  2022-02-06 20:44       ` Jan Vrany
@ 2022-02-06 20:46         ` Simon Marchi
  2022-02-07  9:46         ` Lancelot SIX
  1 sibling, 0 replies; 41+ messages in thread
From: Simon Marchi @ 2022-02-06 20:46 UTC (permalink / raw)
  To: Jan Vrany, Lancelot SIX; +Cc: gdb-patches



On 2022-02-06 15:44, Jan Vrany wrote:
> Hi, 
> 
> thanks! I'd just like to mention that my understanding is that
> in the end we'd go with Andrew's rework of this patch series, 
> see the thread: 
> 
> https://sourceware.org/pipermail/gdb-patches/2022-January/185236.html
> 
> and
> 
> https://sourceware.org/pipermail/gdb-patches/2022-February/185662.html
> 
> Jan

Oh, thanks!  I saw yours, sent on Jan 17th, I thought "that's recent, it
must be the latest version" :).

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 0/5] create GDB/MI commands using python
  2022-01-21 15:22     ` Andrew Burgess
  2022-01-24 12:59       ` Jan Vrany
@ 2022-02-06 21:16       ` Simon Marchi
  2022-02-07 15:56         ` [PATCHv2] gdb/python/mi: create MI " Andrew Burgess
  1 sibling, 1 reply; 41+ messages in thread
From: Simon Marchi @ 2022-02-06 21:16 UTC (permalink / raw)
  To: Andrew Burgess, Jan Vrany; +Cc: gdb-patches

>  3. The top level result name can be changed from 'result' to anything
>     the user wants, here's an example session:
> 
>     (gdb) python
>     >class MyCommand(gdb.MICommand):
>     >  def __init__(self):
>     >    super(MyCommand, self).__init__("-my-command", "greeting")
>     >  def invoke(self, args):
>     >    return "Hello World"
>     >
>     >end
>     (gdb) python cmd = MyCommand()
>     (gdb) interpreter-exec mi "-my-command"
>     ^done,greeting="Hello World"
>     (gdb)

I find this a bit surprising, why is "greeting" passed to the
constructor?  Since an MI result, at the root, is basically a dict,
my intuition would be to only allow the invoke method to return a dict.

  def invoke(self, args):
      return {'a': 1, 'b': "hello"}

which would result int:

  ^done,a="1",b="hello"

Returning None (which is returned if there's no explicit "return") would
also be allowed, in which case there is not result:

  ^done

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands
  2022-02-06 20:44       ` Jan Vrany
  2022-02-06 20:46         ` Simon Marchi
@ 2022-02-07  9:46         ` Lancelot SIX
  1 sibling, 0 replies; 41+ messages in thread
From: Lancelot SIX @ 2022-02-07  9:46 UTC (permalink / raw)
  To: Jan Vrany; +Cc: Simon Marchi, gdb-patches

On Sun, Feb 06, 2022 at 08:44:16PM +0000, Jan Vrany wrote:
> Hi, 
> 
> thanks! I'd just like to mention that my understanding is that
> in the end we'd go with Andrew's rework of this patch series, 
> see the thread: 

Hi,

My bad, sorry.  I did saw that but did not fully linked the dots when
I went back to my [growing] backlog on the mailing list.

Thanks,
Lancelot.

^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCHv2] gdb/python/mi: create MI commands using python
  2022-02-06 21:16       ` Simon Marchi
@ 2022-02-07 15:56         ` Andrew Burgess
  2022-02-08 15:16           ` Simon Marchi
  0 siblings, 1 reply; 41+ messages in thread
From: Andrew Burgess @ 2022-02-07 15:56 UTC (permalink / raw)
  To: gdb-patches; +Cc: Simon Marchi, Andrew Burgess, Jan Vrany

Simon,

Thanks for your feedback.  You are completely correct of course.  For
anyone else who wants to read up, the mi output format is documented
here:

  https://sourceware.org/gdb/onlinedocs/gdb/GDB_002fMI-Output-Syntax.html#GDB_002fMI-Output-Syntax

I've rewritten the patch now so that the invoke method is required to
return a dictionary.  The values within that dictionary can then be
dictionaries, sequences, iterators, or object to convert to strings,
just like before.

Jan, this means the new patch is no longer compatible with your
original work unfortunately.  But, hopefully, adapting to this new
scheme isn't too hard.

I've restructured the commit message, added new tests for the things
changed in this commit, and updated the documentation.

Feedback welcome.

Thanks,
Andrew


---

This commit allows an user to create custom MI commands using Python
similarly to what is possible for Python CLI commands.

A new subclass of mi_command is defined for Python MI commands,
mi_command_py. A new file, py-micmd.c contains the logic for Python MI
commands.

This commit is based on work linked too from this mailing list thread:

  https://sourceware.org/pipermail/gdb/2021-November/049774.html

Which has also been previously posted to the mailing list here:

  https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html

And was recently reposted here:

  https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html

This patch takes some core code from the previous posted patches, but
also has some significant differences, especially after the feedback
given here:

  https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html

A new MI command can be implemented in Python like this:

  class echo_args(gdb.MICommand):
      def invoke(self,args):
          return { 'args': args }

  echo_args("-echo-args")

The 'args' parameter is a list containing all command line arguments
passed to the MI command.  This list can be empty if the MI command
was passed no arguments.

When used within gdb the above command produced output like this:

  (gdb)
  -echo-args a b c
  ^done,args=["a","b","c"]
  (gdb)

The 'invoke' method of the new command must return a dictionary.  The
keys of this dictionary are then used as the field names in the mi
command output (e.g. 'args' in the above).

The values of the result returned by invoke can be dictionaries,
lists, iterators, or an object that can be converted to a string.
These are processed recursively to create the mi output.  And so, this
is valid:

  class new_command(gdb.MICommand):
      def invoke(self,args):
          return { 'result_one': { 'abc': 123, 'def': 'Hello' },
                   'result_two': [ { 'a': 1, 'b': 2 },
                                   { 'c': 3, 'd': 4 } ] }

Which produces output like:

  (gdb)
  -new-command
  ^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
  (gdb)

I have required that the fields names used in mi result output must
follow C identifier restrictions (i.e. must match the regexp
"[a-zA-Z][a-zA-Z0-9_]*").  This restriction was never written down
anywhere before, but seems sensible to me.  We can always loosen this
rule later if it proves to be a problem.  Much harder to try and add a
restriction later, if folk are already using the API, and breaking the
restriction.

What follows are some details about how this implementation differs
from the original patch that was posted to the mailing list.

In this patch, I have changed how the lifetime of the Python
gdb.MICommand objects is managed.  In the original patch, these object
were kept alive by an owned reference within the mi_command_py object.
As such, the Python object would not be deleted until the
mi_command_py object itself was deleted.

This caused a problem, the mi_command_py were held in the global mi
command table (in mi/mi-cmds.c), which, as a global, was not cleared
until program shutdown.  By this point the Python interpreter has
already been shutdown.  Attempting to delete the mi_command_py object
at this point was causing GDB to try and invoke Python code after
finalising the Python interpreter, and we would crash.

To work around this problem, the original patch added code in
python/python.c that would search the mi command table, and delete the
mi_command_py objects before the Python environment was finalised.

In contrast, in this patch, I have added a new global dictionary to
the gdb module, gdb._mi_commands.  We already have several such global
data stores related to pretty printers, and frame unwinders.

The MICommand objects are placed into the new gdb.mi_commands
dictionary, and it is this reference that keeps the objects alive.
When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
and any MICommand objects within it are deleted at this point.

This change avoids having to make the mi_cmd_table global, and walk
over it from within GDB's python related code.

This patch handles command redefinition entirely within GDB's python
code, though this does impose one small restriction which is not
present in the original code (detailed below), I don't think this is a
big issue.  However, the original patch relied on being able to
finish executing the mi_command::do_invoke member function after the
mi_command object had been deleted.  Though continuing to execute a
member function after an object is deleted is well defined, it is
also (IMHO) risky, its too easy for someone to later add a use of the
object without realising that the object might sometimes, have been
deleted.  The new patch avoids this issue.

The one restriction that is added to avoid this, is that an MICommand
object can't be reinitialised with a different command name, so:

  (gdb) python cmd = MyMICommand("-abc")
  (gdb) python cmd.__init__("-def")
  can't reinitialize object with a different command name

This feels like a pretty weird edge case, and I'm happy to live with
this restriction.

I have also changed how the memory is managed for the command name.
In the most recently posted patch series, the command name is moved
into a subclass of mi_command, the python mi_command_py, which
inherits from mi_command is then free to use a smart pointer to manage
the memory for the name.

In this patch, I leave the mi_command class unchanged, and instead
hold the memory for the name within the Python object, as the lifetime
of the Python object always exceeds the c++ object stored in the
mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
leaves the mi_command class nice and simple.

Next, this patch adds some extra functionality, there's a
MICommand.name read-only attribute containing the name of the command,
and a read-write MICommand.installed attribute that can be used to
install (make the command available for use) and uninstall (remove the
command from the mi_cmd_table so it can't be used) the command.  This
attribute will be automatically updated if a second command replaces
an earlier command.

This patch adds additional error handling, and makes more use the
gdbpy_handle_exception function.

Co-Authored-By: Jan Vrany <jan.vrany@labware.com>
---
 gdb/Makefile.in                        |   1 +
 gdb/NEWS                               |   2 +
 gdb/doc/python.texi                    | 159 ++++-
 gdb/mi/mi-cmds.c                       |  22 +-
 gdb/mi/mi-cmds.h                       |  15 +
 gdb/python/lib/gdb/__init__.py         |   3 +
 gdb/python/py-micmd.c                  | 815 +++++++++++++++++++++++++
 gdb/python/python-internal.h           |   2 +
 gdb/python/python.c                    |   3 +-
 gdb/testsuite/gdb.python/py-mi-cmd.exp | 286 +++++++++
 gdb/testsuite/gdb.python/py-mi-cmd.py  | 100 +++
 11 files changed, 1393 insertions(+), 15 deletions(-)
 create mode 100644 gdb/python/py-micmd.c
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.exp
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index bf19db45343..681c9cb682f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index c739930bfc7..bf44f7da004 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -168,6 +168,8 @@ info win
      manager that temporarily sets the gdb parameter NAME to VALUE,
      then resets it when the context is exited.
 
+  ** It is now possible to add GDB/MI commands implemented in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index da88b8a7690..358e6a53e87 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -204,7 +204,8 @@
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -419,7 +420,8 @@
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2136,7 +2138,7 @@
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3840,11 +3842,12 @@
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
-@cindex commands in python
-@cindex python commands
+@cindex CLI commands in python
+@cindex commands in python, CLI
+@cindex python commands, CLI
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
 command is implemented using an instance of the @code{gdb.Command}
 class, most commonly using a subclass.
@@ -4123,6 +4126,146 @@
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python, GDB/MI
+@cindex python commands, GDB/MI
+It is possible to add @sc{GDB/MI} (@pxref{GDB/MI}) commands
+implemented in Python.  A @sc{GDB/MI} command is implemented using an
+instance of the @code{gdb.MICommand} class, most commonly using a
+subclass.
+
+@defun MICommand.__init__ (name)
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid name of a
+@sc{GDB/MI} command, and in particular must start with a hyphen
+(@code{-}).  Reusing the name of a built-in @sc{GDB/MI} is not
+allowed, and a @code{RuntimeError} will be raised.  Using the name
+of an @sc{GDB/MI} command previously defined in Python is allowed, the
+previous command will be replaced with the new command.
+@end defun
+
+@defun MICommand.invoke (arguments)
+This method is called by @value{GDBN} when the new MI command is
+invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method throws a @code{gdb.GdbError} exception, it is turned
+into a @sc{GDB/MI} @code{^error} response.  If this method returns
+@code{None}, then the @sc{GDB/MI} command will return a @code{^done}
+response with no additional values.
+
+Otherwise, the return value must be a dictionary, which is converted
+to a @sc{GDB/MI} @var{RESULT-RECORD} (@pxref{GDB/MI Output Syntax}).
+The keys of this dictionary must be strings, and are used as
+@emph{VARIABLE} names in the @emph{RESULT-RECORD}, these strings must
+comply with the naming rules detailed below.  The values of this
+dictionary are recursively handled as follows:
+
+@itemize
+@item If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @emph{LIST} with elements converted recursively.
+
+@item If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @emph{TUPLE}.  Keys in that dictionary must be strings,
+which comply with the @emph{VARIABLE} naming rules detailed below.
+Values are converted recursively.
+
+@item Otherwise, value is first converted to a Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @emph{CONST}.
+@end itemize
+
+The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
+must follow C variable naming rules; the string must be at least one
+character long, the first character must be in the set
+@code{[a-zA-Z]}, while every subsequent character must be in the set
+@code{[a-zA-Z0-9_]}.
+
+@end defun
+
+An instance of @code{MICommand} has the following attributes:
+
+@defvar MICommand.name
+A string, the name of this @sc{GDB/MI} command, as was passed to the
+@code{__init__} method.  This attribute is read-only.
+@end defvar
+
+@defvar MICommand.installed
+A boolean value indicating if this command is installed ready for a
+user to call from the command line.  Commands are automatically
+installed when they are instantiated, after which this attribute will
+be @code{True}.
+
+If later, a new command is created with the same name, then the
+original command will become uninstalled, and this attribute will be
+@code{False}.
+
+This attribute can be is read-write, setting this attribute to
+@code{False} will uninstall the command, removing it from the set of
+available commands.  Setting this attribute to @code{True} will
+install the command for use.  If there is already a Python command
+with this name installed, the currently installed command will be
+uninstalled, and this command installed in its place.
+@end defvar
+
+The following code snippet shows how a two trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo arguments passed to the command."""
+
+    def __init__(self, name, mode, toplevel = None):
+        self._mode = mode
+        super(MIEcho, self).__init__(name, toplevel)
+
+    def invoke(self, argv):
+        if self._mode == 'dict':
+            return @{ 'result': @{ 'argv' : argv @} @}
+        elif self._mode == 'list':
+            return @{ 'result': argv @}
+        else:
+            return @{ 'result': ", ".join(argv) @}
+
+
+MIEcho("-echo-dict", "dict")
+MIEcho("-echo-list", "list")
+MIEcho("-echo-string", "string", "argv")
+@end smallexample
+
+The last three lines instantiate the class three times, creating three
+new @sc{GDB/MI} commands @code{-echo-dict}, @code{-echo-list}, and
+@code{-echo-string}.  Each time a subclass of @code{gdb.MICommand} is
+instantiated, the new command is automatically registered with
+@value{GDBN}.
+
+Depending on how the Python code is read into @value{GDBN}, you may
+need to import the @code{gdb} module explicitly.
+
+The following example shows a @value{GDBN} session in which the above
+commands have been added:
+
+@smallexample
+(@value{GDBP})
+-echo-dict abc def ghi
+^done,dict=@{argv=["abc","def","ghi"]@}
+(@value{GDBP})
+-echo-list abc def ghi
+^done,list=["abc","def","ghi"]
+(@value{GDBP})
+-echo-string abc def ghi
+^done,string="abc, def, ghi"
+(@value{GDBP})
+@end smallexample
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4160,7 +4303,7 @@
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..dd0243e5bfe 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -113,12 +109,12 @@ struct mi_command_cli : public mi_command
    not have been added to mi_cmd_table.  Otherwise, return true, and
    COMMAND was added to mi_cmd_table.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
 
-  const std::string &name = command->name ();
+  const std::string name (command->name ());
 
   if (mi_cmd_table.find (name) != mi_cmd_table.end ())
     return false;
@@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+bool
+remove_mi_cmd_entry (mi_command *command)
+{
+  gdb_assert (command != nullptr);
+
+  const std::string name (command->name ());
+
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..3f4fb854d68 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the MI_CMD_TABLE.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,15 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert a new mi-command into the command table.  Return true if
+   insertion was successful.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove a mi-command from the command table.  Return true if the removal
+   was success, otherwise return false.  */
+
+extern bool remove_mi_cmd_entry (mi_command *command);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 9734a0d9437..e2d2ab10467 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -82,6 +82,9 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Hash containing all user created MI commands, the key is the command
+# name, and the value is the gdb.MICommand object.
+_mi_commands = {}
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..e289785c61e
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,815 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* gdb MI commands implemented in Python  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "cli/cli-cmds.h"
+
+#include <string>
+
+/* Debugging of Python mi commands.  */
+
+static bool pymicmd_debug;
+
+/* Implementation of "show debug py-micmd".  */
+
+static void
+show_pymicmd_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Python mi command debugging is %s.\n"), value);
+}
+
+/* Print a "py-micmd" debug statement.  */
+
+#define pymicmd_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
+
+/* Print a "py-micmd" enter/exit debug statements.  */
+
+#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
+  scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
+
+struct mi_command_py;
+
+/* Representation of a python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the mi command table.  This
+     pointer can be nullptr if the command is not currently installed into
+     the mi command table (see gdb.MICommand.installed property).  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing the name of this command, without the leading
+     dash.  This string is never nullptr once the python object has been
+     initialised.
+
+     The memory for this string was allocated with malloc, and needs to be
+     deallocated with free when the python object is deallocated.
+
+     When the MI_COMMAND variable is not nullptr, then the mi_command_py
+     object's name will point back to this string.  */
+  char *mi_command_name;
+};
+
+/* The mi command implemented in python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object should inherit from gdb.MICommand and should
+     implement method invoke (args). */
+
+  mi_command_py (const char *name, micmdpy_object *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  {
+    pymicmd_debug_printf ("this = %p", this);
+  }
+
+  ~mi_command_py ()
+  {
+    /* The python object representing a mi command contains a pointer back
+       to this c++ object.  We can safely set this pointer back to nullptr
+       now, to indicate the python object no longer references a valid c++
+       object.
+
+       However, the python object also holds the storage for our name
+       string.  We can't clear that here as our parent's destructor might
+       still want to reference that string.  Instead we rely on the python
+       object deallocator to free that memory, and reset the pointer.  */
+    m_pyobj->mi_command = nullptr;
+
+    pymicmd_debug_printf ("this = %p", this);
+  };
+
+  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the mi
+     command table correctly.  This function looks up the command in the mi
+     command table and checks that the object we get back references
+     CMD_OBJ.  This function is only intended for calling within a
+     gdb_assert.  This function performs many assertions internally, and
+     then always returns true.  */
+  static bool validate_installation (micmdpy_object *cmd_obj);
+
+  /* Update m_pyobj to NEW_PYOBJ.  The pointer from M_PYOBJ that points
+     back to this object is swapped with the pointer in NEW_PYOBJ, which
+     must be nullptr, so that NEW_PYOBJ now points back to this object.
+     Additionally our parent's name string is stored in m_pyobj, so we
+     swap the name string with NEW_PYOBJ.
+
+     Before this call m_pyobj is the python object representing this mi
+     command object.  After this call has completed, NEW_PYOBJ now
+     represents this mi command object.  */
+  void swap_python_object (micmdpy_object *new_pyobj)
+  {
+    gdb_assert (new_pyobj->mi_command == nullptr);
+    std::swap (new_pyobj->mi_command, m_pyobj->mi_command);
+    std::swap (new_pyobj->mi_command_name, m_pyobj->mi_command_name);
+    m_pyobj = new_pyobj;
+  }
+
+protected:
+  /* Called when the mi command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The python object representing this mi command.  */
+  micmdpy_object *m_pyobj;
+};
+
+extern PyTypeObject micmdpy_object_type
+	CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* Holds a python object containing the string 'invoke'.  */
+
+static PyObject *invoke_cst;
+
+/* Convert KEY_OBJ into a string that can be used as a field name in MI
+   output.  KEY_OBJ must be a Python string object, and must only contain
+   characters suitable for use as an MI field name.
+
+   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
+   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
+   and returned.  */
+
+static gdb::unique_xmalloc_ptr<char>
+py_object_to_mi_key (PyObject *key_obj)
+{
+  /* The key must be a string.  */
+  if (!PyString_Check (key_obj))
+    {
+      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
+      gdb::unique_xmalloc_ptr<char> key_repr_string;
+      if (key_repr != nullptr)
+	key_repr_string = python_string_to_target_string (key_repr.get ());
+
+      if (key_repr_string != nullptr)
+	PyErr_Format (PyExc_TypeError,
+		      _("Non-string object used as key: %s"),
+		      key_repr_string.get ());
+      gdbpy_handle_exception ();
+    }
+
+  gdb::unique_xmalloc_ptr<char> key_string
+    = python_string_to_target_string (key_obj);
+  if (key_string == nullptr)
+    gdbpy_handle_exception ();
+
+  /* Predicate function, returns true if NAME is a valid field name for use
+     in mi result output, otherwise, returns false.  */
+  auto is_valid_key_name = [] (const char *name) -> bool
+  {
+    gdb_assert (name != nullptr);
+
+    if (*name == '\0' || !isalpha (*name))
+      return false;
+
+    for (; *name != '\0'; ++name)
+      if (!isalnum (*name) && *name != '_')
+	return false;
+
+    return true;
+  };
+
+  if (!is_valid_key_name (key_string.get ()))
+    {
+      if (*key_string.get () == '\0')
+	PyErr_Format (PyExc_ValueError, _("Invalid empty key in MI result"));
+      else
+	PyErr_Format (PyExc_ValueError, _("Invalid key in MI result: %s"),
+		      key_string.get ());
+      gdbpy_handle_exception ();
+    }
+
+  return key_string;
+}
+
+/* Parse RESULT and print it in mi format to the current_uiout.  FIELD_NAME
+   is used as the name of this result field.
+
+   RESULT can be a dictionary, a sequence, an iterator, or an object that
+   can be converted to a string, these are converted to the matching mi
+   output format (dictionaries as tuples, sequences and iterators as lists,
+   and strings as named fields).
+
+   If anything goes wrong while formatting the output then an error is
+   thrown.
+
+   This function is the recursive inner core of parse_mi_result, and
+   should only be called from that function.  */
+
+static void
+parse_mi_result_1 (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  gdb::unique_xmalloc_ptr<char> key_string
+	    (py_object_to_mi_key (key));
+	  parse_mi_result_1 (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      Py_ssize_t len = PySequence_Size (result);
+      if (len == -1)
+	gdbpy_handle_exception ();
+      for (Py_ssize_t i = 0; i < len; ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  if (item == nullptr)
+	    gdbpy_handle_exception ();
+	  parse_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (true)
+	{
+	  item.reset (PyIter_Next (result));
+	  if (item == nullptr)
+	    {
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+	      break;
+	    }
+	  parse_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      if (string == nullptr)
+	gdbpy_handle_exception ();
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Parse RESULT and print it in mi format to the current_uiout.
+
+   This function handles the top-level result initially returned from the
+   invoke method of the Python command implementation.  At the top-level
+   the result must be a dictionary.  The values within this dictionary can
+   be a wider range of types.  Handling the values of the top-level
+   dictionary is done by parse_mi_result_1, see that function for more
+   details.
+
+   If anything goes wrong while parsing and printing the mi output then an
+   error is thrown.  */
+
+static void
+parse_mi_result (PyObject *result)
+{
+  /* At the top-level, the result must be a dictionary.  */
+
+  if (!PyDict_Check (result))
+    {
+      PyErr_SetString (PyExc_TypeError,
+		       _("Result from invoke must be a dictionary"));
+      gdbpy_handle_exception ();
+    }
+
+  PyObject *key, *value;
+  Py_ssize_t pos = 0;
+  while (PyDict_Next (result, &pos, &key, &value))
+    {
+      gdb::unique_xmalloc_ptr<char> key_string
+	(py_object_to_mi_key (key));
+      parse_mi_result_1 (value, key_string.get ());
+    }
+}
+
+/* Called when the mi command is invoked.  PARSE contains the parsed
+   command line arguments from the user.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == nullptr)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = (PyObject *) this->m_pyobj;
+  gdb_assert (obj != nullptr);
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  if (!PyObject_HasAttr (obj, invoke_cst))
+      error (_("-%s: Python command object missing 'invoke' method."),
+	     name ());
+
+  /* Place all the arguments into a list which we pass as a single
+     argument to the mi command's invoke method.  */
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    {
+      gdbpy_print_stack ();
+      error (_("-%s: failed to create the Python arguments list."),
+	     name ());
+    }
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i],
+					 strlen (parse->argv[i]),
+					 host_charset (), nullptr));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
+	{
+	  gdbpy_print_stack ();
+	  error (_("-%s: failed to create the Python arguments list."),
+		 name ());
+	}
+    }
+
+  gdb_assert (PyErr_Occurred () == nullptr);
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, invoke_cst,
+						  argobj.get (), nullptr));
+  if (result == nullptr)
+    gdbpy_handle_exception ();
+
+  if (result != Py_None)
+    parse_mi_result (result.get ());
+}
+
+/* See declaration above.  */
+
+bool
+mi_command_py::validate_installation (micmdpy_object *cmd_obj)
+{
+  gdb_assert (cmd_obj != nullptr);
+  mi_command_py *cmd = cmd_obj->mi_command;
+  gdb_assert (cmd != nullptr);
+  const char *name = cmd_obj->mi_command_name;
+  gdb_assert (name != nullptr);
+  gdb_assert (name == cmd->name ());
+  mi_command *mi_cmd = mi_cmd_lookup (name);
+  gdb_assert (mi_cmd == cmd);
+  gdb_assert (cmd->m_pyobj == cmd_obj);
+
+  return true;
+}
+
+/* Return a reference to the gdb.mi_commands dictionary.  */
+
+static gdbpy_ref<>
+micmdpy_global_command_dictionary ()
+{
+  if (gdb_python_module == nullptr
+      || ! PyObject_HasAttrString (gdb_python_module, "_mi_commands"))
+    error (_("unable to find gdb.mi_commands dictionary"));
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "_mi_commands"));
+  if (mi_cmd_dict == nullptr || !PyDict_Check (mi_cmd_dict.get ()))
+    error (_("unable to fetch gdb.mi_commands dictionary"));
+
+  return mi_cmd_dict;
+}
+
+/* Uninstall OBJ, making the mi command represented by OBJ unavailable for
+   use by the user.  On success 0 is returned, otherwise -1 is returned
+   and a python exception will be set.  */
+
+static int
+micmdpy_uninstall_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command != nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  /* Remove the command from the internal mi table of commands, this will
+     cause the c++ object to be deleted, which will clear the mi_command
+     member variable within the python object.  */
+  remove_mi_cmd_entry (obj->mi_command);
+  gdb_assert (obj->mi_command == nullptr);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+
+  /* Grab the name for this command.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+  if (name_obj == nullptr)
+    return -1;
+
+  /* Lookup the gdb.MICommand object in the dictionary of all python mi
+     commands, this is gdb.mi_command, and remove it.  */
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+
+  /* Did we encounter an error?  Failing to find the object in the
+     dictionary isn't an error, that's fine.  */
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+
+  /* Did we find this command in the gdb.mi_commands dictionary?  If so,
+     then remove it.  */
+  if (curr != nullptr)
+    {
+      /* Yes we did!  Remove it.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  return 0;
+}
+
+/* Install OBJ as a usable mi command.  Return 0 on success, and -1 on
+   error, in which case, a python error will have been set.
+
+   After successful completion the command name associated with OBJ will
+   be installed in the mi command table (so it can be found if the user
+   enters that command name), additionally, OBJ will have been added to
+   the gdb.mi_commands dictionary (using the command name as its key),
+   this will ensure that OBJ remains live even if the user gives up all
+   references.  */
+
+static int
+micmdpy_install_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command == nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+
+  /* Look up this command name in the gdb.mi_commands dictionary, a command
+     with this name may already exist.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb.mi_commands
+	 dictionary.  First, validate that the object in the dictionary is
+	 of the expected type, just in case something weird has happened.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb.mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* To get to this function OBJ should not be installed, which should
+	 mean OBJ is not in the gdb.mi_commands dictionary.  If we find
+	 that OBJ is the thing in the dictionary, then something weird is
+	 going on, we should throw an error.  */
+      micmdpy_object *other = (micmdpy_object *) curr;
+      if (other == obj || other->mi_command == nullptr)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("uninstalled command found in gdb.mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* All python mi command object should always have a name set.  */
+      gdb_assert (other->mi_command_name != nullptr);
+
+      /* We always insert commands into the gdb.mi_commands dictionary
+	 using their name as a key, if this check fails then the dictionary
+	 is in some weird state.  */
+      if (other->mi_command_name != other->mi_command->name ()
+	  || strcmp (other->mi_command_name, obj->mi_command_name) != 0)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("gdb.mi_commands dictionary is corrupted"));
+	  return -1;
+	}
+
+      /* Switch the state of the c++ object held in the mi command table
+	 so that it now references OBJ.  After this action the old python
+	 object that used to be referenced from the mi command table will
+	 now show as uninstalled, while the new python object will show as
+	 installed.  */
+      other->mi_command->swap_python_object (obj);
+
+      gdb_assert (other->mi_command == nullptr);
+      gdb_assert (obj->mi_command != nullptr);
+      gdb_assert (obj->mi_command->name () == obj->mi_command_name);
+
+      /* Remove the previous python object from the gdb.mi_commands
+	 dictionary, we'll install the new object below.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+       return -1;
+    }
+  else
+    {
+      /* There's no python object for this command name in the
+	 gdb.mi_commands dictionary from which we can steal an existing
+	 object already held in the mi commands table, and so, we now
+	 create a new c++ object, and install it into the mi table.  */
+      obj->mi_command = new mi_command_py (obj->mi_command_name, obj);
+      mi_command_up micommand (obj->mi_command);
+
+      /* Add the command to the gdb internal mi command table.  */
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+      if (!result)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+    }
+
+  /* Finally, add the python object to the gdb.mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), (PyObject *) obj) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.__init__.  The init method takes the name of
+   the mi command as the first argument, which must be a string, starting
+   with a single dash.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  static const char *keywords[] = { "name", nullptr };
+  const char *name;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
+					&name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      error (_("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      error (_("MI command name does not start with '-'"
+               " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    {
+      for (int i = 2; i < name_len; i++)
+	{
+	  if (!isalnum (name[i]) && name[i] != '-')
+	    {
+	      error (_("MI command name contains invalid character: %c."),
+		     name[i]);
+	      return -1;
+	    }
+	}
+
+      /* Skip over the leading dash.  For the rest of this function the
+	 dash is not important.  */
+      ++name;
+    }
+
+  /* Check that there's an 'invoke' method.  */
+  if (!PyObject_HasAttr (self, invoke_cst))
+    error (_("-%s: Python command object missing 'invoke' method."), name);
+
+  /* If this object already has a name set, then this object has been
+     initialized before.  We handle this case a little differently.  */
+  if (cmd->mi_command_name != nullptr)
+    {
+      /* First, we don't allow the user to change the mi command name.
+	 Supporting this would be tricky as we would need to delete the
+	 mi_command_py from the mi command table, however, the user might
+	 be trying to perform this reinitialization from within the very
+	 command we're about to delete... it all gets very messy.
+
+	 So, for now at least, we don't allow this.  This doesn't seem like
+	 an excessive restriction.  */
+      if (strcmp (cmd->mi_command_name, name) != 0)
+	error (_("can't reinitialize object with a different command name"));
+
+      /* If there's already an object registered with the mi command table,
+	 then we're done.  That object must be a mi_command_py, which
+	 should reference back to this micmdpy_object.  */
+      if (cmd->mi_command != nullptr)
+	{
+	  gdb_assert (mi_command_py::validate_installation (cmd));
+	  return 0;
+	}
+    }
+  else
+    cmd->mi_command_name = xstrdup (name);
+
+  /* Now we can install this mi_command_py in the mi command table.  */
+  return micmdpy_install_command (cmd);
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+
+  /* If the python object failed to initialize, then the name field might
+     be nullptr.  */
+  pymicmd_debug_printf ("obj = %p, name = %s", cmd,
+			(cmd->mi_command_name == nullptr
+			 ? "(null)" : cmd->mi_command_name));
+
+  /* Remove the command from the mi command table if needed.  This will
+     cause the mi_command_py object to be deleted, which, in turn, will
+     clear the cmd->mi_command member variable, hence the assert.  */
+  if (cmd->mi_command != nullptr)
+    remove_mi_cmd_entry (cmd->mi_command);
+  gdb_assert (cmd->mi_command == nullptr);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->mi_command_name);
+  cmd->mi_command_name = nullptr;
+
+  /* Finally, free the memory for this python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Python initialization for the mi commands components.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == nullptr)
+    return -1;
+
+  return 0;
+}
+
+/* Get the gdb.MICommand.name attribute, returns a string, the name of this
+   mi command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->mi_command_name != nullptr);
+  std::string name_str = string_printf ("-%s", micmd_obj->mi_command_name);
+  return PyString_FromString (name_str.c_str ());
+}
+
+/* Get the gdb.MICommand.installed property.  Returns true if this mi
+   command is installed into the mi command table, otherwise returns
+   false.  */
+
+static PyObject *
+micmdpy_get_installed (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  if (micmd_obj->mi_command == nullptr)
+    Py_RETURN_FALSE;
+  Py_RETURN_TRUE;
+}
+
+/* Set the gdb.MICommand.installed property.  The property can be set to
+   either true or false.  Setting the property to true will cause the
+   command to be installed into the mi command table (if it isn't
+   already), while setting this property to false will cause the command
+   to be removed from the mi command table (if it is present).  */
+
+static int
+micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  bool installed_p = PyObject_IsTrue (newvalue);
+  if (installed_p == (micmd_obj->mi_command != nullptr))
+    return 0;
+
+  if (installed_p)
+    return micmdpy_install_command (micmd_obj);
+  else
+    return micmdpy_uninstall_command (micmd_obj);
+}
+
+/* The gdb.MICommand properties.   */
+
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { "installed", micmdpy_get_installed, micmdpy_set_installed,
+    "Is this command installed for use.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+/* The gdb.MICommand descriptor.  */
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (nullptr, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
+
+void _initialize_py_micmd ();
+void
+_initialize_py_micmd ()
+{
+  add_setshow_boolean_cmd
+    ("py-micmd", class_maintenance, &pymicmd_debug,
+     _("Set Python micmd debugging."),
+     _("Show Python micmd debugging."),
+     _("When on, Python micmd debugging is enabled."),
+     nullptr,
+     show_pymicmd_debug,
+     &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5e15f62f745..d9854b4ff7c 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -562,6 +562,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 2e659ee6e14..03c0408d772 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1904,7 +1904,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..dd8012f1f7a
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,286 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi2"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+#
+# Start here
+#
+
+
+mi_gdb_test "set python print-stack full" \
+    ".*\\^done" \
+    "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+    ".*\\^done" \
+    "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+    ".*\\^done" \
+    "define -pycmd MI command"
+
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+    "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+    "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+    "\\^done,result={hello=\"world\",times=\"42\"}" \
+    "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+    [multi_line \
+	 "&\"TypeError: Non-string object used as key: Bad Key..\"" \
+	 "\\^error,msg=\"Error occurred in Python: Non-string object used as key: Bad Key\""] \
+    "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+    [multi_line \
+	 "&\"TypeError: Non-string object used as key: 1..\"" \
+	 "\\^error,msg=\"Error occurred in Python: Non-string object used as key: 1\""] \
+    "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+    [multi_line \
+	 "&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
+	 "\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
+    "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+    "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+    "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+    "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+    "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+    "\\^done" \
+    "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+    "\\^done,result=\\\[\"None\"\\\]" \
+    "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+    "\\^error,msg=\"Invalid parameter: bogus\"" \
+    "-pycmd bogus"
+
+# Check that the top-level result from 'invoke' must be a dictionary.
+foreach test_name { nd1 nd2 nd3 } {
+    mi_gdb_test "-pycmd ${test_name}" \
+	[multi_line \
+	     "&\"TypeError: Result from invoke must be a dictionary..\"" \
+	     "\\^error,msg=\"Error occurred in Python: Result from invoke must be a dictionary\""]
+}
+
+# Check for invalid strings in the result.
+foreach test_desc { {ik1 "xxx yyy"} {ik2 "xxx yyy"} {ik3 "xxx-yyy"} \
+			{ik4 "xxx\\.yyy"} {ik5 "123xxxyyy"} } {
+    lassign $test_desc name pattern
+
+    mi_gdb_test "-pycmd ${name}" \
+	[multi_line \
+	     "&\"ValueError: Invalid key in MI result: ${pattern}..\"" \
+	     "\\^error,msg=\"Error occurred in Python: Invalid key in MI result: ${pattern}\""]
+}
+
+mi_gdb_test "-pycmd empty_key" \
+    [multi_line \
+	 "&\"ValueError: Invalid empty key in MI result..\"" \
+	 "\\^error,msg=\"Error occurred in Python: Invalid empty key in MI result\""]
+
+# With this argument the command raises a gdb.GdbError with no message
+# string.  GDB considers this a bug in the user program, so prints a
+# backtrace, and a generic error message.
+mi_gdb_test "-pycmd exp" \
+    [multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
+	 "&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
+	 "&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
+	 "&\"gdb.GdbError..\"" \
+	 "\\^error,msg=\"Error occurred in Python\\.\""] \
+    "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+    ".*\\^done" \
+    "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+    "\\^error,msg=\"Invalid parameter: int\"" \
+    "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+    "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"Command redefined but we failing anyway\"" \
+    "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int - redefined from MI"
+
+mi_gdb_test "-pycmd-new int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd-new int - defined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+    ".*\\^error,msg=\"MI command name is empty.\"" \
+    "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+    ".*\\^error,msg=\"MI command name does not start with '-' followed by at least one letter or digit\\.\"" \
+    "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+    ".*\\^error,msg=\"MI command name contains invalid character: @\\.\"" \
+    "invalid character in MI command name"
+
+mi_gdb_test "python cmd=pycmd1('-abc')" \
+    ".*\\^done" \
+    "create command -abc, stored in a python variable"
+
+mi_gdb_test "python print(cmd.name)" \
+    ".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
+    "print the name of the stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str"
+
+mi_gdb_test "python cmd.installed = False" \
+    ".*\\^done" \
+    "uninstall the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
+    "-abc str, but now the command is gone"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the command is back again"
+
+mi_gdb_test "python other=pycmd2('-abc')" \
+    ".*\\^done" \
+    "create another command called -abc, stored in a separate python variable"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the other stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "print the installed status of the original stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-abc str, when the other command is in place"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the original mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the original command is back again"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "the other command is now not installed"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "the original command is now installed"
+
+mi_gdb_test "python aa = pycmd3('-aa', 'message one', 'xxx')" \
+    ".*\\^done" \
+    "created a new -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message one\"}" \
+    "call the -aa command"
+
+mi_gdb_test "python aa.__init__('-aa', 'message two', 'yyy')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "call the -aa command, get the new message"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    ".*\\^error,msg=\"can't reinitialize object with a different command name\"" \
+    "attempt to reinitialise aa variable to a new command name"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "check the aa object has not changed after failed initialization"
+
+mi_gdb_test "python aa.installed = False" \
+    ".*\\^done" \
+    "uninstall the -aa command"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    ".*\\^error,msg=\"can't reinitialize object with a different command name\"" \
+    "attempt to reinitialise aa variable to a new command name while uninstalled"
+
+mi_gdb_test "python aa.__init__('-aa', 'message three', 'zzz')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message while uninstalled"
+
+mi_gdb_test "python aa.installed = True" \
+    ".*\\^done" \
+    "install the -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,zzz={msg=\"message three\"}" \
+    "call the -aa command looking for message three"
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..031c5307589
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,100 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'int':
+            return { 'result': 42 }
+        elif argv[0] == 'str':
+            return { 'result': "Hello world!" }
+        elif argv[0] == 'ary':
+            return { 'result': [ 'Hello', 42 ] }
+        elif argv[0] == "dct":
+            return { 'result': { 'hello' : 'world', 'times' : 42} }
+        elif argv[0] == "bk1":
+            return { 'result': { BadKey() : 'world' } }
+        elif argv[0] == "bk2":
+            return { 'result': { 1 : 'world' } }
+        elif argv[0] == "bk3":
+            return { 'result': { ReallyBadKey() : 'world' } }
+        elif argv[0] == 'tpl':
+            return { 'result': ( 42 , 'Hello' ) }
+        elif argv[0] == 'itr':
+            return { 'result': iter([1,2,3]) }
+        elif argv[0] == 'nn1':
+            return None
+        elif argv[0] == 'nn2':
+            return { 'result': [ None ] }
+        elif argv[0] == 'red':
+            pycmd2('-pycmd')
+            return None
+        elif argv[0] == 'nd1':
+            return [ 1, 2, 3 ]
+        elif argv[0] == 'nd2':
+            return 123
+        elif argv[0] == 'nd3':
+            return "abc"
+        elif argv[0] == 'ik1':
+            return { 'xxx yyy': 123 }
+        elif argv[0] == 'ik2':
+            return { 'result': { 'xxx yyy': 123 } }
+        elif argv[0] == 'ik3':
+            return { 'xxx-yyy': 123 }
+        elif argv[0] == 'ik4':
+            return { 'xxx.yyy': 123 }
+        elif argv[0] == 'ik5':
+            return { '123xxxyyy': 123 }
+        elif argv[0] == 'empty_key':
+            return { '': 123 }
+        elif argv[0] == 'exp':
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == 'str':
+            return { 'result': "Ciao!" }
+        elif argv[0] == 'red':
+            pycmd1('-pycmd')
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == 'new':
+            pycmd1('-pycmd-new')
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+# This class creates a command that returns a string, which is passed
+# when the command is created.
+class pycmd3(gdb.MICommand):
+    def __init__(self, name, msg, top_level):
+        super(pycmd3, self).__init__(name)
+        self._msg = msg
+        self._top_level = top_level
+
+    def invoke(self, args):
+        return { self._top_level: { 'msg' : self._msg } }
-- 
2.25.4


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv2] gdb/python/mi: create MI commands using python
  2022-02-07 15:56         ` [PATCHv2] gdb/python/mi: create MI " Andrew Burgess
@ 2022-02-08 15:16           ` Simon Marchi
  2022-02-09 12:25             ` [PATCHv3] " Andrew Burgess
  0 siblings, 1 reply; 41+ messages in thread
From: Simon Marchi @ 2022-02-08 15:16 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches; +Cc: Jan Vrany

> +@defun MICommand.invoke (arguments)
> +This method is called by @value{GDBN} when the new MI command is
> +invoked.
> +
> +@var{arguments} is a list of strings.  Note, that @code{--thread}
> +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
> +they do not show up in @code{arguments}.
> +
> +If this method throws a @code{gdb.GdbError} exception, it is turned
> +into a @sc{GDB/MI} @code{^error} response.  If this method returns

Hmm, not only gdb.GdbError, but all exceptions will result in a ^error,
I think?

> +@code{None}, then the @sc{GDB/MI} command will return a @code{^done}
> +response with no additional values.
> +
> +Otherwise, the return value must be a dictionary, which is converted
> +to a @sc{GDB/MI} @var{RESULT-RECORD} (@pxref{GDB/MI Output Syntax}).
> +The keys of this dictionary must be strings, and are used as
> +@emph{VARIABLE} names in the @emph{RESULT-RECORD}, these strings must
> +comply with the naming rules detailed below.  The values of this
> +dictionary are recursively handled as follows:
> +
> +@itemize
> +@item If the value is Python sequence or iterator, it is converted to
> +@sc{GDB/MI} @emph{LIST} with elements converted recursively.
> +
> +@item If the value is Python dictionary, it is converted to
> +@sc{GDB/MI} @emph{TUPLE}.  Keys in that dictionary must be strings,
> +which comply with the @emph{VARIABLE} naming rules detailed below.
> +Values are converted recursively.
> +
> +@item Otherwise, value is first converted to a Python string using
> +@code{str ()} and then converted to @sc{GDB/MI} @emph{CONST}.
> +@end itemize
> +
> +The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
> +must follow C variable naming rules; the string must be at least one
> +character long, the first character must be in the set
> +@code{[a-zA-Z]}, while every subsequent character must be in the set
> +@code{[a-zA-Z0-9_]}.

Well, in C, identifiers can start with an underscore.  Not that I need
to use it.

> @@ -113,12 +109,12 @@ struct mi_command_cli : public mi_command
>     not have been added to mi_cmd_table.  Otherwise, return true, and
>     COMMAND was added to mi_cmd_table.  */

Change this to /* See ... */.  Also, this contains a bit more details
than what was put in the .h, notably the reason why the function can
fail.  It would be nice to preserve that.

>  
> -static bool
> +bool
>  insert_mi_cmd_entry (mi_command_up command)
>  {
>    gdb_assert (command != nullptr);
>  
> -  const std::string &name = command->name ();
> +  const std::string name (command->name ());

Is this change needed?

>  
>    if (mi_cmd_table.find (name) != mi_cmd_table.end ())
>      return false;
> @@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
>    return true;
>  }
>  
> +bool
> +remove_mi_cmd_entry (mi_command *command)
> +{
> +  gdb_assert (command != nullptr);

If command can't be nullptr, it could be a reference.

> diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
> index 9734a0d9437..e2d2ab10467 100644
> --- a/gdb/python/lib/gdb/__init__.py
> +++ b/gdb/python/lib/gdb/__init__.py
> @@ -82,6 +82,9 @@ frame_filters = {}
>  # Initial frame unwinders.
>  frame_unwinders = []
>  
> +# Hash containing all user created MI commands, the key is the command
> +# name, and the value is the gdb.MICommand object.
> +_mi_commands = {}

I would say "Dict" instead of "Hash", since that's how they are called
in Python.

>  
>  def _execute_unwinders(pending_frame):
>      """Internal function called from GDB to execute all unwinders.
> diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
> new file mode 100644
> index 00000000000..e289785c61e
> --- /dev/null
> +++ b/gdb/python/py-micmd.c
> @@ -0,0 +1,815 @@
> +/* MI Command Set for GDB, the GNU debugger.
> +
> +   Copyright (C) 2019 Free Software Foundation, Inc.

Wrong year?

> +/* Representation of a python gdb.MICommand object.  */
> +
> +struct micmdpy_object
> +{
> +  PyObject_HEAD
> +
> +  /* The object representing this command in the mi command table.  This
> +     pointer can be nullptr if the command is not currently installed into
> +     the mi command table (see gdb.MICommand.installed property).  */
> +  struct mi_command_py *mi_command;
> +
> +  /* The string representing the name of this command, without the leading
> +     dash.  This string is never nullptr once the python object has been

In comments, python -> Python.

> +     initialised.
> +
> +     The memory for this string was allocated with malloc, and needs to be
> +     deallocated with free when the python object is deallocated.
> +
> +     When the MI_COMMAND variable is not nullptr, then the mi_command_py

variable -> field?

> +     object's name will point back to this string.  */
> +  char *mi_command_name;
> +};
> +
> +/* The mi command implemented in python.  */

In comments, mi -> MI.

> +
> +struct mi_command_py : public mi_command
> +{
> +  /* Constructs a new mi_command_py object.  NAME is command name without
> +     leading dash.  OBJECT is a reference to a Python object implementing
> +     the command.  This object should inherit from gdb.MICommand and should
> +     implement method invoke (args). */

Two spaces at the end.

Also, should -> must?

> +
> +  mi_command_py (const char *name, micmdpy_object *object)
> +    : mi_command (name, nullptr),
> +      m_pyobj (object)
> +  {
> +    pymicmd_debug_printf ("this = %p", this);
> +  }
> +
> +  ~mi_command_py ()
> +  {
> +    /* The python object representing a mi command contains a pointer back
> +       to this c++ object.  We can safely set this pointer back to nullptr
> +       now, to indicate the python object no longer references a valid c++
> +       object.
> +
> +       However, the python object also holds the storage for our name
> +       string.  We can't clear that here as our parent's destructor might
> +       still want to reference that string.  Instead we rely on the python
> +       object deallocator to free that memory, and reset the pointer.  */
> +    m_pyobj->mi_command = nullptr;
> +
> +    pymicmd_debug_printf ("this = %p", this);
> +  };
> +
> +  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the mi
> +     command table correctly.  This function looks up the command in the mi
> +     command table and checks that the object we get back references
> +     CMD_OBJ.  This function is only intended for calling within a
> +     gdb_assert.  This function performs many assertions internally, and
> +     then always returns true.  */
> +  static bool validate_installation (micmdpy_object *cmd_obj);

If validate_installation does some gdb_asserts and then returns true, I
don't really see the point of calling it inside a gdb_assert.  You can
just call it normally:

  validate_installation (cmd);

> +/* Parse RESULT and print it in mi format to the current_uiout.
> +
> +   This function handles the top-level result initially returned from the
> +   invoke method of the Python command implementation.  At the top-level
> +   the result must be a dictionary.  The values within this dictionary can
> +   be a wider range of types.  Handling the values of the top-level
> +   dictionary is done by parse_mi_result_1, see that function for more
> +   details.
> +
> +   If anything goes wrong while parsing and printing the mi output then an
> +   error is thrown.  */
> +
> +static void
> +parse_mi_result (PyObject *result)

Naming nit: I don't think parse is the appropriate word.  We're doing
the opposite here, so maybe use "serialize"?  Parsing would be to read
the text version of an MI result, to create an object hierarchy.

> +/* Return a reference to the gdb.mi_commands dictionary.  */
> +
> +static gdbpy_ref<>
> +micmdpy_global_command_dictionary ()
> +{
> +  if (gdb_python_module == nullptr
> +      || ! PyObject_HasAttrString (gdb_python_module, "_mi_commands"))
> +    error (_("unable to find gdb.mi_commands dictionary"));
> +
> +  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
> +						   "_mi_commands"));
> +  if (mi_cmd_dict == nullptr || !PyDict_Check (mi_cmd_dict.get ()))
> +    error (_("unable to fetch gdb.mi_commands dictionary"));

gdb.mi_commands -> gdb._mi_commands (twice in error messages, one in a
comment)?

I wonder if calling PyObject_HasAttrString is necessary, since you call
PyObject_GetAttrString just after.

> +/* Implement gdb.MICommand.__init__.  The init method takes the name of
> +   the mi command as the first argument, which must be a string, starting
> +   with a single dash.  */
> +
> +static int
> +micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
> +{
> +  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
> +
> +  micmdpy_object *cmd = (micmdpy_object *) self;
> +
> +  static const char *keywords[] = { "name", nullptr };
> +  const char *name;
> +
> +  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
> +					&name))
> +    return -1;
> +
> +  /* Validate command name */
> +  const int name_len = strlen (name);
> +  if (name_len == 0)
> +    {
> +      error (_("MI command name is empty."));

I think that error() calls in this function should be replaced with
setting the appropriate Python exception type.  For example, the above
should raise a ValueError.

> +      return -1;
> +    }
> +  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
> +    {
> +      error (_("MI command name does not start with '-'"
> +               " followed by at least one letter or digit."));
> +      return -1;
> +    }
> +  else
> +    {
> +      for (int i = 2; i < name_len; i++)
> +	{
> +	  if (!isalnum (name[i]) && name[i] != '-')
> +	    {
> +	      error (_("MI command name contains invalid character: %c."),
> +		     name[i]);
> +	      return -1;
> +	    }
> +	}
> +
> +      /* Skip over the leading dash.  For the rest of this function the
> +	 dash is not important.  */
> +      ++name;
> +    }
> +
> +  /* Check that there's an 'invoke' method.  */
> +  if (!PyObject_HasAttr (self, invoke_cst))
> +    error (_("-%s: Python command object missing 'invoke' method."), name);
> +
> +  /* If this object already has a name set, then this object has been
> +     initialized before.  We handle this case a little differently.  */
> +  if (cmd->mi_command_name != nullptr)

Huh, how can this happen?

> diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
> new file mode 100644
> index 00000000000..dd8012f1f7a
> --- /dev/null
> +++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
> @@ -0,0 +1,286 @@
> +# Copyright (C) 2019-2022 Free Software Foundation, Inc.
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +# Test custom MI commands implemented in Python.
> +
> +load_lib gdb-python.exp
> +load_lib mi-support.exp
> +set MIFLAGS "-i=mi2"

Any reason to use mi2 specifically?

> diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
> new file mode 100644
> index 00000000000..031c5307589
> --- /dev/null
> +++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
> @@ -0,0 +1,100 @@
> +# Copyright (C) 2019-2022 Free Software Foundation, Inc.
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU General Public License as published by
> +# the Free Software Foundation; either version 3 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +
> +import gdb
> +
> +class BadKey:
> +    def __repr__(self):
> +        return "Bad Key"
> +
> +class ReallyBadKey:
> +    def __repr__(self):
> +        return BadKey()
> +
> +
> +class pycmd1(gdb.MICommand):
> +    def invoke(self, argv):
> +        if argv[0] == 'int':
> +            return { 'result': 42 }
> +        elif argv[0] == 'str':
> +            return { 'result': "Hello world!" }

Don't forget to run black.

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCHv3] gdb/python/mi: create MI commands using python
  2022-02-08 15:16           ` Simon Marchi
@ 2022-02-09 12:25             ` Andrew Burgess
  2022-02-09 14:08               ` Simon Marchi
  2022-02-24 10:37               ` [PATCHv4] " Andrew Burgess
  0 siblings, 2 replies; 41+ messages in thread
From: Andrew Burgess @ 2022-02-09 12:25 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Jan Vrany

Simon,

Thanks for the feedback.  There's an updated version of the patch
included at the bottom, but I had some questions on a couple of
points, so I might need to roll another version to fix the last few
points.

* Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> [2022-02-08 10:16:50 -0500]:

> > +@defun MICommand.invoke (arguments)
> > +This method is called by @value{GDBN} when the new MI command is
> > +invoked.
> > +
> > +@var{arguments} is a list of strings.  Note, that @code{--thread}
> > +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
> > +they do not show up in @code{arguments}.
> > +
> > +If this method throws a @code{gdb.GdbError} exception, it is turned
> > +into a @sc{GDB/MI} @code{^error} response.  If this method returns
> 
> Hmm, not only gdb.GdbError, but all exceptions will result in a ^error,
> I think?

Yes, that's correct.  I've reworded this a bit.  What I was trying to
say is that gdb.GdbError will _only_ produce an ^error, that is,
raising gdb.GdbError is a legitimate way for a command to signal to
the user that something was wrong, e.g.

  class Foo(gdb.MICommand):
    def invoke(self, args):
      raise gdb.GdbError("command not implemented")
  Foo('-foo')

leading to:

  -foo
  ^error,msg="command not implemented"

In contrast, any other exception type indicates an error in the user's
Python code, so we'll also get a Python stack trace, e.g.:

  class Foo(gdb.MICommand):
    def invoke(self, args):
      return {'result': 5/0}
  Foo('-foo')

leading to:

  -foo
  &"Python Exception <class 'ZeroDivisionError'>: division by zero\n"
  ^error,msg="Error occurred in Python: division by zero"

Hopefully, the updated docs make this difference a little clearer.

> 
> > +@code{None}, then the @sc{GDB/MI} command will return a @code{^done}
> > +response with no additional values.
> > +
> > +Otherwise, the return value must be a dictionary, which is converted
> > +to a @sc{GDB/MI} @var{RESULT-RECORD} (@pxref{GDB/MI Output Syntax}).
> > +The keys of this dictionary must be strings, and are used as
> > +@emph{VARIABLE} names in the @emph{RESULT-RECORD}, these strings must
> > +comply with the naming rules detailed below.  The values of this
> > +dictionary are recursively handled as follows:
> > +
> > +@itemize
> > +@item If the value is Python sequence or iterator, it is converted to
> > +@sc{GDB/MI} @emph{LIST} with elements converted recursively.
> > +
> > +@item If the value is Python dictionary, it is converted to
> > +@sc{GDB/MI} @emph{TUPLE}.  Keys in that dictionary must be strings,
> > +which comply with the @emph{VARIABLE} naming rules detailed below.
> > +Values are converted recursively.
> > +
> > +@item Otherwise, value is first converted to a Python string using
> > +@code{str ()} and then converted to @sc{GDB/MI} @emph{CONST}.
> > +@end itemize
> > +
> > +The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
> > +must follow C variable naming rules; the string must be at least one
> > +character long, the first character must be in the set
> > +@code{[a-zA-Z]}, while every subsequent character must be in the set
> > +@code{[a-zA-Z0-9_]}.
> 
> Well, in C, identifiers can start with an underscore.  Not that I need
> to use it.

Yeah, that was a stupid mistake.  I've removed the reference to C.
I've left the restriction the same for now.  If someone comes along
with a compelling argument that we should be less strict, then it
should be harmless to relax the restrictions for later releases of gdb.

> 
> > @@ -113,12 +109,12 @@ struct mi_command_cli : public mi_command
> >     not have been added to mi_cmd_table.  Otherwise, return true, and
> >     COMMAND was added to mi_cmd_table.  */
> 
> Change this to /* See ... */.  Also, this contains a bit more details
> than what was put in the .h, notably the reason why the function can
> fail.  It would be nice to preserve that.

Done.

> 
> >  
> > -static bool
> > +bool
> >  insert_mi_cmd_entry (mi_command_up command)
> >  {
> >    gdb_assert (command != nullptr);
> >  
> > -  const std::string &name = command->name ();
> > +  const std::string name (command->name ());
> 
> Is this change needed?

No, it just felt clearer.  They both compile to the same thing as
command->name() returns a 'char *', so you end up creating a new
std::string in both cases.

Anyway, I reverted this as it's not important for this patch.

> 
> >  
> >    if (mi_cmd_table.find (name) != mi_cmd_table.end ())
> >      return false;
> > @@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
> >    return true;
> >  }
> >  
> > +bool
> > +remove_mi_cmd_entry (mi_command *command)
> > +{
> > +  gdb_assert (command != nullptr);
> 
> If command can't be nullptr, it could be a reference.

I wasn't sure about this change.  All the caller ever has is a
pointer, so this would force every caller to (a) add an assert, then
(b) call the function with the dereferenced pointer.

I'd hope the compiler would be smart enough to generate pretty much
the same code - except for moving the assert out of the function to
every call site - which doesn't fell like a win to me.

If we had an actual object at, even just some of, the call sites, I'd
probably agree with you...

... have I convinced you?  Or would you still like this changed to a
reference?

> 
> > diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
> > index 9734a0d9437..e2d2ab10467 100644
> > --- a/gdb/python/lib/gdb/__init__.py
> > +++ b/gdb/python/lib/gdb/__init__.py
> > @@ -82,6 +82,9 @@ frame_filters = {}
> >  # Initial frame unwinders.
> >  frame_unwinders = []
> >  
> > +# Hash containing all user created MI commands, the key is the command
> > +# name, and the value is the gdb.MICommand object.
> > +_mi_commands = {}
> 
> I would say "Dict" instead of "Hash", since that's how they are called
> in Python.

Done.

> 
> >  
> >  def _execute_unwinders(pending_frame):
> >      """Internal function called from GDB to execute all unwinders.
> > diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
> > new file mode 100644
> > index 00000000000..e289785c61e
> > --- /dev/null
> > +++ b/gdb/python/py-micmd.c
> > @@ -0,0 +1,815 @@
> > +/* MI Command Set for GDB, the GNU debugger.
> > +
> > +   Copyright (C) 2019 Free Software Foundation, Inc.
> 
> Wrong year?

I updated this to 2019 - 2022.  The patch was originally posted way
back in 2019, and my understanding is that the from date should be
when the code was first posted to the m/l.

> 
> > +/* Representation of a python gdb.MICommand object.  */
> > +
> > +struct micmdpy_object
> > +{
> > +  PyObject_HEAD
> > +
> > +  /* The object representing this command in the mi command table.  This
> > +     pointer can be nullptr if the command is not currently installed into
> > +     the mi command table (see gdb.MICommand.installed property).  */
> > +  struct mi_command_py *mi_command;
> > +
> > +  /* The string representing the name of this command, without the leading
> > +     dash.  This string is never nullptr once the python object has been
> 
> In comments, python -> Python.

Done.  Sorry if I missed any.

> 
> > +     initialised.
> > +
> > +     The memory for this string was allocated with malloc, and needs to be
> > +     deallocated with free when the python object is deallocated.
> > +
> > +     When the MI_COMMAND variable is not nullptr, then the mi_command_py
> 
> variable -> field?

Done.

> 
> > +     object's name will point back to this string.  */
> > +  char *mi_command_name;
> > +};
> > +
> > +/* The mi command implemented in python.  */
> 
> In comments, mi -> MI.

Done, again, sorry if I missed any.

> 
> > +
> > +struct mi_command_py : public mi_command
> > +{
> > +  /* Constructs a new mi_command_py object.  NAME is command name without
> > +     leading dash.  OBJECT is a reference to a Python object implementing
> > +     the command.  This object should inherit from gdb.MICommand and should
> > +     implement method invoke (args). */
> 
> Two spaces at the end.
> 
> Also, should -> must?

Both done.

> 
> > +
> > +  mi_command_py (const char *name, micmdpy_object *object)
> > +    : mi_command (name, nullptr),
> > +      m_pyobj (object)
> > +  {
> > +    pymicmd_debug_printf ("this = %p", this);
> > +  }
> > +
> > +  ~mi_command_py ()
> > +  {
> > +    /* The python object representing a mi command contains a pointer back
> > +       to this c++ object.  We can safely set this pointer back to nullptr
> > +       now, to indicate the python object no longer references a valid c++
> > +       object.
> > +
> > +       However, the python object also holds the storage for our name
> > +       string.  We can't clear that here as our parent's destructor might
> > +       still want to reference that string.  Instead we rely on the python
> > +       object deallocator to free that memory, and reset the pointer.  */
> > +    m_pyobj->mi_command = nullptr;
> > +
> > +    pymicmd_debug_printf ("this = %p", this);
> > +  };
> > +
> > +  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the mi
> > +     command table correctly.  This function looks up the command in the mi
> > +     command table and checks that the object we get back references
> > +     CMD_OBJ.  This function is only intended for calling within a
> > +     gdb_assert.  This function performs many assertions internally, and
> > +     then always returns true.  */
> > +  static bool validate_installation (micmdpy_object *cmd_obj);
> 
> If validate_installation does some gdb_asserts and then returns true, I
> don't really see the point of calling it inside a gdb_assert.  You can
> just call it normally:
> 
>   validate_installation (cmd);

Done.  My original thinking was that, if we did ever have a mode where
asserts were compiled out then this would remove the call.  But I
realise that (a) that's just dumb premature optimisation, and (b) the
compiler would probably nuke the call to the empty function anyway.

> 
> > +/* Parse RESULT and print it in mi format to the current_uiout.
> > +
> > +   This function handles the top-level result initially returned from the
> > +   invoke method of the Python command implementation.  At the top-level
> > +   the result must be a dictionary.  The values within this dictionary can
> > +   be a wider range of types.  Handling the values of the top-level
> > +   dictionary is done by parse_mi_result_1, see that function for more
> > +   details.
> > +
> > +   If anything goes wrong while parsing and printing the mi output then an
> > +   error is thrown.  */
> > +
> > +static void
> > +parse_mi_result (PyObject *result)
> 
> Naming nit: I don't think parse is the appropriate word.  We're doing
> the opposite here, so maybe use "serialize"?  Parsing would be to read
> the text version of an MI result, to create an object hierarchy.

Changed to serialize.

> 
> > +/* Return a reference to the gdb.mi_commands dictionary.  */
> > +
> > +static gdbpy_ref<>
> > +micmdpy_global_command_dictionary ()
> > +{
> > +  if (gdb_python_module == nullptr
> > +      || ! PyObject_HasAttrString (gdb_python_module, "_mi_commands"))
> > +    error (_("unable to find gdb.mi_commands dictionary"));
> > +
> > +  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
> > +						   "_mi_commands"));
> > +  if (mi_cmd_dict == nullptr || !PyDict_Check (mi_cmd_dict.get ()))
> > +    error (_("unable to fetch gdb.mi_commands dictionary"));
> 
> gdb.mi_commands -> gdb._mi_commands (twice in error messages, one in a
> comment)?

Done.  This mistake was all over the place actually, I believe I fixed
them all.  Result of a last minute naming change.

> 
> I wonder if calling PyObject_HasAttrString is necessary, since you call
> PyObject_GetAttrString just after.

You're right.  Removed.

> 
> > +/* Implement gdb.MICommand.__init__.  The init method takes the name of
> > +   the mi command as the first argument, which must be a string, starting
> > +   with a single dash.  */
> > +
> > +static int
> > +micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
> > +{
> > +  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
> > +
> > +  micmdpy_object *cmd = (micmdpy_object *) self;
> > +
> > +  static const char *keywords[] = { "name", nullptr };
> > +  const char *name;
> > +
> > +  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
> > +					&name))
> > +    return -1;
> > +
> > +  /* Validate command name */
> > +  const int name_len = strlen (name);
> > +  if (name_len == 0)
> > +    {
> > +      error (_("MI command name is empty."));
> 
> I think that error() calls in this function should be replaced with
> setting the appropriate Python exception type.  For example, the above
> should raise a ValueError.

This opened a can of worms :)  The biggest change in this new
iteration is the error handling.  Far fewer calls to error() now, in
most cases we set a Python exception instead.

> 
> > +      return -1;
> > +    }
> > +  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
> > +    {
> > +      error (_("MI command name does not start with '-'"
> > +               " followed by at least one letter or digit."));
> > +      return -1;
> > +    }
> > +  else
> > +    {
> > +      for (int i = 2; i < name_len; i++)
> > +	{
> > +	  if (!isalnum (name[i]) && name[i] != '-')
> > +	    {
> > +	      error (_("MI command name contains invalid character: %c."),
> > +		     name[i]);
> > +	      return -1;
> > +	    }
> > +	}
> > +
> > +      /* Skip over the leading dash.  For the rest of this function the
> > +	 dash is not important.  */
> > +      ++name;
> > +    }
> > +
> > +  /* Check that there's an 'invoke' method.  */
> > +  if (!PyObject_HasAttr (self, invoke_cst))
> > +    error (_("-%s: Python command object missing 'invoke' method."), name);
> > +
> > +  /* If this object already has a name set, then this object has been
> > +     initialized before.  We handle this case a little differently.  */
> > +  if (cmd->mi_command_name != nullptr)
> 
> Huh, how can this happen?

Like this

  class Foo(gdb.MICommand):
      def __init__(self, name, msg):
          super(Foo, self).__init__(name)
          self.__msg = msg
      def invoke(self, args):
          return {'msg': self.__msg}

  cmd = Foo('-foo', 'Hello')
  cmd.__init__('-foo', 'Goodbye')

Now:

  -foo
  ^done,msg="Goodbye"

This is a pretty unusual use case, but is valid so we need to do
something sane.  The only restriction is that we can't support
changing the actual command name this way, so this will not work:

  cmd = Foo('-foo', 'Hello')
  cmd.__init__('-bar', 'Goodbye')
  ...
  ValueError: can't reinitialize object with a different command name

> 
> > diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
> > new file mode 100644
> > index 00000000000..dd8012f1f7a
> > --- /dev/null
> > +++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
> > @@ -0,0 +1,286 @@
> > +# Copyright (C) 2019-2022 Free Software Foundation, Inc.
> > +# This program is free software; you can redistribute it and/or modify
> > +# it under the terms of the GNU General Public License as published by
> > +# the Free Software Foundation; either version 3 of the License, or
> > +# (at your option) any later version.
> > +#
> > +# This program is distributed in the hope that it will be useful,
> > +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +# GNU General Public License for more details.
> > +#
> > +# You should have received a copy of the GNU General Public License
> > +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> > +
> > +# Test custom MI commands implemented in Python.
> > +
> > +load_lib gdb-python.exp
> > +load_lib mi-support.exp
> > +set MIFLAGS "-i=mi2"
> 
> Any reason to use mi2 specifically?

Nope, just lazy copy 'n' paste from another script.  Relaxed to
'-i=mi' now.

> 
> > diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
> > new file mode 100644
> > index 00000000000..031c5307589
> > --- /dev/null
> > +++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
> > @@ -0,0 +1,100 @@
> > +# Copyright (C) 2019-2022 Free Software Foundation, Inc.
> > +# This program is free software; you can redistribute it and/or modify
> > +# it under the terms of the GNU General Public License as published by
> > +# the Free Software Foundation; either version 3 of the License, or
> > +# (at your option) any later version.
> > +#
> > +# This program is distributed in the hope that it will be useful,
> > +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +# GNU General Public License for more details.
> > +#
> > +# You should have received a copy of the GNU General Public License
> > +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> > +
> > +import gdb
> > +
> > +class BadKey:
> > +    def __repr__(self):
> > +        return "Bad Key"
> > +
> > +class ReallyBadKey:
> > +    def __repr__(self):
> > +        return BadKey()
> > +
> > +
> > +class pycmd1(gdb.MICommand):
> > +    def invoke(self, argv):
> > +        if argv[0] == 'int':
> > +            return { 'result': 42 }
> > +        elif argv[0] == 'str':
> > +            return { 'result': "Hello world!" }
> 
> Don't forget to run black.

Done.

New patch below.

Thanks,
Andrew

---

commit 0e7ab9fc15f424ce2640869e750df3c87356e682
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Tue Jun 23 14:45:38 2020 +0100

    gdb/python/mi: create MI commands using python
    
    This commit allows an user to create custom MI commands using Python
    similarly to what is possible for Python CLI commands.
    
    A new subclass of mi_command is defined for Python MI commands,
    mi_command_py. A new file, py-micmd.c contains the logic for Python MI
    commands.
    
    This commit is based on work linked too from this mailing list thread:
    
      https://sourceware.org/pipermail/gdb/2021-November/049774.html
    
    Which has also been previously posted to the mailing list here:
    
      https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html
    
    And was recently reposted here:
    
      https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html
    
    This patch takes some core code from the previous posted patches, but
    also has some significant differences, especially after the feedback
    given here:
    
      https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html
    
    A new MI command can be implemented in Python like this:
    
      class echo_args(gdb.MICommand):
          def invoke(self,args):
              return { 'args': args }
    
      echo_args("-echo-args")
    
    The 'args' parameter is a list containing all command line arguments
    passed to the MI command.  This list can be empty if the MI command
    was passed no arguments.
    
    When used within gdb the above command produced output like this:
    
      (gdb)
      -echo-args a b c
      ^done,args=["a","b","c"]
      (gdb)
    
    The 'invoke' method of the new command must return a dictionary.  The
    keys of this dictionary are then used as the field names in the mi
    command output (e.g. 'args' in the above).
    
    The values of the result returned by invoke can be dictionaries,
    lists, iterators, or an object that can be converted to a string.
    These are processed recursively to create the mi output.  And so, this
    is valid:
    
      class new_command(gdb.MICommand):
          def invoke(self,args):
              return { 'result_one': { 'abc': 123, 'def': 'Hello' },
                       'result_two': [ { 'a': 1, 'b': 2 },
                                       { 'c': 3, 'd': 4 } ] }
    
    Which produces output like:
    
      (gdb)
      -new-command
      ^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
      (gdb)
    
    I have required that the fields names used in mi result output must
    follow C identifier restrictions (i.e. must match the regexp
    "[a-zA-Z][a-zA-Z0-9_]*").  This restriction was never written down
    anywhere before, but seems sensible to me.  We can always loosen this
    rule later if it proves to be a problem.  Much harder to try and add a
    restriction later, if folk are already using the API, and breaking the
    restriction.
    
    What follows are some details about how this implementation differs
    from the original patch that was posted to the mailing list.
    
    In this patch, I have changed how the lifetime of the Python
    gdb.MICommand objects is managed.  In the original patch, these object
    were kept alive by an owned reference within the mi_command_py object.
    As such, the Python object would not be deleted until the
    mi_command_py object itself was deleted.
    
    This caused a problem, the mi_command_py were held in the global mi
    command table (in mi/mi-cmds.c), which, as a global, was not cleared
    until program shutdown.  By this point the Python interpreter has
    already been shutdown.  Attempting to delete the mi_command_py object
    at this point was causing GDB to try and invoke Python code after
    finalising the Python interpreter, and we would crash.
    
    To work around this problem, the original patch added code in
    python/python.c that would search the mi command table, and delete the
    mi_command_py objects before the Python environment was finalised.
    
    In contrast, in this patch, I have added a new global dictionary to
    the gdb module, gdb._mi_commands.  We already have several such global
    data stores related to pretty printers, and frame unwinders.
    
    The MICommand objects are placed into the new gdb.mi_commands
    dictionary, and it is this reference that keeps the objects alive.
    When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
    and any MICommand objects within it are deleted at this point.
    
    This change avoids having to make the mi_cmd_table global, and walk
    over it from within GDB's python related code.
    
    This patch handles command redefinition entirely within GDB's python
    code, though this does impose one small restriction which is not
    present in the original code (detailed below), I don't think this is a
    big issue.  However, the original patch relied on being able to
    finish executing the mi_command::do_invoke member function after the
    mi_command object had been deleted.  Though continuing to execute a
    member function after an object is deleted is well defined, it is
    also (IMHO) risky, its too easy for someone to later add a use of the
    object without realising that the object might sometimes, have been
    deleted.  The new patch avoids this issue.
    
    The one restriction that is added to avoid this, is that an MICommand
    object can't be reinitialised with a different command name, so:
    
      (gdb) python cmd = MyMICommand("-abc")
      (gdb) python cmd.__init__("-def")
      can't reinitialize object with a different command name
    
    This feels like a pretty weird edge case, and I'm happy to live with
    this restriction.
    
    I have also changed how the memory is managed for the command name.
    In the most recently posted patch series, the command name is moved
    into a subclass of mi_command, the python mi_command_py, which
    inherits from mi_command is then free to use a smart pointer to manage
    the memory for the name.
    
    In this patch, I leave the mi_command class unchanged, and instead
    hold the memory for the name within the Python object, as the lifetime
    of the Python object always exceeds the c++ object stored in the
    mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
    leaves the mi_command class nice and simple.
    
    Next, this patch adds some extra functionality, there's a
    MICommand.name read-only attribute containing the name of the command,
    and a read-write MICommand.installed attribute that can be used to
    install (make the command available for use) and uninstall (remove the
    command from the mi_cmd_table so it can't be used) the command.  This
    attribute will be automatically updated if a second command replaces
    an earlier command.
    
    This patch adds additional error handling, and makes more use the
    gdbpy_handle_exception function.
    
    Co-Authored-By: Jan Vrany <jan.vrany@labware.com>

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index bf19db45343..681c9cb682f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index b4a515120db..20acb34f56b 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -175,6 +175,8 @@ info win
      set styling').  When false, which is the default if the argument
      is not given, then no styling is applied to the returned string.
 
+  ** It is now possible to add GDB/MI commands implemented in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index c1a3f5f2a7e..ac52ad43c2e 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -95,6 +95,7 @@
 23
 @end smallexample
 
+@anchor{set_python_print_stack}
 @kindex set python print-stack
 @item set python print-stack
 By default, @value{GDBN} will print only the message component of a
@@ -204,7 +205,8 @@
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -419,7 +421,8 @@
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2146,7 +2149,7 @@
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3850,11 +3853,12 @@
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
-@cindex commands in python
-@cindex python commands
+@cindex CLI commands in python
+@cindex commands in python, CLI
+@cindex python commands, CLI
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
 command is implemented using an instance of the @code{gdb.Command}
 class, most commonly using a subclass.
@@ -4133,6 +4137,152 @@
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python, GDB/MI
+@cindex python commands, GDB/MI
+It is possible to add @sc{GDB/MI} (@pxref{GDB/MI}) commands
+implemented in Python.  A @sc{GDB/MI} command is implemented using an
+instance of the @code{gdb.MICommand} class, most commonly using a
+subclass.
+
+@defun MICommand.__init__ (name)
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid name of a
+@sc{GDB/MI} command, and in particular must start with a hyphen
+(@code{-}).  Reusing the name of a built-in @sc{GDB/MI} is not
+allowed, and a @code{RuntimeError} will be raised.  Using the name
+of an @sc{GDB/MI} command previously defined in Python is allowed, the
+previous command will be replaced with the new command.
+@end defun
+
+@defun MICommand.invoke (arguments)
+This method is called by @value{GDBN} when the new MI command is
+invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method raises an exception, then it is turned into a
+@sc{GDB/MI} @code{^error} response.  Only @code{gdb.GdbError}
+exceptions (or its sub-classes) should be used for reporting errors to
+users, any other exception type is treated as a failure of the
+@code{invoke} method, and the exception will be printed to the error
+stream according to the @kbd{set python print-stack} setting
+(@pxref{set_python_print_stack,,@kbd{set python print-stack}}).
+
+If this method returns @code{None}, then the @sc{GDB/MI} command will
+return a @code{^done} response with no additional values.
+
+Otherwise, the return value must be a dictionary, which is converted
+to a @sc{GDB/MI} @var{RESULT-RECORD} (@pxref{GDB/MI Output Syntax}).
+The keys of this dictionary must be strings, and are used as
+@emph{VARIABLE} names in the @emph{RESULT-RECORD}, these strings must
+comply with the naming rules detailed below.  The values of this
+dictionary are recursively handled as follows:
+
+@itemize
+@item If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @emph{LIST} with elements converted recursively.
+
+@item If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @emph{TUPLE}.  Keys in that dictionary must be strings,
+which comply with the @emph{VARIABLE} naming rules detailed below.
+Values are converted recursively.
+
+@item Otherwise, value is first converted to a Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @emph{CONST}.
+@end itemize
+
+The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
+must follow the following rules; the string must be at least one
+character long, the first character must be in the set
+@code{[a-zA-Z]}, while every subsequent character must be in the set
+@code{[a-zA-Z0-9_]}.
+
+@end defun
+
+An instance of @code{MICommand} has the following attributes:
+
+@defvar MICommand.name
+A string, the name of this @sc{GDB/MI} command, as was passed to the
+@code{__init__} method.  This attribute is read-only.
+@end defvar
+
+@defvar MICommand.installed
+A boolean value indicating if this command is installed ready for a
+user to call from the command line.  Commands are automatically
+installed when they are instantiated, after which this attribute will
+be @code{True}.
+
+If later, a new command is created with the same name, then the
+original command will become uninstalled, and this attribute will be
+@code{False}.
+
+This attribute can be is read-write, setting this attribute to
+@code{False} will uninstall the command, removing it from the set of
+available commands.  Setting this attribute to @code{True} will
+install the command for use.  If there is already a Python command
+with this name installed, the currently installed command will be
+uninstalled, and this command installed in its place.
+@end defvar
+
+The following code snippet shows how a two trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo arguments passed to the command."""
+
+    def __init__(self, name, mode, toplevel = None):
+        self._mode = mode
+        super(MIEcho, self).__init__(name, toplevel)
+
+    def invoke(self, argv):
+        if self._mode == 'dict':
+            return @{ 'result': @{ 'argv' : argv @} @}
+        elif self._mode == 'list':
+            return @{ 'result': argv @}
+        else:
+            return @{ 'result': ", ".join(argv) @}
+
+
+MIEcho("-echo-dict", "dict")
+MIEcho("-echo-list", "list")
+MIEcho("-echo-string", "string", "argv")
+@end smallexample
+
+The last three lines instantiate the class three times, creating three
+new @sc{GDB/MI} commands @code{-echo-dict}, @code{-echo-list}, and
+@code{-echo-string}.  Each time a subclass of @code{gdb.MICommand} is
+instantiated, the new command is automatically registered with
+@value{GDBN}.
+
+Depending on how the Python code is read into @value{GDBN}, you may
+need to import the @code{gdb} module explicitly.
+
+The following example shows a @value{GDBN} session in which the above
+commands have been added:
+
+@smallexample
+(@value{GDBP})
+-echo-dict abc def ghi
+^done,dict=@{argv=["abc","def","ghi"]@}
+(@value{GDBP})
+-echo-list abc def ghi
+^done,list=["abc","def","ghi"]
+(@value{GDBP})
+-echo-string abc def ghi
+^done,string="abc, def, ghi"
+(@value{GDBP})
+@end smallexample
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4170,7 +4320,7 @@
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..8a617aeced3 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -108,12 +104,9 @@ struct mi_command_cli : public mi_command
   bool m_args_p;
 };
 
-/* Insert COMMAND into the global mi_cmd_table.  Return false if
-   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
-   not have been added to mi_cmd_table.  Otherwise, return true, and
-   COMMAND was added to mi_cmd_table.  */
+/* See mi-cmds.h.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
@@ -127,6 +120,22 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+/* See mi-cmds.h.  */
+
+bool
+remove_mi_cmd_entry (mi_command *command)
+{
+  gdb_assert (command != nullptr);
+
+  const std::string name (command->name ());
+
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..89f680d7c4f 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the global mi_cmd_table.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,18 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert COMMAND into the global mi_cmd_table.  Return false if
+   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
+   not have been added to mi_cmd_table.  Otherwise, return true, and
+   COMMAND was added to mi_cmd_table.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove COMMAND from the global mi_cmd_table.  Return true if the removal
+   was success, otherwise return false, which indicates COMMAND was not
+   found in the mi_cmd_table. */
+
+extern bool remove_mi_cmd_entry (mi_command *command);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 9734a0d9437..0780cb4ec22 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -82,6 +82,10 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Dictionary containing all user created MI commands, the key is the
+# command name, and the value is the gdb.MICommand object.
+_mi_commands = {}
+
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..8c236a4a8c4
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,848 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* GDB/MI commands implemented in Python.  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "cli/cli-cmds.h"
+#include <string>
+
+/* Debugging of Python MI commands.  */
+
+static bool pymicmd_debug;
+
+/* Implementation of "show debug py-micmd".  */
+
+static void
+show_pymicmd_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Python MI command debugging is %s.\n"), value);
+}
+
+/* Print a "py-micmd" debug statement.  */
+
+#define pymicmd_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
+
+/* Print a "py-micmd" enter/exit debug statements.  */
+
+#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
+  scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
+
+struct mi_command_py;
+
+/* Representation of a Python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the MI command table.  This
+     pointer can be nullptr if the command is not currently installed into
+     the MI command table (see gdb.MICommand.installed property).  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing the name of this command, without the leading
+     dash.  This string is never nullptr once the Python object has been
+     initialised.
+
+     The memory for this string was allocated with malloc, and needs to be
+     deallocated with free when the Python object is deallocated.
+
+     When the MI_COMMAND field is not nullptr, then the mi_command_py
+     object's name will point back to this string.  */
+  char *mi_command_name;
+};
+
+/* The MI command implemented in Python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object must inherit from gdb.MICommand and must
+     implement the invoke method.  */
+
+  mi_command_py (const char *name, micmdpy_object *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  {
+    pymicmd_debug_printf ("this = %p", this);
+  }
+
+  ~mi_command_py ()
+  {
+    /* The Python object representing a MI command contains a pointer back
+       to this c++ object.  We can safely set this pointer back to nullptr
+       now, to indicate the Python object no longer references a valid c++
+       object.
+
+       However, the Python object also holds the storage for our name
+       string.  We can't clear that here as our parent's destructor might
+       still want to reference that string.  Instead we rely on the Python
+       object deallocator to free that memory, and reset the pointer.  */
+    m_pyobj->mi_command = nullptr;
+
+    pymicmd_debug_printf ("this = %p", this);
+  };
+
+  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the MI
+     command table correctly.  This function looks up the command in the MI
+     command table and checks that the object we get back references
+     CMD_OBJ.  This function is only intended for calling within a
+     gdb_assert.  This function performs many assertions internally, and
+     then always returns true.  */
+  static void validate_installation (micmdpy_object *cmd_obj);
+
+  /* Update m_pyobj to NEW_PYOBJ.  The pointer from M_PYOBJ that points
+     back to this object is swapped with the pointer in NEW_PYOBJ, which
+     must be nullptr, so that NEW_PYOBJ now points back to this object.
+     Additionally our parent's name string is stored in m_pyobj, so we
+     swap the name string with NEW_PYOBJ.
+
+     Before this call m_pyobj is the Python object representing this MI
+     command object.  After this call has completed, NEW_PYOBJ now
+     represents this MI command object.  */
+  void swap_python_object (micmdpy_object *new_pyobj)
+  {
+    gdb_assert (new_pyobj->mi_command == nullptr);
+    std::swap (new_pyobj->mi_command, m_pyobj->mi_command);
+    std::swap (new_pyobj->mi_command_name, m_pyobj->mi_command_name);
+    m_pyobj = new_pyobj;
+  }
+
+protected:
+  /* Called when the MI command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The Python object representing this MI command.  */
+  micmdpy_object *m_pyobj;
+};
+
+extern PyTypeObject micmdpy_object_type
+	CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* Holds a Python object containing the string 'invoke'.  */
+
+static PyObject *invoke_cst;
+
+/* Check that OBJECT has an attribute named after the string in the global
+   invoke_cst.  Return true if OBJECT does have a matching attribute,
+   otherwise, return false, and a suitable Python exception will have been
+   set.  */
+static bool
+micmdpy_check_invoke_attr (PyObject *object, const char *name)
+{
+  gdb_assert (invoke_cst != nullptr);
+  gdb_assert (object != nullptr);
+
+  if (!PyObject_HasAttr (object, invoke_cst))
+    {
+      PyErr_Format (PyExc_ValueError,
+		    _("-%s: Python command object missing 'invoke' method."),
+		    name);
+      return false;
+    }
+
+  return true;
+}
+
+/* Convert KEY_OBJ into a string that can be used as a field name in MI
+   output.  KEY_OBJ must be a Python string object, and must only contain
+   characters suitable for use as an MI field name.
+
+   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
+   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
+   and returned.  */
+
+static gdb::unique_xmalloc_ptr<char>
+py_object_to_mi_key (PyObject *key_obj)
+{
+  /* The key must be a string.  */
+  if (!PyString_Check (key_obj))
+    {
+      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
+      gdb::unique_xmalloc_ptr<char> key_repr_string;
+      if (key_repr != nullptr)
+	key_repr_string = python_string_to_target_string (key_repr.get ());
+
+      if (key_repr_string != nullptr)
+	PyErr_Format (PyExc_TypeError,
+		      _("Non-string object used as key: %s"),
+		      key_repr_string.get ());
+      gdbpy_handle_exception ();
+    }
+
+  gdb::unique_xmalloc_ptr<char> key_string
+    = python_string_to_target_string (key_obj);
+  if (key_string == nullptr)
+    gdbpy_handle_exception ();
+
+  /* Predicate function, returns true if NAME is a valid field name for use
+     in MI result output, otherwise, returns false.  */
+  auto is_valid_key_name = [] (const char *name) -> bool
+  {
+    gdb_assert (name != nullptr);
+
+    if (*name == '\0' || !isalpha (*name))
+      return false;
+
+    for (; *name != '\0'; ++name)
+      if (!isalnum (*name) && *name != '_')
+	return false;
+
+    return true;
+  };
+
+  if (!is_valid_key_name (key_string.get ()))
+    {
+      if (*key_string.get () == '\0')
+	PyErr_Format (PyExc_ValueError, _("Invalid empty key in MI result"));
+      else
+	PyErr_Format (PyExc_ValueError, _("Invalid key in MI result: %s"),
+		      key_string.get ());
+      gdbpy_handle_exception ();
+    }
+
+  return key_string;
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+   FIELD_NAME is used as the name of this result field.
+
+   RESULT can be a dictionary, a sequence, an iterator, or an object that
+   can be converted to a string, these are converted to the matching MI
+   output format (dictionaries as tuples, sequences and iterators as lists,
+   and strings as named fields).
+
+   If anything goes wrong while formatting the output then an error is
+   thrown.
+
+   This function is the recursive inner core of serialize_mi_result, and
+   should only be called from that function.  */
+
+static void
+serialize_mi_result_1 (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  gdb::unique_xmalloc_ptr<char> key_string
+	    (py_object_to_mi_key (key));
+	  serialize_mi_result_1 (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      Py_ssize_t len = PySequence_Size (result);
+      if (len == -1)
+	gdbpy_handle_exception ();
+      for (Py_ssize_t i = 0; i < len; ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  if (item == nullptr)
+	    gdbpy_handle_exception ();
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (true)
+	{
+	  item.reset (PyIter_Next (result));
+	  if (item == nullptr)
+	    {
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+	      break;
+	    }
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      if (string == nullptr)
+	gdbpy_handle_exception ();
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+
+   This function handles the top-level result initially returned from the
+   invoke method of the Python command implementation.  At the top-level
+   the result must be a dictionary.  The values within this dictionary can
+   be a wider range of types.  Handling the values of the top-level
+   dictionary is done by serialize_mi_result_1, see that function for more
+   details.
+
+   If anything goes wrong while parsing and printing the MI output then an
+   error is thrown.  */
+
+static void
+serialize_mi_result (PyObject *result)
+{
+  /* At the top-level, the result must be a dictionary.  */
+
+  if (!PyDict_Check (result))
+    {
+      PyErr_SetString (PyExc_TypeError,
+		       _("Result from invoke must be a dictionary"));
+      gdbpy_handle_exception ();
+    }
+
+  PyObject *key, *value;
+  Py_ssize_t pos = 0;
+  while (PyDict_Next (result, &pos, &key, &value))
+    {
+      gdb::unique_xmalloc_ptr<char> key_string
+	(py_object_to_mi_key (key));
+      serialize_mi_result_1 (value, key_string.get ());
+    }
+}
+
+/* Called when the MI command is invoked.  PARSE contains the parsed
+   command line arguments from the user.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == nullptr)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = (PyObject *) this->m_pyobj;
+  gdb_assert (obj != nullptr);
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  /* Check this object has the invoke attribute.  */
+  if (!micmdpy_check_invoke_attr (obj, name ()))
+    gdbpy_handle_exception ();
+
+  /* Place all the arguments into a list which we pass as a single
+     argument to the MI command's invoke method.  */
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    gdbpy_handle_exception ();
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i],
+					 strlen (parse->argv[i]),
+					 host_charset (), nullptr));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
+	gdbpy_handle_exception ();
+    }
+
+  gdb_assert (PyErr_Occurred () == nullptr);
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, invoke_cst,
+						  argobj.get (), nullptr));
+  if (result == nullptr)
+    gdbpy_handle_exception ();
+
+  if (result != Py_None)
+    serialize_mi_result (result.get ());
+}
+
+/* See declaration above.  */
+
+void
+mi_command_py::validate_installation (micmdpy_object *cmd_obj)
+{
+  gdb_assert (cmd_obj != nullptr);
+  mi_command_py *cmd = cmd_obj->mi_command;
+  gdb_assert (cmd != nullptr);
+  const char *name = cmd_obj->mi_command_name;
+  gdb_assert (name != nullptr);
+  gdb_assert (name == cmd->name ());
+  mi_command *mi_cmd = mi_cmd_lookup (name);
+  gdb_assert (mi_cmd == cmd);
+  gdb_assert (cmd->m_pyobj == cmd_obj);
+}
+
+/* Return a reference to the gdb._mi_commands dictionary.  If the
+   dictionary can't be found for any reason then nullptr is returned, and
+   a Python exception will be set.  */
+
+static gdbpy_ref<>
+micmdpy_global_command_dictionary ()
+{
+  if (gdb_python_module == nullptr)
+    {
+      PyErr_SetString (PyExc_RuntimeError, _("unable to find gdb module"));
+      return nullptr;
+    }
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "_mi_commands"));
+  if (mi_cmd_dict == nullptr)
+    return nullptr;
+
+  if (!PyDict_Check (mi_cmd_dict.get ()))
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("gdb._mi_commands is not a dictionary as expected"));
+      return nullptr;
+    }
+
+  return mi_cmd_dict;
+}
+
+/* Uninstall OBJ, making the MI command represented by OBJ unavailable for
+   use by the user.  On success 0 is returned, otherwise -1 is returned
+   and a Python exception will be set.  */
+
+static int
+micmdpy_uninstall_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command != nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  /* Remove the command from the internal MI table of commands, this will
+     cause the c++ object to be deleted, which will clear the mi_command
+     member variable within the Python object.  */
+  remove_mi_cmd_entry (obj->mi_command);
+  gdb_assert (obj->mi_command == nullptr);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Grab the name for this command.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+  if (name_obj == nullptr)
+    return -1;
+
+  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
+     commands, this is gdb._mi_command, and remove it.  */
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+
+  /* Did we encounter an error?  Failing to find the object in the
+     dictionary isn't an error, that's fine.  */
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+
+  /* Did we find this command in the gdb._mi_commands dictionary?  If so,
+     then remove it.  */
+  if (curr != nullptr)
+    {
+      /* Yes we did!  Remove it.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  return 0;
+}
+
+/* Install OBJ as a usable MI command.  Return 0 on success, and -1 on
+   error, in which case, a Python error will have been set.
+
+   After successful completion the command name associated with OBJ will
+   be installed in the MI command table (so it can be found if the user
+   enters that command name), additionally, OBJ will have been added to
+   the gdb._mi_commands dictionary (using the command name as its key),
+   this will ensure that OBJ remains live even if the user gives up all
+   references.  */
+
+static int
+micmdpy_install_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command == nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Look up this command name in the gdb._mi_commands dictionary, a
+     command with this name may already exist.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb._mi_commands
+	 dictionary.  First, validate that the object in the dictionary is
+	 of the expected type, just in case something weird has happened.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* To get to this function OBJ should not be installed, which should
+	 mean OBJ is not in the gdb._mi_commands dictionary.  If we find
+	 that OBJ is the thing in the dictionary, then something weird is
+	 going on, we should throw an error.  */
+      micmdpy_object *other = (micmdpy_object *) curr;
+      if (other == obj || other->mi_command == nullptr)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("uninstalled command found in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* All Python mi command object should always have a name set.  */
+      gdb_assert (other->mi_command_name != nullptr);
+
+      /* We always insert commands into the gdb._mi_commands dictionary
+	 using their name as a key, if this check fails then the dictionary
+	 is in some weird state.  */
+      if (other->mi_command_name != other->mi_command->name ()
+	  || strcmp (other->mi_command_name, obj->mi_command_name) != 0)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("gdb._mi_commands dictionary is corrupted"));
+	  return -1;
+	}
+
+      /* Switch the state of the c++ object held in the MI command table
+	 so that it now references OBJ.  After this action the old Python
+	 object that used to be referenced from the MI command table will
+	 now show as uninstalled, while the new Python object will show as
+	 installed.  */
+      other->mi_command->swap_python_object (obj);
+
+      gdb_assert (other->mi_command == nullptr);
+      gdb_assert (obj->mi_command != nullptr);
+      gdb_assert (obj->mi_command->name () == obj->mi_command_name);
+
+      /* Remove the previous Python object from the gdb._mi_commands
+	 dictionary, we'll install the new object below.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+       return -1;
+    }
+  else
+    {
+      /* There's no Python object for this command name in the
+	 gdb._mi_commands dictionary from which we can steal an existing
+	 object already held in the MI commands table, and so, we now
+	 create a new c++ object, and install it into the MI table.  */
+      obj->mi_command = new mi_command_py (obj->mi_command_name, obj);
+      mi_command_up micommand (obj->mi_command);
+
+      /* Add the command to the gdb internal MI command table.  */
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+      if (!result)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+    }
+
+  /* Finally, add the Python object to the gdb._mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), (PyObject *) obj) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.__init__.  The init method takes the name of
+   the MI command as the first argument, which must be a string, starting
+   with a single dash.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  static const char *keywords[] = { "name", nullptr };
+  const char *name;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
+					&name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      PyErr_SetString (PyExc_ValueError, _("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      PyErr_SetString (PyExc_ValueError,
+		       _("MI command name does not start with '-'"
+			 " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    {
+      for (int i = 2; i < name_len; i++)
+	{
+	  if (!isalnum (name[i]) && name[i] != '-')
+	    {
+	      PyErr_Format
+		(PyExc_ValueError,
+		 _("MI command name contains invalid character: %c."),
+		 name[i]);
+	      return -1;
+	    }
+	}
+
+      /* Skip over the leading dash.  For the rest of this function the
+	 dash is not important.  */
+      ++name;
+    }
+
+  /* Check that there's an 'invoke' method.  */
+  if (!micmdpy_check_invoke_attr (self, name))
+    return -1;
+
+  /* If this object already has a name set, then this object has been
+     initialized before.  We handle this case a little differently.  */
+  if (cmd->mi_command_name != nullptr)
+    {
+      /* First, we don't allow the user to change the MI command name.
+	 Supporting this would be tricky as we would need to delete the
+	 mi_command_py from the MI command table, however, the user might
+	 be trying to perform this reinitialization from within the very
+	 command we're about to delete... it all gets very messy.
+
+	 So, for now at least, we don't allow this.  This doesn't seem like
+	 an excessive restriction.  */
+      if (strcmp (cmd->mi_command_name, name) != 0)
+	{
+	  PyErr_SetString
+	    (PyExc_ValueError,
+	     _("can't reinitialize object with a different command name"));
+	  return -1;
+	}
+
+      /* If there's already an object registered with the MI command table,
+	 then we're done.  That object must be a mi_command_py, which
+	 should reference back to this micmdpy_object.  */
+      if (cmd->mi_command != nullptr)
+	{
+	  mi_command_py::validate_installation (cmd);
+	  return 0;
+	}
+    }
+  else
+    cmd->mi_command_name = xstrdup (name);
+
+  /* Now we can install this mi_command_py in the MI command table.  */
+  return micmdpy_install_command (cmd);
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+
+  /* If the Python object failed to initialize, then the name field might
+     be nullptr.  */
+  pymicmd_debug_printf ("obj = %p, name = %s", cmd,
+			(cmd->mi_command_name == nullptr
+			 ? "(null)" : cmd->mi_command_name));
+
+  /* Remove the command from the MI command table if needed.  This will
+     cause the mi_command_py object to be deleted, which, in turn, will
+     clear the cmd->mi_command member variable, hence the assert.  */
+  if (cmd->mi_command != nullptr)
+    remove_mi_cmd_entry (cmd->mi_command);
+  gdb_assert (cmd->mi_command == nullptr);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->mi_command_name);
+  cmd->mi_command_name = nullptr;
+
+  /* Finally, free the memory for this Python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Python initialization for the MI commands components.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == nullptr)
+    return -1;
+
+  return 0;
+}
+
+/* Get the gdb.MICommand.name attribute, returns a string, the name of this
+   MI command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->mi_command_name != nullptr);
+  std::string name_str = string_printf ("-%s", micmd_obj->mi_command_name);
+  return PyString_FromString (name_str.c_str ());
+}
+
+/* Get the gdb.MICommand.installed property.  Returns true if this MI
+   command is installed into the MI command table, otherwise returns
+   false.  */
+
+static PyObject *
+micmdpy_get_installed (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  if (micmd_obj->mi_command == nullptr)
+    Py_RETURN_FALSE;
+  Py_RETURN_TRUE;
+}
+
+/* Set the gdb.MICommand.installed property.  The property can be set to
+   either true or false.  Setting the property to true will cause the
+   command to be installed into the MI command table (if it isn't
+   already), while setting this property to false will cause the command
+   to be removed from the MI command table (if it is present).  */
+
+static int
+micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  bool installed_p = PyObject_IsTrue (newvalue);
+  if (installed_p == (micmd_obj->mi_command != nullptr))
+    return 0;
+
+  if (installed_p)
+    return micmdpy_install_command (micmd_obj);
+  else
+    return micmdpy_uninstall_command (micmd_obj);
+}
+
+/* The gdb.MICommand properties.   */
+
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { "installed", micmdpy_get_installed, micmdpy_set_installed,
+    "Is this command installed for use.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+/* The gdb.MICommand descriptor.  */
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (nullptr, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
+
+void _initialize_py_micmd ();
+void
+_initialize_py_micmd ()
+{
+  add_setshow_boolean_cmd
+    ("py-micmd", class_maintenance, &pymicmd_debug,
+     _("Set Python micmd debugging."),
+     _("Show Python micmd debugging."),
+     _("When on, Python micmd debugging is enabled."),
+     nullptr,
+     show_pymicmd_debug,
+     &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5e15f62f745..d9854b4ff7c 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -562,6 +562,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 2e659ee6e14..03c0408d772 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1904,7 +1904,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..f5b33f3f76b
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,393 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+mi_gdb_test "set python print-stack full" \
+    ".*\\^done" \
+    "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+    ".*\\^done" \
+    "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+    ".*\\^done" \
+    "define -pycmd MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+    "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+    "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+    "\\^done,result={hello=\"world\",times=\"42\"}" \
+    "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+    [multi_line \
+	 "&\"TypeError: Non-string object used as key: Bad Key..\"" \
+	 "\\^error,msg=\"Error occurred in Python: Non-string object used as key: Bad Key\""] \
+    "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+    [multi_line \
+	 "&\"TypeError: Non-string object used as key: 1..\"" \
+	 "\\^error,msg=\"Error occurred in Python: Non-string object used as key: 1\""] \
+    "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+    [multi_line \
+	 "&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
+	 "\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
+    "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+    "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+    "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+    "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+    "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+    "\\^done" \
+    "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+    "\\^done,result=\\\[\"None\"\\\]" \
+    "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+    "\\^error,msg=\"Invalid parameter: bogus\"" \
+    "-pycmd bogus"
+
+# Check that the top-level result from 'invoke' must be a dictionary.
+foreach test_name { nd1 nd2 nd3 } {
+    mi_gdb_test "-pycmd ${test_name}" \
+	[multi_line \
+	     "&\"TypeError: Result from invoke must be a dictionary..\"" \
+	     "\\^error,msg=\"Error occurred in Python: Result from invoke must be a dictionary\""]
+}
+
+# Check for invalid strings in the result.
+foreach test_desc { {ik1 "xxx yyy"} {ik2 "xxx yyy"} {ik3 "xxx-yyy"} \
+			{ik4 "xxx\\.yyy"} {ik5 "123xxxyyy"} } {
+    lassign $test_desc name pattern
+
+    mi_gdb_test "-pycmd ${name}" \
+	[multi_line \
+	     "&\"ValueError: Invalid key in MI result: ${pattern}..\"" \
+	     "\\^error,msg=\"Error occurred in Python: Invalid key in MI result: ${pattern}\""]
+}
+
+mi_gdb_test "-pycmd empty_key" \
+    [multi_line \
+	 "&\"ValueError: Invalid empty key in MI result..\"" \
+	 "\\^error,msg=\"Error occurred in Python: Invalid empty key in MI result\""]
+
+# With this argument the command raises a gdb.GdbError with no message
+# string.  GDB considers this a bug in the user program, so prints a
+# backtrace, and a generic error message.
+mi_gdb_test "-pycmd exp" \
+    [multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
+	 "&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
+	 "&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
+	 "&\"gdb.GdbError..\"" \
+	 "\\^error,msg=\"Error occurred in Python\\.\""] \
+    "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+    ".*\\^done" \
+    "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+    "\\^error,msg=\"Invalid parameter: int\"" \
+    "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+    "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"Command redefined but we failing anyway\"" \
+    "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int - redefined from MI"
+
+mi_gdb_test "-pycmd-new int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd-new int - defined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+    ".*&\"ValueError: MI command name is empty\\...\".*\\^error,msg=\"Error while executing Python code\\.\"" \
+    "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name does not start with '-' followed by at least one letter or digit\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name contains invalid character: @\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid character in MI command name"
+
+mi_gdb_test "python cmd=pycmd1('-abc')" \
+    ".*\\^done" \
+    "create command -abc, stored in a python variable"
+
+mi_gdb_test "python print(cmd.name)" \
+    ".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
+    "print the name of the stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str"
+
+mi_gdb_test "python cmd.installed = False" \
+    ".*\\^done" \
+    "uninstall the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
+    "-abc str, but now the command is gone"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the command is back again"
+
+mi_gdb_test "python other=pycmd2('-abc')" \
+    ".*\\^done" \
+    "create another command called -abc, stored in a separate python variable"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the other stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "print the installed status of the original stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-abc str, when the other command is in place"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the original mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the original command is back again"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "the other command is now not installed"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "the original command is now installed"
+
+mi_gdb_test "python aa = pycmd3('-aa', 'message one', 'xxx')" \
+    ".*\\^done" \
+    "created a new -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message one\"}" \
+    "call the -aa command"
+
+mi_gdb_test "python aa.__init__('-aa', 'message two', 'yyy')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "call the -aa command, get the new message"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "check the aa object has not changed after failed initialization"
+
+mi_gdb_test "python aa.installed = False" \
+    ".*\\^done" \
+    "uninstall the -aa command"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name while uninstalled"
+
+mi_gdb_test "python aa.__init__('-aa', 'message three', 'zzz')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message while uninstalled"
+
+mi_gdb_test "python aa.installed = True" \
+    ".*\\^done" \
+    "install the -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,zzz={msg=\"message three\"}" \
+    "call the -aa command looking for message three"
+
+# Remove the gdb._mi_commands dictionary, then try to register a new
+# command.
+mi_gdb_test "python del(gdb._mi_commands)" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: module 'gdb' has no attribute '_mi_commands'..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command with no gdb._mi_commands available"
+
+# Set gdb._mi_commands to be something other than a dictionary, and
+# try to register a command.
+mi_gdb_test "python gdb._mi_commands = 'string'" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: gdb._mi_commands is not a dictionary as expected..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command when gdb._mi_commands is not a dictionary"
+
+# Restore gdb._mi_commands to a dictionary.
+mi_gdb_test "python gdb._mi_commands = {}" ".*\\^done"
+
+# Try to register a command object that is missing an invoke method.
+mi_gdb_test "python no_invoke('-no-invoke')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: -no-invoke: Python command object missing 'invoke' method\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to register command with no invoke method"
+
+# Register a command, then delete its invoke method.  What is the user thinking!!
+mi_gdb_test "python setattr(no_invoke, 'invoke', free_invoke)" ".*\\^done"
+mi_gdb_test "python cmd = no_invoke('-hello')" ".*\\^done"
+mi_gdb_test "-hello" ".*\\^done,result=\\\[\\\]" \
+    "execute no_invoke command, while it still has an invoke attribute"
+mi_gdb_test "python delattr(no_invoke, 'invoke')" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: -hello: Python command object missing 'invoke' method\\...\"" \
+	 "\\^error,msg=\"Error occurred in Python: -hello: Python command object missing 'invoke' method\\.\""] \
+    "execute command missing the invoke method"
+mi_gdb_test "python cmd.invoke = 'string'" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"TypeError: 'str' object is not callable..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'str' object is not callable\""] \
+    "execute command with invoke set to a string"
+
+# Further checking of corruption to the gdb._mi_commands dictionary.
+#
+# First, insert an object of the wrong type, then try to register an
+# MI command that will go into that same dictionary slot.
+mi_gdb_test "python gdb._mi_commands\['blah'\] = 'blah blah blah'" ".*\\^done"
+mi_gdb_test "python pycmd2('-blah')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unexpected object in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "hit unexpected object in gdb._mi_commands dictionary"
+
+# Next, create a command, uninstall it, then force the command back
+# into the dictionary.
+mi_gdb_test "python cmd = pycmd2('-foo')" ".*\\^done"
+mi_gdb_test "python cmd.installed = False" ".*\\^done"
+mi_gdb_test "python gdb._mi_commands\['foo'\] = cmd" ".*\\^done"
+mi_gdb_test "python cmd.installed = True" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: uninstalled command found in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "found uninstalled command in gdb._mi_commands dictionary"
+
+# Try to create a new MI command that uses the name of a builtin MI command.
+mi_gdb_test "python cmd = pycmd2('-data-disassemble')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unable to add command, name may already be in use..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "try to register a command that replaces -data-disassemble"
+
+
+
+mi_gdb_test "python run_exception_tests()" \
+    [multi_line \
+	 ".*" \
+	 "~\"PASS..\"" \
+	 "\\^done"]
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..a2374d3aab3
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,118 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "int":
+            return {"result": 42}
+        elif argv[0] == "str":
+            return {"result": "Hello world!"}
+        elif argv[0] == "ary":
+            return {"result": ["Hello", 42]}
+        elif argv[0] == "dct":
+            return {"result": {"hello": "world", "times": 42}}
+        elif argv[0] == "bk1":
+            return {"result": {BadKey(): "world"}}
+        elif argv[0] == "bk2":
+            return {"result": {1: "world"}}
+        elif argv[0] == "bk3":
+            return {"result": {ReallyBadKey(): "world"}}
+        elif argv[0] == "tpl":
+            return {"result": (42, "Hello")}
+        elif argv[0] == "itr":
+            return {"result": iter([1, 2, 3])}
+        elif argv[0] == "nn1":
+            return None
+        elif argv[0] == "nn2":
+            return {"result": [None]}
+        elif argv[0] == "red":
+            pycmd2("-pycmd")
+            return None
+        elif argv[0] == "nd1":
+            return [1, 2, 3]
+        elif argv[0] == "nd2":
+            return 123
+        elif argv[0] == "nd3":
+            return "abc"
+        elif argv[0] == "ik1":
+            return {"xxx yyy": 123}
+        elif argv[0] == "ik2":
+            return {"result": {"xxx yyy": 123}}
+        elif argv[0] == "ik3":
+            return {"xxx-yyy": 123}
+        elif argv[0] == "ik4":
+            return {"xxx.yyy": 123}
+        elif argv[0] == "ik5":
+            return {"123xxxyyy": 123}
+        elif argv[0] == "empty_key":
+            return {"": 123}
+        elif argv[0] == "exp":
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "str":
+            return {"result": "Ciao!"}
+        elif argv[0] == "red":
+            pycmd1("-pycmd")
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == "new":
+            pycmd1("-pycmd-new")
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+# This class creates a command that returns a string, which is passed
+# when the command is created.
+class pycmd3(gdb.MICommand):
+    def __init__(self, name, msg, top_level):
+        super(pycmd3, self).__init__(name)
+        self._msg = msg
+        self._top_level = top_level
+
+    def invoke(self, args):
+        return {self._top_level: {"msg": self._msg}}
+
+
+# A command that is missing it's invoke method.
+class no_invoke(gdb.MICommand):
+    def __init__(self, name):
+        super(no_invoke, self).__init__(name)
+
+
+def free_invoke(obj, args):
+    return {"result": args}
+
+
+# Run some test involving catching exceptions.  It's easier to write
+# these as a Python function which is then called from the exp script.
+def run_exception_tests():
+    print("PASS")


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv3] gdb/python/mi: create MI commands using python
  2022-02-09 12:25             ` [PATCHv3] " Andrew Burgess
@ 2022-02-09 14:08               ` Simon Marchi
  2022-02-10 18:26                 ` Andrew Burgess
  2022-02-24 10:37               ` [PATCHv4] " Andrew Burgess
  1 sibling, 1 reply; 41+ messages in thread
From: Simon Marchi @ 2022-02-09 14:08 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: gdb-patches, Jan Vrany

>>> -static bool
>>> +bool
>>>  insert_mi_cmd_entry (mi_command_up command)
>>>  {
>>>    gdb_assert (command != nullptr);
>>>  
>>> -  const std::string &name = command->name ();
>>> +  const std::string name (command->name ());
>>
>> Is this change needed?
> 
> No, it just felt clearer.  They both compile to the same thing as
> command->name() returns a 'char *', so you end up creating a new
> std::string in both cases.
> 
> Anyway, I reverted this as it's not important for this patch.

Ah, I see.  Well I think it's the same in the end.

I was surprised to see the change from an assignment to constructor
parameters (the parenthesis), I thought we preferred the assignment
syntax.

>>>    if (mi_cmd_table.find (name) != mi_cmd_table.end ())
>>>      return false;
>>> @@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
>>>    return true;
>>>  }
>>>  
>>> +bool
>>> +remove_mi_cmd_entry (mi_command *command)
>>> +{
>>> +  gdb_assert (command != nullptr);
>>
>> If command can't be nullptr, it could be a reference.
> 
> I wasn't sure about this change.  All the caller ever has is a
> pointer, so this would force every caller to (a) add an assert, then
> (b) call the function with the dereferenced pointer.
> 
> I'd hope the compiler would be smart enough to generate pretty much
> the same code - except for moving the assert out of the function to
> every call site - which doesn't fell like a win to me.
> 
> If we had an actual object at, even just some of, the call sites, I'd
> probably agree with you...
> 
> ... have I convinced you?  Or would you still like this changed to a
> reference?

Well, since it is documented as not accepting a nullptr argument, it
means the callers must already have a check nullptr check or assert
gating the calls to remove_mi_cmd_entry.  Otherwise, there is a bug.
You have two callers, in py-micmd.c:

 - in micmdpy_uninstall_command, there is already a gdb_assert for this
 - in micmdpy_dealloc, the call is in a `if (cmd->mi_command != nullptr)`

It's really not that important.  I have just been wondering lately why
in GDB we tend to use pointers for parameters even if they must not be
nullptr, instead of references.  Perhaps it's just out of habit.  Taking
a reference just takes the "can this parameter be nulltpr?" question out
of the equation.

Although here, actually, we could also just pass a string, since that's
all remove_mi_cmd_entry needs.

Again, not very important, but I am asking to see if we should maybe try
to adjust our habits.

>> I think that error() calls in this function should be replaced with
>> setting the appropriate Python exception type.  For example, the above
>> should raise a ValueError.
> 
> This opened a can of worms :)  The biggest change in this new
> iteration is the error handling.  Far fewer calls to error() now, in
> most cases we set a Python exception instead.

Actually, I think there are some cases that should stay calls to error
:).  See below.

> 
>>
>>> +      return -1;
>>> +    }
>>> +  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
>>> +    {
>>> +      error (_("MI command name does not start with '-'"
>>> +               " followed by at least one letter or digit."));
>>> +      return -1;
>>> +    }
>>> +  else
>>> +    {
>>> +      for (int i = 2; i < name_len; i++)
>>> +	{
>>> +	  if (!isalnum (name[i]) && name[i] != '-')
>>> +	    {
>>> +	      error (_("MI command name contains invalid character: %c."),
>>> +		     name[i]);
>>> +	      return -1;
>>> +	    }
>>> +	}
>>> +
>>> +      /* Skip over the leading dash.  For the rest of this function the
>>> +	 dash is not important.  */
>>> +      ++name;
>>> +    }
>>> +
>>> +  /* Check that there's an 'invoke' method.  */
>>> +  if (!PyObject_HasAttr (self, invoke_cst))
>>> +    error (_("-%s: Python command object missing 'invoke' method."), name);
>>> +
>>> +  /* If this object already has a name set, then this object has been
>>> +     initialized before.  We handle this case a little differently.  */
>>> +  if (cmd->mi_command_name != nullptr)
>>
>> Huh, how can this happen?
> 
> Like this
> 
>   class Foo(gdb.MICommand):
>       def __init__(self, name, msg):
>           super(Foo, self).__init__(name)
>           self.__msg = msg
>       def invoke(self, args):
>           return {'msg': self.__msg}
> 
>   cmd = Foo('-foo', 'Hello')
>   cmd.__init__('-foo', 'Goodbye')

Well, that surprised me.  I was going to ask if that was even permitted
(the language allows this, by is the convention in the Python world to
support this?).

But then I read:

  https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_init

  This function corresponds to the __init__() method of classes. Like
  __init__(), it is possible to create an instance without calling
  __init__(), and it is possible to reinitialize an instance by calling
  its __init__() method again.

I learned that today, one more thing to watch out for when implementing
Python types in C/C++.

> +/* Convert KEY_OBJ into a string that can be used as a field name in MI
> +   output.  KEY_OBJ must be a Python string object, and must only contain
> +   characters suitable for use as an MI field name.
> +
> +   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
> +   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
> +   and returned.  */
> +
> +static gdb::unique_xmalloc_ptr<char>
> +py_object_to_mi_key (PyObject *key_obj)
> +{
> +  /* The key must be a string.  */
> +  if (!PyString_Check (key_obj))
> +    {
> +      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
> +      gdb::unique_xmalloc_ptr<char> key_repr_string;
> +      if (key_repr != nullptr)
> +	key_repr_string = python_string_to_target_string (key_repr.get ());
> +
> +      if (key_repr_string != nullptr)
> +	PyErr_Format (PyExc_TypeError,
> +		      _("Non-string object used as key: %s"),
> +		      key_repr_string.get ());
> +      gdbpy_handle_exception ();

I don't think you need to set a Python exception and call
gdbpy_handle_exception here, you can just call error() directly.  In
fact, I would say that manually setting an exception and calling
gdbpy_handle_exception is an anti-pattern, since it's just an indirect
way of calling error().

> +
> +  gdb::unique_xmalloc_ptr<char> key_string
> +    = python_string_to_target_string (key_obj);
> +  if (key_string == nullptr)
> +    gdbpy_handle_exception ();
> +
> +  /* Predicate function, returns true if NAME is a valid field name for use
> +     in MI result output, otherwise, returns false.  */
> +  auto is_valid_key_name = [] (const char *name) -> bool
> +  {
> +    gdb_assert (name != nullptr);
> +
> +    if (*name == '\0' || !isalpha (*name))
> +      return false;
> +
> +    for (; *name != '\0'; ++name)
> +      if (!isalnum (*name) && *name != '_')
> +	return false;
> +
> +    return true;
> +  };
> +
> +  if (!is_valid_key_name (key_string.get ()))
> +    {
> +      if (*key_string.get () == '\0')
> +	PyErr_Format (PyExc_ValueError, _("Invalid empty key in MI result"));
> +      else
> +	PyErr_Format (PyExc_ValueError, _("Invalid key in MI result: %s"),
> +		      key_string.get ());
> +      gdbpy_handle_exception ();

Another case.

> +    }
> +
> +  return key_string;
> +}
> +
> +/* Serialize RESULT and print it in MI format to the current_uiout.
> +   FIELD_NAME is used as the name of this result field.
> +
> +   RESULT can be a dictionary, a sequence, an iterator, or an object that
> +   can be converted to a string, these are converted to the matching MI
> +   output format (dictionaries as tuples, sequences and iterators as lists,
> +   and strings as named fields).
> +
> +   If anything goes wrong while formatting the output then an error is
> +   thrown.
> +
> +   This function is the recursive inner core of serialize_mi_result, and
> +   should only be called from that function.  */
> +
> +static void
> +serialize_mi_result_1 (PyObject *result, const char *field_name)
> +{
> +  struct ui_out *uiout = current_uiout;
> +
> +  if (PyDict_Check (result))
> +    {
> +      PyObject *key, *value;
> +      Py_ssize_t pos = 0;
> +      ui_out_emit_tuple tuple_emitter (uiout, field_name);
> +      while (PyDict_Next (result, &pos, &key, &value))
> +	{
> +	  gdb::unique_xmalloc_ptr<char> key_string
> +	    (py_object_to_mi_key (key));
> +	  serialize_mi_result_1 (value, key_string.get ());
> +	}
> +    }
> +  else if (PySequence_Check (result) && !PyString_Check (result))
> +    {
> +      ui_out_emit_list list_emitter (uiout, field_name);
> +      Py_ssize_t len = PySequence_Size (result);
> +      if (len == -1)
> +	gdbpy_handle_exception ();
> +      for (Py_ssize_t i = 0; i < len; ++i)
> +	{
> +          gdbpy_ref<> item (PySequence_ITEM (result, i));
> +	  if (item == nullptr)
> +	    gdbpy_handle_exception ();
> +	  serialize_mi_result_1 (item.get (), nullptr);
> +	}
> +    }
> +  else if (PyIter_Check (result))
> +    {
> +      gdbpy_ref<> item;
> +      ui_out_emit_list list_emitter (uiout, field_name);
> +      while (true)
> +	{
> +	  item.reset (PyIter_Next (result));
> +	  if (item == nullptr)
> +	    {
> +	      if (PyErr_Occurred () != nullptr)
> +		gdbpy_handle_exception ();
> +	      break;
> +	    }
> +	  serialize_mi_result_1 (item.get (), nullptr);
> +	}
> +    }
> +  else
> +    {
> +      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
> +      if (string == nullptr)
> +	gdbpy_handle_exception ();
> +      uiout->field_string (field_name, string.get ());
> +    }
> +}
> +
> +/* Serialize RESULT and print it in MI format to the current_uiout.
> +
> +   This function handles the top-level result initially returned from the
> +   invoke method of the Python command implementation.  At the top-level
> +   the result must be a dictionary.  The values within this dictionary can
> +   be a wider range of types.  Handling the values of the top-level
> +   dictionary is done by serialize_mi_result_1, see that function for more
> +   details.
> +
> +   If anything goes wrong while parsing and printing the MI output then an
> +   error is thrown.  */
> +
> +static void
> +serialize_mi_result (PyObject *result)
> +{
> +  /* At the top-level, the result must be a dictionary.  */
> +
> +  if (!PyDict_Check (result))
> +    {
> +      PyErr_SetString (PyExc_TypeError,
> +		       _("Result from invoke must be a dictionary"));
> +      gdbpy_handle_exception ();
> +    }

Another case.

> +
> +  PyObject *key, *value;
> +  Py_ssize_t pos = 0;
> +  while (PyDict_Next (result, &pos, &key, &value))
> +    {
> +      gdb::unique_xmalloc_ptr<char> key_string
> +	(py_object_to_mi_key (key));
> +      serialize_mi_result_1 (value, key_string.get ());
> +    }
> +}
> +
> +/* Called when the MI command is invoked.  PARSE contains the parsed
> +   command line arguments from the user.  */
> +
> +void
> +mi_command_py::do_invoke (struct mi_parse *parse) const
> +{
> +  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
> +
> +  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
> +
> +  mi_parse_argv (parse->args, parse);
> +
> +  if (parse->argv == nullptr)
> +    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
> +
> +  PyObject *obj = (PyObject *) this->m_pyobj;
> +  gdb_assert (obj != nullptr);
> +
> +  gdbpy_enter enter_py (get_current_arch (), current_language);
> +
> +  /* Check this object has the invoke attribute.  */
> +  if (!micmdpy_check_invoke_attr (obj, name ()))
> +    gdbpy_handle_exception ();

I would not do this check here.  Just try to do the call, if the method
doesn't exist (for some reason), PyObject_CallMethodObjArgs will fail
with a clear error.

The micmdpy_check_invoke_attr in micmdpy_init is also not necessary, in
my opinion, but I can see how it can be a little convenience for the
user.  We just need to remember that it's not bullet-proof.  For
example, it checks that an invoke method exists, but it doesn't check
that it is callable with a single argument (well, two if you count
self).  Also, it's possible to add / remove methods to a type after the
fact.

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv3] gdb/python/mi: create MI commands using python
  2022-02-09 14:08               ` Simon Marchi
@ 2022-02-10 18:26                 ` Andrew Burgess
  2022-02-13 14:27                   ` Joel Brobecker
  0 siblings, 1 reply; 41+ messages in thread
From: Andrew Burgess @ 2022-02-10 18:26 UTC (permalink / raw)
  To: Simon Marchi; +Cc: Jan Vrany, gdb-patches

* Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> [2022-02-09 09:08:53 -0500]:

> >>> -static bool
> >>> +bool
> >>>  insert_mi_cmd_entry (mi_command_up command)
> >>>  {
> >>>    gdb_assert (command != nullptr);
> >>>  
> >>> -  const std::string &name = command->name ();
> >>> +  const std::string name (command->name ());
> >>
> >> Is this change needed?
> > 
> > No, it just felt clearer.  They both compile to the same thing as
> > command->name() returns a 'char *', so you end up creating a new
> > std::string in both cases.
> > 
> > Anyway, I reverted this as it's not important for this patch.
> 
> Ah, I see.  Well I think it's the same in the end.
> 
> I was surprised to see the change from an assignment to constructor
> parameters (the parenthesis), I thought we preferred the assignment
> syntax.

I guess the construction syntax felt clearer as it is creating a new
std::string.  Especially the assignment to a reference bugs me, as
name isn't a reference to some other std::string, it's a whole new
string in its own right.

> 
> >>>    if (mi_cmd_table.find (name) != mi_cmd_table.end ())
> >>>      return false;
> >>> @@ -127,6 +123,20 @@ insert_mi_cmd_entry (mi_command_up command)
> >>>    return true;
> >>>  }
> >>>  
> >>> +bool
> >>> +remove_mi_cmd_entry (mi_command *command)
> >>> +{
> >>> +  gdb_assert (command != nullptr);
> >>
> >> If command can't be nullptr, it could be a reference.
> > 
> > I wasn't sure about this change.  All the caller ever has is a
> > pointer, so this would force every caller to (a) add an assert, then
> > (b) call the function with the dereferenced pointer.
> > 
> > I'd hope the compiler would be smart enough to generate pretty much
> > the same code - except for moving the assert out of the function to
> > every call site - which doesn't fell like a win to me.
> > 
> > If we had an actual object at, even just some of, the call sites, I'd
> > probably agree with you...
> > 
> > ... have I convinced you?  Or would you still like this changed to a
> > reference?
> 
> Well, since it is documented as not accepting a nullptr argument, it
> means the callers must already have a check nullptr check or assert
> gating the calls to remove_mi_cmd_entry.  Otherwise, there is a bug.
> You have two callers, in py-micmd.c:
> 
>  - in micmdpy_uninstall_command, there is already a gdb_assert for this
>  - in micmdpy_dealloc, the call is in a `if (cmd->mi_command != nullptr)`
> 
> It's really not that important.  I have just been wondering lately why
> in GDB we tend to use pointers for parameters even if they must not be
> nullptr, instead of references.  Perhaps it's just out of habit.  Taking
> a reference just takes the "can this parameter be nulltpr?" question out
> of the equation.
> 
> Although here, actually, we could also just pass a string, since that's
> all remove_mi_cmd_entry needs.

I just updated this to take a 'const std::string &' and this whole
discussion is resolved.

> 
> Again, not very important, but I am asking to see if we should maybe try
> to adjust our habits.
> 
> >> I think that error() calls in this function should be replaced with
> >> setting the appropriate Python exception type.  For example, the above
> >> should raise a ValueError.
> > 
> > This opened a can of worms :)  The biggest change in this new
> > iteration is the error handling.  Far fewer calls to error() now, in
> > most cases we set a Python exception instead.
> 
> Actually, I think there are some cases that should stay calls to error
> :).  See below.
> 
> > 
> >>
> >>> +      return -1;
> >>> +    }
> >>> +  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
> >>> +    {
> >>> +      error (_("MI command name does not start with '-'"
> >>> +               " followed by at least one letter or digit."));
> >>> +      return -1;
> >>> +    }
> >>> +  else
> >>> +    {
> >>> +      for (int i = 2; i < name_len; i++)
> >>> +	{
> >>> +	  if (!isalnum (name[i]) && name[i] != '-')
> >>> +	    {
> >>> +	      error (_("MI command name contains invalid character: %c."),
> >>> +		     name[i]);
> >>> +	      return -1;
> >>> +	    }
> >>> +	}
> >>> +
> >>> +      /* Skip over the leading dash.  For the rest of this function the
> >>> +	 dash is not important.  */
> >>> +      ++name;
> >>> +    }
> >>> +
> >>> +  /* Check that there's an 'invoke' method.  */
> >>> +  if (!PyObject_HasAttr (self, invoke_cst))
> >>> +    error (_("-%s: Python command object missing 'invoke' method."), name);
> >>> +
> >>> +  /* If this object already has a name set, then this object has been
> >>> +     initialized before.  We handle this case a little differently.  */
> >>> +  if (cmd->mi_command_name != nullptr)
> >>
> >> Huh, how can this happen?
> > 
> > Like this
> > 
> >   class Foo(gdb.MICommand):
> >       def __init__(self, name, msg):
> >           super(Foo, self).__init__(name)
> >           self.__msg = msg
> >       def invoke(self, args):
> >           return {'msg': self.__msg}
> > 
> >   cmd = Foo('-foo', 'Hello')
> >   cmd.__init__('-foo', 'Goodbye')
> 
> Well, that surprised me.  I was going to ask if that was even permitted
> (the language allows this, by is the convention in the Python world to
> support this?).
> 
> But then I read:
> 
>   https://docs.python.org/3/c-api/typeobj.html#c.PyTypeObject.tp_init
> 
>   This function corresponds to the __init__() method of classes. Like
>   __init__(), it is possible to create an instance without calling
>   __init__(), and it is possible to reinitialize an instance by calling
>   its __init__() method again.
> 
> I learned that today, one more thing to watch out for when implementing
> Python types in C/C++.
> 
> > +/* Convert KEY_OBJ into a string that can be used as a field name in MI
> > +   output.  KEY_OBJ must be a Python string object, and must only contain
> > +   characters suitable for use as an MI field name.
> > +
> > +   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
> > +   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
> > +   and returned.  */
> > +
> > +static gdb::unique_xmalloc_ptr<char>
> > +py_object_to_mi_key (PyObject *key_obj)
> > +{
> > +  /* The key must be a string.  */
> > +  if (!PyString_Check (key_obj))
> > +    {
> > +      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
> > +      gdb::unique_xmalloc_ptr<char> key_repr_string;
> > +      if (key_repr != nullptr)
> > +	key_repr_string = python_string_to_target_string (key_repr.get ());
> > +
> > +      if (key_repr_string != nullptr)
> > +	PyErr_Format (PyExc_TypeError,
> > +		      _("Non-string object used as key: %s"),
> > +		      key_repr_string.get ());
> > +      gdbpy_handle_exception ();
> 
> I don't think you need to set a Python exception and call
> gdbpy_handle_exception here, you can just call error() directly.  In
> fact, I would say that manually setting an exception and calling
> gdbpy_handle_exception is an anti-pattern, since it's just an indirect
> way of calling error().

Hmm, OK.  I see your point.  However, I wasn't really happy with
switching these to just call error() either; I felt the format of the
error message from gdbpy_handle_exception was better.

What I think I miss is the "Error occurred in Python: " prefix - I
think this adds real value, it lets the user know that the mistake was
from their Python code, and should be fixed there.  Obviously, we can
just make all the error() calls more descriptive to achieve the same
result, but, I think having consistent formatting is good.

So, I propose adding a new function gdbpy_error() - this is a straight
up wrapper around error that adds the prefix mentioned above.  The
idea is that if I want to call error() due to something that came from
Python, I should actually call gdbpy_error() instead.

I've gone though all the uses of gdbpy_handle_exception (in this
patch), the only calls left are in places where a Python exception
could have been set.  In places where I just see that something is
wrong, I now call gdbpy_error().

What do you think of this?

> 
> > +
> > +  gdb::unique_xmalloc_ptr<char> key_string
> > +    = python_string_to_target_string (key_obj);
> > +  if (key_string == nullptr)
> > +    gdbpy_handle_exception ();
> > +
> > +  /* Predicate function, returns true if NAME is a valid field name for use
> > +     in MI result output, otherwise, returns false.  */
> > +  auto is_valid_key_name = [] (const char *name) -> bool
> > +  {
> > +    gdb_assert (name != nullptr);
> > +
> > +    if (*name == '\0' || !isalpha (*name))
> > +      return false;
> > +
> > +    for (; *name != '\0'; ++name)
> > +      if (!isalnum (*name) && *name != '_')
> > +	return false;
> > +
> > +    return true;
> > +  };
> > +
> > +  if (!is_valid_key_name (key_string.get ()))
> > +    {
> > +      if (*key_string.get () == '\0')
> > +	PyErr_Format (PyExc_ValueError, _("Invalid empty key in MI result"));
> > +      else
> > +	PyErr_Format (PyExc_ValueError, _("Invalid key in MI result: %s"),
> > +		      key_string.get ());
> > +      gdbpy_handle_exception ();
> 
> Another case.
> 
> > +    }
> > +
> > +  return key_string;
> > +}
> > +
> > +/* Serialize RESULT and print it in MI format to the current_uiout.
> > +   FIELD_NAME is used as the name of this result field.
> > +
> > +   RESULT can be a dictionary, a sequence, an iterator, or an object that
> > +   can be converted to a string, these are converted to the matching MI
> > +   output format (dictionaries as tuples, sequences and iterators as lists,
> > +   and strings as named fields).
> > +
> > +   If anything goes wrong while formatting the output then an error is
> > +   thrown.
> > +
> > +   This function is the recursive inner core of serialize_mi_result, and
> > +   should only be called from that function.  */
> > +
> > +static void
> > +serialize_mi_result_1 (PyObject *result, const char *field_name)
> > +{
> > +  struct ui_out *uiout = current_uiout;
> > +
> > +  if (PyDict_Check (result))
> > +    {
> > +      PyObject *key, *value;
> > +      Py_ssize_t pos = 0;
> > +      ui_out_emit_tuple tuple_emitter (uiout, field_name);
> > +      while (PyDict_Next (result, &pos, &key, &value))
> > +	{
> > +	  gdb::unique_xmalloc_ptr<char> key_string
> > +	    (py_object_to_mi_key (key));
> > +	  serialize_mi_result_1 (value, key_string.get ());
> > +	}
> > +    }
> > +  else if (PySequence_Check (result) && !PyString_Check (result))
> > +    {
> > +      ui_out_emit_list list_emitter (uiout, field_name);
> > +      Py_ssize_t len = PySequence_Size (result);
> > +      if (len == -1)
> > +	gdbpy_handle_exception ();
> > +      for (Py_ssize_t i = 0; i < len; ++i)
> > +	{
> > +          gdbpy_ref<> item (PySequence_ITEM (result, i));
> > +	  if (item == nullptr)
> > +	    gdbpy_handle_exception ();
> > +	  serialize_mi_result_1 (item.get (), nullptr);
> > +	}
> > +    }
> > +  else if (PyIter_Check (result))
> > +    {
> > +      gdbpy_ref<> item;
> > +      ui_out_emit_list list_emitter (uiout, field_name);
> > +      while (true)
> > +	{
> > +	  item.reset (PyIter_Next (result));
> > +	  if (item == nullptr)
> > +	    {
> > +	      if (PyErr_Occurred () != nullptr)
> > +		gdbpy_handle_exception ();
> > +	      break;
> > +	    }
> > +	  serialize_mi_result_1 (item.get (), nullptr);
> > +	}
> > +    }
> > +  else
> > +    {
> > +      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
> > +      if (string == nullptr)
> > +	gdbpy_handle_exception ();
> > +      uiout->field_string (field_name, string.get ());
> > +    }
> > +}
> > +
> > +/* Serialize RESULT and print it in MI format to the current_uiout.
> > +
> > +   This function handles the top-level result initially returned from the
> > +   invoke method of the Python command implementation.  At the top-level
> > +   the result must be a dictionary.  The values within this dictionary can
> > +   be a wider range of types.  Handling the values of the top-level
> > +   dictionary is done by serialize_mi_result_1, see that function for more
> > +   details.
> > +
> > +   If anything goes wrong while parsing and printing the MI output then an
> > +   error is thrown.  */
> > +
> > +static void
> > +serialize_mi_result (PyObject *result)
> > +{
> > +  /* At the top-level, the result must be a dictionary.  */
> > +
> > +  if (!PyDict_Check (result))
> > +    {
> > +      PyErr_SetString (PyExc_TypeError,
> > +		       _("Result from invoke must be a dictionary"));
> > +      gdbpy_handle_exception ();
> > +    }
> 
> Another case.
> 
> > +
> > +  PyObject *key, *value;
> > +  Py_ssize_t pos = 0;
> > +  while (PyDict_Next (result, &pos, &key, &value))
> > +    {
> > +      gdb::unique_xmalloc_ptr<char> key_string
> > +	(py_object_to_mi_key (key));
> > +      serialize_mi_result_1 (value, key_string.get ());
> > +    }
> > +}
> > +
> > +/* Called when the MI command is invoked.  PARSE contains the parsed
> > +   command line arguments from the user.  */
> > +
> > +void
> > +mi_command_py::do_invoke (struct mi_parse *parse) const
> > +{
> > +  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
> > +
> > +  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
> > +
> > +  mi_parse_argv (parse->args, parse);
> > +
> > +  if (parse->argv == nullptr)
> > +    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
> > +
> > +  PyObject *obj = (PyObject *) this->m_pyobj;
> > +  gdb_assert (obj != nullptr);
> > +
> > +  gdbpy_enter enter_py (get_current_arch (), current_language);
> > +
> > +  /* Check this object has the invoke attribute.  */
> > +  if (!micmdpy_check_invoke_attr (obj, name ()))
> > +    gdbpy_handle_exception ();
> 
> I would not do this check here.  Just try to do the call, if the method
> doesn't exist (for some reason), PyObject_CallMethodObjArgs will fail
> with a clear error.
> 
> The micmdpy_check_invoke_attr in micmdpy_init is also not necessary, in
> my opinion, but I can see how it can be a little convenience for the
> user.  We just need to remember that it's not bullet-proof.  For
> example, it checks that an invoke method exists, but it doesn't check
> that it is callable with a single argument (well, two if you count
> self).  Also, it's possible to add / remove methods to a type after the
> fact.

Indeed, I even included a test that does remove the invoke method.

I've now removed micmdpy_check_invoke_attr and all uses, and just rely
on the remaining calls into Python to spot the error.

Revised patch is below.

Thanks,
Andrew

----

commit 49d4372140b733a8081e1b3b702db8153cc125ad
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Tue Jun 23 14:45:38 2020 +0100

    gdb/python/mi: create MI commands using python
    
    This commit allows an user to create custom MI commands using Python
    similarly to what is possible for Python CLI commands.
    
    A new subclass of mi_command is defined for Python MI commands,
    mi_command_py. A new file, py-micmd.c contains the logic for Python MI
    commands.
    
    This commit is based on work linked too from this mailing list thread:
    
      https://sourceware.org/pipermail/gdb/2021-November/049774.html
    
    Which has also been previously posted to the mailing list here:
    
      https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html
    
    And was recently reposted here:
    
      https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html
    
    This patch takes some core code from the previous posted patches, but
    also has some significant differences, especially after the feedback
    given here:
    
      https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html
    
    A new MI command can be implemented in Python like this:
    
      class echo_args(gdb.MICommand):
          def invoke(self,args):
              return { 'args': args }
    
      echo_args("-echo-args")
    
    The 'args' parameter is a list containing all command line arguments
    passed to the MI command.  This list can be empty if the MI command
    was passed no arguments.
    
    When used within gdb the above command produced output like this:
    
      (gdb)
      -echo-args a b c
      ^done,args=["a","b","c"]
      (gdb)
    
    The 'invoke' method of the new command must return a dictionary.  The
    keys of this dictionary are then used as the field names in the mi
    command output (e.g. 'args' in the above).
    
    The values of the result returned by invoke can be dictionaries,
    lists, iterators, or an object that can be converted to a string.
    These are processed recursively to create the mi output.  And so, this
    is valid:
    
      class new_command(gdb.MICommand):
          def invoke(self,args):
              return { 'result_one': { 'abc': 123, 'def': 'Hello' },
                       'result_two': [ { 'a': 1, 'b': 2 },
                                       { 'c': 3, 'd': 4 } ] }
    
    Which produces output like:
    
      (gdb)
      -new-command
      ^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
      (gdb)
    
    I have required that the fields names used in mi result output must
    follow C identifier restrictions (i.e. must match the regexp
    "[a-zA-Z][a-zA-Z0-9_]*").  This restriction was never written down
    anywhere before, but seems sensible to me.  We can always loosen this
    rule later if it proves to be a problem.  Much harder to try and add a
    restriction later, if folk are already using the API, and breaking the
    restriction.
    
    What follows are some details about how this implementation differs
    from the original patch that was posted to the mailing list.
    
    In this patch, I have changed how the lifetime of the Python
    gdb.MICommand objects is managed.  In the original patch, these object
    were kept alive by an owned reference within the mi_command_py object.
    As such, the Python object would not be deleted until the
    mi_command_py object itself was deleted.
    
    This caused a problem, the mi_command_py were held in the global mi
    command table (in mi/mi-cmds.c), which, as a global, was not cleared
    until program shutdown.  By this point the Python interpreter has
    already been shutdown.  Attempting to delete the mi_command_py object
    at this point was causing GDB to try and invoke Python code after
    finalising the Python interpreter, and we would crash.
    
    To work around this problem, the original patch added code in
    python/python.c that would search the mi command table, and delete the
    mi_command_py objects before the Python environment was finalised.
    
    In contrast, in this patch, I have added a new global dictionary to
    the gdb module, gdb._mi_commands.  We already have several such global
    data stores related to pretty printers, and frame unwinders.
    
    The MICommand objects are placed into the new gdb.mi_commands
    dictionary, and it is this reference that keeps the objects alive.
    When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
    and any MICommand objects within it are deleted at this point.
    
    This change avoids having to make the mi_cmd_table global, and walk
    over it from within GDB's python related code.
    
    This patch handles command redefinition entirely within GDB's python
    code, though this does impose one small restriction which is not
    present in the original code (detailed below), I don't think this is a
    big issue.  However, the original patch relied on being able to
    finish executing the mi_command::do_invoke member function after the
    mi_command object had been deleted.  Though continuing to execute a
    member function after an object is deleted is well defined, it is
    also (IMHO) risky, its too easy for someone to later add a use of the
    object without realising that the object might sometimes, have been
    deleted.  The new patch avoids this issue.
    
    The one restriction that is added to avoid this, is that an MICommand
    object can't be reinitialised with a different command name, so:
    
      (gdb) python cmd = MyMICommand("-abc")
      (gdb) python cmd.__init__("-def")
      can't reinitialize object with a different command name
    
    This feels like a pretty weird edge case, and I'm happy to live with
    this restriction.
    
    I have also changed how the memory is managed for the command name.
    In the most recently posted patch series, the command name is moved
    into a subclass of mi_command, the python mi_command_py, which
    inherits from mi_command is then free to use a smart pointer to manage
    the memory for the name.
    
    In this patch, I leave the mi_command class unchanged, and instead
    hold the memory for the name within the Python object, as the lifetime
    of the Python object always exceeds the c++ object stored in the
    mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
    leaves the mi_command class nice and simple.
    
    Next, this patch adds some extra functionality, there's a
    MICommand.name read-only attribute containing the name of the command,
    and a read-write MICommand.installed attribute that can be used to
    install (make the command available for use) and uninstall (remove the
    command from the mi_cmd_table so it can't be used) the command.  This
    attribute will be automatically updated if a second command replaces
    an earlier command.
    
    This patch adds additional error handling, and makes more use the
    gdbpy_handle_exception function.
    
    Co-Authored-By: Jan Vrany <jan.vrany@labware.com>

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index bf19db45343..681c9cb682f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index b4a515120db..20acb34f56b 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -175,6 +175,8 @@ info win
      set styling').  When false, which is the default if the argument
      is not given, then no styling is applied to the returned string.
 
+  ** It is now possible to add GDB/MI commands implemented in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index c1a3f5f2a7e..ac52ad43c2e 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -95,6 +95,7 @@
 23
 @end smallexample
 
+@anchor{set_python_print_stack}
 @kindex set python print-stack
 @item set python print-stack
 By default, @value{GDBN} will print only the message component of a
@@ -204,7 +205,8 @@
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -419,7 +421,8 @@
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2146,7 +2149,7 @@
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3850,11 +3853,12 @@
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
-@cindex commands in python
-@cindex python commands
+@cindex CLI commands in python
+@cindex commands in python, CLI
+@cindex python commands, CLI
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
 command is implemented using an instance of the @code{gdb.Command}
 class, most commonly using a subclass.
@@ -4133,6 +4137,152 @@
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python, GDB/MI
+@cindex python commands, GDB/MI
+It is possible to add @sc{GDB/MI} (@pxref{GDB/MI}) commands
+implemented in Python.  A @sc{GDB/MI} command is implemented using an
+instance of the @code{gdb.MICommand} class, most commonly using a
+subclass.
+
+@defun MICommand.__init__ (name)
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid name of a
+@sc{GDB/MI} command, and in particular must start with a hyphen
+(@code{-}).  Reusing the name of a built-in @sc{GDB/MI} is not
+allowed, and a @code{RuntimeError} will be raised.  Using the name
+of an @sc{GDB/MI} command previously defined in Python is allowed, the
+previous command will be replaced with the new command.
+@end defun
+
+@defun MICommand.invoke (arguments)
+This method is called by @value{GDBN} when the new MI command is
+invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method raises an exception, then it is turned into a
+@sc{GDB/MI} @code{^error} response.  Only @code{gdb.GdbError}
+exceptions (or its sub-classes) should be used for reporting errors to
+users, any other exception type is treated as a failure of the
+@code{invoke} method, and the exception will be printed to the error
+stream according to the @kbd{set python print-stack} setting
+(@pxref{set_python_print_stack,,@kbd{set python print-stack}}).
+
+If this method returns @code{None}, then the @sc{GDB/MI} command will
+return a @code{^done} response with no additional values.
+
+Otherwise, the return value must be a dictionary, which is converted
+to a @sc{GDB/MI} @var{RESULT-RECORD} (@pxref{GDB/MI Output Syntax}).
+The keys of this dictionary must be strings, and are used as
+@emph{VARIABLE} names in the @emph{RESULT-RECORD}, these strings must
+comply with the naming rules detailed below.  The values of this
+dictionary are recursively handled as follows:
+
+@itemize
+@item If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @emph{LIST} with elements converted recursively.
+
+@item If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @emph{TUPLE}.  Keys in that dictionary must be strings,
+which comply with the @emph{VARIABLE} naming rules detailed below.
+Values are converted recursively.
+
+@item Otherwise, value is first converted to a Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @emph{CONST}.
+@end itemize
+
+The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
+must follow the following rules; the string must be at least one
+character long, the first character must be in the set
+@code{[a-zA-Z]}, while every subsequent character must be in the set
+@code{[a-zA-Z0-9_]}.
+
+@end defun
+
+An instance of @code{MICommand} has the following attributes:
+
+@defvar MICommand.name
+A string, the name of this @sc{GDB/MI} command, as was passed to the
+@code{__init__} method.  This attribute is read-only.
+@end defvar
+
+@defvar MICommand.installed
+A boolean value indicating if this command is installed ready for a
+user to call from the command line.  Commands are automatically
+installed when they are instantiated, after which this attribute will
+be @code{True}.
+
+If later, a new command is created with the same name, then the
+original command will become uninstalled, and this attribute will be
+@code{False}.
+
+This attribute can be is read-write, setting this attribute to
+@code{False} will uninstall the command, removing it from the set of
+available commands.  Setting this attribute to @code{True} will
+install the command for use.  If there is already a Python command
+with this name installed, the currently installed command will be
+uninstalled, and this command installed in its place.
+@end defvar
+
+The following code snippet shows how a two trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo arguments passed to the command."""
+
+    def __init__(self, name, mode, toplevel = None):
+        self._mode = mode
+        super(MIEcho, self).__init__(name, toplevel)
+
+    def invoke(self, argv):
+        if self._mode == 'dict':
+            return @{ 'result': @{ 'argv' : argv @} @}
+        elif self._mode == 'list':
+            return @{ 'result': argv @}
+        else:
+            return @{ 'result': ", ".join(argv) @}
+
+
+MIEcho("-echo-dict", "dict")
+MIEcho("-echo-list", "list")
+MIEcho("-echo-string", "string", "argv")
+@end smallexample
+
+The last three lines instantiate the class three times, creating three
+new @sc{GDB/MI} commands @code{-echo-dict}, @code{-echo-list}, and
+@code{-echo-string}.  Each time a subclass of @code{gdb.MICommand} is
+instantiated, the new command is automatically registered with
+@value{GDBN}.
+
+Depending on how the Python code is read into @value{GDBN}, you may
+need to import the @code{gdb} module explicitly.
+
+The following example shows a @value{GDBN} session in which the above
+commands have been added:
+
+@smallexample
+(@value{GDBP})
+-echo-dict abc def ghi
+^done,dict=@{argv=["abc","def","ghi"]@}
+(@value{GDBP})
+-echo-list abc def ghi
+^done,list=["abc","def","ghi"]
+(@value{GDBP})
+-echo-string abc def ghi
+^done,string="abc, def, ghi"
+(@value{GDBP})
+@end smallexample
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4170,7 +4320,7 @@
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..38fbe0d8a32 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -108,12 +104,9 @@ struct mi_command_cli : public mi_command
   bool m_args_p;
 };
 
-/* Insert COMMAND into the global mi_cmd_table.  Return false if
-   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
-   not have been added to mi_cmd_table.  Otherwise, return true, and
-   COMMAND was added to mi_cmd_table.  */
+/* See mi-cmds.h.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
@@ -127,6 +120,18 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+/* See mi-cmds.h.  */
+
+bool
+remove_mi_cmd_entry (const std::string &name)
+{
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..0fc43478635 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the global mi_cmd_table.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,18 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert COMMAND into the global mi_cmd_table.  Return false if
+   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
+   not have been added to mi_cmd_table.  Otherwise, return true, and
+   COMMAND was added to mi_cmd_table.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove the command called NAME from the global mi_cmd_table.  Return
+   true if the removal was a success, otherwise return false, which
+   indicates no command called NAME was found in the mi_cmd_table. */
+
+extern bool remove_mi_cmd_entry (const std::string &name);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 9734a0d9437..0780cb4ec22 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -82,6 +82,10 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Dictionary containing all user created MI commands, the key is the
+# command name, and the value is the gdb.MICommand object.
+_mi_commands = {}
+
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..704fbfe1461
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,812 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* GDB/MI commands implemented in Python.  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "cli/cli-cmds.h"
+#include <string>
+
+/* Debugging of Python MI commands.  */
+
+static bool pymicmd_debug;
+
+/* Implementation of "show debug py-micmd".  */
+
+static void
+show_pymicmd_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Python MI command debugging is %s.\n"), value);
+}
+
+/* Print a "py-micmd" debug statement.  */
+
+#define pymicmd_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
+
+/* Print a "py-micmd" enter/exit debug statements.  */
+
+#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
+  scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
+
+struct mi_command_py;
+
+/* Representation of a Python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the MI command table.  This
+     pointer can be nullptr if the command is not currently installed into
+     the MI command table (see gdb.MICommand.installed property).  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing the name of this command, without the leading
+     dash.  This string is never nullptr once the Python object has been
+     initialised.
+
+     The memory for this string was allocated with malloc, and needs to be
+     deallocated with free when the Python object is deallocated.
+
+     When the MI_COMMAND field is not nullptr, then the mi_command_py
+     object's name will point back to this string.  */
+  char *mi_command_name;
+};
+
+/* The MI command implemented in Python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object must inherit from gdb.MICommand and must
+     implement the invoke method.  */
+
+  mi_command_py (const char *name, micmdpy_object *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  {
+    pymicmd_debug_printf ("this = %p", this);
+  }
+
+  ~mi_command_py ()
+  {
+    /* The Python object representing a MI command contains a pointer back
+       to this c++ object.  We can safely set this pointer back to nullptr
+       now, to indicate the Python object no longer references a valid c++
+       object.
+
+       However, the Python object also holds the storage for our name
+       string.  We can't clear that here as our parent's destructor might
+       still want to reference that string.  Instead we rely on the Python
+       object deallocator to free that memory, and reset the pointer.  */
+    m_pyobj->mi_command = nullptr;
+
+    pymicmd_debug_printf ("this = %p", this);
+  };
+
+  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the MI
+     command table correctly.  This function looks up the command in the MI
+     command table and checks that the object we get back references
+     CMD_OBJ.  This function is only intended for calling within a
+     gdb_assert.  This function performs many assertions internally, and
+     then always returns true.  */
+  static void validate_installation (micmdpy_object *cmd_obj);
+
+  /* Update m_pyobj to NEW_PYOBJ.  The pointer from M_PYOBJ that points
+     back to this object is swapped with the pointer in NEW_PYOBJ, which
+     must be nullptr, so that NEW_PYOBJ now points back to this object.
+     Additionally our parent's name string is stored in m_pyobj, so we
+     swap the name string with NEW_PYOBJ.
+
+     Before this call m_pyobj is the Python object representing this MI
+     command object.  After this call has completed, NEW_PYOBJ now
+     represents this MI command object.  */
+  void swap_python_object (micmdpy_object *new_pyobj)
+  {
+    gdb_assert (new_pyobj->mi_command == nullptr);
+    std::swap (new_pyobj->mi_command, m_pyobj->mi_command);
+    std::swap (new_pyobj->mi_command_name, m_pyobj->mi_command_name);
+    m_pyobj = new_pyobj;
+  }
+
+protected:
+  /* Called when the MI command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The Python object representing this MI command.  */
+  micmdpy_object *m_pyobj;
+};
+
+extern PyTypeObject micmdpy_object_type
+	CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* Holds a Python object containing the string 'invoke'.  */
+
+static PyObject *invoke_cst;
+
+/* Convert KEY_OBJ into a string that can be used as a field name in MI
+   output.  KEY_OBJ must be a Python string object, and must only contain
+   characters suitable for use as an MI field name.
+
+   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
+   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
+   and returned.  */
+
+static gdb::unique_xmalloc_ptr<char>
+py_object_to_mi_key (PyObject *key_obj)
+{
+  /* The key must be a string.  */
+  if (!PyString_Check (key_obj))
+    {
+      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
+      gdb::unique_xmalloc_ptr<char> key_repr_string;
+      if (key_repr != nullptr)
+	key_repr_string = python_string_to_target_string (key_repr.get ());
+      if (key_repr_string == nullptr)
+	gdbpy_handle_exception ();
+
+      gdbpy_error (_("non-string object used as key: %s"),
+		   key_repr_string.get ());
+    }
+
+  gdb::unique_xmalloc_ptr<char> key_string
+    = python_string_to_target_string (key_obj);
+  if (key_string == nullptr)
+    gdbpy_handle_exception ();
+
+  /* Predicate function, returns true if NAME is a valid field name for use
+     in MI result output, otherwise, returns false.  */
+  auto is_valid_key_name = [] (const char *name) -> bool
+  {
+    gdb_assert (name != nullptr);
+
+    if (*name == '\0' || !isalpha (*name))
+      return false;
+
+    for (; *name != '\0'; ++name)
+      if (!isalnum (*name) && *name != '_')
+	return false;
+
+    return true;
+  };
+
+  if (!is_valid_key_name (key_string.get ()))
+    {
+      if (*key_string.get () == '\0')
+	gdbpy_error (_("Invalid empty key in MI result"));
+      else
+	gdbpy_error (_("Invalid key in MI result: %s"), key_string.get ());
+    }
+
+  return key_string;
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+   FIELD_NAME is used as the name of this result field.
+
+   RESULT can be a dictionary, a sequence, an iterator, or an object that
+   can be converted to a string, these are converted to the matching MI
+   output format (dictionaries as tuples, sequences and iterators as lists,
+   and strings as named fields).
+
+   If anything goes wrong while formatting the output then an error is
+   thrown.
+
+   This function is the recursive inner core of serialize_mi_result, and
+   should only be called from that function.  */
+
+static void
+serialize_mi_result_1 (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  gdb::unique_xmalloc_ptr<char> key_string
+	    (py_object_to_mi_key (key));
+	  serialize_mi_result_1 (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      Py_ssize_t len = PySequence_Size (result);
+      if (len == -1)
+	gdbpy_handle_exception ();
+      for (Py_ssize_t i = 0; i < len; ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  if (item == nullptr)
+	    gdbpy_handle_exception ();
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (true)
+	{
+	  item.reset (PyIter_Next (result));
+	  if (item == nullptr)
+	    {
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+	      break;
+	    }
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      if (string == nullptr)
+	gdbpy_handle_exception ();
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+
+   This function handles the top-level result initially returned from the
+   invoke method of the Python command implementation.  At the top-level
+   the result must be a dictionary.  The values within this dictionary can
+   be a wider range of types.  Handling the values of the top-level
+   dictionary is done by serialize_mi_result_1, see that function for more
+   details.
+
+   If anything goes wrong while parsing and printing the MI output then an
+   error is thrown.  */
+
+static void
+serialize_mi_result (PyObject *result)
+{
+  /* At the top-level, the result must be a dictionary.  */
+
+  if (!PyDict_Check (result))
+    gdbpy_error (_("Result from invoke must be a dictionary"));
+
+  PyObject *key, *value;
+  Py_ssize_t pos = 0;
+  while (PyDict_Next (result, &pos, &key, &value))
+    {
+      gdb::unique_xmalloc_ptr<char> key_string
+	(py_object_to_mi_key (key));
+      serialize_mi_result_1 (value, key_string.get ());
+    }
+}
+
+/* Called when the MI command is invoked.  PARSE contains the parsed
+   command line arguments from the user.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == nullptr)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = (PyObject *) this->m_pyobj;
+  gdb_assert (obj != nullptr);
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  /* Place all the arguments into a list which we pass as a single argument
+     to the MI command's invoke method.  */
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    gdbpy_handle_exception ();
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i],
+					 strlen (parse->argv[i]),
+					 host_charset (), nullptr));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
+	gdbpy_handle_exception ();
+    }
+
+  gdb_assert (PyErr_Occurred () == nullptr);
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, invoke_cst,
+						  argobj.get (), nullptr));
+  if (result == nullptr)
+    gdbpy_handle_exception ();
+
+  if (result != Py_None)
+    serialize_mi_result (result.get ());
+}
+
+/* See declaration above.  */
+
+void
+mi_command_py::validate_installation (micmdpy_object *cmd_obj)
+{
+  gdb_assert (cmd_obj != nullptr);
+  mi_command_py *cmd = cmd_obj->mi_command;
+  gdb_assert (cmd != nullptr);
+  const char *name = cmd_obj->mi_command_name;
+  gdb_assert (name != nullptr);
+  gdb_assert (name == cmd->name ());
+  mi_command *mi_cmd = mi_cmd_lookup (name);
+  gdb_assert (mi_cmd == cmd);
+  gdb_assert (cmd->m_pyobj == cmd_obj);
+}
+
+/* Return a reference to the gdb._mi_commands dictionary.  If the
+   dictionary can't be found for any reason then nullptr is returned, and
+   a Python exception will be set.  */
+
+static gdbpy_ref<>
+micmdpy_global_command_dictionary ()
+{
+  if (gdb_python_module == nullptr)
+    {
+      PyErr_SetString (PyExc_RuntimeError, _("unable to find gdb module"));
+      return nullptr;
+    }
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "_mi_commands"));
+  if (mi_cmd_dict == nullptr)
+    return nullptr;
+
+  if (!PyDict_Check (mi_cmd_dict.get ()))
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("gdb._mi_commands is not a dictionary as expected"));
+      return nullptr;
+    }
+
+  return mi_cmd_dict;
+}
+
+/* Uninstall OBJ, making the MI command represented by OBJ unavailable for
+   use by the user.  On success 0 is returned, otherwise -1 is returned
+   and a Python exception will be set.  */
+
+static int
+micmdpy_uninstall_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command != nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  /* Remove the command from the internal MI table of commands, this will
+     cause the c++ object to be deleted, which will clear the mi_command
+     member variable within the Python object.  */
+  remove_mi_cmd_entry (obj->mi_command->name ());
+  gdb_assert (obj->mi_command == nullptr);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Grab the name for this command.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+  if (name_obj == nullptr)
+    return -1;
+
+  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
+     commands, this is gdb._mi_command, and remove it.  */
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+
+  /* Did we encounter an error?  Failing to find the object in the
+     dictionary isn't an error, that's fine.  */
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+
+  /* Did we find this command in the gdb._mi_commands dictionary?  If so,
+     then remove it.  */
+  if (curr != nullptr)
+    {
+      /* Yes we did!  Remove it.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  return 0;
+}
+
+/* Install OBJ as a usable MI command.  Return 0 on success, and -1 on
+   error, in which case, a Python error will have been set.
+
+   After successful completion the command name associated with OBJ will
+   be installed in the MI command table (so it can be found if the user
+   enters that command name), additionally, OBJ will have been added to
+   the gdb._mi_commands dictionary (using the command name as its key),
+   this will ensure that OBJ remains live even if the user gives up all
+   references.  */
+
+static int
+micmdpy_install_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command == nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Look up this command name in the gdb._mi_commands dictionary, a
+     command with this name may already exist.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb._mi_commands
+	 dictionary.  First, validate that the object in the dictionary is
+	 of the expected type, just in case something weird has happened.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* To get to this function OBJ should not be installed, which should
+	 mean OBJ is not in the gdb._mi_commands dictionary.  If we find
+	 that OBJ is the thing in the dictionary, then something weird is
+	 going on, we should throw an error.  */
+      micmdpy_object *other = (micmdpy_object *) curr;
+      if (other == obj || other->mi_command == nullptr)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("uninstalled command found in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* All Python mi command object should always have a name set.  */
+      gdb_assert (other->mi_command_name != nullptr);
+
+      /* We always insert commands into the gdb._mi_commands dictionary
+	 using their name as a key, if this check fails then the dictionary
+	 is in some weird state.  */
+      if (other->mi_command_name != other->mi_command->name ()
+	  || strcmp (other->mi_command_name, obj->mi_command_name) != 0)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("gdb._mi_commands dictionary is corrupted"));
+	  return -1;
+	}
+
+      /* Switch the state of the c++ object held in the MI command table
+	 so that it now references OBJ.  After this action the old Python
+	 object that used to be referenced from the MI command table will
+	 now show as uninstalled, while the new Python object will show as
+	 installed.  */
+      other->mi_command->swap_python_object (obj);
+
+      gdb_assert (other->mi_command == nullptr);
+      gdb_assert (obj->mi_command != nullptr);
+      gdb_assert (obj->mi_command->name () == obj->mi_command_name);
+
+      /* Remove the previous Python object from the gdb._mi_commands
+	 dictionary, we'll install the new object below.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+       return -1;
+    }
+  else
+    {
+      /* There's no Python object for this command name in the
+	 gdb._mi_commands dictionary from which we can steal an existing
+	 object already held in the MI commands table, and so, we now
+	 create a new c++ object, and install it into the MI table.  */
+      obj->mi_command = new mi_command_py (obj->mi_command_name, obj);
+      mi_command_up micommand (obj->mi_command);
+
+      /* Add the command to the gdb internal MI command table.  */
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+      if (!result)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+    }
+
+  /* Finally, add the Python object to the gdb._mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), (PyObject *) obj) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.__init__.  The init method takes the name of
+   the MI command as the first argument, which must be a string, starting
+   with a single dash.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  static const char *keywords[] = { "name", nullptr };
+  const char *name;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
+					&name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      PyErr_SetString (PyExc_ValueError, _("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      PyErr_SetString (PyExc_ValueError,
+		       _("MI command name does not start with '-'"
+			 " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    {
+      for (int i = 2; i < name_len; i++)
+	{
+	  if (!isalnum (name[i]) && name[i] != '-')
+	    {
+	      PyErr_Format
+		(PyExc_ValueError,
+		 _("MI command name contains invalid character: %c."),
+		 name[i]);
+	      return -1;
+	    }
+	}
+
+      /* Skip over the leading dash.  For the rest of this function the
+	 dash is not important.  */
+      ++name;
+    }
+
+  /* If this object already has a name set, then this object has been
+     initialized before.  We handle this case a little differently.  */
+  if (cmd->mi_command_name != nullptr)
+    {
+      /* First, we don't allow the user to change the MI command name.
+	 Supporting this would be tricky as we would need to delete the
+	 mi_command_py from the MI command table, however, the user might
+	 be trying to perform this reinitialization from within the very
+	 command we're about to delete... it all gets very messy.
+
+	 So, for now at least, we don't allow this.  This doesn't seem like
+	 an excessive restriction.  */
+      if (strcmp (cmd->mi_command_name, name) != 0)
+	{
+	  PyErr_SetString
+	    (PyExc_ValueError,
+	     _("can't reinitialize object with a different command name"));
+	  return -1;
+	}
+
+      /* If there's already an object registered with the MI command table,
+	 then we're done.  That object must be a mi_command_py, which
+	 should reference back to this micmdpy_object.  */
+      if (cmd->mi_command != nullptr)
+	{
+	  mi_command_py::validate_installation (cmd);
+	  return 0;
+	}
+    }
+  else
+    cmd->mi_command_name = xstrdup (name);
+
+  /* Now we can install this mi_command_py in the MI command table.  */
+  return micmdpy_install_command (cmd);
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+
+  /* If the Python object failed to initialize, then the name field might
+     be nullptr.  */
+  pymicmd_debug_printf ("obj = %p, name = %s", cmd,
+			(cmd->mi_command_name == nullptr
+			 ? "(null)" : cmd->mi_command_name));
+
+  /* Remove the command from the MI command table if needed.  This will
+     cause the mi_command_py object to be deleted, which, in turn, will
+     clear the cmd->mi_command member variable, hence the assert.  */
+  if (cmd->mi_command != nullptr)
+    remove_mi_cmd_entry (cmd->mi_command->name ());
+  gdb_assert (cmd->mi_command == nullptr);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->mi_command_name);
+  cmd->mi_command_name = nullptr;
+
+  /* Finally, free the memory for this Python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Python initialization for the MI commands components.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == nullptr)
+    return -1;
+
+  return 0;
+}
+
+/* Get the gdb.MICommand.name attribute, returns a string, the name of this
+   MI command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->mi_command_name != nullptr);
+  std::string name_str = string_printf ("-%s", micmd_obj->mi_command_name);
+  return PyString_FromString (name_str.c_str ());
+}
+
+/* Get the gdb.MICommand.installed property.  Returns true if this MI
+   command is installed into the MI command table, otherwise returns
+   false.  */
+
+static PyObject *
+micmdpy_get_installed (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  if (micmd_obj->mi_command == nullptr)
+    Py_RETURN_FALSE;
+  Py_RETURN_TRUE;
+}
+
+/* Set the gdb.MICommand.installed property.  The property can be set to
+   either true or false.  Setting the property to true will cause the
+   command to be installed into the MI command table (if it isn't
+   already), while setting this property to false will cause the command
+   to be removed from the MI command table (if it is present).  */
+
+static int
+micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  bool installed_p = PyObject_IsTrue (newvalue);
+  if (installed_p == (micmd_obj->mi_command != nullptr))
+    return 0;
+
+  if (installed_p)
+    return micmdpy_install_command (micmd_obj);
+  else
+    return micmdpy_uninstall_command (micmd_obj);
+}
+
+/* The gdb.MICommand properties.   */
+
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { "installed", micmdpy_get_installed, micmdpy_set_installed,
+    "Is this command installed for use.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+/* The gdb.MICommand descriptor.  */
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (nullptr, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
+
+void _initialize_py_micmd ();
+void
+_initialize_py_micmd ()
+{
+  add_setshow_boolean_cmd
+    ("py-micmd", class_maintenance, &pymicmd_debug,
+     _("Set Python micmd debugging."),
+     _("Show Python micmd debugging."),
+     _("When on, Python micmd debugging is enabled."),
+     nullptr,
+     show_pymicmd_debug,
+     &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 73c860bcc96..838853c896c 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -382,6 +382,23 @@ gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
   return result;
 }
 
+/* See python-internal.h.  */
+
+void
+gdbpy_error (const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  std::string str = string_vprintf (fmt, ap);
+  va_end (ap);
+
+  const char *msg = str.c_str ();
+  if (msg != nullptr && *msg != '\0')
+    error (_("Error occurred in Python: %s"), msg);
+  else
+    error (_("Error occurred in Python."));
+}
+
 /* Handle a Python exception when the special gdb.GdbError treatment
    is desired.  This should only be called when an exception is set.
    If the exception is a gdb.GdbError, throw a gdb exception with the
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5e15f62f745..083c4dbdbc3 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -562,6 +562,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
@@ -730,6 +732,17 @@ void gdbpy_print_stack (void);
 void gdbpy_print_stack_or_quit ();
 void gdbpy_handle_exception () ATTRIBUTE_NORETURN;
 
+/* A wrapper around calling 'error'.  Prefixes the error message with an
+   'Error occurred in Python' string.  Use this in C++ code if we spot
+   something wrong with an object returned from Python code.  The prefix
+   string gives the user a hint that the mistake is within Python code,
+   rather than some other part of GDB.
+
+   This always calls error, and never returns.  */
+
+void gdbpy_error (const char *fmt, ...)
+  ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
+
 gdbpy_ref<> python_string_to_unicode (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
 gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 2e659ee6e14..03c0408d772 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1904,7 +1904,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..077cea5e221
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,386 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+mi_gdb_test "set python print-stack full" \
+    ".*\\^done" \
+    "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+    ".*\\^done" \
+    "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+    ".*\\^done" \
+    "define -pycmd MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+    "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+    "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+    "\\^done,result={hello=\"world\",times=\"42\"}" \
+    "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+    "\\^error,msg=\"Error occurred in Python: non-string object used as key: Bad Key\"" \
+    "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+    "\\^error,msg=\"Error occurred in Python: non-string object used as key: 1\"" \
+    "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+    [multi_line \
+	 "&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
+	 "\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
+    "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+    "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+    "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+    "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+    "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+    "\\^done" \
+    "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+    "\\^done,result=\\\[\"None\"\\\]" \
+    "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+    "\\^error,msg=\"Invalid parameter: bogus\"" \
+    "-pycmd bogus"
+
+# Check that the top-level result from 'invoke' must be a dictionary.
+foreach test_name { nd1 nd2 nd3 } {
+    mi_gdb_test "-pycmd ${test_name}" \
+	"\\^error,msg=\"Error occurred in Python: Result from invoke must be a dictionary\""
+}
+
+# Check for invalid strings in the result.
+foreach test_desc { {ik1 "xxx yyy"} {ik2 "xxx yyy"} {ik3 "xxx-yyy"} \
+			{ik4 "xxx\\.yyy"} {ik5 "123xxxyyy"} } {
+    lassign $test_desc name pattern
+
+    mi_gdb_test "-pycmd ${name}" \
+	"\\^error,msg=\"Error occurred in Python: Invalid key in MI result: ${pattern}\""
+}
+
+mi_gdb_test "-pycmd empty_key" \
+    "\\^error,msg=\"Error occurred in Python: Invalid empty key in MI result\""
+
+# With this argument the command raises a gdb.GdbError with no message
+# string.  GDB considers this a bug in the user program, so prints a
+# backtrace, and a generic error message.
+mi_gdb_test "-pycmd exp" \
+    [multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
+	 "&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
+	 "&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
+	 "&\"gdb.GdbError..\"" \
+	 "\\^error,msg=\"Error occurred in Python\\.\""] \
+    "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+    ".*\\^done" \
+    "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+    "\\^error,msg=\"Invalid parameter: int\"" \
+    "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+    "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"Command redefined but we failing anyway\"" \
+    "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int - redefined from MI"
+
+mi_gdb_test "-pycmd-new int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd-new int - defined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+    ".*&\"ValueError: MI command name is empty\\...\".*\\^error,msg=\"Error while executing Python code\\.\"" \
+    "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name does not start with '-' followed by at least one letter or digit\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name contains invalid character: @\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid character in MI command name"
+
+mi_gdb_test "python cmd=pycmd1('-abc')" \
+    ".*\\^done" \
+    "create command -abc, stored in a python variable"
+
+mi_gdb_test "python print(cmd.name)" \
+    ".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
+    "print the name of the stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str"
+
+mi_gdb_test "python cmd.installed = False" \
+    ".*\\^done" \
+    "uninstall the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
+    "-abc str, but now the command is gone"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the command is back again"
+
+mi_gdb_test "python other=pycmd2('-abc')" \
+    ".*\\^done" \
+    "create another command called -abc, stored in a separate python variable"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the other stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "print the installed status of the original stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-abc str, when the other command is in place"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the original mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the original command is back again"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "the other command is now not installed"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "the original command is now installed"
+
+mi_gdb_test "python aa = pycmd3('-aa', 'message one', 'xxx')" \
+    ".*\\^done" \
+    "created a new -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message one\"}" \
+    "call the -aa command"
+
+mi_gdb_test "python aa.__init__('-aa', 'message two', 'yyy')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "call the -aa command, get the new message"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "check the aa object has not changed after failed initialization"
+
+mi_gdb_test "python aa.installed = False" \
+    ".*\\^done" \
+    "uninstall the -aa command"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name while uninstalled"
+
+mi_gdb_test "python aa.__init__('-aa', 'message three', 'zzz')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message while uninstalled"
+
+mi_gdb_test "python aa.installed = True" \
+    ".*\\^done" \
+    "install the -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,zzz={msg=\"message three\"}" \
+    "call the -aa command looking for message three"
+
+# Remove the gdb._mi_commands dictionary, then try to register a new
+# command.
+mi_gdb_test "python del(gdb._mi_commands)" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: module 'gdb' has no attribute '_mi_commands'..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command with no gdb._mi_commands available"
+
+# Set gdb._mi_commands to be something other than a dictionary, and
+# try to register a command.
+mi_gdb_test "python gdb._mi_commands = 'string'" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: gdb._mi_commands is not a dictionary as expected..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command when gdb._mi_commands is not a dictionary"
+
+# Restore gdb._mi_commands to a dictionary.
+mi_gdb_test "python gdb._mi_commands = {}" ".*\\^done"
+
+# Try to register a command object that is missing an invoke method.
+# This is accepted, but will give an error when the user tries to run
+# the command.
+mi_gdb_test "python no_invoke('-no-invoke')" ".*\\^done" \
+    "attempt to register command with no invoke method"
+mi_gdb_test "-no-invoke" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
+    "execute -no-invoke command, which is missing the invoke method"
+
+# Register a command, then delete its invoke method.  What is the user thinking!!
+mi_gdb_test "python setattr(no_invoke, 'invoke', free_invoke)" ".*\\^done"
+mi_gdb_test "python cmd = no_invoke('-hello')" ".*\\^done"
+mi_gdb_test "-hello" ".*\\^done,result=\\\[\\\]" \
+    "execute no_invoke command, while it still has an invoke attribute"
+mi_gdb_test "python delattr(no_invoke, 'invoke')" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
+    "execute -hello command, that had its invoke method removed"
+mi_gdb_test "python cmd.invoke = 'string'" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"TypeError: 'str' object is not callable..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'str' object is not callable\""] \
+    "execute command with invoke set to a string"
+
+# Further checking of corruption to the gdb._mi_commands dictionary.
+#
+# First, insert an object of the wrong type, then try to register an
+# MI command that will go into that same dictionary slot.
+mi_gdb_test "python gdb._mi_commands\['blah'\] = 'blah blah blah'" ".*\\^done"
+mi_gdb_test "python pycmd2('-blah')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unexpected object in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "hit unexpected object in gdb._mi_commands dictionary"
+
+# Next, create a command, uninstall it, then force the command back
+# into the dictionary.
+mi_gdb_test "python cmd = pycmd2('-foo')" ".*\\^done"
+mi_gdb_test "python cmd.installed = False" ".*\\^done"
+mi_gdb_test "python gdb._mi_commands\['foo'\] = cmd" ".*\\^done"
+mi_gdb_test "python cmd.installed = True" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: uninstalled command found in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "found uninstalled command in gdb._mi_commands dictionary"
+
+# Try to create a new MI command that uses the name of a builtin MI command.
+mi_gdb_test "python cmd = pycmd2('-data-disassemble')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unable to add command, name may already be in use..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "try to register a command that replaces -data-disassemble"
+
+
+
+mi_gdb_test "python run_exception_tests()" \
+    [multi_line \
+	 ".*" \
+	 "~\"PASS..\"" \
+	 "\\^done"]
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..a2374d3aab3
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,118 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "int":
+            return {"result": 42}
+        elif argv[0] == "str":
+            return {"result": "Hello world!"}
+        elif argv[0] == "ary":
+            return {"result": ["Hello", 42]}
+        elif argv[0] == "dct":
+            return {"result": {"hello": "world", "times": 42}}
+        elif argv[0] == "bk1":
+            return {"result": {BadKey(): "world"}}
+        elif argv[0] == "bk2":
+            return {"result": {1: "world"}}
+        elif argv[0] == "bk3":
+            return {"result": {ReallyBadKey(): "world"}}
+        elif argv[0] == "tpl":
+            return {"result": (42, "Hello")}
+        elif argv[0] == "itr":
+            return {"result": iter([1, 2, 3])}
+        elif argv[0] == "nn1":
+            return None
+        elif argv[0] == "nn2":
+            return {"result": [None]}
+        elif argv[0] == "red":
+            pycmd2("-pycmd")
+            return None
+        elif argv[0] == "nd1":
+            return [1, 2, 3]
+        elif argv[0] == "nd2":
+            return 123
+        elif argv[0] == "nd3":
+            return "abc"
+        elif argv[0] == "ik1":
+            return {"xxx yyy": 123}
+        elif argv[0] == "ik2":
+            return {"result": {"xxx yyy": 123}}
+        elif argv[0] == "ik3":
+            return {"xxx-yyy": 123}
+        elif argv[0] == "ik4":
+            return {"xxx.yyy": 123}
+        elif argv[0] == "ik5":
+            return {"123xxxyyy": 123}
+        elif argv[0] == "empty_key":
+            return {"": 123}
+        elif argv[0] == "exp":
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "str":
+            return {"result": "Ciao!"}
+        elif argv[0] == "red":
+            pycmd1("-pycmd")
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == "new":
+            pycmd1("-pycmd-new")
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+# This class creates a command that returns a string, which is passed
+# when the command is created.
+class pycmd3(gdb.MICommand):
+    def __init__(self, name, msg, top_level):
+        super(pycmd3, self).__init__(name)
+        self._msg = msg
+        self._top_level = top_level
+
+    def invoke(self, args):
+        return {self._top_level: {"msg": self._msg}}
+
+
+# A command that is missing it's invoke method.
+class no_invoke(gdb.MICommand):
+    def __init__(self, name):
+        super(no_invoke, self).__init__(name)
+
+
+def free_invoke(obj, args):
+    return {"result": args}
+
+
+# Run some test involving catching exceptions.  It's easier to write
+# these as a Python function which is then called from the exp script.
+def run_exception_tests():
+    print("PASS")


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv3] gdb/python/mi: create MI commands using python
  2022-02-10 18:26                 ` Andrew Burgess
@ 2022-02-13 14:27                   ` Joel Brobecker
  2022-02-13 21:46                     ` Jan Vrany
  0 siblings, 1 reply; 41+ messages in thread
From: Joel Brobecker @ 2022-02-13 14:27 UTC (permalink / raw)
  To: Andrew Burgess via Gdb-patches; +Cc: Simon Marchi, Jan Vrany, Joel Brobecker

Hi Andrew and Jan,

I'm trying to keep track of this patch series in relation to the GDB 12
release. This was requested by Andrew when I first suggested we start
working on that release.

Andrew send me the following link...

    https://sourceware.org/pipermail/gdb-patches/2022-January/185332.html

... which pointed to a 5-commits series, and now I see single-commit
v2 and v3 patches. Can you summarize for me what happened to each of
the 5 patches, and the status of each patch?

Thanks!
-- 
Joel

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv3] gdb/python/mi: create MI commands using python
  2022-02-13 14:27                   ` Joel Brobecker
@ 2022-02-13 21:46                     ` Jan Vrany
  0 siblings, 0 replies; 41+ messages in thread
From: Jan Vrany @ 2022-02-13 21:46 UTC (permalink / raw)
  To: Joel Brobecker, Andrew Burgess via Gdb-patches

Hi Joel, 

On Sun, 2022-02-13 at 18:27 +0400, Joel Brobecker wrote:
> Hi Andrew and Jan,
> 
> I'm trying to keep track of this patch series in relation to the GDB 12
> release. This was requested by Andrew when I first suggested we start
> working on that release.
> 
> Andrew send me the following link...
> 
>     https://urldefense.proofpoint.com/v2/url?u=https-3A__sourceware.org_pipermail_gdb-2Dpatches_2022-2DJanuary_185332.html&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=k-1bsYWC541K5oo2QXDgJKmS_rYbszy_0EulyjUipVk&s=MT3A9mLXoH0L507fRUP-VwrlZDmiVDnlG1NxnnMXO9o&e= 
> 
> ... which pointed to a 5-commits series, and now I see single-commit
> v2 and v3 patches. Can you summarize for me what happened to each of
> the 5 patches, and the status of each patch?
> 

"My" 5-commit series is abandoned in favour of Andrew's 1-commit version
(the v2 and v3). 

After I posted my (5-commit) version, Andrew came up with his (1-commit) 
version based on mine. It's essentially my 5 commits squashed and 
significantly modified to use different internal implementation and adding
some useful features. I believe we all agree to proceed with Andrew's as 
it is arguably nicer. AFAIK all comments on my original series have been
addressed in Andrew's v2. 

So, v3 is the latest iteration of this code and would be nice to have
in GDB 12 (once everyone's happy with it, indeed). 

Thanks!

Jan


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCHv4] gdb/python/mi: create MI commands using python
  2022-02-09 12:25             ` [PATCHv3] " Andrew Burgess
  2022-02-09 14:08               ` Simon Marchi
@ 2022-02-24 10:37               ` Andrew Burgess
  2022-02-25 19:22                 ` Tom Tromey
  2022-02-28 16:48                 ` [PATCHv5] " Andrew Burgess
  1 sibling, 2 replies; 41+ messages in thread
From: Andrew Burgess @ 2022-02-24 10:37 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess, Jan Vrany

This v4 patch is identical to the version I posted here:

   https://sourceware.org/pipermail/gdb-patches/2022-February/185821.html

except I've rebased onto current master.  I just wanted to make sure
it was clear what the latest version of this patch was.

Thanks,
Andrew

---

This commit allows an user to create custom MI commands using Python
similarly to what is possible for Python CLI commands.

A new subclass of mi_command is defined for Python MI commands,
mi_command_py. A new file, py-micmd.c contains the logic for Python MI
commands.

This commit is based on work linked too from this mailing list thread:

  https://sourceware.org/pipermail/gdb/2021-November/049774.html

Which has also been previously posted to the mailing list here:

  https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html

And was recently reposted here:

  https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html

This patch takes some core code from the previous posted patches, but
also has some significant differences, especially after the feedback
given here:

  https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html

A new MI command can be implemented in Python like this:

  class echo_args(gdb.MICommand):
      def invoke(self,args):
          return { 'args': args }

  echo_args("-echo-args")

The 'args' parameter is a list containing all command line arguments
passed to the MI command.  This list can be empty if the MI command
was passed no arguments.

When used within gdb the above command produced output like this:

  (gdb)
  -echo-args a b c
  ^done,args=["a","b","c"]
  (gdb)

The 'invoke' method of the new command must return a dictionary.  The
keys of this dictionary are then used as the field names in the mi
command output (e.g. 'args' in the above).

The values of the result returned by invoke can be dictionaries,
lists, iterators, or an object that can be converted to a string.
These are processed recursively to create the mi output.  And so, this
is valid:

  class new_command(gdb.MICommand):
      def invoke(self,args):
          return { 'result_one': { 'abc': 123, 'def': 'Hello' },
                   'result_two': [ { 'a': 1, 'b': 2 },
                                   { 'c': 3, 'd': 4 } ] }

Which produces output like:

  (gdb)
  -new-command
  ^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
  (gdb)

I have required that the fields names used in mi result output must
follow C identifier restrictions (i.e. must match the regexp
"[a-zA-Z][a-zA-Z0-9_]*").  This restriction was never written down
anywhere before, but seems sensible to me.  We can always loosen this
rule later if it proves to be a problem.  Much harder to try and add a
restriction later, if folk are already using the API, and breaking the
restriction.

What follows are some details about how this implementation differs
from the original patch that was posted to the mailing list.

In this patch, I have changed how the lifetime of the Python
gdb.MICommand objects is managed.  In the original patch, these object
were kept alive by an owned reference within the mi_command_py object.
As such, the Python object would not be deleted until the
mi_command_py object itself was deleted.

This caused a problem, the mi_command_py were held in the global mi
command table (in mi/mi-cmds.c), which, as a global, was not cleared
until program shutdown.  By this point the Python interpreter has
already been shutdown.  Attempting to delete the mi_command_py object
at this point was causing GDB to try and invoke Python code after
finalising the Python interpreter, and we would crash.

To work around this problem, the original patch added code in
python/python.c that would search the mi command table, and delete the
mi_command_py objects before the Python environment was finalised.

In contrast, in this patch, I have added a new global dictionary to
the gdb module, gdb._mi_commands.  We already have several such global
data stores related to pretty printers, and frame unwinders.

The MICommand objects are placed into the new gdb.mi_commands
dictionary, and it is this reference that keeps the objects alive.
When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
and any MICommand objects within it are deleted at this point.

This change avoids having to make the mi_cmd_table global, and walk
over it from within GDB's python related code.

This patch handles command redefinition entirely within GDB's python
code, though this does impose one small restriction which is not
present in the original code (detailed below), I don't think this is a
big issue.  However, the original patch relied on being able to
finish executing the mi_command::do_invoke member function after the
mi_command object had been deleted.  Though continuing to execute a
member function after an object is deleted is well defined, it is
also (IMHO) risky, its too easy for someone to later add a use of the
object without realising that the object might sometimes, have been
deleted.  The new patch avoids this issue.

The one restriction that is added to avoid this, is that an MICommand
object can't be reinitialised with a different command name, so:

  (gdb) python cmd = MyMICommand("-abc")
  (gdb) python cmd.__init__("-def")
  can't reinitialize object with a different command name

This feels like a pretty weird edge case, and I'm happy to live with
this restriction.

I have also changed how the memory is managed for the command name.
In the most recently posted patch series, the command name is moved
into a subclass of mi_command, the python mi_command_py, which
inherits from mi_command is then free to use a smart pointer to manage
the memory for the name.

In this patch, I leave the mi_command class unchanged, and instead
hold the memory for the name within the Python object, as the lifetime
of the Python object always exceeds the c++ object stored in the
mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
leaves the mi_command class nice and simple.

Next, this patch adds some extra functionality, there's a
MICommand.name read-only attribute containing the name of the command,
and a read-write MICommand.installed attribute that can be used to
install (make the command available for use) and uninstall (remove the
command from the mi_cmd_table so it can't be used) the command.  This
attribute will be automatically updated if a second command replaces
an earlier command.

This patch adds additional error handling, and makes more use the
gdbpy_handle_exception function.

Co-Authored-By: Jan Vrany <jan.vrany@labware.com>
---
 gdb/Makefile.in                        |   1 +
 gdb/NEWS                               |   2 +
 gdb/doc/python.texi                    | 166 ++++-
 gdb/mi/mi-cmds.c                       |  23 +-
 gdb/mi/mi-cmds.h                       |  18 +
 gdb/python/lib/gdb/__init__.py         |   4 +
 gdb/python/py-micmd.c                  | 812 +++++++++++++++++++++++++
 gdb/python/py-utils.c                  |  17 +
 gdb/python/python-internal.h           |  13 +
 gdb/python/python.c                    |   3 +-
 gdb/testsuite/gdb.python/py-mi-cmd.exp | 386 ++++++++++++
 gdb/testsuite/gdb.python/py-mi-cmd.py  | 118 ++++
 12 files changed, 1545 insertions(+), 18 deletions(-)
 create mode 100644 gdb/python/py-micmd.c
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.exp
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 70cef6e28b5..453adb7fe89 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index fdd42049994..1bc68695d7b 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -187,6 +187,8 @@ GNU/Linux/LoongArch    loongarch*-*-linux*
      set styling').  When false, which is the default if the argument
      is not given, then no styling is applied to the returned string.
 
+  ** It is now possible to add GDB/MI commands implemented in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index c1a3f5f2a7e..ac52ad43c2e 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -95,6 +95,7 @@
 23
 @end smallexample
 
+@anchor{set_python_print_stack}
 @kindex set python print-stack
 @item set python print-stack
 By default, @value{GDBN} will print only the message component of a
@@ -204,7 +205,8 @@
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -419,7 +421,8 @@
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2146,7 +2149,7 @@
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3850,11 +3853,12 @@
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
-@cindex commands in python
-@cindex python commands
+@cindex CLI commands in python
+@cindex commands in python, CLI
+@cindex python commands, CLI
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
 command is implemented using an instance of the @code{gdb.Command}
 class, most commonly using a subclass.
@@ -4133,6 +4137,152 @@
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python, GDB/MI
+@cindex python commands, GDB/MI
+It is possible to add @sc{GDB/MI} (@pxref{GDB/MI}) commands
+implemented in Python.  A @sc{GDB/MI} command is implemented using an
+instance of the @code{gdb.MICommand} class, most commonly using a
+subclass.
+
+@defun MICommand.__init__ (name)
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid name of a
+@sc{GDB/MI} command, and in particular must start with a hyphen
+(@code{-}).  Reusing the name of a built-in @sc{GDB/MI} is not
+allowed, and a @code{RuntimeError} will be raised.  Using the name
+of an @sc{GDB/MI} command previously defined in Python is allowed, the
+previous command will be replaced with the new command.
+@end defun
+
+@defun MICommand.invoke (arguments)
+This method is called by @value{GDBN} when the new MI command is
+invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method raises an exception, then it is turned into a
+@sc{GDB/MI} @code{^error} response.  Only @code{gdb.GdbError}
+exceptions (or its sub-classes) should be used for reporting errors to
+users, any other exception type is treated as a failure of the
+@code{invoke} method, and the exception will be printed to the error
+stream according to the @kbd{set python print-stack} setting
+(@pxref{set_python_print_stack,,@kbd{set python print-stack}}).
+
+If this method returns @code{None}, then the @sc{GDB/MI} command will
+return a @code{^done} response with no additional values.
+
+Otherwise, the return value must be a dictionary, which is converted
+to a @sc{GDB/MI} @var{RESULT-RECORD} (@pxref{GDB/MI Output Syntax}).
+The keys of this dictionary must be strings, and are used as
+@emph{VARIABLE} names in the @emph{RESULT-RECORD}, these strings must
+comply with the naming rules detailed below.  The values of this
+dictionary are recursively handled as follows:
+
+@itemize
+@item If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @emph{LIST} with elements converted recursively.
+
+@item If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @emph{TUPLE}.  Keys in that dictionary must be strings,
+which comply with the @emph{VARIABLE} naming rules detailed below.
+Values are converted recursively.
+
+@item Otherwise, value is first converted to a Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @emph{CONST}.
+@end itemize
+
+The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
+must follow the following rules; the string must be at least one
+character long, the first character must be in the set
+@code{[a-zA-Z]}, while every subsequent character must be in the set
+@code{[a-zA-Z0-9_]}.
+
+@end defun
+
+An instance of @code{MICommand} has the following attributes:
+
+@defvar MICommand.name
+A string, the name of this @sc{GDB/MI} command, as was passed to the
+@code{__init__} method.  This attribute is read-only.
+@end defvar
+
+@defvar MICommand.installed
+A boolean value indicating if this command is installed ready for a
+user to call from the command line.  Commands are automatically
+installed when they are instantiated, after which this attribute will
+be @code{True}.
+
+If later, a new command is created with the same name, then the
+original command will become uninstalled, and this attribute will be
+@code{False}.
+
+This attribute can be is read-write, setting this attribute to
+@code{False} will uninstall the command, removing it from the set of
+available commands.  Setting this attribute to @code{True} will
+install the command for use.  If there is already a Python command
+with this name installed, the currently installed command will be
+uninstalled, and this command installed in its place.
+@end defvar
+
+The following code snippet shows how a two trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo arguments passed to the command."""
+
+    def __init__(self, name, mode, toplevel = None):
+        self._mode = mode
+        super(MIEcho, self).__init__(name, toplevel)
+
+    def invoke(self, argv):
+        if self._mode == 'dict':
+            return @{ 'result': @{ 'argv' : argv @} @}
+        elif self._mode == 'list':
+            return @{ 'result': argv @}
+        else:
+            return @{ 'result': ", ".join(argv) @}
+
+
+MIEcho("-echo-dict", "dict")
+MIEcho("-echo-list", "list")
+MIEcho("-echo-string", "string", "argv")
+@end smallexample
+
+The last three lines instantiate the class three times, creating three
+new @sc{GDB/MI} commands @code{-echo-dict}, @code{-echo-list}, and
+@code{-echo-string}.  Each time a subclass of @code{gdb.MICommand} is
+instantiated, the new command is automatically registered with
+@value{GDBN}.
+
+Depending on how the Python code is read into @value{GDBN}, you may
+need to import the @code{gdb} module explicitly.
+
+The following example shows a @value{GDBN} session in which the above
+commands have been added:
+
+@smallexample
+(@value{GDBP})
+-echo-dict abc def ghi
+^done,dict=@{argv=["abc","def","ghi"]@}
+(@value{GDBP})
+-echo-list abc def ghi
+^done,list=["abc","def","ghi"]
+(@value{GDBP})
+-echo-string abc def ghi
+^done,string="abc, def, ghi"
+(@value{GDBP})
+@end smallexample
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4170,7 +4320,7 @@
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..38fbe0d8a32 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -108,12 +104,9 @@ struct mi_command_cli : public mi_command
   bool m_args_p;
 };
 
-/* Insert COMMAND into the global mi_cmd_table.  Return false if
-   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
-   not have been added to mi_cmd_table.  Otherwise, return true, and
-   COMMAND was added to mi_cmd_table.  */
+/* See mi-cmds.h.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
@@ -127,6 +120,18 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+/* See mi-cmds.h.  */
+
+bool
+remove_mi_cmd_entry (const std::string &name)
+{
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..0fc43478635 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the global mi_cmd_table.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,18 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert COMMAND into the global mi_cmd_table.  Return false if
+   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
+   not have been added to mi_cmd_table.  Otherwise, return true, and
+   COMMAND was added to mi_cmd_table.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove the command called NAME from the global mi_cmd_table.  Return
+   true if the removal was a success, otherwise return false, which
+   indicates no command called NAME was found in the mi_cmd_table. */
+
+extern bool remove_mi_cmd_entry (const std::string &name);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 5f63bced320..32945838775 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -82,6 +82,10 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Dictionary containing all user created MI commands, the key is the
+# command name, and the value is the gdb.MICommand object.
+_mi_commands = {}
+
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..704fbfe1461
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,812 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* GDB/MI commands implemented in Python.  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "cli/cli-cmds.h"
+#include <string>
+
+/* Debugging of Python MI commands.  */
+
+static bool pymicmd_debug;
+
+/* Implementation of "show debug py-micmd".  */
+
+static void
+show_pymicmd_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Python MI command debugging is %s.\n"), value);
+}
+
+/* Print a "py-micmd" debug statement.  */
+
+#define pymicmd_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
+
+/* Print a "py-micmd" enter/exit debug statements.  */
+
+#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
+  scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
+
+struct mi_command_py;
+
+/* Representation of a Python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the MI command table.  This
+     pointer can be nullptr if the command is not currently installed into
+     the MI command table (see gdb.MICommand.installed property).  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing the name of this command, without the leading
+     dash.  This string is never nullptr once the Python object has been
+     initialised.
+
+     The memory for this string was allocated with malloc, and needs to be
+     deallocated with free when the Python object is deallocated.
+
+     When the MI_COMMAND field is not nullptr, then the mi_command_py
+     object's name will point back to this string.  */
+  char *mi_command_name;
+};
+
+/* The MI command implemented in Python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object must inherit from gdb.MICommand and must
+     implement the invoke method.  */
+
+  mi_command_py (const char *name, micmdpy_object *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  {
+    pymicmd_debug_printf ("this = %p", this);
+  }
+
+  ~mi_command_py ()
+  {
+    /* The Python object representing a MI command contains a pointer back
+       to this c++ object.  We can safely set this pointer back to nullptr
+       now, to indicate the Python object no longer references a valid c++
+       object.
+
+       However, the Python object also holds the storage for our name
+       string.  We can't clear that here as our parent's destructor might
+       still want to reference that string.  Instead we rely on the Python
+       object deallocator to free that memory, and reset the pointer.  */
+    m_pyobj->mi_command = nullptr;
+
+    pymicmd_debug_printf ("this = %p", this);
+  };
+
+  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the MI
+     command table correctly.  This function looks up the command in the MI
+     command table and checks that the object we get back references
+     CMD_OBJ.  This function is only intended for calling within a
+     gdb_assert.  This function performs many assertions internally, and
+     then always returns true.  */
+  static void validate_installation (micmdpy_object *cmd_obj);
+
+  /* Update m_pyobj to NEW_PYOBJ.  The pointer from M_PYOBJ that points
+     back to this object is swapped with the pointer in NEW_PYOBJ, which
+     must be nullptr, so that NEW_PYOBJ now points back to this object.
+     Additionally our parent's name string is stored in m_pyobj, so we
+     swap the name string with NEW_PYOBJ.
+
+     Before this call m_pyobj is the Python object representing this MI
+     command object.  After this call has completed, NEW_PYOBJ now
+     represents this MI command object.  */
+  void swap_python_object (micmdpy_object *new_pyobj)
+  {
+    gdb_assert (new_pyobj->mi_command == nullptr);
+    std::swap (new_pyobj->mi_command, m_pyobj->mi_command);
+    std::swap (new_pyobj->mi_command_name, m_pyobj->mi_command_name);
+    m_pyobj = new_pyobj;
+  }
+
+protected:
+  /* Called when the MI command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The Python object representing this MI command.  */
+  micmdpy_object *m_pyobj;
+};
+
+extern PyTypeObject micmdpy_object_type
+	CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* Holds a Python object containing the string 'invoke'.  */
+
+static PyObject *invoke_cst;
+
+/* Convert KEY_OBJ into a string that can be used as a field name in MI
+   output.  KEY_OBJ must be a Python string object, and must only contain
+   characters suitable for use as an MI field name.
+
+   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
+   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
+   and returned.  */
+
+static gdb::unique_xmalloc_ptr<char>
+py_object_to_mi_key (PyObject *key_obj)
+{
+  /* The key must be a string.  */
+  if (!PyString_Check (key_obj))
+    {
+      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
+      gdb::unique_xmalloc_ptr<char> key_repr_string;
+      if (key_repr != nullptr)
+	key_repr_string = python_string_to_target_string (key_repr.get ());
+      if (key_repr_string == nullptr)
+	gdbpy_handle_exception ();
+
+      gdbpy_error (_("non-string object used as key: %s"),
+		   key_repr_string.get ());
+    }
+
+  gdb::unique_xmalloc_ptr<char> key_string
+    = python_string_to_target_string (key_obj);
+  if (key_string == nullptr)
+    gdbpy_handle_exception ();
+
+  /* Predicate function, returns true if NAME is a valid field name for use
+     in MI result output, otherwise, returns false.  */
+  auto is_valid_key_name = [] (const char *name) -> bool
+  {
+    gdb_assert (name != nullptr);
+
+    if (*name == '\0' || !isalpha (*name))
+      return false;
+
+    for (; *name != '\0'; ++name)
+      if (!isalnum (*name) && *name != '_')
+	return false;
+
+    return true;
+  };
+
+  if (!is_valid_key_name (key_string.get ()))
+    {
+      if (*key_string.get () == '\0')
+	gdbpy_error (_("Invalid empty key in MI result"));
+      else
+	gdbpy_error (_("Invalid key in MI result: %s"), key_string.get ());
+    }
+
+  return key_string;
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+   FIELD_NAME is used as the name of this result field.
+
+   RESULT can be a dictionary, a sequence, an iterator, or an object that
+   can be converted to a string, these are converted to the matching MI
+   output format (dictionaries as tuples, sequences and iterators as lists,
+   and strings as named fields).
+
+   If anything goes wrong while formatting the output then an error is
+   thrown.
+
+   This function is the recursive inner core of serialize_mi_result, and
+   should only be called from that function.  */
+
+static void
+serialize_mi_result_1 (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  gdb::unique_xmalloc_ptr<char> key_string
+	    (py_object_to_mi_key (key));
+	  serialize_mi_result_1 (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      Py_ssize_t len = PySequence_Size (result);
+      if (len == -1)
+	gdbpy_handle_exception ();
+      for (Py_ssize_t i = 0; i < len; ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  if (item == nullptr)
+	    gdbpy_handle_exception ();
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (true)
+	{
+	  item.reset (PyIter_Next (result));
+	  if (item == nullptr)
+	    {
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+	      break;
+	    }
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      if (string == nullptr)
+	gdbpy_handle_exception ();
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+
+   This function handles the top-level result initially returned from the
+   invoke method of the Python command implementation.  At the top-level
+   the result must be a dictionary.  The values within this dictionary can
+   be a wider range of types.  Handling the values of the top-level
+   dictionary is done by serialize_mi_result_1, see that function for more
+   details.
+
+   If anything goes wrong while parsing and printing the MI output then an
+   error is thrown.  */
+
+static void
+serialize_mi_result (PyObject *result)
+{
+  /* At the top-level, the result must be a dictionary.  */
+
+  if (!PyDict_Check (result))
+    gdbpy_error (_("Result from invoke must be a dictionary"));
+
+  PyObject *key, *value;
+  Py_ssize_t pos = 0;
+  while (PyDict_Next (result, &pos, &key, &value))
+    {
+      gdb::unique_xmalloc_ptr<char> key_string
+	(py_object_to_mi_key (key));
+      serialize_mi_result_1 (value, key_string.get ());
+    }
+}
+
+/* Called when the MI command is invoked.  PARSE contains the parsed
+   command line arguments from the user.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == nullptr)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = (PyObject *) this->m_pyobj;
+  gdb_assert (obj != nullptr);
+
+  gdbpy_enter enter_py (get_current_arch (), current_language);
+
+  /* Place all the arguments into a list which we pass as a single argument
+     to the MI command's invoke method.  */
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    gdbpy_handle_exception ();
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i],
+					 strlen (parse->argv[i]),
+					 host_charset (), nullptr));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
+	gdbpy_handle_exception ();
+    }
+
+  gdb_assert (PyErr_Occurred () == nullptr);
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, invoke_cst,
+						  argobj.get (), nullptr));
+  if (result == nullptr)
+    gdbpy_handle_exception ();
+
+  if (result != Py_None)
+    serialize_mi_result (result.get ());
+}
+
+/* See declaration above.  */
+
+void
+mi_command_py::validate_installation (micmdpy_object *cmd_obj)
+{
+  gdb_assert (cmd_obj != nullptr);
+  mi_command_py *cmd = cmd_obj->mi_command;
+  gdb_assert (cmd != nullptr);
+  const char *name = cmd_obj->mi_command_name;
+  gdb_assert (name != nullptr);
+  gdb_assert (name == cmd->name ());
+  mi_command *mi_cmd = mi_cmd_lookup (name);
+  gdb_assert (mi_cmd == cmd);
+  gdb_assert (cmd->m_pyobj == cmd_obj);
+}
+
+/* Return a reference to the gdb._mi_commands dictionary.  If the
+   dictionary can't be found for any reason then nullptr is returned, and
+   a Python exception will be set.  */
+
+static gdbpy_ref<>
+micmdpy_global_command_dictionary ()
+{
+  if (gdb_python_module == nullptr)
+    {
+      PyErr_SetString (PyExc_RuntimeError, _("unable to find gdb module"));
+      return nullptr;
+    }
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "_mi_commands"));
+  if (mi_cmd_dict == nullptr)
+    return nullptr;
+
+  if (!PyDict_Check (mi_cmd_dict.get ()))
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("gdb._mi_commands is not a dictionary as expected"));
+      return nullptr;
+    }
+
+  return mi_cmd_dict;
+}
+
+/* Uninstall OBJ, making the MI command represented by OBJ unavailable for
+   use by the user.  On success 0 is returned, otherwise -1 is returned
+   and a Python exception will be set.  */
+
+static int
+micmdpy_uninstall_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command != nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  /* Remove the command from the internal MI table of commands, this will
+     cause the c++ object to be deleted, which will clear the mi_command
+     member variable within the Python object.  */
+  remove_mi_cmd_entry (obj->mi_command->name ());
+  gdb_assert (obj->mi_command == nullptr);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Grab the name for this command.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+  if (name_obj == nullptr)
+    return -1;
+
+  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
+     commands, this is gdb._mi_command, and remove it.  */
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+
+  /* Did we encounter an error?  Failing to find the object in the
+     dictionary isn't an error, that's fine.  */
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+
+  /* Did we find this command in the gdb._mi_commands dictionary?  If so,
+     then remove it.  */
+  if (curr != nullptr)
+    {
+      /* Yes we did!  Remove it.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  return 0;
+}
+
+/* Install OBJ as a usable MI command.  Return 0 on success, and -1 on
+   error, in which case, a Python error will have been set.
+
+   After successful completion the command name associated with OBJ will
+   be installed in the MI command table (so it can be found if the user
+   enters that command name), additionally, OBJ will have been added to
+   the gdb._mi_commands dictionary (using the command name as its key),
+   this will ensure that OBJ remains live even if the user gives up all
+   references.  */
+
+static int
+micmdpy_install_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command == nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Look up this command name in the gdb._mi_commands dictionary, a
+     command with this name may already exist.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb._mi_commands
+	 dictionary.  First, validate that the object in the dictionary is
+	 of the expected type, just in case something weird has happened.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* To get to this function OBJ should not be installed, which should
+	 mean OBJ is not in the gdb._mi_commands dictionary.  If we find
+	 that OBJ is the thing in the dictionary, then something weird is
+	 going on, we should throw an error.  */
+      micmdpy_object *other = (micmdpy_object *) curr;
+      if (other == obj || other->mi_command == nullptr)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("uninstalled command found in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* All Python mi command object should always have a name set.  */
+      gdb_assert (other->mi_command_name != nullptr);
+
+      /* We always insert commands into the gdb._mi_commands dictionary
+	 using their name as a key, if this check fails then the dictionary
+	 is in some weird state.  */
+      if (other->mi_command_name != other->mi_command->name ()
+	  || strcmp (other->mi_command_name, obj->mi_command_name) != 0)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("gdb._mi_commands dictionary is corrupted"));
+	  return -1;
+	}
+
+      /* Switch the state of the c++ object held in the MI command table
+	 so that it now references OBJ.  After this action the old Python
+	 object that used to be referenced from the MI command table will
+	 now show as uninstalled, while the new Python object will show as
+	 installed.  */
+      other->mi_command->swap_python_object (obj);
+
+      gdb_assert (other->mi_command == nullptr);
+      gdb_assert (obj->mi_command != nullptr);
+      gdb_assert (obj->mi_command->name () == obj->mi_command_name);
+
+      /* Remove the previous Python object from the gdb._mi_commands
+	 dictionary, we'll install the new object below.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+       return -1;
+    }
+  else
+    {
+      /* There's no Python object for this command name in the
+	 gdb._mi_commands dictionary from which we can steal an existing
+	 object already held in the MI commands table, and so, we now
+	 create a new c++ object, and install it into the MI table.  */
+      obj->mi_command = new mi_command_py (obj->mi_command_name, obj);
+      mi_command_up micommand (obj->mi_command);
+
+      /* Add the command to the gdb internal MI command table.  */
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+      if (!result)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+    }
+
+  /* Finally, add the Python object to the gdb._mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), (PyObject *) obj) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.__init__.  The init method takes the name of
+   the MI command as the first argument, which must be a string, starting
+   with a single dash.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  static const char *keywords[] = { "name", nullptr };
+  const char *name;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
+					&name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      PyErr_SetString (PyExc_ValueError, _("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      PyErr_SetString (PyExc_ValueError,
+		       _("MI command name does not start with '-'"
+			 " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    {
+      for (int i = 2; i < name_len; i++)
+	{
+	  if (!isalnum (name[i]) && name[i] != '-')
+	    {
+	      PyErr_Format
+		(PyExc_ValueError,
+		 _("MI command name contains invalid character: %c."),
+		 name[i]);
+	      return -1;
+	    }
+	}
+
+      /* Skip over the leading dash.  For the rest of this function the
+	 dash is not important.  */
+      ++name;
+    }
+
+  /* If this object already has a name set, then this object has been
+     initialized before.  We handle this case a little differently.  */
+  if (cmd->mi_command_name != nullptr)
+    {
+      /* First, we don't allow the user to change the MI command name.
+	 Supporting this would be tricky as we would need to delete the
+	 mi_command_py from the MI command table, however, the user might
+	 be trying to perform this reinitialization from within the very
+	 command we're about to delete... it all gets very messy.
+
+	 So, for now at least, we don't allow this.  This doesn't seem like
+	 an excessive restriction.  */
+      if (strcmp (cmd->mi_command_name, name) != 0)
+	{
+	  PyErr_SetString
+	    (PyExc_ValueError,
+	     _("can't reinitialize object with a different command name"));
+	  return -1;
+	}
+
+      /* If there's already an object registered with the MI command table,
+	 then we're done.  That object must be a mi_command_py, which
+	 should reference back to this micmdpy_object.  */
+      if (cmd->mi_command != nullptr)
+	{
+	  mi_command_py::validate_installation (cmd);
+	  return 0;
+	}
+    }
+  else
+    cmd->mi_command_name = xstrdup (name);
+
+  /* Now we can install this mi_command_py in the MI command table.  */
+  return micmdpy_install_command (cmd);
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+
+  /* If the Python object failed to initialize, then the name field might
+     be nullptr.  */
+  pymicmd_debug_printf ("obj = %p, name = %s", cmd,
+			(cmd->mi_command_name == nullptr
+			 ? "(null)" : cmd->mi_command_name));
+
+  /* Remove the command from the MI command table if needed.  This will
+     cause the mi_command_py object to be deleted, which, in turn, will
+     clear the cmd->mi_command member variable, hence the assert.  */
+  if (cmd->mi_command != nullptr)
+    remove_mi_cmd_entry (cmd->mi_command->name ());
+  gdb_assert (cmd->mi_command == nullptr);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->mi_command_name);
+  cmd->mi_command_name = nullptr;
+
+  /* Finally, free the memory for this Python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Python initialization for the MI commands components.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == nullptr)
+    return -1;
+
+  return 0;
+}
+
+/* Get the gdb.MICommand.name attribute, returns a string, the name of this
+   MI command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->mi_command_name != nullptr);
+  std::string name_str = string_printf ("-%s", micmd_obj->mi_command_name);
+  return PyString_FromString (name_str.c_str ());
+}
+
+/* Get the gdb.MICommand.installed property.  Returns true if this MI
+   command is installed into the MI command table, otherwise returns
+   false.  */
+
+static PyObject *
+micmdpy_get_installed (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  if (micmd_obj->mi_command == nullptr)
+    Py_RETURN_FALSE;
+  Py_RETURN_TRUE;
+}
+
+/* Set the gdb.MICommand.installed property.  The property can be set to
+   either true or false.  Setting the property to true will cause the
+   command to be installed into the MI command table (if it isn't
+   already), while setting this property to false will cause the command
+   to be removed from the MI command table (if it is present).  */
+
+static int
+micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  bool installed_p = PyObject_IsTrue (newvalue);
+  if (installed_p == (micmd_obj->mi_command != nullptr))
+    return 0;
+
+  if (installed_p)
+    return micmdpy_install_command (micmd_obj);
+  else
+    return micmdpy_uninstall_command (micmd_obj);
+}
+
+/* The gdb.MICommand properties.   */
+
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { "installed", micmdpy_get_installed, micmdpy_set_installed,
+    "Is this command installed for use.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+/* The gdb.MICommand descriptor.  */
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (nullptr, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
+
+void _initialize_py_micmd ();
+void
+_initialize_py_micmd ()
+{
+  add_setshow_boolean_cmd
+    ("py-micmd", class_maintenance, &pymicmd_debug,
+     _("Set Python micmd debugging."),
+     _("Show Python micmd debugging."),
+     _("When on, Python micmd debugging is enabled."),
+     nullptr,
+     show_pymicmd_debug,
+     &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 73c860bcc96..838853c896c 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -382,6 +382,23 @@ gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
   return result;
 }
 
+/* See python-internal.h.  */
+
+void
+gdbpy_error (const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  std::string str = string_vprintf (fmt, ap);
+  va_end (ap);
+
+  const char *msg = str.c_str ();
+  if (msg != nullptr && *msg != '\0')
+    error (_("Error occurred in Python: %s"), msg);
+  else
+    error (_("Error occurred in Python."));
+}
+
 /* Handle a Python exception when the special gdb.GdbError treatment
    is desired.  This should only be called when an exception is set.
    If the exception is a gdb.GdbError, throw a gdb exception with the
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5e15f62f745..083c4dbdbc3 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -562,6 +562,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
@@ -730,6 +732,17 @@ void gdbpy_print_stack (void);
 void gdbpy_print_stack_or_quit ();
 void gdbpy_handle_exception () ATTRIBUTE_NORETURN;
 
+/* A wrapper around calling 'error'.  Prefixes the error message with an
+   'Error occurred in Python' string.  Use this in C++ code if we spot
+   something wrong with an object returned from Python code.  The prefix
+   string gives the user a hint that the mistake is within Python code,
+   rather than some other part of GDB.
+
+   This always calls error, and never returns.  */
+
+void gdbpy_error (const char *fmt, ...)
+  ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
+
 gdbpy_ref<> python_string_to_unicode (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
 gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 79f9826365a..97955881627 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1983,7 +1983,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..077cea5e221
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,386 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+mi_gdb_test "set python print-stack full" \
+    ".*\\^done" \
+    "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+    ".*\\^done" \
+    "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+    ".*\\^done" \
+    "define -pycmd MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+    "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+    "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+    "\\^done,result={hello=\"world\",times=\"42\"}" \
+    "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+    "\\^error,msg=\"Error occurred in Python: non-string object used as key: Bad Key\"" \
+    "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+    "\\^error,msg=\"Error occurred in Python: non-string object used as key: 1\"" \
+    "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+    [multi_line \
+	 "&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
+	 "\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
+    "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+    "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+    "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+    "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+    "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+    "\\^done" \
+    "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+    "\\^done,result=\\\[\"None\"\\\]" \
+    "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+    "\\^error,msg=\"Invalid parameter: bogus\"" \
+    "-pycmd bogus"
+
+# Check that the top-level result from 'invoke' must be a dictionary.
+foreach test_name { nd1 nd2 nd3 } {
+    mi_gdb_test "-pycmd ${test_name}" \
+	"\\^error,msg=\"Error occurred in Python: Result from invoke must be a dictionary\""
+}
+
+# Check for invalid strings in the result.
+foreach test_desc { {ik1 "xxx yyy"} {ik2 "xxx yyy"} {ik3 "xxx-yyy"} \
+			{ik4 "xxx\\.yyy"} {ik5 "123xxxyyy"} } {
+    lassign $test_desc name pattern
+
+    mi_gdb_test "-pycmd ${name}" \
+	"\\^error,msg=\"Error occurred in Python: Invalid key in MI result: ${pattern}\""
+}
+
+mi_gdb_test "-pycmd empty_key" \
+    "\\^error,msg=\"Error occurred in Python: Invalid empty key in MI result\""
+
+# With this argument the command raises a gdb.GdbError with no message
+# string.  GDB considers this a bug in the user program, so prints a
+# backtrace, and a generic error message.
+mi_gdb_test "-pycmd exp" \
+    [multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
+	 "&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
+	 "&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
+	 "&\"gdb.GdbError..\"" \
+	 "\\^error,msg=\"Error occurred in Python\\.\""] \
+    "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+    ".*\\^done" \
+    "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+    "\\^error,msg=\"Invalid parameter: int\"" \
+    "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+    "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"Command redefined but we failing anyway\"" \
+    "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int - redefined from MI"
+
+mi_gdb_test "-pycmd-new int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd-new int - defined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+    ".*&\"ValueError: MI command name is empty\\...\".*\\^error,msg=\"Error while executing Python code\\.\"" \
+    "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name does not start with '-' followed by at least one letter or digit\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name contains invalid character: @\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid character in MI command name"
+
+mi_gdb_test "python cmd=pycmd1('-abc')" \
+    ".*\\^done" \
+    "create command -abc, stored in a python variable"
+
+mi_gdb_test "python print(cmd.name)" \
+    ".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
+    "print the name of the stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str"
+
+mi_gdb_test "python cmd.installed = False" \
+    ".*\\^done" \
+    "uninstall the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
+    "-abc str, but now the command is gone"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the command is back again"
+
+mi_gdb_test "python other=pycmd2('-abc')" \
+    ".*\\^done" \
+    "create another command called -abc, stored in a separate python variable"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the other stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "print the installed status of the original stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-abc str, when the other command is in place"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the original mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the original command is back again"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "the other command is now not installed"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "the original command is now installed"
+
+mi_gdb_test "python aa = pycmd3('-aa', 'message one', 'xxx')" \
+    ".*\\^done" \
+    "created a new -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message one\"}" \
+    "call the -aa command"
+
+mi_gdb_test "python aa.__init__('-aa', 'message two', 'yyy')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "call the -aa command, get the new message"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "check the aa object has not changed after failed initialization"
+
+mi_gdb_test "python aa.installed = False" \
+    ".*\\^done" \
+    "uninstall the -aa command"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name while uninstalled"
+
+mi_gdb_test "python aa.__init__('-aa', 'message three', 'zzz')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message while uninstalled"
+
+mi_gdb_test "python aa.installed = True" \
+    ".*\\^done" \
+    "install the -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,zzz={msg=\"message three\"}" \
+    "call the -aa command looking for message three"
+
+# Remove the gdb._mi_commands dictionary, then try to register a new
+# command.
+mi_gdb_test "python del(gdb._mi_commands)" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: module 'gdb' has no attribute '_mi_commands'..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command with no gdb._mi_commands available"
+
+# Set gdb._mi_commands to be something other than a dictionary, and
+# try to register a command.
+mi_gdb_test "python gdb._mi_commands = 'string'" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: gdb._mi_commands is not a dictionary as expected..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command when gdb._mi_commands is not a dictionary"
+
+# Restore gdb._mi_commands to a dictionary.
+mi_gdb_test "python gdb._mi_commands = {}" ".*\\^done"
+
+# Try to register a command object that is missing an invoke method.
+# This is accepted, but will give an error when the user tries to run
+# the command.
+mi_gdb_test "python no_invoke('-no-invoke')" ".*\\^done" \
+    "attempt to register command with no invoke method"
+mi_gdb_test "-no-invoke" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
+    "execute -no-invoke command, which is missing the invoke method"
+
+# Register a command, then delete its invoke method.  What is the user thinking!!
+mi_gdb_test "python setattr(no_invoke, 'invoke', free_invoke)" ".*\\^done"
+mi_gdb_test "python cmd = no_invoke('-hello')" ".*\\^done"
+mi_gdb_test "-hello" ".*\\^done,result=\\\[\\\]" \
+    "execute no_invoke command, while it still has an invoke attribute"
+mi_gdb_test "python delattr(no_invoke, 'invoke')" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
+    "execute -hello command, that had its invoke method removed"
+mi_gdb_test "python cmd.invoke = 'string'" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"TypeError: 'str' object is not callable..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'str' object is not callable\""] \
+    "execute command with invoke set to a string"
+
+# Further checking of corruption to the gdb._mi_commands dictionary.
+#
+# First, insert an object of the wrong type, then try to register an
+# MI command that will go into that same dictionary slot.
+mi_gdb_test "python gdb._mi_commands\['blah'\] = 'blah blah blah'" ".*\\^done"
+mi_gdb_test "python pycmd2('-blah')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unexpected object in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "hit unexpected object in gdb._mi_commands dictionary"
+
+# Next, create a command, uninstall it, then force the command back
+# into the dictionary.
+mi_gdb_test "python cmd = pycmd2('-foo')" ".*\\^done"
+mi_gdb_test "python cmd.installed = False" ".*\\^done"
+mi_gdb_test "python gdb._mi_commands\['foo'\] = cmd" ".*\\^done"
+mi_gdb_test "python cmd.installed = True" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: uninstalled command found in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "found uninstalled command in gdb._mi_commands dictionary"
+
+# Try to create a new MI command that uses the name of a builtin MI command.
+mi_gdb_test "python cmd = pycmd2('-data-disassemble')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unable to add command, name may already be in use..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "try to register a command that replaces -data-disassemble"
+
+
+
+mi_gdb_test "python run_exception_tests()" \
+    [multi_line \
+	 ".*" \
+	 "~\"PASS..\"" \
+	 "\\^done"]
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..a2374d3aab3
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,118 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "int":
+            return {"result": 42}
+        elif argv[0] == "str":
+            return {"result": "Hello world!"}
+        elif argv[0] == "ary":
+            return {"result": ["Hello", 42]}
+        elif argv[0] == "dct":
+            return {"result": {"hello": "world", "times": 42}}
+        elif argv[0] == "bk1":
+            return {"result": {BadKey(): "world"}}
+        elif argv[0] == "bk2":
+            return {"result": {1: "world"}}
+        elif argv[0] == "bk3":
+            return {"result": {ReallyBadKey(): "world"}}
+        elif argv[0] == "tpl":
+            return {"result": (42, "Hello")}
+        elif argv[0] == "itr":
+            return {"result": iter([1, 2, 3])}
+        elif argv[0] == "nn1":
+            return None
+        elif argv[0] == "nn2":
+            return {"result": [None]}
+        elif argv[0] == "red":
+            pycmd2("-pycmd")
+            return None
+        elif argv[0] == "nd1":
+            return [1, 2, 3]
+        elif argv[0] == "nd2":
+            return 123
+        elif argv[0] == "nd3":
+            return "abc"
+        elif argv[0] == "ik1":
+            return {"xxx yyy": 123}
+        elif argv[0] == "ik2":
+            return {"result": {"xxx yyy": 123}}
+        elif argv[0] == "ik3":
+            return {"xxx-yyy": 123}
+        elif argv[0] == "ik4":
+            return {"xxx.yyy": 123}
+        elif argv[0] == "ik5":
+            return {"123xxxyyy": 123}
+        elif argv[0] == "empty_key":
+            return {"": 123}
+        elif argv[0] == "exp":
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "str":
+            return {"result": "Ciao!"}
+        elif argv[0] == "red":
+            pycmd1("-pycmd")
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == "new":
+            pycmd1("-pycmd-new")
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+# This class creates a command that returns a string, which is passed
+# when the command is created.
+class pycmd3(gdb.MICommand):
+    def __init__(self, name, msg, top_level):
+        super(pycmd3, self).__init__(name)
+        self._msg = msg
+        self._top_level = top_level
+
+    def invoke(self, args):
+        return {self._top_level: {"msg": self._msg}}
+
+
+# A command that is missing it's invoke method.
+class no_invoke(gdb.MICommand):
+    def __init__(self, name):
+        super(no_invoke, self).__init__(name)
+
+
+def free_invoke(obj, args):
+    return {"result": args}
+
+
+# Run some test involving catching exceptions.  It's easier to write
+# these as a Python function which is then called from the exp script.
+def run_exception_tests():
+    print("PASS")
-- 
2.25.4


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv4] gdb/python/mi: create MI commands using python
  2022-02-24 10:37               ` [PATCHv4] " Andrew Burgess
@ 2022-02-25 19:22                 ` Tom Tromey
  2022-02-25 19:31                   ` Jan Vrany
  2022-02-28 16:48                 ` [PATCHv5] " Andrew Burgess
  1 sibling, 1 reply; 41+ messages in thread
From: Tom Tromey @ 2022-02-25 19:22 UTC (permalink / raw)
  To: Andrew Burgess via Gdb-patches; +Cc: Andrew Burgess, Jan Vrany

>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:

Andrew> This v4 patch is identical to the version I posted here:
Andrew>    https://sourceware.org/pipermail/gdb-patches/2022-February/185821.html

Andrew> except I've rebased onto current master.  I just wanted to make sure
Andrew> it was clear what the latest version of this patch was.

Thank you both for working on this.

Andrew> When used within gdb the above command produced output like this:
Andrew>   (gdb)
Andrew>   -echo-args a b c
Andrew>   ^done,args=["a","b","c"]
Andrew>   (gdb)

Not necessary for this patch but I wonder if there should also be a way
to emit an async event from Python.

Andrew> I have required that the fields names used in mi result output must
Andrew> follow C identifier restrictions (i.e. must match the regexp
Andrew> "[a-zA-Z][a-zA-Z0-9_]*").

I think '-' is also supported, e.g.:

      uiout->field_string ("thread-id", print_thread_id (thr));

Andrew> +@defun MICommand.invoke (arguments)
Andrew> +This method is called by @value{GDBN} when the new MI command is
Andrew> +invoked.
Andrew> +
Andrew> +@var{arguments} is a list of strings.  Note, that @code{--thread}
Andrew> +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
Andrew> +they do not show up in @code{arguments}.

I guess this is done because an option may or may not take a value?
That kind of too bad since it would be nice if this could just be a
dictionary.

I suppose we could always also add an MI parsing helper, though Python
also has tons of things for this as well.

Andrew> +The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
Andrew> +must follow the following rules; the string must be at least one
Andrew> +character long, the first character must be in the set
Andrew> +@code{[a-zA-Z]}, while every subsequent character must be in the set
Andrew> +@code{[a-zA-Z0-9_]}.

This should be updated to include '-'.

Andrew> +@smallexample
Andrew> +class MIEcho(gdb.MICommand):
Andrew> +    """Echo arguments passed to the command."""
Andrew> +
Andrew> +    def __init__(self, name, mode, toplevel = None):
Andrew> +        self._mode = mode
Andrew> +        super(MIEcho, self).__init__(name, toplevel)

What is 'toplevel' here?  It isn't documented for gdb.MICommand.

Andrew> +  /* Called when the MI command is invoked.  */
Andrew> +  virtual void do_invoke(struct mi_parse *parse) const override;

Andrew> +      if (key_repr != nullptr)
Andrew> +	key_repr_string = python_string_to_target_string (key_repr.get ());
Andrew> +      if (key_repr_string == nullptr)
Andrew> +	gdbpy_handle_exception ();
Andrew> +
Andrew> +      gdbpy_error (_("non-string object used as key: %s"),
Andrew> +		   key_repr_string.get ());

Someday I'd like gdb to solve the "denaturation" problem, where if
Python code throw some exception, it's turned into a generic exception
of some kind by gdb ... which if it is then caught again by Python, has
transmuted into something else.

Anyway this seems fine, just felt like I had to get that out.

Andrew> +    for (; *name != '\0'; ++name)
Andrew> +      if (!isalnum (*name) && *name != '_')
Andrew> +	return false;

'-'

Andrew> +  else if (PyIter_Check (result))
Andrew> +    {
Andrew> +      gdbpy_ref<> item;
Andrew> +      ui_out_emit_list list_emitter (uiout, field_name);
Andrew> +      while (true)
Andrew> +	{

Not for this patch but I wonder if emitting dictionaries via ui_out
might make for nicer CLI commands sometimes.

Andrew> +  gdbpy_enter enter_py (get_current_arch (), current_language);

Probably should just drop the parameters here.

thanks,
Tom

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv4] gdb/python/mi: create MI commands using python
  2022-02-25 19:22                 ` Tom Tromey
@ 2022-02-25 19:31                   ` Jan Vrany
  0 siblings, 0 replies; 41+ messages in thread
From: Jan Vrany @ 2022-02-25 19:31 UTC (permalink / raw)
  To: Tom Tromey, Andrew Burgess via Gdb-patches; +Cc: Andrew Burgess

On Fri, 2022-02-25 at 12:22 -0700, Tom Tromey wrote:
> > > > > > "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Andrew> This v4 patch is identical to the version I posted here:
> Andrew>   
> https://urldefense.proofpoint.com/v2/url?u=https-3A__sourceware.org_pipermail_gdb-2Dpatches_2022-2DFebruary_185821.html&d=DwIBAg&c=sPZ6DeHLiehUHQWKIrsNwWp3t7snrE-az24ztT0w7Jc&r=WpFFGgYa98Yp-c29WHTCwU1wAGFBvszA6a4RzgpMSqc&m=WKDSYr2J171tvbvDjhBen4GJ7U_Ub6ZV_q_Nyl7TQb8&s=Yz53CWD-zdMB2I_L-G0Xai7hGsPlRXGOkvDDoaMoy3c&e=
> 
> Andrew> except I've rebased onto current master.  I just wanted to make sure
> Andrew> it was clear what the latest version of this patch was.
> 
> Thank you both for working on this.
> 
> Andrew> When used within gdb the above command produced output like this:
> Andrew>   (gdb)
> Andrew>   -echo-args a b c
> Andrew>   ^done,args=["a","b","c"]
> Andrew>   (gdb)
> 
> Not necessary for this patch but I wonder if there should also be a way
> to emit an async event from Python.

This is on my list already but I decided leave this for later patch. 
I have not even started working on this as to not having too many balls
in the air. 

> 
> Andrew> I have required that the fields names used in mi result output must
> Andrew> follow C identifier restrictions (i.e. must match the regexp
> Andrew> "[a-zA-Z][a-zA-Z0-9_]*").
> 
> I think '-' is also supported, e.g.:
> 
>       uiout->field_string ("thread-id", print_thread_id (thr));
> 
> Andrew> +@defun MICommand.invoke (arguments)
> Andrew> +This method is called by @value{GDBN} when the new MI command is
> Andrew> +invoked.
> Andrew> +
> Andrew> +@var{arguments} is a list of strings.  Note, that @code{--thread}
> Andrew> +and @code{--frame} arguments are handled by @value{GDBN} itself therefore
> Andrew> +they do not show up in @code{arguments}.
> 
> I guess this is done because an option may or may not take a value?
> That kind of too bad since it would be nice if this could just be a
> dictionary.
> 
> I suppose we could always also add an MI parsing helper, though Python
> also has tons of things for this as well.
> 
> Andrew> +The strings used for @emph{VARIABLE} names in the @sc{GDB/MI} output
> Andrew> +must follow the following rules; the string must be at least one
> Andrew> +character long, the first character must be in the set
> Andrew> +@code{[a-zA-Z]}, while every subsequent character must be in the set
> Andrew> +@code{[a-zA-Z0-9_]}.
> 
> This should be updated to include '-'.
> 
> Andrew> +@smallexample
> Andrew> +class MIEcho(gdb.MICommand):
> Andrew> +    """Echo arguments passed to the command."""
> Andrew> +
> Andrew> +    def __init__(self, name, mode, toplevel = None):
> Andrew> +        self._mode = mode
> Andrew> +        super(MIEcho, self).__init__(name, toplevel)
> 
> What is 'toplevel' here?  It isn't documented for gdb.MICommand.
> 
> Andrew> +  /* Called when the MI command is invoked.  */
> Andrew> +  virtual void do_invoke(struct mi_parse *parse) const override;
> 
> Andrew> +      if (key_repr != nullptr)
> Andrew> +	key_repr_string = python_string_to_target_string (key_repr.get ());
> Andrew> +      if (key_repr_string == nullptr)
> Andrew> +	gdbpy_handle_exception ();
> Andrew> +
> Andrew> +      gdbpy_error (_("non-string object used as key: %s"),
> Andrew> +		   key_repr_string.get ());
> 
> Someday I'd like gdb to solve the "denaturation" problem, where if
> Python code throw some exception, it's turned into a generic exception
> of some kind by gdb ... which if it is then caught again by Python, has
> transmuted into something else.
> 
> Anyway this seems fine, just felt like I had to get that out.
> 
> Andrew> +    for (; *name != '\0'; ++name)
> Andrew> +      if (!isalnum (*name) && *name != '_')
> Andrew> +	return false;
> 
> '-'
> 
> Andrew> +  else if (PyIter_Check (result))
> Andrew> +    {
> Andrew> +      gdbpy_ref<> item;
> Andrew> +      ui_out_emit_list list_emitter (uiout, field_name);
> Andrew> +      while (true)
> Andrew> +	{
> 
> Not for this patch but I wonder if emitting dictionaries via ui_out
> might make for nicer CLI commands sometimes.
> 
> Andrew> +  gdbpy_enter enter_py (get_current_arch (), current_language);
> 
> Probably should just drop the parameters here.
> 
> thanks,
> Tom


^ permalink raw reply	[flat|nested] 41+ messages in thread

* [PATCHv5] gdb/python/mi: create MI commands using python
  2022-02-24 10:37               ` [PATCHv4] " Andrew Burgess
  2022-02-25 19:22                 ` Tom Tromey
@ 2022-02-28 16:48                 ` Andrew Burgess
  2022-02-28 18:40                   ` Tom Tromey
                                     ` (2 more replies)
  1 sibling, 3 replies; 41+ messages in thread
From: Andrew Burgess @ 2022-02-28 16:48 UTC (permalink / raw)
  To: gdb-patches; +Cc: Andrew Burgess, Jan Vrany

In v5:

  - variable names within the mi output (from Python) can now contain
    the '-' character,

  - the example in the docs has been updated to remove the reference
    to an outdated feature (dropped in an earlier version, but the
    example got left in by mistake),

  - updated the call to gdbpy_enter to pass no arguments,

  - rebased to latest upstream master.


---

This commit allows a user to create custom MI commands using Python
similarly to what is possible for Python CLI commands.

A new subclass of mi_command is defined for Python MI commands,
mi_command_py. A new file, gdb/python/py-micmd.c contains the logic
for Python MI commands.

This commit is based on work linked too from this mailing list thread:

  https://sourceware.org/pipermail/gdb/2021-November/049774.html

Which has also been previously posted to the mailing list here:

  https://sourceware.org/pipermail/gdb-patches/2019-May/158010.html

And was recently reposted here:

  https://sourceware.org/pipermail/gdb-patches/2022-January/185190.html

The version in this patch takes some core code from the previously
posted patches, but also has some significant differences, especially
after the feedback given here:

  https://sourceware.org/pipermail/gdb-patches/2022-February/185767.html

A new MI command can be implemented in Python like this:

  class echo_args(gdb.MICommand):
      def invoke(self, args):
          return { 'args': args }

  echo_args("-echo-args")

The 'args' parameter (to the invoke method) is a list
containing (almost) all command line arguments passed to the MI
command (--thread and --frame are handled before the Python code is
called, and removed from the args list).  This list can be empty if
the MI command was passed no arguments.

When used within gdb the above command produced output like this:

  (gdb)
  -echo-args a b c
  ^done,args=["a","b","c"]
  (gdb)

The 'invoke' method of the new command must return a dictionary.  The
keys of this dictionary are then used as the field names in the mi
command output (e.g. 'args' in the above).

The values of the result returned by invoke can be dictionaries,
lists, iterators, or an object that can be converted to a string.
These are processed recursively to create the mi output.  And so, this
is valid:

  class new_command(gdb.MICommand):
      def invoke(self,args):
          return { 'result_one': { 'abc': 123, 'def': 'Hello' },
                   'result_two': [ { 'a': 1, 'b': 2 },
                                   { 'c': 3, 'd': 4 } ] }

Which produces output like:

  (gdb)
  -new-command
  ^done,result_one={abc="123",def="Hello"},result_two=[{a="1",b="2"},{c="3",d="4"}]
  (gdb)

I have required that the fields names used in mi result output must
match the regexp: "^[a-zA-Z][-_a-zA-Z0-9]*$" (without the quotes).
This restriction was never written down anywhere before, but seems
sensible to me, and we can always loosen this rule later if it proves
to be a problem.  Much harder to try and add a restriction later, once
people are already using the API.

What follows are some details about how this implementation differs
from the original patch that was posted to the mailing list.

In this patch, I have changed how the lifetime of the Python
gdb.MICommand objects is managed.  In the original patch, these object
were kept alive by an owned reference within the mi_command_py object.
As such, the Python object would not be deleted until the
mi_command_py object itself was deleted.

This caused a problem, the mi_command_py were held in the global mi
command table (in mi/mi-cmds.c), which, as a global, was not cleared
until program shutdown.  By this point the Python interpreter has
already been shutdown.  Attempting to delete the mi_command_py object
at this point was causing GDB to try and invoke Python code after
finalising the Python interpreter, and we would crash.

To work around this problem, the original patch added code in
python/python.c that would search the mi command table, and delete the
mi_command_py objects before the Python environment was finalised.

In contrast, in this patch, I have added a new global dictionary to
the gdb module, gdb._mi_commands.  We already have several such global
data stores related to pretty printers, and frame unwinders.

The MICommand objects are placed into the new gdb.mi_commands
dictionary, and it is this reference that keeps the objects alive.
When GDB's Python interpreter is shut down gdb._mi_commands is deleted,
and any MICommand objects within it are deleted at this point.

This change avoids having to make the mi_cmd_table global, and walk
over it from within GDB's python related code.

This patch handles command redefinition entirely within GDB's python
code, though this does impose one small restriction which is not
present in the original code (detailed below), I don't think this is a
big issue.  However, the original patch relied on being able to
finish executing the mi_command::do_invoke member function after the
mi_command object had been deleted.  Though continuing to execute a
member function after an object is deleted is well defined, it is
also (IMHO) risky, its too easy for someone to later add a use of the
object without realising that the object might sometimes, have been
deleted.  The new patch avoids this issue.

The one restriction that is added to avoid this, is that an MICommand
object can't be reinitialised with a different command name, so:

  (gdb) python cmd = MyMICommand("-abc")
  (gdb) python cmd.__init__("-def")
  can't reinitialize object with a different command name

This feels like a pretty weird edge case, and I'm happy to live with
this restriction.

I have also changed how the memory is managed for the command name.
In the most recently posted patch series, the command name is moved
into a subclass of mi_command, the python mi_command_py, which
inherits from mi_command is then free to use a smart pointer to manage
the memory for the name.

In this patch, I leave the mi_command class unchanged, and instead
hold the memory for the name within the Python object, as the lifetime
of the Python object always exceeds the c++ object stored in the
mi_cmd_table.  This adds a little more complexity in py-micmd.c, but
leaves the mi_command class nice and simple.

Next, this patch adds some extra functionality, there's a
MICommand.name read-only attribute containing the name of the command,
and a read-write MICommand.installed attribute that can be used to
install (make the command available for use) and uninstall (remove the
command from the mi_cmd_table so it can't be used) the command.  This
attribute will be automatically updated if a second command replaces
an earlier command.

This patch adds additional error handling, and makes more use the
gdbpy_handle_exception function.

Co-Authored-By: Jan Vrany <jan.vrany@labware.com>
---
 gdb/Makefile.in                        |   1 +
 gdb/NEWS                               |   2 +
 gdb/doc/python.texi                    | 168 ++++-
 gdb/mi/mi-cmds.c                       |  23 +-
 gdb/mi/mi-cmds.h                       |  18 +
 gdb/python/lib/gdb/__init__.py         |   4 +
 gdb/python/py-micmd.c                  | 812 +++++++++++++++++++++++++
 gdb/python/py-utils.c                  |  17 +
 gdb/python/python-internal.h           |  13 +
 gdb/python/python.c                    |   3 +-
 gdb/testsuite/gdb.python/py-mi-cmd.exp | 390 ++++++++++++
 gdb/testsuite/gdb.python/py-mi-cmd.py  | 120 ++++
 12 files changed, 1553 insertions(+), 18 deletions(-)
 create mode 100644 gdb/python/py-micmd.c
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.exp
 create mode 100644 gdb/testsuite/gdb.python/py-mi-cmd.py

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 6cbbc32466e..bd544fd70b7 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -409,6 +409,7 @@ SUBDIR_PYTHON_SRCS = \
 	python/py-lazy-string.c \
 	python/py-linetable.c \
 	python/py-membuf.c \
+	python/py-micmd.c \
 	python/py-newobjfileevent.c \
 	python/py-objfile.c \
 	python/py-param.c \
diff --git a/gdb/NEWS b/gdb/NEWS
index 41ea84e6063..500a6734ce5 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -201,6 +201,8 @@ GNU/Linux/LoongArch    loongarch*-*-linux*
      set styling').  When false, which is the default if the argument
      is not given, then no styling is applied to the returned string.
 
+  ** It is now possible to add GDB/MI commands implemented in Python.
+
 * New features in the GDB remote stub, GDBserver
 
   ** GDBserver is now supported on OpenRISC GNU/Linux.
diff --git a/gdb/doc/python.texi b/gdb/doc/python.texi
index c1a3f5f2a7e..6a5fd256608 100644
--- a/gdb/doc/python.texi
+++ b/gdb/doc/python.texi
@@ -95,6 +95,7 @@
 23
 @end smallexample
 
+@anchor{set_python_print_stack}
 @kindex set python print-stack
 @item set python print-stack
 By default, @value{GDBN} will print only the message component of a
@@ -204,7 +205,8 @@
 * Events In Python::            Listening for events from @value{GDBN}.
 * Threads In Python::           Accessing inferior threads from Python.
 * Recordings In Python::        Accessing recordings from Python.
-* Commands In Python::          Implementing new commands in Python.
+* CLI Commands In Python::      Implementing new CLI commands in Python.
+* GDB/MI Commands In Python::   Implementing new @sc{GDB/MI} commands in Python.
 * Parameters In Python::        Adding new @value{GDBN} parameters.
 * Functions In Python::         Writing new convenience functions.
 * Progspaces In Python::        Program spaces.
@@ -419,7 +421,8 @@
 @code{gdb.Value}.
 
 This function can be useful when implementing a new command
-(@pxref{Commands In Python}), as it provides a way to parse the
+(@pxref{CLI Commands In Python}, @pxref{GDB/MI Commands In Python}),
+as it provides a way to parse the
 command's argument as an expression.  It is also useful simply to
 compute values.
 @end defun
@@ -2146,7 +2149,7 @@
 frame decorator.  If no frames are being elided this function may
 return an empty iterable, or @code{None}.  Elided frames are indented
 from normal frames in a @code{CLI} backtrace, or in the case of
-@code{GDB/MI}, are placed in the @code{children} field of the eliding
+@sc{GDB/MI}, are placed in the @code{children} field of the eliding
 frame.
 
 It is the frame filter's task to also filter out the elided frames from
@@ -3850,11 +3853,12 @@
     return count
 @end smallexample
 
-@node Commands In Python
-@subsubsection Commands In Python
+@node CLI Commands In Python
+@subsubsection CLI Commands In Python
 
-@cindex commands in python
-@cindex python commands
+@cindex CLI commands in python
+@cindex commands in python, CLI
+@cindex python commands, CLI
 You can implement new @value{GDBN} CLI commands in Python.  A CLI
 command is implemented using an instance of the @code{gdb.Command}
 class, most commonly using a subclass.
@@ -4133,6 +4137,154 @@
 Python code is read into @value{GDBN}, you may need to import the
 @code{gdb} module explicitly.
 
+@node GDB/MI Commands In Python
+@subsubsection @sc{GDB/MI} Commands In Python
+
+@cindex MI commands in python
+@cindex commands in python, GDB/MI
+@cindex python commands, GDB/MI
+It is possible to add @sc{GDB/MI} (@pxref{GDB/MI}) commands
+implemented in Python.  A @sc{GDB/MI} command is implemented using an
+instance of the @code{gdb.MICommand} class, most commonly using a
+subclass.
+
+@defun MICommand.__init__ (name)
+The object initializer for @code{MICommand} registers the new command
+with @value{GDBN}.  This initializer is normally invoked from the
+subclass' own @code{__init__} method.
+
+@var{name} is the name of the command.  It must be a valid name of a
+@sc{GDB/MI} command, and in particular must start with a hyphen
+(@code{-}).  Reusing the name of a built-in @sc{GDB/MI} is not
+allowed, and a @code{RuntimeError} will be raised.  Using the name
+of an @sc{GDB/MI} command previously defined in Python is allowed, the
+previous command will be replaced with the new command.
+@end defun
+
+@defun MICommand.invoke (arguments)
+This method is called by @value{GDBN} when the new MI command is
+invoked.
+
+@var{arguments} is a list of strings.  Note, that @code{--thread}
+and @code{--frame} arguments are handled by @value{GDBN} itself therefore
+they do not show up in @code{arguments}.
+
+If this method raises an exception, then it is turned into a
+@sc{GDB/MI} @code{^error} response.  Only @code{gdb.GdbError}
+exceptions (or its sub-classes) should be used for reporting errors to
+users, any other exception type is treated as a failure of the
+@code{invoke} method, and the exception will be printed to the error
+stream according to the @kbd{set python print-stack} setting
+(@pxref{set_python_print_stack,,@kbd{set python print-stack}}).
+
+If this method returns @code{None}, then the @sc{GDB/MI} command will
+return a @code{^done} response with no additional values.
+
+Otherwise, the return value must be a dictionary, which is converted
+to a @sc{GDB/MI} @var{result-record} (@pxref{GDB/MI Output Syntax}).
+The keys of this dictionary must be strings, and are used as
+@var{variable} names in the @var{result-record}, these strings must
+comply with the naming rules detailed below.  The values of this
+dictionary are recursively handled as follows:
+
+@itemize
+@item
+If the value is Python sequence or iterator, it is converted to
+@sc{GDB/MI} @var{list} with elements converted recursively.
+
+@item
+If the value is Python dictionary, it is converted to
+@sc{GDB/MI} @var{tuple}.  Keys in that dictionary must be strings,
+which comply with the @var{variable} naming rules detailed below.
+Values are converted recursively.
+
+@item
+Otherwise, value is first converted to a Python string using
+@code{str ()} and then converted to @sc{GDB/MI} @var{const}.
+@end itemize
+
+The strings used for @var{variable} names in the @sc{GDB/MI} output
+must follow the following rules; the string must be at least one
+character long, the first character must be in the set
+@code{[a-zA-Z]}, while every subsequent character must be in the set
+@code{[-_a-zA-Z0-9]}.
+@end defun
+
+An instance of @code{MICommand} has the following attributes:
+
+@defvar MICommand.name
+A string, the name of this @sc{GDB/MI} command, as was passed to the
+@code{__init__} method.  This attribute is read-only.
+@end defvar
+
+@defvar MICommand.installed
+A boolean value indicating if this command is installed ready for a
+user to call from the command line.  Commands are automatically
+installed when they are instantiated, after which this attribute will
+be @code{True}.
+
+If later, a new command is created with the same name, then the
+original command will become uninstalled, and this attribute will be
+@code{False}.
+
+This attribute is read-write, setting this attribute to @code{False}
+will uninstall the command, removing it from the set of available
+commands.  Setting this attribute to @code{True} will install the
+command for use.  If there is already a Python command with this name
+installed, the currently installed command will be uninstalled, and
+this command installed in its place.
+@end defvar
+
+The following code snippet shows how a two trivial MI command can be
+implemented in Python:
+
+@smallexample
+class MIEcho(gdb.MICommand):
+    """Echo arguments passed to the command."""
+
+    def __init__(self, name, mode):
+        self._mode = mode
+        super(MIEcho, self).__init__(name)
+
+    def invoke(self, argv):
+        if self._mode == 'dict':
+            return @{ 'dict': @{ 'argv' : argv @} @}
+        elif self._mode == 'list':
+            return @{ 'list': argv @}
+        else:
+            return @{ 'string': ", ".join(argv) @}
+
+
+MIEcho("-echo-dict", "dict")
+MIEcho("-echo-list", "list")
+MIEcho("-echo-string", "string")
+@end smallexample
+
+The last three lines instantiate the class three times, creating three
+new @sc{GDB/MI} commands @code{-echo-dict}, @code{-echo-list}, and
+@code{-echo-string}.  Each time a subclass of @code{gdb.MICommand} is
+instantiated, the new command is automatically registered with
+@value{GDBN}.
+
+Depending on how the Python code is read into @value{GDBN}, you may
+need to import the @code{gdb} module explicitly.
+
+The following example shows a @value{GDBN} session in which the above
+commands have been added:
+
+@smallexample
+(@value{GDBP})
+-echo-dict abc def ghi
+^done,dict=@{argv=["abc","def","ghi"]@}
+(@value{GDBP})
+-echo-list abc def ghi
+^done,list=["abc","def","ghi"]
+(@value{GDBP})
+-echo-string abc def ghi
+^done,string="abc, def, ghi"
+(@value{GDBP})
+@end smallexample
+
 @node Parameters In Python
 @subsubsection Parameters In Python
 
@@ -4170,7 +4322,7 @@
 can be found, an exception is raised.
 
 @var{command-class} should be one of the @samp{COMMAND_} constants
-(@pxref{Commands In Python}).  This argument tells @value{GDBN} how to
+(@pxref{CLI Commands In Python}).  This argument tells @value{GDBN} how to
 categorize the new parameter in the help system.
 
 @var{parameter-class} should be one of the @samp{PARAM_} constants
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabdda9b..38fbe0d8a32 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -26,10 +26,6 @@
 #include <map>
 #include <string>
 
-/* A command held in the MI_CMD_TABLE.  */
-
-using mi_command_up = std::unique_ptr<struct mi_command>;
-
 /* MI command table (built at run time). */
 
 static std::map<std::string, mi_command_up> mi_cmd_table;
@@ -108,12 +104,9 @@ struct mi_command_cli : public mi_command
   bool m_args_p;
 };
 
-/* Insert COMMAND into the global mi_cmd_table.  Return false if
-   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
-   not have been added to mi_cmd_table.  Otherwise, return true, and
-   COMMAND was added to mi_cmd_table.  */
+/* See mi-cmds.h.  */
 
-static bool
+bool
 insert_mi_cmd_entry (mi_command_up command)
 {
   gdb_assert (command != nullptr);
@@ -127,6 +120,18 @@ insert_mi_cmd_entry (mi_command_up command)
   return true;
 }
 
+/* See mi-cmds.h.  */
+
+bool
+remove_mi_cmd_entry (const std::string &name)
+{
+  if (mi_cmd_table.find (name) == mi_cmd_table.end ())
+    return false;
+
+  mi_cmd_table.erase (name);
+  return true;
+}
+
 /* Create and register a new MI command with an MI specific implementation.
    NAME must name an MI command that does not already exist, otherwise an
    assertion will trigger.  */
diff --git a/gdb/mi/mi-cmds.h b/gdb/mi/mi-cmds.h
index 2a93a9f5476..0fc43478635 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -187,6 +187,10 @@ struct mi_command
   int *m_suppress_notification;
 };
 
+/* A command held in the global mi_cmd_table.  */
+
+using mi_command_up = std::unique_ptr<struct mi_command>;
+
 /* Lookup a command in the MI command table, returns nullptr if COMMAND is
    not found.  */
 
@@ -194,4 +198,18 @@ extern mi_command *mi_cmd_lookup (const char *command);
 
 extern void mi_execute_command (const char *cmd, int from_tty);
 
+/* Insert COMMAND into the global mi_cmd_table.  Return false if
+   COMMAND->name already exists in mi_cmd_table, in which case COMMAND will
+   not have been added to mi_cmd_table.  Otherwise, return true, and
+   COMMAND was added to mi_cmd_table.  */
+
+extern bool insert_mi_cmd_entry (mi_command_up command);
+
+/* Remove the command called NAME from the global mi_cmd_table.  Return
+   true if the removal was a success, otherwise return false, which
+   indicates no command called NAME was found in the mi_cmd_table. */
+
+extern bool remove_mi_cmd_entry (const std::string &name);
+
+
 #endif /* MI_MI_CMDS_H */
diff --git a/gdb/python/lib/gdb/__init__.py b/gdb/python/lib/gdb/__init__.py
index 5f63bced320..32945838775 100644
--- a/gdb/python/lib/gdb/__init__.py
+++ b/gdb/python/lib/gdb/__init__.py
@@ -82,6 +82,10 @@ frame_filters = {}
 # Initial frame unwinders.
 frame_unwinders = []
 
+# Dictionary containing all user created MI commands, the key is the
+# command name, and the value is the gdb.MICommand object.
+_mi_commands = {}
+
 
 def _execute_unwinders(pending_frame):
     """Internal function called from GDB to execute all unwinders.
diff --git a/gdb/python/py-micmd.c b/gdb/python/py-micmd.c
new file mode 100644
index 00000000000..4665fcc75c7
--- /dev/null
+++ b/gdb/python/py-micmd.c
@@ -0,0 +1,812 @@
+/* MI Command Set for GDB, the GNU debugger.
+
+   Copyright (C) 2019-2022 Free Software Foundation, Inc.
+
+   This file is part of GDB.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+/* GDB/MI commands implemented in Python.  */
+
+#include "defs.h"
+#include "python-internal.h"
+#include "arch-utils.h"
+#include "charset.h"
+#include "language.h"
+#include "mi/mi-cmds.h"
+#include "mi/mi-parse.h"
+#include "cli/cli-cmds.h"
+#include <string>
+
+/* Debugging of Python MI commands.  */
+
+static bool pymicmd_debug;
+
+/* Implementation of "show debug py-micmd".  */
+
+static void
+show_pymicmd_debug (struct ui_file *file, int from_tty,
+		    struct cmd_list_element *c, const char *value)
+{
+  fprintf_filtered (file, _("Python MI command debugging is %s.\n"), value);
+}
+
+/* Print a "py-micmd" debug statement.  */
+
+#define pymicmd_debug_printf(fmt, ...) \
+  debug_prefixed_printf_cond (pymicmd_debug, "py-micmd", fmt, ##__VA_ARGS__)
+
+/* Print a "py-micmd" enter/exit debug statements.  */
+
+#define PYMICMD_SCOPED_DEBUG_ENTER_EXIT \
+  scoped_debug_enter_exit (pymicmd_debug, "py-micmd")
+
+struct mi_command_py;
+
+/* Representation of a Python gdb.MICommand object.  */
+
+struct micmdpy_object
+{
+  PyObject_HEAD
+
+  /* The object representing this command in the MI command table.  This
+     pointer can be nullptr if the command is not currently installed into
+     the MI command table (see gdb.MICommand.installed property).  */
+  struct mi_command_py *mi_command;
+
+  /* The string representing the name of this command, without the leading
+     dash.  This string is never nullptr once the Python object has been
+     initialised.
+
+     The memory for this string was allocated with malloc, and needs to be
+     deallocated with free when the Python object is deallocated.
+
+     When the MI_COMMAND field is not nullptr, then the mi_command_py
+     object's name will point back to this string.  */
+  char *mi_command_name;
+};
+
+/* The MI command implemented in Python.  */
+
+struct mi_command_py : public mi_command
+{
+  /* Constructs a new mi_command_py object.  NAME is command name without
+     leading dash.  OBJECT is a reference to a Python object implementing
+     the command.  This object must inherit from gdb.MICommand and must
+     implement the invoke method.  */
+
+  mi_command_py (const char *name, micmdpy_object *object)
+    : mi_command (name, nullptr),
+      m_pyobj (object)
+  {
+    pymicmd_debug_printf ("this = %p", this);
+  }
+
+  ~mi_command_py ()
+  {
+    /* The Python object representing a MI command contains a pointer back
+       to this c++ object.  We can safely set this pointer back to nullptr
+       now, to indicate the Python object no longer references a valid c++
+       object.
+
+       However, the Python object also holds the storage for our name
+       string.  We can't clear that here as our parent's destructor might
+       still want to reference that string.  Instead we rely on the Python
+       object deallocator to free that memory, and reset the pointer.  */
+    m_pyobj->mi_command = nullptr;
+
+    pymicmd_debug_printf ("this = %p", this);
+  };
+
+  /* Validate that CMD_OBJ, a non-nullptr pointer, is installed into the MI
+     command table correctly.  This function looks up the command in the MI
+     command table and checks that the object we get back references
+     CMD_OBJ.  This function is only intended for calling within a
+     gdb_assert.  This function performs many assertions internally, and
+     then always returns true.  */
+  static void validate_installation (micmdpy_object *cmd_obj);
+
+  /* Update m_pyobj to NEW_PYOBJ.  The pointer from M_PYOBJ that points
+     back to this object is swapped with the pointer in NEW_PYOBJ, which
+     must be nullptr, so that NEW_PYOBJ now points back to this object.
+     Additionally our parent's name string is stored in m_pyobj, so we
+     swap the name string with NEW_PYOBJ.
+
+     Before this call m_pyobj is the Python object representing this MI
+     command object.  After this call has completed, NEW_PYOBJ now
+     represents this MI command object.  */
+  void swap_python_object (micmdpy_object *new_pyobj)
+  {
+    gdb_assert (new_pyobj->mi_command == nullptr);
+    std::swap (new_pyobj->mi_command, m_pyobj->mi_command);
+    std::swap (new_pyobj->mi_command_name, m_pyobj->mi_command_name);
+    m_pyobj = new_pyobj;
+  }
+
+protected:
+  /* Called when the MI command is invoked.  */
+  virtual void do_invoke(struct mi_parse *parse) const override;
+
+private:
+  /* The Python object representing this MI command.  */
+  micmdpy_object *m_pyobj;
+};
+
+extern PyTypeObject micmdpy_object_type
+	CPYCHECKER_TYPE_OBJECT_FOR_TYPEDEF ("micmdpy_object");
+
+/* Holds a Python object containing the string 'invoke'.  */
+
+static PyObject *invoke_cst;
+
+/* Convert KEY_OBJ into a string that can be used as a field name in MI
+   output.  KEY_OBJ must be a Python string object, and must only contain
+   characters suitable for use as an MI field name.
+
+   If KEY_OBJ is not a string, or if KEY_OBJ contains invalid characters,
+   then an error is thrown.  Otherwise, KEY_OBJ is converted to a string
+   and returned.  */
+
+static gdb::unique_xmalloc_ptr<char>
+py_object_to_mi_key (PyObject *key_obj)
+{
+  /* The key must be a string.  */
+  if (!PyString_Check (key_obj))
+    {
+      gdbpy_ref<> key_repr (PyObject_Repr (key_obj));
+      gdb::unique_xmalloc_ptr<char> key_repr_string;
+      if (key_repr != nullptr)
+	key_repr_string = python_string_to_target_string (key_repr.get ());
+      if (key_repr_string == nullptr)
+	gdbpy_handle_exception ();
+
+      gdbpy_error (_("non-string object used as key: %s"),
+		   key_repr_string.get ());
+    }
+
+  gdb::unique_xmalloc_ptr<char> key_string
+    = python_string_to_target_string (key_obj);
+  if (key_string == nullptr)
+    gdbpy_handle_exception ();
+
+  /* Predicate function, returns true if NAME is a valid field name for use
+     in MI result output, otherwise, returns false.  */
+  auto is_valid_key_name = [] (const char *name) -> bool
+  {
+    gdb_assert (name != nullptr);
+
+    if (*name == '\0' || !isalpha (*name))
+      return false;
+
+    for (; *name != '\0'; ++name)
+      if (!isalnum (*name) && *name != '_' && *name != '-')
+	return false;
+
+    return true;
+  };
+
+  if (!is_valid_key_name (key_string.get ()))
+    {
+      if (*key_string.get () == '\0')
+	gdbpy_error (_("Invalid empty key in MI result"));
+      else
+	gdbpy_error (_("Invalid key in MI result: %s"), key_string.get ());
+    }
+
+  return key_string;
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+   FIELD_NAME is used as the name of this result field.
+
+   RESULT can be a dictionary, a sequence, an iterator, or an object that
+   can be converted to a string, these are converted to the matching MI
+   output format (dictionaries as tuples, sequences and iterators as lists,
+   and strings as named fields).
+
+   If anything goes wrong while formatting the output then an error is
+   thrown.
+
+   This function is the recursive inner core of serialize_mi_result, and
+   should only be called from that function.  */
+
+static void
+serialize_mi_result_1 (PyObject *result, const char *field_name)
+{
+  struct ui_out *uiout = current_uiout;
+
+  if (PyDict_Check (result))
+    {
+      PyObject *key, *value;
+      Py_ssize_t pos = 0;
+      ui_out_emit_tuple tuple_emitter (uiout, field_name);
+      while (PyDict_Next (result, &pos, &key, &value))
+	{
+	  gdb::unique_xmalloc_ptr<char> key_string
+	    (py_object_to_mi_key (key));
+	  serialize_mi_result_1 (value, key_string.get ());
+	}
+    }
+  else if (PySequence_Check (result) && !PyString_Check (result))
+    {
+      ui_out_emit_list list_emitter (uiout, field_name);
+      Py_ssize_t len = PySequence_Size (result);
+      if (len == -1)
+	gdbpy_handle_exception ();
+      for (Py_ssize_t i = 0; i < len; ++i)
+	{
+          gdbpy_ref<> item (PySequence_ITEM (result, i));
+	  if (item == nullptr)
+	    gdbpy_handle_exception ();
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else if (PyIter_Check (result))
+    {
+      gdbpy_ref<> item;
+      ui_out_emit_list list_emitter (uiout, field_name);
+      while (true)
+	{
+	  item.reset (PyIter_Next (result));
+	  if (item == nullptr)
+	    {
+	      if (PyErr_Occurred () != nullptr)
+		gdbpy_handle_exception ();
+	      break;
+	    }
+	  serialize_mi_result_1 (item.get (), nullptr);
+	}
+    }
+  else
+    {
+      gdb::unique_xmalloc_ptr<char> string (gdbpy_obj_to_string (result));
+      if (string == nullptr)
+	gdbpy_handle_exception ();
+      uiout->field_string (field_name, string.get ());
+    }
+}
+
+/* Serialize RESULT and print it in MI format to the current_uiout.
+
+   This function handles the top-level result initially returned from the
+   invoke method of the Python command implementation.  At the top-level
+   the result must be a dictionary.  The values within this dictionary can
+   be a wider range of types.  Handling the values of the top-level
+   dictionary is done by serialize_mi_result_1, see that function for more
+   details.
+
+   If anything goes wrong while parsing and printing the MI output then an
+   error is thrown.  */
+
+static void
+serialize_mi_result (PyObject *result)
+{
+  /* At the top-level, the result must be a dictionary.  */
+
+  if (!PyDict_Check (result))
+    gdbpy_error (_("Result from invoke must be a dictionary"));
+
+  PyObject *key, *value;
+  Py_ssize_t pos = 0;
+  while (PyDict_Next (result, &pos, &key, &value))
+    {
+      gdb::unique_xmalloc_ptr<char> key_string
+	(py_object_to_mi_key (key));
+      serialize_mi_result_1 (value, key_string.get ());
+    }
+}
+
+/* Called when the MI command is invoked.  PARSE contains the parsed
+   command line arguments from the user.  */
+
+void
+mi_command_py::do_invoke (struct mi_parse *parse) const
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  pymicmd_debug_printf ("this = %p, name = %s", this, name ());
+
+  mi_parse_argv (parse->args, parse);
+
+  if (parse->argv == nullptr)
+    error (_("Problem parsing arguments: %s %s"), parse->command, parse->args);
+
+  PyObject *obj = (PyObject *) this->m_pyobj;
+  gdb_assert (obj != nullptr);
+
+  gdbpy_enter enter_py;
+
+  /* Place all the arguments into a list which we pass as a single argument
+     to the MI command's invoke method.  */
+  gdbpy_ref<> argobj (PyList_New (parse->argc));
+  if (argobj == nullptr)
+    gdbpy_handle_exception ();
+
+  for (int i = 0; i < parse->argc; ++i)
+    {
+      gdbpy_ref<> str (PyUnicode_Decode (parse->argv[i],
+					 strlen (parse->argv[i]),
+					 host_charset (), nullptr));
+      if (PyList_SetItem (argobj.get (), i, str.release ()) < 0)
+	gdbpy_handle_exception ();
+    }
+
+  gdb_assert (PyErr_Occurred () == nullptr);
+  gdbpy_ref<> result (PyObject_CallMethodObjArgs (obj, invoke_cst,
+						  argobj.get (), nullptr));
+  if (result == nullptr)
+    gdbpy_handle_exception ();
+
+  if (result != Py_None)
+    serialize_mi_result (result.get ());
+}
+
+/* See declaration above.  */
+
+void
+mi_command_py::validate_installation (micmdpy_object *cmd_obj)
+{
+  gdb_assert (cmd_obj != nullptr);
+  mi_command_py *cmd = cmd_obj->mi_command;
+  gdb_assert (cmd != nullptr);
+  const char *name = cmd_obj->mi_command_name;
+  gdb_assert (name != nullptr);
+  gdb_assert (name == cmd->name ());
+  mi_command *mi_cmd = mi_cmd_lookup (name);
+  gdb_assert (mi_cmd == cmd);
+  gdb_assert (cmd->m_pyobj == cmd_obj);
+}
+
+/* Return a reference to the gdb._mi_commands dictionary.  If the
+   dictionary can't be found for any reason then nullptr is returned, and
+   a Python exception will be set.  */
+
+static gdbpy_ref<>
+micmdpy_global_command_dictionary ()
+{
+  if (gdb_python_module == nullptr)
+    {
+      PyErr_SetString (PyExc_RuntimeError, _("unable to find gdb module"));
+      return nullptr;
+    }
+
+  gdbpy_ref<> mi_cmd_dict (PyObject_GetAttrString (gdb_python_module,
+						   "_mi_commands"));
+  if (mi_cmd_dict == nullptr)
+    return nullptr;
+
+  if (!PyDict_Check (mi_cmd_dict.get ()))
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+		       _("gdb._mi_commands is not a dictionary as expected"));
+      return nullptr;
+    }
+
+  return mi_cmd_dict;
+}
+
+/* Uninstall OBJ, making the MI command represented by OBJ unavailable for
+   use by the user.  On success 0 is returned, otherwise -1 is returned
+   and a Python exception will be set.  */
+
+static int
+micmdpy_uninstall_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command != nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  /* Remove the command from the internal MI table of commands, this will
+     cause the c++ object to be deleted, which will clear the mi_command
+     member variable within the Python object.  */
+  remove_mi_cmd_entry (obj->mi_command->name ());
+  gdb_assert (obj->mi_command == nullptr);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Grab the name for this command.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+  if (name_obj == nullptr)
+    return -1;
+
+  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
+     commands, this is gdb._mi_command, and remove it.  */
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+
+  /* Did we encounter an error?  Failing to find the object in the
+     dictionary isn't an error, that's fine.  */
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+
+  /* Did we find this command in the gdb._mi_commands dictionary?  If so,
+     then remove it.  */
+  if (curr != nullptr)
+    {
+      /* Yes we did!  Remove it.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+	return -1;
+    }
+
+  return 0;
+}
+
+/* Install OBJ as a usable MI command.  Return 0 on success, and -1 on
+   error, in which case, a Python error will have been set.
+
+   After successful completion the command name associated with OBJ will
+   be installed in the MI command table (so it can be found if the user
+   enters that command name), additionally, OBJ will have been added to
+   the gdb._mi_commands dictionary (using the command name as its key),
+   this will ensure that OBJ remains live even if the user gives up all
+   references.  */
+
+static int
+micmdpy_install_command (micmdpy_object *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  gdb_assert (obj->mi_command == nullptr);
+  gdb_assert (obj->mi_command_name != nullptr);
+
+  pymicmd_debug_printf ("name = %s", obj->mi_command_name);
+
+  gdbpy_ref<> mi_cmd_dict = micmdpy_global_command_dictionary ();
+  if (mi_cmd_dict == nullptr)
+    return -1;
+
+  /* Look up this command name in the gdb._mi_commands dictionary, a
+     command with this name may already exist.  */
+  gdbpy_ref<> name_obj
+    = host_string_to_python_string (obj->mi_command_name);
+
+  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
+					    name_obj.get ());
+  if (curr == nullptr && PyErr_Occurred ())
+    return -1;
+  if (curr != nullptr)
+    {
+      /* There is a command with this name already in the gdb._mi_commands
+	 dictionary.  First, validate that the object in the dictionary is
+	 of the expected type, just in case something weird has happened.  */
+      if (!PyObject_IsInstance (curr, (PyObject *) &micmdpy_object_type))
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unexpected object in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* To get to this function OBJ should not be installed, which should
+	 mean OBJ is not in the gdb._mi_commands dictionary.  If we find
+	 that OBJ is the thing in the dictionary, then something weird is
+	 going on, we should throw an error.  */
+      micmdpy_object *other = (micmdpy_object *) curr;
+      if (other == obj || other->mi_command == nullptr)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("uninstalled command found in gdb._mi_commands dictionary"));
+	  return -1;
+	}
+
+      /* All Python mi command object should always have a name set.  */
+      gdb_assert (other->mi_command_name != nullptr);
+
+      /* We always insert commands into the gdb._mi_commands dictionary
+	 using their name as a key, if this check fails then the dictionary
+	 is in some weird state.  */
+      if (other->mi_command_name != other->mi_command->name ()
+	  || strcmp (other->mi_command_name, obj->mi_command_name) != 0)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("gdb._mi_commands dictionary is corrupted"));
+	  return -1;
+	}
+
+      /* Switch the state of the c++ object held in the MI command table
+	 so that it now references OBJ.  After this action the old Python
+	 object that used to be referenced from the MI command table will
+	 now show as uninstalled, while the new Python object will show as
+	 installed.  */
+      other->mi_command->swap_python_object (obj);
+
+      gdb_assert (other->mi_command == nullptr);
+      gdb_assert (obj->mi_command != nullptr);
+      gdb_assert (obj->mi_command->name () == obj->mi_command_name);
+
+      /* Remove the previous Python object from the gdb._mi_commands
+	 dictionary, we'll install the new object below.  */
+      if (PyDict_DelItem (mi_cmd_dict.get (), name_obj.get ()) < 0)
+       return -1;
+    }
+  else
+    {
+      /* There's no Python object for this command name in the
+	 gdb._mi_commands dictionary from which we can steal an existing
+	 object already held in the MI commands table, and so, we now
+	 create a new c++ object, and install it into the MI table.  */
+      obj->mi_command = new mi_command_py (obj->mi_command_name, obj);
+      mi_command_up micommand (obj->mi_command);
+
+      /* Add the command to the gdb internal MI command table.  */
+      bool result = insert_mi_cmd_entry (std::move (micommand));
+      if (!result)
+	{
+	  PyErr_SetString (PyExc_RuntimeError,
+			   _("unable to add command, name may already be in use"));
+	  return -1;
+	}
+    }
+
+  /* Finally, add the Python object to the gdb._mi_commands dictionary.  */
+  if (PyDict_SetItem (mi_cmd_dict.get (), name_obj.get (), (PyObject *) obj) < 0)
+    return -1;
+
+  return 0;
+}
+
+/* Implement gdb.MICommand.__init__.  The init method takes the name of
+   the MI command as the first argument, which must be a string, starting
+   with a single dash.  */
+
+static int
+micmdpy_init (PyObject *self, PyObject *args, PyObject *kwargs)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) self;
+
+  static const char *keywords[] = { "name", nullptr };
+  const char *name;
+
+  if (!gdb_PyArg_ParseTupleAndKeywords (args, kwargs, "s", keywords,
+					&name))
+    return -1;
+
+  /* Validate command name */
+  const int name_len = strlen (name);
+  if (name_len == 0)
+    {
+      PyErr_SetString (PyExc_ValueError, _("MI command name is empty."));
+      return -1;
+    }
+  else if ((name_len < 2) || (name[0] != '-') || !isalnum (name[1]))
+    {
+      PyErr_SetString (PyExc_ValueError,
+		       _("MI command name does not start with '-'"
+			 " followed by at least one letter or digit."));
+      return -1;
+    }
+  else
+    {
+      for (int i = 2; i < name_len; i++)
+	{
+	  if (!isalnum (name[i]) && name[i] != '-')
+	    {
+	      PyErr_Format
+		(PyExc_ValueError,
+		 _("MI command name contains invalid character: %c."),
+		 name[i]);
+	      return -1;
+	    }
+	}
+
+      /* Skip over the leading dash.  For the rest of this function the
+	 dash is not important.  */
+      ++name;
+    }
+
+  /* If this object already has a name set, then this object has been
+     initialized before.  We handle this case a little differently.  */
+  if (cmd->mi_command_name != nullptr)
+    {
+      /* First, we don't allow the user to change the MI command name.
+	 Supporting this would be tricky as we would need to delete the
+	 mi_command_py from the MI command table, however, the user might
+	 be trying to perform this reinitialization from within the very
+	 command we're about to delete... it all gets very messy.
+
+	 So, for now at least, we don't allow this.  This doesn't seem like
+	 an excessive restriction.  */
+      if (strcmp (cmd->mi_command_name, name) != 0)
+	{
+	  PyErr_SetString
+	    (PyExc_ValueError,
+	     _("can't reinitialize object with a different command name"));
+	  return -1;
+	}
+
+      /* If there's already an object registered with the MI command table,
+	 then we're done.  That object must be a mi_command_py, which
+	 should reference back to this micmdpy_object.  */
+      if (cmd->mi_command != nullptr)
+	{
+	  mi_command_py::validate_installation (cmd);
+	  return 0;
+	}
+    }
+  else
+    cmd->mi_command_name = xstrdup (name);
+
+  /* Now we can install this mi_command_py in the MI command table.  */
+  return micmdpy_install_command (cmd);
+}
+
+/* Called when a gdb.MICommand object is deallocated.  */
+
+static void
+micmdpy_dealloc (PyObject *obj)
+{
+  PYMICMD_SCOPED_DEBUG_ENTER_EXIT;
+
+  micmdpy_object *cmd = (micmdpy_object *) obj;
+
+  /* If the Python object failed to initialize, then the name field might
+     be nullptr.  */
+  pymicmd_debug_printf ("obj = %p, name = %s", cmd,
+			(cmd->mi_command_name == nullptr
+			 ? "(null)" : cmd->mi_command_name));
+
+  /* Remove the command from the MI command table if needed.  This will
+     cause the mi_command_py object to be deleted, which, in turn, will
+     clear the cmd->mi_command member variable, hence the assert.  */
+  if (cmd->mi_command != nullptr)
+    remove_mi_cmd_entry (cmd->mi_command->name ());
+  gdb_assert (cmd->mi_command == nullptr);
+
+  /* Free the memory that holds the command name.  */
+  xfree (cmd->mi_command_name);
+  cmd->mi_command_name = nullptr;
+
+  /* Finally, free the memory for this Python object.  */
+  Py_TYPE (obj)->tp_free (obj);
+}
+
+/* Python initialization for the MI commands components.  */
+
+int
+gdbpy_initialize_micommands ()
+{
+  micmdpy_object_type.tp_new = PyType_GenericNew;
+  if (PyType_Ready (&micmdpy_object_type) < 0)
+    return -1;
+
+  if (gdb_pymodule_addobject (gdb_module, "MICommand",
+			      (PyObject *) &micmdpy_object_type)
+      < 0)
+    return -1;
+
+  invoke_cst = PyString_FromString ("invoke");
+  if (invoke_cst == nullptr)
+    return -1;
+
+  return 0;
+}
+
+/* Get the gdb.MICommand.name attribute, returns a string, the name of this
+   MI command.  */
+
+static PyObject *
+micmdpy_get_name (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  gdb_assert (micmd_obj->mi_command_name != nullptr);
+  std::string name_str = string_printf ("-%s", micmd_obj->mi_command_name);
+  return PyString_FromString (name_str.c_str ());
+}
+
+/* Get the gdb.MICommand.installed property.  Returns true if this MI
+   command is installed into the MI command table, otherwise returns
+   false.  */
+
+static PyObject *
+micmdpy_get_installed (PyObject *self, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  if (micmd_obj->mi_command == nullptr)
+    Py_RETURN_FALSE;
+  Py_RETURN_TRUE;
+}
+
+/* Set the gdb.MICommand.installed property.  The property can be set to
+   either true or false.  Setting the property to true will cause the
+   command to be installed into the MI command table (if it isn't
+   already), while setting this property to false will cause the command
+   to be removed from the MI command table (if it is present).  */
+
+static int
+micmdpy_set_installed (PyObject *self, PyObject *newvalue, void *closure)
+{
+  struct micmdpy_object *micmd_obj = (struct micmdpy_object *) self;
+
+  bool installed_p = PyObject_IsTrue (newvalue);
+  if (installed_p == (micmd_obj->mi_command != nullptr))
+    return 0;
+
+  if (installed_p)
+    return micmdpy_install_command (micmd_obj);
+  else
+    return micmdpy_uninstall_command (micmd_obj);
+}
+
+/* The gdb.MICommand properties.   */
+
+static gdb_PyGetSetDef micmdpy_object_getset[] = {
+  { "name", micmdpy_get_name, nullptr, "The command's name.", nullptr },
+  { "installed", micmdpy_get_installed, micmdpy_set_installed,
+    "Is this command installed for use.", nullptr },
+  { nullptr }	/* Sentinel.  */
+};
+
+/* The gdb.MICommand descriptor.  */
+
+PyTypeObject micmdpy_object_type = {
+  PyVarObject_HEAD_INIT (nullptr, 0) "gdb.MICommand", /*tp_name */
+  sizeof (micmdpy_object),			   /*tp_basicsize */
+  0,						   /*tp_itemsize */
+  micmdpy_dealloc,				   /*tp_dealloc */
+  0,						   /*tp_print */
+  0,						   /*tp_getattr */
+  0,						   /*tp_setattr */
+  0,						   /*tp_compare */
+  0,						   /*tp_repr */
+  0,						   /*tp_as_number */
+  0,						   /*tp_as_sequence */
+  0,						   /*tp_as_mapping */
+  0,						   /*tp_hash */
+  0,						   /*tp_call */
+  0,						   /*tp_str */
+  0,						   /*tp_getattro */
+  0,						   /*tp_setattro */
+  0,						   /*tp_as_buffer */
+  Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,	/*tp_flags */
+  "GDB mi-command object",			   /* tp_doc */
+  0,						   /* tp_traverse */
+  0,						   /* tp_clear */
+  0,						   /* tp_richcompare */
+  0,						   /* tp_weaklistoffset */
+  0,						   /* tp_iter */
+  0,						   /* tp_iternext */
+  0,						   /* tp_methods */
+  0,						   /* tp_members */
+  micmdpy_object_getset,			   /* tp_getset */
+  0,						   /* tp_base */
+  0,						   /* tp_dict */
+  0,						   /* tp_descr_get */
+  0,						   /* tp_descr_set */
+  0,						   /* tp_dictoffset */
+  micmdpy_init,					   /* tp_init */
+  0,						   /* tp_alloc */
+};
+
+void _initialize_py_micmd ();
+void
+_initialize_py_micmd ()
+{
+  add_setshow_boolean_cmd
+    ("py-micmd", class_maintenance, &pymicmd_debug,
+     _("Set Python micmd debugging."),
+     _("Show Python micmd debugging."),
+     _("When on, Python micmd debugging is enabled."),
+     nullptr,
+     show_pymicmd_debug,
+     &setdebuglist, &showdebuglist);
+}
diff --git a/gdb/python/py-utils.c b/gdb/python/py-utils.c
index 73c860bcc96..838853c896c 100644
--- a/gdb/python/py-utils.c
+++ b/gdb/python/py-utils.c
@@ -382,6 +382,23 @@ gdb_pymodule_addobject (PyObject *module, const char *name, PyObject *object)
   return result;
 }
 
+/* See python-internal.h.  */
+
+void
+gdbpy_error (const char *fmt, ...)
+{
+  va_list ap;
+  va_start (ap, fmt);
+  std::string str = string_vprintf (fmt, ap);
+  va_end (ap);
+
+  const char *msg = str.c_str ();
+  if (msg != nullptr && *msg != '\0')
+    error (_("Error occurred in Python: %s"), msg);
+  else
+    error (_("Error occurred in Python."));
+}
+
 /* Handle a Python exception when the special gdb.GdbError treatment
    is desired.  This should only be called when an exception is set.
    If the exception is a gdb.GdbError, throw a gdb exception with the
diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 5e15f62f745..083c4dbdbc3 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -562,6 +562,8 @@ int gdbpy_initialize_membuf ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 int gdbpy_initialize_connection ()
   CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
+int gdbpy_initialize_micommands (void)
+  CPYCHECKER_NEGATIVE_RESULT_SETS_EXCEPTION;
 
 /* A wrapper for PyErr_Fetch that handles reference counting for the
    caller.  */
@@ -730,6 +732,17 @@ void gdbpy_print_stack (void);
 void gdbpy_print_stack_or_quit ();
 void gdbpy_handle_exception () ATTRIBUTE_NORETURN;
 
+/* A wrapper around calling 'error'.  Prefixes the error message with an
+   'Error occurred in Python' string.  Use this in C++ code if we spot
+   something wrong with an object returned from Python code.  The prefix
+   string gives the user a hint that the mistake is within Python code,
+   rather than some other part of GDB.
+
+   This always calls error, and never returns.  */
+
+void gdbpy_error (const char *fmt, ...)
+  ATTRIBUTE_NORETURN ATTRIBUTE_PRINTF (1, 2);
+
 gdbpy_ref<> python_string_to_unicode (PyObject *obj);
 gdb::unique_xmalloc_ptr<char> unicode_to_target_string (PyObject *unicode_str);
 gdb::unique_xmalloc_ptr<char> python_string_to_target_string (PyObject *obj);
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 79f9826365a..97955881627 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1983,7 +1983,8 @@ do_start_initialization ()
       || gdbpy_initialize_unwind () < 0
       || gdbpy_initialize_membuf () < 0
       || gdbpy_initialize_connection () < 0
-      || gdbpy_initialize_tui () < 0)
+      || gdbpy_initialize_tui () < 0
+      || gdbpy_initialize_micommands () < 0)
     return false;
 
 #define GDB_PY_DEFINE_EVENT_TYPE(name, py_name, doc, base)	\
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.exp b/gdb/testsuite/gdb.python/py-mi-cmd.exp
new file mode 100644
index 00000000000..c102efb4932
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.exp
@@ -0,0 +1,390 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test custom MI commands implemented in Python.
+
+load_lib gdb-python.exp
+load_lib mi-support.exp
+set MIFLAGS "-i=mi"
+
+gdb_exit
+if {[mi_gdb_start]} {
+    continue
+}
+
+if {[lsearch -exact [mi_get_features] python] < 0} {
+    unsupported "python support is disabled"
+    return -1
+}
+
+standard_testfile
+
+mi_gdb_test "set python print-stack full" \
+    ".*\\^done" \
+    "set python print-stack full"
+
+mi_gdb_test "source ${srcdir}/${subdir}/${testfile}.py" \
+    ".*\\^done" \
+    "load python file"
+
+mi_gdb_test "python pycmd1('-pycmd')" \
+    ".*\\^done" \
+    "define -pycmd MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-pycmd str"
+
+mi_gdb_test "-pycmd ary" \
+    "\\^done,result=\\\[\"Hello\",\"42\"\\\]" \
+    "-pycmd ary"
+
+mi_gdb_test "-pycmd dct" \
+    "\\^done,result={hello=\"world\",times=\"42\"}" \
+    "-pycmd dct"
+
+mi_gdb_test "-pycmd bk1" \
+    "\\^error,msg=\"Error occurred in Python: non-string object used as key: Bad Key\"" \
+    "-pycmd bk1"
+
+mi_gdb_test "-pycmd bk2" \
+    "\\^error,msg=\"Error occurred in Python: non-string object used as key: 1\"" \
+    "-pycmd bk2"
+
+mi_gdb_test "-pycmd bk3" \
+    [multi_line \
+	 "&\"TypeError: __repr__ returned non-string \\(type BadKey\\)..\"" \
+	 "\\^error,msg=\"Error occurred in Python: __repr__ returned non-string \\(type BadKey\\)\""] \
+    "-pycmd bk3"
+
+mi_gdb_test "-pycmd tpl" \
+    "\\^done,result=\\\[\"42\",\"Hello\"\\\]" \
+    "-pycmd tpl"
+
+mi_gdb_test "-pycmd itr" \
+    "\\^done,result=\\\[\"1\",\"2\",\"3\"\\\]" \
+    "-pycmd itr"
+
+mi_gdb_test "-pycmd nn1" \
+    "\\^done" \
+    "-pycmd nn1"
+
+mi_gdb_test "-pycmd nn2" \
+    "\\^done,result=\\\[\"None\"\\\]" \
+    "-pycmd nn2"
+
+mi_gdb_test "-pycmd bogus" \
+    "\\^error,msg=\"Invalid parameter: bogus\"" \
+    "-pycmd bogus"
+
+# Check that the top-level result from 'invoke' must be a dictionary.
+foreach test_name { nd1 nd2 nd3 } {
+    mi_gdb_test "-pycmd ${test_name}" \
+	"\\^error,msg=\"Error occurred in Python: Result from invoke must be a dictionary\""
+}
+
+# Check for invalid strings in the result.
+foreach test_desc { {ik1 "xxx yyy"} {ik2 "xxx yyy"} {ik3 "xxx\\+yyy"} \
+			{ik4 "xxx\\.yyy"} {ik5 "123xxxyyy"} } {
+    lassign $test_desc name pattern
+
+    mi_gdb_test "-pycmd ${name}" \
+	"\\^error,msg=\"Error occurred in Python: Invalid key in MI result: ${pattern}\""
+}
+
+mi_gdb_test "-pycmd empty_key" \
+    "\\^error,msg=\"Error occurred in Python: Invalid empty key in MI result\""
+
+# Check that a dash ('-') can be used in a key name.
+mi_gdb_test "-pycmd dash-key" \
+    "\\^done,the-key=\"123\""
+
+# With this argument the command raises a gdb.GdbError with no message
+# string.  GDB considers this a bug in the user program, so prints a
+# backtrace, and a generic error message.
+mi_gdb_test "-pycmd exp" \
+    [multi_line ".*&\"Traceback \\(most recent call last\\):..\"" \
+	 "&\"\[^\r\n\]+${testfile}.py\[^\r\n\]+\"" \
+	 "&\"\[^\r\n\]+raise gdb.GdbError\\(\\)..\"" \
+	 "&\"gdb.GdbError..\"" \
+	 "\\^error,msg=\"Error occurred in Python\\.\""] \
+    "-pycmd exp"
+
+mi_gdb_test "python pycmd2('-pycmd')" \
+    ".*\\^done" \
+    "redefine -pycmd MI command from CLI command"
+
+mi_gdb_test "-pycmd str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-pycmd str - redefined from CLI"
+
+mi_gdb_test "-pycmd int" \
+    "\\^error,msg=\"Invalid parameter: int\"" \
+    "-pycmd int - redefined from CLI"
+
+mi_gdb_test "-pycmd new" \
+    "\\^done" \
+    "Define new command -pycmd-new MI command from Python MI command"
+
+mi_gdb_test "-pycmd red" \
+    "\\^error,msg=\"Command redefined but we failing anyway\"" \
+    "redefine -pycmd MI command from Python MI command"
+
+mi_gdb_test "-pycmd int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd int - redefined from MI"
+
+mi_gdb_test "-pycmd-new int" \
+    "\\^done,result=\"42\"" \
+    "-pycmd-new int - defined from MI"
+
+mi_gdb_test "python pycmd1('')" \
+    ".*&\"ValueError: MI command name is empty\\...\".*\\^error,msg=\"Error while executing Python code\\.\"" \
+    "empty MI command name"
+
+mi_gdb_test "python pycmd1('-')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name does not start with '-' followed by at least one letter or digit\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid MI command name"
+
+mi_gdb_test "python pycmd1('-bad-character-@')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: MI command name contains invalid character: @\\...\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "invalid character in MI command name"
+
+mi_gdb_test "python cmd=pycmd1('-abc')" \
+    ".*\\^done" \
+    "create command -abc, stored in a python variable"
+
+mi_gdb_test "python print(cmd.name)" \
+    ".*\r\n~\"-abc\\\\n\"\r\n\\^done" \
+    "print the name of the stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str"
+
+mi_gdb_test "python cmd.installed = False" \
+    ".*\\^done" \
+    "uninstall the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^error,msg=\"Undefined MI command: abc\",code=\"undefined-command\"" \
+    "-abc str, but now the command is gone"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the command is back again"
+
+mi_gdb_test "python other=pycmd2('-abc')" \
+    ".*\\^done" \
+    "create another command called -abc, stored in a separate python variable"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "print the installed status of the other stored mi command"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "print the installed status of the original stored mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Ciao!\"" \
+    "-abc str, when the other command is in place"
+
+mi_gdb_test "python cmd.installed = True" \
+    ".*\\^done" \
+    "re-install the original mi command"
+
+mi_gdb_test "-abc str" \
+    "\\^done,result=\"Hello world!\"" \
+    "-abc str, the original command is back again"
+
+mi_gdb_test "python print(other.installed)" \
+    ".*\r\n~\"False\\\\n\"\r\n\\^done" \
+    "the other command is now not installed"
+
+mi_gdb_test "python print(cmd.installed)" \
+    ".*\r\n~\"True\\\\n\"\r\n\\^done" \
+    "the original command is now installed"
+
+mi_gdb_test "python aa = pycmd3('-aa', 'message one', 'xxx')" \
+    ".*\\^done" \
+    "created a new -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,xxx={msg=\"message one\"}" \
+    "call the -aa command"
+
+mi_gdb_test "python aa.__init__('-aa', 'message two', 'yyy')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "call the -aa command, get the new message"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,yyy={msg=\"message two\"}" \
+    "check the aa object has not changed after failed initialization"
+
+mi_gdb_test "python aa.installed = False" \
+    ".*\\^done" \
+    "uninstall the -aa command"
+
+mi_gdb_test "python aa.__init__('-bb', 'message three', 'zzz')" \
+    [multi_line \
+	 ".*" \
+	 "&\"ValueError: can't reinitialize object with a different command name..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "attempt to reinitialise aa variable to a new command name while uninstalled"
+
+mi_gdb_test "python aa.__init__('-aa', 'message three', 'zzz')" \
+    ".*\\^done" \
+    "reinitialise -aa command with a new message while uninstalled"
+
+mi_gdb_test "python aa.installed = True" \
+    ".*\\^done" \
+    "install the -aa command"
+
+mi_gdb_test "-aa" \
+    ".*\\^done,zzz={msg=\"message three\"}" \
+    "call the -aa command looking for message three"
+
+# Remove the gdb._mi_commands dictionary, then try to register a new
+# command.
+mi_gdb_test "python del(gdb._mi_commands)" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: module 'gdb' has no attribute '_mi_commands'..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command with no gdb._mi_commands available"
+
+# Set gdb._mi_commands to be something other than a dictionary, and
+# try to register a command.
+mi_gdb_test "python gdb._mi_commands = 'string'" ".*\\^done"
+mi_gdb_test "python pycmd3('-hello', 'Hello', 'msg')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: gdb._mi_commands is not a dictionary as expected..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "register a command when gdb._mi_commands is not a dictionary"
+
+# Restore gdb._mi_commands to a dictionary.
+mi_gdb_test "python gdb._mi_commands = {}" ".*\\^done"
+
+# Try to register a command object that is missing an invoke method.
+# This is accepted, but will give an error when the user tries to run
+# the command.
+mi_gdb_test "python no_invoke('-no-invoke')" ".*\\^done" \
+    "attempt to register command with no invoke method"
+mi_gdb_test "-no-invoke" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
+    "execute -no-invoke command, which is missing the invoke method"
+
+# Register a command, then delete its invoke method.  What is the user thinking!!
+mi_gdb_test "python setattr(no_invoke, 'invoke', free_invoke)" ".*\\^done"
+mi_gdb_test "python cmd = no_invoke('-hello')" ".*\\^done"
+mi_gdb_test "-hello" ".*\\^done,result=\\\[\\\]" \
+    "execute no_invoke command, while it still has an invoke attribute"
+mi_gdb_test "python delattr(no_invoke, 'invoke')" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"AttributeError: 'no_invoke' object has no attribute 'invoke'..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'no_invoke' object has no attribute 'invoke'\""] \
+    "execute -hello command, that had its invoke method removed"
+mi_gdb_test "python cmd.invoke = 'string'" ".*\\^done"
+mi_gdb_test "-hello" \
+    [multi_line \
+	 ".*" \
+	 "&\"TypeError: 'str' object is not callable..\"" \
+	 "\\^error,msg=\"Error occurred in Python: 'str' object is not callable\""] \
+    "execute command with invoke set to a string"
+
+# Further checking of corruption to the gdb._mi_commands dictionary.
+#
+# First, insert an object of the wrong type, then try to register an
+# MI command that will go into that same dictionary slot.
+mi_gdb_test "python gdb._mi_commands\['blah'\] = 'blah blah blah'" ".*\\^done"
+mi_gdb_test "python pycmd2('-blah')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unexpected object in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "hit unexpected object in gdb._mi_commands dictionary"
+
+# Next, create a command, uninstall it, then force the command back
+# into the dictionary.
+mi_gdb_test "python cmd = pycmd2('-foo')" ".*\\^done"
+mi_gdb_test "python cmd.installed = False" ".*\\^done"
+mi_gdb_test "python gdb._mi_commands\['foo'\] = cmd" ".*\\^done"
+mi_gdb_test "python cmd.installed = True" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: uninstalled command found in gdb\\._mi_commands dictionary..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "found uninstalled command in gdb._mi_commands dictionary"
+
+# Try to create a new MI command that uses the name of a builtin MI command.
+mi_gdb_test "python cmd = pycmd2('-data-disassemble')" \
+    [multi_line \
+	 ".*" \
+	 "&\"RuntimeError: unable to add command, name may already be in use..\"" \
+	 "&\"Error while executing Python code\\...\"" \
+	 "\\^error,msg=\"Error while executing Python code\\.\""] \
+    "try to register a command that replaces -data-disassemble"
+
+
+
+mi_gdb_test "python run_exception_tests()" \
+    [multi_line \
+	 ".*" \
+	 "~\"PASS..\"" \
+	 "\\^done"]
diff --git a/gdb/testsuite/gdb.python/py-mi-cmd.py b/gdb/testsuite/gdb.python/py-mi-cmd.py
new file mode 100644
index 00000000000..ffe27c52cf1
--- /dev/null
+++ b/gdb/testsuite/gdb.python/py-mi-cmd.py
@@ -0,0 +1,120 @@
+# Copyright (C) 2019-2022 Free Software Foundation, Inc.
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+import gdb
+
+
+class BadKey:
+    def __repr__(self):
+        return "Bad Key"
+
+
+class ReallyBadKey:
+    def __repr__(self):
+        return BadKey()
+
+
+class pycmd1(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "int":
+            return {"result": 42}
+        elif argv[0] == "str":
+            return {"result": "Hello world!"}
+        elif argv[0] == "ary":
+            return {"result": ["Hello", 42]}
+        elif argv[0] == "dct":
+            return {"result": {"hello": "world", "times": 42}}
+        elif argv[0] == "bk1":
+            return {"result": {BadKey(): "world"}}
+        elif argv[0] == "bk2":
+            return {"result": {1: "world"}}
+        elif argv[0] == "bk3":
+            return {"result": {ReallyBadKey(): "world"}}
+        elif argv[0] == "tpl":
+            return {"result": (42, "Hello")}
+        elif argv[0] == "itr":
+            return {"result": iter([1, 2, 3])}
+        elif argv[0] == "nn1":
+            return None
+        elif argv[0] == "nn2":
+            return {"result": [None]}
+        elif argv[0] == "red":
+            pycmd2("-pycmd")
+            return None
+        elif argv[0] == "nd1":
+            return [1, 2, 3]
+        elif argv[0] == "nd2":
+            return 123
+        elif argv[0] == "nd3":
+            return "abc"
+        elif argv[0] == "ik1":
+            return {"xxx yyy": 123}
+        elif argv[0] == "ik2":
+            return {"result": {"xxx yyy": 123}}
+        elif argv[0] == "ik3":
+            return {"xxx+yyy": 123}
+        elif argv[0] == "ik4":
+            return {"xxx.yyy": 123}
+        elif argv[0] == "ik5":
+            return {"123xxxyyy": 123}
+        elif argv[0] == "empty_key":
+            return {"": 123}
+        elif argv[0] == "dash-key":
+            return {"the-key": 123}
+        elif argv[0] == "exp":
+            raise gdb.GdbError()
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+class pycmd2(gdb.MICommand):
+    def invoke(self, argv):
+        if argv[0] == "str":
+            return {"result": "Ciao!"}
+        elif argv[0] == "red":
+            pycmd1("-pycmd")
+            raise gdb.GdbError("Command redefined but we failing anyway")
+        elif argv[0] == "new":
+            pycmd1("-pycmd-new")
+            return None
+        else:
+            raise gdb.GdbError("Invalid parameter: %s" % argv[0])
+
+
+# This class creates a command that returns a string, which is passed
+# when the command is created.
+class pycmd3(gdb.MICommand):
+    def __init__(self, name, msg, top_level):
+        super(pycmd3, self).__init__(name)
+        self._msg = msg
+        self._top_level = top_level
+
+    def invoke(self, args):
+        return {self._top_level: {"msg": self._msg}}
+
+
+# A command that is missing it's invoke method.
+class no_invoke(gdb.MICommand):
+    def __init__(self, name):
+        super(no_invoke, self).__init__(name)
+
+
+def free_invoke(obj, args):
+    return {"result": args}
+
+
+# Run some test involving catching exceptions.  It's easier to write
+# these as a Python function which is then called from the exp script.
+def run_exception_tests():
+    print("PASS")
-- 
2.25.4


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-02-28 16:48                 ` [PATCHv5] " Andrew Burgess
@ 2022-02-28 18:40                   ` Tom Tromey
  2022-03-13  4:47                   ` Joel Brobecker
  2022-03-18 15:06                   ` Simon Marchi
  2 siblings, 0 replies; 41+ messages in thread
From: Tom Tromey @ 2022-02-28 18:40 UTC (permalink / raw)
  To: Andrew Burgess via Gdb-patches; +Cc: Andrew Burgess, Jan Vrany

>>>>> "Andrew" == Andrew Burgess via Gdb-patches <gdb-patches@sourceware.org> writes:

Andrew> In v5:

I think this addresses all my comments and looks good.  Thank you again.

Tom

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-02-28 16:48                 ` [PATCHv5] " Andrew Burgess
  2022-02-28 18:40                   ` Tom Tromey
@ 2022-03-13  4:47                   ` Joel Brobecker
  2022-03-14 14:13                     ` Andrew Burgess
  2022-03-18 15:06                   ` Simon Marchi
  2 siblings, 1 reply; 41+ messages in thread
From: Joel Brobecker @ 2022-03-13  4:47 UTC (permalink / raw)
  To: Andrew Burgess via Gdb-patches; +Cc: Andrew Burgess, Jan Vrany, Joel Brobecker

Hi Andrew,


On Mon, Feb 28, 2022 at 04:48:01PM +0000, Andrew Burgess via Gdb-patches wrote:
> In v5:
> 
>   - variable names within the mi output (from Python) can now contain
>     the '-' character,
> 
>   - the example in the docs has been updated to remove the reference
>     to an outdated feature (dropped in an earlier version, but the
>     example got left in by mistake),
> 
>   - updated the call to gdbpy_enter to pass no arguments,
> 
>   - rebased to latest upstream master.

Now that Tom as confirmed all his comments are approved, can we
push the patch to master?

-- 
Joel

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-03-13  4:47                   ` Joel Brobecker
@ 2022-03-14 14:13                     ` Andrew Burgess
  2022-03-16  8:10                       ` Joel Brobecker
  2022-03-16 12:29                       ` Simon Marchi
  0 siblings, 2 replies; 41+ messages in thread
From: Andrew Burgess @ 2022-03-14 14:13 UTC (permalink / raw)
  To: Joel Brobecker via Gdb-patches, Andrew Burgess via Gdb-patches
  Cc: Jan Vrany, Joel Brobecker

Joel Brobecker via Gdb-patches <gdb-patches@sourceware.org> writes:

> Hi Andrew,
>
>
> On Mon, Feb 28, 2022 at 04:48:01PM +0000, Andrew Burgess via Gdb-patches wrote:
>> In v5:
>> 
>>   - variable names within the mi output (from Python) can now contain
>>     the '-' character,
>> 
>>   - the example in the docs has been updated to remove the reference
>>     to an outdated feature (dropped in an earlier version, but the
>>     example got left in by mistake),
>> 
>>   - updated the call to gdbpy_enter to pass no arguments,
>> 
>>   - rebased to latest upstream master.
>
> Now that Tom as confirmed all his comments are approved, can we
> push the patch to master?

I had been waiting to see if Simon wanted to give any feedback, as he
commented on the earlier iterations.

But, since his last round of feedback has been addressed, and was pretty
minor, I've gone ahead and pushed this patch now.

If anything else comes up, then I am, of course, happy to address that
in a follow up patch.

Thanks,
Andrew


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-03-14 14:13                     ` Andrew Burgess
@ 2022-03-16  8:10                       ` Joel Brobecker
  2022-03-16 12:29                       ` Simon Marchi
  1 sibling, 0 replies; 41+ messages in thread
From: Joel Brobecker @ 2022-03-16  8:10 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: Joel Brobecker via Gdb-patches, Jan Vrany, Joel Brobecker

> I had been waiting to see if Simon wanted to give any feedback, as he
> commented on the earlier iterations.
> 
> But, since his last round of feedback has been addressed, and was pretty
> minor, I've gone ahead and pushed this patch now.
> 
> If anything else comes up, then I am, of course, happy to address that
> in a follow up patch.

Perfect. Thanks Andrew!

-- 
Joel

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-03-14 14:13                     ` Andrew Burgess
  2022-03-16  8:10                       ` Joel Brobecker
@ 2022-03-16 12:29                       ` Simon Marchi
  1 sibling, 0 replies; 41+ messages in thread
From: Simon Marchi @ 2022-03-16 12:29 UTC (permalink / raw)
  To: Andrew Burgess, Joel Brobecker via Gdb-patches; +Cc: Jan Vrany, Joel Brobecker



On 2022-03-14 10:13, Andrew Burgess via Gdb-patches wrote:
> Joel Brobecker via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
>> Hi Andrew,
>>
>>
>> On Mon, Feb 28, 2022 at 04:48:01PM +0000, Andrew Burgess via Gdb-patches wrote:
>>> In v5:
>>>
>>>   - variable names within the mi output (from Python) can now contain
>>>     the '-' character,
>>>
>>>   - the example in the docs has been updated to remove the reference
>>>     to an outdated feature (dropped in an earlier version, but the
>>>     example got left in by mistake),
>>>
>>>   - updated the call to gdbpy_enter to pass no arguments,
>>>
>>>   - rebased to latest upstream master.
>>
>> Now that Tom as confirmed all his comments are approved, can we
>> push the patch to master?
> 
> I had been waiting to see if Simon wanted to give any feedback, as he
> commented on the earlier iterations.
> 
> But, since his last round of feedback has been addressed, and was pretty
> minor, I've gone ahead and pushed this patch now.
> 
> If anything else comes up, then I am, of course, happy to address that
> in a follow up patch.

Sorry for not being more responsive.  I think you did the right thing
indeed.

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-02-28 16:48                 ` [PATCHv5] " Andrew Burgess
  2022-02-28 18:40                   ` Tom Tromey
  2022-03-13  4:47                   ` Joel Brobecker
@ 2022-03-18 15:06                   ` Simon Marchi
  2022-03-18 16:12                     ` Andrew Burgess
  2 siblings, 1 reply; 41+ messages in thread
From: Simon Marchi @ 2022-03-18 15:06 UTC (permalink / raw)
  To: Andrew Burgess, gdb-patches; +Cc: Jan Vrany

> +  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
> +     commands, this is gdb._mi_command, and remove it.  */
> +  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
> +					    name_obj.get ());

This doesn't build with Python 2:

/home/simark/src/binutils-gdb/gdb/python/py-micmd.c: In function ‘int micmdpy_uninstall_command(micmdpy_object*)’:
/home/simark/src/binutils-gdb/gdb/python/py-micmd.c:430:20: error: ‘PyDict_GetItemWithError’ was not declared in this scope; did you mean ‘PyDict_GetItemString’?
  430 |   PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
      |                    ^~~~~~~~~~~~~~~~~~~~~~~
      |                    PyDict_GetItemString


Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-03-18 15:06                   ` Simon Marchi
@ 2022-03-18 16:12                     ` Andrew Burgess
  2022-03-18 19:57                       ` Simon Marchi
  0 siblings, 1 reply; 41+ messages in thread
From: Andrew Burgess @ 2022-03-18 16:12 UTC (permalink / raw)
  To: Simon Marchi; +Cc: gdb-patches, Jan Vrany

* Simon Marchi <simark@simark.ca> [2022-03-18 11:06:43 -0400]:

> > +  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
> > +     commands, this is gdb._mi_command, and remove it.  */
> > +  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
> > +					    name_obj.get ());
> 
> This doesn't build with Python 2:
> 
> /home/simark/src/binutils-gdb/gdb/python/py-micmd.c: In function ‘int micmdpy_uninstall_command(micmdpy_object*)’:
> /home/simark/src/binutils-gdb/gdb/python/py-micmd.c:430:20: error: ‘PyDict_GetItemWithError’ was not declared in this scope; did you mean ‘PyDict_GetItemString’?
>   430 |   PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
>       |                    ^~~~~~~~~~~~~~~~~~~~~~~
>       |                    PyDict_GetItemString
> 

Simon,

After discussion on IRC, I believe you are working on a patch that
switches this code to make use of a C++ map (or some other
container).  If/when that patch is ready then that has my +1 as a
better solution.

However, just in case you don't have time for that, below is a work
around which I believe should be acceptable. I believe this patch is
OK only because I know that in a couple of weeks Py2 support is going
to be removed from master, so this work around would only live on in
the GDB 12 branch.

Comments welcome,

Thanks,
Andrew

---

commit 5ec0444e8312c17e628739ebd7b8f0e29df3c9e9
Author: Andrew Burgess <aburgess@redhat.com>
Date:   Fri Mar 18 15:55:34 2022 +0000

    gdb: supply PyDict_GetItemWithError when compiling with Python2
    
    Commit
    
      commit 740b42ceb7c7ae7b5343183782973576a93bc7b3
      Date:   Tue Jun 23 14:45:38 2020 +0100
    
          gdb/python/mi: create MI commands using python
    
    Introduced a use of PyDict_GetItemWithError, which is not available in
    Python2, and so the Python2 build was broken.
    
    This commit provides a work around for this:
    
      #define PyDict_GetItemWithError PyDict_GetItem
    
    The arguments passed to these calls are identical, the difference is
    that the WithError version will return with an exception raised in
    some error cases, while the PyDict_GetItem suppresses some errors, and
    just returns nullptr.
    
    Given that we are almost at the GDB 12 branch point, this commit is
    the easiest change to get the Python2 build working again.  After GDB
    12 branches the plan is to remove Python2 support, so this #define
    will be removed anyway.
    
    As far as functionality I think this #define will be fine.  In the
    "normal" no error case, then obviously, everything will be fine.  The
    only difference is what happens when an error occurs.
    
    The only errors I think could happen if Python runs into problems
    hashing the string, or comparing the strings for equality.  Given the
    limited strings we're working with I don't expect that this should
    occur during normal operation.  So, my expectation is that this should
    only happen if they user has gone and messed around with this hash in
    some way - in which case I think it's OK if weird things happen.
    
    This issue was discussed on IRC, and I think Simon might be working on
    an alternative patch to remove the _mi_commands dictionary from the
    gdb module, in which case the use of PyDict_GetItemWithError will be
    removed completely.  I'm posting this just in case people want to
    consider a minimal work around for the breakage in the short term.

diff --git a/gdb/python/python-internal.h b/gdb/python/python-internal.h
index 083c4dbdbc3..e9f786ca61c 100644
--- a/gdb/python/python-internal.h
+++ b/gdb/python/python-internal.h
@@ -102,6 +102,9 @@
 #define PyString_Decode PyUnicode_Decode
 #define PyString_FromFormat PyUnicode_FromFormat
 #define PyString_Check PyUnicode_Check
+#else
+/* Until Python2 support is removed.  */
+#define PyDict_GetItemWithError PyDict_GetItem
 #endif
 
 /* If Python.h does not define WITH_THREAD, then the various


^ permalink raw reply	[flat|nested] 41+ messages in thread

* Re: [PATCHv5] gdb/python/mi: create MI commands using python
  2022-03-18 16:12                     ` Andrew Burgess
@ 2022-03-18 19:57                       ` Simon Marchi
  0 siblings, 0 replies; 41+ messages in thread
From: Simon Marchi @ 2022-03-18 19:57 UTC (permalink / raw)
  To: Andrew Burgess; +Cc: Jan Vrany, gdb-patches

On 2022-03-18 12:12, Andrew Burgess via Gdb-patches wrote:
> * Simon Marchi <simark@simark.ca> [2022-03-18 11:06:43 -0400]:
>
>>> +  /* Lookup the gdb.MICommand object in the dictionary of all Python MI
>>> +     commands, this is gdb._mi_command, and remove it.  */
>>> +  PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
>>> +					    name_obj.get ());
>>
>> This doesn't build with Python 2:
>>
>> /home/simark/src/binutils-gdb/gdb/python/py-micmd.c: In function ‘int micmdpy_uninstall_command(micmdpy_object*)’:
>> /home/simark/src/binutils-gdb/gdb/python/py-micmd.c:430:20: error: ‘PyDict_GetItemWithError’ was not declared in this scope; did you mean ‘PyDict_GetItemString’?
>>   430 |   PyObject *curr = PyDict_GetItemWithError (mi_cmd_dict.get (),
>>       |                    ^~~~~~~~~~~~~~~~~~~~~~~
>>       |                    PyDict_GetItemString
>>
>
> Simon,
>
> After discussion on IRC, I believe you are working on a patch that
> switches this code to make use of a C++ map (or some other
> container).  If/when that patch is ready then that has my +1 as a
> better solution.
>
> However, just in case you don't have time for that, below is a work
> around which I believe should be acceptable. I believe this patch is
> OK only because I know that in a couple of weeks Py2 support is going
> to be removed from master, so this work around would only live on in
> the GDB 12 branch.

Hi,

Here's my patch:

https://sourceware.org/pipermail/gdb-patches/2022-March/186799.html

In the end I think it's simpler if we don't use a separate container at
all, just make the mi_command_py objects hold the reference to the
Python objects.

Simon

^ permalink raw reply	[flat|nested] 41+ messages in thread

end of thread, other threads:[~2022-03-18 19:57 UTC | newest]

Thread overview: 41+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-17 12:44 [PATCH 0/5] create GDB/MI commands using python Jan Vrany
2022-01-17 12:44 ` [PATCH 1/5] gdb/mi: introduce new class mi_command_builtin Jan Vrany
2022-01-17 12:44 ` [PATCH 2/5] gdb/python: create GDB/MI commands using python Jan Vrany
2022-02-06 16:52   ` Lancelot SIX
2022-01-17 12:44 ` [PATCH 3/5] gdb/python: allow redefinition of python GDB/MI commands Jan Vrany
2022-02-06 17:13   ` Lancelot SIX
2022-02-06 20:33     ` Simon Marchi
2022-02-06 20:44       ` Jan Vrany
2022-02-06 20:46         ` Simon Marchi
2022-02-07  9:46         ` Lancelot SIX
2022-01-17 12:44 ` [PATCH 4/5] gdb/testsuite: add tests for python-defined MI commands Jan Vrany
2022-01-17 12:44 ` [PATCH 5/5] gdb/python: document GDB/MI commands in Python Jan Vrany
2022-01-17 13:15   ` Eli Zaretskii
2022-01-17 13:20   ` Eli Zaretskii
2022-01-18 12:34     ` Jan Vrany
2022-01-18 15:09       ` Eli Zaretskii
2022-01-18 13:55 ` [PATCH 0/5] create GDB/MI commands using python Andrew Burgess
2022-01-18 15:13   ` Jan Vrany
2022-01-21 15:22     ` Andrew Burgess
2022-01-24 12:59       ` Jan Vrany
2022-02-02 16:57         ` Andrew Burgess
2022-02-06 21:16       ` Simon Marchi
2022-02-07 15:56         ` [PATCHv2] gdb/python/mi: create MI " Andrew Burgess
2022-02-08 15:16           ` Simon Marchi
2022-02-09 12:25             ` [PATCHv3] " Andrew Burgess
2022-02-09 14:08               ` Simon Marchi
2022-02-10 18:26                 ` Andrew Burgess
2022-02-13 14:27                   ` Joel Brobecker
2022-02-13 21:46                     ` Jan Vrany
2022-02-24 10:37               ` [PATCHv4] " Andrew Burgess
2022-02-25 19:22                 ` Tom Tromey
2022-02-25 19:31                   ` Jan Vrany
2022-02-28 16:48                 ` [PATCHv5] " Andrew Burgess
2022-02-28 18:40                   ` Tom Tromey
2022-03-13  4:47                   ` Joel Brobecker
2022-03-14 14:13                     ` Andrew Burgess
2022-03-16  8:10                       ` Joel Brobecker
2022-03-16 12:29                       ` Simon Marchi
2022-03-18 15:06                   ` Simon Marchi
2022-03-18 16:12                     ` Andrew Burgess
2022-03-18 19:57                       ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).