c - 如何找到堆栈深度?

标签 c debugging

我想用 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/

相关文章:

python - Python-小小的初学者代码困惑

Javascript 调试 Visual Studio 2008

c - 打印结构

c - 确定一个字符串是否具有所有唯一字符?

c - 如何获取句子中回文词的总数

c# - 如何调试由 Windows 服务托管的 WCF

node.js - 调试 Protractor 时 Intellij Idea 挂起

debugging - 如何在 Visual Studio 中中断程序的第一条指令

将链表复制到C中的另一个列表

c - 是否可以将文件下载到指定目录?