Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to call inline functions in gdb and/or emit them using GCC?

We all know that inline functions can make debugging trickier, as they can be elided from stack traces etc. But suppose I want to call an inline function from within gdb, and I know its name and its arguments. I think I should be able to do that, but I get this:

Cannot evaluate function -- may be inlined

I used nm to list the symbols in the shared library I'm using, and found that the functions I want to call are not in there. No big surprise. What I'd like is a way to generate visible definitions of these inline functions. I have access to the header files which contain the inline definitions currently, but I can't really modify those headers. Perhaps there's some way to tell the compiler to emit definitions of all the inline functions it sees in a translation unit? Or some other trick that can make it easier to call and inspect the results of inline functions in gdb?

I'm using GCC 4.7.2 and GDB 7.5.1 on Linux. And I can't really switch to a non-optimized build because I'm debugging core dumps from production (in development I would just turn off optimization, then things are easier).

like image 775
John Zwinck Avatar asked Feb 26 '14 01:02

John Zwinck


1 Answers

One way to get the compiler to generate a callable version of an inline function is to include code that takes the address of the function. There is also an option you can give to gcc. From the gcc documentation on inline functions:

When a function is both inline and static, if all calls to the function are integrated into the caller, and the function's address is never used, then the function's own assembler code is never referenced. In this case, GCC does not actually output assembler code for the function, unless you specify the option -fkeep-inline-functions.

like image 165
Mark Plotnick Avatar answered Sep 24 '22 00:09

Mark Plotnick