Instrument realization Principle of Zorka Source code interpretation

Source: Internet
Author: User

The main use of three aspects of technology:

    1. BeanShell to achieve scalability: Tell the zorkaagent the specific needs of the pile, including the method and value of the insertion pile, the timing of the insertion pile, the insertion pile tracking record way.
    2. Instrument to access the JVM through proxies, enabling access to class bytecode when classes are loaded.
    3. ASM bytecode manipulation framework for real-class byte code modification.

First, BeanShell implementation of the specific requirements of the insertion pile (BSH How will the contents of the pile to tell the agent?) )
To add an instance of Spydefinition to Classtransformer, the spydefinition defines a variety of insertion-pile behaviors, which are passed to the underlying main package to Spyprobe and Spyprocessor. Both the instance collection of the Spyprobe and the instance collection of the Spyprocessor are spydefinition properties.

Second, the instrument pile-inserting process

1, Agentmain.premain method execution

premain method primarily performs two tasks:
spydefinition contains all the information about the piles.
2 add instrumentation to transformer.

2, Classtransformer.transform method execution
Each time a class is loaded, the transformer transform method executes once to see if a conversion is required. At the end of the transform function, if a null value is returned, it means that no conversion of class bytecode is required, otherwise the converted class byte array is returned.
transform is the bytecode access technology that calls ASM:

 classreader cr = new  Classreader (CBF); // CBF is a byte array of class bytecode  Classwriter CW = new  Classwriter (CR, Cbf[7] > ( byte ) 0x32? Classwriter.compute_frames:0); Classvisitor SCV  = Createvisitor (ClassLoader, Clazzname, found, Tracer, CW); // found is the collection of Spydefinition  cr.accept (SCV, 0); //  Pass the byte-code visitor to Classreader, and this method begins with a formal bytecode access.  buf = Cw.tobytearray (); 

1) The invocation of various visit methods starting visitor (Spyclassvisitor) in the Accept. such as visit (Access class declaration), Visitsource, Visitouterclass, Visitinnerclass, Readmethod (Read method, let specify visitor access), Readfield, Visitannotaiton, Visittypeannotation, Visitattribute, Visitend, these methods are executed in a certain order. Here we focus on the invocation of Readmethod.
2) Readmethod Call Spyclassvisitor's Visitmethod method. Visitmethod parses the spydefinition and gets the Spycontext context passed to the next node visitor according to the Spymatcher matching target method. For example, method visitor Spymethodvisitor. This object is returned to the Readmethod method to continue the access execution.
3) The Visit method in Spymethodvisitor is called. Among them are visitannotation, Visitcode, VISITINSN, Visitmaxs and so on.
4) The visit access above invokes the Emitprobes method. The visit is based on the probe in the spydefinition,spydefinition defined in the Spycontext.

Private int emitprobes (int stage, Spycontext CTX)

5) The Emitprobes method iterates through the probe inside the Spycontext and calls the Probe.emit method for inserting piles.

Instrument realization Principle of Zorka Source code interpretation

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.