crash: add a minimal handler for coredumping signals

This commit is contained in:
Danny Robson 2019-10-10 17:31:40 +11:00
parent c63bc5df4e
commit 0fb910fb5f
4 changed files with 92 additions and 0 deletions

View File

@ -124,6 +124,7 @@ if (NOT WIN32)
memory/system.cpp
memory/system.hpp
debug_posix.cpp
debug/crash_posix.cpp
io_posix.cpp
io_posix.hpp
library_posix.hpp
@ -145,6 +146,7 @@ if (WIN32)
list (
APPEND UTIL_FILES
debug_win32.cpp
debug/crash_win32.cpp
exe_win32.cpp
io_win32.cpp
io_win32.hpp
@ -303,6 +305,7 @@ list (
debug/assert.hpp
debug/compiler.cpp
debug/compiler.hpp
debug/crash.hpp
debug/debugger.cpp
debug/debugger.hpp
debug/memory.cpp

13
debug/crash.hpp Normal file
View File

@ -0,0 +1,13 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/
#pragma once
namespace cruft::debug {
void init_crash_handler (void);
}

68
debug/crash_posix.cpp Normal file
View File

@ -0,0 +1,68 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/
#include "crash.hpp"
#include "../backtrace.hpp"
#include "../posix/except.hpp"
#include "../map/fixed.hpp"
#include <signal.h>
#include <cstring>
///////////////////////////////////////////////////////////////////////////////
static constexpr
int to_handle[] = {
SIGBUS,
SIGFPE,
SIGILL,
SIGSEGV,
SIGABRT,
};
auto constexpr handled_count = std::size (to_handle);
cruft::map::fixed<handled_count, int, struct sigaction> s_old_handlers;
//-----------------------------------------------------------------------------
void dumping_handler (int sig, siginfo_t *info, void *ucontext)
{
std::clog << cruft::backtrace () << '\n';
// Forward the signal to the previously registered handler
auto const next_handler = s_old_handlers.at (sig);
if (next_handler.sa_flags & SA_SIGINFO)
next_handler.sa_sigaction (sig, info, ucontext);
else
next_handler.sa_handler (sig);
}
///////////////////////////////////////////////////////////////////////////////
void
cruft::debug::init_crash_handler ()
{
struct sigaction new_handler {};
memset (&new_handler, 0, sizeof (new_handler));
new_handler.sa_flags = SA_SIGINFO;
new_handler.sa_sigaction = dumping_handler;
for (auto const sigid: to_handle) {
cruft::posix::error::try_code (
sigaction (
sigid,
&new_handler,
&s_old_handlers[sigid]
)
);
}
}

8
debug/crash_win32.cpp Normal file
View File

@ -0,0 +1,8 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* Copyright 2019, Danny Robson <danny@nerdcruft.net>
*/