简体   繁体   English

如何在gdb中转储STL容器数据?

[英]How to dump STL container data in gdb?

I am not able to dump STL unordered map container values in gdb. 我无法在gdb中转储STL无序映射容器值。 variable type is std::unordered_map<> var; 变量类型是std :: unordered_map <> var;

my gdb version - 7.7.1 Gdb configuration: 我的gdb版本 - 7.7.1 Gdb配置:

 configure --host=x86_64-linux-gnu --target=x86_64-linux-gnu
             --with-auto-load-dir=$debugdir:$datadir/auto-load
             --with-auto-load-safe-path=$debugdir:$datadir/auto-load
             --with-expat
             --with-gdb-datadir=/usr/local/share/gdb (relocatable)
             --with-jit-reader-dir=/usr/local/lib/gdb (relocatable)
             --without-libunwind-ia64
             --with-lzma
             --with-separate-debug-dir=/usr/local/lib/debug (relocatable)
             --with-system-gdbinit=/etc/gdb/gdbinit
             --with-zlib
             --without-babeltrace

g++ (Ubuntu 4.8.4-2ubuntu1~14.04.3) 4.8.4 g ++(Ubuntu 4.8.4-2ubuntu1~14.04.3)4.8.4

what is right way to print STL container values n gdb? 什么是打印STL容器值n gdb的正确方法?

gdb output of map container: 地图容器的gdb输出:

p var p var

$3 = {<std::__allow_copy_cons<true>> = {<No data fields>},                                                                                                                          [13/5219]
  _M_h = {<std::__detail::_Hashtable_base<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<ch
ar> > const, Metrics_s*>, std::__detail::_Select1st, std::equal_to<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::hash<std::basic_string<char, std::ch
ar_traits<char>, std::allocator<char> > >, std::__detail::_Mod_range_hashing, std::__detail::_Default_ranged_hash, std::__detail::_Hashtable_traits<true, false, true> >> = {<std::__detail::
_Hash_code_base<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char> > const, Metric
s_s*>, std::__detail::_Select1st, std::hash<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >

Your gdb --configuration output is missing --with-python clause, so I assume your gdb really cannot use python extensions. 你的gdb --configuration输出缺少--with-python子句,所以我假设你的gdb实际上不能使用python扩展。 According to this SO answer gdb pretty printing is not working it seems to be necessary for pretty-print to work. 根据这个SO回答gdb漂亮的打印不起作用似乎有必要漂亮的打印工作。

My ubuntu 14.04 in docker comes with pretty-print working as well as gdb is configured using --with-python. 我的ubuntu 14.04在docker中配备了漂亮的打印工作,并且gdb是使用--with-python配置的。 It seems, your gdb installation is somehow customized. 看来,你的gdb安装是以某种方式定制的。 You can either recompile the gdb from sources with correct options or try to clean reinstall gdb from your distribution packages. 您可以使用正确的选项从源重新编译gdb,也可以尝试从分发包中清除重新安装gdb。

Try checking out this: https://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python/ 试试看看: https//gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python/

And adding these line's to your: ~/.gdbinit 并将这些行添加到您的: ~/.gdbinit

python
import sys
sys.path.insert(0, '<Path to SVN Checkout Directory>')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

If it doesn't work you can checkout an older release from SVN nearer to the version of GDB you are using. 如果它不起作用,您可以从SVN中检出较旧的版本,该版本更接近您正在使用的GDB版本。

Note: Assuming your GDB has python back-end enabled. 注意:假设您的GDB启用了python后端。

Update: In-case you are using the gdb package from Ubuntu packages you can try to install following package to add support for "STL pretty-printing" : libstdc++6-4.8-dbg . 更新:如果您正在使用Ubuntu软件包中的gdb软件包,您可以尝试安装以下软件包以添加对“STL漂亮打印”的支持libstdc++6-4.8-dbg
With this update gdb should automatically support pretty printing for STL container. 有了这个更新,gdb应该自动支持STL容器的漂亮打印。

1) Python is not required by older versions of gdb to print STL objects. 1)旧版本的gdb不需要Python来打印STL对象。 The error with python has something to do with your configs. python的错误与你的配置有关。

gdbinit is not a gdb config gdbinit不是gdb配置

2) There is a solution that will work regardless: uninstall and reinstall old ones(look for stl pretty print packages on your distribution) gdb dbg packages also check your user's .bashrc (you may do something with gdb there which you don't want), purge it, restart terminal and it will work. 2)有一个解决方案无论如何都可以工作:卸载并重新安装旧的解决方案(在你的发行版上查找stl漂亮的打印包)gdb dbg包也检查你的用户的.bashrc(你可以用gdb做一些你不想要的东西) ),清除它,重新启动终端,它会工作。

Note there are recent versions of gdb that need python of only particular version and flavor and they have quite a few bugs and some Linux distributions include them as default and it's their problem, gdb is not a one thing - it's a tree of things. 注意有最新版本的gdb需要python只有特定的版本和风格 ,它们有很多错误,一些Linux发行版包含它们作为默认值,这是他们的问题,gdb不是一回事 - 它是一个事物树。 Make sure you get the right one, in mine opinion it should nothing to do with python. 确保你得到正确的,我认为它应该与python无关。

Here is the link describing on when that bad idea was introduced in gdb and why people seemed to like it https://bbs.archlinux.org/viewtopic.php?id=87299 下面是描述gdb中引入这个坏主意的链接以及为什么人们似乎喜欢它https://bbs.archlinux.org/viewtopic.php?id=87299

My guess is that you have 2 gdb binaries installed in your system: 我的猜测是你的系统中安装了2个gdb二进制文件:

  • the one that comes with Ubuntu 14.04 Ubuntu 14.04附带的那个
  • the one that you have built yourself 你自己建造的那个

The one that you have built yourself does not support python pretty printers as it was not configured with --with-python option, according to gdb --configure output. 根据gdb --configure输出,你自己构建的那个不支持python pretty打印机,因为它没有配置--with-python选项。 This gdb binary is invoked when you type gdb in command prompt. 在命令提示符下键入gdb时,将调用此gdb二进制文件。

But there should be another binary installed from Ubuntu dpkg package. 但是应该从Ubuntu dpkg包中安装另一个二进制文件。 It should be located in /usr/bin/gdb and should support pretty printing. 它应该位于/usr/bin/gdb并且应该支持漂亮的打印。 Try to invoke it with full path: 尝试使用完整路径调用它:

/usr/bin/gdb
define dump_map
    #arg0 is  your unordered map address
    set $map = $arg0
    set $i = 0
    set $itr = $map._M_h._M_buckets[0]->_M_nxt
    while $i < $map._M_h._M_element_count
        printf "Object name = [ %s ] and Object address = [0x%llx]\n", ((char *)((<give your map name> *)($itr))->_name) , $itr
        set $itr  = $itr->_M_nxt
        set $i = ($i + 1)
    end 
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM