mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-08-13 18:14:13 +02:00
Add crash-time ring flush and a diagnostics export UI (discussion #644, steps 4-5)
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.
This commit is contained in:
+99
-38
@@ -17,75 +17,136 @@
|
||||
*/
|
||||
#include "logring.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
/**
|
||||
@brief LogRing::LogRing
|
||||
Preallocates all kCapacityEntries slots up front -- the only
|
||||
allocation this class ever does.
|
||||
*/
|
||||
LogRing::LogRing()
|
||||
{
|
||||
m_entries.resize(kCapacityEntries);
|
||||
}
|
||||
#ifdef Q_OS_WIN
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <cerrno>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
static_assert(std::atomic<int>::is_always_lock_free,
|
||||
"LogRing::Entry::length must be a lock-free atomic<int> -- "
|
||||
"dumpToFd() reads it from a signal handler and must never block.");
|
||||
|
||||
namespace {
|
||||
|
||||
/**
|
||||
@brief LogRing::append
|
||||
@param line one already-formatted log line (no further formatting
|
||||
is done here). Truncated to kEntryBytes - 1 bytes if longer, with a
|
||||
trailing marker, so the stored entry is always a complete,
|
||||
independently-readable line.
|
||||
@brief writeAllSignalSafe
|
||||
Loops until length bytes have been written to fd or an unrecoverable
|
||||
error occurs. write(2) may write fewer bytes than requested and may
|
||||
return EINTR -- both are *more* likely from inside a signal handler
|
||||
than in normal code, so a single write() call is not enough here.
|
||||
Async-signal-safe: only calls write(2)/errno, nothing else.
|
||||
*/
|
||||
void LogRing::append(const QByteArray &line)
|
||||
void writeAllSignalSafe(int fd, const char *data, int length) noexcept
|
||||
{
|
||||
int remaining = length;
|
||||
const char *p = data;
|
||||
|
||||
while (remaining > 0) {
|
||||
#ifdef Q_OS_WIN
|
||||
const int n = _write(fd, p, static_cast<unsigned int>(remaining));
|
||||
if (n <= 0) {
|
||||
return;
|
||||
}
|
||||
#else
|
||||
const ssize_t n = ::write(fd, p, static_cast<size_t>(remaining));
|
||||
if (n < 0) {
|
||||
if (errno == EINTR) {
|
||||
continue;
|
||||
}
|
||||
return; // unrecoverable -- give up silently, never block/throw
|
||||
}
|
||||
if (n == 0) {
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
p += n;
|
||||
remaining -= static_cast<int>(n);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
LogRing::LogRing() :
|
||||
// The sized constructor value-initialises each Entry in place; unlike
|
||||
// resize(), it doesn't require Entry to be move/copy-constructible,
|
||||
// which std::atomic<int> deliberately never is. The only allocation
|
||||
// this class ever does.
|
||||
m_entries(kCapacityEntries)
|
||||
{
|
||||
}
|
||||
|
||||
void LogRing::append(const QByteArray &line) noexcept
|
||||
{
|
||||
static const char kMarker[] = "...[ring-truncated]\n";
|
||||
const int marker_len = static_cast<int>(sizeof(kMarker)) - 1;
|
||||
|
||||
QMutexLocker locker(&m_mutex);
|
||||
const quint64 idx = m_write_cursor.fetch_add(1, std::memory_order_relaxed);
|
||||
Entry &slot = m_entries[static_cast<size_t>(idx % static_cast<quint64>(kCapacityEntries))];
|
||||
|
||||
Entry &slot = m_entries[static_cast<size_t>(m_next_index)];
|
||||
// Zero the length first so a concurrent reader landing on this exact
|
||||
// slot mid-copy sees "not ready" rather than the previous lap's
|
||||
// (now-being-overwritten) content at a stale length.
|
||||
slot.length.store(0, std::memory_order_relaxed);
|
||||
|
||||
int len;
|
||||
if (line.size() < kEntryBytes) {
|
||||
std::memcpy(slot.data, line.constData(), static_cast<size_t>(line.size()));
|
||||
slot.length = line.size();
|
||||
len = line.size();
|
||||
} else {
|
||||
const int keep = kEntryBytes - marker_len;
|
||||
std::memcpy(slot.data, line.constData(), static_cast<size_t>(keep));
|
||||
std::memcpy(slot.data + keep, kMarker, static_cast<size_t>(marker_len));
|
||||
slot.length = kEntryBytes;
|
||||
len = kEntryBytes;
|
||||
}
|
||||
|
||||
m_next_index = (m_next_index + 1) % kCapacityEntries;
|
||||
if (m_count < kCapacityEntries) {
|
||||
++m_count;
|
||||
}
|
||||
slot.length.store(len, std::memory_order_release);
|
||||
}
|
||||
|
||||
/**
|
||||
@brief LogRing::snapshot
|
||||
@return the entries currently held, oldest first. Safe to call from
|
||||
normal (non-signal) code only.
|
||||
*/
|
||||
QVector<QByteArray> LogRing::snapshot() const
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
const quint64 cursor = m_write_cursor.load(std::memory_order_acquire);
|
||||
const quint64 cap = static_cast<quint64>(kCapacityEntries);
|
||||
const quint64 count = (cursor < cap) ? cursor : cap;
|
||||
const quint64 start = (cursor < cap) ? 0 : (cursor - cap);
|
||||
|
||||
QVector<QByteArray> result;
|
||||
result.reserve(m_count);
|
||||
result.reserve(static_cast<int>(count));
|
||||
|
||||
const int start = (m_count < kCapacityEntries) ? 0 : m_next_index;
|
||||
for (int i = 0; i < m_count; ++i) {
|
||||
const Entry &slot = m_entries[static_cast<size_t>((start + i) % kCapacityEntries)];
|
||||
result.append(QByteArray(slot.data, slot.length));
|
||||
for (quint64 i = 0; i < count; ++i) {
|
||||
const Entry &slot = m_entries[static_cast<size_t>((start + i) % cap)];
|
||||
const int len = slot.length.load(std::memory_order_acquire);
|
||||
if (len > 0) {
|
||||
result.append(QByteArray(slot.data, len));
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
void LogRing::dumpToFd(int fd) const noexcept
|
||||
{
|
||||
const quint64 cursor = m_write_cursor.load(std::memory_order_acquire);
|
||||
const quint64 cap = static_cast<quint64>(kCapacityEntries);
|
||||
const quint64 count = (cursor < cap) ? cursor : cap;
|
||||
const quint64 start = (cursor < cap) ? 0 : (cursor - cap);
|
||||
|
||||
for (quint64 i = 0; i < count; ++i) {
|
||||
const Entry &slot = m_entries[static_cast<size_t>((start + i) % cap)];
|
||||
const int len = slot.length.load(std::memory_order_acquire);
|
||||
if (len > 0) {
|
||||
writeAllSignalSafe(fd, slot.data, len);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LogRing::clear()
|
||||
{
|
||||
QMutexLocker locker(&m_mutex);
|
||||
m_next_index = 0;
|
||||
m_count = 0;
|
||||
m_write_cursor.store(0, std::memory_order_relaxed);
|
||||
for (auto &entry : m_entries) {
|
||||
entry.length.store(0, std::memory_order_relaxed);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user