Skip to content

shared: add crash handler that prints a backtrace - #3985

Open
Hasshey wants to merge 1 commit into
linux-nvme:masterfrom
Hasshey:crash-handler-backtrace
Open

shared: add crash handler that prints a backtrace#3985
Hasshey wants to merge 1 commit into
linux-nvme:masterfrom
Hasshey:crash-handler-backtrace

Conversation

@Hasshey

@Hasshey Hasshey commented Sep 7, 2026

Copy link
Copy Markdown

Print the call stack via backtrace_symbols_fd() on SIGSEGV/SIGABRT/SIGFPE/ SIGILL/SIGBUS, then re-raise with default disposition so core dumps and kill status are preserved.

Lives in shared/ so the CLI, libnvme, discoverd and nvmf-autoconnect can all use it. libnvme installs it automatically via a constructor (libnvme_init_crash_handler) so every consumer of libnvme gets crash backtraces without any code changes. The nvme CLI also calls nvme_install_crash_handler() at startup as a belt-and-suspenders measure for builds that don't link libnvme.

Closes #3716

@Hasshey
Hasshey force-pushed the crash-handler-backtrace branch from 8cf12e5 to e400dd0 Compare September 7, 2026 02:43
Print the call stack via backtrace() on fatal signals (SIGSEGV, SIGABRT,
SIGFPE, SIGILL, SIGBUS), then re-raise with default disposition so core
dumps and kill status are preserved. Lives in shared/ so the CLI, libnvme,
discoverd and nvmf-autoconnect can all use it.

libnvme installs the handler automatically via a constructor so every
consumer gets crash backtraces without code changes. The nvme CLI also
calls shr_install_crash_handler() at startup as a belt-and-suspenders
measure.

Per-signal skip: shr_install_crash_handler() leaves a signal alone if
the embedding application has already installed a handler for it (e.g.
Sentry, systemd-coredump, SA_SIGINFO with richer context), but still
installs handlers for the rest.

backtrace() may lazy-load libgcc_s.so on its first call via malloc(),
which is unsafe inside a signal handler if the heap is already corrupted.
shr_warmup_backtrace() runs before any handler is installed so the
lazy-load happens safely during normal startup.

Closes: linux-nvme#3716

Signed-off-by: Harshith Allamsetti <harshith.allamsetti@gmail.com>
@Hasshey
Hasshey force-pushed the crash-handler-backtrace branch from e400dd0 to 998a356 Compare September 7, 2026 16:03
@igaw
igaw requested a balanced review from Copilot September 8, 2026 09:08
@igaw

igaw commented Sep 8, 2026

Copy link
Copy Markdown
Collaborator

A bunch of the build targets do not build.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Windows builds are currently broken, and static libnvme consumers do not receive the promised automatic handler installation.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds shared fatal-signal handling that prints backtraces while preserving signal termination semantics.

Changes:

  • Adds crash-handler utilities and tests.
  • Installs handlers from the CLI and libnvme.
  • Enables exported CLI symbols for clearer traces.
File summaries
File Description
src/nvme.c Installs the handler during CLI startup.
src/logging.h Declares crash-handler wrappers.
src/logging.c Connects CLI logging to shared utilities.
shared/crash-util.h Defines the crash utility API.
shared/crash-util-linux.c Implements signal handling and backtraces.
shared/meson.build Adds the implementation to non-Windows builds.
shared/tests/test-crash-util.c Tests installation and signal behavior.
shared/tests/meson.build Registers crash utility tests.
meson.build Adds -rdynamic when supported.
libnvme/src/nvme/crash-handler.c Adds automatic libnvme initialization.
libnvme/src/meson.build Includes the constructor on Linux.
Review details
  • Files reviewed: 11/11 changed files
  • Comments generated: 5
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread shared/meson.build
]
else
sources += [
'crash-util-linux.c',
Comment thread shared/tests/meson.build
Comment on lines +291 to +300
test_crash_util = executable(
'test-crash-util',
['test-crash-util.c'],
dependencies: [
config_dep,
shared_dep,
],
)

test('shared - crash-util', test_crash_util)

#include <shared/crash-util.h>

static __attribute__((constructor)) void libnvme_init_crash_handler(void)
Comment thread shared/crash-util-linux.c
Comment on lines +87 to +90
int n = backtrace(shr_crash_bt, SHR_CRASH_BT_DEPTH);

crash_write_str(fd, "backtrace:\n");
backtrace_symbols_fd(shr_crash_bt, n, fd);
Comment on lines +60 to +61
pass &= check_bool("install returns success",
shr_install_crash_handler() == 0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

print stack trace when crashing

3 participants