2006-11-22 17:38:46 +00:00
|
|
|
/*******************************************************************************
|
|
|
|
* Copyright (c) 2002 - 2006 IBM Corporation.
|
|
|
|
* All rights reserved. This program and the accompanying materials
|
|
|
|
* are made available under the terms of the Eclipse Public License v1.0
|
|
|
|
* which accompanies this distribution, and is available at
|
|
|
|
* http://www.eclipse.org/legal/epl-v10.html
|
|
|
|
*
|
|
|
|
* Contributors:
|
|
|
|
* IBM Corporation - initial API and implementation
|
|
|
|
*******************************************************************************/
|
|
|
|
package com.ibm.wala.viz;
|
|
|
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
|
|
|
import com.ibm.wala.cfg.CFGSanitizer;
|
2007-06-01 03:26:18 +00:00
|
|
|
import com.ibm.wala.ipa.cha.IClassHierarchy;
|
2006-11-22 17:38:46 +00:00
|
|
|
import com.ibm.wala.ssa.IR;
|
2007-09-02 14:45:41 +00:00
|
|
|
import com.ibm.wala.ssa.ISSABasicBlock;
|
2006-11-22 17:38:46 +00:00
|
|
|
import com.ibm.wala.ssa.SSACFG;
|
2011-04-03 04:08:05 +00:00
|
|
|
import com.ibm.wala.ssa.SSACFG.BasicBlock;
|
|
|
|
import com.ibm.wala.ssa.SSACFG.ExceptionHandlerBasicBlock;
|
2006-11-22 17:38:46 +00:00
|
|
|
import com.ibm.wala.ssa.SSAGetCaughtExceptionInstruction;
|
|
|
|
import com.ibm.wala.ssa.SSAInstruction;
|
|
|
|
import com.ibm.wala.ssa.SSAPhiInstruction;
|
|
|
|
import com.ibm.wala.ssa.SSAPiInstruction;
|
2011-08-16 16:51:11 +00:00
|
|
|
import com.ibm.wala.util.WalaException;
|
2007-07-11 21:08:12 +00:00
|
|
|
import com.ibm.wala.util.collections.HashMapFactory;
|
2006-11-22 17:38:46 +00:00
|
|
|
import com.ibm.wala.util.graph.Graph;
|
2008-01-24 22:05:28 +00:00
|
|
|
import com.ibm.wala.util.strings.StringStuff;
|
2006-11-22 17:38:46 +00:00
|
|
|
|
|
|
|
/**
|
2009-04-29 18:04:03 +00:00
|
|
|
* utilities for integrating with ghostview (or another PS/PDF viewer)
|
2006-11-22 17:38:46 +00:00
|
|
|
*/
|
2009-04-03 18:25:05 +00:00
|
|
|
public class PDFViewUtil {
|
2006-11-22 17:38:46 +00:00
|
|
|
|
|
|
|
/**
|
2009-04-29 18:04:03 +00:00
|
|
|
* spawn a process to view a WALA IR
|
2006-11-22 17:38:46 +00:00
|
|
|
*
|
|
|
|
* @return a handle to the ghostview process
|
|
|
|
*/
|
2009-04-29 18:04:03 +00:00
|
|
|
public static Process ghostviewIR(IClassHierarchy cha, IR ir, String pdfFile, String dotFile, String dotExe, String pdfViewExe)
|
2007-07-06 03:07:14 +00:00
|
|
|
throws WalaException {
|
2009-04-29 18:04:03 +00:00
|
|
|
return ghostviewIR(cha, ir, pdfFile, dotFile, dotExe, pdfViewExe, null);
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2009-04-29 18:04:03 +00:00
|
|
|
* spawn a process to view a WALA IR
|
2006-11-22 17:38:46 +00:00
|
|
|
*
|
2009-04-29 18:04:03 +00:00
|
|
|
* @return a handle to the pdf viewer process
|
2009-07-13 17:51:58 +00:00
|
|
|
* @throws IllegalArgumentException if ir is null
|
2006-11-22 17:38:46 +00:00
|
|
|
*/
|
2009-04-29 18:04:03 +00:00
|
|
|
public static Process ghostviewIR(IClassHierarchy cha, IR ir, String pdfFile, String dotFile, String dotExe, String pdfViewExe,
|
2007-07-06 03:07:14 +00:00
|
|
|
NodeDecorator annotations) throws WalaException {
|
2006-11-22 17:38:46 +00:00
|
|
|
|
2007-05-08 21:49:58 +00:00
|
|
|
if (ir == null) {
|
|
|
|
throw new IllegalArgumentException("ir is null");
|
|
|
|
}
|
2007-09-02 14:45:41 +00:00
|
|
|
Graph<? extends ISSABasicBlock> g = ir.getControlFlowGraph();
|
2006-11-22 17:38:46 +00:00
|
|
|
|
|
|
|
NodeDecorator labels = makeIRDecorator(ir);
|
|
|
|
if (annotations != null) {
|
2007-05-08 21:49:58 +00:00
|
|
|
labels = new ConcatenatingNodeDecorator(annotations, labels);
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g = CFGSanitizer.sanitize(ir, cha);
|
|
|
|
|
2009-04-29 18:04:03 +00:00
|
|
|
DotUtil.dotify(g, labels, dotFile, pdfFile, dotExe);
|
2006-11-22 17:38:46 +00:00
|
|
|
|
2009-04-29 18:04:03 +00:00
|
|
|
return launchPDFView(pdfFile, pdfViewExe);
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public static NodeDecorator makeIRDecorator(IR ir) {
|
2007-05-15 03:15:05 +00:00
|
|
|
if (ir == null) {
|
|
|
|
throw new IllegalArgumentException("ir is null");
|
|
|
|
}
|
2007-07-11 21:08:12 +00:00
|
|
|
final HashMap<BasicBlock, String> labelMap = HashMapFactory.make();
|
2007-03-21 21:07:30 +00:00
|
|
|
for (Iterator it = ir.getControlFlowGraph().iterator(); it.hasNext();) {
|
2006-11-22 17:38:46 +00:00
|
|
|
SSACFG.BasicBlock bb = (SSACFG.BasicBlock) it.next();
|
|
|
|
labelMap.put(bb, getNodeLabel(ir, bb));
|
|
|
|
}
|
|
|
|
NodeDecorator labels = new NodeDecorator() {
|
|
|
|
public String getLabel(Object o) {
|
|
|
|
return labelMap.get(o);
|
2007-05-08 21:49:58 +00:00
|
|
|
}
|
|
|
|
};
|
2006-11-22 17:38:46 +00:00
|
|
|
return labels;
|
|
|
|
}
|
2007-05-08 21:49:58 +00:00
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
/**
|
2009-07-13 17:51:58 +00:00
|
|
|
* A node decorator which concatenates the labels from two other node decorators
|
2006-11-22 17:38:46 +00:00
|
|
|
*/
|
|
|
|
private final static class ConcatenatingNodeDecorator implements NodeDecorator {
|
|
|
|
|
|
|
|
private final NodeDecorator A;
|
2007-05-08 21:49:58 +00:00
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
private final NodeDecorator B;
|
2007-05-08 21:49:58 +00:00
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
ConcatenatingNodeDecorator(NodeDecorator A, NodeDecorator B) {
|
|
|
|
this.A = A;
|
|
|
|
this.B = B;
|
|
|
|
}
|
2007-05-08 21:49:58 +00:00
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
public String getLabel(Object o) throws WalaException {
|
|
|
|
return A.getLabel(o) + B.getLabel(o);
|
|
|
|
}
|
2007-05-08 21:49:58 +00:00
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private static String getNodeLabel(IR ir, BasicBlock bb) {
|
|
|
|
StringBuffer result = new StringBuffer();
|
2007-05-08 21:49:58 +00:00
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
int start = bb.getFirstInstructionIndex();
|
|
|
|
int end = bb.getLastInstructionIndex();
|
|
|
|
result.append("BB").append(bb.getNumber());
|
|
|
|
if (bb.isEntryBlock()) {
|
|
|
|
result.append(" (en)\\n");
|
|
|
|
} else if (bb.isExitBlock()) {
|
|
|
|
result.append(" (ex)\\n");
|
|
|
|
}
|
|
|
|
if (bb instanceof ExceptionHandlerBasicBlock) {
|
|
|
|
result.append("<Handler>");
|
|
|
|
}
|
|
|
|
result.append("\\n");
|
|
|
|
for (Iterator it = bb.iteratePhis(); it.hasNext();) {
|
|
|
|
SSAPhiInstruction phi = (SSAPhiInstruction) it.next();
|
|
|
|
if (phi != null) {
|
2009-03-09 17:29:24 +00:00
|
|
|
result.append(" " + phi.toString(ir.getSymbolTable())).append("\\l");
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (bb instanceof ExceptionHandlerBasicBlock) {
|
|
|
|
ExceptionHandlerBasicBlock ebb = (ExceptionHandlerBasicBlock) bb;
|
|
|
|
SSAGetCaughtExceptionInstruction s = ebb.getCatchInstruction();
|
|
|
|
if (s != null) {
|
2009-03-09 17:29:24 +00:00
|
|
|
result.append(" " + s.toString(ir.getSymbolTable())).append("\\l");
|
2006-11-22 17:38:46 +00:00
|
|
|
} else {
|
2009-03-09 17:29:24 +00:00
|
|
|
result.append(" " + " No catch instruction. Unreachable?\\l");
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
SSAInstruction[] instructions = ir.getInstructions();
|
|
|
|
for (int j = start; j <= end; j++) {
|
|
|
|
if (instructions[j] != null) {
|
2009-03-09 17:29:24 +00:00
|
|
|
StringBuffer x = new StringBuffer(j + " " + instructions[j].toString(ir.getSymbolTable()));
|
2006-11-22 17:38:46 +00:00
|
|
|
StringStuff.padWithSpaces(x, 35);
|
|
|
|
result.append(x);
|
2009-03-09 17:29:24 +00:00
|
|
|
result.append("\\l");
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for (Iterator it = bb.iteratePis(); it.hasNext();) {
|
|
|
|
SSAPiInstruction pi = (SSAPiInstruction) it.next();
|
|
|
|
if (pi != null) {
|
2009-03-09 17:29:24 +00:00
|
|
|
result.append(" " + pi.toString(ir.getSymbolTable())).append("\\l");
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result.toString();
|
|
|
|
}
|
2009-07-13 17:51:58 +00:00
|
|
|
|
2009-04-03 18:25:05 +00:00
|
|
|
/**
|
2009-04-29 18:04:03 +00:00
|
|
|
* Launch a process to view a PDF file
|
2009-04-03 18:25:05 +00:00
|
|
|
*/
|
|
|
|
public static Process launchPDFView(String pdfFile, String gvExe) throws WalaException {
|
|
|
|
// set up a viewer for the ps file.
|
|
|
|
if (gvExe == null) {
|
|
|
|
throw new IllegalArgumentException("null gvExe");
|
|
|
|
}
|
|
|
|
if (pdfFile == null) {
|
|
|
|
throw new IllegalArgumentException("null psFile");
|
|
|
|
}
|
|
|
|
final PDFViewLauncher gv = new PDFViewLauncher();
|
|
|
|
gv.setGvExe(gvExe);
|
|
|
|
gv.setPDFFile(pdfFile);
|
|
|
|
gv.run();
|
|
|
|
if (gv.getProcess() == null) {
|
|
|
|
throw new WalaException(" problem spawning process ");
|
|
|
|
}
|
|
|
|
return gv.getProcess();
|
|
|
|
}
|
|
|
|
|
2006-11-22 17:38:46 +00:00
|
|
|
}
|