Skip to content

实战Arthas

官方地址

下载arthas-boot.jar

curl -O https://arthas.aliyun.com/arthas-boot.jar

启动arthas

shell
java -jar arthas-boot.jar

输入 1,选择wxserver-1.0.0.jar,再输入回车。Arthas 会 attach 到目标进程上,并输出日志

shell
(base) [root@diyai ~]# java -jar arthas-boot.jar
[INFO] JAVA_HOME: /root/Java/graalvm-jdk-17
[INFO] arthas-boot version: 3.7.2
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 22073 /root/api/wxserver-1.0.0.jar
1
[INFO] Start download arthas from remote server: https://arthas.aliyun.com/download/3.7.2?mirror=aliyun
[INFO] Download arthas success.
[INFO] arthas home: /root/.arthas/lib/3.7.2/arthas
[INFO] Try to attach process 22073
Picked up JAVA_TOOL_OPTIONS:
[INFO] Attach process 22073 success.
[INFO] arthas-client connect 127.0.0.1 3658
  ,---.  ,------. ,--------.,--.  ,--.  ,---.   ,---.
 /  O  \ |  .--. ''--.  .--'|  '--'  | /  O  \ '   .-'
|  .-.  ||  '--'.'   |  |   |  .--.  ||  .-.  |`.  `-.
|  | |  ||  |\  \    |  |   |  |  |  ||  | |  |.-'    |
`--' `--'`--' '--'   `--'   `--'  `--'`--' `--'`-----'

wiki       https://arthas.aliyun.com/doc
tutorials  https://arthas.aliyun.com/doc/arthas-tutorials.html
version    3.7.2
main_class
pid        22073
time       2024-02-11 22:06:12

dashboard

更多

[arthas@22073]$ dashboard
ID       NAME                                                GROUP                     PRIORITY          STATE            %CPU             DELTA_TIME        TIME             INTERRUPTED       DAEMON
46       JFR Periodic Tasks                                  RMI Runtime               5                 TIMED_WAITING    0.0              0.000             0:28.157         false             true
44       DestroyJavaVM                                       main                      5                 RUNNABLE         0.0              0.000             0:4.434          false             false
26       lettuce-timer-3-1                                   main                      5                 TIMED_WAITING    0.0              0.000             0:3.389          false             true
78       RMI TCP Connection(88)-120.244.196.126              RMI Runtime               5                 RUNNABLE         0.0              0.000             0:1.913          false             true
19       Catalina-utility-2                                  main                      1                 TIMED_WAITING    0.0              0.000             0:0.810          false             false
18       Catalina-utility-1                                  main                      1                 WAITING          0.0              0.000             0:0.792          false             false
82       RMI TCP Connection(92)-120.244.196.126              RMI Runtime               5                 RUNNABLE         0.0              0.000             0:0.728          false             true
39       https-jsse-nio-443-Poller                           main                      5                 RUNNABLE         0.0              0.000             0:0.602          false             true
77       JMX server connection timeout 77                    RMI Runtime               5                 TIMED_WAITING    0.0              0.000             0:0.566          false             true
24       Abandoned connection cleanup thread                 main                      5                 TIMED_WAITING    0.0              0.000             0:0.191          false             true
38       https-jsse-nio-443-exec-10                          main                      5                 WAITING          0.0              0.000             0:0.188          false             true
29       https-jsse-nio-443-exec-1                           main                      5                 WAITING          0.0              0.000             0:0.122          false             true
34       https-jsse-nio-443-exec-6                           main                      5                 WAITING          0.0              0.000             0:0.117          false             true
30       https-jsse-nio-443-exec-2                           main                      5                 WAITING          0.0              0.000             0:0.107          false             true
32       https-jsse-nio-443-exec-4                           main                      5                 WAITING          0.0              0.000             0:0.087          false             true
36       https-jsse-nio-443-exec-8                           main                      5                 WAITING          0.0              0.000             0:0.082          false             true
37       https-jsse-nio-443-exec-9                           main                      5                 WAITING          0.0              0.000             0:0.082          false             true
33       https-jsse-nio-443-exec-5                           main                      5                 WAITING          0.0              0.000             0:0.079          false             true
94       arthas-NettyHttpTelnetBootstrap-3-2                 system                    5                 RUNNABLE         0.0              0.000             0:0.070          false             true
35       https-jsse-nio-443-exec-7                           main                      5                 WAITING          0.0              0.000             0:0.067          false             true
31       https-jsse-nio-443-exec-3                           main                      5                 WAITING          0.0              0.000             0:0.065          false             true
27       lettuce-nioEventLoop-4-1                            main                      5                 RUNNABLE         0.0              0.000             0:0.047          false             true
20       container-0                                         main                      5                 TIMED_WAITING    0.0              0.000             0:0.031          false             false
40       https-jsse-nio-443-Acceptor                         main                      5                 RUNNABLE         0.0              0.000             0:0.021          false             true
13       RMI TCP Accept-45000                                system                    5                 RUNNABLE         0.0              0.000             0:0.019          false             true
88       arthas-NettyHttpTelnetBootstrap-3-1                 system                    5                 RUNNABLE         0.0              0.000             0:0.013          false             true
42       lettuce-nioEventLoop-4-2                            main                      5                 RUNNABLE         0.0              0.000             0:0.012          false             true
Memory                                       used           total          max            usage          GC
heap                                         43M            72M            425M           10.16%         gc.copy.count                                       162
tenured_gen                                  42M            50M            293M           14.46%         gc.copy.time(ms)                                    423
eden_space                                   733K           20608K         120192K        0.61%          gc.marksweepcompact.count                           8
survivor_space                               70K            2560K          14976K         0.47%          gc.marksweepcompact.time(ms)                        498
nonheap                                      99M            105M           -1             94.31%
codeheap_'non-nmethods'                      1M             2M             5M             23.81%
metaspace                                    75M            75M            -1             99.24%
codeheap_'profiled_nmethods'                 8M             12M            117M           7.43%
compressed_class_space                       8M             9M             1024M          0.87%
codeheap_'non-profiled_nmethods'             4M             4M             117M           4.07%
mapped                                       0K             0K             -              0.00%
direct                                       12M            12M            -              100.00%
mapped - 'non-volatile memory'               0K             0K             -              0.00%
Runtime
os.name                                                                                                  Linux
os.version                                                                                               3.10.0-1160.105.1.el7.x86_64
java.version                                                                                             17.0.8
java.home                                                                                                /root/Java/graalvm-jdk-17
systemload.average                                                                                       0.00
processors                                                                                               2
timestamp/uptime                                                                                         Sun Feb 11 22:08:14 CST 2024/12578

