Skip to content
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
25 changes: 25 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ jobs:
- name: clang
flags: -DCOUNTLY_USE_SQLITE=ON -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++
apt: clang
- name: breakpad
flags: -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_BREAKPAD=ON

env:
ASAN_OPTIONS: "detect_leaks=1"
Expand Down Expand Up @@ -130,6 +132,29 @@ jobs:
- name: Run tests
run: ./build/countly-tests

# ──────────────────────────────────────────────
# Native crash capture with Breakpad on Windows (MSVC).
# The tests crash a helper process and send its dump.
# ──────────────────────────────────────────────
breakpad-windows:
name: breakpad-windows
runs-on: windows-2025
timeout-minutes: 30
steps:
- name: Checkout code
uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
submodules: "recursive"

- name: Configure
run: cmake -DCOUNTLY_BUILD_TESTS=1 -DCOUNTLY_USE_SQLITE=ON -DCOUNTLY_USE_BREAKPAD=ON -B build .

- name: Build
run: cmake --build build --config Release -j 4

- name: Run tests
run: ./build/Release/countly-tests.exe

# ──────────────────────────────────────────────
# Oldest supported CMake (3.13, as documented).
# Catches accidental use of newer CMake features.
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
- Added `setCrashFilterCallback` for changing or dropping crash reports before they are queued, and `setCustomCrashSegmentation` for segmentation added to every crash report.
- Added `crash().clearBreadcrumbs()`, `views().stopAllViews()`, and segmentation for `closeViewWithName` and `closeViewWithID`.
- Added `disableLocation()`. A location given with `setLocation` before `start()` is now used from `start()` on.
- Added `setCrashDumpDirectory()` for native crashes. At `start()` the SDK sends every minidump (`*.dmp`) in that directory as an unhandled crash with `_native_cpp`, then deletes the file. Only the default instance handles native crashes.
- A native crash dump is sent Base64-encoded in `_error` and is never cut by the internal limits. Without the "crashes" consent it is deleted unsent; while the server's "Allow crash tracking" is off, it stays for a later start.
- A native crash dump larger than 10 MB is deleted without being sent and logged as an error, because the server does not accept a request that large.
- Added `enableCrashReporting()` and the CMake option `COUNTLY_USE_BREAKPAD` (off by default; Windows and Linux). The SDK's Breakpad handler writes a minidump on a crash, then calls the handler that was installed before it.
- The minidumps of the SDK's crash handler hold the breadcrumbs and the app version of the crash moment. The server's "Automatic crash reporting" setting (`acr`) turns the handler on and off.
- The vendored Breakpad client (`vendor/breakpad`, updated with `scripts/update_breakpad.py`) needs a C++17 compiler; the SDK itself still builds with C++14.

- Mitigated an issue where reaching the event queue threshold logged a warning that events were dropped, although they were sent.
- Mitigated an issue where a view that was open during a device ID change without merge was ended under the new device ID.
Expand Down
48 changes: 46 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ option(COUNTLY_USE_CUSTOM_SHA256 "Use a custom SHA 256 library" OFF)
option(COUNTLY_USE_SQLITE "Use SQLite" OFF)
option(COUNTLY_USE_SYSTEM_SQLITE "Link the system SQLite instead of compiling the vendored amalgamation" OFF)
option(COUNTLY_USE_SYSTEM_JSON "Compile against the system nlohmann_json instead of the vendored copy" OFF)
option(COUNTLY_USE_BREAKPAD "Capture native crashes with the vendored Breakpad client (Windows, Linux)" OFF)
option(COUNTLY_BUILD_TESTS "Build test programs" OFF)
option(COUNTLY_BUILD_SAMPLE "Build Sample programs" OFF)

Expand All @@ -30,6 +31,7 @@ message("Use a custom SHA 256 library:" ${COUNTLY_USE_CUSTOM_SHA256})
message("Use SQLite:" ${COUNTLY_USE_SQLITE})
message("Use system SQLite:" ${COUNTLY_USE_SYSTEM_SQLITE})
message("Use system nlohmann_json:" ${COUNTLY_USE_SYSTEM_JSON})
message("Use Breakpad:" ${COUNTLY_USE_BREAKPAD})
message("Build test programs:" ${COUNTLY_BUILD_TESTS})
message("Build Sample programs:" ${COUNTLY_BUILD_SAMPLE})

Expand Down Expand Up @@ -65,6 +67,7 @@ add_library(countly
${CMAKE_CURRENT_SOURCE_DIR}/src/configuration_module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/consent_module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/health_check_module.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/native_crash.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/platform_info.cpp)
add_library(countly::countly ALIAS countly)

