gpt4 book ai didi

c++ - GNU gdb 无法进入模板函数(OS X Mavericks)

转载 作者:可可西里 更新时间:2023-11-01 16:21:15 26 4
gpt4 key购买 nike

我已经安装了 gdb 7.7 (来自 GNU 来源)在 OS X Mavericks (10.9.2) 下。我对它进行了代码签名,所以每当我调试 c++ 时它都能正常工作不包含模板的文件。但是,它无法进入模板函数(可以进入常规函数,但无法进入模板函数)。当我输入 step命令在调试 session 中,它只是跳过函数,就好像模板函数的调试符号不存在一样。我的模板函数甚至不是成员函数,只是在 main.cpp 中定义的一个简单函数.

我用 g++ -g -O0 main.cpp 编译我的程序(我使用 g++4.8.2 来自 macports 而不是 clang++ ),甚至尝试了 -fno-inline ,仍然是相同的行为,无法进入模板函数。这是gdb吗? OS X Mavericks 下的已知问题?或者,任何人都可以重现该错误吗?

我的代码:

#include <iostream>

template <typename T> // template
void f(T x)
{
std::cout << "In f:" << std::endl;
std::cout << x << std::endl;
}

void g() // non-template
{
std::cout << "It works here!" << std::endl;
std::cout << "Exiting g()..." << std::endl;
}

int main()
{
f<double>(12.3); // gdb does not step into f()
g(); // gdb steps into g()
}

不能介入ff<double>(12.3) 处的断点之后.如果f()是一个常规函数,例如 g()在我的代码中,然后它进入。谢谢!

更新:gdb在任何其他操作系统下都可以正常工作,例如 linux/windows/solaris .这似乎是与 OS X 相关的问题。

Reading symbols from a.out...done.
(gdb) disassemble main
Dump of assembler code for function main():
0x0000000100000d62 <+0>: push %rbp
0x0000000100000d63 <+1>: mov %rsp,%rbp
0x0000000100000d66 <+4>: movabs $0x402899999999999a,%rax
0x0000000100000d70 <+14>: movq %rax,%xmm0
0x0000000100000d75 <+19>: callq 0x100000e44
0x0000000100000d7a <+24>: callq 0x100000d0c <g()>
0x0000000100000d7f <+29>: mov $0x0,%eax
0x0000000100000d84 <+34>: pop %rbp
0x0000000100000d85 <+35>: retq
End of assembler dump.
(gdb)

现在我们在main()的入口处设置断点

(gdb) b main
Breakpoint 1 at 0x100000d66: file src/templated_bug.cpp, line 22.
(gdb) r
Starting program: /Users/vlad/template_gdb_bug/a.out

Breakpoint 1, main () at src/templated_bug.cpp:22
22 f<double>(12.3);
(gdb) s
In f:
12.3
23 g();
(gdb) s
g () at src/templated_bug.cpp:16
16 cout<<"It works here!"<<endl;
(gdb) s
It works here!
17 cout<<"Exiting g()..."<<endl;
(gdb) s
Exiting g()...
18 }
(gdb) s
main () at src/templated_bug.cpp:24
24 }
(gdb) s
[Inferior 1 (process 50945) exited normally]
(gdb)

如您所见,它并没有进入f()但走进去g() .此外,

(gdb) disassemble f
No symbol "f" in current context.

然而,对于另一个函数g()那是非模板,符号已加载,我可以反汇编它。

(gdb) disassemble g
Dump of assembler code for function g():
0x0000000100000d0c <+0>: push %rbp
0x0000000100000d0d <+1>: mov %rsp,%rbp
0x0000000100000d10 <+4>: lea 0x193(%rip),%rsi # 0x100000eaa
0x0000000100000d17 <+11>: mov 0x2ea(%rip),%rax # 0x100001008
0x0000000100000d1e <+18>: mov %rax,%rdi
0x0000000100000d21 <+21>: callq 0x100000e5c
0x0000000100000d26 <+26>: mov 0x2e3(%rip),%rdx # 0x100001010
0x0000000100000d2d <+33>: mov %rdx,%rsi
0x0000000100000d30 <+36>: mov %rax,%rdi
0x0000000100000d33 <+39>: callq 0x100000e4a
0x0000000100000d38 <+44>: lea 0x17a(%rip),%rsi # 0x100000eb9
0x0000000100000d3f <+51>: mov 0x2c2(%rip),%rax # 0x100001008
0x0000000100000d46 <+58>: mov %rax,%rdi
0x0000000100000d49 <+61>: callq 0x100000e5c
0x0000000100000d4e <+66>: mov 0x2bb(%rip),%rdx # 0x100001010
0x0000000100000d55 <+73>: mov %rdx,%rsi
0x0000000100000d58 <+76>: mov %rax,%rdi
0x0000000100000d5b <+79>: callq 0x100000e4a
0x0000000100000d60 <+84>: pop %rbp
0x0000000100000d61 <+85>: retq
End of assembler dump.

最佳答案

在 gdb 的 MacOS 端口上,step into 命令似乎存在问题。作为解决方法,您可以在函数开始处设置断点以进入模板函数:

(gdb) b f<double>
Breakpoint 1 at 0x1000015ab: file ./gdb_tmpl.cpp, line 6.
(gdb) run
Starting program: /Users/vershov/tmp/tests/a.out

Breakpoint 1, f<double> (x=12.300000000000001) at ./gdb_tmpl.cpp:6
6 std::cout << "In f:" << std::endl;

另外,你也可以反汇编它,只需要使用正确的命令:

(gdb) disass f
No symbol "f" in current context.
(gdb) disass f<double>
Dump of assembler code for function f<double>(double):
0x0000000100001590 <+0>: push %rbp
0x0000000100001591 <+1>: mov %rsp,%rbp
0x0000000100001594 <+4>: sub $0x40,%rsp
0x0000000100001598 <+8>: mov 0xa71(%rip),%rdi # 0x100002010
0x000000010000159f <+15>: lea 0x9a0(%rip),%rsi # 0x100001f46
...

关于c++ - GNU gdb 无法进入模板函数(OS X Mavericks),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23330641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com