gdb-add-index: add --help and --version options

Message ID 242b5ae9638501daf254f997a452befb481f1ded.1741892361.git.aburgess@redhat.com
State New
Headers
Series gdb-add-index: add --help and --version options |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gdb_build--master-aarch64 fail Build failed
linaro-tcwg-bot/tcwg_gdb_build--master-arm fail Build failed

Commit Message

Andrew Burgess March 13, 2025, 6:59 p.m. UTC
  Update the gdb-add-index script to offer --help and --version options.

The script currently accepts the argument '-dwarf-5' with a single
leading '-'.  As two '--' is more common for long options, the
preferred argument form is now '--dwarf-5', the docs have been
updated, and the new help text uses this form.

For backward compatibility, the old '-dwarf-5' form is still
accepted.

The new arguments are '--help' or '-h', but I also accept '-help' for
consistency with '-dwarf-5'.  And likewise for the version argument.

Handling of the gdb-add-index script is done basically the same as for
gcore and gstack; we use config.status to create a .in file within the
build directory, which is then processed by the Makefile to create the
final script.

The difference with gdb-add-index is that I left the original script
as gdb/contrib/gdb-add-index.sh rather than renaming it to something
like gdb/contrib/gdb-add-index-1.in, which is how gcore and gstack are
handled (though they are not in the contrib directory).

The reason for this is that the contrib/cc-with-tweaks.sh script looks
for gdb-add-index.sh within the gdb/contrib/ source directory.

As the only reason we process gdb-add-index.sh into the build
directory is to support the PKGVERSION and VERSION variables, allowing
cc-with-tweaks to continue using the unprocessed version seems
harmless, and avoids having to change cc-with-tweaks.sh at all.

I tested that I can still run tests using the cc-with-gdb-index target
board, and that the installed gdb-add-index script correctly shows a
version number when asked.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=32325
---
 gdb/Makefile.in              |  7 ++--
 gdb/NEWS                     |  7 ++--
 gdb/configure                |  3 ++
 gdb/configure.ac             |  1 +
 gdb/contrib/gdb-add-index.sh | 64 +++++++++++++++++++++++++++++++++---
 gdb/doc/gdb.texinfo          | 15 ++++++---
 6 files changed, 84 insertions(+), 13 deletions(-)


base-commit: e8112abd3a1590820462f59af6db7cb1b6928da1
  

Patch

diff --git a/gdb/Makefile.in b/gdb/Makefile.in
index 8c5e2ff7510..bccfd788643 100644
--- a/gdb/Makefile.in
+++ b/gdb/Makefile.in
@@ -2135,7 +2135,7 @@  install-only: $(CONFIG_INSTALL)
 	else \
 	  true ; \
 	fi ; \
-	$(INSTALL_SCRIPT) $(srcdir)/contrib/gdb-add-index.sh \
+	$(INSTALL_SCRIPT) gdb-add-index \
 		$(DESTDIR)$(bindir)/$$transformed_name
 	@$(MAKE) DO=install "DODIRS=$(SUBDIRS)" $(FLAGS_TO_PASS) subdir_do
 
@@ -2355,7 +2355,10 @@  jit-reader.h: $(srcdir)/jit-reader.in config.status
 gstack.in gcore.in: %.in : $(srcdir)/%-1.in config.status
 	$(ECHO_GEN) $(SHELL) config.status $(SILENT_FLAG) $@
 
-gstack gcore: % : %.in version.c
+gdb-add-index.in : $(srcdir)/contrib/gdb-add-index.sh config.status
+	$(ECHO_GEN) $(SHELL) config.status $(SILENT_FLAG) $@
+
+gstack gcore gdb-add-index: % : %.in version.c
 	$(ECHO_GEN) \
 	vv=`grep 'version\[\] = ' version.c | grep -o '".*"' | tr -d \"`; \
 	sed -e "s,@VERSION@,$$vv," $< > $@
diff --git a/gdb/NEWS b/gdb/NEWS
index fae24d0cf61..e3e976a1978 100644
--- a/gdb/NEWS
+++ b/gdb/NEWS
@@ -26,9 +26,10 @@ 
 
 * Linux checkpoint code has been updated to work with multiple inferiors.
 
-* The gcore script now has a -v or --version option, which prints the
-  version number, and then exits.  As well as a -h or --help option,
-  which prints each options and a brief description.
+* The gcore and gdb-add-index scripts now have a -v or --version
+  option, which prints the version number, and then exits.  As well as
+  a -h or --help option, which prints each options and a brief
+  description.
 
 * New commands
 
diff --git a/gdb/configure b/gdb/configure
index 3e990064197..92adb53d4d2 100755
--- a/gdb/configure
+++ b/gdb/configure
@@ -33779,6 +33779,8 @@  ac_config_files="$ac_config_files gcore.in:gcore-1.in"
 
 ac_config_files="$ac_config_files gstack.in:gstack-1.in"
 
+ac_config_files="$ac_config_files gdb-add-index.in:contrib/gdb-add-index.sh"
+
 ac_config_files="$ac_config_files Makefile gdb-gdb.gdb gdb-gdb.py doc/Makefile data-directory/Makefile"
 
 
