Search in sources :

Example 1 with DotGraphNode

use of soot.util.dot.DotGraphNode in project soot by Sable.

the class CFGToDotGraph method drawCFG.

/**
 * Create a <code>DotGraph</code> whose nodes and edges depict the
 * control flow in a <code>ExceptionalGraph</code>, with
 * distinguished edges for exceptional control flow.
 *
 * @param graph the control flow graph
 *
 * @return a visualization of <code>graph</code>.
 */
public <N> DotGraph drawCFG(ExceptionalGraph<N> graph) {
    Body body = graph.getBody();
    DotGraph canvas = initDotGraph(body);
    DotNamer namer = new DotNamer((int) (graph.size() / 0.7f), 0.7f);
    NodeComparator nodeComparator = new NodeComparator(namer);
    // comparisons between graphs of a given method.
    for (Iterator<N> nodesIt = graph.iterator(); nodesIt.hasNext(); ) {
        namer.getName(nodesIt.next());
    }
    for (Iterator<N> nodesIt = graph.iterator(); nodesIt.hasNext(); ) {
        N node = nodesIt.next();
        canvas.drawNode(namer.getName(node));
        for (Iterator<N> succsIt = sortedIterator(graph.getUnexceptionalSuccsOf(node), nodeComparator); succsIt.hasNext(); ) {
            N succ = succsIt.next();
            DotGraphEdge edge = canvas.drawEdge(namer.getName(node), namer.getName(succ));
            edge.setAttribute(unexceptionalControlFlowAttr);
        }
        for (Iterator<N> succsIt = sortedIterator(graph.getExceptionalSuccsOf(node), nodeComparator); succsIt.hasNext(); ) {
            N succ = succsIt.next();
            DotGraphEdge edge = canvas.drawEdge(namer.getName(node), namer.getName(succ));
            edge.setAttribute(exceptionalControlFlowAttr);
        }
        if (showExceptions) {
            for (Iterator<ExceptionDest<N>> destsIt = sortedIterator(graph.getExceptionDests(node), new ExceptionDestComparator(namer)); destsIt.hasNext(); ) {
                ExceptionDest<N> dest = destsIt.next();
                Object handlerStart = dest.getHandlerNode();
                if (handlerStart == null) {
                    // Giving each escaping exception its own, invisible
                    // exceptional exit node produces a less cluttered
                    // graph.
                    handlerStart = new Object() {

                        public String toString() {
                            return "Esc";
                        }
                    };
                    DotGraphNode escapeNode = canvas.drawNode(namer.getName(handlerStart));
                    escapeNode.setStyle(DotGraphConstants.NODE_STYLE_INVISIBLE);
                }
                DotGraphEdge edge = canvas.drawEdge(namer.getName(node), namer.getName(handlerStart));
                edge.setAttribute(exceptionEdgeAttr);
                edge.setLabel(formatThrowableSet(dest.getThrowables()));
            }
        }
    }
    setStyle(graph.getHeads(), canvas, namer, DotGraphConstants.NODE_STYLE_FILLED, headAttr);
    setStyle(graph.getTails(), canvas, namer, DotGraphConstants.NODE_STYLE_FILLED, tailAttr);
    if (!isBrief) {
        formatNodeText(graph.getBody(), canvas, namer);
    }
    return canvas;
}
Also used : ExceptionDest(soot.toolkits.graph.ExceptionalGraph.ExceptionDest) DotGraphEdge(soot.util.dot.DotGraphEdge) DotGraph(soot.util.dot.DotGraph) DotGraphNode(soot.util.dot.DotGraphNode)

Example 2 with DotGraphNode

use of soot.util.dot.DotGraphNode in project soot by Sable.

the class AbstractInterproceduralAnalysis method drawAsOneDot.

/**
 * Dump the interprocedural analysis result as a graph. One node / subgraph
 * for each analysed method that contains the method summary, and call-to
 * edges.
 *
 * Note: this graph does not show filtered-out methods for which a
 * conservative summary was asked via summaryOfUnanalysedMethod.
 *
 * @param name output filename
 *
 * @see fillDotGraph
 */
