2

I have built gdb-7.12 with python support and have enabled pretty printing and configured my gdbinit file by following https://sourceware.org/gdb/wiki/STLSupport.

But whenever i print the size of any container:

p ivec.size()
Cannot evaluate function -- may be inlined

Here is the MCVE i am using

#include <vector>

using namespace std;

int main(){
  vector<int> ivec;
  return 0;
}

I have tried different compiling options

g++-6 -g -O0 -fno-inline-functions -gdwarf-2 Source.cpp --std=c++14

In fact i have tried every combination of the above options, and always the same problem.

I tried switching to gdb-7.11 (also built from source) to see if it fixes the problem and also switched to g++-4.8, none of them seem to fix the issue.

What am i doing wrong? Is there some specific order in which you have to give the options?

EDIT: Many people have suggested some macros to solve the problem, but my problem isn't to somehow print these functions, i can write my own pretty print methods for that.

My question is why are the functions showing up as inlined even after Disabling optimizations with -O0 options?

Vikash Balasubramanian
  • 2,921
  • 3
  • 33
  • 74
  • duplicate of http://stackoverflow.com/questions/22163730/cannot-evaluate-function-may-be-inlined ? – UKMonkey Oct 21 '16 at 09:39
  • 1
    or here http://stackoverflow.com/questions/427589/inspecting-standard-container-stdmap-contents-with-gdb – UKMonkey Oct 21 '16 at 09:40
  • 1
    @UKMonkey I don't think it is, i have seen those question and i tried out the optimization options and i don't require pretty printing to print the size of a vector – Vikash Balasubramanian Oct 21 '16 at 09:40
  • Does this answer your question? ["Cannot evaluate function -- may be in-lined" error in GDB for STL template container](https://stackoverflow.com/questions/40633787/cannot-evaluate-function-may-be-in-lined-error-in-gdb-for-stl-template-cont) (okay I guarantee *this* duplicate target is correct) – user202729 Dec 07 '21 at 05:53

2 Answers2

3

My question is why are the functions showing up as inlined even after Disabling optimizations with -O0 options?

g++ will only instantiate templates that are actually used by your program, and your program doesn't actually use the size method.

You can check this using nm:

$ nm -C q|grep size
$

If I change your program to use return ivec.size(), then I can:

(gdb) p ivec.size()
$1 = 0

This whole situation with inlining and non-instantiation is why the gdb xmethod support was written. And, libstdc++ has some xmethods (though I didn't check if it specifically has this one). I recommend using that.

Tom Tromey
  • 21,507
  • 2
  • 45
  • 63
3

The GDB error message Cannot evaluate function -- may be inlined is a bit misleading as inlining isn't the only reason for a function being unavailable.

In your example, std::vector<T>::size() simple isn't instantiated (into std::vector<int>::size()) at compile time because it isn't used.

It depends on the version of your GDB pretty printing support if it can work around the missing size() instantiation when you type p ivec.size() or call ivec.size() (e.g. GDB 8.0.1 on Fedora 27 can). But even with older versions of the STL pretty printing support you can work around this issue by just printing the vector itself, i.e.:

(gdb) p ivec
$1 = std::vector of length 0, capacity 0

However, you can explicitly make sure that size() is instantiated either by using it somewhere in your program or by explicitly instantiating the complete template class for int in one translation unit:

template class std::vector<int>;
maxschlepzig
  • 35,645
  • 14
  • 145
  • 182