如何在lldb中跳过objc_msgSend函数?

时间:2016-12-28 11:13:14

标签: objective-c lldb

在objective-c中,函数cal被转换为objc_msgSend。 e.g。

[foo doSomething:@"keke"]

被翻译为

objc_msgSend(foo, "doSomething:", @"keke")

如何在lldb中调试时直接进入foo.doSomething:

2 个答案:

答案 0 :(得分:0)

lldb确实提供了可以控制步骤逻辑的线程计划。

class GotoUser:
    def __init__ (self, thread_plan, dict):
        self.start_time = time.time()
        self.thread_plan = thread_plan
        target = self.thread_plan.GetThread().GetProcess().GetTarget();
        module = target.GetModuleAtIndex(0)
        sbaddr = lldb.SBAddress(module.GetObjectFileHeaderAddress())
        self.start_address = sbaddr.GetLoadAddress(target)
        module = target.GetModuleAtIndex(1)
        sbaddr = lldb.SBAddress(module.GetObjectFileHeaderAddress())
        self.end_address = sbaddr.GetLoadAddress(target)
        print "start addr: ", hex(self.start_address), " end addr: ", hex(self.end_address)

    def explains_stop (self, event):
        if self.thread_plan.GetThread().GetStopReason()== lldb.eStopReasonTrace:
            return True
        else:
            return False

    def should_stop (self, event):
        cur_pc = self.thread_plan.GetThread().GetFrameAtIndex(0).GetPC()
        if cur_pc >= self.start_address and cur_pc <= self.end_address:
            self.thread_plan.SetPlanComplete(True)
            print 'time used ', (time.time() - self.start_time)
            return True
        else:
            return False

    def should_step (self):
        return True

创建一个python脚本,将其加载到lldb。 运行thread step-scripted gotouser.GotoUser
任务完成。

完整源代码: https://github.com/Jichao/lldb-scripts/blob/master/gotouser.py

版本内置于lldb: https://github.com/Jichao/lldb

答案 1 :(得分:0)

使用Python线程计划很聪明!但是你不应该为ObjC消息做这个。 lldb知道objc_msgSend&amp;其他一些是ObjC消息的调度功能。因此,如果step in以objc_msgSend结束,lldb将从传入的对象/选择器对中找出方法实现,在那里设置断点,然后继续。

例如:

(lldb) run
Process 55502 launched: '/private/tmp/trivial' (x86_64)
Process 55502 stopped
* thread #1: tid = 0x32619ba, function: main , stop reason = breakpoint 1.1
    frame #0: 0x0000000100000f28 trivial`main at trivial.m:18
   15   main()
   16   {
   17     Trivial *my_foo = [[Trivial alloc] init];
-> 18     [my_foo doSomething];
   19     return 0;
   20   }
(lldb) s
Process 55502 stopped
* thread #1: tid = 0x32619ba, function: -[Trivial doSomething] , stop reason = step in
    frame #0: 0x0000000100000ed7 trivial`-[Trivial doSomething] at trivial.m:10
   7    @implementation Trivial
   8    - (void) doSomething
   9    {
-> 10     NSLog(@"%@ called doSomething.", self);
   11   }
   12   @end
   13   

所以在这种情况下,步骤停在了实际的消息接收器。如果那不是你发生的事情,很可能是某些东西愚弄lldb的那部分做对象/选择器 - &gt;实现查找。我必须更多地了解您的代码,以找出原因。