Skip to content

Commit

Permalink
Move isInstalledStatically field to a separate object (Kotlin#2913)
Browse files Browse the repository at this point in the history
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 <psoffritti@google.com>
  • Loading branch information
2 people authored and pablobaxter committed Sep 14, 2022
1 parent afac72e commit 13cca94
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
12 changes: 5 additions & 7 deletions kotlinx-coroutines-core/jvm/src/debug/AgentPremain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

package kotlinx.coroutines.debug

import kotlinx.coroutines.debug.internal.DebugProbesImpl
import android.annotation.*
import kotlinx.coroutines.debug.internal.*
import sun.misc.*
import java.lang.instrument.*
import java.lang.instrument.ClassFileTransformer
import java.security.*
import android.annotation.*

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

public var isInstalledStatically = false

private val enableCreationStackTraces = runCatching {
System.getProperty("kotlinx.coroutines.debug.enable.creation.stack.trace")?.toBoolean()
}.getOrNull() ?: DebugProbesImpl.enableCreationStackTraces

@JvmStatic
public fun premain(args: String?, instrumentation: Instrumentation) {
isInstalledStatically = true
fun premain(args: String?, instrumentation: Instrumentation) {
AgentInstallationType.isInstalledStatically = true
instrumentation.addTransformer(DebugProbesTransformer)
DebugProbesImpl.enableCreationStackTraces = enableCreationStackTraces
DebugProbesImpl.install()
Expand All @@ -52,7 +50,7 @@ internal object AgentPremain {
* on the fly (-> get rid of ASM dependency).
* You can verify its content either by using javap on it or looking at out integration test module.
*/
isInstalledStatically = true
AgentInstallationType.isInstalledStatically = true
return loader.getResourceAsStream("DebugProbesKt.bin").readBytes()
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.coroutines.debug.internal

/**
* Object used to differentiate between agent installed statically or dynamically.
* This is done in a separate object so [DebugProbesImpl] can check for static installation
* without having to depend on [kotlinx.coroutines.debug.AgentPremain], which is not compatible with Android.
* Otherwise, access to `AgentPremain.isInstalledStatically` triggers the load of its internal `ClassFileTransformer`
* that is not available on Android.
*/
internal object AgentInstallationType {
internal var isInstalledStatically = false
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ internal object DebugProbesImpl {
public fun install(): Unit = coroutineStateLock.write {
if (++installations > 1) return
startWeakRefCleanerThread()
if (AgentPremain.isInstalledStatically) return
if (AgentInstallationType.isInstalledStatically) return
dynamicAttach?.invoke(true) // attach
}

Expand All @@ -92,7 +92,7 @@ internal object DebugProbesImpl {
stopWeakRefCleanerThread()
capturedCoroutinesMap.clear()
callerInfoCache.clear()
if (AgentPremain.isInstalledStatically) return
if (AgentInstallationType.isInstalledStatically) return
dynamicAttach?.invoke(false) // detach
}

Expand Down

0 comments on commit 13cca94

Please sign in to comment.