1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
// Copyright (C) 2024 The Qt Company Ltd.
// Copyright (C) 2024 Intel Corporation.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include <QtTest/private/qtestcrashhandler_p.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qstring.h>
#include <QtTest/qtestcase.h>
#include <QtTest/private/qtestlog_p.h>
#include <stdio.h>
#include <stdlib.h>
#if !defined(Q_CC_MINGW) || (defined(Q_CC_MINGW) && defined(__MINGW64_VERSION_MAJOR))
# include <crtdbg.h>
#endif
#include <qt_windows.h>
QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals;
namespace QTest {
namespace CrashHandler {
bool alreadyDebugging()
{
return IsDebuggerPresent();
}
void maybeDisableCoreDump()
{
}
static DebuggerProgram debugger = None;
void prepareStackTrace()
{
bool ok = false;
const int disableStackDump = qEnvironmentVariableIntValue("QTEST_DISABLE_STACK_DUMP", &ok);
if (ok && disableStackDump)
return;
// ### Implement finding a debugger on Windows
}
void printTestRunTime()
{
const int msecsFunctionTime = qRound(QTestLog::msecsFunctionTime());
const int msecsTotalTime = qRound(QTestLog::msecsTotalTime());
const char *const name = QTest::currentTestFunction();
// Windows doesn't have the concept of async-safety, so fprintf() is
// probably as good as WriteFile() and WriteConsole().
fprintf(stderr, "\n %s function time: %dms, total time: %dms\n",
name ? name : "[Non-test]", msecsFunctionTime, msecsTotalTime);
}
void generateStackTrace()
{
if (debugger == None || alreadyDebugging())
return;
// ### Implement starting a debugger on Windows
}
void blockUnixSignals()
{
// Windows does have C signals, but doesn't use them for the purposes we're
// talking about here
}
namespace {
// Helper class for resolving symbol names by dynamically loading "dbghelp.dll".
class DebugSymbolResolver
{
Q_DISABLE_COPY_MOVE(DebugSymbolResolver)
public:
struct Symbol
{
Symbol() : name(nullptr), address(0) {}
const char *name; // Must be freed by caller.
DWORD64 address;
};
explicit DebugSymbolResolver(HANDLE process);
~DebugSymbolResolver() { cleanup(); }
bool isValid() const { return m_symFromAddr; }
Symbol resolveSymbol(DWORD64 address) const;
private:
// typedefs from DbgHelp.h/.dll
struct DBGHELP_SYMBOL_INFO { // SYMBOL_INFO
ULONG SizeOfStruct;
ULONG TypeIndex; // Type Index of symbol
ULONG64 Reserved[2];
ULONG Index;
ULONG Size;
ULONG64 ModBase; // Base Address of module comtaining this symbol
ULONG Flags;
ULONG64 Value; // Value of symbol, ValuePresent should be 1
ULONG64 Address; // Address of symbol including base address of module
ULONG Register; // register holding value or pointer to value
ULONG Scope; // scope of the symbol
ULONG Tag; // pdb classification
ULONG NameLen; // Actual length of name
ULONG MaxNameLen;
CHAR Name[1]; // Name of symbol
};
typedef BOOL (__stdcall *SymInitializeType)(HANDLE, PCSTR, BOOL);
typedef BOOL (__stdcall *SymFromAddrType)(HANDLE, DWORD64, PDWORD64, DBGHELP_SYMBOL_INFO *);
void cleanup();
const HANDLE m_process;
HMODULE m_dbgHelpLib;
SymFromAddrType m_symFromAddr;
};
void DebugSymbolResolver::cleanup()
{
if (m_dbgHelpLib)
FreeLibrary(m_dbgHelpLib);
m_dbgHelpLib = 0;
m_symFromAddr = nullptr;
}
DebugSymbolResolver::DebugSymbolResolver(HANDLE process)
: m_process(process), m_dbgHelpLib(0), m_symFromAddr(nullptr)
{
bool success = false;
m_dbgHelpLib = LoadLibraryW(L"dbghelp.dll");
if (m_dbgHelpLib) {
SymInitializeType symInitialize = reinterpret_cast<SymInitializeType>(
reinterpret_cast<QFunctionPointer>(GetProcAddress(m_dbgHelpLib, "SymInitialize")));
m_symFromAddr = reinterpret_cast<SymFromAddrType>(
reinterpret_cast<QFunctionPointer>(GetProcAddress(m_dbgHelpLib, "SymFromAddr")));
success = symInitialize && m_symFromAddr && symInitialize(process, NULL, TRUE);
}
if (!success)
cleanup();
}
DebugSymbolResolver::Symbol DebugSymbolResolver::resolveSymbol(DWORD64 address) const
{
// reserve additional buffer where SymFromAddr() will store the name
struct NamedSymbolInfo : public DBGHELP_SYMBOL_INFO {
enum { symbolNameLength = 255 };
char name[symbolNameLength + 1];
};
Symbol result;
if (!isValid())
return result;
NamedSymbolInfo symbolBuffer;
memset(&symbolBuffer, 0, sizeof(NamedSymbolInfo));
symbolBuffer.MaxNameLen = NamedSymbolInfo::symbolNameLength;
symbolBuffer.SizeOfStruct = sizeof(DBGHELP_SYMBOL_INFO);
if (!m_symFromAddr(m_process, address, 0, &symbolBuffer))
return result;
result.name = qstrdup(symbolBuffer.Name);
result.address = symbolBuffer.Address;
return result;
}
} // unnamed namespace
static LONG WINAPI windowsFaultHandler(struct _EXCEPTION_POINTERS *exInfo);
WindowsFaultHandler::WindowsFaultHandler()
{
# if !defined(Q_CC_MINGW)
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
# endif
SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX);
SetUnhandledExceptionFilter(windowsFaultHandler);
}
static LONG WINAPI windowsFaultHandler(struct _EXCEPTION_POINTERS *exInfo)
{
enum { maxStackFrames = 100 };
char appName[MAX_PATH];
if (!GetModuleFileNameA(NULL, appName, MAX_PATH))
appName[0] = 0;
const int msecsFunctionTime = qRound(QTestLog::msecsFunctionTime());
const int msecsTotalTime = qRound(QTestLog::msecsTotalTime());
const void *exceptionAddress = exInfo->ExceptionRecord->ExceptionAddress;
fprintf(stderr, "A crash occurred in %s.\n", appName);
if (const char *name = QTest::currentTestFunction())
fprintf(stderr, "While testing %s\n", name);
fprintf(stderr, "Function time: %dms Total time: %dms\n\n"
"Exception address: 0x%p\n"
"Exception code : 0x%lx\n",
msecsFunctionTime, msecsTotalTime, exceptionAddress,
exInfo->ExceptionRecord->ExceptionCode);
DebugSymbolResolver resolver(GetCurrentProcess());
if (resolver.isValid()) {
DebugSymbolResolver::Symbol exceptionSymbol = resolver.resolveSymbol(DWORD64(exceptionAddress));
if (exceptionSymbol.name) {
fprintf(stderr, "Nearby symbol : %s\n", exceptionSymbol.name);
delete [] exceptionSymbol.name;
}
Q_DECL_UNINITIALIZED void *stack[maxStackFrames];
fputs("\nStack:\n", stderr);
const unsigned frameCount = CaptureStackBackTrace(0, DWORD(maxStackFrames), stack, NULL);
for (unsigned f = 0; f < frameCount; ++f) {
DebugSymbolResolver::Symbol symbol = resolver.resolveSymbol(DWORD64(stack[f]));
if (symbol.name) {
fprintf(stderr, "#%3u: %s() - 0x%p\n", f + 1, symbol.name, (const void *)symbol.address);
delete [] symbol.name;
} else {
fprintf(stderr, "#%3u: Unable to obtain symbol\n", f + 1);
}
}
}
fputc('\n', stderr);
return EXCEPTION_EXECUTE_HANDLER;
}
} // namespace CrashHandler
} // namespace QTest
QT_END_NAMESPACE
|