[bitbake-devel,v2,2/4] build: print a backtrace when a Bash shell function fails

Submitted by Chris Laplante via lists.openembedded.org on Aug. 2, 2020, 2:35 p.m. | Patch ID: 174991

Details

Message ID 20200802143505.88409-3-chris.laplante@agilent.com
State Superseded
Commit 32d036daf5a6b5676150967b0cec5040623141f7
Headers show

Commit Message

Signed-off-by: Chris Laplante <chris.laplante@agilent.com>
---
 lib/bb/build.py | 36 +++++++++++++++++++++++++-----------
 1 file changed, 25 insertions(+), 11 deletions(-)

Patch hide | download patch | download mbox

diff --git a/lib/bb/build.py b/lib/bb/build.py
index 977b02fc..ad445aa9 100644
--- a/lib/bb/build.py
+++ b/lib/bb/build.py
@@ -304,19 +304,33 @@  def exec_func_python(func, d, runfile, cwd=None):
 def shell_trap_code():
     return '''#!/bin/sh\n
 # Emit a useful diagnostic if something fails:
-bb_exit_handler() {
+bb_sh_exit_handler() {
     ret=$?
-    case $ret in
-    0)  ;;
-    *)  case $BASH_VERSION in
-        "") echo "WARNING: exit code $ret from a shell command.";;
-        *)  echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[0]} exit $ret from '$BASH_COMMAND'";;
-        esac
-        exit $ret
-    esac
+    if [ "$ret" != 0 ]; then
+        echo "WARNING: exit code $ret from a shell command."
+    fi
+    exit $ret
 }
-trap 'bb_exit_handler' 0
-set -e
+
+bb_bash_err_handler() {
+    ret=$?
+    echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[0]} exit $ret from '$BASH_COMMAND'"
+
+    echo "WARNING: Backtrace (BB generated script): "
+    for ((i=1; i<${#FUNCNAME[@]}; i++)); do
+        echo "\t#$((i-1)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${BASH_LINENO[$((i-1))]}"
+    done
+
+    exit $ret
+}
+
+case $BASH_VERSION in
+"") trap 'bb_sh_exit_handler' 0
+    set -e
+    ;;
+*)  trap 'bb_bash_err_handler' ERR
+    set -eE
+esac
 '''
 
 def create_progress_handler(func, progress, logfile, d):

Comments

This is a system generated Comment: Patch 174991 was automatically marked as superseded by patch 175194.
This is a system generated Comment: Patch 174991 was automatically marked as superseded by patch 175354.