Skip to content

Commit 42b3217

Browse files
qwwdfsadPierfrancesco Soffritti
andauthored
Move isInstalledStatically field to a separate object (#2913)
By doing this DebugProbesImpl can check for static installation without having to depend on AgentPremain, which is not compatible with Android. Co-authored-by: Pierfrancesco Soffritti <[email protected]>
1 parent 0f22c38 commit 42b3217

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

kotlinx-coroutines-core/jvm/src/debug/AgentPremain.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44

55
package kotlinx.coroutines.debug
66

7-
import kotlinx.coroutines.debug.internal.DebugProbesImpl
7+
import android.annotation.*
8+
import kotlinx.coroutines.debug.internal.*
89
import sun.misc.*
910
import java.lang.instrument.*
1011
import java.lang.instrument.ClassFileTransformer
1112
import java.security.*
12-
import android.annotation.*
1313

1414
/*
1515
* This class is loaded if and only if kotlinx-coroutines-core was used as -javaagent argument,
@@ -19,15 +19,13 @@ import android.annotation.*
1919
@SuppressLint("all")
2020
internal object AgentPremain {
2121

22-
public var isInstalledStatically = false
23-
2422
private val enableCreationStackTraces = runCatching {
2523
System.getProperty("kotlinx.coroutines.debug.enable.creation.stack.trace")?.toBoolean()
2624
}.getOrNull() ?: DebugProbesImpl.enableCreationStackTraces
2725

2826
@JvmStatic
29-
public fun premain(args: String?, instrumentation: Instrumentation) {
30-
isInstalledStatically = true
27+
fun premain(args: String?, instrumentation: Instrumentation) {
28+
AgentInstallationType.isInstalledStatically = true
3129
instrumentation.addTransformer(DebugProbesTransformer)
3230
DebugProbesImpl.enableCreationStackTraces = enableCreationStackTraces
3331
DebugProbesImpl.install()
@@ -52,7 +50,7 @@ internal object AgentPremain {
5250
* on the fly (-> get rid of ASM dependency).
5351
* You can verify its content either by using javap on it or looking at out integration test module.
5452
*/
55-
isInstalledStatically = true
53+
AgentInstallationType.isInstalledStatically = true
5654
return loader.getResourceAsStream("DebugProbesKt.bin").readBytes()
5755
}
5856
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package kotlinx.coroutines.debug.internal
6+
7+
/**
8+
* Object used to differentiate between agent installed statically or dynamically.
9+
* This is done in a separate object so [DebugProbesImpl] can check for static installation
10+
* without having to depend on [kotlinx.coroutines.debug.AgentPremain], which is not compatible with Android.
11+
* Otherwise, access to `AgentPremain.isInstalledStatically` triggers the load of its internal `ClassFileTransformer`
12+
* that is not available on Android.
13+
*/
14+
internal object AgentInstallationType {
15+
internal var isInstalledStatically = false
16+
}

kotlinx-coroutines-core/jvm/src/debug/internal/DebugProbesImpl.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ internal object DebugProbesImpl {
8282
public fun install(): Unit = coroutineStateLock.write {
8383
if (++installations > 1) return
8484
startWeakRefCleanerThread()
85-
if (AgentPremain.isInstalledStatically) return
85+
if (AgentInstallationType.isInstalledStatically) return
8686
dynamicAttach?.invoke(true) // attach
8787
}
8888

@@ -92,7 +92,7 @@ internal object DebugProbesImpl {
9292
stopWeakRefCleanerThread()
9393
capturedCoroutinesMap.clear()
9494
callerInfoCache.clear()
95-
if (AgentPremain.isInstalledStatically) return
95+
if (AgentInstallationType.isInstalledStatically) return
9696
dynamicAttach?.invoke(false) // detach
9797
}
9898

0 commit comments

Comments
 (0)