Skip to content

Implement stacktraces in Sys::Drop()/Error() #1741

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,9 @@ environment:
matrix:
# see 96d5c1f3ed77b09c64ce7c3c7cbd37c70456b3db
# for NMake template
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
generator: Visual Studio 16 2019
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2022
generator: Visual Studio 17 2022
platform: x64
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019
generator: Visual Studio 16 2019
platform: win32

build:
parallel: true
Expand All @@ -51,14 +48,15 @@ build_script:
:: /Wv pins warnings to a specific compiler version so that new ones
:: don't make the build error after Appveyor updates the compiler.

set CFLAGS=/Wv:19.29.30037
set CFLAGS=/Wv:19.34

set CXXFLAGS=/Wv:19.29.30037
set CXXFLAGS=/Wv:19.34

cmake
-Wdev -Wdeprecated
-G"%generator%" -A"%platform%"
-DUSE_PRECOMPILED_HEADER=0 -DUSE_WERROR=1 -DBE_VERBOSE=1
-DUSE_CPP23=1
-DBUILD_DUMMY_APP=1 -DBUILD_TESTS=1
-S. -Bbuild

Expand Down
18 changes: 13 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,20 @@ try_flag(WARNINGS "-W${WARNMODE}old-style-cast")
try_flag(WARNINGS "-Woverloaded-virtual")
try_flag(WARNINGS "-Wstrict-null-sentinel")
try_flag(WARNINGS "-W${WARNMODE}sign-compare")
try_flag(WARNINGS "-Wno-deprecated-enum-compare")
try_flag(WARNINGS "-Wno-deprecated-enum-compare-conditional")
try_flag(WARNINGS "-Wno-deprecated-enum-enum-conversion")
try_flag(WARNINGS "-Wno-deprecated-enum-float-conversion")
try_flag(WARNINGS "-Wno-nonnull")

# MSVC /wd = warning disable
try_flag(WARNINGS "/wd4127") # conditional expression is constant
try_flag(WARNINGS "/wd4324") # 'XXX': structure was padded due to alignment specifier
try_flag(WARNINGS "/wd4458") # declaration of 'XXX' hides class member
try_flag(WARNINGS "/wd4459") # declaration of 'XXX' hides global declaration
try_flag(WARNINGS "/wd4701") # potentially uninitialized local variable 'XXX' used
try_flag(WARNINGS "/wd5054") # operator '+': deprecated between enumerations of different types
try_flag(WARNINGS "/wd5055") # operator '==': deprecated between enumerations and floating-point types
try_flag(WARNINGS "/wd26495") # Variable 'XXX' is uninitialized. Always initialize a member variable.

################################################################################
Expand Down Expand Up @@ -857,7 +864,7 @@ endif()
################################################################################
macro(AddApplicationInternal Target Executable)
add_executable(${Target} ${Sources})
target_link_libraries(${Target} ${A_Target}-objects)
target_link_libraries(${Target} ${A_Target}-objects ${CPP23SupportLibrary})

if (DEPS_DIR)
add_dependencies(${Target} runtime_deps)
Expand All @@ -882,15 +889,16 @@ endmacro()

function(AddApplication)
set(oneValueArgs Target ExecutableName)
set(multiValueArgs ApplicationMain Definitions Flags Files Libs Tests)
set(multiValueArgs ApplicationMain Definitions Flags CompileFeatures Files Libs Tests)
cmake_parse_arguments(A "" "${oneValueArgs}" "${multiValueArgs}" ${ARGN})

# Reuse object files between the real application and the test one
add_library(${A_Target}-objects OBJECT EXCLUDE_FROM_ALL ${A_Files} ${PCH_FILE})
target_link_libraries(${A_Target}-objects engine-lib ${A_Libs} ${LIBS_BASE})
target_link_libraries(${A_Target}-objects engine-lib ${A_Libs} ${LIBS_BASE} ${CPP23SupportLibrary})
set_property(TARGET ${A_Target}-objects APPEND PROPERTY COMPILE_OPTIONS ${A_Flags})
set_property(TARGET ${A_Target}-objects APPEND PROPERTY INCLUDE_DIRECTORIES ${ENGINE_DIR} ${MOUNT_DIR} ${LIB_DIR})
set_property(TARGET ${A_Target}-objects APPEND PROPERTY COMPILE_DEFINITIONS ${A_Definitions})

