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;
}
我將其另存為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
func從 GDB呼叫按預期作業:
(gdb) call (void)func()
func
然而,其他人不會:
(gdb) call (void)ns::test()
No symbol "ns" in current context.
(gdb) call (void)cl::test()
No symbol "cl" in current context.
如果使用 編譯它可以正常作業-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
如果我按 Tab 鍵,即使在call命令中,這些名稱也會自動完成,這意味著在這種情況下,它會自動完成一些不起作用的內容。
此外,如果我使用它們的原始名稱,呼叫這些函式可以正常作業:
(gdb) call (void)_ZN2ns4testEv()
ns::test
(gdb) call (void)_ZN2cl4testEv()
cl::test
那么這里有什么關系呢?這是 GDB 中的錯誤,還是有某種我不知道的特殊語法?
uj5u.com熱心網友回復:
您的 C 編譯器請放入ns::test符號表。我們需要做的就是阻止 GDB 的運算式求值器嘗試查找不存在的符號ns。為此,請將整個函式名稱放在單引號中。
(gdb) call (void)'ns::test'()
ns::test
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/322005.html
上一篇:除錯時jest測驗不按順序執行
