gpt4 book ai didi

c++ - Eclipse/CDT pretty-print 错误

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:13 25 4
gpt4 key购买 nike

首先让我说,我已经在这里搜索了其他非常密切相关的问题,但它们并没有帮助解决我的问题。

设置:

  • Win7 笔记本电脑,SSH/X11 到 CentOS 6.4 Final,运行 Eclipse Kepler SR2 w/CDT 8.3
  • GDB 7.2
  • 无法访问 Internet 的托管公司服务器(因此没有 svn co...)

我刚开始使用 Eclipse/CDT,但熟悉 GDB。当我尝试调试使用 STL 的应用程序时,在第一次出现 STL 对象时,我收到以下错误。在有很多很多 STL 对象的程序中,我会遇到很多错误,以至于无法进行单步执行。我这里有一个用于说明目的的小示例程序。

这是我的示例程序:

#include <iostream>
using namespace std;

int main() {
string sComplex;
sComplex = "!!!Hello World!!!";
cout << sComplex << endl; // prints !!!Hello World!!!
//cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
cout << "This is a new string that writes out a numeric..." << endl;
int i = 1000;
cout << "Value for integer 'i' is : '" << i << "'." << endl;
cout << " In HEX: '";
cout << std::hex << std::showbase << i;
cout << "'." <<endl;
return 0;
}

以下是它一到达第一行(STL 字符串实例化)就打印出来的错误:

Traceback (most recent call last): File "/usr/lib64/../share/gdb/python/libstdcxx/v6/printers.py", line 558, in to_string return self.val['_M_dataplus']['_M_p'].lazy_string (length = len) RuntimeError: Cannot access memory at address 0xffffffffffffffe8

Traceback (most recent call last): File "/usr/lib64/../share/gdb/python/libstdcxx/v6/printers.py", line 558, in to_string return self.val['_M_dataplus']['_M_p'].lazy_string (length = len) RuntimeError: Cannot access memory at address 0xffffffffffffffe8

Traceback (most recent call last): File "/usr/lib64/../share/gdb/python/libstdcxx/v6/printers.py", line 558, in to_string return self.val['_M_dataplus']['_M_p'].lazy_string (length = len) RuntimeError: Cannot access memory at address 0xffffffffffffffe8

首先,请注意这个对象有 3 个独立的错误。我已验证是否安装了 python pretty-print 模块,并且我尝试了限制 to_string 中的长度的建议,但无济于事。如果我越过字符串实例化,一切正常,我可以看到变量的简单字符串值。鼠标悬停看起来也不错。当我直接在命令行上使用 gdb 调试同一个应用程序时,我没有看到任何此类错误和变量值打印漂亮

(gdb) p sComplex
$1 = "!!!Hello World!!!"
(gdb) p sComplex.c_str()
$2 = 0x602028 "!!!Hello World!!!"

我已经对我的 .gdbinit 文件和 Eclipse 的 Window->Preferences->C/C++->Debug->GDB 设置尝试了各种建议,甚至禁用 pretty-print ,但它仍然发生。我不知道接下来还能尝试什么。

最佳答案

我最终决定重新审视这个问题,并最终通过类似于 this one 的解决方案解决了它。 ,并通过额外编辑我的自定义 printers.py。希望这对遇到此问题的其他人有所帮助。

  1. 检查 pretty-print (svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python)到$HOME/gdb/gdb_printers/
    • 如果您无法 checkout (如果您的组织像我一样阻止外部 svn),请查看是否可以从系统位置(例如 /usr/share/gdb/)复制它。完成后你应该有一个像这样的目录结构:

gdb printer tree

这个结构必须是精确的,否则它不会使用/v6/中的 printers.py。

  1. 编辑 printers.py,特别是 StdStringPrinter::to_string 如下(添加 try/except/else):

    def to_string(self):
    # Make sure &string works, too.
    type = self.val.type
    if type.code == gdb.TYPE_CODE_REF:
    type = type.target ()

    # Calculate the length of the string so that to_string returns
    # the string according to length, not according to first null
    # encountered.
    # I wrapped this section in a try/except/else block so that uninitialized
    # strings don't cause massive RuntimeError exception reporting during debugging
    # with or without pretty printers enabled. -jkw
    try:
    ptr = self.val ['_M_dataplus']['_M_p']
    realtype = type.unqualified ().strip_typedefs ()
    reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer ()
    header = ptr.cast(reptype) - 1
    len = header.dereference ()['_M_length']
    except RuntimeError:
    #print 'Caught exception'
    return ''
    else:
    return self.val['_M_dataplus']['_M_p'].lazy_string (length = len)
  2. 创建/编辑$HOME/gdb/.gdbinit 文件并将以下内容放入其中。 请注意,该路径必须与上图/ TreeView 中“python”目录的路径相匹配。

     python
    import sys
    sys.path.insert(0, '/home/(user_id)/gdb/gdb_printers/python')
    from libstdcxx.v6.printers import register_libstdcxx_printers
    register_libstdcxx_printers (None)
    end
  3. 在 Eclipse 中,在 Window -> preferences -> C/C++ -> Debug -> GDB 下,将路径设置为 gdb 和你的 .gdbinit 文件。您可能还需要在您想要使用 printers.py 的任何现有调试配置上进行设置。

    GDB debugger: /usr/bin/gdb
    GDB command file: /home/(user_id)/gdb/.gdbinit

从那里开始,调试功能就像您认为的那样。

关于c++ - Eclipse/CDT pretty-print 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23837549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com