Is there any way to set a breakpoint in gdb that is conditional on the call stack?(有没有办法在 gdb 中设置一个以调用堆栈为条件的断点?)
问题描述
我正在 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?
只有在 c() 调用 b() 时,有什么办法可以做到这一点,以此类推吗?
Is there any way to do it only if b() was called from c(), and so on ad infinitum?
推荐答案
更新: 现在有一个 更好地回答这个问题:使用 GDB _is_caller 便利函数.
Update: There is now a better answer to this question: use GDB _is_caller convenience function.
您描述的需求经常出现,通常是在 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() 上设置一个 disabled 断点,然后通过命令启用它,附加到 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
瞧:我们已经停止了从 b() 调用的 a(),忽略了之前对 a() 的 100 次调用.
Voila: we've stopped on a() called from b(), ignoring previous 100 calls to a().
这篇关于有没有办法在 gdb 中设置一个以调用堆栈为条件的断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:有没有办法在 gdb 中设置一个以调用堆栈为条件的
基础教程推荐
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- CString 到 char* 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
