Degg Degg - 19 days ago 5
C++ Question

C++, STL, GDB: Cannot evaluate function maybe inlined

I want to be able to get the address and print a single pair from an STL container using GDB.

I.E: Given the following toy program:

#include <map>

int main()
{
std::map<int,int> amap;
amap.insert(std::make_pair(1,2));

}


when I try to examine a single element of map (by for example p amap.begin()) I get:

"Cannot evaluate function -- may be in-lined"

Removing optimization and enabling full debug mode i.e (-O0 and -g3) doesnt work.

Why is this happening and how do I work around it?

Answer

This is because amap.begin() does not exist in resulting binary. This is how C++ templates work: if you don't use or explicitly instantiate some template method it is not generated in resulting binary.

If you do want to call amap.begin() from gdb you have to instantiate it. One way to do it is to instantiate all methods of std::map:

#include <map>

template class std::map<int,int>;

int main()
{
  std::map<int,int> amap;
  amap.insert(std::make_pair(1,2));
}

gdb session:

(gdb) p amap.begin()
$1 = {first = 1, second = 2}
Comments