Tectrendz
Tectrendz

Reputation: 1454

How to find stack depth?

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

How to find the stack depth ?

Upvotes: 4

Views: 781

Answers (1)

rici
rici

Reputation: 241671

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.

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

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.

Upvotes: 1

Related Questions