[Soot-list] regarding cfg's

John Jorgensen jorgnsn at lcd.uregina.ca
Sun Apr 23 14:48:05 EDT 2006


>>>>> "sunitha20" == sunitha pasagadugula <sunitha20 at gmail.com> writes:

    sunitha20> hello i'm a new user of soot, trying to draw cfg
    sunitha20> using the api (exceptional unit graphs) for my
    sunitha20> project.

I'm taking this to mean that your project is to draw the CFGs.
But just in case visualizing CFGs is not the main point of your
project, but just something you would like to be able to do to
help you understand some program, I should point out that Soot
already has some facilities for producing CFG visualizations:

A command like

 java -classpath $CLASS_PATH_INCLUDING_SOOT \
  soot.tools.CFGViewer  --soot-classpath $CLASS_PATH_INCLUDING_ARGUMENT_CLASS \
  --graph=ExceptionalUnitGraph --ir=grimp --show-exception-dests ArgumentClass

will create a file with the extension ".dot" for each method in
the argument class.  These files contain descriptions of the CFGs
in the dot language used by the graphviz package for graph
visualization (http://www.graphviz.org/).  There are various ways
to process the dot files. I usually turn them into PDF files and
use acroread to look at them. E.g.:

 dot -Gmargin=0.013 -Gsize=7.5,10 -Tps2 'sootOutput/void method2(int[],int,int,int).dot' | ps2pdf14 > method2.pdf
 acroread method2.pdf

You can also soot's "--dump-cfg <phase-name>" option to tell Soot
to create such dot files for the CFGs it creates internally for
its analyses. See the command line and phase options tutorials at
http://www.sable.mcgill.ca/soot/tutorial/ for more information
about "--dump-cfg" and about the values you can substitute for
<phase-name>.

I know almost nothing about Soot's eclipse plug-in, but I notice
that it includes a number of classes with cfg in their name, so
maybe the plug-in also has a GUI for inspecting CFG
visualizations.  Hopefully some other soot-list reader who is
familiar with eclipse will say more about that possibility.

    sunitha20> here i had a grimp file and i need draw cfg using
    sunitha20> that, but m very confused about converting the
    sunitha20> grimp file to grimp body 4 using in cfg
    sunitha20> APIs. should i need to use body transformers 4
    sunitha20> this? i will be very thankful if any one helps me
    sunitha20> understand this.

I don't believe that Soot has a grimp parser, so I don't think
Soot will be able to take grimp files as input unless you wrote a
parser for grimp. There's seems little point to that, though,
since presumably you could just use the class, jimple, or java
files that you used to produce the grimp files as input instead.

I'll attach a bare-bones example of adding a BodyTransformer to
Soot's gb stage that uses some of the CFGViewer infrastructure to
output a dot file corresponding to the CFG of each method that
Soot transforms into grimp. Maybe it will help clarify how to use
the APIs. I ran the program with the following 
command line (the classpaths would, of course, need to be
adjusted for your installation):

java \
 -classpath .:/nobackup/soot/trunk/classes:/nobackup/jasmin/trunk/classes:/nobackup/soot/polyglot.jar:/nobackup/soot/jedd-runtime.jar \
 GrimpGrapher  --via-grimp \
 --soot-classpath .:/usr/local/pkgs/java/jre/lib/rt.jar:/usr/local/pkgs/java/jre/lib/jce.jar:/nobackup/soot/trunk/classes:/nobackup/jasmin/trunk/classes:/nobackup/soot/polyglot.jar:/nobackup/soot/jedd-runtime.jar:${HOME}/sable/exceptions/cfg_eg/ExceptionalGraphDemo \
 ArgumentClass

-------------- next part --------------
import java.util.Map;
import soot.Body;
import soot.BodyTransformer;
import soot.G;
import soot.Main;
import soot.PackManager;
import soot.Transform;
import soot.options.Options;
import soot.toolkits.graph.ExceptionalUnitGraph;
import soot.util.cfgcmd.CFGToDotGraph;
import soot.util.dot.DotGraph;

/**
 * A class for constructing an inventory of the
 * ThrowableSet objects constructed during a Soot run.
 */
  
public class GrimpGrapher extends BodyTransformer {

  private static final String packToJoin = "gb";
  private static final String phaseSubname = "grimpgraph";
  private static final String phaseFullname = packToJoin + '.' + phaseSubname;
  CFGToDotGraph drawer = null;


  private GrimpGrapher() {
    drawer = new CFGToDotGraph();
    drawer.setBriefLabels(false);
    drawer.setOnePage(true);
    drawer.setUnexceptionalControlFlowAttr("color", "black");
    drawer.setExceptionalControlFlowAttr("color", "red");
    drawer.setExceptionEdgeAttr("color", "lightgray");
    drawer.setShowExceptions(true);
  }

  protected void internalTransform(Body b, String phase, Map options) {

    DotGraph graph = drawer.drawCFG(new ExceptionalUnitGraph(b));

    // Derive a name for the dot file from the input body:
    String methodname = b.getMethod().getSubSignature();
    String filename = soot.SourceLocator.v().getOutputDir();
    if (filename.length() > 0) {
	filename = filename + java.io.File.separator;
    }
    filename = filename + 
      methodname.replace(java.io.File.separatorChar, '.') + 
      DotGraph.DOT_EXTENSION;
    G.v().out.println("Generate dot file in "+filename);
    graph.plot(filename);
  }

  public static void main(String[] args) {
    GrimpGrapher grapher = new GrimpGrapher();
    Transform graphTransform = new Transform(phaseFullname, grapher);
    graphTransform.setDeclaredOptions("enabled ");
    graphTransform.setDefaultOptions("enabled ");
    PackManager.v().getPack(packToJoin).add(graphTransform);
    soot.Main.main(args);
  }
}


More information about the Soot-list mailing list