Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save udaken/0092516becfb67b8296d2bde52b34c66 to your computer and use it in GitHub Desktop.
Save udaken/0092516becfb67b8296d2bde52b34c66 to your computer and use it in GitHub Desktop.
#include <chrono>
#include <thread>
#include <process.h>
#include <Windows.h>
#include <winternl.h>
#include <ProcessSnapshot.h>
using namespace std::literals;
int main(int argc, char *argv[]) {
unsigned tid;
auto thread = (HANDLE)_beginthreadex(nullptr, 128 * 1024, [](auto) -> unsigned {
std::this_thread::sleep_for(10s);
return 0;
}, nullptr, 0, &tid);
CloseHandle(thread);
printf("_beginthreadex(): created thread id: %u\n", tid);
printf("dump all stack sizes.\n");
HPSS snapshot;
auto pid = std::atoi(argv[1]);
auto hProcess = OpenProcess(GENERIC_READ, FALSE,pid);
PssCaptureSnapshot(hProcess, PSS_CAPTURE_THREADS, CONTEXT_ALL, &snapshot);
HPSSWALK walk;
PssWalkMarkerCreate(nullptr, &walk);
for (PSS_THREAD_ENTRY te; PssWalkSnapshot(snapshot, PSS_WALK_THREADS, walk, &te, sizeof te) == ERROR_SUCCESS;) {
auto pteb = reinterpret_cast<const TEB*>(te.TebBaseAddress);
NT_TIB teb = {};
SIZE_T size;
ReadProcessMemory(hProcess, pteb, &teb, sizeof(teb), &size);
auto stack_size = static_cast<const char*>(teb.StackBase) - static_cast<const char*>(teb.StackLimit);
printf(" tid: % 5u, % 7lldbytes\n", te.ThreadId, (LONGLONG)stack_size);
}
PssFreeSnapshot(hProcess, snapshot);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment