mirror of
https://github.com/NixOS/nix
synced 2025-06-25 10:41:16 +02:00
Merge pull request #12713 from NixOS/mergify/bp/2.27-maintenance/pr-12636
port crash-handler from lix to nix (backport #12636)
This commit is contained in:
commit
d72fc01ffd
4 changed files with 82 additions and 0 deletions
67
src/nix/crash-handler.cc
Normal file
67
src/nix/crash-handler.cc
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
#include "crash-handler.hh"
|
||||||
|
#include "fmt.hh"
|
||||||
|
#include "logging.hh"
|
||||||
|
|
||||||
|
#include <boost/core/demangle.hpp>
|
||||||
|
#include <exception>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
// Darwin and FreeBSD stdenv do not define _GNU_SOURCE but do have _Unwind_Backtrace.
|
||||||
|
#if __APPLE__ || __FreeBSD__
|
||||||
|
# define BOOST_STACKTRACE_GNU_SOURCE_NOT_REQUIRED
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#include <boost/stacktrace/stacktrace.hpp>
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
# include <syslog.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
void logFatal(std::string const & s)
|
||||||
|
{
|
||||||
|
writeToStderr(s + "\n");
|
||||||
|
// std::string for guaranteed null termination
|
||||||
|
#ifndef _WIN32
|
||||||
|
syslog(LOG_CRIT, "%s", s.c_str());
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
void onTerminate()
|
||||||
|
{
|
||||||
|
logFatal(
|
||||||
|
"Nix crashed. This is a bug. Please report this at https://github.com/NixOS/nix/issues with the following information included:\n");
|
||||||
|
try {
|
||||||
|
std::exception_ptr eptr = std::current_exception();
|
||||||
|
if (eptr) {
|
||||||
|
std::rethrow_exception(eptr);
|
||||||
|
} else {
|
||||||
|
logFatal("std::terminate() called without exception");
|
||||||
|
}
|
||||||
|
} catch (const std::exception & ex) {
|
||||||
|
logFatal(fmt("Exception: %s: %s", boost::core::demangle(typeid(ex).name()), ex.what()));
|
||||||
|
} catch (...) {
|
||||||
|
logFatal("Unknown exception!");
|
||||||
|
}
|
||||||
|
|
||||||
|
logFatal("Stack trace:");
|
||||||
|
std::stringstream ss;
|
||||||
|
ss << boost::stacktrace::stacktrace();
|
||||||
|
logFatal(ss.str());
|
||||||
|
|
||||||
|
std::abort();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void registerCrashHandler()
|
||||||
|
{
|
||||||
|
// DO NOT use this for signals. Boost stacktrace is very much not
|
||||||
|
// async-signal-safe, and in a world with ASLR, addr2line is pointless.
|
||||||
|
//
|
||||||
|
// If you want signals, set up a minidump system and do it out-of-process.
|
||||||
|
std::set_terminate(onTerminate);
|
||||||
|
}
|
||||||
|
}
|
11
src/nix/crash-handler.hh
Normal file
11
src/nix/crash-handler.hh
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#pragma once
|
||||||
|
/// @file Crash handler for Nix that prints back traces (hopefully in instances where it is not just going to crash the
|
||||||
|
/// process itself).
|
||||||
|
|
||||||
|
namespace nix {
|
||||||
|
|
||||||
|
/** Registers the Nix crash handler for std::terminate (currently; will support more crashes later). See also
|
||||||
|
* detectStackOverflow(). */
|
||||||
|
void registerCrashHandler();
|
||||||
|
|
||||||
|
}
|
|
@ -20,6 +20,7 @@
|
||||||
#include "flake/flake.hh"
|
#include "flake/flake.hh"
|
||||||
#include "self-exe.hh"
|
#include "self-exe.hh"
|
||||||
#include "json-utils.hh"
|
#include "json-utils.hh"
|
||||||
|
#include "crash-handler.hh"
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
@ -354,6 +355,8 @@ void mainWrapped(int argc, char * * argv)
|
||||||
{
|
{
|
||||||
savedArgv = argv;
|
savedArgv = argv;
|
||||||
|
|
||||||
|
registerCrashHandler();
|
||||||
|
|
||||||
/* The chroot helper needs to be run before any threads have been
|
/* The chroot helper needs to be run before any threads have been
|
||||||
started. */
|
started. */
|
||||||
#ifndef _WIN32
|
#ifndef _WIN32
|
||||||
|
|
|
@ -77,6 +77,7 @@ nix_sources = [config_h] + files(
|
||||||
'config-check.cc',
|
'config-check.cc',
|
||||||
'config.cc',
|
'config.cc',
|
||||||
'copy.cc',
|
'copy.cc',
|
||||||
|
'crash-handler.cc',
|
||||||
'derivation-add.cc',
|
'derivation-add.cc',
|
||||||
'derivation-show.cc',
|
'derivation-show.cc',
|
||||||
'derivation.cc',
|
'derivation.cc',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue