The main use of three aspects of technology:
- 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.
- Instrument to access the JVM through proxies, enabling access to class bytecode when classes are loaded.
- 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