在gdb - C ++中打印流值

时间:2011-12-04 21:25:25

标签: c++ stream gdb

我正在尝试查看stream的值(ifstream,但它应该适用于所有类型的流我猜)。 示例代码可能如下所示:

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

我试图通过以下方式实现这一目标,但似乎没有一个能够发挥作用:

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

......等等。

有什么办法可以看到流的价值吗?

6 个答案:

答案 0 :(得分:11)

您必须确保包含使用调试标志编译的libstdc++库。

我安装了libstdc++6-8-dbg软件包,现在我可以在gdb中查看所有流对象数据了。

答案 1 :(得分:3)

通过使用-D_GLIBCXX_DEBUG重新编译所有内容(不只是一个或两个翻译单元),我得到了我所需要的内容。然后我就可以了

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

其中isstd::istream&。以前我得到了

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

另外,当我只重建一个编译单元时,它运行但是与

崩溃
...
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)

另请参阅:http://gcc.gnu.org/onlinedocs/libstdc++/manual/debug_mode_using.html#debug_mode.using.mode

答案 2 :(得分:1)

(gdb) call 'operator<<'(_ZSt4cout, your_object)

答案 3 :(得分:0)

您是否尝试过print in.str()甚至是print in.str().c_str()

因为stringstream has a str method提供std::stringstring has a c_str method提供char*

答案 4 :(得分:0)

没有。流的整个想法是它在数据可用时读取数据,无论是从硬盘驱动器,从网络还是从任何地方读取数据。例如,我可以编写一个支持流的类,它只是无限期地发出字符'a'。

如果你想这样做,你只需要在自己的程序中编写一个辅助函数,从函数流中读取所需的数据。

答案 5 :(得分:0)

快速解决方案

要找出哪个版本的libstdc ++-dbg软件包可以工作:在终端中键入apt-cache search libstdc++ | grep dbg。查找最新版本的软件包,格式为libstdc++6-5-dbg

在我的一台机器上libstdc++6-5-dbg可以工作,而在另一台libstdc++6-8-dbg上可以工作。

安装libstdc++6-8-dbg也对我有用。我有一个18.04的仿生海狸。早些时候,我尝试安装与我的libstdc ++-dev版本匹配的dbg版本,但是没有用。

彻底的解决方案:

  1. 如果尝试在gdb中打印字符串时看到<incomplete type>,则需要安装与libstdc++6-8-dbg类似的软件包,可供dist使用。运行ldd <executable>。您将看到类似以下的输出:
    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)

如果在libstdc++.so.6链接中看不到调试版本,请尝试使用locate libstdc++.so.6找到相应的库。在可执行文件的链接阶段,在调试目录中包含-L标志。还请在-rpath中包含相同目录,以将其包含在运行时库中。重新编译您的可执行文件。再次运行ldd <executable>,以验证是否包含调试目录。这样可以处理不完整的类型。

  1. 现在,如果您看到这样的输出,则尝试打印字符串时:
$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}}

然后您的gdb版本需要一台漂亮的打印机。首先验证gdb是否安装了python支持,可以通过在gdb中键入show configuration来找到它:

(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)

通过键入gdb-datadirls /home/zephyr/utils/gdb-8.3-install/share/gdb中查找。如果看不到python文件夹,则需要在gdb支持下安装python。在配置,编译和安装python-dev之前,请确保已安装gdb。现在,按照本页上的说明安装漂亮的打印机:https://sourceware.org/gdb/wiki/STLSupport

恭喜!您现在已经安装了漂亮的打印机。