Skip to content

Instantly share code, notes, and snippets.

@anthonyaxenov
Forked from akostadinov/stack_trace.sh
Last active June 28, 2023 14:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anthonyaxenov/925e2db217730a49f20600520b748039 to your computer and use it in GitHub Desktop.
Save anthonyaxenov/925e2db217730a49f20600520b748039 to your computer and use it in GitHub Desktop.
[SHELL] Print stacktrace
# Original: https://gist.github.com/akostadinov/33bb2606afe1b334169dfbf202991d36
# The difference is that this func outputs stacktrace in reverse order (from top level to lower ones)
function print_stacktrace () {
STACK=""
local i
local stack_size=${#FUNCNAME[@]}
echo "Stacktrace:"
# skip this function and "MAIN non_file_source:0"
for (( i=$stack_size-1; i>=1; 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
echo -e "\n at $func $src:$linen"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment