Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find stack depth?

Tags:

c

debugging

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 ?

like image 951
Tectrendz Avatar asked Sep 09 '26 18:09

Tectrendz


1 Answers

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.

like image 78
rici Avatar answered Sep 11 '26 09:09

rici



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!