package Sequenic.T2ext; import java.io.BufferedReader; import java.io.InputStreamReader; import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.Set; import Sequenic.T2ext.Analyzer.CoverageResult; import Sequenic.T2ext.Analyzer.T2TraceFileCoverageAnalyzer; import Sequenic.T2ext.Analyzer.T2TraceFileCoverageResult; import Sequenic.T2ext.CoverageEngine.BaseEngine; import Sequenic.T2ext.CoverageEngine.Engine_10; import Sequenic.T2ext.CoverageEngine.ga.CustomEvaluator; import Sequenic.T2ext.CoverageEngine.ga.GAEngine; import Sequenic.T2ext.Instrumenter.CFG; import Sequenic.T2ext.Instrumenter.Node; /** * Contains a main method for T2ext that can run the instrumenter, T2 and the analyzer. * Currently it will run the GAEngine after that. * * @author Christiaan Hees */ public class Main { private T2TraceFileCoverageAnalyzer analz; private T2TraceFileCoverageResult result; private String[] t2Args; private static void usage() { System.out.println("Runs the instrumenter, T2 and the analyzer on a class.\n"); System.out.println("Example:"); System.out.println(" java -ea -cp .:../libs/bcel-5.2.jar:../libs/TT.2.1.4.3.jar" + " Sequenic.T2ext.Main Examples.SimpleConditionals\n"); System.exit(1); } private static void instrument(String cut) { System.out.println("==="); System.out.println("=== Starting the instrumenter"); System.out.println("==="); // Run the instrumenter in a separate process so it won't mess up the analyzer: try { Process p = Runtime.getRuntime().exec( "java -cp "+System.getProperty("java.class.path")+ " Sequenic.T2ext.Instrumenter.Main . "+cut); p.waitFor(); // Uncomment if you want to see the output: ///* BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { System.out.println(inputLine); } // and the error stream: in = new BufferedReader(new InputStreamReader(p.getErrorStream())); while((inputLine = in.readLine()) != null) { System.out.println(inputLine); } in.close(); //*/ } catch (Exception e) { e.printStackTrace(); System.exit(1); } System.out.println(); } private void runT2(String[] args) { System.out.println("==="); System.out.println("=== Starting T2"); System.out.println("==="); int t2ArgCount = 4; // change this if you add or remove an arg here String[] t2Args = new String[args.length+t2ArgCount]; t2Args[0] = args[0]; t2Args[1] = "--nmax=10000"; t2Args[2] = " "; //"--lenexec=10"; t2Args[3] = "--savegoodtr=1000"; // BaseDomain0 seems to be much better than the default BaseDomainSmall: t2Args[4] = "--bdomain=Sequenic.T2.BaseDomain0"; for(int i=1; i> entries = new LinkedList>(result.totCoverage.entrySet()); for (Map.Entry entry : entries) { CFG cfg = entry.getKey(); CoverageResult cr = entry.getValue(); System.out.println("Method: "+cfg.getMethodName()); if(cfg.getMethodName().endsWith("customTraceDirector") || cfg.getMethodName().endsWith("_gen") || cfg.getMethodName().endsWith(CustomEvaluator.METHOD_EXTENSION)) { System.out.println("Skipped this method.\n"); continue; } int targetPathCount = cfg.getTargetPaths().size(); totalTargetPathCount += targetPathCount; int coveredPathCount = cr.directlyCoveredPaths.size(); totalCoveredPathCountBefore += coveredPathCount; Set> directlyCoveredPaths = new HashSet>(cr.directlyCoveredPaths); for (LinkedList ppath : cfg.getTargetPaths()) { // If a prime path isn't directly covered yet... if (!cr.directlyCoveredPaths.contains(ppath)) { Engine_10 eng10 = new Engine_10(result, analz, ppath); // ...generate new traces eng10.generateTraces(); System.out.println("\n===== The coverage of the newly-generated traces:\n"); CoverageResult crAfter = eng10.testGeneratedTraces(); // Add the paths to the directlyCoveredPaths set (no duplicates) directlyCoveredPaths.addAll(crAfter.directlyCoveredPaths); } } System.out.println("Target Path Count : "+targetPathCount); System.out.println("Covered Path Count before: "+coveredPathCount); System.out.println("Covered Path Count after : "+directlyCoveredPaths.size()); totalCoveredPathCountAfter += directlyCoveredPaths.size(); System.out.println(); } System.out.println("------------"); System.out.println("Total Target Path Count : "+totalTargetPathCount); System.out.println("Total Covered Path Count before: "+totalCoveredPathCountBefore); System.out.println("Total Covered Path Count after : "+totalCoveredPathCountAfter); System.out.println(); // TODO: do the new traces actually get saved? } private void coverGA() { System.out.println("==="); System.out.println("=== Calling the GA Coverage Engine"); System.out.println("==="); // Now use GAEngine to try to cover each uncovered path: int totalTargetPathCount = 0; int totalCoveredPathCountBefore = 0; int totalCoveredPathCountAfter = 0; analz.debug = false; List> entries = new LinkedList>(result.totCoverage.entrySet()); for (Map.Entry entry : entries) { CFG cfg = entry.getKey(); CoverageResult cr = entry.getValue(); System.out.println("Method: "+cfg.getMethodName()); if(cfg.getMethodName().endsWith("customTraceDirector") || cfg.getMethodName().endsWith("_gen") || cfg.getMethodName().endsWith(CustomEvaluator.METHOD_EXTENSION)) { System.out.println("Skipped this method.\n"); continue; } int targetPathCount = cfg.getTargetPaths().size(); totalTargetPathCount += targetPathCount; int coveredPathCount = cr.directlyCoveredPaths.size(); totalCoveredPathCountBefore += coveredPathCount; Set> directlyCoveredPaths = new HashSet>(cr.directlyCoveredPaths); for (LinkedList ppath : cfg.getTargetPaths()) { // If a prime path isn't directly covered yet... if (!cr.directlyCoveredPaths.contains(ppath)) { //System.out.println("DEBUG "+ppath); //BaseEngine engine = new Engine_10(result, analz, ppath); BaseEngine engine = new GAEngine(result, analz, ppath, t2Args); engine.generateTraces(); System.out.println("\n===== The coverage of the newly-generated traces:\n"); CoverageResult crAfter = engine.testGeneratedTraces(); // Add the paths to the directlyCoveredPaths set (no duplicates) directlyCoveredPaths.addAll(crAfter.directlyCoveredPaths); System.out.println(); } } System.out.println("Target Path Count : "+targetPathCount); System.out.println("Covered Path Count before: "+coveredPathCount); System.out.println("Covered Path Count after : "+directlyCoveredPaths.size()); totalCoveredPathCountAfter += directlyCoveredPaths.size(); System.out.println(); } System.out.println("------------"); System.out.println("Total Target Path Count : "+totalTargetPathCount); System.out.println("Total Covered Path Count before: "+totalCoveredPathCountBefore); System.out.println("Total Covered Path Count after : "+totalCoveredPathCountAfter); System.out.println(); // TODO: do the new traces actually get saved? } public void run(String[] args) { if(args.length < 1) usage(); instrument(args[0]); runT2(args); analyze(args[0]); //cover(); coverGA(); } public static void main(String[] args) { Main m = new Main(); m.run(args); } }