GDB:仅当前一个Break在func2上时,才在func1上Break

GDB: Break on func1 only if previous break was on func2

本文关键字:Break func2 上时 func1 才在 一个 GDB      更新时间:2023-10-16

我有两个函数,func1func2,每个函数都有一个断点集。

如果上一个断点命中是func1,是否可以在func2断点上停止GDB?

实现这一点的最佳方法是在断点中使用命令。

当命中两个断点时,您可以指示GDB执行某些命令(例如,增加计数器)。根据这些变量/标志的计数,有条件地暂停执行。

我在这个链接上找到了这些信息。请参阅相同内容以了解更多详细信息。这篇文章写得很好,有恰当的例子。希望这能有所帮助。

让一个断点设置另一个断点。为了避免gdb意大利面条,建议使用define来创建函数。

main.cpp

int c1=0, c2=0;
void func1(){
    c1++;
}
void func2(){
    c2++;
}
int main(){
    // we shouldn't see a breakpoint here
    for(int i=0; i < 5; i++)
        func1();
    func2();
    // get a breakpoint
    func1();
    return 0;
}

编译并运行gdb

clang++ main.cpp -o main.exe -g
gdb --args ./main.exe

gdb命令

break func2
commands
    break func1
    # run a few commands when we hit func1()
    commands
    print c1
    backtrace
    end
    # continue to func1() breakpoint
    continue
end
run