Commit b9cf6b42 authored by Henrik Niehaus's avatar Henrik Niehaus
Browse files

Implemented analysis in DummyDataModel. Also added EventAdmin

parent d4b7a074
......@@ -27,7 +27,7 @@ public class CFGViewPart extends ViewPart {
}
Composite composite = new Composite(parent, SWT.EMBEDDED | SWT.NO_BACKGROUND);
GraphManager manager = new GraphManager("test", "url('file:../../styles/stylesheet.css')");
GraphManager manager = new GraphManager("test", "url('file:styles/stylesheet.css')");
Thread t = new Thread(manager);
t.start();
......
......@@ -9,8 +9,8 @@ import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
......@@ -38,12 +38,14 @@ import org.graphstream.ui.view.View;
import org.graphstream.ui.view.Viewer;
import org.graphstream.ui.view.ViewerListener;
import org.graphstream.ui.view.ViewerPipe;
import org.osgi.service.event.Event;
import org.osgi.service.event.EventConstants;
import org.osgi.service.event.EventHandler;
import de.unipaderborn.visuflow.model.DataModel;
import de.unipaderborn.visuflow.model.VFClass;
import de.unipaderborn.visuflow.model.VFMethod;
import de.unipaderborn.visuflow.util.ServiceUtil;
import de.visuflow.callgraph.CallGraphGenerator;
import de.visuflow.callgraph.ControlFlowGraph;
public class GraphManager implements Runnable, ViewerListener {
......@@ -62,7 +64,7 @@ public class GraphManager implements Runnable, ViewerListener {
JToolBar settingsBar;
JTextField attribute;
JScrollPane scrollbar;
JComboBox<String> methodList;
JComboBox<VFMethod> methodList;
double zoomInDelta, zoomOutDelta, maxZoomPercent, minZoomPercent;
......@@ -81,6 +83,18 @@ public class GraphManager implements Runnable, ViewerListener {
this.styleSheet = styleSheet;
createGraph(graphName);
createUI();
EventHandler dataModelHandler = new EventHandler() {
@Override
public void handleEvent(Event event) {
@SuppressWarnings("unchecked")
List<VFClass> vfClasses = (List<VFClass>) event.getProperty("model");
System.out.println("Model changed " + vfClasses.size() + " " + vfClasses);
}
};
Hashtable<String, String> properties = new Hashtable<String, String>();
properties.put(EventConstants.EVENT_TOPIC, DataModel.EA_TOPIC_DATA_MODEL_CHANGED);
ServiceUtil.registerService(EventHandler.class, dataModelHandler, properties);
}
public Container getApplet() {
......@@ -146,8 +160,8 @@ public class GraphManager implements Runnable, ViewerListener {
private void createMethodComboBox()
{
methodList = new JComboBox<String>();
methodList.addItem("Select Method");
methodList = new JComboBox<VFMethod>();
// methodList.addItem("Select Method");
methodList.addActionListener(new ActionListener() {
......@@ -159,7 +173,10 @@ public class GraphManager implements Runnable, ViewerListener {
if(analysisData == null)
System.out.println("analysis data is null");
try {
//renderMethodCFG((ControlFlowGraph) analysisData.values().toArray()[methodBox.getSelectedIndex()-1]);
VFMethod selectedMethod = (VFMethod) methodBox.getSelectedItem();
System.out.println(selectedMethod.getControlFlowGraph().listEdges.size());
System.out.println(selectedMethod.getControlFlowGraph().listNodes.size());
renderMethodCFG(selectedMethod.getControlFlowGraph());
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
......@@ -215,10 +232,10 @@ public class GraphManager implements Runnable, ViewerListener {
while(nodes.hasNext())
{
Node curr = (Node) nodes.next();
System.out.println("left " + curr.getAttribute("leftOp"));
System.out.println("right " + curr.getAttribute("rightOp"));
// System.out.println("left " + curr.getAttribute("leftOp"));
// System.out.println("right " + curr.getAttribute("rightOp"));
}
System.out.println("Node to display pop info about " + ((View) e.getComponent()).allNodesOrSpritesIn(e.getX(), e.getY(), e.getX(), e.getY()));
//System.out.println("Node to display pop info about " + ((View) e.getComponent()).allNodesOrSpritesIn(e.getX(), e.getY(), e.getX(), e.getY()));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
......@@ -358,16 +375,14 @@ public class GraphManager implements Runnable, ViewerListener {
void generateGraphFromGraphStructure()
{
CallGraphGenerator generator = new CallGraphGenerator();
analysisData = new ArrayList<>();
generator.runAnalysis(analysisData);
DataModel dataModel = ServiceUtil.getService(DataModel.class);
List<VFMethod> methods = dataModel.listMethods(null);
for (VFMethod vfMethod : methods) {
methodList.addItem(vfMethod.getSootMethod().getName());
analysisData = dataModel.listClasses();
if(!analysisData.isEmpty()) {
VFClass first = analysisData.get(0);
for (VFMethod vfMethod : first.getMethods()) {
methodList.addItem(vfMethod);
}
}
}
private void renderMethodCFG(ControlFlowGraph interGraph) throws Exception
......
......@@ -4,7 +4,6 @@ import java.util.ArrayList;
import java.util.List;
import soot.SootClass;
import soot.SootMethod;
public class VFClass {
......@@ -14,9 +13,6 @@ public class VFClass {
public VFClass(SootClass sootClass) {
this.wrapped = sootClass;
for (SootMethod sootMethod : sootClass.getMethods()) {
methods.add(new VFMethod(sootMethod));
}
}
public SootClass getSootClass() {
......
......@@ -6,7 +6,6 @@ import java.util.List;
import de.visuflow.callgraph.ControlFlowGraph;
import soot.Body;
import soot.SootMethod;
import soot.Unit;
public class VFMethod {
......@@ -17,9 +16,6 @@ public class VFMethod {
public VFMethod(SootMethod wrapped) {
this.wrapped = wrapped;
for (Unit unit : wrapped.getActiveBody().getUnits()) {
units.add(new VFUnit(unit));
}
}
public SootMethod getSootMethod() {
......@@ -45,5 +41,10 @@ public class VFMethod {
public void setControlFlowGraph(ControlFlowGraph controlFLowGraph) {
this.controlFlowGraph = controlFLowGraph;
}
@Override
public String toString() {
return wrapped != null ? /*wrapped.getDeclaringClass().getName()+"."+*/ wrapped.getName() : super.toString();
}
}
package de.unipaderborn.visuflow.model.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Dictionary;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.event.Event;
......@@ -17,38 +14,41 @@ import de.unipaderborn.visuflow.model.DataModel;
import de.unipaderborn.visuflow.model.VFClass;
import de.unipaderborn.visuflow.model.VFMethod;
import de.unipaderborn.visuflow.model.VFUnit;
import de.visuflow.callgraph.CallGraphGenerator;
import de.visuflow.callgraph.ControlFlowGraph;
import de.visuflow.callgraph.ICFGStructure;
import de.visuflow.callgraph.JimpleModelAnalysis;
public class DummyDataModel implements DataModel {
private List<VFClass> analysisData = new ArrayList<VFClass>();
private List<VFClass> jimpleClasses = new ArrayList<VFClass>();
private EventAdmin eventAdmin;
@Override
public List<VFClass> listClasses() {
// TODO Auto-generated method stub
return null;
return jimpleClasses;
}
@Override
public List<VFMethod> listMethods(VFClass vfClass) {
List<VFMethod> methods = new ArrayList<VFMethod>();
// Iterator<Entry<VFMethod, ControlFlowGraph>> methodIterator = analysisData.entrySet().iterator();
// while(methodIterator.hasNext())
// {
// Entry<VFMethod, ControlFlowGraph> curr = methodIterator.next();
// VFMethod currMethod = curr.getKey();
// methods.add(currMethod);
// }
List<VFMethod> methods = Collections.emptyList();
for (VFClass current : jimpleClasses) {
if(current == vfClass) {
methods = vfClass.getMethods();
}
}
return methods;
}
@Override
public List<VFUnit> listUnits(VFMethod vfMethod) {
// TODO Auto-generated method stub
return null;
List<VFUnit> units = Collections.emptyList();
for (VFClass currentClass : jimpleClasses) {
for (VFMethod currentMethod : currentClass.getMethods()) {
if(currentMethod == vfMethod) {
units = vfMethod.getUnits();
}
}
}
return units;
}
public void setEventAdmin(EventAdmin eventAdmin) {
......@@ -57,13 +57,30 @@ public class DummyDataModel implements DataModel {
protected void activate(ComponentContext context)
{
CallGraphGenerator generator = new CallGraphGenerator();
generator.runAnalysis(analysisData);
ICFGStructure icfg = new ICFGStructure();
JimpleModelAnalysis analysis = new JimpleModelAnalysis();
analysis.createICFG(icfg, jimpleClasses);
Dictionary<String, Object> properties = new Hashtable<String, Object>();
properties.put("model", analysisData);
properties.put("model", jimpleClasses);
Event modelChanged = new Event(DataModel.EA_TOPIC_DATA_MODEL_CHANGED, properties);
eventAdmin.postEvent(modelChanged);
new Thread() {
public void run() {
try {
Thread.sleep(20000);
System.out.println("Fire!!!!");
Dictionary<String, Object> properties = new Hashtable<String, Object>();
properties.put("model", jimpleClasses);
Event modelChanged = new Event(DataModel.EA_TOPIC_DATA_MODEL_CHANGED, properties);
eventAdmin.postEvent(modelChanged);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
}.start();
}
protected void deactivate(ComponentContext context)
......
package de.unipaderborn.visuflow.util;
import java.util.Dictionary;
import org.osgi.framework.BundleContext;
import org.osgi.framework.FrameworkUtil;
import org.osgi.framework.ServiceException;
......@@ -19,4 +21,9 @@ public class ServiceUtil {
throw new ServiceException("Service "+serviceClass.getName()+" not available");
}
}
public static <T> void registerService(Class<T> serviceClass, T service, Dictionary<String, ?> properties) {
BundleContext ctx = FrameworkUtil.getBundle(ServiceUtil.class).getBundleContext();
ctx.registerService(serviceClass, service, properties);
}
}
package de.visuflow.callgraph;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import soot.Body;
import soot.Unit;
import soot.toolkits.graph.ExceptionalUnitGraph;
public class ControlFlowGraphGenerator {
static ExceptionalUnitGraph eg;
private int nodeNumber;
private int edgeNumber;
private List<Node> listNodes;
private List<Edge> listEdges;
public ControlFlowGraph generateControlFlowGraph(Body b) {
nodeNumber=0;
edgeNumber=0;
listNodes = new ArrayList<>();
listEdges = new ArrayList<>();
ControlFlowGraph g = new ControlFlowGraph();
Unit head = null;
eg = new ExceptionalUnitGraph(b);
List<Unit> list = eg.getHeads();
Iterator<Unit> it1 = list.iterator();
while (it1.hasNext()) {
head = it1.next();
nodeNumber++;
Node node = new Node(head, nodeNumber);
listNodes.add(node);
break;
}
traverseUnits(head);
g.listEdges = listEdges;
g.listNodes = listNodes;
return g;
}
private void traverseUnits(Unit currentNode) {
boolean present = false;
List<Unit> l = eg.getSuccsOf(currentNode);
Iterator<Unit> it = l.iterator();
while (it.hasNext()) {
Unit temp = it.next();
Iterator<Node> nodesIterator = listNodes.iterator();
while (nodesIterator.hasNext()) {
Node node = (Node) nodesIterator.next();
if (node.getLabel().equals(temp)) {
present = true;
}
}
if (!present) {
nodeNumber++;
Node node = new Node(temp, nodeNumber);
listNodes.add(node);
}
Node source = null, destination = null;
Iterator<Node> it1 = listNodes.iterator();
while (it1.hasNext()) {
Node node = (Node) it1.next();
if (node.getLabel().equals(currentNode)) {
source = node;
}
if (node.getLabel().equals(temp)) {
destination = node;
}
}
edgeNumber++;
Edge edgeEntry = new Edge(edgeNumber, source, destination);
listEdges.add(edgeEntry);
traverseUnits(temp);
}
}
}
package de.visuflow.callgraph;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import de.visuflow.callgraph.ICFGStructure;
import de.visuflow.callgraph.Method;
import de.visuflow.callgraph.Edge;
import soot.G;
import soot.Main;
import soot.PackManager;
import soot.Scene;
import soot.SceneTransformer;
import soot.SootMethod;
import soot.Transform;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Targets;
import soot.options.Options;
public class InterProceduralGraph {
public static int methodcount = 0;
public static int edgeCount = 0;
public static int methodNumber = 0;
public static List<Method> listMethods = new ArrayList<>();
public static List<Edge> listEdges = new ArrayList<>();
public static List<SootMethod> list = new ArrayList<>();
public static HashMap<Integer, SootMethod> methodMap = new HashMap<Integer, SootMethod>();
public static void main(String args[])
{
// ICFGStructure graph = new ICFGStructure();
// runAnalysis(graph);
}
public void createICFG(final ICFGStructure methodGraph) {
G.reset();
Transform transform = new Transform("wjtp.myTransform", new SceneTransformer() {
@Override
protected void internalTransform(String phase, Map<String, String> arg1) {
Options.v().set_keep_line_number(true);
Options.v().set_print_tags_in_output(true);
SootMethod entryMethod = null;
CallGraph cg =Scene.v().getCallGraph();
Scene.v().getClasses();
java.util.List<SootMethod> listMethod = Scene.v().getEntryPoints();
Iterator<SootMethod> iterEntryMethod = listMethod.iterator();
while(iterEntryMethod.hasNext())
{
entryMethod = iterEntryMethod.next();
if(entryMethod.isMain())
{
methodcount++;
Method method = new Method(methodcount, entryMethod);
listMethods.add(method);
break;
}
}
traverseMethods(entryMethod, cg);
/* Chain<SootClass> classChain = Scene.v().getApplicationClasses();
Iterator<SootClass> classIterator = classChain.iterator();
while(classIterator.hasNext())
{
SootClass sootClass = classIterator.next();
System.out.println(sootClass.getJavaSourceStartLineNumber());
System.out.println("======================="+sootClass+"===========================");
List<SootMethod> methodChain = sootClass.getMethods();
Iterator<SootMethod> methodIterator = methodChain.iterator();
while(methodIterator.hasNext())
{
SootMethod sootMethod = methodIterator.next();
if(sootMethod.hasActiveBody())
{
Body body = sootMethod.getActiveBody();
System.out.println(body);
methodNumber++;
System.out.println(methodNumber);
System.out.println(sootMethod);
methodMap.put(methodNumber, sootMethod);
// Chain<Unit> unitChain = body.getUnits();
// Iterator<Unit> unitIterator = unitChain.iterator();
// while(unitIterator.hasNext())
// {
// Unit unit = unitIterator.next();
// System.out.println("Unit is =============>"+unit);
// LineNumberTag tag = (LineNumberTag)unit.getTag("LineNumberTag");
// if(tag!=null)
// {
// System.out.println("Unit number is "+tag.getLineNumber());
// }
// List<Tag> listTags = unit.getTags();
// Iterator it = listTags.iterator();
// while(it.hasNext())
// {
// Tag ta = (Tag)it.next();
// System.out.println(ta.getName()+"<<<<===============>>>>"+ta.getValue());
// }
// }
}
}
}*/
//GraphStructure g = new GraphStructure();
//MainClass.runAnalysis(g, methodMap.get(1));
}
});
PackManager.v().getPack("wjtp").add(transform);
String rtJar = System.getProperty("java.home") + File.separator + "lib" + File.separator + "rt.jar";
// Run Soot
Main.main(new String[] { "-cp", "./bin" + File.pathSeparator + rtJar, "-exclude", "javax",
"-allow-phantom-refs", "-no-bodies-for-excluded", "-process-dir", "./targetBin1", "-src-prec",
"only-class", "-w", "-output-format", "J", "-keep-line-number","tag.ln","on","de.visuflow.analyzeMe.ex1.TargetClass1" });
}
public static void traverseMethods(SootMethod source, CallGraph cg)
{
Targets tc = new Targets(cg.edgesOutOf(source));
while(tc.hasNext())
{
SootMethod destination = (SootMethod)tc.next();
if(!destination.isJavaLibraryMethod())
{
System.out.println(destination+" has active body "+destination.hasActiveBody());
boolean methodPresent = false;
Iterator<Method> iteratorMethod = listMethods.iterator();
while(iteratorMethod.hasNext())
{
if(iteratorMethod.next().getMethod().equals(destination))
{
methodPresent = true;
break;
}
}
if(!methodPresent)
{
methodcount++;
Method method = new Method(methodcount, destination);
listMethods.add(method);
}
Method sourceMethod = null, destinationMethod = null;
Iterator<Method> iteratorMethods = listMethods.iterator();
while(iteratorMethods.hasNext())
{
Method method = iteratorMethods.next();
if(method.getMethod().equals(source))
{
sourceMethod = method;
}
if(method.getMethod().equals(destination))
{
destinationMethod = method;
}
}
edgeCount++;
Edge edge = new Edge(edgeCount, sourceMethod, destinationMethod);
listEdges.add(edge);
traverseMethods(destination, cg);
}
}
}
}
package de.visuflow.callgraph;
import java.io.File;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import de.unipaderborn.visuflow.model.VFClass;
import de.unipaderborn.visuflow.model.VFMethod;
import de.unipaderborn.visuflow.model.VFUnit;
import soot.Body;
import soot.G;
import soot.Main;
import soot.PackManager;
import soot.Scene;
import soot.SceneTransformer;
import soot.SootClass;
import soot.SootMethod;
import soot.Transform;
import soot.Unit;
import soot.jimple.toolkits.callgraph.CallGraph;
import soot.jimple.toolkits.callgraph.Targets;
import soot.util.Chain;
public class JimpleModelAnalysis {
private int methodcount = 0;
private int edgeCount = 0;
public void createICFG(final ICFGStructure methodGraph, List<VFClass> vfClasses) {
G.reset();
Transform transform = new Transform("wjtp.myTransform", new SceneTransformer() {
@Override
protected void internalTransform(String phase, Map<String, String> arg1) {
createJimpleHierarchyWithCfgs(vfClasses);
createICFG();
}
private void createICFG() {
CallGraph cg = Scene.v().getCallGraph();
SootMethod entryMethod = null;
java.util.List<SootMethod> listMethod = Scene.v().getEntryPoints();
Iterator<SootMethod> iterEntryMethod = listMethod.iterator();
while(iterEntryMethod.hasNext())
{
entryMethod = iterEntryMethod.next();
if(entryMethod.isMain())
{
methodcount++;
Method method = new Method(methodcount, entryMethod);