From d66f775fe5ca14fc8693f8dbfe9ec1ccf35633fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=99=9F=20Wu=20Sheng?= Date: Sun, 28 Jul 2019 23:33:48 +0800 Subject: [PATCH] Remove javassist from agent dependency (#3176) * Remove javassist dependency * Finish the PR. * Change doc title level. * Remove the duplicated codes. --- apm-sniffer/apm-agent-core/pom.xml | 15 ----- .../plugin}/InstrumentDebuggingClass.java | 10 ++-- .../bootstrap/BootstrapInstrumentBoost.java | 57 +++++++++++-------- apm-sniffer/apm-agent/pom.xml | 1 - .../skywalking/apm/agent/SkyWalkingAgent.java | 3 +- .../setup/service-agent/java-agent/README.md | 2 +- 6 files changed, 43 insertions(+), 45 deletions(-) rename apm-sniffer/{apm-agent/src/main/java/org/apache/skywalking/apm/agent => apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin}/InstrumentDebuggingClass.java (89%) diff --git a/apm-sniffer/apm-agent-core/pom.xml b/apm-sniffer/apm-agent-core/pom.xml index 4cedde0b66..575d7fa1f4 100644 --- a/apm-sniffer/apm-agent-core/pom.xml +++ b/apm-sniffer/apm-agent-core/pom.xml @@ -38,10 +38,6 @@ 1.14.0 20.0 1.9.2 - - - - 3.21.0-GA 3.3.6 2.6.0 2.0.7.Final @@ -58,8 +54,6 @@ ${shade.package}.${shade.io.netty.source} io.opencensus ${shade.package}.${shade.io.opencensus.source} - javassist - ${shade.package}.${shade.javaassist.source} 1.18.0 @@ -104,11 +98,6 @@ netty-tcnative-boringssl-static ${netty-tcnative-boringssl-static.version} - - org.javassist - javassist - ${javaassist.version} - net.bytebuddy byte-buddy-agent @@ -192,10 +181,6 @@ - - ${shade.javaassist.source} - ${shade.javaassist.target} - ${shade.com.lmax.disruptor.source} ${shade.com.lmax.disruptor.target} diff --git a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/InstrumentDebuggingClass.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/InstrumentDebuggingClass.java similarity index 89% rename from apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/InstrumentDebuggingClass.java rename to apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/InstrumentDebuggingClass.java index acb2711a38..bbf2b750e5 100644 --- a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/InstrumentDebuggingClass.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/InstrumentDebuggingClass.java @@ -17,11 +17,10 @@ */ -package org.apache.skywalking.apm.agent; +package org.apache.skywalking.apm.agent.core.plugin; import java.io.File; import java.io.IOException; -import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.dynamic.DynamicType; import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException; import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath; @@ -30,6 +29,9 @@ import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; /** + * The manipulated class output. Write the dynamic classes to the `debugging` folder, when we need to do some debug and + * recheck. + * * @author wu-sheng */ public enum InstrumentDebuggingClass { @@ -38,7 +40,7 @@ public enum InstrumentDebuggingClass { private static final ILog logger = LogManager.getLogger(InstrumentDebuggingClass.class); private File debuggingClassesRootPath; - public void log(TypeDescription typeDescription, DynamicType dynamicType) { + public void log(DynamicType dynamicType) { if (!Config.Agent.IS_OPEN_DEBUGGING_CLASS) { return; } @@ -62,7 +64,7 @@ public enum InstrumentDebuggingClass { try { dynamicType.saveIn(debuggingClassesRootPath); } catch (IOException e) { - logger.error(e, "Can't save class {} to file." + typeDescription.getActualName()); + logger.error(e, "Can't save class {} to file." + dynamicType.getTypeDescription().getActualName()); } } catch (Throwable t) { logger.error(t, "Save debugging classes fail."); diff --git a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java index c1dda6d13e..6e12d9d96b 100644 --- a/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java +++ b/apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/plugin/bootstrap/BootstrapInstrumentBoost.java @@ -26,16 +26,19 @@ import java.lang.instrument.Instrumentation; import java.util.HashMap; import java.util.List; import java.util.Map; -import javassist.ClassPool; -import javassist.CtClass; -import javassist.CtField; +import net.bytebuddy.ByteBuddy; import net.bytebuddy.agent.builder.AgentBuilder; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.dynamic.ClassFileLocator; +import net.bytebuddy.dynamic.DynamicType; import net.bytebuddy.dynamic.loading.ClassInjector; +import net.bytebuddy.pool.TypePool; import org.apache.skywalking.apm.agent.core.boot.AgentPackageNotFoundException; import org.apache.skywalking.apm.agent.core.boot.AgentPackagePath; import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine; +import org.apache.skywalking.apm.agent.core.plugin.InstrumentDebuggingClass; import org.apache.skywalking.apm.agent.core.plugin.PluginException; import org.apache.skywalking.apm.agent.core.plugin.PluginFinder; import org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; @@ -43,6 +46,8 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsIn import org.apache.skywalking.apm.agent.core.plugin.interceptor.StaticMethodsInterceptPoint; import org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader; +import static net.bytebuddy.matcher.ElementMatchers.named; + /** * If there is Bootstrap instrumentation plugin declared in plugin list, BootstrapInstrumentBoost inject the necessary * classes into bootstrap class loader, including generated dynamic delegate classes. @@ -51,6 +56,7 @@ import org.apache.skywalking.apm.agent.core.plugin.loader.AgentClassLoader; */ public class BootstrapInstrumentBoost { private static final ILog logger = LogManager.getLogger(BootstrapInstrumentBoost.class); + private static final String SHADE_PACKAGE = "org.apache.skywalking.apm.dependencies."; private static final String[] HIGH_PRIORITY_CLASSES = { "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance", "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.BootstrapInterRuntimeAssist", @@ -59,7 +65,14 @@ public class BootstrapInstrumentBoost { "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor", "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult", "org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.OverrideCallable", - "org.apache.skywalking.apm.agent.core.plugin.bootstrap.IBootstrapLog" + "org.apache.skywalking.apm.agent.core.plugin.bootstrap.IBootstrapLog", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.RuntimeType", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.This", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.AllArguments", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.AllArguments$Assignment", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.SuperCall", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.Origin", + SHADE_PACKAGE + "net.bytebuddy.implementation.bind.annotation.Morph" }; private static String INSTANCE_METHOD_DELEGATE_TEMPLATE = "org.apache.skywalking.apm.agent.core.plugin.bootstrap.template.InstanceMethodInterTemplate"; private static String INSTANCE_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE = "org.apache.skywalking.apm.agent.core.plugin.bootstrap.template.InstanceMethodInterWithOverrideArgsTemplate"; @@ -128,26 +141,26 @@ public class BootstrapInstrumentBoost { */ private static boolean prepareJREInstrumentation(PluginFinder pluginFinder, Map classesTypeMap) throws PluginException { - ClassPool classPool = ClassPool.getDefault(); + TypePool typePool = TypePool.Default.of(BootstrapInstrumentBoost.class.getClassLoader()); List bootstrapClassMatchDefines = pluginFinder.getBootstrapClassMatchDefine(); for (AbstractClassEnhancePluginDefine define : bootstrapClassMatchDefines) { for (InstanceMethodsInterceptPoint point : define.getInstanceMethodsInterceptPoints()) { if (point.isOverrideArgs()) { - generateDelegator(classesTypeMap, classPool, INSTANCE_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, INSTANCE_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } else { - generateDelegator(classesTypeMap, classPool, INSTANCE_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, INSTANCE_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } } for (ConstructorInterceptPoint point : define.getConstructorsInterceptPoints()) { - generateDelegator(classesTypeMap, classPool, CONSTRUCTOR_DELEGATE_TEMPLATE, point.getConstructorInterceptor()); + generateDelegator(classesTypeMap, typePool, CONSTRUCTOR_DELEGATE_TEMPLATE, point.getConstructorInterceptor()); } for (StaticMethodsInterceptPoint point : define.getStaticMethodsInterceptPoints()) { if (point.isOverrideArgs()) { - generateDelegator(classesTypeMap, classPool, STATIC_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, STATIC_METHOD_WITH_OVERRIDE_ARGS_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } else { - generateDelegator(classesTypeMap, classPool, STATIC_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); + generateDelegator(classesTypeMap, typePool, STATIC_METHOD_DELEGATE_TEMPLATE, point.getMethodsInterceptor()); } } } @@ -160,29 +173,27 @@ public class BootstrapInstrumentBoost { * One key step to avoid class confliction between AppClassLoader and BootstrapClassLoader * * @param classesTypeMap hosts injected binary of generated class - * @param classPool to generate new class + * @param typePool to generate new class * @param templateClassName represents the class as template in this generation process. The templates are * pre-defined in SkyWalking agent core. * @param methodsInterceptor */ - private static void generateDelegator(Map classesTypeMap, ClassPool classPool, + private static void generateDelegator(Map classesTypeMap, TypePool typePool, String templateClassName, String methodsInterceptor) { String internalInterceptorName = internalDelegate(methodsInterceptor); try { - CtClass interClass = classPool.get(templateClassName); - interClass.setName(internalInterceptorName); - - CtField interceptorDefine = interClass.getField("TARGET_INTERCEPTOR"); - interClass.removeField(interceptorDefine); - - interceptorDefine = CtField.make("private static String TARGET_INTERCEPTOR = \"" + methodsInterceptor + "\";", interClass); - interClass.addField(interceptorDefine); + TypeDescription templateTypeDescription = typePool.describe(templateClassName) + .resolve(); - byte[] bytes = interClass.toBytecode(); + DynamicType.Unloaded interceptorType = new ByteBuddy() + .redefine(templateTypeDescription, ClassFileLocator.ForClassLoader.of(BootstrapInstrumentBoost.class.getClassLoader())) + .name(internalInterceptorName) + .field(named("TARGET_INTERCEPTOR")).value(methodsInterceptor) + .make(); - interClass.toClass(); + classesTypeMap.put(internalInterceptorName, interceptorType.getBytes()); - classesTypeMap.put(internalInterceptorName, bytes); + InstrumentDebuggingClass.INSTANCE.log(interceptorType); } catch (Exception e) { throw new PluginException("Generate Dynamic plugin failure", e); } diff --git a/apm-sniffer/apm-agent/pom.xml b/apm-sniffer/apm-agent/pom.xml index 6f95267517..64d51bd078 100644 --- a/apm-sniffer/apm-agent/pom.xml +++ b/apm-sniffer/apm-agent/pom.xml @@ -81,7 +81,6 @@ io.opencensus:* com.google.*:* com.google.guava:guava - org.javassist:* diff --git a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java index 98b3b5367c..8b02e86cb8 100644 --- a/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java +++ b/apm-sniffer/apm-agent/src/main/java/org/apache/skywalking/apm/agent/SkyWalkingAgent.java @@ -39,6 +39,7 @@ import org.apache.skywalking.apm.agent.core.logging.api.ILog; import org.apache.skywalking.apm.agent.core.logging.api.LogManager; import org.apache.skywalking.apm.agent.core.plugin.AbstractClassEnhancePluginDefine; import org.apache.skywalking.apm.agent.core.plugin.EnhanceContext; +import org.apache.skywalking.apm.agent.core.plugin.InstrumentDebuggingClass; import org.apache.skywalking.apm.agent.core.plugin.PluginBootstrap; import org.apache.skywalking.apm.agent.core.plugin.PluginException; import org.apache.skywalking.apm.agent.core.plugin.PluginFinder; @@ -171,7 +172,7 @@ public class SkyWalkingAgent { logger.debug("On Transformation class {}.", typeDescription.getName()); } - InstrumentDebuggingClass.INSTANCE.log(typeDescription, dynamicType); + InstrumentDebuggingClass.INSTANCE.log(dynamicType); } @Override diff --git a/docs/en/setup/service-agent/java-agent/README.md b/docs/en/setup/service-agent/java-agent/README.md index e5f556e43f..e283708f1c 100644 --- a/docs/en/setup/service-agent/java-agent/README.md +++ b/docs/en/setup/service-agent/java-agent/README.md @@ -105,7 +105,7 @@ Now, we have the following known optional plugins. * [Customize enhance](Customize-enhance-trace.md) Trace methods based on description files, rather than write plugin or change source codes. * Plugin of Spring Cloud Gateway 2.1.x in optional plugin folder. Please only active this plugin when you install agent in Spring Gateway. -# Bootstrap class plugins +## Bootstrap class plugins All bootstrap plugins are optional, due to unexpected risk. Bootstrap plugins are provided in `bootstrap-plugins` folder. **Make sure `bootstrapJarTmp` writable by agent, which is required by bootstrap instrumentation. New temp jar files will be generated there.**. -- GitLab