Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 74 additions & 5 deletions backends/osmand/crash_reporter.cpp
Original file line number Diff line number Diff line change
@@ -1,26 +1,42 @@
#include "crash_reporter.h"

#include <cxxabi.h>
#include <fcntl.h>
#include <pthread.h>
#include <signal.h>
#include <sys/stat.h>
#include <time.h>
#include <ucontext.h>
#include <unistd.h>

#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <typeinfo>

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
Expand All @@ -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<std::uintptr_t>(signal));
put(" pid=");
put_hex(static_cast<std::uintptr_t>(::getpid()));
put(" tid=");
put_hex(static_cast<std::uintptr_t>(pthread_self()));
put(" addr=");
put_hex(reinterpret_cast<std::uintptr_t>(info ? info->si_addr : nullptr));
put(" time=");
put_hex(static_cast<std::uintptr_t>(::time(nullptr)));
#if defined(__arm__) && defined(__QNX__)
const auto* uc = static_cast<const ucontext_t*>(context);
if (uc) {
Expand All @@ -58,7 +84,7 @@ void handler(int signal, siginfo_t* info, void* context) noexcept
reinterpret_cast<const std::uint32_t*>(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);
}
Expand All @@ -68,22 +94,65 @@ 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<std::uintptr_t>(::getpid()));
put(" tid=");
put_hex(static_cast<std::uintptr_t>(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;
sigemptyset(&action.sa_mask);
(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<long>(::time(nullptr)));
put(stamp);
put(text);
put("\n");
}

} // namespace navicore_osmand
18 changes: 12 additions & 6 deletions backends/osmand/crash_reporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
52 changes: 51 additions & 1 deletion backends/osmand/osmand_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,13 @@

#include <QByteArray>
#include <QString>
#include <QtGlobal>

#include <algorithm>
#include <atomic>
#include <cctype>
#include <cstdlib>
#include <mutex>
#include <stdexcept>
#include <chrono>
#include <cmath>
Expand All @@ -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<bool> 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};

Expand Down Expand Up @@ -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<OsmAnd::ObfsCollection>();
Expand Down Expand Up @@ -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;
Expand Down
59 changes: 59 additions & 0 deletions src/compat/qr39/navicore_compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>
Expand All @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -2906,19 +2962,22 @@ 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();
open_navi_pointer_backend_reset();
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);
Expand Down