我正在尝试在家庭作业程序中调试段错误,并且发现我的GDB甚至无法再打印std :: strings。我该如何解决?
我正在使用Ubuntu 18.04 LTS。
CLang ++版本:
$ clang++ --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
GDB版本:
$ gdb --version
GNU gdb (Ubuntu 8.1-0ubuntu3.1) 8.1.0.20180409-git
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
我已经编写了一个名为gdbbroke.cpp的小型测试程序:
#include <string>
int main()
{
std::string test = "sanity check";
return 0;
}
gdbbroke$ clang++ -o gdbbroke gdbbroke.cpp -std=c++11 -Wall -Wextra -Wpedantic -Wconversion -Wnon-virtual-d
tor -ggdb
gdbbroke$ gdb ./gdbbroke
[...]
Reading symbols from ./gdbbroke...done.
(gdb) break main()
Breakpoint 1 at 0x4007a3: file gdbbroke.cpp, line 5.
(gdb) run
Starting program: gdbbroke/gdbbroke
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Breakpoint 1, main () at gdbbroke.cpp:5
5 std::string test = "sanity check";
(gdb) print test
$1 = Python Exception <class 'gdb.error'> There is no member named _M_dataplus.:
(gdb)
我希望print test
输出:
(gdb) print test
$1 = "sanity check"
但是它只是在上面抛出了Python错误。
答案 0 :(得分:1)
使用Clang,要打印字符串,需要安装libstdc ++的调试符号。 看到此Clang错误已解决为无效https://bugs.llvm.org/show_bug.cgi?id=24202。
如果安装libstdc ++调试符号,则应打印字符串。另一方面,您可以简单地使用GCC代替Clang。在那种情况下,您不需要安装libstdc ++调试符号,因为GCC已经发出了它们。 Clang不会发出它们,因为它会调试信息优化,而GCC不会。另请参阅相关问题Cannot view std::string when compiled with clang。