Skip to content

Commit ae1aa4e

Browse files
chris-laplanterpurdie
authored andcommitted
build: print a backtrace with the original metadata locations of Bash shell funcs
Leverage the comments that emit_var writes and the backtrace that the shell func writes to generate an additional metadata-relative backtrace. This will help the user troubleshoot shell funcs much more easily. Example: | WARNING: /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.68955:171 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.68955, line 171 | #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.68955, line 166 | #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.68955, line 153 | #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.68955, line 155 | #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.68955, line 141 | #6: main, /home/laplante/repos/oe-core/build/tmp-glibc/work/core2-64-oe-linux/libsolv/0.7.14-r0/temp/run.do_compile.68955, line 184 | | Backtrace (metadata-relative locations): | #1: myclass_do_something, /home/laplante/repos/oe-core/meta/classes/myclass.bbclass, line 2 | #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 ERROR: Task (/home/laplante/repos/oe-core/meta/recipes-extended/libsolv/libsolv_0.7.14.bb:do_compile) failed with exit code '1' NOTE: Tasks Summary: Attempted 542 tasks of which 541 didn't need to be rerun and 1 failed. Summary: 1 task failed: /home/laplante/repos/oe-core/meta/recipes-extended/libsolv/libsolv_0.7.14.bb:do_compile Summary: There was 1 ERROR message shown, returning a non-zero exit code. Signed-off-by: Chris Laplante <chris.laplante@agilent.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
1 parent 8366e2a commit ae1aa4e

File tree

2 files changed

+60
-1
lines changed

2 files changed

+60
-1
lines changed

lib/bb/build.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
import sys
1717
import logging
1818
import glob
19+
import itertools
1920
import time
21+
import re
2022
import stat
2123
import bb
2224
import bb.msg
@@ -495,6 +497,62 @@ def readfifo(data):
495497
bb.debug(2, "Executing shell function %s" % func)
496498
with open(os.devnull, 'r+') as stdin, logfile:
497499
bb.process.run(cmd, shell=False, stdin=stdin, log=logfile, extrafiles=[(fifo,readfifo)])
500+
except bb.process.ExecutionError as exe:
501+
# Find the backtrace that the shell trap generated
502+
backtrace_marker_regex = re.compile(r"WARNING: Backtrace \(BB generated script\)")
503+
stdout_lines = (exe.stdout or "").split("\n")
504+
backtrace_start_line = None
505+
for i, line in enumerate(reversed(stdout_lines)):
506+
if backtrace_marker_regex.search(line):
507+
backtrace_start_line = len(stdout_lines) - i
508+
break
509+
510+
# Read the backtrace frames, starting at the location we just found
511+
backtrace_entry_regex = re.compile(r"#(?P<frameno>\d+): (?P<funcname>[^\s]+), (?P<file>.+?), line ("
512+
r"?P<lineno>\d+)")
513+
backtrace_frames = []
514+
if backtrace_start_line:
515+
for line in itertools.islice(stdout_lines, backtrace_start_line, None):
516+
match = backtrace_entry_regex.search(line)
517+
if match:
518+
backtrace_frames.append(match.groupdict())
519+
520+
with open(runfile, "r") as script:
521+
script_lines = [line.rstrip() for line in script.readlines()]
522+
523+
# For each backtrace frame, search backwards in the script (from the line number called out by the frame),
524+
# to find the comment that emit_vars injected when it wrote the script. This will give us the metadata
525+
# filename (e.g. .bb or .bbclass) and line number where the shell function was originally defined.
526+
script_metadata_comment_regex = re.compile(r"# line: (?P<lineno>\d+), file: (?P<file>.+)")
527+
better_frames = []
528+
# Skip the very last frame since it's just the call to the shell task in the body of the script
529+
for frame in backtrace_frames[:-1]:
530+
# Check whether the frame corresponds to a function defined in the script vs external script.
531+
if os.path.samefile(frame["file"], runfile):
532+
# Search backwards from the frame lineno to locate the comment that BB injected
533+
i = int(frame["lineno"]) - 1
534+
while i >= 0:
535+
match = script_metadata_comment_regex.match(script_lines[i])
536+
if match:
537+
# Calculate the relative line in the function itself
538+
relative_line_in_function = int(frame["lineno"]) - i - 2
539+
# Calculate line in the function as declared in the metadata
540+
metadata_function_line = relative_line_in_function + int(match["lineno"])
541+
better_frames.append("#{frameno}: {funcname}, {file}, line {lineno}".format(
542+
frameno=frame["frameno"],
543+
funcname=frame["funcname"],
544+
file=match["file"],
545+
lineno=metadata_function_line
546+
))
547+
break
548+
i -= 1
549+
else:
550+
better_frames.append("#{frameno}: {funcname}, {file}, line {lineno}".format(**frame))
551+
552+
if better_frames:
553+
better_frames = ("\t{0}".format(frame) for frame in better_frames)
554+
exe.extra_message = "\nBacktrace (metadata-relative locations):\n{0}".format("\n".join(better_frames))
555+
raise
498556
finally:
499557
os.unlink(fifopath)
500558

lib/bb/process.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self, command, exitcode, stdout = None, stderr = None):
4141
self.exitcode = exitcode
4242
self.stdout = stdout
4343
self.stderr = stderr
44+
self.extra_message = None
4445

4546
def __str__(self):
4647
message = ""
@@ -51,7 +52,7 @@ def __str__(self):
5152
if message:
5253
message = ":\n" + message
5354
return (CmdError.__str__(self) +
54-
" with exit code %s" % self.exitcode + message)
55+
" with exit code %s" % self.exitcode + message + (self.extra_message or ""))
5556

5657
class Popen(subprocess.Popen):
5758
defaults = {

0 commit comments

Comments
 (0)