public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: add build mode to reverse order of _initialize function calls
@ 2021-05-17 18:00 Simon Marchi
  2021-05-17 19:26 ` Andrew Burgess
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2021-05-17 18:00 UTC (permalink / raw)
  To: gdb-patches

We should not assume any order in which the _initialize functions get
called.  However, since their order comes from the order of source files
baked into the Makefile, it's possible for such dependencies to creep in
and for us not to notice.

I think that a good way to test for such dependencies is to reverse the
order of the calls.  Add an option in the Makefile to do this.  It can
be used as:

  $ make REVERSE_INIT_FILES=1

before init.c gets generated (if you want to re-generate init.c, either
do a "make clean" or remove stamp-init).

Building with this mode finds one dependency.  In symtab.c, we try to
add the alias "maintenance flush-symbol-cache" for the command
"maintenance flush symbol-cache".  The add_alias_cmd that takes a target
command name is used, which requires a comman lookup to be done.  At
this point, the "maintenance flush" prefix command hasn't been added
(it happens in maint.c), so the lookup fails.  The add_alias_cmd doesn't
add an alias and returns NULL.  That NULL is then passed to
deprecate_cmd, which causes a segfault.

Fix that by using the add_alias_cmd overload that accepts a
cmd_list_element as the target.  This doesn't care whether the prefix
has been created or not, since it doesn't need to do a command lookup.

gdb/ChangeLog:

	* Makefile.in: Add option to reverse INIT_FILES.
	* symtab.c (_initialize_symtab): Pass cmd_list_element to
	add_alias_cmd.

Change-Id: Id8ae7860c822da48455087ea50741c137f237922
---
 gdb/Makefile.in | 17 +++++++++++++++++
 gdb/symtab.c    |  6 +++---
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index ba0cabb0216a..80de9334cd1f 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -1850,6 +1850,23 @@ INIT_FILES = \
 	    $(filter-out init.o version.o %_S.o %_U.o,\
 	      $(COMMON_OBS))))
 
+# This mode can be used to reverse the order in which _initialize functions
+# are called, to verify that there is no hidden dependencies between them.
+#
+# Use with:
+#
+#   $ make REVERSE_INIT_FILES=1
+#
+# _before_ init.c is generated.
+
+ifeq ($(REVERSE_INIT_FILES),1)
+INIT_FILES := \
+	$(shell \
+	    for i in $(INIT_FILES); do \
+		echo $$i; \
+	    done | tac)
+endif
+
 init.c: stamp-init; @true
 stamp-init: $(INIT_FILES) config.status
 	@$(ECHO_INIT_C) echo "Making init.c"
diff --git a/gdb/symtab.c b/gdb/symtab.c
index 9555f94707de..8ad319266772 100644
--- a/gdb/symtab.c
+++ b/gdb/symtab.c
@@ -6897,12 +6897,12 @@ If zero then the symbol cache is disabled."),
 	   _("Print symbol cache statistics for each program space."),
 	   &maintenanceprintlist);
 
-  add_cmd ("symbol-cache", class_maintenance,
+  c = add_cmd ("symbol-cache", class_maintenance,
 	   maintenance_flush_symbol_cache,
 	   _("Flush the symbol cache for each program space."),
 	   &maintenanceflushlist);
-  c = add_alias_cmd ("flush-symbol-cache", "flush symbol-cache",
-		     class_maintenance, 0, &maintenancelist);
+  c = add_alias_cmd ("flush-symbol-cache", c, class_maintenance, 0,
+		     &maintenancelist);
   deprecate_cmd (c, "maintenancelist flush symbol-cache");
 
   gdb::observers::executable_changed.attach (symtab_observer_executable_changed,
-- 
2.31.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-17 20:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-17 18:00 [PATCH] gdb: add build mode to reverse order of _initialize function calls Simon Marchi
2021-05-17 19:26 ` Andrew Burgess
2021-05-17 20:43   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).