vsk created this revision.
vsk added reviewers: JDevlieghere, teemperor, mgorny.
Herald added a project: LLDB.
vsk requested review of this revision.

Introduce lldb-env, a tool that prints out the environment variables
needed to launch lldb.

Usually, no environment variables need to be set to launch lldb: in
these cases lldb-env prints nothing.

In a sanitized build, lldb-env prints out any mandatory `DYLD_*` options
needed to set up sanitizer interceptors early. The Darwin-specific logic
is just a starting point. The end goal would be to support launching
lldb on any platform via `export $(path/to/lldb-env); path/to/lldb`. The
current logic is taken/moved from test/API/lit.cfg. As a follow-up, we
could have lldb-dotest use lldb-env as well.

Testing: check-lldb with LLVM_USE_SANITIZER='Address'


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D90276

Files:
  lldb/test/API/lit.cfg.py
  lldb/test/CMakeLists.txt
  lldb/utils/lldb-dotest/CMakeLists.txt
  lldb/utils/lldb-dotest/lldb-env.in

Index: lldb/utils/lldb-dotest/lldb-env.in
===================================================================
--- /dev/null
+++ lldb/utils/lldb-dotest/lldb-env.in
@@ -0,0 +1,37 @@
+#!@Python3_EXECUTABLE@
+import os
+import platform
+import subprocess
+import sys
+
+llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
+compiler = '@CMAKE_CXX_COMPILER@'
+
+def get_dyld_option_for_sanitizer(name):
+    # FIXME: Get the host OS from cmake, instead of assuming macOS.
+    resource_dir = subprocess.check_output(
+        [compiler, '-print-resource-dir']).decode('utf-8').strip()
+    rt_path = os.path.join(resource_dir, 'lib', 'darwin',
+                           'libclang_rt.{0}_osx_dynamic.dylib'.format(name))
+    return 'DYLD_INSERT_LIBRARIES={0}'.format(rt_path)
+
+if __name__ == '__main__':
+    env_vars = []
+
+    asan_enabled = 'Address' in llvm_use_sanitizer
+    tsan_enabled = 'Thread' in llvm_use_sanitizer
+
+    if asan_enabled:
+        env_vars.append("ASAN_OPTIONS='detect_stack_use_after_return=1'")
+
+    if platform.system() == 'Darwin':
+        if asan_enabled:
+            env_vars.append(get_dyld_option_for_sanitizer('asan'))
+
+        if tsan_enabled:
+            env_vars.append(get_dyld_option_for_sanitizer('tsan'))
+
+    if len(env_vars):
+        print('\n'.join(env_vars))
+
+    sys.exit(0)
Index: lldb/utils/lldb-dotest/CMakeLists.txt
===================================================================
--- lldb/utils/lldb-dotest/CMakeLists.txt
+++ lldb/utils/lldb-dotest/CMakeLists.txt
@@ -1,3 +1,7 @@
+# Make lldb-env a custom target.
+add_custom_target(lldb-env)
+set_target_properties(lldb-env PROPERTIES FOLDER "lldb utils")
+
 # Make lldb-dotest a custom target.
 add_custom_target(lldb-dotest)
 add_dependencies(lldb-dotest lldb-test-deps)
@@ -66,6 +70,10 @@
       lldb-dotest.in
       ${config_runtime_output_dir}/lldb-dotest @ONLY
     )
+    configure_file(
+      lldb-env.in
+      ${config_runtime_output_dir}/lldb-env @ONLY
+    )
   endforeach()
 elseif(NOT "${CMAKE_CFG_INTDIR}" STREQUAL ".")
   foreach(LLVM_BUILD_MODE ${CMAKE_CONFIGURATION_TYPES})
@@ -87,6 +95,10 @@
       lldb-dotest.in
       ${LLDB_DOTEST_DIR}/lldb-dotest
       )
+    configure_file(
+      lldb-env.in
+      ${LLDB_DOTEST_DIR}/lldb-env
+      )
   endforeach()
 else()
   set(LLDB_DOTEST_ARGS_CONFIGURED "${LLDB_DOTEST_ARGS}")
@@ -106,4 +118,8 @@
     lldb-dotest.in
     ${LLVM_RUNTIME_OUTPUT_INTDIR}/lldb-dotest
     )
+  configure_file(
+    lldb-env.in
+    ${LLVM_RUNTIME_OUTPUT_INTDIR}/lldb-env
+    )
 endif()
Index: lldb/test/CMakeLists.txt
===================================================================
--- lldb/test/CMakeLists.txt
+++ lldb/test/CMakeLists.txt
@@ -69,6 +69,10 @@
   add_lldb_test_dependency(lldb-framework)
 endif()
 
+if(TARGET lldb-env)
+  add_lldb_test_dependency(lldb-env)
+endif()
+
 # Add dependencies that are not exported targets when building standalone.
 if(NOT LLDB_BUILT_STANDALONE)
   add_lldb_test_dependency(
Index: lldb/test/API/lit.cfg.py
===================================================================
--- lldb/test/API/lit.cfg.py
+++ lldb/test/API/lit.cfg.py
@@ -33,13 +33,6 @@
     raise OSError(errno.ENOTDIR, "%s is not a directory"%path)
 
 
-def find_sanitizer_runtime(name):
-  resource_dir = subprocess.check_output(
-      [config.cmake_cxx_compiler,
-       '-print-resource-dir']).decode('utf-8').strip()
-  return os.path.join(resource_dir, 'lib', 'darwin', name)
-
-
 def find_shlibpath_var():
   if platform.system() in ['Linux', 'FreeBSD', 'NetBSD', 'SunOS']:
     yield 'LD_LIBRARY_PATH'
@@ -102,16 +95,13 @@
     shutil.rmtree(path)
 
 if is_configured('llvm_use_sanitizer'):
-  if 'Address' in config.llvm_use_sanitizer:
-    config.environment['ASAN_OPTIONS'] = 'detect_stack_use_after_return=1'
-    if 'Darwin' in config.host_os and 'x86' in config.host_triple:
-      config.environment['DYLD_INSERT_LIBRARIES'] = find_sanitizer_runtime(
-          'libclang_rt.asan_osx_dynamic.dylib')
-
-  if 'Thread' in config.llvm_use_sanitizer:
-    if 'Darwin' in config.host_os and 'x86' in config.host_triple:
-      config.environment['DYLD_INSERT_LIBRARIES'] = find_sanitizer_runtime(
-          'libclang_rt.tsan_osx_dynamic.dylib')
+  lldb_env_path = os.path.join(os.path.dirname(config.lldb_executable),
+                               'lldb-env')
+  cmd = [config.python_executable, lldb_env_path]
+  env_vars = subprocess.check_output(cmd).decode('utf-8').split('\n')
+  for env_var in env_vars:
+    varname, _, val = env_var.partition('=')
+    config.environment[varname] = val
 
 if 'DYLD_INSERT_LIBRARIES' in config.environment and platform.system() == 'Darwin':
   config.python_executable = find_python_interpreter()
_______________________________________________
lldb-commits mailing list
lldb-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits

Reply via email to