GenweiWu / Blog

个人技术能力提升
MIT License
4 stars 0 forks source link

arthas #79

Open GenweiWu opened 2 years ago

GenweiWu commented 2 years ago

软件安装

# java -jar arthas-boot.jar --repo-mirror aliyun --use-http
[INFO] arthas-boot version: 3.5.4
[INFO] Found existing java process, please choose one and input the serial number of the process, eg : 1. Then hit ENTER.
* [1]: 39407 math-game.jar
1
[ERROR] Can not read arthas version from: https://arthas.aliyun.com/api/latest_version
[ERROR] Can not find Arthas under local: /root/.arthas/lib and remote repo mirror: aliyun
[ERROR] Unable to download arthas from remote server, please download the full package according to wiki: https://github.com/alibaba/arthas

## 规避方法
## 参考:https://github.com/alibaba/arthas/blob/master/README_CN.md
curl -L https://arthas.aliyun.com/install.sh -k | sh

## run
java -jar arthas-boot.jar
# 已经启动可以直接连接 
telnet localhost 3658

入门

https://arthas.aliyun.com/doc/quick-start.html

dashboard查看所有进程
thread 1查看具体线程
jad demo.MathGame 反编译
watch demo.MathGame primeFactors returnObj 观察方法的返回值

docker中

## docker exec 进去
curl -L https://arthas.aliyun.com/install.sh -k | sh

## 需要安装jdk
yum install -y \
   java-1.8.0-openjdk \
   java-1.8.0-openjdk-devel

## 临时用一下 
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk/
export JAVA_HOME

命令

https://arthas.aliyun.com/doc/thread.html


## 查看对应状态的线程
thread --state RUNNABLE

sc

search class 查找所有JVM已经加载到的类。

sc javax.servlet.Filter sc exampleController*

sm

search method,查找类的方法

sm java.lang.String java.lang.String ([BII)V ... java.lang.String equals(Ljava/lang/Object;)Z java.lang.String toString()Ljava/lang/String; java.lang.String hashCode()I java.lang.String compareTo(Ljava/lang/Object;)I java.lang.String indexOf(Ljava/lang/String;I)I

watch监听异常信息,和对应的参数

watch 类 方法(支持通配符) ongl表达式

watch com.example.demo.arthas.user.UserController * '{params,throwExp}'

x 2表示展示具体信息到2层,默认1层

watch com.example.demo.arthas.user.UserController * '{params,throwExp}' -x 2

watch支持条件表达式,

比如限制参数值大于100

watch com.example.demo.arthas.user.UserController '{params,throwExp}' 'params[0]>100' watch com.example.demo.arthas.user.UserController '{params,throwExp}' 'params[0]>100' -x 2

-e,捕获异常时

watch com.example.demo.arthas.user.UserController * '{params,throwExp}' -e

cost>10'时间超过一定阈值时

watch com.example.demo.arthas.user.UserController * '{params,throwExp}' '#cost>1'


> 热修改代码
>
> https://arthas.aliyun.com/doc/arthas-tutorials.html?language=cn&id=arthas-advanced

```bash
## 反编译
jad --source-only com.example.demo.arthas.user.UserController > UserController.java
## 然后修改文件
vim UserController.java

## sc查看类的加载器
## -d可以输出类的详细信息,包括类加载器
sc -d com.example.demo.arthas.user.UserController

## mc(memory compile)编译得到新的class文件
mc --classLoaderClass org.springframework.boot.loader.LaunchedURLClassLoader UserController.java

## redefine热加载
redefine /tmp/com/example/demo/arthas/user/UserController.class

参考

GenweiWu commented 1 year ago

查看方法的返回值

watch demo.MathGame primeFactors returnObj 
# 通过-x来看更详细,值越大越详细
watch demo.MathGame primeFactors returnObj -x 10 
GenweiWu commented 2 months ago

ognl

1. 获取静态类的静态字段

ognl '@com.njust.TestConstants@XXX_URL'
@String[/api/xxx/{id}]
GenweiWu commented 2 months ago

jad反编译

jad java.lang.String