Skip to content
Advertisement

__builtin_return_address returns null for index >0?

I want to get the return address of the caller function. I’m using __builtin_return_address() funtion, but if I give index value greater than 0 it is returning NULL.

Please help me with this or tell me any other function to get the same.

Advertisement

Answer

See this answer to a related question.

__builtin_return_address is GCC and processor specific (also available in some versions of Clang on some processors with some -lack of- optimizations), and documented as

On some machines it may be impossible to determine the return address of any function other than the current one

The compiler might optimize a function (e.g. when it is compiled with -fomit-frame-pointer, or for tail-calls, or by function inlining) without the relevant information.

So probably you are getting NULL because the information is not available!

Advertisement