Ref T401, crash handling in application

* function to simulate crash
* init crash info so file is written + add arguments for handler
* enable / disable uploads
This commit is contained in:
Klaus Basan
2019-02-20 20:24:57 +01:00
committed by Mat Sutcliffe
parent df3ac2b02d
commit 16dd6d7451
2 changed files with 92 additions and 17 deletions

View File

@@ -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<std::string> 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<CrashpadClient>();
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 <signal.h>
#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)

View File

@@ -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<crashpad::CrashReportDatabase> m_crashReportDatabase;
BlackMisc::CSettingReadOnly<Application::TCrashDumpSettings> 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