Click here to Skip to main content
15,881,844 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello.

I want to get function call depth, for example
C++
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;
}

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.
C++
int d2(int i)
{trigger();
   if(i<=1){trigger();return i;}
   else
{trigger();
   return i*d1(i-1);}
}

And should look like
C#
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):
C++
//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?
Posted
Comments
nv3 27-Jun-12 9:09am    
Please tell us something about the operating system or systems that the solution must be suitable for. Also about the generality you expect, i.e. does it only have to work for some functions that are being instrumented (as you did with your trigger() call). And do you intend to use this very often, or just in an error reporting situation?
zzzteph 27-Jun-12 9:12am    
Linux,QNX,Win - 32b.
Yes, only functions that call trigger logged it current depth.

Your approach with the trigger function does not quite work if you call trigger 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.
 
Share this answer
 
Comments
zzzteph 28-Jun-12 5:19am    
Yes , will be right but main purpose is that trigger args shouldn't be like
1-function start and 2-function ends, because if project is big and have a lot of files it can take an hour to do this. The main reason is to automate this process: " knowing entries put there trigger and get call graph"
nv3 28-Jun-12 5:39am    
I understand. But just as I wrote, just recording the function entries will not give you enough information to track the nesting level. Example: Functions A and B use 1000 bytes on the stack. Function C uses 1500. Now call these functions in the following order:

A called
B called
B returned
A returned
C called

When C is called, your stack level drops from 2000 (recorder when B was called) to 1500. What you can't know is whether botch A and B have returned, or just B has returned.

Hence just recording the entries will be insufficient. I guess there is no other way then to record both, function entries AND exits.
zzzteph 28-Jun-12 6:04am    
Ok.I may be wrong.(c called,r - returned)
main c
A c
B c
B r
A r
C c
C r
main r

Put in stack address of var1.(main)
Put in stack address of var1(A because A.var1 will be less than M.var1)
Put in stack address of var1(B because B.var1 will be less than A.var1)
B returned - it's stack dropped(programm stack,not "virtually created"
A returned - it's stack dropped(programm stack,not "virtually created"
C called - If during main stack will become more than A+B stacks,I will get depth 4(A-2 , B-3 , main-1).And it is wrong.
nv3 28-Jun-12 7:07am    
Exactly. And in case C takes between A and A+B in stack space, your counting will tell you a depth of 3, which is also wrong. Hence I think you will need both, entry and exit reports.
zzzteph 28-Jun-12 7:42am    
No, B has depth 3, but doesn't matter, in linux there is backtrace(which work fine),windows(stackwalk) and need to get something like it in QNX.
use stack trace (using System.Diagnostics) for frame count.
 
Share this answer
 
Comments
zzzteph 27-Jun-12 8:06am    
Using C, not C#, also without adding special libraries like execinfo.h and other.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900