Skip to main content

arthas

curl -O https://arthas.aliyun.com/arthas-boot.jar

# web console http://127.0.0.1:8563
# --tunnel-server 'ws://127.0.0.1:7777/ws'
java -jar arthas-boot.jar -h # 帮助
java -jar arthas-boot.jar 1 # 交互式命令行 - Docker 里 pid 为 1

# Alpine
curl -O https://arthas.aliyun.com/arthas-boot.jar
touch /proc/1/cwd/.attach_pid1 && kill -SIGQUIT 1 && sleep 2 && ls /proc/1/root/tmp/.java_pid$1
java -jar arthas-boot.jar 1
sc -d com.wener.Application
vmtool --action getInstances --className java.lang.String --limit 10

常用

# 观察 Spring HTTP 请求的响应时间
watch org.springframework.web.servlet.DispatcherServlet doService '{params[0].getRequestURI()+" "+ #cost}' -n 5 -x 3 '#cost>1'

# 观察 Spring 响应头信息
watch org.springframework.web.servlet.DispatcherServlet doService '{params[0].getRequestURI()+" header="+params[1].getHeaders("trace-id")}' -n 10 -x 3 -f

# 观察 MyBatis SQL
watch org.apache.ibatis.mapping.BoundSql getSql '{params,returnObj,target.parameterObject,throwExp}' -n 5 -x 3

# JDBC
watch java.sql.Connection prepareStatement '{params,throwExp}' -n 5 -x 3 'clazz.getName().startsWith("com.mysql") and params.length==1' and #cost>1

watch java.sql.Connection prepareStatement '{params,throwExp}' -n 5 -x 3

tt -t org.springframework.data.redis.connection.jedis.JedisConnectionFactory getConnection
tt -t org.springframework.data.redis.cache.DefaultRedisCacheWriter get

# Controller 方法内不慢,但整体慢时,排查请求方法前的逻辑
# getMethodArgumentValues 慢说明注解校验问题
# doInvoke 慢说明是方法慢
trace org.springframework.web.method.support.InvocableHandlerMethod invokeForRequest

Commands

  • tt - TimeTunnel - 记录现场,通过 index 回查
    • -l list
    • -s search, Advice 对象
    • -i INDEX 回查
  • trace - 跟踪向下调用方法时间消耗
  • stack - 获取调用链 - 尝试往上排查慢的节点
trace demo.MathGame run

redefine

# 生成源码
jad --source-only com.example.demo.arthas.user.UserController > /tmp/UserController.java
# 编译
mc /tmp/UserController.java -d /tmp
# 加载
redefine /tmp/com/example/demo/arthas/user/UserController.class
  • 不支持新方法新字段

Arthas Tunnel

java -jar arthas-tunnel-server.jar

Spring Boot Starter

<dependency>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-spring-boot-starter</artifactId>
<version>${arthas.version}</version>
</dependency>
arthas.agent-id=XXX
arthas.tunnel-server=ws://127.0.0.1:7777/ws

FAQ