diff options
author | Hein-Pieter van Braam <hp@tmm.cx> | 2017-09-13 18:05:31 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-13 18:05:31 +0200 |
commit | d6b392825fc3e22d9e0c62f0d17633c4224eb28f (patch) | |
tree | 5c9bc70e0a9ad094fb22398d9ba6e57bc09aba85 /platform/osx | |
parent | 322b0bee4967545383fba8a2c9b1253e2a2bbf43 (diff) | |
parent | 83fe9373621ab9f7e175a43868b2eda935107539 (diff) |
Merge pull request #11061 from marcelofg55/dump_backtrace
Add a segfault handler to dump the backtrace on Windows, Linux and OS X
Diffstat (limited to 'platform/osx')
-rw-r--r-- | platform/osx/SCsub | 7 | ||||
-rw-r--r-- | platform/osx/crash_handler_osx.h | 47 | ||||
-rw-r--r-- | platform/osx/crash_handler_osx.mm | 178 | ||||
-rw-r--r-- | platform/osx/godot_main_osx.mm | 1 | ||||
-rw-r--r-- | platform/osx/os_osx.h | 6 | ||||
-rw-r--r-- | platform/osx/os_osx.mm | 10 |
6 files changed, 247 insertions, 2 deletions
diff --git a/platform/osx/SCsub b/platform/osx/SCsub index 27117c2e8d..5b2de54535 100644 --- a/platform/osx/SCsub +++ b/platform/osx/SCsub @@ -3,6 +3,7 @@ Import('env') files = [ + 'crash_handler_osx.mm', 'os_osx.mm', 'godot_main_osx.mm', 'audio_driver_osx.cpp', @@ -12,4 +13,8 @@ files = [ 'power_osx.cpp', ] -env.Program('#bin/godot', files) +prog = env.Program('#bin/godot', files) +if (env['target'] == "debug" or env['target'] == "release_debug"): + # Build the .dSYM file for atos + action = "dsymutil " + File(prog)[0].path + " -o " + File(prog)[0].path + ".dSYM" + env.AddPostAction(prog, action) diff --git a/platform/osx/crash_handler_osx.h b/platform/osx/crash_handler_osx.h new file mode 100644 index 0000000000..ff037e6b7a --- /dev/null +++ b/platform/osx/crash_handler_osx.h @@ -0,0 +1,47 @@ +/*************************************************************************/ +/* crash_handler_osx.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_OSX_H +#define CRASH_HANDLER_OSX_H + +class CrashHandler { + + bool disabled; + +public: + void initialize(); + + void disable(); + bool is_disabled() const { return disabled; }; + + CrashHandler(); + ~CrashHandler(); +}; + +#endif diff --git a/platform/osx/crash_handler_osx.mm b/platform/osx/crash_handler_osx.mm new file mode 100644 index 0000000000..9239573734 --- /dev/null +++ b/platform/osx/crash_handler_osx.mm @@ -0,0 +1,178 @@ +/*************************************************************************/ +/* crash_handler_osx.mm */ +/*************************************************************************/ +/* 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. */ +/*************************************************************************/ +#include "main/main.h" +#include "os_osx.h" + +#include <string.h> +#include <unistd.h> + +// Note: Dump backtrace in 32bit mode is getting a bus error on the fgets by the ->execute, so enable only on 64bit +#if defined(DEBUG_ENABLED) && defined(__x86_64__) +#define CRASH_HANDLER_ENABLED 1 +#endif + +#ifdef CRASH_HANDLER_ENABLED +#include <cxxabi.h> +#include <dlfcn.h> +#include <execinfo.h> +#include <signal.h> + +#include <mach-o/dyld.h> +#include <mach-o/getsect.h> + +#ifdef __x86_64__ +static uint64_t load_address() { + const struct segment_command_64 *cmd = getsegbyname("__TEXT"); +#else +static uint32_t load_address() { + const struct segment_command *cmd = getsegbyname("__TEXT"); +#endif + char full_path[1024]; + uint32_t size = sizeof(full_path); + + if (cmd && !_NSGetExecutablePath(full_path, &size)) { + uint32_t dyld_count = _dyld_image_count(); + for (uint32_t i = 0; i < dyld_count; i++) { + const char *image_name = _dyld_get_image_name(i); + if (image_name && strncmp(image_name, full_path, 1024) == 0) { + return cmd->vmaddr + _dyld_get_image_vmaddr_slide(i); + } + } + } + + return 0; +} + +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) { + void *load_addr = (void *)load_address(); + + for (int 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); + } + } + + String output = fname; + + // Try to get the file/line number using atos + if (bt_buffer[i] > (void *)0x0 && OS::get_singleton()) { + List<String> args; + char str[1024]; + + args.push_back("-o"); + args.push_back(_execpath); + args.push_back("-arch"); +#ifdef __x86_64__ + args.push_back("x86_64"); +#else + args.push_back("i386"); +#endif + args.push_back("-l"); + snprintf(str, 1024, "%p", load_addr); + args.push_back(str); + snprintf(str, 1024, "%p", bt_buffer[i]); + args.push_back(str); + + int ret; + String out = ""; + Error err = OS::get_singleton()->execute(String("atos"), args, true, NULL, &out, &ret); + if (err == OK && out.substr(0, 2) != "0x") { + out.erase(out.length() - 1, 1); + output = out; + } + } + + fprintf(stderr, "[%d] %ls\n", i, 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/osx/godot_main_osx.mm b/platform/osx/godot_main_osx.mm index 84e58a7323..e2740fc666 100644 --- a/platform/osx/godot_main_osx.mm +++ b/platform/osx/godot_main_osx.mm @@ -35,7 +35,6 @@ #include <unistd.h> int main(int argc, char **argv) { - int first_arg = 1; const char *dbg_arg = "-NSDocumentRevisionsDebugMode"; printf("arguments\n"); diff --git a/platform/osx/os_osx.h b/platform/osx/os_osx.h index ee44d8c570..ca3e5745e9 100644 --- a/platform/osx/os_osx.h +++ b/platform/osx/os_osx.h @@ -30,6 +30,7 @@ #ifndef OS_OSX_H #define OS_OSX_H +#include "crash_handler_osx.h" #include "drivers/alsa/audio_driver_alsa.h" #include "drivers/rtaudio/audio_driver_rtaudio.h" #include "drivers/unix/os_unix.h" @@ -110,6 +111,8 @@ public: power_osx *power_manager; + CrashHandler crash_handler; + float _mouse_scale(float p_scale) { if (display_scale > 1.0) return p_scale; @@ -224,6 +227,9 @@ public: void set_mouse_mode(MouseMode p_mode); MouseMode get_mouse_mode() const; + void disable_crash_handler(); + bool is_disable_crash_handler() const; + OS_OSX(); }; diff --git a/platform/osx/os_osx.mm b/platform/osx/os_osx.mm index ffc497b780..beda6f4c38 100644 --- a/platform/osx/os_osx.mm +++ b/platform/osx/os_osx.mm @@ -910,6 +910,8 @@ OS::VideoMode OS_OSX::get_default_video_mode() const { void OS_OSX::initialize_core() { + crash_handler.initialize(); + OS_Unix::initialize_core(); DirAccess::make_default<DirAccessOSX>(DirAccess::ACCESS_RESOURCES); @@ -2022,3 +2024,11 @@ OS_OSX::OS_OSX() { bool OS_OSX::_check_internal_feature_support(const String &p_feature) { return p_feature == "pc" || p_feature == "s3tc"; } + +void OS_OSX::disable_crash_handler() { + crash_handler.disable(); +} + +bool OS_OSX::is_disable_crash_handler() const { + return crash_handler.is_disabled(); +} |