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

前端 未结 2 879
后悔当初
后悔当初 2021-01-17 23:26

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 

        
相关标签:
2条回答
  • 2021-01-17 23:53

    @ks1322 has the correct answer. Here are some additional information that may be useful in the future.

    Only the constructor, destructor and insert methods on std::map are in the debuginfo:

    (gdb) info functions std::map
    All functions matching regular expression "std::map":
    
    File /usr/include/c++/6/bits/stl_map.h:
    std::pair<std::_Rb_tree_iterator<std::pair<int const, int> >, bool> std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::insert<std::pair<int, int>, void>(std::pair<int, int>&&);
    void std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::map();
    void std::map<int, int, std::less<int>, std::allocator<std::pair<int const, int> > >::~map();
    

    Still, we can call both the size and empty methods:

    (gdb) p amap.size()
    $1 = 1
    (gdb) p amap.empty()
    $2 = false
    

    That's because gdb has something called xmethods, a python API for calling mockup functions meant to work identically to the functions that have not been instantiated. The libstdc++ xmethods can be found here. If we disable them, then the same error message appears:

    (gdb) disable xmethod
    (gdb) p amap.size()
    Cannot evaluate function -- may be inlined
    (gdb) p amap.empty()
    Cannot evaluate function -- may be inlined
    (gdb) 
    
    0 讨论(0)
  • 2021-01-17 23:59

    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}
    
    0 讨论(0)
提交回复
热议问题