You do not know Java probe?

java tool probe technology based on the principle of injection technology and Java bytecode JavaAgent

img

We use javaAgent and ASM bytecode java probe technology development tools to achieve the principle as follows:

javaAgent jdk1.5 after the introduction of technology, javaAgent before operating method interceptors. We use javaAgent and ASM bytecode technology, JVM load the class binary file when using the ASM dynamically modify the loaded class file, add a timer function before and after the monitoring method, monitoring method for calculating time-consuming, while the method processed into the case and internal call processor, the processor utilizes a stack of advanced call features make order processing method, when a request completion processing, the trajectory and time consuming methods into the reference map output to a file, and then the map or the corresponding parameter track time-consuming method to distinguish our critical code to crawl consuming business. Finally, the appropriate time-consuming to take down the track files, into xml format and parsed by the browser will show the code hierarchy out, time-consuming analysis of convenience, as shown in FIG 0-1.

img

Java tool probe functions:

1, supports the gripping range setting method of performing time-consuming, time-consuming range setting code range trajectory occur depending Processed gripping system is running.

2, support for crawling specific code configured to facilitate configuration of a particular method is fetched, the code relations filtered time-consuming situation.

3, support layer APP inlet filtration process before the inlet configuration monitoring operation, the monitoring corresponding to the unique time consuming, thematic analysis.

4, an inlet support method parameter output, to facilitate tracking the time corresponding to the high parameter time-consuming.

5, providing WEB page display interfaces consuming display, the code calls the graph shows, the percentage of time-consuming method of display, highlighting the questionable method function.

A simple example JavaAgent demo

JavaAgent is introduced after the 1.5 JDK, also called Java agent.

JavaAgent run blocker before the main method, it is the default method named premain, that is to say before the implementation of premain method and then execute the main method.

So how do you achieve a JavaAgent it? Very simple, only need to increase premain method can, subsequent injection can be achieved on the basis of intercepts, AOP and so on.

package com.xifj.agent.demo;
import java.lang.instrument.Instrumentation;
 
/**
 * Created by uc on 2018/4/18.
 */
public class agentDemo {
    /**
     * 该方法在main方法之前运行,与main方法运行在同一个JVM中
     *
     * @param agentArgs
     * @param inst
     * @author xifeijian
     * @create  2018年4月18日
     */
    public static void premain(String agentArgs, Instrumentation inst) {
        System.out.println("=========premain方法执行1========");
        System.out.println(agentArgs);
    }
 
    /**
     * 如果不存在 premain(String agentArgs, Instrumentation inst)
     * 则会执行 premain(String agentArgs)
     *
     * @param agentArgs
     * @author xifeijian
     * @create  2018年4月18日
     */
    public static void premain(String agentArgs) {
        System.out.println("=========premain方法执行2========");
        System.out.println(agentArgs);
    }
}

In this premain function, developers can perform various operations of the class.
1, agentArgs program parameters premain function is obtained, along with "- javaagent" Incoming together. The difference is that the main function, this parameter is a string and not a string array, if there are a plurality of program parameters, the program parses the string itself.

2, Inst instance java.lang.instrument.Instrumentation is automatically passed from the JVM. java.lang.instrument.Instrumentation instrument is an interface defined in the package, but also the core portion of the packet, almost all of which focus on the features methods, such as class definition and conversion operations and the like.

After writing this class, we need to do further work to configure, generate META-INF / MANIFEST.MF file in the src directory.
img

img

Then edit META-INF / MANIFEST.MF, the content is defined as follows:

Pay special attention to the last line is a blank line, behind Premain-Class there is a colon followed by a space, for example:

Manifest-Version: 1.0
Premain-Class: com.xifj.agent.demo.agentDemo

img

Then we pack code javaagent.jar (Build-Artifact)

img

Then we create the main program in a project with a main method, shots are as follows:

img

Cheng finished code, configuration parameters before running VM:

-javaagent:D:\workspace\javaagent\out\artifacts\javaagent_jar\javaagent.jar=Hello -javaagent:D:\workspace\javaagent\out\artifacts\javaagent_jar\javaagent.jar=World
img

运行agentTest程序,得到以下结果:

img

