feat: add support for backtracing in Bash scripts
This change introduces `backtrace.sh`, a small script providing four
utility functions:
- `dump_stack`, which dumps the call stack
- `dump_process_tree`, which dumps the process tree
- `dump`, which dumps both of the above
- `backtrace`, a backtracing trap handler for errors
This change also introduces an ERR-trapped backtracing handler into the
`utils.sh` script, such that any detected errors will dump the script
state.
An example backtrace looks like the following:
ERROR: Command at /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh:445 exited with error 2:
ERROR:
ERROR: MAKEFLAGS= make -r -j "$parallel" ${keep_going+-k} 5>&1 &> "make.log"
ERROR:
ERROR: Process tree:
ERROR:
ERROR: /bin/bash --init-file /vscode/vscode-server/bin/linux-arm64/6445d93c81ebe42c4cbd7a60712e0b17d9463e97/out/vs/workbench/contrib/terminal/browser/media/shellIntegration-bash.sh
ERROR: \_ bash /workspaces/tf-a/run-tests.sh
ERROR: \_ bash /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh
ERROR: \_ bash /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh
ERROR: | \_ bash /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh
ERROR: | | \_ ps --forest --no-headers --format command
ERROR: | \_ bash /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh
ERROR: \_ bash /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh
ERROR:
ERROR: Call stack:
ERROR:
ERROR: [1]: /workspaces/tf-a/tf-a-ci-scripts/backtrace.sh:78 (backtrace)
ERROR: [2]: /workspaces/tf-a/tf-a-ci-scripts/script/run_local_ci.sh:445 (main)
ERROR: [3]: <unknown>:0 (<unknown>)
Change-Id: Id2c9450400027437fcc082d6849fca8326b33b1c
Signed-off-by: Chris Kay <chris.kay@arm.com>
diff --git a/utils.sh b/utils.sh
index 0c3c143..87ed147 100644
--- a/utils.sh
+++ b/utils.sh
@@ -12,10 +12,13 @@
# ci_root="$(readlink -f "$(dirname "$0")/..")"
#
+set -ETe -o pipefail
+
# Accept root of CI location from $CI_ROOT or $ci_root, in that order
ci_root="${ci_root:-$CI_ROOT}"
ci_root="${ci_root:?}"
+source "${ci_root}/backtrace.sh" && trap backtrace ERR
source "${ci_root}/lava_utils.sh"
# Optionally source a file containing environmental settings.