Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get stack size and stack limit of any thread using Win32 API

Is it possible to obtain the size and limit of the stack of ANY thread using the Win32 API? I know that this is possible for the current thread in the following way:

NT_TIB *tib = (NT_TIB*)NtCurrentTeb();
DWORD stackBase = (DWORD)tib->StackBase;
DWORD stackLimit = (DWORD) tib->StackLimit;

However, I haven't found a Win32 API function that returns the NT_TIB structure for any given thread HANDLE or TID as input parameter.

like image 361
Benny Avatar asked Feb 13 '14 12:02

Benny


People also ask

How do you measure stack size?

size() method in Java is used to get the size of the Stack or the number of elements present in the Stack. Parameters: The method does not take any parameter. Return Value: The method returns the size or the number of elements present in the Stack.

What is stack size for threads?

Thread or fiber creation fails if there is not enough memory to reserve or commit the number of bytes requested. The default stack reservation size used by the linker is 1 MB.

What is the maximum size of stack?

In Visual Studio the default stack size is 1 MB i think, so with a recursion depth of 10,000 each stack frame can be at most ~100 bytes which should be sufficient for a DFS algorithm. Most compilers including Visual Studio let you specify the stack size.

How big is the stack on Windows?

For ARM64, x86, and x64 machines, the default stack size is 1 MB. The commit value is subject to interpretation by the operating system. In WindowsRT, it specifies the amount of physical memory to allocate at a time.


1 Answers

If you're debugging the process, you can get the TIB/TEB address from the lpThreadLocalBase field in the CREATE_THREAD_DEBUG_INFO structure.

Otherwise you'll have to rely on the semi-documented NtQueryInformationThread API. If you use the ThreadBasicInformation category, the returned THREAD_BASIC_INFORMATION struct has a TebBaseAddress field.

like image 93
Igor Skochinsky Avatar answered Nov 14 '22 23:11

Igor Skochinsky