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 中设置一个以调用堆栈为条件的
基础教程推荐
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 这个宏可以转换成函数吗? 2022-01-01
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
