在 gdb 中显示解引用的 STL 迭代器
我有一个映射元素的迭代器,我希望 gdb 显示该迭代器的“第一个”和“第二个”元素的值。 例如:
std::map<int,double> aMap;
...fill map...
std::map<int,double>::const_iterator p = aMap.begin();
我可以在代码中使用p.first和p.second,但在gdb中看不到它们。 就其价值而言,在 dbx 中可以执行类似“print p.node.second_”的操作,但我可以在 gbd 中找到类似的操作。
我完全愿意有一个函数来传递对象类型,但我也无法让它工作。
有任何想法吗? 谢谢!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我是这样做的:
Here is how i do it:
您可以使用 p (*it)->second
You can use p (*it)->second
您可以尝试 Archer,这是一个主要致力于改善 C++ 调试体验的 gdb 开发分支。 点击此处查看 C++ 漂亮打印机的演示。
这个新项目还允许使用 python 脚本控制 gdb。 主要开发人员 Tom Tromey 写了很多关于这个激动人心的项目的博客。
You can try Archer, a gdb development branch primarily dedicated to improving the C++ debugging experience. Click here to see the demo of pretty printer for C++.
This new project also allows one to control gdb with python script. The primary developer, Tom Tromey, wrote quite a few blogs about this excited project.
我意识到这是一个老问题,但我认为我已经找到了“最佳”答案。 在搜索过程中,我发现了一个 .gdbinit 文件,它可以很好地取消引用 stl 类型。 显然有很多版本,但这是我能找到的最新版本:
http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
I realize that this is an old question, but I think I've found the "best" answer to it yet. In searching around, I came across a .gdbinit file that dereferences the stl types very well. There are apparently many versions of this floating around, but this is the newest one that I've been able to find:
http://www.yolinux.com/TUTORIALS/src/dbinit_stl_views-1.03.txt
p
将是std::pair
的迭代器,因此您真正想要的是p->first
。 不过,我认为 GDB 不能很好地处理重载运算符,因此您可能需要p.
{some member that indicates thepair
object}.first
。 libstdc++ 有 Doxygen 文档,因此您可以找出您的成员想要,在这种情况下,它看起来是((_Rb_tree_node >*)(p._M_node))->; _M_value_field.first
。 因为这非常冗长,所以我会首先检查运算符重载是否有效(不,我认为没有更简单的事情;抱歉)。 您也可以尝试显式调用运算符,但我认为 gcc 也无法做到这一点(例如it.operator*().first
)。编辑:等等,litb 的帖子似乎表明 gcc 确实支持 * 上的运算符重载。 奇怪,我总是发现这不起作用!
p
will be an iterator tostd::pair<const int, double>
, so what you actually want isp->first
. I don't think GDB handles overloaded operators well, though, so you probably wantp.
{some member that represents thepair
object}.first
. There is Doxygen documentation for libstdc++, so you can figure out what member you want, in this case it looks to be((_Rb_tree_node<pair<const int, double> >*)(p._M_node))-> _M_value_field.first
. Because this is pretty verbose, I would check to see if operator overloading works first (and no, I don't think there's anything simpler; sorry). You could also try explicitly calling operators, but I don't think gcc can do that either (e.g.it.operator*().first
).EDIT: wait, litb's post seems to show that gcc does support operator overloads on *. Weird, I always found that didn't work!