Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Python Exception <class 'gdb.error'> There is no member named _M_dataplus." when trying to print string

I'm trying to debug a segfault in a homework program and I've discovered that my GDB can no longer even print std::strings. How can I fix it?

I'm on Ubuntu 18.04 LTS.

CLang++ version:

$ 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 version:

$ 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".

I've writen a small test program called 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)

I expected print test to output:

(gdb) print test
$1 = "sanity check"

However it just throws the Python error above.

like image 905
dan9er Avatar asked Oct 12 '19 16:10

dan9er


1 Answers

With Clang, to print the string you need debug symbols of libstdc++ to be installed. See this Clang bug resolved as INVALID https://bugs.llvm.org/show_bug.cgi?id=24202.

The string should be printed if you install libstdc++ debug symbols. On the other hand you can simply use GCC instead of Clang. In that case you don't need to install libstdc++ debug symbols because GCC already emits them. Clang does not emit them because it does debug information optimization while GCC does not do it. See also related question Cannot view std::string when compiled with clang.

like image 55
ks1322 Avatar answered Nov 10 '22 19:11

ks1322