Arthas 是 阿里巴巴最近开源出来的一个针对 java 的工具,主要是针对 java 的问题进行诊断!
一、概述
这个工具可以协助你做下面这些事情:
- 这个类是从哪个 jar 包加载而来的?
- 为什么会报各种类相关的 Exception?
- 线上遇到问题无法 debug 好蛋疼,难道只能反复通过增加 System.out 或通过加日志再重新发布吗?
- 线上的代码为什么没有执行到这里?是由于代码没有 commit?还是搞错了分支?
- 线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
- 是否有一个全局视角来查看系统的运行状况?
- 有什么办法可以监控到JVM的实时运行状态?
二、安装方式
Linux 安装方式
安装Arthas:
curl -L https://alibaba.github.io/arthas/install.sh | sh
启动Arthas:
./as.sh
成功启动后,会看到如下界面。
三、常用命令
3.1 基础命令
help——查看命令帮助信息
cls——清空当前屏幕区域
session——查看当前会话的信息
reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
version——输出当前目标 Java 进程所加载的 Arthas 版本号
quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
shutdown——关闭 Arthas 服务端,所有 Arthas 客户端全部退出
keymap——Arthas快捷键列表及自定义快捷键
jvm相关
dashboard——当前系统的实时数据面板
thread——查看当前 JVM 的线程堆栈信息
jvm——查看当前 JVM 的信息
sysprop——查看和修改JVM的系统属性
New! getstatic——查看类的静态属性
class/classloader相关
sc——查看JVM已加载的类信息
sm——查看已加载类的方法信息
dump——dump 已加载类的 byte code 到特定目录
redefine——加载外部的.class文件,redefine到JVM里
jad——反编译指定已加载类的源码
classloader——查看classloader的继承树,urls,类加载信息,使用classloader去getResource
monitor/watch/trace相关
请注意,这些命令,都通过字节码增强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测,因此在线上、预发使用时,请尽量明确需要观测的类、方法以及条件,诊断结束要执行 shutdown 或将增强过的类执行 reset 命令。
monitor——方法执行监控
watch——方法执行数据观测
trace——方法内部调用路径,并输出方法路径上的每个节点上耗时
stack——输出当前方法被调用的调用路径
tt——方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测
options
options——查看或设置Arthas全局开关
管道
Arthas支持使用管道对上述命令的结果进行进一步的处理,如sm org.apache.log4j.Logger | grep
grep——搜索满足条件的结果
plaintext——将命令的结果去除颜色
wc——按行统计输出结果
Web Console
通过websocket连接Arthas。
Web Console
其他特性
- 异步命令支持
- 执行结果存日志
- 批处理的支持
- ognl表达式的用法说明
3.2 使用示例
首先,在窗口中,输入 help 查看一下所有提供的可用命令(他的通信本质是通过 telnet 协议来通信的),如下图:
Attach success.
Connecting to arthas server... current timestamp is 1537266148
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
,---. ,------. ,--------.,--. ,--. ,---. ,---.
/ O \ | .--. ''--. .--'| '--' | / O \ ' .-'
| .-. || '--'.' | | | .--. || .-. |`. `-.
| | | || |\ \ | | | | | || | | |.-' |
`--' `--'`--' '--' `--' `--' `--'`--' `--'`-----'
wiki: https://alibaba.github.io/arthas
version: 3.0.4
pid: 25206
timestamp: 1537266148841
$ help
NAME DESCRIPTION
help Display Arthas Help
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 method invocation
tt Time Tunnel
jvm Display the target JVM information
dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info.
dump Dump class byte array from JVM
options View and change various Arthas options
cls Clear the screen
reset Reset all the enhanced classes
version Display Arthas version
shutdown Shut down Arthas server and exit the console
session Display current session information
sysprop Display, and change the system properties.
redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...)
$