Expand Down Expand Up @@ -144,6 +147,37 @@ if(COUNTLY_USE_SQLITE)
endif()
endif()

if(COUNTLY_USE_BREAKPAD)
if(NOT WIN32 AND NOT CMAKE_SYSTEM_NAME STREQUAL "Linux")
message(FATAL_ERROR "COUNTLY_USE_BREAKPAD supports Windows and Linux only.")
endif()
message("Compiling the vendored Breakpad client")
include(${CMAKE_CURRENT_SOURCE_DIR}/vendor/breakpad/sources.cmake)
if(WIN32)
set(COUNTLY_BREAKPAD_SOURCES ${COUNTLY_BREAKPAD_WINDOWS_SOURCES})
else()
set(COUNTLY_BREAKPAD_SOURCES ${COUNTLY_BREAKPAD_LINUX_SOURCES})
include(CheckFunctionExists)
check_function_exists(getcontext COUNTLY_HAVE_GETCONTEXT)
if(NOT COUNTLY_HAVE_GETCONTEXT)
enable_language(ASM)
list(APPEND COUNTLY_BREAKPAD_SOURCES ${COUNTLY_BREAKPAD_LINUX_GETCONTEXT_SOURCE})
endif()
endif()
# An object library inside the countly library: Breakpad needs C++17 and its own definitions, the SDK stays on C++14
add_library(countly_breakpad OBJECT ${COUNTLY_BREAKPAD_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/src/native_crash_breakpad.cpp)
target_include_directories(countly_breakpad PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/vendor/breakpad/src)
set_target_properties(countly_breakpad PROPERTIES POSITION_INDEPENDENT_CODE ON CXX_EXTENSIONS NO)
target_compile_features(countly_breakpad PRIVATE cxx_std_17)
if(WIN32)
target_compile_definitions(countly_breakpad PRIVATE UNICODE _UNICODE)
elseif(COUNTLY_HAVE_GETCONTEXT)
target_compile_definitions(countly_breakpad PRIVATE HAVE_GETCONTEXT=1)
endif()
target_sources(countly PRIVATE $<TARGET_OBJECTS:countly_breakpad>)
target_compile_definitions(countly PRIVATE COUNTLY_USE_BREAKPAD)
endif()

if(COUNTLY_BUILD_TESTS)
message("Building test directories")
add_executable(countly-tests
Expand Down Expand Up @@ -175,11 +209,21 @@ if(COUNTLY_BUILD_TESTS)
${CMAKE_CURRENT_SOURCE_DIR}/tests/crash_reporting.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/location.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/server_url_app_key.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/request_scenarios.cpp)

${CMAKE_CURRENT_SOURCE_DIR}/tests/request_scenarios.cpp
${CMAKE_CURRENT_SOURCE_DIR}/tests/native_crash.cpp)

