From 360db7dfd3dff563c2258854b64794bb2495f044 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Sat, 16 May 2020 16:44:52 +0800 Subject: [PATCH] support MethodInfo binding --- .../arthas/bytekit/asm/binding/Binding.java | 15 ++++++++++ .../asm/binding/MethodInfoBinding.java | 30 +++++++++++++++++++ .../taobao/arthas/core/advisor/Enhancer.java | 16 +++++----- spy/src/main/java/java/arthas/SpyAPI.java | 24 +++++++-------- 4 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/MethodInfoBinding.java diff --git a/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/Binding.java b/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/Binding.java index 6e102ffc6..7ae63cffe 100644 --- a/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/Binding.java +++ b/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/Binding.java @@ -312,6 +312,21 @@ public abstract class Binding { } } + @Documented + @Retention(RetentionPolicy.RUNTIME) + @java.lang.annotation.Target(ElementType.PARAMETER) + @BindingParserHandler(parser = MethodInfoBindingParser.class) + public static @interface MethodInfo { + boolean optional() default false; + } + + public static class MethodInfoBindingParser implements BindingParser { + @Override + public Binding parse(Annotation annotation) { + return new MethodDeclarationBinding(); + } + } + @Documented @Retention(RetentionPolicy.RUNTIME) @java.lang.annotation.Target(ElementType.PARAMETER) diff --git a/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/MethodInfoBinding.java b/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/MethodInfoBinding.java new file mode 100644 index 000000000..c1141c58d --- /dev/null +++ b/bytekit/src/main/java/com/taobao/arthas/bytekit/asm/binding/MethodInfoBinding.java @@ -0,0 +1,30 @@ +package com.taobao.arthas.bytekit.asm.binding; + +import com.alibaba.arthas.deps.org.objectweb.asm.Type; +import com.alibaba.arthas.deps.org.objectweb.asm.tree.InsnList; +import com.alibaba.arthas.deps.org.objectweb.asm.tree.MethodNode; +import com.taobao.arthas.bytekit.asm.MethodProcessor; +import com.taobao.arthas.bytekit.utils.AsmOpUtils; + +/** + * method name | method desc 的方式组织 + * + * TODO 是否要有 line number ? + * + * @author hengyunabc 2020-05-16 + * + */ +public class MethodInfoBinding extends Binding { + + @Override + public void pushOntoStack(InsnList instructions, BindingContext bindingContext) { + MethodProcessor methodProcessor = bindingContext.getMethodProcessor(); + MethodNode methodNode = methodProcessor.getMethodNode(); + AsmOpUtils.ldc(instructions, methodNode.name + methodNode.desc); + } + + @Override + public Type getType(BindingContext bindingContext) { + return Type.getType(String.class); + } +} diff --git a/core/src/main/java/com/taobao/arthas/core/advisor/Enhancer.java b/core/src/main/java/com/taobao/arthas/core/advisor/Enhancer.java index 4675e5cff..ee25164d3 100644 --- a/core/src/main/java/com/taobao/arthas/core/advisor/Enhancer.java +++ b/core/src/main/java/com/taobao/arthas/core/advisor/Enhancer.java @@ -92,23 +92,21 @@ public class Enhancer implements ClassFileTransformer { @AtEnter(inline = true) public static void atEnter(@Binding.This Object target, @Binding.Class Class clazz, - @Binding.MethodName String methodName, @Binding.MethodDesc String methodDesc, - @Binding.Args Object[] args) { - SpyAPI.atEnter(clazz, methodName, methodDesc, target, args); + @Binding.MethodInfo String methodInfo, @Binding.Args Object[] args) { + SpyAPI.atEnter(clazz, methodInfo, target, args); } @AtExit(inline = true) public static void atExit(@Binding.This Object target, @Binding.Class Class clazz, - @Binding.MethodName String methodName, @Binding.MethodDesc String methodDesc, - @Binding.Args Object[] args, @Binding.Return Object returnObj) { - SpyAPI.atExit(clazz, methodName, methodDesc, target, args, returnObj); + @Binding.MethodInfo String methodInfo, @Binding.Args Object[] args, @Binding.Return Object returnObj) { + SpyAPI.atExit(clazz, methodInfo, target, args, returnObj); } @AtExceptionExit(inline = true) public static void atExceptionExit(@Binding.This Object target, @Binding.Class Class clazz, - @Binding.MethodName String methodName, @Binding.MethodDesc String methodDesc, - @Binding.Args Object[] args, @Binding.Throwable Throwable throwable) { - SpyAPI.atExceptionExit(clazz, methodName, methodDesc, target, args, throwable); + @Binding.MethodInfo String methodInfo, @Binding.Args Object[] args, + @Binding.Throwable Throwable throwable) { + SpyAPI.atExceptionExit(clazz, methodInfo, target, args, throwable); } } diff --git a/spy/src/main/java/java/arthas/SpyAPI.java b/spy/src/main/java/java/arthas/SpyAPI.java index 67b92936f..67d73adbd 100644 --- a/spy/src/main/java/java/arthas/SpyAPI.java +++ b/spy/src/main/java/java/arthas/SpyAPI.java @@ -32,18 +32,18 @@ public class SpyAPI { spyInstance = spy; } - public static void atEnter(Class clazz, String methodName, String methodDesc, Object target, Object[] args) { - spyInstance.atEnter(clazz, methodName, methodDesc, target, args); + public static void atEnter(Class clazz, String methodInfo, Object target, Object[] args) { + spyInstance.atEnter(clazz, methodInfo, target, args); } - public static void atExit(Class clazz, String methodName, String methodDesc, Object target, Object[] args, + public static void atExit(Class clazz, String methodInfo, Object target, Object[] args, Object returnObject) { - spyInstance.atExit(clazz, methodName, methodDesc, target, args, returnObject); + spyInstance.atExit(clazz, methodInfo, target, args, returnObject); } - public static void atExceptionExit(Class clazz, String methodName, String methodDesc, Object target, + public static void atExceptionExit(Class clazz, String methodInfo, Object target, Object[] args, Throwable throwable) { - spyInstance.atExceptionExit(clazz, methodName, methodDesc, target, args, throwable); + spyInstance.atExceptionExit(clazz, methodInfo, target, args, throwable); } public static void atBeforeInvoke(Class clazz, String invokeInfo, Object target) { @@ -59,13 +59,13 @@ public class SpyAPI { } public static abstract class AbstractSpy { - public abstract void atEnter(Class clazz, String methodName, String methodDesc, Object target, + public abstract void atEnter(Class clazz, String methodInfo, Object target, Object[] args); - public abstract void atExit(Class clazz, String methodName, String methodDesc, Object target, Object[] args, + public abstract void atExit(Class clazz, String methodInfo, Object target, Object[] args, Object returnObject); - public abstract void atExceptionExit(Class clazz, String methodName, String methodDesc, Object target, + public abstract void atExceptionExit(Class clazz, String methodInfo, Object target, Object[] args, Throwable throwable); public abstract void atBeforeInvoke(Class clazz, String invokeInfo, Object target); @@ -78,16 +78,16 @@ public class SpyAPI { static class NopSpy extends AbstractSpy { @Override - public void atEnter(Class clazz, String methodName, String methodDesc, Object target, Object[] args) { + public void atEnter(Class clazz, String methodInfo, Object target, Object[] args) { } @Override - public void atExit(Class clazz, String methodName, String methodDesc, Object target, Object[] args, + public void atExit(Class clazz, String methodInfo, Object target, Object[] args, Object returnObject) { } @Override - public void atExceptionExit(Class clazz, String methodName, String methodDesc, Object target, Object[] args, + public void atExceptionExit(Class clazz, String methodInfo, Object target, Object[] args, Throwable throwable) { }