如何为 C++ 启用 gdb 漂亮打印Eclipse CDT 中的 STL 对象?

发布于 2024-10-17 03:11:14 字数 1406 浏览 8 评论 0原文

我正在尝试为 eclipse cdt 中的 STL 对象添加漂亮的打印。我尝试按照此处描述的步骤操作:

http://sourceware.org/gdb/wiki/STLSupport

我检查了 python 文件夹,但我似乎无法完成此操作...

我创建了一个 gdbinit 并选择了我的调试配置,但每当我尝试开始调试时,我都会收到以下错误:

Error while executing Python code.
!STACK 0
java.lang.Exception: /home/lizardking/workspace/eu.sofia.kpi.cpp.x86.testapp/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
        at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.processMIOutput(AbstractMIControl.java:824)
        at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.run(AbstractMIControl.java:662)

如果我尝试执行在 python shell 中查看 gdbinit 的内容时,我收到此错误:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import sys
sys.path.insert(0, '/home/Documents/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libstdcxx.v6.printers

看来我没有这样的模块...我对 Python 没有任何了解,所以我什至不知道“模块”是什么Python...

有人可以帮我解决这个问题吗?能够看到真实的调试信息对我来说非常重要,或者说很有用。或者以某种方式我什至可以从控制台进行调试并从 gdb 获得良好的输出,因为如果我打印一个字符串,例如我会得到无用的输出......

问候, 亚历克斯

I'm trying to add pretty printing for STL objects in eclipse cdt. I tried to follow the steps described here:

http://sourceware.org/gdb/wiki/STLSupport

I checked out the python folder, but I can't seem to get this done...

I created a gdbinit and selected for my debug configuration, but whenever I try to start debugging I get the following error:

Error while executing Python code.
!STACK 0
java.lang.Exception: /home/lizardking/workspace/eu.sofia.kpi.cpp.x86.testapp/.gdbinit:6: Error in sourced command file:
Error while executing Python code.
        at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.processMIOutput(AbstractMIControl.java:824)
        at org.eclipse.cdt.dsf.mi.service.command.AbstractMIControl$RxThread.run(AbstractMIControl.java:662)

If I try to execute the contents of gdbinit in a python shell, I get this error:

Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
import sys
sys.path.insert(0, '/home/Documents/python')
from libstdcxx.v6.printers import register_libstdcxx_printers
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named libstdcxx.v6.printers

It seems that I don't have such module...I don't have a clue about Python so I don't even know what a "module" is in Python....

Can somebody help me with this? It is very important for me to be able to see real debug information, or useful to put it that way. Or someway I can debug even from console and get nice output from gdb, cuz if I go print a string for instance I get useless output....

Regards,
Alex

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

孤檠 2024-10-24 03:11:14

这是适合我的解决方案。

下载 ( http://www.gnu.org/software/gdb/download/ )并安装最新的 gdb(即使用 --prefix $HOME)。它支持Python脚本。

通过执行获取 python 漂亮的打印机

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

在您选择的目录中(即 $(HOME)/distribs/gdb_printers)。您将在结帐目录中获得“python”子目录。

将其放入 $(HOME)/.gdbinit 文件中,并提供漂亮打印机的正确路径:

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

这使得可以通过 gdb 的命令行界面使用漂亮打印 (>(gdb) p my_std_string)。

接下来解释在 Eclipse 中调试时的用法。

下载 (http://download.eclipse.org/eclipse/downloads/) 最新的 Stream Stable构建或发布 Eclipse(>=3.7 版本)。

下载 ( http://download.eclipse.org/tools/cdt /builds/8.0.0/index.html 对于 Eclipse Indigo 或 http://www .eclipse.org/cdt/downloads.php for Eclipse Juno) 最新的 Eclipse C/C++ 开发工具 (Eclipse CDT)。

运行 Eclipse 并选择将存储选项的工作区目录(即 $HOME/projects)。单击帮助 -> 安装新软件... 单击添加...-> 存档... 并选择您刚刚下载的 CDT 版本。然后你必须选择要安装的组件:点击CDT主要功能-> C/C++ 开发工具(可能还有您选择的其他组件)。然后继续安装并重新启动 Eclipse。

在 Eclipse 中指定 gdb 和 .gdbinit 的正确位置,并确保启用“漂亮打印”选项:

Window -> preferences -> C/C++ -> Debug -> GDB

现在,在 Eclipse 中调试时,您可以在“变量”视图中看到漂亮打印的 STL 容器。

其他命令可用于使 gdb 输出更加体面:

set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off

更新:关于使其适用于旧项目,请参阅下面 rustyx 答案中的第 4)点。

UPDATE2:ubuntu 12.04 有 libstdc++6-4.6-dbg 为您安装 /usr/share/gcc-4.6/python/libstdcxx/ python 模块

This is the solution that works for me.

Download ( http://www.gnu.org/software/gdb/download/) and install latest gdb (i.e. with --prefix $HOME). It supports python scripting.

Get python pretty printers by executing

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

in a directory of your choice (i.e. $(HOME)/distribs/gdb_printers). You will get 'python' subdirectory in the checkout directory.

Put this in your $(HOME)/.gdbinit file with proper path to pretty printers:

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

This makes pretty printing usable via command-line interface of gdb (>(gdb) p my_std_string).

Next explains usage while debugging in Eclipse.

Download ( http://download.eclipse.org/eclipse/downloads/) latest Stream Stable Build or Release of Eclipse (>=3.7 version).

Download ( http://download.eclipse.org/tools/cdt/builds/8.0.0/index.html for Eclipse Indigo or http://www.eclipse.org/cdt/downloads.php for Eclipse Juno) latest Eclipse C/C++ Development Tooling (Eclipse CDT).

Run Eclipse and chose workspace directory where your options will be stored (i.e. $HOME/projects). Click Help->Install New Software... Click Add...->Archive... and choose the CDT build that you've just downloaded. Then you must choose components to install: click CDT Main Features -> C/C++ Development Tools (and possibly other components of your choice). Then proceed with installation and restart Eclipse.

Specify proper location of gdb and .gdbinit in Eclipse and make sure the Pretty Printing option is enabled:

Window -> preferences -> C/C++ -> Debug -> GDB

Now you can see STL containers pretty-printed in Variables view while debugging in Eclipse.

Other commands can be used to make gdb output more decent:

set print pretty on
set print object on
set print static-members on
set print vtbl on
set print demangle on
set demangle-style gnu-v3
set print sevenbit-strings off

UPDATE: Regarding getting it to work for old projects, see point 4) in rustyx answer below.

UPDATE2: ubuntu 12.04 has libstdc++6-4.6-dbg that installs /usr/share/gcc-4.6/python/libstdcxx/ python module for you

驱逐舰岛风号 2024-10-24 03:11:14

我知道这并没有回答最初的问题,但我认为这对于那些在 Windows / MinGW 上的 Eclipse 中进行调试的人可能很有用。 Windows 的过程类似:

1) 从 http://python.org/download/

2) 在包含以下内容的某个位置创建 .gdbinit

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

3) 配置 Eclipse CDT 以使用 C:\MinGW\bin\gdb-python27.exe 作为调试器和您的.gdbinit 作为配置文件。

4) 重新创建调试启动会话(删除旧会话并从头开始创建一个新会话)。

I know this does not answer the original question, but I thought it might be useful for those who debug in Eclipse on Windows / MinGW. The procedure for Windows is similar:

1) Get Python 2.7.x from http://python.org/download/

