dan*_*9er 11 c++ linux debugging gdb clang
我正在尝试调试作业程序中的段错误,我发现我的 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
Run Code Online (Sandbox Code Playgroud)
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".
Run Code Online (Sandbox Code Playgroud)
我编写了一个名为 gdbbroke.cpp 的小测试程序:
$ clang++ --version
clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
我希望print test输出:
(gdb) print test
$1 = "sanity check"
Run Code Online (Sandbox Code Playgroud)
然而,它只是抛出上面的 Python 错误。
ks1*_*322 10
使用 Clang,要打印字符串,您需要安装 libstdc++ 的调试符号。看到这个 Clang 错误被解决为 INVALID https://bugs.llvm.org/show_bug.cgi?id=24202。
如果您安装 libstdc++ 调试符号,则应打印该字符串。另一方面,您可以简单地使用 GCC 而不是 Clang。在这种情况下,您不需要安装 libstdc++ 调试符号,因为 GCC 已经发出它们。Clang 不会发出它们,因为它会进行调试信息优化,而 GCC 不会这样做。另请参阅相关问题使用 clang 编译时无法查看 std::string。