使用Soot运行功能

时间:2013-03-02 21:28:04

标签: soot

我正在尝试使用soot来测量特定类中每个函数的执行时间。我曾尝试阅读Eric Bodden撰写的所有Soot Framework教程。

到目前为止我所得到的是

package test;

import java.util.Map;

import soot.Body;
import soot.BodyTransformer;
import soot.PackManager;
import soot.Scene;
import soot.SootClass;
import soot.SootMethod;
import soot.Transform;

public class MyMain {
    public static void main(String[] args) {

        PackManager
                .v()
                .getPack("jtp")
                .add(new Transform("jtp.GotoInstrumenter", GotoInstrumenter.v()));

        System.out.println(Scene.v().defaultClassPath());
        SootClass sootClass = Scene.v().loadClassAndSupport("test.TestClass");


        if (sootClass == null || !(sootClass instanceof SootClass)) {
            System.out.println("sootClass not initialized");
            System.exit(0);
        } else {
            System.out.println(sootClass.getMethodCount());
        }
        sootClass.setApplicationClass();
        for (SootMethod m : sootClass.getMethods()) {

            try {
                new Transform("jtp.GotoInstrumenter", GotoInstrumenter.v())
                        .apply(m.retrieveActiveBody());
            } catch (Exception e) {
                System.out.println("Exeception in for loop : " + e);
            }
        }

    }

}

@SuppressWarnings("all")
class GotoInstrumenter extends BodyTransformer {
    private static GotoInstrumenter instance = new GotoInstrumenter();

    private GotoInstrumenter() {
    }

    public static GotoInstrumenter v() {
        return instance;
    }

    protected void internalTransform(Body body, String phaseName, Map options) {

        System.out.println("Processing method : "
                + body.getMethod().getSignature());
    }
}

这是我的TestClass

package test;

public class TestClass {
    public static void main(String[] args) {
        System.out.println("I am in test class");
    }
    public void f1(){
        System.out.println(Math.pow(2, 10));
    }
}

我已经在eclipse中使用add变量添加了sootclasses并像Java Application一样执行它。

这是我的节目输出:

C:\Users\Aks\Java\soot-2.5.0.jar;C:\Users\Aks\workspace\SootAnalysis\bin;D:\Installs\eclipse\plugins\ca.mcgill.sable.soot.lib_2.5.2\lib\sootclasses.jar;;C:\Program Files\Java\jre7\lib\rt.jar
3
Processing method : <test.TestClass: void <init>()>
Processing method : <test.TestClass: void main(java.lang.String[])>
Processing method : <test.TestClass: void f1()>

现在我明白了Soot已经读取了字节码并且正在逐步完成所有方法。我想做的是,运行每一个。这是否可以使用Soot Framework ??

1 个答案:

答案 0 :(得分:2)

可能这不是人们想要使用烟灰的原因。我不知道是否有任何优雅的方法来使用烟灰。但是,当然,你可以在这里利用反射的力量。在 internalTransform()方法的末尾添加以下行应该有效:

TestClass.class.getMethod(body.getMethod().getName()).invoke(new TestClass());

当然,类 TestClass 中没有类似 init()的方法。所以,你不想调用那个方法。此外,对于 main()方法,您需要根据反射语法指定正确的参数。否则,会有例外。

希望它会有所帮助。谢谢。