Path: blob/master/platform/linuxbsd/crash_handler_linuxbsd.cpp
10277 views
/**************************************************************************/1/* crash_handler_linuxbsd.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "crash_handler_linuxbsd.h"3132#include "core/config/project_settings.h"33#include "core/object/script_language.h"34#include "core/os/os.h"35#include "core/string/print_string.h"36#include "core/version.h"37#include "main/main.h"3839#ifndef DEBUG_ENABLED40#undef CRASH_HANDLER_ENABLED41#endif4243#ifdef CRASH_HANDLER_ENABLED44#include <cxxabi.h>45#include <dlfcn.h>46#include <execinfo.h>47#include <link.h>48#include <csignal>49#include <cstdlib>5051static void handle_crash(int sig) {52signal(SIGSEGV, SIG_DFL);53signal(SIGFPE, SIG_DFL);54signal(SIGILL, SIG_DFL);5556if (OS::get_singleton() == nullptr) {57abort();58}5960if (OS::get_singleton()->is_crash_handler_silent()) {61std::_Exit(0);62}6364void *bt_buffer[256];65size_t size = backtrace(bt_buffer, 256);66String _execpath = OS::get_singleton()->get_executable_path();6768String msg;69if (ProjectSettings::get_singleton()) {70msg = GLOBAL_GET("debug/settings/crash_handler/message");71}7273// Tell MainLoop about the crash. This can be handled by users too in Node.74if (OS::get_singleton()->get_main_loop()) {75OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);76}7778// Dump the backtrace to stderr with a message to the user79print_error("\n================================================================");80print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));8182// Print the engine version just before, so that people are reminded to include the version in backtrace reports.83if (String(GODOT_VERSION_HASH).is_empty()) {84print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));85} else {86print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));87}88print_error(vformat("Dumping the backtrace. %s", msg));89char **strings = backtrace_symbols(bt_buffer, size);90// PIE executable relocation, zero for non-PIE executables91#ifdef __GLIBC__92// This is a glibc only thing apparently.93uintptr_t relocation = _r_debug.r_map->l_addr;94#else95// Non glibc systems apparently don't give PIE relocation info.96uintptr_t relocation = 0;97#endif //__GLIBC__98if (strings) {99List<String> args;100for (size_t i = 0; i < size; i++) {101char str[1024];102snprintf(str, 1024, "%p", (void *)((uintptr_t)bt_buffer[i] - relocation));103args.push_back(str);104}105args.push_back("-e");106args.push_back(_execpath);107108// Try to get the file/line number using addr2line109int ret;110String output = "";111Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);112Vector<String> addr2line_results;113if (err == OK) {114addr2line_results = output.substr(0, output.length() - 1).split("\n", false);115}116117for (size_t i = 1; i < size; i++) {118char fname[1024];119Dl_info info;120121snprintf(fname, 1024, "%s", strings[i]);122123// Try to demangle the function name to provide a more readable one124if (dladdr(bt_buffer[i], &info) && info.dli_sname) {125if (info.dli_sname[0] == '_') {126int status = 0;127char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);128129if (status == 0 && demangled) {130snprintf(fname, 1024, "%s", demangled);131}132133if (demangled) {134free(demangled);135}136}137}138139// Simplify printed file paths to remove redundant `/./` sections (e.g. `/opt/godot/./core` -> `/opt/godot/core`).140print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, err == OK ? addr2line_results[i].replace("/./", "/") : ""));141}142143free(strings);144}145print_error("-- END OF C++ BACKTRACE --");146print_error("================================================================");147148for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {149if (!backtrace->is_empty()) {150print_error(backtrace->format());151print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));152print_error("================================================================");153}154}155156// Abort to pass the error to the OS157abort();158}159#endif160161CrashHandler::CrashHandler() {162disabled = false;163}164165CrashHandler::~CrashHandler() {166disable();167}168169void CrashHandler::disable() {170if (disabled) {171return;172}173174#ifdef CRASH_HANDLER_ENABLED175signal(SIGSEGV, SIG_DFL);176signal(SIGFPE, SIG_DFL);177signal(SIGILL, SIG_DFL);178#endif179180disabled = true;181}182183void CrashHandler::initialize() {184#ifdef CRASH_HANDLER_ENABLED185signal(SIGSEGV, handle_crash);186signal(SIGFPE, handle_crash);187signal(SIGILL, handle_crash);188#endif189}190191192