Implement crash handler

This commit is contained in:
mrkubax10 2023-08-30 21:31:57 +02:00
parent 2191b589aa
commit fc174d4b76
6 changed files with 104 additions and 1 deletions

View File

@ -39,7 +39,6 @@ if(NOT BUILD_FLAG_SUPPRESS_WARNING_MODE AND NOT MSVC)
set(DEFAULT_COMPILE_OPTIONS -Wall -Wpedantic -Werror)
endif()
include_directories(deps)
include_directories("${CMAKE_SOURCE_DIR}/src")
include_directories("${CMAKE_BINARY_DIR}")
@ -103,6 +102,15 @@ if(SERENITYOS)
endif()
option(WINDOW_SERENITYOS "Enable SerenityOS window creation" ${WINDOW_SERENITYOS_DEFAULT})
option(ENABLE_BACKTRACE_PRINTING "Enable backtrace printing" OFF)
if(ENABLE_BACKTRACE_PRINTING)
if(MSVC)
else()
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic")
endif()
endif()
if(BUILD_CLIENT)
set(CLIENT_SOURCES
src/game/audio/audio.cpp
@ -253,6 +261,9 @@ if(BUILD_TOOLS)
target_compile_options(extract_strings PRIVATE ${DEFAULT_COMPILE_OPTIONS})
endif()
file(GLOB_RECURSE COMMON_SOURCES "src/common/**.cpp")
if(NOT ENABLE_BACKTRACE_PRINTING)
list(REMOVE_ITEM COMMON_SOURCES ${CMAKE_SOURCE_DIR}/src/common/backtrace_handler.cpp)
endif()
if(BUILD_SERVER AND NOT BUILD_CLIENT)
set(PROJECT_NAME "polygun_server")

View File

@ -33,5 +33,7 @@ SOFTWARE.
#cmakedefine WINDOW_WAYLAND
#cmakedefine WINDOW_SERENITYOS
#cmakedefine ENABLE_BACKTRACE_PRINTING
#cmakedefine HAVE_STRSIGNAL
#define POLYGUN_VERSION "git"

View File

@ -17,6 +17,7 @@
- `WINDOW_X11`, if true window creation using X11 will be enabled. It's only valid on Unix-like systems and is detected there automatically.
- `WINDOW_WAYLAND`, if true window creation using Wayland will be enabled. It's only valid on Linux and is detected there automatically.
- `WINDOW_SERENITYOS` if true window creation on SerenityOS will be supported.
- `ENABLE_BACKTRACE_PRINTING` if true PolyGun will print backtrace after crash. It should be only used when building release executable, while developing use proper tools (GDB, AddressSanitizer etc).
- `HAVE_STRSIGNAL`, if true `strsignal` from C library will be used, otherwise PolyGun will provide it's own
implementation. Note that you shouldn't have to set this manually because it's automatically detected.
- `X11_INCLUDE_DIR`, only valid if `WINDOW_X11` was set to `TRUE`, it's path to X11 headers. It should point to folder with `X11` directory.

View File

@ -0,0 +1,49 @@
/*
PolyGun
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
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 "common/backtrace_handler.hpp"
#include <csignal>
#include <iostream>
#if defined(__GNUC__)
#include <execinfo.h>
#include <unistd.h>
#endif
void polygun::utils::backtrace_handler::initialize() {
signal(SIGSEGV, handler);
signal(SIGILL, handler);
signal(SIGFPE, handler);
}
void polygun::utils::backtrace_handler::handler(int signal) {
std::cout<<"PolyGun has crashed :/"<<std::endl;
std::cout<<"Backtrace: "<<std::endl;
#if defined(__GNUC__)
void* backtrace_symbols[1024];
size_t size = backtrace(backtrace_symbols, 1024);
backtrace_symbols_fd(backtrace_symbols, size, STDOUT_FILENO);
exit(1);
#endif
}

View File

@ -0,0 +1,33 @@
/*
PolyGun
Copyright (c) 2023 mrkubax10 <mrkubax10@onet.pl>
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 POLYGUN_UTILS_BACKTRACE_HANDLER_HPP
#define POLYGUN_UTILS_BACKTRACE_HANDLER_HPP
namespace polygun::utils::backtrace_handler {
void initialize();
void handler(int signal);
}
#endif // POLYGUN_UTILS_BACKTRACE_HANDLER_HPP

View File

@ -11,8 +11,15 @@
#include "common/command_arguments_parser.hpp"
#include "common/logger.hpp"
#include "common/locale/translation_manager.hpp"
#if defined(ENABLE_BACKTRACE_PRINTING)
#include "common/backtrace_handler.hpp"
#endif
int main(int argc, char** argv) {
#if defined(ENABLE_BACKTRACE_PRINTING)
polygun::utils::backtrace_handler::initialize();
#endif
polygun::utils::Logger::create(false, true);
polygun::locale::TranslationManager::create("locale");