在gdb-C++中打印流值

Print stream value in gdb - C++

本文关键字:打印 gdb-C++      更新时间:2023-10-16

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

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

我试着用以下方式来做,但似乎都不起作用:

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

等等

有没有办法,看看流的价值?

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

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

我用-D_GLIBCXX_DEBUG重新编译了所有内容(而不仅仅是一两个翻译单元(,得到了我需要的东西。然后我就可以做了

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

其中CCD_ 5是CCD_。以前我得到

(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

快速解决方案

要确定哪个版本的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-00000000000000000", 
    _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)

键入ls /home/zephyr/utils/gdb-8.3-install/share/gdb查看gdb-datadir内部。如果您没有看到python文件夹,则需要安装支持pythongdb。在配置、编译和安装gdb之前,请确保已安装python-dev。现在按照本页上的说明安装漂亮的打印机:https://sourceware.org/gdb/wiki/STLSupport.

祝贺你!您现在安装了漂亮的打印机。

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

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

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

因为stringstreamstr方法给出std::string,而stringc_str方法给出了char*

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

我从gdb std::ifstream::is_open方法中得到错误,找不到。我在寻找答案时找到了这个线索。如果您在Cygwin中工作,则不存在libstdc++6-8-dbg包。我发现安装Cygwin包gcc-debuginfo是有效的。