From 41bbedefdbc052f78010551cb1ba6b7a87786f43 Mon Sep 17 00:00:00 2001 From: hengyunabc Date: Mon, 1 Jun 2020 19:23:23 +0800 Subject: [PATCH] EnhancerAffect save exception and print the exception message #1224 --- .../com/taobao/arthas/core/advisor/Enhancer.java | 9 ++++++--- .../core/command/monitor200/EnhancerCommand.java | 6 ++++++ .../arthas/core/util/affect/EnhancerAffect.java | 15 +++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) 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 55a1400cd..1e0e7af38 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 @@ -319,6 +319,7 @@ public class Enhancer implements ClassFileTransformer { return enhanceClassByteArray; } catch (Throwable t) { logger.warn("transform loader[{}]:class[{}] failed.", inClassLoader, className, t); + affect.setThrowable(t); } return null; @@ -426,11 +427,12 @@ public class Enhancer implements ClassFileTransformer { // 过滤掉无法被增强的类 filter(matchingClasses); + logger.info("enhance matched classes: {}", matchingClasses); + affect.setTransformer(this); try { ArthasBootstrap.getInstance().getTransformerManager().addTransformer(this, isTracing); - //inst.addTransformer(enhancer, true); // 批量增强 if (GlobalOptions.isBatchReTransform) { @@ -459,8 +461,9 @@ public class Enhancer implements ClassFileTransformer { } } } - } finally { - //inst.removeTransformer(enhancer); + } catch (Throwable e) { + logger.error("Enhancer error, matchingClasses: {}", matchingClasses, e); + affect.setThrowable(e); } return affect; diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/EnhancerCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/EnhancerCommand.java index 2b88f69de..6fb9bace2 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/EnhancerCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/EnhancerCommand.java @@ -134,6 +134,12 @@ public abstract class EnhancerCommand extends AnnotatedCommand { process.register(listener, enhancer); EnhancerAffect effect = enhancer.enhance(inst); + if (effect.getThrowable() != null) { + process.write(effect.toString() + ", check arthas log: " + LogUtil.loggingFile() + "\n"); + process.end(1); + return; + } + if (effect.cCnt() == 0 || effect.mCnt() == 0) { // no class effected // might be method code too large diff --git a/core/src/main/java/com/taobao/arthas/core/util/affect/EnhancerAffect.java b/core/src/main/java/com/taobao/arthas/core/util/affect/EnhancerAffect.java index 4189e708e..e67997d2b 100644 --- a/core/src/main/java/com/taobao/arthas/core/util/affect/EnhancerAffect.java +++ b/core/src/main/java/com/taobao/arthas/core/util/affect/EnhancerAffect.java @@ -16,6 +16,7 @@ import static java.lang.String.format; * 增强影响范围
* 统计影响类/方法/耗时 * Created by vlinux on 15/5/19. + * @author hengyunabc 2020-06-01 */ public final class EnhancerAffect extends Affect { @@ -23,6 +24,9 @@ public final class EnhancerAffect extends Affect { private final AtomicInteger mCnt = new AtomicInteger(); private ClassFileTransformer transformer; private long listenerId; + + private Throwable throwable; + /** * dumpClass的文件存放集合 */ @@ -101,6 +105,14 @@ public final class EnhancerAffect extends Affect { this.listenerId = listenerId; } + public Throwable getThrowable() { + return throwable; + } + + public void setThrowable(Throwable throwable) { + this.throwable = throwable; + } + @Override public String toString() { final StringBuilder infoSB = new StringBuilder(); @@ -122,6 +134,9 @@ public final class EnhancerAffect extends Affect { mCnt(), cost(), listenerId)); + if (this.throwable != null) { + infoSB.append("\nEnhance error! exception: " + this.throwable); + } return infoSB.toString(); }