JVMJavajava~知识

Arthas - Java诊断神器

2019-03-06  本文已影响128人  十毛tenmao

Arthas 是Alibaba开源的Java诊断工具,深受开发者喜爱。

当你遇到以下类似问题而束手无策时,Arthas可以帮助你解决:

启动图

Arthas

快速使用

Arthas无需安装,下载就可以直接使用

wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar

常用场景

sc -d {className} 支持通配符,从code-source可以看出来类从哪个jar包加载的

sc -d *MathGame
$ sc -d *MathGame
 class-info        demo.MathGame
 code-source       /home/scrapbook/tutorial/arthas-demo.jar
 name              demo.MathGame
 isInterface       false
 isAnnotation      false
 isEnum            false
 isAnonymousClass  false
 isArray           false
 isLocalClass      false
 isMemberClass     false
 isPrimitive       false
 isSynthetic       false
 simple-name       MathGame
 modifier          public
 annotation
 interfaces
 super-class       +-java.lang.Object
 class-loader      +-sun.misc.Launcher$AppClassLoader@55f96302
                     +-sun.misc.Launcher$ExtClassLoader@378df983
 classLoaderHash   55f96302
jad demo.MathGame
$ jad demo.MathGame

ClassLoader:
+-sun.misc.Launcher$AppClassLoader@55f96302
  +-sun.misc.Launcher$ExtClassLoader@378df983

Location:
/home/scrapbook/tutorial/arthas-demo.jar

/*
 * Decompiled with CFR 0_132.
 */
package demo;

import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.concurrent.TimeUnit;

public class MathGame {
    private static Random random = new Random();
    public int illegalArgumentCount = 0;

    public List<Integer> primeFactors(int number) {
        if (number < 2) {
            ++this.illegalArgumentCount;
            throw new IllegalArgumentException("number is: " + number + ", need >= 2");
        }
        ArrayList<Integer> result = new ArrayList<Integer>();
        int i = 2;
        while (i <= number) {
            if (number % i == 0) {
watch demo.MathGame primeFactors "{params, target, returnObj, throwExp}" -x 2 -n 2
$ watch demo.MathGame primeFactors "{params, target, returnObj, throwExp}" -x 2 -n 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 75 ms.
ts=2019-03-06 14:09:40; [cost=0.284671ms] result=@ArrayList[
    @Object[][
        @Integer[51463],
    ],
    @MathGame[
        random=@Random[java.util.Random@1a6c5a9e],
        illegalArgumentCount=@Integer[1230],
    ],
    @ArrayList[
        @Integer[53],
        @Integer[971],
    ],
    null,
]
ts=2019-03-06 14:09:41; [cost=0.09573ms] result=@ArrayList[
    @Object[][
        @Integer[133515],
    ],
    @MathGame[
        random=@Random[java.util.Random@1a6c5a9e],
        illegalArgumentCount=@Integer[1230],
    ],
    @ArrayList[
        @Integer[3],
        @Integer[3],
        @Integer[3],
        @Integer[5],
        @Integer[23],
        @Integer[43],
    ],
    null,
]

方法同上,不过可以增加参数过滤,针对特定用户查看调用情况

watch demo.MathGame primeFactors "{params, target, returnObj, throwExp}" "params[0] < 0" -x 2 -n 2

其中params[0]<0就是过滤条件

$ watch demo.MathGame primeFactors "{params, target, returnObj, throwExp}" "params[0] < 0" -x 2 -n 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 101 ms.
ts=2019-03-06 14:14:24; [cost=0.211055ms] result=@ArrayList[
    @Object[][
        @Integer[-212657],
    ],
    @MathGame[
        random=@Random[java.util.Random@1a6c5a9e],
        illegalArgumentCount=@Integer[1377],
    ],
    null,
    java.lang.IllegalArgumentException: number is: -212657, need >= 2
        at demo.MathGame.primeFactors(MathGame.java:46)
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(MathGame.java:16)
,
]
ts=2019-03-06 14:14:25; [cost=0.111067ms] result=@ArrayList[
    @Object[][
        @Integer[-197020],
    ],
    @MathGame[
        random=@Random[java.util.Random@1a6c5a9e],
        illegalArgumentCount=@Integer[1378],
    ],
    null,
    java.lang.IllegalArgumentException: number is: -197020, need >= 2
        at demo.MathGame.primeFactors(MathGame.java:46)
        at demo.MathGame.run(MathGame.java:24)
        at demo.MathGame.main(MathGame.java:16)
,
]

基本命令

其他高级用法

watch

watch com.example.demo.arthas.user.UserController * "{params[0],throwExp}" -e
watch com.example.demo.arthas.user.UserController * '{params, returnObj}' '#cost>200'

参考

上一篇 下一篇

猜你喜欢

热点阅读