Skip to content
Open
Changes from 1 commit
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
37 changes: 22 additions & 15 deletions kotlinx-coroutines-debug/src/Attach.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,30 @@ internal class ByteBuddyDynamicAttach : Function1<Boolean, Unit> {

private fun attach() {
ByteBuddyAgent.install(ByteBuddyAgent.AttachmentProvider.ForEmulatedAttachment.INSTANCE)
val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt")
val cl2 = Class.forName("kotlinx.coroutines.debug.internal.DebugProbesKt")

ByteBuddy()
.redefine(cl2)
.name(cl.name)
.make()
.load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent())
dynamicTypeWithProbes.load(targetClassLoader, ClassReloadingStrategy.fromInstalledAgent())
}

private fun detach() {
val cl = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt")
val cl2 = Class.forName("kotlinx.coroutines.debug.NoOpProbesKt")
ByteBuddy()
.redefine(cl2)
.name(cl.name)
.make()
.load(cl.classLoader, ClassReloadingStrategy.fromInstalledAgent())
dynamicTypeWithoutProbes.load(targetClassLoader, ClassReloadingStrategy.fromInstalledAgent())
}
}

private val targetClassLoader = Class.forName(classNameToOverride).classLoader

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These things probably belong to ByteBuddyDynamicAttach's companion, potentially as @JvmStatic encapsulation-wise.

Beware of targetClassLoader computed eagerly in that case though


private const val classNameToOverride = "kotlin.coroutines.jvm.internal.DebugProbesKt"

private val dynamicTypeWithProbes by lazy {
val classWithProbes = Class.forName("kotlinx.coroutines.debug.internal.DebugProbesKt")
ByteBuddy()
.redefine(classWithProbes)
.name(classNameToOverride)
.make()
}

private val dynamicTypeWithoutProbes by lazy {
val classWithoutProbes = Class.forName("kotlinx.coroutines.debug.NoOpProbesKt")
ByteBuddy()
.redefine(classWithoutProbes)
.name(classNameToOverride)
.make()
}