3 # Copyright (C) 2024 Free Software Foundation, Inc.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Print a stack trace of a running process.
19 # Similar to the gcore command, but instead of creating a core file,
20 # we simply have gdb print out the stack backtrace to the terminal.
22 GDB
=${GDB:-$(command -v gdb)}
25 PKGVERSION
=@PKGVERSION@
28 # Find an appropriate awk interpreter if one was not specified
29 # via the environment.
31 if [ -z "$AWK" ]; then
32 for prog
in gawk mawk nawk
awk; do
33 awk_prog
=$
(command -v $prog)
34 test -n "$awk_prog" && break
38 if [ ! -x "$AWK" ]; then
39 echo "$0: could not find usable awk interpreter" 1>&2
43 function print_usage
() {
44 echo "Usage: $0 [-h|--help] [-v|--version] PID"
47 function print_try_help
() {
48 echo "Try '$0 --help' for more information."
51 function print_help
() {
53 echo "Print a stack trace of a running program"
55 echo " -h, --help Print this message then exit."
56 echo " -v, --version Print version information then exit."
59 function print_version
() {
60 echo "GNU gstack (${PKGVERSION}) ${VERSION}"
64 while getopts hv-
: OPT
; do
65 if [ "$OPT" = "-" ]; then
67 OPTARG
="${OPTARG#'$OPT'}"
81 # getopts has already output an error message.
85 echo "$0: unrecognized option '--$OPT'" 1>&2
93 # The sole remaining argument should be the PID of the process
94 # whose backtrace is desired.
102 awk_script
=$
(cat << EOF
128 if (attach_okay == 0)
134 # Run GDB and remove some unwanted noise.
135 "$GDB" --quiet -nx --readnever $GDBARGS <<EOF |
139 set debuginfod enabled off
147 $AWK -- "$awk_script"