Skip to content

Commit

Permalink
twister: Add logging of stderr for BinaryHandlers
Browse files Browse the repository at this point in the history
In the case where a test causes the test executor to crash, the stderr
is currently lost, making it hard to debug failures. This changes it
so that the process' stderr gets captured to 'handler_stderr.log' for
inspection.

Signed-off-by: Benjamin Gwin <[email protected]>
  • Loading branch information
Benjamin Gwin authored and jhedberg committed May 19, 2024
1 parent 0ca7ef7 commit 723a8fc
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
8 changes: 6 additions & 2 deletions scripts/pylib/twister/twisterlib/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,9 @@ def handle(self, harness):
harness.run_robot_test(command, self)
return

with subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=subprocess.PIPE, cwd=self.build_dir, env=env) as proc:
stderr_log = "{}/handler_stderr.log".format(self.instance.build_dir)
with open(stderr_log, "w+") as stderr_log_fp, subprocess.Popen(command, stdout=subprocess.PIPE,
stderr=stderr_log_fp, cwd=self.build_dir, env=env) as proc:
logger.debug("Spawning BinaryHandler Thread for %s" % self.name)
t = threading.Thread(target=self._output_handler, args=(proc, harness,), daemon=True)
t.start()
Expand All @@ -318,6 +319,9 @@ def handle(self, harness):
t.join()
proc.wait()
self.returncode = proc.returncode
if proc.returncode != 0:
self.instance.status = "error"
self.instance.reason = "BinaryHandler returned {}".format(proc.returncode)
self.try_kill_process_by_pid()

handler_time = time.time() - start_time
Expand Down
4 changes: 4 additions & 0 deletions scripts/pylib/twister/twisterlib/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ def log_info(self, filename, inline_logs, log_testcases=False):
def log_info_file(self, inline_logs):
build_dir = self.instance.build_dir
h_log = "{}/handler.log".format(build_dir)
he_log = "{}/handler_stderr.log".format(build_dir)
b_log = "{}/build.log".format(build_dir)
v_log = "{}/valgrind.log".format(build_dir)
d_log = "{}/device.log".format(build_dir)
Expand All @@ -584,6 +585,8 @@ def log_info_file(self, inline_logs):
self.log_info("{}".format(pytest_log), inline_logs, log_testcases=True)
elif os.path.exists(h_log) and os.path.getsize(h_log) > 0:
self.log_info("{}".format(h_log), inline_logs)
elif os.path.exists(he_log) and os.path.getsize(he_log) > 0:
self.log_info("{}".format(he_log), inline_logs)
elif os.path.exists(d_log) and os.path.getsize(d_log) > 0:
self.log_info("{}".format(d_log), inline_logs)
else:
Expand Down Expand Up @@ -757,6 +760,7 @@ def cleanup_artifacts(self, additional_keep: List[str] = []):
allow = [
os.path.join('zephyr', '.config'),
'handler.log',
'handler_stderr.log',
'build.log',
'device.log',
'recording.csv',
Expand Down

0 comments on commit 723a8fc

Please sign in to comment.