|
10 | 10 |
|
11 | 11 | #include "freer.h"
|
12 | 12 |
|
| 13 | +#include <iomanip> |
13 | 14 | #include <memory>
|
14 |
| -#include <string> |
15 | 15 | #include <sstream>
|
| 16 | +#include <string> |
16 | 17 |
|
17 | 18 | #include <iostream>
|
18 | 19 |
|
| 20 | +#ifdef _WIN32 |
| 21 | +// the ordering of includes is required |
| 22 | +// clang-format off |
| 23 | +#include <windows.h> |
| 24 | +#include <dbghelp.h> |
| 25 | +// clang-format on |
| 26 | +#endif |
| 27 | + |
19 | 28 | bool cbmc_invariants_should_throw = false;
|
20 | 29 |
|
21 | 30 | // Backtraces compiler and C library specific
|
@@ -93,6 +102,35 @@ void print_backtrace(
|
93 | 102 | out << '\n' << std::flush;
|
94 | 103 | }
|
95 | 104 |
|
| 105 | +#elif defined(_WIN32) |
| 106 | + |
| 107 | + void *stack[50]; |
| 108 | + HANDLE process = GetCurrentProcess(); |
| 109 | + |
| 110 | + SymInitialize(process, NULL, TRUE); |
| 111 | + |
| 112 | + auto number_of_frames = |
| 113 | + CaptureStackBackTrace(0, sizeof(stack) / sizeof(void *), stack, NULL); |
| 114 | + |
| 115 | + // Read |
| 116 | + // https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/ns-dbghelp-symbol_info |
| 117 | + // for the rationale behind the size of 'symbol' |
| 118 | + const auto max_name_len = 255; |
| 119 | + auto symbol = static_cast<SYMBOL_INFO *>( |
| 120 | + calloc(sizeof SYMBOL_INFO + (max_name_len - 1) * sizeof(TCHAR), 1)); |
| 121 | + symbol->MaxNameLen = max_name_len; |
| 122 | + symbol->SizeOfStruct = sizeof SYMBOL_INFO; |
| 123 | + |
| 124 | + for(std::size_t i = 0; i < number_of_frames; i++) |
| 125 | + { |
| 126 | + SymFromAddr(process, (DWORD64)(stack[i]), 0, symbol); |
| 127 | + out << std::setw(3) << i; |
| 128 | + out << " 0x" << std::hex << std::setw(8) << symbol->Address; |
| 129 | + out << ' ' << symbol->Name; |
| 130 | + out << '\n' << std::flush; |
| 131 | + } |
| 132 | + |
| 133 | + free(symbol); |
96 | 134 | #else
|
97 | 135 | out << "Backtraces not supported\n" << std::flush;
|
98 | 136 | #endif
|
|
0 commit comments