123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- #ifdef DEBUG_ENABLED
- #define CRASH_HANDLER_ENABLED 1
- #endif
- #include "crash_handler_x11.h"
- #include "core/os/os.h"
- #include "core/project_settings.h"
- #include "main/main.h"
- #ifdef CRASH_HANDLER_ENABLED
- #include <cxxabi.h>
- #include <dlfcn.h>
- #include <execinfo.h>
- #include <signal.h>
- #include <stdlib.h>
- static void handle_crash(int sig) {
- if (OS::get_singleton() == NULL) {
- abort();
- }
- 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/crash_handler/message");
-
- fprintf(stderr, "%s: Program crashed with signal %d\n", __FUNCTION__, sig);
- if (OS::get_singleton()->get_main_loop())
- OS::get_singleton()->get_main_loop()->notification(MainLoop::NOTIFICATION_CRASH);
- 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]);
-
- 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 = "";
-
- 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();
- }
- #endif
- CrashHandler::CrashHandler() {
- disabled = false;
- }
- CrashHandler::~CrashHandler() {
- disable();
- }
- 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
- }
|