Specific embodiment
The embodiment of the present application provides the method and system that a kind of java applet injects failure.
In order that those skilled in the art more fully understand the technical scheme in the application, below in conjunction with this
Accompanying drawing in application embodiment, is clearly and completely described to the technical scheme in the embodiment of the present application,
Obviously, described embodiment is only some embodiments of the present application, rather than whole embodiments.Base
Embodiment in the application, those of ordinary skill in the art are obtained under the premise of creative work is not made
The every other embodiment for obtaining, should all belong to the scope of the application protection.
After programmer has write Java source code, Java source code can be compiled as by Java compiler can quilt
Java Virtual Machine explains the bytecode (Class files) for performing.After Java Virtual Machine startup, Class is completed
The work such as loading, checking, preparation, parsing and the initialization of file, and then explain execution Class files.
Under normal circumstances, after Java Virtual Machine starts, a Class file can only be loaded.
In the embodiment of method one of the application java applet injection failure, including JVMTI as shown in Figure 1
Interaction models.
As shown in figure 1, including client, master control end and target Java processes.Wherein, target Java enters
Journey can include agency (Agent), JVM tool interfaces (JVM (Java Virtual Machine) Tool Interface,
JVMTI), java applet (Java Application).
JVMTI can be operated together in same Java Virtual Machine with java applet, and JVMTI is responsible for monitoring
Safeguard java applet.When other programs need to be communicated with JVMTI, JVMTI agencies can be created,
That is Agent.The Agent of establishment can be performed with target Java Virtual Machine in same process, and Agent can
As JVMTI and the extraneous medium for interacting.
Client is the interface of user mutual, and all of operation behavior of user can be completed in client.User
By that after client-side editing direct fault location code, failure code can also be uploaded into master control end by client.
Master control end can be communicated with client, and the command information from client is parsed, and be obtained
Parameter in failure code.On the other hand, master control end can monitor the execution state of Java processes, it is possible to
Interacted with Agent, and then control information can be sent to the JVMTI in Java processes.Specifically,
Java Virtual Machine completes the loading to Class files after starting after, master control end can take from Java Virtual Machine
The Class files of loading are returned, then Class files are modified, Java is back to after modification empty
Plan machine, and replace original Class files.In this process, on the one hand, master control end can be entered with client
Row communication, the command information from client is parsed, and obtains failure code.On the other hand, master control
End can fetch the Class files loaded after Java Virtual Machine starts.And then, master control end can be according to failure
Code enters edlin, the Class files after generation editor to the Class files fetched.After generation editor
After Class files, master control end can by the Class files passes after editing to Java Virtual Machine, with
Replace original Class files.
Fig. 2 shows that the application java applet injects an embodiment of the method for failure, as shown in Fig. 2 including:
S210:Client receives the failure code of input, and the failure code is sent into master control end.
As it was previously stated, client is the interface of user mutual, all of operation behavior of user can be in client
Complete.Specifically, client can include operating console and and communication module.Wherein, the operation control
Platform processed can submit the interactive interface of failure code to as user, receive the operational order that user assigns.It is described
Failure code can be uploaded to master control end by communication module.
The operating console can include script-editor.The script-editor can provide the user event
Hinder the editting function of injection script, editor's failure code in the way of aiding in user to edit direct fault location script.
Specifically, the script-editor can provide modification Class titles, method name and operation behavior
Operation;So, user can utilize the script-editor to be intended to Class titles, the method name of modification
And operation behavior is made as injection script.
The client can also include script correction verification module, for verifying whether the script that user edits meets
Specification.
The master control end can be communicated using Socket modes with the client, will be from the client
The command information at end is parsed, and obtains the failure code.Using Socket programming techniques, at master control end
Open listening port, it is possible to achieve repeatedly change the Class files of the same Java Virtual Machine being currently running.
So, when there is new direct fault location to ask, client is by way of Socket is connected by direct fault location pin
To master control end, master control end can parse the direct fault location script submitted to from client for this transmission.
S220:Java Virtual Machine starts and loads original Class files.
This step similarly to the prior art, is repeated no more.
It should be noted that S220 and S210 does not exist strict sequencing, S220 can be in S210
Before or after perform, certain S220 can also be with S210 executed in parallel.
S230:The master control end monitors the ruuning situation of Java processes where the Java Virtual Machine, fetches
The original Class files of the Java Virtual Machine loading.
As it was previously stated, Java processes can include agency and Java Virtual Machine.In other words, the Agent
Can be performed in same process with the Java Virtual Machine.JVMTI can be operated together with java applet
In the Java Virtual Machine.The JVMTI can monitor the maintenance java applet.The Agent
Can be as JVMTI and the extraneous medium for interacting.JVMTI related interface interchange information can be sent to
The Agent.
Master control end can be designed as servo progress, monitor the execution state of the Java processes, and with it is described
Agent is interacted.By the Agent, master control end can send the control related to the JVMTI
Information.
Specifically, in this step, the master control end can monitor the Java Virtual Machine by JVMTI
Ruuning situation, and monitoring after the Java Virtual Machine loads original Class files, fetch the original
Beginning Class file.Further, the master control end can fetch module including bytecode, by the module,
The original Class files of the Java Virtual Machine loading can be fetched.More specifically, can determine in the application
One abstract class Transformer of justice, such can realize ClassFileTransformer interfaces, its
Transform methods give the editing process of Class bytecodes.In Transform methods, word is used
Section code fetches module and fetches the Class files.By using the get methods of ClassPool classes, can obtain
Fetching determines the byte code files corresponding to class name, and returns to the CtClass classes of the classification class of the byte code files
The reference of type.CtClass classes provide modification class titles, increase member variable, increase case method,
Obtain a series of interfaces such as designation method.
S240:The original Class files that the master control end is fetched according to the failure code editor, it is raw
Into the Class files after editor.
The original Class that the master control end can fetch according to the failure code editor received in S210
File.Specifically, the master control end can include bytecode editor module, by calling Javassit softwares
To realize the editor to the original Class files.Javassist is an analysis increased income, editor and creates
The class libraries of Java bytecode is built, is the Shigeru of mathematics by Tokyo Institute of Technology and computer science department
Chiba (Chiba taste) is created.It has added open source code JBoss application server projects, passes through
Bytecode is operated for JBoss realizes dynamic " AOP " framework using Javassist.
Treatment on Java bytecode, also contains some other similar means, such as BCEL, ASM at present.
For example, Javassist contrasts as follows with ASM features:
1st, javassist is based on the API of source-code level, simpler than the ASM based on bytecode;
2nd, developed based on javassist, it is not necessary to understand some knowledge of bytecode, and its encapsulation one
A little tool-class can be with some Premium Features of simple realization, such as HotSwaper.
3rd, than javassist performance faster, flexible row is also higher for ASM.
Failure code according to receiving enters edlin to original Class files, the Class after being edited
File.Class files after the editor already have the failure code of injection.
More specifically, the abstract class Transformer according to defined in above-mentioned S230, need to such
When certain method is modified, getMethod methods can be called to obtain the corresponding example of CtMethod classes
method.CtMethod classes provide a series of amending methods function, such as nominated bank insertion code,
Method inserts code, increases exception handling information, addition local variable, remapping method body etc. before performing.When
After having edited the corresponding bytecode of Class files, the toBytecode methods of CtClass classes are called to give birth to again
Into such byte code files, you can to complete the modification of bytecode.
Instrumentation interfaces are the cores of java.lang.instrumentation bags, and the interface is given
The methods such as the conversion of class and operation.AddTransformer methods can register ClassFileTransformer,
And redefineClasses methods can then redefine Class.Master control end can preserve Instrumentation
The reference of interface, so, as the Class changed the need for having newly, it is necessary to instantiate such correspondence again
Transformer subclasses, and register the Transformer, retransformClasses methods are called therewith.
Agent can set a static method:Agentmain methods.This static method is broken into
Jar bags, and the value of Agent-Class is set to Agent class names in MANIFEST.MF, and will
The value of Can-Redefine-Classes and Can-Retransform-Classes is set to true.Held in program
Class byte code files are changed during row, it is necessary to by Agent attach to specified Java processes.Need
By calling the attache methods of VirtualMachine classes, and to return to process correspondence Java Virtual Machine
The instance objects of VirtualMachine classes, call the loadAgent methods of the instance objects to complete therewith
The attach tasks of Agent.After attach successes, Java Virtual Machine can call correspondence agentmain methods,
Reach the purpose that Java Virtual Machine bytecode is changed in program process.
S250:Master control end is by the Class files passes after the editor to the Java Virtual Machine to replace
State original Class files.
In this step, specifically, master control end can call the Agent in Java processes, after editor
Class files are uploaded to Java Virtual Machine by Agent, and so, original Class files can be replaced
It is the Class files after editor.
Further, Transform classes can be quoted in JVMTI communication modules, master control end uses the reference,
Communicated with the JVMTI in Java Virtual Machine by Agent, bytecode editor module can be generated
Class files passes to Java Virtual Machine.
S260:The Java Virtual Machine explains the Class files after performing the editor.
The Class files after editor, contain the failure code of injection, so as to virtual in the Java
Machine explains the Class files after performing the editor, can complete direct fault location, and then can realize evaluation and test
Try the fault freedom and stability or reliability of program to be verified.
After above-mentioned steps are performed, the embodiment of the present application can also include:
S270:The master control end is by fail result data back to the client.
After the completion of direct fault location, the master control end can by fail result data back to the client, with
Prompting user.Specifically, master control end can define a data transfer class TransferProtocol, the transmission
Class can by target class name (targetClass), goal approach (targetMethod), insertion script (codes),
Fault type (faultType), fail result (faultResult) etc. are constituted.
Also, the application is by after Java Virtual Machine starts, monitoring the Java Virtual Machine ruuning situation
And the original Class files of the loading are fetched, the Class files after editing generation editor are to replace
The original Class files, it is possible to achieve Java Virtual Machine need not restart Java Virtual Machine after starting
Please in the case of inject the failure of specified type, it is possible to keep context data when reporting an error.
Further, using the above embodiments of the present application, can be inserted in java applet bytecode specified location
Code segment, adjustment control statement execution logic, amending method parameter, force method to return in advance, dish out it is different
Often.
In addition, a kind of direct fault location instrument in the prior art for java applet, it is necessary to collect corresponding in advance
Fault message, set up corresponding fault database, so as to class files are scanned matching after, injection
Corresponding failure code, starts Java Virtual Machine and performs corresponding byte code files.Prior art needs pre-
First scan Java files, it is impossible to meet to the purpose that Arbitrary Fault code is injected in byte code files.The instrument
It is unable to reach the purpose that failure is injected to the Java Virtual Machine in being currently running.Relative to above-mentioned prior art,
Because the application need not set up fault database, can be to injection Arbitrary Fault code in byte code files.
The embodiment of the present application is provided and also provides a kind of system, it is possible to achieve above-mentioned method and step, and the system
Can be realized by software, it is also possible to realized by way of hardware or software and hardware combining.It is implemented in software
As a example by, as the system on logical meaning, be by the CPU of server (Central Process Unit,
Central processing unit) corresponding computer program instructions are read run in internal memory what is formed.
Specifically, the system that a kind of java applet of the application injects failure, can with as shown in figure 3, including:
Client 31, for receiving the failure code of input, and is sent to master control end by the failure code;
Master control end 32, the ruuning situation for monitoring Java processes where the Java Virtual Machine, fetches institute
State the original Class files of Java Virtual Machine loading;That is fetched according to the failure code editor is original
Class files, the Class files after generation editor;By the Class files passes after the editor to described
Java Virtual Machine is replacing the original Class files;
Java Virtual Machine 33, is used to load original Class files, and perform the volume for explaining after startup
Class files after volume.
In one embodiment, it is preferred that, the client can include operating console and and communication module,
Wherein:
The operating console submits the interactive interface of failure code to as user, receives the operation that user assigns
Instruction;
Failure code is uploaded to master control end by the communication module.
In one embodiment, it is preferred that, the master control end can use Socket modes and the client
Communicated.
In one embodiment, it is preferred that, the client can also include script correction verification module, for testing
Whether the script that card user edits meets specification.
In one embodiment, it is preferred that, it is empty that the master control end can monitor the Java by JVMTI
The ruuning situation of plan machine, and after the original Class files of the Java Virtual Machine loading are monitored, fetch
The original Class files.
In one embodiment, it is preferred that, the master control end is designed as servo progress.
In one embodiment, it is preferred that, the master control end is realized to institute by calling following any software
State the editor of original Class files:
Javassit, BCEL, ASM.
In one embodiment, it is preferred that, the master control end, will by calling the Agent in Java processes
Class files after editor are uploaded to Java Virtual Machine by Agent, to replace the original Class texts
Part.
In one embodiment, it is preferred that, the master control end can also include passback module, for by failure
Result data is back to the client.
In the nineties in 20th century, it is changing on hardware that can clearly be distinguished for the improvement of a technology
The improvement entered on (for example, the improvement to circuit structures such as diode, transistor, switches) or software is (right
In the improvement of method flow).However, with the development of technology, the improvement of current many method flows is
Through directly improving for hardware circuit can be considered as.Designer is nearly all by by improved method flow
It is programmed into hardware circuit to obtain corresponding hardware circuit.Therefore, it cannot be said that method flow
Improvement cannot be realized with hardware entities module.For example, PLD (Programmable
Logic Device, PLD) (for example field programmable gate array (Field Programmable Gate Array,
FPGA it is exactly)) such a integrated circuit, its logic function is determined by user to device programming.By setting
Meter personnel are voluntarily programmed a digital display circuit " integrated " on a piece of PLD, without asking chip manufacturing
Manufacturer designs and makes special IC chip.And, nowadays, substitution manually makes integrated electricity
Road chip, this programming also uses " logic compiler (logic compiler) " software instead to realize mostly, it
Software compiler used is similar when being write with program development, and the source code before compiling is also handy
Specific programming language is write, and this is referred to as hardware description language (Hardware Description
Language, HDL), and HDL is also not only a kind of, but have many kinds, such as ABEL (Advanced
Boolean Expression Language)、AHDL(Altera Hardware Description Language)、
Confluence、CUPL(Cornell University Programming Language)、HDCal、JHDL
(Java Hardware Description Language)、Lava、Lola、MyHDL、PALASM、RHDL
(Ruby Hardware Description Language) etc., that most commonly use at present is VHDL
(Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog.
Those skilled in the art also will be apparent to the skilled artisan that only need to by method flow with above-mentioned several hardware description languages slightly
Programming in logic is simultaneously programmed into integrated circuit, it is possible to be readily available the hardware for realizing the logical method flow
Circuit.
Controller can be implemented in any suitable manner, for example, controller can take such as microprocessor
Or processor and storage can be by computer readable program code (such as softwares of (micro-) computing device
Or firmware) computer-readable medium, gate, switch, application specific integrated circuit (Application Specific
Integrated Circuit, ASIC), the form of programmable logic controller (PLC) and embedded microcontroller, controller
Example include but is not limited to following microcontroller:ARC 625D、Atmel AT91SAM、Microchip
PIC18F26K20 and Silicone Labs C8051F320, Memory Controller is also implemented as depositing
A part for the control logic of reservoir.It is also known in the art that except with pure computer-readable program
Code means are realized beyond controller, can cause to control by the way that method and step is carried out into programming in logic completely
Device is with the shape of gate, switch, application specific integrated circuit, programmable logic controller (PLC) and embedded microcontroller etc.
Formula realizes identical function.Therefore this controller is considered a kind of hardware component, and to bag in it
The structure that the device for realizing various functions for including can also be considered as in hardware component.Or even, can be with
To be used to realizing that the device of various functions is considered as not only being the software module of implementation method but also can be hardware
Structure in part.
System, device, module or unit that above-described embodiment is illustrated, specifically can be by computer chip or reality
Body is realized, or is realized by the product with certain function.
For convenience of description, it is divided into various units with function during description apparatus above to describe respectively.Certainly,
The function of each unit can be realized in same or multiple softwares and/or hardware when the application is implemented.
It should be understood by those skilled in the art that, embodiments of the invention can be provided as method, system or meter
Calculation machine program product.Therefore, the present invention can be using complete hardware embodiment, complete software embodiment or knot
Close the form of the embodiment in terms of software and hardware.And, the present invention can be used and wherein wrapped at one or more
Containing computer usable program code computer-usable storage medium (including but not limited to magnetic disk storage,
CD-ROM, optical memory etc.) on implement computer program product form.
The present invention is produced with reference to method according to embodiments of the present invention, equipment (system) and computer program
The flow chart and/or block diagram of product is described.It should be understood that can by computer program instructions realize flow chart and
/ or block diagram in each flow and/or the flow in square frame and flow chart and/or block diagram and/
Or the combination of square frame.These computer program instructions to all-purpose computer, special-purpose computer, insertion can be provided
The processor of formula processor or other programmable data processing devices is producing a machine so that by calculating
The instruction of the computing device of machine or other programmable data processing devices is produced for realizing in flow chart one
The device of the function of being specified in individual flow or multiple one square frame of flow and/or block diagram or multiple square frames.
These computer program instructions may be alternatively stored in can guide computer or the treatment of other programmable datas to set
In the standby computer-readable memory for working in a specific way so that storage is in the computer-readable memory
Instruction produce include the manufacture of command device, the command device realization in one flow of flow chart or multiple
The function of being specified in one square frame of flow and/or block diagram or multiple square frames.
These computer program instructions can be also loaded into computer or other programmable data processing devices, made
Obtain and series of operation steps is performed on computer or other programmable devices to produce computer implemented place
Reason, so as to the instruction performed on computer or other programmable devices is provided for realizing in flow chart one
The step of function of being specified in flow or multiple one square frame of flow and/or block diagram or multiple square frames.
In a typical configuration, computing device includes one or more processors (CPU), input/output
Interface, network interface and internal memory.
Internal memory potentially includes the volatile memory in computer-readable medium, random access memory
And/or the form, such as read-only storage (ROM) or flash memory (flash RAM) such as Nonvolatile memory (RAM).It is interior
Deposit be computer-readable medium example.
Computer-readable medium includes that permanent and non-permanent, removable and non-removable media can be by appointing
What method or technique realizes information Store.Information can be computer-readable instruction, data structure, program
Module or other data.The example of the storage medium of computer include, but are not limited to phase transition internal memory (PRAM),
It is static RAM (SRAM), dynamic random access memory (DRAM), other kinds of random
Access memory (RAM), read-only storage (ROM), Electrically Erasable Read Only Memory (EEPROM),
Fast flash memory bank or other memory techniques, read-only optical disc read-only storage (CD-ROM), digital multi light
Disk (DVD) or other optical storages, magnetic cassette tape, the storage of tape magnetic rigid disk or other magnetic storage apparatus
Or any other non-transmission medium, can be used to store the information that can be accessed by a computing device.According to herein
Define, computer-readable medium does not include temporary computer readable media (transitory media), such as modulates
Data-signal and carrier wave.
Also, it should be noted that term " including ", "comprising" or its any other variant be intended to non-row
His property is included, so that process, method, commodity or equipment including a series of key elements not only include
Those key elements, but also other key elements including being not expressly set out, or also include for this process,
Method, commodity or the intrinsic key element of equipment.In the absence of more restrictions, by sentence " including
One ... " key element that limits, it is not excluded that in the process including the key element, method, commodity or set
Also there is other identical element in standby.
It will be understood by those skilled in the art that embodiments herein can be provided as method, system or computer journey
Sequence product.Therefore, the application can using complete hardware embodiment, complete software embodiment or combine software and
The form of the embodiment of hardware aspect.And, the application can be used and wherein include calculating at one or more
Machine usable program code computer-usable storage medium (including but not limited to magnetic disk storage, CD-ROM,
Optical memory etc.) on implement computer program product form.
The application can be described in the general context of computer executable instructions, example
Such as program module.Usually, program module includes performing particular task or realizes particular abstract data type
Routine, program, object, component, data structure etc..This can also in a distributed computing environment be put into practice
Application, in these DCEs, by the remote processing devices connected by communication network come
Execution task.In a distributed computing environment, program module may be located at including local including storage device
In remote computer storage medium.
Each embodiment in this specification is described by the way of progressive, identical phase between each embodiment
As part mutually referring to what each embodiment was stressed is the difference with other embodiment.
For especially for system embodiment, because it is substantially similar to embodiment of the method, so the comparing of description
Simply, the relevent part can refer to the partial explaination of embodiments of method.
Embodiments herein is the foregoing is only, the application is not limited to.For this area skill
For art personnel, the application can have various modifications and variations.All institutes within spirit herein and principle
Any modification, equivalent substitution and improvements of work etc., within the scope of should be included in claims hereof.