diff options
author | Marcelo Fernandez <marcelofg55@gmail.com> | 2017-09-07 22:01:49 -0300 |
---|---|---|
committer | Marcelo Fernandez <marcelofg55@gmail.com> | 2017-09-13 10:07:23 -0300 |
commit | 83fe9373621ab9f7e175a43868b2eda935107539 (patch) | |
tree | 116a92b2c32713b4f3f907b451e457a9a623d50a /platform/x11 | |
parent | 06138bf18c25a8c5f7e2e5bb1ebd9dcf0b2f28dd (diff) |
Added a crash handler to dump the backtrace on Windows, Linux and OS X
Diffstat (limited to 'platform/x11')
-rw-r--r-- | platform/x11/SCsub | 13 | ||||
-rw-r--r-- | platform/x11/crash_handler_x11.cpp | 135 | ||||
-rw-r--r-- | platform/x11/crash_handler_x11.h | 47 | ||||
-rw-r--r-- | platform/x11/detect.py | 1 | ||||
-rw-r--r-- | platform/x11/os_x11.cpp | 15 | ||||
-rw-r--r-- | platform/x11/os_x11.h | 7 |
6 files changed, 212 insertions, 6 deletions
diff --git a/platform/x11/SCsub b/platform/x11/SCsub index fc9208c563..62717f3221 100644 --- a/platform/x11/SCsub +++ b/platform/x11/SCsub @@ -3,12 +3,13 @@ Import('env') -common_x11 = [\ - "context_gl_x11.cpp",\ - "os_x11.cpp",\ - "key_mapping_x11.cpp",\ - "joypad_linux.cpp",\ - "power_x11.cpp",\ +common_x11 = [ + "context_gl_x11.cpp", + "crash_handler_x11.cpp", + "os_x11.cpp", + "key_mapping_x11.cpp", + "joypad_linux.cpp", + "power_x11.cpp", ] env.Program('#bin/godot', ['godot_x11.cpp'] + common_x11) diff --git a/platform/x11/crash_handler_x11.cpp b/platform/x11/crash_handler_x11.cpp new file mode 100644 index 0000000000..c926b7799d --- /dev/null +++ b/platform/x11/crash_handler_x11.cpp @@ -0,0 +1,135 @@ +/*************************************************************************/ +/* crash_handler_x11.cpp */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifdef DEBUG_ENABLED +#define CRASH_HANDLER_ENABLED 1 +#endif + +#include "main/main.h" +#include "os_x11.h" + +#ifdef CRASH_HANDLER_ENABLED +#include <cxxabi.h> +#include <dlfcn.h> +#include <execinfo.h> +#include <signal.h> + +static void handle_crash(int sig) { + if (OS::get_singleton() == NULL) + return; + + void *bt_buffer[256]; + size_t size = backtrace(bt_buffer, 256); + String _execpath = OS::get_singleton()->get_executable_path(); + String msg = GLOBAL_GET("debug/settings/backtrace/message"); + + // Dump the backtrace to stderr with a message to the user + fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig); + fprintf(stderr, "Dumping the backtrace. %ls\n", msg.c_str()); + 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; + char *demangled = abi::__cxa_demangle(info.dli_sname, NULL, 0, &status); + + if (status == 0 && demangled) { + snprintf(fname, 1024, "%s", demangled); + } + + if (demangled) + free(demangled); + } + } + + 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 + if (OS::get_singleton()) { + int ret; + Error err = OS::get_singleton()->execute(String("addr2line"), args, true, NULL, &output, &ret); + if (err == OK) { + output.erase(output.length() - 1, 1); + } + } + + fprintf(stderr, "[%ld] %s (%ls)\n", i, fname, output.c_str()); + } + + free(strings); + } + fprintf(stderr, "-- END OF BACKTRACE --\n"); + + // Abort to pass the error to the OS + abort(); +} +#endif + +CrashHandler::CrashHandler() { + disabled = false; +} + +CrashHandler::~CrashHandler() { +} + +void CrashHandler::disable() { + if (disabled) + return; + +#ifdef CRASH_HANDLER_ENABLED + signal(SIGSEGV, NULL); + signal(SIGFPE, NULL); + signal(SIGILL, NULL); +#endif + + disabled = true; +} + +void CrashHandler::initialize() { +#ifdef CRASH_HANDLER_ENABLED + signal(SIGSEGV, handle_crash); + signal(SIGFPE, handle_crash); + signal(SIGILL, handle_crash); +#endif +} diff --git a/platform/x11/crash_handler_x11.h b/platform/x11/crash_handler_x11.h new file mode 100644 index 0000000000..e01334cbf2 --- /dev/null +++ b/platform/x11/crash_handler_x11.h @@ -0,0 +1,47 @@ +/*************************************************************************/ +/* crash_handler_x11.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2017 Godot Engine contributors (cf. AUTHORS.md) */ +/* */ +/* 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. */ +/*************************************************************************/ +#ifndef CRASH_HANDLER_X11_H +#define CRASH_HANDLER_X11_H + +class CrashHandler { + + bool disabled; + +public: + void initialize(); + + void disable(); + bool is_disabled() const { return disabled; }; + + CrashHandler(); + ~CrashHandler(); +}; + +#endif diff --git a/platform/x11/detect.py b/platform/x11/detect.py index 086681d4a4..d61175da60 100644 --- a/platform/x11/detect.py +++ b/platform/x11/detect.py @@ -87,6 +87,7 @@ def configure(env): elif (env["target"] == "debug"): env.Prepend(CCFLAGS=['-g2', '-DDEBUG_ENABLED', '-DDEBUG_MEMORY_ENABLED']) + env.Append(LINKFLAGS=['-rdynamic']) ## Architecture diff --git a/platform/x11/os_x11.cpp b/platform/x11/os_x11.cpp index 4dd85a558f..f61ccacb98 100644 --- a/platform/x11/os_x11.cpp +++ b/platform/x11/os_x11.cpp @@ -93,6 +93,13 @@ const char *OS_X11::get_audio_driver_name(int p_driver) const { return AudioDriverManager::get_driver(p_driver)->get_name(); } +void OS_X11::initialize_core() { + + crash_handler.initialize(); + + OS_Unix::initialize_core(); +} + void OS_X11::initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver) { long im_event_mask = 0; @@ -2172,6 +2179,14 @@ int OS_X11::get_power_percent_left() { return power_manager->get_power_percent_left(); } +void OS_X11::disable_crash_handler() { + crash_handler.disable(); +} + +bool OS_X11::is_disable_crash_handler() const { + return crash_handler.is_disabled(); +} + OS_X11::OS_X11() { #ifdef RTAUDIO_ENABLED diff --git a/platform/x11/os_x11.h b/platform/x11/os_x11.h index 51240fa023..14c737d45e 100644 --- a/platform/x11/os_x11.h +++ b/platform/x11/os_x11.h @@ -31,6 +31,7 @@ #define OS_X11_H #include "context_gl_x11.h" +#include "crash_handler_x11.h" #include "drivers/unix/os_unix.h" #include "os/input.h" #include "servers/visual_server.h" @@ -170,6 +171,8 @@ class OS_X11 : public OS_Unix { PowerX11 *power_manager; + CrashHandler crash_handler; + int audio_driver_index; unsigned int capture_idle; bool maximized; @@ -191,6 +194,7 @@ protected: virtual int get_audio_driver_count() const; virtual const char *get_audio_driver_name(int p_driver) const; + virtual void initialize_core(); virtual void initialize(const VideoMode &p_desired, int p_video_driver, int p_audio_driver); virtual void finalize(); @@ -273,6 +277,9 @@ public: void run(); + void disable_crash_handler(); + bool is_disable_crash_handler() const; + OS_X11(); }; |