资源简介
在java工程中利用soot-jar包,生成java程序的dot文件,再利用graphviz画出java程序的控制流图。
代码片段和文件信息
package soot;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import soot.Body;
import soot.BodyTransformer;
import soot.G;
import soot.PackManager;
import soot.PhaseOptions;
import soot.SootMethod;
import soot.Transform;
import soot.Unit;
import soot.jimple.JimpleBody;
import soot.options.Options;
import soot.toolkits.graph.DirectedGraph;
import soot.toolkits.graph.UnitGraph;
import soot.util.cfgcmd.AltClassLoader;
import soot.util.cfgcmd.CFGGraphType;
import soot.util.cfgcmd.CFGIntermediateRep;
import soot.util.cfgcmd.CFGToDotGraph;
import soot.util.dot.DotGraph;
public class Generate extends BodyTransformer {
private static final String altClassPathOptionName = “alt-class-path“;
private static final String graphTypeOptionName = “graph-type“;
private static final String defaultGraph = “BriefUnitGraph“;
private static final String irOptionName = “ir“;
private static final String defaultIR = “jimple“;
private static final String multipageOptionName = “multipages“;
private static final String briefLabelOptionName = “brief“;
private CFGGraphType graphtype;
private CFGIntermediateRep ir;
private CFGToDotGraph drawer;
protected void internalTransform(Body b String phaseName Map options) {
initialize(options);
System.out.println(options);
System.out.println(b);
Body body = ir.getBody((JimpleBody) b);
//System.out.println(body);
print_cfg(body);
}
public static void main(String[] args) {
Generate viewer = new Generate();
Transform printTransform = new Transform(“jtp.printcfg“ viewer);
printTransform.setDeclaredOptions(“enabled “ + altClassPathOptionName + ‘ ‘ + graphTypeOptionName + ‘ ‘
+ irOptionName + ‘ ‘ + multipageOptionName + ‘ ‘ + briefLabelOptionName + ‘ ‘);
printTransform.setDefaultOptions(“enabled “ + altClassPathOptionName + “: “ + graphTypeOptionName + ‘:‘
+ defaultGraph + ‘ ‘ + irOptionName + ‘:‘ + defaultIR + ‘ ‘ + multipageOptionName + “:false “ + ‘ ‘
+ briefLabelOptionName + “:false “);
PackManager.v().getPack(“jtp“).add(printTransform);
//args = viewer.parse_options(args);
String[] soot_args = new String[3];
//soot_args[0] = “-cp“;
soot_args[0] = “--soot-classpath“;
soot_args[1] = “bin/;C:/Program Files/Java/jre1.8.0_121/lib/rt.jar“;
soot_args[2] =“TriangleClass.Triangle“;
soot.Main.main(soot_args);
}
private void initialize(Map options) {
if (drawer == null) {
drawer = new CFGToDotGraph();
drawer.setBriefLabels(PhaseOptions.getBoolean(options briefLabelOptionName));
drawer.setOnePage(!PhaseOptions.getBoolean(options multipageOptionName));
drawer.setUnexceptionalControlFlowAttr(“color“ “black“);
drawer.setExceptionalControlFlowAttr(“color“ “red“);
drawer.setExceptionEdgeAttr(“color“ “lightgray“);
drawer.setShowExceptions(Options.v().show_exception_dests(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2017-03-23 20:36 CfgDemo\
文件 358 2017-03-23 20:36 CfgDemo\.classpath
文件 383 2017-03-23 14:58 CfgDemo\.project
目录 0 2017-03-23 14:58 CfgDemo\.settings\
文件 598 2017-03-23 14:58 CfgDemo\.settings\org.eclipse.jdt.core.prefs
目录 0 2017-03-23 20:36 CfgDemo\bin\
目录 0 2017-03-23 20:36 CfgDemo\bin\TriangleClass\
文件 623 2017-03-23 20:36 CfgDemo\bin\TriangleClass\Triangle.class
目录 0 2017-03-23 20:36 CfgDemo\bin\soot\
文件 6185 2017-03-23 20:36 CfgDemo\bin\soot\Generate.class
目录 0 2017-03-23 20:35 CfgDemo\lib\
文件 13892031 2017-03-23 20:35 CfgDemo\lib\soot-trunk.jar
目录 0 2017-03-23 20:37 CfgDemo\sootOutput\
目录 0 2017-03-23 20:36 CfgDemo\sootOutput\TriangleClass\
文件 399 2017-03-23 20:36 CfgDemo\sootOutput\TriangleClass\Triangle.class
文件 1498 2017-03-23 20:36 CfgDemo\sootOutput\TriangleClass.Triangle java.lang.String triangle(intintint).dot
目录 0 2017-03-23 20:36 CfgDemo\src\
目录 0 2017-03-23 14:59 CfgDemo\src\TriangleClass\
文件 816 2017-03-23 15:00 CfgDemo\src\TriangleClass\Triangle.java
目录 0 2017-03-23 20:35 CfgDemo\src\soot\
文件 4585 2017-03-23 19:40 CfgDemo\src\soot\Generate.java
评论
共有 条评论