How to find stack depth?

故事扮演 提交于 2020-02-20 11:50:50

问题


I want to replace following function calla with callb (Reference : Get call stack from any thread within C )

int calla()
{
   printf("Inside calla\n");
   printf("A1=%x\n",__builtin_return_address (0));
   printf("A2=%x\n",__builtin_return_address (1) );
   printf("A3=%x\n",__builtin_return_address (2) );
}


int callb()
{
   int i,j;
   j = stackdepth(); 
   for (i=0 ; i<j ;i++) 
   printf("%x\n",__builtin_return_address (i));
}

How to find the stack depth ?


回答1:


This only works with gcc, and on certain platforms. I could retype all the documentation here, but it's easy enough to get: it's section 6.48 of the gcc manual (info gcc) if you have version 4.7.2, at least, and it's online here.

Note the sentence "The level argument must be a constant integer." which will make looping tricky.

You cannot reliably get the stack height from __builtin_return_address, but according to the documention __builtin_frame_address will return 0 when you hit the top of the stack.



来源:https://stackoverflow.com/questions/13944372/how-to-find-stack-depth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!