diff --git a/CHANGELOG.md b/CHANGELOG.md index 756680f45..64068579e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,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/Windows: fix minidump failures on CPUs with AMX enabled. ([#2353](https://github.com/getsentry/sentry-native/pull/2353)) +- Native/Android: fix the OS and device contexts in out-of-process crashes. ([#2354](https://github.com/getsentry/sentry-native/pull/2354)) ## 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/src/sentry_os.c b/src/sentry_os.c index 53831d378..6bad9260a 100644 --- a/src/sentry_os.c +++ b/src/sentry_os.c @@ -20,6 +20,9 @@ # include # include #endif +#ifdef SENTRY_PLATFORM_ANDROID +# include +#endif #if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID) ssize_t @@ -1023,6 +1026,27 @@ static # endif // defined(SENTRY_PLATFORM_LINUX) +# if defined(SENTRY_PLATFORM_ANDROID) +// Android is reported as the OS itself, like the Android SDK does. +static bool +set_os_android(sentry_value_t os, const char *release) +{ + char version[PROP_VALUE_MAX]; + if (__system_property_get("ro.build.version.release", version) <= 0) { + return false; + } + sentry_value_set_by_key(os, "name", sentry_value_new_string("Android")); + sentry_value_set_by_key(os, "version", sentry_value_new_string(version)); + char build[PROP_VALUE_MAX]; + if (__system_property_get("ro.build.display.id", build) > 0) { + sentry_value_set_by_key(os, "build", sentry_value_new_string(build)); + } + sentry_value_set_by_key( + os, "kernel_version", sentry_value_new_string(release)); + return true; +} +# endif + sentry_value_t sentry__get_os_context(void) { @@ -1036,6 +1060,12 @@ sentry__get_os_context(void) goto fail; } +# if defined(SENTRY_PLATFORM_ANDROID) + if (set_os_android(os, uts.release)) { + return os; + } +# endif + if (!set_os_uname(os, uts.sysname, uts.release)) { goto fail; } diff --git a/tests/test_e2e_sentry.py b/tests/test_e2e_sentry.py index 7d46116a0..0d4572532 100644 --- a/tests/test_e2e_sentry.py +++ b/tests/test_e2e_sentry.py @@ -1069,6 +1069,22 @@ def assert_android_native_event( assert event["release"]["version"] == "sentry-native-e2e" assert event["user"]["id"] == test_id + def getprop(name): + return adb( + "shell", "getprop", name, capture_output=True, text=True, check=True + ).stdout.strip() + + contexts = event["contexts"] + assert contexts["os"]["name"] == "Android" + assert contexts["os"]["version"] == getprop("ro.build.version.release") + assert contexts["os"]["build"] == getprop("ro.build.display.id") + assert contexts["os"]["kernel_version"] + assert contexts["device"]["manufacturer"] == getprop("ro.product.manufacturer") + assert contexts["device"]["brand"] == getprop("ro.product.brand") + assert contexts["device"]["model"] == getprop("ro.product.model") + assert contexts["device"]["model_id"] == getprop("ro.build.id") + assert contexts["device"]["family"] == contexts["device"]["model"].split(" ")[0] + exception = get_exception_from_event(event)["values"][0] mechanism = exception["mechanism"] assert mechanism["handled"] is False 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) diff --git a/tests/unit/test_utils.c b/tests/unit/test_utils.c index 85ce19e53..aea173d2b 100644 --- a/tests/unit/test_utils.c +++ b/tests/unit/test_utils.c @@ -12,6 +12,11 @@ # include "sentry_unix_pageallocator.h" #endif +#ifdef SENTRY_PLATFORM_ANDROID +# include +# include +#endif + #ifdef SENTRY_PLATFORM_WINDOWS # define setenv(k, v, o) _putenv_s(k, v) # define unsetenv(k) _putenv_s(k, "") @@ -462,6 +467,21 @@ SENTRY_TEST(os) == SENTRY_VALUE_TYPE_STRING); #endif +#ifdef SENTRY_PLATFORM_ANDROID + char version[PROP_VALUE_MAX]; + TEST_ASSERT(__system_property_get("ro.build.version.release", version) > 0); + struct utsname uts; + TEST_ASSERT(uname(&uts) == 0); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(os, "name")), "Android"); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(os, "version")), + version); + TEST_CHECK_STRING_EQUAL( + sentry_value_as_string(sentry_value_get_by_key(os, "kernel_version")), + uts.release); +#endif + sentry_value_decref(os); }