【问题标题】:GDB: Break on func1 only if previous break was on func2GDB:仅当先前的中断在 func2 上时才在 func1 上中断
【发布时间】:2012-09-09 19:33:11
【问题描述】:

我有两个函数,func1func2,每个函数都设置了断点。

如果之前的断点命中是func1,是否可以让 GDB 在 func2 断点处停止?

【问题讨论】:

  • 您是否有一个小代码 sn-p 可以演示您要完成的工作?
  • 不需要代码 sn-p;这个问题很清楚。
  • 断点命令列表是你的朋友。如果您的程序由于第一个断点而停止,您可以给func1 的断点设置func2 的断点。 ofb.net/gnu/gdb/gdb_35.html#SEC35

标签: c++ c gdb


【解决方案1】:

最好的方法是在断点中使用命令。

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

我在this link 上找到了此信息。详情请参阅相同内容。这篇文章写得很好,有适当的例子。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    让一个断点设置另一个断点。为避免 gdb spaghetti 使用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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-20
      • 2020-12-12
      • 2021-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-22
      相关资源
      最近更新 更多