Alibaba Arthas 使用學(xué)習(xí)之路,最詳細(xì)講解

1.Arthas 是什么世杀?

?? Arthas 是Alibaba 開發(fā)得一款java診斷工具阀参,可以幫我們查看jvm使用情況,查看線程數(shù)瞻坝,排除阻塞線程蛛壳;監(jiān)聽類和方法得調(diào)用次數(shù),失敗次數(shù)(失敗率)所刀,執(zhí)行時(shí)常衙荐;監(jiān)聽方法得入?yún)ⅲ鰠⒏〈矗惓P畔⒂且鳎豢焖俜淳幾g查看源碼,并修改源碼斩披,編譯運(yùn)行赴蝇;查看堆棧信息等鄙才。

2.這里列出一些常用命令并一一講解

?? 最常用得命令:dashboard芋类,thread卵牍,jvm弟翘,sc蕾久,sm荞估,jad剩膘,mc耳奕,redefine绑青,monitor,watch屋群。官方地址:https://arthas.aliyun.com/

dashboard

dashboard

?? dashboard 當(dāng)前系統(tǒng)的實(shí)時(shí)數(shù)據(jù)面板闸婴,按 ctrl+c 退出,當(dāng)運(yùn)行在Ali-tomcat時(shí)芍躏,會(huì)顯示當(dāng)前tomcat的實(shí)時(shí)信息邪乍,如HTTP請(qǐng)求的qps, rt, 錯(cuò)誤數(shù), 線程池信息等等。
?? 數(shù)據(jù)說明如下:

  • ID: Java級(jí)別的線程ID对竣,注意這個(gè)ID不能跟jstack中的nativeID一一對(duì)應(yīng)
  • NAME: 線程名
  • GROUP: 線程組名
  • PRIORITY: 線程優(yōu)先級(jí), 1~10之間的數(shù)字庇楞,越大表示優(yōu)先級(jí)越高
  • STATE: 線程的狀態(tài)
  • CPU%: 線程消耗的cpu占比,采樣100ms否纬,將所有線程在這100ms內(nèi)的cpu使用量求和吕晌,再算出每個(gè)線程的cpu使用占比。
  • TIME: 線程運(yùn)行總時(shí)間临燃,數(shù)據(jù)格式為 分:秒
  • INTERRUPTED: 線程當(dāng)前的中斷位狀態(tài)
  • DAEMON: 是否是守護(hù)線程

thread

?? 查看當(dāng)前線程信息睛驳,查看線程的堆棧
?? 參數(shù)說明:id(線程id)烙心,[n:](指定最忙的前N個(gè)線程并打印堆棧),[b](找出當(dāng)前阻塞其他線程的線程)乏沸,[i<value>](指定cpu占比統(tǒng)計(jì)的采樣間隔淫茵,單位為毫秒,建議5000)
?? 示例: thread -n 3 找出前3個(gè)線程的堆棧信息

$ thread -n 3
"as-command-execute-daemon" Id=58 cpuUsage=76% RUNNABLE
    at java.management@12.0.1/sun.management.ThreadImpl.dumpThreads0(Native Method)
    at java.management@12.0.1/sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:466)
    at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:133)
    at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:79)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
    at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:370)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base@12.0.1/java.lang.Thread.run(Thread.java:835)

    Number of locked synchronizers = 1
    - java.util.concurrent.ThreadPoolExecutor$Worker@7808db3c


"NioBlockingSelector.BlockPoller-1" Id=24 cpuUsage=17% RUNNABLE (in native)
    at java.base@12.0.1/sun.nio.ch.EPoll.wait(Native Method)
    at java.base@12.0.1/sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:120)
    at java.base@12.0.1/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:124)
    -  locked sun.nio.ch.Util$2@1a574b17
    -  locked sun.nio.ch.EPollSelectorImpl@7b0d44f5
    at java.base@12.0.1/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:136)
    at org.apache.tomcat.util.net.NioBlockingSelector$BlockPoller.run(NioBlockingSelector.java:304)


"http-nio-8087-ClientPoller-1" Id=36 cpuUsage=5% RUNNABLE (in native)
    at java.base@12.0.1/sun.nio.ch.EPoll.wait(Native Method)
    at java.base@12.0.1/sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:120)
    at java.base@12.0.1/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:124)
    -  locked sun.nio.ch.Util$2@703ba4c0
    -  locked sun.nio.ch.EPollSelectorImpl@6a6a92bd
    at java.base@12.0.1/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:136)
    at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:743)
    at java.base@12.0.1/java.lang.Thread.run(Thread.java:835)


Affect(row-cnt:0) cost in 131 ms.

?? 示例:thread 打出所有運(yùn)行線程

