Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'''
Break on Objective-C 's method using its address'
'''
import shlex
import lldb
import re
def breakonmethod(debugger, command, exe_ctx,result, internal_dict):
args=shlex.split(command)
Class=args[0]
Method=args[1]
ClassMethod = lldb.SBCommandReturnObject()
CMCommand='expr -- (IMP)method_getImplementation((Method)class_getClassMethod((Class)objc_getClass("{0}"),@selector({1})));'.format(Class,Method)
IMCommand='expr -- (IMP)method_getImplementation((Method)class_getInstanceMethod((Class)objc_getClass("{0}"),@selector({1})));'.format(Class,Method)
debugger.GetCommandInterpreter().HandleCommand(CMCommand, ClassMethod)
InstanceMethod =lldb.SBCommandReturnObject()
debugger.GetCommandInterpreter().HandleCommand(IMCommand,InstanceMethod)
CMObj=ClassMethod.GetOutput()
IMObj=InstanceMethod.GetOutput()
CMObj=re.search(r'0x([a-zA-Z0-9]|x)*', CMObj).group(0)
IMObj=re.search(r'0x([a-zA-Z0-9]|x)*', IMObj).group(0)
CMObj=int(CMObj, 16)
IMObj=int(IMObj, 16)
ADDR=max(CMObj,IMObj)
debugger.HandleCommand("breakpoint set -a %i"%ADDR)
def __lldb_init_module (debugger, dict):
#Avoid Name Collision
debugger.HandleCommand('command script add -f BreakMessage.breakonmethod bom')
@KevinGong2013

This comment has been minimized.

Show comment
Hide comment
@KevinGong2013

KevinGong2013 Apr 24, 2018

对应lldb-902.0.79.2的版本


'''
Break on Objective-C 's method using its address'
'''
import shlex
import lldb
import re


def breakonmethod(debugger, command, result, internal_dict):
    args = shlex.split(command)
    Class = args[0]
    Method = args[1]
    ClassMethod = lldb.SBCommandReturnObject()
    CMCommand = 'expr -- (IMP)method_getImplementation((Method)class_getClassMethod((Class)objc_getClass("{0}"),@selector({1})));'.format(
        Class, Method)
    IMCommand = 'expr -- (IMP)method_getImplementation((Method)class_getInstanceMethod((Class)objc_getClass("{0}"),@selector({1})));'.format(
        Class, Method)
    debugger.GetCommandInterpreter().HandleCommand(CMCommand, ClassMethod)
    InstanceMethod = lldb.SBCommandReturnObject()
    debugger.GetCommandInterpreter().HandleCommand(IMCommand, InstanceMethod)
    CMObj = ClassMethod.GetOutput()
    IMObj = InstanceMethod.GetOutput()
    CMObj = re.search(r'0x([a-zA-Z0-9]|x)*', CMObj).group(0)
    IMObj = re.search(r'0x([a-zA-Z0-9]|x)*', IMObj).group(0)
    CMObj = int(CMObj, 16)
    IMObj = int(IMObj, 16)
    ADDR = max(CMObj, IMObj)
    debugger.HandleCommand("breakpoint set -a %i" % ADDR)


def __lldb_init_module(debugger, dict):
    # Avoid Name Collision
    debugger.HandleCommand(
        'command script add -f BreakOnMethod.breakonmethod bom')
    print('The "bom" python command has been installed and is ready for use.')

KevinGong2013 commented Apr 24, 2018

对应lldb-902.0.79.2的版本


'''
Break on Objective-C 's method using its address'
'''
import shlex
import lldb
import re


def breakonmethod(debugger, command, result, internal_dict):
    args = shlex.split(command)
    Class = args[0]
    Method = args[1]
    ClassMethod = lldb.SBCommandReturnObject()
    CMCommand = 'expr -- (IMP)method_getImplementation((Method)class_getClassMethod((Class)objc_getClass("{0}"),@selector({1})));'.format(
        Class, Method)
    IMCommand = 'expr -- (IMP)method_getImplementation((Method)class_getInstanceMethod((Class)objc_getClass("{0}"),@selector({1})));'.format(
        Class, Method)
    debugger.GetCommandInterpreter().HandleCommand(CMCommand, ClassMethod)
    InstanceMethod = lldb.SBCommandReturnObject()
    debugger.GetCommandInterpreter().HandleCommand(IMCommand, InstanceMethod)
    CMObj = ClassMethod.GetOutput()
    IMObj = InstanceMethod.GetOutput()
    CMObj = re.search(r'0x([a-zA-Z0-9]|x)*', CMObj).group(0)
    IMObj = re.search(r'0x([a-zA-Z0-9]|x)*', IMObj).group(0)
    CMObj = int(CMObj, 16)
    IMObj = int(IMObj, 16)
    ADDR = max(CMObj, IMObj)
    debugger.HandleCommand("breakpoint set -a %i" % ADDR)


def __lldb_init_module(debugger, dict):
    # Avoid Name Collision
    debugger.HandleCommand(
        'command script add -f BreakOnMethod.breakonmethod bom')
    print('The "bom" python command has been installed and is ready for use.')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment