diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractMethodBasedCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractMethodBasedCommand.java new file mode 100644 index 0000000000..5a3bcdfac3 --- /dev/null +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/AbstractMethodBasedCommand.java @@ -0,0 +1,44 @@ +package com.taobao.arthas.core.command.monitor200; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import com.taobao.arthas.core.shell.command.CommandProcess; +import com.taobao.middleware.cli.annotations.Argument; +import com.taobao.middleware.cli.annotations.Description; + +/** + * 基于方法命令抽像类. + */ +public abstract class AbstractMethodBasedCommand extends EnhancerCommand { + + + protected String classPattern; + protected String methodPattern; + + @Argument(argName = "class-pattern", index = 0) + @Description("Path and classname of Pattern Matching") + public void setClassPattern(final String classPattern) { + this.classPattern = classPattern; + } + + @Argument(argName = "method-pattern", index = 1) + @Description("Method of Pattern Matching, ~ means take from class-pattern com.a.C:method or com.a.C.method") + public void setMethodPattern(final String methodPattern) { + this.methodPattern = methodPattern; + } + + @Override + protected void enhance(final CommandProcess process) { + if ("=".equals(methodPattern) || "~".equals(methodPattern) || ".".equals(methodPattern)) { //smart way + final Pattern smartPattern = Pattern.compile("([.][A-Z][^.]*)([.: ]+)([a-z][^. ]*)$"); + final Matcher matcher = smartPattern.matcher(classPattern); + if (matcher.find()) { + methodPattern = matcher.group(3); + classPattern = matcher.replaceAll("$1"); + process.echoTips("smart way input ==> " + classPattern + " " + methodPattern + "\n"); + } + } + super.enhance(process); + } +} diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/MonitorCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/MonitorCommand.java index cef8f2e659..4f02ddd06f 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/MonitorCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/MonitorCommand.java @@ -22,33 +22,21 @@ @Summary("Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. ") @Description("\nExamples:\n" + " monitor org.apache.commons.lang.StringUtils isBlank\n" + + " monitor org.apache.commons.lang.StringUtils.isBlank =\n" + + " monitor org.apache.commons.lang.StringUtils:isBlank =\n" + " monitor org.apache.commons.lang.StringUtils isBlank -c 5\n" + " monitor org.apache.commons.lang.StringUtils isBlank params[0]!=null\n" + " monitor -b org.apache.commons.lang.StringUtils isBlank params[0]!=null\n" + " monitor -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank\n" + Constants.WIKI + Constants.WIKI_HOME + "monitor") -public class MonitorCommand extends EnhancerCommand { +public class MonitorCommand extends AbstractMethodBasedCommand { - private String classPattern; - private String methodPattern; private String conditionExpress; private int cycle = 60; private boolean isRegEx = false; private int numberOfLimit = 100; private boolean isBefore = false; - @Argument(argName = "class-pattern", index = 0) - @Description("Path and classname of Pattern Matching") - public void setClassPattern(String classPattern) { - this.classPattern = classPattern; - } - - @Argument(argName = "method-pattern", index = 1) - @Description("Method of Pattern Matching") - public void setMethodPattern(String methodPattern) { - this.methodPattern = methodPattern; - } - @Argument(argName = "condition-express", index = 2, required = false) @Description(Constants.CONDITION_EXPRESS) public void setConditionExpress(String conditionExpress) { diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackCommand.java index 985ae5e6c4..8fbada782d 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/StackCommand.java @@ -23,14 +23,15 @@ @Summary("Display the stack trace for the specified class and method") @Description(Constants.EXPRESS_DESCRIPTION + Constants.EXAMPLE + " stack org.apache.commons.lang.StringUtils isBlank\n" + + " stack org.apache.commons.lang.StringUtils.isBlank =\n" + + " stack org.apache.commons.lang.StringUtils:isBlank =\n" + " stack *StringUtils isBlank\n" + " stack *StringUtils isBlank params[0].length==1\n" + " stack *StringUtils isBlank '#cost>100'\n" + " stack -E org\\.apache\\.commons\\.lang\\.StringUtils isBlank\n" + Constants.WIKI + Constants.WIKI_HOME + "stack") -public class StackCommand extends EnhancerCommand { - private String classPattern; - private String methodPattern; +public class StackCommand extends AbstractMethodBasedCommand { + private String conditionExpress; private boolean isRegEx = false; private int numberOfLimit = 100; diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelCommand.java index 700416165a..8c6001613f 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/TimeTunnelCommand.java @@ -40,6 +40,8 @@ @Summary("Time Tunnel") @Description(Constants.EXPRESS_DESCRIPTION + Constants.EXAMPLE + " tt -t *StringUtils isEmpty\n" + + " tt -t *StringUtils.isEmpty ~\n" + + " tt -t *StringUtils:isEmpty ~\n" + " tt -t *StringUtils isEmpty params[0].length==1\n" + " tt -l\n" + " tt -i 1000\n" + @@ -49,7 +51,7 @@ " tt -s '{params[0] > 1}' -w '{params}' \n" + " tt --delete-all\n" + Constants.WIKI + Constants.WIKI_HOME + "tt") -public class TimeTunnelCommand extends EnhancerCommand { +public class TimeTunnelCommand extends AbstractMethodBasedCommand { // 时间隧道(时间碎片的集合) // TODO 并非线程安全? private static final Map timeFragmentMap = new LinkedHashMap(); @@ -57,8 +59,6 @@ public class TimeTunnelCommand extends EnhancerCommand { private static final AtomicInteger sequence = new AtomicInteger(1000); // TimeTunnel the method call private boolean isTimeTunnel = false; - private String classPattern; - private String methodPattern; private String conditionExpress; // list the TimeTunnel private boolean isList = false; diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/TraceCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/TraceCommand.java index 1aaa08c78f..8d9edf97bc 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/TraceCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/TraceCommand.java @@ -31,6 +31,8 @@ @Summary("Trace the execution time of specified method invocation.") @Description(value = Constants.EXPRESS_DESCRIPTION + Constants.EXAMPLE + " trace org.apache.commons.lang.StringUtils isBlank\n" + + " trace org.apache.commons.lang.StringUtils.isBlank =\n" + + " trace org.apache.commons.lang.StringUtils:isBlank =\n" + " trace *StringUtils isBlank\n" + " trace *StringUtils isBlank params[0].length==1\n" + " trace *StringUtils isBlank '#cost>100'\n" + @@ -42,10 +44,8 @@ " trace OuterClass$InnerClass *\n" + Constants.WIKI + Constants.WIKI_HOME + "trace") //@formatter:on -public class TraceCommand extends EnhancerCommand { +public class TraceCommand extends AbstractMethodBasedCommand { - private String classPattern; - private String methodPattern; private String conditionExpress; private boolean isRegEx = false; private int numberOfLimit = 100; diff --git a/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchCommand.java b/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchCommand.java index f0556465fc..9f7f9cf337 100644 --- a/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchCommand.java +++ b/core/src/main/java/com/taobao/arthas/core/command/monitor200/WatchCommand.java @@ -22,6 +22,8 @@ @Summary("Display the input/output parameter, return object, and thrown exception of specified method invocation") @Description(Constants.EXPRESS_DESCRIPTION + "\nExamples:\n" + " watch org.apache.commons.lang.StringUtils isBlank\n" + + " watch org.apache.commons.lang.StringUtils.isBlank =\n" + + " watch org.apache.commons.lang.StringUtils:isBlank =\n" + " watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj, throwExp}' -x 2\n" + " watch *StringUtils isBlank params[0] params[0].length==1\n" + " watch *StringUtils isBlank params '#cost>100'\n" + @@ -31,10 +33,8 @@ " watch javax.servlet.Filter * --exclude-class-pattern com.demo.TestFilter\n" + " watch OuterClass$InnerClass\n" + Constants.WIKI + Constants.WIKI_HOME + "watch") -public class WatchCommand extends EnhancerCommand { +public class WatchCommand extends AbstractMethodBasedCommand { - private String classPattern; - private String methodPattern; private String express; private String conditionExpress; private boolean isBefore = false;