Instrumentation implementations in the JVM

Source: Internet
Author: User

Presumably a lot of people have heard of javaagent, but few people heard instrumentation, in fact, instrumentation is javaagent realization mechanism, speaking of instrumentation, Must want to understand the Java attach mechanism, that first say attach implementation.

When you do jstack, is not often see two threads Signal Dispatcher and Attach Listener threads, may not know what to do, these two threads are the key to achieve attach, which, 前者是在jvm启动的时候就会创建的 as the 后者只有接收过attach请求的时候vm才会创建 name implies, Signal Dispatcher是分发信号的 Attach Listener 是处理attach请求的 ,, So what's the relationship between the two, when we 执行attach方法的时候,会向目标vm发出一个SIGQUIT 的信号,目标vm收到这个信号之后就会创建Attach Listener线程了 , of course, the JVM guarantees that it won't be created more.

The attach mechanism is simple to say is to provide a process can be connected to the B process (of course, the Java process), create a socket to communicate, a command to b,b and then intercept the command to get information from their own VMS sent back to the client VM, but not just send instructions will be processed, So what commands does the attach listener receive, as follows:

Instrumentation implementation of the main use of load this directive, it is used to achieve the implementation of the target vm动态加载agentlib instrumentation in a named libinstrument.dylib Dynamic Lib Library, Linux is libinstrument.so , it is based on JVMTI interface implementation , so when it is load, an agent instance is created and some callback methods are registered to the JVMTI environment, such as, and so on, to 监听类文件加载的事件 vm初始化完成事件 execute Agent_onattach, Here a instrumentation instance is created and returned to the user for extension instrumentation, such as adding some transform and executing the instrumentation instance loadClassAndCallAgentmain method. This method mainly executes the Agentmain method of the Agent-class class defined in the Agent's MF file, and when the VM initialization is complete, the method is called. loadClassAndCallPremain This method mainly executes the Premain method of the Agent-class class defined in the agent's MF file. 在类进行加载的时候会调用Instrumentation的transform方法, you can see that there is a byte array in the parameter, this array is actually the class byte code that is being loaded, so if you want to get the bytecode enhancement here, you can even implement bogus.

If the agent is loaded during VM startup, during VM initialization 先执行libinstrument.dylib里InvocationAdapter.c的Agent_OnLoad方法 , the method mainly: Instantiate the agent, parse the agent's MF file, remove the related attributes, and register some of the JVMTI callback functions, after the VM initialization is complete, The instrumentation implementation object is instantiated through the callback function, the Classfileloadhook function is set, and the Premain method of the specified class is called Pre-main.

If the load agent is passed through the Attach API at run time, it will be called after the load instruction is received, InvocationAdapter.c的Agent_OnAttach方法 its implementation is basically and agent_onload consistent, and 只是会调用Agent-Class的agentmain方法 a bit different is no longer concerned about the VM Init event (both run-time, Concern is not useful), but directly to the classfileload attention, and will not call the Pre-main specified class Premain method (as the name implies, is executed before the main method, so the run time to execute Pre-main class is not appropriate).

Instrumentation implementations in the JVM

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.