使用 Eclipse/CDT 调试 C++ 代码时更好的变量探索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3651862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Better variable exploring when debugging C++ code with Eclipse/CDT
提问by Danvil
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).
使用 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, i.e. a list of values for the std::vector. Are there any tools, plugins or other modifications around which can do this?
即使这些类型的内部信息可能有用,但在几乎任何情况下,我都希望这里有更清晰的介绍,即 std::vector 的值列表。是否有任何工具、插件或其他修改可以做到这一点?
EDIT
编辑
The following solutions does not work for linux. I am using ubuntu 14.04, eclipse, g++, gdb.
以下解决方案不适用于 linux。我正在使用 ubuntu 14.04、eclipse、g++、gdb。
I cant find a package gdb-python and linux does not use mingw
我找不到包 gdb-python 和 linux 不使用 mingw
采纳答案by DanChianucci
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.
您需要一个能够使用 python 来漂亮打印结构的 GDB 版本。我至少在使用 mingw 的 Windows 上知道默认安装中没有提供。
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 是 Python 模块,它告诉 gdb 如何显示给定的结构。您可以自己编写,但已经有 STL 打印机可供下载。
To Get Pretty Printers working on Windows (instructions should be similiar for other OS's):
让漂亮的打印机在 Windows 上工作(其他操作系统的说明应该类似):
Prerequisites
先决条件
Make sure you have you have Python 2.7 installed and in the system path.
Make sure MinGW-get is installed
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/
Make sure you have an SVN client are installed
确保你已经安装了 Python 2.7 并且在系统路径中。
确保安装了 MinGW-get
http://sourceforge.net/projects/mingw/files/Installer/mingw-get-inst/
确保您安装了 SVN 客户端
Installation:
安装:
Open a command Shell and type:
mingw-get install gdb-python
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
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
import sys
sys.path.insert(0, 'C:/directory')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
打开命令 Shell 并键入:
mingw-get install gdb-python
当它完成 cd 到本地目录并通过键入以下内容安装打印机:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
打开 .gdbinit(如果需要,请在文本编辑器中创建它)并键入以下内容,将“C:/directory”替换为您将打印机签入的文件夹。
Python
import sys
sys.path.insert(0, 'C:/directory')
from libstdcxx.v6.printers import register_libstdcxx_printers
register_libstdcxx_printers (None)
end
Eclipse Setup
日食设置
- Go To Windows > Preferences > C/C++ > Debug > 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
- Where it says GDB Command File put the path to the .gdb init file you made earlier.
- 转到 Windows > 首选项 > C/C++ > 调试 > GDB
- 它说 GDB 调试器将路径放在启用了 python 的 GDB 中,它很可能位于 mingw /bin 文件夹中,名称类似于 gdb-python27.exe
- 它说 GDB 命令文件将路径放在您之前创建的 .gdb init 文件中。
That's it, debug like normal, the stl structures should be much easier to read.
就是这样,像往常一样调试,stl 结构应该更容易阅读。
回答by pedromanoel
Well, gdb don't natively support STL containers. 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?
好吧,gdb 本身并不支持 STL 容器。你不能说这是不正确的,因为它会暴露 STL 对象的内部工作原理,但大多数时候它不是我们想要的,对吧?
If you're using gdb 7.0 you can take advantage of the pretty printers. This website http://sourceware.org/gdb/wiki/STLSupporthas a pretty simple tutorial on how to set them. I copied below the part that interests you:
如果您使用的是 gdb 7.0,您可以利用漂亮的打印机。这个网站http://sourceware.org/gdb/wiki/STLSupport有一个关于如何设置它们的非常简单的教程。我在下面复制了您感兴趣的部分:
Check-out the latest Python libstdc++ printers to a place on your machine. In a local directory, do:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
Add the following to your ~/.gdbinit. The path needs to match where the python module above was checked-out. So if checked out to: /home/maude/gdb_printers/, the path would be as written in the example:
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. To print the classes in the old style, use the /r (raw) switch in the print command (i.e., print /r foo). This will print the classes as if the Python pretty-printers were not loaded.
将最新的 Python libstdc++ 打印机签出到您机器上的某个位置。在本地目录中,执行:
svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python
将以下内容添加到您的 ~/.gdbinit。路径需要与上面的 python 模块检出的位置相匹配。因此,如果签出到:/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
路径应该是上例中唯一需要调整的元素。加载后,打印机支持的 STL 类应该以更易读的格式打印。要以旧样式打印类,请在打印命令中使用 /r(原始)开关(即,print /r foo)。这将打印类,就像未加载 Python 漂亮打印机一样。
Since you're using eclipse cdt, don't forget to point your debug configuration to your .gdbinit file. 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.
由于您使用的是 eclipse cdt,请不要忘记将您的调试配置指向您的 .gdbinit 文件。创建新的调试配置时,转到调试器选项卡并将 .gdbinit 文件的路径放在“GDB 命令文件”字段中。
I hope that helps!
我希望这有帮助!
回答by 0x0000eWan
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. Now you can expand each item of your vector.
然后右键单击_M_start
并选择“显示为数组...”,键入其长度,然后单击“确定”。现在您可以展开矢量的每个项目。
回答by Adri C.S.
If you have gdb
support for CDT
(see, for example, GDB in Eclipse), you could try this: De-referencing STL containers
如果您gdb
支持CDT
(例如,参见Eclipse 中的 GDB),您可以尝试以下操作:取消引用 STL 容器
Long ago I also stumbled upon your same problem. It was a pain to check the STL containers. Then I found that link and added to my .gdbinit
file some of those definitions. Life was easier after that.
很久以前我也偶然发现了你同样的问题。检查 STL 容器很痛苦。然后我找到了该链接并将.gdbinit
其中一些定义添加到我的文件中。之后的生活就轻松多了。
NOTE:My gdb
version is 7.1 and adding those definitions work fine. I don't know if in newer versions of gdb
they are already included.
注意:我的gdb
版本是 7.1,添加这些定义工作正常。我不知道gdb
它们是否已经包含在较新版本中。
回答by William Symionow
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 e.g.
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. Notice the 3rd line is pointing to where the python scripts are located. See notes below about this!
2a) 打开一个文本回车,输入以下语句。请注意,第三行指向 python 脚本所在的位置。请参阅下面的注释!
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. Most text edits (including Notepad) will let you. GDB init files are like 'scripts' of GDB commands that GBD will execute upon loading.
2b) 另存为“.gdbinit” 注意:Windows 资源管理器不允许您命名以资源管理器中的句点开头的文件。大多数文本编辑(包括记事本)都可以。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. Go to the C++ Debugger sub-menu.
3) 打开 Eclipse(或其他 IDE)首选项对话框。转到 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
的配置文件用作配置文件。
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! This directory is created by MinGW, so don't go looking to download the pretty printers, MinGW did it for you in step zero. Just goto your MinGW install director, the share folder, the GCC folder (has version number) and you will find python folder. This location is what should be in python script loaded by GDB.
--别忘了把上面python代码中的位置改成python目录!这个目录是由 MinGW 创建的,所以不要去寻找下载漂亮的打印机,MinGW 在第 0 步为你做了。只需转到您的 MinGW 安装目录、共享文件夹、GCC 文件夹(有版本号),您就会找到 python 文件夹。这个位置应该在 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! 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.
--此外,.gdbinit 是一个 PITA,请确保其命名正确且位于 GDB 的工作文件夹中,而 GDB 的工作文件夹不一定是 gdb-python.exe 所在的位置!在加载 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' (e.g. 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. But still, sometimes the .gbdinit needs an absoulte path. if it works you'll see output from gbd (console->gdb traces) like this on startup of debugger:
--'gdb-python27' 的目录(例如 C:\MinGW\bin')也应该在你的路径上,如果是,它会让 eclipse 的设置更好一些,因为你不需要放入绝对路径. 但是,有时 .gbdinit 需要一个绝对路径。如果它有效,您将在调试器启动时看到 gbd(控制台->gdb 跟踪)的输出,如下所示:
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)
回答by Manuel Selva
I know that JDT (Java environment in eclipse) provides custom "formatters" to be applied when displaying variable values in debug views. A quick look at google for the same in CDT brings this page:
我知道 JDT(Eclipse 中的 Java 环境)提供了在调试视图中显示变量值时应用的自定义“格式化程序”。在 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. If not available I recomend you to add a new tracker entry in CDT tracker to ask this enhancement.
我不知道这是否已经集成到主 CDT 行中,也许您可以尝试在调试时右键单击一个变量(在最后一个 CDT 中),看看是否有自定义格式化程序条目。如果不可用,我建议您在 CDT 跟踪器中添加一个新的跟踪器条目以询问此增强功能。