@@ -590,6 +590,7 @@ CONFIG_CLEAN = @CONFIG_CLEAN@
CONFIG_INSTALL = @CONFIG_INSTALL@
CONFIG_UNINSTALL = @CONFIG_UNINSTALL@
HAVE_NATIVE_GCORE_TARGET = @HAVE_NATIVE_GCORE_TARGET@
+HAVE_GSTACK = @HAVE_GSTACK@
CONFIG_SRC_SUBDIR = arch cli dwarf2 mi compile tui unittests guile python \
target nat
@@ -1951,7 +1952,7 @@ generated_files = \
# Flags needed to compile Python code
PYTHON_CFLAGS = @PYTHON_CFLAGS@
-all: gdb$(EXEEXT) $(CONFIG_ALL) gdb-gdb.py gdb-gdb.gdb gcore
+all: gdb$(EXEEXT) $(CONFIG_ALL) gdb-gdb.py gdb-gdb.gdb gcore gstack
@$(MAKE) $(FLAGS_TO_PASS) DO=all "DODIRS=$(SUBDIRS)" subdir_do
# Rule for compiling .c files in the top-level gdb directory.
@@ -2110,6 +2111,19 @@ install-only: $(CONFIG_INSTALL)
$(INSTALL_SCRIPT) gcore \
$(DESTDIR)$(bindir)/$$transformed_name; \
fi
+ if test "x${HAVE_GSTACK}" != x; \
+ then \
+ transformed_name=`t='$(program_transform_name)'; \
+ echo gstack | sed -e "$$t"` ; \
+ if test "x$$transformed_name" = x; then \
+ transformed_name=gstack ; \
+ else \
+ true ; \
+ fi ; \
+ $(SHELL) $(srcdir)/../mkinstalldirs $(DESTDIR)$(bindir) ; \
+ $(INSTALL_SCRIPT) gstack \
+ $(DESTDIR)$(bindir)/$$transformed_name; \
+ fi
transformed_name=`t='$(program_transform_name)'; \
echo gdb-add-index | sed -e "$$t"` ; \
if test "x$$transformed_name" = x; then \
@@ -2154,6 +2168,17 @@ uninstall: force $(CONFIG_UNINSTALL)
fi ; \
rm -f $(DESTDIR)$(bindir)/$$transformed_name; \
fi
+ if test "x$(HAVE_GSTACK)" != x; \
+ then \
+ transformed_name=`t='$(program_transform_name)'; \
+ echo gstack | sed -e "$$t"` ; \
+ if test "x$$transformed_name" = x; then \
+ transformed_name=gstack ; \
+ else \
+ true ; \
+ fi ; \
+ rm -f $(DESTDIR)$(bindir)/$$transformed_name; \
+ fi
transformed_name=`t='$(program_transform_name)'; \
echo gdb-add-index | sed -e "$$t"` ; \
if test "x$$transformed_name" = x; then \
@@ -2265,7 +2290,7 @@ clean mostlyclean: $(CONFIG_CLEAN)
# functionality described is if the distributed files are unmodified.
distclean: clean
@$(MAKE) $(FLAGS_TO_PASS) DO=distclean "DODIRS=$(CLEANDIRS)" subdir_do
- rm -f nm.h config.status config.h stamp-h b jit-reader.h gcore stamp-nmh
+ rm -f nm.h config.status config.h stamp-h b jit-reader.h gcore gstack gstack.in stamp-nmh
rm -f gdb-gdb.py gdb-gdb.gdb
rm -f y.output yacc.acts yacc.tmp y.tab.h
rm -f config.log config.cache
@@ -2326,6 +2351,15 @@ jit-reader.h: $(srcdir)/jit-reader.in config.status
gcore: $(srcdir)/gcore.in config.status
$(ECHO_GEN) $(SHELL) config.status $(SILENT_FLAG) $@
+gstack: gstack.in version.c
+ $(ECHO_GEN) \
+ vv=`grep 'version\[\] = ' version.c | grep -o '".*"' | tr -d \"`; \
+ sed -e "s,@VERSION@,$$vv," $< > $@
+ @chmod +x $@
+
+gstack.in: $(srcdir)/gstack-1.in config.status
+ $(ECHO_GEN) $(SHELL) config.status $(SILENT_FLAG) $@
+
gdb-gdb.py: $(srcdir)/gdb-gdb.py.in config.status
$(ECHO_GEN) $(SHELL) config.status $(SILENT_FLAG) $@
@@ -56,6 +56,9 @@
* The Ada 'Object_Size attribute is now supported.
+* Newly installed $prefix/bin/gstack uses GDB to print stack traces of
+ running processes.
+
* Python API
** Added gdb.record.clear. Clears the trace data of the current recording.
@@ -760,6 +760,7 @@ HAVE_NATIVE_GCORE_TARGET
TARGET_OBS
AMD_DBGAPI_LIBS
AMD_DBGAPI_CFLAGS
+HAVE_GSTACK
ENABLE_BFD_64_BIT_FALSE
ENABLE_BFD_64_BIT_TRUE
subdirs
@@ -11499,7 +11500,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
-#line 11502 "configure"
+#line 11503 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@@ -11605,7 +11606,7 @@ else
lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
lt_status=$lt_dlunknown
cat > conftest.$ac_ext <<_LT_EOF
-#line 11608 "configure"
+#line 11609 "configure"
#include "confdefs.h"
#if HAVE_DLFCN_H
@@ -24974,6 +24975,12 @@ if test x${all_targets} = xtrue; then
fi
fi
+HAVE_GSTACK=0
+if test $gdb_native = yes; then
+ HAVE_GSTACK=1
+fi
+
+
# AMD debugger API support.
@@ -33694,6 +33701,8 @@ fi
ac_config_files="$ac_config_files gcore"
+ac_config_files="$ac_config_files gstack.in:gstack-1.in"
+
ac_config_files="$ac_config_files Makefile gdb-gdb.gdb gdb-gdb.py doc/Makefile data-directory/Makefile"
@@ -34794,6 +34803,7 @@ do
"jit-reader.h") CONFIG_FILES="$CONFIG_FILES jit-reader.h:jit-reader.in" ;;
"nm.h") CONFIG_LINKS="$CONFIG_LINKS nm.h:$GDB_NM_FILE" ;;
"gcore") CONFIG_FILES="$CONFIG_FILES gcore" ;;
+ "gstack.in") CONFIG_FILES="$CONFIG_FILES gstack.in:gstack-1.in" ;;
"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" ;;
@@ -36340,6 +36350,7 @@ _LT_EOF
done ;;
"nm.h":L) echo > stamp-nmh ;;
"gcore":F) chmod +x gcore ;;
+ "gstack.in":F) chmod +x gstack.in ;;
esac
done # for ac_tag
@@ -261,6 +261,12 @@ if test x${all_targets} = xtrue; then
fi
fi
+HAVE_GSTACK=0
+if test $gdb_native = yes; then
+ HAVE_GSTACK=1
+fi
+AC_SUBST(HAVE_GSTACK)
+
# AMD debugger API support.
AC_ARG_WITH([amd-dbgapi],
@@ -2231,6 +2237,7 @@ GDB_AC_SELFTEST([
GDB_AC_TRANSFORM([gdb], [GDB_TRANSFORM_NAME])
GDB_AC_TRANSFORM([gcore], [GCORE_TRANSFORM_NAME])
AC_CONFIG_FILES([gcore], [chmod +x gcore])
+AC_CONFIG_FILES([gstack.in:gstack-1.in], [chmod +x gstack.in])
AC_CONFIG_FILES([Makefile gdb-gdb.gdb gdb-gdb.py doc/Makefile data-directory/Makefile])
AC_OUTPUT
@@ -188,7 +188,7 @@ TEXI2POD = perl $(srcdir)/../../etc/texi2pod.pl \
POD2MAN = pod2man --center="GNU Development Tools"
# List of man pages generated from gdb.texi
-MAN1S = gdb.1 gdbserver.1 gcore.1 gdb-add-index.1
+MAN1S = gdb.1 gdbserver.1 gcore.1 gstack.1 gdb-add-index.1
MAN5S = gdbinit.5
MANS = $(MAN1S) $(MAN5S)
@@ -199,6 +199,7 @@ POD_FILE_TMPS = $(patsubst %.1,%.pod,$(MAN1S)) \
HAVE_NATIVE_GCORE_TARGET = @HAVE_NATIVE_GCORE_TARGET@
HAVE_NATIVE_GCORE_HOST = @HAVE_NATIVE_GCORE_HOST@
+HAVE_GSTACK = @HAVE_GSTACK@
###
@@ -339,6 +340,10 @@ install-man1: $(MAN1S)
-a "$$p" = gcore.1; then \
continue; \
fi; \
+ if test "x$(HAVE_GSTACK)" = x \
+ -a "$$p" = gstack.1; then \
+ continue; \
+ fi; \
if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \
f=`echo $$p | sed -e 's|^.*/||' -e '$(transform)'`; \
echo " $(INSTALL_DATA) '$$d$$p' '$(DESTDIR)$(man1dir)/$$f'"; \
@@ -363,6 +368,10 @@ uninstall-man1:
-a "$$i" = gcore.1; then \
continue; \
fi; \
+ if test "x$(HAVE_GSTACK)" = x \
+ -a "$$i" = gstack.1; then \
+ continue; \
+ fi; \
echo "$$i"; \
done | \
sed -n '/\.1[a-z]*$$/p'; \
@@ -50580,6 +50580,7 @@ Show the current verbosity setting.
* gdb man:: The GNU Debugger man page
* gdbserver man:: Remote Server for the GNU Debugger man page
* gcore man:: Generate a core file of a running program
+* gstack man:: Print a stack trace of a running program
* gdbinit man:: gdbinit scripts
* gdb-add-index man:: Add index files to speed up GDB
@end menu
@@ -51262,6 +51263,53 @@ Richard M. Stallman and Roland H. Pesch, July 1991.
@end ifset
@c man end
+@node gstack man
+@heading gstack
+
+@c man title gstack Print a stack trace of a running program
+
+@format
+@c man begin SYNOPSIS gstack
+gstack [-h | --help] [-v | --version] @var{pid}
+@c man end
+@end format
+
+@c man begin DESCRIPTION gstack
+Print a stack trace of a running program with process ID @var{pid}. If the process
+is multi-threaded, @command{gstack} outputs backtraces for every thread which exists
+in the process.
+@c man end
+
+@c man begin OPTIONS gstack
+@table @env
+@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
+
+@c man begin SEEALSO gstack
+@ifset man
+The full documentation for @value{GDBN} is maintained as a Texinfo manual.
+If the @code{info} and @code{gdb} programs and @value{GDBN}'s Texinfo
+documentation are properly installed at your site, the command
+
+@smallexample
+info gdb
+@end smallexample
+
+@noindent
+should give you access to the complete manual.
+
+@cite{Using GDB: A Guide to the GNU Source-Level Debugger},
+Richard M. Stallman and Roland H. Pesch, July 1991.
+@end ifset
+@c man end
+
@node gdbinit man
@heading gdbinit
new file mode 100755
@@ -0,0 +1,126 @@
+#!/usr/bin/env bash
+
+# Copyright (C) 2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+# Print a stack trace of a running process.
+# Similar to the gcore command, but instead of creating a core file,
+# we simply have gdb print out the stack backtrace to the terminal.
+
+GDB=${GDB:-$(command -v gdb)}
+GDBARGS=${GDBARGS:-}
+PKGVERSION=@PKGVERSION@
+VERSION=@VERSION@
+
+function print_usage() {
+ echo "Usage: $0 [-h|--help] [-v|--version] PID" 1>&2
+}
+
+function print_try_help() {
+ echo "Try '$0 --help' for more information."
+}
+
+function print_help() {
+ print_usage
+ echo "Print a stack trace of a running program"
+ echo
+ echo " -h, --help Print this message then exit."
+ echo " -v, --version Print version information then exit."
+}
+
+function print_version() {
+ echo "GNU gstack (${PKGVERSION}) ${VERSION}"
+}
+
+# Parse options.
+while getopts hv-: OPT; do
+ if [ "$OPT" = "-" ]; then
+ OPT="${OPTARG%%=*}"
+ OPTARG="${OPTARG#'$OPT'}"
+ OPTARG="${OPTARG#=}"
+ fi
+
+ case "$OPT" in
+ h | help)
+ print_help
+ exit 0
+ ;;
+ v | version)
+ print_version
+ exit 0
+ ;;
+ \?)
+ # getopts has already output an error message.
+ print_try_help
+ exit 2 ;;
+ *)
+ echo "$0: unrecognized option '--$OPT'" >&2
+ print_try_help
+ exit 2
+ ;;
+ esac
+done
+shift $((OPTIND-1))
+
+# The sole remaining argument should be the PID of the process
+# whose backtrace is desired.
+if [ $# -ne 1 ]; then
+ print_usage
+ exit 1
+fi
+
+PID=$1
+
+awk_script=$(cat << EOF
+BEGIN {
+ first=1
+ attach_okay=0
+}
+
+/ATTACHED/ {
+ attach_okay=1
+}
+
+/^#/ {
+ if (attach_okay) {
+ print \$0
+ }
+}
+
+/^Thread/ {
+ if (attach_okay) {
+ if (!first)
+ print ""
+ first=0
+ print \$0
+ }
+}
+EOF
+ )
+
+# Run GDB and remove some unwanted noise.
+$GDB --quiet -nx $GDBARGS <<EOF |
+set width 0
+set height 0
+set pagination no
+set debuginfod enabled off
+define attach-bt
+attach \$arg0
+echo "ATTACHED"
+thread apply all bt
+end
+attach-bt $PID
+EOF
+awk -- "$awk_script"
new file mode 100644
@@ -0,0 +1,32 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2024 Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+#include <unistd.h>
+#include <string.h>
+
+int
+main (void)
+{
+ const char msg[] = "looping\n";
+
+ /* Output a simple string for the expect script to monitor us. */
+ write (1, msg, strlen (msg));
+
+ for (;;) ;
+
+ return 0;
+}
new file mode 100644
@@ -0,0 +1,78 @@
+# Copyright (C) 2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+require !gdb_protocol_is_remote
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile {debug}] == -1} {
+ return -1
+}
+
+set command "$binfile"
+set res [remote_spawn host $command]
+if { ![gdb_assert { ![expr {$res < 0 || $res == ""}] } "spawn inferior"] } {
+ return
+}
+
+# The spawn id of the test inferior.
+set test_spawn_id $res
+
+# Wait for the spawned program to loop.
+set test "wait for inferior to loop"
+gdb_expect {
+ -re "looping\r\n" {
+ pass $test
+ }
+ eof {
+ fail "$test (eof)"
+ return
+ }
+ timeout {
+ fail "$test (timeout)"
+ return
+ }
+}
+
+# The test case uses a very simple notification not to get caught by attach on
+# exiting the function.
+
+set test "spawn gstack"
+set pid [spawn_id_get_pid $test_spawn_id]
+set command "sh -c GDB=$GDB\\ GDBARGS=-data-directory\\\\\\ $GDB_DATA_DIRECTORY\\ sh\\ ${srcdir}/../contrib/gstack.sh\\ $pid\\;echo\\ GSTACK-END"
+set res [remote_spawn host $command]
+if { ![gdb_assert { ![expr {$res < 0 || $res == ""}] } $test] } {
+ return
+}
+
+set test "got backtrace"
+gdb_test_multiple "" $test {
+ -i "$res" -re "^#0 +(0x\[0-9a-f\]+ in )?\\.?func \\(\\) at \[^\r\n\]*\r\n#1 +0x\[0-9a-f\]+ in \\.?main \\(\\) at \[^\r\n\]*\r\nGSTACK-END\r\n\$" {
+ pass $test
+ exp_continue
+ }
+
+ eof {
+ set result [wait -i $res]
+ verbose $result
+
+ gdb_assert { [lindex $result 2] == 0 } "gstack exits with no error"
+ gdb_assert { [lindex $result 3] == 0 } "gstack's exit status is 0"
+
+ remote_close host
+ }
+}
+
+# Kill the test inferior.
+kill_wait_spawned_process $test_spawn_id