__builtin_extract_return_addr 更多gcc的东西见 http://gcc.gnu.org/onlinedocs/gcc/

6.47 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 will return the address of the function that will be 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 will return 0 or a random 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_address.

This function should only be used with a nonzero argument for debugging purposes.

— Built-in Function: void * __builtin_extract_return_address ( 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_address ( void *addr)

This function does the reverse of __builtin_extract_return_address.

— 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 which 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 will return 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 will return 0 if the first frame pointer is properly initialized by the startup code.

This function should only be used with a nonzero argument for debugging purposes.

`__builtin_extract_return_addr()`函数是GCC和Clang编译器提供的一个内建函数(built-in function),用于提取当前函数调用者的返回地址。 该函数的作用是提取当前函数调用者所在代码位置的返回地址。返回地址是指在函数调用结束后,程序将继续执行的下一条指令的地址。通过`__builtin_extract_return_addr()`函数,您可以获取到这个返回地址。 一般情况下,`__builtin_extract_return_addr()`函数需要与`__builtin_return_address()`函数配合使用。`__builtin_return_address()`函数用于获取当前函数的返回地址,然后可以通过`__builtin_extract_return_addr()`函数来提取调用者的返回地址。 以下是一个示例代码,演示了如何使用`__builtin_return_address()`和`__builtin_extract_return_addr()`函数来获取当前函数和调用者的返回地址: ```c #include <stdio.h> void foo() { void* return_addr = __builtin_return_address(0); void* caller = __builtin_extract_return_addr(return_addr); printf("Current function return address: %p\n", return_addr); printf("Caller function return address: %p\n", caller); } int main() { foo(); return 0; } ``` 在上述示例中,`__builtin_return_address(0)`用于获取当前函数的返回地址,然后将其作为参数传递给`__builtin_extract_return_addr()`函数以提取调用者的返回地址。 请注意,`__builtin_extract_return_addr()`函数可能依赖于编译器的实现,并且在不同的编译器上行为可能有所不同。因此,使用时请注意兼容性和可移植性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值