testlib: Unite Windows and Unix signal handler code
Change-Id: I7b9d480008167c071bf925d655eb97ef437bc206 Reviewed-by: Simon Hausmann <simon.hausmann@qt.io>
This commit is contained in:
parent
d005d743de
commit
0c8ff269dc
@ -1533,121 +1533,6 @@ void TestMethods::invokeTests(QObject *testObject) const
|
|||||||
QTestResult::setCurrentTestFunction(nullptr);
|
QTestResult::setCurrentTestFunction(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
|
||||||
|
|
||||||
class FatalSignalHandler
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
FatalSignalHandler()
|
|
||||||
{
|
|
||||||
sigemptyset(&handledSignals);
|
|
||||||
|
|
||||||
const int fatalSignals[] = {
|
|
||||||
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGBUS, SIGFPE, SIGSEGV, SIGPIPE, SIGTERM, 0 };
|
|
||||||
|
|
||||||
struct sigaction act;
|
|
||||||
memset(&act, 0, sizeof(act));
|
|
||||||
act.sa_handler = FatalSignalHandler::signal;
|
|
||||||
|
|
||||||
// Remove the handler after it is invoked.
|
|
||||||
#if !defined(Q_OS_INTEGRITY)
|
|
||||||
act.sa_flags = SA_RESETHAND;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// tvOS/watchOS both define SA_ONSTACK (in sys/signal.h) but mark sigaltstack() as
|
|
||||||
// unavailable (__WATCHOS_PROHIBITED __TVOS_PROHIBITED in signal.h)
|
|
||||||
#if defined(SA_ONSTACK) && !defined(Q_OS_TVOS) && !defined(Q_OS_WATCHOS)
|
|
||||||
// Let the signal handlers use an alternate stack
|
|
||||||
// This is necessary if SIGSEGV is to catch a stack overflow
|
|
||||||
# if defined(Q_CC_GNU) && defined(Q_OF_ELF)
|
|
||||||
// Put the alternate stack in the .lbss (large BSS) section so that it doesn't
|
|
||||||
// interfere with normal .bss symbols
|
|
||||||
__attribute__((section(".lbss.altstack"), aligned(4096)))
|
|
||||||
# endif
|
|
||||||
static char alternate_stack[16 * 1024];
|
|
||||||
stack_t stack;
|
|
||||||
stack.ss_flags = 0;
|
|
||||||
stack.ss_size = sizeof alternate_stack;
|
|
||||||
stack.ss_sp = alternate_stack;
|
|
||||||
sigaltstack(&stack, nullptr);
|
|
||||||
act.sa_flags |= SA_ONSTACK;
|
|
||||||
#endif
|
|
||||||
|
|
||||||
// Block all fatal signals in our signal handler so we don't try to close
|
|
||||||
// the testlog twice.
|
|
||||||
sigemptyset(&act.sa_mask);
|
|
||||||
for (int i = 0; fatalSignals[i]; ++i)
|
|
||||||
sigaddset(&act.sa_mask, fatalSignals[i]);
|
|
||||||
|
|
||||||
struct sigaction oldact;
|
|
||||||
|
|
||||||
for (int i = 0; fatalSignals[i]; ++i) {
|
|
||||||
sigaction(fatalSignals[i], &act, &oldact);
|
|
||||||
if (
|
|
||||||
#ifdef SA_SIGINFO
|
|
||||||
oldact.sa_flags & SA_SIGINFO ||
|
|
||||||
#endif
|
|
||||||
oldact.sa_handler != SIG_DFL) {
|
|
||||||
sigaction(fatalSignals[i], &oldact, nullptr);
|
|
||||||
} else
|
|
||||||
{
|
|
||||||
sigaddset(&handledSignals, fatalSignals[i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
~FatalSignalHandler()
|
|
||||||
{
|
|
||||||
// Unregister any of our remaining signal handlers
|
|
||||||
struct sigaction act;
|
|
||||||
memset(&act, 0, sizeof(act));
|
|
||||||
act.sa_handler = SIG_DFL;
|
|
||||||
|
|
||||||
struct sigaction oldact;
|
|
||||||
|
|
||||||
for (int i = 1; i < 32; ++i) {
|
|
||||||
if (!sigismember(&handledSignals, i))
|
|
||||||
continue;
|
|
||||||
sigaction(i, &act, &oldact);
|
|
||||||
|
|
||||||
// If someone overwrote it in the mean time, put it back
|
|
||||||
if (oldact.sa_handler != FatalSignalHandler::signal)
|
|
||||||
sigaction(i, &oldact, nullptr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
|
||||||
static void signal(int signum)
|
|
||||||
{
|
|
||||||
const int msecsFunctionTime = qRound(QTestLog::msecsFunctionTime());
|
|
||||||
const int msecsTotalTime = qRound(QTestLog::msecsTotalTime());
|
|
||||||
if (signum != SIGINT) {
|
|
||||||
stackTrace();
|
|
||||||
if (qEnvironmentVariableIsSet("QTEST_PAUSE_ON_CRASH")) {
|
|
||||||
fprintf(stderr, "Pausing process %d for debugging\n", getpid());
|
|
||||||
raise(SIGSTOP);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
qFatal("Received signal %d\n"
|
|
||||||
" Function time: %dms Total time: %dms",
|
|
||||||
signum, msecsFunctionTime, msecsTotalTime);
|
|
||||||
#if defined(Q_OS_INTEGRITY)
|
|
||||||
{
|
|
||||||
struct sigaction act;
|
|
||||||
memset(&act, 0, sizeof(struct sigaction));
|
|
||||||
act.sa_handler = SIG_DFL;
|
|
||||||
sigaction(signum, &act, NULL);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
sigset_t handledSignals;
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
||||||
|
|
||||||
} // namespace
|
|
||||||
|
|
||||||
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
||||||
|
|
||||||
// Helper class for resolving symbol names by dynamically loading "dbghelp.dll".
|
// Helper class for resolving symbol names by dynamically loading "dbghelp.dll".
|
||||||
@ -1746,49 +1631,177 @@ DebugSymbolResolver::Symbol DebugSymbolResolver::resolveSymbol(DWORD64 address)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static LONG WINAPI windowsFaultHandler(struct _EXCEPTION_POINTERS *exInfo)
|
#endif // Q_OS_WIN && !Q_OS_WINRT
|
||||||
{
|
|
||||||
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;
|
|
||||||
printf("A crash occurred in %s.\n"
|
|
||||||
"Function time: %dms Total time: %dms\n\n"
|
|
||||||
"Exception address: 0x%p\n"
|
|
||||||
"Exception code : 0x%lx\n",
|
|
||||||
appName, msecsFunctionTime, msecsTotalTime,
|
|
||||||
exceptionAddress, exInfo->ExceptionRecord->ExceptionCode);
|
|
||||||
|
|
||||||
DebugSymbolResolver resolver(GetCurrentProcess());
|
class FatalSignalHandler
|
||||||
if (resolver.isValid()) {
|
{
|
||||||
DebugSymbolResolver::Symbol exceptionSymbol = resolver.resolveSymbol(DWORD64(exceptionAddress));
|
public:
|
||||||
if (exceptionSymbol.name) {
|
FatalSignalHandler()
|
||||||
printf("Nearby symbol : %s\n", exceptionSymbol.name);
|
{
|
||||||
delete [] exceptionSymbol.name;
|
#if defined(Q_OS_WIN)
|
||||||
}
|
# if !defined(Q_CC_MINGW)
|
||||||
void *stack[maxStackFrames];
|
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
|
||||||
fputs("\nStack:\n", stdout);
|
# endif
|
||||||
const unsigned frameCount = CaptureStackBackTrace(0, DWORD(maxStackFrames), stack, NULL);
|
# if !defined(Q_OS_WINRT)
|
||||||
for (unsigned f = 0; f < frameCount; ++f) {
|
SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX);
|
||||||
DebugSymbolResolver::Symbol symbol = resolver.resolveSymbol(DWORD64(stack[f]));
|
SetUnhandledExceptionFilter(windowsFaultHandler);
|
||||||
if (symbol.name) {
|
# endif
|
||||||
printf("#%3u: %s() - 0x%p\n", f + 1, symbol.name, (const void *)symbol.address);
|
#elif defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
||||||
delete [] symbol.name;
|
sigemptyset(&handledSignals);
|
||||||
} else {
|
|
||||||
printf("#%3u: Unable to obtain symbol\n", f + 1);
|
const int fatalSignals[] = {
|
||||||
|
SIGHUP, SIGINT, SIGQUIT, SIGILL, SIGBUS, SIGFPE, SIGSEGV, SIGPIPE, SIGTERM, 0 };
|
||||||
|
|
||||||
|
struct sigaction act;
|
||||||
|
memset(&act, 0, sizeof(act));
|
||||||
|
act.sa_handler = FatalSignalHandler::signal;
|
||||||
|
|
||||||
|
// Remove the handler after it is invoked.
|
||||||
|
# if !defined(Q_OS_INTEGRITY)
|
||||||
|
act.sa_flags = SA_RESETHAND;
|
||||||
|
# endif
|
||||||
|
|
||||||
|
// tvOS/watchOS both define SA_ONSTACK (in sys/signal.h) but mark sigaltstack() as
|
||||||
|
// unavailable (__WATCHOS_PROHIBITED __TVOS_PROHIBITED in signal.h)
|
||||||
|
# if defined(SA_ONSTACK) && !defined(Q_OS_TVOS) && !defined(Q_OS_WATCHOS)
|
||||||
|
// Let the signal handlers use an alternate stack
|
||||||
|
// This is necessary if SIGSEGV is to catch a stack overflow
|
||||||
|
# if defined(Q_CC_GNU) && defined(Q_OF_ELF)
|
||||||
|
// Put the alternate stack in the .lbss (large BSS) section so that it doesn't
|
||||||
|
// interfere with normal .bss symbols
|
||||||
|
__attribute__((section(".lbss.altstack"), aligned(4096)))
|
||||||
|
# endif
|
||||||
|
static char alternate_stack[16 * 1024];
|
||||||
|
stack_t stack;
|
||||||
|
stack.ss_flags = 0;
|
||||||
|
stack.ss_size = sizeof alternate_stack;
|
||||||
|
stack.ss_sp = alternate_stack;
|
||||||
|
sigaltstack(&stack, nullptr);
|
||||||
|
act.sa_flags |= SA_ONSTACK;
|
||||||
|
# endif
|
||||||
|
|
||||||
|
// Block all fatal signals in our signal handler so we don't try to close
|
||||||
|
// the testlog twice.
|
||||||
|
sigemptyset(&act.sa_mask);
|
||||||
|
for (int i = 0; fatalSignals[i]; ++i)
|
||||||
|
sigaddset(&act.sa_mask, fatalSignals[i]);
|
||||||
|
|
||||||
|
struct sigaction oldact;
|
||||||
|
|
||||||
|
for (int i = 0; fatalSignals[i]; ++i) {
|
||||||
|
sigaction(fatalSignals[i], &act, &oldact);
|
||||||
|
if (
|
||||||
|
# ifdef SA_SIGINFO
|
||||||
|
oldact.sa_flags & SA_SIGINFO ||
|
||||||
|
# endif
|
||||||
|
oldact.sa_handler != SIG_DFL) {
|
||||||
|
sigaction(fatalSignals[i], &oldact, nullptr);
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
sigaddset(&handledSignals, fatalSignals[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
||||||
}
|
}
|
||||||
|
|
||||||
fputc('\n', stdout);
|
~FatalSignalHandler()
|
||||||
fflush(stdout);
|
{
|
||||||
|
#if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
||||||
|
// Unregister any of our remaining signal handlers
|
||||||
|
struct sigaction act;
|
||||||
|
memset(&act, 0, sizeof(act));
|
||||||
|
act.sa_handler = SIG_DFL;
|
||||||
|
|
||||||
return EXCEPTION_EXECUTE_HANDLER;
|
struct sigaction oldact;
|
||||||
}
|
|
||||||
#endif // Q_OS_WIN) && !Q_OS_WINRT
|
for (int i = 1; i < 32; ++i) {
|
||||||
|
if (!sigismember(&handledSignals, i))
|
||||||
|
continue;
|
||||||
|
sigaction(i, &act, &oldact);
|
||||||
|
|
||||||
|
// If someone overwrote it in the mean time, put it back
|
||||||
|
if (oldact.sa_handler != FatalSignalHandler::signal)
|
||||||
|
sigaction(i, &oldact, nullptr);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
#if defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
||||||
|
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;
|
||||||
|
printf("A crash occurred in %s.\n"
|
||||||
|
"Function time: %dms Total time: %dms\n\n"
|
||||||
|
"Exception address: 0x%p\n"
|
||||||
|
"Exception code : 0x%lx\n",
|
||||||
|
appName, msecsFunctionTime, msecsTotalTime,
|
||||||
|
exceptionAddress, exInfo->ExceptionRecord->ExceptionCode);
|
||||||
|
|
||||||
|
DebugSymbolResolver resolver(GetCurrentProcess());
|
||||||
|
if (resolver.isValid()) {
|
||||||
|
DebugSymbolResolver::Symbol exceptionSymbol = resolver.resolveSymbol(DWORD64(exceptionAddress));
|
||||||
|
if (exceptionSymbol.name) {
|
||||||
|
printf("Nearby symbol : %s\n", exceptionSymbol.name);
|
||||||
|
delete [] exceptionSymbol.name;
|
||||||
|
}
|
||||||
|
void *stack[maxStackFrames];
|
||||||
|
fputs("\nStack:\n", stdout);
|
||||||
|
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) {
|
||||||
|
printf("#%3u: %s() - 0x%p\n", f + 1, symbol.name, (const void *)symbol.address);
|
||||||
|
delete [] symbol.name;
|
||||||
|
} else {
|
||||||
|
printf("#%3u: Unable to obtain symbol\n", f + 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fputc('\n', stdout);
|
||||||
|
fflush(stdout);
|
||||||
|
|
||||||
|
return EXCEPTION_EXECUTE_HANDLER;
|
||||||
|
}
|
||||||
|
#endif // defined(Q_OS_WIN) && !defined(Q_OS_WINRT)
|
||||||
|
|
||||||
|
#if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
||||||
|
static void signal(int signum)
|
||||||
|
{
|
||||||
|
const int msecsFunctionTime = qRound(QTestLog::msecsFunctionTime());
|
||||||
|
const int msecsTotalTime = qRound(QTestLog::msecsTotalTime());
|
||||||
|
if (signum != SIGINT) {
|
||||||
|
stackTrace();
|
||||||
|
if (qEnvironmentVariableIsSet("QTEST_PAUSE_ON_CRASH")) {
|
||||||
|
fprintf(stderr, "Pausing process %d for debugging\n", getpid());
|
||||||
|
raise(SIGSTOP);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
qFatal("Received signal %d\n"
|
||||||
|
" Function time: %dms Total time: %dms",
|
||||||
|
signum, msecsFunctionTime, msecsTotalTime);
|
||||||
|
# if defined(Q_OS_INTEGRITY)
|
||||||
|
{
|
||||||
|
struct sigaction act;
|
||||||
|
memset(&act, 0, sizeof(struct sigaction));
|
||||||
|
act.sa_handler = SIG_DFL;
|
||||||
|
sigaction(signum, &act, NULL);
|
||||||
|
}
|
||||||
|
# endif
|
||||||
|
}
|
||||||
|
|
||||||
|
sigset_t handledSignals;
|
||||||
|
#endif // defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
static void initEnvironment()
|
static void initEnvironment()
|
||||||
{
|
{
|
||||||
@ -1896,18 +1909,6 @@ int QTest::qRun()
|
|||||||
try {
|
try {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if defined(Q_OS_WIN)
|
|
||||||
if (!noCrashHandler) {
|
|
||||||
# ifndef Q_CC_MINGW
|
|
||||||
_CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
|
|
||||||
# endif
|
|
||||||
# ifndef Q_OS_WINRT
|
|
||||||
SetErrorMode(SetErrorMode(0) | SEM_NOGPFAULTERRORBOX);
|
|
||||||
SetUnhandledExceptionFilter(windowsFaultHandler);
|
|
||||||
# endif
|
|
||||||
} // !noCrashHandler
|
|
||||||
#endif // Q_OS_WIN
|
|
||||||
|
|
||||||
#if QT_CONFIG(valgrind)
|
#if QT_CONFIG(valgrind)
|
||||||
if (QBenchmarkGlobalData::current->mode() == QBenchmarkGlobalData::CallgrindParentProcess) {
|
if (QBenchmarkGlobalData::current->mode() == QBenchmarkGlobalData::CallgrindParentProcess) {
|
||||||
if (Q_UNLIKELY(!qApp))
|
if (Q_UNLIKELY(!qApp))
|
||||||
@ -1922,11 +1923,10 @@ int QTest::qRun()
|
|||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
#if defined(Q_OS_UNIX) && !defined(Q_OS_WASM)
|
|
||||||
QScopedPointer<FatalSignalHandler> handler;
|
QScopedPointer<FatalSignalHandler> handler;
|
||||||
if (!noCrashHandler)
|
if (!noCrashHandler)
|
||||||
handler.reset(new FatalSignalHandler);
|
handler.reset(new FatalSignalHandler);
|
||||||
#endif
|
|
||||||
TestMethods::MetaMethods commandLineMethods;
|
TestMethods::MetaMethods commandLineMethods;
|
||||||
for (const QString &tf : qAsConst(QTest::testFunctions)) {
|
for (const QString &tf : qAsConst(QTest::testFunctions)) {
|
||||||
const QByteArray tfB = tf.toLatin1();
|
const QByteArray tfB = tf.toLatin1();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user