From 3ece229929263057faca0f6374a1580ff6bb7f8a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 1 Jul 2026 19:39:23 +0200 Subject: [PATCH 01/13] WIP: feat: scope observer --- src/sentry_core.c | 31 ++- src/sentry_scope.c | 55 +++- src/sentry_scope.h | 78 ++++++ tests/unit/test_scope.c | 585 ++++++++++++++++++++++++++++++++++++++++ tests/unit/tests.inc | 13 + 5 files changed, 754 insertions(+), 8 deletions(-) diff --git a/src/sentry_core.c b/src/sentry_core.c index e169f7308f..ebc7507a40 100644 --- a/src/sentry_core.c +++ b/src/sentry_core.c @@ -899,6 +899,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_NOTIFY_OBSERVERS(scope, set_release, release, release_len); } } @@ -917,6 +918,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_NOTIFY_OBSERVERS( + scope, set_environment, environment, environment_len); } } @@ -981,9 +984,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 @@ -991,6 +992,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_NOTIFY_OBSERVERS(scope, remove_tag, key, key_len); } } @@ -1015,6 +1017,8 @@ sentry_remove_extra(const char *key) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key(scope->extra, key); + SENTRY_NOTIFY_OBSERVERS( + scope, remove_extra, key, sentry__guarded_strlen(key)); } } @@ -1023,6 +1027,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_NOTIFY_OBSERVERS(scope, remove_extra, key, key_len); } } @@ -1130,6 +1135,8 @@ sentry_remove_context(const char *key) { SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_remove_by_key(scope->contexts, key); + SENTRY_NOTIFY_OBSERVERS( + scope, remove_context, key, sentry__guarded_strlen(key)); } } @@ -1138,6 +1145,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_NOTIFY_OBSERVERS(scope, remove_context, key, key_len); } } @@ -1174,6 +1182,7 @@ sentry_remove_fingerprint(void) SENTRY_WITH_SCOPE_MUT (scope) { sentry_value_decref(scope->fingerprint); scope->fingerprint = sentry_value_new_null(); + SENTRY_NOTIFY_OBSERVERS(scope, set_fingerprint, scope->fingerprint); } } @@ -1242,6 +1251,8 @@ sentry_set_transaction(const char *transaction) if (scope->transaction_object) { sentry_transaction_set_name(scope->transaction_object, transaction); } + SENTRY_NOTIFY_OBSERVERS(scope, set_transaction, transaction, + sentry__guarded_strlen(transaction)); } } @@ -1257,6 +1268,8 @@ sentry_set_transaction_n(const char *transaction, size_t transaction_len) sentry_transaction_set_name_n( scope->transaction_object, transaction, transaction_len); } + SENTRY_NOTIFY_OBSERVERS( + scope, set_transaction, transaction, transaction_len); } } @@ -1910,6 +1923,9 @@ add_attachment(sentry_attachment_t *attachment) } SENTRY_WITH_SCOPE_MUT (scope) { attachment = sentry__attachments_add(&scope->attachments, attachment); + if (attachment) { + SENTRY_NOTIFY_OBSERVERS(scope, add_attachment, attachment); + } } return attachment; } @@ -1947,12 +1963,14 @@ 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) { + for (sentry_attachment_t *it = scope->attachments; it; + it = it->next) { + if (options->backend + && options->backend->remove_attachment_func) { options->backend->remove_attachment_func( options->backend, it); } + SENTRY_NOTIFY_OBSERVERS(scope, remove_attachment, it); } sentry__attachments_free(scope->attachments); scope->attachments = NULL; @@ -1970,6 +1988,7 @@ sentry_remove_attachment(sentry_attachment_t *attachment) } } SENTRY_WITH_SCOPE_MUT (scope) { + SENTRY_NOTIFY_OBSERVERS(scope, remove_attachment, attachment); sentry__attachments_remove(&scope->attachments, attachment); } } diff --git a/src/sentry_scope.c b/src/sentry_scope.c index 1a6f54a55a..58654361bf 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_observing = false; } 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 @@ -169,6 +178,37 @@ sentry__scope_flush_unlock(void) } } +sentry_scope_observer_t * +sentry__scope_observer_new(void) +{ + return SENTRY_MAKE(sentry_scope_observer_t); +} + +void +sentry__scope_add_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer) +{ + if (!observer) { + return; + } + + 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; + } + 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; +} + sentry_scope_t * sentry_local_scope_new(void) { @@ -516,6 +556,7 @@ void sentry_scope_add_breadcrumb(sentry_scope_t *scope, sentry_value_t breadcrumb) { sentry__ringbuffer_append(scope->breadcrumbs, breadcrumb); + SENTRY_NOTIFY_OBSERVERS(scope, add_breadcrumb, breadcrumb); } void @@ -523,12 +564,14 @@ sentry_scope_set_user(sentry_scope_t *scope, sentry_value_t user) { sentry_value_decref(scope->user); scope->user = user; + SENTRY_NOTIFY_OBSERVERS(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 +580,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_NOTIFY_OBSERVERS(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 +595,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_NOTIFY_OBSERVERS(scope, set_extra, key, key_len, value); } void @@ -590,6 +635,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_NOTIFY_OBSERVERS( + scope, set_context, key, sentry__guarded_strlen(key), value); } void @@ -597,6 +644,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_NOTIFY_OBSERVERS(scope, set_context, key, key_len, value); } void @@ -633,6 +681,7 @@ sentry__scope_set_fingerprint_va( sentry_value_decref(scope->fingerprint); scope->fingerprint = fingerprint_value; + SENTRY_NOTIFY_OBSERVERS(scope, set_fingerprint, fingerprint_value); } void @@ -687,12 +736,14 @@ sentry_scope_set_fingerprints( sentry_value_decref(scope->fingerprint); scope->fingerprint = fingerprints; + SENTRY_NOTIFY_OBSERVERS(scope, set_fingerprint, fingerprints); } void sentry_scope_set_level(sentry_scope_t *scope, sentry_level_t level) { scope->level = level; + SENTRY_NOTIFY_OBSERVERS(scope, set_level, level); } sentry_attachment_t * diff --git a/src/sentry_scope.h b/src/sentry_scope.h index 6047fa8035..dd817ca1e1 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; + bool is_observing; }; /** @@ -128,6 +171,41 @@ 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 use or free it after + * this call. Must be called while holding the scope lock (i.e., inside + * SENTRY_WITH_SCOPE_MUT). Registration order is respected — observers are + * notified in registration order. + */ +void sentry__scope_add_observer( + sentry_scope_t *scope, sentry_scope_observer_t *observer); + +/** Re-entrancy guard: set while notifying observers. */ +#define SENTRY_NOTIFY_OBSERVERS(scope, callback, ...) \ + do { \ + if ((scope)->is_observing) \ + break; \ + (scope)->is_observing = true; \ + for (size_t _i = 0; _i < (scope)->num_observers; _i++) { \ + sentry_scope_observer_t *_observer = (scope)->observers[_i]; \ + if (_observer->callback) { \ + _observer->callback(_observer->data, ##__VA_ARGS__); \ + } \ + } \ + (scope)->is_observing = false; \ + } 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/unit/test_scope.c b/tests/unit/test_scope.c index 0ac3951ff9..1f11da71e1 100644 --- a/tests/unit/test_scope.c +++ b/tests/unit/test_scope.c @@ -1132,3 +1132,588 @@ 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; + +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_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) { + sentry__scope_add_observer(scope, observer1); + 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"); + + sentry_value_decref(d1.tags); + sentry_value_decref(d2.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"); + + 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"))); + + sentry_value_decref(d.attachments); + sentry_close(); +} diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 86addc6da0..31105bdce9 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -292,6 +292,19 @@ 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_null) +XX(scope_observer_release) +XX(scope_observer_tags) +XX(scope_observer_transaction) +XX(scope_observer_user) XX(scope_tags) XX(scope_user) XX(scope_user_id) From 5850b8731b70a2adfce33757c695719408c138f6 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 14:13:54 +0200 Subject: [PATCH 02/13] feat: add sentry_options_add_integration API Adds a sentry_integration_t struct with a register_func callback that is called from sentry_init() while the scope lock is held. Integrations can use this to create and register scope observers. sentry_options_add_integration() stores integrations in options, which takes ownership. An optional free_func is called before the integration struct is freed. --- include/sentry.h | 21 +++++++++++++++++++++ src/sentry_core.c | 8 ++++++++ src/sentry_integration.h | 23 +++++++++++++++++++++++ src/sentry_options.c | 34 ++++++++++++++++++++++++++++++++++ src/sentry_options.h | 3 +++ 5 files changed, 89 insertions(+) create mode 100644 src/sentry_integration.h diff --git a/include/sentry.h b/include/sentry.h index 2c41e9dfd2..416d41ac64 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -860,6 +860,15 @@ SENTRY_API void sentry_capture_envelope(sentry_envelope_t *envelope); struct sentry_options_s; typedef struct sentry_options_s sentry_options_t; +/** + * This represents an integration that can be configured via options and is + * automatically wired up during `sentry_init`. + * + * See `sentry_options_add_integration`. + */ +struct sentry_integration_s; +typedef struct sentry_integration_s sentry_integration_t; + /** * This represents an interface for user-defined transports. * @@ -1993,6 +2002,18 @@ SENTRY_API uint64_t sentry_options_get_transfer_timeout(sentry_options_t *opts); SENTRY_API void sentry_options_set_backend( sentry_options_t *opts, sentry_backend_t *backend); +/** + * Adds an integration to the options. + * + * The integration's `register_func` callback will be invoked during + * `sentry_init` (while the scope lock is held), allowing the integration to + * set up scope observers and other SDK internals. + * + * Takes ownership of `integration`. + */ +SENTRY_EXPERIMENTAL_API void sentry_options_add_integration( + sentry_options_t *opts, sentry_integration_t *integration); + /* -- Global/Scope APIs -- */ /** diff --git a/src/sentry_core.c b/src/sentry_core.c index ebc7507a40..ac1390e4e8 100644 --- a/src/sentry_core.c +++ b/src/sentry_core.c @@ -227,6 +227,14 @@ sentry_init(sentry_options_t *options) scope->breadcrumbs, options->max_breadcrumbs); sentry__scope_update_dsc(scope, options); + + for (size_t i = 0; i < options->num_integrations; i++) { + sentry_integration_t *integration = options->integrations[i]; + if (integration->register_func) { + integration->register_func( + integration->data, scope, options); + } + } } if (backend && backend->user_consent_changed_func) { backend->user_consent_changed_func(backend); diff --git a/src/sentry_integration.h b/src/sentry_integration.h new file mode 100644 index 0000000000..972989e6fa --- /dev/null +++ b/src/sentry_integration.h @@ -0,0 +1,23 @@ +#ifndef SENTRY_INTEGRATION_H_INCLUDED +#define SENTRY_INTEGRATION_H_INCLUDED + +#include "sentry_boot.h" + +/** + * Integration callback that is invoked during `sentry_init` while the scope + * lock is held. Implementors use this to, e.g., create and register a scope + * observer. + */ +typedef struct sentry_integration_s { + void *data; + + void (*register_func)( + void *data, sentry_scope_t *scope, const sentry_options_t *options); + + /** + * Optional cleanup callback, invoked before the integration struct is freed. + */ + void (*free_func)(void *data); +} sentry_integration_t; + +#endif diff --git a/src/sentry_options.c b/src/sentry_options.c index 01d1ab1f7b..3bfb79ec7a 100644 --- a/src/sentry_options.c +++ b/src/sentry_options.c @@ -156,6 +156,15 @@ sentry_options_free(sentry_options_t *opts) sentry__attachments_free(opts->attachments); sentry__run_free(opts->run); + for (size_t i = 0; i < opts->num_integrations; i++) { + sentry_integration_t *integration = opts->integrations[i]; + if (integration->free_func) { + integration->free_func(integration->data); + } + sentry_free(integration); + } + sentry_free(opts->integrations); + sentry_free(opts); } @@ -930,6 +939,31 @@ sentry_options_set_backend(sentry_options_t *opts, sentry_backend_t *backend) opts->backend = backend; } +void +sentry_options_add_integration( + sentry_options_t *opts, sentry_integration_t *integration) +{ + if (!integration) { + return; + } + + size_t new_count = opts->num_integrations + 1; + sentry_integration_t **new_array + = sentry__calloc(new_count, sizeof(sentry_integration_t *)); + if (!new_array) { + sentry_free(integration); + return; + } + if (opts->integrations) { + memcpy(new_array, opts->integrations, + opts->num_integrations * sizeof(sentry_integration_t *)); + sentry_free(opts->integrations); + } + new_array[opts->num_integrations] = integration; + opts->integrations = new_array; + opts->num_integrations = new_count; +} + void sentry_options_set_enable_logs(sentry_options_t *opts, int enable_logs) { diff --git a/src/sentry_options.h b/src/sentry_options.h index 0947ef452a..e9f848964c 100644 --- a/src/sentry_options.h +++ b/src/sentry_options.h @@ -5,6 +5,7 @@ #include "sentry_attachment.h" #include "sentry_database.h" +#include "sentry_integration.h" #include "sentry_logger.h" #include "sentry_session.h" #include "sentry_utils.h" @@ -95,6 +96,8 @@ struct sentry_options_s { not exposed through the options API */ struct sentry_backend_s *backend; sentry_session_t *session; + sentry_integration_t **integrations; + size_t num_integrations; long refcount; uint64_t shutdown_timeout; From 50ca97220cf5218e3e64e1869eff568855bcc8bf Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 14:17:19 +0200 Subject: [PATCH 03/13] feat: add WER integration via scope observer Adds sentry_integration_wer_new() which creates an integration that uses a scope observer to sync sentry scope state to Windows Error Reporting: - Tags are synced via WerRegisterCustomMetadata (loaded dynamically for compatibility with pre-19H1 Windows 10 builds). - File attachments are registered with WerRegisterFile. - Buffer attachments are registered with WerRegisterMemoryBlock. When tags or attachments are removed via the sentry API, the corresponding WER registrations are cleaned up. --- include/sentry.h | 9 + src/CMakeLists.txt | 21 +- src/integrations/sentry_integration_wer.c | 228 ++++++++++++++++++++++ 3 files changed, 248 insertions(+), 10 deletions(-) create mode 100644 src/integrations/sentry_integration_wer.c diff --git a/include/sentry.h b/include/sentry.h index 416d41ac64..c65aa3f62d 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -2014,6 +2014,15 @@ SENTRY_API void sentry_options_set_backend( SENTRY_EXPERIMENTAL_API void sentry_options_add_integration( sentry_options_t *opts, sentry_integration_t *integration); +/** + * Creates a WER (Windows Error Reporting) integration that syncs scope tags + * and attachments to WER via `WerRegisterCustomMetadata`, `WerRegisterFile`, + * and `WerRegisterMemoryBlock`. + * + * Use with `sentry_options_add_integration`. + */ +SENTRY_EXPERIMENTAL_API sentry_integration_t *sentry_integration_wer_new(void); + /* -- Global/Scope APIs -- */ /** diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 20a8036243..2a7066a4e5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -83,16 +83,17 @@ sentry_target_sources_cwd(sentry ) # generic platform / path / symbolizer -if(WIN32) - sentry_target_sources_cwd(sentry - sentry_random.c - sentry_random.h - sentry_windows_dbghelp.c - sentry_windows_dbghelp.h - path/sentry_path_windows.c - process/sentry_process_windows.c - symbolizer/sentry_symbolizer_windows.c - ) + if(WIN32) + sentry_target_sources_cwd(sentry + sentry_random.c + sentry_random.h + sentry_windows_dbghelp.c + sentry_windows_dbghelp.h + integrations/sentry_integration_wer.c + path/sentry_path_windows.c + process/sentry_process_windows.c + symbolizer/sentry_symbolizer_windows.c + ) elseif(NX OR PROSPERO) sentry_target_sources_cwd(sentry sentry_unix_spinlock.h diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c new file mode 100644 index 0000000000..5273d78f33 --- /dev/null +++ b/src/integrations/sentry_integration_wer.c @@ -0,0 +1,228 @@ +#include "sentry_boot.h" + +#ifdef SENTRY_PLATFORM_WINDOWS + +# include "sentry_alloc.h" +# include "sentry_attachment.h" +# include "sentry_integration.h" +# include "sentry_logger.h" +# include "sentry_scope.h" +# include "sentry_string.h" + +# include + +# ifndef WER_FILE_ANONYMOUS_DATA +# define WER_FILE_ANONYMOUS_DATA 0x2 +# endif + +typedef struct { + HMODULE wer_dll; + HRESULT(WINAPI *WerRegisterCustomMetadata_fn)(PCWSTR key, PCWSTR value); + HRESULT(WINAPI *WerUnregisterCustomMetadata_fn)(PCWSTR key); +} wer_state_t; + +static HRESULT +wer_set_tag(void *data, const char *key, size_t key_len, const char *value, + size_t value_len) +{ + wer_state_t *state = (wer_state_t *)data; + if (!state->WerRegisterCustomMetadata_fn) { + return S_OK; + } + if (!key || !value) { + return S_OK; + } + + 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 E_OUTOFMEMORY; + } + + HRESULT hr = state->WerRegisterCustomMetadata_fn(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); + return hr; +} + +static HRESULT +wer_remove_tag(void *data, const char *key, size_t key_len) +{ + wer_state_t *state = (wer_state_t *)data; + if (!state->WerUnregisterCustomMetadata_fn) { + return S_OK; + } + if (!key) { + return S_OK; + } + + 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 E_OUTOFMEMORY; + } + + HRESULT hr = state->WerUnregisterCustomMetadata_fn(key_w); + if (FAILED(hr)) { + SENTRY_WARNF("WerUnregisterCustomMetadata failed: hr=0x%08lx", + (unsigned long)hr); + } + + sentry_free(key_w); + return hr; +} + +static HRESULT +wer_add_attachment(void *data, sentry_attachment_t *attachment) +{ + (void)data; + + if (!attachment) { + return S_OK; + } + + if (attachment->path) { + const wchar_t *path_w = attachment->path->path_w; + if (!path_w) { + return S_OK; + } + HRESULT hr + = WerRegisterFile(path_w, WerRegFileTypeOther, WER_FILE_ANONYMOUS_DATA); + if (FAILED(hr)) { + SENTRY_WARNF("WerRegisterFile failed: hr=0x%08lx", + (unsigned long)hr); + } + return hr; + } + + if (attachment->buf && attachment->buf_len > 0) { + if (attachment->buf_len > MAXDWORD) { + SENTRY_WARNF("WerRegisterMemoryBlock: buffer too large (%zu bytes)", + attachment->buf_len); + return E_INVALIDARG; + } + HRESULT hr = WerRegisterMemoryBlock( + (PVOID)attachment->buf, (DWORD)attachment->buf_len); + if (FAILED(hr)) { + SENTRY_WARNF("WerRegisterMemoryBlock failed: hr=0x%08lx", + (unsigned long)hr); + } + return hr; + } + + return S_OK; +} + +static HRESULT +wer_remove_attachment(void *data, sentry_attachment_t *attachment) +{ + (void)data; + + if (!attachment) { + return S_OK; + } + + if (attachment->path) { + const wchar_t *path_w = attachment->path->path_w; + if (!path_w) { + return S_OK; + } + HRESULT hr = WerUnregisterFile(path_w); + if (FAILED(hr)) { + SENTRY_WARNF("WerUnregisterFile failed: hr=0x%08lx", + (unsigned long)hr); + } + return hr; + } + + if (attachment->buf) { + HRESULT hr = WerUnregisterMemoryBlock((PVOID)attachment->buf); + if (FAILED(hr)) { + SENTRY_WARNF("WerUnregisterMemoryBlock failed: hr=0x%08lx", + (unsigned long)hr); + } + return hr; + } + + return S_OK; +} + +static void +wer_free(void *data) +{ + wer_state_t *state = (wer_state_t *)data; + if (state->wer_dll) { + FreeLibrary(state->wer_dll); + } + sentry_free(state); +} + +static void +wer_register(void *data, sentry_scope_t *scope, const sentry_options_t *options) +{ + (void)options; + + wer_state_t *state = (wer_state_t *)data; + state->wer_dll = LoadLibraryW(L"wer.dll"); + if (state->wer_dll) { + state->WerRegisterCustomMetadata_fn = (HRESULT(WINAPI *)(PCWSTR, PCWSTR)) + GetProcAddress(state->wer_dll, "WerRegisterCustomMetadata"); + state->WerUnregisterCustomMetadata_fn + = (HRESULT(WINAPI *)(PCWSTR)) + GetProcAddress(state->wer_dll, "WerUnregisterCustomMetadata"); + } + + if (!state->WerRegisterCustomMetadata_fn) { + SENTRY_DEBUG("WerRegisterCustomMetadata not available; " + "tag sync to WER will be skipped"); + } + + sentry_scope_observer_t *observer = sentry__scope_observer_new(); + if (!observer) { + return; + } + observer->data = state; + observer->set_tag = wer_set_tag; + observer->remove_tag = wer_remove_tag; + observer->add_attachment = wer_add_attachment; + observer->remove_attachment = wer_remove_attachment; + + sentry__scope_add_observer(scope, observer); +} + +sentry_integration_t * +sentry_integration_wer_new(void) +{ + wer_state_t *state = SENTRY_MAKE(wer_state_t); + if (!state) { + return NULL; + } + + sentry_integration_t *integration = SENTRY_MAKE(sentry_integration_t); + if (!integration) { + sentry_free(state); + return NULL; + } + + integration->data = state; + integration->register_func = wer_register; + integration->free_func = wer_free; + + return integration; +} + +#endif /* SENTRY_PLATFORM_WINDOWS */ From 3fafc2992a09b5d57c64e5964fc1b99eac4b38fc Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 14:58:43 +0200 Subject: [PATCH 04/13] refactor: use Qt-style compile flag for WER integration Replace the generic sentry_integration_t approach with a simpler pattern matching the existing Qt integration (SENTRY_INTEGRATION_QT): - Remove sentry_integration_t, sentry_options_add_integration(), and all the dynamic array management in options. - Add a SENTRY_INTEGRATION_WER CMake option that conditionally compiles the WER integration source and sets the compile definition. - sentry_integration_setup_wer(scope, options) is called from sentry_init() inside SENTRY_WITH_SCOPE_MUT. - The WER integration uses static globals for wer.dll function pointers instead of a heap-allocated state struct. --- CMakeLists.txt | 7 ++ include/sentry.h | 30 ------ src/CMakeLists.txt | 8 +- src/integrations/sentry_integration_wer.c | 116 ++++++++-------------- src/integrations/sentry_integration_wer.h | 15 +++ src/sentry_core.c | 14 +-- src/sentry_integration.h | 23 ----- src/sentry_options.c | 34 ------- src/sentry_options.h | 3 - 9 files changed, 78 insertions(+), 172 deletions(-) create mode 100644 src/integrations/sentry_integration_wer.h delete mode 100644 src/sentry_integration.h diff --git a/CMakeLists.txt b/CMakeLists.txt index bacef22283..f07a9994e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -952,6 +952,13 @@ 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") +if(SENTRY_INTEGRATION_WER) + if(NOT WIN32) + message(FATAL_ERROR "SENTRY_INTEGRATION_WER requires Windows") + endif() +endif() + include(CMakePackageConfigHelpers) configure_package_config_file(sentry-config.cmake.in sentry-config.cmake INSTALL_DESTINATION "${CMAKE_INSTALL_CMAKEDIR}") diff --git a/include/sentry.h b/include/sentry.h index c65aa3f62d..2c41e9dfd2 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -860,15 +860,6 @@ SENTRY_API void sentry_capture_envelope(sentry_envelope_t *envelope); struct sentry_options_s; typedef struct sentry_options_s sentry_options_t; -/** - * This represents an integration that can be configured via options and is - * automatically wired up during `sentry_init`. - * - * See `sentry_options_add_integration`. - */ -struct sentry_integration_s; -typedef struct sentry_integration_s sentry_integration_t; - /** * This represents an interface for user-defined transports. * @@ -2002,27 +1993,6 @@ SENTRY_API uint64_t sentry_options_get_transfer_timeout(sentry_options_t *opts); SENTRY_API void sentry_options_set_backend( sentry_options_t *opts, sentry_backend_t *backend); -/** - * Adds an integration to the options. - * - * The integration's `register_func` callback will be invoked during - * `sentry_init` (while the scope lock is held), allowing the integration to - * set up scope observers and other SDK internals. - * - * Takes ownership of `integration`. - */ -SENTRY_EXPERIMENTAL_API void sentry_options_add_integration( - sentry_options_t *opts, sentry_integration_t *integration); - -/** - * Creates a WER (Windows Error Reporting) integration that syncs scope tags - * and attachments to WER via `WerRegisterCustomMetadata`, `WerRegisterFile`, - * and `WerRegisterMemoryBlock`. - * - * Use with `sentry_options_add_integration`. - */ -SENTRY_EXPERIMENTAL_API sentry_integration_t *sentry_integration_wer_new(void); - /* -- Global/Scope APIs -- */ /** diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 2a7066a4e5..0509f4d974 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -89,7 +89,6 @@ sentry_target_sources_cwd(sentry sentry_random.h sentry_windows_dbghelp.c sentry_windows_dbghelp.h - integrations/sentry_integration_wer.c path/sentry_path_windows.c process/sentry_process_windows.c symbolizer/sentry_symbolizer_windows.c @@ -307,6 +306,13 @@ if(SENTRY_INTEGRATION_QT) integrations/sentry_integration_qt.h ) endif() +if(SENTRY_INTEGRATION_WER) + 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 index 5273d78f33..0f5d78ecaf 100644 --- a/src/integrations/sentry_integration_wer.c +++ b/src/integrations/sentry_integration_wer.c @@ -1,12 +1,10 @@ -#include "sentry_boot.h" +#include "sentry_integration_wer.h" #ifdef SENTRY_PLATFORM_WINDOWS # include "sentry_alloc.h" # include "sentry_attachment.h" -# include "sentry_integration.h" # include "sentry_logger.h" -# include "sentry_scope.h" # include "sentry_string.h" # include @@ -15,18 +13,35 @@ # define WER_FILE_ANONYMOUS_DATA 0x2 # endif -typedef struct { - HMODULE wer_dll; - HRESULT(WINAPI *WerRegisterCustomMetadata_fn)(PCWSTR key, PCWSTR value); - HRESULT(WINAPI *WerUnregisterCustomMetadata_fn)(PCWSTR key); -} wer_state_t; +static HMODULE g_wer_dll = NULL; +static HRESULT(WINAPI *g_WerRegisterCustomMetadata)(PCWSTR, PCWSTR) = NULL; +static HRESULT(WINAPI *g_WerUnregisterCustomMetadata)(PCWSTR) = NULL; + +static void +ensure_wer_loaded(void) +{ + if (g_wer_dll) { + return; + } + g_wer_dll = LoadLibraryW(L"wer.dll"); + if (g_wer_dll) { + g_WerRegisterCustomMetadata = (HRESULT(WINAPI *)(PCWSTR, + PCWSTR))GetProcAddress(g_wer_dll, "WerRegisterCustomMetadata"); + g_WerUnregisterCustomMetadata = (HRESULT(WINAPI *)( + PCWSTR))GetProcAddress(g_wer_dll, "WerUnregisterCustomMetadata"); + } + if (!g_WerRegisterCustomMetadata) { + SENTRY_DEBUG("WerRegisterCustomMetadata not available; " + "tag sync to WER will be skipped"); + } +} static HRESULT wer_set_tag(void *data, const char *key, size_t key_len, const char *value, size_t value_len) { - wer_state_t *state = (wer_state_t *)data; - if (!state->WerRegisterCustomMetadata_fn) { + (void)data; + if (!g_WerRegisterCustomMetadata) { return S_OK; } if (!key || !value) { @@ -46,10 +61,10 @@ wer_set_tag(void *data, const char *key, size_t key_len, const char *value, return E_OUTOFMEMORY; } - HRESULT hr = state->WerRegisterCustomMetadata_fn(key_w, value_w); + HRESULT hr = g_WerRegisterCustomMetadata(key_w, value_w); if (FAILED(hr)) { - SENTRY_WARNF("WerRegisterCustomMetadata failed: hr=0x%08lx", - (unsigned long)hr); + SENTRY_WARNF( + "WerRegisterCustomMetadata failed: hr=0x%08lx", (unsigned long)hr); } sentry_free(value_w); @@ -60,8 +75,8 @@ wer_set_tag(void *data, const char *key, size_t key_len, const char *value, static HRESULT wer_remove_tag(void *data, const char *key, size_t key_len) { - wer_state_t *state = (wer_state_t *)data; - if (!state->WerUnregisterCustomMetadata_fn) { + (void)data; + if (!g_WerUnregisterCustomMetadata) { return S_OK; } if (!key) { @@ -76,7 +91,7 @@ wer_remove_tag(void *data, const char *key, size_t key_len) return E_OUTOFMEMORY; } - HRESULT hr = state->WerUnregisterCustomMetadata_fn(key_w); + HRESULT hr = g_WerUnregisterCustomMetadata(key_w); if (FAILED(hr)) { SENTRY_WARNF("WerUnregisterCustomMetadata failed: hr=0x%08lx", (unsigned long)hr); @@ -100,11 +115,11 @@ wer_add_attachment(void *data, sentry_attachment_t *attachment) if (!path_w) { return S_OK; } - HRESULT hr - = WerRegisterFile(path_w, WerRegFileTypeOther, WER_FILE_ANONYMOUS_DATA); + HRESULT hr = WerRegisterFile( + path_w, WerRegFileTypeOther, WER_FILE_ANONYMOUS_DATA); if (FAILED(hr)) { - SENTRY_WARNF("WerRegisterFile failed: hr=0x%08lx", - (unsigned long)hr); + SENTRY_WARNF( + "WerRegisterFile failed: hr=0x%08lx", (unsigned long)hr); } return hr; } @@ -118,8 +133,8 @@ wer_add_attachment(void *data, sentry_attachment_t *attachment) HRESULT hr = WerRegisterMemoryBlock( (PVOID)attachment->buf, (DWORD)attachment->buf_len); if (FAILED(hr)) { - SENTRY_WARNF("WerRegisterMemoryBlock failed: hr=0x%08lx", - (unsigned long)hr); + SENTRY_WARNF( + "WerRegisterMemoryBlock failed: hr=0x%08lx", (unsigned long)hr); } return hr; } @@ -143,8 +158,8 @@ wer_remove_attachment(void *data, sentry_attachment_t *attachment) } HRESULT hr = WerUnregisterFile(path_w); if (FAILED(hr)) { - SENTRY_WARNF("WerUnregisterFile failed: hr=0x%08lx", - (unsigned long)hr); + SENTRY_WARNF( + "WerUnregisterFile failed: hr=0x%08lx", (unsigned long)hr); } return hr; } @@ -161,41 +176,15 @@ wer_remove_attachment(void *data, sentry_attachment_t *attachment) return S_OK; } -static void -wer_free(void *data) -{ - wer_state_t *state = (wer_state_t *)data; - if (state->wer_dll) { - FreeLibrary(state->wer_dll); - } - sentry_free(state); -} - -static void -wer_register(void *data, sentry_scope_t *scope, const sentry_options_t *options) +void +sentry_integration_wer_setup(sentry_scope_t *scope) { - (void)options; - - wer_state_t *state = (wer_state_t *)data; - state->wer_dll = LoadLibraryW(L"wer.dll"); - if (state->wer_dll) { - state->WerRegisterCustomMetadata_fn = (HRESULT(WINAPI *)(PCWSTR, PCWSTR)) - GetProcAddress(state->wer_dll, "WerRegisterCustomMetadata"); - state->WerUnregisterCustomMetadata_fn - = (HRESULT(WINAPI *)(PCWSTR)) - GetProcAddress(state->wer_dll, "WerUnregisterCustomMetadata"); - } - - if (!state->WerRegisterCustomMetadata_fn) { - SENTRY_DEBUG("WerRegisterCustomMetadata not available; " - "tag sync to WER will be skipped"); - } + ensure_wer_loaded(); sentry_scope_observer_t *observer = sentry__scope_observer_new(); if (!observer) { return; } - observer->data = state; observer->set_tag = wer_set_tag; observer->remove_tag = wer_remove_tag; observer->add_attachment = wer_add_attachment; @@ -204,25 +193,4 @@ wer_register(void *data, sentry_scope_t *scope, const sentry_options_t *options) sentry__scope_add_observer(scope, observer); } -sentry_integration_t * -sentry_integration_wer_new(void) -{ - wer_state_t *state = SENTRY_MAKE(wer_state_t); - if (!state) { - return NULL; - } - - sentry_integration_t *integration = SENTRY_MAKE(sentry_integration_t); - if (!integration) { - sentry_free(state); - return NULL; - } - - integration->data = state; - integration->register_func = wer_register; - integration->free_func = wer_free; - - return integration; -} - #endif /* SENTRY_PLATFORM_WINDOWS */ 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_core.c b/src/sentry_core.c index ac1390e4e8..8486c373e0 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,13 +232,9 @@ sentry_init(sentry_options_t *options) sentry__scope_update_dsc(scope, options); - for (size_t i = 0; i < options->num_integrations; i++) { - sentry_integration_t *integration = options->integrations[i]; - if (integration->register_func) { - integration->register_func( - integration->data, 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); diff --git a/src/sentry_integration.h b/src/sentry_integration.h deleted file mode 100644 index 972989e6fa..0000000000 --- a/src/sentry_integration.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef SENTRY_INTEGRATION_H_INCLUDED -#define SENTRY_INTEGRATION_H_INCLUDED - -#include "sentry_boot.h" - -/** - * Integration callback that is invoked during `sentry_init` while the scope - * lock is held. Implementors use this to, e.g., create and register a scope - * observer. - */ -typedef struct sentry_integration_s { - void *data; - - void (*register_func)( - void *data, sentry_scope_t *scope, const sentry_options_t *options); - - /** - * Optional cleanup callback, invoked before the integration struct is freed. - */ - void (*free_func)(void *data); -} sentry_integration_t; - -#endif diff --git a/src/sentry_options.c b/src/sentry_options.c index 3bfb79ec7a..01d1ab1f7b 100644 --- a/src/sentry_options.c +++ b/src/sentry_options.c @@ -156,15 +156,6 @@ sentry_options_free(sentry_options_t *opts) sentry__attachments_free(opts->attachments); sentry__run_free(opts->run); - for (size_t i = 0; i < opts->num_integrations; i++) { - sentry_integration_t *integration = opts->integrations[i]; - if (integration->free_func) { - integration->free_func(integration->data); - } - sentry_free(integration); - } - sentry_free(opts->integrations); - sentry_free(opts); } @@ -939,31 +930,6 @@ sentry_options_set_backend(sentry_options_t *opts, sentry_backend_t *backend) opts->backend = backend; } -void -sentry_options_add_integration( - sentry_options_t *opts, sentry_integration_t *integration) -{ - if (!integration) { - return; - } - - size_t new_count = opts->num_integrations + 1; - sentry_integration_t **new_array - = sentry__calloc(new_count, sizeof(sentry_integration_t *)); - if (!new_array) { - sentry_free(integration); - return; - } - if (opts->integrations) { - memcpy(new_array, opts->integrations, - opts->num_integrations * sizeof(sentry_integration_t *)); - sentry_free(opts->integrations); - } - new_array[opts->num_integrations] = integration; - opts->integrations = new_array; - opts->num_integrations = new_count; -} - void sentry_options_set_enable_logs(sentry_options_t *opts, int enable_logs) { diff --git a/src/sentry_options.h b/src/sentry_options.h index e9f848964c..0947ef452a 100644 --- a/src/sentry_options.h +++ b/src/sentry_options.h @@ -5,7 +5,6 @@ #include "sentry_attachment.h" #include "sentry_database.h" -#include "sentry_integration.h" #include "sentry_logger.h" #include "sentry_session.h" #include "sentry_utils.h" @@ -96,8 +95,6 @@ struct sentry_options_s { not exposed through the options API */ struct sentry_backend_s *backend; sentry_session_t *session; - sentry_integration_t **integrations; - size_t num_integrations; long refcount; uint64_t shutdown_timeout; From 5f897b3c8f66386773d69d562aa27465c978bc53 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:32:55 +0200 Subject: [PATCH 05/13] Allow SENTRY_INTEGRATION_WER to be enabled on non-Windows platforms The source file is already guarded with #ifdef SENTRY_PLATFORM_WINDOWS, so it quietly becomes a no-op on other platforms without needing a FATAL_ERROR. --- CMakeLists.txt | 5 ----- 1 file changed, 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f07a9994e1..8f2f039f71 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -953,11 +953,6 @@ if(SENTRY_INTEGRATION_QT) endif() option(SENTRY_INTEGRATION_WER "Build WER (Windows Error Reporting) integration") -if(SENTRY_INTEGRATION_WER) - if(NOT WIN32) - message(FATAL_ERROR "SENTRY_INTEGRATION_WER requires Windows") - endif() -endif() include(CMakePackageConfigHelpers) configure_package_config_file(sentry-config.cmake.in sentry-config.cmake From d05e7cc78d092a9064756ada6d0e65487d5a5f40 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:35:17 +0200 Subject: [PATCH 06/13] fix: only define SENTRY_INTEGRATION_WER on Windows --- src/CMakeLists.txt | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0509f4d974..d573f27142 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -306,13 +306,15 @@ if(SENTRY_INTEGRATION_QT) integrations/sentry_integration_qt.h ) endif() -if(SENTRY_INTEGRATION_WER) - target_compile_definitions(sentry PRIVATE SENTRY_INTEGRATION_WER) - sentry_target_sources_cwd(sentry - integrations/sentry_integration_wer.c - integrations/sentry_integration_wer.h - ) -endif() + if(SENTRY_INTEGRATION_WER) + if(WIN32) + target_compile_definitions(sentry PRIVATE SENTRY_INTEGRATION_WER) + endif() + sentry_target_sources_cwd(sentry + integrations/sentry_integration_wer.c + integrations/sentry_integration_wer.h + ) + endif() # screenshot if(SENTRY_SCREENSHOT_WINDOWS) From 39bb76d88ad2a4550b92e93446b3c0ed9f4cfa06 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:35:38 +0200 Subject: [PATCH 07/13] Skip compiling WER integration sources on non-Windows --- src/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index d573f27142..085f0cc192 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -309,11 +309,11 @@ endif() if(SENTRY_INTEGRATION_WER) if(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() - sentry_target_sources_cwd(sentry - integrations/sentry_integration_wer.c - integrations/sentry_integration_wer.h - ) endif() # screenshot From 6b115e240cd3c66593330503e64977f65a039b77 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:36:12 +0200 Subject: [PATCH 08/13] Simplify SENTRY_INTEGRATION_WER guard to single condition --- src/CMakeLists.txt | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 085f0cc192..8d6c700bb1 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -306,15 +306,13 @@ if(SENTRY_INTEGRATION_QT) integrations/sentry_integration_qt.h ) endif() - if(SENTRY_INTEGRATION_WER) - if(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() - 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) From 806214ee76c905b687a8372b462b58b81e420786 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:37:01 +0200 Subject: [PATCH 09/13] Remove redundant SENTRY_PLATFORM_WINDOWS guard from wer integration --- src/integrations/sentry_integration_wer.c | 32 ++++++++++------------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c index 0f5d78ecaf..265a513a03 100644 --- a/src/integrations/sentry_integration_wer.c +++ b/src/integrations/sentry_integration_wer.c @@ -1,34 +1,32 @@ #include "sentry_integration_wer.h" -#ifdef SENTRY_PLATFORM_WINDOWS +#include "sentry_alloc.h" +#include "sentry_attachment.h" +#include "sentry_logger.h" +#include "sentry_string.h" -# include "sentry_alloc.h" -# include "sentry_attachment.h" -# include "sentry_logger.h" -# include "sentry_string.h" +#include -# include +#ifndef WER_FILE_ANONYMOUS_DATA +# define WER_FILE_ANONYMOUS_DATA 0x2 +#endif -# ifndef WER_FILE_ANONYMOUS_DATA -# define WER_FILE_ANONYMOUS_DATA 0x2 -# endif - -static HMODULE g_wer_dll = NULL; +static HMODULE g_kernel32 = NULL; static HRESULT(WINAPI *g_WerRegisterCustomMetadata)(PCWSTR, PCWSTR) = NULL; static HRESULT(WINAPI *g_WerUnregisterCustomMetadata)(PCWSTR) = NULL; static void ensure_wer_loaded(void) { - if (g_wer_dll) { + if (g_WerRegisterCustomMetadata) { return; } - g_wer_dll = LoadLibraryW(L"wer.dll"); - if (g_wer_dll) { + g_kernel32 = GetModuleHandleW(L"kernel32.dll"); + if (g_kernel32) { g_WerRegisterCustomMetadata = (HRESULT(WINAPI *)(PCWSTR, - PCWSTR))GetProcAddress(g_wer_dll, "WerRegisterCustomMetadata"); + PCWSTR))GetProcAddress(g_kernel32, "WerRegisterCustomMetadata"); g_WerUnregisterCustomMetadata = (HRESULT(WINAPI *)( - PCWSTR))GetProcAddress(g_wer_dll, "WerUnregisterCustomMetadata"); + PCWSTR))GetProcAddress(g_kernel32, "WerUnregisterCustomMetadata"); } if (!g_WerRegisterCustomMetadata) { SENTRY_DEBUG("WerRegisterCustomMetadata not available; " @@ -192,5 +190,3 @@ sentry_integration_wer_setup(sentry_scope_t *scope) sentry__scope_add_observer(scope, observer); } - -#endif /* SENTRY_PLATFORM_WINDOWS */ From ddaa2981fc823da78920771924a5b4bface46b6e Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:37:51 +0200 Subject: [PATCH 10/13] Make kernel32 module handle a local variable --- src/integrations/sentry_integration_wer.c | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c index 265a513a03..0095e1031b 100644 --- a/src/integrations/sentry_integration_wer.c +++ b/src/integrations/sentry_integration_wer.c @@ -11,7 +11,6 @@ # define WER_FILE_ANONYMOUS_DATA 0x2 #endif -static HMODULE g_kernel32 = NULL; static HRESULT(WINAPI *g_WerRegisterCustomMetadata)(PCWSTR, PCWSTR) = NULL; static HRESULT(WINAPI *g_WerUnregisterCustomMetadata)(PCWSTR) = NULL; @@ -21,12 +20,12 @@ ensure_wer_loaded(void) if (g_WerRegisterCustomMetadata) { return; } - g_kernel32 = GetModuleHandleW(L"kernel32.dll"); - if (g_kernel32) { + HMODULE kernel32 = GetModuleHandleW(L"kernel32.dll"); + if (kernel32) { g_WerRegisterCustomMetadata = (HRESULT(WINAPI *)(PCWSTR, - PCWSTR))GetProcAddress(g_kernel32, "WerRegisterCustomMetadata"); + PCWSTR))GetProcAddress(kernel32, "WerRegisterCustomMetadata"); g_WerUnregisterCustomMetadata = (HRESULT(WINAPI *)( - PCWSTR))GetProcAddress(g_kernel32, "WerUnregisterCustomMetadata"); + PCWSTR))GetProcAddress(kernel32, "WerUnregisterCustomMetadata"); } if (!g_WerRegisterCustomMetadata) { SENTRY_DEBUG("WerRegisterCustomMetadata not available; " From 610a159ab54c642b57c42a278e1092f58d0348b3 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:38:41 +0200 Subject: [PATCH 11/13] Add clarifying comment about Windows 10 version 1703+ requirement --- src/integrations/sentry_integration_wer.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c index 0095e1031b..268b5f32eb 100644 --- a/src/integrations/sentry_integration_wer.c +++ b/src/integrations/sentry_integration_wer.c @@ -11,6 +11,9 @@ # define WER_FILE_ANONYMOUS_DATA 0x2 #endif +// WerRegisterCustomMetadata / WerUnregisterCustomMetadata require +// Windows 10, version 1703+. They are loaded at runtime so the +// integration degrades gracefully on older builds. static HRESULT(WINAPI *g_WerRegisterCustomMetadata)(PCWSTR, PCWSTR) = NULL; static HRESULT(WINAPI *g_WerUnregisterCustomMetadata)(PCWSTR) = NULL; From 727e266f4bf8fd108ed0c3d8bb99edf0f8281e04 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:39:56 +0200 Subject: [PATCH 12/13] Explain WER_FILE_ANONYMOUS_DATA fallback --- src/integrations/sentry_integration_wer.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c index 268b5f32eb..f37608898a 100644 --- a/src/integrations/sentry_integration_wer.c +++ b/src/integrations/sentry_integration_wer.c @@ -7,6 +7,8 @@ #include +// WER_FILE_ANONYMOUS_DATA was added in the Windows 8 SDK; +// provide a fallback for older SDKs. #ifndef WER_FILE_ANONYMOUS_DATA # define WER_FILE_ANONYMOUS_DATA 0x2 #endif From de2ad2d1579c829bbab80c8829b7a307690558f0 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 2 Jul 2026 15:43:01 +0200 Subject: [PATCH 13/13] tweak --- src/integrations/sentry_integration_wer.c | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/integrations/sentry_integration_wer.c b/src/integrations/sentry_integration_wer.c index f37608898a..1d5fe4bdab 100644 --- a/src/integrations/sentry_integration_wer.c +++ b/src/integrations/sentry_integration_wer.c @@ -7,20 +7,17 @@ #include -// WER_FILE_ANONYMOUS_DATA was added in the Windows 8 SDK; -// provide a fallback for older SDKs. +// Windows 8+ SDK #ifndef WER_FILE_ANONYMOUS_DATA # define WER_FILE_ANONYMOUS_DATA 0x2 #endif -// WerRegisterCustomMetadata / WerUnregisterCustomMetadata require -// Windows 10, version 1703+. They are loaded at runtime so the -// integration degrades gracefully on older builds. +// Windows 10 1703+ static HRESULT(WINAPI *g_WerRegisterCustomMetadata)(PCWSTR, PCWSTR) = NULL; static HRESULT(WINAPI *g_WerUnregisterCustomMetadata)(PCWSTR) = NULL; static void -ensure_wer_loaded(void) +wer_init(void) { if (g_WerRegisterCustomMetadata) { return; @@ -181,7 +178,7 @@ wer_remove_attachment(void *data, sentry_attachment_t *attachment) void sentry_integration_wer_setup(sentry_scope_t *scope) { - ensure_wer_loaded(); + wer_init(); sentry_scope_observer_t *observer = sentry__scope_observer_new(); if (!observer) {