Forked from t-mat/win32-capture-stack-back-trace.cpp
Last active
November 19, 2020 10:21
-
-
Save Melonpi/1197e7d999c1d1192f42e62cdb65a224 to your computer and use it in GitHub Desktop.
Win32: CaptureStackBackTrace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define WIN32_LEAN_AND_MEAN | |
| #include "windows.h" | |
| #pragma warning(push) | |
| #pragma warning(disable : 4091) | |
| #include "DbgHelp.h" | |
| #pragma comment(lib, "DbgHelp.lib") | |
| #pragma warning(pop) | |
| void printStack(void) | |
| { | |
| static const int MAX_STACK_COUNT = 64; | |
| void* stack[MAX_STACK_COUNT]; | |
| unsigned short frames; | |
| SYMBOL_INFO* symbol; | |
| HANDLE process; | |
| process = GetCurrentProcess(); | |
| SymInitialize(process, NULL, TRUE); | |
| frames = CaptureStackBackTrace(0, 100, stack, NULL); | |
| symbol = (SYMBOL_INFO*)calloc(sizeof(SYMBOL_INFO) + 256 * sizeof(char), 1); | |
| symbol->MaxNameLen = 255; | |
| symbol->SizeOfStruct = sizeof(SYMBOL_INFO); | |
| printf("=========call stack==========\n"); | |
| for (int i = 1; i < frames; i++) | |
| { | |
| SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol); | |
| printf("%i: %s - 0x%0llX\n", frames - i - 1, symbol->Name, symbol->Address); | |
| } | |
| printf("=============================\n"); | |
| free(symbol); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment