Chinaunix首页 | 论坛 | 博客
  • 博客访问: 301185
  • 博文数量: 94
  • 博客积分: 2163
  • 博客等级: 大尉
  • 技术积分: 932
  • 用 户 组: 普通用户
  • 注册时间: 2010-12-20 09:23
文章存档

2012年(2)

2011年(92)

分类: C/C++

2011-10-09 10:24:23

  __builtin_return_address(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.
  The LEVEL argument must be a constant integer.
  On some machine 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';
 
This function should only be used with a non-zero argument for debuging perposes.
 1 /* return the address of the current function*/
  2 #include
  3 void foo(void);
  4 int main()
  5 {
  6         foo();
  7         size_t *p = __builtin_return_address(0);
  8         printf("main = %x", p);
  9
 10 return 0;
 11 }
 12 void foo()
 13 {
 14         size_t *p;
 15         p = __builtin_return_address(0);
 16         printf("0 = %x",p);
 17         p = __builtin_return_address(1);
 18         printf("1 =%x", p);
 19         p = __builtin_return_address(2);
 20         printf("2 = %x", p);
 21 }
 22
[wangzhen@wangzhen kernel]$./__builtin_return_address
0 = 80483ba 1 =9dee9c 2 = 80482f1 main = 9dee9c
 main 函数地址 9dee9c和 p = __builtin_return_address(1)相同。
0 返回的函数地址。
__builtin_return_address()函数在gcc源代码中。就不分析了。
阅读(1680) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~