2023-06-01 18:35:41 +02:00
|
|
|
#include <hex/api/project_file_manager.hpp>
|
2023-11-18 14:50:43 +01:00
|
|
|
#include <hex/api/task_manager.hpp>
|
2023-12-11 15:54:22 +01:00
|
|
|
#include <hex/api/workspace_manager.hpp>
|
2023-06-20 11:55:56 +02:00
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
#include <init/tasks.hpp>
|
|
|
|
|
2023-06-01 18:35:41 +02:00
|
|
|
#include <hex/helpers/logger.hpp>
|
|
|
|
#include <hex/helpers/fs.hpp>
|
|
|
|
|
|
|
|
#include <wolv/utils/string.hpp>
|
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
#include <window.hpp>
|
2023-06-01 18:35:41 +02:00
|
|
|
|
|
|
|
#include <nlohmann/json.hpp>
|
|
|
|
|
2024-01-04 00:37:56 +01:00
|
|
|
#include <stacktrace.hpp>
|
2023-06-01 18:35:41 +02:00
|
|
|
#include <llvm/Demangle/Demangle.h>
|
|
|
|
|
|
|
|
#include <csignal>
|
2023-08-25 23:11:05 +02:00
|
|
|
#include <exception>
|
|
|
|
#include <typeinfo>
|
2023-06-01 18:35:41 +02:00
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
#if defined (OS_MACOS)
|
|
|
|
#include <sys/utsname.h>
|
|
|
|
#endif
|
|
|
|
|
2023-06-01 18:35:41 +02:00
|
|
|
namespace hex::crash {
|
|
|
|
|
|
|
|
constexpr static auto CrashBackupFileName = "crash_backup.hexproj";
|
2023-07-22 20:16:36 +02:00
|
|
|
constexpr static auto Signals = { SIGSEGV, SIGILL, SIGABRT, SIGFPE };
|
2023-06-01 18:35:41 +02:00
|
|
|
|
2023-07-01 12:32:28 +02:00
|
|
|
void resetCrashHandlers();
|
2023-06-01 18:35:41 +02:00
|
|
|
|
|
|
|
static void sendNativeMessage(const std::string& message) {
|
2023-10-21 20:40:24 +02:00
|
|
|
hex::nativeErrorMessage(hex::format("ImHex crashed during initial setup!\nError: {}", message));
|
2023-06-01 18:35:41 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 12:16:37 +02:00
|
|
|
// Function that decides what should happen on a crash
|
|
|
|
// (either sending a message or saving a crash file, depending on when the crash occurred)
|
2023-08-25 23:19:13 +02:00
|
|
|
using CrashCallback = void (*) (const std::string&);
|
|
|
|
static CrashCallback crashCallback = sendNativeMessage;
|
2023-06-01 18:35:41 +02:00
|
|
|
|
|
|
|
static void saveCrashFile(const std::string& message) {
|
2023-06-13 23:31:49 +02:00
|
|
|
log::fatal(message);
|
2023-06-01 18:35:41 +02:00
|
|
|
|
2023-08-26 23:31:25 +02:00
|
|
|
nlohmann::json crashData {
|
2024-05-08 21:30:20 +02:00
|
|
|
{ "logFile", wolv::io::fs::toNormalizedPathString(hex::log::impl::getFile().getPath()) },
|
|
|
|
{ "project", wolv::io::fs::toNormalizedPathString(ProjectFile::getPath()) },
|
2023-06-01 18:35:41 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
2023-06-11 13:10:04 +02:00
|
|
|
wolv::io::File file(path / "crash.json", wolv::io::File::Mode::Create);
|
2023-06-01 18:35:41 +02:00
|
|
|
if (file.isValid()) {
|
|
|
|
file.writeString(crashData.dump(4));
|
|
|
|
file.close();
|
|
|
|
log::info("Wrote crash.json file to {}", wolv::util::toUTF8String(file.getPath()));
|
2023-06-20 11:55:56 +02:00
|
|
|
|
2023-06-01 18:35:41 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2023-08-25 23:19:13 +02:00
|
|
|
|
|
|
|
log::warn("Could not write crash.json file!");
|
2023-06-01 18:35:41 +02:00
|
|
|
}
|
|
|
|
|
2023-06-20 11:55:56 +02:00
|
|
|
static void printStackTrace() {
|
|
|
|
for (const auto &stackFrame : stacktrace::getStackTrace()) {
|
|
|
|
if (stackFrame.line == 0)
|
2024-03-15 17:53:12 +01:00
|
|
|
log::fatal(" ({}) | {}", stackFrame.file, stackFrame.function);
|
2023-06-20 11:55:56 +02:00
|
|
|
else
|
|
|
|
log::fatal(" ({}:{}) | {}", stackFrame.file, stackFrame.line, stackFrame.function);
|
|
|
|
}
|
|
|
|
}
|
2023-06-01 18:35:41 +02:00
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
extern "C" void triggerSafeShutdown(int signalNumber = 0) {
|
|
|
|
// Trigger an event so that plugins can handle crashes
|
2023-12-08 10:29:44 +01:00
|
|
|
EventAbnormalTermination::post(signalNumber);
|
2023-08-25 22:53:33 +02:00
|
|
|
|
|
|
|
// Run exit tasks
|
2023-10-11 22:38:54 +02:00
|
|
|
init::runExitTasks();
|
2023-08-25 22:53:33 +02:00
|
|
|
|
|
|
|
// Terminate all asynchronous tasks
|
|
|
|
TaskManager::exit();
|
|
|
|
|
|
|
|
// Trigger a breakpoint if we're in a debug build or raise the signal again for the default handler to handle it
|
|
|
|
#if defined(DEBUG)
|
2023-08-25 23:51:51 +02:00
|
|
|
|
2023-12-20 13:38:13 +01:00
|
|
|
static bool breakpointTriggered = false;
|
|
|
|
if (!breakpointTriggered) {
|
|
|
|
breakpointTriggered = true;
|
|
|
|
#if defined(OS_WINDOWS)
|
|
|
|
__debugbreak();
|
|
|
|
#else
|
|
|
|
raise(SIGTRAP);
|
|
|
|
#endif
|
|
|
|
}
|
2023-12-20 10:50:58 +01:00
|
|
|
|
|
|
|
std::exit(signalNumber);
|
2023-08-25 23:51:51 +02:00
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
#else
|
2023-08-25 23:51:51 +02:00
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
if (signalNumber == 0)
|
|
|
|
std::abort();
|
|
|
|
else
|
|
|
|
std::exit(signalNumber);
|
2023-08-25 23:51:51 +02:00
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void handleCrash(const std::string &msg) {
|
2023-08-25 23:19:13 +02:00
|
|
|
// Call the crash callback
|
2023-07-01 12:32:28 +02:00
|
|
|
crashCallback(msg);
|
2023-06-11 21:55:48 +02:00
|
|
|
|
2023-08-25 23:19:13 +02:00
|
|
|
// Print the stacktrace to the console or log file
|
2023-06-20 11:55:56 +02:00
|
|
|
printStackTrace();
|
2023-08-25 22:53:33 +02:00
|
|
|
|
2023-08-25 23:19:13 +02:00
|
|
|
// Flush all streams
|
2023-08-25 22:53:33 +02:00
|
|
|
fflush(stdout);
|
|
|
|
fflush(stderr);
|
2023-07-01 12:32:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Custom signal handler to print various information and a stacktrace when the application crashes
|
|
|
|
static void signalHandler(int signalNumber, const std::string &signalName) {
|
2023-07-22 20:16:36 +02:00
|
|
|
// Reset crash handlers, so we can't have a recursion if this code crashes
|
2023-07-01 12:32:28 +02:00
|
|
|
resetCrashHandlers();
|
|
|
|
|
|
|
|
// Actually handle the crash
|
2023-08-25 22:53:33 +02:00
|
|
|
handleCrash(hex::format("Received signal '{}' ({})", signalName, signalNumber));
|
2023-06-01 18:35:41 +02:00
|
|
|
|
|
|
|
// Detect if the crash was due to an uncaught exception
|
|
|
|
if (std::uncaught_exceptions() > 0) {
|
|
|
|
log::fatal("Uncaught exception thrown!");
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
triggerSafeShutdown(signalNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void uncaughtExceptionHandler() {
|
|
|
|
// Reset crash handlers, so we can't have a recursion if this code crashes
|
|
|
|
resetCrashHandlers();
|
|
|
|
|
2023-08-25 23:19:13 +02:00
|
|
|
// Print the current exception info
|
2023-08-25 22:53:33 +02:00
|
|
|
try {
|
|
|
|
std::rethrow_exception(std::current_exception());
|
|
|
|
} catch (std::exception &ex) {
|
2023-11-10 20:47:08 +01:00
|
|
|
std::string exceptionStr = hex::format("{}()::what() -> {}", llvm::itaniumDemangle(typeid(ex).name()), ex.what());
|
2023-08-25 22:53:33 +02:00
|
|
|
|
2023-10-21 20:40:24 +02:00
|
|
|
handleCrash(exceptionStr);
|
|
|
|
log::fatal("Program terminated with uncaught exception: {}", exceptionStr);
|
2023-08-25 22:53:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
triggerSafeShutdown();
|
2023-06-01 18:35:41 +02:00
|
|
|
}
|
|
|
|
|
2023-06-08 12:16:37 +02:00
|
|
|
// Setup functions to handle signals, uncaught exception, or similar stuff that will crash ImHex
|
2023-06-01 18:35:41 +02:00
|
|
|
void setupCrashHandlers() {
|
2024-01-04 00:37:56 +01:00
|
|
|
stacktrace::initialize();
|
|
|
|
|
|
|
|
// Register signal handlers
|
2023-06-01 18:35:41 +02:00
|
|
|
{
|
2023-08-25 23:51:51 +02:00
|
|
|
#define HANDLE_SIGNAL(name) \
|
|
|
|
std::signal(name, [](int signalNumber) { \
|
|
|
|
signalHandler(signalNumber, #name); \
|
2023-06-01 18:35:41 +02:00
|
|
|
})
|
|
|
|
|
2023-08-25 23:51:51 +02:00
|
|
|
HANDLE_SIGNAL(SIGSEGV);
|
|
|
|
HANDLE_SIGNAL(SIGILL);
|
|
|
|
HANDLE_SIGNAL(SIGABRT);
|
|
|
|
HANDLE_SIGNAL(SIGFPE);
|
2023-06-01 18:35:41 +02:00
|
|
|
|
2024-01-09 11:49:49 +01:00
|
|
|
#if defined (SIGBUS)
|
|
|
|
HANDLE_SIGNAL(SIGBUS);
|
|
|
|
#endif
|
|
|
|
|
2023-06-01 18:35:41 +02:00
|
|
|
#undef HANDLE_SIGNAL
|
|
|
|
}
|
|
|
|
|
2023-08-25 22:53:33 +02:00
|
|
|
// Configure the uncaught exception handler
|
|
|
|
std::set_terminate(uncaughtExceptionHandler);
|
2023-06-01 18:35:41 +02:00
|
|
|
|
|
|
|
// Save a backup project when the application crashes
|
|
|
|
// We need to save the project no mater if it is dirty,
|
|
|
|
// because this save is responsible for telling us which files
|
|
|
|
// were opened in case there wasn't a project
|
2023-07-01 12:32:28 +02:00
|
|
|
// Only do it when ImHex has finished its loading
|
2023-12-08 10:29:44 +01:00
|
|
|
EventImHexStartupFinished::subscribe([] {
|
|
|
|
EventAbnormalTermination::subscribe([](int) {
|
2023-12-11 15:54:22 +01:00
|
|
|
WorkspaceManager::exportToFile();
|
2023-07-01 12:32:28 +02:00
|
|
|
|
2023-08-25 23:19:13 +02:00
|
|
|
// Create crash backup if any providers are open
|
2023-08-25 22:53:33 +02:00
|
|
|
if (ImHexApi::Provider::isValid()) {
|
|
|
|
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Config)) {
|
|
|
|
if (ProjectFile::store(path / CrashBackupFileName, false))
|
|
|
|
break;
|
|
|
|
}
|
2023-07-01 12:32:28 +02:00
|
|
|
}
|
|
|
|
});
|
2023-06-01 18:35:41 +02:00
|
|
|
});
|
|
|
|
|
2023-08-25 23:19:13 +02:00
|
|
|
// Change the crash callback when ImHex has finished startup
|
2023-12-08 10:29:44 +01:00
|
|
|
EventImHexStartupFinished::subscribe([]{
|
2023-06-01 18:35:41 +02:00
|
|
|
crashCallback = saveCrashFile;
|
|
|
|
});
|
|
|
|
}
|
2023-07-01 12:32:28 +02:00
|
|
|
|
|
|
|
void resetCrashHandlers() {
|
2023-08-25 22:53:33 +02:00
|
|
|
std::set_terminate(nullptr);
|
2023-07-01 12:32:28 +02:00
|
|
|
|
2023-08-25 23:19:13 +02:00
|
|
|
for (auto signal : Signals)
|
|
|
|
std::signal(signal, SIG_DFL);
|
2023-07-01 12:32:28 +02:00
|
|
|
}
|
2023-06-01 18:35:41 +02:00
|
|
|
}
|