赞
踩
点击关注公众号,利用碎片时间学习
云原生这么多微服务,当然需要一个诊断利器来排查问题。
Arthas 是阿里开源的 Java 诊断工具,深受开发者喜爱。在线排查问题,无需重启;动态跟踪 Java 代码;实时监控 JVM 状态。Arthas 支持 JDK 6+,支持 Linux/Mac/Windows,采用命令行交互模式,同时提供丰富的 Tab 自动补全功能,进一步方便进行问题的定位和诊断。
官方定义为Java应用诊断利器,截至目前github收获29.4K个star。
可以用来查看线程,内存,GC和运行时状态,查看入参/返回值/异常,快速定位应用的热点,生成火焰图等功能,帮助更快排查疑难问题。本文主要讲述常见命令的使用。
执行如下命令下载 arthas-demo.jar
,再用java -jar命令启动案例程序:
- wget https://arthas.aliyun.com/arthas-demo.jar;
- java -jar arthas-demo.jar
执行如下命令下载arthas-boot.jar
,再用java -jar
命令启动:
- wget https://arthas.aliyun.com/arthas-boot.jar;
- java -jar arthas-boot.jar
arthas-boot是Arthas的启动程序,它启动后,会列出所有的Java进程,用户可以选择需要诊断的目标进程。
选择要诊断的Java程序,我这里输入 1 ,再按回车键(Enter)。
Attach成功之后,会打印Arthas LOGO。
输入 help 可以获取到Arthas相关命令帮助信息。
- [arthas@1266]$ help
- NAME DESCRIPTION
- help Display Arthas Help
- auth Authenticates the current session
- 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 me
- thod invocation
- tt Time Tunnel
- jvm Display the target JVM information
- memory Display jvm memory info.
- perfcounter Display the perf counter information.
- ognl Execute ognl expression.
- mc Memory compiler, compiles java files into bytecode and class files in memory.
- redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)
- retransform Retransform classes. @see Instrumentation#retransformClasses(Class...)
- 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
- base64 Encode and decode using Base64 representation
- 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
- vmtool jvm tool
- stop Stop/Shutdown Arthas server and exit the console.

与linux同样规则的命令此处不再赘述。如:history,cat,echo,pwd,grep。
dashboard 命令可以查看当前系统的实时数据面板。可以查看到CPU、内存、GC、运行环境等信息。
输入 q 或者 Ctrl+C 可以退出dashboard命令。
thread 1 命令会打印线程ID 1的栈。用 thread 1 | grep 'main(' 查找到main class。
可以通过 sc 命令来查找JVM里已加载的类,通过-d参数,可以打印出类加载的具体信息,很方便查找类加载问题。
- [arthas@1266]$ sc -d *MathGame
- class-info demo.MathGame
- code-source /home/shell/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@1b6d3586
- +-sun.misc.Launcher$ExtClassLoader@107df6e5
- classLoaderHash 1b6d3586
-
- Affect(row-cnt:1) cost in 50 ms.

sc支持通配,比如搜索所有的StringUtils:
sc *StringUtils
查找UserController的ClassLoader
- [arthas@1266]$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash
- classLoaderHash 19469ea2
sm命令则是查找类的具体函数。比如:
sm java.math.RoundingMode
通过-d参数可以打印函数的具体属性:
sm -d java.math.RoundingMode
查找特定的函数,比如查找构造函数:
sm java.math.RoundingMode <init>
jad demo.MathGame
通过
--source-only
参数可以只打印出在反编译的源代码:
jad --source-only com.example.demo.arthas.user.UserController
在Arthas里,有一个单独的ognl命令,可以动态执行代码。这个有点秀啊
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。