diff options
author | Chris Laplante <chris.laplante@agilent.com> | 2020-08-14 16:55:56 -0400 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-08-15 11:44:20 +0100 |
commit | b1cd7723c4d81081c6d1935be7373c7f06ae1b97 (patch) | |
tree | 1f8d1b21b4649cb74a6354458335b601112b4593 /bitbake/lib/bb | |
parent | bd1da2aca0bc26720f438a9c1d54f5b04084da4e (diff) | |
download | poky-b1cd7723c4d81081c6d1935be7373c7f06ae1b97.tar.gz |
bitbake: build: print a backtrace when a Bash shell function fails
The trick here is to use a DEBUG trap to record the line
number of each command as they execute. This is so we
can report the real line number of the failing command,
which is otherwise not possible in a Bash EXIT trap. See
http://gnu-bash.2382.n7.nabble.com/trap-echo-quot-trap-exit-on-LINENO-quot-EXIT-gt-wrong-linenumber-td3666.html
(Bitbake rev: 8366e2ad5fdad3972766b40b23188908330067ee)
Signed-off-by: Chris Laplante <chris.laplante@agilent.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'bitbake/lib/bb')
-rw-r--r-- | bitbake/lib/bb/build.py | 61 |
1 files changed, 50 insertions, 11 deletions
diff --git a/bitbake/lib/bb/build.py b/bitbake/lib/bb/build.py index 977b02fc63..98cbc48cd4 100644 --- a/bitbake/lib/bb/build.py +++ b/bitbake/lib/bb/build.py | |||
@@ -303,20 +303,59 @@ def exec_func_python(func, d, runfile, cwd=None): | |||
303 | 303 | ||
304 | def shell_trap_code(): | 304 | def shell_trap_code(): |
305 | return '''#!/bin/sh\n | 305 | return '''#!/bin/sh\n |
306 | __BITBAKE_LAST_LINE=0 | ||
307 | |||
306 | # Emit a useful diagnostic if something fails: | 308 | # Emit a useful diagnostic if something fails: |
307 | bb_exit_handler() { | 309 | bb_sh_exit_handler() { |
308 | ret=$? | 310 | ret=$? |
309 | case $ret in | 311 | if [ "$ret" != 0 ]; then |
310 | 0) ;; | 312 | echo "WARNING: exit code $ret from a shell command." |
311 | *) case $BASH_VERSION in | 313 | fi |
312 | "") echo "WARNING: exit code $ret from a shell command.";; | 314 | exit $ret |
313 | *) echo "WARNING: ${BASH_SOURCE[0]}:${BASH_LINENO[0]} exit $ret from '$BASH_COMMAND'";; | ||
314 | esac | ||
315 | exit $ret | ||
316 | esac | ||
317 | } | 315 | } |
318 | trap 'bb_exit_handler' 0 | 316 | |
319 | set -e | 317 | bb_bash_exit_handler() { |
318 | ret=$? | ||
319 | trap "" DEBUG | ||
320 | if [ "$ret" != 0 ]; then | ||
321 | echo "WARNING: ${BASH_SOURCE[0]}:${__BITBAKE_LAST_LINE} exit $ret from '$1'" | ||
322 | |||
323 | echo "WARNING: Backtrace (BB generated script): " | ||
324 | for i in $(seq 1 $((${#FUNCNAME[@]} - 1))); do | ||
325 | if [ "$i" -eq 1 ]; then | ||
326 | echo -e "\t#$((i)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${__BITBAKE_LAST_LINE}" | ||
327 | else | ||
328 | echo -e "\t#$((i)): ${FUNCNAME[$i]}, ${BASH_SOURCE[$((i-1))]}, line ${BASH_LINENO[$((i-1))]}" | ||
329 | fi | ||
330 | done | ||
331 | fi | ||
332 | exit $ret | ||
333 | } | ||
334 | |||
335 | bb_bash_debug_handler() { | ||
336 | local line=${BASH_LINENO[0]} | ||
337 | # For some reason the DEBUG trap trips with lineno=1 when scripts exit; ignore it | ||
338 | if [ "$line" -eq 1 ]; then | ||
339 | return | ||
340 | fi | ||
341 | |||
342 | # Track the line number of commands as they execute. This is so we can have access to the failing line number | ||
343 | # in the EXIT trap. See http://gnu-bash.2382.n7.nabble.com/trap-echo-quot-trap-exit-on-LINENO-quot-EXIT-gt-wrong-linenumber-td3666.html | ||
344 | if [ "${FUNCNAME[1]}" != "bb_bash_exit_handler" ]; then | ||
345 | __BITBAKE_LAST_LINE=$line | ||
346 | fi | ||
347 | } | ||
348 | |||
349 | case $BASH_VERSION in | ||
350 | "") trap 'bb_sh_exit_handler' 0 | ||
351 | set -e | ||
352 | ;; | ||
353 | *) trap 'bb_bash_exit_handler "$BASH_COMMAND"' 0 | ||
354 | trap 'bb_bash_debug_handler' DEBUG | ||
355 | set -eE | ||
356 | shopt -s extdebug | ||
357 | ;; | ||
358 | esac | ||
320 | ''' | 359 | ''' |
321 | 360 | ||
322 | def create_progress_handler(func, progress, logfile, d): | 361 | def create_progress_handler(func, progress, logfile, d): |