线上应用调试利器 --Arthas
博客园:等你归去来:线上应用调试利器 --Arthas
arthas 官网地址:https://alibaba.github.io/arthas/
Arthas
- 查到一类是从哪个 jar 包加载的?为什么会报各种类相关的 Exception?
- 怀疑自己的代码未被部署到服务器,可以通过命令快速验证服务器上的代码就是本地的代码;
- 通过 arthas 进行线上debug, 查看方法返回值以确认问题所在;
- 嵌入自己的debug代码,快速验证猜想;
- 操作完成后,可以将所有debug代码删除,从而避免影响线上运行;
安装
wget https://alibaba.github.io/arthas/arthas-boot.jar
java -jar arthas-boot.jar
操作
arthas 会检测到目前正在有几个运行中的java程序,你只需按照序号选择就就可以了。
问题1. 我如何查找某个只知道大概的类,或者说我想确认某个类是否已被系统加载?
arthas 怎么查找一个类?
sc *DispatcharServlet # 即可以找到需要的类全路径,如果存在的话
sm org.springframework.web.servlet.DispatcherServlet getHandler # 查看某个方法的信息,如果存在的话
使用通配符列出所有的方法:
问题2. 如何查看一个class类的具体信息?
jad org.springframework.web.servlet.DispatcherServlet # 直接反编译出java 源代码,包含一此额外信息的
问题3. 如何跟踪某个方法的返回值、入参… ?
watch com.test.ob testMethod "{params, returnObj, throwExp}" -e -x 2 # 同时监控入参,返回值,及异常
如果有异常,直接打印出来,否则出入参直接监控,超级方便!
问题4. 查看最繁忙的线程,以及是否有阻塞情况发生?
thread -n 3 # 查看最繁忙的三个线程栈信息
thread # 以直观的方式展现所有的线程情况
thread -b #找出当前阻塞其他线程的线程
问题5. 如何验证自己的代码猜想,临时更改代码运行?
jad --source-only com.example.demo.arthas.user.UserController > /tmp/UserController.java # 先反编译出class源码
# 然后使用外部工具编辑内容
mc /tmp/UserController.java -d /tmp # 再编译成class
# 最后,重新载入定义的类,就可以实时验证你的猜测了
redefine /tmp/com/example/demo/arthas/user/UserController.class
如上,是直接更改线上代码的方式,但是一般好像是编译不成功的。所以,最好是 本地ide 编译成 class 文件后,再上传替换为好!
问题6. 我如何测试某个方法的性能问题?
monitor -c 5 demo.MathGame primeFactors