trace java_使用java动态字节码技术简单实现arthas的trace功能。
參考資料
用過[Arthas]的都知道,Arthas是alibaba開源的一個(gè)非常強(qiáng)大的Java診斷工具。
不管是線上還是線下,我們都可以用Arthas分析程序的線程狀態(tài)、查看jvm的實(shí)時(shí)運(yùn)行狀態(tài)、打印方法的出入?yún)⒑头祷仡愋?、收集方法中每個(gè)代碼塊耗時(shí),
甚至可以監(jiān)控類、方法的調(diào)用次數(shù)、成功次數(shù)、失敗次數(shù)、平均響應(yīng)時(shí)長、失敗率等。
前幾天學(xué)習(xí)java動(dòng)態(tài)字節(jié)碼技術(shù)時(shí),突然想起這款java診斷工具的trace功能:打印方法中每個(gè)節(jié)點(diǎn)的調(diào)用耗時(shí)。簡簡單單的,正好拿來做動(dòng)態(tài)字節(jié)碼入門學(xué)習(xí)的demo。
程序結(jié)構(gòu)
src
├── agent-package.bat
├── java
│ ├── asm
│ │ ├── MANIFEST.MF
│ │ ├── TimerAgent.java
│ │ ├── TimerAttach.java
│ │ ├── TimerMethodVisitor.java
│ │ ├── TimerTrace.java
│ │ └── TimerTransformer.java
│ └── demo
│ ├── MANIFEST.MF
│ ├── Operator.java
│ └── Test.java
├── run-agent.bat
├── target-package.bat
└── tools.jar
編寫目標(biāo)程序
代碼
package com.gravel.demo.test.asm;
/**
* @Auther: syh
* @Date: 2020/10/12
* @Description:
*/
public class Test {
public static boolean runnable = true;
public static void main(String[] args) throws Exception {
while (runnable) {
test();
}
}
// 目標(biāo):分析這個(gè)方法中每個(gè)節(jié)點(diǎn)的耗時(shí)
public static void test() throws Exception {
Operator.handler();
long time_wait = (long) ((Math.random() * 1000) + 2000);
Operator.callback();
Operator.pause(time_wait);
}
}
Operator.java
/**
* @Auther: syh
* @Date: 2020/10/28
* @Description: 輔助類,同樣可用于分析耗時(shí)
*/
public class Operator {
public static void handler() throws Exception {
long time_wait = (long) ((Math.random() * 10) + 20);
sleep(time_wait);
}
public static void callback() throws Exception {
long time_wait = (long) ((Math.random() * 10) + 20);
sleep(time_wait);
}
public static void pause(long time_wait) throws Exception {
sleep(time_wait);
}
public static void stop() throws Exception {
Test.runnable = false;
System.out.println("business stopped.");
}
private static void sleep(long time_wait) throws Exception {
Thread.sleep(time_wait);
}
}
MANIFEST.MF
編寫MANIFEST.MF文件,指定main-class。注意:冒號(hào)后面加空格,結(jié)尾加兩行空白行。
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: syh
Created-By: Apache Maven
Build-Jdk: 1.8.0_202
Main-Class: com.gravel.demo.test.asm.Target
打包
偷懶寫了bat批命令,生成target.jar
@echo off & setlocal
attrib -s -h -r -a /s /d demo
rd /s /q demo
rd /q target.jar
javac -encoding utf-8 -d . ./java/demo/*.java
jar cvfm target.jar ./java/demo/MANIFEST.MF demo
rd /s /q demo
pause
java -jar target.jar
java agent探針
instrument 是 JVM 提供的一個(gè)可以修改已加載類文件的類庫。而要實(shí)現(xiàn)代碼的修改,我們需要實(shí)現(xiàn)一個(gè) instrument agent。
jdk1.5時(shí),agent有個(gè)內(nèi)定方法premain。是在類加載前修改。所以無法做到修改正在運(yùn)行的類。
jdk1.6后,agent新增了agentmain方法。agentmain是在虛擬機(jī)啟動(dòng)以后加載的。所以可以做攔截、熱部署等。
講JAVA探針技術(shù),實(shí)際上我自己也是半吊子。所以這里用的是邊分析別人例子邊摸索的思路來實(shí)現(xiàn)我的簡單的trace功能。
例子使用的是ASM字節(jié)碼生成框架
MANIFEST.MF
首先一個(gè)可用的jar,關(guān)鍵之一是MAINFEST.MF文件是吧。
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Created-By: Apache Maven
Built-By: syh
Build-Jdk: 1.8.0_202
Agent-Class: asm.TimerAgent
Can-Retransform-Classes: true
Can-Redefine-Classes: true
Class-Path: ./tools.jar
Main-Class: asm.TimerAttach
我們從MANIFEST.MF中提取幾個(gè)關(guān)鍵的屬性
屬性
說明
Agent-Class
agentmain入口類
Premain-Class
premain入口類,與agent-class至少指定一個(gè)。
Can-Retransform-Classes
對于已經(jīng)加載的類重新進(jìn)行轉(zhuǎn)換處理,即會(huì)觸發(fā)重新加載類定義。
Can-Redefine-Classes
對已經(jīng)加載的類不做轉(zhuǎn)換處理,而是直接把處理結(jié)果(bytecode)直接給JVM
Class-Path
asm動(dòng)態(tài)字節(jié)碼技術(shù)依賴tools.jar,如果沒有可以從jdk的lib目錄下拷貝。
Main-Class
這里并不是agent的關(guān)鍵屬性,為了方便,我把加載虛擬機(jī)的程序和agent合并了。
代碼
然后我們來看看兩個(gè)入口類,首先分析一個(gè)可執(zhí)行jar的入口類Main-Class。
public class TimerAttach {
public static void main(String[] args) throws Exception {
/**
* 啟動(dòng)jar時(shí),需要指定兩個(gè)參數(shù):1目標(biāo)程序的pid。 2 要修改的類路徑及方法,格式 package.class#methodName
*/
if (args.length < 2) {
System.out.println("pid and class must be specify.");
return;
}
if (!args[1].contains("#")) {
System.out.println("methodName must be specify.");
return;
}
VirtualMachine vm = VirtualMachine.attach(args[0]);
// 這里為了方便我把 vm和agent整合在一個(gè)jar里面了, args[1]就是agentmain的入?yún)ⅰ?/p>
vm.loadAgent("agent.jar", args[1]);
}
}
代碼很簡單,1:args入?yún)⑿r?yàn);2:加載目標(biāo)進(jìn)程pid(args[0]);3:加載agent jar包(因?yàn)楹喜⒘?#xff0c;所以這個(gè)jar其實(shí)就是自己)。
其中vm.loadAgent(agent.jar, args[1])會(huì)調(diào)用agent-class中的agentmain方法,而args[1]就是agentmain的第一個(gè)入?yún)ⅰ?/p>
public class TimerAgent {
public static void agentmain(String agentArgs, Instrumentation inst) {
String[] ownerAndMethod = agentArgs.split("#");
inst.addTransformer(new TimerTransformer(ownerAndMethod[1]), true);
try {
inst.retransformClasses(Class.forName(ownerAndMethod[0]));
System.out.println("agent load done.");
} catch (Exception e) {
e.printStackTrace();
System.out.println("agent load failed!");
}
}
}
在 agentmain 方法里,我們調(diào)用retransformClassess方法載入目標(biāo)類,調(diào)用addTransformer方法加載TimerTransformer類實(shí)現(xiàn)對目標(biāo)類的重新定義。
類轉(zhuǎn)換器
public class TimerTransformer implements ClassFileTransformer {
private String methodName;
public TimerTransformer(String methodName) {
this.methodName = methodName;
}
@Override
public byte[] transform(ClassLoader loader, String className, Class> classBeingRedefined, ProtectionDomain protectionDomain, byte[] classFileBuffer) {
ClassReader reader = new ClassReader(classFileBuffer);
ClassWriter classWriter = new ClassWriter(ClassWriter.COMPUTE_MAXS);
ClassVisitor classVisitor = new TimerTrace(Opcodes.ASM5, classWriter, methodName);
reader.accept(classVisitor, ClassReader.EXPAND_FRAMES);
return classWriter.toByteArray();
}
}
對被匹配到的類中的方法進(jìn)行修改
public class TimerTrace extends ClassVisitor implements Opcodes {
private String owner;
private boolean isInterface;
private String methodName;
public TimerTrace(int i, ClassVisitor classVisitor, String methodName) {
super(i, classVisitor);
this.methodName = methodName;
}
@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
owner = name;
isInterface = (access & ACC_INTERFACE) != 0;
}
@Override
public MethodVisitor visitMethod(int access, String name, String descriptor, String signature,
String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
// 匹配到指定methodName時(shí),進(jìn)行字節(jié)碼修改
if (!isInterface && mv != null && name.equals(methodName)) {
// System.out.println(" package.className:methodName()")
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "", "()V", false);
mv.visitLdcInsn(" " + owner.replace("/", ".")
+ ":" + methodName + "() ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
// 方法代碼塊耗時(shí)統(tǒng)計(jì)并打印
TimerMethodVisitor at = new TimerMethodVisitor(owner, access, name, descriptor, mv);
return at.getLocalVariablesSorter();
}
return mv;
}
public static void main(String[] args) throws IOException {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);
TraceClassVisitor tv = new TraceClassVisitor(cw, new PrintWriter(System.out));
TimerTrace addFiled = new TimerTrace(Opcodes.ASM5, tv, "test");
ClassReader classReader = new ClassReader("demo.Test");
classReader.accept(addFiled, ClassReader.EXPAND_FRAMES);
File file = new File("out/production/asm-demo/demo/Test.class");
String parent = file.getParent();
File parent1 = new File(parent);
parent1.mkdirs();
file.createNewFile();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(cw.toByteArray());
}
}
要統(tǒng)計(jì)方法中每行代碼耗時(shí),只需要在每一行代碼的前后加上當(dāng)前時(shí)間戳然后相減即可。
所以我們的代碼是這么寫的。
public class TimerMethodVisitor extends MethodVisitor implements Opcodes {
private int start;
private int end;
private int maxStack;
private String lineContent;
public boolean instance = false;
private LocalVariablesSorter localVariablesSorter;
private AnalyzerAdapter analyzerAdapter;
public TimerMethodVisitor(String owner, int access, String name, String descriptor, MethodVisitor methodVisitor) {
super(Opcodes.ASM5, methodVisitor);
this.analyzerAdapter = new AnalyzerAdapter(owner, access, name, descriptor, this);
localVariablesSorter = new LocalVariablesSorter(access, descriptor, this.analyzerAdapter);
}
public LocalVariablesSorter getLocalVariablesSorter() {
return localVariablesSorter;
}
/**
* 進(jìn)入方法后,最先執(zhí)行
* 所以我們可以在這里定義一個(gè)最開始的時(shí)間戳, 然后創(chuàng)建一個(gè)局部變量var_end
* Long var_start = System.nanoTime();
* Long var_end;
*/
@Override
public void visitCode() {
mv.visitCode();
mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
start = localVariablesSorter.newLocal(Type.LONG_TYPE);
mv.visitVarInsn(ASTORE, start);
end = localVariablesSorter.newLocal(Type.LONG_TYPE);
maxStack = 4;
}
/**
* 在每行代碼后面增加以下代碼
* var_end = System.nanoTime();
* System.out.println("[" + String.valueOf((var_end.doubleValue() - var_start.doubleValue()) / 1000000.0D) + "ms] " + "package.className:methodName() #lineNumber");
* var_start = var_end;
* @param lineNumber
* @param label
*/
@Override
public void visitLineNumber(int lineNumber, Label label) {
super.visitLineNumber(lineNumber, label);
if (instance) {
mv.visitMethodInsn(INVOKESTATIC, "java/lang/System", "nanoTime", "()J", false);
mv.visitMethodInsn(INVOKESTATIC, "java/lang/Long", "valueOf", "(J)Ljava/lang/Long;", false);
mv.visitVarInsn(ASTORE, end);
// System.out
mv.visitFieldInsn(GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;");
// new StringBuilder();
mv.visitTypeInsn(NEW, "java/lang/StringBuilder");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/StringBuilder", "", "()V", false);
mv.visitLdcInsn(" -[");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitVarInsn(ALOAD, end);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "doubleValue", "()D", false);
mv.visitVarInsn(ALOAD, start);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/Long", "doubleValue", "()D", false);
mv.visitInsn(DSUB);
mv.visitLdcInsn(new Double(1000 * 1000));
mv.visitInsn(DDIV);
// String.valueOf((end - start)/1000000)
mv.visitMethodInsn(INVOKESTATIC, "java/lang/String", "valueOf", "(D)Ljava/lang/String;", false);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
mv.visitLdcInsn("ms] ");
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
// .append("owner:methodName() #line")
mv.visitLdcInsn(this.lineContent + "#" + lineNumber);
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "append",
"(Ljava/lang/String;)Ljava/lang/StringBuilder;", false);
// stringBuilder.toString()
mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/StringBuilder", "toString", "()Ljava/lang/String;", false);
// println stringBuilder.toString()
mv.visitMethodInsn(INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false);
// start = end
mv.visitVarInsn(ALOAD, end);
mv.visitVarInsn(ASTORE, start);
maxStack = Math.max(analyzerAdapter.stack.size() + 4, maxStack);
}
instance = true;
}
/**
* 拼接字節(jié)碼內(nèi)容
* @param opcode
* @param owner
* @param methodName
* @param descriptor
* @param isInterface
*/
@Override
public void visitMethodInsn(int opcode, String owner, String methodName, String descriptor, boolean isInterface) {
super.visitMethodInsn(opcode, owner, methodName, descriptor, isInterface);
if (!isInterface && opcode == Opcodes.INVOKESTATIC) {
this.lineContent = owner.replace("/", ".")
+ ":" + methodName + "() ";
}
}
@Override
public void visitMaxs(int maxStack, int maxLocals) {
super.visitMaxs(Math.max(maxStack, this.maxStack), maxLocals);
}
}
如果初學(xué)者不會(huì)改字節(jié)碼??梢岳胕dea自帶的asm插件做參考。
打包
這樣,一個(gè)可執(zhí)行的agent jar就寫完了,然后打包
@echo off
attrib -s -h -r -a /s /d asm
rd /s /q asm
rd /q agent.jar
javac -XDignore.symbol.file=true -encoding utf-8 -d . ./java/asm/*.java
jar cvfm agent.jar ./java/asm/MANIFEST.MF asm
rd /s /q asm
exit
測試
運(yùn)行目標(biāo)程序 target.jar
java -jar target.jar
打印Test.test中每個(gè)節(jié)點(diǎn)耗時(shí)
java -jar agent.jar [pid] demo.Test#test
結(jié)果
打印Operator.handler方法每個(gè)節(jié)點(diǎn)耗時(shí)
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的trace java_使用java动态字节码技术简单实现arthas的trace功能。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql范式与反范式_MySQL 三种
- 下一篇: java线程6种状态转换,Java线程的