Skip to content

Commit

Permalink
Merge pull request #75 from doronz88/bugfix/hilda_root
Browse files Browse the repository at this point in the history
Bugfix/hilda root
  • Loading branch information
doronz88 authored Aug 22, 2024
2 parents 46f5461 + e457d68 commit d8fad0d
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ Here is a gist of methods you can access from `p`:
- Stop process.
- `cont`
- Continue process.
- `run_for`
- Run the process for given interval.
- `detach`
- Detach from process.
Useful in order to exit gracefully so process doesn't get killed
Expand Down
20 changes: 15 additions & 5 deletions hilda/hilda_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def __init__(self, debugger: lldb.SBDebugger):
self._dynamic_env_loaded = False
self._symbols_loaded = False
self.globals: typing.MutableMapping[str, Any] = globals()
self._hilda_root = Path(__file__).parent

# the frame called within the context of the hit BP
self._bp_frame = None
Expand All @@ -168,7 +169,7 @@ def lsof(self) -> dict:
Get dictionary of all open FDs
:return: Mapping between open FDs and their paths
"""
data = (Path(__file__).parent / 'objective_c' / 'lsof.m').read_text()
data = (self._hilda_root / 'objective_c' / 'lsof.m').read_text()
result = json.loads(self.po(data))
# convert FDs into int
return {int(k): v for k, v in result.items()}
Expand Down Expand Up @@ -347,6 +348,16 @@ def stop(self, *args) -> None:
if not self.process.Stop().Success():
self.log_critical('failed to stop process')

def run_for(self, seconds: float) -> None:
"""
Run the process for a given time
:return:
"""
self.cont()
self.logger.info(f'Running for {seconds} seconds')
time.sleep(seconds)
self.stop()

def cont(self, *args) -> None:
""" Continue process. """
is_running = self.process.GetState() == lldb.eStateRunning
Expand Down Expand Up @@ -828,8 +839,7 @@ def ns(self, data: CfSerializable) -> Symbol:
json_data = json.dumps({'root': data}, default=self._to_ns_json_default)
except TypeError as e:
raise ConvertingToNsObjectError from e

obj_c_code = (Path(__file__).parent / 'objective_c' / 'to_ns_from_json.m').read_text()
obj_c_code = (self._hilda_root / 'objective_c' / 'to_ns_from_json.m').read_text()
expression = obj_c_code.replace('__json_object_dump__', json_data.replace('"', r'\"'))
try:
return self.evaluate_expression(expression)
Expand All @@ -842,7 +852,7 @@ def decode_cf(self, address: Union[int, str]) -> CfSerializable:
:param address: NS object.
:return: Python object.
"""
obj_c_code = (Path(__file__).parent / 'objective_c' / 'from_ns_to_json.m').read_text()
obj_c_code = (self._hilda_root / 'objective_c' / 'from_ns_to_json.m').read_text()
address = f'0x{address:x}' if isinstance(address, int) else address
expression = obj_c_code.replace('__ns_object_address__', address)
try:
Expand Down Expand Up @@ -1212,7 +1222,7 @@ def _get_module_class_list(self, module_name: str):
continue
objc_classlist = m.FindSection('__DATA').FindSubSection('__objc_classlist')
objc_classlist_addr = self.symbol(objc_classlist.GetLoadAddress(self.target))
obj_c_code = (Path(__file__).parent / 'objective_c' / 'get_objectivec_class_by_module.m').read_text()
obj_c_code = (self._hilda_root / 'objective_c' / 'get_objectivec_class_by_module.m').read_text()
obj_c_code = obj_c_code.replace('__count_objc_class', f'{objc_classlist.size // 8}').replace(
'__objc_class_list',
f'{objc_classlist_addr}')
Expand Down
5 changes: 2 additions & 3 deletions hilda/objective_c_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
from collections import namedtuple
from dataclasses import dataclass, field
from functools import partial
from pathlib import Path
from typing import Any
from uuid import uuid4

Expand Down Expand Up @@ -120,7 +119,7 @@ def from_class_name(client, class_name: str):
:param hilda.hilda_client.HildaClient client: Hilda client.
:param class_name: Class name.
"""
obj_c_code = (Path(__file__).parent / 'objective_c' / 'get_objectivec_class_description.m').read_text()
obj_c_code = (client._hilda_root / 'objective_c' / 'get_objectivec_class_description.m').read_text()
obj_c_code = obj_c_code.replace('__class_address__', '0').replace('__class_name__', class_name)
class_symbol = Class(client, class_data=json.loads(client.po(obj_c_code)))
if class_symbol.name != class_name:
Expand All @@ -143,7 +142,7 @@ def reload(self):
Reload class object data.
Should be used whenever the class layout changes (for example, during method swizzling)
"""
obj_c_code = (Path(__file__).parent / 'objective_c' / 'get_objectivec_class_description.m').read_text()
obj_c_code = (self._client._hilda_root / 'objective_c' / 'get_objectivec_class_description.m').read_text()
obj_c_code = obj_c_code.replace('__class_address__', f'{self._class_object:d}')
obj_c_code = obj_c_code.replace('__class_name__', self.name)
self._load_class_data(json.loads(self._client.po(obj_c_code)))
Expand Down
3 changes: 1 addition & 2 deletions hilda/objective_c_symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
from contextlib import suppress
from dataclasses import dataclass
from functools import partial
from pathlib import Path

from objc_types_decoder.decode import decode as decode_type
from pygments import highlight
Expand Down Expand Up @@ -60,7 +59,7 @@ def reload(self):
self.methods.clear()
self.class_ = None

obj_c_code = (Path(__file__).parent / 'objective_c' / 'get_objectivec_symbol_data.m').read_text()
obj_c_code = (self._client._hilda_root / 'objective_c' / 'get_objectivec_symbol_data.m').read_text()
obj_c_code = obj_c_code.replace('__symbol_address__', f'{self:d}')
data = json.loads(self._client.po(obj_c_code))

Expand Down

0 comments on commit d8fad0d

Please sign in to comment.