在调试一个失败的程序时,我无法在gdb中获得调用堆栈.我在Mavericks上使用了Homebrew的g ++ 4.8和gdb.
/usr/local/bin/g++-4.8 --version
g++-4.8 (GCC) 4.8.2
/usr/local/bin/gdb --version
GNU gdb (GDB) 7.6.2
Run Code Online (Sandbox Code Playgroud)
这是重建问题的最小测试
//test.cpp
#include <iostream>
#include <cassert>
int main()
{
int i = 42;
std::cout << "Hello World!" << i << std::endl;
assert(0); // this also happens with abort() which assert(0) winds up calling
}
Run Code Online (Sandbox Code Playgroud)
编译和使用
/usr/local/bin/g++-4.8 -g -c test.cpp -o test.o
/usr/local/bin/g++-4.8 -g test.o -o test
/usr/local/bin/gdb test
(gdb) r
Starting program: /Users/pmelsted/tmp/test/test
Hello World!42
Assertion failed: (0), function main, file test.cpp, line 7.
Program received signal SIGABRT, Aborted.
0x00007fff9447d866 in ?? ()
(gdb) where
#0 0x00007fff9447d866 in ?? ()
#1 0x00007fff9229835c in ?? ()
#2 0x0000000000000000 in ?? ()
Run Code Online (Sandbox Code Playgroud)
对于64位程序,MacOS上的gdb似乎没有正确显示调用堆栈(或者在assert()函数调用之后调用堆栈已损坏).这是程序略有修改:
//test.cpp
#include <iostream>
#include <cassert>
int foo() {
assert(0);
}
int bar() {
return foo();
}
int main()
{
int i = 42;
std::cout << "Hello World!" << i << std::endl;
return bar();
}
Run Code Online (Sandbox Code Playgroud)
我编译它调用g ++ -g 15.cpp -m32命令并在ggdb下运行它.该BT全命令显示调用堆栈,如下所示:
(gdb) bt full
#0 0x9843f952 in ?? ()
No symbol table info available.
#1 0x96193340 in ?? ()
No symbol table info available.
#2 0x9615e43e in ?? ()
No symbol table info available.
#3 0x0000216f in foo () at 15.cpp:6
No locals.
#4 0x0000217b in bar () at 15.cpp:9
No locals.
#5 0x000021e4 in main () at 15.cpp:15
i = 42
(gdb) quit
Run Code Online (Sandbox Code Playgroud)
因此,所有调试符号都正确显示,前3个函数地址已更正且没有名称,因为我的libgcc处于发布模式.
如果我在编译期间不使用-m32键,则调用堆栈如下:
(gdb) bt full
#0 0x00007fff8b442866 in ?? ()
No symbol table info available.
#1 0x00007fff8c64735c in ?? ()
No symbol table info available.
#2 0x0000000000000000 in ?? ()
No symbol table info available.
Run Code Online (Sandbox Code Playgroud)
那肯定是错误的调用栈,#2帧功能地址是0x0.因此,根本原因是gdb无法正确显示64位应用程序的调用堆栈.
| 归档时间: |
|
| 查看次数: |
1191 次 |
| 最近记录: |