编辑
2019-12-19
编程
00

目录

arthas 快速入门
1.启动
2.简单使用
2.1 监控调用路径耗时
2.2 修改日志级别
2.3 获取 ApplicationContext 为所欲为
2.4 ognl
3.侵入原理

arthas 快速入门

1.启动

  • java -jar arthas-boot.jar
bat
D:\software\arthas-packaging-3.1.0-bin>java -jar arthas-boot.jar [INFO] arthas-boot version: 3.1.0 [INFO] Found existing java process, please choose one and hit RETURN. * [1]: 5924 [2]: 11560 org.jetbrains.idea.maven.server.RemoteMavenServer [3]: 1580 com.yui.demo.arthas.ArthasApplication

选择一个进程启动 arthas

bash
D:\software\arthas-packaging-3.1.0-bin>java -jar arthas-boot.jar [INFO] arthas-boot version: 3.1.0 [INFO] Found existing java process, please choose one and hit RETURN. * [1]: 5924 [2]: 11560 org.jetbrains.idea.maven.server.RemoteMavenServer [3]: 1580 com.yui.demo.arthas.ArthasApplication 3 [INFO] arthas home: D:\software\arthas-packaging-3.1.0-bin [INFO] Try to attach process 1580 [INFO] Found java home from System Env JAVA_HOME: D:\software\JDK\jdk1.8.0_111 [INFO] Attach process 1580 success. [INFO] arthas-client connect 127.0.0.1 3658 ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.1.0 pid: 1580 time: 2019-12-16 10:01:44

2.简单使用

官方文档链接: https://alibaba.github.io/arthas/

详细使用请查看官方文档。

2.1 监控调用路径耗时

trace ['#cost > time'] [-n times]

  • package: 包名
  • method: 方法名
  • #cost: 过滤时间,如 '#cost > 10'
  • -n: 指定监控次数
bash
$ trace -j com.yui.demo.arthas.controller.StudyController addStudent -n 1 Press Q or Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 49 ms. `---ts=2019-12-16 11:27:31;thread_name=http-nio-8081-exec-8;id=29;is_daemon=true;priority=5;TCCL=org.springframework.boot.web.embedded.tomcat.TomcatEmbeddedWebappClassLoader@5eb2172 `---[300.923668ms] com.yui.demo.arthas.controller.StudyController:addStudent() +---[300.041455ms] com.yui.demo.arthas.service.ArthasService:getSuccess() `---[0.554227ms] com.yui.demo.arthas.service.ArthasService:addStudent() Command execution times exceed limit: 1, so command will exit. You can set it wi th -n option.

2.2 修改日志级别

logger --name ROOT --level debug

bash
[arthas@9984]$ logger name ROOT class ch.qos.logback.classic.Logger classLoader sun.misc.Launcher$AppClassLoader@18b4aac2 classLoader 18b4aac2 Hash level INFO effectiveLe INFO vel additivity true codeSource file:/D:/software/apache-maven-3.5.4/.m2/ch/qos/logback/logback- classic/1.2.3/logback-classic-1.2.3.jar appenders name CONSOLE class ch.qos.logback.core.ConsoleAppender classLoader sun.misc.Launcher$AppClassLoader@18b4aac2 classLoaderHash 18b4aac2 target System.out [arthas@9984]$ logger --name ROOT --level debug update logger level success.

2.3 获取 ApplicationContext 为所欲为

tt -t [params]

记录 package.method 的执行情况 (params==n 指定重载函数,如 params.length==1,‘params[1] instanceof Integer’)

  • 返回参数
表格字段字段解释
INDEX时间片段记录编号,每一个编号代表着一次调用,后续tt还有很多命令都是基于此编号指定记录操作,非常重要。
TIMESTAMP方法执行的本机时间,记录了这个时间片段所发生的本机时间
COST(ms)方法执行的耗时
IS-RET方法是否以正常返回的形式结束
IS-EXP方法是否以抛异常的形式结束
OBJECT执行对象的hashCode(),注意,曾经有人误认为是对象在JVM中的内存地址,但很遗憾他不是。但他能帮助你简单的标记当前执行方法的类实体
CLASS执行的类名
METHOD执行的方法名

tt -i

通过执行完 tt -t 记录的 INDEX 使用对象应用

  • 获取 ApplicationContext 并使用

    1. 记录 RequestMappingHandlerAdapter 对象引用
    2. 通过 tt -i 使用对象引用, -w 使用 ognl , target 为 arthas 表达式核心变量,指定为 本次调用类的实例
  • $ ognl '@com.alibaba.dubbo.config.spring.ServiceBean@getSpringContext()'

bash
[arthas@14420]$ tt -t org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter invokeHandlerMethod Press Q or Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 60 ms. IND TIMESTAMP COST IS- IS- OBJECT CLASS METHOD EX (ms) RET EXP ------------------------------------------------------------------------------- 100 2019-12-16 71.1 tru fal 0x77863 RequestMappingHan invokeHandlerMet 0 15:22:57 8253 e se 8ad dlerAdapter hod 5 [arthas@14420]$ tt -i 1000 -w 'target.getApplicationContext().getBean("arthasServiceImpl").showLog("test")' @String[success] Affect(row-cnt:1) cost in 6 ms. [arthas@14420]$ tt -i 1000 -w 'target.getApplicationContext().getBean("arthasServiceImpl").addStudent(new com.yui.demo.arthas.dto.Student().setName("test01" ).setAge(14).setClassId({1,4,8}))' @CopyOnWriteArrayList[ @Student[Student(name=小江, age=15, classId=[1, 2, 5])], @Student[Student(name=小红, age=13, classId=[1, 2, 5])], @Student[Student(name=test01, age=14, classId=[1, 4, 8])], ] Affect(row-cnt:1) cost in 38 ms.

2.4 ognl

  • 静态方法:

    @Class@method(args)

  • 静态属性:

    @Class@field

  • map:

    #{k1,v1,......}

  • List:

    {v1,v2.....}

  • 新建对象:

    new package.ClassName()

  • 调用非静态方法:

    object.method(args)

bash
[arthas@14420]$ ognl '@com.yui.demo.arthas.service.impl.ArthasServiceImpl@show()' null [arthas@14420]$ ognl '@java.lang.System@currentTimeMillis()' @Long[1576565975950] [arthas@14420]$ ognl '#s1=new com.yui.demo.arthas.dto.Student(),#s1.setName("test01").setAge(14).setClassId({1,4,8}),#s1.toString()' @String[Student(name=test01, age=14, classId=[1, 4, 8])]

3.侵入原理

  • java agent

本文作者:Yui_HTT

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!