From a44dee556b3df078fb66a546e77c0f57a7a9c655 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 12:05:47 +0100 Subject: [PATCH 1/2] fix(android): Device context in crash daemon events Inproc crashes are sent from the outbox by the Android SDK, which adds the device context. The crash daemon sends its events itself, so they had none. Add the same static fields as the Android SDK: | Field | Example | Source | |----------------|-------------------|---------------------------| | `manufacturer` | `Google` | `ro.product.manufacturer` | | `brand` | `google` | `ro.product.brand` | | `model` | `Pixel 8 Pro` | `ro.product.model` | | `model_id` | `CP3A.261005.005` | `ro.build.id` | | `family` | `Pixel` | first word of `model` | --- src/backends/native/sentry_crash_daemon.c | 48 +++++++++++++++++++++++ tests/test_integration_android.py | 29 +++++++++++--- 2 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index 7d983e2db9..317f3a5d7e 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -55,6 +55,9 @@ # if defined(SENTRY_PLATFORM_LINUX) # include "unwinder/sentry_unwinder.h" # endif +# if defined(SENTRY_PLATFORM_ANDROID) +# include +# endif # if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID) /** * Read one logical line of `/proc//maps`. A pathname longer than the @@ -4186,6 +4189,49 @@ ensure_device_arch(sentry_value_t event) } #endif +#if defined(SENTRY_PLATFORM_ANDROID) +static void +set_device_property(sentry_value_t device, const char *key, const char *name) +{ + char value[PROP_VALUE_MAX]; + if (__system_property_get(name, value) > 0) { + sentry_value_set_by_key(device, key, sentry_value_new_string(value)); + } +} + +// The Android SDK adds the device context only to the crashes it sends from +// the outbox, so synthesize the same static fields for the daemon's events. +static void +ensure_device_context(sentry_value_t event) +{ + sentry_value_t contexts = sentry_value_get_by_key(event, "contexts"); + if (sentry_value_is_null(contexts)) { + contexts = sentry_value_new_object(); + if (sentry_value_set_by_key(event, "contexts", contexts) != 0) { + return; + } + } + if (!sentry_value_is_null(sentry_value_get_by_key(contexts, "device"))) { + return; + } + sentry_value_t device = sentry_value_new_object(); + sentry_value_set_by_key(device, "type", sentry_value_new_string("device")); + set_device_property(device, "manufacturer", "ro.product.manufacturer"); + set_device_property(device, "brand", "ro.product.brand"); + set_device_property(device, "model", "ro.product.model"); + set_device_property(device, "model_id", "ro.build.id"); + const char *model + = sentry_value_as_string(sentry_value_get_by_key(device, "model")); + if (model[0] != '\0') { + const char *space = strchr(model, ' '); + sentry_value_set_by_key(device, "family", + sentry_value_new_string_n( + model, space ? (size_t)(space - model) : strlen(model))); + } + sentry_value_set_by_key(contexts, "device", device); +} +#endif + /** * Removes the crash envelope once the transport has sent or dumped the crash, * or renames it for a later send. Either way, it also removes the crash event, @@ -4328,6 +4374,8 @@ sentry__process_crash(const sentry_options_t *options, sentry_crash_ipc_t *ipc, } #if defined(SENTRY_PLATFORM_WINDOWS) ensure_device_arch(base); +#elif defined(SENTRY_PLATFORM_ANDROID) + ensure_device_context(base); #endif // Apply the app's current upload gate (user consent, offline mode) before diff --git a/tests/test_integration_android.py b/tests/test_integration_android.py index f38ec242c1..e676aa8a4a 100644 --- a/tests/test_integration_android.py +++ b/tests/test_integration_android.py @@ -2,7 +2,7 @@ import pytest -from . import adb, run +from . import Envelope, adb, run from .conditions import has_native, is_android, is_asan, is_tsan pytestmark = pytest.mark.skipif( @@ -18,13 +18,13 @@ def test_native_android(cmake): database_path = "/data/local/tmp/.sentry-native" - def find_minidumps(timeout=30 if is_asan or is_tsan else 10): + def find_run_files(pattern, timeout=30 if is_asan or is_tsan else 10): deadline = time.time() + timeout last_result = None while last_result is None or time.time() < deadline: last_result = adb( "shell", - f'for f in {database_path}/*.run/__sentry-crash.dmp; do [ -f "$f" ] && echo "$f"; done', + f'for f in {database_path}/*.run/{pattern}; do [ -f "$f" ] && echo "$f"; done', capture_output=True, text=True, check=False, @@ -47,16 +47,35 @@ def find_minidumps(timeout=30 if is_asan or is_tsan else 10): ) adb("shell", f"rm -rf {database_path}", check=True) - assert find_minidumps(timeout=0) == [] + assert find_run_files("__sentry-crash.dmp", timeout=0) == [] try: run(tmp_path, "sentry_test_integration", ["log", "crash"], expect_failure=True) - minidumps = find_minidumps() + minidumps = find_run_files("__sentry-crash.dmp") assert minidumps, "native backend should create a minidump on Android" local_minidump = tmp_path / "android.dmp" adb("pull", minidumps[0], str(local_minidump), check=True, capture_output=True) assert local_minidump.stat().st_size > 0 + + envelopes = find_run_files("*.envelope") + assert envelopes, "native backend should create a crash envelope on Android" + + local_envelope = tmp_path / "android.envelope" + adb("pull", envelopes[0], str(local_envelope), check=True, capture_output=True) + event = Envelope.deserialize(local_envelope.read_bytes()).get_event() + + def getprop(name): + return adb( + "shell", "getprop", name, capture_output=True, text=True, check=True + ).stdout.strip() + + device = event["contexts"]["device"] + assert device["manufacturer"] == getprop("ro.product.manufacturer") + assert device["brand"] == getprop("ro.product.brand") + assert device["model"] == getprop("ro.product.model") + assert device["model_id"] == getprop("ro.build.id") + assert device["family"] == device["model"].split(" ")[0] finally: adb("shell", f"rm -rf {database_path}", check=False) From 15a5fd33dd222a506a22ed8ab433c32da2fe457c Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 30 Nov 2026 12:38:31 +0100 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fdf46d7e4a..98cd2c45fb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Drop the outdated 1 GiB size limit for TUS attachment uploads. ([#2352](https://github.com/getsentry/sentry-native/pull/2352)) - Native: detect daemon startup failure to avoid a 10s stall in `sentry_init` on Windows and Linux. ([#2349](https://github.com/getsentry/sentry-native/pull/2349)) -- Native/Android: fix the OS context in out-of-process crashes. ([#2354](https://github.com/getsentry/sentry-native/pull/2354)) +- Native/Android: fix the OS and device contexts in out-of-process crashes. ([#2354](https://github.com/getsentry/sentry-native/pull/2354), [#2355](https://github.com/getsentry/sentry-native/pull/2355)) ## 1.0.0-beta.2