[all-commits] [llvm/llvm-project] bbef51: [lldb] make it easier to find LLDB's python

lawrence-danna-apple via All-commits all-commits at lists.llvm.org
Wed Nov 10 10:34:11 PST 2021


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: bbef51eb43c2e8f8e36fbbc0d0b4cca75b6f0863
      https://github.com/llvm/llvm-project/commit/bbef51eb43c2e8f8e36fbbc0d0b4cca75b6f0863
  Author: Lawrence D'Anna <lawrence_danna at apple.com>
  Date:   2021-11-10 (Wed, 10 Nov 2021)

  Changed paths:
    M lldb/bindings/interface/SBDebugger.i
    M lldb/bindings/python/CMakeLists.txt
    A lldb/bindings/python/lldb-python
    M lldb/docs/man/lldb.rst
    M lldb/include/lldb/API/SBDebugger.h
    M lldb/include/lldb/Interpreter/ScriptInterpreter.h
    M lldb/source/API/SBDebugger.cpp
    M lldb/source/Interpreter/ScriptInterpreter.cpp
    M lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.cpp
    M lldb/source/Plugins/ScriptInterpreter/Lua/ScriptInterpreterLua.h
    M lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.cpp
    M lldb/source/Plugins/ScriptInterpreter/Python/PythonDataObjects.h
    M lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
    M lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
    M lldb/test/API/functionalities/paths/TestPaths.py
    M lldb/test/Shell/Driver/TestHelp.test
    M lldb/tools/driver/Driver.cpp
    M lldb/tools/driver/Driver.h
    M lldb/tools/driver/Options.td

  Log Message:
  -----------
  [lldb] make it easier to find LLDB's python

It is surprisingly difficult to write a simple python script that
can reliably `import lldb` without failing, or crashing.   I'm
currently resorting to convolutions like this:

    def find_lldb(may_reexec=False):
		if prefix := os.environ.get('LLDB_PYTHON_PREFIX'):
			if os.path.realpath(prefix) != os.path.realpath(sys.prefix):
				raise Exception("cannot import lldb.\n"
					f"  sys.prefix should be: {prefix}\n"
					f"  but it is: {sys.prefix}")
		else:
			line1, line2 = subprocess.run(
				['lldb', '-x', '-b', '-o', 'script print(sys.prefix)'],
				encoding='utf8', stdout=subprocess.PIPE,
				check=True).stdout.strip().splitlines()
			assert line1.strip() == '(lldb) script print(sys.prefix)'
			prefix = line2.strip()
			os.environ['LLDB_PYTHON_PREFIX'] = prefix

		if sys.prefix != prefix:
			if not may_reexec:
				raise Exception(
					"cannot import lldb.\n" +
					f"  This python, at {sys.prefix}\n"
					f"  does not math LLDB's python at {prefix}")
			os.environ['LLDB_PYTHON_PREFIX'] = prefix
			python_exe = os.path.join(prefix, 'bin', 'python3')
			os.execl(python_exe, python_exe, *sys.argv)

		lldb_path = subprocess.run(['lldb', '-P'],
			check=True, stdout=subprocess.PIPE,
				encoding='utf8').stdout.strip()

		sys.path = [lldb_path] + sys.path

This patch aims to replace all that with:

  #!/usr/bin/env lldb-python
  import lldb
  ...

... by adding the following features:

* new command line option: --print-script-interpreter-info.  This
   prints language-specific information about the script interpreter
   in JSON format.

* new tool (unix only): lldb-python which finds python and exec's it.

Reviewed By: JDevlieghere

Differential Revision: https://reviews.llvm.org/D112973




More information about the All-commits mailing list