Files
shinobu/platform/linuxbsd/crash_handler_linuxbsd.cpp
Ed Lu a2e061936f Added Breakpad crash dump generation
Fixed compiling on Windows

Make crash dump printing work better on Windows

Tweaked Breakpad build

Fixed Breakpad build for Godot 4

Switched to WINDOWS_ENABLED

Removed some non buildable files listed in the files to build

Ran clang format

Fixed the other formatting issues detected by CI

Removed a comment and added clarifying comment on crash dump message

as to why it is printed twice on Windows

Make an ugly string conversion to make Windows build work

Tweaked the build configuration and formatted again

removed lss

Add lss properly

Reinitialize breakpad after mono initialization on Linux

otherwise the breakpad signal handlers are not active

Disable Windows crash handler on destruction similarly to Linux

Renamed breakpad_enabled to use_breakpad

Forgot to wrap one piece of code inside ifdef USE_BREAKPAD

Updated copyright years in the added files

Fix register types for breakpad

Fixed dir access

Removed musl and elf_reader

which was the only thing seemingly depending on it

Updated header guards

Removed the memdelete call
2024-07-20 19:10:14 +02:00

197 lines
6.3 KiB
C++

/**************************************************************************/
/* crash_handler_linuxbsd.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "crash_handler_linuxbsd.h"
#include "core/config/project_settings.h"
#include "core/os/os.h"
#include "core/string/print_string.h"
#include "core/version.h"
#include "main/main.h"
#ifndef DEBUG_ENABLED
#undef CRASH_HANDLER_ENABLED
#endif
#ifdef USE_BREAKPAD
#include "modules/breakpad/breakpad.h"
#endif
#ifdef CRASH_HANDLER_ENABLED
#include <cxxabi.h>
#include <dlfcn.h>
#include <execinfo.h>
#include <link.h>
#include <signal.h>
#include <stdlib.h>
static void handle_crash(int sig) {
#ifdef USE_BREAKPAD
breakpad_handle_signal(sig);
#endif
signal(SIGSEGV, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGILL, SIG_DFL);
if (OS::get_singleton() == nullptr) {
abort();
}
void *bt_buffer[256];
size_t size = backtrace(bt_buffer, 256);
String _execpath = OS::get_singleton()->get_executable_path();
String msg;
const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
if (proj_settings) {
msg = proj_settings->get("debug/settings/crash_handler/message");
}
// Tell MainLoop about the crash. This can be handled by users too in Node.
if (OS::get_singleton()->get_main_loop()) {
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
}
// Dump the backtrace to stderr with a message to the user
print_error("\n================================================================");
print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));
// Print the engine version just before, so that people are reminded to include the version in backtrace reports.
if (String(VERSION_HASH).is_empty()) {
print_error(vformat("Engine version: %s", VERSION_FULL_NAME));
} else {
print_error(vformat("Engine version: %s (%s)", VERSION_FULL_NAME, VERSION_HASH));
}
print_error(vformat("Dumping the backtrace. %s", msg));
char **strings = backtrace_symbols(bt_buffer, size);
// PIE executable relocation, zero for non-PIE executables
#ifdef __GLIBC__
// This is a glibc only thing apparently.
uintptr_t relocation = _r_debug.r_map->l_addr;
#else
// Non glibc systems apparently don't give PIE relocation info.
uintptr_t relocation = 0;
#endif //__GLIBC__
if (strings) {
List<String> args;
for (size_t i = 0; i < size; i++) {
char str[1024];
snprintf(str, 1024, "%p", (void *)((uintptr_t)bt_buffer[i] - relocation));
args.push_back(str);
}
args.push_back("-e");
args.push_back(_execpath);
// Try to get the file/line number using addr2line
int ret;
String output = "";
Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);
Vector<String> addr2line_results;
if (err == OK) {
addr2line_results = output.substr(0, output.length() - 1).split("\n", false);
}
for (size_t i = 1; i < size; i++) {
char fname[1024];
Dl_info info;
snprintf(fname, 1024, "%s", strings[i]);
// Try to demangle the function name to provide a more readable one
if (dladdr(bt_buffer[i], &info) && info.dli_sname) {
if (info.dli_sname[0] == '_') {
int status = 0;
char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
if (status == 0 && demangled) {
snprintf(fname, 1024, "%s", demangled);
}
if (demangled) {
free(demangled);
}
}
}
print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, err == OK ? addr2line_results[i] : ""));
}
free(strings);
}
print_error("-- END OF BACKTRACE --");
print_error("================================================================");
// Abort to pass the error to the OS
abort();
}
#endif
CrashHandler::CrashHandler() {
disabled = false;
}
CrashHandler::~CrashHandler() {
disable();
}
void CrashHandler::disable() {
if (disabled) {
return;
}
#ifdef CRASH_HANDLER_ENABLED
signal(SIGSEGV, SIG_DFL);
signal(SIGFPE, SIG_DFL);
signal(SIGILL, SIG_DFL);
#endif
#ifdef USE_BREAKPAD
disable_breakpad();
#endif
disabled = true;
}
void CrashHandler::initialize() {
#ifdef CRASH_HANDLER_ENABLED
signal(SIGSEGV, handle_crash);
signal(SIGFPE, handle_crash);
signal(SIGILL, handle_crash);
#ifdef USE_BREAKPAD
initialize_breakpad(false);
#endif
#elif defined(USE_BREAKPAD)
initialize_breakpad(true);
#endif
}