public void drawAsOneDot(String name) {
    DotGraph dot = new DotGraph(name);
    dot.setGraphLabel(name);
    dot.setGraphAttribute("compound", "true");
    // dot.setGraphAttribute("rankdir","LR");
    int id = 0;
    Map<SootMethod, Integer> idmap = new HashMap<SootMethod, Integer>();
    // draw sub-graph cluster
    for (SootMethod m : dg) {
        DotGraph sub = dot.createSubGraph("cluster" + id);
        DotGraphNode label = sub.drawNode("head" + id);
        idmap.put(m, id);
        sub.setGraphLabel("");
        label.setLabel("(" + order.get(m) + ") " + m.toString());
        label.setAttribute("fontsize", "18");
        label.setShape("box");
        if (data.containsKey(m)) {
            fillDotGraph("X" + id, data.get(m), sub);
        }
        id++;
    }
    // connect edges
    for (SootMethod m : dg) {
        for (SootMethod mm : dg.getSuccsOf(m)) {
            DotGraphEdge edge = dot.drawEdge("head" + idmap.get(m), "head" + idmap.get(mm));
            edge.setAttribute("ltail", "cluster" + idmap.get(m));
            edge.setAttribute("lhead", "cluster" + idmap.get(mm));
        }
    }
    File f = new File(SourceLocator.v().getOutputDir(), name + DotGraph.DOT_EXTENSION);
    dot.plot(f.getPath());
}
Also used : DotGraphEdge(soot.util.dot.DotGraphEdge) DotGraph(soot.util.dot.DotGraph) SootMethod(soot.SootMethod) DotGraphNode(soot.util.dot.DotGraphNode) File(java.io.File)

Example 3 with DotGraphNode

use of soot.util.dot.DotGraphNode in project soot by Sable.

the class PurityIntraproceduralAnalysis method drawAsOneDot.

/**
 * Draw the result of the intra-procedural analysis as one big dot file,
 * named className.methodName.dot, containing one purity graph for each
 * statement in the method.
 *
 * @param prefix
 * @param name
 */
public void drawAsOneDot(String prefix, String name) {
    DotGraph dot = new DotGraph(name);
    dot.setGraphLabel(name);
    dot.setGraphAttribute("compound", "true");
    dot.setGraphAttribute("rankdir", "LR");
    Map<Unit, Integer> node = new HashMap<Unit, Integer>();
    int id = 0;
    for (Unit stmt : graph) {
        PurityGraphBox ref = getFlowAfter(stmt);
        DotGraph sub = dot.createSubGraph("cluster" + id);
        DotGraphNode label = sub.drawNode("head" + id);
        String lbl = stmt.toString();
        if (lbl.startsWith("lookupswitch")) {
            lbl = "lookupswitch...";
        }
        if (lbl.startsWith("tableswitch")) {
            lbl = "tableswitch...";
        }
        sub.setGraphLabel(" ");
        label.setLabel(lbl);
        label.setAttribute("fontsize", "18");
        label.setShape("box");
        ref.g.fillDotGraph("X" + id, sub);
        node.put(stmt, id);
        id++;
    }
    for (Unit src : graph) {
        for (Unit dst : graph.getSuccsOf(src)) {
            DotGraphEdge edge = dot.drawEdge("head" + node.get(src), "head" + node.get(dst));
            edge.setAttribute("ltail", "cluster" + node.get(src));
            edge.setAttribute("lhead", "cluster" + node.get(dst));
        }
    }
    File f = new File(SourceLocator.v().getOutputDir(), prefix + name + DotGraph.DOT_EXTENSION);
    dot.plot(f.getPath());
}
Also used : HashMap(java.util.HashMap) DotGraphEdge(soot.util.dot.DotGraphEdge) DotGraph(soot.util.dot.DotGraph) DotGraphNode(soot.util.dot.DotGraphNode) File(java.io.File)

