Skip to content

Commit

Permalink
add lldb_importer to safely manage import lldb
Browse files Browse the repository at this point in the history
  • Loading branch information
netanelc305 committed May 23, 2024
1 parent 1503d05 commit f0669d6
Show file tree
Hide file tree
Showing 15 changed files with 37 additions and 16 deletions.
2 changes: 1 addition & 1 deletion hilda/__main__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from hilda.launch_lldb import cli
from hilda.cli import cli


def main():
Expand Down
2 changes: 1 addition & 1 deletion hilda/hilda_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
from hilda.exceptions import AccessingMemoryError, AccessingRegisterError, AddingLldbSymbolError, \
BrokenLocalSymbolsJarError, ConvertingFromNSObjectError, ConvertingToNsObjectError, CreatingObjectiveCSymbolError, \
DisableJetsamMemoryChecksError, EvaluatingExpressionError, HildaException, SymbolAbsentError
from hilda.launch_lldb import disable_logs # noqa: F401
from hilda.lldb_importer import lldb
from hilda.objective_c_symbol import ObjectiveCSymbol
from hilda.registers import Registers
from hilda.snippets.mach import CFRunLoopServiceMachPort_hooks
Expand Down
23 changes: 23 additions & 0 deletions hilda/lldb_importer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import logging
import subprocess
import sys
from types import ModuleType
from typing import Optional

logger = logging.getLogger(__name__)


def get_lldb_python_path() -> str:
result = subprocess.run(['lldb', '-P'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=True)
return result.stdout.strip()


def import_lldb() -> Optional[ModuleType]:
lldb_python_path = get_lldb_python_path()
if lldb_python_path not in sys.path:
sys.path.append(lldb_python_path)
import lldb
return lldb


lldb = import_lldb()
2 changes: 1 addition & 1 deletion hilda/snippets/boringssl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lldb
from hilda.lldb_importer import lldb

_FILENAME = '/tmp/hilda-keylog.txt'

Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/dyld.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lldb
from cached_property import cached_property

from hilda.lldb_importer import lldb
from hilda.snippets.macho.all_image_infos import AllImageInfos
from hilda.snippets.syslog import open_syslog_socket

Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/fs_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json

import lldb
from hilda.lldb_importer import lldb


def dirlist(path: str):
Expand Down
3 changes: 1 addition & 2 deletions hilda/snippets/mach/CFRunLoopServiceMachPort_hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import lldb

from hilda.exceptions import SymbolAbsentError
from hilda.lldb_importer import lldb


def _CFRunLoopServiceMachPort_hook(hilda, *args):
Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/macho/all_image_infos.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import logging

import lldb
from construct import Array, CString, Hex, If, Int32ub, Int32ul, Int64ul, Pointer, Struct, Tell, this
from humanfriendly import prompts

from hilda.lldb_importer import lldb
from hilda.snippets.macho.image_info import ImageInfo, dyld_image_info_t
from hilda.snippets.macho.macho import mach_header_t
from hilda.snippets.uuid import uuid_t
Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/macho/image_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lldb
from construct import CString, If, Int64ul, Pointer, Struct, this

from hilda.lldb_importer import lldb
from hilda.snippets.macho.macho import mach_header_t
from hilda.snippets.macho.macho_load_commands import LoadCommands
from hilda.snippets.uuid import uuid_t
Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/macho/macho_load_commands.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from typing import List

import lldb
from construct import Array, Bytes, Enum, Hex, Int8ul, Int32ul, Int64ul, PaddedString, Pointer, Seek, Struct, Switch, \
Tell, this

from hilda.lldb_importer import lldb
from hilda.snippets.macho.apple_version import version_t
from hilda.symbol import SymbolFormatField

Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/remotepairingd.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import lldb
from hilda.lldb_importer import lldb

TLV_MAP = {
0x00: 'METHOD',
Expand Down
2 changes: 1 addition & 1 deletion hilda/snippets/xpc.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pprint import pformat

import lldb
from pygments import highlight
from pygments.formatters import TerminalTrueColorFormatter
from pygments.lexers import PythonLexer

from hilda.exceptions import ConvertingFromNSObjectError
from hilda.lldb_importer import lldb

# module global for storing all active xpc connections
active_xpc_connections = {}
Expand Down
2 changes: 1 addition & 1 deletion hilda/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from contextlib import contextmanager
from typing import Any, Optional

import lldb
from construct import FormatField

from hilda.common import CfSerializable
from hilda.lldb_importer import lldb
from hilda.objective_c_class import Class

ADDRESS_SIZE_TO_STRUCT_FORMAT = {1: 'B', 2: 'H', 4: 'I', 8: 'Q'}
Expand Down
3 changes: 1 addition & 2 deletions hilda/symbols_jar.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from contextlib import suppress

import lldb

from hilda.exceptions import AddingLldbSymbolError, SymbolAbsentError
from hilda.lldb_importer import lldb


class SymbolsJar(dict):
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import lldb
import pytest

from hilda.hilda_client import HildaClient
from hilda.lldb_importer import lldb


@pytest.fixture(scope='function')
Expand Down

0 comments on commit f0669d6

Please sign in to comment.