From 16dd6d7451e1578892d8bfed9415943f122b51a9 Mon Sep 17 00:00:00 2001 From: Klaus Basan Date: Wed, 20 Feb 2019 20:24:57 +0100 Subject: [PATCH] Ref T401, crash handling in application * function to simulate crash * init crash info so file is written + add arguments for handler * enable / disable uploads --- src/blackcore/application.cpp | 87 +++++++++++++++++++++++++++++------ src/blackcore/application.h | 22 ++++++++- 2 files changed, 92 insertions(+), 17 deletions(-) diff --git a/src/blackcore/application.cpp b/src/blackcore/application.cpp index 42738cf46..551ef67eb 100644 --- a/src/blackcore/application.cpp +++ b/src/blackcore/application.cpp @@ -360,14 +360,11 @@ namespace BlackCore // crashpad dump if (this->isSet(m_cmdTestCrashpad)) { + msgs.push_back(CLogMessage(this).info(u"About to simulate crash")); QTimer::singleShot(10 * 1000, [ = ] { if (!sApp || sApp->isShuttingDown()) { return; } -#ifdef BLACK_USE_CRASHPAD - CRASHPAD_SIMULATE_CRASH(); -#else - CLogMessage(this).warning(u"This compiler or platform does not support crashpad. Cannot simulate crash dump!"); -#endif + this->simulateCrash(); }); } @@ -1667,17 +1664,24 @@ namespace BlackCore annotations["format"] = "minidump"; annotations["version"] = CBuildConfig::getVersionString().toStdString(); - QString logFilePath = m_fileLogger->getLogFilePath(); - QString logFileName = m_fileLogger->getLogFileName(); - QString logAttachment = QString("--attachment=%1=%2").arg(logFileName, logFilePath); + // add our logfile + const QString logFilePath = m_fileLogger->getLogFilePath(); // file and path + const QString logFileName = m_fileLogger->getLogFileName(); + const QString logAttachment = QStringLiteral("--attachment=attachment_%1=%2").arg(logFileName, logFilePath); std::vector arguments; arguments.push_back(logAttachment.toStdString()); + // and the simplified crash info if any + const QString crashInfoFileName("swiftcrashinfo.txt"); + const QString crashInfoFilePath(CFileUtils::appendFilePaths(CFileUtils::stripFileFromPath(logFilePath), crashInfoFileName)); + m_crashAndLogInfo.setLogPathAndFileName(crashInfoFilePath); + const QString crashAttachment = QStringLiteral("--attachment=attachment_%1=%2").arg(crashInfoFileName, crashInfoFilePath); + arguments.push_back(crashAttachment.toStdString()); + QDir().mkpath(database); m_crashReportDatabase = CrashReportDatabase::Initialize(qstringToFilePath(database)); - crashpad::Settings *settings = m_crashReportDatabase->GetSettings(); - settings->SetUploadsEnabled(CBuildConfig::isReleaseBuild() && m_crashDumpSettings.getThreadLocal().isEnabled()); + this->onCrashDumpUploadEnabledChanged(); // settings for crashpad uploads m_crashpadClient = std::make_unique(); m_crashpadClient->StartHandler(qstringToFilePath(handler), qstringToFilePath(database), @@ -1686,6 +1690,8 @@ namespace BlackCore annotations, arguments, false, true); + + this->crashAndLogAppendInfo(u"Init crash info at " % QDateTime::currentDateTimeUtc().toString()); return CStatusMessage(this).info(u"Using crash handler"); #else return CStatusMessage(this).info(u"Not using crash handler"); @@ -1694,36 +1700,87 @@ namespace BlackCore void CApplication::onCrashDumpUploadEnabledChanged() { -#ifdef BLACK_USE_CRASHPAD - if (!m_crashReportDatabase) { return; } - auto settings = m_crashReportDatabase->GetSettings(); - settings->SetUploadsEnabled(CBuildConfig::isReleaseBuild() && m_crashDumpSettings.getThreadLocal().isEnabled()); -#endif + const bool enabled = CBuildConfig::isReleaseBuild() && m_crashDumpSettings.getThreadLocal().isEnabled(); + this->enableCrashDumpUpload(enabled); + } + + void CApplication::triggerCrashInfoWrite() + { + m_crashAndLogInfo.triggerWritingFile(); } void CApplication::setCrashInfo(const CCrashInfo &info) { m_crashAndLogInfo = info; + m_dsCrashAndLogInfo.inputSignal(); } void CApplication::crashAndLogInfoUserName(const QString &name) { m_crashAndLogInfo.setUserName(name); + m_dsCrashAndLogInfo.inputSignal(); } void CApplication::crashAndLogInfoSimulator(const QString &simulator) { m_crashAndLogInfo.setSimulatorString(simulator); + m_dsCrashAndLogInfo.inputSignal(); } void CApplication::crashAndLogInfoFlightNetwork(const QString &flightNetwork) { m_crashAndLogInfo.setFlightNetworkString(flightNetwork); + m_dsCrashAndLogInfo.inputSignal(); } void CApplication::crashAndLogAppendInfo(const QString &info) { m_crashAndLogInfo.appendInfo(info); + m_dsCrashAndLogInfo.inputSignal(); + } + + void CApplication::simulateCrash() + { +#ifdef BLACK_USE_CRASHPAD + CRASHPAD_SIMULATE_CRASH(); + // real crash + // raise(SIGSEGV); #include +#else + CLogMessage(this).warning(u"This compiler or platform does not support crashpad. Cannot simulate crash dump!"); +#endif + } + + void CApplication::enableCrashDumpUpload(bool enable) + { +#ifdef BLACK_USE_CRASHPAD + if (!m_crashReportDatabase) { return; } + crashpad::Settings *settings = m_crashReportDatabase->GetSettings(); + settings->SetUploadsEnabled(enable); +#else + Q_UNUSED(enable); +#endif + } + + bool CApplication::isCrashDumpUploadEnabled() const + { +#ifdef BLACK_USE_CRASHPAD + if (!m_crashReportDatabase) { return false; } + crashpad::Settings *settings = m_crashReportDatabase->GetSettings(); + bool enabled = false; + bool ok = settings->GetUploadsEnabled(&enabled); + return ok && enabled; +#else + return false; +#endif + } + + bool CApplication::isSupportingCrashpad() const + { +#ifdef BLACK_USE_CRASHPAD + return true; +#else + return false; +#endif } void CApplication::httpRequestImplInQAMThread(const QNetworkRequest &request, int logId, const CallbackSlot &callback, const ProgressSlot &progress, int maxRedirects, NetworkRequestOrPostFunction requestOrPostMethod) diff --git a/src/blackcore/application.h b/src/blackcore/application.h index 2455ba397..0005dc271 100644 --- a/src/blackcore/application.h +++ b/src/blackcore/application.h @@ -24,6 +24,7 @@ #include "blackmisc/network/networkutils.h" #include "blackmisc/identifiable.h" #include "blackmisc/slot.h" +#include "blackmisc/digestsignal.h" #include "blackmisc/applicationinfolist.h" #include "blackmisc/statusmessagelist.h" #include "blackmisc/crashinfo.h" @@ -300,7 +301,7 @@ namespace BlackCore virtual QString cmdLineArgumentsAsString(bool withExecutable = true); //! @} - // ----------------------- CrashPad info --------------------------------- + // ----------------------- Crash info --------------------------------- //! Extra annotation for crash to easier identify annotation void setCrashInfo(const BlackMisc::CCrashInfo &info); @@ -320,6 +321,20 @@ namespace BlackCore //! Get the crash info const BlackMisc::CCrashInfo &getCrashInfo() const { return m_crashAndLogInfo; } + //! Simulate a crash + //! \private only for testing purposes + void simulateCrash(); + + //! Enable crash upload + //! \remark only change for testing + void enableCrashDumpUpload(bool enable); + + //! Is crash dump upload enabled + bool isCrashDumpUploadEnabled() const; + + //! Has crashpad support? + bool isSupportingCrashpad() const; + // ----------------------- Input ---------------------------------------- //! The input manager, if available @@ -720,8 +735,11 @@ namespace BlackCore std::unique_ptr m_crashReportDatabase; BlackMisc::CSettingReadOnly m_crashDumpSettings { this, &CApplication::onCrashDumpUploadEnabledChanged }; #endif - BlackMisc::CCrashInfo m_crashAndLogInfo; //!< info representing details + // crash info + void triggerCrashInfoWrite(); + BlackMisc::CCrashInfo m_crashAndLogInfo; //!< info representing details + BlackMisc::CDigestSignal m_dsCrashAndLogInfo { this, &CApplication::triggerCrashInfoWrite, 10000, 5 }; }; } // namespace