target_compile_options(countly-tests PRIVATE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-g>)
# the request scenarios read and write their records under api/requests/
target_compile_definitions(countly-tests PRIVATE COUNTLY_BUILD_TESTS COUNTLY_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}")
# the native crash tests use the SDK's internal dump helpers
target_include_directories(countly-tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
if(COUNTLY_USE_BREAKPAD)
add_executable(countly-native-crash-helper ${CMAKE_CURRENT_SOURCE_DIR}/tests/native_crash_helper.cpp)
target_link_libraries(countly-native-crash-helper countly)
target_compile_features(countly-native-crash-helper PRIVATE cxx_std_14)
add_dependencies(countly-tests countly-native-crash-helper)
target_compile_definitions(countly-tests PRIVATE COUNTLY_USE_BREAKPAD COUNTLY_CRASH_HELPER_PATH="$<TARGET_FILE:countly-native-crash-helper>")
endif()
if(COUNTLY_USE_SQLITE)
if(COUNTLY_USE_SYSTEM_SQLITE)
target_link_libraries(countly-tests SQLite::SQLite3)
Expand Down
7 changes: 7 additions & 0 deletions api/countly-sdk-cpp.api
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ cly::ConfigurationModule class public
cly::ConfigurationModule::ConfigurationModule constructor void (cly::CountlyDelegates *, std::shared_ptr<CountlyConfiguration>, std::shared_ptr<LoggerModule>, std::shared_ptr<RequestBuilder>, std::shared_ptr<StorageModuleBase>, std::shared_ptr<RequestModule>, std::shared_ptr<std::mutex>) public
cly::ConfigurationModule::fetchConfigFromServer method void (nlohmann::json) public
cly::ConfigurationModule::fetchConfigFromStorage method void () public
cly::ConfigurationModule::getAutomaticCrashReporting method int () const public
cly::ConfigurationModule::getAutomaticSessionTracking method int () const public
cly::ConfigurationModule::getBackoffAcceptedTimeout method unsigned int () const public
cly::ConfigurationModule::getBackoffDuration method unsigned int () const public
Expand Down Expand Up @@ -65,6 +66,7 @@ cly::Countly::ConsentRequirementChanged method void () public
cly::Countly::Countly constructor void () public
cly::Countly::Countly constructor void (Countly &&) public
cly::Countly::Countly constructor void (const Countly &) public
cly::Countly::CrashReportingSettingsChanged method void () public
cly::Countly::CurrentViewId method std::string () public
cly::Countly::DeviceMetrics method nlohmann::json () public
cly::Countly::IsSessionRunning method bool () public
Expand Down Expand Up @@ -111,6 +113,7 @@ cly::Countly::disableGradualRequestCleaner method void () public
cly::Countly::disableHealthCheck method void () public
cly::Countly::disableLocation method void () public
cly::Countly::disableSDKBehaviorSettingsUpdates method void () public
cly::Countly::enableCrashReporting method void () public
cly::Countly::enableImmediateRequestOnStop method void () public
cly::Countly::enableManualSessionControl method void () public
cly::Countly::enableRemoteConfig method void () public
Expand Down Expand Up @@ -146,6 +149,7 @@ cly::Countly::setConsent method void (const std::vector<std::string> &, bool) pu
cly::Countly::setConsentEnabled method void (const std::vector<std::string> &) public
cly::Countly::setConsentFeatureGroup method void (const std::string &, bool) public
cly::Countly::setCountry method void (const std::string &) public
cly::Countly::setCrashDumpDirectory method void (const std::string &) public
cly::Countly::setCrashFilterCallback method void (CrashFilterCallback) public
cly::Countly::setCustomCrashSegmentation method void (const std::map<std::string, std::string> &) public
cly::Countly::setCustomUserDetails method void (const std::map<std::string, std::string> &) public
Expand Down Expand Up @@ -189,9 +193,11 @@ cly::CountlyConfiguration struct public
cly::CountlyConfiguration::CountlyConfiguration constructor void (const std::string, std::string) public
cly::CountlyConfiguration::appKey field std::string public
cly::CountlyConfiguration::autoEventsOnUserProperties field bool public
cly::CountlyConfiguration::automaticCrashReporting field bool public
cly::CountlyConfiguration::backoffMechanismEnabled field bool public
cly::CountlyConfiguration::breadcrumbsThreshold field unsigned int public
cly::CountlyConfiguration::clearStoredDeviceID field bool public
cly::CountlyConfiguration::crashDumpDirectory field std::string public
cly::CountlyConfiguration::crashFilter field CrashFilterCallback public
cly::CountlyConfiguration::crashSegmentation field std::map<std::string, std::string> public
cly::CountlyConfiguration::databasePath field std::string public
Expand Down Expand Up @@ -226,6 +232,7 @@ cly::CountlyConfiguration::sha256_function field SHA256Function public
cly::CountlyDelegates class public
cly::CountlyDelegates::AutomaticSessionTrackingChanged method void () virtual public
cly::CountlyDelegates::ConsentRequirementChanged method void () virtual public
cly::CountlyDelegates::CrashReportingSettingsChanged method void () virtual public
cly::CountlyDelegates::CurrentViewId method std::string () virtual public
cly::CountlyDelegates::DeviceMetrics method nlohmann::json () virtual public
cly::CountlyDelegates::IsSessionRunning method bool () virtual public
Expand Down
50 changes: 50 additions & 0 deletions include/countly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,29 @@ class Countly : public cly::CountlyDelegates {
*/
void setCrashFilterCallback(CrashFilterCallback callback);

/**
* Sets the directory of native crash dumps, the minidump files ("*.dmp") that a crash handler writes when the process
* crashes. At start() the SDK sends every dump in it as an unhandled crash, Base64-encoded in "_error" with
* "_native_cpp", and deletes the file; without the "crashes" consent it deletes the file without sending, and while
* the server's "Allow crash tracking" setting is off it leaves the files for a later start. A dump larger than 10 MB is
* deleted without being sent, because the server does not accept it. The dumps can come from the SDK's own handler
* (enableCrashReporting()) or from another tool, for example the LocalDumps of Windows Error Reporting. Native crashes
* belong to the whole process, so only the default instance (getInstance()) handles them. Call it before start().
*
* @param path: an existing directory that holds only crash dumps, best as an absolute path
*/
void setCrashDumpDirectory(const std::string &path);

/**
* Turns on the automatic capture of native crashes. From start() on, the SDK's crash handler writes a minidump into
* the directory of setCrashDumpDirectory() when the process crashes, with the breadcrumbs and the app version of that
* moment, and the next start() sends it. Then the handler that was installed before runs, so the process ends the way
* it would without the SDK. The handler needs a build with COUNTLY_USE_BREAKPAD (Windows, Linux) and the
* "crashes" consent; the server's "Automatic crash reporting" and "Allow crash tracking" settings turn it off and on.
* stop() removes it. Only the default instance (getInstance()) can install it. Call it before start().
*/
void enableCrashReporting();

/**
* Turns location tracking off until setLocation is called with a value: the stored location values are cleared,
* an erase request is sent, and every session start carries the empty location, so that the server works out no
Expand Down Expand Up @@ -348,6 +371,12 @@ class Countly : public cly::CountlyDelegates {
*/
void AutomaticSessionTrackingChanged() override;

/**
* Applies a change of the server's "Automatic crash reporting" or "Allow crash tracking" setting: the native crash
* handler is installed or removed to match.
*/
void CrashReportingSettingsChanged() override;

/*
* Checks and returns the size of the event queue in memory or persistent storage.
*/
Expand Down Expand Up @@ -998,6 +1027,27 @@ class Countly : public cly::CountlyDelegates {
* state and, when location is off and no session start will be sent, a location erase. Called at the end of start().
*/
void activateConsent();

/**
* Whether this instance is the default instance, the one getInstance() returns.
*/
bool isDefaultInstance();

/**
* Sends every dump of the crash dump directory as a native crash and deletes the files. Called at the end of start().
*/
void sendNativeCrashDumps();

/**
* Installs the native crash handler when the app or the server's "acr" setting turned automatic capture on, the
* crash dump directory is set, crash tracking is allowed, the "crashes" consent is given and the SDK runs; otherwise
* removes it.
*/
void updateNativeCrashHandler();
// Whether this instance installed the process-wide native crash handler. Guarded by native_crash_mutex.
bool owns_native_crash_handler = false;
// Taken after consent_change_mutex and before the instance mutex
std::mutex native_crash_mutex;
bool began_session = false;
// Set by disableLocation(), cleared by setLocation() with a value; the server's "lt" setting wins over it
std::atomic<bool> location_disabled_by_app{false};
Expand Down
6 changes: 6 additions & 0 deletions include/countly/configuration_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ class ConfigurationModule : public ConfigurationProvider {
*/
int getAutomaticSessionTracking() const;

/**
* The server's "Automatic crash reporting" setting ("acr"): -1 while the settings don't contain it, 0 when it is
* off, 1 when it is on.
*/
int getAutomaticCrashReporting() const;

unsigned int getRequestQueueSizeLimit() const override;
unsigned int getRequestDropAgeHours() const override;
bool isBackoffEnabled() const override;
Expand Down
6 changes: 6 additions & 0 deletions include/countly/constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,12 @@ class CountlyDelegates {
*/
virtual void LocationSettingChanged() {}

/**
* Called by the SDK behavior settings when the server's "Automatic crash reporting" or "Allow crash tracking" setting
* changes.
*/
virtual void CrashReportingSettingsChanged() {}

virtual void RecordEvent(const std::string &key, int count) = 0;

virtual void RecordEvent(const std::string &key, int count, double sum) = 0;
Expand Down
11 changes: 11 additions & 0 deletions include/countly/countly_configuration.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,17 @@ struct CountlyConfiguration {
*/
CrashFilterCallback crashFilter = nullptr;

/**
* The directory of native crash dumps ("*.dmp"), empty when the app set none.
*/
// cppcheck-suppress unusedStructMember ; read and written by Countly in countly.cpp
std::string crashDumpDirectory;

/**
* Whether the app turned on the automatic capture of native crashes with enableCrashReporting().
*/
bool automaticCrashReporting = false;

/**
* Set the maximum amount of breadcrumbs.
*/
Expand Down
13 changes: 13 additions & 0 deletions include/countly/crash_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,19 @@ class CrashModule {
* Forgets every breadcrumb, without counting it as a call of the app.
*/
void forgetBreadcrumbs();

/**
* Turns on or off the copy of the crash-time metrics and the breadcrumbs into the memory block that the native crash
* handler's dumps contain. On while the handler is installed.
*/
void setNativeContextEnabled(bool enabled);

/**
* Queues a native crash dump as an unhandled crash: the dump goes Base64-encoded into "_error" with "_native_cpp",
* and is never cut by the internal limits. The metrics and breadcrumbs saved in the dump at crash time win over the
* current ones. The crash filter sees the report. The caller checks the "crashes" consent.
*/
void recordNativeCrash(const std::string &dump);
};
} // namespace cly
#endif
Loading
Loading