16

I want to examine the contents of a std::vector in gdb but I don't have access to _M_impl because I'm using icc, not gcc, how do I do it? Let's say it's a std::vector for the sake of simplicity.

There is a very nice answer here but this doesn't work if I use icc, the error message is "There is no member or method named _M_impl". There appears to be a nice debug toolset here but it also relies on _M_impl.

Community
  • 1
  • 1
Brett Hall
  • 275
  • 1
  • 4
  • 8

3 Answers3

11

Not sure this will work with your vector, but it worked for me.

#include <string>
#include <vector>

int main() {
    std::vector<std::string> vec;
    vec.push_back("Hello");
    vec.push_back("world");
    vec.push_back("!");
    return 0;
}

gdb:

(gdb) break source.cpp:8
(gdb) run
(gdb) p vec.begin()
$1 = {
   _M_current = 0x300340
}
(gdb) p $1._M_current->c_str()
$2 = 0x3002fc "Hello"
(gdb) p $1._M_current +1
$3 = (string *) 0x300344
(gdb) p $3->c_str()
$4 = 0x30032c "world"
Mic
  • 6,741
  • 3
  • 24
  • 25
4

Generally when I deal with the container classes in a debugger, I build a reference to the element, as a local variable, so it is easy to see in the debugger, without mucking about in the container implementation.

Here is a contrived example.

vector<WeirdStructure>  myWeird;

/* push back a lot of stuff into the vector */ 

size_t z;
for (z = 0; z < myWeird.size(); z++)
{
    WeirdStructure& weird = myWeird[z];

    /* at this point weird is directly observable by the debugger */ 

    /* your code to manipulate weird goes here */  
}

That is the idiom I use.

EvilTeach
  • 28,120
  • 21
  • 85
  • 141
0

The std::vector template guarantees the data is stored contiguously. If you take the address of the front element (say, &v[0], for instance), you can access any other element in the vector through a C-style array. That doesn't require you to have the source code of the STL available to your debugger.


After messing with this some, it appears that v.front() and v.begin() are likely inlined and GDB isn't finding them. I'll keep looking, but personally I would simply add the line int* i = &v[0] to the source file, and then use GDB commands on i while debugging. Note that the compiler is free to remove that dead code. You may need to output the value of i to avoid that, or simply not crank up optimizations.

Max Lybbert
  • 19,717
  • 4
  • 46
  • 69
  • how do I access the first element? myvector[0] doesn't work in gdb..? – Brett Hall Nov 26 '08 at 22:46
  • thanks for your help. gdb doesn't let me call myvector.front() on the print line? --> Couldn't find method std::vector >::front – Brett Hall Nov 26 '08 at 22:49
  • Sorry, last time I tried to call member functions in gdb I had to pass in the name-mangled name (that is, the symbol that actually appears in the binary). I'm sure they fixed it by now. &v[0] is more common to see than &(v.front()), so I've changed the answer to use that. – Max Lybbert Nov 26 '08 at 22:52
  • thanks again for your help. This is the first thing I tried, the [] operator also doesn't work. >print nactive_serverByTB[0] One of the arguments you tried to pass to operator[] could not be converted to what the function wants. – Brett Hall Nov 26 '08 at 22:58
  • Thanks. The icc compiled version (v9.1) doesn't seem to allow this. Strange. – Brett Hall Nov 27 '08 at 00:55