Is there any way to set a breakpoint in gdb that is conditional on the call stack?

前端 未结 6 464
野的像风
野的像风 2020-11-28 13:15

I am debugging C++ in gdb 7.1 on Linux.

I have a function a() that is called in many places in the code. I want to set a breakpoint in it, but only if

6条回答
  •  借酒劲吻你
    2020-11-28 13:50

    I have tested this on gdb 7.6 that is already available but it does not work on gdb 7.2 and probably on gdb 7.1:

    So this is main.cpp:

    int a()
    {
      int p = 0;
      p = p +1;
      return  p;
    }
    
    int b()
    {
      return a();
    }
    
    int c()
    {
      return a();
    }
    
    int main()
    {
      c();
      b();
      a();
      return 0;
    }
    

    Then g++ -g main.cpp

    This is my_check.py:

    class MyBreakpoint (gdb.Breakpoint):
        def stop (self):
            if gdb.selected_frame().older().name()=="b":
              gdb.execute("bt")
              return True
            else:
              return False
    
    MyBreakpoint("a")
    

    And this is how it works:

    4>gdb -q -x my_check.py ./a.out
    Reading symbols from /home/a.out...done.
    Breakpoint 1 at 0x400540: file main.cpp, line 3.
    (gdb) r
    Starting program: /home/a.out
    #0  a () at main.cpp:3
    #1  0x0000000000400559 in b () at main.cpp:10
    #2  0x0000000000400574 in main () at main.cpp:21
    
    Breakpoint 1, a () at main.cpp:3
    3         int p = 0;
    (gdb) c
    Continuing.
    [Inferior 1 (process 16739) exited normally]
    (gdb) quit
    

提交回复
热议问题