2012-07-23 112 views
38

我按照说明on the GDB wiki安装了用于查看STL容器的python漂亮打印机。我~/.gdbinit现在看起来是这样的:如何在GDB中漂亮地打印STL容器?

python 
import sys 
sys.path.insert(0, '/opt/gdb_prettyprint/python') 
from libstdcxx.v6.printers import register_libstdcxx_printers 
register_libstdcxx_printers (None) 
end 

然而,当我运行GDB并尝试打印STL类型,我得到如下:

print myString 
Python Exception <class 'gdb.error'> No type named std::basic_string<char>::_Rep.: 
$3 = 

任何人都可以阐明这一些轻?我运行的是Ubuntu 12.04,它带有GDB 7.4。

+5

它可能只是因为C++库已经改变了它的内部类型和成员变量,而且Python模块还没有跟上。 – 2012-07-23 07:21:32

+0

你能否粘贴更多的信息,比如C++源代码,编译选项等?我只是在Ubuntu 12.04上测试了它,它适用于我。 – user1202136 2012-08-14 15:53:55

+0

Fedora 17适合我。 – Omnifarious 2012-10-17 18:22:31

回答

1

我认为你使用的是非GNU STL库,或者可能是一个非常古老的GCC libstdc++。我的编译器上的正常STL字符串的类型是:std::basic_string<char, std::char_traits<char>, std::allocator<char> >。请注意,这不是std::basic_string<char>

的Python代码有这个在它:

reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer() 

此查找嵌套类型::Rep什么的基本字符串类型实际上是。该错误消息表明,您正在使用的任何奇怪库的字符串类实际上并没有::Rep嵌套类型。

7

你可以用下面GDB宏(其附加到你的〜/ .gdbinit文件)打印STL containter类型的数据,甚至它们的数据成员尝试:https://gist.github.com/3978082

2

如果Python的后键入info type _Rep例外情况,gdb会通知你加载的匹配_Rep的类。该列表可以帮助您找到为什么python找不到您的std::string class

我刚刚面临你的问题,在我的情况是英特尔C编译器,ICC,谁打破漂亮的打印。特别是,对于std::string结果不合格的ICC名称:

std::basic_string<char, std::char_traits<char>, std::allocator<char> >::std::basic_string<char, std::char_traits<char>, std::allocator<char> >::_Rep; 

,但相当打印机一直在寻找不合格GCC名称:

std::basic_string<char, std::char_traits<char>, std::allocator<char>::_Rep; 

我做了什么,以解决我的问题是在printers.py修改StdStringPrinter类,将字符串的非限定名称添加到类型名以查找gdb。从info type

reptype = gdb.lookup_type (str (realtype) + '::' + str (realtype) + '::_Rep').pointer() 

使用所得到的列表中,您可以治好你的漂亮的打印机,以使他们的工作:更换行:

reptype = gdb.lookup_type (str (realtype) + '::_Rep').pointer() 

与此有关。

0

我遇到了这个问题,并试图弄清楚这个问题。我最终修复了它,我认为这是值得分享我的经验。

我使用的是gcc-5.2,所以我从svn repo下载了美图打印机的gcc-5分支版本。但是,我不得不做这两个器官功能障碍综合征:

1)编辑.gitinit文件时,建议除了是

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

但是,我不得不作出评论线register_libstdcxx_printers (None),因为我一直得到一个错误告诉我libstdcxx_printers已经注册。显然他们在导入阶段注册。

2)我必须编辑std::setstd::map的printers.py文件。由于_Rep_type这两种类型都是私有的。具体来说,我用svn repo上gcc-4_6分支版本的漂亮打印机版本中的相应版本替换std::mapstd::set中的例程children。从此以后没有错误,现在打印出来的东西很好。

希望这会有所帮助。

0

这只是工作在Ubuntu 17.04

Debian的似乎终于集成事情妥善现在:

#include <map> 
#include <utility> 
#include <vector> 

int main() { 
    std::vector<int> v; 
    v.push_back(0); 
    v.push_back(1); 
    v.push_back(2); 
    std::map<int,int> m; 
    m.insert(std::make_pair(0, 0)); 
    m.insert(std::make_pair(1, -1)); 
    m.insert(std::make_pair(2, -2)); 
} 

编译:

g++ -O0 -ggdb3 -o container.out -std=c++98 container.cpp 

结果:

(gdb) p v 
$1 = std::vector of length 3, capacity 4 = {0, 1, 2} 
(gdb) p m 
$2 = std::map with 3 elements = {[0] = 0, [1] = -1, [2] = -2} 

我们可以看到漂亮的打印机安装有:

info pretty-printer 

其中包含了行:

global pretty-printers: 
    objfile /usr/lib/x86_64-linux-gnu/libstdc++.so.6 pretty-printers: 
    libstdc++-v6 
    std::map 
    std::vector 

打印机按照文件provieded:

附带
/usr/share/gcc-7/python/libstdcxx/v6/printers.py 

主C++库包libstdc++6,位于GCC源代码中的libstdc++-v3/python/libstdcxx下:https://github.com/gcc-mirror/gcc/blob/gcc-6_3_0-release/libstdc%2B%2B-v3/python/libstdcxx/v6/printers.py#L244

TODO:GDB如何发现该文件是最终的神秘莫测,它不在我的Python路径中:python -c "import sys; print('\n'.join(sys.path))"因此它必须在某处硬编码?

0

上面发布的错误通常在程序是LLVM-build(由clang编译)时出现,并且您尝试通过gdb(它应该用于GCC-build程序)进行调试。 理论上,LLVM-build程序可能会被调试gdb,反之亦然。但是 为了避免上面发布的问题,如果您使用clang,则应该使用lldb,如果使用g++,则应该使用gdb