也可以将agentTest打成jar包通过java命令行执行,我们通过 -javaagent 参数来指定我们的Java代理包,值得一说的是 -javaagent 这个参数的个数是不限的,如果指定了多个,则会按指定的先后执行,执行完各个 agent 后,才会执行主程序的 main 方法。命令如下:

java -javaagent:D:\workspace\javaagent\out\artifacts\javaagent_jar\javaagent.jar=hello1 -javaagent:D:\workspace\javaagent\out\artifacts\javaagent_jar\javaagent.jar=hello2 -jar D:\workspace\myTest\out\artifacts\myTest_jar\myTest.jar

执行结果和上图是完全一致的。

特别提醒:如果你把 -javaagent 放在 -jar 后面,则不会生效。也就是说,放在主程序后面的 agent 是无效的。

常见问题

一、什么是java agent?

Java agent是在JDK1.5引入的,是一种可以动态修改Java字节码的技术。java类编译之后形成字节码被JVM执行,JVM在执行这些字节码之前获取这些字节码信息,并且对这些字节码进行修改,来完成一些额外的功能,这种就是java agent技术。

二、java agent可以实现什么样的功能?

1.java agent能够在加载java字节码之前进行拦截并对字节码进行修改

2.在jvm运行期间修改已经加载的字节码

通过以上两种就可以实现在一些框架或是技术的采集点进行字节码修改,可以对应用进行监控,或是对执行指定方法或是接口时额外添加操作(打印日志、打印方法执行时间、采集方法的入参和结果等)

三、java agent的实现原理?

了解java agent的实现原理就必须先了解java的类加载机制(还不了解的自行了解),这个是了解java agent的前提。

其次需要了解的是JVMTI以及JVMTIAgent,下面分别介绍下

3.1、JVMTI

JVMTI是JVM Tool Interface的缩写,是JVM暴露出来给用户扩展使用的接口集合,JVMTI是基于事件驱动的,JVM每执行一定的逻辑就会调用一些事件的回调接口,这些接口可以给用户自行扩展来实现自己的逻辑

3.2、JVMTIAgent

JVMTIAgent是一个动态库,利用JVMTI暴露出来的接口实现用户自行的逻辑(eclipse、idea等工具等代码调试就是通过这个实现的)

JVMTIAgent主要有三个方法,

Agent_OnLoad方法,如果agent在启动时加载,就执行这个方法

Agent_OnAttach方法,如果agent不是在启动的时候加载的,是我们先attach到目标线程上,然后对对应的目标进程发送load命令来加载agent,在加载过程中调用Agent_OnAttach函数

Agent_OnUnload方法,在agent做卸载掉时候调用

3.3、instrument agent

instrument agent实现了Agent_OnLoad方法和Agent_OnAttach方法,也就是即能在启动的时候加载agent,也可以在运行期来加动态加载agent,运行期动态加载agent依赖JVM的attach机制实现,通过发送load命令来加载agent

3.4、JVM Attach机制

jvm attach机制上JVM提供的一种JVM进程间通信的功能,能让一个进程传命令给另一个进程,并进行一些内部的操作,比如进行线程dump,那么就需要执行jstack进行,然后把pid等参数传递给需要dump的线程来执行,这就是一种java attach。

3.5、Class Transform的实现

The first class is loaded when asked to transform the scene, issued ClassFileLoad event to load the class file, to the instrument agent to call the java agent in registered ClassFileTransformer achieve bytecode modification

3.6, Class Redefind implementation

Redefining the class, it has been mainly used in the loaded class

Fourth, implement byte-code enhancement technology framework for what?

The principle of a clear understanding on the need to achieve, java agent to achieve bytecode enhancement process is about:

1. Modify the bytecode 2 Load New bytecode, 3 replacing the old bytecodes

When the second step can be customized ClassLoader to load the bytecode modification, the third step can be replaced when loaded by the JVM or byte code, then the first step how to modify the bytecode it, at least most people does not modify, then you need to use tools to modify,

Currently bytecode modification tool to achieve the main ASM, Javassist and byte buddy, next will focus on the three-byte code generation framework and usage

Reference article:
https://www.cnblogs.com/jackion5/p/10679728.html
https://www.cnblogs.com/aspirant/p/8796974.html
https://blog.csdn.net/xifeijian/article / details / 79991913

Published 108 original articles · won praise 14 · views 40000 +

Guess you like

Origin blog.csdn.net/belongtocode/article/details/103976167