使用 Eclipse/CDT 调试 C++ 代码时更好地探索变量

2022-01-20 00:00:00 gdb c++ eclipse eclipse-cdt

使用 Eclipse 和 CDT 调试 C++ 代码,变量 windows 很麻烦,并且对于标准模板库或 boost 中定义的类型(例如 shared_ptr)来说信息量不是很大.

Using Eclipse and CDT to debug C++ code the variable windows is cumbersome and not very informative for types defined in the standard template library or in boost (e.g. shared_ptr).

只是一个示例,这对于 std::vector 的外观如何:

Just an example how this may look like for an std::vector:

bar {…}
    std::_Vector_base<TSample<MyTraits>, std::allocator<TSample<MyTraits> > >   
        _M_impl {…} 
            std::allocator<TSample<MyTraits> >  {…} 
            _M_start    0x00007ffff7fb5010  
            _M_finish   0x00007ffff7fd4410  
            _M_end_of_storage   0x00007ffff7fd5010  

即使这些类型的内部信息可能有用,但在几乎任何情况下,我都希望这里有更清晰的表示,即 std::vector 的值列表.是否有任何工具、插件或其他修改可以做到这一点?

Even if this information about the internals of those types may be useful, in almost any cases I would expect a clearer presentation here, i.e. a list of values for the std::vector. Are there any tools, plugins or other modifications around which can do this?

编辑

以下解决方案不适用于 linux.我正在使用 ubuntu 14.04、eclipse、g++、gdb.

The following solutions does not work for linux. I am using ubuntu 14.04, eclipse, g++, gdb.

我找不到包 gdb-python 并且 linux 不使用 mingw

I cant find a package gdb-python and linux does not use mingw

推荐答案

您需要一个能够使用 python 来漂亮地打印结构的 GDB 版本.我至少在使用 mingw 的 Windows 上知道默认安装中没有提供此功能.

You need a version of GDB capable of using python to pretty print structures. I know at least on windows using mingw that this is not provided in the default install.

Pretty Printers 是告诉 gdb 如何显示给定结构的 python 模块.您可以自己编写,但已经有 STL 打印机可供下载.

Pretty Printers are python modules which tell gdb how to display a given structure. You can write your own, but there are already printers for STL available for download.

让 Pretty Printers 在 Windows 上工作(说明应该与其他操作系统类似):

To Get Pretty Printers working on Windows (instructions should be similiar for other OS's):

先决条件

  • 确保您已安装 Python 2.7 并位于系统路径中.

  • Make sure you have you have Python 2.7 installed and in the system path.

http://www.python.org/download/

  • 确保安装了 MinGW-get

  • Make sure MinGW-get is installed

    http://sourceforge.net/projects/mingw/files/安装程序/mingw-get-inst/

  • 确保您已安装 SVN 客户端

  • Make sure you have an SVN client are installed

    安装:

    • 打开命令Shell并输入:

    • Open a command Shell and type:

    mingw-get install gdb-python
    

  • 完成后 cd 到本地目录并通过键入以下内容安装打印机:

  • When its finished cd to a local directory and install the printers by typing:

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

  • 打开 .gdbinit(如果需要,在文本编辑器中创建它)并键入以下内容,将C:/directory"替换为您将打印机签入的文件夹.

  • Open the .gdbinit (create it in a text editor if need be) and type the following replaceing "C:/directory" with the folder that you checked the printers into.

    Python
    导入系统
    sys.path.insert(0, 'C:/directory')
    从 libstdcxx.v6.printers 导入 register_libstdcxx_printers
    register_libstdcxx_printers(无)
    结束

    Python
    import sys
    sys.path.insert(0, 'C:/directory')
    from libstdcxx.v6.printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)
    end

  • Eclipse 设置

    • 转到 Windows > 首选项 > C/C++ > 调试 > GDB
    • 显示 GDB 调试器的位置放置了启用 python 的 GDB 的路径,它很可能位于 mingw/bin 文件夹中,名称类似于 gdb-python27.exe
    • 上面写着 GDB 命令文件的地方是你之前创建的 .gdb 初始化文件的路径.

    就是这样,正常调试,stl结构应该更容易阅读.

    That's it, debug like normal, the stl structures should be much easier to read.

    相关文章