Repository: Kotlin/kotlinx.coroutines
File: kotlinx-coroutines-core/jvm/src/debug/internal/AgentPremain.kt (Kotlin)

Code before:
    private fun installSignalHandler() {
        try {
            Signal.handle(Signal("TRAP")) { // kill -5
                if (DebugProbesImpl.isInstalled) {
                    // Case with 'isInstalled' changed between this check-and-act is not considered
                    // a real debug probes use-case, thus is not guarded against.
                    DebugProbesImpl.dumpCoroutines(System.out)
                } else {
                    println("Cannot perform coroutines dump, debug probes are disabled")
                }
            }
        } catch (t: Throwable) {
            // Do nothing, signal cannot be installed, e.g. because we are on Windows
        }
    }
}

/**
 * This class is loaded if and only if kotlinx-coroutines-core was used as a javaagent argument.
 * It serves as a "safe" wrapper around [AgentPremain] that does not require any kotlin-stdlib classes to be loaded for its initialization.
 *
 * Initialization of [AgentPremain] may fail, e.g., because it started before the required kotlin-stdlib classes were loaded.
 * If `kotlinx.coroutines.apply.debug.agent` is set to `false`, the coroutine debug agent will not be applied.
 */
@Suppress("unused")
internal object SafeAgentPremain {
    @JvmStatic
    fun premain(args: String?, instrumentation: Instrumentation) {
        // If this property is disabled, the debug agent will not be applied at all
        val applyDebugAgent = System.getProperty("kotlinx.coroutines.apply.debug.agent", "true").toBoolean()
        if (!applyDebugAgent) return
        try {
            AgentPremain.premain(args, instrumentation)
        } catch (t: Throwable) {
            error("Something went wrong while loading kotlinx.coroutines debug agent.\n" +
                "Please ensure that kotlin-stdlib is present in the classpath.\n" +
                "Alternatively, you can disable kotlinx.coroutines debug agent with this system property: `kotlinx.coroutines.apply.debug.agent=false`\n" +
                t.cause)
        }
    }
}

Review comment (bug):
I don't understand the purpose of this wrapper. What's stopping us from adding the check to `AgentPremain` itself? Won't it be enough to call something like this function as the first thing in `AgentPremain.premain`?
```kotlin
private fun checkIfStdlibIsAvailable() {
  try {
    Result.success(42)
  } catch (_: Throwable) {
    error("kotlinx.coroutines debug agent failed to load " +
        "because the Kotlin standard library is not present in the classpath.")
  }
}
```

Code after:
            className: String,
            classBeingRedefined: Class<*>?,
            protectionDomain: ProtectionDomain,
            classfileBuffer: ByteArray?
        ): ByteArray? {
            if (loader == null || className != "kotlin/coroutines/jvm/internal/DebugProbesKt") {
               return null
            }
            /*
             * DebugProbesKt.bin contains `kotlin.coroutines.jvm.internal.DebugProbesKt` class
             * with method bodies that delegate all calls directly to their counterparts in
             * kotlinx.coroutines.debug.DebugProbesImpl. This is done to avoid classfile patching
             * 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.
             */
            AgentInstallationType.isInstalledStatically = true
            return loader.getResourceAsStream("DebugProbesKt.bin").readBytes()
        }
    }

    private fun installSignalHandler() {
        try {
            Signal.handle(Signal("TRAP")) { // kill -5
                if (DebugProbesImpl.isInstalled) {
                    // Case with 'isInstalled' changed between this check-and-act is not considered
                    // a real debug probes use-case, thus is not guarded against.
                    DebugProbesImpl.dumpCoroutines(System.out)
                } else {
                    println("Cannot perform coroutines dump, debug probes are disabled")
                }
            }
        } catch (t: Throwable) {
            // Do nothing, signal cannot be installed, e.g. because we are on Windows
        }
    }
}