漂亮的地图打印机会抛出类型错误

发布于 2025-01-01 03:17:48 字数 1166 浏览 0 评论 0 原文

我已经使用 http://wiki.eclipse.org/CDT/ 配置了漂亮的打印机用户/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F。它成功地适用于矢量和其他容器。但是我无法检查地图,如下例所示:

#include <map>
#include <iostream>

using namespace std;

int main ()
{
map <int, string> mapIntToString;
map <int, int> mapInt2;
 mapIntToString.insert (map <int, string>::value_type (3, "Three"));
 mapInt2.insert (map <int, int>::value_type (3, 4));
 return 0;
}

使用 gdb 打印时出现以下错误:

(gdb) p mapInt2
$1 = std::map with 1 elementsTraceback (most recent call last):
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 422, in    children
rep_type = find_type(self.val.type, '_Rep_type')
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 45, in    find_type
 raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
ValueError: Cannot find type std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::_Rep_type

I've configured pretty printers using http://wiki.eclipse.org/CDT/User/FAQ#How_can_I_inspect_the_contents_of_STL_containers.3F. It successfully works for vector and other containers. However I can't get to inspect maps as in the example below:

#include <map>
#include <iostream>

using namespace std;

int main ()
{
map <int, string> mapIntToString;
map <int, int> mapInt2;
 mapIntToString.insert (map <int, string>::value_type (3, "Three"));
 mapInt2.insert (map <int, int>::value_type (3, 4));
 return 0;
}

I get the following error when printing using gdb:

(gdb) p mapInt2
$1 = std::map with 1 elementsTraceback (most recent call last):
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 422, in    children
rep_type = find_type(self.val.type, '_Rep_type')
File "/home/myuser/opt/gdb_printers/python/libstdcxx/v6/printers.py", line 45, in    find_type
 raise ValueError, "Cannot find type %s::%s" % (str(orig), name)
ValueError: Cannot find type std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::_Rep_type

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

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

发布评论

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

评论(2

放飞的风筝 2025-01-08 03:17:48

您使用什么编译器(以及哪个版本)来构建测试源?

我猜这不是 g++ 的最新版本。这是我使用 g++ 4.4.3-4ubuntu5 得到的结果:

$ gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) b 12   
Breakpoint 1 at 0x400de3: file t.cc, line 12.
(gdb) r

Breakpoint 1, main () at t.cc:12
12   return 0;
(gdb) p mapInt2
$1 = std::map with 1 elements = {[3] = 4}

更新:

这是我得到的版本:g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

我看到了问题。您引用的说明不正确

特别是,指令建议:svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python,但问题是Python代码到达libstdc++ 内部结构,因此必须匹配这些内部结构(这就是漂亮打印机是 GCC 的一部分而不是 GDB 的一部分的原因,这是事实bruce.banner 抱怨)。

当您执行新的 svn co ... 时,您获取了一份 Python 代码副本,该代码不再与您的 libstdc++ 内部匹配,这就是是什么给你带来了问题。

特别是,svn log 显示此处添加了 find_type

r183732 | tromey | 2012-01-30 08:25:11 -0800 (Mon, 30 Jan 2012) | 27 lines

这比 gcc-4.4.3很多。然后,您想要做的是获得与您的 libstdc++ 版本相匹配的漂亮打印机,如下所示:

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

除了上述命令将不起作用,因为 gcc 4.4.3 早于漂亮的打印机。

不管怎样,std::map 的实现(以及 STL 内部的大部分其他部分)在 4.4.3 和 4.6 之间没有改变,并且这个命令确实有效:

 svn co svn://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch/libstdc++-v3/python

What compiler (and which version) did you use to build your test source?

I am guessing it wasn't a recent version of g++. Here is what I get with g++ 4.4.3-4ubuntu5:

$ gdb -q ./a.out
Reading symbols from /tmp/a.out...done.
(gdb) b 12   
Breakpoint 1 at 0x400de3: file t.cc, line 12.
(gdb) r

Breakpoint 1, main () at t.cc:12
12   return 0;
(gdb) p mapInt2
$1 = std::map with 1 elements = {[3] = 4}

Update:

This is what I get for the version: g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3

I see the problem. The instructions you've referenced are incorrect.

In particular, the instructions suggest: svn co svn://gcc.gnu.org/svn/gcc/trunk/libstdc++-v3/python, but the problem is that the python code reaches into libstdc++ internals, and therefore must match these internals (this is the reason the pretty printers are part of GCC and not part of GDB, a fact bruce.banner complained about).

When you did a fresh svn co ..., you picked up a copy of python code that no longer matches your libstdc++ internals, and that's what is causing you problems.

In particular, svn log shows that find_type was added here:

r183732 | tromey | 2012-01-30 08:25:11 -0800 (Mon, 30 Jan 2012) | 27 lines

That's much later than gcc-4.4.3. What you want to do then, is to get pretty printers that match your version of libstdc++, like so:

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

Except above command will not work, because gcc 4.4.3 predates the pretty printers.

No matter, the implementation of std::map (and much of the rest of STL internals) has not changed between 4.4.3 and 4.6, and this command does work:

 svn co svn://gcc.gnu.org/svn/gcc/branches/gcc-4_6-branch/libstdc++-v3/python
只是偏爱你 2025-01-08 03:17:48

在我的系统上,类型 _Rep_type 不是 std::map 的公共类型(它是私有 typedef),因此脚本试图找出一个._M_t 变量的类型,其类型为 _Rep_type ...

我尝试过:

typedef std::map<int,int> map_t;
map_t m;
m.insert(map_t::value_type(3,4));

然后在 gdb 中我可以打印密钥3 像这样(遵循下面链接的脚本中的打印功能):

p *(int*)(void*)(m._M_t._M_impl._M_header._M_left+1)

其中 std::map 中的 _M_t _Rb_tree 类型,但该类型在地图中不是公开的(您可以在 map 标头中看到这一点,特别是 /

不确定这是否有一点帮助,基本上您正在加载的漂亮打印 python 函数似乎存在问题。

我刚刚尝试添加到 .gdbinit< code> 来自 来自 yolinux.com 的 GNU GDB 调试器命令备忘单 的内容(我用 google 搜索)为了gdb Pretty print),这样我就得到了合理的输出:

(gdb) pmap m int int
elem[0].left: $3 = 3
elem[0].right: $4 = 4

On my system the type _Rep_type is not public type of std::map (it's private typedef) so the script is trying to figure out a type of <yourmap>._M_t variable which is of type _Rep_type ...

I tried:

typedef std::map<int,int> map_t;
map_t m;
m.insert(map_t::value_type(3,4));

then in gdb I can print the key 3 like this (following the print function from the script I linked below):

p *(int*)(void*)(m._M_t._M_impl._M_header._M_left+1)

Where the _M_t in std::map is of _Rb_tree type but the type is not public in map (you can see that in your map header, specifically <path/to/std-headers/dir/bits/stl_map.h header file.

Not sure if that helps one bit, basically there seems to be an issue with the pretty print python function that you are loading.

I've just tried adding to .gdbinit stuff from GNU GDB Debugger Command Cheat Sheet from yolinux.com (I googled for gdb pretty print) and with that I get reasonable output:

(gdb) pmap m int int
elem[0].left: $3 = 3
elem[0].right: $4 = 4
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文