$ thread
Threads Total: 37, NEW: 0, RUNNABLE: 14, BLOCKED: 0, WAITING: 17, TIMED_WAITING: 6, TERMINATED: 0                                                                                                                                       
ID                 NAME                                                      GROUP                                  PRIORITY           STATE               %CPU               TIME               INTERRUPTED         DAEMON             
59                 as-command-execute-daemon                                 system                                 10                 RUNNABLE            100                0:0                false               true               
42                 AsyncAppender-Worker-arthas-cache.result.AsyncAppender    system                                 9                  WAITING             0                  0:0                false               true               
40                 Attach Listener                                           system                                 9                  RUNNABLE            0                  0:0                false               true               
14                 Catalina-utility-1                                        main                                   1                  WAITING             0                  0:1                false               false              
15                 Catalina-utility-2                                        main                                   1                  TIMED_WAITING       0                  0:0                false               false              
10                 Common-Cleaner                                            InnocuousThreadGroup                   8                  TIMED_WAITING       0                  0:0                false               true               
39                 DestroyJavaVM                                             main                                   5                  RUNNABLE            0                  0:12               false               false              
3                  Finalizer                                                 system                                 8                  WAITING             0                  0:0                false               true               
19                 MQTT Call: mqttId_inbound                                 main                                   5                  WAITING             0                  0:0                false               false              
23                 MQTT Ping: mqttId_inbound                                 main                                   5                  TIMED_WAITING       0                  0:0                false               false              
20                 MQTT Rec: mqttId_inbound                                  main                                   5                  RUNNABLE            0                  0:0                false               false              
21                 MQTT Snd: mqttId_inbound                                  main                                   5                  WAITING             0                  0:0                false               false              
24                 NioBlockingSelector.BlockPoller-1                         main                                   5                  RUNNABLE            0                  0:0                false               true               
2                  Reference Handler                                         system                                 10                 RUNNABLE            0                  0:0                false               true               
4                  Signal Dispatcher                                         system                                 9                  RUNNABLE            0                  0:0                false               true               
16                 container-0                                               main                                   5                  TIMED_WAITING       0                  0:0                false               false              
37                 http-nio-8087-Acceptor-0                                  main                                   5                  RUNNABLE            0                  0:0                false               true               
35                 http-nio-8087-ClientPoller-0                              main                                   5                  RUNNABLE            0                  0:0                false               true               
36                 http-nio-8087-ClientPoller-1                              main                                   5                  RUNNABLE            0                  0:0                false               true               
25                 http-nio-8087-exec-1                                      main                                   5                  WAITING             0                  0:0                false               true               
34                 http-nio-8087-exec-10                                     main                                   5                  WAITING             0                  0:0                false               true               
26                 http-nio-8087-exec-2                                      main                                   5                  WAITING             0                  0:0                false               true               
27                 http-nio-8087-exec-3                                      main                                   5                  WAITING             0                  0:0                false               true               
28                 http-nio-8087-exec-4                                      main                                   5                  WAITING             0                  0:0                false               true               
29                 http-nio-8087-exec-5                                      main                                   5                  WAITING             0                  0:0                false               true               
30                 http-nio-8087-exec-6                                      main                                   5                  WAITING             0                  0:0                false               true               
31                 http-nio-8087-exec-7                                      main                                   5                  WAITING             0                  0:0                false               true               
32                 http-nio-8087-exec-8                                      main                                   5                  WAITING             0                  0:0                false               true               
33                 http-nio-8087-exec-9                                      main                                   5                  WAITING             0                  0:0                false               true               
44                 job-timeout                                               system                                 9                  TIMED_WAITING       0                  0:0                false               true               
45                 nioEventLoopGroup-2-1                                     system                                 10                 RUNNABLE            0                  0:0                false               false              
49                 nioEventLoopGroup-2-2                                     system                                 10                 RUNNABLE            0                  0:1                false               false              
53                 nioEventLoopGroup-2-3                                     system                                 10                 RUNNABLE            0                  0:0                false               false              
46                 nioEventLoopGroup-3-1                                     system                                 10                 RUNNABLE            0                  0:0                false               false              
22                 pool-2-thread-4                                           main                                   5                  WAITING             0                  0:0                false               false              
47                 pool-3-thread-1                                           system                                 5                  TIMED_WAITING       0                  0:0                false               false              
48                 pool-4-thread-1                                           system                                 5                  WAITING             0                  0:0                false               false              
Affect(row-cnt:0) cost in 113 ms.

?? 示例:thread 20 查看線程id為20的堆棧信息

$ thread 20
"MQTT Rec: mqttId_inbound" Id=20 RUNNABLE (in native)
    at java.base@12.0.1/java.net.SocketInputStream.socketRead0(Native Method)
    at java.base@12.0.1/java.net.SocketInputStream.socketRead(SocketInputStream.java:115)
    at java.base@12.0.1/java.net.SocketInputStream.read(SocketInputStream.java:168)
    at java.base@12.0.1/java.net.SocketInputStream.read(SocketInputStream.java:140)
    at java.base@12.0.1/java.net.SocketInputStream.read(SocketInputStream.java:200)
    at java.base@12.0.1/java.io.DataInputStream.readByte(DataInputStream.java:270)
    at org.eclipse.paho.client.mqttv3.internal.wire.MqttInputStream.readMqttWireMessage(MqttInputStream.java:92)
    at org.eclipse.paho.client.mqttv3.internal.CommsReceiver.run(CommsReceiver.java:133)
    at java.base@12.0.1/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:515)
    at java.base@12.0.1/java.util.concurrent.FutureTask.run(FutureTask.java:264)
    at java.base@12.0.1/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base@12.0.1/java.lang.Thread.run(Thread.java:835)

    Number of locked synchronizers = 1
    - java.util.concurrent.ThreadPoolExecutor$Worker@79cf8c25

