public static void main(String... args) throws Exception { if (args.length == 0) { printHelp(); return; } DependencyGraph graph; switch (args[0]) { case "print": graph = loadGraphs(1, args); System.out.println(prettyFormat(graph)); break; case "findCycle": graph = loadGraphs(1, args); List<Dependency> cycle = graph.findCycle(); if (cycle == null) { System.out.println("no deadlock found"); } else { System.out.println("deadlocked threads: \n" + cycle); } break; case "findImpasse": graph = loadGraphs(1, args); graph = graph.findLongestCallChain(); if (graph == null) { System.out.println("no long call chain could be found!"); } else { System.out.println("longest call chain: \n" + prettyFormat(graph)); } break; case "findObject": graph = loadGraphs(2, args); List<DependencyGraph> graphs = graph.findDependenciesWith(args[1]); if (graphs.isEmpty()) { System.out.println( "thread not found! Try using the print command to see all threads and locate the name of the one you're interested in?"); } else { int numGraphs = graphs.size(); int i = 0; System.out.println("findObject \"" + args[1] + "\"\n\n"); for (DependencyGraph g : graphs) { i += 1; System.out.println("graph " + i + " of " + numGraphs + ":"); System.out.println(prettyFormat(sortDependencies(g.getEdges()))); if (i < numGraphs) { System.out.println("\n\n\n"); } } } break; default: printHelp(); break; } }
private static DependencyGraph loadGraphs(int startingAt, String... mainArgs) throws Exception { String filename; if (mainArgs.length < startingAt + 1) { return loadGraph("thread_dependency_graph.ser"); } DependencyGraph result = new DependencyGraph(); for (int i = startingAt; i < mainArgs.length; i++) { filename = mainArgs[i]; DependencyGraph gr = loadGraph(filename); if (gr == null) { return null; } result.addEdges(gr.getEdges()); } return result; }
/** Format dependency graph for displaying to a user. */ public static String prettyFormat(DependencyGraph graph) { return prettyFormat(graph.getEdges()); }