How do I examine the contents of an std::vector in gdb, using the icc compiler?
17,078
Solution 1
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"
Solution 2
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.
Author by
Brett Hall
Updated on June 08, 2022Comments
-
Brett Hall almost 2 years
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.
-
Brett Hall over 15 yearsthanks for your help. gdb doesn't let me call myvector.front() on the print line? --> Couldn't find method std::vector<int, std::allocator<int> >::front
-
Max Lybbert over 15 yearsSorry, 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.
-
Brett Hall over 15 yearsthanks 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 over 15 yearsThanks. The icc compiled version (v9.1) doesn't seem to allow this. Strange.