Affect(row-cnt:0) cost in 33 ms.

?? 示例:thread -b 找出阻塞其他線程的線程

$ thread -b
No most blocking thread found!
Affect(row-cnt:0) cost in 43 ms.

?? 示例:thread -n 3 -i 5000 阻塞指定5秒后收到前3線程的堆棧信息

$ thread -n 3  -i 5000
"Catalina-utility-1" Id=14 cpuUsage=21% TIMED_WAITING on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@72b49c8
    at java.base@12.0.1/jdk.internal.misc.Unsafe.park(Native Method)
    -  waiting on java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject@72b49c8
    at java.base@12.0.1/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:235)
    at java.base@12.0.1/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2123)
    at java.base@12.0.1/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:1182)
    at java.base@12.0.1/java.util.concurrent.ScheduledThreadPoolExecutor$DelayedWorkQueue.take(ScheduledThreadPoolExecutor.java:899)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1054)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1114)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base@12.0.1/java.lang.Thread.run(Thread.java:835)


"http-nio-8087-ClientPoller-1" Id=36 cpuUsage=14% RUNNABLE (in native)
    at java.base@12.0.1/sun.nio.ch.EPoll.wait(Native Method)
    at java.base@12.0.1/sun.nio.ch.EPollSelectorImpl.doSelect(EPollSelectorImpl.java:120)
    at java.base@12.0.1/sun.nio.ch.SelectorImpl.lockAndDoSelect(SelectorImpl.java:124)
    -  locked sun.nio.ch.Util$2@703ba4c0
    -  locked sun.nio.ch.EPollSelectorImpl@6a6a92bd
    at java.base@12.0.1/sun.nio.ch.SelectorImpl.select(SelectorImpl.java:136)
    at org.apache.tomcat.util.net.NioEndpoint$Poller.run(NioEndpoint.java:743)
    at java.base@12.0.1/java.lang.Thread.run(Thread.java:835)


"as-command-execute-daemon" Id=62 cpuUsage=10% RUNNABLE
    at java.management@12.0.1/sun.management.ThreadImpl.dumpThreads0(Native Method)
    at java.management@12.0.1/sun.management.ThreadImpl.getThreadInfo(ThreadImpl.java:466)
    at com.taobao.arthas.core.command.monitor200.ThreadCommand.processTopBusyThreads(ThreadCommand.java:133)
    at com.taobao.arthas.core.command.monitor200.ThreadCommand.process(ThreadCommand.java:79)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.process(AnnotatedCommandImpl.java:82)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl.access$100(AnnotatedCommandImpl.java:18)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:111)
    at com.taobao.arthas.core.shell.command.impl.AnnotatedCommandImpl$ProcessHandler.handle(AnnotatedCommandImpl.java:108)
    at com.taobao.arthas.core.shell.system.impl.ProcessImpl$CommandProcessTask.run(ProcessImpl.java:370)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base@12.0.1/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base@12.0.1/java.lang.Thread.run(Thread.java:835)

    Number of locked synchronizers = 1
    - java.util.concurrent.ThreadPoolExecutor$Worker@2d2909be


Affect(row-cnt:0) cost in 5045 ms.

jvm

?? 查看當(dāng)前JVM信息蹬跃,這里只拿部分信息來說

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 THREAD                                                                                                                                                                                                                                 
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 COUNT                                                             37                                                                                                                                                                   
 DAEMON-COUNT                                                      22                                                                                                                                                                   
 PEAK-COUNT                                                        38                                                                                                                                                                   
 STARTED-COUNT                                                     55                                                                                                                                                                   
 DEADLOCK-COUNT                                                    0                                                                                                                                                                    
                                                                                                                                                                                                                                        
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 FILE-DESCRIPTOR                                                                                                                                                                                                                        
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MAX-FILE-DESCRIPTOR-COUNT                                         -1                                                                                                                                                                   
 OPEN-FILE-DESCRIPTOR-COUNT                                        -1         

?? THREAD 相關(guān)

  • COUNT: JVM當(dāng)前活躍的線程數(shù)
  • DAEMON-COUNT: JVM當(dāng)前活躍的守護(hù)線程數(shù)
  • PEAK-COUNT: 從JVM啟動(dòng)開始曾經(jīng)活著的最大線程數(shù)
  • STARTED-COUNT: 從JVM啟動(dòng)開始總共啟動(dòng)過的線程次數(shù)
  • DEADLOCK-COUNT: JVM當(dāng)前死鎖的線程數(shù)

?? FILE-DESCRIPTOR 相關(guān)

  • MAX-FILE-DESCRIPTOR-COUNT:JVM進(jìn)程最大可以打開的文件描述符數(shù)
  • OPEN-FILE-DESCRIPTOR-COUNT:JVM當(dāng)前打開的文件描述符數(shù)

sc

