Skip to content

Commit f891bc1

Browse files
author
Daniel Kroening
authored
Merge pull request #5370 from diffblue/windows_stack_dumps
add stack dumps for Windows
2 parents 6cfbec2 + a34e0f0 commit f891bc1

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

src/common

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ else ifeq ($(BUILD_ENV_),MSVC)
137137
CP_CFLAGS =
138138
CP_CXXFLAGS +=
139139
LINKLIB = lib /NOLOGO /OUT:$@ $^
140-
LINKBIN = $(CXX) $(LINKFLAGS) /Fe$@ /nologo $^ $(LIBS)
140+
LINKBIN = $(CXX) $(LINKFLAGS) /Fe$@ /Z7 /nologo $^ DbgHelp.lib $(LIBS)
141141
LINKNATIVE = $(HOSTCXX) /Fe$@ /nologo /EHsc $^
142142
ifeq ($(origin CC),default)
143143
CC = cl

src/util/invariant.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,21 @@ Author: Martin Brain, [email protected]
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+
1928
bool 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

Comments
 (0)