Skip to content
Open
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
14 changes: 14 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ jobs:
- name: Setup Gradle
uses: gradle/actions/setup-gradle@8da46b539cd8fe6ab27ca75128919a18d757604c # v6.4.1

# DO NOT MERGE (remove after releasing getsentry/sentry-native#2301)
- name: Checkout sentry-native
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
repository: getsentry/sentry-native
ref: jpnurmi/fix/native-android-21
path: sentry-native
submodules: 'recursive'

# DO NOT MERGE (remove after releasing getsentry/sentry-native#2301)
- name: Publish sentry-native-ndk-outproc to Maven Local
working-directory: sentry-native/ndk
run: ./gradlew --no-daemon -PversionName=0.0.0-local :sentry-native-ndk-outproc:publishToMavenLocal

- name: Run Tests and Lint
run: make preMerge

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

- Nav3: Stop attributing errors and spans from an Activity or ViewPager2 page without a `SentryNavEffect` to the most recently visited Nav3 root ([#6483](https://github.com/getsentry/sentry-java/pull/6483))
- Android: Count deep sleep in breadcrumb, screenshot and view hierarchy debounce windows, so the first low-memory or battery breadcrumb after a long Doze is no longer dropped ([#6590](https://github.com/getsentry/sentry-java/pull/6590))
- Android: Out-of-process crash reporting now supports Android 5.0 (API level 21) and later, previously required Android 11 (API level 30) ([#6614](https://github.com/getsentry/sentry-java/pull/6614))

### Dependencies

Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ reactor-core = { module = "io.projectreactor:reactor-core", version = "3.5.3" }
retrofit = { module = "com.squareup.retrofit2:retrofit", version.ref = "retrofit" }
retrofit-gson = { module = "com.squareup.retrofit2:converter-gson", version.ref = "retrofit" }
sentry-native-ndk = { module = "io.sentry:sentry-native-ndk", version = "0.17.6" }
sentry-native-ndk-outproc = { module = "io.sentry:sentry-native-ndk-outproc", version = "1.0.0-beta.1" }
sentry-native-ndk-outproc = { module = "io.sentry:sentry-native-ndk-outproc", version = "0.0.0-local" }
servlet-api = { module = "javax.servlet:javax.servlet-api", version = "3.1.0" }
servlet4-api = { module = "javax.servlet:javax.servlet-api", version = "4.0.1" }
servlet-jakarta-api = { module = "jakarta.servlet:jakarta.servlet-api", version = "6.1.0" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import io.sentry.android.fragment.FragmentLifecycleIntegration;
import io.sentry.android.timber.SentryTimberIntegration;
import io.sentry.util.AutoClosableReentrantLock;
import io.sentry.util.FileUtils;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -291,13 +294,26 @@ private static void deduplicateIntegrations(
}
}

// the crash service only runs on API 30+
@SuppressLint("NewApi")
private static boolean isNdkCrashServiceProcess() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) {
return false;
}
final @Nullable String processName = Application.getProcessName();
final @Nullable String processName =
Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
? Application.getProcessName()
: readProcessName();
return processName != null && processName.endsWith(NDK_CRASH_SERVICE_PROCESS_SUFFIX);
}

// argv[0] of an app process is its process name
private static @Nullable String readProcessName() {
try {
final @Nullable String cmdline = FileUtils.readText(new File("/proc/self/cmdline"));
if (cmdline == null) {
return null;
}
final int end = cmdline.indexOf('\0');
return end >= 0 ? cmdline.substring(0, end) : cmdline;
} catch (IOException e) {
return null;
}
}
}
3 changes: 0 additions & 3 deletions sentry-android-ndk-outproc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,3 @@ configurations.configureEach {
```

Otherwise, the build fails because it contains both native backends.

The crash service needs API 30+. On older versions, native crash reporting is
disabled.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import android.os.RemoteException;
import io.sentry.DateUtils;
import io.sentry.ILogger;
import io.sentry.ISentryExecutorService;
import io.sentry.SentryEnvelope;
import io.sentry.SentryEnvelopeItem;
import io.sentry.SentryEvent;
Expand All @@ -42,8 +43,9 @@
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand All @@ -58,6 +60,7 @@ public final class SentryNdkCrashService extends Service {
private static final @NotNull String MERGED_SUFFIX = ".merged";
private static final long POLL_INTERVAL_MILLIS = 250;
private static final long MATCH_TOLERANCE_MILLIS = 30000;
private static final long CONNECT_TIMEOUT_MILLIS = 10000;

private final @NotNull IBinder binder =
new Binder() {
Expand Down Expand Up @@ -137,7 +140,7 @@ protected boolean onTransact(
return null;
}

final int shmFd = io.sentry.ndk.SentryNdk.createCrashShm();
final int shmFd = io.sentry.ndk.SentryNdk.createCrashShm(nativeDatabaseDir.getPath());
if (shmFd < 0) {
options.getLogger().log(SentryLevel.DEBUG, "Failed to create NDK crash shared memory.");
return null;
Expand All @@ -150,6 +153,7 @@ protected boolean onTransact(
ParcelFileDescriptor readyWrite = null;
ParcelFileDescriptor messageWrite = null;
ParcelFileDescriptor messageRead = null;
@Nullable ServiceEnds ends = null;
try {
final ParcelFileDescriptor[] notifyPipe = ParcelFileDescriptor.createPipe();
final ParcelFileDescriptor[] readyPipe = ParcelFileDescriptor.createPipe();
Expand All @@ -160,42 +164,45 @@ protected boolean onTransact(
final ParcelFileDescriptor[] messageSocket = ParcelFileDescriptor.createSocketPair();
messageWrite = messageSocket[0];
messageRead = messageSocket[1];
ends = new ServiceEnds(shm.dup(), notifyRead, readyWrite, messageRead);
notifyRead = null;
readyWrite = null;
messageRead = null;

final Intent intent = new Intent(context, SentryNdkCrashService.class);
final @Nullable ServiceConnection connection =
configureService(
context,
intent,
options.getLogger(),
options.getExecutorService(),
nativeDatabaseDir.getPath(),
context.getApplicationInfo().nativeLibraryDir,
shm,
Process.myPid(),
Process.myTid(),
options.isTombstoneEnabled() && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S,
options.isDebug(),
options.getShutdownTimeoutMillis(),
new ArrayList<>(options.getInAppIncludes()),
new ArrayList<>(options.getInAppExcludes()),
notifyRead,
readyWrite,
messageRead);
ends);
ends = null;
if (connection == null) {
options.getLogger().log(SentryLevel.DEBUG, "NDK crash service did not configure.");
options.getLogger().log(SentryLevel.DEBUG, "NDK crash service did not bind.");
return null;
}

// the daemon attaches once the service connects
io.sentry.ndk.SentryNdk.initCrashDaemon(
shm.detachFd(), notifyWrite.detachFd(), readyRead.detachFd(), messageWrite.detachFd());
return connection;
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
options.getLogger().log(SentryLevel.DEBUG, e, "Failed to start NDK crash service.");
return null;
} catch (IOException | SecurityException e) {
options.getLogger().log(SentryLevel.DEBUG, e, "Failed to start NDK crash service.");
return null;
} finally {
if (ends != null) {
ends.close();
}
closeQuietly(shm);
closeQuietly(notifyRead);
closeQuietly(notifyWrite);
Expand Down Expand Up @@ -225,35 +232,30 @@ private static void closeQuietly(final @Nullable Closeable closeable) {
final @NotNull Context context,
final @NotNull Intent intent,
final @NotNull ILogger logger,
final @NotNull ISentryExecutorService executorService,
final @NotNull String nativeDatabasePath,
final @Nullable String nativeLibraryDir,
final @NotNull ParcelFileDescriptor shm,
final int appPid,
final long appTid,
final boolean tombstoneEnabled,
final boolean debug,
final long shutdownTimeoutMillis,
final @NotNull ArrayList<String> includes,
final @NotNull ArrayList<String> excludes,
final @NotNull ParcelFileDescriptor notifyRead,
final @NotNull ParcelFileDescriptor readyWrite,
final @NotNull ParcelFileDescriptor messageRead)
throws InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
final boolean[] configured = new boolean[] {false};
final @NotNull ServiceEnds ends) {
// a foreground app raises the service process only to visible without BIND_IMPORTANT
final int flags = Context.BIND_AUTO_CREATE | Context.BIND_IMPORTANT;

final ServiceConnection connection =
new ServiceConnection() {
private boolean connected;
private volatile boolean configured;

@Override
public void onServiceConnected(
final @NotNull ComponentName name, final @NotNull IBinder service) {
if (connected) {
// a restarted service process has no daemon for this app
reattach(
if (!ends.take()) {
// a restarted or late service process has no daemon for this app
if (reattach(
service,
logger,
nativeDatabasePath,
Expand All @@ -264,60 +266,75 @@ public void onServiceConnected(
debug,
shutdownTimeoutMillis,
includes,
excludes);
excludes)) {
configured = true;
}
return;
}
connected = true;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
configured[0] =
configured =
transactConfigure(
service,
nativeDatabasePath,
nativeLibraryDir,
shm,
ends.shm,
appPid,
appTid,
tombstoneEnabled,
debug,
shutdownTimeoutMillis,
includes,
excludes,
notifyRead,
readyWrite,
messageRead);
latch.countDown();
ends.notifyRead,
ends.readyWrite,
ends.messageRead);
ends.close();
if (!configured) {
logger.log(SentryLevel.ERROR, "NDK crash service did not configure.");
}
}

@Override
public void onServiceDisconnected(final @NotNull ComponentName name) {
logger.log(SentryLevel.ERROR, "NDK crash service disconnected.");
latch.countDown();
}

@Override
public void onBindingDied(final @NotNull ComponentName name) {
logger.log(SentryLevel.ERROR, "NDK crash service binding died.");
latch.countDown();
// a dead binding does not reconnect, it must be bound again
if (configured[0]) {
if (configured) {
context.unbindService(this);
bind(context, intent, flags, this);
}
}
};

if (!bind(context, intent, flags, connection)) {
return null;
}
boolean ready = false;
boolean bound = false;
try {
ready = latch.await(10000, TimeUnit.MILLISECONDS) && configured[0];
// stay bound, so the service process keeps the app's importance and is not frozen
return ready ? connection : null;
bound = bind(context, intent, flags, connection);
} finally {
if (!ready) {
context.unbindService(connection);
if (!bound) {
ends.close();
}
}
if (!bound) {
return null;
}
try {
executorService.schedule(
() -> {
if (ends.take()) {
// without a reader, native sends and the crash handler fail at once
ends.close();
logger.log(SentryLevel.ERROR, "NDK crash service did not connect in time.");
}
},
CONNECT_TIMEOUT_MILLIS);
} catch (RejectedExecutionException e) {
logger.log(SentryLevel.DEBUG, e, "Failed to schedule the NDK crash service timeout.");
}
// stay bound, so the service process keeps the app's importance and is not frozen
return connection;
}

private static boolean bind(
Expand All @@ -326,13 +343,13 @@ private static boolean bind(
final int flags,
final @NotNull ServiceConnection connection) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// deliver callbacks on the binder thread, the caller blocks on the latch
// deliver callbacks on the binder thread instead of the main thread
return context.bindService(intent, flags, Runnable::run, connection);
}
return context.bindService(intent, connection, flags);
}

private static void reattach(
private static boolean reattach(
final @NotNull IBinder service,
final @NotNull ILogger logger,
final @NotNull String nativeDatabasePath,
Expand All @@ -347,7 +364,7 @@ private static void reattach(
final int shmFd = io.sentry.ndk.SentryNdk.dupCrashShmFd();
if (shmFd < 0) {
logger.log(SentryLevel.ERROR, "NDK crash service restarted, but the app can't reattach.");
return;
return false;
}

final ParcelFileDescriptor shm = ParcelFileDescriptor.adoptFd(shmFd);
Expand Down Expand Up @@ -391,8 +408,10 @@ private static void reattach(
} else {
logger.log(SentryLevel.ERROR, "NDK crash service restarted, failed to reattach the app.");
}
return reattached;
} catch (IOException | SecurityException e) {
logger.log(SentryLevel.ERROR, e, "Failed to reattach to the NDK crash service.");
return false;
} finally {
closeQuietly(shm);
closeQuietly(notifyRead);
Expand Down Expand Up @@ -810,4 +829,35 @@ private DaemonConfig(
this.debug = debug;
}
}

// the service's ends of the crash IPC, handed over on the first connection or closed
private static final class ServiceEnds {
private final @NotNull AtomicBoolean taken = new AtomicBoolean();
private final @NotNull ParcelFileDescriptor shm;
private final @NotNull ParcelFileDescriptor notifyRead;
private final @NotNull ParcelFileDescriptor readyWrite;
private final @NotNull ParcelFileDescriptor messageRead;

private ServiceEnds(
final @NotNull ParcelFileDescriptor shm,
final @NotNull ParcelFileDescriptor notifyRead,
final @NotNull ParcelFileDescriptor readyWrite,
final @NotNull ParcelFileDescriptor messageRead) {
this.shm = shm;
this.notifyRead = notifyRead;
this.readyWrite = readyWrite;
this.messageRead = messageRead;
}

private boolean take() {
return taken.compareAndSet(false, true);
}

private void close() {
closeQuietly(shm);
closeQuietly(notifyRead);
closeQuietly(readyWrite);
closeQuietly(messageRead);
}
}
}
Loading
Loading