mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-08-13 10:04:13 +02:00
5dec36cb29
Stacked on the steps 1-3 branch (feature-diagnostic-logging, PR #646). Kept as its own PR rather than folded into that one, matching the discussion's own framing: step 4 is explicitly "the highest-risk piece ... lands last, behind its own switch." ## Step 4 -- crash-time ring flush (CrashHandler) Installs a handler for SIGSEGV/SIGABRT/SIGBUS/SIGFPE/SIGILL (POSIX) / SetUnhandledExceptionFilter (Windows) that flushes the in-memory ring to a fixed crash_dump.log before the process dies. This required reworking LogRing (step 3) to be genuinely lock-free, not just mutex-protected: a signal handler that blocks on a lock the crashing thread (or another thread) already holds turns a clean crash into a hang -- no ring dump *and* no core dump, worse than doing nothing. append() now claims a slot with a single atomic fetch-add; dumpToFd() reads the preallocated entries directly and writes them with write(2) only, looping on EINTR/short writes. Accepted tradeoff: at most one entry can be read torn if a crash lands mid-append into that exact slot -- documented in logring.h, and the alternative (a seqlock to detect and retry) wasn't judged worth the complexity for that window. Other invariants implemented per the discussion: - sigaltstack with a static 64 KiB buffer, SA_ONSTACK -- a stack- overflow SIGSEGV has no usable stack for a handler without one. - Nothing under the actual handler touches Qt, QString or the allocator: the dump path and a small header (version/git/OS/Qt) are precomputed into fixed char buffers by install(), which runs once at startup in normal context. - Atomic test-and-set so only the first crash writes a dump; a second concurrent/nested fault goes straight to restore-and-re-raise. - After writing, the handler restores SIG_DFL and re-raises (POSIX) / returns EXCEPTION_CONTINUE_SEARCH (Windows) so the OS's own crash path -- core dump, Windows Error Reporting -- still runs. A handler that "fixed" the crash by swallowing the signal would destroy exactly the post-mortem evidence this whole design exists to preserve. Tested in this environment: POSIX/Linux only, all five signals. Sent each directly to a running process and confirmed (a) crash_dump.log is written with the correct header and ring contents, mode 0600, and (b) the process still terminates via the signal with the kernel's own "core dumped" flag set (exit code 128+signal, confirmed for all five). The Windows path is implemented per the discussion's guidance but is untested -- no Windows build available in this sandbox. ## Step 5 -- getting the data back out - QETApp::checkCrashDump(), called from checkBackupFiles() only when there's no stale project file to recover this run (so the two prompts never both show, per the discussion), offers an unretrieved crash dump via DiagnosticsReportDialog and then deletes it regardless of the user's choice -- offered exactly once. - A new "Aide > Enregistrer un rapport de diagnostic..." action (QETMainWindow) builds the same kind of report from the *current* session (QetLogger::buildDiagnosticsReport(): header + this session's log file) for a manual "attach this to a bug report" flow, not tied to a crash. - Both go through QetLogger::redact() before ever reaching the user: the one redaction implemented is a literal replace of the home directory with "~", since an absolute path under it leaks the account name. The discussion's fancier "optionally redact project filenames too" isn't attempted -- reliably telling a project path apart from arbitrary log text is a much fuzzier problem than a literal prefix match. - DiagnosticsReportDialog shows the full (already-redacted) content before saving, per the discussion: "the user is about to attach this to a public tracker." Verified in a real GUI session (Xvfb): triggered a SIGSEGV, relaunched, confirmed the crash-report dialog appears with the right header/content, confirmed it does not reappear on a second relaunch, and confirmed the manual "Save report" action produces a correctly-formatted report and saves it to a chosen path. Built clean, no new warnings. ## Build systems Registered in both: cmake/qet_compilation_vars.cmake, and qelectrotech.pro. The .pro needed explicit globs for the new sources/logging/ui/ subfolder -- sources/logging/*.{h,cpp} was already globbed, but unlike the other ui/ subfolders that one had no entry of its own, so diagnosticsreportdialog.{h,cpp} would not have been built under qmake.
172 lines
4.7 KiB
C++
172 lines
4.7 KiB
C++
/*
|
|
Copyright 2006-2026 The QElectroTech Team
|
|
This file is part of QElectroTech.
|
|
|
|
QElectroTech is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
QElectroTech is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#include "crashhandler.h"
|
|
|
|
#include "logring.h"
|
|
#include "../qetversion.h"
|
|
|
|
#include <QByteArray>
|
|
#include <QSysInfo>
|
|
#include <atomic>
|
|
#include <cstring>
|
|
|
|
#ifdef Q_OS_WIN
|
|
#include <fcntl.h>
|
|
#include <io.h>
|
|
#include <share.h>
|
|
#include <sys/stat.h>
|
|
#include <windows.h>
|
|
#else
|
|
#include <cerrno>
|
|
#include <csignal>
|
|
#include <fcntl.h>
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
namespace {
|
|
|
|
// Everything the handler touches is preallocated here and filled in by
|
|
// install() (normal context, runs once at startup) -- nothing under the
|
|
// actual signal/exception path may allocate or touch QString/Qt.
|
|
const LogRing *g_ring = nullptr;
|
|
char g_dump_path[1024] = {};
|
|
char g_header[1024] = {};
|
|
int g_header_len = 0;
|
|
|
|
// Guards against two threads crashing at once, or the handler itself
|
|
// faulting while dumping: only the first crash writes a dump. See
|
|
// crashhandler.h invariant 4.
|
|
std::atomic<bool> g_already_dumped{false};
|
|
|
|
#ifndef Q_OS_WIN
|
|
|
|
// A stack-overflow SIGSEGV leaves no usable stack for a handler to run
|
|
// on at all, hence the alternate signal stack (invariant: sized well
|
|
// above any known SIGSTKSZ so this doesn't depend on
|
|
// sysconf(_SC_SIGSTKSZ), which some libc versions require at runtime
|
|
// rather than offering as a compile-time constant).
|
|
char g_altstack[65536];
|
|
|
|
const int kHandledSignals[] = {SIGSEGV, SIGABRT, SIGBUS, SIGFPE, SIGILL};
|
|
|
|
void restoreDefaultAndReraise(int sig)
|
|
{
|
|
struct sigaction sa {};
|
|
sa.sa_handler = SIG_DFL;
|
|
sigemptyset(&sa.sa_mask);
|
|
sa.sa_flags = 0;
|
|
sigaction(sig, &sa, nullptr);
|
|
raise(sig);
|
|
}
|
|
|
|
void signalHandler(int sig)
|
|
{
|
|
if (g_already_dumped.exchange(true, std::memory_order_acq_rel)) {
|
|
// Not the first crash (concurrent fault on another thread, or
|
|
// this handler faulting while dumping): skip straight to
|
|
// restore-and-re-raise rather than risk a second, interleaved
|
|
// write to the same file.
|
|
restoreDefaultAndReraise(sig);
|
|
return;
|
|
}
|
|
|
|
// open/write/close are all on the POSIX async-signal-safe function
|
|
// list; nothing else is called here.
|
|
const int fd = ::open(g_dump_path, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
if (fd >= 0) {
|
|
if (g_header_len > 0) {
|
|
::write(fd, g_header, static_cast<size_t>(g_header_len));
|
|
}
|
|
if (g_ring) {
|
|
g_ring->dumpToFd(fd);
|
|
}
|
|
::close(fd);
|
|
}
|
|
|
|
restoreDefaultAndReraise(sig);
|
|
}
|
|
|
|
#else // Q_OS_WIN
|
|
|
|
LONG WINAPI windowsExceptionFilter(EXCEPTION_POINTERS *)
|
|
{
|
|
bool expected = false;
|
|
if (!g_already_dumped.compare_exchange_strong(expected, true, std::memory_order_acq_rel)) {
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
}
|
|
|
|
int fd = -1;
|
|
errno_t err = _sopen_s(&fd, g_dump_path,
|
|
_O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY,
|
|
_SH_DENYWR, _S_IREAD | _S_IWRITE);
|
|
if (err == 0 && fd >= 0) {
|
|
if (g_header_len > 0) {
|
|
_write(fd, g_header, g_header_len);
|
|
}
|
|
if (g_ring) {
|
|
g_ring->dumpToFd(fd);
|
|
}
|
|
_close(fd);
|
|
}
|
|
|
|
// Do not suppress Windows Error Reporting / an attached debugger --
|
|
// same invariant as re-raising on POSIX (see crashhandler.h,
|
|
// invariant 3).
|
|
return EXCEPTION_CONTINUE_SEARCH;
|
|
}
|
|
|
|
#endif
|
|
|
|
} // namespace
|
|
|
|
void CrashHandler::install(const LogRing *ring, const QString &dump_path)
|
|
{
|
|
g_ring = ring;
|
|
|
|
const QByteArray path_utf8 = dump_path.toUtf8();
|
|
std::strncpy(g_dump_path, path_utf8.constData(), sizeof(g_dump_path) - 1);
|
|
|
|
const QByteArray header = QByteArray("QET crash dump\n")
|
|
+ "Version: " + QetVersion::displayedVersion().toUtf8() + "\n"
|
|
+ "Git: " GIT_COMMIT_SHA "\n"
|
|
+ "OS: " + QSysInfo::prettyProductName().toUtf8() + " (" + QSysInfo::currentCpuArchitecture().toUtf8() + ")\n"
|
|
+ "Qt: " QT_VERSION_STR "\n"
|
|
+ "---\n";
|
|
g_header_len = qMin<int>(header.size(), static_cast<int>(sizeof(g_header)) - 1);
|
|
std::memcpy(g_header, header.constData(), static_cast<size_t>(g_header_len));
|
|
|
|
#ifdef Q_OS_WIN
|
|
SetUnhandledExceptionFilter(windowsExceptionFilter);
|
|
#else
|
|
stack_t ss;
|
|
ss.ss_sp = g_altstack;
|
|
ss.ss_size = sizeof(g_altstack);
|
|
ss.ss_flags = 0;
|
|
sigaltstack(&ss, nullptr);
|
|
|
|
struct sigaction sa {};
|
|
sa.sa_handler = signalHandler;
|
|
sigemptyset(&sa.sa_mask);
|
|
sa.sa_flags = SA_ONSTACK;
|
|
|
|
for (int sig : kHandledSignals) {
|
|
sigaction(sig, &sa, nullptr);
|
|
}
|
|
#endif
|
|
}
|