thread 获取进程的Class

更多

java
[arthas@22073]$ thread 39
"https-jsse-nio-443-Poller" Id=39 RUNNABLE (in native)
    at java.base@17.0.8/sun.nio.ch.EPoll.wait(Native Method)
    at java.base@17.0.8/sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:118)
    at java.base@17.0.8/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:129)
    at java.base@17.0.8/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:141)
    at app//org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:805)
    at java.base@17.0.8/java.lang.Thread.run(Thread.java:833)

jad 反编译Class

更多

java
[arthas@22073]$ jad sun.nio.ch.EPoll

ClassLoader:

Location:

        /*
         * Decompiled with CFR.
         */
        package sun.nio.ch;

        import java.io.IOException;
        import jdk.internal.misc.Unsafe;
        import sun.nio.ch.IOUtil;

        class EPoll {
            private static final Unsafe unsafe = Unsafe.getUnsafe();
            private static final int SIZEOF_EPOLLEVENT = EPoll.eventSize();
            private static final int OFFSETOF_EVENTS = EPoll.eventsOffset();
            private static final int OFFSETOF_FD = EPoll.dataOffset();
            static final int EPOLL_CTL_ADD = 1;
            static final int EPOLL_CTL_DEL = 2;
            static final int EPOLL_CTL_MOD = 3;
            static final int EPOLLIN = 1;
            static final int EPOLLOUT = 4;
            static final int EPOLLONESHOT = 0x40000000;

            ...
/*101*/         return unsafe.getInt(eventAddress + (long)OFFSETOF_EVENTS);
            }
        }

Affect(row-cnt:1) cost in 471 ms.

watch

更多

查看函数的返回值

java
[arthas@22073]$ watch cn.diyai.controller.interceptor.LoginInterceptor preHandle returnObj
Press Q or Ctrl+C to abort.
Affect(class count: 1 , method count: 1) cost in 54 ms, listenerId: 2
method=cn.diyai.controller.interceptor.LoginInterceptor.preHandle location=AtExit
ts=2024-02-11 22:18:52; [cost=14.692255ms] result=@Boolean[false]
method=cn.diyai.controller.interceptor.LoginInterceptor.preHandle location=AtExit
ts=2024-02-11 22:18:52; [cost=9.518998ms] result=@Boolean[true]

可知方法执行耗时9.518998ms,返回结果为true