Last active
March 3, 2024 19:50
-
-
Save akostadinov/33bb2606afe1b334169dfbf202991d36 to your computer and use it in GitHub Desktop.
Get stack trace in Bash shell script/program.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# LICENSE: MIT, wtfpl or whatever OSS license you like | |
function get_stack () { | |
STACK="" | |
local i message="${1:-""}" | |
local stack_size=${#FUNCNAME[@]} | |
# to avoid noise we start with 1 to skip the get_stack function | |
for (( i=1; i<$stack_size; i++ )); do | |
local func="${FUNCNAME[$i]}" | |
[ x$func = x ] && func=MAIN | |
local linen="${BASH_LINENO[$(( i - 1 ))]}" | |
local src="${BASH_SOURCE[$i]}" | |
[ x"$src" = x ] && src=non_file_source | |
STACK+=$'\n'" at: "$func" "$src" "$linen | |
done | |
STACK="${message}${STACK}" | |
} |
Here's a version that prints arguments, if shopt -s extdebug
is in effect:
function stack_trace() {
local -a stack=()
local stack_size=${#FUNCNAME[@]}
local -i start=${1:-1}
local -i max_frames=${2:-$stack_size}
((max_frames > stack_size)) && max_frames=$stack_size
local -i i
local -i max_funcname=0
local -i stack_size_len=${#max_frames}
local -i max_filename_len=0
local -i max_line_len=0
# to avoid noise we start with 1 to skip the stack function
for (( i = start; i < max_frames; i++ )); do
local func="${FUNCNAME[$i]:-(top level)}"
((${#func} > max_funcname)) && max_funcname=${#func}
local src="${BASH_SOURCE[$i]:-(no file)}"
# Line number is used as a string here, not an int,
# since we want the length of it as a string.
local line="${BASH_LINENO[$(( i - 1 ))]}"
((${#src} > max_filename_len)) && max_filename_len=${#src}
((${#line} > max_line_len)) && max_line_len=${#line}
done
local stack_frame_str=" (%${stack_size_len}d) %${max_filename_len}s:%-${max_line_len}d %${max_funcname}s%s"
local -i arg_count=${BASH_ARGC[0]}
for (( i = start; i < max_frames; i++ )); do
local func="${FUNCNAME[$i]:-(top level)}"
local -i line="${BASH_LINENO[$(( i - 1 ))]}"
local src="${BASH_SOURCE[$i]:-(no file)}"
local -i frame_arg_count=${BASH_ARGC[$i]}
local argstr=
if ((frame_arg_count > 0)) ; then
local -i j
for ((j = arg_count + frame_arg_count - 1; j >= arg_count; j--)) ; do
argstr+=" ${BASH_ARGV[$j]}"
done
fi
# We need a dynamically generated string to get the columns correct.
# shellcheck disable=SC2059
stack+=("$(printf "$stack_frame_str" "$((i - start))" "$src" "$line" "$func" "${argstr:+ $argstr}")")
arg_count=$((arg_count + frame_arg_count))
done
(IFS=$'\n'; echo "${stack[*]}")
}
I'm impressed with all improvements I see here. Perhaps somebody would submit their version as an improvement to https://github.com/olivergondza/bash-strict-mode/
P.S. when printing stack traces, it's better to use STDERR
I did it that way so that higher level code can more conveniently capture the output; it can also redirect it to stderr. Either way works, certainly.
I find the arguments to be invaluable for debugging. I have a very complex script that itself provides an API, and there's a lot of control flow complexity. Ideally it would be written in Go or something, but none of those languages provide the convenience of shell scripting for running other commands.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A complete example with @RobertKrawitz solution :
which outputs: