From 33d560ed15c774b83c3c8f287cb67cd1e26f41f9 Mon Sep 17 00:00:00 2001 From: Oleksandr Shyshatskyi Date: Wed, 28 Oct 2026 13:40:04 +0200 Subject: [PATCH] diagnostics: persistent crash and lifecycle records for shutdown aborts nv_navigation died with SIGABRT on its main thread during an ignition-off on the car (M9148801407, 2026-10-27 06:07:41 radio time) and left no trace: the crash reporter skipped SIGABRT and every log lived in /tmp. - crash reporter: also SIGABRT (re-raised after the record), libc return addresses in the stack scan, a persistent copy in /datastore/navicore-backend/crash.log (restarted past 256 KB) - std::set_terminate names the uncaught exception before abort - Qt fatal/critical messages are recorded (QThread destroyed while running at exit), then passed to the previous handler - an atexit record registered after InitializeCore says whether exit() ran with the OsmAnd core still live - provider lifecycle breadcrumbs with monotonic ms in /datastore/navicore-backend/lifecycle.log (panaInitialize, panaDeinitialize, each deinit stage) to time deinit against the 30 s System Manager stop limit Co-Authored-By: Claude Opus 5.5 --- backends/osmand/crash_reporter.cpp | 79 ++++++++++++++++++++++++++++-- backends/osmand/crash_reporter.h | 18 ++++--- backends/osmand/osmand_engine.cpp | 52 +++++++++++++++++++- src/compat/qr39/navicore_compat.c | 59 ++++++++++++++++++++++ 4 files changed, 196 insertions(+), 12 deletions(-) diff --git a/backends/osmand/crash_reporter.cpp b/backends/osmand/crash_reporter.cpp index 980d2c5..2879874 100644 --- a/backends/osmand/crash_reporter.cpp +++ b/backends/osmand/crash_reporter.cpp @@ -1,26 +1,42 @@ #include "crash_reporter.h" +#include #include #include #include +#include +#include #include #include #include +#include +#include #include +#include +#include namespace navicore_osmand { namespace { int crash_fd = -1; +int persistent_fd = -1; // Shared libraries are mapped in this window on the radio. constexpr std::uintptr_t library_low = 0x70000000u; constexpr std::uintptr_t library_high = 0x80000000u; -constexpr int stack_words = 256; +// procnto maps libc.so.3 here; abort() and raise() return addresses land in it. +constexpr std::uintptr_t libc_low = 0x01000000u; +constexpr std::uintptr_t libc_high = 0x01100000u; +constexpr int stack_words = 512; +// The persistent copy survives a reboot; start it over when it grows past this. +constexpr off_t persistent_limit_bytes = 256 * 1024; void put(const char* text) noexcept { + const std::size_t length = std::strlen(text); if (crash_fd >= 0) - (void)::write(crash_fd, text, std::strlen(text)); + (void)::write(crash_fd, text, length); + if (persistent_fd >= 0) + (void)::write(persistent_fd, text, length); } void put_hex(std::uintptr_t value) noexcept @@ -35,14 +51,24 @@ void put_hex(std::uintptr_t value) noexcept put(text); } +bool code_address(std::uint32_t word) noexcept +{ + return (word >= library_low && word < library_high) || + (word >= libc_low && word < libc_high); +} + void handler(int signal, siginfo_t* info, void* context) noexcept { put("crash signal="); put_hex(static_cast(signal)); + put(" pid="); + put_hex(static_cast(::getpid())); put(" tid="); put_hex(static_cast(pthread_self())); put(" addr="); put_hex(reinterpret_cast(info ? info->si_addr : nullptr)); + put(" time="); + put_hex(static_cast(::time(nullptr))); #if defined(__arm__) && defined(__QNX__) const auto* uc = static_cast(context); if (uc) { @@ -58,7 +84,7 @@ void handler(int signal, siginfo_t* info, void* context) noexcept reinterpret_cast(gpr[ARM_REG_SP]); for (int i = 0; stack && i < stack_words; ++i) { const std::uint32_t word = stack[i]; - if (word >= library_low && word < library_high) { + if (code_address(word)) { put(" "); put_hex(word); } @@ -68,15 +94,46 @@ void handler(int signal, siginfo_t* info, void* context) noexcept (void)context; #endif put("\n"); - // Die normally: restore the default action and return to re-fault. + // Die normally: restore the default action. A fault re-faults on return; + // SIGABRT was raised on purpose, so raise it again (delivered on return). ::signal(signal, SIG_DFL); + if (signal == SIGABRT) + (void)::raise(SIGABRT); +} + +// std::terminate (uncaught exception, joinable std::thread destroyed, ...) +// ends in abort(); name the exception first, the SIGABRT line follows. +[[noreturn]] void terminate_handler() noexcept +{ + put("terminate pid="); + put_hex(static_cast(::getpid())); + put(" tid="); + put_hex(static_cast(pthread_self())); + const std::type_info* type = abi::__cxa_current_exception_type(); + put(" exception="); + put(type ? type->name() : "none"); + put("\n"); + std::abort(); +} + +int open_persistent(const char* path) noexcept +{ + int fd = ::open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); + struct stat status {}; + if (fd >= 0 && ::fstat(fd, &status) == 0 && + status.st_size > persistent_limit_bytes) + (void)::ftruncate(fd, 0); + return fd; } } // namespace -void install_crash_reporter(const char* path) noexcept +void install_crash_reporter(const char* path, + const char* persistent_path) noexcept { if (crash_fd < 0) crash_fd = ::open(path, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (persistent_fd < 0 && persistent_path) + persistent_fd = open_persistent(persistent_path); struct sigaction action {}; action.sa_sigaction = handler; action.sa_flags = SA_SIGINFO; @@ -84,6 +141,18 @@ void install_crash_reporter(const char* path) noexcept (void)::sigaction(SIGSEGV, &action, nullptr); (void)::sigaction(SIGBUS, &action, nullptr); (void)::sigaction(SIGILL, &action, nullptr); + (void)::sigaction(SIGABRT, &action, nullptr); + std::set_terminate(terminate_handler); +} + +void crash_report_line(const char* text) noexcept +{ + char stamp[24]; + (void)std::snprintf(stamp, sizeof stamp, "%ld ", + static_cast(::time(nullptr))); + put(stamp); + put(text); + put("\n"); } } // namespace navicore_osmand diff --git a/backends/osmand/crash_reporter.h b/backends/osmand/crash_reporter.h index 45e140a..9e7cec1 100644 --- a/backends/osmand/crash_reporter.h +++ b/backends/osmand/crash_reporter.h @@ -3,11 +3,17 @@ namespace navicore_osmand { // Writes the faulting thread's PC, LR, SP and the stack words that look like -// shared-library code addresses to `path` on SIGSEGV/SIGBUS/SIGILL, then -// restores the default action so the process still dies and System Manager -// sees the crash. Pair the output with the "load_base" trace lines and -// addr2line to recover a backtrace when no core file is available (the -// radio's dumper skips processes over 10 MB). Safe to call more than once. -void install_crash_reporter(const char* path) noexcept; +// shared-library or libc code addresses to `path` (and, when given, to +// `persistent_path`, which survives a reboot) on SIGSEGV/SIGBUS/SIGILL/SIGABRT, +// then restores the default action so the process still dies and System +// Manager sees the crash. std::terminate logs the exception type first. Pair +// the output with the "load_base" trace lines and addr2line to recover a +// backtrace when no core file is available (the radio's dumper skips +// processes over 10 MB). Safe to call more than once. +void install_crash_reporter(const char* path, + const char* persistent_path = nullptr) noexcept; + +// Appends one timestamped line to the same files (Qt fatal messages, exit). +void crash_report_line(const char* text) noexcept; } // namespace navicore_osmand diff --git a/backends/osmand/osmand_engine.cpp b/backends/osmand/osmand_engine.cpp index d0db501..7da001b 100644 --- a/backends/osmand/osmand_engine.cpp +++ b/backends/osmand/osmand_engine.cpp @@ -32,9 +32,13 @@ #include #include +#include #include +#include #include +#include +#include #include #include #include @@ -59,6 +63,46 @@ namespace { enum class Phase { Empty, Resources, CoreEntering, CoreReady, RenderingEntering, Ready, Quarantined }; +constexpr const char* kCrashLog = "/tmp/open-navicore-crash.log"; +constexpr const char* kPersistentCrashLog = "/datastore/navicore-backend/crash.log"; + +// Set between InitializeCore and ReleaseCore. An exit() while it is set runs +// OsmAndCore's static destructors over a live core (QThread qFatal, asserts). +std::atomic core_live{false}; +QtMessageHandler previous_qt_handler = nullptr; + +void report_qt_message(QtMsgType type, const QMessageLogContext& context, + const QString& message) +{ + if (type == QtFatalMsg || type == QtCriticalMsg) { + char line[320]; + (void)std::snprintf(line, sizeof line, "qt %s: %s (%s:%d)", + type == QtFatalMsg ? "fatal" : "critical", + message.toLocal8Bit().constData(), + context.file ? context.file : "?", context.line); + crash_report_line(line); + } + if (previous_qt_handler) + previous_qt_handler(type, context, message); +} + +void report_exit() +{ + crash_report_line(core_live.load() ? "exit with OsmAnd core live" + : "exit after OsmAnd core release"); +} + +// Registered after InitializeCore, so it runs before OsmAndCore's own static +// destructors. +void install_exit_diagnostics() +{ + static std::once_flag once; + std::call_once(once, [] { + previous_qt_handler = qInstallMessageHandler(report_qt_message); + (void)std::atexit(report_exit); + }); +} + constexpr std::chrono::milliseconds kClusterPublishInterval{500}; constexpr std::chrono::milliseconds kDevMenuRefreshInterval{500}; @@ -519,7 +563,10 @@ bool OsmAndEngine::initialize(const Config& config, return failure("error=initialize_core_partial"); phase_ = Phase::CoreReady; trace("init=core"); - install_crash_reporter("/tmp/open-navicore-crash.log"); + install_crash_reporter(kCrashLog, kPersistentCrashLog); + core_live.store(true); + install_exit_diagnostics(); + crash_report_line("OsmAnd core initialized"); log_load_bases(); obfs_ = std::make_shared(); @@ -2093,7 +2140,10 @@ ReleaseResult OsmAndEngine::release(ReleaseMode mode) basemap_obfs_.reset(); if (phase_ == Phase::CoreReady) { phase_ = Phase::Quarantined; + crash_report_line("OsmAnd core release begin"); OsmAnd::ReleaseCore(); + core_live.store(false); + crash_report_line("OsmAnd core released"); } resources_.reset(); phase_ = Phase::Empty; diff --git a/src/compat/qr39/navicore_compat.c b/src/compat/qr39/navicore_compat.c index 4b4285d..b62a8b3 100644 --- a/src/compat/qr39/navicore_compat.c +++ b/src/compat/qr39/navicore_compat.c @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,10 @@ #ifndef OPEN_NAVI_TRACE_PATH #define OPEN_NAVI_TRACE_PATH "/tmp/open-navi.log" #endif +#ifndef OPEN_NAVI_BREADCRUMB_PATH +#define OPEN_NAVI_BREADCRUMB_PATH "/datastore/navicore-backend/lifecycle.log" +#endif +#define OPEN_NAVI_BREADCRUMB_LIMIT_BYTES (64 * 1024) #if defined(__QNXNTO__) /* * The QR39 consumer starts five proxy-worker singletons in parallel with the @@ -709,6 +714,54 @@ static void open_navi_log(int priority, const char *format, ...) #endif } +/* + * Lifecycle breadcrumbs that survive a reboot. The /tmp trace is gone after + * the next boot, and a shutdown abort (2026-10-27, car) needs the deinit + * timing: System Manager allows nv_navigation 30 s to stop. + */ +static void open_navi_breadcrumb(const char *format, ...) +{ +#if defined(OPEN_NAVI_DISABLE_LOG) + (void)format; +#else + char message[200]; + char line[256]; + struct timespec monotonic; + struct stat status; + va_list arguments; + int saved_errno = errno; + int fd; + int length; + int truncate_result; + ssize_t write_result; + + va_start(arguments, format); + (void)vsnprintf(message, sizeof(message), format, arguments); + va_end(arguments); + (void)clock_gettime(CLOCK_MONOTONIC, &monotonic); + length = snprintf(line, sizeof(line), "%ld mono_ms=%lld pid=%d tid=%d %s\n", + (long)time(NULL), + (long long)monotonic.tv_sec * 1000 + + monotonic.tv_nsec / 1000000, + (int)getpid(), (int)pthread_self(), message); + if (length > (int)sizeof(line) - 1) { + length = (int)sizeof(line) - 1; + } + fd = open(OPEN_NAVI_BREADCRUMB_PATH, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd >= 0) { + if (fstat(fd, &status) == 0 && + status.st_size > OPEN_NAVI_BREADCRUMB_LIMIT_BYTES) { + truncate_result = ftruncate(fd, 0); + (void)truncate_result; + } + write_result = write(fd, line, (size_t)length); + (void)write_result; + (void)close(fd); + } + errno = saved_errno; +#endif +} + static void prepare_objects(void) { uint32_t adapter; @@ -2805,6 +2858,7 @@ uint32_t panaInitialize(const void *init_record) open_navi_clear_host_proxies_locked(); (void)pthread_mutex_unlock(&g_lifecycle_lock); + open_navi_breadcrumb("panaInitialize"); /* Log pointer values only. The provider does not own or dereference paths. */ open_navi_log(LOG_NOTICE, "panaInitialize paths=%p/%p/%p/%p/%p/%p", @@ -2871,6 +2925,7 @@ static void open_navi_finish_deinitialize(void) int join_worker; int self_worker; + open_navi_breadcrumb("deinitialize: finish begin"); (void)pthread_mutex_lock(&g_worker_lock); g_worker_stop = 1; (void)pthread_cond_broadcast(&g_worker_condition); @@ -2892,6 +2947,7 @@ static void open_navi_finish_deinitialize(void) (void)pthread_mutex_unlock(&g_worker_lock); open_navi_log(LOG_NOTICE, "readiness worker joined"); } + open_navi_breadcrumb("deinitialize: readiness worker stopped"); (void)pthread_mutex_lock(&g_worker_lock); g_worker_epoch = 0u; g_si_ready_sent = 0; @@ -2906,6 +2962,7 @@ static void open_navi_finish_deinitialize(void) open_navi_renderer_destroy(); open_navi_renderer_end_lifecycle(); open_navi_log(LOG_NOTICE, "renderer destroyed"); + open_navi_breadcrumb("deinitialize: renderer destroyed"); open_navi_gps_backend_reset(); open_navi_scalar_backend_reset(); open_navi_api_telemetry_reset(); @@ -2913,12 +2970,14 @@ static void open_navi_finish_deinitialize(void) open_navi_callback_backend_reset(); open_navi_cluster_relay_stop(); open_navi_cluster_backend_reset(); + open_navi_breadcrumb("deinitialize: finish end"); } void panaDeinitialize(void) { int finish = 0; + open_navi_breadcrumb("panaDeinitialize"); (void)pthread_mutex_lock(&g_lifecycle_lock); while (g_state == OPEN_NAVI_STARTING) { (void)pthread_cond_wait(&g_lifecycle_condition, &g_lifecycle_lock);