35

I wrote a function similar to this:

class abc {
    private :
    int m_var ;
    public :
    int func() { return m_var ; }
};

When I try to print the func() using an abc object pointer in gdb, it is giving the error:

**Cannot evaluate function -- may be inlined**

How to can I print values from an inlined function?

Antonio Pérez
  • 6,702
  • 4
  • 36
  • 61
Avinash Kumar
  • 739
  • 2
  • 10
  • 25
  • 1
    Try this to avoid inline function. [how-can-i-tell-gcc-not-to-inline-a-function](http://stackoverflow.com/questions/1474030/how-can-i-tell-gcc-not-to-inline-a-function) – rmi Mar 04 '14 at 05:38
  • ^ Possible duplicate of [How can I tell gcc not to inline a function?](http://stackoverflow.com/questions/1474030/how-can-i-tell-gcc-not-to-inline-a-function?lq=1) – Jason C Mar 04 '14 at 05:49
  • For template function see [C++, STL, GDB: Cannot evaluate function maybe inlined - Stack Overflow](https://stackoverflow.com/questions/40633787/c-stl-gdb-cannot-evaluate-function-maybe-inlined) (summary: requires explicit template instantiation) – user202729 Dec 04 '21 at 12:01
  • See also [c++ - Possible to call inline functions in gdb and/or emit them using GCC? - Stack Overflow](https://stackoverflow.com/questions/22029834/possible-to-call-inline-functions-in-gdb-and-or-emit-them-using-gcc) – user202729 Dec 07 '21 at 05:55

2 Answers2

22

You got this error because you put func's definition in the class body and it's small enough, so, first, the compiler inlined this function ---- that means, the compile will substitute all the appearance of this function's call with its definition, and no definition of this function will be in the executable file. And, second, you didn't really call that function in your program, so in fact, this function never exist in your final executable file!

To solve that:

  1. You can put the definition of func outside the class body.
  2. Call func in your program anywhere.
nicky_zs
  • 3,633
  • 1
  • 18
  • 26
17

When the function is inlined, it doesn't appear as a proper symbol in the executable, so there's no way for gdb to execute it. The simplest thing to do is probably to compile with function inlining disabled, either by -fno-inline-functions or (still better) -O0.

Scott Lawrence
  • 1,023
  • 6
  • 14
  • 1
    Adding to this answer, you can disable it on specific functions with `__attribute((noinline))__`. There is also `-fno-inline-small-functions`, IIRC. Also, function inlining will not be enabled with `-O1` either, if you still want some optimization. – Jason C Mar 04 '14 at 05:46
  • 3
    In this case, adding -fno-inline-functions doesn't solve the problem. TRY IT with your compiler first and then post your answer. – nicky_zs Mar 04 '14 at 05:52
  • @nicky_zs, it is working for me with -O0 or -fno-inline-functions. – Scott Lawrence Mar 04 '14 at 06:01
  • 2
    @bytbox, really? Did you call the function func in your code? I tried this: if I didn't call this function in my code, -fno-inline-functions doesn't work. – nicky_zs Mar 04 '14 at 06:06
  • Ah no, I assumed it would actually be called elsewhere. – Scott Lawrence Mar 04 '14 at 06:10
  • I just ran into this problem, and the problem wasn't inlining, as @nicky_zs pointed out, the function was being removed by the linker because it wasn't directly called. – Wug Oct 30 '14 at 06:16
  • 8
    To prevent the linker from removing an uncalled function, add `__attribute__((used))`. – Eran Nov 14 '17 at 12:20
  • Unfortunately even these methods doesn't work when the data type is function-local (internal linkage). – user202729 Dec 06 '21 at 15:53