Arthas常用命令

Arthas常用命令

安装

  1. 安装 curl -O https://arthas.aliyun.com/arthas-boot.jar
  2. 启动 java -jar arthas-boot.jar

常用命令

  1. jad + 全类名 + 方法名(可选),反编译代码

    jad com.bsj.studentcard.location.service.impl.WeatherServiceImpl

  2. watch+全类名+方法名,监听返回值

    watch com.bsj.studentcard.location.config.forest.ForestWeatherServiceImpl getWeather -bf -x 2

    参数名称 参数说明
    class-pattern 类名表达式匹配
    method-pattern 函数名表达式匹配
    express 观察表达式,默认值:{params, target, returnObj}
    condition-express 条件表达式
    [b] 函数调用之前观察
    [e] 函数异常之后观察
    [s] 函数返回之后观察
    [f] 函数结束之后(正常返回和异常返回)观察 f=e+s
    [E] 开启正则表达式匹配,默认为通配符匹配
    [x:] 指定输出结果的属性遍历深度,默认为 1,最大值是 4
  3. stack +全类名+方法名,输出当前方法调用栈,确定该方法是被谁调用的

    stack com.bsj.studentcard.location.service.impl.WeatherServiceImpl getWeather

  4. 在线更新代码

    1. 将代码反编译到某个目录

      jad --source-only com.bsj.studentcard.location.service.impl.WeatherServiceImpl > /tmp/WeatherServiceImpl.java

    2. vim修改代码后通过mc命令编译为class文件

      mc -d /tmp/output /tmp/WeatherServiceImpl.java

    3. 通过retransform命令加载class文件到JVM内存

      retransform /tmp/output/com/bsj/studentcard/location/service/impl/WeatherServiceImpl.class

    ps:

    如果不清除掉所有的 retransform entry,并重新触发 retransform ,则 arthas stop 时,retransform 过的类仍然生效。重启服务则失效

  5. retransform使用

    1. retransform /tmp/output/com/bsj/studentcard/location/service/impl/WeatherServiceImpl.class 加载指定类到JVM
    2. retransform -l 查看retransform entry
    3. retransform -d 1 删除指定retransform entry,1是entry的编号
    4. retransform –deleteAll 删除所有retransform entry
  6. 上传 .class 文件到服务器的技巧

    使用mc命令来编译jad的反编译的代码有可能失败。可以在本地修改代码,编译好后再上传到服务器上。有的服务器不允许直接上传文件,可以使用base64命令来绕过。

    在本地先转换.class文件为 base64,再保存为 result.txt

    1
    base64 < Test.class > result.txt

    到服务器上,新建并编辑result.txt,复制本地的内容,粘贴再保存

    把服务器上的 result.txt还原为.class

    1
    base64 -d < result.txt > Test.class

    用 md5 命令计算哈希值,校验是否一致

  7. 查看JVM内存对象

    1. 查看内存对象

      vmtool --action getInstances --className java.lang.String --limit 10'

    2. 强制GC

      vmtool --action forceGc

线程

thread

参数名称 参数说明
id 线程 id
[n:] 指定最忙的前 N 个线程并打印堆栈
[b] 找出当前阻塞其他线程的线程
[i <value>] 指定 cpu 使用率统计的采样间隔,单位为毫秒,默认值为 200
[–all] 显示所有匹配的线程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 查看所有线程 不带参数时查看的是第一页的线程
thread --all

# 查看最忙的3个线程
thread -n 3

# 查看指定线程 148为线程ID
thread 148

# 查看当前阻塞其他线程的线程 目前只支持找出 synchronized 关键字阻塞住的线程, 如果是java.util.concurrent.Lock, 目前还不支持
thread -b

# 统计近1分钟内的线程CPU使用率
thread -i 6000

# 查询指定状态的线程
thread --state WAITING

日志

1
2
# 查看logger信息
logger

logger信息输出如下:

image-20220916151517598

1
2
3
4
# 设置日志级别
# -c + classloader的ID
# --name + name
logger -c 2a139a55 --name ROOT --level debug

Profiler

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 开始采样
profiler start

# 获取已经采样的数量
profiler getSamples

# 查询profiler状态
profiler status

# 停止采样并输出
profiler stop --format html

# 查看路径 例如
http://localhost:8563/arthas-output/20220916-153518.html

Arthas常用命令
http://example.com/2023/03/09/服务治理/Arthas/
作者
UncleBryan
发布于
2023年3月9日
许可协议