执行命令 : options json-format true
执行命令: options save-result true
执行命令: options dump true
无论是匹配表达式也好、观察表达式也罢,他们核心判断变量都是围绕着一个 Arthas 中的通用通知对象 Advice 进行。
它的简略代码结构如下
public class Advice { private final ClassLoader loader; private final Class<?> clazz; private final ArthasMethod method; private final Object target; private final Object[] params; private final Object returnObj; private final Throwable throwExp; private final boolean isBefore; private final boolean isThrow; private final boolean isReturn; // getter/setter }所有变量都可以在表达式中直接使用,如果在表达式中编写了不符合 OGNL 脚本语法或者引入了不在表格中的变量,则退出命令的执行;用户可以根据当前的异常信息修正条件表达式或观察表达式
help——查看命令帮助信息
cls——清空当前屏幕区域
session——查看当前会话的信息
reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
version——输出当前目标 Java 进程所加载的 Arthas 版本号
history——打印命令历史
quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
stop——关闭 Arthas 服务端,所有 Arthas 客户端全部退出
keymap——Arthas快捷键列表及自定义快捷键
“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息 。
USAGE: sc [-c <value>] [-d] [-x <value>] [-f] [-h] [-n <value>] [-E] class-pattern SUMMARY: Search all the classes loaded by JVM EXAMPLES: sc *test.demo.* sc -d org.apache.commons.lang.StringUtils sc -d org/apache/commons/lang/StringUtils sc -d *StringUtils sc -d -f org.apache.commons.lang.StringUtils sc -E org\\.apache\\.commons\\.lang\\.StringUtils OPTIONS: -c, --classloader <value> The hash code of the special class's classLoader -d, --details Display the details of class -x, --expand <value> Expand level of object (0 by default) -f, --field Display all the member variables -h, --help this help -n, --limits <value> Maximum number of matching classes with details (100 by default) -E, --regex Enable regular expression to match (wildcard matching by default) <class-pattern> Class name pattern, use either '.' or '/' as separator“Search-Method” 的简写,这个命令能搜索出所有已经加载了 Class 信息的方法信息。 sm 命令只能看到由当前类所声明 (declaring) 的方法,父类则无法看到。
USAGE: sm [-c <value>] [-d] [-h] [-n <value>] [-E] class-pattern [method-pattern] EXAMPLES: sm java.lang.String sm -d org.apache.commons.lang.StringUtils sm -d org.apache.commons.lang.StringUtils isEmpty sm -d org/apache/commons/lang/StringUtils sm *StringUtils * sm -Ed org\\.apache\\.commons\\.lang\.StringUtils .* WIKI: https://arthas.aliyun.com/doc/sm OPTIONS: -c, --classloader <value> The hash code of the special class's classLoader -d, --details Display the details of method -h, --help this help -n, --limits <value> Maximum number of matching classes (100 by default) -E, --regex Enable regular expression to match (wildcard matching by default) <class-pattern> Class name pattern, use either '.' or '/' as separator <method-pattern> Method name patternwatch命令是用来查看某个类的某个方法的运行情况,包括入参,出参,异常等。
USAGE: watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [--listenerId <value>] [-E] [-M <value>] [-s] [-v] class-pattern method-pattern [express] [condition-express] SUMMARY: Display the input/output parameter, return object, and thrown exception of specified method invocation The express may be one of the following expression (evaluated dynamically): target : the object clazz : the object's class method : the constructor or method params : the parameters array of method params[0..n] : the element of parameters array returnObj : the returned object of method throwExp : the throw exception of method isReturn : the method ended by return isThrow : the method ended by throwing exception #cost : the execution time in ms of method invocation Examples: watch -b org.apache.commons.lang.StringUtils isBlank params watch -f org.apache.commons.lang.StringUtils isBlank returnObj watch org.apache.commons.lang.StringUtils isBlank '{params, target, returnObj}' -x 2 watch -bf *StringUtils isBlank params watch *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] params[0].length==1 watch *StringUtils isBlank params '#cost>100' watch -E -b org\.apache\.commons\.lang\.StringUtils isBlank params[0] OPTIONS: -b, --before Watch before invocation -e, --exception Watch after throw exception -x, --expand <value> Expand level of object (1 by default) -f, --finish Watch after invocation, enable by default -h, --help this help -n, --limits <value> Threshold of execution times --listenerId <value> The special listenerId -E, --regex Enable regular expression to match (wildcard matching by default) -M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1024 by default) -s, --success Watch after successful invocation -v, --verbose Enables print verbose information, default value false. <class-pattern> The full qualified class name you want to watch <method-pattern> The method name you want to watch <express> the content you want to watch, written by ognl. Examples: params params[0] 'params[0]+params[1]' '{params[0], target, returnObj}' returnObj throwExp target clazz method <condition-express> Conditional expression in ognl style, for example: TRUE : 1==1 TRUE : true FALSE : false TRUE : 'params.length>=0' FALSE : 1==2方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,异常,并能对这些不同的时间下调用进行观测 。
USAGE: tt [-d] [--delete-all] [-x <value>] [-h] [-i <value>] [-n <value>] [-l] [--listenerId <value>] [-p] [-E] [--replay-interval <value>] [--replay-times <value>] [-s <value>] [-M <value>] [-t] [-v] [-w <valu e>] [class-pattern] [method-pattern] [condition-express] SUMMARY: Time Tunnel The express may be one of the following expression (evaluated dynamically): target : the object clazz : the object's class method : the constructor or method params : the parameters array of method params[0..n] : the element of parameters array returnObj : the returned object of method throwExp : the throw exception of method isReturn : the method ended by return isThrow : the method ended by throwing exception #cost : the execution time in ms of method invocation EXAMPLES: tt -t *StringUtils isEmpty tt -t *StringUtils isEmpty params[0].length==1 tt -l tt -i 1000 tt -i 1000 -w params[0] tt -i 1000 -p tt -i 1000 -p --replay-times 3 --replay-interval 3000 tt -s '{params[0] > 1}' -w '{params}' tt --delete-all OPTIONS: -d, --delete Delete time fragment specified by index --delete-all Delete all the time fragments -x, --expand <value> Expand level of object (1 by default) -h, --help this help -i, --index <value> Display the detailed information from specified time fragment -n, --limits <value> Threshold of execution times -l, --list List all the time fragments --listenerId <value> The special listenerId -p, --play Replay the time fragment specified by index -E, --regex Enable regular expression to match (wildcard matching by default) --replay-interval <value> replay interval for play tt with option r greater than 1 --replay-times <value> execution times when play tt -s, --search-express <value> Search-expression, to search the time fragments by ognl express. The structure of 'advice' like conditional expression -M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1024 by default) -t, --time-tunnel Record the method invocation within time fragments -v, --verbose Enables print verbose information, default value false. -w, --watch-express <value> watch the time fragment by ognl express. Examples: params params[0] 'params[0]+params[1]' '{params[0], target, returnObj}' returnObj throwExp target clazz method <class-pattern> Path and classname of Pattern Matching <method-pattern> Method of Pattern Matching <condition-express> Conditional expression in ognl style, for example: TRUE : 1==1 TRUE : true FALSE : false TRUE : 'params.length>=0' FALSE : 1==2记录方法的每一次调用
tt -t demo.TstDemo prime一个调用量不高的方法时可以使用该命令,但如果遇到调用量非常大的方法,瞬间就能将JVM 内存撑爆记录方法的调用指定记录次数tt -n 3 demo.TstDemo prime-n 参数指定你需要记录的次数,当达到记录次数时 Arthas 会主动中断tt命令的记录过程检索调用记录
tt -l 根据被调用的方法名称检索记录tt -s 'method.name=="xxxxx"'xxx是方法的名称查看某一次的调用信息tt -i INDEXINDEX表示记录的INDEX列的值查看某一次的调用信息并输出到指定的文件中tt -i INDEX >> /home/hik/log.log使用 >> 可以追加输出到某一个文件。使用 > 可以清空文档之后写入数据记录方法调用情况
查询记录
产看抹一次记录并输出
classloader 命令将 JVM 中所有的classloader的信息统计出来,并可以展示继承树,urls等。
USAGE: classloader [-a] [-c <value>] [-h] [-i] [-l] [--load <value>] [-r <value>] [-t] EXAMPLES: classloader classloader -t classloader -l classloader -c 327a647b classloader -c 327a647b -r META-INF/MANIFEST.MF classloader -a classloader -a -c 327a647b classloader -c 659e0bfd --load demo.MathGame OPTIONS: -a, --all Display all classes loaded by ClassLoader -c, --classloader <value> The hash code of the special ClassLoader -h, --help this help -i, --include-reflection-classloader Include sun.reflect.DelegatingClassLoader -l, --list-classloader Display statistics info by classloader instance --load <value> Use ClassLoader to load class, won't work without -c specified -r, --resource <value> Use ClassLoader to find resources, won't work without -c specified -t, --tree Display ClassLoader tree 要求描述命令备注按类加载实例查看统计信息 classloader -l 按类加载类型查看统计信息 classloader 查看URLClassLoader实际的urlsclassloader -c [hashCode]hashcode的值可以使用 classloader -l获取查找类的class文件 classloader -c [hashCode] -r java/lang/String.classhashcode的值可以使用 classloader -l获取使用ClassLoader去加载类 classloader -c [hashCode] --load [类路径]hashcode的值可以使用 classloader -l获取
主要作用是加载外部的.class文件,用来替换JVM已经加载的类。实现了Java的热更新。
redefine的class不能修改、添加、删除类的field和method,包括方法参数、方法名称及返回值。除此之外不能替换正在运行的方法(类似无限自循环的的方法)参考:
https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/Instrumentation.html#redefineClasses-java.lang.instrument.ClassDefinition...-
注意:
reset命令对redefine的类无效。如果想重置,需要redefine原始的字节码。
redefine命令和jad/watch/trace/monitor/tt等命令会冲突。执行完redefine之后,如果再执行上面提到的命令,则会把redefine的字节码重置。 原因是jdk本身redefine和Retransform是不同的机制,同时使用两种机制来更新字节码,只有最后修改的会生效。
USAGE: redefine [-c <value>] [-h] classfilePaths... EXAMPLES: redefine /tmp/Test.class redefine -c 327a647b /tmp/Test.class /tmp/Test\$Inner.class WIKI: https://arthas.aliyun.com/doc/redefine OPTIONS: -c, --classloader <value> classLoader hashcode -h, --help this help <classfilePaths> .class file paths,支持多个 #redefine jvm已加载的类 redefine /tmp/Test.class #redefine jvm已加载的类(多个),如果是内部类类(带$)注意写法---指定的classloader的hashCode(classloader命令可查看) redefine -c 327a647b /tmp/Test.class /tmp/Test\$Inner.class #redefine jvm已加载的类(多个),如果是内部类类(带$)注意写法---指定的classloader(classloader命令可查看) redefine --classLoaderClass sun.misc.Launcher$AppClassLoader /tmp/Test.class /tmp/Test\$Inner.classtrhead
thread -n [number]
thread [id]
thread –state [status]
线程状态有 RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, NEW, TERMINATED
有时候我们发现应用卡住了, 通常是由于某个线程拿住了某个锁, 并且其他线程都在等待这把锁造成的。 为了排查这类问题, arthas提供了thread -b, 一键找出那个罪魁祸首。
注意, 目前只支持找出synchronized关键字阻塞住的线程, 如果是java.util.concurrent.Lock, 目前还不支持。
jad 命令将 JVM 中实际运行的 class 的 byte code 反编译成 java 代码。
USAGE: jad [-c <value>] [-h] [--hideUnicode] [-E] [--source-only] class-pattern [method-name] EXAMPLES: jad java.lang.String jad java.lang.String toString jad --source-only java.lang.String jad -c 39eb305e org/apache/log4j/Logger jad -c 39eb305e -E org\\.apache\\.*\\.StringUtils WIKI: https://arthas.aliyun.com/doc/jad OPTIONS: -c, --code <value> The hash code of the special class's classLoader -h, --help this help --hideUnicode hide unicode, default value false -E, --regex Enable regular expression to match (wildcard matching by default) --source-only Output source code only <class-pattern> Class name pattern, use either '.' or '/' as separator <method-name> method name pattern, decompile a specific method instead of the whole class 默认情况下,反编译结果里会带有ClassLoader信息,通过--source-only选项,可以只打印源代码。当有多个 ClassLoader 都加载了这个类时,jad 命令会输出对应 ClassLoader 实例的 hashcode,然后你只需要重新执行 jad 命令,并使用参数 -c <hashcode> 就可以反编译指定 ClassLoader 加载的那个类了。