12

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!

kchoose2
  • 796
  • 1
  • 6
  • 13

5 Answers5

18

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)
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
4

You can use p (*it)->second

Raj
  • 41
  • 1
  • 1
    This is the most helpful answer I've found for actually accessing the contents of complex times in the first or second part of the pair. – Schamp Aug 04 '11 at 15:47
2

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

spuriousdata
  • 543
  • 2
  • 10
2

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.

Teng Lin
  • 129
  • 1
  • 1
  • 6
1

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!

Community
  • 1
  • 1
coppro
  • 14,338
  • 5
  • 58
  • 73
  • 1
    *it fails here. May be versioning issues with the library, the compiler, and/or gdb (or maybe a missing debug option). – strager Nov 26 '08 at 22:25
  • For me, *it works if i've filled the map using a[10]=9, but not if i've filled the map using a.insert(std::pair(10,9)). That seems very odd to me... – kchoose2 Nov 29 '08 at 18:31