Skip to content

Commit ab0a0b3

Browse files
authored
Normalize LLDB version parsing on Darwin to handle both Apple Clang and LLVM versions (#56)
* Normalize LLDB version parsing on Darwin to handle both Apple Clang and LLVM versions
1 parent 7989b87 commit ab0a0b3

2 files changed

Lines changed: 43 additions & 4 deletions

File tree

common/util.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Utility functions."""
22

33
import os
4+
import re
45
import shutil
56
from argparse import ArgumentTypeError
67
from collections.abc import Callable
@@ -490,3 +491,29 @@ def find_darwin_heap_regions(process: SBProcess) -> Union[list[tuple[int, int]],
490491
return None
491492

492493
return heap_regions
494+
495+
496+
def parse_llvm_version_string(version: str) -> Union[list[int], None]:
497+
"""
498+
Parse the version string for an LLDB version built from the official LLVM sources
499+
(e.g. 'lldb version 16.0.0').
500+
501+
:param version: The version string to parse.
502+
:return: A list of integers representing the version, or None if parsing failed.
503+
"""
504+
if match := re.search(r"lldb version\s+([\d.]+)", version):
505+
return [int(x) for x in match.group(1).split(".")]
506+
return None
507+
508+
509+
def parse_apple_clang_version_string(version: str) -> Union[list[int], None]:
510+
"""
511+
Parse the version string for an LLDB version shipped with Apple Clang
512+
(e.g. 'lldb-1600.0.36.3').
513+
514+
:param version: The version string to parse.
515+
:return: A list of integers representing the version, or None if parsing failed.
516+
"""
517+
if match := re.search(r"lldb-(\d+[\d.]+)", version):
518+
return [int(x) for x in match.group(1).split(".")]
519+
return None

llef.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from commands.settings import SettingsCommand
3636
from commands.xinfo import XinfoCommand
3737
from common.state import LLEFState
38+
from common.util import lldb_version_to_clang, parse_apple_clang_version_string, parse_llvm_version_string
3839
from handlers.stop_hook import StopHookHandler
3940

4041

@@ -69,8 +70,19 @@ def __lldb_init_module(debugger: SBDebugger, _: dict[Any, Any]) -> None:
6970

7071
LLEFState.platform = platform.system()
7172
if LLEFState.platform == "Darwin":
72-
# Getting Clang version (e.g. lldb-1600.0.36.3)
73-
LLEFState.version = [int(x) for x in debugger.GetVersionString().split()[0].split("-")[1].split(".")]
73+
# On Darwin, we have two possibilities:
74+
# - LLDB shipped with Apple Clang
75+
# - LLDB built from the LLVM sources
76+
# Those have different versioning schemes, and they need to be normalized to the Apple Clang version
77+
# to adhere to the assumptions taken in other places regarding LLDB on Darwin.
78+
if version := parse_apple_clang_version_string(debugger.GetVersionString()):
79+
LLEFState.version = version
80+
elif version := parse_llvm_version_string(debugger.GetVersionString()):
81+
LLEFState.version = lldb_version_to_clang(version)
82+
else:
83+
raise ValueError(f"Unable to parse LLDB version string: {debugger.GetVersionString()}")
7484
else:
75-
# Getting LLDB version (e.g. lldb version 16.0.0)
76-
LLEFState.version = [int(x) for x in debugger.GetVersionString().split("version")[1].split()[0].split(".")]
85+
if version := parse_llvm_version_string(debugger.GetVersionString()):
86+
LLEFState.version = version
87+
else:
88+
raise ValueError(f"Unable to parse LLDB version string: {debugger.GetVersionString()}")

0 commit comments

Comments
 (0)