【问题标题】:Using libc++ causes GDB to segfault on OS X使用 libc++ 会导致 GDB 在 OS X 上出现段错误
【发布时间】:2013-09-23 15:07:02
【问题描述】:

我正在尝试将 C++11(在 OS X 上使用 Clang 和 libc++)用于程序,但每当我使用 gdb 进行调试并尝试检查标准容器时,gdb segfaults。这是一个最小的例子:

文件.cpp:

#include <iostream>
#include <string>

int main(int argc, char* argv[])
{
    std::string str = "Hello world";

    std::cout << str << std::endl; // Breakpoint here
}

如果我使用以下代码编译 C++11:

$ c++ --version
Apple LLVM version 4.2 (clang-425.0.28) (based on LLVM 3.2svn)
Target: x86_64-apple-darwin12.4.0
Thread model: posix
$
$ c++ -ggdb -std=c++11 -stdlib=libc++ -Wall -pedantic -O0 -c file.cpp
$ c++ -ggdb -std=c++11 -stdlib=libc++ -Wall -pedantic -O0 file.o -o program

然后调试如下,当我尝试p str.size()时它崩溃了:

$ gdb program
GNU gdb 6.3.50-20050815 (Apple version gdb-1824) (Wed Feb  6 22:51:23 UTC 2013)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries ... done

(gdb) br file.cpp:8
Breakpoint 1 at 0x100000d80: file file.cpp, line 8.
(gdb) run
Starting program: /Users/mjbshaw/School/cs6640/2/program 
Reading symbols for shared libraries ++............................. done

Breakpoint 1, main (argc=1, argv=0x7fff5fbffab0) at file.cpp:8
8       std::cout << str << std::endl; // Breakpoint here
(gdb) p str.size()

Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000000
std::__1::operator<< <char, std::__1::char_traits<char>, std::__1::allocator<char> > (__os=@0x7fff5fc3d628, __str=@0x1) at string:1243
1243    
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on"
Evaluation of the expression containing the function (std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >::__is_long() const) will be abandoned.

如果我不在 gdb 中运行它,我不会崩溃并且它工作正常(但我需要 gdb 来调试我的程序)。另外,如果我从编译选项中删除 -std=c++11 -stdlib=libc++,那么它可以正常工作(即使在 gdb 中),但我的程序需要 C++11。

gdb 和 C++11(特别是 libc++)是否存在一些已知问题?我知道如果一起使用 libc++ 和 libstdc++ 可能会导致问题,但我并没有尝试将它们一起使用(至少不是有意识的;我只想使用 libc++)。我是否指定了一些错误的编译选项?有没有办法在 OS X 上为 C++11 正确编译并且仍然能够正确调试?

【问题讨论】:

  • gdb 6.3 是一个化石。升级到最新版本。
  • @n.m.如果您发布为答案,我会接受。我刚刚更新到 7.6.1,它现在可以工作了。诅咒 Apple 运送旧的 GDB。
  • 对于那些想知道我如何更新 GDB 的人,我使用了 the answer from here 并做了:brew install https://raw.github.com/Homebrew/homebrew-dupes/master/gdb.rb。请注意,您需要 codesign the new gdb 以非 root 用户身份进行调试。
  • 在 OS X 上使用 Clang/libc++,使用lldb 可能会更好。
  • 请注意,如果您通过 Xcode 而不是通过命令行运行 gdb,请不要尝试按照上述 cmets 中的说明升级 gdb。我只是浪费了很多时间试图只发现 xcode 使用的 gdb 被大量修改为仅适用于 Xcode。您能做的最好的事情是升级到 Xcode 4.6.3 附带的最新 gdb(它实际上在 Xcode 3 中的 Mavericks 上工作,而 Xcode 3 附带的 gdb 在 Mavericks 上崩溃)。 Xcode 5 及更高版本仅附带 lldb。

标签: c++ macos c++11 gdb libc++


【解决方案1】:

GDB 6.3 已经快九岁了。这只是互联网时代的永恒。从那时起,该产品有了很大的改进。每个开发者都必须更新到最新的稳定版本。

【讨论】:

    猜你喜欢
    • 2016-03-22
    • 2014-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    相关资源
    最近更新 更多