views:

1631

answers:

4

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!

+10  A: 

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
Once you print those, how can you walk other structures inside of them (assuming your map contains pointers to other things)?
Paul Tarjan
+1  A: 

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!

coppro
well, it->first doesn't work here too :) but *it works fine.
Johannes Schaub - litb
*it fails here. May be versioning issues with the library, the compiler, and/or gdb (or maybe a missing debug option).
strager
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<int,int>(10,9)).That seems very odd to me...
kchoose2
+2  A: 

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
A: 

You can use p (*it)->second

Raj