Sign in to follow this  

Getting Runtime stack info

Recommended Posts

Hi everyone, I was wondering if there is a way to obtain in C++ a way to display some information about stack usage (such as how much I'm using, how much I have left, etc.). There doesn't appear to be any easy way to do this. Thanks in advance, Tek

Share this post

Link to post
Share on other sites
Nothing in standard C++ will let you do this. Depending on your compiler and/or operating system there may be platform dependent ways at getting at this information.

Share this post

Link to post
Share on other sites
You'll have to use inline assembler routines. On Windows, the info you seek is stored in an NT_TIB structure, defined in winnt.h.

02995 typedef struct _NT_TIB {
02996 struct _EXCEPTION_REGISTRATION_RECORD *ExceptionList;
02997 PVOID StackBase;
02998 PVOID StackLimit;
02999 PVOID SubSystemTib;
03000 _ANONYMOUS_UNION union {
03001 PVOID FiberData;
03002 DWORD Version;
03004 PVOID ArbitraryUserPointer;
03005 struct _NT_TIB *Self;
03006 } NT_TIB,*PNT_TIB;

You can access a user mode instance of this structure via the FS segment. This code was written for lcc-win32. You can adapt it to work with your compiler - pretty much flip the register order should be enough to convert it to intel syntax. YMMV.

void __declspec(naked) _stdcall
RtlpGetStackLimits(DWORD *pStackLimit, DWORD *pStackBase)

"pushl %ebp; \n"
"movl %esp,%ebp; \n"
"movl 12(%ebp),%ecx; \n"
"movl $0,(%ecx); \n" // *pStackBase = 0;
"fs; \n"
"movl (0x4),%eax; \n" // stack base found at %fs:(0x4)
"movl %eax,(%ecx); \n"
"movl 8(%ebp),%ecx; \n"
"movl $0,(%ecx); \n" // *pStackLimit = 0;
"fs; \n"
"movl (0x8),%eax; \n" // stack limit found at %fs:(0x8)
"movl %eax,(%ecx); \n"
"popl %ebp; \n"
"ret $8;"


call example:

DWORD StackLimit,StackBase;

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this