crash: switch to cpptrace from breakpad

This commit is contained in:
outfoxxed 2026-03-02 08:09:57 -08:00
parent cddb4f061b
commit cdde4c63f4
No known key found for this signature in database
GPG key ID: 4C88A185FB89301E
19 changed files with 372 additions and 173 deletions

View file

@ -1,7 +1,10 @@
#include "main.hpp"
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <cpptrace/basic.hpp>
#include <cpptrace/formatting.hpp>
#include <qapplication.h>
#include <qconfig.h>
#include <qcoreapplication.h>
@ -13,13 +16,17 @@
#include <qtenvironmentvariables.h>
#include <qtextstream.h>
#include <qtversion.h>
#include <qtypes.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <unistd.h>
#include "../core/instanceinfo.hpp"
#include "../core/logcat.hpp"
#include "../core/logging.hpp"
#include "../core/logging_p.hpp"
#include "../core/paths.hpp"
#include "../core/ringbuf.hpp"
#include "build.hpp"
#include "interface.hpp"
@ -61,6 +68,76 @@ int tryDup(int fd, const QString& path) {
return 0;
}
QString readRecentLogs(int logFd, int maxLines, qint64 maxAgeSecs) {
QFile file;
if (!file.open(logFd, QFile::ReadOnly, QFile::AutoCloseHandle)) {
return QStringLiteral("(failed to open log fd)\n");
}
file.seek(0);
qs::log::EncodedLogReader reader;
reader.setDevice(&file);
bool readable = false;
quint8 logVersion = 0;
quint8 readerVersion = 0;
if (!reader.readHeader(&readable, &logVersion, &readerVersion) || !readable) {
return QStringLiteral("(failed to read log header)\n");
}
// Read all messages, keeping last maxLines in a ring buffer
auto tail = RingBuffer<qs::log::LogMessage>(maxLines);
qs::log::LogMessage message;
while (reader.read(&message)) {
tail.emplace(message);
}
if (tail.size() == 0) {
return QStringLiteral("(no logs)\n");
}
// Filter to only messages within maxAgeSecs of the newest message
auto cutoff = tail.at(0).time.addSecs(-maxAgeSecs);
QString result;
auto stream = QTextStream(&result);
for (auto i = tail.size() - 1; i != -1; i--) {
if (tail.at(i).time < cutoff) continue;
qs::log::LogMessage::formatMessage(stream, tail.at(i), false, true);
stream << '\n';
}
if (result.isEmpty()) {
return QStringLiteral("(no recent logs)\n");
}
return result;
}
cpptrace::stacktrace resolveStacktrace(int dumpFd) {
QFile sourceFile;
if (!sourceFile.open(dumpFd, QFile::ReadOnly, QFile::AutoCloseHandle)) {
qCCritical(logCrashReporter) << "Failed to open trace memfd.";
return {};
}
sourceFile.seek(0);
auto data = sourceFile.readAll();
auto frameCount = static_cast<size_t>(data.size()) / sizeof(cpptrace::safe_object_frame);
if (frameCount == 0) return {};
const auto* frames = reinterpret_cast<const cpptrace::safe_object_frame*>(data.constData());
cpptrace::object_trace objectTrace;
for (size_t i = 0; i < frameCount; i++) {
objectTrace.frames.push_back(frames[i].resolve()); // NOLINT
}
return objectTrace.resolve();
}
void recordCrashInfo(const QDir& crashDir, const InstanceInfo& instance) {
qCDebug(logCrashReporter) << "Recording crash information at" << crashDir.path();
@ -71,32 +148,25 @@ void recordCrashInfo(const QDir& crashDir, const InstanceInfo& instance) {
}
auto crashProc = qEnvironmentVariable("__QUICKSHELL_CRASH_DUMP_PID").toInt();
auto crashSignal = qEnvironmentVariable("__QUICKSHELL_CRASH_SIGNAL").toInt();
auto dumpFd = qEnvironmentVariable("__QUICKSHELL_CRASH_DUMP_FD").toInt();
auto logFd = qEnvironmentVariable("__QUICKSHELL_CRASH_LOG_FD").toInt();
qCDebug(logCrashReporter) << "Saving minidump from fd" << dumpFd;
auto dumpDupStatus = tryDup(dumpFd, crashDir.filePath("minidump.dmp.log"));
if (dumpDupStatus != 0) {
qCCritical(logCrashReporter) << "Failed to write minidump:" << dumpDupStatus;
}
qCDebug(logCrashReporter) << "Resolving stacktrace from fd" << dumpFd;
auto stacktrace = resolveStacktrace(dumpFd);
qCDebug(logCrashReporter) << "Saving log from fd" << logFd;
auto logDupStatus = tryDup(logFd, crashDir.filePath("log.qslog.log"));
qCDebug(logCrashReporter) << "Reading recent log lines from fd" << logFd;
auto logDupFd = dup(logFd);
auto recentLogs = readRecentLogs(logFd, 100, 10);
qCDebug(logCrashReporter) << "Saving log from fd" << logDupFd;
auto logDupStatus = tryDup(logDupFd, crashDir.filePath("log.qslog.log"));
if (logDupStatus != 0) {
qCCritical(logCrashReporter) << "Failed to save log:" << logDupStatus;
}
auto copyBinStatus = 0;
if (!DISTRIBUTOR_DEBUGINFO_AVAILABLE) {
qCDebug(logCrashReporter) << "Copying binary to crash folder";
if (!QFile(QCoreApplication::applicationFilePath()).copy(crashDir.filePath("executable.txt"))) {
copyBinStatus = 1;
qCCritical(logCrashReporter) << "Failed to copy binary.";
}
}
{
auto extraInfoFile = QFile(crashDir.filePath("info.txt"));
auto extraInfoFile = QFile(crashDir.filePath("report.txt"));
if (!extraInfoFile.open(QFile::WriteOnly)) {
qCCritical(logCrashReporter) << "Failed to open crash info file for writing.";
} else {
@ -111,16 +181,12 @@ void recordCrashInfo(const QDir& crashDir, const InstanceInfo& instance) {
stream << "\n===== Runtime Information =====\n";
stream << "Runtime Qt Version: " << qVersion() << '\n';
stream << "Signal: " << strsignal(crashSignal) << " (" << crashSignal << ")\n"; // NOLINT
stream << "Crashed process ID: " << crashProc << '\n';
stream << "Run ID: " << instance.instanceId << '\n';
stream << "Shell ID: " << instance.shellId << '\n';
stream << "Config Path: " << instance.configPath << '\n';
stream << "\n===== Report Integrity =====\n";
stream << "Minidump save status: " << dumpDupStatus << '\n';
stream << "Log save status: " << logDupStatus << '\n';
stream << "Binary copy status: " << copyBinStatus << '\n';
stream << "\n===== System Information =====\n\n";
stream << "/etc/os-release:";
auto osReleaseFile = QFile("/etc/os-release");
@ -140,6 +206,18 @@ void recordCrashInfo(const QDir& crashDir, const InstanceInfo& instance) {
stream << "FAILED TO OPEN\n";
}
stream << "\n===== Stacktrace =====\n";
if (stacktrace.empty()) {
stream << "(no trace available)\n";
} else {
auto formatter = cpptrace::formatter().header(std::string());
auto traceStr = formatter.format(stacktrace);
stream << QString::fromStdString(traceStr) << '\n';
}
stream << "\n===== Log Tail =====\n";
stream << recentLogs;
extraInfoFile.close();
}
}