From 9ceaa7de2c9ae8f097db124c65432b4bcb6d6d85 Mon Sep 17 00:00:00 2001 From: Arpad Borsos Date: Mon, 24 Feb 2020 17:54:34 +0100 Subject: [PATCH] refactor: Restructure CMake build and internal definitions (#149) * refactor: Restructure CMake build and internal definitions This moves a lot of files around, and changes the modulefinder and symbolizer to be defined via compiletime definitions. * fix build on windows and android --- CMakeLists.txt | 92 +++---------- src/CMakeLists.txt | 121 ++++++++++++++++++ src/backends/sentry_crashpad_backend.cpp | 10 +- src/backends/sentry_crashpad_backend.h | 4 +- src/backends/sentry_inproc_backend.c | 12 +- src/backends/sentry_inproc_backend.h | 6 +- src/darwin/sentry_darwin_modulefinder.h | 8 -- .../sentry_modulefinder_apple.c} | 9 +- .../sentry_modulefinder_linux.c} | 16 ++- .../sentry_modulefinder_linux.h} | 4 +- .../sentry_modulefinder_windows.c} | 10 +- src/{ => path}/sentry_path.c | 0 .../sentry_path_unix.c} | 10 +- .../sentry_path_windows.c} | 12 +- src/sentry_alloc.c | 4 +- src/sentry_modulefinder.c | 34 ----- src/sentry_string.c | 13 ++ src/sentry_symbolizer.c | 27 ---- src/sentry_sync.c | 2 +- src/{unix => }/sentry_unix_pageallocator.c | 2 +- src/{unix => }/sentry_unix_pageallocator.h | 4 +- src/{unix => }/sentry_unix_spinlock.h | 4 +- src/sentry_uuid.h | 3 +- src/sentry_windows_dbghelp.c | 24 ++++ src/sentry_windows_dbghelp.h | 8 ++ .../sentry_symbolizer_unix.c} | 8 +- src/symbolizer/sentry_symbolizer_windows.c | 39 ++++++ src/transports/sentry_disk_transport.c | 10 +- src/transports/sentry_disk_transport.h | 4 +- src/transports/sentry_function_transport.c | 12 +- src/transports/sentry_function_transport.h | 4 +- src/transports/sentry_libcurl_transport.c | 12 +- src/transports/sentry_libcurl_transport.h | 4 +- src/unix/sentry_unix_symbolizer_dladdr.h | 11 -- src/unix/sentry_unix_unwinder_libbacktrace.h | 9 -- .../sentry_unix_unwinder_libunwindstack.h | 16 --- .../sentry_unwinder.c} | 31 ++--- src/unwinder/sentry_unwinder_dbghelp.c | 47 +++++++ .../sentry_unwinder_libbacktrace.c} | 17 +-- .../sentry_unwinder_libunwindstack.cpp} | 9 +- src/windows/sentry_windows_dbghelp.c | 100 --------------- src/windows/sentry_windows_dbghelp.h | 13 -- src/windows/sentry_windows_modulefinder.h | 8 -- src/windows/sentry_windows_string.c | 12 -- tests/sentry_testsupport.h | 2 +- tests/test_attachments.c | 8 +- tests/test_basic.c | 2 +- tests/test_consent.c | 2 +- tests/test_database.c | 4 +- tests/test_envelopes.c | 4 +- tests/test_modulefinder.c | 4 +- tests/test_path.c | 2 +- tests/test_symbolizer.c | 2 +- tests/test_sync.c | 4 +- tests/test_unwinder.c | 2 +- tests/test_utils.c | 4 +- tests/test_value.c | 2 +- 57 files changed, 406 insertions(+), 441 deletions(-) create mode 100644 src/CMakeLists.txt delete mode 100644 src/darwin/sentry_darwin_modulefinder.h rename src/{darwin/sentry_darwin_modulefinder.c => modulefinder/sentry_modulefinder_apple.c} (96%) rename src/{linux/sentry_procmaps_modulefinder.c => modulefinder/sentry_modulefinder_linux.c} (98%) rename src/{linux/sentry_procmaps_modulefinder.h => modulefinder/sentry_modulefinder_linux.h} (85%) rename src/{windows/sentry_windows_modulefinder.c => modulefinder/sentry_modulefinder_windows.c} (95%) rename src/{ => path}/sentry_path.c (100%) rename src/{unix/sentry_unix_path.c => path/sentry_path_unix.c} (98%) rename src/{windows/sentry_windows_path.c => path/sentry_path_windows.c} (98%) delete mode 100644 src/sentry_modulefinder.c delete mode 100644 src/sentry_symbolizer.c rename src/{unix => }/sentry_unix_pageallocator.c (99%) rename src/{unix => }/sentry_unix_pageallocator.h (89%) rename src/{unix => }/sentry_unix_spinlock.h (94%) create mode 100644 src/sentry_windows_dbghelp.c create mode 100644 src/sentry_windows_dbghelp.h rename src/{unix/sentry_unix_symbolizer_dladdr.c => symbolizer/sentry_symbolizer_unix.c} (87%) create mode 100644 src/symbolizer/sentry_symbolizer_windows.c delete mode 100644 src/unix/sentry_unix_symbolizer_dladdr.h delete mode 100644 src/unix/sentry_unix_unwinder_libbacktrace.h delete mode 100644 src/unix/sentry_unix_unwinder_libunwindstack.h rename src/{sentry_unwind.c => unwinder/sentry_unwinder.c} (57%) create mode 100644 src/unwinder/sentry_unwinder_dbghelp.c rename src/{unix/sentry_unix_unwinder_libbacktrace.c => unwinder/sentry_unwinder_libbacktrace.c} (55%) rename src/{unix/sentry_unix_unwinder_libunwindstack.cpp => unwinder/sentry_unwinder_libunwindstack.cpp} (95%) delete mode 100644 src/windows/sentry_windows_dbghelp.c delete mode 100644 src/windows/sentry_windows_dbghelp.h delete mode 100644 src/windows/sentry_windows_modulefinder.h delete mode 100644 src/windows/sentry_windows_string.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 7355eb229..0401a940f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,6 +3,10 @@ project (Sentry-Native LANGUAGES C CXX ASM) include(CMakeDependentOption) +if(CMAKE_SYSTEM_NAME STREQUAL "Linux") + set(LINUX TRUE) +endif() + OPTION(BUILD_SHARED_LIBS "Build shared libraries (.dll/.so) instead of static ones (.lib/.a)" ON) CMAKE_DEPENDENT_OPTION(WITH_CRASHPAD "Build and use the Crashpad integration" ON "APPLE OR WIN32" OFF) @@ -22,10 +26,6 @@ if(WITH_TSAN_OPTION) link_libraries("-fsanitize=thread") endif() -include_directories("include") - -set(SENTRY_NATIVE_SOURCE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/src) - if(BUILD_SHARED_LIBS) add_definitions(-DSENTRY_BUILD_SHARED) else() @@ -34,6 +34,7 @@ endif() FIND_PACKAGE(CURL) if(CURL_FOUND) + set(WITH_CURL TRUE) add_definitions(-DSENTRY_WITH_LIBCURL_TRANSPORT) include_directories(${CURL_INCLUDE_DIR}) set(LINK_LIBRARIES ${LINK_LIBRARIES} ${CURL_LIBRARIES}) @@ -56,75 +57,24 @@ if(WITH_CRASHPAD AND WIN32) add_definitions(/wd4005) endif() -file(GLOB SENTRY_NATIVE_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/*.c - ${SENTRY_NATIVE_SOURCE_ROOT}/transports/sentry_disk_transport.c - ${SENTRY_NATIVE_SOURCE_ROOT}/transports/sentry_function_transport.c -) - -file(GLOB SENTRY_NATIVE_LIBCURL_TRANSPORT_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/transports/sentry_libcurl_transport.c -) - -file(GLOB SENTRY_NATIVE_INPROC_BACKEND_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/backends/sentry_inproc_*.c -) - -file(GLOB SENTRY_NATIVE_UNIX_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/unix/*.c -) - -file(GLOB SENTRY_NATIVE_LINUX_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/linux/*.c -) - -file(GLOB SENTRY_NATIVE_DARWIN_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/darwin/*.c -) - -file(GLOB SENTRY_NATIVE_ANDROID_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/linux/sentry_procmaps_modulefinder.c - ${SENTRY_NATIVE_SOURCE_ROOT}/unix/sentry_unix_unwinder_libunwindstack.cpp -) +if(ANDROID) + set(WITH_LIBUNWINDSTACK TRUE) +elseif(NOT WIN32) + set(WITH_LIBBACKTRACE TRUE) +endif() -file(GLOB SENTRY_NATIVE_WINDOWS_SOURCES - ${SENTRY_NATIVE_SOURCE_ROOT}/windows/*.c -) +include_directories("include") +include_directories("src") file(GLOB_RECURSE SENTRY_NATIVE_TEST_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/tests/*.c ) -set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_SOURCES}) -if(WIN32) - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_WINDOWS_SOURCES}) -else() - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_UNIX_SOURCES} ${SENTRY_NATIVE_INPROC_BACKEND_SOURCES}) -endif() - -if(CURL_FOUND) - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_LIBCURL_TRANSPORT_SOURCES}) -endif() - -if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_DARWIN_SOURCES}) -endif() - -if(CMAKE_SYSTEM_NAME STREQUAL "Linux") - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_LINUX_SOURCES}) - +if(LINUX) set(LINK_LIBRARIES ${LINK_LIBRARIES} "pthread" "dl") endif() -if(CMAKE_SYSTEM_NAME STREQUAL "Android") - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_ANDROID_SOURCES}) - +if(WITH_LIBUNWINDSTACK) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/external/libunwindstack-ndk/include) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/external/libunwindstack-ndk/cmake) set(LINK_LIBRARIES ${LINK_LIBRARIES} "unwindstack") @@ -149,17 +99,14 @@ if(WITH_CRASHPAD) include_directories(external/crashpad external/crashpad/third_party/mini_chromium/mini_chromium) add_definitions(-DSENTRY_WITH_CRASHPAD_BACKEND) set(LINK_LIBRARIES ${LINK_LIBRARIES} crashpad_client crashpad_util) - set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} - ${SENTRY_NATIVE_SOURCE_ROOT}/backends/sentry_crashpad_backend.cpp) install(TARGETS "crashpad_handler") endif() -# msgpack -set(SENTRY_NATIVE_ALL_SOURCES ${SENTRY_NATIVE_ALL_SOURCES} vendor/mpack.c) - # ===== sentry library ===== -add_library("sentry" ${SENTRY_NATIVE_ALL_SOURCES}) +add_library("sentry" "vendor/mpack.c") +add_subdirectory(src) + set_target_properties("sentry" PROPERTIES PUBLIC_HEADER "include/sentry.h") target_link_libraries("sentry" ${LINK_LIBRARIES}) install(TARGETS "sentry") @@ -172,11 +119,14 @@ endif() # compile tests separately and pass an extra preprocessor define so we can # switch some internal modes for the unittests. +# TODO: get sources from "sentry" +get_target_property(SENTRY_NATIVE_ALL_SOURCES sentry SOURCES) add_executable("sentry_tests" EXCLUDE_FROM_ALL ${SENTRY_NATIVE_ALL_SOURCES} ${SENTRY_NATIVE_TEST_SOURCES} ) -target_compile_definitions("sentry_tests" PUBLIC -DSENTRY_UNITTEST) +get_target_property(SENTRY_DEFS sentry COMPILE_DEFINITIONS) +target_compile_definitions("sentry_tests" PUBLIC SENTRY_UNITTEST ${SENTRY_DEFS}) target_link_libraries("sentry_tests" ${LINK_LIBRARIES}) # to fix some issues with tests (dladdr can't find functions otherwise) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 000000000..cbe4d0d30 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,121 @@ +list(APPEND SENTRY_SOURCES + sentry_alloc.c + sentry_alloc.h + sentry_backend.c + sentry_backend.h + sentry_boot.h + sentry_core.c + sentry_core.h + sentry_database.c + sentry_database.h + sentry_envelope.c + sentry_envelope.h + sentry_json.c + sentry_json.h + sentry_modulefinder.h + sentry_path.h + sentry_random.c + sentry_random.h + sentry_scope.c + sentry_scope.h + sentry_slice.c + sentry_slice.h + sentry_string.c + sentry_string.h + sentry_symbolizer.h + sentry_sync.c + sentry_sync.h + sentry_transport.c + sentry_transport.h + sentry_utils.c + sentry_utils.h + sentry_uuid.c + sentry_uuid.h + sentry_value.c + sentry_value.h + path/sentry_path.c + transports/sentry_disk_transport.c + transports/sentry_disk_transport.h + transports/sentry_function_transport.c + transports/sentry_function_transport.h + unwinder/sentry_unwinder.c +) + +# generic platform / path / symbolizer +if(WIN32) + list(APPEND SENTRY_SOURCES + sentry_windows_dbghelp.c + sentry_windows_dbghelp.h + path/sentry_path_windows.c + symbolizer/sentry_symbolizer_windows.c + ) +else() + list(APPEND SENTRY_SOURCES + sentry_unix_pageallocator.c + sentry_unix_pageallocator.h + sentry_unix_spinlock.h + path/sentry_path_unix.c + symbolizer/sentry_symbolizer_unix.c + ) +endif() + +# module finder +if(WIN32) + list(APPEND SENTRY_SOURCES + modulefinder/sentry_modulefinder_windows.c + ) +elseif(APPLE) + list(APPEND SENTRY_SOURCES + modulefinder/sentry_modulefinder_apple.c + ) +elseif(LINUX OR ANDROID) + list(APPEND SENTRY_SOURCES + modulefinder/sentry_modulefinder_linux.c + ) +endif() + +# curl transport +if(WITH_CURL) + list(APPEND SENTRY_SOURCES + transports/sentry_libcurl_transport.c + transports/sentry_libcurl_transport.h + ) +endif() + +# backends +if(WITH_CRASHPAD) + list(APPEND SENTRY_SOURCES + backends/sentry_crashpad_backend.cpp + backends/sentry_crashpad_backend.h + ) +elseif(NOT WIN32) + list(APPEND SENTRY_SOURCES + backends/sentry_inproc_backend.c + backends/sentry_inproc_backend.h + ) +endif() + +# unwinder +if(WITH_LIBBACKTRACE) + target_compile_definitions(sentry PUBLIC SENTRY_WITH_UNWINDER_LIBBACKTRACE) + list(APPEND SENTRY_SOURCES + unwinder/sentry_unwinder_libbacktrace.c + ) +endif() +if(WITH_LIBUNWINDSTACK) + target_compile_definitions(sentry PUBLIC SENTRY_WITH_UNWINDER_LIBUNWINDSTACK) + list(APPEND SENTRY_SOURCES + unwinder/sentry_unwinder_libunwindstack.cpp + ) +endif() +if(WIN32) + target_compile_definitions(sentry PUBLIC SENTRY_WITH_UNWINDER_DBGHELP) + list(APPEND SENTRY_SOURCES + unwinder/sentry_unwinder_dbghelp.c + ) +endif() + +# make paths absolute +list(TRANSFORM SENTRY_SOURCES PREPEND "${CMAKE_CURRENT_SOURCE_DIR}/") +target_sources(sentry PUBLIC ${SENTRY_SOURCES}) +target_compile_definitions(sentry PUBLIC ) diff --git a/src/backends/sentry_crashpad_backend.cpp b/src/backends/sentry_crashpad_backend.cpp index 4b9230e12..35b50a91c 100644 --- a/src/backends/sentry_crashpad_backend.cpp +++ b/src/backends/sentry_crashpad_backend.cpp @@ -1,11 +1,11 @@ #include "sentry_crashpad_backend.h" extern "C" { -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_database.h" -#include "../sentry_path.h" -#include "../sentry_utils.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_database.h" +#include "sentry_path.h" +#include "sentry_utils.h" } #include diff --git a/src/backends/sentry_crashpad_backend.h b/src/backends/sentry_crashpad_backend.h index 85db16de9..496b06393 100644 --- a/src/backends/sentry_crashpad_backend.h +++ b/src/backends/sentry_crashpad_backend.h @@ -5,9 +5,9 @@ extern "C" { #endif -#include "../sentry_boot.h" +#include "sentry_boot.h" -#include "../sentry_backend.h" +#include "sentry_backend.h" sentry_backend_t *sentry__new_crashpad_backend(void); diff --git a/src/backends/sentry_inproc_backend.c b/src/backends/sentry_inproc_backend.c index d84ae5508..5f2635400 100644 --- a/src/backends/sentry_inproc_backend.c +++ b/src/backends/sentry_inproc_backend.c @@ -1,11 +1,11 @@ #include "sentry_inproc_backend.h" -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_envelope.h" -#include "../sentry_scope.h" -#include "../sentry_sync.h" -#include "../unix/sentry_unix_pageallocator.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_envelope.h" +#include "sentry_scope.h" +#include "sentry_sync.h" +#include "sentry_unix_pageallocator.h" #include #define SIGNAL_DEF(Sig, Desc) \ diff --git a/src/backends/sentry_inproc_backend.h b/src/backends/sentry_inproc_backend.h index 4e8beb207..7509d2052 100644 --- a/src/backends/sentry_inproc_backend.h +++ b/src/backends/sentry_inproc_backend.h @@ -1,10 +1,10 @@ #ifndef SENTRY_INPROC_BACKEND_H_INCLUDED #define SENTRY_INPROC_BACKEND_H_INCLUDED -#include "../sentry_boot.h" +#include "sentry_boot.h" -#include "../sentry_backend.h" +#include "sentry_backend.h" sentry_backend_t *sentry__new_inproc_backend(void); -#endif \ No newline at end of file +#endif diff --git a/src/darwin/sentry_darwin_modulefinder.h b/src/darwin/sentry_darwin_modulefinder.h deleted file mode 100644 index 6e2cc1e9f..000000000 --- a/src/darwin/sentry_darwin_modulefinder.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SENTRY_DARWIN_MODULEFINDER_H_INCLUDED -#define SENTRY_DARWIN_MODULEFINDER_H_INCLUDED - -#include "../sentry_boot.h" - -sentry_value_t sentry__darwin_modules_get_list(void); - -#endif diff --git a/src/darwin/sentry_darwin_modulefinder.c b/src/modulefinder/sentry_modulefinder_apple.c similarity index 96% rename from src/darwin/sentry_darwin_modulefinder.c rename to src/modulefinder/sentry_modulefinder_apple.c index ff6e655bd..2275b14f5 100644 --- a/src/darwin/sentry_darwin_modulefinder.c +++ b/src/modulefinder/sentry_modulefinder_apple.c @@ -1,6 +1,7 @@ -#include "sentry_darwin_modulefinder.h" -#include "../sentry_sync.h" -#include "../sentry_value.h" +#include "sentry_boot.h" + +#include "sentry_sync.h" +#include "sentry_value.h" #include #include @@ -125,7 +126,7 @@ remove_image(const struct mach_header *mh, intptr_t vmaddr_slide) } sentry_value_t -sentry__darwin_modules_get_list(void) +sentry__modules_get_list(void) { sentry__mutex_lock(&g_mutex); if (!g_initialized) { diff --git a/src/linux/sentry_procmaps_modulefinder.c b/src/modulefinder/sentry_modulefinder_linux.c similarity index 98% rename from src/linux/sentry_procmaps_modulefinder.c rename to src/modulefinder/sentry_modulefinder_linux.c index 9a44f8e2a..3797ab5a4 100644 --- a/src/linux/sentry_procmaps_modulefinder.c +++ b/src/modulefinder/sentry_modulefinder_linux.c @@ -1,9 +1,11 @@ -#include "sentry_procmaps_modulefinder.h" -#include "../sentry_core.h" -#include "../sentry_path.h" -#include "../sentry_string.h" -#include "../sentry_sync.h" -#include "../sentry_value.h" +#include "sentry_modulefinder_linux.h" + +#include "sentry_core.h" +#include "sentry_path.h" +#include "sentry_string.h" +#include "sentry_sync.h" +#include "sentry_value.h" + #include #include #include @@ -334,7 +336,7 @@ load_modules(sentry_value_t modules) } sentry_value_t -sentry__procmaps_modules_get_list(void) +sentry__modules_get_list(void) { sentry__mutex_lock(&g_mutex); if (!g_initialized) { diff --git a/src/linux/sentry_procmaps_modulefinder.h b/src/modulefinder/sentry_modulefinder_linux.h similarity index 85% rename from src/linux/sentry_procmaps_modulefinder.h rename to src/modulefinder/sentry_modulefinder_linux.h index 4d46c7e7c..b1dce2bc3 100644 --- a/src/linux/sentry_procmaps_modulefinder.h +++ b/src/modulefinder/sentry_modulefinder_linux.h @@ -1,8 +1,8 @@ #ifndef SENTRY_PROCMAPS_MODULEFINDER_H_INCLUDED #define SENTRY_PROCMAPS_MODULEFINDER_H_INCLUDED -#include "../sentry_boot.h" -#include "../sentry_slice.h" +#include "sentry_boot.h" +#include "sentry_slice.h" typedef struct { void *start; diff --git a/src/windows/sentry_windows_modulefinder.c b/src/modulefinder/sentry_modulefinder_windows.c similarity index 95% rename from src/windows/sentry_windows_modulefinder.c rename to src/modulefinder/sentry_modulefinder_windows.c index 57bec3d75..946bcbd2d 100644 --- a/src/windows/sentry_windows_modulefinder.c +++ b/src/modulefinder/sentry_modulefinder_windows.c @@ -1,8 +1,8 @@ -#include "sentry_windows_modulefinder.h" +#include "sentry_boot.h" -#include "../sentry_sync.h" -#include "../sentry_uuid.h" -#include "../sentry_value.h" +#include "sentry_sync.h" +#include "sentry_uuid.h" +#include "sentry_value.h" #include #include @@ -116,7 +116,7 @@ load_modules(void) } sentry_value_t -sentry__windows_modules_get_list(void) +sentry__modules_get_list(void) { sentry__mutex_lock(&g_mutex); if (!g_initialized) { diff --git a/src/sentry_path.c b/src/path/sentry_path.c similarity index 100% rename from src/sentry_path.c rename to src/path/sentry_path.c diff --git a/src/unix/sentry_unix_path.c b/src/path/sentry_path_unix.c similarity index 98% rename from src/unix/sentry_unix_path.c rename to src/path/sentry_path_unix.c index 24697f5f6..017c48056 100644 --- a/src/unix/sentry_unix_path.c +++ b/src/path/sentry_path_unix.c @@ -1,8 +1,8 @@ -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_path.h" -#include "../sentry_string.h" -#include "../sentry_utils.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_path.h" +#include "sentry_string.h" +#include "sentry_utils.h" #include #include diff --git a/src/windows/sentry_windows_path.c b/src/path/sentry_path_windows.c similarity index 98% rename from src/windows/sentry_windows_path.c rename to src/path/sentry_path_windows.c index 31b475aee..a4720d17e 100644 --- a/src/windows/sentry_windows_path.c +++ b/src/path/sentry_path_windows.c @@ -1,10 +1,10 @@ -#include "../sentry_boot.h" +#include "sentry_boot.h" -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_path.h" -#include "../sentry_string.h" -#include "../sentry_utils.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_path.h" +#include "sentry_string.h" +#include "sentry_utils.h" #include #include diff --git a/src/sentry_alloc.c b/src/sentry_alloc.c index 03cb8168e..c0116d5e7 100644 --- a/src/sentry_alloc.c +++ b/src/sentry_alloc.c @@ -6,7 +6,7 @@ /* on unix platforms we add support for a simplistic page allocator that can be enabled to make code async safe */ #ifdef SENTRY_PLATFORM_UNIX -# include "unix/sentry_unix_pageallocator.h" +# include "sentry_unix_pageallocator.h" # define WITH_PAGE_ALLOCATOR #endif @@ -31,4 +31,4 @@ sentry_free(void *ptr) } #endif free(ptr); -} \ No newline at end of file +} diff --git a/src/sentry_modulefinder.c b/src/sentry_modulefinder.c deleted file mode 100644 index b2264ae1c..000000000 --- a/src/sentry_modulefinder.c +++ /dev/null @@ -1,34 +0,0 @@ -#include "sentry_modulefinder.h" - -#ifdef SENTRY_PLATFORM_LINUX -# include "linux/sentry_procmaps_modulefinder.h" -#endif -#ifdef SENTRY_PLATFORM_DARWIN -# include "darwin/sentry_darwin_modulefinder.h" -#endif -#ifdef SENTRY_PLATFORM_WINDOWS -# include "windows/sentry_windows_modulefinder.h" -#endif - -#define TRY_MODULEFINDER(Func) \ - do { \ - sentry_value_t rv = Func(); \ - if (!sentry_value_is_null(rv)) { \ - return rv; \ - } \ - } while (0) - -sentry_value_t -sentry__modules_get_list(void) -{ -#ifdef SENTRY_PLATFORM_LINUX - TRY_MODULEFINDER(sentry__procmaps_modules_get_list); -#endif -#ifdef SENTRY_PLATFORM_DARWIN - TRY_MODULEFINDER(sentry__darwin_modules_get_list); -#endif -#ifdef SENTRY_PLATFORM_WINDOWS - TRY_MODULEFINDER(sentry__windows_modules_get_list); -#endif - return sentry_value_new_null(); -} diff --git a/src/sentry_string.c b/src/sentry_string.c index 08a96c1df..dbff58122 100644 --- a/src/sentry_string.c +++ b/src/sentry_string.c @@ -113,3 +113,16 @@ sentry__string_clonen(const char *str, size_t n) rv[n] = 0; return rv; } + +#ifdef SENTRY_PLATFORM_WINDOWS +char * +sentry__string_from_wstr(wchar_t *s) +{ + int len = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL); + char *rv = sentry_malloc(len); + if (rv) { + WideCharToMultiByte(CP_UTF8, 0, s, -1, rv, len, NULL, NULL); + } + return rv; +} +#endif diff --git a/src/sentry_symbolizer.c b/src/sentry_symbolizer.c deleted file mode 100644 index 7a8b2ab48..000000000 --- a/src/sentry_symbolizer.c +++ /dev/null @@ -1,27 +0,0 @@ -#include "sentry_symbolizer.h" - -#ifdef SENTRY_PLATFORM_UNIX -# include "unix/sentry_unix_symbolizer_dladdr.h" -# define HAVE_DLADDR -#endif -#ifdef SENTRY_PLATFORM_WINDOWS -# include "windows/sentry_windows_dbghelp.h" -# define HAVE_DBGHELP -#endif - -bool -sentry__symbolize( - void *addr, void (*func)(const sentry_frame_info_t *, void *), void *data) -{ -#ifdef HAVE_DLADDR - if (sentry__symbolize_dladdr(addr, func, data)) { - return true; - } -#endif -#ifdef HAVE_DBGHELP - if (sentry__symbolize_dbghelp(addr, func, data)) { - return true; - } -#endif - return false; -} diff --git a/src/sentry_sync.c b/src/sentry_sync.c index 543b9a4ea..8ef1708c6 100644 --- a/src/sentry_sync.c +++ b/src/sentry_sync.c @@ -197,7 +197,7 @@ sentry__bgworker_submit(sentry_bgworker_t *bgw, } #ifdef SENTRY_PLATFORM_UNIX -# include "unix/sentry_unix_spinlock.h" +# include "sentry_unix_spinlock.h" static sig_atomic_t g_in_signal_handler = 0; static sentry_threadid_t g_signal_handling_thread; diff --git a/src/unix/sentry_unix_pageallocator.c b/src/sentry_unix_pageallocator.c similarity index 99% rename from src/unix/sentry_unix_pageallocator.c rename to src/sentry_unix_pageallocator.c index ce3d0b114..fe2742696 100644 --- a/src/unix/sentry_unix_pageallocator.c +++ b/src/sentry_unix_pageallocator.c @@ -1,5 +1,5 @@ #include "sentry_unix_pageallocator.h" -#include "../sentry_core.h" +#include "sentry_core.h" #include "sentry_unix_spinlock.h" #include diff --git a/src/unix/sentry_unix_pageallocator.h b/src/sentry_unix_pageallocator.h similarity index 89% rename from src/unix/sentry_unix_pageallocator.h rename to src/sentry_unix_pageallocator.h index f1fae828b..ed6efea3d 100644 --- a/src/unix/sentry_unix_pageallocator.h +++ b/src/sentry_unix_pageallocator.h @@ -1,7 +1,7 @@ #ifndef SENTRY_UNIX_PAGEALLOCATOR_H_INCLUDED #define SENTRY_UNIX_PAGEALLOCATOR_H_INCLUDED -#include "../sentry_boot.h" +#include "sentry_boot.h" bool sentry__page_allocator_enabled(void); void sentry__page_allocator_enable(void); @@ -11,4 +11,4 @@ void *sentry__page_allocator_alloc(size_t size); void sentry__page_allocator_disable(void); #endif -#endif \ No newline at end of file +#endif diff --git a/src/unix/sentry_unix_spinlock.h b/src/sentry_unix_spinlock.h similarity index 94% rename from src/unix/sentry_unix_spinlock.h rename to src/sentry_unix_spinlock.h index daff7ce10..489d2a354 100644 --- a/src/unix/sentry_unix_spinlock.h +++ b/src/sentry_unix_spinlock.h @@ -1,7 +1,7 @@ #ifndef SENTRY_UNIX_SPINLOCK_H_INCLUDED #define SENTRY_UNIX_SPINLOCK_H_INCLUDED -#include "../sentry_boot.h" +#include "sentry_boot.h" typedef volatile sig_atomic_t sentry_spinlock_t; @@ -18,4 +18,4 @@ typedef volatile sig_atomic_t sentry_spinlock_t; } #define sentry__spinlock_unlock(spinlock_ref) (*spinlock_ref = 0) -#endif \ No newline at end of file +#endif diff --git a/src/sentry_uuid.h b/src/sentry_uuid.h index d7c7cf057..209733fab 100644 --- a/src/sentry_uuid.h +++ b/src/sentry_uuid.h @@ -1,4 +1,3 @@ - #ifndef SENTRY_UUID_H_INCLUDED #define SENTRY_UUID_H_INCLUDED @@ -8,4 +7,4 @@ sentry_uuid_t sentry__uuid_from_native(GUID *guid); #endif -#endif \ No newline at end of file +#endif diff --git a/src/sentry_windows_dbghelp.c b/src/sentry_windows_dbghelp.c new file mode 100644 index 000000000..c4543d23a --- /dev/null +++ b/src/sentry_windows_dbghelp.c @@ -0,0 +1,24 @@ +#include "sentry_windows_dbghelp.h" + +#include "sentry_sync.h" + +#include + +static sentry_mutex_t g_sym_mutex = SENTRY__MUTEX_INIT; +static bool g_initialized; +static HANDLE g_proc = INVALID_HANDLE_VALUE; + +HANDLE +sentry__init_dbghelp(void) +{ + sentry__mutex_lock(&g_sym_mutex); + if (!g_initialized) { + DWORD options = SymGetOptions(); + SymSetOptions(options | SYMOPT_UNDNAME); + g_proc = GetCurrentProcess(); + SymInitialize(g_proc, NULL, TRUE); + g_initialized = true; + } + sentry__mutex_unlock(&g_sym_mutex); + return g_proc; +} diff --git a/src/sentry_windows_dbghelp.h b/src/sentry_windows_dbghelp.h new file mode 100644 index 000000000..8352c0c6b --- /dev/null +++ b/src/sentry_windows_dbghelp.h @@ -0,0 +1,8 @@ +#ifndef SENTRY_WINDOWS_DBGHELP_H_INCLUDED +#define SENTRY_WINDOWS_DBGHELP_H_INCLUDED + +#include "sentry_boot.h" + +HANDLE sentry__init_dbghelp(void); + +#endif diff --git a/src/unix/sentry_unix_symbolizer_dladdr.c b/src/symbolizer/sentry_symbolizer_unix.c similarity index 87% rename from src/unix/sentry_unix_symbolizer_dladdr.c rename to src/symbolizer/sentry_symbolizer_unix.c index 050ca6e7f..b4cbe453c 100644 --- a/src/unix/sentry_unix_symbolizer_dladdr.c +++ b/src/symbolizer/sentry_symbolizer_unix.c @@ -1,10 +1,12 @@ -#include "sentry_unix_symbolizer_dladdr.h" +#include "sentry_boot.h" + +#include "sentry_symbolizer.h" #include #include bool -sentry__symbolize_dladdr( +sentry__symbolize( void *addr, void (*func)(const sentry_frame_info_t *, void *), void *data) { Dl_info info; @@ -23,4 +25,4 @@ sentry__symbolize_dladdr( func(&frame_info, data); return true; -} \ No newline at end of file +} diff --git a/src/symbolizer/sentry_symbolizer_windows.c b/src/symbolizer/sentry_symbolizer_windows.c new file mode 100644 index 000000000..382669cb2 --- /dev/null +++ b/src/symbolizer/sentry_symbolizer_windows.c @@ -0,0 +1,39 @@ +#include "sentry_boot.h" + +#include "sentry_symbolizer.h" +#include "sentry_windows_dbghelp.h" + +#include +#include + +#define MAX_SYM 1024 + +bool +sentry__symbolize( + void *addr, void (*func)(const sentry_frame_info_t *, void *), void *data) +{ + HANDLE proc = sentry__init_dbghelp(); + + SYMBOL_INFO *sym = (SYMBOL_INFO *)_alloca(sizeof(SYMBOL_INFO) + MAX_SYM); + memset(sym, 0, sizeof(SYMBOL_INFO) + MAX_SYM); + sym->MaxNameLen = MAX_SYM; + sym->SizeOfStruct = sizeof(SYMBOL_INFO); + + if (!SymFromAddr(proc, (DWORD64)addr, 0, sym)) { + return false; + } + + char mod_name[MAX_PATH]; + GetModuleFileNameA((HMODULE)sym->ModBase, mod_name, sizeof(mod_name)); + + sentry_frame_info_t frame_info; + memset(&frame_info, 0, sizeof(sentry_frame_info_t)); + frame_info.load_addr = (void *)sym->ModBase; + frame_info.instruction_addr = addr; + frame_info.symbol_addr = (void *)sym->Address; + frame_info.symbol = sym->Name; + frame_info.object_name = mod_name; + func(&frame_info, data); + + return true; +} diff --git a/src/transports/sentry_disk_transport.c b/src/transports/sentry_disk_transport.c index 2e771eb48..00e9ceb26 100644 --- a/src/transports/sentry_disk_transport.c +++ b/src/transports/sentry_disk_transport.c @@ -1,9 +1,9 @@ #include "sentry_disk_transport.h" -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_envelope.h" -#include "../sentry_path.h" -#include "../sentry_string.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_envelope.h" +#include "sentry_path.h" +#include "sentry_string.h" static void send_envelope(sentry_transport_t *transport, sentry_envelope_t *envelope) diff --git a/src/transports/sentry_disk_transport.h b/src/transports/sentry_disk_transport.h index 14a9fdb48..fdf3ebd35 100644 --- a/src/transports/sentry_disk_transport.h +++ b/src/transports/sentry_disk_transport.h @@ -1,8 +1,8 @@ #ifndef SENTRY_TRANSPORTS_DISK_TRANSPORT_H_INCLUDED #define SENTRY_TRANSPORTS_DISK_TRANSPORT_H_INCLUDED -#include "../sentry_boot.h" -#include "../sentry_database.h" +#include "sentry_boot.h" +#include "sentry_database.h" sentry_transport_t *sentry_new_disk_transport(const sentry_run_t *run); diff --git a/src/transports/sentry_function_transport.c b/src/transports/sentry_function_transport.c index d9320ed89..734647341 100644 --- a/src/transports/sentry_function_transport.c +++ b/src/transports/sentry_function_transport.c @@ -1,9 +1,9 @@ #include "sentry_function_transport.h" -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_envelope.h" -#include "../sentry_string.h" -#include "../sentry_sync.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_envelope.h" +#include "sentry_string.h" +#include "sentry_sync.h" struct transport_state { void (*func)(sentry_envelope_t *envelope, void *data); @@ -49,4 +49,4 @@ sentry_new_function_transport( transport->shutdown_func = NULL; return transport; -} \ No newline at end of file +} diff --git a/src/transports/sentry_function_transport.h b/src/transports/sentry_function_transport.h index d1d5dc17a..3bc81fb18 100644 --- a/src/transports/sentry_function_transport.h +++ b/src/transports/sentry_function_transport.h @@ -1,9 +1,9 @@ #ifndef SENTRY_TRANSPORTS_FUNCTION_TRANSPORT_H_INCLUDED #define SENTRY_TRANSPORTS_FUNCTION_TRANSPORT_H_INCLUDED -#include "../sentry_boot.h" +#include "sentry_boot.h" sentry_transport_t *sentry_new_function_transport( void (*func)(sentry_envelope_t *envelope, void *data), void *data); -#endif \ No newline at end of file +#endif diff --git a/src/transports/sentry_libcurl_transport.c b/src/transports/sentry_libcurl_transport.c index 965043568..e6a5b232f 100644 --- a/src/transports/sentry_libcurl_transport.c +++ b/src/transports/sentry_libcurl_transport.c @@ -1,9 +1,9 @@ #include "sentry_libcurl_transport.h" -#include "../sentry_alloc.h" -#include "../sentry_core.h" -#include "../sentry_envelope.h" -#include "../sentry_string.h" -#include "../sentry_sync.h" +#include "sentry_alloc.h" +#include "sentry_core.h" +#include "sentry_envelope.h" +#include "sentry_string.h" +#include "sentry_sync.h" #include #include #include @@ -220,4 +220,4 @@ sentry__new_libcurl_transport(void) transport->shutdown_func = shutdown_transport; return transport; -} \ No newline at end of file +} diff --git a/src/transports/sentry_libcurl_transport.h b/src/transports/sentry_libcurl_transport.h index 7e256e775..da7759553 100644 --- a/src/transports/sentry_libcurl_transport.h +++ b/src/transports/sentry_libcurl_transport.h @@ -1,8 +1,8 @@ #ifndef SENTRY_TRANSPORTS_LIBCURL_TRANSPORT_H_INCLUDED #define SENTRY_TRANSPORTS_LIBCURL_TRANSPORT_H_INCLUDED -#include "../sentry_boot.h" +#include "sentry_boot.h" sentry_transport_t *sentry__new_libcurl_transport(void); -#endif \ No newline at end of file +#endif diff --git a/src/unix/sentry_unix_symbolizer_dladdr.h b/src/unix/sentry_unix_symbolizer_dladdr.h deleted file mode 100644 index 40b18353c..000000000 --- a/src/unix/sentry_unix_symbolizer_dladdr.h +++ /dev/null @@ -1,11 +0,0 @@ -#ifndef SENTRY_UNIX_SYMBOLIZER_DLADDR_H_INCLUDED -#define SENTRY_UNIX_SYMBOLIZER_DLADDR_H_INCLUDED - -#include "../sentry_boot.h" - -#include "../sentry_symbolizer.h" - -bool sentry__symbolize_dladdr( - void *addr, void (*func)(const sentry_frame_info_t *, void *), void *data); - -#endif diff --git a/src/unix/sentry_unix_unwinder_libbacktrace.h b/src/unix/sentry_unix_unwinder_libbacktrace.h deleted file mode 100644 index 4b72b3442..000000000 --- a/src/unix/sentry_unix_unwinder_libbacktrace.h +++ /dev/null @@ -1,9 +0,0 @@ -#ifndef SENTRY_UNIX_UNWINDER_LIBBACKTRACE_H_INCLUDED -#define SENTRY_UNIX_UNWINDER_LIBBACKTRACE_H_INCLUDED - -#include "../sentry_boot.h" - -size_t sentry__unwind_stack_backtrace( - void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames); - -#endif diff --git a/src/unix/sentry_unix_unwinder_libunwindstack.h b/src/unix/sentry_unix_unwinder_libunwindstack.h deleted file mode 100644 index f95e75c08..000000000 --- a/src/unix/sentry_unix_unwinder_libunwindstack.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef SENTRY_UNIX_UNWINDER_LIBUNWINDSTACK_H_INCLUDED -#define SENTRY_UNIX_UNWINDER_LIBUNWINDSTACK_H_INCLUDED - -#ifdef __cplusplus -extern "C" { -#endif - -#include "../sentry_boot.h" - -size_t sentry__unwind_stack_unwindstack( - void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/src/sentry_unwind.c b/src/unwinder/sentry_unwinder.c similarity index 57% rename from src/sentry_unwind.c rename to src/unwinder/sentry_unwinder.c index 78c0760c4..cc7ffecfb 100644 --- a/src/sentry_unwind.c +++ b/src/unwinder/sentry_unwinder.c @@ -1,36 +1,33 @@ #include "sentry_boot.h" -#ifdef SENTRY_PLATFORM_ANDROID -# include "unix/sentry_unix_unwinder_libunwindstack.h" -# define HAVE_LIBUNWINDSTACK -#elif defined(SENTRY_PLATFORM_DARWIN) || defined(SENTRY_PLATFORM_LINUX) -# include "unix/sentry_unix_unwinder_libbacktrace.h" -# define HAVE_LIBBACKTRACE -#elif defined(SENTRY_PLATFORM_WINDOWS) -# include "windows/sentry_windows_dbghelp.h" -# define HAVE_DBGHELP -#endif +#define DEFINE_UNWINDER(Func) \ + size_t sentry__unwind_stack_##Func(void *addr, \ + const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames) #define TRY_UNWINDER(Func) \ do { \ - size_t rv = Func(addr, uctx, ptrs, max_frames); \ + size_t rv = sentry__unwind_stack_##Func(addr, uctx, ptrs, max_frames); \ if (rv > 0) { \ return rv; \ } \ } while (0) +DEFINE_UNWINDER(libunwindstack); +DEFINE_UNWINDER(libbacktrace); +DEFINE_UNWINDER(dbghelp); + static size_t unwind_stack( void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames) { -#ifdef HAVE_LIBBACKTRACE - TRY_UNWINDER(sentry__unwind_stack_backtrace); +#ifdef SENTRY_WITH_UNWINDER_LIBUNWINDSTACK + TRY_UNWINDER(libunwindstack); #endif -#ifdef HAVE_LIBUNWINDSTACK - TRY_UNWINDER(sentry__unwind_stack_unwindstack); +#ifdef SENTRY_WITH_UNWINDER_LIBBACKTRACE + TRY_UNWINDER(libbacktrace); #endif -#ifdef HAVE_DBGHELP - TRY_UNWINDER(sentry__unwind_stack_dbghelp); +#ifdef SENTRY_WITH_UNWINDER_DBGHELP + TRY_UNWINDER(dbghelp); #endif return 0; } diff --git a/src/unwinder/sentry_unwinder_dbghelp.c b/src/unwinder/sentry_unwinder_dbghelp.c new file mode 100644 index 000000000..cee05e104 --- /dev/null +++ b/src/unwinder/sentry_unwinder_dbghelp.c @@ -0,0 +1,47 @@ +#include "sentry_windows_dbghelp.h" + +#include + +size_t +sentry__unwind_stack_dbghelp( + void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames) +{ + if (!uctx && !addr) { + return (size_t)CaptureStackBackTrace(1, (ULONG)max_frames, ptrs, 0); + } + + sentry__init_dbghelp(); + + CONTEXT *ctx = uctx->exception_ptrs.ContextRecord; + STACKFRAME64 stack_frame; + memset(&stack_frame, 0, sizeof(stack_frame)); + + size_t size = 0; +#if defined(_WIN64) + int machine_type = IMAGE_FILE_MACHINE_AMD64; + stack_frame.AddrPC.Offset = ctx->Rip; + stack_frame.AddrFrame.Offset = ctx->Rbp; + stack_frame.AddrStack.Offset = ctx->Rsp; +#else + int machine_type = IMAGE_FILE_MACHINE_I386; + stack_frame.AddrPC.Offset = ctx->Eip; + stack_frame.AddrFrame.Offset = ctx->Ebp; + stack_frame.AddrStack.Offset = ctx->Esp; +#endif + stack_frame.AddrPC.Mode = AddrModeFlat; + stack_frame.AddrFrame.Mode = AddrModeFlat; + stack_frame.AddrStack.Mode = AddrModeFlat; + + if (addr) { + stack_frame.AddrPC.Offset = (DWORD64)addr; + } + + while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(), + &stack_frame, &ctx, NULL, SymFunctionTableAccess64, + SymGetModuleBase64, NULL) + && size < max_frames) { + ptrs[size++] = (void *)stack_frame.AddrPC.Offset; + } + + return size; +} diff --git a/src/unix/sentry_unix_unwinder_libbacktrace.c b/src/unwinder/sentry_unwinder_libbacktrace.c similarity index 55% rename from src/unix/sentry_unix_unwinder_libbacktrace.c rename to src/unwinder/sentry_unwinder_libbacktrace.c index eb0a1da96..7ecc818c0 100644 --- a/src/unix/sentry_unix_unwinder_libbacktrace.c +++ b/src/unwinder/sentry_unwinder_libbacktrace.c @@ -1,24 +1,19 @@ -#include "sentry_unix_unwinder_libbacktrace.h" -#ifndef SENTRY_PLATFORM_ANDROID -# include -#endif +#include "sentry_boot.h" + +#include size_t -sentry__unwind_stack_backtrace( +sentry__unwind_stack_libbacktrace( void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames) { -#ifdef SENTRY_PLATFORM_ANDROID - return 0; -#else if (addr) { -# ifdef MAC_OS_X_VERSION_10_14 +#ifdef MAC_OS_X_VERSION_10_14 return backtrace_from_fp(addr, ptrs, max_frames); -# endif +#endif return 0; } else if (uctx) { return 0; } else { return backtrace(ptrs, max_frames); } -#endif } diff --git a/src/unix/sentry_unix_unwinder_libunwindstack.cpp b/src/unwinder/sentry_unwinder_libunwindstack.cpp similarity index 95% rename from src/unix/sentry_unix_unwinder_libunwindstack.cpp rename to src/unwinder/sentry_unwinder_libunwindstack.cpp index 1da7a1d0c..563e553fb 100644 --- a/src/unix/sentry_unix_unwinder_libunwindstack.cpp +++ b/src/unwinder/sentry_unwinder_libunwindstack.cpp @@ -1,4 +1,6 @@ -#include "sentry_unix_unwinder_libunwindstack.h" +extern "C" { +#include "sentry_boot.h" +} #include #include @@ -9,8 +11,10 @@ #include #include +extern "C" { + size_t -sentry__unwind_stack_unwindstack( +sentry__unwind_stack_libunwindstack( void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames) { std::unique_ptr regs; @@ -63,3 +67,4 @@ sentry__unwind_stack_unwindstack( return rv; } +} diff --git a/src/windows/sentry_windows_dbghelp.c b/src/windows/sentry_windows_dbghelp.c deleted file mode 100644 index 253f51459..000000000 --- a/src/windows/sentry_windows_dbghelp.c +++ /dev/null @@ -1,100 +0,0 @@ -#include "sentry_windows_dbghelp.h" - -#include "../sentry_sync.h" - -#include -#include - -static sentry_mutex_t g_sym_mutex = SENTRY__MUTEX_INIT; -static bool g_initialized; -static HANDLE g_proc = INVALID_HANDLE_VALUE; - -#define MAX_SYM 1024 - -static void -init_dbghelp(void) -{ - sentry__mutex_lock(&g_sym_mutex); - if (!g_initialized) { - DWORD options = SymGetOptions(); - SymSetOptions(options | SYMOPT_UNDNAME); - g_proc = GetCurrentProcess(); - SymInitialize(g_proc, NULL, TRUE); - g_initialized = true; - } - sentry__mutex_unlock(&g_sym_mutex); -} - -bool -sentry__symbolize_dbghelp( - void *addr, void (*func)(const sentry_frame_info_t *, void *), void *data) -{ - init_dbghelp(); - - SYMBOL_INFO *sym = (SYMBOL_INFO *)_alloca(sizeof(SYMBOL_INFO) + MAX_SYM); - memset(sym, 0, sizeof(SYMBOL_INFO) + MAX_SYM); - sym->MaxNameLen = MAX_SYM; - sym->SizeOfStruct = sizeof(SYMBOL_INFO); - - if (!SymFromAddr(g_proc, (DWORD64)addr, 0, sym)) { - return false; - } - - char mod_name[MAX_PATH]; - GetModuleFileNameA((HMODULE)sym->ModBase, mod_name, sizeof(mod_name)); - - sentry_frame_info_t frame_info; - memset(&frame_info, 0, sizeof(sentry_frame_info_t)); - frame_info.load_addr = (void *)sym->ModBase; - frame_info.instruction_addr = addr; - frame_info.symbol_addr = (void *)sym->Address; - frame_info.symbol = sym->Name; - frame_info.object_name = mod_name; - func(&frame_info, data); - - return true; -} - -size_t -sentry__unwind_stack_dbghelp( - void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames) -{ - if (!uctx && !addr) { - return (size_t)CaptureStackBackTrace(1, (ULONG)max_frames, ptrs, 0); - } - - init_dbghelp(); - - CONTEXT *ctx = uctx->exception_ptrs.ContextRecord; - STACKFRAME64 stack_frame; - memset(&stack_frame, 0, sizeof(stack_frame)); - - size_t size = 0; -#if defined(_WIN64) - int machine_type = IMAGE_FILE_MACHINE_AMD64; - stack_frame.AddrPC.Offset = ctx->Rip; - stack_frame.AddrFrame.Offset = ctx->Rbp; - stack_frame.AddrStack.Offset = ctx->Rsp; -#else - int machine_type = IMAGE_FILE_MACHINE_I386; - stack_frame.AddrPC.Offset = ctx->Eip; - stack_frame.AddrFrame.Offset = ctx->Ebp; - stack_frame.AddrStack.Offset = ctx->Esp; -#endif - stack_frame.AddrPC.Mode = AddrModeFlat; - stack_frame.AddrFrame.Mode = AddrModeFlat; - stack_frame.AddrStack.Mode = AddrModeFlat; - - if (addr) { - stack_frame.AddrPC.Offset = (DWORD64)addr; - } - - while (StackWalk64(machine_type, GetCurrentProcess(), GetCurrentThread(), - &stack_frame, &ctx, NULL, SymFunctionTableAccess64, - SymGetModuleBase64, NULL) - && size < max_frames) { - ptrs[size++] = (void *)stack_frame.AddrPC.Offset; - } - - return size; -} diff --git a/src/windows/sentry_windows_dbghelp.h b/src/windows/sentry_windows_dbghelp.h deleted file mode 100644 index fd468eb53..000000000 --- a/src/windows/sentry_windows_dbghelp.h +++ /dev/null @@ -1,13 +0,0 @@ -#ifndef SENTRY_WINDOWS_DBGHELP_H_INCLUDED -#define SENTRY_WINDOWS_DBGHELP_H_INCLUDED - -#include "../sentry_boot.h" - -#include "../sentry_symbolizer.h" - -bool sentry__symbolize_dbghelp( - void *addr, void (*func)(const sentry_frame_info_t *, void *), void *data); -size_t sentry__unwind_stack_dbghelp( - void *addr, const sentry_ucontext_t *uctx, void **ptrs, size_t max_frames); - -#endif diff --git a/src/windows/sentry_windows_modulefinder.h b/src/windows/sentry_windows_modulefinder.h deleted file mode 100644 index 348c3b16f..000000000 --- a/src/windows/sentry_windows_modulefinder.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef SENTRY_WINDOWS_MODULEFINDER_H_INCLUDED -#define SENTRY_WINDOWS_MODULEFINDER_H_INCLUDED - -#include "../sentry_boot.h" - -sentry_value_t sentry__windows_modules_get_list(void); - -#endif diff --git a/src/windows/sentry_windows_string.c b/src/windows/sentry_windows_string.c deleted file mode 100644 index 50592a29c..000000000 --- a/src/windows/sentry_windows_string.c +++ /dev/null @@ -1,12 +0,0 @@ -#include "../sentry_string.h" - -char * -sentry__string_from_wstr(wchar_t *s) -{ - int len = WideCharToMultiByte(CP_UTF8, 0, s, -1, NULL, 0, NULL, NULL); - char *rv = sentry_malloc(len); - if (rv) { - WideCharToMultiByte(CP_UTF8, 0, s, -1, rv, len, NULL, NULL); - } - return rv; -} diff --git a/tests/sentry_testsupport.h b/tests/sentry_testsupport.h index 1c36e9245..2ef5ee882 100644 --- a/tests/sentry_testsupport.h +++ b/tests/sentry_testsupport.h @@ -1,4 +1,4 @@ -#include "../src/sentry_boot.h" +#include "sentry_boot.h" #include #include diff --git a/tests/test_attachments.c b/tests/test_attachments.c index fe6b2b9d3..05314714b 100644 --- a/tests/test_attachments.c +++ b/tests/test_attachments.c @@ -1,8 +1,8 @@ -#include "../src/sentry_envelope.h" -#include "../src/sentry_path.h" -#include "../src/sentry_string.h" -#include "../src/transports/sentry_function_transport.h" +#include "sentry_envelope.h" +#include "sentry_path.h" +#include "sentry_string.h" #include "sentry_testsupport.h" +#include "transports/sentry_function_transport.h" #include typedef struct { diff --git a/tests/test_basic.c b/tests/test_basic.c index f4766994a..6fb00cf58 100644 --- a/tests/test_basic.c +++ b/tests/test_basic.c @@ -1,5 +1,5 @@ -#include "../src/transports/sentry_function_transport.h" #include "sentry_testsupport.h" +#include "transports/sentry_function_transport.h" #include static void diff --git a/tests/test_consent.c b/tests/test_consent.c index 6add258a3..5f04779c8 100644 --- a/tests/test_consent.c +++ b/tests/test_consent.c @@ -1,4 +1,4 @@ -#include "../src/sentry_path.h" +#include "sentry_path.h" #include "sentry_testsupport.h" #include diff --git a/tests/test_database.c b/tests/test_database.c index a422027ac..7690eb614 100644 --- a/tests/test_database.c +++ b/tests/test_database.c @@ -1,6 +1,6 @@ -#include "../src/sentry_envelope.h" -#include "../src/sentry_value.h" +#include "sentry_envelope.h" #include "sentry_testsupport.h" +#include "sentry_value.h" #include typedef struct { diff --git a/tests/test_envelopes.c b/tests/test_envelopes.c index 9b8f53e95..558e29016 100644 --- a/tests/test_envelopes.c +++ b/tests/test_envelopes.c @@ -1,6 +1,6 @@ -#include "../src/sentry_envelope.h" -#include "../src/sentry_value.h" +#include "sentry_envelope.h" #include "sentry_testsupport.h" +#include "sentry_value.h" #include static bool diff --git a/tests/test_modulefinder.c b/tests/test_modulefinder.c index d04e3ec3f..34cd36f08 100644 --- a/tests/test_modulefinder.c +++ b/tests/test_modulefinder.c @@ -1,9 +1,9 @@ -#include "../src/sentry_modulefinder.h" +#include "sentry_modulefinder.h" #include "sentry_testsupport.h" #include #ifdef SENTRY_PLATFORM_LINUX -# include "../src/linux/sentry_procmaps_modulefinder.h" +# include "modulefinder/sentry_modulefinder_linux.h" #endif SENTRY_TEST(test_module_finder) diff --git a/tests/test_path.c b/tests/test_path.c index a697a826d..cc7dd2d6e 100644 --- a/tests/test_path.c +++ b/tests/test_path.c @@ -1,4 +1,4 @@ -#include "../src/sentry_path.h" +#include "sentry_path.h" #include "sentry_testsupport.h" #include diff --git a/tests/test_symbolizer.c b/tests/test_symbolizer.c index bb688c492..06bd23dd8 100644 --- a/tests/test_symbolizer.c +++ b/tests/test_symbolizer.c @@ -1,4 +1,4 @@ -#include "../src/sentry_symbolizer.h" +#include "sentry_symbolizer.h" #include "sentry_testsupport.h" #include diff --git a/tests/test_sync.c b/tests/test_sync.c index 75de3982b..bad1a73cf 100644 --- a/tests/test_sync.c +++ b/tests/test_sync.c @@ -1,4 +1,4 @@ -#include "../src/sentry_sync.h" +#include "sentry_sync.h" #include "sentry_testsupport.h" struct task_state { @@ -41,4 +41,4 @@ SENTRY_TEST(background_worker) TEST_CHECK_INT_EQUAL(ts.executed, 10); TEST_CHECK(!ts.running); } -} \ No newline at end of file +} diff --git a/tests/test_unwinder.c b/tests/test_unwinder.c index 160c0e780..f620ab83e 100644 --- a/tests/test_unwinder.c +++ b/tests/test_unwinder.c @@ -1,4 +1,4 @@ -#include "../src/sentry_symbolizer.h" +#include "sentry_symbolizer.h" #include "sentry_testsupport.h" #include diff --git a/tests/test_utils.c b/tests/test_utils.c index 2e9561def..4adc022ec 100644 --- a/tests/test_utils.c +++ b/tests/test_utils.c @@ -1,9 +1,9 @@ -#include "../src/sentry_utils.h" #include "sentry_testsupport.h" +#include "sentry_utils.h" #include #ifdef SENTRY_PLATFORM_UNIX -# include "../src/unix/sentry_unix_pageallocator.h" +# include "sentry_unix_pageallocator.h" #endif SENTRY_TEST(url_parsing_complete) diff --git a/tests/test_value.c b/tests/test_value.c index 3e294c83c..552c6775a 100644 --- a/tests/test_value.c +++ b/tests/test_value.c @@ -1,5 +1,5 @@ -#include "../src/sentry_value.h" #include "sentry_testsupport.h" +#include "sentry_value.h" #include SENTRY_TEST(value_null)