当前位置:   article > 正文

Java 诊断工具 Arthas 常见命令(超详细实战教程)

arthas命令

点击关注公众号,利用碎片时间学习

基本概念

云原生这么多微服务,当然需要一个诊断利器来排查问题。

Arthas 是阿里开源的 Java 诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪 Java 代码;实时监控 JVM 状态。Arthas 支持 JDK 6+,支持 Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。

16dd66d896d6d71c503211d35a5295f9.png

官方定义为Java应用诊断利器,截至目前github收获29.4K个star。

可以用来查看线程,内存,GC和运行时状态,查看入参/返回值/异常,快速定位应用的热点,生成火焰图等功能,帮助更快排查疑难问题。本文主要讲述常见命令的使用。

常见命令

启动arthas-demo(案例程序)

执行如下命令下载 arthas-demo.jar,再用java -jar命令启动案例程序:

  1. wget https://arthas.aliyun.com/arthas-demo.jar;
  2. java -jar arthas-demo.jar

启动arthas-boot(诊断工具程序)

执行如下命令下载arthas-boot.jar,再用java -jar命令启动:

  1. wget https://arthas.aliyun.com/arthas-boot.jar;
  2. java -jar arthas-boot.jar

arthas-boot是Arthas的启动程序,它启动后,会列出所有的Java进程,用户可以选择需要诊断的目标进程。

4797c5fc6a267c52a6e3e20967b80de0.png选择要诊断的Java程序,我这里输入 1 ,再按回车键(Enter)。

Attach成功之后,会打印Arthas LOGO。

46b082fb4541c06c2394cc04577667c0.png输入 help 可以获取到Arthas相关命令帮助信息。

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

与linux同样规则的命令此处不再赘述。如:history,cat,echo,pwd,grep。

系统的实时数据面板 dashboard 命令

313c57308e6ec54f713aaab00cb255da.pngdashboard 命令可以查看当前系统的实时数据面板。可以查看到CPU、内存、GC、运行环境等信息。

输入 q 或者 Ctrl+C 可以退出dashboard命令。

打印线程ID 的栈 thread

thread 1 命令会打印线程ID 1的栈。用 thread 1 | grep 'main(' 查找到main class。

dd56d094379c051ed40f8a9358ca437b.png

查找JVM里已加载的类 sc/sm

可以通过 sc 命令来查找JVM里已加载的类,通过-d参数,可以打印出类加载的具体信息,很方便查找类加载问题。

  1. [arthas@1266]$ sc -d *MathGame
  2.  class-info        demo.MathGame                                                                                             
  3.  code-source       /home/shell/arthas-demo.jar                                                                               
  4.  name              demo.MathGame                                                                                             
  5.  isInterface       false                                                                                                     
  6.  isAnnotation      false                                                                                                     
  7.  isEnum            false                                                                                                     
  8.  isAnonymousClass  false                                                                                                     
  9.  isArray           false                                                                                                     
  10.  isLocalClass      false                                                                                                     
  11.  isMemberClass     false                                                                                                     
  12.  isPrimitive       false                                                                                                     
  13.  isSynthetic       false                                                                                                     
  14.  simple-name       MathGame                                                                                                  
  15.  modifier          public                                                                                                    
  16.  annotation                                                                                                                  
  17.  interfaces                                                                                                                  
  18.  super-class       +-java.lang.Object                                                                                        
  19.  class-loader      +-sun.misc.Launcher$AppClassLoader@1b6d3586                                                               
  20.                      +-sun.misc.Launcher$ExtClassLoader@107df6e5                                                             
  21.  classLoaderHash   1b6d3586                                                                                                  
  22. Affect(row-cnt:1) cost in 50 ms.

sc支持通配,比如搜索所有的StringUtils:

sc *StringUtils

查找UserController的ClassLoader

  1. [arthas@1266]$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
  2.  classLoaderHash   19469ea2

sm命令则是查找类的具体函数。比如:

sm java.math.RoundingMode

通过-d参数可以打印函数的具体属性:

sm -d java.math.RoundingMode

查找特定的函数,比如查找构造函数:

sm java.math.RoundingMode <init>

反编译代码 jad命令

jad demo.MathGame

827a221fa48f6a89ecb74bd6fdce4048.png通过--source-only参数可以只打印出在反编译的源代码:

jad --source-only com.example.demo.arthas.user.UserController

动态执行代码 ognl 命令

在Arthas里,有一个单独的ognl命令,可以动态执行代码。这个有点秀啊

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/995039
推荐阅读
相关标签