问题
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).
回答1:
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.
回答2:
Building your project with GCC option -gdwarf-2
may be helpful in this situation.
According to 11.1 Inline Functions in GDB documentation:
For gdb to support inlined functions, the compiler must record information about inlining in the debug information — gcc using the dwarf 2 format does this, and several other compilers do also. gdb only supports inlined functions when using dwarf 2. Versions of gcc before 4.1 do not emit two required attributes (‘DW_AT_call_file’ and ‘DW_AT_call_line’); gdb does not display inlined function calls with earlier versions of gcc. It instead displays the arguments and local variables of inlined functions as local variables in the caller.
Also see 3.9 Options for Debugging Your Program or GCC
来源:https://stackoverflow.com/questions/22029834/possible-to-call-inline-functions-in-gdb-and-or-emit-them-using-gcc