diff --git a/CHANGELOG.md b/CHANGELOG.md index 790e0db36e..dcf0c4a2a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Changelog +## Unreleased + +**Features** + +- Windows: add WER integration for syncing tags and attachments to WER. ([#1837](https://github.com/getsentry/sentry-native/pull/1837)) + ## 0.15.3 **Features** diff --git a/CMakeLists.txt b/CMakeLists.txt index bacef22283..8f2f039f71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -952,6 +952,8 @@ if(SENTRY_INTEGRATION_QT) target_link_libraries(sentry PRIVATE Qt${Qt_VERSION_MAJOR}::Core) endif() +option(SENTRY_INTEGRATION_WER "Build WER (Windows Error Reporting) integration") + include(CMakePackageConfigHelpers) configure_package_config_file(sentry-config.cmake.in sentry-config.cmake INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKEDIR}") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 20a8036243..57c57dead8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -306,6 +306,13 @@ if(SENTRY_INTEGRATION_QT) integrations/sentry_integration_qt.h ) endif() +if(SENTRY_INTEGRATION_WER AND WIN32) + target_compile_definitions(sentry PRIVATE SENTRY_INTEGRATION_WER) + sentry_target_sources_cwd(sentry + integrations/sentry_integration_wer.c + integrations/sentry_integration_wer.h + ) +endif() # screenshot if(SENTRY_SCREENSHOT_WINDOWS) diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c new file mode 100644 index 0000000000..fee237bcaf --- /dev/null +++ b/src/integrations/sentry_integration_wer.c @@ -0,0 +1,191 @@ +#include "sentry_integration_wer.h" + +#include "sentry_alloc.h" +#include "sentry_attachment.h" +#include "sentry_logger.h" +#include "sentry_string.h" + +#include + +// Windows 8+ SDK +#ifndef WER_FILE_ANONYMOUS_DATA +# define WER_FILE_ANONYMOUS_DATA 0x2 +#endif + +// Windows 10 1703+ +static HRESULT(WINAPI *g_WerRegisterCustomMetadata)(PCWSTR, PCWSTR) = NULL; +static HRESULT(WINAPI *g_WerUnregisterCustomMetadata)(PCWSTR) = NULL; + +static void +wer_init(void) +{ + if (g_WerRegisterCustomMetadata) { + return; + } + HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll"); + if (kernel32) { + g_WerRegisterCustomMetadata = (HRESULT(WINAPI *)(PCWSTR, + PCWSTR))GetProcAddress(kernel32, "WerRegisterCustomMetadata"); + g_WerUnregisterCustomMetadata = (HRESULT(WINAPI *)( + PCWSTR))GetProcAddress(kernel32, "WerUnregisterCustomMetadata"); + } + if (!g_WerRegisterCustomMetadata) { + SENTRY_DEBUG("WerRegisterCustomMetadata not available; " + "tag sync to WER will be skipped"); + } +} + +static void +wer_set_tag(void *data, const char *key, size_t key_len, const char *value, + size_t value_len) +{ + (void)data; + if (!g_WerRegisterCustomMetadata) { + return; + } + if (!key || !value) { + return; + } + + char *key_n = sentry__string_clone_n(key, key_len); + char *value_n = sentry__string_clone_n(value, value_len); + wchar_t *key_w = sentry__string_to_wstr(key_n); + wchar_t *value_w = sentry__string_to_wstr(value_n); + sentry_free(key_n); + sentry_free(value_n); + + if (!key_w || !value_w) { + sentry_free(key_w); + sentry_free(value_w); + return; + } + + HRESULT hr = g_WerRegisterCustomMetadata(key_w, value_w); + if (FAILED(hr)) { + SENTRY_WARNF( + "WerRegisterCustomMetadata failed: hr=0x%08lx", (unsigned long)hr); + } + + sentry_free(value_w); + sentry_free(key_w); +} + +static void +wer_remove_tag(void *data, const char *key, size_t key_len) +{ + (void)data; + if (!g_WerUnregisterCustomMetadata) { + return; + } + if (!key) { + return; + } + + char *key_n = sentry__string_clone_n(key, key_len); + wchar_t *key_w = sentry__string_to_wstr(key_n); + sentry_free(key_n); + + if (!key_w) { + return; + } + + HRESULT hr = g_WerUnregisterCustomMetadata(key_w); + if (FAILED(hr)) { + SENTRY_WARNF("WerUnregisterCustomMetadata failed: hr=0x%08lx", + (unsigned long)hr); + } + + sentry_free(key_w); +} + +static void +wer_add_attachment(void *data, sentry_attachment_t *attachment) +{ + (void)data; + + if (!attachment) { + return; + } + + if (attachment->path) { + const wchar_t *path_w = attachment->path->path_w; + if (!path_w) { + return; + } + HRESULT hr = WerRegisterFile( + path_w, WerRegFileTypeOther, WER_FILE_ANONYMOUS_DATA); + if (FAILED(hr)) { + SENTRY_WARNF( + "WerRegisterFile failed: hr=0x%08lx", (unsigned long)hr); + } + return; + } + + if (attachment->buf && attachment->buf_len > 0) { + if (attachment->buf_len > MAXDWORD) { + SENTRY_WARNF("WerRegisterMemoryBlock: buffer too large (%zu bytes)", + attachment->buf_len); + return; + } + HRESULT hr = WerRegisterMemoryBlock( + (PVOID)attachment->buf, (DWORD)attachment->buf_len); + if (FAILED(hr)) { + SENTRY_WARNF( + "WerRegisterMemoryBlock failed: hr=0x%08lx", (unsigned long)hr); + } + return; + } +} + +static void +wer_remove_attachment(void *data, sentry_attachment_t *attachment) +{ + (void)data; + + if (!attachment) { + return; + } + + if (attachment->path) { + const wchar_t *path_w = attachment->path->path_w; + if (!path_w) { + return; + } + HRESULT hr = WerUnregisterFile(path_w); + if (FAILED(hr)) { + SENTRY_WARNF( + "WerUnregisterFile failed: hr=0x%08lx", (unsigned long)hr); + } + return; + } + + if (attachment->buf) { + HRESULT hr = WerUnregisterMemoryBlock((PVOID)attachment->buf); + if (FAILED(hr)) { + SENTRY_WARNF("WerUnregisterMemoryBlock failed: hr=0x%08lx", + (unsigned long)hr); + } + } +} + +void +sentry_integration_wer_setup(sentry_scope_t *scope) +{ + wer_init(); + + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + if (!observer) { + return; + } + observer->set_tag = wer_set_tag; + observer->remove_tag = wer_remove_tag; + observer->add_attachment = wer_add_attachment; + observer->remove_attachment = wer_remove_attachment; + + if (sentry__scope_add_observer(scope, observer)) { + for (sentry_attachment_t *attachment = scope->attachments; attachment; + attachment = attachment->next) { + wer_add_attachment(NULL, attachment); + } + } +} diff --git a/src/integrations/sentry_integration_wer.h b/src/integrations/sentry_integration_wer.h new file mode 100644 index 0000000000..ff704b1163 --- /dev/null +++ b/src/integrations/sentry_integration_wer.h @@ -0,0 +1,15 @@ +#ifndef SENTRY_INTEGRATION_WER_H_INCLUDED +#define SENTRY_INTEGRATION_WER_H_INCLUDED + +#include "sentry_scope.h" + +/** + * Sets up the WER integration, creating and registering a scope observer + * that syncs tags and attachments to Windows Error Reporting. + * + * Must be called while holding the scope lock (i.e., from inside + * SENTRY_WITH_SCOPE_MUT). + */ +void sentry_integration_wer_setup(sentry_scope_t *scope); + +#endif diff --git a/src/sentry_attachment.c b/src/sentry_attachment.c index 57d11bb4bc..c337a02f87 100644 --- a/src/sentry_attachment.c +++ b/src/sentry_attachment.c @@ -262,12 +262,12 @@ sentry__attachments_add_path(sentry_attachment_t **attachments_ptr, return sentry__attachments_add(attachments_ptr, attachment); } -void +bool sentry__attachments_remove( sentry_attachment_t **attachments_ptr, sentry_attachment_t *attachment) { if (!attachment) { - return; + return false; } sentry_attachment_t **next_ptr = attachments_ptr; @@ -275,12 +275,14 @@ sentry__attachments_remove( for (sentry_attachment_t *it = *attachments_ptr; it; it = it->next) { if (it == attachment) { *next_ptr = it->next; - sentry__attachment_free(it); - return; + it->next = NULL; + return true; } next_ptr = &it->next; } + + return false; } static sentry_attachment_t * diff --git a/src/sentry_attachment.h b/src/sentry_attachment.h index 0004164e76..0ddf96f35d 100644 --- a/src/sentry_attachment.h +++ b/src/sentry_attachment.h @@ -90,8 +90,9 @@ sentry_attachment_t *sentry__attachments_add_path( /** * Removes an attachment from the attachments list at `attachments_ptr`. + * Returns true if the attachment was found and removed. */ -void sentry__attachments_remove( +bool sentry__attachments_remove( sentry_attachment_t **attachments_ptr, sentry_attachment_t *attachment); /** diff --git a/src/sentry_core.c b/src/sentry_core.c index 2dc25f3e8f..0925ce385d 100644 --- a/src/sentry_core.c +++ b/src/sentry_core.c @@ -38,6 +38,10 @@ # include "integrations/sentry_integration_qt.h" #endif +#ifdef SENTRY_INTEGRATION_WER +# include "integrations/sentry_integration_wer.h" +#endif + static sentry_options_t *g_options = NULL; #ifdef SENTRY__MUTEX_INIT_DYN SENTRY__MUTEX_INIT_DYN(g_options_lock) @@ -228,6 +232,10 @@ sentry_init(sentry_options_t *options) scope->breadcrumbs, options->max_breadcrumbs); sentry__scope_update_dsc(scope, options); + +#ifdef SENTRY_INTEGRATION_WER + sentry_integration_wer_setup(scope); +#endif } if (backend && backend->user_consent_changed_func) { backend->user_consent_changed_func(backend); @@ -900,6 +908,7 @@ sentry_set_release_n(const char *release, size_t release_len) scope->release = sentry__string_clone_n(release, release_len); sentry_value_set_by_key(scope->dynamic_sampling_context, "release", sentry_value_new_string(scope->release)); + SENTRY_SCOPE_NOTIFY(scope, set_release, release, release_len); } } @@ -918,6 +927,8 @@ sentry_set_environment_n(const char *environment, size_t environment_len) = sentry__string_clone_n(environment, environment_len); sentry_value_set_by_key(scope->dynamic_sampling_context, "environment", sentry_value_new_string(scope->environment)); + SENTRY_SCOPE_NOTIFY( + scope, set_environment, environment, environment_len); } } @@ -982,9 +993,7 @@ sentry_set_tag_n( void sentry_remove_tag(const char *key) { - SENTRY_WITH_SCOPE_MUT (scope) { - sentry_value_remove_by_key(scope->tags, key); - } + sentry_remove_tag_n(key, sentry__guarded_strlen(key)); } void @@ -992,6 +1001,7 @@ sentry_remove_tag_n(const char *key, size_t key_len) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key_n(scope->tags, key, key_len); + SENTRY_SCOPE_NOTIFY(scope, remove_tag, key, key_len); } } @@ -1016,6 +1026,8 @@ sentry_remove_extra(const char *key) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key(scope->extra, key); + SENTRY_SCOPE_NOTIFY( + scope, remove_extra, key, sentry__guarded_strlen(key)); } } @@ -1024,6 +1036,7 @@ sentry_remove_extra_n(const char *key, size_t key_len) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key_n(scope->extra, key, key_len); + SENTRY_SCOPE_NOTIFY(scope, remove_extra, key, key_len); } } @@ -1097,6 +1110,8 @@ sentry__set_propagation_context(const char *key, sentry_value_t value) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_set_by_key(scope->propagation_context, key, value); + SENTRY_SCOPE_NOTIFY( + scope, set_context, key, sentry__guarded_strlen(key), value); } } @@ -1131,6 +1146,8 @@ sentry_remove_context(const char *key) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key(scope->contexts, key); + SENTRY_SCOPE_NOTIFY( + scope, remove_context, key, sentry__guarded_strlen(key)); } } @@ -1139,6 +1156,7 @@ sentry_remove_context_n(const char *key, size_t key_len) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key_n(scope->contexts, key, key_len); + SENTRY_SCOPE_NOTIFY(scope, remove_context, key, key_len); } } @@ -1175,6 +1193,7 @@ sentry_remove_fingerprint(void) SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_decref(scope->fingerprint); scope->fingerprint = sentry_value_new_null(); + SENTRY_SCOPE_NOTIFY(scope, set_fingerprint, scope->fingerprint); } } @@ -1229,6 +1248,9 @@ sentry_regenerate_trace(void) generate_propagation_context(scope->propagation_context); scope->trace_managed = false; sentry__scope_update_dsc(scope, options); + SENTRY_SCOPE_NOTIFY(scope, set_context, "trace", + sentry__guarded_strlen("trace"), + sentry_value_get_by_key(scope->propagation_context, "trace")); } } } @@ -1243,6 +1265,8 @@ sentry_set_transaction(const char *transaction) if (scope->transaction_object) { sentry_transaction_set_name(scope->transaction_object, transaction); } + SENTRY_SCOPE_NOTIFY(scope, set_transaction, transaction, + sentry__guarded_strlen(transaction)); } } @@ -1258,6 +1282,8 @@ sentry_set_transaction_n(const char *transaction, size_t transaction_len) sentry_transaction_set_name_n( scope->transaction_object, transaction, transaction_len); } + SENTRY_SCOPE_NOTIFY( + scope, set_transaction, transaction, transaction_len); } } @@ -1330,6 +1356,10 @@ sentry_transaction_start_ts(sentry_transaction_context_t *opaque_tx_ctx, sentry_value_remove_by_key(tx, "parent_span_id"); sentry_value_remove_by_key(tx, "sampled"); sentry__scope_update_dsc(scope, options); + SENTRY_SCOPE_NOTIFY(scope, set_context, "trace", + sentry__guarded_strlen("trace"), + sentry_value_get_by_key( + scope->propagation_context, "trace")); } } } @@ -1904,13 +1934,17 @@ sentry_capture_minidump_n(const char *path, size_t path_len) static sentry_attachment_t * add_attachment(sentry_attachment_t *attachment) { + if (!attachment) { + return NULL; + } + SENTRY_WITH_OPTIONS (options) { if (options->backend && options->backend->add_attachment_func) { options->backend->add_attachment_func(options->backend, attachment); } } SENTRY_WITH_SCOPE_MUT (scope) { - attachment = sentry__attachments_add(&scope->attachments, attachment); + attachment = sentry__scope_add_attachment(scope, attachment); } return attachment; } @@ -1948,15 +1982,17 @@ sentry_clear_attachments(void) { SENTRY_WITH_OPTIONS (options) { SENTRY_WITH_SCOPE_MUT (scope) { - if (options->backend && options->backend->remove_attachment_func) { - for (sentry_attachment_t *it = scope->attachments; it; - it = it->next) { + sentry_attachment_t *attachments = scope->attachments; + scope->attachments = NULL; + for (sentry_attachment_t *it = attachments; it; it = it->next) { + if (options->backend + && options->backend->remove_attachment_func) { options->backend->remove_attachment_func( options->backend, it); } + SENTRY_SCOPE_NOTIFY(scope, remove_attachment, it); } - sentry__attachments_free(scope->attachments); - scope->attachments = NULL; + sentry__attachments_free(attachments); } } } @@ -1964,15 +2000,23 @@ sentry_clear_attachments(void) void sentry_remove_attachment(sentry_attachment_t *attachment) { + if (!attachment) { + return; + } + SENTRY_WITH_OPTIONS (options) { - if (options->backend && options->backend->remove_attachment_func) { - options->backend->remove_attachment_func( - options->backend, attachment); + SENTRY_WITH_SCOPE_MUT (scope) { + if (sentry__attachments_remove(&scope->attachments, attachment)) { + if (options->backend + && options->backend->remove_attachment_func) { + options->backend->remove_attachment_func( + options->backend, attachment); + } + SENTRY_SCOPE_NOTIFY(scope, remove_attachment, attachment); + sentry__attachment_free(attachment); + } } } - SENTRY_WITH_SCOPE_MUT (scope) { - sentry__attachments_remove(&scope->attachments, attachment); - } } #ifdef SENTRY_PLATFORM_WINDOWS diff --git a/src/sentry_scope.c b/src/sentry_scope.c index 1a6f54a55a..8c75ee7962 100644 --- a/src/sentry_scope.c +++ b/src/sentry_scope.c @@ -88,6 +88,9 @@ init_scope(sentry_scope_t *scope) scope->transaction_object = NULL; scope->span = NULL; scope->trace_managed = true; + scope->observers = NULL; + scope->num_observers = 0; + scope->is_notifying = 0; } static sentry_scope_t * @@ -127,6 +130,12 @@ cleanup_scope(sentry_scope_t *scope) sentry__attachments_free(scope->attachments); sentry__transaction_decref(scope->transaction_object); sentry__span_decref(scope->span); + for (size_t i = 0; i < scope->num_observers; i++) { + sentry_free(scope->observers[i]); + } + sentry_free(scope->observers); + scope->observers = NULL; + scope->num_observers = 0; } void @@ -159,7 +168,11 @@ sentry__scope_unlock(void) void sentry__scope_flush_unlock(void) { + bool was_notifying = g_scope.is_notifying > 0; sentry__scope_unlock(); + if (was_notifying) { + return; + } SENTRY_WITH_OPTIONS (options) { // we try to unlock the scope as soon as possible. The // backend will do its own `WITH_SCOPE` internally. @@ -169,6 +182,101 @@ sentry__scope_flush_unlock(void) } } +sentry_scope_observer_t * +sentry__scope_observer_new(void) +{ + return SENTRY_MAKE(sentry_scope_observer_t); +} + +bool +sentry__scope_add_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer) +{ + if (!observer) { + return false; + } + + size_t new_count = scope->num_observers + 1; + sentry_scope_observer_t **new_array + = sentry__calloc(new_count, sizeof(sentry_scope_observer_t *)); + if (!new_array) { + sentry_free(observer); + return false; + } + if (scope->observers) { + memcpy(new_array, scope->observers, + scope->num_observers * sizeof(sentry_scope_observer_t *)); + sentry_free(scope->observers); + } + new_array[scope->num_observers] = observer; + scope->observers = new_array; + scope->num_observers = new_count; + return true; +} + +void +sentry__scope_remove_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer) +{ + if (!observer || !scope->observers) { + return; + } + + for (size_t i = 0; i < scope->num_observers; i++) { + if (scope->observers[i] != observer) { + continue; + } + + sentry_free(observer); + if (scope->is_notifying) { + // avoid shifting the array while SENTRY_SCOPE_NOTIFY is iterating + scope->observers[i] = NULL; + return; + } + for (size_t j = i + 1; j < scope->num_observers; j++) { + scope->observers[j - 1] = scope->observers[j]; + } + scope->num_observers--; + if (scope->num_observers == 0) { + sentry_free(scope->observers); + scope->observers = NULL; + } + return; + } +} + +size_t +sentry__scope_begin_notify(sentry_scope_t *scope) +{ + scope->is_notifying++; + return scope->num_observers; +} + +void +sentry__scope_end_notify(sentry_scope_t *scope) +{ + if (--scope->is_notifying > 0) { + return; + } + if (!scope->observers) { + return; + } + + // removals during notification leave tombstones to avoid shifting the array + // while it is being iterated + size_t j = 0; + for (size_t i = 0; i < scope->num_observers; i++) { + if (scope->observers[i]) { + scope->observers[j++] = scope->observers[i]; + } + } + scope->num_observers = j; + if (scope->num_observers == 0) { + sentry_free(scope->observers); + scope->observers = NULL; + } +} + sentry_scope_t * sentry_local_scope_new(void) { @@ -515,7 +623,9 @@ sentry__scope_apply_to_event(const sentry_scope_t *scope, void sentry_scope_add_breadcrumb(sentry_scope_t *scope, sentry_value_t breadcrumb) { - sentry__ringbuffer_append(scope->breadcrumbs, breadcrumb); + if (sentry__ringbuffer_append(scope->breadcrumbs, breadcrumb) == 0) { + SENTRY_SCOPE_NOTIFY(scope, add_breadcrumb, breadcrumb); + } } void @@ -523,12 +633,14 @@ sentry_scope_set_user(sentry_scope_t *scope, sentry_value_t user) { sentry_value_decref(scope->user); scope->user = user; + SENTRY_SCOPE_NOTIFY(scope, set_user, user); } void sentry_scope_set_tag(sentry_scope_t *scope, const char *key, const char *value) { - sentry_value_set_by_key(scope->tags, key, sentry_value_new_string(value)); + sentry_scope_set_tag_n(scope, key, sentry__guarded_strlen(key), value, + sentry__guarded_strlen(value)); } void @@ -537,13 +649,14 @@ sentry_scope_set_tag_n(sentry_scope_t *scope, const char *key, size_t key_len, { sentry_value_set_by_key_n( scope->tags, key, key_len, sentry_value_new_string_n(value, value_len)); + SENTRY_SCOPE_NOTIFY(scope, set_tag, key, key_len, value, value_len); } void sentry_scope_set_extra( sentry_scope_t *scope, const char *key, sentry_value_t value) { - sentry_value_set_by_key(scope->extra, key, value); + sentry_scope_set_extra_n(scope, key, sentry__guarded_strlen(key), value); } void @@ -551,6 +664,7 @@ sentry_scope_set_extra_n(sentry_scope_t *scope, const char *key, size_t key_len, sentry_value_t value) { sentry_value_set_by_key_n(scope->extra, key, key_len, value); + SENTRY_SCOPE_NOTIFY(scope, set_extra, key, key_len, value); } void @@ -590,6 +704,8 @@ sentry_scope_set_context( sentry_scope_t *scope, const char *key, sentry_value_t value) { sentry_value_set_by_key(scope->contexts, key, value); + SENTRY_SCOPE_NOTIFY( + scope, set_context, key, sentry__guarded_strlen(key), value); } void @@ -597,6 +713,7 @@ sentry_scope_set_context_n(sentry_scope_t *scope, const char *key, size_t key_len, sentry_value_t value) { sentry_value_set_by_key_n(scope->contexts, key, key_len, value); + SENTRY_SCOPE_NOTIFY(scope, set_context, key, key_len, value); } void @@ -619,6 +736,7 @@ sentry_scope_update_context_n(sentry_scope_t *scope, const char *key, sentry__value_merge_objects(value, context); sentry_value_set_by_key_n(scope->contexts, key, key_len, value); } + SENTRY_SCOPE_NOTIFY(scope, set_context, key, key_len, value); } void @@ -633,6 +751,7 @@ sentry__scope_set_fingerprint_va( sentry_value_decref(scope->fingerprint); scope->fingerprint = fingerprint_value; + SENTRY_SCOPE_NOTIFY(scope, set_fingerprint, fingerprint_value); } void @@ -687,12 +806,30 @@ sentry_scope_set_fingerprints( sentry_value_decref(scope->fingerprint); scope->fingerprint = fingerprints; + SENTRY_SCOPE_NOTIFY(scope, set_fingerprint, fingerprints); } void sentry_scope_set_level(sentry_scope_t *scope, sentry_level_t level) { scope->level = level; + SENTRY_SCOPE_NOTIFY(scope, set_level, level); +} + +sentry_attachment_t * +sentry__scope_add_attachment( + sentry_scope_t *scope, sentry_attachment_t *attachment) +{ + if (!attachment) { + return NULL; + } + + sentry_attachment_t *added + = sentry__attachments_add(&scope->attachments, attachment); + if (added == attachment) { + SENTRY_SCOPE_NOTIFY(scope, add_attachment, attachment); + } + return added; } sentry_attachment_t * @@ -706,8 +843,8 @@ sentry_attachment_t * sentry_scope_attach_file_n( sentry_scope_t *scope, const char *path, size_t path_len) { - return sentry__attachments_add_path(&scope->attachments, - sentry__path_from_str_n(path, path_len), NULL, NULL); + return sentry__scope_add_attachment(scope, + sentry__attachment_from_path(sentry__path_from_str_n(path, path_len))); } sentry_attachment_t * @@ -722,7 +859,7 @@ sentry_attachment_t * sentry_scope_attach_bytes_n(sentry_scope_t *scope, const char *buf, size_t buf_len, const char *filename, size_t filename_len) { - return sentry__attachments_add(&scope->attachments, + return sentry__scope_add_attachment(scope, sentry__attachment_from_buffer( buf, buf_len, sentry__path_from_str_n(filename, filename_len))); } @@ -739,8 +876,8 @@ sentry_attachment_t * sentry_scope_attach_filew_n( sentry_scope_t *scope, const wchar_t *path, size_t path_len) { - return sentry__attachments_add_path(&scope->attachments, - sentry__path_from_wstr_n(path, path_len), NULL, NULL); + return sentry__scope_add_attachment(scope, + sentry__attachment_from_path(sentry__path_from_wstr_n(path, path_len))); } sentry_attachment_t * @@ -756,7 +893,7 @@ sentry_attachment_t * sentry_scope_attach_bytesw_n(sentry_scope_t *scope, const char *buf, size_t buf_len, const wchar_t *filename, size_t filename_len) { - return sentry__attachments_add(&scope->attachments, + return sentry__scope_add_attachment(scope, sentry__attachment_from_buffer( buf, buf_len, sentry__path_from_wstr_n(filename, filename_len))); } diff --git a/src/sentry_scope.h b/src/sentry_scope.h index 6047fa8035..389ed75e5e 100644 --- a/src/sentry_scope.h +++ b/src/sentry_scope.h @@ -8,6 +8,45 @@ #include "sentry_session.h" #include "sentry_value.h" +/** + * Scope observer — one callback per scope property. + * + * Implementors set the function pointers they care about. NULL pointers are + * skipped. Callbacks are invoked while the scope lock is held. + * + * Ownership: the scope takes ownership of the observer pointer on + * registration; the caller must not free it after that point. + */ +typedef struct sentry_scope_observer_s { + void *data; + + void (*set_release)(void *data, const char *release, size_t release_len); + void (*set_environment)( + void *data, const char *environment, size_t environment_len); + void (*set_transaction)( + void *data, const char *transaction, size_t transaction_len); + void (*set_fingerprint)(void *data, sentry_value_t fingerprint); + void (*set_level)(void *data, sentry_level_t level); + void (*set_user)(void *data, sentry_value_t user); + + void (*add_breadcrumb)(void *data, sentry_value_t breadcrumb); + + void (*set_tag)(void *data, const char *key, size_t key_len, + const char *value, size_t value_len); + void (*remove_tag)(void *data, const char *key, size_t key_len); + + void (*set_extra)( + void *data, const char *key, size_t key_len, sentry_value_t value); + void (*remove_extra)(void *data, const char *key, size_t key_len); + + void (*set_context)( + void *data, const char *key, size_t key_len, sentry_value_t value); + void (*remove_context)(void *data, const char *key, size_t key_len); + + void (*add_attachment)(void *data, sentry_attachment_t *attachment); + void (*remove_attachment)(void *data, sentry_attachment_t *attachment); +} sentry_scope_observer_t; + /** * This represents the current scope. */ @@ -39,6 +78,10 @@ struct sentry_scope_s { sentry_transaction_t *transaction_object; sentry_span_t *span; bool trace_managed; + + sentry_scope_observer_t **observers; + size_t num_observers; + size_t is_notifying; }; /** @@ -114,6 +157,9 @@ void sentry__scope_remove_attribute(sentry_scope_t *scope, const char *key); void sentry__scope_remove_attribute_n( sentry_scope_t *scope, const char *key, size_t key_len); +sentry_attachment_t *sentry__scope_add_attachment( + sentry_scope_t *scope, sentry_attachment_t *attachment); + /** * These are convenience macros to automatically lock/unlock the global scope * inside a code block. @@ -128,6 +174,50 @@ void sentry__scope_remove_attribute_n( for (sentry_scope_t *Scope = sentry__scope_lock(); Scope; \ sentry__scope_unlock(), Scope = NULL) +/** + * Allocate and zero-initialize a scope observer. + * + * Returns NULL on allocation failure. The caller sets whichever callback + * function pointers and the `data` pointer they need, then registers the + * observer with `sentry__scope_add_observer`, which takes ownership. + */ +sentry_scope_observer_t *sentry__scope_observer_new(void); + +/** + * Register a scope observer. + * + * Takes ownership of `observer`; the caller must not free it after this call. + * Must be called while holding the scope lock. Registration order is respected + * — observers are notified in registration order. + */ +bool sentry__scope_add_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer); + +/** + * Remove a scope observer. + * + * Frees `observer` if it is registered. Must be called while holding the scope + * lock. Does nothing if `observer` is NULL or not registered. + */ +void sentry__scope_remove_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer); + +size_t sentry__scope_begin_notify(sentry_scope_t *scope); +void sentry__scope_end_notify(sentry_scope_t *scope); + +/** Notify observers registered before this notification started. */ +#define SENTRY_SCOPE_NOTIFY(scope, callback, ...) \ + do { \ + size_t _end = sentry__scope_begin_notify(scope); \ + for (size_t _i = 0; _i < _end && _i < (scope)->num_observers; _i++) { \ + sentry_scope_observer_t *_observer = (scope)->observers[_i]; \ + if (_observer && _observer->callback) { \ + _observer->callback(_observer->data, __VA_ARGS__); \ + } \ + } \ + sentry__scope_end_notify(scope); \ + } while (0) + /** * Rebuilds the scope's dynamic sampling context (DSC) from the SDK options * and the current propagation context. The previous DSC is discarded. diff --git a/tests/test_integration_wer.py b/tests/test_integration_wer.py new file mode 100644 index 0000000000..3e5efc802d --- /dev/null +++ b/tests/test_integration_wer.py @@ -0,0 +1,187 @@ +import ctypes +from ctypes import wintypes +import os +import subprocess +import sys + +from pathlib import Path + +import pytest + +from . import run +from .assertions import wait_for +from .conditions import has_breakpad, has_crashpad, has_native, is_qemu + +pytestmark = [ + pytest.mark.skipif( + sys.platform != "win32" or bool(os.environ.get("TEST_MINGW")), + reason="WER integration tests are only available in MSVC Windows builds", + ), + pytest.mark.with_wer, +] + +S_OK = 0 + +E_STORE_USER_ARCHIVE = 0 +E_STORE_USER_QUEUE = 1 +E_STORE_MACHINE_ARCHIVE = 2 +E_STORE_MACHINE_QUEUE = 3 + + +class WerStore: + def __init__(self): + self._wer = ctypes.WinDLL("wer.dll") + self._wer.WerStoreOpen.argtypes = [ + ctypes.c_int, + ctypes.POINTER(wintypes.HANDLE), + ] + self._wer.WerStoreOpen.restype = ctypes.c_long + self._wer.WerStoreClose.argtypes = [wintypes.HANDLE] + self._wer.WerStoreClose.restype = None + self._wer.WerStoreGetFirstReportKey.argtypes = [ + wintypes.HANDLE, + ctypes.POINTER(ctypes.c_wchar_p), + ] + self._wer.WerStoreGetFirstReportKey.restype = ctypes.c_long + self._wer.WerStoreGetNextReportKey.argtypes = [ + wintypes.HANDLE, + ctypes.POINTER(ctypes.c_wchar_p), + ] + self._wer.WerStoreGetNextReportKey.restype = ctypes.c_long + self._wer.WerFreeString.argtypes = [ctypes.c_wchar_p] + self._wer.WerFreeString.restype = None + + def report_dirs(self): + for store_type in ( + E_STORE_USER_ARCHIVE, + E_STORE_USER_QUEUE, + E_STORE_MACHINE_ARCHIVE, + E_STORE_MACHINE_QUEUE, + ): + handle = wintypes.HANDLE() + hr = self._wer.WerStoreOpen(store_type, ctypes.byref(handle)) + if hr != S_OK: + continue + + try: + key = ctypes.c_wchar_p() + hr = self._wer.WerStoreGetFirstReportKey(handle, ctypes.byref(key)) + while hr == S_OK and key.value: + report_dir = key.value + self._wer.WerFreeString(key) + yield Path(report_dir) + + key = ctypes.c_wchar_p() + hr = self._wer.WerStoreGetNextReportKey(handle, ctypes.byref(key)) + finally: + self._wer.WerStoreClose(handle) + + +def wait_for_wer_report(store, test_id): + seen_report_dirs = 0 + readable_reports = 0 + matching_report = None + + def find_report(): + nonlocal matching_report + nonlocal seen_report_dirs + nonlocal readable_reports + + for report_dir in store.report_dirs(): + seen_report_dirs += 1 + report_path = report_dir / "Report.wer" + try: + report = report_path.read_text(encoding="utf-16-le") + except OSError: + continue + + readable_reports += 1 + report_lower = report.lower() + if "test.id" in report_lower and test_id in report: + matching_report = (report_path, report) + return True + + return False + + if wait_for(find_report): + return matching_report + + details = [ + f"searched {seen_report_dirs} WER report directories", + f"read {readable_reports} Report.wer files", + ] + pytest.fail( + f"WER report with test.id={test_id} was not found ({', '.join(details)})" + ) + + +@pytest.mark.parametrize( + "backend", + [ + "none", + "inproc", + pytest.param( + "breakpad", + marks=[ + pytest.mark.skipif( + not has_breakpad or is_qemu, reason="breakpad backend not available" + ), + pytest.mark.xfail( + reason="breakpad swallows the exception", + strict=True, + ), + ], + ), + pytest.param( + "crashpad", + marks=[ + pytest.mark.skipif( + not has_crashpad, reason="crashpad backend not available" + ), + pytest.mark.xfail( + reason="crashpad handler terminates the process", + strict=True, + ), + ], + ), + pytest.param( + "native", + marks=pytest.mark.skipif( + not has_native or is_qemu, reason="native backend not available" + ), + id="native", + ), + ], +) +def test_wer_custom_metadata(cmake, backend): + tmp_path = cmake( + ["sentry_example"], + { + "SENTRY_BACKEND": backend, + "SENTRY_TRANSPORT": "none", + "SENTRY_INTEGRATION_WER": "ON", + }, + ) + + completed = run( + tmp_path, + "sentry_example", + ["e2e-test", "crash"], + expect_failure=True, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + text=True, + ) + + test_id = None + for line in completed.stdout.splitlines(): + if line.startswith("TEST_ID:"): + test_id = line.removeprefix("TEST_ID:") + break + assert test_id, completed.stdout + + report_path, report = wait_for_wer_report(WerStore(), test_id) + assert report_path.name == "Report.wer" + assert "expected-tag" in report + assert "some value" in report + assert "not-expected-tag" not in report diff --git a/tests/unit/test_scope.c b/tests/unit/test_scope.c index 0ac3951ff9..56bfdf7d26 100644 --- a/tests/unit/test_scope.c +++ b/tests/unit/test_scope.c @@ -1132,3 +1132,706 @@ SENTRY_TEST(scope_local_attributes) sentry_close(); } + +typedef struct { + sentry_value_t release; + sentry_value_t environment; + sentry_value_t transaction; + sentry_value_t fingerprint; + sentry_level_t level; + sentry_value_t user; + sentry_value_t breadcrumbs; + sentry_value_t tags; + sentry_value_t extras; + sentry_value_t contexts; + sentry_value_t attachments; + bool was_called; +} test_observer_data_t; + +typedef struct { + test_observer_data_t *self_data; + test_observer_data_t *nested_data; + sentry_scope_observer_t *self; + sentry_scope_observer_t *added; +} reentrant_observer_data_t; + +static void +observe_set_release(void *data, const char *release, size_t release_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->release = sentry_value_new_string_n(release, release_len); + d->was_called = true; +} + +static void +observe_set_environment( + void *data, const char *environment, size_t environment_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->environment = sentry_value_new_string_n(environment, environment_len); + d->was_called = true; +} + +static void +observe_set_transaction( + void *data, const char *transaction, size_t transaction_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->transaction = sentry_value_new_string_n(transaction, transaction_len); + d->was_called = true; +} + +static void +observe_set_fingerprint(void *data, sentry_value_t fingerprint) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (!sentry_value_is_null(d->fingerprint)) { + sentry_value_decref(d->fingerprint); + } + sentry_value_incref(fingerprint); + d->fingerprint = fingerprint; + d->was_called = true; +} + +static void +observe_set_level(void *data, sentry_level_t level) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->level = level; + d->was_called = true; +} + +static void +observe_add_attachment(void *data, sentry_attachment_t *attachment) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->attachments)) { + d->attachments = sentry_value_new_list(); + } + sentry_value_t obj = sentry_value_new_object(); + const char *filename = sentry__attachment_get_filename(attachment); + if (filename) { + sentry_value_set_by_key( + obj, "filename", sentry_value_new_string(filename)); + } + if (attachment->buf) { + sentry_value_set_by_key(obj, "buf", + sentry_value_new_string_n(attachment->buf, attachment->buf_len)); + } + sentry_value_append(d->attachments, obj); + d->was_called = true; +} + +static void +observe_remove_attachment(void *data, sentry_attachment_t *attachment) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->attachments)) { + d->attachments = sentry_value_new_list(); + } + sentry_value_t obj = sentry_value_new_object(); + const char *filename = sentry__attachment_get_filename(attachment); + if (filename) { + sentry_value_set_by_key( + obj, "filename", sentry_value_new_string(filename)); + } + sentry_value_set_by_key(obj, "removed", sentry_value_new_bool(true)); + sentry_value_append(d->attachments, obj); + d->was_called = true; +} + +static void +observe_set_user(void *data, sentry_value_t user) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + d->user = user; + d->was_called = true; +} + +static void +observe_add_breadcrumb(void *data, sentry_value_t breadcrumb) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->breadcrumbs)) { + d->breadcrumbs = sentry_value_new_list(); + } + sentry_value_incref(breadcrumb); + sentry_value_append(d->breadcrumbs, breadcrumb); + d->was_called = true; +} + +static void +observe_set_tag(void *data, const char *key, size_t key_len, const char *value, + size_t value_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->tags)) { + d->tags = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->tags, key, key_len, sentry_value_new_string_n(value, value_len)); + d->was_called = true; +} + +static void +observe_set_tag_remove_self_and_add(void *data, const char *key, size_t key_len, + const char *value, size_t value_len) +{ + reentrant_observer_data_t *d = (reentrant_observer_data_t *)data; + observe_set_tag(d->self_data, key, key_len, value, value_len); + SENTRY_WITH_SCOPE_MUT_NO_FLUSH (scope) { + sentry__scope_remove_observer(scope, d->self); + TEST_CHECK(sentry__scope_add_observer(scope, d->added)); + } + d->nested_data->was_called = false; + sentry_set_extra("nested", sentry_value_new_string("notify")); + TEST_CHECK(d->nested_data->was_called); +} + +static void +observe_remove_tag(void *data, const char *key, size_t key_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->tags)) { + d->tags = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->tags, key, key_len, sentry_value_new_string("(removed)")); + d->was_called = true; +} + +static void +observe_set_extra( + void *data, const char *key, size_t key_len, sentry_value_t value) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->extras)) { + d->extras = sentry_value_new_object(); + } + sentry_value_incref(value); + sentry_value_set_by_key_n(d->extras, key, key_len, value); + d->was_called = true; +} + +static void +observe_remove_extra(void *data, const char *key, size_t key_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->extras)) { + d->extras = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->extras, key, key_len, sentry_value_new_string("(removed)")); + d->was_called = true; +} + +static void +observe_set_context( + void *data, const char *key, size_t key_len, sentry_value_t value) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->contexts)) { + d->contexts = sentry_value_new_object(); + } + sentry_value_incref(value); + sentry_value_set_by_key_n(d->contexts, key, key_len, value); + d->was_called = true; +} + +static void +observe_remove_context(void *data, const char *key, size_t key_len) +{ + test_observer_data_t *d = (test_observer_data_t *)data; + if (sentry_value_is_null(d->contexts)) { + d->contexts = sentry_value_new_object(); + } + sentry_value_set_by_key_n( + d->contexts, key, key_len, sentry_value_new_string("(removed)")); + d->was_called = true; +} + +SENTRY_TEST(scope_observer_null) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .tags = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_tag = observe_set_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_tag("my-tag", "my-value"); + TEST_CHECK(d.was_called); + + d.was_called = false; + sentry_remove_tag("my-tag"); + TEST_CHECK(!d.was_called); + + sentry_value_decref(d.tags); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_multiple) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d1 = { .tags = sentry_value_new_null() }; + test_observer_data_t d2 = { .tags = sentry_value_new_null() }; + sentry_scope_observer_t *observer1 = sentry__scope_observer_new(); + observer1->data = &d1; + observer1->set_tag = observe_set_tag; + + sentry_scope_observer_t *observer2 = sentry__scope_observer_new(); + observer2->data = &d2; + observer2->set_tag = observe_set_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + TEST_CHECK(sentry__scope_add_observer(scope, observer1)); + TEST_CHECK(sentry__scope_add_observer(scope, observer2)); + } + + sentry_set_tag("multi", "test"); + TEST_CHECK(d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d1.tags, "multi")), + "test"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d2.tags, "multi")), + "test"); + + d1.was_called = false; + d2.was_called = false; + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_remove_observer(scope, observer1); + } + + sentry_set_tag("multi", "again"); + TEST_CHECK(!d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d2.tags, "multi")), + "again"); + + sentry_value_decref(d1.tags); + sentry_value_decref(d2.tags); + sentry_close(); +} + +SENTRY_TEST(scope_observer_mutate) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d1 = { .tags = sentry_value_new_null() }; + test_observer_data_t d2 = { .tags = sentry_value_new_null() }; + test_observer_data_t d3 = { .tags = sentry_value_new_null() }; + reentrant_observer_data_t reentrant + = { .self_data = &d1, .nested_data = &d2 }; + + sentry_scope_observer_t *observer1 = sentry__scope_observer_new(); + reentrant.self = observer1; + observer1->data = &reentrant; + observer1->set_tag = observe_set_tag_remove_self_and_add; + + sentry_scope_observer_t *observer2 = sentry__scope_observer_new(); + observer2->data = &d2; + observer2->set_tag = observe_set_tag; + observer2->set_extra = observe_set_extra; + + sentry_scope_observer_t *observer3 = sentry__scope_observer_new(); + reentrant.added = observer3; + observer3->data = &d3; + observer3->set_tag = observe_set_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + TEST_CHECK(sentry__scope_add_observer(scope, observer1)); + TEST_CHECK(sentry__scope_add_observer(scope, observer2)); + } + + sentry_set_tag("reentrant", "first"); + TEST_CHECK(d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK(!d3.was_called); + + d1.was_called = false; + d2.was_called = false; + sentry_set_tag("reentrant", "second"); + TEST_CHECK(!d1.was_called); + TEST_CHECK(d2.was_called); + TEST_CHECK(d3.was_called); + + sentry_value_decref(d1.tags); + sentry_value_decref(d2.tags); + sentry_value_decref(d3.tags); + sentry_close(); +} + +SENTRY_TEST(scope_observer_release) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_release = observe_set_release; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_release("my-release"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL(sentry_value_as_string(d.release), "my-release"); + + sentry_value_decref(d.release); + sentry_close(); +} + +SENTRY_TEST(scope_observer_environment) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_environment = observe_set_environment; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_environment("my-env"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL(sentry_value_as_string(d.environment), "my-env"); + + sentry_value_decref(d.environment); + sentry_close(); +} + +SENTRY_TEST(scope_observer_transaction) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_transaction = observe_set_transaction; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_transaction("my-transaction"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(d.transaction), "my-transaction"); + + sentry_value_decref(d.transaction); + sentry_close(); +} + +SENTRY_TEST(scope_observer_fingerprint) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_fingerprint = observe_set_fingerprint; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_fingerprint("my-fingerprint", NULL); + TEST_CHECK(d.was_called); + TEST_CHECK(!sentry_value_is_null(d.fingerprint)); + TEST_CHECK_JSON_VALUE(d.fingerprint, "[\"my-fingerprint\"]"); + + d.was_called = false; + sentry_remove_fingerprint(); + TEST_CHECK(d.was_called); + TEST_CHECK(sentry_value_is_null(d.fingerprint)); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_level) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_level = observe_set_level; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_level(SENTRY_LEVEL_WARNING); + TEST_CHECK(d.was_called); + TEST_CHECK(d.level == SENTRY_LEVEL_WARNING); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_user) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { 0 }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_user = observe_set_user; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_value_t user = sentry_value_new_object(); + sentry_value_set_by_key(user, "id", sentry_value_new_string("user123")); + sentry_set_user(user); + TEST_CHECK(d.was_called); + TEST_CHECK(!sentry_value_is_null(d.user)); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.user, "id")), + "user123"); + + d.was_called = false; + sentry_remove_user(); + TEST_CHECK(d.was_called); + TEST_CHECK(sentry_value_is_null(d.user)); + + sentry_close(); +} + +SENTRY_TEST(scope_observer_breadcrumbs) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .breadcrumbs = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->add_breadcrumb = observe_add_breadcrumb; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_add_breadcrumb( + sentry_value_new_breadcrumb(NULL, "first breadcrumb")); + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.breadcrumbs), 1); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(d.breadcrumbs, 0), "message")), + "first breadcrumb"); + + sentry_add_breadcrumb( + sentry_value_new_breadcrumb("warning", "second breadcrumb")); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.breadcrumbs), 2); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(d.breadcrumbs, 1), "message")), + "second breadcrumb"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key( + sentry_value_get_by_index(d.breadcrumbs, 1), "type")), + "warning"); + + sentry_value_decref(d.breadcrumbs); + sentry_close(); +} + +SENTRY_TEST(scope_observer_tags) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .tags = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_tag = observe_set_tag; + observer->remove_tag = observe_remove_tag; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_set_tag("my-tag", "my-value"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.tags, "my-tag")), + "my-value"); + TEST_CHECK_INT_EQUAL( + sentry_value_get_length(sentry_value_get_by_key(d.tags, "my-tag")), 8); + + d.was_called = false; + sentry_remove_tag("my-tag"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.tags, "my-tag")), + "(removed)"); + + sentry_value_decref(d.tags); + sentry_close(); +} + +SENTRY_TEST(scope_observer_extras) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .extras = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_extra = observe_set_extra; + observer->remove_extra = observe_remove_extra; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_value_t val = sentry_value_new_string("extra-value"); + sentry_set_extra("my-extra", val); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.extras, "my-extra")), + "extra-value"); + + d.was_called = false; + sentry_remove_extra("my-extra"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(d.extras, "my-extra")), + "(removed)"); + + sentry_value_decref(d.extras); + sentry_close(); +} + +SENTRY_TEST(scope_observer_contexts) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .contexts = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->set_context = observe_set_context; + observer->remove_context = observe_remove_context; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_value_t ctx = sentry_value_new_object(); + sentry_value_set_by_key(ctx, "type", sentry_value_new_string("device")); + sentry_set_context("my-context", ctx); + TEST_CHECK(d.was_called); + sentry_value_t received = sentry_value_get_by_key(d.contexts, "my-context"); + TEST_CHECK(!sentry_value_is_null(received)); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(received, "type")), + "device"); + + sentry_value_t update = sentry_value_new_object(); + sentry_value_set_by_key(update, "version", sentry_value_new_string("1.0")); + d.was_called = false; + sentry_update_context("my-context", update); + TEST_CHECK(d.was_called); + received = sentry_value_get_by_key(d.contexts, "my-context"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(received, "type")), + "device"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(received, "version")), + "1.0"); + + d.was_called = false; + sentry_remove_context("my-context"); + TEST_CHECK(d.was_called); + TEST_CHECK_STRING_EQUAL(sentry_value_as_string(sentry_value_get_by_key( + d.contexts, "my-context")), + "(removed)"); + + sentry_value_decref(d.contexts); + sentry_close(); +} + +SENTRY_TEST(scope_observer_attachments) +{ + SENTRY_TEST_OPTIONS_NEW(options); + sentry_init(options); + + test_observer_data_t d = { .attachments = sentry_value_new_null() }; + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + observer->data = &d; + observer->add_attachment = observe_add_attachment; + observer->remove_attachment = observe_remove_attachment; + + SENTRY_WITH_SCOPE_MUT (scope) { + sentry__scope_add_observer(scope, observer); + } + + sentry_attachment_t *attachment = sentry_attach_bytes("buf", 3, "test.txt"); + TEST_CHECK(d.was_called); + TEST_CHECK(attachment != NULL); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 1); + sentry_value_t added = sentry_value_get_by_index(d.attachments, 0); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(added, "buf")), "buf"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(added, "filename")), + "test.txt"); + + d.was_called = false; + sentry_remove_attachment(attachment); + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 2); + sentry_value_t removed = sentry_value_get_by_index(d.attachments, 1); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(removed, "filename")), + "test.txt"); + TEST_CHECK( + sentry_value_is_true(sentry_value_get_by_key(removed, "removed"))); + + attachment = sentry_attach_file("test.txt"); + TEST_CHECK(d.was_called); + TEST_CHECK(attachment != NULL); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 3); + + d.was_called = false; + sentry_attachment_t *duplicate = sentry_attach_file("test.txt"); + TEST_CHECK(duplicate == attachment); + TEST_CHECK(!d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 3); + + d.was_called = false; + sentry_remove_attachment(attachment); + TEST_CHECK(d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 4); + + d.was_called = false; + sentry_remove_attachment(duplicate); + TEST_CHECK(!d.was_called); + TEST_CHECK_INT_EQUAL(sentry_value_get_length(d.attachments), 4); + + sentry_value_decref(d.attachments); + sentry_close(); +} diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 86addc6da0..965bda59ac 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -285,14 +285,28 @@ XX(sampling_decision) XX(sampling_transaction) XX(scope_breadcrumbs) XX(scope_contexts) -XX(scope_update_context) XX(scope_extra) XX(scope_fingerprint) XX(scope_fingerprint_n) XX(scope_global_attributes) XX(scope_level) XX(scope_local_attributes) +XX(scope_observer_attachments) +XX(scope_observer_breadcrumbs) +XX(scope_observer_contexts) +XX(scope_observer_environment) +XX(scope_observer_extras) +XX(scope_observer_fingerprint) +XX(scope_observer_level) +XX(scope_observer_multiple) +XX(scope_observer_mutate) +XX(scope_observer_null) +XX(scope_observer_release) +XX(scope_observer_tags) +XX(scope_observer_transaction) +XX(scope_observer_user) XX(scope_tags) +XX(scope_update_context) XX(scope_user) XX(scope_user_id) XX(scoped_txn)