shared: add crash handler that prints a backtrace - #3985
Open
Hasshey wants to merge 1 commit into
Open
Conversation
Hasshey
force-pushed
the
crash-handler-backtrace
branch
from
September 7, 2026 02:43
8cf12e5 to
e400dd0
Compare
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
force-pushed
the
crash-handler-backtrace
branch
from
September 7, 2026 16:03
e400dd0 to
998a356
Compare
Collaborator
|
A bunch of the build targets do not build. |
There was a problem hiding this comment.
🟡 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.
| ] | ||
| else | ||
| sources += [ | ||
| 'crash-util-linux.c', |
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 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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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