Last active
November 8, 2024 20:05
-
-
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}" | |
} |
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
Here's a version that prints arguments, if
shopt -s extdebug
is in effect: