我是靠谱客的博主 怡然大叔,这篇文章主要介绍Arthas常见配置和命令常见配置表达式核心变量常见命令,现在分享给大家,希望可以做个参考。

常见配置

全局开关

名称默认值描述
unsafefalse是否支持对系统级别的类进行增强,打开该开关可能导致把JVM搞挂,请慎重选择!
dumpfalse是否支持被增强了的类dump到外部文件中,如果打开开关,class文件会被dump到/${application working dir}/arthas-class-dump/目录下,具体位置详见控制台输出
batch-re-transformtrue是否支持批量对匹配到的类执行retransform操作
json-formatfalse是否支持json化的输出
disable-sub-classfalse是否禁用子类匹配,默认在匹配目标类的时候会默认匹配到其子类,如果想精确匹配,可以关闭此开关
support-default-methodtrue是否支持匹配到default method,默认会查找interface,匹配里面的default method。参考 #1105
save-resultfalse是否打开执行结果存日志功能,打开之后所有命令的运行结果都将保存到~/logs/arthas-cache/result.log
job-timeout1d异步后台任务的默认超时时间,超过这个时间,任务自动停止;比如设置 1d, 2h, 3m, 25s,分别代表天、小时、分、秒
print-parent-fieldstrue是否打印在parent class里的filed

1. 结果、参数以json的方式输出

 执行命令 : options json-format true

2. 开启执行结果存日志功能(默认是关闭的)

执行命令: options save-result true

3. 把监控的类dum出class文件,然后通过反编译文件进行查看

执行命令: options dump true   

 

表达式核心变量

无论是匹配表达式也好、观察表达式也罢,他们核心判断变量都是围绕着一个 Arthas 中的通用通知对象 Advice 进行。

它的简略代码结构如下

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }

变量的含义解释

变量名变量解释
loader本次调用类所在的 ClassLoader
clazz本次调用类的 Class 引用
method本次调用方法反射引用
target本次调用类的实例
params本次调用参数列表,这是一个数组,如果方法是无参方法则为空数组
returnObj本次调用返回的对象。当且仅当 isReturn==true 成立时候有效,表明方法调用是以正常返回的方式结束。如果当前方法无返回值 void,则值为 null
throwExp本次调用抛出的异常。当且仅当 isThrow==true 成立时有效,表明方法调用是以抛出异常的方式结束。
isBefore辅助判断标记,当前的通知节点有可能是在方法一开始就通知,此时 isBefore==true 成立,同时 isThrow==false 和 isReturn==false,因为在方法刚开始时,还无法确定方法调用将会如何结束。
isThrow辅助判断标记,当前的方法调用以抛异常的形式结束。
isReturn辅助判断标记,当前的方法调用以正常返回的形式结束。

所有变量都可以在表达式中直接使用,如果在表达式中编写了不符合 OGNL 脚本语法或者引入了不在表格中的变量,则退出命令的执行;用户可以根据当前的异常信息修正条件表达式观察表达式

常见命令

Arthas 基础命令

  • help——查看命令帮助信息

  • cls——清空当前屏幕区域

  • session——查看当前会话的信息

  • reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类

  • version——输出当前目标 Java 进程所加载的 Arthas 版本号

  • history——打印命令历史

  • quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响

  • stop——关闭 Arthas 服务端,所有 Arthas 客户端全部退出

  • keymap——Arthas快捷键列表及自定义快捷键

其他命令

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
help Display Arthas Help keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information perfcounter Display the perf counter infornation. ognl Execute ognl expression. mc Memory compiler, compiles java files into bytecode and class files in memory. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM heapdump Heap dump options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version session Display current session information sysprop Display, and change the system properties. sysenv Display the system env. vmoption Display, and update the vm diagnostic options. logger Print logger info, and update the logger level history Display command history cat Concatenate and print files echo write arguments to the standard output pwd Return working directory name mbean Display the mbean information grep grep command for pipes. tee tee command for pipes. profiler Async Profiler. https://github.com/jvm-profiling-tools/async-profiler stop Stop/Shutdown Arthas server and exit the console.

 条件表达式

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Arthas 似乎很难区分出重载的方法 我只需要观察特定参数,但是 tt 却全部都给我记录了下来 条件表达式也是用 OGNL 来编写,核心的判断对象依然是 Advice 对象。除了 tt 命令之外,watch、trace、stack 命令也都支持条件表达式。 解决方法重载 tt -t *Test print params.length==1 通过制定参数个数的形式解决不同的方法签名,如果参数个数一样,你还可以这样写 tt -t *Test print 'params[1] instanceof Integer' 解决指定参数 tt -t *Test print params[0].mobile=="13989838402" 构成条件表达式的 Advice 对象 特殊用法请参考:https://github.com/alibaba/arthas/issues/71 OGNL表达式官网:https://commons.apache.org/proper/commons-ognl/language-guide.html