?? 查看JVM已加載的類信息痘昌,
?? 參數(shù)說明:lass(包名+類名),method(方法名)炬转,[d](輸出詳細(xì)信息)辆苔,[E](開啟正則表達(dá)式匹配,默認(rèn)為通配符匹配)扼劈,[f](輸出當(dāng)前類的成員變量信息(需要配合參數(shù)-d一起使用))驻啤,[x:](指定輸出靜態(tài)變量時(shí)屬性的遍歷深度,默認(rèn)為 0荐吵,即直接使用 toString 輸出)
?? class-pattern支持全限定名骑冗,如com.taobao.test.AAA,也支持com/taobao/test/AAA這樣的格式先煎,這樣贼涩,我們從異常堆棧里面把類名拷貝過來的時(shí)候,不需要在手動(dòng)把/替換為 . 啦薯蝎。
?? 示例:sc com.cloud*

$ sc com.cloud*
com.cloud.mqtt.MqttApplication
com.cloud.mqtt.MqttApplication$$EnhancerBySpringCGLIB$$b4fbeb8a
com.cloud.mqtt.mqtt.MqttConfig
com.cloud.mqtt.mqtt.MqttConfig$$EnhancerBySpringCGLIB$$d1953d4e
com.cloud.mqtt.mqtt.MqttConfig$$EnhancerBySpringCGLIB$$d1953d4e$$FastClassBySpringCGLIB$$69e081d8
com.cloud.mqtt.mqtt.MqttConfig$$FastClassBySpringCGLIB$$bf72748c
com.cloud.mqtt.mqtt.MqttConfig$1
com.cloud.mqtt.mqtt.MqttReceiveConfig
com.cloud.mqtt.mqtt.MqttReceiveConfig$$EnhancerBySpringCGLIB$$5631a71d
com.cloud.mqtt.mqtt.MqttSenderConfig
com.cloud.mqtt.mqtt.MqttSenderConfig$$EnhancerBySpringCGLIB$$3bfb2a03
com.cloud.mqtt.mqtt.Publisher
com.cloud.mqtt.web.MqttGateway
com.cloud.mqtt.web.TestController
com.sun.proxy.$Proxy65
Affect(row-cnt:15) cost in 20 ms.

?? 示例:sc -d -f com.cloud.mqtt.web.TestController 打印類的信息以及字段的信息

$ sc -d -f com.cloud.mqtt.web.TestController
 class-info        com.cloud.mqtt.web.TestController                                                                                                                                                                                    
 code-source       file:/home/fengqianrun/opt/server/mqtt-0.0.1.jar!/BOOT-INF/classes!/                                                                                                                                                 
 name              com.cloud.mqtt.web.TestController                                                                                                                                                                                    
 isInterface       false                                                                                                                                                                                                                
 isAnnotation      false                                                                                                                                                                                                                
 isEnum            false                                                                                                                                                                                                                
 isAnonymousClass  false                                                                                                                                                                                                                
 isArray           false                                                                                                                                                                                                                
 isLocalClass      false                                                                                                                                                                                                                
 isMemberClass     false                                                                                                                                                                                                                
 isPrimitive       false                                                                                                                                                                                                                
 isSynthetic       false                                                                                                                                                                                                                
 simple-name       TestController                                                                                                                                                                                                       
 modifier          public                                                                                                                                                                                                               
 annotation        org.springframework.web.bind.annotation.RestController,org.springframework.web.bind.annotation.RequestMapping                                                                                                        
 interfaces                                                                                                                                                                                                                             
 super-class       +-java.lang.Object                                                                                                                                                                                                   
 class-loader      +-org.springframework.boot.loader.LaunchedURLClassLoader@439f5b3d                                                                                                                                                    
                     +-jdk.internal.loader.ClassLoaders$AppClassLoader@5bc2b487                                                                                                                                                         
                       +-jdk.internal.loader.ClassLoaders$PlatformClassLoader@75d2da2d                                                                                                                                                  
 classLoaderHash   439f5b3d                                                                                                                                                                                                             
 fields            modifier  private                                                                                                                                                                                                    
                   type      com.cloud.mqtt.web.MqttGateway                                                                                                                                                                                                      
                   name      mqttGateway                                                                                                                                                                                                
                   annotationjavax.annotation.Resource                                                                                                                                                                                  
                                                                                                                                                                                                                                        

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

sm

?? 查看已加載類的方法信息遥倦,命令只能看到由當(dāng)前類所聲明 (declaring) 的方法,父類則無法看到占锯。
?? 參數(shù)說明:class(包名+類名)袒哥,method(方法名稱),[d](展示每個(gè)方法的詳細(xì)信息)消略,[E](開啟正則表達(dá)式匹配堡称,默認(rèn)為通配符匹配)
?? 示例:sm com.cloud.mqtt.web.TestController 查詢TestController下的所有方法

