尝试打印字符串时"Python Exception <class 'gdb.error'> There is no member named _M_dataplus."



我正在尝试调试家庭作业程序中的段错误,我发现我的 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错误。

使用 Clang,要打印字符串,您需要安装 libstdc++ 的调试符号。 请参阅此解决为无效 https://bugs.llvm.org/show_bug.cgi?id=24202 的 Clang 错误。

如果安装 libstdc++ 调试符号,则应打印该字符串。另一方面,您可以简单地使用GCC而不是Clang。在这种情况下,您不需要安装 libstdc++ 调试符号,因为 GCC 已经发出了它们。Clang不会发出它们,因为它会调试信息优化,而GCC不会这样做。另请参阅相关问题 使用 clang 编译时无法查看 std::string。

相关内容

最新更新