简体   繁体   English

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

[英]Better variable exploring when debugging C++ code with Eclipse/CDT

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 (eg shared_ptr).使用 Eclipse 和 CDT 来调试 C++ 代码,变量 windows 很麻烦,而且对于标准模板库或 boost 中定义的类型(例如 shared_ptr)来说信息量不大。

Just an example how this may look like for an std::vector:只是一个例子,这对于 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  

Even if this information about the internals of those types may be useful, in almost any cases I would expect a clearer presentation here, ie a list of values for the std::vector.即使有关这些类型内部的信息可能有用,在几乎任何情况下,我都希望这里有更清晰的介绍,即 std::vector 的值列表。 Are there any tools, plugins or other modifications around which can do this?是否有任何工具、插件或其他修改可以做到这一点?

EDIT编辑

The following solutions does not work for linux.以下解决方案不适用于 linux。 I am using ubuntu 14.04, eclipse, g++, gdb.我正在使用 ubuntu 14.04、eclipse、g++、gdb。

I cant find a package gdb-python and linux does not use mingw我找不到包 gdb-python 和 linux 不使用 mingw

You need a version of GDB capable of using python to pretty print structures.您需要一个能够使用 python 来漂亮打印结构的 GDB 版本。 I know at least on windows using mingw that this is not provided in the default install.我至少在使用 mingw 的 Windows 上知道默认安装中没有提供。

Pretty Printers are python modules which tell gdb how to display a given structure. Pretty Printers 是 Python 模块,它告诉 gdb 如何显示给定的结构。 You can write your own, but there are already printers for STL available for download.您可以自己编写,但已经有 STL 打印机可供下载。

