diff --git a/CHANGELOG.md b/CHANGELOG.md index fdf46d7e4..98cd2c45f 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 diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index 7d983e2db..317f3a5d7 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 f38ec242c..e676aa8a4 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)