public ulong GetThreadTeb(uint thread) { SetClientInstance(); ulong teb = 0; int hr = _systemObjects.GetCurrentThreadId(out uint id); bool haveId = hr == 0; if (_systemObjects.GetThreadIdBySystemId(thread, out id) == 0 && _systemObjects.SetCurrentThreadId(id) == 0) { _systemObjects.GetCurrentThreadTeb(out teb); } if (haveId) { _systemObjects.SetCurrentThreadId(id); } return(teb); }
private ulong GetStacksSize(DataTarget target) { // Find all the TEBs and then sum StackBase - StackLimit for all of them. // This gives us the committed size for each thread, but we don't have the // reserved size (which is the actual address space consumed). Theoretically, // we could get it from enumerating the memory region adjacent to the committed // pages and the guard page that follows. Also, for WoW64 threads, we are only // reporting the x86 stack (the x64 stack doesn't live in the 4GB address space // anyway, so it's not that relevant). IDebugSystemObjects sysObjects = (IDebugSystemObjects)target.DebuggerInterface; uint numThreads; HR.Verify(sysObjects.GetNumberThreads(out numThreads)); ulong totalCommit = 0; for (uint i = 0; i < numThreads; ++i) { HR.Verify(sysObjects.SetCurrentThreadId(i)); ulong tebAddress; HR.Verify(sysObjects.GetCurrentThreadTeb(out tebAddress)); int read; byte[] teb = new byte[IntPtr.Size * 3]; // ExceptionList, StackBase, StackLimit if (target.ReadProcessMemory(tebAddress, teb, teb.Length, out read) && read == teb.Length) { ulong stackBase = AddressFromBytes(teb, IntPtr.Size); ulong stackLimit = AddressFromBytes(teb, IntPtr.Size * 2); totalCommit = stackBase - stackLimit; } } return(totalCommit); }