1 package gov.nasa.jpf.tools; 20 21 import gov.nasa.jpf.VMListener; 22 import gov.nasa.jpf.VM; 23 import gov.nasa.jpf.SearchListener; 24 import gov.nasa.jpf.jvm.JVM; 25 import gov.nasa.jpf.jvm.bytecode.Instruction; 26 import gov.nasa.jpf.Search; 27 import gov.nasa.jpf.Config; 28 import gov.nasa.jpf.JPF; 29 30 34 public class InsnCounter implements VMListener, SearchListener { 35 36 String [] opCodes = new String [256]; 37 int[] counts = new int[256]; 38 int total; 39 40 public void searchFinished(Search search) { 42 reportStatistics(); 43 } 44 45 public void stateBacktracked(Search search) { 46 } 48 public void propertyViolated(Search search) { 49 } 51 public void searchConstraintHit(Search search) { 52 } 54 public void stateAdvanced(Search search) { 55 } 57 public void stateProcessed(Search search) { 58 } 60 public void searchStarted(Search search) { 61 } 63 public void stateRestored(Search search) { 64 } 66 67 68 public void instructionExecuted(VM vm) { 70 JVM jvm = (JVM)vm; 71 Instruction insn = jvm.getLastInstruction(); 72 int bc = insn.getByteCode(); 73 74 if (opCodes[bc] == null) { 75 opCodes[bc] = insn.getMnemonic(); 76 } 77 counts[bc]++; 78 total++; 79 } 80 81 public void threadStarted(VM vm) { 82 } 84 public void threadTerminated(VM vm) { 85 } 87 public void objectCreated(VM vm) { 88 } 90 public void objectReleased(VM vm) { 91 } 93 public void gcBegin (VM vm) { 94 } 96 public void gcEnd (VM vm) { 97 } 99 public void classLoaded(VM vm) { 100 } 102 public void exceptionThrown(VM vm) { 103 } 105 106 107 void reportStatistics () { 109 int[] sorted = getSortedCounts(); 110 int i; 111 112 for (i=0; i<sorted.length; i++) { 113 int idx = sorted[i]; 114 String opc = opCodes[idx]; 115 116 if (counts[idx] > 0) { 117 System.out.print( i); 118 System.out.print( " "); 119 System.out.print( opc); 120 System.out.print( " : "); 121 System.out.println( counts[idx]); 122 } else { 123 break; 124 } 125 } 126 } 127 128 int[] getSortedCounts () { 129 int[] sorted = new int[256]; 130 int last = -1; 131 int i, j; 132 133 for (i=0; i<256; i++) { 134 int c = counts[i]; 135 if (c > 0) { 136 for (j=0; j<last; j++) { 137 if (counts[sorted[j]] < c) { 138 System.arraycopy(sorted, j, sorted, j+1, (last-j)); 139 break; 140 } 141 } 142 sorted[j] = i; 143 last++; 144 } 145 } 146 147 return sorted; 148 } 149 150 void filterArgs (String [] args) { 151 } 153 154 public static void main (String [] args) { 155 InsnCounter listener = new InsnCounter(); 156 listener.filterArgs(args); 157 158 Config conf = JPF.createConfig(args); 159 161 JPF jpf = new JPF(conf); 162 jpf.addSearchListener(listener); 163 jpf.addVMListener(listener); 164 165 jpf.run(); 166 } 167 } 168 169 | Popular Tags |