c++ - mac 上 gdb 7.6 断言缺少调用堆栈帧

标签 c++ macos gdb

在调试断言失败的程序时,我无法在 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

这是重构问题的最小测试

//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
}

编译和使用

/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 ?? ()

最佳答案

对于 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();
}

我已调用 g++ -g 15.cpp -m32 命令编译它并在 ggdb 下运行它。 bt full 命令显示调用堆栈如下:

(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

因此,所有调试符号都正确显示,前 3 个函数地址已更正并且没有名称,因为我的 libgcc 处于 Release模式。

如果我在编译时不使用-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.

那肯定是错误的调用堆栈,#2 帧函数地址是 0x0。所以,根本原因是 gdb 无法正确显示 64 位应用程序的调用堆栈。

关于c++ - mac 上 gdb 7.6 断言缺少调用堆栈帧,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909547/

相关文章:

c++ - wchar_t 对 Windows API 有用吗?

c++ - 编译时是否需要动态库

cocoa - 更改 NSOpenPanel (Cocoa OSX) 取消按钮的文本

xcode - 使用终端和git时出现Xcode错误

Python 多处理故障排除

Objective-C - 使用 GDB 打印方法参数

c++ - 为什么基于指针交换两个值在函数范围之外不起作用?

c++ - constexpr 可以和 volatile 结合使用吗?

debugging - GDB - 获取当前线程调用堆栈上的帧总数

function - 如何在没有痛苦的类型转换的情况下评估 GDB 中的函数?