aboutsummaryrefslogtreecommitdiff
path: root/gdb/python
diff options
context:
space:
mode:
authorAndrew Burgess <aburgess@redhat.com>2026-03-27 09:52:55 +0000
committerAndrew Burgess <aburgess@redhat.com>2026-04-20 22:15:26 +0100
commit4ecaac39c26eb424a39a7427a524036c5fb85af6 (patch)
tree9aa3a93c45bd9ef1f61f730988332af95b4d32c1 /gdb/python
parent3780b9993c973a2b68b496b80eddb820c0932cc0 (diff)
downloadbinutils-4ecaac39c26eb424a39a7427a524036c5fb85af6.tar.gz
binutils-4ecaac39c26eb424a39a7427a524036c5fb85af6.tar.bz2
binutils-4ecaac39c26eb424a39a7427a524036c5fb85af6.zip
gdb/python: new events.corefile_changed event
Add a new Python event registry, events.corefile_changed. This event is emitted each time the corefile within an inferior changes. The event object has a single 'inferior' attribute which is the gdb.Inferior object for which the core file changed. The user can then inspect Inferior.corefile to see details about the new core file, or this will be None if the core file was removed from the inferior. I've updated the existing test to cover this new event. The new test covers both the corefile_changed event, but also monitors the exited event. This ties into the work done in the previous commit where we use whether the inferior has exited or not as a guard for whether core_target::exit_core_file_inferior should be called. Unloading a core file should result in a single corefile_changed event and a single exited event. Reviewed-By: Eli Zaretskii <eliz@gnu.org>
Diffstat (limited to 'gdb/python')
-rw-r--r--gdb/python/py-all-events.def1
-rw-r--r--gdb/python/py-corefile.c39
-rw-r--r--gdb/python/py-event-types.def5
3 files changed, 43 insertions, 2 deletions
diff --git a/gdb/python/py-all-events.def b/gdb/python/py-all-events.def
index 24724038562..3711cf29287 100644
--- a/gdb/python/py-all-events.def
+++ b/gdb/python/py-all-events.def
@@ -47,3 +47,4 @@ GDB_PY_DEFINE_EVENT(new_progspace)
GDB_PY_DEFINE_EVENT(free_progspace)
GDB_PY_DEFINE_EVENT(tui_enabled)
GDB_PY_DEFINE_EVENT(selected_context)
+GDB_PY_DEFINE_EVENT(corefile_changed)
diff --git a/gdb/python/py-corefile.c b/gdb/python/py-corefile.c
index 1daeabb5915..0fa4e90488c 100644
--- a/gdb/python/py-corefile.c
+++ b/gdb/python/py-corefile.c
@@ -23,6 +23,7 @@
#include "inferior.h"
#include "gdbcore.h"
#include "gdbsupport/rsp-low.h"
+#include "py-event.h"
/* A gdb.Corefile object. */
@@ -320,13 +321,47 @@ cfpy_mapped_files (PyObject *self, PyObject *args)
return obj->mapped_files;
}
-/* Callback from gdb::observers::core_file_changed. The core file in
- PSPACE has been changed. */
+/* Emit a CorefileChangedEvent event, INF is the inferior in which the core
+ file changed. Return 0 on success, or a negative value on error. */
+
+static int
+emit_corefile_changed_event (inferior *inf)
+{
+ /* If there are no listeners then we are done. */
+ if (evregpy_no_listeners_p (gdb_py_events.corefile_changed))
+ return 0;
+
+ gdbpy_ref<> event_obj
+ = create_event_object (&corefile_changed_event_object_type);
+ if (event_obj == nullptr)
+ return -1;
+
+ gdbpy_ref<inferior_object> inf_obj = inferior_to_inferior_object (inf);
+ if (inf_obj == nullptr
+ || evpy_add_attribute (event_obj.get (), "inferior",
+ inf_obj.get ()) < 0)
+ return -1;
+
+ return evpy_emit_event (event_obj.get (), gdb_py_events.corefile_changed);
+}
+
+/* Callback from gdb::observers::core_file_changed. The core file for
+ INF has been changed. */
static void
cfpy_corefile_changed (inferior *inf)
{
+ /* It's safe to do this even if Python is not initialized, but there
+ should be nothing to clear in that case. */
cfpy_inferior_corefile_data_key.clear (inf);
+
+ if (!gdb_python_initialized)
+ return;
+
+ gdbpy_enter enter_py;
+
+ if (emit_corefile_changed_event (inf) < 0)
+ gdbpy_print_stack ();
}
/* Called when a gdb.Corefile is destroyed. */
diff --git a/gdb/python/py-event-types.def b/gdb/python/py-event-types.def
index fe3e0978a55..7dddcc156ec 100644
--- a/gdb/python/py-event-types.def
+++ b/gdb/python/py-event-types.def
@@ -150,3 +150,8 @@ GDB_PY_DEFINE_EVENT_TYPE (selected_context,
"SelectedContextEvent",
"GDB user selected context event object",
event_object_type);
+
+GDB_PY_DEFINE_EVENT_TYPE (corefile_changed,
+ "CorefileChangedEvent",
+ "GDB corefile changed event",
+ event_object_type);