|
| 1 | +package spp.probe |
| 2 | + |
| 3 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint |
| 4 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint |
| 5 | +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine |
| 6 | +import org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch |
| 7 | +import org.apache.skywalking.apm.agent.core.plugin.match.NameMatch |
| 8 | +import java.lang.instrument.ClassFileTransformer |
| 9 | +import java.lang.instrument.Instrumentation |
| 10 | +import java.lang.reflect.Field |
| 11 | +import java.lang.reflect.Modifier |
| 12 | +import java.lang.reflect.Proxy |
| 13 | +import java.util.concurrent.atomic.AtomicBoolean |
| 14 | + |
| 15 | +/** |
| 16 | + * Borrows Instrumentation from SkyWalking/Byte Buddy and boots SourceProbe. |
| 17 | + */ |
| 18 | +class SourceProbePluginDefine : ClassInstanceMethodsEnhancePluginDefine() { |
| 19 | + |
| 20 | + override fun enhanceClass(): ClassMatch { |
| 21 | + val defaultClass = Class.forName( |
| 22 | + "org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$Default" |
| 23 | + ) |
| 24 | + val dispatcherField = defaultClass.getDeclaredField("DISPATCHER") |
| 25 | + makeAccessible(dispatcherField) |
| 26 | + val realDispatcher = dispatcherField.get(null) |
| 27 | + dispatcherField.set(null, null) |
| 28 | + |
| 29 | + val dispatcherClass = Class.forName( |
| 30 | + "org.apache.skywalking.apm.dependencies.net.bytebuddy.agent.builder.AgentBuilder\$Default\$Dispatcher" |
| 31 | + ) |
| 32 | + val addTransformerMethod = dispatcherClass.getDeclaredMethod( |
| 33 | + "addTransformer", |
| 34 | + Instrumentation::class.java, ClassFileTransformer::class.java, Boolean::class.java |
| 35 | + ) |
| 36 | + val setNativeMethodPrefixMethod = dispatcherClass.getDeclaredMethod( |
| 37 | + "setNativeMethodPrefix", |
| 38 | + Instrumentation::class.java, ClassFileTransformer::class.java, String::class.java |
| 39 | + ) |
| 40 | + val isNativeMethodPrefixSupportedMethod = dispatcherClass.getDeclaredMethod( |
| 41 | + "isNativeMethodPrefixSupported", Instrumentation::class.java |
| 42 | + ) |
| 43 | + |
| 44 | + val probeStarted = AtomicBoolean() |
| 45 | + val proxyDispatcher = Proxy.newProxyInstance( |
| 46 | + dispatcherClass.classLoader, |
| 47 | + arrayOf(dispatcherClass) |
| 48 | + ) { _, method, args -> |
| 49 | + return@newProxyInstance when (method.name) { |
| 50 | + "addTransformer" -> { |
| 51 | + if (probeStarted.compareAndSet(false, true)) { |
| 52 | + SourceProbe.bootAsPlugin(args?.get(0) as Instrumentation) |
| 53 | + } |
| 54 | + addTransformerMethod.invoke(realDispatcher, args[0], args[1], args[2]) |
| 55 | + } |
| 56 | + "setNativeMethodPrefix" -> { |
| 57 | + setNativeMethodPrefixMethod.invoke(realDispatcher, args[0], args[1], args[2]) |
| 58 | + } |
| 59 | + "isNativeMethodPrefixSupported" -> { |
| 60 | + isNativeMethodPrefixSupportedMethod.invoke(realDispatcher, args[0]) |
| 61 | + } |
| 62 | + else -> throw IllegalStateException("Unknown method: ${method.name}") |
| 63 | + } |
| 64 | + } |
| 65 | + dispatcherField.set(null, proxyDispatcher) |
| 66 | + |
| 67 | + return NameMatch.byName("") //ignore |
| 68 | + } |
| 69 | + |
| 70 | + @Throws(Exception::class) |
| 71 | + fun makeAccessible(field: Field) { |
| 72 | + field.isAccessible = true |
| 73 | + val modifiersField: Field = Field::class.java.getDeclaredField("modifiers") |
| 74 | + modifiersField.isAccessible = true |
| 75 | + modifiersField.setInt(field, field.modifiers and Modifier.FINAL.inv()) |
| 76 | + } |
| 77 | + |
| 78 | + override fun getConstructorsInterceptPoints(): Array<ConstructorInterceptPoint> { |
| 79 | + return emptyArray() //ignore |
| 80 | + } |
| 81 | + |
| 82 | + override fun getInstanceMethodsInterceptPoints(): Array<InstanceMethodsInterceptPoint> { |
| 83 | + return emptyArray() //ignore |
| 84 | + } |
| 85 | +} |
0 commit comments