aboutsummaryrefslogtreecommitdiff
path: root/gdb/mi
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2020-06-23 14:45:38 +0100
committerAndrew Burgess <aburgess@redhat.com>2022-03-14 14:09:09 +0000
commit740b42ceb7c7ae7b5343183782973576a93bc7b3 (patch)
tree7aded4562770e0c288f2fbaa5d26c0e52d1d59cb /gdb/mi
parenta5118a18db47c8ccaf4995fbb62e2a1eb377fa3e (diff)
downloadgdb-740b42ceb7c7ae7b5343183782973576a93bc7b3.zip
gdb-740b42ceb7c7ae7b5343183782973576a93bc7b3.tar.gz
gdb-740b42ceb7c7ae7b5343183782973576a93bc7b3.tar.bz2
gdb/python/mi: create MI commands using python
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>
Diffstat (limited to 'gdb/mi')
-rw-r--r--gdb/mi/mi-cmds.c23
-rw-r--r--gdb/mi/mi-cmds.h18
2 files changed, 32 insertions, 9 deletions
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index cd7cabd..38fbe0d 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 @@ private:
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 785652e..47b90a2 100644
--- a/gdb/mi/mi-cmds.h
+++ b/gdb/mi/mi-cmds.h
@@ -199,6 +199,10 @@ private:
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. */
@@ -206,4 +210,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 */