110 likes | 334 Views
Code Instrumentation Tools. Tools that modify production code for various purposes during code execution:DebuggersAdd information to allow for break points or step-by-step execution. Coverage measurementReport on the number of times various code elements have been executed.ProfilersMeasure amo
E N D
1. Tutorial / lab 2: Code instrumentation Goals of this session:
Create some tests for a Java class.
Measure the code coverage.
2. Code Instrumentation Tools Tools that modify production code for various purposes during code execution:
Debuggers
Add information to allow for break points or step-by-step execution.
Coverage measurement
Report on the number of times various code elements have been executed.
Profilers
Measure amount of time spent in various parts of the code.
3. Coverage tools Program is typically compiled with special options, to add extra source or object code.
Additional data structures, such as a flow graph, may also be created.
Program is run, possibly via test cases
During execution, information is accumulated and written to an output file.
Post-processing phase:
User report is generated from output file.
4. How to measure coverage? Instrument the source code before compilation
Instrument the virtual machine or object code.
The Apache Byte Code Engineering Library (BCEL) is used by several Java coverage tools
http://jakarta.apache.org/bcel
Use a customized virtual machine.
5. Source code instrumentation Create a copy of the source code, instrument the copy, and compile the instrumented copy to .class files.
This is the approach used by the Clover code coverage tool.
Typical method:
Set up an array of counters.
Insert source code at points of interest that increment specific counters.
Coverage tool calls main() method
After main method returns, dump values of counters.
6. Source code before instrumentation public final void add( final MatrixRow<T> newValues )
{
if ( getNumColumns( ) == 0 )
{
contents = newValues.copy( ).getContents( );
}
else
{
contents.addAll( newValues.copy( ).getContents( ) );
}
}
A counter is inserted for each method, statement, and branch.
Statement counters are before each statement, in case the statement throws an exception.
Branch counters need to identify true and false branches taken.
7. Same code after instrumentation public final void add( final MatrixRow<T> newValues ) { CLOVER.M[348]++; CLOVER.S[1814]++; if ( ( getNumColumns( )==0 && ++CLOVER.CT[338]!=0 | true ) || ( ++CLOVER.CF[338] == 0 & false ) ) { CLOVER.S[1815]++; contents = newValues.copy( ).getContents( ); } else { CLOVER.S[1816]++; contents.addAll( newValues.copy( ).getContents( ) ); } }