Skip to content

Commit

Permalink
perf(port): various debug.cpp improvements (cataclysmbnteam#3101)
Browse files Browse the repository at this point in the history
* chore: unindent common messages

was very confusing with `CMAKE_BUILD_TYPE`

* fix(port): BSD macro in src/debug.cpp

from: CleverRaven/Cataclysm-DDA#47440

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* fix(port): Implement C++ symbol demangling on macOS

from: CleverRaven/Cataclysm-DDA#47486

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* refactor(port):  Extract symbol demangling to a separate function

from: CleverRaven/Cataclysm-DDA#47517

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* feat(port):  Demangle symbol names on MinGW

from: CleverRaven/Cataclysm-DDA#47517

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* feat(port):  Implement C++ symbol demangling on Linux

from: CleverRaven/Cataclysm-DDA#47621

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* feat(port):  Implement C++ symbol demangling on FreeBSD and OpenBSD

from: CleverRaven/Cataclysm-DDA#48152

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* fix(port):  C++ symbol demangling on cross-compile MinGW

from: CleverRaven/Cataclysm-DDA#48220

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* fix(port): memory leak in demangle()

from: CleverRaven/Cataclysm-DDA#48230

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* feat(port):
Backtrace on Android

from: CleverRaven/Cataclysm-DDA#48371

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* chore(port): hide write_demangled_frame() for Android build

from: CleverRaven/Cataclysm-DDA#48414

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* feat(port):
demangle RTTI type names in debug messages

from: CleverRaven/Cataclysm-DDA#52633

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* perf(port): optimize null stream in DebugLog

from: CleverRaven/Cataclysm-DDA#55156

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>

* fix(port): reported version of newer Windows 10 releases

from: CleverRaven/Cataclysm-DDA#55506

Co-authored-by: xuv <18742123+xuvatilavv@users.noreply.github.com>

* feat(port): include build number in windows version report

CleverRaven/Cataclysm-DDA#55646

Co-authored-by: xuv <18742123+xuvatilavv@users.noreply.github.com>

* feat(port): get the actual static image base of modules

from: CleverRaven/Cataclysm-DDA#55782

Co-authored-by: Qrox <qrox@sina.com>

* ci: fix version typo

* feat(port): support libbacktrace on linux

from: CleverRaven/Cataclysm-DDA#59020

Co-authored-by: John Bytheway <jbytheway@gmail.com>

* build(cmake): support custom linker

context:
https://stackoverflow.com//questions/1867745/cmake-use-a-custom-linker#answer-66730019

* docs(cmake): linker, backtrace and libbacktrace

---------

Co-authored-by: Brett Dong <brett.browning.dong@gmail.com>
Co-authored-by: xuv <18742123+xuvatilavv@users.noreply.github.com>
Co-authored-by: Qrox <qrox@sina.com>
Co-authored-by: John Bytheway <jbytheway@gmail.com>
  • Loading branch information
5 people authored Sep 3, 2023
1 parent 0028a70 commit 1eeea09
Show file tree
Hide file tree
Showing 17 changed files with 458 additions and 99 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/matrix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ jobs:
sound: 0
lua: 0
test-stage: 1
libbacktrace: 1
title: GCC 12, Ubuntu, Curses
native: linux64
- compiler: g++-12
Expand All @@ -51,9 +52,10 @@ jobs:
sound: 1
lua: 1
test-stage: 1
title: GCC 11, Ubuntu, Tiles, Sound, Lua
libbacktrace: 1
title: GCC 12, Ubuntu, Tiles, Sound, Lua
native: linux64
- compiler: g++-11
- compiler: g++-12
os: ubuntu-22.04
cmake: 0
tiles: 1
Expand Down Expand Up @@ -95,6 +97,7 @@ jobs:
TEST_STAGE: ${{ matrix.test-stage }}
LANGUAGES: ${{ matrix.languages }}
EXTRA_TEST_OPTS:
LIBBACKTRACE: ${{ matrix.libbacktrace }}
NATIVE: ${{ matrix.native }}
RELEASE: 1
SKIP: ${{ needs.skip-duplicates.outputs.should_skip == 'true' }}
Expand Down
36 changes: 23 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ set(CATA_CLANG_TIDY_INCLUDE_DIR "" CACHE STRING "Path to internal clang-tidy hea
set(CATA_CHECK_CLANG_TIDY "" CACHE STRING "Path to check_clang_tidy.py for plugin tests")
set(GIT_BINARY "" CACHE STRING "Git binary name or path.")
set(PREFIX "" CACHE STRING "Location of Data & GFX directories")
set(LINKER "" CACHE STRING "Custom Linker to use")

include(CTest)

Expand All @@ -52,6 +53,10 @@ MESSAGE(STATUS "${PROJECT} build environment -- \n")

MESSAGE(STATUS "Build realm is : ${CMAKE_SYSTEM_NAME} ${CMAKE_HOST_SYSTEM_PROCESSOR}")

IF(LINKER)
add_link_options("-fuse-ld=${LINKER}")
ENDIF()

IF(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
ENDIF(NOT CMAKE_BUILD_TYPE)
Expand All @@ -62,7 +67,7 @@ if (NOT ${GIT_VERSION} MATCHES GIT-NOTFOUND)
string(REPLACE "-NOTFOUND" "" GIT_VERSION ${GIT_VERSION})
FILE(WRITE ${CMAKE_SOURCE_DIR}/src/version.h
"// NOLINT(cata-header-guard)\n\#define VERSION \"${GIT_VERSION}\"\n")
MESSAGE(STATUS "${PROJECT_NAME} build version is : ${GIT_VERSION}\n")
MESSAGE(STATUS "${PROJECT_NAME} build version is : ${GIT_VERSION}\n")
ADD_DEFINITIONS(-DGIT_VERSION)
ELSE (NOT ${GIT_VERSION} MATCHES GIT-NOTFOUND)
MESSAGE("")
Expand Down Expand Up @@ -196,22 +201,27 @@ ELSE (CMAKE_BUILD_TYPE STREQUAL Debug)
ADD_DEFINITIONS(-DDATA_DIR_PREFIX)
ENDIF (PREFIX AND NOT WIN32)
ENDIF (CMAKE_BUILD_TYPE STREQUAL Debug)
MESSAGE(STATUS "GIT_BINARY : ${GIT_EXECUTABLE}")
MESSAGE(STATUS "DYNAMIC_LINKING : ${DYNAMIC_LINKING}")

MESSAGE(STATUS "TILES : ${TILES}")
MESSAGE(STATUS "CURSES : ${CURSES}")
MESSAGE(STATUS "SOUND : ${SOUND}")
MESSAGE(STATUS "LUA : ${LUA}")
MESSAGE(STATUS "BACKTRACE : ${BACKTRACE}")
MESSAGE(STATUS "USE_HOME_DIR : ${USE_HOME_DIR}\n")
MESSAGE(STATUS "GIT_BINARY : ${GIT_EXECUTABLE}")
MESSAGE(STATUS "DYNAMIC_LINKING : ${DYNAMIC_LINKING}")

MESSAGE(STATUS "UNITY_BUILD : ${USE_UNITY_BUILD}")
MESSAGE(STATUS "PCH_HEADER : ${USE_PCH_HEADER}")
MESSAGE(STATUS "TILES : ${TILES}")
MESSAGE(STATUS "CURSES : ${CURSES}")
MESSAGE(STATUS "SOUND : ${SOUND}")
MESSAGE(STATUS "LUA : ${LUA}")
MESSAGE(STATUS "BACKTRACE : ${BACKTRACE}")
MESSAGE(STATUS "LIBBACKTRACE : ${LIBBACKTRACE}")
MESSAGE(STATUS "USE_HOME_DIR : ${USE_HOME_DIR}\n")

MESSAGE(STATUS "LANGUAGES : ${LANGUAGES}\n")
MESSAGE(STATUS "UNITY_BUILD : ${USE_UNITY_BUILD}")
MESSAGE(STATUS "PCH_HEADER : ${USE_PCH_HEADER}")
MESSAGE(STATUS "LANGUAGES : ${LANGUAGES}\n")

IF(LINKER)
MESSAGE(STATUS "LINKER : ${LINKER}\n")
ENDIF()

MESSAGE(STATUS "See INSTALL file for details and more info --\n")
MESSAGE(STATUS "See INSTALL file for details and more info --\n")

IF(MSVC)
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
Expand Down
14 changes: 8 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
# make TILES=1 SOUND=1
# Disable backtrace support, not available on all platforms
# make BACKTRACE=0
# Use libbacktrace. Only has effect if BACKTRACE=1. (currently only for MinGW builds)
# Use libbacktrace. Only has effect if BACKTRACE=1. (currently only for MinGW and Linux builds)
# make LIBBACKTRACE=1
# Compile localization files for specified languages
# make localization LANGUAGES="<lang_id_1>[ lang_id_2][ ...]"
Expand Down Expand Up @@ -399,7 +399,11 @@ ifeq ($(RELEASE), 1)
OTHERS += $(RELEASE_FLAGS)
DEBUG =
ifndef DEBUG_SYMBOLS
DEBUGSYMS =
ifeq ($(LIBBACKTRACE), 1)
DEBUGSYMS = -g1
else
DEBUGSYMS =
endif
endif
DEFINES += -DRELEASE
# Check for astyle or JSON regressions on release builds.
Expand Down Expand Up @@ -763,16 +767,14 @@ ifeq ($(TARGETSYSTEM),WINDOWS)
LDFLAGS += -lgdi32 -lwinmm -limm32 -lole32 -loleaut32 -lversion
ifeq ($(BACKTRACE),1)
LDFLAGS += -ldbghelp
ifeq ($(LIBBACKTRACE),1)
LDFLAGS += -lbacktrace
endif
endif
endif

ifeq ($(BACKTRACE),1)
DEFINES += -DBACKTRACE
ifeq ($(LIBBACKTRACE),1)
DEFINES += -DLIBBACKTRACE
LDFLAGS += -lbacktrace
endif
endif

Expand Down Expand Up @@ -827,7 +829,7 @@ ifeq ($(LUA), 1)
DEFINES += -DLUA
LUA_OBJS = $(sort $(LUA_SOURCES:$(LUA_SRC_DIR)/%.c=$(ODIRLUA)/%.o))
else
LUA_OBJS =
LUA_OBJS =
endif

ifdef LANGUAGES
Expand Down
3 changes: 2 additions & 1 deletion build-scripts/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ then
-DCMAKE_BUILD_TYPE="$build_type" \
-DTILES=${TILES:-0} \
-DSOUND=${SOUND:-0} \
-DLIBBACKTRACE=${LIBBACKTRACE:-0} \
"${cmake_extra_opts[@]}" \
..
if [ -n "$CATA_CLANG_TIDY" ]
Expand Down Expand Up @@ -149,7 +150,7 @@ else
else
export BACKTRACE=1
fi
make -j "$num_jobs" RELEASE=1 CCACHE=1 CROSS="$CROSS_COMPILATION" LANGUAGES="all" LINTJSON=0
make -j "$num_jobs" RELEASE=1 CCACHE=1 CROSS="$CROSS_COMPILATION" LANGUAGES="all" LINTJSON=0 LIBBACKTRACE="$LIBBACKTRACE"

export UBSAN_OPTIONS=print_stacktrace=1
if [ "$OS" == "macos-12" ]
Expand Down
11 changes: 11 additions & 0 deletions build-scripts/requirements.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@ set -x
echo "::add-matcher::build-scripts/problem-matchers/catch2.json"
echo "::add-matcher::build-scripts/problem-matchers/debugmsg.json"

if [[ "$LIBBACKTRACE" == "1" ]]; then
git clone https://github.com/ianlancetaylor/libbacktrace.git
(
cd libbacktrace
git checkout 4d2dd0b172f2c9192f83ba93425f868f2a13c553
./configure
make -j$(nproc)
sudo make install
)
fi

if [ -n "$CATA_CLANG_TIDY" ]; then
pip install --user wheel --upgrade
pip install --user 'lit==0.11.1' 'click==7.1.2'
Expand Down
42 changes: 32 additions & 10 deletions doc/COMPILING/COMPILING-CMAKE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ For official way to build CataclysmBN See:

# Contents

* [Prerequisites](#prerequisites)
* [Build Environment](#build-environment)
* [UNIX Environment](#unix-environment)
* [Windows Environment](#windows-environment-msys2)
* [CMake Build](#cmake-build)
* [MinGW,MSYS,MSYS2](#cmake-build-for-msys2-mingw)
* [MSBuild, VisualStudio](#cmake-build-for-visual-studio--msbuild)
* [Build Options](#build-options)
* [CMake specific options](#cmake-specific-options)
* [CataclysmBN specific options])(#cataclysmbn-specific-options)
- [Disclaimer](#disclaimer)
- [Contents](#contents)
- [Prerequisites](#prerequisites)
- [Build Environment](#build-environment)
- [UNIX Environment](#unix-environment)
- [Windows Environment (MSYS2)](#windows-environment-msys2)
- [CMake Build](#cmake-build)
- [CMake Build for MSYS2 (MinGW)](#cmake-build-for-msys2-mingw)
- [CMake Build for Visual Studio / MSBuild](#cmake-build-for-visual-studio--msbuild)
- [Build Options](#build-options)
- [CMake specific options](#cmake-specific-options)
- [CataclysmBN specific options](#cataclysmbn-specific-options)

# Prerequisites

Expand Down Expand Up @@ -321,6 +323,26 @@ Run the game. Should work.

Use dynamic linking. Or use static to remove MinGW dependency instead.

* CUSTOM LINKER=`<str>`

Choose custom linkers such as [gold], [lld] or [mold].

- Choose ld if you don't use libbacktrace.
- Choose mold if use libbacktrace. It's the fastest linker, outperforming gold by 24x.

[gold]: https://en.wikipedia.org/wiki/Gold_(linker)
[lld]: https://lld.llvm.org
[mold]: https://github.com/rui314/mold

* BACKTRACE=`<boolean>`

On crash, print a backtrace to the console. Defaults to `ON` for debug builds.

* LIBBACKTRACE=`<boolean>`

Print backtrace with [libbacktrace]. This allows lld and mold to print backtrace, and is generally much faster.

[libbacktrace]: https://github.com/ianlancetaylor/libbacktrace

* GIT_BINARY=`<str>`

Expand Down
2 changes: 1 addition & 1 deletion src/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ LOCAL_SHARED_LIBRARIES := libhidapi SDL2 SDL2_mixer SDL2_image SDL2_ttf mpg123

LOCAL_LDLIBS := -lGLESv1_CM -lGLESv2 -llog

LOCAL_CFLAGS += -DTILES=1 -DSDL_SOUND=1 -DLUA=1 -DCATA_NO_CPP11_STRING_CONVERSIONS=1 -Wextra -Wall -fsigned-char -ffast-math
LOCAL_CFLAGS += -DTILES=1 -DSDL_SOUND=1 -DLUA=1 -DBACKTRACE=1 -Wextra -Wall -fsigned-char -ffast-math

LOCAL_LDFLAGS += $(LOCAL_CFLAGS)

Expand Down
16 changes: 8 additions & 8 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,13 @@ IF(TILES)

IF(BACKTRACE)
target_link_libraries(cataclysm-tiles-common dbghelp.lib)

IF(LIBBACKTRACE)
target_link_libraries(cataclysm-tiles-common backtrace)
ENDIF(LIBBACKTRACE)
ENDIF(BACKTRACE)
ENDIF(WIN32)

IF(LIBBACKTRACE)
target_link_libraries(cataclysm-tiles-common backtrace)
ENDIF(LIBBACKTRACE)

IF(RELEASE)
install(TARGETS cataclysm-tiles DESTINATION ${BIN_PREFIX})
ENDIF(RELEASE)
Expand Down Expand Up @@ -205,13 +205,13 @@ IF(CURSES)

IF(BACKTRACE)
target_link_libraries(cataclysm-common dbghelp.lib)

IF(LIBBACKTRACE)
target_link_libraries(cataclysm-common backtrace)
ENDIF(LIBBACKTRACE)
ENDIF(BACKTRACE)
ENDIF(WIN32)

IF(LIBBACKTRACE)
target_link_libraries(cataclysm-common backtrace)
ENDIF(LIBBACKTRACE)

IF(RELEASE)
install(TARGETS cataclysm DESTINATION ${BIN_PREFIX})
ENDIF(RELEASE)
Expand Down
17 changes: 17 additions & 0 deletions src/crash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ extern "C" {
#endif
const std::string crash_log_file = PATH_INFO::crash();
std::ostringstream log_text;
#if defined(__ANDROID__)
// At this point, Android JVM is already doomed
// No further UI interaction (including the SDL message box)
// Show a dialogue at next launch
log_text << "VERSION: " << getVersionString()
<< '\n' << type << ' ' << msg;
#else
log_text << "The program has crashed."
<< "\nSee the log file for a stack trace."
<< "\nCRASH LOG FILE: " << crash_log_file
Expand All @@ -66,6 +73,7 @@ extern "C" {
log_text.str().c_str(), nullptr ) != 0 ) {
log_text << "Error creating SDL message box: " << SDL_GetError() << '\n';
}
#endif
#endif
log_text << "\nSTACK TRACE:\n";
debug_write_backtrace( log_text );
Expand All @@ -75,6 +83,15 @@ extern "C" {
fwrite( log_text.str().data(), 1, log_text.str().size(), file );
fclose( file );
}
#if defined(__ANDROID__)
// Create a placeholder dummy file "config/crash.log.prompt"
// to let the app show a dialog box at next start
file = fopen( ( crash_log_file + ".prompt" ).c_str(), "w" );
if( file ) {
fwrite( "0", 1, 1, file );
fclose( file );
}
#endif
}

static void signal_handler( int sig )
Expand Down
Loading

0 comments on commit 1eeea09

Please sign in to comment.