Last active
March 6, 2023 17:15
-
-
Save abdulowork/d61303baf22b6d0feb469f12f949b1f9 to your computer and use it in GitHub Desktop.
Dump view any time viewDidAppear is called
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
from typing import Dict, Any | |
from pathlib import Path | |
from tempfile import mkdtemp | |
from lldb import SBDebugger, SBCommandReturnObject, SBFrame, SBBreakpointLocation, SBExpressionOptions, \ | |
SBLanguageRuntime, SBTarget, SBBreakpoint | |
image_number = 0 | |
temporary_directory = Path(mkdtemp()) | |
def on_view_on_view_did_appear( | |
frame: SBFrame, | |
bp_loc: SBBreakpointLocation, | |
dict: Dict[str, Any], | |
): | |
try: | |
global image_number | |
view_controller_address = hex(frame.FindRegister('arg1').unsigned) | |
output_path = str(temporary_directory.joinpath(f'{image_number}.png')) | |
print(f'Creating image at {output_path}') | |
options = SBExpressionOptions() | |
options.SetLanguage(SBLanguageRuntime.GetLanguageTypeFromString('swift')) | |
swift = f'''let vc = unsafeBitCast({view_controller_address}, to: UIViewController.self) | |
UIGraphicsBeginImageContextWithOptions(vc.view!.frame.size, false, 0) | |
vc.view!.layer.renderInContext(UIGraphicsGetCurrentContext()!) | |
try! UIGraphicsGetImageFromCurrentImageContext()!.pngData()!.write(to: URL(fileURLWithPath: "{output_path}")) | |
UIGraphicsEndImageContext() | |
'''.replace('\n', ';') | |
frame.EvaluateExpression('import UIKit', options) | |
frame.EvaluateExpression(swift, options) | |
image_number += 1 | |
except Exception as e: | |
print(e) | |
module = os.path.splitext(os.path.basename(__file__))[0] | |
def dump_view_on_view_did_appear( | |
debugger: SBDebugger, | |
command: str, | |
result: SBCommandReturnObject, | |
dict: Dict[str, Any] | |
): | |
target: SBTarget = debugger.GetSelectedTarget() | |
print(f'Target: {target}') | |
breakpoint: SBBreakpoint = target.BreakpointCreateByName('-[UIViewController viewDidAppear:]') | |
breakpoint.SetScriptCallbackFunction(f'{module}.{on_view_on_view_did_appear.__name__}') | |
breakpoint.SetAutoContinue(True) | |
print(f'Breakpoint: {breakpoint}') | |
def __lldb_init_module(debugger: SBDebugger, dict): | |
for function in [ | |
dump_view_on_view_did_appear.__name__, | |
]: | |
print(f'\nRegistering {module}.{function}. Call "{function}" from lldb to run the script') | |
debugger.HandleCommand(f'command script add -f {module}.{function} {function}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment