Skip to content

Commit 3af6a23

Browse files
chris-laplanterpurdie
authored andcommitted
build: add back the Bash EXIT trap to handle cases that ERR doesn't
ERR unfortunately doesn't trigger for e.g. 'exit 1'. Unfortunately the backtrace we generate in the EXIT trap won't be able to get the correct line number for the first frame. See http://gnu-bash.2382.n7.nabble.com/trap-echo-quot-trap-exit-on-LINENO-quot-EXIT-gt-wrong-linenumber-td3666.html Therefore the metadata-relative backtrace will just not have the first frame. Example: | WARNING: /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057:1 exit 1 from 'exit 1' | WARNING: Backtrace (BB generated script): | #1: myclass_do_something, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057, line ? | #2: do_something, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057, line 156 | #3: actually_fail, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057, line 144 | #4: my_compile_extra, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057, line 146 | #5: do_compile, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057, line 137 | #6: main, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.85057, line 180 | | Backtrace (metadata-relative locations): | #2: do_something, autogenerated, line 2 | #3: actually_fail, /home/laplante/repos/oe-core/meta/recipes-extended/libsolv/libsolv_0.7.14.bb, line 36 | #4: my_compile_extra, /home/laplante/repos/oe-core/meta/recipes-extended/libsolv/libsolv_0.7.14.bb, line 38 | #5: do_compile, autogenerated, line 3 Signed-off-by: Chris Laplante <chris.laplante@agilent.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
1 parent e686794 commit 3af6a23

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

lib/bb/build.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,26 @@ def shell_trap_code():
314314
exit $ret
315315
}
316316
317+
bb_bash_exit_handler() {
318+
ret=$?
319+
if [ "$ret" != 0 ]; then
320+
echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[0]} exit $ret from '$BASH_COMMAND'"
321+
322+
echo "WARNING: Backtrace (BB generated script): "
323+
for ((i=1; i<${#FUNCNAME[@]}; i++)); do
324+
if [ $i -eq 1 ]; then
325+
# TODO: maybe manually track LINENO with a DEBUG trap?
326+
# LINENO / BASH_LINENO for the first frame in an EXIT trap is wrong :/
327+
# http://gnu-bash.2382.n7.nabble.com/trap-echo-quot-trap-exit-on-LINENO-quot-EXIT-gt-wrong-linenumber-td3666.html
328+
echo "\t#$((i)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ?"
329+
else
330+
echo "\t#$((i)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${BASH_LINENO[$((i-1))]}"
331+
fi
332+
done
333+
fi
334+
exit $ret
335+
}
336+
317337
bb_bash_err_handler() {
318338
ret=$?
319339
echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[0]} exit $ret from '$BASH_COMMAND'"
@@ -323,6 +343,7 @@ def shell_trap_code():
323343
echo "\t#$((i-1)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${BASH_LINENO[$((i-1))]}"
324344
done
325345
346+
trap "" EXIT
326347
exit $ret
327348
}
328349
@@ -331,6 +352,7 @@ def shell_trap_code():
331352
set -e
332353
;;
333354
*) trap 'bb_bash_err_handler' ERR
355+
trap 'bb_bash_exit_handler' 0
334356
set -eE
335357
esac
336358
'''

0 commit comments

Comments
 (0)