有什么办法在gdb中设置一个断点,它是调用堆栈的条件吗? [英] Is there any way to set a breakpoint in gdb that is conditional on the call stack?
问题描述
我在Linux上的gdb 7.1中调试C ++。
I am debugging C++ in gdb 7.1 on Linux.
我有一个函数 a()
在许多地方的代码中调用。我想在其中设置断点,但只有当它从 b()
调用。有什么办法吗?
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 it was called from b()
. Is there any way to do it?
只有当 b()
c()
,以此类推。
推荐答案
需要你描述很经常出现,通常在 some_utility_fn
的上下文中被调用很多,但是你只对来自 some_other_fn的调用感兴趣
The need you describe comes up quite often, usually in the context of some_utility_fn
being called a lot, but you only are interested in the call which comes from some_other_fn
.
您可以使用CVS中继的GDB中新的嵌入式Python支持来脚本化整个交互。
You could probably script this entire interaction using the new embedded Python support in GDB from CVS trunk.
没有Python,你可以做什么,但通常的方法是在 a()$ c上设置禁用断点$ c>,并通过
b()
上的断点附加的命令启用它。
Without Python, you are limited in what you can do, but the usual technique is to have a disabled breakpoint on a()
, and enable it from a command, attached to a breakpoint on b()
.
例如:
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
Voila: c $ c> a()从 b()
调用,忽略先前对$ c $ c>。
Voila: we've stopped on a()
called from b()
, ignoring previous 100 calls to a()
.
这篇关于有什么办法在gdb中设置一个断点,它是调用堆栈的条件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!