[gdb/cli] Fix return from frame containing inline frame
[binutils-gdb.git] / gdb / gstack-1.in
blob5e413290c9a06ba3c87b7f9aa127a6bba5c0218e
1 #!/usr/bin/env bash
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)}
23 GDBARGS=${GDBARGS:-}
24 AWK=${AWK:-}
25 PKGVERSION=@PKGVERSION@
26 VERSION=@VERSION@
28 # Find an appropriate awk interpreter if one was not specified
29 # via the environment.
30 awk_prog=""
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
35 done
36 AWK="$awk_prog"
38 if [ ! -x "$AWK" ]; then
39 echo "$0: could not find usable awk interpreter" 1>&2
40 exit 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() {
52 print_usage
53 echo "Print a stack trace of a running program"
54 echo
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}"
63 # Parse options.
64 while getopts hv-: OPT; do
65 if [ "$OPT" = "-" ]; then
66 OPT="${OPTARG%%=*}"
67 OPTARG="${OPTARG#'$OPT'}"
68 OPTARG="${OPTARG#=}"
71 case "$OPT" in
72 h | help)
73 print_help
74 exit 0
76 v | version)
77 print_version
78 exit 0
80 \?)
81 # getopts has already output an error message.
82 print_try_help 1>&2
83 exit 2 ;;
85 echo "$0: unrecognized option '--$OPT'" 1>&2
86 print_try_help 1>&2
87 exit 2
89 esac
90 done
91 shift $((OPTIND-1))
93 # The sole remaining argument should be the PID of the process
94 # whose backtrace is desired.
95 if [ $# -ne 1 ]; then
96 print_usage 1>&2
97 exit 1
100 PID=$1
102 awk_script=$(cat << EOF
103 BEGIN {
104 first=1
105 attach_okay=0
108 /ATTACHED/ {
109 attach_okay=1
112 /^#/ {
113 if (attach_okay) {
114 print \$0
118 /^Thread/ {
119 if (attach_okay) {
120 if (first == 0)
121 print ""
122 first=0
123 print \$0
127 END {
128 if (attach_okay == 0)
129 exit 2
134 # Run GDB and remove some unwanted noise.
135 "$GDB" --quiet -nx --readnever $GDBARGS <<EOF |
136 set width 0
137 set height 0
138 set pagination no
139 set debuginfod enabled off
140 define attach-bt
141 attach \$arg0
142 echo "ATTACHED"
143 thread apply all bt
145 attach-bt $PID
147 $AWK -- "$awk_script"