Path: blob/master/platform/linuxbsd/crash_handler_linuxbsd.cpp
20831 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/io/file_access.h"34#include "core/object/script_language.h"35#include "core/os/main_loop.h"36#include "core/os/os.h"37#include "core/string/print_string.h"38#include "core/version.h"39#include "main/main.h"4041#ifndef DEBUG_ENABLED42#undef CRASH_HANDLER_ENABLED43#endif4445#ifdef CRASH_HANDLER_ENABLED46#include <cxxabi.h>47#include <dlfcn.h>48#include <execinfo.h>49#include <link.h>50#include <csignal>51#include <cstdlib>5253static void handle_crash(int sig) {54signal(SIGSEGV, SIG_DFL);55signal(SIGFPE, SIG_DFL);56signal(SIGILL, SIG_DFL);5758if (OS::get_singleton() == nullptr) {59abort();60}6162if (OS::get_singleton()->is_crash_handler_silent()) {63std::_Exit(0);64}6566void *bt_buffer[256];67size_t size = backtrace(bt_buffer, 256);68String _execpath = OS::get_singleton()->get_executable_path();6970if (FileAccess::exists(_execpath + ".debugsymbols")) {71_execpath = _execpath + ".debugsymbols";72}7374String msg;75if (ProjectSettings::get_singleton()) {76msg = GLOBAL_GET("debug/settings/crash_handler/message");77}7879// Tell MainLoop about the crash. This can be handled by users too in Node.80if (OS::get_singleton()->get_main_loop()) {81OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);82}8384// Dump the backtrace to stderr with a message to the user85print_error("\n================================================================");86print_error(vformat("%s: Program crashed with signal %d", __FUNCTION__, sig));8788// Print the engine version just before, so that people are reminded to include the version in backtrace reports.89if (String(GODOT_VERSION_HASH).is_empty()) {90print_error(vformat("Engine version: %s", GODOT_VERSION_FULL_NAME));91} else {92print_error(vformat("Engine version: %s (%s)", GODOT_VERSION_FULL_NAME, GODOT_VERSION_HASH));93}94print_error(vformat("Dumping the backtrace. %s", msg));95char **strings = backtrace_symbols(bt_buffer, size);96// PIE executable relocation, zero for non-PIE executables97#ifdef __GLIBC__98// This is a glibc only thing apparently.99uintptr_t relocation = _r_debug.r_map->l_addr;100#else101// Non glibc systems apparently don't give PIE relocation info.102uintptr_t relocation = 0;103#endif //__GLIBC__104if (strings) {105List<String> args;106for (size_t i = 0; i < size; i++) {107char str[1024];108snprintf(str, 1024, "%p", (void *)((uintptr_t)bt_buffer[i] - relocation));109args.push_back(str);110}111args.push_back("-e");112args.push_back(_execpath);113114// Try to get the file/line number using addr2line115int ret;116String output = "";117Error err = OS::get_singleton()->execute(String("addr2line"), args, &output, &ret);118Vector<String> addr2line_results;119if (err == OK) {120addr2line_results = output.substr(0, output.length() - 1).split("\n", false);121}122123for (size_t i = 1; i < size; i++) {124char fname[1024];125Dl_info info;126127snprintf(fname, 1024, "%s", strings[i]);128129// Try to demangle the function name to provide a more readable one130if (dladdr(bt_buffer[i], &info) && info.dli_sname) {131if (info.dli_sname[0] == '_') {132int status = 0;133char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);134135if (status == 0 && demangled) {136snprintf(fname, 1024, "%s", demangled);137}138139if (demangled) {140free(demangled);141}142}143}144145// Simplify printed file paths to remove redundant `/./` sections (e.g. `/opt/godot/./core` -> `/opt/godot/core`).146print_error(vformat("[%d] %s (%s)", (int64_t)i, fname, err == OK ? addr2line_results[i].replace("/./", "/") : ""));147}148149free(strings);150}151print_error("-- END OF C++ BACKTRACE --");152print_error("================================================================");153154for (const Ref<ScriptBacktrace> &backtrace : ScriptServer::capture_script_backtraces(false)) {155if (!backtrace->is_empty()) {156print_error(backtrace->format());157print_error(vformat("-- END OF %s BACKTRACE --", backtrace->get_language_name().to_upper()));158print_error("================================================================");159}160}161162// Abort to pass the error to the OS163abort();164}165#endif166167CrashHandler::CrashHandler() {168disabled = false;169}170171CrashHandler::~CrashHandler() {172disable();173}174175void CrashHandler::disable() {176if (disabled) {177return;178}179180#ifdef CRASH_HANDLER_ENABLED181signal(SIGSEGV, SIG_DFL);182signal(SIGFPE, SIG_DFL);183signal(SIGILL, SIG_DFL);184#endif185186disabled = true;187}188189void CrashHandler::initialize() {190#ifdef CRASH_HANDLER_ENABLED191signal(SIGSEGV, handle_crash);192signal(SIGFPE, handle_crash);193signal(SIGILL, handle_crash);194#endif195}196197198