arthas official documentation: Arthas
Arthas usage steps (based on docker container)
Record some common command operations and continue to update them later.
Usage steps:
1. Enter the container
docker exec -it [容器名称] bash
2. Download arthas
curl -O https://arthas.aliyun.com/arthas-boot.jar
3. Start arthas
java -jar arthas-boot.jar
输入 java PID 回车(可通过 jps 查看)
4. Method calls of the monitoring class, the role of the -x [N] parameter: specify the depth of the traversal -n [N] represents the number of monitoring calls
watch [类的全路径名] [要监测的方法名] -x 2 -n 1
5. Static method return value
ognl @java.lang.System@currentTimeMillis() -x 1
6. Record the environment scene of each call of the com.rank.vault.users.application.impl.AuthMethodServiceImpl.findAllBy method
tt -t [类的全路径名] [要监测的方法名]
7. List all existing records
tt -l
8. For information about a specific time slice, you can view its detailed information through the -i parameter followed by the corresponding INDEX number.
tt -i 1000
9. Methods to decompile classes
jad [类的全路径名] [要监测的方法名]
10. Use watch to observe method input parameters, output parameters, class object member variable values, and exception information.
watch [类的全路径名] [要监测的方法名] '{params,returnObj,target,throwExp}' -x 4
params: method input parameters
returnObj: method return value
target: member attribute value of the class
throwExp: thrown exception information
11. How to use Arthas to modify the online environment source code for debugging:
// 背景,线上环境出问题,想排查问题,比如添加一些日志,比如修改之后,想自测一下
1.读取 .class 文件在 JVM 中类加载的 hash 值
sc -d *Test* 查询要修改的类在jvm 中类加载的 hash 值,比如为 31221be2,该值有用;
2.使用 jad 命令将 .class 文件反编译为 .java 文件
jad -c 31221be2 --source-only 要反编译的类 > 反编译之后的文件路径,比如
jad -c 31221be2 --source-only com.example.Test > /var/log/Test.java
3.修改 java 文件
4.使用 mc 命令将 .java 文件编译成.class文件
mc -c 31221be2 -d /var/log /var/log/Test.java
注意:直接编译反编译的 .java 文件,可能会报一些错误,比如 List 泛型擦除之后的类型不对的问题,可以采取直接修改我们代码中的文件,然后将其放到反编译的 .java 的文件路径处,再做编译操作;
5.使用 redefine 命令,将 .class 文件重新加载进 JVM
redefine -c 31221be2 /var/log/com/example/Test.class