2013-09-23 46 views
0

我想爲程序使用C++ 11(在OS X上使用Clang和libC++),但是每當我使用gdb進行調試並嘗試檢查標準容器時,gdb segfaults 。這裏有一個小例子:使用libC++會導致GDB在OS X上發生段錯誤

file.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> > ([email protected], [email protected]) 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. 

如果我d不要在gdb中運行這個,我沒有崩潰,它工作正常(但我需要gdb來調試我的程序)。另外,如果我從編譯選項中刪除-std=c++11 -stdlib=libc++,那麼它工作正常(即使在gdb中),但我需要C++ 11用於我的程序。

gdb和C++ 11(特別是libC++)有一些已知問題嗎?我知道如果一起使用libC++和libstdC++可能會導致問題,但我並未嘗試將它們一起使用(至少不自覺;我想要使用的是libC++)。我是否指定了一些編譯選項錯誤?有沒有辦法在OS X上正確編譯C++ 11並仍然能夠正確調試?

+3

gdb 6.3是一個化石。升級到最新版本。 –

+0

@ n.m。如果您發佈答案,我會接受。我剛剛更新到7.6.1,現在正在工作。詛咒蘋果運送老GDB。 – Cornstalks

+0

對於那些想知道我如何更新GDB的人,我使用[來自這裏的答案](http://stackoverflow.com/questions/8336433/gdb-on-macosx-lion)並且做了:'brew install https:// raw .github.com /自制/自制-愚弄/主/ gdb.rb'。請注意,您需要[用新的gdb](http://sourceware.org/gdb/wiki/BuildingOnDarwin)對其進行調試,將其作爲非root用戶進行調試。 – Cornstalks

回答

1

GDB 6.3已經快9歲了。這只是互聯網時代的永恆。從那以後,產品大大改善了。更新到上一個穩定版本是每個開發人員必須的。