1 21 22 25 import soot.*; 26 import soot.jimple.*; 27 import soot.util.*; 28 import java.util.*; 29 30 public class InvokeStaticInstrumenter extends BodyTransformer{ 31 32 33 static SootClass counterClass; 34 static SootMethod increaseCounter, reportCounter; 35 36 static { 37 counterClass = Scene.v().loadClassAndSupport("MyCounter"); 38 increaseCounter = counterClass.getMethod("void increase(int)"); 39 reportCounter = counterClass.getMethod("void report()"); 40 } 41 42 45 protected void internalTransform(Body body, String phase, Map options) { 46 SootMethod method = body.getMethod(); 48 49 System.out.println("instrumenting method : " + method.getSignature()); 51 52 Chain units = body.getUnits(); 54 55 Iterator stmtIt = units.snapshotIterator(); 59 60 while (stmtIt.hasNext()) { 62 63 Stmt stmt = (Stmt)stmtIt.next(); 65 66 if (!stmt.containsInvokeExpr()) { 71 continue; 72 } 73 74 InvokeExpr expr = (InvokeExpr)stmt.getInvokeExpr(); 76 77 if (! (expr instanceof StaticInvokeExpr)) { 79 continue; 80 } 81 82 InvokeExpr incExpr= Jimple.v().newStaticInvokeExpr(increaseCounter.makeRef(), 87 IntConstant.v(1)); 88 Stmt incStmt = Jimple.v().newInvokeStmt(incExpr); 90 91 units.insertBefore(incStmt, stmt); 94 } 95 96 97 100 String signature = method.getSubSignature(); 102 boolean isMain = signature.equals("void main(java.lang.String[])"); 103 104 if (isMain) { 106 stmtIt = units.snapshotIterator(); 107 108 while (stmtIt.hasNext()) { 109 Stmt stmt = (Stmt)stmtIt.next(); 110 111 if ((stmt instanceof ReturnStmt) 113 ||(stmt instanceof ReturnVoidStmt)) { 114 InvokeExpr reportExpr= Jimple.v().newStaticInvokeExpr(reportCounter.makeRef()); 116 117 Stmt reportStmt = Jimple.v().newInvokeStmt(reportExpr); 119 120 units.insertBefore(reportStmt, stmt); 123 } 124 } 125 } 126 } 127 } 128
| Popular Tags
|