Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
48 changes: 48 additions & 0 deletions src/backends/native/sentry_crash_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
# if defined(SENTRY_PLATFORM_LINUX)
# include "unwinder/sentry_unwinder.h"
# endif
# if defined(SENTRY_PLATFORM_ANDROID)
# include <sys/system_properties.h>
# endif
# if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
/**
* Read one logical line of `/proc/<pid>/maps`. A pathname longer than the
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
30 changes: 30 additions & 0 deletions src/sentry_os.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
# include <sys/uio.h>
# include <unistd.h>
#endif
#ifdef SENTRY_PLATFORM_ANDROID
# include <sys/system_properties.h>
#endif

#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
ssize_t
Expand Down Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
16 changes: 16 additions & 0 deletions tests/test_e2e_sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 24 additions & 5 deletions tests/test_integration_android.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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,
Expand All @@ -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)
20 changes: 20 additions & 0 deletions tests/unit/test_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
# include "sentry_unix_pageallocator.h"
#endif

#ifdef SENTRY_PLATFORM_ANDROID
# include <sys/system_properties.h>
# include <sys/utsname.h>
#endif

#ifdef SENTRY_PLATFORM_WINDOWS
# define setenv(k, v, o) _putenv_s(k, v)
# define unsetenv(k) _putenv_s(k, "")
Expand Down Expand Up @@ -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);
}

Expand Down
Loading