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

Is there any way to set a breakpoint in gdb that is conditional on the call stack?(有没有办法在 gdb 中设置一个以调用堆栈为条件的断点?)
本文介绍了有没有办法在 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 中设置一个以调用堆栈为条件的断点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Rising edge interrupt triggering multiple times on STM32 Nucleo(在STM32 Nucleo上多次触发上升沿中断)
How to use va_list correctly in a sequence of wrapper functions calls?(如何在一系列包装函数调用中正确使用 va_list?)
OpenGL Perspective Projection Clipping Polygon with Vertex Outside Frustum = Wrong texture mapping?(OpenGL透视投影裁剪多边形,顶点在视锥外=错误的纹理映射?)
How does one properly deserialize a byte array back into an object in C++?(如何正确地将字节数组反序列化回 C++ 中的对象?)
What free tiniest flash file system could you advice for embedded system?(您可以为嵌入式系统推荐什么免费的最小闪存文件系统?)
Volatile member variables vs. volatile object?(易失性成员变量与易失性对象?)