4

I am using Eclipse 4.4.2 on Ubuntu 14.04, and GDB 7.7.1. I am trying to inspect the contents of some C++ standard library containers in the Eclipse debugger.

What I have tried so far: following instructions here, I ran the command

svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python

Copying it into /home/myusername/prettyprint.

Then I copied this text into my .gdbinit:

python
import sys
sys.path.insert(0, '/home/myusername/prettyprint/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

When I ran gdb, I got this error message:

Traceback (most recent call last):
  File "<string>", line 4, in <module>
  File "/home/myusername/prettyprint/python/libstdcxx/v6/printers.py", line 1266, in register_libstdcxx_printers
    gdb.printing.register_pretty_printer(obj, libstdcxx_printer)
  File "/usr/myusername/gdb/python/gdb/printing.py", line 146, in register_pretty_printer
    printer.name)
RuntimeError: pretty-printer already registered: libstdc++-v6

I searched for help on this, and found that the second last line in the file (register_libstdcxx_printers (None)) was not necessary, so I removed it. Then, when I ran gdb and typed:

info pretty-print

I got this output, indicating that the backend was installed properly:

global pretty-printers:
  .*
    bound
  libstdc++-v6
    __gnu_cxx::_Slist_iterator
    __gnu_cxx::__7::_Slist_iterator
    __gnu_cxx::__7::__normal_iterator
    __gnu_cxx::__7::slist
    __gnu_cxx::__normal_iterator
    __gnu_cxx::slist
    __gnu_debug::_Safe_iterator
    std::_Deque_const_iterator
    std::_Deque_iterator
    std::_List_const_iterator
    std::_List_iterator
    std::_Rb_tree_const_iterator
    std::_Rb_tree_iterator
    std::__7::_Deque_const_iterator
    std::__7::_Deque_iterator
    std::__7::_List_const_iterator
    std::__7::_List_iterator
    std::__7::_Rb_tree_const_iterator
---Type <return> to continue, or q <return> to quit---
    std::__7::_Rb_tree_iterator
    std::__7::__cxx11::basic_string
    std::__7::basic_string
    std::__7::bitset
    std::__7::deque
    std::__7::forward_list
    std::__7::list
    std::__7::map
    std::__7::multimap
    std::__7::multiset
    std::__7::priority_queue
    std::__7::queue
    std::__7::set
    std::__7::shared_ptr
    std::__7::stack
    std::__7::tuple
    std::__7::unique_ptr
    std::__7::unordered_map
    std::__7::unordered_multimap
    std::__7::unordered_multiset
    std::__7::unordered_set
    std::__7::vector
    [... many more lines of output omitted]

For good measure, I added the following lines to .gdbinit:

set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off

So I opened Eclipse and started debugging my application, only to find that the ugly print was still in effect:

Debugger still using ugly print

How can I fix this to use GDB's pretty print; is pretty print even installed?

EMBLEM
  • 2,207
  • 4
  • 24
  • 32
  • Note: `gdb -nx` and `info pretty-print` does not produce any output. – EMBLEM Apr 20 '15 at 04:46
  • Normally your distro should install everything properly. That's what that error message really means -- your install was redundant. `gdb -nx` isn't a sufficient test, because the pretty-printers are loaded on demand; in this case, when libstdc++.so is loaded by the inferior. So you have to try that with `-nx`. I don't know why Eclipse isn't working, it probably should. – Tom Tromey Apr 20 '15 at 21:25
  • @TomTromey I should note that I installed Eclipse as a download from the Eclipse Foundation website and not with my system's package manager. And could you explain a little more what I should do? Load the libstdc++.so file with gdb? – EMBLEM Apr 20 '15 at 21:27
  • Eclipse shouldn't matter. What matters is gdb. One thing to try is to debug an ordinary C++ program with gdb -nx. If pretty-printing works there, then the bug is somehow with Eclipse. Otherwise it is with your gdb (or libstdc++ or ...) setup. – Tom Tromey Apr 21 '15 at 01:44
  • @TomTromey I've never used GDB before, but I got a rudimentary session running and was able to print the contents of an `std::vector` after it was initialized: `$3 = { >> = {> = {}, __begin_ = 0x606010, __end_ = 0x6060e0, __end_cap_ = {, 2>> = {> = {}, __first_ = 0x6060e0}, }}, }` So it would appear that the problem is with GDB. – EMBLEM Apr 21 '15 at 02:00
  • @TomTromey After some more tinkering today I got gdb to work using the extra line in [this answer](http://stackoverflow.com/questions/26205564/gdb-pretty-printing-importerror-no-module-named-printers), and I had to install `libstdc++6-4.6-dbg`. However, it still produces the same results in Eclipse, and trying to print an `std::unordered_map` gave me this error message: `Python Exception cannot resolve overloaded method `_M_before_begin': no arguments supplied: $1 = std::unordered_map with 2 elements`. – EMBLEM May 01 '15 at 01:06
  • http://stackoverflow.com/questions/4985414/enable-pretty-printing-for-gdb-in-eclipse-cdt – Ciro Santilli OurBigBook.com Apr 12 '17 at 08:02

1 Answers1

4

The values in your screenshot look like memory addresses, which leads me to believe you're trying to inspect a pointer to an instance of an STL type. AFAIK, the pretty printers won't automatically dereference pointers, but will pretty-print if you add a watch expression that deferences the pointer.

The additional .gdbinit lines you're using shouldn't be necessary; the following steps are all that should be required (for Eclipse Mars.1):

  1. Download the pretty printers:

    svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
    
  2. Create a /home/<user>/.gdbinit file with the following contents:

    python
    import sys
    sys.path.insert(0, '/path/to/prettyprint/python')
    from libstdcxx.v6.printers import register_libstdcxx_printers
    end
    
  3. In the GDB settings for Eclipse (Window->Preferences->C/C++->Debug->GDB), set the GDB command file path to the .gdbinit file you just created.

  4. For any existing debug configurations (Run->Debug Configurations) which require pretty-printing, select the debug configuration, then set the GDB command file path on the Debugger tab.

You can verify that GDB is pretty-printing as follows (once the first two steps of the previous procedure have been completed):

  1. Save the following code in a file named test.cpp:

    #include <map>
    int main() {
        std::map<char, int> first;
        first['a'] = 10;
        first['b'] = 20;
    }
    
  2. Compile with g++:

    $ g++ -g -o test test.cpp
    
  3. Run gdb:

    $ gdb test
    
  4. Set a break point:

    (gdb) b test.cpp:5
    Breakpoint 1 at 0x40093f: file src/test.cpp, line 5.
    
  5. Run the program:

    (gdb) run
    Starting program: /path/to/test
    
  6. When the break point is hit, the gdb prompt will be displayed. Print the map with the following command:

    (gdb) p first
    

    If all is well, you should see the following output:

    $1 = std::map with 1 elements = {[97 'a'] = 10}
    
cqcallaw
  • 1,463
  • 3
  • 18
  • 29
  • your forgetting the line `register_libstdcxx_printers (None)` which comes before `end` in the `.gdbinit` file. – skyfire May 29 '21 at 20:42