To Get Pretty Printers working on Windows (instructions should be similiar for other OS's):让漂亮的打印机在 Windows 上工作(其他操作系统的说明应该类似):

Prerequisites先决条件

Installation:安装:

  • Open a command Shell and type:打开命令 Shell 并键入:

     mingw-get install gdb-python
  • When its finished cd to a local directory and install the printers by typing:当它完成 cd 到本地目录并通过键入以下内容安装打印机:

     svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
  • 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.打开 .gdbinit(如果需要,请在文本编辑器中创建它)并键入以下内容,将“C:/directory”替换为您将打印机签入的文件夹。

    Python蟒蛇
    import sys导入系统
    sys.path.insert(0, 'C:/directory') sys.path.insert(0, 'C:/目录')
    from libstdcxx.v6.printers import register_libstdcxx_printers从 libstdcxx.v6.printers 导入 register_libstdcxx_printers
    register_libstdcxx_printers (None) register_libstdcxx_printers(无)
    end结束

Eclipse Setup日食设置

  • Go To Windows > Preferences > C/C++ > Debug > GDB转到 Windows > 首选项 > C/C++ > 调试 > GDB
  • Where it Says GDB Debugger put the path to the python enabled GDB it will most likely be in the mingw /bin folder with a name like gdb-python27.exe它说 GDB 调试器将路径放在启用了 python 的 GDB 中,它很可能位于 mingw /bin 文件夹中,名称类似于 gdb-python27.exe
  • Where it says GDB Command File put the path to the .gdb init file you made earlier.它说 GDB 命令文件将路径放在您之前创建的 .gdb init 文件中。

That's it, debug like normal, the stl structures should be much easier to read.就是这样,像往常一样调试,stl 结构应该更容易阅读。

Well, gdb don't natively support STL containers.好吧,gdb 本身并不支持 STL 容器。 You can't say this is incorrect, since it will expose the inner workings of the STL objects, but most of the time it is not what we want, right?你不能说这是不正确的,因为它会暴露 STL 对象的内部工作原理,但大多数时候它不是我们想要的,对吧?

If you're using gdb 7.0 you can take advantage of the pretty printers.如果您使用的是 gdb 7.0,您可以利用漂亮的打印机。 This website http://sourceware.org/gdb/wiki/STLSupport has a pretty simple tutorial on how to set them.这个网站http://sourceware.org/gdb/wiki/STLSupport有一个关于如何设置它们的非常简单的教程。 I copied below the part that interests you:我在下面复制了您感兴趣的部分:

  1. Check-out the latest Python libstdc++ printers to a place on your machine.将最新的 Python libstdc++ 打印机签出到您机器上的某个位置。 In a local directory, do:在本地目录中,执行:

     svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
  2. Add the following to your ~/.gdbinit.将以下内容添加到您的 ~/.gdbinit。 The path needs to match where the python module above was checked-out.路径需要与上面的 python 模块检出的位置相匹配。 So if checked out to: /home/maude/gdb_printers/, the path would be as written in the example:因此,如果签出到:/home/maude/gdb_printers/,路径将如示例中所写:

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

The path should be the only element that needs to be adjusted in the example above.路径应该是上例中唯一需要调整的元素。 Once loaded, STL classes that the printers support should printed in a more human-readable format.加载后,打印机支持的 ​​STL 类应该以更易读的格式打印。 To print the classes in the old style, use the /r (raw) switch in the print command (ie, print /r foo).要以旧样式打印类,请在打印命令中使用 /r(原始)开关(即,print /r foo)。 This will print the classes as if the Python pretty-printers were not loaded.这将打印类,就像未加载 Python 漂亮打印机一样。

Since you're using eclipse cdt, don't forget to point your debug configuration to your .gdbinit file.由于您使用的是 eclipse cdt,请不要忘记将您的调试配置指向您的 .gdbinit 文件。 When creating a new Debug Configuration, go to the Debugger tab and put the path to the .gdbinit file in the "GDB command file" field.创建新的调试配置时,转到调试器选项卡并将 .gdbinit 文件的路径放在“GDB 命令文件”字段中。

I hope that helps!我希望这有帮助!

In debug view in variables list expand vector:在变量列表中的调试视图中展开向量:

"vector_name" -> std::_Vector_base<"datatype"> -> _M_impl "vector_name" -> std::_Vector_base<"datatype"> -> _M_impl

then right click on _M_start and select "Display as array...", type its length and then click OK.然后右键单击_M_start并选择“显示为数组...”,键入其长度,然后单击“确定”。 Now you can expand each item of your vector.现在您可以展开矢量的每个项目。

If you have gdb support for CDT (see, for example, GDB in Eclipse ), you could try this: De-referencing STL containers如果您有CDT gdb支持(例如,参见Eclipse 中的 GDB ),您可以试试这个: 取消引用 STL 容器

Long ago I also stumbled upon your same problem.很久以前我也偶然发现了你同样的问题。 It was a pain to check the STL containers.检查 STL 容器很痛苦。 Then I found that link and added to my .gdbinit file some of those definitions.然后我找到了该链接并将其中一些定义添加到我的.gdbinit文件中。 Life was easier after that.之后的生活就轻松多了。

NOTE: My gdb version is 7.1 and adding those definitions work fine.注意:我的gdb版本是 7.1,添加这些定义工作正常。 I don't know if in newer versions of gdb they are already included.我不知道在较新版本的gdb是否已经包含它们。

I would like to expand on the Windows 7 response because some key steps are left out:我想扩展 Windows 7 的响应,因为省略了一些关键步骤:

This is for MinGW users with Eclipse CDT这适用于使用 Eclipse CDT 的 MinGW 用户

0) If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install' Python-enabled GDB eg 0)如果你没有 python GDB,打开一个 shell/命令并使用 MinGW-get.exe 来“安装”启用 Python 的 GDB,例如

   MinGw-get.exe install gdb-python

1a) Get Python 2.7.x from http://python.org/download/ and install 1a) 从http://python.org/download/获取 Python 2.7.x 并安装

1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment: 1b) 确保在您的环境中设置了 PYTHONPATH 和 PYTHONHOME:

 PYTHONPATH should be C:\Python27\Lib   (or similar)
 PYTHONHOME should be C:\Python27

1c) Add PYTHONHOME to your PATH 1c) 将 PYTHONHOME 添加到您的 PATH

 %PYTHONHOME%;...

2a) Open a text enter, enter the following statements. 2a) 打开一个文本回车,输入以下语句。 Notice the 3rd line is pointing to where the python scripts are located.请注意,第三行指向 python 脚本所在的位置。 See notes below about this!请参阅下面的注释!

python
import sys
sys.path.insert(0, 'C:/MinGW/share/gcc-4.6.1/python')         
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end

2b) Save as '.gdbinit' NOTE: Windows explorer will not let you name a file that starts with with a period from explorer. 2b) 另存为“.gdbinit” 注意:Windows 资源管理器不允许您命名以资源管理器中的句点开头的文件。 Most text edits (including Notepad) will let you.大多数文本编辑(包括记事本)都可以。 GDB init files are like 'scripts' of GDB commands that GBD will execute upon loading. GDB 初始化文件就像 GBD 在加载时将执行的 GDB 命令的“脚本”。

