Fix gdb.base/gcorebg.exp and --program-prefix

When GDB is configured with --program-prefix, we see:

 Running /home/pedro/gdb/src/gdb/testsuite/gdb.base/gcorebg.exp ...
 FAIL: gdb.base/gcorebg.exp: detached=detached: Spawned gcore finished
 FAIL: gdb.base/gcorebg.exp: detached=detached: Core file generated by gcore
 FAIL: gdb.base/gcorebg.exp: detached=standard: Spawned gcore finished
 FAIL: gdb.base/gcorebg.exp: detached=standard: Core file generated by gcore

The problem is here (with --program-prefix=prefix-), from gdb.log:
 gcore: GDB binary (/home/pedro/gdb/build-program-prefix/gdb/testsuite/../../gdb/prefix-gdb) not found
 FAIL: gdb.base/gcorebg.exp: detached=detached: Spawned gcore finished

That is gcore (the script, not the GDB command) trying to run the
installed GDB:

if [ ! -f "$binary_path/@GDB_TRANSFORM_NAME@" ]; then
  echo "gcore: GDB binary (${binary_path}/@GDB_TRANSFORM_NAME@) not found"
  exit 1
fi
...
	"$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \
...

When running the testsuite with the just-built GDB, the GDB binary is
'gdb', not @GDB_TRANSFORM_NAME@.

Fix this by adding a new '-g gdb" option to the 'gcore' script, that
lets you override the GDB binary gcore runs, and then making
gdb.base/gcorebg.exp pass it to gcore.  The GDB binary we're testing
is always in the $GDB global.  This is similar to how it is already
possible to specify GDB's data directory with an option to gcore, and
then gdb.base/gcorebg.exp uses it.

NEWS and documentation changes included.

Approved-by: Kevin Buettner <kevinb@redhat.com>
Change-Id: I6c60fba8768618eeba8d8d03b131dc756b57ee78
This commit is contained in:
Pedro Alves
2025-08-20 10:30:25 +01:00
parent 4d7d74c958
commit b655a89ff5
4 changed files with 31 additions and 8 deletions

View File

@@ -67,6 +67,9 @@ single-inf-arg in qSupported
a -h or --help option, which prints each options and a brief
description.
* The gcore script now has a -g option that lets you specify the GDB
binary invoked by gcore.
* On systems that support linker namespaces, the output of the command
"info sharedlibraries" may add one more column, NS, which identifies the
namespace into which the library was loaded, if more than one namespace

View File

@@ -51790,6 +51790,12 @@ composed as @file{@var{prefix}.@var{pid}}, where @var{pid} is the
process ID of the running program being analyzed by @command{gcore}.
If not specified, @var{prefix} defaults to @var{core}.
@item -g @var{gdb}
Use @var{gdb} as the executable binary to invoke @value{GDBN} for
running the gcore command. This argument is optional, and defaults to
invoking the @value{GDBN} binary that is installed alongside
@command{gcore}.
@item -d @var{directory}
Use @var{directory} as the data directory when invoking @value{GDBN} for running
the gcore command. This argument is optional.

26
gdb/gcore-1.in Normal file → Executable file
View File

@@ -32,12 +32,15 @@ dump_all_cmds=()
data_directory_opt=()
# The GDB binary to run.
gdb_binary=
function print_usage() {
prefix="Usage: $0"
padding=$(printf '%*s' ${#prefix})
echo "$prefix [-h|--help] [-v|--version]"
echo "$padding [-a] [-o prefix] [-d data-directory]"
echo "$padding [-a] [-o prefix] [-g gdb] [-d data-directory]"
echo "$padding pid1 [pid2...pidN]"
}
@@ -55,6 +58,8 @@ function print_help() {
echo " -a Dump all memory mappings."
echo " -o prefix Use 'prefix.pid' as the core file name."
echo " The default prefix is 'core'."
echo " -g gdb The GDB binary to run."
echo " Defaults to GDB installed alongside gcore."
echo " -d dir Pass '--data-directory dir' as an argument"
echo " to GDB."
}
@@ -63,7 +68,7 @@ function print_version() {
echo "GNU gcore (${PKGVERSION}) ${VERSION}"
}
while getopts vhao:d:-: OPT; do
while getopts vhao:g:d:-: OPT; do
if [ "$OPT" = "-" ]; then
OPT="${OPTARG%%=*}"
OPTARG="${OPTARG#'$OPT'}"
@@ -82,6 +87,9 @@ while getopts vhao:d:-: OPT; do
o)
prefix=$OPTARG
;;
g)
gdb_binary="$OPTARG"
;;
d)
data_directory_opt=("--data-directory" "$OPTARG")
;;
@@ -144,10 +152,16 @@ if test "x$binary_path" = x. ; then
fi
fi
if [ -z "$gdb_binary" ]; then
gdb_binary="$binary_path/@GDB_TRANSFORM_NAME@"
fi
gdb_binary_basename=`basename "$gdb_binary"`
# Check if the GDB binary is in the expected path. If not, just
# quit with a message.
if [ ! -f "$binary_path/@GDB_TRANSFORM_NAME@" ]; then
echo "gcore: GDB binary (${binary_path}/@GDB_TRANSFORM_NAME@) not found"
if [ ! -f "$gdb_binary" ]; then
echo "gcore: GDB binary ($gdb_binary) not found"
exit 1
fi
@@ -159,7 +173,7 @@ for pid in "$@"
do
# `</dev/null' to avoid touching interactive terminal if it is
# available but not accessible as GDB would get stopped on SIGTTIN.
"$binary_path/@GDB_TRANSFORM_NAME@" </dev/null \
"$gdb_binary" </dev/null \
"${data_directory_opt[@]}" \
--nx --batch --readnever -iex 'set debuginfod enabled off' \
-ex "set pagination off" -ex "set height 0" -ex "set width 0" \
@@ -169,7 +183,7 @@ do
if [ -r "$prefix.$pid" ] ; then
rc=0
else
echo "@GCORE_TRANSFORM_NAME@: failed to create $prefix.$pid"
echo "$gdb_binary_basename: failed to create $prefix.$pid"
rc=1
break
fi

View File

@@ -46,10 +46,10 @@ proc test_body { detached } {
global binfile
global GCORE
global corefile
global GDB_DATA_DIRECTORY
global GDB GDB_DATA_DIRECTORY
# We can't use gdb_test_multiple here because GDB is not started.
set gcore_cmd $GCORE
set gcore_cmd "$GCORE -g $GDB"
if {$GDB_DATA_DIRECTORY ne ""} {
set gcore_cmd "$gcore_cmd -d '$GDB_DATA_DIRECTORY'"
}