1 package samples.callgraph; 2 3 import java.awt.Dimension ; 4 import java.awt.GridLayout ; 5 import java.awt.Toolkit ; 6 import java.awt.event.WindowAdapter ; 7 import java.awt.event.WindowEvent ; 8 import java.io.InputStream ; 9 import java.io.BufferedInputStream ; 10 import java.io.FileInputStream ; 11 import java.util.EmptyStackException ; 12 import java.util.HashMap ; 13 import java.util.Map ; 14 import java.util.Properties ; 15 import java.util.Stack ; 16 17 import javax.swing.JFrame ; 18 19 import org.apache.log4j.Category; 20 import org.apache.log4j.PropertyConfigurator; 21 22 import salvo.jesus.graph.DirectedEdgeImpl; 23 import salvo.jesus.graph.EdgeImpl; 24 import salvo.jesus.graph.Graph; 25 import salvo.jesus.graph.Tree; 27 import salvo.jesus.graph.TreeImpl; 28 import salvo.jesus.graph.Vertex; 29 import salvo.jesus.graph.visual.GraphEditor; 30 import salvo.jesus.graph.visual.VisualGraph; 31 import salvo.jesus.graph.visual.layout.GraphLayoutManager; 33 import salvo.jesus.graph.visual.layout.LayeredTreeLayout; 34 36 import alt.jiapi.InstrumentationContext; 37 import alt.jiapi.InstrumentationDescriptor; 38 import alt.jiapi.event.MethodEvent; 39 import alt.jiapi.event.MethodEventProducer; 40 import alt.jiapi.event.MethodListener; 41 import alt.jiapi.reflect.Loader; 42 import alt.jiapi.util.InstrumentingClassLoader; 43 44 public class CallGraphMonitor extends JFrame implements MethodListener { 45 private static Category log = Category.getInstance(CallGraphMonitor.class.getName()); 46 47 private Stack callStack; 48 private Map createdClasses; 49 private int count = 1; 50 51 private Tree tree; 53 private VisualGraph visual; 54 private GraphEditor editor; 55 private GraphLayoutManager layout; 58 59 public CallGraphMonitor(Properties props) throws Exception { 60 PropertyConfigurator.configure(props); 61 62 editor = new GraphEditor(); 63 tree = new TreeImpl(); 65 visual = editor.getVisualGraph(); 66 callStack = new Stack (); 67 createdClasses = new HashMap (); 68 69 editor.setGraph(tree); 71 72 this.getContentPane().setLayout(new GridLayout (1,2)); 73 this.getContentPane().add(editor); 74 75 layout = new LayeredTreeLayout(visual); 79 editor.setGraphLayoutManager(layout); 80 81 this.addWindowListener(new WindowAdapter () { 82 public void windowClosing(WindowEvent e) { System.exit(0); } 83 }); 84 85 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 86 Dimension frameSize = new Dimension (screenSize.width - 80, 87 screenSize.height - 200); 88 89 this.setSize(frameSize); 90 this.setLocation((int)(screenSize.getWidth() - frameSize.getWidth()) / 2, (int)(screenSize.getHeight() - frameSize.getHeight()) / 2); 91 92 } 94 95 96 public void constructorEntered(MethodEvent event) { 98 } 99 100 public void constructorExited(MethodEvent event) { 101 } 102 103 public void methodEntered(MethodEvent event) { 104 enterMethod(event.getClassName(), event.getMethodName()); 105 } 106 107 public void methodExited(MethodEvent event){ 108 exitMethod(event.getClassName(), event.getMethodName()); 109 } 110 111 private void enterMethod(String className, String methodName) { 112 117 log.debug(className + "." + methodName + " entered"); 118 128 ClassVertex classVertex = null; 129 if (!callStack.empty()) { 130 ClassVertex parent = (ClassVertex) callStack.peek(); 131 if (parent.hasChild(className)) { 132 classVertex = parent.getChild(className); 133 MethodEdge edge = parent.getEdge(className); 134 edge.addMethodCall(methodName, count); 135 try { 137 tree.removeEdge(edge); 138 tree.addEdge(edge); 139 } catch (Exception e) { 140 log.error(e.getMessage(), e); 141 } 142 } 143 else { 144 classVertex = new ClassVertex(className); 145 MethodEdge edge = null; 146 try { 147 tree.add(classVertex); 148 edge = new MethodEdge(parent, classVertex); 149 edge.addMethodCall(methodName, count); 150 tree.addEdge(edge); 151 } catch (Exception e) { 152 log.error(e.getMessage(), e); 153 } 154 155 parent.addChild(classVertex, edge); 156 } 157 158 log.debug("added edge from " + parent.getName() + 159 " to " + classVertex.getName()); 160 } 161 else { 162 try { 163 classVertex = new ClassVertex(className); 164 tree.addNode(null, classVertex); 165 } catch (Exception e) { 166 log.error(e.getMessage(), e); 167 } 168 } 169 170 callStack.push(classVertex); 171 count++; 172 visual.layout(); 173 } 174 175 private void exitMethod(String className, String methodName) { 176 log.debug(className + "." + methodName + " exited"); 177 181 try { 182 ClassVertex parent = (ClassVertex) callStack.pop(); 183 } catch (EmptyStackException ese) { 187 } 188 } 189 190 public static void main(String [] args) throws Exception { 191 InputStream config = new BufferedInputStream (new FileInputStream (args[0])); 192 Properties props = new Properties (); 193 props.load(config); 194 195 CallGraphMonitor monitor = new CallGraphMonitor(props); 196 monitor.setTitle("CallGraphMonitor"); 197 monitor.setVisible(true); 198 199 log.debug("created CallGraphMonitor"); 200 try { 201 Thread.currentThread().sleep(1000); 202 } 203 catch (Exception e) { 204 } 205 206 log.debug("creating the InstrumentationDescriptor"); 207 InstrumentationContext ctx = new InstrumentationContext(); 208 209 InstrumentationDescriptor id = new InstrumentationDescriptor(); 210 id.addInclusionRule("test.callgraph"); 211 MethodEventProducer eventProducer = new MethodEventProducer(id); 212 eventProducer.addMethodListener(monitor); 213 ctx.addInstrumentationDescriptor(id); 214 215 log.debug("creating a class loader"); 216 ClassLoader cl = InstrumentingClassLoader.createClassLoader(ctx); 217 218 log.debug("loading classes"); 219 Class clazz = cl.loadClass("test.callgraph.Main"); 220 221 log.debug("starting program"); 222 java.lang.reflect.Method m = 223 clazz.getMethod("main", new Class [] {String [].class}); 224 225 String []restArgs = new String [args.length - 1]; 226 for (int i = 1; i < args.length; i++) { 227 restArgs[i - 1] = args[i]; 228 } 229 230 for (int i = 0; i < restArgs.length; i++) { 231 System.out.println(i + ": " + restArgs[i]); 232 } 233 234 m.invoke(clazz, new Object [] {restArgs}); 235 236 log.debug("main thread dying"); 237 } 238 } 239 | Popular Tags |