2012-10-03 79 views
1

我想用Soot做一個Java程序的靜態分析,包括例如控制流圖。是否可以在不調用soot.Main.main(...)的情況下使用Soot分析?

各種tutorials說的「標準方式」使用煤煙是要創造一個一個添加自定義的轉換到煙塵管道,然後調用soot.Main.main(...)主要方法:

public static void main(String[] args) {   
    PackManager.v().getPack("jtp").add(
     new Transform("jtp.gotoinstrumenter", GotoInstrumenter.v())); 
    soot.Main.main(args); 
} 

當然,這如果您想在除命令行工具之外的其他位置使用Soot,則會有一些嚴重的限制。例如,我不清楚在程序中多次調用Soot的主要方法是否合法。

那麼是否有人知道可以通過一個更復雜一點的API直接使用菸灰分析工具?

回答

7

答案是肯定的。在你的主,你可以設置你一起工作的類:

configure("../yourClasspath/"); 
SootClass sootClass = Scene.v().loadClassAndSupport("className"); 
sootClass.setApplicationClass(); 

// Retrieve the method and its body 
SootMethod m = c.getMethodByName("methodName"); 
Body b = m.retrieveActiveBody(); 

// Instruments bytecode 
new YourTransform().transform(b); 

之後,你可能會建立CFG並運行一些分析。

它遵循的配置方法:

public static void configure(String classpath) { 

     Options.v().set_verbose(false); 
     Options.v().set_keep_line_number(true); 
     Options.v().set_src_prec(Options.src_prec_class); 
     Options.v().set_soot_classpath(classpath); 
     Options.v().set_prepend_classpath(true); 

     PhaseOptions.v().setPhaseOption("bb", "off"); 
     PhaseOptions.v().setPhaseOption("tag.ln", "on"); 
     PhaseOptions.v().setPhaseOption("jj.a", "on"); 
     PhaseOptions.v().setPhaseOption("jj.ule", "on"); 

     Options.v().set_whole_program(true); 
    } 
+2

謝謝。自從我問這個問題以來,我學到了很多關於Soot的知識。我發現'G.reset()'對於單元測試非常有用。 – rolve

+0

好的@rolve。 –

相關問題