8

I am not able to dump STL unordered map container values in gdb. variable type is std::unordered_map<> var;

my gdb version - 7.7.1 Gdb configuration:

 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

what is right way to print STL container values n gdb?

gdb output of map container:

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> > >
user1762571
  • 1,888
  • 7
  • 28
  • 47
  • Show us, properly, what you tried. This output suggests something that is _not_ a `std::map`. – Lightness Races in Orbit May 05 '17 at 14:52
  • 1
    It suggests it is a std::unordered_map, though –  May 05 '17 at 14:58
  • I suspect the OP's libstdc++ is simply too old to have a pretty-printer for that. I spent a while bisecting the libstdc++ repo for gcc/libstdc++-v3/python/libstdcxx/v6/printers.py, but the github UI is pretty terrible and I gave up trying to come up with a version number to go off. In any case, OP needs to provide many more details. – Lightness Races in Orbit May 05 '17 at 15:07
  • What is the output of `ptype /tm your_variable` ? – Mark Plotnick May 05 '17 at 16:48
  • What is the output of `info pretty-printer` ? Are pretty-printers properly installed ? – ks1322 May 06 '17 at 09:56
  • (gdb) info pretty-printer Undefined info command: "pretty-printer". Try "help info". – user1762571 May 06 '17 at 17:50
  • The native gdb that comes with ubuntu 14.04 has pretty-printer support, so that error message from `info pretty-printer` is odd. Are you using the gdb from /usr/bin, or some customized version? – Mark Plotnick May 07 '17 at 21:24
  • Also uploading an output of `gdb --configuration` would help. There you can see how your gdb was configured. – pe3k May 08 '17 at 08:58
  • Would this work for you http://stackoverflow.com/q/31062010/72178 ? – ks1322 May 08 '17 at 10:46
  • @ks1322 I am getting this error. -bash: /home/admin/.gdbinit: line 1: syntax error near unexpected token `(' -bash: /home/admin/.gdbinit: line 1: `python sys.path.append("/usr/share/gcc-4.8/python");' – user1762571 May 08 '17 at 18:42
  • @Mark Plotnick I am using /usr/bin/gdb – user1762571 May 08 '17 at 18:52
  • @user1762571, you are doing it a bit wrong. `.gdbinit` is not a shell script, it is gdb startup script, it should get executed at gdb startup. Are there any errors like in http://stackoverflow.com/q/31062010/72178 when launching gdb with modified (or not modified) `.gdbinit` ? – ks1322 May 08 '17 at 19:31
  • @ ks1322 gdb is picking up this file. I just ran source .gdbinit and got above error – user1762571 May 08 '17 at 22:47

5 Answers5

5

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

And adding these line's to your: ~/.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.

Note: Assuming your GDB has python back-end enabled.

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.
With this update gdb should automatically support pretty printing for STL container.

AmeyaVS
  • 814
  • 10
  • 26
4

Your gdb --configuration output is missing --with-python clause, so I assume your gdb really cannot use python extensions. According to this SO answer gdb pretty printing is not working it seems to be necessary for pretty-print to work.

My ubuntu 14.04 in docker comes with pretty-print working as well as gdb is configured using --with-python. It seems, your gdb installation is somehow customized. You can either recompile the gdb from sources with correct options or try to clean reinstall gdb from your distribution packages.

mosh
  • 1,402
  • 15
  • 16
pe3k
  • 796
  • 5
  • 15
2

1) Python is not required by older versions of gdb to print STL objects. The error with python has something to do with your configs.

gdbinit is not a gdb config

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.

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. Make sure you get the right one, in mine opinion it should nothing to do with 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

iantonuk
  • 1,178
  • 8
  • 28
0

My guess is that you have 2 gdb binaries installed in your system:

  • the one that comes with 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. This gdb binary is invoked when you type gdb in command prompt.

But there should be another binary installed from Ubuntu dpkg package. It should be located in /usr/bin/gdb and should support pretty printing. Try to invoke it with full path:

/usr/bin/gdb
ks1322
  • 33,961
  • 14
  • 109
  • 164
0
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
  • 4
    Thank you for this code snippet, which might provide some limited short-term help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its long-term value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you've made. – Toby Speight Jan 18 '18 at 11:05