diff options
author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 00:22:50 -0400 |
---|---|---|
committer | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2015-06-20 10:56:02 -0400 |
commit | 9c2ed42daa8fbbef4a919c21ec564e2db55e8d60 (patch) | |
tree | 3814f79c10d7b490948d8cb7b112ac1dd41ceff1 /src/mongo/util/signal_handlers_synchronous.cpp | |
parent | 01965cf52bce6976637ecb8f4a622aeb05ab256a (diff) | |
download | mongo-9c2ed42daa8fbbef4a919c21ec564e2db55e8d60.tar.gz |
SERVER-18579: Clang-Format - reformat code, no comment reflow
Diffstat (limited to 'src/mongo/util/signal_handlers_synchronous.cpp')
-rw-r--r-- | src/mongo/util/signal_handlers_synchronous.cpp | 357 |
1 files changed, 180 insertions, 177 deletions
diff --git a/src/mongo/util/signal_handlers_synchronous.cpp b/src/mongo/util/signal_handlers_synchronous.cpp index c49e35395f6..d2fb677a0f3 100644 --- a/src/mongo/util/signal_handlers_synchronous.cpp +++ b/src/mongo/util/signal_handlers_synchronous.cpp @@ -61,232 +61,235 @@ namespace mongo { namespace { #if defined(_WIN32) - const char* strsignal(int signalNum) { - // should only see SIGABRT on windows - switch (signalNum) { - case SIGABRT: return "SIGABRT"; - default: return "UNKNOWN"; - } +const char* strsignal(int signalNum) { + // should only see SIGABRT on windows + switch (signalNum) { + case SIGABRT: + return "SIGABRT"; + default: + return "UNKNOWN"; } +} #endif - // This should only be used with MallocFreeOSteam - class MallocFreeStreambuf : public std::streambuf { - MONGO_DISALLOW_COPYING(MallocFreeStreambuf); - public: - MallocFreeStreambuf() { - setp(_buffer, _buffer + maxLogLineSize); - } +// This should only be used with MallocFreeOSteam +class MallocFreeStreambuf : public std::streambuf { + MONGO_DISALLOW_COPYING(MallocFreeStreambuf); - StringData str() const { return StringData(pbase(), pptr() - pbase()); } - void rewind() { setp(pbase(), epptr()); } - - private: - static const size_t maxLogLineSize = 16*1000; - char _buffer[maxLogLineSize]; - }; - - class MallocFreeOStream : public std::ostream { - MONGO_DISALLOW_COPYING(MallocFreeOStream); - public: - MallocFreeOStream() : std::ostream(&_buf) {} - - StringData str() const { return _buf.str(); } - void rewind() { _buf.rewind(); } - private: - MallocFreeStreambuf _buf; - }; - - MallocFreeOStream mallocFreeOStream; - - // This guards mallocFreeOStream. While locking a pthread_mutex isn't guaranteed to be - // signal-safe, this file does it anyway. The assumption is that the main safety risk to locking - // a mutex is that you could deadlock with yourself. That risk is protected against by only - // locking the mutex in fatal functions that log then exit. There is a remaining risk that one - // of these functions recurses (possible if logging segfaults while handing a segfault). This is - // currently acceptable because if things are that broken, there is little we can do about it. - // - // If in the future, we decide to be more strict about posix signal safety, we could switch to - // an atomic test-and-set loop, possibly with a mechanism for detecting signals raised while - // handling other signals. - stdx::mutex streamMutex; - - // must hold streamMutex to call - void writeMallocFreeStreamToLog() { - logger::globalLogDomain()->append( - logger::MessageEventEphemeral(Date_t::now(), - logger::LogSeverity::Severe(), - getThreadName(), - mallocFreeOStream.str())); - mallocFreeOStream.rewind(); +public: + MallocFreeStreambuf() { + setp(_buffer, _buffer + maxLogLineSize); } - // must hold streamMutex to call - void printSignalAndBacktrace(int signalNum) { - mallocFreeOStream << "Got signal: " << signalNum << " (" << strsignal(signalNum) << ").\n"; - printStackTrace(mallocFreeOStream); - writeMallocFreeStreamToLog(); + StringData str() const { + return StringData(pbase(), pptr() - pbase()); + } + void rewind() { + setp(pbase(), epptr()); } - // this will be called in certain c++ error cases, for example if there are two active - // exceptions - void myTerminate() { - stdx::lock_guard<stdx::mutex> lk(streamMutex); +private: + static const size_t maxLogLineSize = 16 * 1000; + char _buffer[maxLogLineSize]; +}; - // In c++11 we can recover the current exception to print it. - if (std::exception_ptr eptr = std::current_exception()) { - mallocFreeOStream << "terminate() called. An exception is active;" - << " attempting to gather more information"; - writeMallocFreeStreamToLog(); +class MallocFreeOStream : public std::ostream { + MONGO_DISALLOW_COPYING(MallocFreeOStream); - const std::type_info* typeInfo = nullptr; +public: + MallocFreeOStream() : std::ostream(&_buf) {} + + StringData str() const { + return _buf.str(); + } + void rewind() { + _buf.rewind(); + } + +private: + MallocFreeStreambuf _buf; +}; + +MallocFreeOStream mallocFreeOStream; + +// This guards mallocFreeOStream. While locking a pthread_mutex isn't guaranteed to be +// signal-safe, this file does it anyway. The assumption is that the main safety risk to locking +// a mutex is that you could deadlock with yourself. That risk is protected against by only +// locking the mutex in fatal functions that log then exit. There is a remaining risk that one +// of these functions recurses (possible if logging segfaults while handing a segfault). This is +// currently acceptable because if things are that broken, there is little we can do about it. +// +// If in the future, we decide to be more strict about posix signal safety, we could switch to +// an atomic test-and-set loop, possibly with a mechanism for detecting signals raised while +// handling other signals. +stdx::mutex streamMutex; + +// must hold streamMutex to call +void writeMallocFreeStreamToLog() { + logger::globalLogDomain()->append(logger::MessageEventEphemeral( + Date_t::now(), logger::LogSeverity::Severe(), getThreadName(), mallocFreeOStream.str())); + mallocFreeOStream.rewind(); +} + +// must hold streamMutex to call +void printSignalAndBacktrace(int signalNum) { + mallocFreeOStream << "Got signal: " << signalNum << " (" << strsignal(signalNum) << ").\n"; + printStackTrace(mallocFreeOStream); + writeMallocFreeStreamToLog(); +} + +// this will be called in certain c++ error cases, for example if there are two active +// exceptions +void myTerminate() { + stdx::lock_guard<stdx::mutex> lk(streamMutex); + + // In c++11 we can recover the current exception to print it. + if (std::exception_ptr eptr = std::current_exception()) { + mallocFreeOStream << "terminate() called. An exception is active;" + << " attempting to gather more information"; + writeMallocFreeStreamToLog(); + + const std::type_info* typeInfo = nullptr; + try { try { - try { - std::rethrow_exception(eptr); - } - catch (const DBException& ex) { - typeInfo = &typeid(ex); - mallocFreeOStream << "DBException::toString(): " << ex.toString() << '\n'; - } - catch (const std::exception& ex) { - typeInfo = &typeid(ex); - mallocFreeOStream << "std::exception::what(): " << ex.what() << '\n'; - } - catch (const boost::exception& ex) { - typeInfo = &typeid(ex); - mallocFreeOStream << "boost::diagnostic_information(): " - << boost::diagnostic_information(ex) << '\n'; - } - catch (...) { - mallocFreeOStream << "A non-standard exception type was thrown\n"; - } - - if (typeInfo) { - const std::string name = demangleName(*typeInfo); - mallocFreeOStream << "Actual exception type: " << name << '\n'; - } + std::rethrow_exception(eptr); + } catch (const DBException& ex) { + typeInfo = &typeid(ex); + mallocFreeOStream << "DBException::toString(): " << ex.toString() << '\n'; + } catch (const std::exception& ex) { + typeInfo = &typeid(ex); + mallocFreeOStream << "std::exception::what(): " << ex.what() << '\n'; + } catch (const boost::exception& ex) { + typeInfo = &typeid(ex); + mallocFreeOStream << "boost::diagnostic_information(): " + << boost::diagnostic_information(ex) << '\n'; + } catch (...) { + mallocFreeOStream << "A non-standard exception type was thrown\n"; } - catch (...) { - mallocFreeOStream << "Exception while trying to print current exception.\n"; - if (typeInfo) { - // It is possible that we failed during demangling. At least try to print the - // mangled name. - mallocFreeOStream << "Actual exception type: " << typeInfo->name() << '\n'; - } + + if (typeInfo) { + const std::string name = demangleName(*typeInfo); + mallocFreeOStream << "Actual exception type: " << name << '\n'; + } + } catch (...) { + mallocFreeOStream << "Exception while trying to print current exception.\n"; + if (typeInfo) { + // It is possible that we failed during demangling. At least try to print the + // mangled name. + mallocFreeOStream << "Actual exception type: " << typeInfo->name() << '\n'; } } - else { - mallocFreeOStream << "terminate() called. No exception is active"; - } + } else { + mallocFreeOStream << "terminate() called. No exception is active"; + } - printStackTrace(mallocFreeOStream); - writeMallocFreeStreamToLog(); + printStackTrace(mallocFreeOStream); + writeMallocFreeStreamToLog(); #if defined(_WIN32) - doMinidump(); + doMinidump(); #endif - breakpoint(); - quickExit(EXIT_ABRUPT); - } + breakpoint(); + quickExit(EXIT_ABRUPT); +} - void abruptQuit(int signalNum) { - stdx::lock_guard<stdx::mutex> lk(streamMutex); - printSignalAndBacktrace(signalNum); +void abruptQuit(int signalNum) { + stdx::lock_guard<stdx::mutex> lk(streamMutex); + printSignalAndBacktrace(signalNum); - // Don't go through normal shutdown procedure. It may make things worse. - quickExit(EXIT_ABRUPT); - } + // Don't go through normal shutdown procedure. It may make things worse. + quickExit(EXIT_ABRUPT); +} #if defined(_WIN32) - void myInvalidParameterHandler( - const wchar_t* expression, - const wchar_t* function, - const wchar_t* file, - unsigned int line, - uintptr_t pReserved) { - severe() << "Invalid parameter detected in function " << toUtf8String(function) << - " File: " << toUtf8String(file) << " Line: " << line; - severe() << "Expression: " << toUtf8String(expression); +void myInvalidParameterHandler(const wchar_t* expression, + const wchar_t* function, + const wchar_t* file, + unsigned int line, + uintptr_t pReserved) { + severe() << "Invalid parameter detected in function " << toUtf8String(function) + << " File: " << toUtf8String(file) << " Line: " << line; + severe() << "Expression: " << toUtf8String(expression); - doMinidump(); + doMinidump(); - severe() << "immediate exit due to invalid parameter"; + severe() << "immediate exit due to invalid parameter"; - abruptQuit(SIGABRT); - } + abruptQuit(SIGABRT); +} - void myPureCallHandler() { - severe() << "Pure call handler invoked"; +void myPureCallHandler() { + severe() << "Pure call handler invoked"; - doMinidump(); + doMinidump(); - severe() << "immediate exit due to invalid pure call"; + severe() << "immediate exit due to invalid pure call"; - abruptQuit(SIGABRT); - } + abruptQuit(SIGABRT); +} #else - void abruptQuitWithAddrSignal( int signalNum, siginfo_t *siginfo, void * ) { - stdx::lock_guard<stdx::mutex> lk(streamMutex); +void abruptQuitWithAddrSignal(int signalNum, siginfo_t* siginfo, void*) { + stdx::lock_guard<stdx::mutex> lk(streamMutex); - const char* action = (signalNum == SIGSEGV || signalNum == SIGBUS) ? "access" : "operation"; - mallocFreeOStream << "Invalid " << action << " at address: " << siginfo->si_addr; + const char* action = (signalNum == SIGSEGV || signalNum == SIGBUS) ? "access" : "operation"; + mallocFreeOStream << "Invalid " << action << " at address: " << siginfo->si_addr; - // Writing out message to log separate from the stack trace so at least that much gets - // logged. This is important because we may get here by jumping to an invalid address which - // could cause unwinding the stack to break. - writeMallocFreeStreamToLog(); + // Writing out message to log separate from the stack trace so at least that much gets + // logged. This is important because we may get here by jumping to an invalid address which + // could cause unwinding the stack to break. + writeMallocFreeStreamToLog(); - printSignalAndBacktrace(signalNum); - breakpoint(); - quickExit(EXIT_ABRUPT); - } + printSignalAndBacktrace(signalNum); + breakpoint(); + quickExit(EXIT_ABRUPT); +} #endif } // namespace - void setupSynchronousSignalHandlers() { - std::set_terminate(myTerminate); - std::set_new_handler(reportOutOfMemoryErrorAndExit); +void setupSynchronousSignalHandlers() { + std::set_terminate(myTerminate); + std::set_new_handler(reportOutOfMemoryErrorAndExit); - // SIGABRT is the only signal we want handled by signal handlers on both windows and posix. - invariant(signal(SIGABRT, abruptQuit) != SIG_ERR); + // SIGABRT is the only signal we want handled by signal handlers on both windows and posix. + invariant(signal(SIGABRT, abruptQuit) != SIG_ERR); #if defined(_WIN32) - _set_purecall_handler(myPureCallHandler); - _set_invalid_parameter_handler(myInvalidParameterHandler); - setWindowsUnhandledExceptionFilter(); + _set_purecall_handler(myPureCallHandler); + _set_invalid_parameter_handler(myInvalidParameterHandler); + setWindowsUnhandledExceptionFilter(); #else - invariant(signal(SIGHUP, SIG_IGN ) != SIG_ERR); - invariant(signal(SIGUSR2, SIG_IGN ) != SIG_ERR); - invariant(signal(SIGPIPE, SIG_IGN) != SIG_ERR); + invariant(signal(SIGHUP, SIG_IGN) != SIG_ERR); + invariant(signal(SIGUSR2, SIG_IGN) != SIG_ERR); + invariant(signal(SIGPIPE, SIG_IGN) != SIG_ERR); - struct sigaction addrSignals; - memset(&addrSignals, 0, sizeof(struct sigaction)); - addrSignals.sa_sigaction = abruptQuitWithAddrSignal; - sigemptyset(&addrSignals.sa_mask); - addrSignals.sa_flags = SA_SIGINFO; + struct sigaction addrSignals; + memset(&addrSignals, 0, sizeof(struct sigaction)); + addrSignals.sa_sigaction = abruptQuitWithAddrSignal; + sigemptyset(&addrSignals.sa_mask); + addrSignals.sa_flags = SA_SIGINFO; - // ^\ is the stronger ^C. Log and quit hard without waiting for cleanup. - invariant(signal(SIGQUIT, abruptQuit) != SIG_ERR); + // ^\ is the stronger ^C. Log and quit hard without waiting for cleanup. + invariant(signal(SIGQUIT, abruptQuit) != SIG_ERR); - invariant(sigaction(SIGSEGV, &addrSignals, 0) == 0); - invariant(sigaction(SIGBUS, &addrSignals, 0) == 0); - invariant(sigaction(SIGILL, &addrSignals, 0) == 0); - invariant(sigaction(SIGFPE, &addrSignals, 0) == 0); + invariant(sigaction(SIGSEGV, &addrSignals, 0) == 0); + invariant(sigaction(SIGBUS, &addrSignals, 0) == 0); + invariant(sigaction(SIGILL, &addrSignals, 0) == 0); + invariant(sigaction(SIGFPE, &addrSignals, 0) == 0); - setupSIGTRAPforGDB(); + setupSIGTRAPforGDB(); #endif - } - - void reportOutOfMemoryErrorAndExit() { - stdx::lock_guard<stdx::mutex> lk(streamMutex); - printStackTrace(mallocFreeOStream << "out of memory.\n"); - writeMallocFreeStreamToLog(); - quickExit(EXIT_ABRUPT); - } +} + +void reportOutOfMemoryErrorAndExit() { + stdx::lock_guard<stdx::mutex> lk(streamMutex); + printStackTrace(mallocFreeOStream << "out of memory.\n"); + writeMallocFreeStreamToLog(); + quickExit(EXIT_ABRUPT); +} } // namespace mongo |