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 中设置一个以调用堆栈为条件的


基础教程推荐
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- 运算符重载的基本规则和习语是什么? 2022-10-31
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- C++,'if' 表达式中的变量声明 2021-01-01