Coverage: Add llvm-symbolizer to path for symbolized stacks

[email protected],[email protected]

Bug: 784464, 841513
Change-Id: Ia592f2c0145d9117e6c59d14c6b1685d104429c0
Reviewed-on: https://chromium-review.googlesource.com/1053161
Reviewed-by: Max Moroz <[email protected]>
Commit-Queue: Abhishek Arya <[email protected]>
Cr-Commit-Position: refs/heads/master@{#557446}
diff --git a/tools/code_coverage/coverage.py b/tools/code_coverage/coverage.py
index 962f8515..71957a1 100755
--- a/tools/code_coverage/coverage.py
+++ b/tools/code_coverage/coverage.py
@@ -91,8 +91,9 @@
 
 # Absolute path to the code coverage tools binary.
 LLVM_BUILD_DIR = clang_update.LLVM_BUILD_DIR
-LLVM_COV_PATH = os.path.join(LLVM_BUILD_DIR, 'bin', 'llvm-cov')
-LLVM_PROFDATA_PATH = os.path.join(LLVM_BUILD_DIR, 'bin', 'llvm-profdata')
+LLVM_BIN_DIR = os.path.join(LLVM_BUILD_DIR, 'bin')
+LLVM_COV_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-cov')
+LLVM_PROFDATA_PATH = os.path.join(LLVM_BIN_DIR, 'llvm-profdata')
 
 # Build directory, the value is parsed from command line arguments.
 BUILD_DIR = None
@@ -130,7 +131,8 @@
 LOGS_DIR_NAME = 'logs'
 
 # Used to extract a mapping between directories and components.
-COMPONENT_MAPPING_URL = 'https://storage.googleapis.com/chromium-owners/component_map.json'
+COMPONENT_MAPPING_URL = (
+    'https://storage.googleapis.com/chromium-owners/component_map.json')
 
 # Caches the results returned by _GetBuildArgs, don't use this variable
 # directly, call _GetBuildArgs instead.
@@ -400,6 +402,16 @@
     return 'mac'
 
 
+def _GetPathWithLLVMSymbolizerDir():
+  """Add llvm-symbolizer directory to path for symbolized stacks."""
+  path = os.getenv('PATH')
+  dirs = path.split(os.pathsep)
+  if LLVM_BIN_DIR in dirs:
+    return path
+
+  return path + os.pathsep + LLVM_BIN_DIR
+
+
 def _GetTargetOS():
   """Returns the target os specified in args.gn file.
 
@@ -1009,10 +1021,15 @@
     output = subprocess.check_output(
         shlex.split(command),
         stderr=subprocess.STDOUT,
-        env={'LLVM_PROFILE_FILE': expected_profraw_file_path})
+        env={
+            'LLVM_PROFILE_FILE': expected_profraw_file_path,
+            'PATH': _GetPathWithLLVMSymbolizerDir()
+        })
   except subprocess.CalledProcessError as e:
     output = e.output
-    logging.warning('Command: "%s" exited with non-zero return code.', command)
+    logging.warning(
+        'Command: "%s" exited with non-zero return code. Output:\n%s', command,
+        output)
 
   return output
 
@@ -1045,7 +1062,10 @@
   try:
     output = subprocess.check_output(
         shlex.split(command),
-        env={'LLVM_PROFILE_FILE': iossim_profraw_file_path})
+        env={
+            'LLVM_PROFILE_FILE': iossim_profraw_file_path,
+            'PATH': _GetPathWithLLVMSymbolizerDir()
+        })
   except subprocess.CalledProcessError as e:
     # iossim emits non-zero return code even if tests run successfully, so
     # ignore the return code.