set_target_properties(${A_Target}-objects PROPERTIES FOLDER "engine/objects")

set(Sources WIN32 ${A_ApplicationMain})
Expand All @@ -904,7 +912,7 @@ function(AddApplication)
# -Wl,--whole-archive -l<library path> -Wl,--no-whole-archive
set(Sources ${A_ApplicationMain} ${A_Tests})
AddApplicationInternal(test-${A_Target} test-${A_Target})
target_link_libraries(test-${A_Target} GTest::gmock)
target_link_libraries(test-${A_Target} ${CPP23SupportLibrary} GTest::gmock)
endif()

ADD_PRECOMPILED_HEADER(${A_Target}-objects)
Expand All @@ -914,7 +922,7 @@ daemon_write_buildinfo("Engine")

if (NOT NACL)
add_library(engine-lib EXCLUDE_FROM_ALL ${PCH_FILE} ${BUILDINFOLIST} ${COMMONLIST} ${ENGINELIST})
target_link_libraries(engine-lib ${LIBS_BASE} ${LIBS_ENGINE_BASE})
target_link_libraries(engine-lib ${LIBS_BASE} ${LIBS_ENGINE_BASE} ${CPP23SupportLibrary})
set_property(TARGET engine-lib APPEND PROPERTY COMPILE_DEFINITIONS BUILD_ENGINE)
set_property(TARGET engine-lib APPEND PROPERTY INCLUDE_DIRECTORIES ${ENGINE_DIR} ${MOUNT_DIR} ${LIB_DIR})
set_property(TARGET engine-lib APPEND PROPERTY COMPILE_OPTIONS ${WARNINGS})
Expand Down
6 changes: 4 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,29 @@ jobs:

- job: Linux
pool:
vmImage: 'ubuntu-22.04'
vmImage: 'ubuntu-24.04'
strategy:
matrix:
GCC:
BUILD_TYPE: Release
C_COMPILER: gcc
CXX_COMPILER: g++
CXX: g++-14
EXTRA_PACKAGES:
EXTRA_INSTALLS:
TOOLCHAIN_FILE:
Clang:
BUILD_TYPE: Release
C_COMPILER: clang
CXX_COMPILER: clang++
CXX: clang++-19
EXTRA_PACKAGES:
EXTRA_INSTALLS:
TOOLCHAIN_FILE:
Mingw:
BUILD_TYPE: Debug
C_COMPILER: x86_64-w64-mingw32-gcc
CXX_COMPILER: x86_64-w64-mingw32-g++
CXX_COMPILER: x86_64-w64-mingw32-g++-14
EXTRA_PACKAGES: gcc-mingw-w64-x86-64 g++-mingw-w64-x86-64 mingw-w64-x86-64-dev
EXTRA_INSTALLS: sudo update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix ; sudo update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
TOOLCHAIN_FILE: cmake/cross-toolchain-mingw64.cmake
Expand Down
78 changes: 77 additions & 1 deletion cmake/DaemonFlags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,70 @@ endif()
option(USE_RECOMMENDED_CXX_STANDARD "Use recommended C++ standard" ON)
mark_as_advanced(USE_RECOMMENDED_CXX_STANDARD)

option(USE_CPP23 "Use C++23 standard where possible" OFF)

# Required for <stacktrace> on Clang/GCC
if(USE_CPP23)
if (DAEMON_CXX_COMPILER_Clang_COMPATIBILITY OR DAEMON_CXX_COMPILER_GCC_COMPATIBILITY)
if ((DAEMON_CXX_COMPILER_Clang_VERSION VERSION_GREATER_EQUAL 19.1.0) OR (DAEMON_CXX_COMPILER_GCC_VERSION VERSION_GREATER_EQUAL 13.3))
set(CPP23SupportLibraryTryExp TRUE)
endif()

