How to pretty-print STL containers in GDB?

前端 未结 9 556
一整个雨季
一整个雨季 2020-11-30 20:45

I\'ve followed the instructions on the GDB wiki to install the python pretty-printers for viewing STL containers. My ~/.gdbinit now looks like this:

<         


        
相关标签:
9条回答
  • 2020-11-30 21:09

    Errors like you post above usually appears when program is LLVM-build (compiled by clang), and you try to debug it by gdb (which should be used for GCC-build programs). In theory, LLVM-build program may be debugged by gdb, and vice versa. But to avoid problems like posted above, you should use lldb if you use clang, and should use gdb if you use g++.

    0 讨论(0)
  • 2020-11-30 21:11

    You can try with below GDB macro (append it to your ~/.gdbinit file) to print STL containter types data and even their data members: https://gist.github.com/3978082

    0 讨论(0)
  • 2020-11-30 21:14

    Similar to enter link description here Worked for me in ~/.gdbinit:

    python
    import sys
    sys.path.insert(0, '/usr/share/gcc-8/python')
    from libstdcxx.v6.printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)
    end
    
    0 讨论(0)
  • 2020-11-30 21:19

    Check your gcc version. If it is less than 4.7, you need use another printer.py file. Get the file from http://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch/libstdc++-v3/python/.

    0 讨论(0)
  • 2020-11-30 21:20

    Instead of methods listed in the link you mentioned, you can try the script here,

    Do as follows:

    1) Download the script to /your/path. Name it to some name e.g. your_name.conf.

    2) Add a ~/.gdbinit file to home directory if you don't have one.

    3) Add a line source /your/path/your_name.conf to your ~/.gdbinit.

    4) Restart gdb. Try pvector

    You can find help information with commands like help pvector.

    e.g.

    pvector vec 5      # Prints element[5] in vec
    pvector vec 5 10   # Prints elements in range [5, 10] in vec. (5, 6, 7, 8, 9, 10)
    

    FYI, the script adds several commands (pvector, plist, pmap etc.) to gdb whose function is to print the elements of STL. It also adds print pretty, yielding nice format like this:

    Also, if you wanna know how exactly the elements of STL are accessed in gdb, just read the code of the commands. There's no secret in the code. ^_^

    e.g. vectors are accessed by ._M_impl._M_start

    p vec._M_impl._M_start + 4 # prints vec[4]

    0 讨论(0)
  • 2020-11-30 21:23

    I think you are using a non-GNU STL library, or possible a very old GCC libstdc++. The type of a normal STL string on my compiler is: std::basic_string<char, std::char_traits<char>, std::allocator<char> >. Note that this is not std::basic_string<char>.

    The Python code has this in it:

    reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer ()
    

    This look up a nested type ::Rep of whatever the base string type actually is. The error message inidicates that the string class of whatever strange library you're using doesn't actually have a ::Rep nested type.

    0 讨论(0)
提交回复
热议问题