Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print stream value in gdb - C++

Tags:

c++

stream

gdb

I'm trying to see the value of stream (ifstream, but it should work for all kind of streams I guess). The example code could look like this:

stringstream in("One Two Three Four Five");
while(in)
cout << in;

I was trying to do it in following ways but none of them seems to work:

(gdb) print in
(gdb) call cout << in
(gdb) call in.getline()

... and so on.

Is there any way, to see the value of the stream?

like image 682
thim Avatar asked Dec 04 '11 21:12

thim


3 Answers

You have to make sure that you have the package with the libstdc++ library compiled with the debugging flags.

I have the libstdc++6-8-dbg package installed and now I can view all the stream object data in gdb.

like image 52
Jeremy Avatar answered Nov 15 '22 03:11

Jeremy


I got what I needed by recompiling everything (not just one or two translation units) with -D_GLIBCXX_DEBUG. Then I can just do

(gdb) p is.tellg()
$21 = {_M_off = 0, _M_state = {__count = 0, __value = {__wch = 0, __wchb = "\000\000\000"}}}
(gdb) 

where is is a std::istream&. Previously I was getting

(gdb) p is.tellg()
Couldn't find method std::istream::tellg
(gdb) p is

Also, when I only rebuilt one compilation unit, it ran but crashed with

...
305d85d000-305d85e000 r--p 0005d000 fd:01 1180082                        /lib64/libfreebl3.so
305d85e000-305d85f000 rw-p 0005e000 fd:01 118
Program received signal SIGABRT, Aborted.
0x0000003052e35215 in raise () from /lib64/libc.so.6
(gdb)

See also: http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_using.html#debug_mode.using.mode

like image 36
Ben Avatar answered Nov 15 '22 04:11

Ben


Quick Solution

To find out which version of libstdc++-dbg package will work: type apt-cache search libstdc++ | grep dbg in the terminal. Find the latest version package, which is in the format libstdc++6-5-dbg.

On one of my machines libstdc++6-5-dbg works, while on the other one libstdc++6-8-dbg does.

Installing libstdc++6-8-dbg worked for me too. I have a 18.04 bionic beaver. Earlier I tried installing a dbg version that matched my libstdc++-dev version, but that did not work.

Thorough Solution:

  1. If you see <incomplete type> when trying to print a string inside gdb, then you need to install a packages similar to libstdc++6-8-dbg available for your dist. Run ldd <executable> . You will see an output like:
    linux-vdso.so.1 =>  (0x00007ffe4cbea000)
    libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/debug/libstdc++.so.6 (0x00007f523eab1000)
    libmpi.so.12 => /opt/mpich-3.2/lib/libmpi.so.12 (0x00007f523e36c000)

If you do not see a debug version in the libstdc++.so.6 link, then try to locate the corresponding library using locate libstdc++.so.6. Include that debug directory with the -L flag during linking stage of your executable. Also include the same directory in -rpath to include it in the runtime library. Recompile your executable. Run again ldd <executable> to verify if the debug directory is included or not. This takes care of the incomplete type.

  1. Now while trying to print a string if you see an output like this:
$1 = {static npos = 18446744073709551615, 
  _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
    _M_p = 0x7fffffffda70 "dump-000"}, _M_string_length = 8, {_M_local_buf = "dump-000\000\000\000\000\000\000\000", 
    _M_allocated_capacity = 3472328284420535652}}

then your gdb version needs a pretty printer. First verify that the gdb is installed with python support, which can be found out by typing show configuration in gdb:

(gdb) show configuration
This GDB was configured as follows:
   configure --host=x86_64-pc-linux-gnu --target=x86_64-pc-linux-gnu
             --with-auto-load-dir=$debugdir:$datadir/auto-load
             --with-auto-load-safe-path=$debugdir:$datadir/auto-load
             --with-expat
             --with-gdb-datadir=/home/zephyr/utils/gdb-8.3-install/share/gdb (relocatable)
             --with-jit-reader-dir=/home/zephyr/utils/gdb-8.3-install/lib/gdb (relocatable)
             --without-libunwind-ia64
             --without-lzma
             --without-babeltrace
             --without-intel-pt
             --disable-libmcheck
             --without-mpfr
             --without-python
             --without-guile
             --disable-source-highlight
             --with-separate-debug-dir=/home/zephyr/utils/gdb-8.3-install/lib/debug (relocatable)

Look inside gdb-datadir by typing ls /home/zephyr/utils/gdb-8.3-install/share/gdb. If you do not see a python folder, then your gdb needs to be installed with python support. Ensure that python-dev is installed before configuring, compiling and installing your gdb. Now install the pretty printers by following the instructions given on this page: https://sourceware.org/gdb/wiki/STLSupport.

Congratulations! you now have pretty printers installed.

like image 2
CyclicUniverse Avatar answered Nov 15 '22 03:11

CyclicUniverse