if ((DAEMON_CXX_COMPILER_Clang_VERSION VERSION_GREATER_EQUAL 17.0.1) OR (DAEMON_CXX_COMPILER_GCC_VERSION VERSION_GREATER_EQUAL 12.1))
set(CPP23SupportLibraryTryBacktrace TRUE)
endif()

if (CPP23SupportLibraryTryExp)
set(CPP23SupportLibrary "-lstdc++exp")
set(CPP23SupportLibraryCompatibleCompiler TRUE)

find_library(HAVE_CPP23SupportLibrary "libstdc++exp")
endif()

if (CPP23SupportLibraryTryBacktrace AND (HAVE_CPP23SupportLibrary-NOTFOUND OR NOT CPP23SupportLibraryTryExp))
if (CPP23SupportLibraryCompatibleCompiler)
set(CPP23SupportLibraryOldLibrary TRUE)
endif()

set(CPP23SupportLibrary "-lstdc++_libbacktrace")
set(CPP23SupportLibraryCompatibleCompiler TRUE)

find_library(HAVE_CPP23SupportLibrary "libstdc++_libbacktrace")
endif()

if (HAVE_CPP23SupportLibrary-NOTFOUND)
if (NOT CPP23SupportLibraryCompatibleCompiler)
message(WARNING "Not using <stacktrace>: the compiler is too old (requires clang >= 17.0.1 or GCC >= 12.1)")
else()
message(WARNING "Not using <stacktrace>: libstdc++exp or libstdc++_backtrace is required, but wasn't found in system paths")
endif()

set(CPP23SupportLibrary "")
elseif (CXX_FLAGS MATCHES ".*\\-stdlib\\=libc\\+\\+*")
message(WARNING "Not using <stacktrace>: only -stdlib=libstdc++ is supported")

set(CPP23SupportLibrary "")
else()
add_definitions(-DDAEMON_CPP23_SUPPORT_LIBRARY_ENABLED=1)
# FIXME: Doesn't work?
add_compile_options("-fmacro-prefix-map=${CMAKE_CURRENT_SOURCE_DIR}/src=.")

if (CPP23SupportLibraryOldLibrary)
message(STATUS "Using <stacktrace>: found ${CPP23SupportLibrary} (recommended to use libc++exp on this compiler version instead, but it wasn't found)")
else()
message(STATUS "Using <stacktrace>: found ${CPP23SupportLibrary}")
endif()
endif()
elseif (MSVC)
# FIXME: Doesn't work in sgame/cgame?
string(REPLACE "/" "\\" backslashed_dir ${CMAKE_CURRENT_SOURCE_DIR}/src)
add_compile_options("/d1trimfile:${backslashed_dir}")

string(REPLACE "/" "\\" backslashed_dir ${CMAKE_CURRENT_SOURCE_DIR}/daemon/src)
add_compile_options("/d1trimfile:${backslashed_dir}")
endif()
endif()

# Set flag without checking, optional argument specifies build type
macro(set_c_flag FLAG)
if (${ARGC} GREATER 1)
Expand Down Expand Up @@ -255,7 +319,19 @@ else()
endif()
endif()

if (USE_RECOMMENDED_CXX_STANDARD)
if (USE_CPP23)
if (MSVC)
add_compile_options("/std:c++23preview")
else()
try_cxx_flag(GNUXX23 "-std=gnu++23")

if (NOT FLAG_GNUXX23)
message(WARNING "Requested C++23 is not supported, falling back to C++14")
endif()
endif()
endif()

if (NOT USE_CPP23 AND (NOT FLAG_GNUXX23 OR USE_RECOMMENDED_CXX_STANDARD))
# PNaCl only defines isascii if __STRICT_ANSI__ is not defined,
# always prefer GNU dialect.
try_cxx_flag(GNUXX14 "-std=gnu++14")
Expand Down
6 changes: 4 additions & 2 deletions cmake/DaemonGame.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,11 @@ function(buildGameModule module_slug)
set_target_properties(${module_target} PROPERTIES
OUTPUT_NAME "${GAMEMODULE_NAME}"
SUFFIX "${PLATFORM_EXE_SUFFIX}")
endif()

target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE})
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE})
else()
target_link_libraries(${module_target} ${GAMEMODULE_LIBS} ${LIBS_BASE} ${CPP23SupportLibrary})
endif()