1.sc 命令

“Search-Class” 的简写,这个命令能搜索出所有已经加载到 JVM 中的 Class 信息 。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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

部分命令介绍 

要求描述命令
模糊搜索sc demo.*
打印类的详细信息sc -d demo.MathGame
打印出类的Field信息sc -d -f demo.MathGame

2. sm命令

“Search-Method” 的简写,这个命令能搜索出所有已经加载了 Class 信息的方法信息。
sm 命令只能看到由当前类所声明 (declaring) 的方法,父类则无法看到。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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 pattern

3. watch 命令

watch命令是用来查看某个类的某个方法的运行情况,包括入参,出参,异常等。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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

部分命令介绍 

要求描述命令
查看某个类的方法的返回参数watch [类的路径-package路径+类名] [方法名] returnObj
查看某个类的方法的传入参数watch [类的路径-package路径+类名] [方法名] params
查看某个类的方法的传入参数 并输出到文件watch [类的路径-package路径+类名] [方法名] params >> /home/aaa/log.log
查看某个类的方法的传入参数(不只查看100行数据)watch [类的路径-package路径+类名] [方法名] params -n 300000
同时异常(-e)时的监控入参,返回值watch [类的路径-package路径+类名] [方法名] "{params, returnObj, throwExp}" -e -x 2  

4. tt 命令

方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,异常,并能对这些不同的时间下调用进行观测 。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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使用 >> 可以追加输出到某一个文件。使用 > 可以清空文档之后写入数据

记录方法调用情况

 查询记录

 产看抹一次记录并输出

5. ognl

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
USAGE: ognl [-c <value>] [--classLoaderClass <value>] [-x <value>] [-h] express SUMMARY: Execute ognl expression. EXAMPLES: ognl '@java.lang.System@out.println("hello")' ognl -x 2 '@Singleton@getInstance()' ognl '@Demo@staticFiled' ognl '#value1=@System@getProperty("java.home"), #value2=@System@getProperty("java.runtime.name"), {#value1, #value2}' ognl -c 5d113a51 '@com.taobao.arthas.core.GlobalOptions@isDump' WIKI: https://arthas.aliyun.com/doc/ognl https://commons.apache.org/proper/commons-ognl/language-guide.html OPTIONS: -c, --classLoader <value> The hash code of the special class's classLoader, default classLoader is SystemClassLoader. --classLoaderClass <value> The class name of the special class's classLoader. -x, --expand <value> Expand level of object (1 by default). -h, --help this help <express> The ognl expression.

6. classloader

   classloader 命令将 JVM 中所有的classloader的信息统计出来,并可以展示继承树,urls等。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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获取

 

7. redefine

        主要作用是加载外部的.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是不同的机制,同时使用两种机制来更新字节码,只有最后修改的会生效。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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,支持多个
复制代码
1
2
3
4
5
6
7
#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.class

8. thread 命令 --- 查看当前 JVM 的线程堆栈信息

参数名称参数说明
id线程id
[n:]指定最忙的前N个线程并打印堆栈
[b]找出当前阻塞其他线程的线程
[i <value>]指定cpu占比统计的采样间隔,单位为毫秒

1) 当没有参数时,显示所有线程的信息

trhead

2) 展示当前最忙的前N个线程并打印堆栈

thread -n [number]

3) 显示指定线程的运行堆栈

thread [id]

4) 查看指定状态的线程

thread –state [status]

线程状态有  RUNNABLE, BLOCKED, WAITING, TIMED_WAITING, NEW, TERMINATED

5) 找出当前阻塞其他线程的线程

thread -b

有时候我们发现应用卡住了, 通常是由于某个线程拿住了某个锁, 并且其他线程都在等待这把锁造成的。 为了排查这类问题, arthas提供了thread -b, 一键找出那个罪魁祸首。

注意, 目前只支持找出synchronized关键字阻塞住的线程, 如果是java.util.concurrent.Lock, 目前还不支持。

9. jad命令

jad 命令将 JVM 中实际运行的 class 的 byte code 反编译成 java 代码。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 加载的那个类了。

最后

以上就是怡然大叔最近收集整理的关于Arthas常见配置和命令常见配置表达式核心变量常见命令的全部内容,更多相关Arthas常见配置和命令常见配置表达式核心变量常见命令内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(88)

评论列表共有 0 条评论

立即
投稿
返回
顶部