Python pretty printer for debugging stdlib variables won't work

后端 未结 1 998
说谎
说谎 2020-12-20 04:04

I followed this post for debugging variables in a pretty way The value of strings doesn't appear in eclipse mars CDT

however I ended up having an error message

相关标签:
1条回答
  • 2020-12-20 04:32

    It looks like your .gdbinit does not have the correct contents, or is not being run.

    Ensure the path added is correct for your machine and that the gdbinit file is running.

    Since you are getting that error, it should only be necessary to add the correct path to python.

    Here is a sample trace that first does not work and then does work once the path is corrected:

    $ cat hello.cc 
    #include <string>
    using namespace std;
    
    int main() {
        string mystring = "my string here";
        return 0;
    }
    
    $ g++ hello.cc -g -o hello.elf
    
    $ gdb hello.elf --quiet
    
    Reading symbols from hello.elf...done.
    (gdb) b hello.cc:6
    Breakpoint 1 at 0x400863: file hello.cc, line 6.
    (gdb) r
    Starting program: /tmp/x/hello.elf 
    Traceback (most recent call last):
      File "/usr/share/gdb/auto-load/usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.19-gdb.py", line 63, in <module>
        from libstdcxx.v6.printers import register_libstdcxx_printers
    ImportError: No module named 'libstdcxx'
    
    Breakpoint 1, main () at hello.cc:6
    6       return 0;
    (gdb) p mystring
    $1 = {static npos = <optimised out>, _M_dataplus = {<std::allocator<char>> = {<__gnu_cxx::new_allocator<char>> = {<No data fields>}, <No data fields>}, 
        _M_p = 0x602028 "my string here"}}
    (gdb) python
    >import sys
    >sys.path.insert(0, '/usr/share/gcc-4.8/python/')
    >end
    (gdb) r
    The program being debugged has been started already.
    Start it from the beginning? (y or n) y
    Starting program: /tmp/x/hello.elf 
    
    Breakpoint 1, main () at hello.cc:6
    6       return 0;
    (gdb) p mystring
    $2 = "my string here"
    (gdb) 
    

    Version info for the above example:

    $ g++ --version
    g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
    Copyright (C) 2013 Free Software Foundation, Inc.
    This is free software; see the source for copying conditions.  There is NO
    warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    
    $ gdb --version
    GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
    Copyright (C) 2014 Free Software Foundation, Inc.
    License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
    This is free software: you are free to change and redistribute it.
    There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
    and "show warranty" for details.
    This GDB was configured as "x86_64-linux-gnu".
    Type "show configuration" for configuration details.
    For bug reporting instructions, please see:
    <http://www.gnu.org/software/gdb/bugs/>.
    Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.
    For help, type "help".
    Type "apropos word" to search for commands related to "word".
    

    When using Eclipse CDT

    When using Eclipse CDT you need to specify the gdbinit file manually. Eclipse CDT starts GDB with the --nx flag, which prevents GDB from picking up any .gdbinit files automatically. You instead should specify a CDT appropriate init file in the Launch Configuration:

    Additionally, you can change your default launch gdbinit for new launch configuration in the Preferences as shown in this dialog:

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