2019-05-17 12:26:08 +10:00
|
|
|
/*
|
|
|
|
* 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 2010-2019 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "./system.hpp"
|
|
|
|
|
2021-04-14 10:23:33 +10:00
|
|
|
#include "./assert.hpp"
|
|
|
|
#include "./debugger.hpp"
|
|
|
|
#include "./crash.hpp"
|
2019-05-17 12:26:08 +10:00
|
|
|
|
2024-05-29 16:29:08 +10:00
|
|
|
#include <cruft/util/except.hpp>
|
|
|
|
|
2019-05-17 12:26:08 +10:00
|
|
|
#include "../backtrace.hpp"
|
|
|
|
#include "../log.hpp"
|
|
|
|
|
|
|
|
#include <cstdlib>
|
2023-10-11 11:54:33 +11:00
|
|
|
#include <exception>
|
2019-05-17 12:26:08 +10:00
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2019-07-02 17:34:39 +10:00
|
|
|
static std::terminate_handler old_handler = nullptr;
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2019-05-17 12:26:08 +10:00
|
|
|
static void abort_with_trace (void)
|
|
|
|
{
|
2019-07-02 17:34:39 +10:00
|
|
|
// We used to trigger a breakpoint so that our debugger didn't just let the
|
|
|
|
// child terminate (ie, the behaviour of CLion/Win32/GDB).
|
|
|
|
//
|
|
|
|
// But this might silently terminate the program instead of allowing us the
|
|
|
|
// chance to dump a backtrace depending on the precise mechanism for
|
|
|
|
// `breakpoint`; in particular SIGTRAP and SIGINT trigger this.
|
|
|
|
#if 0
|
2019-05-17 12:26:08 +10:00
|
|
|
breakpoint ();
|
2019-07-02 17:34:39 +10:00
|
|
|
#endif
|
2019-05-17 12:26:08 +10:00
|
|
|
|
2019-07-02 17:34:39 +10:00
|
|
|
// If we're here because of an exception we may as well rethrow and hope
|
|
|
|
// that the system will print exception data during the abort process.
|
2019-05-17 12:26:08 +10:00
|
|
|
//
|
|
|
|
// But we can at least try to print anything derived from std::exception
|
|
|
|
// (which is a likely guess) before we self-destruct.
|
|
|
|
if (auto ptr = std::current_exception (); ptr) {
|
|
|
|
try {
|
|
|
|
std::rethrow_exception (ptr);
|
|
|
|
} catch (std::exception const &x) {
|
2021-04-14 10:23:33 +10:00
|
|
|
LOG_EMERGENCY ("unhandled exception: {}\n{}", x.what (), ::cruft::backtrace {});
|
2019-07-02 17:34:39 +10:00
|
|
|
} catch (cruft::error const &x) {
|
2021-04-14 10:23:33 +10:00
|
|
|
LOG_EMERGENCY ("unhandled exception: {}\n{}", x, ::cruft::backtrace {});
|
2019-05-17 12:26:08 +10:00
|
|
|
} catch (...) {
|
2021-04-14 10:23:33 +10:00
|
|
|
LOG_EMERGENCY ("unhandled exception\n{}", ::cruft::backtrace {});
|
2019-05-17 12:26:08 +10:00
|
|
|
}
|
|
|
|
} else {
|
2021-04-14 10:23:33 +10:00
|
|
|
LOG_EMERGENCY ("aborting: {}", ::cruft::backtrace {});
|
2019-05-17 12:26:08 +10:00
|
|
|
}
|
|
|
|
|
2019-07-02 17:34:39 +10:00
|
|
|
old_handler ();
|
2019-05-17 12:26:08 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
void
|
2019-07-02 17:34:39 +10:00
|
|
|
cruft::debug::init (void)
|
2019-05-17 12:26:08 +10:00
|
|
|
{
|
2019-10-10 17:32:46 +11:00
|
|
|
init_crash_handler ();
|
|
|
|
|
2019-07-02 17:34:39 +10:00
|
|
|
old_handler = std::set_terminate (abort_with_trace);
|
|
|
|
CHECK_NEQ (
|
|
|
|
reinterpret_cast<void*> (old_handler),
|
|
|
|
reinterpret_cast<void*> (abort_with_trace)
|
|
|
|
);
|
2019-05-17 12:26:08 +10:00
|
|
|
|
|
|
|
if (!debug_enabled && !getenv ("DEBUG"))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LOG_DEBUG ("setting debug environment");
|
2021-05-18 08:39:44 +10:00
|
|
|
enable_fpe ();
|
2019-05-17 12:26:08 +10:00
|
|
|
force_console ();
|
|
|
|
prepare_debugger ();
|
|
|
|
|
|
|
|
if (getenv ("DEBUG_WAIT"))
|
|
|
|
await_debugger ();
|
|
|
|
}
|