Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a Java call graph, Eclipse based solutions

I'd like to analyze and understand a certain Java app and I think a call graph would be very useful. How do I generate one? I'm using Eclipse.

like image 513
Guy Avatar asked Dec 30 '09 22:12

Guy


People also ask

How to use call graph in Eclipse?

Navigate to Eclipse > Preferences (or Window > Preferences ). Select Call Graph Construction and check the Enable Library Call Graph Construction checkbox depending on your analysis needs. To create a call graph for a project navigate to Atlas > Manage Project Settings , select the project to analyze and press OK .

What is a call graph in Java?

The Java Call Graph presents the chain of possible calls at runtime in Java. The nodes in the graph represent the project components, while the edges (arrows) represent relationships between the components. Both nodes and edges are color coded to show the type of component or relationship.

What is a call graph in programming?

A call graph (also known as a call multigraph) is a control-flow graph, which represents calling relationships between subroutines in a computer program. Each node represents a procedure and each edge (f, g) indicates that procedure f calls procedure g. Thus, a cycle in the graph indicates recursive procedure calls.


1 Answers

Getting callstack

1) If you can debug the application simply put a breakpoint (double click over the left margin of the code) and wait it to stop. Go to Debug Perspective if you're not there, and open the Call stack View/Panel. It has the call stack :)

2) If you want to print this stack trace somewhere use an Exception:

Exception aux = new Exception("I'm here"); // not for throwing!
aux.printStackTrace(); // if you want it in stdout

or

Exception aux = new Exception("I'm here"); // not for throwing!
StringWriter sw = new StringWriter();
aux.printStackTrace(new PrintWriter(sw));
String result = sw.toString(); // if you want it in a string

Obtaining method references

You can obtain all references to a method by right-clicking, References, Workspace. It will search all callings in your current open projects. Very very useful.

Profiling an app

(thanks those who had answered the profiler option)

Eclipse TPTP provides profiling:

http://www.eclipse.org/tptp/home/project_info/general/whatisTPTP.php

like image 182
helios Avatar answered Oct 18 '22 09:10

helios