Example 4 with DotGraphNode

use of soot.util.dot.DotGraphNode in project soot by Sable.

the class CFGToDotGraph method setStyle.

/**
 * Utility routine for setting some common formatting style for the
 * {@link DotGraphNode}s corresponding to some collection of objects.
 *
 * @param objects is the collection of {@link Object}s whose
 *        nodes are to be styled.
 * @param canvas the {@link DotGraph} containing nodes corresponding
 *        to the collection.
 * @param namer maps from {@link Object} to the strings used
 *        to identify corresponding {@link DotGraphNode}s.
 * @param style the style to set for each of the nodes.
 * @param attrib if non-null, an additional attribute to associate
 *        with each of the nodes.
 */
private <N> void setStyle(Collection<? extends N> objects, DotGraph canvas, DotNamer<N> namer, String style, DotGraphAttribute attrib) {
    // Fill the entry and exit nodes.
    for (N object : objects) {
        DotGraphNode objectNode = canvas.getNode(namer.getName(object));
        objectNode.setStyle(style);
        objectNode.setAttribute(attrib);
    }
}
Also used : DotGraphNode(soot.util.dot.DotGraphNode)

Example 5 with DotGraphNode

use of soot.util.dot.DotGraphNode in project soot by Sable.

the class CFGToDotGraph method formatNodeText.

/**
 * A utility method which formats the text for each node in
 * a <code>DotGraph</code> representing a CFG.
 *
 * @param body the <code>Body</code> whose control flow is visualized in
 *             <code>canvas</code>.
 *
 * @param canvas the <code>DotGraph</code> for visualizing the CFG.
 *
 * @param namer provides a mapping from CFG objects to identifiers in
 *              generated dot source.
 */
private <N> void formatNodeText(Body body, DotGraph canvas, DotNamer<N> namer) {
    LabeledUnitPrinter printer = null;
    if (body != null) {
        printer = new BriefUnitPrinter(body);
        printer.noIndent();
    }
    for (Iterator<N> nodesIt = namer.keySet().iterator(); nodesIt.hasNext(); ) {
        N node = nodesIt.next();
        DotGraphNode dotnode = canvas.getNode(namer.getName(node));
        String nodeLabel = null;
        if (node instanceof DominatorNode) {
            node = ((DominatorNode<N>) node).getGode();
        }
        if (printer == null) {
            nodeLabel = node.toString();
        } else {
            if (node instanceof Unit) {
                ((Unit) node).toString(printer);
                String targetLabel = printer.labels().get(node);
                if (targetLabel == null) {
                    nodeLabel = printer.toString();
                } else {
                    nodeLabel = targetLabel + ": " + printer.toString();
                }
            } else if (node instanceof Block) {
                Iterator<Unit> units = ((Block) node).iterator();
                StringBuffer buffer = new StringBuffer();
                while (units.hasNext()) {
                    Unit unit = units.next();
                    String targetLabel = (String) printer.labels().get(unit);
                    if (targetLabel != null) {
                        buffer.append(targetLabel).append(":\\n");
                    }
                    unit.toString(printer);
                    buffer.append(printer.toString()).append("\\l");
                }
                nodeLabel = buffer.toString();
            } else {
                nodeLabel = node.toString();
            }
        }
        dotnode.setLabel(nodeLabel);
    }
}
Also used : DominatorNode(soot.toolkits.graph.DominatorNode) Block(soot.toolkits.graph.Block) DotGraphNode(soot.util.dot.DotGraphNode)

Aggregations

DotGraphNode (soot.util.dot.DotGraphNode)5 DotGraph (soot.util.dot.DotGraph)3 DotGraphEdge (soot.util.dot.DotGraphEdge)3 File (java.io.File)2 HashMap (java.util.HashMap)1 SootMethod (soot.SootMethod)1 Block (soot.toolkits.graph.Block)1 DominatorNode (soot.toolkits.graph.DominatorNode)1 ExceptionDest (soot.toolkits.graph.ExceptionalGraph.ExceptionDest)1