获取函数调用深度 [英] Get function call depth
本文介绍了获取函数调用深度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好.
我想获取函数调用深度,例如
Hello.
I want to get function call depth, for example
int main()
{
int j=0;
for(j=0;j<10;j++)
printf("%i",d1(j));
return 0;
}
int d2(int i)
{
if(i<=1)return i;
else
return i*d1(i-1);
}
int d1(int i)
{
if(i%2==0)return d2(i/2);
return i;
}
树看起来像:
主要
-> d1
-> d2
-> d1
-> d1
-> d2
---> d2
主要问题是,我不必在每个代码部分中都放置触发器功能.
Tree will look like:
main
->d1
-->d2
->d1
->d1
-->d2
--->d2
The main problem, that i mustn''t be done with puting trigger function in every code part.
int d2(int i)
{trigger();
if(i<=1){trigger();return i;}
else
{trigger();
return i*d1(i-1);}
}
而且应该像
And should look like
int d2(int i)
{trigger();
if(i<=1);return i;
else
return i*d1(i-1);
}
我想使用类似堆栈地址的方法(建立自己的调用堆栈并比较函数地址):
I think to do it using stack address like(build own call stack and compare function addresses):
//pseudo
trigger()
{
int i=0;
if(current_thread.address[last_address]=>&i)
{
while(current_thread.address[last_address--]=>&i)current_thread.depth--;
current_thread.address[last_address]=&i;
}
else
{
current_thread.address[last_address+1]=&i;
current_thread.depth++;
}
}
无需任何概要分析扩展程序(库等)即可完成此操作的主要目的之一.
One of the main purpose to do it without any profiling compilier extensions (libs,etc). Am I right doing such things?
推荐答案
如果仅在函数的开头调用trigger
,则使用trigger
函数的方法将无法正常工作.仅此一项将无法为您提供足够的信息.
但是,如果您引入了在有功能的函数的每次退出之前调用的第二个函数,则该任务将非常容易,即使不查看堆栈地址也是如此.我将这些函数称为InstrEnter和InstrLeave之类,它们只会增加和减少全局级别的计数器字段.那是独立于操作系统的,并且易于调试.
Your approach with thetrigger
function does not quite work if you calltrigger
only at the begin of a function. That alone will not give you enough information.
However, if you introduce a second function that you call before every exit of an instrumented function, the task is quite easy, even without looking at stack addresses. I would call these functions something like InstrEnter and InstrLeave and these will just increment and decrement a global level counter field. That is operating system independent and easy enough to debug.
使用堆栈跟踪(使用System.Diagnostics)进行帧计数.
use stack trace (using System.Diagnostics) for frame count.
这篇关于获取函数调用深度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文