$ sm  com.cloud.mqtt.web.TestController
com.cloud.mqtt.web.TestController <init>()V
com.cloud.mqtt.web.TestController test(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
com.cloud.mqtt.web.TestController sendMqtt(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
com.cloud.mqtt.web.TestController sendTest(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;
Affect(row-cnt:4) cost in 12 ms.

?? 示例:sm -d com.cloud.mqtt.web.TestController test 查詢TestController下的test方法詳情

$ sm -d com.cloud.mqtt.web.TestController test
 declaring-class  com.cloud.mqtt.web.TestController                                                                                                                                                                                     
 method-name      test                                                                                                                                                                                                                  
 modifier         public                                                                                                                                                                                                                
 annotation       org.springframework.web.bind.annotation.GetMapping                                                                                                                                                                    
 parameters       java.lang.String                                                                                                                                                                                                      
                  java.lang.String                                                                                                                                                                                                      
 return           java.lang.String                                                                                                                                                                                                      
 exceptions                                                                                                                                                                                                                             

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

jad

?? jad 反編譯指定已加載類得源碼,jad 在Arthas Console上艺演,反編譯出來得源碼是帶語法高亮得却紧,閱讀方便,當(dāng)然反編譯出來得java代碼可能會(huì)存在語法錯(cuò)誤胎撤,但不影響你的進(jìn)行閱讀晓殊。
?? 參數(shù)說明:class(包名+類名),[c:](類所屬ClassLoader得hashcode)哩照,[E](開啟正則表達(dá)匹配挺物,默認(rèn)通配符)
?? 示例:反編譯 com.cloud.mqtt.web.TestController

$ jad com.cloud.mqtt.web.TestController

ClassLoader:                                                                                                                                                                                                                            
+-org.springframework.boot.loader.LaunchedURLClassLoader@439f5b3d                                                                                                                                                                       
  +-jdk.internal.loader.ClassLoaders$AppClassLoader@5bc2b487                                                                                                                                                                            
    +-jdk.internal.loader.ClassLoaders$PlatformClassLoader@75d2da2d                                                                                                                                                                     

Location:                                                                                                                                                                                                                               
file:/home/fengqianrun/opt/server/mqtt-0.0.1.jar!/BOOT-INF/classes!/                                                                                                                                                                    

/*
 * Decompiled with CFR 0_132.
 * 
 * Could not load the following classes:
 *  com.cloud.mqtt.web.MqttGateway
 *  javax.annotation.Resource
 *  org.springframework.web.bind.annotation.GetMapping
 *  org.springframework.web.bind.annotation.RequestMapping
 *  org.springframework.web.bind.annotation.RequestParam
 *  org.springframework.web.bind.annotation.RestController
 */
package com.cloud.mqtt.web;

import com.cloud.mqtt.web.MqttGateway;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value={"/test"})
public class TestController {
    @Resource
    private MqttGateway mqttGateway;

    @GetMapping(value={"/"})
    public String test(String a, String b) {
        String rt = a + b;
        return rt;
    }

    @GetMapping(value={"/send"})
    public String sendMqtt(@RequestParam(value="data") String data, @RequestParam(value="topic") String topic) {
        String rt = "sendMqtt";
        return rt;
    }

    @GetMapping(value={"/sendTest"})
    public String sendTest(@RequestParam(value="data") String data, @RequestParam(value="topic") String topic) {
        String rt = "sendMqtt";
        return "OK";
    }
}

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

?? 示例:反編譯 com.cloud.mqtt.web.TestController test方法

$ jad com.cloud.mqtt.web.TestController test

ClassLoader:                                                                                                                                                                                                                            
+-org.springframework.boot.loader.LaunchedURLClassLoader@439f5b3d                                                                                                                                                                       
  +-jdk.internal.loader.ClassLoaders$AppClassLoader@5bc2b487                                                                                                                                                                            
    +-jdk.internal.loader.ClassLoaders$PlatformClassLoader@75d2da2d                                                                                                                                                                     

Location:                                                                                                                                                                                                                               
file:/home/fengqianrun/opt/server/mqtt-0.0.1.jar!/BOOT-INF/classes!/                                                                                                                                                                    

@GetMapping(value={"/"})
public String test(String a, String b) {
    String rt = a + b;
    return rt;
}

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

mc

?? 內(nèi)存編譯器,編譯.java文件生成.class文件飘弧。編譯生成.class之后們识藤,可以節(jié)后 redefine 命令實(shí)現(xiàn)熱更新代碼砚著。
?? 參數(shù)說明:[c:](指定classloader),[d](指定輸出目錄)

mc /tmp/Test.java 編譯Test.java文件
mc -c 327a647b /tmp/Test.java 指定classloader編譯
mc -d /tmp/output /tmp/ClassA.java /tmp/ClassB.java 編譯到指定目錄

redefine

?? 加載外部的 .class 文件到redefine jvm已加載的類痴昧。
?? redefine后的原來的類不能恢復(fù)稽穆,redefine有可能失敗(比如增加了新的field)赶撰,參考jdk本身的文檔舌镶。
?? 參數(shù)說明:[c:](ClassLoader的hashcode),[p:](外部的.class文件的完整路徑豪娜,支持多個(gè))

redefine /tmp/Test.class
結(jié)合 jad/mc 命令使用:
jad --source-only com.example.demo.arthas.user.UserController > /tmp/UserController.java
mc /tmp/UserController.java -d /tmp
redefine /tmp/com/example/demo/arthas/user/UserController.class
- jad命令反編譯餐胀,然后可以用其它編譯器,比如vim來修改源碼
- mc命令來內(nèi)存編譯修改過的代碼
- 用redefine命令加載新的字節(jié)碼

?? redefine的限制瘤载,不允許新增加field/method否灾,正在跑的函數(shù),沒有退出不能生效鸣奔,比如下面新增加的System.out.println墨技,只有run()函數(shù)里的會(huì)生效。

monitor

?? monitor 方法監(jiān)控執(zhí)行挎狸,是一個(gè)非實(shí)時(shí)得返回命令扣汪,意思就是可以一直監(jiān)控某一個(gè)方法,直到你按 ctrl+c結(jié)束為止锨匆。
?? 監(jiān)控得信息包括:timestamp(時(shí)間戳)崭别,class(java類),method(方法统刮,包括構(gòu)造方法和普通方法)紊遵,total(調(diào)用次數(shù)),success(成功次數(shù))侥蒙,fail(失敗次數(shù)),rt(平均RT)匀奏,fail-rate(失敗率)鞭衩。
?? 方法參數(shù)說明:class(包名+類名),method(方法名)娃善,[E](開啟正則表達(dá)匹配论衍,默認(rèn)通配符),[c:](得到運(yùn)行報(bào)告得間隔時(shí)間聚磺,默認(rèn)是120秒)

$ monitor -c 5 com.cloud.mqtt.web.TestController test
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 60 ms.
 timestamp            class                              method  total  success  fail  avg-rt(ms)  fail-rate                                                                                                                            
-------------------------------------------------------------------------------------------------------------                                                                                                                           
 2019-08-19 14:29:03  com.cloud.mqtt.web.TestController  test    6      6        0     0.34        0.00%                                                                                                                                

 timestamp            class                              method  total  success  fail  avg-rt(ms)  fail-rate                                                                                                                            
-------------------------------------------------------------------------------------------------------------                                                                                                                           
 2019-08-19 14:29:08  com.cloud.mqtt.web.TestController  test    1      1        0     0.09        0.00%                                                                                                                                

 timestamp            class                              method  total  success  fail  avg-rt(ms)  fail-rate                                                                                                                            
-------------------------------------------------------------------------------------------------------------                                                                                                                           
 2019-08-19 14:29:13  com.cloud.mqtt.web.TestController  test    0      0        0     0.00        0.00%                                                                                                                                

 timestamp            class                              method  total  success  fail  avg-rt(ms)  fail-rate                                                                                                                            
-------------------------------------------------------------------------------------------------------------                                                                                                                           
 2019-08-19 14:29:18  com.cloud.mqtt.web.TestController  test    0      0        0     0.00        0.00%            

??我得類 com.cloud.mqtt.web.TestController 坯台,方法為 test ,-c 5 得意思就是每5秒打印一次瘫寝。

watch

?? watch 方法執(zhí)行數(shù)據(jù)觀測(cè)蜒蕾。
?? 參數(shù)說明:class(類名表達(dá)式匹配)稠炬,method(方法名表達(dá)式匹配),express(觀察表達(dá)式)咪啡,condition-express(條件表達(dá)式)首启,[b](在方法調(diào)用之前觀察),[e](在方法異常之后觀察)撤摸,[s](在方法返回之后觀察)毅桃,[f](在方法結(jié)束之后(正常返回和異常返回)觀察),[E](開啟正則表達(dá)式匹配准夷,默認(rèn)為通配符匹配)钥飞,[x:](指定輸出結(jié)果的屬性遍歷深度,默認(rèn)為 1)
?? 特別說明:

  • watch 命令定義了4個(gè)觀察事件點(diǎn)衫嵌,即 -b 方法調(diào)用前代承,-e 方法異常后,-s 方法返回后渐扮,-f 方法結(jié)束后
  • 4個(gè)觀察事件點(diǎn) -b论悴、-e、-s 默認(rèn)關(guān)閉墓律,-f 默認(rèn)打開膀估,當(dāng)指定觀察點(diǎn)被打開后,在相應(yīng)事件點(diǎn)會(huì)對(duì)觀察表達(dá)式進(jìn)行求值并輸出
  • 這里要注意方法入?yún)⒑头椒ǔ鰠⒌膮^(qū)別耻讽,有可能在中間被修改導(dǎo)致前后不一致察纯,除了 -b 事件點(diǎn) params 代表方法入?yún)⑼猓溆嗍录即矸椒ǔ鰠?/li>
  • 當(dāng)使用 -b 時(shí)针肥,由于觀察事件點(diǎn)是在方法調(diào)用前饼记,此時(shí)返回值或異常均不存在
