From 74daa597e743462d272522512efbe3f65659c4c0 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 3 Oct 2023 22:20:21 -0400 Subject: gdb: add all_objfiles_removed observer The new_objfile observer is currently used to indicate both when a new objfile is added to program space (when passed non-nullptr) and when all objfiles of a program space were just removed (when passed nullptr). I think this is confusing (and Andrew apparently thinks so too [1]). Add a new "all_objfiles_removed" observer to remove the second role from "new_objfile". Some existing users of new_objfile do nothing if the passed objfile is nullptr. For them, we can simply drop the nullptr check. For others, add a new all_objfiles_removed callback, and refactor things a bit to keep the existing behavior as much as possible. Some callbacks relied on current_program_space, and following the refactoring now use either objfile->pspace or the pspace passed to all_objfiles_removed. I think this should be relatively safe, and in general a step in the right direction. On the notify side, I found only one call site to change from new_objfile to all_objfiles_removed, in clear_symtab_users. It is not entirely clear to me that this is entirely correct. clear_symtab_users appears to be called in spots that don't remove all objfiles (functions finish_new_objfile, remove_symbol_file_command, reread_symbols, do_module_cleanups). But I think that this patch at least makes the current code clearer. [1] https://gitlab.com/gnutools/binutils-gdb/-/commit/a0a031bce0527b1521788b5dad640e7883b3a252 Change-Id: Icb648f72862e056267f30f44dd439bd4ec766f13 Approved-By: Tom Tromey --- gdb/python/py-inferior.c | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) (limited to 'gdb/python/py-inferior.c') diff --git a/gdb/python/py-inferior.c b/gdb/python/py-inferior.c index e7a9d82..1f20b9a 100644 --- a/gdb/python/py-inferior.c +++ b/gdb/python/py-inferior.c @@ -190,20 +190,22 @@ python_new_objfile (struct objfile *objfile) if (!gdb_python_initialized) return; - gdbpy_enter enter_py (objfile != NULL - ? objfile->arch () - : target_gdbarch ()); + gdbpy_enter enter_py (objfile->arch ()); - if (objfile == NULL) - { - if (emit_clear_objfiles_event (current_program_space) < 0) - gdbpy_print_stack (); - } - else - { - if (emit_new_objfile_event (objfile) < 0) - gdbpy_print_stack (); - } + if (emit_new_objfile_event (objfile) < 0) + gdbpy_print_stack (); +} + +static void +python_all_objfiles_removed (program_space *pspace) +{ + if (!gdb_python_initialized) + return; + + gdbpy_enter enter_py (target_gdbarch ()); + + if (emit_clear_objfiles_event (pspace) < 0) + gdbpy_print_stack (); } /* Emit a Python event when an objfile is about to be removed. */ @@ -1020,6 +1022,8 @@ gdbpy_initialize_inferior (void) gdb::observers::new_objfile.attach (python_new_objfile, "py-inferior", { &auto_load_new_objfile_observer_token }); + gdb::observers::all_objfiles_removed.attach (python_all_objfiles_removed, + "py-inferior"); gdb::observers::free_objfile.attach (python_free_objfile, "py-inferior"); gdb::observers::inferior_added.attach (python_new_inferior, "py-inferior"); gdb::observers::inferior_removed.attach (python_inferior_deleted, -- cgit v1.1