Jump to content
  • Advertisement
Sign in to follow this  

Getting Runtime stack info

This topic is 4792 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
if you are using windows, John robbins has a good tutorial on htis. Search for Bugslayer. We use it to capture the callstack during crashes.


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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!