如何找到堆栈深度? [英] How to find stack depth?

查看:280
本文介绍了如何找到堆栈深度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用callb替换以下函数calla(参考:获取从C中的任何线程进行调用堆栈

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));
}

如何找到堆栈深度?

推荐答案

仅适用于 gcc 以及某些平台。我可以在此处重新输入所有文档,但很容易获得:如果您的版本至少为4.7.2,则是gcc手册( info gcc )的6.48节,并且在线此处

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.

请注意句子 level参数必须是一个常量整数。

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

您不能可靠地从 __ builtin_return_address 中获取堆栈高度,但是根据记录 __ builtin_frame_address 在您到达堆栈顶部时将返回0。

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.

这篇关于如何找到堆栈深度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