From a792a1d55fd28fab9b79ba8d1f8d336a4840e181 Mon Sep 17 00:00:00 2001 From: David Spickett Date: Fri, 15 May 2020 17:02:55 +0100 Subject: tcwg_bmk-build/tcwg_gnu-build: Add traceback printing The line number you get from the previous message is confusing. For example: ERROR at run_step:524 Means that something in the function run_step, which was called from line 524, called exit. Instead, add a Python style traceback that shows the call stack with file, line and the content of that line. (though unfortunatley we can't get the line no. of the exit itself) Example traceback: ERROR Traceback (most recent call last): File: /tmp/foo.sh, line 37 foo # hey! File: /tmp/foo.sh, line 32 bar # hi there File: /tmp/foo.sh, line 1 (trap handler, 'exit' call line is unknown) Change-Id: Id6a2734e2b5ef74d05cd51cdbde9a95e5d5a4b44 --- jenkins-helpers.sh | 21 +++++++++++++++++++++ tcwg_bmk-build.sh | 2 +- tcwg_gnu-build.sh | 2 +- 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/jenkins-helpers.sh b/jenkins-helpers.sh index 83f950ae..b196edca 100644 --- a/jenkins-helpers.sh +++ b/jenkins-helpers.sh @@ -1302,3 +1302,24 @@ EOF run_step_prev_step="$pretty_step" } + +# Print Python style traceback from a trap EXIT +# More readable version of running "caller" +print_traceback () +{ + echo "ERROR Traceback (most recent call last):" + # Show most recent calls last + # >=1 to skip the trap handler entry + # Start from end-2 to skip the top level "main" entry + # which isn't useful + for (( i=${#FUNCNAME[@]}-2 ; i>=1 ; i-- )) ; do + source_file=${BASH_SOURCE[$i+1]} + line_no=${BASH_LINENO[$i]} + echo " File: $source_file, line $line_no" + # Remove leading whitespace to keep indentation readable + echo " $(sed -e "${line_no}!d" -e 's/^[[:space:]]*//' "$source_file")" + done + # We don't know the line number of the exit itself when we trap EXIT + echo " File: ${BASH_SOURCE[0]}, line ${BASH_LINENO[0]}" + echo " (trap handler, 'exit' call line is unknown)" +} diff --git a/tcwg_bmk-build.sh b/tcwg_bmk-build.sh index bd66fe67..39dc474d 100755 --- a/tcwg_bmk-build.sh +++ b/tcwg_bmk-build.sh @@ -108,7 +108,7 @@ verbose2="${verbose2-false}" if $verbose2; then set -x; fi -trap "eval \"echo ERROR at \${FUNCNAME[0]}:\${BASH_LINENO[0]}\"" EXIT +trap print_traceback EXIT # Set start and finish steps for different modes. default_start_at="" diff --git a/tcwg_gnu-build.sh b/tcwg_gnu-build.sh index f88c8e81..24c5c44c 100755 --- a/tcwg_gnu-build.sh +++ b/tcwg_gnu-build.sh @@ -64,7 +64,7 @@ verbose2="${verbose2-false}" if $verbose2; then set -x; fi -trap "eval \"echo ERROR at \${FUNCNAME[0]}:\${BASH_LINENO[0]}\"" EXIT +trap print_traceback EXIT # Set start and finish steps for different modes. default_start_at="" -- cgit v1.2.3