有没有办法在gdb中设置一个以调用堆栈为条件的断点?

Nat*_*man 26 c++ callstack gdb breakpoints

我正在Linux上的gdb 7.1中调试C++.

我有一个a()在代码中的许多地方调用的函数.我想在其中设置一个断点,但只有从它调用它b().有什么办法吗?

有没有什么方法可以做到这一点,只有b()c()无线电话中调用,等等?

Emp*_*ian 23

您描述的需求经常出现,通常是在_is_caller被调用的情况下,但您只对来自的调用感兴趣some_utility_fn.

您可以使用CVS trunk中的GDB中的新嵌入式Python支持来编写整个交互的脚本.

如果没有Python,你可以做的事情是有限的,但通常的技术是打开一个禁用的断点some_other_fn,并从一个连接到断点的命令启用它a().

这是一个例子:

int a(int x)
{
  return x + 1;
}

int b()
{
  return a(1);
}

int call_a_lots()
{
  int i, sum = 0;
  for (i = 0; i < 100; i++)
    sum += a(i);
}

int main()
{
  call_a_lots();
  return b();
}

gcc -g t.c
gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) break a
Breakpoint 1 at 0x4004cb: file t.c, line 3.
(gdb) disable 1
(gdb) break b
Breakpoint 2 at 0x4004d7: file t.c, line 8.
(gdb) command 2
>silent
>enable 1
>continue
>end
(gdb) run

Breakpoint 1, a (x=1) at t.c:3
3     return x + 1;
(gdb) bt
#0  a (x=1) at t.c:3
#1  0x00000000004004e1 in b () at t.c:8
#2  0x000000000040052c in main () at t.c:21
(gdb) q
Run Code Online (Sandbox Code Playgroud)

瞧:我们已经停止了b()从所谓的a(),忽略之前的100个呼叫b().

  • @NathanFellman`命令2`将命令附加到断点2 (3认同)

Ser*_*kov 8

我已经在已经可用的gdb 7.6上测试了这个,但是它不适用于gdb 7.2并且可能在gdb 7.1上:

所以这是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;
}
Run Code Online (Sandbox Code Playgroud)

然后g ++ -g main.cpp

这是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")
Run Code Online (Sandbox Code Playgroud)

这就是它的工作原理:

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
Run Code Online (Sandbox Code Playgroud)


Jef*_*ull 8

gdb 现在可以直接处理这个,不需要 Python。只需这样做:

b a if $_caller_is("b")
Run Code Online (Sandbox Code Playgroud)


rix*_*rrr 5

比Python脚本更简单的解决方案是使用临时断点.

它看起来像这样:

b ParentFunction
command 1
  tb FunctionImInterestedIn
  c
end
Run Code Online (Sandbox Code Playgroud)

每次进入时ParentFunction,你都会在你真正感兴趣的函数上设置一次性断点,然后继续运行(大概直到你达到那个断点).

由于你只会打开一次FunctionImInterestedIn,如果FunctionImInterestedIn在上下文中多次ParentFunction调用并且你想在每次调用时中断,这将不起作用.