2) Create .gdbinit somewhere containing something like 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

3) Configure Eclipse CDT to use C:\MinGW\bin\gdb-python27.exe as the debugger and your .gdbinit as the config file.

4) Re-create your debug launch session (delete the old one and create a new one from scratch).

绝不放开 2024-10-24 03:11:14

我想扩展 Windows 7 响应,因为遗漏了一些关键步骤:

这是针对使用 Eclipse CDT

0 的 MinGW 用户)如果您没有 python GDB,请打开 shell/命令并使用 MinGW-get.exe '安装'
支持 Python 的 GDB 例如

   MinGw-get.exe install gdb-python

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

1b) 确保在您的环境中设置了 PYTHONPATH 和 PYTHONHOME:

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

1c) 将 PYTHONHOME 添加到您的 PATH

 %PYTHONHOME%;...

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) 另存为“.gdbinit”注意:Windows 资源管理器不允许您命名以以下开头的文件
带有来自探险家的句号。大多数文本编辑(包括记事本)都可以。 GDB初始化
文件就像 GDB 命令的“脚本”,GBD 将在加载时执行。

2c) '.gdbinit' 文件需要位于 GDB 的工作目录中(很可能是
你的项目根目录,但你的 IDE 可以告诉你。

3) 打开 Eclipse(或其他 IDE)首选项对话框。转至 C++ 调试器子菜单。

