[Lldb-commits] [PATCH] D112973: [lldb] make it easier to find LLDB's python

Lawrence D'Anna via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Mon Nov 1 16:58:35 PDT 2021


lawrence_danna created this revision.
lawrence_danna added reviewers: jasonmolenda, JDevlieghere, jingham.
Herald added subscribers: dang, mgorny.
lawrence_danna requested review of this revision.
Herald added a project: LLDB.

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 lldb::PathType for SBHostOS::GetLLDBPath: ePathTypePythonPrefix

is the path to python's sys.prefix

- new command line option: lldb --python-prefix to print that prefix.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D112973

Files:
  lldb/bindings/python/CMakeLists.txt
  lldb/docs/man/lldb.rst
  lldb/docs/python_api_enums.rst
  lldb/include/lldb/lldb-enumerations.h
  lldb/source/API/SBHostOS.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.cpp
  lldb/source/Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h
  lldb/test/API/functionalities/paths/TestPaths.py
  lldb/test/Shell/Driver/TestHelp.test
  lldb/tools/driver/Driver.cpp
  lldb/tools/driver/Driver.h
  lldb/tools/driver/Options.td

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D112973.383926.patch
Type: text/x-patch
Size: 10965 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20211101/b242ab13/attachment-0001.bin>


More information about the lldb-commits mailing list