aboutsummaryrefslogtreecommitdiff
path: root/gdb/mi
diff options
context:
space:
mode:
authorSimon Marchi <simon.marchi@efficios.com>2022-03-18 15:55:26 -0400
committerSimon Marchi <simon.marchi@polymtl.ca>2022-03-18 20:29:57 -0400
commit6f3dfea03aec47efb246e3601686d57595a80805 (patch)
tree46138b2715c97c71540d6878a3d7067c87d1d902 /gdb/mi
parent03a5735dbd0c88c2ac8d9e476b01801722f5cd35 (diff)
downloadgdb-6f3dfea03aec47efb246e3601686d57595a80805.zip
gdb-6f3dfea03aec47efb246e3601686d57595a80805.tar.gz
gdb-6f3dfea03aec47efb246e3601686d57595a80805.tar.bz2
gdb/python: remove gdb._mi_commands dict
The motivation for this patch is the fact that py-micmd.c doesn't build with Python 2, due to PyDict_GetItemWithError being a Python 3-only function: CXX python/py-micmd.o /home/smarchi/src/binutils-gdb/gdb/python/py-micmd.c: In function ‘int micmdpy_uninstall_command(micmdpy_object*)’: /home/smarchi/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 A first solution to fix this would be to try to replace PyDict_GetItemWithError equivalent Python 2 code. But I looked at why we are doing this in the first place: it is to maintain the `gdb._mi_commands` Python dictionary that we use as a `name -> gdb.MICommand object` map. Since the `gdb._mi_commands` dictionary is never actually used in Python, it seems like a lot of trouble to use a Python object for this. My first idea was to replace it with a C++ map (std::unordered_map<std::string, gdbpy_ref<micmdpy_object>>). While implementing this, I realized we don't really need this map at all. The mi_command_py objects registered in the main MI command table can own their backing micmdpy_object (that's a gdb.MICommand, but seen from the C++ code). To know whether an mi_command is an mi_command_py, we can use a dynamic cast. Since there's one less data structure to maintain, there are less chances of messing things up. - Change mi_command_py::m_pyobj to a gdbpy_ref, the mi_command_py is now what keeps the MICommand alive. - Set micmdpy_object::mi_command in the constructor of mi_command_py. If mi_command_py manages setting/clearing that field in swap_python_object, I think it makes sense that it also takes care of setting it initially. - Move a bunch of checks from micmdpy_install_command to swap_python_object, and make them gdb_asserts. - In micmdpy_install_command, start by doing an mi_cmd_lookup. This is needed to know whether there's a Python MI command already registered with that name. But we can already tell if there's a non-Python command registered with that name. Return an error if that happens, rather than waiting for insert_mi_cmd_entry to fail. Change the error message to "name is already in use" rather than "may already be in use", since it's more precise. I asked Andrew about the original intent of using a Python dictionary object to hold the command objects. The reason was to make sure the objects get destroyed when the Python runtime gets finalized, not later. Holding the objects in global C++ data structures and not doing anything more means that the held Python objects will be decref'd after the Python interpreter has been finalized. That's not desirable. I tried it and it indeed segfaults. Handle this by adding a gdbpy_finalize_micommands function called in finalize_python. This is the mirror of gdbpy_initialize_micommands called in do_start_initialization. In there, delete all Python MI commands. I think it makes sense to do it this way: if it was somehow possible to unload Python support from GDB in the middle of a session we'd want to unregister any Python MI command. Otherwise, these MI commands would be backed with a stale PyObject or simply nothing. Delete tests that were related to `gdb._mi_commands`. Co-Authored-By: Andrew Burgess <aburgess@redhat.com> Change-Id: I060d5ebc7a096c67487998a8a4ca1e8e56f12cd3
Diffstat (limited to 'gdb/mi')
-rw-r--r--gdb/mi/mi-cmds.c14
-rw-r--r--gdb/mi/mi-cmds.h7
2 files changed, 21 insertions, 0 deletions
diff --git a/gdb/mi/mi-cmds.c b/gdb/mi/mi-cmds.c
index 60fec0a..0571469 100644
--- a/gdb/mi/mi-cmds.c
+++ b/gdb/mi/mi-cmds.c
@@ -128,6 +128,20 @@ remove_mi_cmd_entry (const std::string &name)
return true;
}
+/* See mi-cmds.h. */
+
+void
+remove_mi_cmd_entries (remove_mi_cmd_entries_ftype callback)
+{
+ for (auto it = mi_cmd_table.cbegin (); it != mi_cmd_table.cend (); )
+ {
+ if (callback (it->second.get ()))
+ it = mi_cmd_table.erase (it);
+ else
+ ++it;
+ }
+}
+
/* 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 05b702f..9ffb11b 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 "gdbsupport/function-view.h"
#include "gdbsupport/gdb_optional.h"
#include "mi/mi-main.h"
@@ -218,5 +219,11 @@ extern bool insert_mi_cmd_entry (mi_command_up command);
extern bool remove_mi_cmd_entry (const std::string &name);
+/* Call CALLBACK for each registered MI command. Remove commands for which
+ CALLBACK returns true. */
+
+using remove_mi_cmd_entries_ftype
+ = gdb::function_view<bool (mi_command *)>;
+extern void remove_mi_cmd_entries (remove_mi_cmd_entries_ftype callback);
#endif /* MI_MI_CMDS_H */