curl "http://192.168.15.130:8087/test/?a=1&b=3" ,-x 2的意思是慰枕,是否深度解析參數(shù)具则,如果你的參數(shù)是一個(gè)對(duì)象,不深度解析具帮,你看到的只是對(duì)象地址博肋,深度解析的值,跟你參數(shù)所嵌套層數(shù)有關(guān)蜂厅。
$ watch com.cloud.mqtt.web.TestController  test "{params,returnObj}" -x 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 41 ms.
ts=2019-08-19 17:02:09; [cost=0.241819ms] result=@ArrayList[
    @Object[][
        @String[1],
        @String[3],
    ],
    @String[13],
]

-b 意思是解析入?yún)ⅲ?s解析出參匪凡, -n 2執(zhí)行兩次的意思,2次完則結(jié)束掘猿。結(jié)果的輸出順序和事件發(fā)生的先后順序一致病游,和命令中 -s -b 的順序無關(guān)。
$ watch com.cloud.mqtt.web.TestController  test "{params,returnObj}" -x 2 -b  -s -n 2
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 46 ms.
ts=2019-08-19 17:06:21; [cost=0.006941ms] result=@ArrayList[
    @Object[][
        @String[1],
        @String[3],
    ],
    null,
]
ts=2019-08-19 17:06:21; [cost=0.945661ms] result=@ArrayList[
    @Object[][
        @String[1],
        @String[3],
    ],
    @String[13],
]
Command execution times exceed limit: 2, so command will exit. You can set it with -n option.

