fla*_*006 6 c++ debugging gdb namespaces
call只要符号存在,GDB 的命令通常非常适合调用函数。但是,如果该函数位于命名空间或类中,那么除非使用调试信息对其进行编译,否则它会突然无法工作。
例如,假设我有这个程序:
#include <iostream>
namespace ns {
void test()
{
std::cout << "ns::test" << std::endl;
}
}
struct cl {
static void test()
{
std::cout << "cl::test" << std::endl;
}
};
void func()
{
std::cout << "func" << std::endl;
}
int main()
{
ns::test();
cl::test();
func();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我将其保存为test.cpp,用 编译它g++ test.cpp -o test,然后在 GDB 中启动它:
$ gdb test
GNU gdb (GDB) 11.1
[...]
Reading symbols from test...
(No debugging symbols found in test)
(gdb) start
Run Code Online (Sandbox Code Playgroud)
从 GDB调用func按预期工作:
(gdb) call (void)func()
func
Run Code Online (Sandbox Code Playgroud)
然而,其他人则没有:
(gdb) call (void)ns::test()
No symbol "ns" in current context.
(gdb) call (void)cl::test()
No symbol "cl" in current context.
Run Code Online (Sandbox Code Playgroud)
如果它是用 编译的,它可以正常工作-ggdb,但如果源代码不可用,那么这通常不是一个选项。
值得指出的是,GDB 知道这些函数及其地址:
(gdb) info functions
...
0x0000000000001169 ns::test()
0x000000000000119b func()
0x000000000000124e cl::test()
...
(gdb) info symbol 0x1169
ns::test() in section .text
(gdb) break cl::test()
Breakpoint 1 at 0x1252
Run Code Online (Sandbox Code Playgroud)
如果我按 Tab 键,这些名称也会自动完成,即使是在call命令中,这意味着在这种情况下,它会自动完成一些不起作用的内容。
此外,如果我使用它们的原始名称,调用这些函数就可以正常工作:
(gdb) call (void)_ZN2ns4testEv()
ns::test
(gdb) call (void)_ZN2cl4testEv()
cl::test
Run Code Online (Sandbox Code Playgroud)
那么这是怎么回事呢?这是 GDB 中的错误,还是有某种我不知道的特殊语法?
请您的 C++ 编译器将其放入ns::test符号表中。我们需要做的就是阻止 GDB 的表达式求值器尝试查找不存在的符号ns。为此,请将整个函数名称放在单引号中。
(gdb) call (void)'ns::test'()
ns::test
Run Code Online (Sandbox Code Playgroud)