Arthas
是Alibaba开源的Java诊断工具,深受开发者喜爱。
Arthas
可以帮助解决:
这个类从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
我改的代码为什么没有执行到?难道是我没 commit?分支搞错了?
遇到问题无法在线上 debug,难道只能通过加日志再重新发布吗?
线上遇到某个用户的数据处理有问题,但线上同样无法 debug,线下无法重现!
是否有一个全局视角来查看系统的运行状况?
有什么办法可以监控到JVM的实时运行状态?
怎么快速定位应用的热点,生成火焰图?
arthas使用笔记:
🌕一、常用命令
📌基础命令
help——查看命令帮助信息
cat——打印文件内容,和linux里的cat命令类似
echo–打印参数,和linux里的echo命令类似
grep——匹配查找,和linux里的grep命令类似
tee——复制标准输入到标准输出和指定的文件,和linux里的tee命令类似
pwd——返回当前的工作目录,和linux命令类似
cls——清空当前屏幕区域
session——查看当前会话的信息
reset——重置增强类,将被 Arthas 增强过的类全部还原,Arthas 服务端关闭时会重置所有增强过的类
version——输出当前目标 Java 进程所加载的 Arthas 版本号
history——打印命令历史
quit——退出当前 Arthas 客户端,其他 Arthas 客户端不受影响
stop——关闭 Arthas 服务端,所有 Arthas 客户端全部退出
keymap——Arthas快捷键列表及自定义快捷键
📌jvm相关
dashboard——当前系统的实时数据面板
thread——查看当前 JVM 的线程堆栈信息
jvm——查看当前 JVM 的信息
sysprop——查看和修改JVM的系统属性
sysenv——查看JVM的环境变量
vmoption——查看和修改JVM里诊断相关的option
perfcounter——查看当前 JVM 的Perf Counter信息
logger——查看和修改logger
getstatic——查看类的静态属性
ognl——执行ognl表达式
mbean——查看 Mbean 的信息
heapdump——dump java heap, 类似jmap命令的heap dump功能
📌class/classloader相关
sc——查看JVM已加载的类信息
sm——查看已加载类的方法信息
jad——反编译指定已加载类的源码
mc——内存编译器,内存编译.java文件为.class文件
redefine——加载外部的.class文件,redefine到JVM里
dump——dump 已加载类的 byte code 到特定目录
classloader——查看classloader的继承树,urls,类加载信息,使用classloader去getResource
🌕二、使用
📌下载
$ wget https://alibaba.github.io/arthas/arthas-boot.jar
📌启动arthas
$ java -jar arthas-boot.jar --启动arthas
$ java -jar arthas-boot.jar -h --查看帮助
启动之后选择对应的java进程,输入序号回车。
📌打开监控大盘
$ dashboard
📌查看对应线程
$ thread pid
$ thread pid | grep 'main(' --查找main class
📌查看jvm已加载的类
$ sc *UserController (-d 打印详细加载信息)
📌查看jvm已加载的类的方法信息
$ sm com.example.demo.arthas.user.UserController
📌反编译jvm已加载的类
$ jad com.example.demo.arthas.user.UserController
📌实时查看方法的返回值、抛出异常、入参
$ watch [class-pattern] [method-pattern] [express] [condition-express]
[express]:主要由ognl表达式,可组合:[params(参数), returnObj(返回值), target(当前对象), throwExp(异常信息)]
[condition-express]:
4个观察事件点:-b 方法调用前,-e 方法异常后,-s 方法返回后,-f 方法结束后;
-x 2:指定输出结果的属性遍历深度,默认为1
-n 2:指定执行次数
条件表达式: 'params[0]<0' 根据参数值过滤、 '#cost>200' 根据耗时过滤
eg:
$ watch com.example.demo.arthas.user.UserController hello "{params,returnObj}" -x 3 --监控该方法的入参、返回值,并且打印属性遍历深度2
$ watch demo.MathGame primeFactors "{params[0],throwExp}" -e -x 2 观察异常信息
$ watch com.example.demo.arthas.user.UserController hello 'target.log' -n 2 观察参数变化情况
📌trace 方法内部调用路径,并输出方法路径上的每个节点上耗时
$ trace [class-pattern] [method-pattern] [condition-express] --与watch相似,[condition-express]: -n 次数、 #cost 耗时
$ [arthas@10107]$ trace com.example.demo.arthas.user.UserController hello -n 1 --监控查看该方法内部调用耗时情况
📌stack 输出当前方法被调用的调用路径
$ trace [class-pattern] [method-pattern] [condition-express] --与watch相似,[condition-express]: -n 次数
📌thread
$ thread [-h] [-b] [--lockedMonitors] [--lockedSynchronizers] [-i <value>] [--state <value>] [-n <value>] [id]
$ thread -n --指定最忙的前N个线程并打印堆栈,cpu占用最高
$ thread 1322 --显示指定线程的运行堆栈
$ thread -b --找出当前阻塞其他线程的线程
$ thread -n 3 -i 5000 --查看5秒内的CPU使用率top n线程栈
📌refine 热更新代码
1.修改源码:
$ jad --source-only com.example.demo.arthas.user.UserController > /tmp/UserController.java --导出源码到目标文件夹
2.使用memory compile,结合classLoader重新编译源码
$ sc -d *UserController | grep classLoaderHash --获取该类对应的classLoader hash
$ mc -c 5674cd4d /tmp/UserController.java -d /tmp --mc重新编译到目标文件夹
3.使用refine重新加载class文件
$ mc -c 5674cd4d /tmp/UserController.java -d /tmp
📌ognl使用
动态更新应用Logger Level:
$ sc -d com.example.demo.arthas.user.UserController | grep classLoaderHash。--获取classLoader hash
$ ognl -c 1be6f5c3 '@com.example.demo.arthas.user.UserController@logger' --根据hash获取UserController.logger属性
$ ognl -c 5674cd4d '@com.example.demo.arthas.user.UserController@logger.setLevel(@ch.qos.lognull.classic.Level@DEBUG)'。--根据hash重新设置logger的值
$ ognl -c 1be6f5c3 '@org.slf4j.LoggerFactory@getLogger("root").setLevel(@ch.qos.logback.classic.Level@DEBUG)' --通过获取root logger,修改全局的logger level
📌tt(TimeTunnel:监控记录指定方法每次调用的入参和返回信息,并能对这些不同的时间下调用进行观测)
获取Spring Context
$ tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod --使用tt命令获取到spring context
$ tt -i 1000 -w 'target.getApplicationContext()' --使用tt命令从调用记录里获取到spring context
$ tt -i 1000 -w 'target.getApplicationContext().getBean("helloWorldService").getHelloMessage()' --获取spring bean,并调用函数
📌classLoader 查看classloader的继承树,urls,类加载信息
📌reset 显式清除清除增加代码,Arthas在 watch/trace 等命令时,实际上是修改了应用的字节码,插入增强的代码。显式执行 reset 命令,可以清除掉这些增强代码。