ADD_PRECOMPILED_HEADER(${module_target})
endfunction()
Expand Down
1 change: 1 addition & 0 deletions src.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(COMMONLIST
${COMMON_DIR}/Optional.h
${COMMON_DIR}/Platform.h
${COMMON_DIR}/Serialize.h
${COMMON_DIR}/StackTrace.h
${COMMON_DIR}/String.cpp
${COMMON_DIR}/String.h
${COMMON_DIR}/System.cpp
Expand Down
4 changes: 3 additions & 1 deletion src/common/CPPStandard.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define CPP_SOURCE_LOCATION
#endif

#if __cpp_lib_stacktrace >= 202011L
/* Clang/GCC support <stacktrace> with -lstdc++exp/-lstdlibc++_libbacktrace, but they don't define the feature macro,
so we set a custom macro in build system */
#if __cpp_lib_stacktrace >= 202011L || defined(DAEMON_CPP23_SUPPORT_LIBRARY_ENABLED)
#define CPP_STACKTRACE
#endif

Expand Down
109 changes: 109 additions & 0 deletions src/common/StackTrace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
===========================================================================

Daemon BSD Source Code
Copyright (c) 2025 Daemon Developers
All rights reserved.

This file is part of the Daemon BSD Source Code (Daemon Source Code).

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daemon developers nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DAEMON DEVELOPERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

===========================================================================
*/
// StackTrace.h

#ifndef STACKTRACE_H
#define STACKTRACE_H

#include "CPPStandard.h"
#include "String.h"

#include "Log.h"

#if defined( CPP_STACKTRACE )

#include <stacktrace>

inline std::string FormatStackTrace( const std::stacktrace& stackTrace,
const bool skipCurrent = false, const bool compact = false ) {
std::string out;
bool skipped = !skipCurrent;
bool addLineEnd = false;

for ( const std::stacktrace_entry& entry : stackTrace ) {
if ( !skipped ) {
skipped = true;
continue;
}

std::string file = entry.source_file();

#if defined( _MSC_VER )
size_t pos = file.find( "src\\n" );
#else
size_t pos = file.find( "src/" );
#endif

if ( pos != std::string::npos ) {
file = file.substr( pos + 4 );
}

if ( compact ) {
#if defined( _MSC_VER )
pos = file.find( "engine\\renderer-vulkan" );
#else
pos = file.find( "engine/renderer-vulkan" );
#endif

if ( pos == std::string::npos ) {
continue;
}

file = file.substr( pos + 23 );
}

if( compact ) {
out += Str::Format( addLineEnd ? "\n%s:%u" : "%s:%u", file, entry.source_line() );
} else {
out += Str::Format( addLineEnd ? "\n%s:%u: %s" : "%s:%u: %s", file, entry.source_line(), entry.description() );
}
addLineEnd = true;
}

return out;
}

inline void PrintStackTrace( const std::stacktrace& stackTrace = std::stacktrace::current() ) {
Log::Warn( "\n\n====================\nStackTrace:\n%s\n====================\n\n", FormatStackTrace( stackTrace ) );
}

#else

inline void PrintStackTrace() {
Log::Warn( "StackTrace unavailable: CPP23 required" );
}

#endif

#endif // STACKTRACE_H
7 changes: 6 additions & 1 deletion src/common/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "qcommon/sys.h"
#endif

#include "StackTrace.h"

namespace Sys {

// https://devblogs.microsoft.com/oldnewthing/20120105-00/?p=8683
Expand Down Expand Up @@ -224,6 +226,8 @@ void Drop(Str::StringRef message)
{
if (!OnMainThread()) {
Sys::Error(message);
} else {
PrintStackTrace();
}

// Transform into a fatal error if too many errors are generated in quick
Expand All @@ -234,8 +238,9 @@ void Drop(Str::StringRef message)
if (now - lastError < std::chrono::milliseconds(100)) {
if (++errorCount > 3)
Sys::Error(message);
} else
} else {
errorCount = 0;
}
lastError = now;

throw DropErr(true, message);
Expand Down
Loading