@@ -34880,6 +34882,7 @@  do
     "nm.h") CONFIG_LINKS="$CONFIG_LINKS nm.h:$GDB_NM_FILE" ;;
     "gcore.in") CONFIG_FILES="$CONFIG_FILES gcore.in:gcore-1.in" ;;
     "gstack.in") CONFIG_FILES="$CONFIG_FILES gstack.in:gstack-1.in" ;;
+    "gdb-add-index.in") CONFIG_FILES="$CONFIG_FILES gdb-add-index.in:contrib/gdb-add-index.sh" ;;
     "Makefile") CONFIG_FILES="$CONFIG_FILES Makefile" ;;
     "gdb-gdb.gdb") CONFIG_FILES="$CONFIG_FILES gdb-gdb.gdb" ;;
     "gdb-gdb.py") CONFIG_FILES="$CONFIG_FILES gdb-gdb.py" ;;
diff --git a/gdb/configure.ac b/gdb/configure.ac
index 4604a2d8e87..eafbf5ac358 100644
--- a/gdb/configure.ac
+++ b/gdb/configure.ac
@@ -2280,6 +2280,7 @@  GDB_AC_TRANSFORM([gdb], [GDB_TRANSFORM_NAME])
 GDB_AC_TRANSFORM([gcore], [GCORE_TRANSFORM_NAME])
 AC_CONFIG_FILES([gcore.in:gcore-1.in])
 AC_CONFIG_FILES([gstack.in:gstack-1.in])
+AC_CONFIG_FILES([gdb-add-index.in:contrib/gdb-add-index.sh])
 AC_CONFIG_FILES([Makefile gdb-gdb.gdb gdb-gdb.py doc/Makefile data-directory/Makefile])
 
 AC_OUTPUT
diff --git a/gdb/contrib/gdb-add-index.sh b/gdb/contrib/gdb-add-index.sh
index 00a9bea5779..94d3ddb9d89 100755
--- a/gdb/contrib/gdb-add-index.sh
+++ b/gdb/contrib/gdb-add-index.sh
@@ -22,16 +22,72 @@  GDB=${GDB:=gdb}
 OBJCOPY=${OBJCOPY:=objcopy}
 READELF=${READELF:=readelf}
 
+PKGVERSION=@PKGVERSION@
+VERSION=@VERSION@
+
 myname="${0##*/}"
 
+function print_usage() {
+    prefix="Usage: $myname"
+    echo "$prefix [-h|--help] [-v|--version] [--dwarf-5] FILENAME"
+}
+
+function print_try_help() {
+    echo "Try '$myname --help' for more information."
+}
+
+function print_help() {
+    print_usage
+    echo
+    echo "Add a .gdb_index section to FILENAME to facilitate faster debug"
+    echo "information loading by GDB."
+    echo
+    echo "  -h, --help         Print this message then exit."
+    echo "  -v, --version      Print version information then exit."
+    echo "  --dwarf-5          Add the DWARF-5 style .debug_names section"
+    echo "                       instead of .gdb_index."
+}
+
+function print_version() {
+    echo "GNU gdb-add-index (${PKGVERSION}) ${VERSION}"
+}
+
 dwarf5=""
-if [ "$1" = "-dwarf-5" ]; then
-    dwarf5="$1"
+
+# Parse options.
+until
+opt=$1
+case ${opt} in
+    --dwarf-5 | -dwarf-5)
+	dwarf5="-dwarf-5"
+	;;
+
+    --help | -help | -h)
+	print_help
+	exit 0
+	;;
+
+    --version | -version | -v)
+	print_version
+	exit 0
+	;;
+
+    -?*)
+	print_try_help 1>&2
+	exit 2
+	;;
+
+    *)
+	# No arguments remaining.
+	;;
+esac
+[ "x${opt:0:1}" != "x-" ]
+do
     shift
-fi
+done
 
 if test $# != 1; then
-    echo "usage: $myname [-dwarf-5] FILE" 1>&2
+    print_try_help
     exit 1
 fi
 
diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo
index 96d624eacac..473431011d1 100644
--- a/gdb/doc/gdb.texinfo
+++ b/gdb/doc/gdb.texinfo
@@ -51643,7 +51643,7 @@  gdb-add-index man
 @c man title gdb-add-index Add index files to speed up GDB
 
 @c man begin SYNOPSIS gdb-add-index
-gdb-add-index [-dwarf-5] @var{filename}
+gdb-add-index [-h | --help] [-v | --version] [--dwarf-5] @var{filename}
 @c man end
 
 @c man begin DESCRIPTION gdb-add-index
@@ -51661,8 +51661,8 @@  gdb-add-index man
 named @code{.debug_*}).
 
 By default @command{gdb-add-index} will add a pre-DWARF 5
-@code{.gdb_index} section to @var{filename}.  With @option{-dwarf-5}
-DWARF 5 sections are added instead.
+@code{.gdb_index} section to @var{filename}.  With @option{--dwarf-5}
+the DWARF 5 section @code{.debug_names} is added instead.
 
 @var{filename} must be writable.
 
@@ -51686,9 +51686,16 @@  gdb-add-index man
 
 @c man begin OPTIONS gdb-add-index
 @table @env
-@item -dwarf-5
+@item --dwarf-5
 Add DWARF 5 sections instead of previous @code{.gdb_index} section.
 
+@item --help
+@itemx -h
+List all options, with brief explanations.
+
+@item --version
+@itemx -v
+Print version information and then exit.
 @end table
 @c man end