Instrumentation interface is designed to collect Java runtime data, used to monitor the state of the running program, log, code analysis. Next from the source of the process to introduce

The implementation class InstrumentationImpl void addTransformer (ClassFileTransformer transformer, Boolean canRetransform);

The implementation of ClassFileTransformer is stored in the TransformerManager’s TransformerInfo array. The initial length of the array is 0. For each addition, the array length is +1, and the contents of the original array are copied to the new array.

VirtualMachine.attach

Go to AttachProvider.providers(), which initializes the AttachProvider and returns a AttachProvider List.

Enter the ServiceLoader. Load (AttachProvider. Class, AttachProvider. Class. GetClassLoader ());

New LazyIterator(service, loader)

See com. Sun. Tools jar package under the meta-inf/services/directory, open the com. Sun. View the attach. Spi. AttachProvider, can see there are different implementation of the operating system platform, is my Windows, Invokes the windos the implementation of the sun. The tools. Attach. WindowsAttachProvider. Everything else is commented out. Code to see this, I knew ServiceLoader. The ultimate load of the load method is sun. View the attach. WindowsAttachProvider.

NextService() below hasNextService() above

Back to the original VirtualMachine. Attach (String var0) method, enter the return var4. AttachVirtualMachine (var0); AttachVirtualMachine ()

Through the findNative method in the ClassLoader class, you can find some native method call names in the JVM source code, which can be associated with the JVM source code to see what the underlying C++ source code does

Find a method named Java_sun_tools_attach_WindowsVirtualMachine_openProcessnative method in attach.dll

Try the JDK source code to find the two methods Java_sun_tools_attach_WindowsVirtualMachine_openProcess and Java_sun_tools_attach_WindowsVirtualMachine_en queue

Virtualmachine.loadagent (), VirtualMachine.detach () source flow and the above similar, also platform-dependent, not here to repeat.

RedefineClasse Configuration precautions

Changes can be made to the bytecode of the loaded class at runtime, but in this case there are many limitations to comparing old and new classes and requirements are as follows:

  • The parent class is the same
  • Implement the same number of interfaces, and be the same interface
  • Class accessors must be consistent
  • The number of fields must be the same as the field name
  • The new method must be private static/final
  • Modify methods can be deleted

reference

JVM source code analysis of the JavaAgent principle of full interpretation

Pay attention and don’t get lost

The article continues to update every week, you can wechat search “ten minutes to learn programming” the first time to read and urge more, if this article is written well, feel something ~ for praise 👍 for attention ❤️ for share ❤️ everyone’s support and recognition, is the biggest power of my creation, we see the next article!