@@ -10,12 +10,21 @@ Author: Martin Brain, martin.brain@diffblue.com
1010
1111#include " freer.h"
1212
13+ #include < iomanip>
1314#include < memory>
14- #include < string>
1515#include < sstream>
16+ #include < string>
1617
1718#include < iostream>
1819
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+
1928bool cbmc_invariants_should_throw = false ;
2029
2130// Backtraces compiler and C library specific
@@ -93,6 +102,35 @@ void print_backtrace(
93102 out << ' \n ' << std::flush;
94103 }
95104
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);
96134#else
97135 out << " Backtraces not supported\n " << std::flush;
98136#endif
0 commit comments