4) 配置 Eclipse 使用 C:\MinGW\bin\gdb-python27.exe 作为调试器,使用 .gdbinit 作为配置文件。

5a) 重新创建所有调试启动配置(删除旧配置并从头开始创建新配置)。

--OR--

5b) 编辑每个调试配置并将其指向新的 gdb-python.exe 并将其指向.

如果遇到问题:

--不要忘记将位置更改为上述 python 代码中的 python 目录!
这个目录是由 MinGW 创建的,所以不要去下载漂亮的打印机,MinGW
在第零步中为您完成了。只需转到您的 MinGW 安装目录、共享文件夹,
GCC 文件夹(有版本号),您将找到 python 文件夹。这个位置是什么
应该在 GDB 加载的 python 脚本中。

--此外,.gdbinit 是一个 PITA,请确保其命名正确并位于 GDB 的工作文件夹中
这不一定是 gdb-python.exe 所在的位置!加载 GDB 时查看 GDB 输出,看看加载期间是否出现 a) 'python-enabled' 以及 .gdbinit 中的语句是否出现。

--最后,我对系统变量有很多问题。如果 python 给你“ImportError”,那么很可能你没有设置 PYTHONPATH 或 PYTHONHOME。

--带有'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) 

I would like to expand on the Windows 7 response because some key steps are left out:

This is for MinGW users with Eclipse CDT

0) If you don't have python GDB, open a shell/command and use MinGW-get.exe to 'install'
Python-enabled GDB e.g.

   MinGw-get.exe install gdb-python

1a) Get Python 2.7.x from http://python.org/download/ and install

1b) Make sure PYTHONPATH and PYTHONHOME are set in your environment:

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

1c) Add PYTHONHOME to your 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!

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.

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.

3) Open your Eclipse (or other IDE) Preferences dialog. Go to the C++ Debugger sub-menu.

4) Configure Eclipse to use C:\MinGW\bin\gdb-python27.exe as the debugger and your .gdbinit as the config file.

5a) Re-create all your debug launch configurations (delete the old one and create a new one from scratch).

--OR--

5b) Edit each debug configuration and point it to the new gdb-python.exe AND point it to the.

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.

--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.

--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.

--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:

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) 
‘画卷フ 2024-10-24 03:11:14

如果您按照接受的答案进行操作,并且 UPDATE2 和 gdb 收到如下错误:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/usr/share/gcc-4.8/python/libstdcxx/v6/printers.py", line 54
    raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
                ^
SyntaxError: invalid syntax
/home/[you]/.gdbinit:6: Error in sourced command file:
Error while executing Python code.

这是因为您的 gdb 版本正在使用 Python 3(确认此答案

解释了此问题的解决方法此处

或者,按照说明从 svn://gcc.gnu.org 获取源代码,与 Python 3 兼容

If you follow the accepted answer and UPDATE2 and gdb receives an error like this:

Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "/usr/share/gcc-4.8/python/libstdcxx/v6/printers.py", line 54
    raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
                ^
SyntaxError: invalid syntax
/home/[you]/.gdbinit:6: Error in sourced command file:
Error while executing Python code.

It is because your version of gdb is using Python 3 (confirm with this answer)

A workaround for this problem is explained here

Alternatively, follow the instructions to get the source from svn://gcc.gnu.org, which is Python 3 compatible

对不⑦ 2024-10-24 03:11:14

要在 Eclipse CDT 中显示 STL 对象,我这样做并且工作正常:

http:// /www.yolinux.com/TUTORIALS/GDB-Commands.html#STLDDEREF

(您发送的链接中的选项号 2)

希望这有帮助

to display STL objects in Eclipse CDT, I do this and it works fine:

http://www.yolinux.com/TUTORIALS/GDB-Commands.html#STLDEREF

(option number 2 in the link that you sent)

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文