Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C Threading
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;
}
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?
Posted 27-Jun-12 1:47am
zzzteph360
Comments
nv3 at 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 at 27-Jun-12 9:12am
   
Linux,QNX,Win - 32b.
Yes, only functions that call trigger logged it current depth.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

use stack trace (using System.Diagnostics) for frame count.
  Permalink  
Comments
zzzteph at 27-Jun-12 8:06am
   
Using C, not C#, also without adding special libraries like execinfo.h and other.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
zzzteph at 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 at 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 at 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 at 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 at 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.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 480
1 mhegazy94 460
2 Ravi Bhavnani 190
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,205
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,644
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 27 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100