條件表達(dá)式稠通,第一個(gè)傳參大于5才執(zhí)行
$ watch com.cloud.mqtt.web.TestController  test "{params[0],returnObj}" "params[0] > 5"
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 39 ms.
ts=2019-08-19 17:21:58; [cost=0.183399ms] result=@ArrayList[
    @String[6],
    @String[63],
]

異常信息舉例:-e 表示拋出異常時(shí)才觸發(fā)衬衬,express中买猖,表示異常信息的變量是throwExp
$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 62 ms.
ts=2018-12-03 19:38:00; [cost=1.414993ms] result=@ArrayList[
    @Integer[-1120397038],
    java.lang.IllegalArgumentException: number is: -1120397038, need >= 2
    at demo.MathGame.primeFactors(MathGame.java:46)
    at demo.MathGame.run(MathGame.java:24)
    at demo.MathGame.main(MathGame.java:16),
]

按照耗時(shí)進(jìn)行過濾:#cost>200(單位是ms)表示只有當(dāng)耗時(shí)大于200ms時(shí)才會(huì)輸出,過濾掉執(zhí)行時(shí)間小于200ms的調(diào)用
$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
$ watch demo.MathGame primeFactors '{params, returnObj}' '#cost>200' -x 2
Press Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 66 ms.
ts=2018-12-03 19:40:28; [cost=2112.168897ms] result=@ArrayList[
    @Object[][
        @Integer[2141897465],
    ],
    @ArrayList[
        @Integer[5],
        @Integer[428379493],
    ],
]

觀察當(dāng)前對(duì)象中的屬性:如果想查看方法運(yùn)行前后佣耐,當(dāng)前對(duì)象中的屬性政勃,可以使用target關(guān)鍵字,代表當(dāng)前對(duì)象兼砖,然后使用target.field_name訪問當(dāng)前對(duì)象的某個(gè)屬性
$ watch com.cloud.mqtt.web.TestController  test 'target'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 47 ms.
ts=2019-08-19 17:27:09; [cost=0.450697ms] result=@TestController[
    mqttGateway=@$Proxy65[gateway proxy for service interface [interface com.cloud.mqtt.web.MqttGateway]],
]

