1、gcc默认不支持__builtin_return_address(LEVEL)的参数为非0。好像只支持参数为0。
2、__builtin_return_address(0)的含义是,得到当前函数返回地址,即此函数被别的函数调用,然后此函数执行完毕后,返回,所谓返回地址就是那时候的地址。
3、__builtin_return_address(1)的含义是,得到当前函数的调用者的返回地址。注意是调用者的返回地址,而不是函数起始地址。
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#define MAX_LEVEL 4
void sigfunc(int signo)
{
printf("%s(0): %p\n", __func__, __builtin_return_address(0));
exit(1);
}
int b()
{
printf("%s(0): %p\n", __func__, __builtin_return_address(0));
printf("%s(1): %p\n", __func__, __builtin_return_address(1));
while(1)
{
sleep(1);
}
}
int a(int temp)
{
temp += 1;
printf("%s(0): %p\n", __func__, __builtin_return_address(0));
b();
return temp;
}
int main()
{
signal(SIGINT, sigfunc);
a(123);
return 0;
}
执行后:
a(0): 0x400675
b(0): 0x400653
b(1): 0x400675
^Csigfunc(0): 0x30bda33140
用gdb调试:
gdb e
...
(gdb) l *0x400675
0x400675 is in main (e.c:37).
32 int main()
33 {
34 signal(SIGINT, sigfunc);
35 a(123);
36
37 return 0;
38 }
Getting the Return or Frame Address of a Function
These functions may be used to get information about the callers of a function.
Built-in Function: void * __builtin_return_address (unsigned int level)
This function returns the return address of the current function, or of one of its callers. The level argument is number of frames to scan up the call stack. A value of 0
yields the return address of the current function, a value of 1
yields the return address of the caller of the current function, and so forth. When inlining the expected behavior is that the function returns the address of the function that is returned to. To work around this behavior use the noinline
function attribute.
The level argument must be a constant integer.
On some machines it may be impossible to determine the return address of any function other than the current one; in such cases, or when the top of the stack has been reached, this function returns an unspecified value. In addition, __builtin_frame_address
may be used to determine if the top of the stack has been reached.
Additional post-processing of the returned value may be needed, see __builtin_extract_return_addr
.
The stored representation of the return address in memory may be different from the address returned by __builtin_return_address
. For example, on AArch64 the stored address may be mangled with return address signing whereas the address returned by __builtin_return_address
is not.
Calling this function with a nonzero argument can have unpredictable effects, including crashing the calling program. As a result, calls that are considered unsafe are diagnosed when the -Wframe-address option is in effect. Such calls should only be made in debugging situations.
On targets where code addresses are representable as void *
,
void *addr = __builtin_extract_return_addr (__builtin_return_address (0));
gives the code address where the current function would return. For example, such an address may be used with dladdr
or other interfaces that work with code addresses.
Built-in Function: void * __builtin_extract_return_addr (void *addr)
The address as returned by __builtin_return_address
may have to be fed through this function to get the actual encoded address. For example, on the 31-bit S/390 platform the highest bit has to be masked out, or on SPARC platforms an offset has to be added for the true next instruction to be executed.
If no fixup is needed, this function simply passes through addr.
Built-in Function: void * __builtin_frob_return_addr (void *addr)
This function does the reverse of __builtin_extract_return_addr
.
Built-in Function: void * __builtin_frame_address (unsigned int level)
This function is similar to __builtin_return_address
, but it returns the address of the function frame rather than the return address of the function. Calling __builtin_frame_address
with a value of 0
yields the frame address of the current function, a value of 1
yields the frame address of the caller of the current function, and so forth.
The frame is the area on the stack that holds local variables and saved registers. The frame address is normally the address of the first word pushed on to the stack by the function. However, the exact definition depends upon the processor and the calling convention. If the processor has a dedicated frame pointer register, and the function has a frame, then __builtin_frame_address
returns the value of the frame pointer register.
On some machines it may be impossible to determine the frame address of any function other than the current one; in such cases, or when the top of the stack has been reached, this function returns 0
if the first frame pointer is properly initialized by the startup code.
Calling this function with a nonzero argument can have unpredictable effects, including crashing the calling program. As a result, calls that are considered unsafe are diagnosed when the -Wframe-address option is in effect. Such calls should only be made in debugging situations.
参考文章:
https://blog.csdn.net/vpwork/article/details/7680102
http://gcc.gnu.org/onlinedocs/gcc/Return-Address.html
http://blog.chinaunix.net/uid-26817832-id-3351553.html