From 13cf75165a03de421272daf906480ba039defee3 Mon Sep 17 00:00:00 2001 From: AnthonyAxenov Date: Tue, 11 Jan 2022 08:47:35 +0800 Subject: [PATCH] stacktrace.sh --- shell/stacktrace.sh | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 shell/stacktrace.sh diff --git a/shell/stacktrace.sh b/shell/stacktrace.sh new file mode 100644 index 0000000..a89ad96 --- /dev/null +++ b/shell/stacktrace.sh @@ -0,0 +1,19 @@ +#!/bin/bash +# https://gist.github.com/anthonyaxenov/925e2db217730a49f20600520b748039 +# 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 +}