2017-09-08 01:01:49 +00:00
|
|
|
/*************************************************************************/
|
2020-03-03 13:36:29 +00:00
|
|
|
/* crash_handler_linuxbsd.cpp */
|
2017-09-08 01:01:49 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/*************************************************************************/
|
2021-01-01 19:13:46 +00:00
|
|
|
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
|
2017-09-08 01:01:49 +00:00
|
|
|
/* */
|
|
|
|
/* 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. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2020-03-03 13:36:29 +00:00
|
|
|
#include "crash_handler_linuxbsd.h"
|
2019-02-12 14:43:54 +00:00
|
|
|
|
2020-11-07 22:33:38 +00:00
|
|
|
#include "core/config/project_settings.h"
|
2018-09-11 16:13:45 +00:00
|
|
|
#include "core/os/os.h"
|
2017-09-08 01:01:49 +00:00
|
|
|
#include "main/main.h"
|
|
|
|
|
2019-02-12 14:43:54 +00:00
|
|
|
#ifdef DEBUG_ENABLED
|
|
|
|
#define CRASH_HANDLER_ENABLED 1
|
|
|
|
#endif
|
|
|
|
|
2017-09-08 01:01:49 +00:00
|
|
|
#ifdef CRASH_HANDLER_ENABLED
|
|
|
|
#include <cxxabi.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <execinfo.h>
|
|
|
|
#include <signal.h>
|
2017-09-19 14:21:45 +00:00
|
|
|
#include <stdlib.h>
|
2017-09-08 01:01:49 +00:00
|
|
|
|
|
|
|
static void handle_crash(int sig) {
|
2020-04-01 23:20:12 +00:00
|
|
|
if (OS::get_singleton() == nullptr) {
|
2018-09-24 21:21:05 +00:00
|
|
|
abort();
|
|
|
|
}
|
2017-09-08 01:01:49 +00:00
|
|
|
|
|
|
|
void *bt_buffer[256];
|
|
|
|
size_t size = backtrace(bt_buffer, 256);
|
|
|
|
String _execpath = OS::get_singleton()->get_executable_path();
|
2019-05-23 18:57:10 +00:00
|
|
|
|
|
|
|
String msg;
|
|
|
|
const ProjectSettings *proj_settings = ProjectSettings::get_singleton();
|
|
|
|
if (proj_settings) {
|
|
|
|
msg = proj_settings->get("debug/settings/crash_handler/message");
|
|
|
|
}
|
2017-09-08 01:01:49 +00:00
|
|
|
|
|
|
|
// Dump the backtrace to stderr with a message to the user
|
|
|
|
fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);
|
2018-07-02 19:18:58 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (OS::get_singleton()->get_main_loop()) {
|
2018-07-02 19:18:58 +00:00
|
|
|
OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2018-07-02 19:18:58 +00:00
|
|
|
|
2020-07-27 10:43:20 +00:00
|
|
|
fprintf(stderr, "Dumping the backtrace. %s\n", msg.utf8().get_data());
|
2017-09-08 01:01:49 +00:00
|
|
|
char **strings = backtrace_symbols(bt_buffer, size);
|
|
|
|
if (strings) {
|
|
|
|
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;
|
2020-04-01 23:20:12 +00:00
|
|
|
char *demangled = abi::__cxa_demangle(info.dli_sname, nullptr, nullptr, &status);
|
2017-09-08 01:01:49 +00:00
|
|
|
|
|
|
|
if (status == 0 && demangled) {
|
|
|
|
snprintf(fname, 1024, "%s", demangled);
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (demangled) {
|
2017-09-08 01:01:49 +00:00
|
|
|
free(demangled);
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-09-08 01:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
List<String> args;
|
|
|
|
|
|
|
|
char str[1024];
|
|
|
|
snprintf(str, 1024, "%p", bt_buffer[i]);
|
|
|
|
args.push_back(str);
|
|
|
|
args.push_back("-e");
|
|
|
|
args.push_back(_execpath);
|
|
|
|
|
|
|
|
String output = "";
|
|
|
|
|
|
|
|
// Try to get the file/line number using addr2line
|
2019-04-04 20:00:16 +00:00
|
|
|
int ret;
|
2020-04-01 23:20:12 +00:00
|
|
|
Error err = OS::get_singleton()->execute(String("addr2line"), args, true, nullptr, &output, &ret);
|
2019-04-04 20:00:16 +00:00
|
|
|
if (err == OK) {
|
|
|
|
output.erase(output.length() - 1, 1);
|
2017-09-08 01:01:49 +00:00
|
|
|
}
|
|
|
|
|
2020-07-27 10:43:20 +00:00
|
|
|
fprintf(stderr, "[%ld] %s (%s)\n", (long int)i, fname, output.utf8().get_data());
|
2017-09-08 01:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
free(strings);
|
|
|
|
}
|
|
|
|
fprintf(stderr, "-- END OF BACKTRACE --\n");
|
|
|
|
|
|
|
|
// Abort to pass the error to the OS
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
CrashHandler::CrashHandler() {
|
|
|
|
disabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CrashHandler::~CrashHandler() {
|
2018-09-24 21:21:05 +00:00
|
|
|
disable();
|
2017-09-08 01:01:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void CrashHandler::disable() {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (disabled) {
|
2017-09-08 01:01:49 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2017-09-08 01:01:49 +00:00
|
|
|
|
|
|
|
#ifdef CRASH_HANDLER_ENABLED
|
2020-04-01 23:20:12 +00:00
|
|
|
signal(SIGSEGV, nullptr);
|
|
|
|
signal(SIGFPE, nullptr);
|
|
|
|
signal(SIGILL, nullptr);
|
2017-09-08 01:01:49 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
disabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CrashHandler::initialize() {
|
|
|
|
#ifdef CRASH_HANDLER_ENABLED
|
|
|
|
signal(SIGSEGV, handle_crash);
|
|
|
|
signal(SIGFPE, handle_crash);
|
|
|
|
signal(SIGILL, handle_crash);
|
|
|
|
#endif
|
|
|
|
}
|