Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot evaluate function -- may be in-lined" error in GDB for STL template container

Tags:

c++

gdb

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

E.g., given the following toy program:

#include <map>

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

}

which I compile as:

g++ -ggdb3 -O0 -std=c++11 -Wall -Wextra -pedantic -o main.out main.cpp

Then, when I try to examine a single element of map, for example:

p amap.begin()

I get:

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

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

Tested in Ubuntu 20.04, GCC 9.3.0, 2.34.

like image 759
SFD Avatar asked Nov 16 '16 13:11

SFD


1 Answers

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}
like image 152
ks1322 Avatar answered Sep 18 '22 05:09

ks1322