【问题标题】:lldb: conditional breakpoint on a most derived typelldb:最派生类型上的条件断点
【发布时间】:2016-12-19 16:31:08
【问题描述】:

典型的调试模式:

class Button : public MyBaseViewClass
{ 
...
};

....
void MyBaseViewClass::Resized()
{
//<---- here I want to stop in case MyBaseViewClass is really a Button, but not a ScrollBar, Checkbox or something else. I.e. I want a breakpoint condition on a dynamic (most derived) type
}

strstr(typeid(*this).name(), "Button") 上的断点之类的简单方法不起作用,因为 typeid lldb 控制台告诉:

(lldb) p typeid(*this)
error: you need to include <typeinfo> before using the 'typeid' operator
error: 1 errors parsing expression

在拨打电话之前在控制台中肯定 #include 没有帮助

【问题讨论】:

    标签: c++ xcode lldb


    【解决方案1】:

    你可以很容易地在 Python 中做到这一点。设置断点 - 说它是断点 1 - 然后做:

    (lldb) break command add -s python 1
    Enter your Python command(s). Type 'DONE' to end.
    def function (frame, bp_loc, internal_dict):
        """frame: the lldb.SBFrame for the location at which you stopped
           bp_loc: an lldb.SBBreakpointLocation for the breakpoint location information
           internal_dict: an LLDB support object not to be used"""
        this_value = frame.FindVariable("this", lldb.eDynamicDontRunTarget) 
        this_type = this_value.GetType().GetPointeeType().GetName() 
        if this_type == "YourClassNameHere": 
            return True 
        return False 
        DONE
    

    这里唯一棘手的一点是,当调用 FindVariable 时,我传递了lldb.eDynamicDontRunTarget,它告诉 lldb 获取变量的“动态”类型,而不是静态类型。顺便说一句,我也可以使用lldb.eDynamicRunTarget,但我碰巧知道 lldb 不必运行目标 go get C++ 动态类型。

    这种解决问题的方法很好,因为您不必使用 RTTI 就可以工作(尽管那样我们将只能获得具有某些虚拟方法的类的类型 - 因为我们使用vtable 来做这个魔术。)它也比需要在被调试者中运行代码的方法更快,因为你的表达式必须这样做。

    顺便说一句,如果你喜欢这个技巧,你也可以将断点代码放入某个python文件中的python函数中(只需复制上面的def),然后使用:

    (lldb) command script import my_functions.py
    (lldb) breakpoint command add -F my_functions.function
    

    这样您就不必不断重新输入。

    【讨论】:

    • 很高兴通过 python 获得支持,但恕我直言,这样的东西应该真正嵌入调试器本身。
    • 请使用 lldb.llvm.org 的错误跟踪器或bugreporter.apple.com 提交请求此错误的错误。
    • 有没有一种方便的方法可以将附加参数传递给上面的 my_functions.function ?显然我可以包装调用,但是编写太多代码会产生异味,理想情况下应该是这样的: >breakpoint command add -F my_functions.function YourClassNameHere 应该作为第四个参数或在这个 internal_dict 中
    猜你喜欢
    • 2019-12-18
    • 2017-03-07
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-03
    • 1970-01-01
    相关资源
    最近更新 更多