gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:44:46 24 4
gpt4 key购买 nike

典型的 Debug模式:

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 不必运行目标去获取 C++ 动态类型。

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

顺便说一句,如果你喜欢这个技巧,你也可以将断点代码放入某个 python 文件中的 python 函数中(只需复制上面的 def),然后使用:
(lldb) command script import my_functions.py
(lldb) breakpoint command add -F my_functions.function

所以你不必继续重新输入它。

关于c++ - lldb:最派生类型上的条件断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38921698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com