gdb: new_objfile observer, update some comments, add some asserts
Checks
Commit Message
I was looking at the new_objfile observer and noticed a comment in
reread_symbols (symfile.c) that seemed out of date, it talked about
calling the new objfile observer with a NULL objfile argument.
A little digging indicates that the comment is indeed out of date, and
that we never call the new_objfile observer with a NULL argument any
more.
So in this commit I have:
1. Updated the out of date comment in reread_symbols.
2. Updated the comment in observable.h to indicate that the objfile
argument should never be NULL.
3. Gone through the existing new_objfile observers, and, where the
objfile argument is used, added a gdb_assert that the argument is
not NULL. These observers are not called that often, so adding
these asserts will have minimal performance impact.
There should be no user visible changes after this commit.
---
gdb/ada-lang.c | 1 +
gdb/ada-tasks.c | 1 +
gdb/agent.c | 2 ++
gdb/arm-tdep.c | 2 ++
gdb/auto-load.c | 2 ++
gdb/linux-thread-db.c | 2 ++
gdb/observable.h | 3 ++-
gdb/python/py-inferior.c | 6 +++---
gdb/remote.c | 1 +
gdb/symfile.c | 4 ++--
gdb/symtab.c | 1 +
11 files changed, 19 insertions(+), 6 deletions(-)
base-commit: e4fe38115fea0a9f357527959893f18ca8d51a03
Comments
On 2026-03-18 06:44, Andrew Burgess wrote:
> I was looking at the new_objfile observer and noticed a comment in
> reread_symbols (symfile.c) that seemed out of date, it talked about
> calling the new objfile observer with a NULL objfile argument.
>
> A little digging indicates that the comment is indeed out of date, and
> that we never call the new_objfile observer with a NULL argument any
> more.
Indeed.
>
> So in this commit I have:
>
> 1. Updated the out of date comment in reread_symbols.
>
> 2. Updated the comment in observable.h to indicate that the objfile
> argument should never be NULL.
>
> 3. Gone through the existing new_objfile observers, and, where the
> objfile argument is used, added a gdb_assert that the argument is
> not NULL. These observers are not called that often, so adding
> these asserts will have minimal performance impact.
You could also change the parameter to be a reference, this way it's
intrinsically never NULL. It would seem better to me than changing all
observers to have an assert.
Simon
@@ -14061,6 +14061,7 @@ static struct cmd_list_element *show_ada_list;
static void
ada_new_objfile_observer (struct objfile *objfile)
{
+ gdb_assert (objfile != nullptr);
ada_clear_symbol_cache (objfile->pspace ());
}
@@ -1468,6 +1468,7 @@ ada_tasks_clear_pspace_data (program_space *pspace)
static void
ada_tasks_new_objfile_observer (objfile *objfile)
{
+ gdb_assert (objfile != nullptr);
ada_tasks_clear_pspace_data (objfile->pspace ());
}
@@ -70,6 +70,8 @@ agent_new_objfile (struct objfile *objfile)
if (can_use_agent == can_use_agent_off)
return;
+ gdb_assert (objfile != nullptr);
+
agent_look_up_symbols (objfile);
}
@@ -2537,6 +2537,8 @@ arm_exidx_new_objfile (struct objfile *objfile)
bfd_vma exidx_vma = 0, extab_vma = 0;
LONGEST i;
+ gdb_assert (objfile != nullptr);
+
/* If we've already touched this file, do nothing. */
if (arm_exidx_data_key.get (objfile->obfd.get ()) != nullptr)
return;
@@ -1201,6 +1201,8 @@ auto_load_section_scripts (struct objfile *objfile, const char *section_name)
void
load_auto_scripts_for_objfile (struct objfile *objfile)
{
+ gdb_assert (objfile != nullptr);
+
/* Return immediately if auto-loading has been globally disabled.
This is to handle sequencing of operations during gdb startup.
Also return immediately if OBJFILE was not created from a file
@@ -1274,6 +1274,8 @@ thread_db_new_objfile (struct objfile *objfile)
/* This observer must always be called with inferior_ptid set
correctly. */
+ gdb_assert (objfile != nullptr);
+
if (/* libpthread with separate debug info has its debug info file already
loaded (and notified without successful thread_db initialization)
the time gdb::observers::new_objfile.notify is called for the library itself.
@@ -111,7 +111,8 @@ extern observable<solib &/* solib */> solib_loaded;
extern observable<program_space *, const solib &/* solib */,
bool /* still_in_use */, bool /* silent */> solib_unloaded;
-/* The symbol file specified by OBJFILE has been loaded. */
+/* The symbol file specified by OBJFILE has been loaded. OBJFILE will not
+ be NULL. */
extern observable<struct objfile */* objfile */> new_objfile;
/* All objfiles from PSPACE were removed. */
@@ -163,9 +163,8 @@ python_inferior_exit (struct inferior *inf)
gdbpy_print_stack ();
}
-/* Callback used to notify Python listeners about new objfiles loaded in the
- inferior. OBJFILE may be NULL which means that the objfile list has been
- cleared (emptied). */
+/* Callback used to notify Python listeners that OBJFILE has been loaded in
+ to the inferior, OBJFILE will not be NULL. */
static void
python_new_objfile (struct objfile *objfile)
@@ -173,6 +172,7 @@ python_new_objfile (struct objfile *objfile)
if (!gdb_python_initialized)
return;
+ gdb_assert (objfile != nullptr);
gdbpy_enter enter_py (objfile->arch ());
if (emit_new_objfile_event (objfile) < 0)
@@ -16228,6 +16228,7 @@ remote_objfile_changed_check_symbols (program_space *pspace)
static void
remote_new_objfile (struct objfile *objfile)
{
+ gdb_assert (objfile != nullptr);
remote_objfile_changed_check_symbols (objfile->pspace ());
}
@@ -2662,8 +2662,8 @@ reread_symbols (int from_tty)
{
clear_symtab_users (0);
- /* The registry for each objfile was cleared and
- gdb::observers::new_objfile.notify (NULL) has been called by
+ /* The registry for each objfile was cleared and the
+ all_objfiles_removed observer was notified by the call to
clear_symtab_users above. Notify the new files now. */
for (auto iter : new_objfiles)
gdb::observers::new_objfile.notify (iter);
@@ -1700,6 +1700,7 @@ maintenance_print_symbol_cache_statistics (const char *args, int from_tty)
static void
symtab_new_objfile_observer (struct objfile *objfile)
{
+ gdb_assert (objfile != nullptr);
symbol_cache_flush (objfile->pspace ());
}