$ watch com.cloud.mqtt.web.TestController  test 'target.mqttGateway'
Press Q or Ctrl+C to abort.
Affect(class-cnt:1 , method-cnt:1) cost in 46 ms.
ts=2019-08-19 17:28:31; [cost=0.352704ms] result=@$Proxy65[
    m1=@Method[public boolean java.lang.Object.equals(java.lang.Object)],
    m10=@Method[public abstract boolean org.springframework.aop.framework.Advised.isExposeProxy()],
    m13=@Method[public abstract void org.springframework.aop.framework.Advised.addAdvisor(org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException],
    m7=@Method[public abstract boolean org.springframework.aop.framework.Advised.isProxyTargetClass()],
    m15=@Method[public abstract void org.springframework.aop.framework.Advised.removeAdvisor(int) throws org.springframework.aop.framework.AopConfigException],
    m23=@Method[public abstract java.lang.Class[] org.springframework.aop.framework.Advised.getProxiedInterfaces()],
    m5=@Method[public abstract int org.springframework.aop.framework.Advised.indexOf(org.springframework.aop.Advisor)],
    m22=@Method[public abstract org.springframework.aop.TargetSource org.springframework.aop.framework.Advised.getTargetSource()],
    m18=@Method[public abstract void org.springframework.aop.framework.Advised.addAdvice(int,org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException],
    m19=@Method[public abstract void org.springframework.aop.framework.Advised.addAdvice(org.aopalliance.aop.Advice) throws org.springframework.aop.framework.AopConfigException],
    m0=@Method[public native int java.lang.Object.hashCode()],
    m24=@Method[public abstract boolean org.springframework.aop.framework.Advised.isInterfaceProxied(java.lang.Class)],
    m20=@Method[public abstract boolean org.springframework.aop.framework.Advised.removeAdvice(org.aopalliance.aop.Advice)],
    m9=@Method[public abstract void org.springframework.aop.framework.Advised.setExposeProxy(boolean)],
    m8=@Method[public abstract void org.springframework.aop.framework.Advised.setTargetSource(org.springframework.aop.TargetSource)],
    m2=@Method[public java.lang.String java.lang.Object.toString()],
    m26=@Method[public abstract java.lang.Class org.springframework.aop.TargetClassAware.getTargetClass()],
    m14=@Method[public abstract void org.springframework.aop.framework.Advised.addAdvisor(int,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException],
    m27=@Method[public abstract java.lang.Class org.springframework.core.DecoratingProxy.getDecoratedClass()],
    m16=@Method[public abstract boolean org.springframework.aop.framework.Advised.removeAdvisor(org.springframework.aop.Advisor)],
    m4=@Method[public abstract int org.springframework.aop.framework.Advised.indexOf(org.aopalliance.aop.Advice)],
    m6=@Method[public abstract boolean org.springframework.aop.framework.Advised.isFrozen()],
    m17=@Method[public abstract boolean org.springframework.aop.framework.Advised.replaceAdvisor(org.springframework.aop.Advisor,org.springframework.aop.Advisor) throws org.springframework.aop.framework.AopConfigException],
    m11=@Method[public abstract void org.springframework.aop.framework.Advised.setPreFiltered(boolean)],
    m3=@Method[public abstract void com.cloud.mqtt.web.MqttGateway.sendToMqtt(java.lang.String,java.lang.String)],
    m21=@Method[public abstract java.lang.String org.springframework.aop.framework.Advised.toProxyConfigString()],
    m25=@Method[public abstract org.springframework.aop.Advisor[] org.springframework.aop.framework.Advised.getAdvisors()],
    m12=@Method[public abstract boolean org.springframework.aop.framework.Advised.isPreFiltered()],
]

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末奸远,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子讽挟,更是在濱河造成了極大的恐慌懒叛,老刑警劉巖,帶你破解...
    沈念sama閱讀 211,743評(píng)論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件耽梅,死亡現(xiàn)場(chǎng)離奇詭異薛窥,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)眼姐,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,296評(píng)論 3 385
  • 文/潘曉璐 我一進(jìn)店門诅迷,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人众旗,你說我怎么就攤上這事罢杉。” “怎么了贡歧?”我有些...
    開封第一講書人閱讀 157,285評(píng)論 0 348
  • 文/不壞的土叔 我叫張陵滩租,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我利朵,道長(zhǎng)律想,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 56,485評(píng)論 1 283
  • 正文 為了忘掉前任绍弟,我火速辦了婚禮技即,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘晌柬。我一直安慰自己姥份,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,581評(píng)論 6 386
  • 文/花漫 我一把揭開白布年碘。 她就那樣靜靜地躺著,像睡著了一般展鸡。 火紅的嫁衣襯著肌膚如雪屿衅。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,821評(píng)論 1 290
  • 那天莹弊,我揣著相機(jī)與錄音涤久,去河邊找鬼涡尘。 笑死,一個(gè)胖子當(dāng)著我的面吹牛响迂,可吹牛的內(nèi)容都是我干的考抄。 我是一名探鬼主播,決...
    沈念sama閱讀 38,960評(píng)論 3 408
  • 文/蒼蘭香墨 我猛地睜開眼蔗彤,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼川梅!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起然遏,我...
    開封第一講書人閱讀 37,719評(píng)論 0 266
  • 序言:老撾萬榮一對(duì)情侶失蹤贫途,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后待侵,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體丢早,經(jīng)...
    沈念sama閱讀 44,186評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,516評(píng)論 2 327
  • 正文 我和宋清朗相戀三年秧倾,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了怨酝。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,650評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡那先,死狀恐怖农猬,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情胃榕,我是刑警寧澤盛险,帶...
    沈念sama閱讀 34,329評(píng)論 4 330
  • 正文 年R本政府宣布,位于F島的核電站勋又,受9級(jí)特大地震影響苦掘,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜楔壤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,936評(píng)論 3 313
  • 文/蒙蒙 一鹤啡、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧蹲嚣,春花似錦递瑰、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,757評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至议惰,卻和暖如春慎颗,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,991評(píng)論 1 266
  • 我被黑心中介騙來泰國(guó)打工俯萎, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留傲宜,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 46,370評(píng)論 2 360
  • 正文 我出身青樓夫啊,卻偏偏與公主長(zhǎng)得像函卒,于是被迫代替她去往敵國(guó)和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子撇眯,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,527評(píng)論 2 349

推薦閱讀更多精彩內(nèi)容

  • 這篇文章是我之前翻閱了不少的書籍以及從網(wǎng)絡(luò)上收集的一些資料的整理报嵌,因此不免有一些不準(zhǔn)確的地方,同時(shí)不同JDK版本的...
    高廣超閱讀 15,565評(píng)論 3 83
  • 一:java概述:1叛本,JDK:Java Development Kit沪蓬,java的開發(fā)和運(yùn)行環(huán)境,java的開發(fā)工...
    ZaneInTheSun閱讀 2,635評(píng)論 0 11
  • 【JAVA 線程】 線程 進(jìn)程:是一個(gè)正在執(zhí)行中的程序来候。每一個(gè)進(jìn)程執(zhí)行都有一個(gè)執(zhí)行順序跷叉。該順序是一個(gè)執(zhí)行路徑,或者...
    Rtia閱讀 2,761評(píng)論 2 20
  • 一营搅、Python簡(jiǎn)介和環(huán)境搭建以及pip的安裝 4課時(shí)實(shí)驗(yàn)課主要內(nèi)容 【Python簡(jiǎn)介】: Python 是一個(gè)...
    _小老虎_閱讀 5,725評(píng)論 0 10
  • 林炳文Evankaka原創(chuàng)作品云挟。轉(zhuǎn)載自http://blog.csdn.net/evankaka 本文主要講了ja...
    ccq_inori閱讀 648評(píng)論 0 4