Skip to content

Commit fb6d1c0

Browse files
committed
[crashlog] Fix and simplify the way we import lldb
Don't try to guess the location of LLDB.framework but use xcrun to ask the command line driver for the location of the lldb module.
1 parent dd6f7ee commit fb6d1c0

File tree

1 file changed

+24
-38
lines changed

1 file changed

+24
-38
lines changed

lldb/examples/python/crashlog.py

Lines changed: 24 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -43,50 +43,36 @@
4343
import time
4444
import uuid
4545

46-
def read_plist(s):
47-
if sys.version_info.major == 3:
48-
return plistlib.loads(s)
49-
else:
50-
return plistlib.readPlistFromString(s)
51-
5246
try:
53-
# Just try for LLDB in case PYTHONPATH is already correctly setup
47+
# First try for LLDB in case PYTHONPATH is already correctly setup.
5448
import lldb
5549
except ImportError:
56-
lldb_python_dirs = list()
57-
# lldb is not in the PYTHONPATH, try some defaults for the current platform
58-
platform_system = platform.system()
59-
if platform_system == 'Darwin':
60-
# On Darwin, try the currently selected Xcode directory
61-
xcode_dir = subprocess.check_output("xcode-select --print-path", shell=True).decode("utf-8")
62-
if xcode_dir:
63-
lldb_python_dirs.append(
64-
os.path.realpath(
65-
xcode_dir +
66-
'/../SharedFrameworks/LLDB.framework/Resources/Python'))
67-
lldb_python_dirs.append(
68-
xcode_dir + '/Library/PrivateFrameworks/LLDB.framework/Resources/Python')
69-
lldb_python_dirs.append(
70-
'/System/Library/PrivateFrameworks/LLDB.framework/Resources/Python')
71-
success = False
72-
for lldb_python_dir in lldb_python_dirs:
73-
if os.path.exists(lldb_python_dir):
74-
if not (sys.path.__contains__(lldb_python_dir)):
75-
sys.path.append(lldb_python_dir)
76-
try:
77-
import lldb
78-
except ImportError:
79-
pass
80-
else:
81-
print('imported lldb from: "%s"' % (lldb_python_dir))
82-
success = True
83-
break
84-
if not success:
50+
# Ask the command line driver for the path to the lldb module. Copy over
51+
# the environment so that SDKROOT is propagated to xcrun.
52+
env = os.environ.copy()
53+
env['LLDB_DEFAULT_PYTHON_VERSION'] = str(sys.version_info.major)
54+
command = ['xcrun', 'lldb', '-P'] if platform.system() == 'Darwin' else ['lldb', '-P']
55+
# Extend the PYTHONPATH if the path exists and isn't already there.
56+
lldb_python_path = subprocess.check_output(command, env=env).decode("utf-8").strip()
57+
if os.path.exists(lldb_python_path) and not sys.path.__contains__(lldb_python_path):
58+
sys.path.append(lldb_python_path)
59+
# Try importing LLDB again.
60+
try:
61+
import lldb
62+
except ImportError:
8563
print("error: couldn't locate the 'lldb' module, please set PYTHONPATH correctly")
8664
sys.exit(1)
8765

8866
from lldb.utils import symbolication
8967

68+
69+
def read_plist(s):
70+
if sys.version_info.major == 3:
71+
return plistlib.loads(s)
72+
else:
73+
return plistlib.readPlistFromString(s)
74+
75+
9076
PARSE_MODE_NORMAL = 0
9177
PARSE_MODE_THREAD = 1
9278
PARSE_MODE_IMAGES = 2
@@ -442,13 +428,13 @@ def __init__(self, path, verbose):
442428
continue
443429
elif line.startswith('Exception Subtype:'): # iOS
444430
self.thread_exception_data = line[18:].strip()
445-
continue
431+
continue
446432
elif line.startswith('Crashed Thread:'):
447433
self.crashed_thread_idx = int(line[15:].strip().split()[0])
448434
continue
449435
elif line.startswith('Triggered by Thread:'): # iOS
450436
self.crashed_thread_idx = int(line[20:].strip().split()[0])
451-
continue
437+
continue
452438
elif line.startswith('Report Version:'):
453439
self.version = int(line[15:].strip())
454440
continue

0 commit comments

Comments
 (0)