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

发布于 2024-10-24 08:33:03 字数 202 浏览 1 评论 0原文

我正在 Linux 上的 gdb 7.1 中调试 C++。

我有一个函数a(),它在代码中的许多地方被调用。我想在其中设置一个断点,但前提是它是从 b() 调用的。有什么办法可以做到吗?

仅当从 c() 调用 b() 时,是否有办法做到这一点,依此类推?

I am debugging C++ in gdb 7.1 on Linux.

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?

Is there any way to do it only if b() was called from c(), and so on ad infinitum?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

五里雾 2024-10-31 08:33:03

更新:这个问题现在有一个更好的答案:使用GDB _is_caller< /code> 便利功能。

您描述的需求经常出现,通常是在多次调用 some_utility_fn 的情况下,但您只对来自 some_other_fn 的调用感兴趣。

您可以使用来自 CVS 主干的 GDB 中新的嵌入式 Python 支持来编写整个交互的脚本。

如果没有 Python,您能做的事情就会受到限制,但通常的技术是在 a() 上设置一个禁用断点,然后通过附加到的命令启用它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 次调用。

Update: There is now a better answer to this question: use GDB _is_caller convenience function.

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.

You could probably script this entire interaction using the new embedded Python support in GDB from CVS trunk.

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().

Here is an example:

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: we've stopped on a() called from b(), ignoring previous 100 calls to a().

半夏半凉 2024-10-31 08:33:03

gdb 现在可以直接处理这个问题,不需要 Python。只需这样做:

b a if $_caller_is("b")

gdb can handle this directly now without any need for Python. Just do this:

b a if $_caller_is("b")
蓝梦月影 2024-10-31 08:33:03

我已经在已经可用的 gdb 7.6 上对此进行了测试,但它在 gdb 7.2 上不起作用,可能在 gdb 7.1 上不起作用:

所以这是 main.cpp:

int a()
{
  int p = 0;
  p = p +1;
  return  p;
}

int b()
{
  return a();
}

int c()
{
  return a();
}

int main()
{
  c();
  b();
  a();
  return 0;
}

然后 g++ -g main.cpp

这是 my_check.py:

class MyBreakpoint (gdb.Breakpoint):
    def stop (self):
        if gdb.selected_frame().older().name()=="b":
          gdb.execute("bt")
          return True
        else:
          return False

MyBreakpoint("a")

这就是它的方式作品:

4>gdb -q -x my_check.py ./a.out
Reading symbols from /home/a.out...done.
Breakpoint 1 at 0x400540: file main.cpp, line 3.
(gdb) r
Starting program: /home/a.out
#0  a () at main.cpp:3
#1  0x0000000000400559 in b () at main.cpp:10
#2  0x0000000000400574 in main () at main.cpp:21

Breakpoint 1, a () at main.cpp:3
3         int p = 0;
(gdb) c
Continuing.
[Inferior 1 (process 16739) exited normally]
(gdb) quit

I have tested this on gdb 7.6 that is already available but it does not work on gdb 7.2 and probably on gdb 7.1:

So this is main.cpp:

int a()
{
  int p = 0;
  p = p +1;
  return  p;
}

int b()
{
  return a();
}

int c()
{
  return a();
}

int main()
{
  c();
  b();
  a();
  return 0;
}

Then g++ -g main.cpp

This is my_check.py:

class MyBreakpoint (gdb.Breakpoint):
    def stop (self):
        if gdb.selected_frame().older().name()=="b":
          gdb.execute("bt")
          return True
        else:
          return False

MyBreakpoint("a")

And this is how it works:

4>gdb -q -x my_check.py ./a.out
Reading symbols from /home/a.out...done.
Breakpoint 1 at 0x400540: file main.cpp, line 3.
(gdb) r
Starting program: /home/a.out
#0  a () at main.cpp:3
#1  0x0000000000400559 in b () at main.cpp:10
#2  0x0000000000400574 in main () at main.cpp:21

Breakpoint 1, a () at main.cpp:3
3         int p = 0;
(gdb) c
Continuing.
[Inferior 1 (process 16739) exited normally]
(gdb) quit
荒人说梦 2024-10-31 08:33:03

比 Python 脚本更简单的解决方案是使用 临时断点

它看起来像这样:

b ParentFunction
command 1
  tb FunctionImInterestedIn
  c
end

每次中断 ParentFunction 时,您都会在您真正感兴趣的函数上设置一个一次性断点,然后继续运行(大概直到您遇到该断点)。

由于您将在 FunctionImInterestedIn 上中断一次,因此如果在 ParentFunction 上下文中多次调用 FunctionImInterestedIn 并且您想要,这将不起作用在每次调用时中断。

A simpler solution than Python scripting is using a temporary breakpoint.

It looks like this:

b ParentFunction
command 1
  tb FunctionImInterestedIn
  c
end

Every time you break in ParentFunction, you'll set a one-time breakpoint on the function you're actually interested in, then continue running (presumably until you hit that breakpoint).

Since you'll break exactly once on FunctionImInterestedIn, this won't work if FunctionImInterestedIn is called multiple times in the context of ParentFunction and you want to break on each invocation.

染墨丶若流云 2024-10-31 08:33:03

不知道如何通过 gdb 来做到这一点。
但是您可以声明全局变量,例如:

bool call_a = false;

当 b 调用 a

call_a = true;   
a();

并将 call_a 设置为 false 当其他函数调用 a() 或在断点之后

然后使用条件断点

break [line-number] if call_a == true

not sure how to do it by gdb.
But you can declare global variable like:

bool call_a = false;

and when b calling a

call_a = true;   
a();

and set call_a to false when other function call a() or after your breakpoint

then use condition break-point

break [line-number] if call_a == true
缱绻入梦 2024-10-31 08:33:03

对于arm来说,一个简单的方法是:

在您感兴趣的函数中设置断点。

break a

将 gdb 命令附加到该断点。

command 1
 up 1
 if $lr == 0x12345678
  echo match \n
  down 1
 else
  echo no match \n
  echo $lr \n
  down 1
  cont 
 end 
end 

当您到达函数 a() 时,该命令会暂时弹出一个堆栈帧,从而更新链接寄存器。当调用者未执行时,可以使用调用者链接寄存器值继续
你需要的路径。

享受。

An easy one for arm is:

Set the breakpoint in the function you are interested.

break a

Attach an gdb command to that breakpoint.

command 1
 up 1
 if $lr == 0x12345678
  echo match \n
  down 1
 else
  echo no match \n
  echo $lr \n
  down 1
  cont 
 end 
end 

When ever you arrive in the function a(), the command temporarily pops up one stack frame thus updating the link register. The callers link register value can then be used continue when the caller is not the execution
path you need.

Enjoy.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文