我想用 callb 替换以下函数 calla(引用: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));
}
如何找到堆栈深度?
最佳答案
这只适用于 gcc
,并且在某些平台上。我可以在这里重新输入所有文档,但它很容易获得:它是 gcc 手册 (info gcc
) 的第 6.48 节,如果你有版本 4.7.2,至少,它在线 here .
请注意“级别参数必须是常量整数”这句话。这将使循环变得棘手。
您无法从 __builtin_return_address
可靠地获取堆栈高度,但根据文档,当您到达堆栈顶部时,__builtin_frame_address
将返回 0。
关于c - 如何找到堆栈深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13944372/