diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a1b37f..58488fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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") diff --git a/config.hpp.in b/config.hpp.in index d478613..9f4a896 100644 --- a/config.hpp.in +++ b/config.hpp.in @@ -33,5 +33,7 @@ SOFTWARE. #cmakedefine WINDOW_WAYLAND #cmakedefine WINDOW_SERENITYOS +#cmakedefine ENABLE_BACKTRACE_PRINTING + #cmakedefine HAVE_STRSIGNAL #define POLYGUN_VERSION "git" diff --git a/docs/INSTALL.MD b/docs/INSTALL.MD index 60e782a..f585d8e 100644 --- a/docs/INSTALL.MD +++ b/docs/INSTALL.MD @@ -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. diff --git a/src/common/backtrace_handler.cpp b/src/common/backtrace_handler.cpp new file mode 100644 index 0000000..c6365cb --- /dev/null +++ b/src/common/backtrace_handler.cpp @@ -0,0 +1,49 @@ +/* +PolyGun + +Copyright (c) 2023 mrkubax10 + +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 +#include +#if defined(__GNUC__) +#include +#include +#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 :/"< + +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 diff --git a/src/main.cpp b/src/main.cpp index b3695e3..a0d68a2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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");