On Thu, Jun 19, 2003 at 02:54:53PM +0200, Eli Billauer wrote:
> Hello Orna.
> 
> What I would do, is to write a small function which returns the value of 
> the stack pointer, when it was called. It's not very accurate, but it 
> can give a good indication of how heavily the stack is consumed.

There are two ways to do this, both straight out of "introduction to
operating systems" 

int foo(void)
{
        char b; 
        
        unsigned long stackaddr = &b; 

        fprintf(stdout, "stack is roughly at %lx\n", stackaddr); 
}

and the other, much more elegant, is using inline asm to get the value
of %esp. 

#include <stdio.h> 

static inline unsigned long getstack(void)
{
        unsigned long stackaddr; 

        /* I think the constraints are correct, but won't bet on it */
        asm volatile("movl %%esp,%0\n\t" :"=m" (stackaddr):); 
        
        return stackaddr;
}


int main(int argc, char* argv[])
{
        fprintf(stdout, "stack pointer: 0x%lx\n", getstack()); 

        return 0; 
}

-- 
Muli Ben-Yehuda
http://www.mulix.org
http://www.livejournal.com/~mulix/

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to