Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

get stack usage of process(or thread)

16 views
Skip to first unread message

Chung Ha-nyung

unread,
May 6, 2002, 9:06:06 PM5/6/02
to

Can I get the usage of stack in a process(or thread)?

--
Chung Ha-nyung <al...@kldp.org>

Radoslav Getov

unread,
May 7, 2002, 10:44:01 AM5/7/02
to
Chung Ha-nyung <al...@kldp.org> wrote in message news:<3CD7287E...@kldp.org>...

> Can I get the usage of stack in a process(or thread)?

something like:

char* stack_base = NULL;

int main()
{
char x;
stack_base = &x;
...

}

int getStatckSize()
{
char x,
*xx = &x;

return xx - stack_base;
}

...but this is neither standard nor portable.

Radoslav Getov
rge...@ultraheap.com

Kasper Dupont

unread,
May 7, 2002, 7:36:10 PM5/7/02
to
Radoslav Getov wrote:
>
> Chung Ha-nyung <al...@kldp.org> wrote in message news:<3CD7287E...@kldp.org>...
> > Can I get the usage of stack in a process(or thread)?
>
> something like:
>
> char* stack_base = NULL;
>
> int main()
> {
> char x;
> stack_base = &x;
> ...
>
> }
>
> int getStatckSize()
> {
> char x,
> *xx = &x;
>
> return xx - stack_base;

On most architectures that would be the other way
around because stacks grows downwards from top of
memory.

BTW It could be simplified a little:

int getStackSize()
{
char x;
return stack_base-&x;
}


> }
>
> ...but this is neither standard nor portable.

Except from the direction in which stacks grow I
think it is very portable. I tried it on IRIX,
Solaris, and Linux. (I will try it on my Amiga
some day. ;-) )

--
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:razor-...@daimi.au.dk

Joshua Jones

unread,
May 7, 2002, 10:25:23 PM5/7/02
to
In comp.os.linux.development.apps Chung Ha-nyung <al...@kldp.org> wrote:

> Can I get the usage of stack in a process(or thread)?

Don't know if this is what you mean exactly, but this can be used
to give the address of the stack pointer:

int main( void )
{
unsigned long * spaddr;

asm( "movl %%esp, %%eax;
movl %%eax, %0;"
:"=r"(spaddr)
:
:"%eax" );

printf("stack address: 0x%x\n", spaddr);
}


--
J o s h u a J o n e s *** email: jajones(at)cc.gatech.edu
__ .~.
College of Computing at the | / / _ _ _ _ _ __ __ /V\
Georgia Institue of Technology | / /__ / / / \// //_// \ \/ / // \\
Atlanta, Georgia, U.S. | /____/ /_/ /_/\/ /___/ /_/\_\ /( )\
*Debian GNU/Linux* ^^-^^

0 new messages