我的天,你工作5年了,连Java agent都不知道...下篇

老规矩,先看测试代码,测试代码很简单,每隔 100ms 运行一次 sayHi 方法,并随机随眠一段时间。

packageorg.xunche.app;publicclassHelloTraceAgent{publicstaticvoidmain(String[] args)throwsInterruptedException{HelloTraceAgent helloTraceAgent =newHelloTraceAgent();while(true) {helloTraceAgent.sayHi(“xunche”);Thread.sleep(100); } }publicStringsayHi(String name)throwsInterruptedException{ sleep();String hi ="hi, “+ name +”, "+ System.currentTimeMillis();returnhi; }publicvoidsleep()throwsInterruptedException{Thread.sleep((long) (Math.random() *200)); }}

接下看 agent 代码,思路同监控方法耗时差不多,在方法出口处,通过 asm 植入采集方法入参和返回值的代码,并通过 Sender 将信息通过 socket 发送到服务端,代码如下:

packageorg.xunche.agent;importjdk.internal.org.objectweb.asm.*;importjdk.internal.org.objectweb.asm.commons.AdviceAdapter;importjava.lang.instrument.ClassFileTransformer;importjava.lang.instrument.Instrumentation;importjava.lang.instrument.UnmodifiableClassException;importjava.security.ProtectionDomain;publicclassTraceAgent{publicstaticvoidagentmain(String args, Instrumentation instrumentation)throwsClassNotFoundException, UnmodifiableClassException{if(args ==null) {return; }intindex = args.lastIndexOf(".");if(index != -1) {String className = args.substring(0, index);String methodName = args.substring(index +1);//目标代码已经加载,需要重新触发加载流程,才会通过注册的转换器进行转换instrumentation.addTransformer(newTraceClassFileTransformer(className.replace(".","/"), methodName),true); instrumentation.retransformClasses(Class.forName(className)); } }publicstaticclassTraceClassFileTransformerimplementsClassFileTransformer{privateString traceClassName;privateString traceMethodName;publicTraceClassFileTransformer(String traceClassName, String traceMethodName){this.traceClassName = traceClassName;this.traceMethodName = traceMethodName; }@Overridepublicbyte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain,byte[] classfileBuffer) {//过滤掉Jdk、agent、非指定类的方法if(className.startsWith(“java”) || className.startsWith(“jdk”) || className.startsWith(“javax”) || className.startsWith(“sun”)|| className.startsWith(“com/sun”) || className.startsWith(“org/xunche/agent”) || !className.equals(traceClassName)) {//return null会执行原来的字节码returnnull; }ClassReader reader =newClassReader(classfileBuffer);ClassWriter writer =newClassWriter(reader, ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);reader.accept(newTraceVisitor(className, traceMethodName, writer), ClassReader.EXPAND_FRAMES);returnwriter.toByteArray(); } }publicstaticclassTraceVisitorextendsClassVisitor{privateString className;privateString traceMethodName;publicTraceVisitor(String className, String traceMethodName, ClassVisitor classVisitor){super(Opcodes.ASM5, classVisitor);this.className = className;this.traceMethodName = traceMethodName; }@OverridepublicMethodVisitorvisitMethod(intmethodAccess, String methodName, String methodDesc, String signature, String[] exceptions){ MethodVisitor methodVisitor = cv.visitMethod(methodAccess, methodName, methodDesc, signature, exceptions);if(traceMethodName.equals(methodName)) {returnnewTraceAdviceAdapter(className, methodVisitor, methodAccess, methodName, methodDesc); }returnmethodVisitor; } }privatestaticclassTraceAdviceAdapterextendsAdviceAdapter{privatefinalString className;privatefinalString methodName;privatefinalType[] methodArgs;privatefinalString[] parameterNames;privatefinalint[] lvtSlotIndex;protectedTraceAdviceAdapter(String className, MethodVisitor methodVisitor,intmethodAccess, String methodName, String methodDesc){super(Opcodes.ASM5, methodVisitor, methodAccess, methodName, methodDesc);this.className = className;this.methodName = methodName;this.methodArgs = Type.getArgumentTypes(methodDesc);this.parameterNames =newString[this.methodArgs.length];this.lvtSlotIndex = computeLvtSlotIndices(isStatic(methodAccess),this.methodArgs); }@OverridepublicvoidvisitLocalVariable(String name, String description, String signature, Label start, Label end,intindex){for(inti =0; i 0; } }}

以上就是 agent 的代码, onMethodExit 方法中的代码含义是获取请求参数和返回参数并调用 Sender.send 方法。这里的访问本地变量表的代码参考了 Spring 的 LocalVariableTableParameterNameDiscoverer ,感兴趣的同学可以自己研究下。接下来看下 Sender 中的代码:

publicclassSender {privatestaticfinal int SERVER_PORT =9876;publicstaticvoidsend(Objectresponse,Object[] request,StringclassName,StringmethodName) {Message message =newMessage(response, request, className, methodName);try{Socket socket =newSocket(“localhost”, SERVER_PORT); socket.getOutputStream().write(message.toString().getBytes()); socket.close();}catch(IOException e) { e.printStackTrace(); } }privatestaticclassMessage {privateObjectresponse;privateObject[] request;privateStringclassName;privateStringmethodName;publicMessage(Objectresponse,Object[] request,StringclassName,StringmethodName) {this.response = response;this.request = request;this.className = className;this.methodName = methodName; }@OverridepublicStringtoString() {return"Message{"+“response=”+ response +", request="+ Arrays.toString(request) +", className=’"+ className +’’’+", methodName=’"+ methodName +’’’+’}’; } }}

Sender 中的代码不复杂,一看就懂,就不多说了。下面我们来看下服务端的代码,服务端要实现开启一个端口监听,接受请求信息,以及使用 attach api 加载 agent 。

packageorg.xunche.app;importcom.sun.tools.attach.AgentInitializationException;importcom.sun.tools.attach.AgentLoadException;importcom.sun.tools.attach.AttachNotSupportedException;importcom.sun.tools.attach.VirtualMachine;importjava.io.BufferedReader;importjava.io.IOException;importjava.io.InputStream;importjava.io.InputStreamReader;importjava.net.ServerSocket;importjava.net.Socket;publicclassTraceAgentMain{privatestaticfinalintSERVER_PORT =9876;publicstaticvoidmain(String[] args)throwsIOException, AttachNotSupportedException, AgentLoadException, AgentInitializationException{newServer().start();//attach的进程VirtualMachine vm = VirtualMachine.attach(“85241”);//加载agent并指明需要采集信息的类和方法vm.loadAgent(“trace-agent.jar”,“org.xunche.app.HelloTraceAgent.sayHi”); vm.detach(); }privatestaticclassServerimplementsRunnable{@Overridepublicvoidrun(){try{ServerSocket serverSocket =newServerSocket(SERVER_PORT);while(true) { Socket socket = serverSocket.accept(); InputStream input = socket.getInputStream();BufferedReader reader =newBufferedReader(newInputStreamReader(input));System.out.println(“receive message:”+ reader.readLine()); }}catch(IOException e) { e.printStackTrace(); } }publicvoidstart(){Thread thread =newThread(this); thread.start(); } }}

运行上面的程序,可以看到服务端收到了 org.xunche.app.HelloTraceAgent.sayHi 的请求和返回信息。

receive message:Message{response=hi, xunche,1581599464436, request=[xunche], className=‘org.xunche.app.HelloTraceAgent’, methodName=‘sayHi’}

小结

在本章内容中,为大家介绍了 agent 的基本使用包括 premain 和 agentmain 。并通过 agentmain 实现了一个采集运行时方法调用信息的小工具,当然由于篇幅和时间问题,代码写的比较随意,大家多体会体会思路。实际上, agent 的作用远不止文章中介绍的这些,像 BTrace、arms、springloaded 等中也都有用到 agent 。

你可能感兴趣的:(java)