Path: blob/master/platform/linuxbsd/crash_handler_linuxbsd.cpp
11351 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/main_loop.h"35#include "core/os/os.h"36#include "core/string/print_string.h"37#include "core/version.h"38#include "main/main.h"3940#ifndef DEBUG_ENABLED41#undef CRASH_HANDLER_ENABLED42#endif4344#ifdef CRASH_HANDLER_ENABLED45#include <cxxabi.h>46#include <dlfcn.h>47#include <execinfo.h>48#include <link.h>49#include <csignal>50#include <cstdlib>5152static void handle_crash(int sig) {53signal(SIGSEGV, SIG_DFL);54signal(SIGFPE, SIG_DFL);55signal(SIGILL, SIG_DFL);5657if (OS::get_singleton() == nullptr) {58abort();59}6061if (OS::get_singleton()->is_crash_handler_silent()) {62std::_Exit(0);63}6465void *bt_buffer[256];66size_t size = backtrace(bt_buffer, 256);67String _execpath = OS::get_singleton()->get_executable_path();6869String msg;70if (ProjectSettings::get_singleton()) {71msg = GLOBAL_GET("debug/settings/crash_handler/message");72}7374// Tell MainLoop about the crash. This can be handled by users too in Node.75if (OS::get_singleton()->get_main_loop()) {76OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);77}7879// Dump the backtrace to stderr with a message to the user80print_error("\n================================================================");81print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));8283// Print the engine version just before, so that people are reminded to include the version in backtrace reports.84if (String(GODOT_VERSION_HASH).is_empty()) {85print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));86} else {87print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));88}89print_error(vformat("Dumping the backtrace. %s", msg));90char **strings = backtrace_symbols(bt_buffer, size);91// PIE executable relocation, zero for non-PIE executables92#ifdef __GLIBC__93// This is a glibc only thing apparently.94uintptr_t relocation = _r_debug.r_map->l_addr;95#else96// Non glibc systems apparently don't give PIE relocation info.97uintptr_t relocation = 0;98#endif //__GLIBC__99if (strings) {100List<String> args;101for (size_t i = 0; i < size; i++) {102char str[1024];103snprintf(str, 1024, "%p", (void *)((uintptr_t)bt_buffer[i] - relocation));104args.push_back(str);105}106args.push_back("-e");107args.push_back(_execpath);108109// Try to get the file/line number using addr2line110int ret;111String output = "";112Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);113Vector<String> addr2line_results;114if (err == OK) {115addr2line_results = output.substr(0, output.length() - 1).split("\n", false);116}117118for (size_t i = 1; i < size; i++) {119char fname[1024];120Dl_info info;121122snprintf(fname, 1024, "%s", strings[i]);123124// Try to demangle the function name to provide a more readable one125if (dladdr(bt_buffer[i], &info) && info.dli_sname) {126if (info.dli_sname[0] == '_') {127int status = 0;128char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);129130if (status == 0 && demangled) {131snprintf(fname, 1024, "%s", demangled);132}133134if (demangled) {135free(demangled);136}137}138}139140// Simplify printed file paths to remove redundant `/./` sections (e.g. `/opt/godot/./core` -> `/opt/godot/core`).141print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, err == OK ? addr2line_results[i].replace("/./", "/") : ""));142}143144free(strings);145}146print_error("-- END OF C++ BACKTRACE --");147print_error("================================================================");148149for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {150if (!backtrace->is_empty()) {151print_error(backtrace->format());152print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));153print_error("================================================================");154}155}156157// Abort to pass the error to the OS158abort();159}160#endif161162CrashHandler::CrashHandler() {163disabled = false;164}165166CrashHandler::~CrashHandler() {167disable();168}169170void CrashHandler::disable() {171if (disabled) {172return;173}174175#ifdef CRASH_HANDLER_ENABLED176signal(SIGSEGV, SIG_DFL);177signal(SIGFPE, SIG_DFL);178signal(SIGILL, SIG_DFL);179#endif180181disabled = true;182}183184void CrashHandler::initialize() {185#ifdef CRASH_HANDLER_ENABLED186signal(SIGSEGV, handle_crash);187signal(SIGFPE, handle_crash);188signal(SIGILL, handle_crash);189#endif190}191192193