Skip to content

Commit

Permalink
core: Add a mechanism to print the stacktrace on crash using qDebug.
Browse files Browse the repository at this point in the history
This mechanism will only provide a stacktrace on platforms that provide
glibc, thus on Windows we will only have the application log without
any stacktrace. For now it should be enough. Research was done on
boost::stacktrace, breakpad also. In the future, Scopy can pe upgraded to
C++ 23 which will provide a mechanism for crash reporting.

Signed-off-by: AlexandraTrifan <[email protected]>
  • Loading branch information
AlexandraTrifan committed Oct 13, 2023
1 parent 890ae2c commit cfe1d2a
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ set_target_properties(
MACOSX_BUNDLE TRUE
MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_BINARY_DIR}/Info.plist
WIN32_EXECUTABLE TRUE
ENABLE_EXPORTS ON # equiv to -rdynamic
)

if(ENABLE_APPLICATION_BUNDLE OR ${CMAKE_SYSTEM_NAME} MATCHES "Windows")
Expand Down
19 changes: 19 additions & 0 deletions core/include/core/crashreport.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CRASHREPORT_H
#define CRASHREPORT_H

#include <QString>
#include "scopy-core_export.h"

namespace scopy
{
class SCOPY_CORE_EXPORT CrashReport
{
public:
static void initSignalHandler();
private:
static void signalHandler(int);
static QString tmpFilePath_;
};
}

#endif // CRASHREPORT_H
1 change: 1 addition & 0 deletions core/include/core/logging_categories.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#define QDEBUG_THREAD_STR "%{threadid}"
#define QDEBUG_PID_STR "%{pid}"
#define QDEBUG_APP_STR "%{appname}"
#define QCRITICAL_BACKTRACE_STR "%{if-critical}%{backtrace depth=100 separator=\n}%{endif}"

#ifndef QT_NO_DEBUG_OUTPUT
Q_DECLARE_LOGGING_CATEGORY(CAT_TOOL_LAUNCHER)
Expand Down
59 changes: 59 additions & 0 deletions core/src/crashreport.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "crashreport.h"
#include <fstream>
#include <signal.h>
#include <stdlib.h>
#include <QDebug>
#include <QDateTime>
#include <QDir>
#include <QFileInfo>
#include <common/scopyconfig.h>
#include "logging_categories.h"

using namespace scopy;

QString CrashReport::tmpFilePath_;

void CrashReport::signalHandler(int)
{
::signal(SIGSEGV, SIG_DFL);
::signal(SIGABRT, SIG_DFL);
qSetMessagePattern(
"[ "
#ifdef QCRITICAL_BACKTRACE_STR
QCRITICAL_BACKTRACE_STR " "
#endif
#ifdef QDEBUG_LOG_TIME
QDEBUG_LOG_TIME_STR
#endif
" ] "
" - "
"%{message}"
);

qCritical("Scopy finished with error\n");
exit(EXIT_SUCCESS);
}

void CrashReport::initSignalHandler()
{
tmpFilePath_ = scopy::config::tempLogFilePath();
QString qd = scopy::config::settingsFolderPath();

QFileInfo previousLog(tmpFilePath_);

if (previousLog.exists()) {
qInfo() << "Found existing crash stack trace";
QString dumpDateAndTime = previousLog.lastModified().toString(Qt::ISODate).replace("T","--").replace(":","-");
QString currentLog = QDir::cleanPath(qd + "/" + "ScopyCrashDump--" + dumpDateAndTime + ".log");
int renamed = QFile::rename(previousLog.filePath(), currentLog);
if (renamed) {
qInfo() << "Successfully renamed crash log to: " << currentLog;
}
QFile::remove(tmpFilePath_);
}

signal(SIGILL, &signalHandler);
signal(SIGSEGV, &signalHandler);
signal(SIGABRT, &signalHandler);
signal(SIGFPE, &signalHandler);
}
7 changes: 5 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <core/cmdlinehandler.h>
#include <core/scopymainwindow_api.h>
#include <gui/utils.h>
#include <core/crashreport.h>

using namespace scopy;

Expand Down Expand Up @@ -70,13 +71,15 @@ void printRuntimeEnvironmentInfo()

int main(int argc, char *argv[])
{
initLogging();
QCoreApplication::setOrganizationName("ADI");
QCoreApplication::setOrganizationDomain("analog.com");
QCoreApplication::setApplicationName("Scopy-v2");
QSettings::setDefaultFormat(QSettings::IniFormat);

QApplication::setAttribute(Qt::AA_ShareOpenGLContexts, true);
initLogging();
CrashReport::initSignalHandler();

QApplication::setAttribute(Qt::AA_ShareOpenGLContexts,true);
QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);

QApplication a(argc, argv);
Expand Down

0 comments on commit cfe1d2a

Please sign in to comment.