1 19 20 package ca.mcgill.sable.soot.interaction; 21 22 import soot.AbstractTrap; 23 import soot.toolkits.graph.interaction.*; 24 import java.util.*; 25 26 import org.eclipse.ui.*; 27 28 import ca.mcgill.sable.soot.launching.*; 29 import org.eclipse.swt.widgets.*; 30 import org.eclipse.jface.dialogs.*; 31 import org.eclipse.swt.*; 32 import ca.mcgill.sable.soot.*; 33 import ca.mcgill.sable.soot.callgraph.*; 34 import ca.mcgill.sable.soot.cfg.*; 35 import soot.jimple.toolkits.annotation.callgraph.*; 36 import soot.*; 37 public class InteractionController implements IInteractionController, IInteractionListener { 38 39 private ArrayList listeners; 40 private Thread sootThread; 41 private boolean available; 42 private InteractionEvent event; 43 private Display display; 44 private SootRunner parent; 45 private soot.toolkits.graph.DirectedGraph currentGraph; 46 private ModelCreator mc; 47 private CallGraphGenerator generator; 48 49 public InteractionController() { 50 } 51 52 53 public void addListener(IInteractionListener listener){ 54 if (listeners == null){ 55 listeners = new ArrayList(); 56 } 57 listeners.add(listener); 58 } 59 60 public void removeListener(IInteractionListener listener){ 61 if (listeners == null) return; 62 if (listeners.contains(listener)) { 63 listeners.remove(listener); 64 } 65 } 66 67 68 69 public void handleEvent(){ 70 if (getEvent().type() == IInteractionConstants.NEW_ANALYSIS){ 71 handleNewAnalysisEvent(event.info()); 72 73 } 74 else if (getEvent().type() == IInteractionConstants.NEW_CFG){ 75 handleCfgEvent(getEvent().info()); 76 } 78 else if (getEvent().type() == IInteractionConstants.NEW_BEFORE_ANALYSIS_INFO){ 79 80 handleBeforeFlowEvent(getEvent().info()); 81 } 83 else if (getEvent().type() == IInteractionConstants.NEW_AFTER_ANALYSIS_INFO){ 84 handleAfterFlowEvent(getEvent().info()); 85 } 87 else if (getEvent().type() == IInteractionConstants.NEW_BEFORE_ANALYSIS_INFO_AUTO){ 88 89 handleBeforeFlowEventAuto(getEvent().info()); 90 } 92 else if (getEvent().type() == IInteractionConstants.NEW_AFTER_ANALYSIS_INFO_AUTO){ 93 handleAfterFlowEventAuto(getEvent().info()); 94 } 96 else if (getEvent().type() == IInteractionConstants.DONE){ 97 waitForContinue(); 99 } 100 else if (getEvent().type() == IInteractionConstants.STOP_AT_NODE){ 101 handleStopAtNodeEvent(getEvent().info()); 102 } 103 else if (getEvent().type() == IInteractionConstants.CLEARTO){ 104 handleClearEvent(getEvent().info()); 105 } 106 else if (getEvent().type() == IInteractionConstants.REPLACE){ 107 handleReplaceEvent(getEvent().info()); 108 } 109 110 else if (getEvent().type() == IInteractionConstants.CALL_GRAPH_START){ 111 handleCallGraphStartEvent(getEvent().info()); 112 } 113 else if (getEvent().type() == IInteractionConstants.CALL_GRAPH_NEXT_METHOD){ 114 handleCallGraphNextMethodEvent(getEvent().info()); 115 } 116 117 else if (getEvent().type() == IInteractionConstants.CALL_GRAPH_PART){ 118 handleCallGraphPartEvent(getEvent().info()); 119 } 120 } 121 122 private Shell getShell(){ 123 IWorkbenchWindow window = SootPlugin.getDefault().getWorkbench().getActiveWorkbenchWindow(); 124 final Shell [] shell = new Shell [1]; 125 getDisplay().syncExec(new Runnable (){ 126 public void run(){ 127 shell[0] = getDisplay().getDefault().getActiveShell(); 128 } 129 }); 130 return shell[0]; 131 } 132 133 private void handleNewAnalysisEvent(Object info){ 134 135 SootPlugin.getDefault().setDataKeeper(new DataKeeper(this)); 136 InteractionHandler.v().setInteractThisAnalysis(true); 137 138 } 139 140 private void handleCfgEvent(Object info){ 141 142 soot.toolkits.graph.DirectedGraph cfg = (soot.toolkits.graph.DirectedGraph)info; 143 setCurrentGraph(cfg); 144 setMc(new ModelCreator()); 145 getMc().setSootGraph(getCurrentGraph()); 146 String editorName = "CFG Editor"; 147 148 if (cfg instanceof soot.toolkits.graph.UnitGraph){ 149 soot.Body body = ((soot.toolkits.graph.UnitGraph)cfg).getBody(); 150 editorName = body.getMethod().getDeclaringClass().getName()+"."+body.getMethod().getName(); 151 } 152 mc.setEditorName(editorName); 153 154 final ModelCreator mc = getMc(); 155 156 getDisplay().syncExec(new Runnable (){ 157 public void run(){ 158 mc.displayModel(); 159 } 160 }); 161 InteractionHandler.v().autoCon(false); 162 163 waitForContinue(); 164 165 } 166 167 private void waitForContinue(){ 168 soot.toolkits.graph.interaction.InteractionHandler.v().waitForContinue(); 169 } 170 171 private void handleBeforeFlowEvent(Object info){ 172 handleBeforeEvent(info); 173 waitForContinue(); 174 175 } 176 177 private void handleBeforeEvent(Object info){ 178 FlowInfo fi = (FlowInfo)info; 179 SootPlugin.getDefault().getDataKeeper().addFlowInfo(info); 180 181 Iterator it = getCurrentGraph().iterator(); 182 183 final Shell myShell = getShell(); 184 final FlowInfo flowBefore = fi; 185 final ModelCreator mc = getMc(); 186 getDisplay().syncExec(new Runnable () { 187 public void run(){ 188 mc.updateNode(flowBefore); 189 }; 190 }); 191 } 192 193 private void handleBeforeFlowEventAuto(Object info){ 194 handleBeforeEvent(info); 195 196 } 197 198 private void handleAfterFlowEvent(Object fi){ 199 handleAfterEvent(fi); 200 waitForContinue(); 201 202 } 203 204 private void handleAfterEvent(Object fi){ 205 final Shell myShell = getShell(); 206 SootPlugin.getDefault().getDataKeeper().addFlowInfo(fi); 207 208 209 final FlowInfo flowAfter = (FlowInfo)fi; 210 final ModelCreator mc = getMc(); 211 212 getDisplay().syncExec(new Runnable () { 213 public void run(){ 214 mc.updateNode(flowAfter); 215 }; 216 }); 217 218 } 219 220 private void handleAfterFlowEventAuto(Object fi){ 221 handleAfterEvent(fi); 222 } 223 224 private void handleStopAtNodeEvent(Object info){ 225 InteractionHandler.v().autoCon(false); 227 228 final soot.Unit stopUnit = (soot.Unit)info; 229 final ModelCreator mc = getMc(); 230 getDisplay().syncExec(new Runnable () { 231 public void run(){ 232 mc.highlightNode(stopUnit); 233 }; 234 }); 235 waitForContinue(); 237 } 238 239 private void handleClearEvent(Object info){ 240 final FlowInfo fi = (FlowInfo)info; 241 final ModelCreator mc = getMc(); 242 243 getDisplay().syncExec(new Runnable () { 244 public void run(){ 245 mc.updateNode(fi); 246 }; 247 }); 248 } 249 250 private void handleReplaceEvent(Object info){ 251 final FlowInfo fi = (FlowInfo)info; 252 final ModelCreator mc = getMc(); 253 254 getDisplay().syncExec(new Runnable () { 255 public void run(){ 256 mc.updateNode(fi); 257 }; 258 }); 259 } 260 261 262 private void handleCallGraphStartEvent(Object info){ 263 if (getGenerator() == null){ 264 setGenerator(new CallGraphGenerator()); 265 } 266 getGenerator().setInfo((CallGraphInfo)info); 267 getGenerator().setController(this); 268 final CallGraphGenerator cgg = getGenerator(); 269 getDisplay().syncExec(new Runnable (){ 270 public void run(){ 271 cgg.run(); 272 } 273 }); 274 waitForContinue(); 275 } 276 277 private void handleCallGraphNextMethodEvent(Object info){ 278 SootMethod meth = (SootMethod)info; 279 InteractionHandler.v().setNextMethod(meth); 280 InteractionHandler.v().setInteractionCon(); 281 } 282 283 private void handleCallGraphPartEvent(Object info){ 284 final CallGraphGenerator cgg = getGenerator(); 285 final Object cgInfo = info; 286 getDisplay().asyncExec(new Runnable (){ 287 public void run(){ 288 cgg.addToGraph(cgInfo); 289 } 290 }); 291 waitForContinue(); 292 } 293 294 297 public Thread getSootThread() { 298 return sootThread; 299 } 300 301 304 public void setSootThread(Thread thread) { 305 sootThread = thread; 306 } 307 308 311 public boolean isAvailable() { 312 return available; 313 } 314 315 316 317 320 public InteractionEvent getEvent() { 321 return event; 322 } 323 324 327 public void setEvent(InteractionEvent event) { 328 this.event = event; 329 } 330 331 334 public Display getDisplay() { 335 return display; 336 } 337 338 341 public void setDisplay(Display display) { 342 this.display = display; 343 } 344 345 348 public SootRunner getParent() { 349 return parent; 350 } 351 352 355 public void setParent(SootRunner runner) { 356 parent = runner; 357 } 358 359 362 public soot.toolkits.graph.DirectedGraph getCurrentGraph() { 363 return currentGraph; 364 } 365 366 369 public ArrayList getListeners() { 370 return listeners; 371 } 372 373 376 public void setCurrentGraph(soot.toolkits.graph.DirectedGraph graph) { 377 currentGraph = graph; 378 } 379 380 383 public void setListeners(ArrayList list) { 384 listeners = list; 385 } 386 387 390 public ModelCreator getMc() { 391 return mc; 392 } 393 394 397 public void setMc(ModelCreator creator) { 398 mc = creator; 399 } 400 401 404 public CallGraphGenerator getGenerator() { 405 return generator; 406 } 407 408 411 public void setGenerator(CallGraphGenerator generator) { 412 this.generator = generator; 413 } 414 415 } 416 | Popular Tags |