Search code examples
c++stlgdbpretty-print

pretty print not working for c++ stl list


While using gdb for the following code, it appears that pretty print works as expected for stl vector and strings but the output for list seems cryptic.

   list<int> l;
   string a = "Hello";
   vector<int> v(2,3);
   l.push_back(5);
   l.push_back(10);

Output of gdb pretty print:

(gdb) set print pretty on
(gdb) disp v
1: v = std::vector of length 2, capacity 2 = {3, 3}
(gdb) disp a
2: a = "Hello"
(gdb) disp l
3: l = {
  <std::__cxx11::_List_base<int, std::allocator<int> >> = {
    _M_impl = {
      <std::allocator<std::_List_node<int> >> = {
        <__gnu_cxx::new_allocator<std::_List_node<int> >> = {<No data fields>}, <No data fields>}, 
      members of std::__cxx11::_List_base<int, std::allocator<int> >::_List_impl: 
      _M_node = {
        <std::__detail::_List_node_base> = {
          _M_next = 0x615c40, 
          _M_prev = 0x615c60
        }, 
        members of std::_List_node<unsigned long>: 
        _M_data = 2
      }
    }
  }, <No data fields>}

Can someone please point out what I am doing wrong?

UPD1: I am using the following versions of gcc and gdb with Ubuntu 16.04 on x86-64

gcc 5.4.1
gdb 7.11.1

I tried using gcc 6.2.0, but the same problem remains ...

UPD2: It appears that the pretty printer for list is enabled (does not have [disabled] next to it)

(gdb) info pretty-printer 
global pretty-printers:
  builtin
    mpx_bound128
  objfile /usr/lib/x86_64-linux-gnu/libstdc++.so.6 pretty-printers:
  libstdc++-v6
    __gnu_cxx::_Slist_iterator
    __gnu_cxx::__7::_Slist_iterator
    ..
    ..
    std::__7::forward_list
    std::__7::list
    std::__7::map

Solution

  • Printing out the data structures nicely is a function (no pun intended) of some Python code that extends GDB. There is a section of the GDB manual on pretty printing in GDB.

    It turns out that for shared libraries (and possibly for statically linked libraries too, it's not completely clear) that GDB has a way to automatically load them. On my Fedora 25 system GDB autoloads /usr/share/gdb/auto-load/usr/lib64/libstdc++.so.6.0.22-gdb.py and this file loads up the libstdc++ pretty printers.

    The online manual has a pretty extensive section on writing your own GDB pretty printer in Python.

    For you to be having the specific problem you're having, something must be fairly messed up about the libstdc++ pretty printers since the one for ::std::vector appears to be turned on and working and the one for ::std::list does not. Perhaps that's exactly what happened. Somehow the one for ::std::list was de-registered or turned off. The section on how GDB selects a pretty printer says that it's possible to enable or disable them on an individual basis in order to deal with non-working ones.