在 gdb 中显示解引用的 STL 迭代器

发布于 2024-07-09 15:07:04 字数 373 浏览 6 评论 0 原文

我有一个映射元素的迭代器,我希望 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 中找到类似的操作。

我完全愿意有一个函数来传递对象类型,但我也无法让它工作。

有任何想法吗? 谢谢!

I have an iterator to a map element, and I would like gdb to show me the values of the "first" and "second" elements of that iterator.
For example:

std::map<int,double> aMap;
...fill map...
std::map<int,double>::const_iterator p = aMap.begin();

I can use p.first and p.second in the code, but can't see them in gdb. For what it's worth, in dbx one could do something like "print p.node.second_", but I can find anything similar in gbd.

I am totally willing to have a function into which I pass the object types, but I've been unable to get that to work either.

Any ideas?
Thanks!

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

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

发布评论

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

评论(5

金橙橙 2024-07-16 15:07:04

我是这样做的:

This GDB was configured as "i686-pc-linux-gnu"...
(gdb) list
1       #include <iostream>
2       #include <map>
3
4       int main()
5       {
6           std::map<int, int> a;
7           a[10] = 9;
8           std::map<int, int>::iterator it = a.begin();
9           ++it;
10      }
(gdb) b test.cpp:9
Breakpoint 1 at 0x8048942: file test.cpp, line 9.
(gdb) r
Starting program: /home/js/cpp/a.out

Breakpoint 1, main () at test.cpp:9
9           ++it;
(gdb) set print pretty on
(gdb) p it
$1 = {
  _M_node = 0x94fa008
}
(gdb) p *it
$2 = (class std::pair<const int, int> &) @0x94fa018: {
  first = 10,
  second = 9
}
(gdb)

Here is how i do it:

This GDB was configured as "i686-pc-linux-gnu"...
(gdb) list
1       #include <iostream>
2       #include <map>
3
4       int main()
5       {
6           std::map<int, int> a;
7           a[10] = 9;
8           std::map<int, int>::iterator it = a.begin();
9           ++it;
10      }
(gdb) b test.cpp:9
Breakpoint 1 at 0x8048942: file test.cpp, line 9.
(gdb) r
Starting program: /home/js/cpp/a.out

Breakpoint 1, main () at test.cpp:9
9           ++it;
(gdb) set print pretty on
(gdb) p it
$1 = {
  _M_node = 0x94fa008
}
(gdb) p *it
$2 = (class std::pair<const int, int> &) @0x94fa018: {
  first = 10,
  second = 9
}
(gdb)
半葬歌 2024-07-16 15:07:04

您可以使用 p (*it)->second

You can use p (*it)->second

嘴硬脾气大 2024-07-16 15:07:04

您可以尝试 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.

晨曦慕雪 2024-07-16 15:07:04

我意识到这是一个老问题,但我认为我已经找到了“最佳”答案。 在搜索过程中,我发现了一个 .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

陈独秀 2024-07-16 15:07:04

p 将是 std::pair 的迭代器,因此您真正想要的是 p->first。 不过,我认为 GDB 不能很好地处理重载运算符,因此您可能需要 p.{some member that indicates the pair 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 to std::pair<const int, double>, so what you actually want is p->first. I don't think GDB handles overloaded operators well, though, so you probably want p.{some member that represents the pair 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!

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