Stack size remaining or stack limit

Stack size remaining or stack limit

Post by David C. Partridg » Sat, 28 Oct 2000 18:53:34



From a running thread I want to be able to report the remaining stack
size available (for debugging purposes).

I tried the following code which gets the current stack pointer from
ESP, and attempted to get the stacklimit from the TIB.

   PNT_TIB  ptib = 0;
   PVOID    currentEsp = 0;
   __asm
   {
     mov  eax,FS:[18h]
     mov  [ptib],eax
     mov  [currentEsp],esp
   }

   return (char*)currentEsp - (char*)ptib->StackLimit;

The problem is that the stack limit in the TIB is the currently
committed low stack address, rather than the brick wall at which a stack
overflow would occur.

I know that the TIB contains the StackBase address as well, so I could
just report the amount of stack USED by returning StackBase-currentEsp,
but I'd v. much prefer to report the amount of stack left.

Does anyone know how to dig out the relevant information?

Regards
Dave Partridge

 
 
 

Stack size remaining or stack limit

Post by David C. Partridg » Sat, 04 Nov 2000 04:00:00


I know its bad form to reply to your own posts, but surely there must be
some maven out there who knows an answer to this ...

Dave


> From a running thread I want to be able to report the remaining stack
> size available (for debugging purposes).

> I tried the following code which gets the current stack pointer from
> ESP, and attempted to get the stacklimit from the TIB.

>    PNT_TIB  ptib = 0;
>    PVOID    currentEsp = 0;
>    __asm
>    {
>      mov  eax,FS:[18h]
>      mov  [ptib],eax
>      mov  [currentEsp],esp
>    }

>    return (char*)currentEsp - (char*)ptib->StackLimit;

> The problem is that the stack limit in the TIB is the currently
> committed low stack address, rather than the brick wall at which a stack
> overflow would occur.

> I know that the TIB contains the StackBase address as well, so I could
> just report the amount of stack USED by returning StackBase-currentEsp,
> but I'd v. much prefer to report the amount of stack left.

> Does anyone know how to dig out the relevant information?

> Regards
> Dave Partridge


 
 
 

Stack size remaining or stack limit

Post by Jan Beulic » Fri, 10 Nov 2000 04:00:00


If all else fails, use VirtualQuery for the current ESP.


> From a running thread I want to be able to report the remaining stack
> size available (for debugging purposes).

> I tried the following code which gets the current stack pointer from
> ESP, and attempted to get the stacklimit from the TIB.

>    PNT_TIB  ptib = 0;
>    PVOID    currentEsp = 0;
>    __asm
>    {
>      mov  eax,FS:[18h]
>      mov  [ptib],eax
>      mov  [currentEsp],esp
>    }

>    return (char*)currentEsp - (char*)ptib->StackLimit;

> The problem is that the stack limit in the TIB is the currently
> committed low stack address, rather than the brick wall at which a stack
> overflow would occur.

> I know that the TIB contains the StackBase address as well, so I could
> just report the amount of stack USED by returning StackBase-currentEsp,
> but I'd v. much prefer to report the amount of stack left.

> Does anyone know how to dig out the relevant information?

> Regards
> Dave Partridge

--
-----------------------------
Jan Beulich
Software Engineer
Novell - the leading provider of Net Services software

Please, respond to news group messages only by another posting, not by
email.