2c) The '.gdbinit' file needs to be in the working directory of GDB (most likely this is your projects root directory but your IDE can tell you. 2c) '.gdbinit' 文件需要在 GDB 的工作目录中(很可能这是你的项目根目录,但你的 IDE 可以告诉你。

3) Open your Eclipse (or other IDE) Preferences dialog. 3) 打开 Eclipse(或其他 IDE)首选项对话框。 Go to the C++ Debugger sub-menu.转到 C++ 调试器子菜单。

4) Configure Eclipse to use C:\\MinGW\\bin\\gdb-python27.exe as the debugger and your .gdbinit as the config file. 4) 将 Eclipse 配置为使用C:\\MinGW\\bin\\gdb-python27.exe .gdbinit作为调试器,使用.gdbinit作为配置文件。

5a) Re-create all your debug launch configurations (delete the old one and create a new one from scratch). 5a) 重新创建所有调试启动配置(删除旧配置并从头开始创建新配置)。

--OR--

5b) Edit each debug configuration and point it to the new gdb-python.exe AND point it to the. 5b) 编辑每个调试配置并将其指向新的 gdb-python.exe 并将其指向 .

If you run into issues:如果遇到问题:

--Don't forget to change the location to the python directory in the above python code! --别忘了把上面python代码中的位置改成python目录! This directory is created by MinGW, so don't go looking to download the pretty printers, MinGW did it for you in step zero.这个目录是由 MinGW 创建的,所以不要去寻找下载漂亮的打印机,MinGW 在第 0 步为你做了。 Just goto your MinGW install director, the share folder, the GCC folder (has version number) and you will find python folder.只需转到您的 MinGW 安装目录、共享文件夹、GCC 文件夹(有版本号),您就会找到 python 文件夹。 This location is what should be in python script loaded by GDB.这个位置应该在 GDB 加载的 python 脚本中。

--Also, the .gdbinit is a PITA, make sure its named correctly and in the working folder of GDB which isn't necessarily where gdb-python.exe is located! --此外,.gdbinit 是 PITA,请确保其命名正确且位于 GDB 的工作文件夹中,该文件夹不一定位于 gdb-python.exe 所在的位置! Look at your GDB output when loading GDB to see if a) 'python-enabled' appears during load and that the statements in the .gdbinit are appearing.在加载 GDB 时查看您的 GDB 输出,看看是否 a) 'python-enabled' 在加载期间出现,以及 .gdbinit 中的语句是否出现。

--Finally, I had alot of issues with the system variables. --最后,我对系统变量有很多问题。 If python gives you 'ImportError' then most likely you have not set PYTHONPATH or PYTHONHOME.如果 python 给你 'ImportError' 那么你很可能没有设置 PYTHONPATH 或 PYTHONHOME。

--The directory with 'gdb-python27' (eg C:\\MinGW\\bin') should also be on your path and if it is, it makes setting up eclipse a bit nicer because you don't need to put in absolute paths. --'gdb-python27' 的目录(例如 C:\\MinGW\\bin')也应该在你的路径上,如果是,它会让 eclipse 的设置更好一些,因为你不需要放入绝对路径. But still, sometimes the .gbdinit needs an absoulte path.但是,有时 .gbdinit 需要一个绝对路径。 if it works you'll see output from gbd (console->gdb traces) like this on startup of debugger:如果它有效,你会在调试器启动时看到这样的 gbd (console->gdb traces) 输出:

835,059 4^done
835,059 (gdb) 
835,059 5-enable-pretty-printing
835,069 5^done
....
835,129 12^done
835,129 (gdb) 
835,129 13source C:\MinGW\bin\.gdbinit
835,139 &"source C:\\MinGW\\bin\\.gdbinit\n"
835,142 13^done
835,142 (gdb) 

I know that JDT (Java environment in eclipse) provides custom "formatters" to be applied when displaying variable values in debug views.我知道 JDT(Eclipse 中的 Java 环境)提供了在调试视图中显示变量值时应用的自定义“格式化程序”。 A quick look at google for the same in CDT brings this page:在 CDT 中快速浏览 google 会带来此页面:

http://wiki.eclipse.org/CDT/Better_Debugging_%28GSoC_project%29 http://wiki.eclipse.org/CDT/Better_Debugging_%28GSoC_project%29

I don't know if this has been yet integrated in the main CDT line, may be you can try to right click on a variable while debugging (in the last CDT) and see if there is a custom formater entry.我不知道这是否已经集成到主 CDT 行中,也许您可​​以尝试在调试时右键单击一个变量(在最后一个 CDT 中),看看是否有自定义格式化程序条目。 If not available I recomend you to add a new tracker entry in CDT tracker to ask this enhancement.如果不可用,我建议您在 CDT 跟踪器中添加一个新的跟踪器条目以询问此增强功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM