繁体   English   中英

lldb:最派生类型上的条件断点

[英]lldb: conditional breakpoint on a most derived type

典型的调试模式:

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在控制台中无济于事

您可以在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.eDynamicRunTarget但是我碰巧知道lldb不必运行目标就可以获取C ++动态类型。

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

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

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

因此您不必继续重新输入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM