简体   繁体   English

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

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

typical debugging pattern: 典型的调试模式:

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
}

trivial approaches like a breakpoint on strstr(typeid(*this).name(), "Button") don't work because on typeid lldb console tells: 诸如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

surely #include in console before making the call doesn't help 肯定在呼叫之前#include在控制台中无济于事

You can do this in Python pretty easily. 您可以在Python中轻松完成此操作。 Set the breakpoint - say it is breakpoint 1 - then do: 设置断点-说它是断点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

The only tricky bit here is that when calling FindVariable I passed lldb.eDynamicDontRunTarget which told lldb to fetch the "dynamic" type of the variable, as opposed to the static type. 这里唯一棘手的一点是,当调用FindVariable时,我传递了lldb.eDynamicDontRunTarget ,它告诉lldb获取变量的“动态”类型,而不是静态类型。 As an aside, I could have also used lldb.eDynamicRunTarget but I happen to know lldb doesn't have to run the target go get C++ dynamic types. lldb.eDynamicRunTarget ,我本来也可以使用lldb.eDynamicRunTarget但是我碰巧知道lldb不必运行目标就可以获取C ++动态类型。

This way of solving the problem is nice in that you don't have to have used RTTI for it to work (though then we'll only be able to get the type of classes that have some virtual method - since we use the vtable to do this magic.) It will also be faster than a method that requires running code in the debugee as your expression would have to do. 这种解决问题的方法很好,因为您不必使用RTTI即可工作(尽管那样,我们只能获得具有某种虚拟方法的类的类型-因为我们使用vtable可以这样做会比需要在被调试程序中运行代码的方法更快,因为您的表达式必须这样做。

BTW, if you like this trick, you can also put the breakpoint code into a python function in some python file (just copy the def above), then use: 顺便说一句,如果您喜欢这个技巧,还可以将断点代码放入某些python文件中的python函数中(只需复制上面的def),然后使用:

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

so you don't have to keep retyping it. 因此您不必继续重新输入。

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

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