Arthas 是 阿里巴巴最近开源出来的一个针对 java 的工具,主要是针对 java 的问题进行诊断!html
这个工具能够协助你作下面这些事情:java
下载地址:http://search.maven.org/classic/#search%7Cga%7C1%7Cg%3A%22com.taobao.arthas%22%20AND%20a%3A%22arthas-packaging%22git
下载完成后,解压缩,以下图所示::github
在Download
栏下载最新的 bin.zip
包,解压后在bin目录有 as.bat
。此脚本暂时只接受一个参数 pid,即只能诊断本机上的 Java 进程。web
启动命令为:express
as.bat <pid>apache
注:我在 window 10 上面启动的时候遇到以下问题,tomcat
D:\download\arthas-packaging-3.0.4-bin>telnet
'telnet' 不是内部或外部命令,也不是可运行的程序
或批处理文件。bash
解决办法为:“控制面板” ——> “启动或关闭Windows功能” ——> 勾选 “Telnet 功能”websocket
安装Arthas:
curl -L https://alibaba.github.io/arthas/install.sh | sh
启动Arthas:
./as.sh
成功启动后,会看到以下界面。
help——查看命令帮助信息
cls——清空当前屏幕区域
session——查看当前会话的信息
reset——重置加强类,将被 Arthas 加强过的类所有还原,Arthas 服务端关闭时会重置全部加强过的类
version——输出当前目标 Java 进程所加载的 Arthas 版本号
quit——退出当前 Arthas 客户端,其余 Arthas 客户端不受影响
shutdown——关闭 Arthas 服务端,全部 Arthas 客户端所有退出
keymap——Arthas快捷键列表及自定义快捷键
dashboard——当前系统的实时数据面板
thread——查看当前 JVM 的线程堆栈信息
jvm——查看当前 JVM 的信息
sysprop——查看和修改JVM的系统属性
New! getstatic——查看类的静态属性
sc——查看JVM已加载的类信息
sm——查看已加载类的方法信息
dump——dump 已加载类的 byte code 到特定目录
redefine——加载外部的.class文件,redefine到JVM里
jad——反编译指定已加载类的源码
classloader——查看classloader的继承树,urls,类加载信息,使用classloader去getResource
请注意,这些命令,都经过字节码加强技术来实现的,会在指定类的方法中插入一些切面来实现数据统计和观测,所以在线上、预发使用时,请尽可能明确须要观测的类、方法以及条件,诊断结束要执行 shutdown 或将加强过的类执行 reset 命令。
monitor——方法执行监控
watch——方法执行数据观测
trace——方法内部调用路径,并输出方法路径上的每一个节点上耗时
stack——输出当前方法被调用的调用路径
tt——方法执行数据的时空隧道,记录下指定方法每次调用的入参和返回信息,并能对这些不一样的时间下调用进行观测
options——查看或设置Arthas全局开关
Arthas支持使用管道对上述命令的结果进行进一步的处理,如sm org.apache.log4j.Logger | grep
grep——搜索知足条件的结果
plaintext——将命令的结果去除颜色
wc——按行统计输出结果
经过websocket链接Arthas。
Web Console
异步命令支持
执行结果存日志
批处理的支持
ognl表达式的用法说明
首先,在窗口中,输入 help 查看一下全部提供的可用命令(他的通讯本质是经过 telnet 协议来通讯的),以下图:
Attach success. Connecting to arthas server... current timestamp is 1537266148 Trying 127.0.0.1... Connected to 127.0.0.1. Escape character is '^]'. ,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.0.4 pid: 25206 timestamp: 1537266148841 $ help NAME DESCRIPTION help Display Arthas Help keymap Display all the available keymap for the specified connection. sc Search all the classes loaded by JVM sm Search the method of classes loaded by JVM classloader Show classloader info jad Decompile class getstatic Show the static field of a class monitor Monitor method execution statistics, e.g. total/success/failure count, average rt, fail rate, etc. stack Display the stack trace for the specified class and method thread Display thread info, thread stack trace Trace the execution time of specified method invocation. watch Display the input/output parameter, return object, and thrown exception of specified method invocation tt Time Tunnel jvm Display the target JVM information dashboard Overview of target jvm's thread, memory, gc, vm, tomcat info. dump Dump class byte array from JVM options View and change various Arthas options cls Clear the screen reset Reset all the enhanced classes version Display Arthas version shutdown Shut down Arthas server and exit the console session Display current session information sysprop Display, and change the system properties. redefine Redefine classes. @see Instrumentation#redefineClasses(ClassDefinition...) $
这里主要说一下 watch ,这个命令对变量进行数据监测。
==========================================================================
首先贴上个人测试代码:
package com.oct.tail; import java.util.UUID; /** * @Author Ryan * @Date 2018/9/18 9:58 * @desc */ public class OtherTestCase { /** * * @return */ public static String uuid(){ return UUID.randomUUID().toString().replaceAll("-", ""); } public static void main(String[] args) { while(true){ System.out.println("uuid = " + uuid()); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
以下我作一个示例 ( 本人是基于 Window 10 , JDK 8 环境, Linux 雷同),(对于 watch 命令我伪装不知道如何使用,当即输入 watch help 来看看会有什么玩意儿):
,---. ,------. ,--------.,--. ,--. ,---. ,---. / O \ | .--. ''--. .--'| '--' | / O \ ' .-' | .-. || '--'.' | | | .--. || .-. |`. `-. | | | || |\ \ | | | | | || | | |.-' | `--' `--'`--' '--' `--' `--' `--'`--' `--'`-----' wiki: https://alibaba.github.io/arthas version: 3.0.4 pid: 11924 timestamp: 1537326702039 $ watch -help USAGE: watch [-b] [-e] [-x <value>] [-f] [-h] [-n <value>] [-E] [-M <value>] [-s] class- pattern method-pattern express [condition-express] SUMMARY: Display the input/output parameter, return object, and thrown exception of specif ied method invocation The express may be one of the following expression (evaluated dynamically): target : the object clazz : the object's class method : the constructor or method params[0..n] : the parameters of method returnObj : the returned object of method throwExp : the throw exception of method isReturn : the method ended by return isThrow : the method ended by throwing exception #cost : the execution time in ms of method invocation Examples: watch -Eb org\.apache\.commons\.lang\.StringUtils isBlank params[0] watch -b org.apache.commons.lang.StringUtils isBlank params[0] watch -f org.apache.commons.lang.StringUtils isBlank returnObj watch -bf *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] watch *StringUtils isBlank params[0] params[0].length==1 watch *StringUtils isBlank '#cost>100' WIKI: https://alibaba.github.io/arthas/watch OPTIONS: -b, --before Watch before invocation -e, --exception Watch after throw exception -x, --expand <value> Expand level of object (1 by default) -f, --finish Watch after invocation, enable by default -h, --help this help -n, --limits <value> Threshold of execution times -E, --regex Enable regular expression to match (wildcard matching b y default) -M, --sizeLimit <value> Upper size limit in bytes for the result (10 * 1024 * 1 024 by default) -s, --success Watch after successful invocation <class-pattern> The full qualified class name you want to watch <method-pattern> The method name you want to watch <express> the content you want to watch, written by ognl. Examples: params[0] 'params[0]+params[1]' returnObj throwExp target clazz method <condition-express> Conditional expression in ognl style, for example: TRUE : 1==1 TRUE : true FALSE : false TRUE : 'params.length>=0' FALSE : 1==2 $
在这里,咱们针对方法 uuid() 返回值进行监测。监测结果以下:
$ $ $ watch -f com.oct.tail.OtherTestCase uuid returnObj Press Ctrl+C to abort. Affect(class-cnt:1 , method-cnt:1) cost in 18 ms. ts=2018-09-19 11:13:48;result=@String[26c80eb505664dbcb14f8d810fb4811c] ts=2018-09-19 11:13:49;result=@String[fc03c43864f94372b646ce6253d90646] ts=2018-09-19 11:13:50;result=@String[55ff41e0d66347c2bc75ab8ff4ffda4e] ts=2018-09-19 11:13:51;result=@String[c504388c0aa74458a41a1b3a77c3d536] ts=2018-09-19 11:13:52;result=@String[18d59c09ffde4c7aab15feb88b3e433f] ts=2018-09-19 11:13:53;result=@String[c19dd8c1e5f8442696c8f886e81e74d5] ts=2018-09-19 11:13:54;result=@String[d37a74aa502f4897aa1ed84dc69b83d8] ts=2018-09-19 11:13:55;result=@String[cc11753b6f424c1e9a6a1ab36f334349] ts=2018-09-19 11:13:56;result=@String[75a9b3c0bed4426d9363168912f16d74] ts=2018-09-19 11:13:57;result=@String[f13022118e5a4115800a6eacc480e6a8]
简直方便的死去活来!太感动了。
总结,这个工具不止这里介绍的功能,还有其余的用法及命令,详细信息, 请移步官网(https://alibaba.github.io/arthas/sm.html)