问题
I am looking for a simple dynamic call graph logger for Java that you can add in a few lines of code. I know there is an Aspect J solution. Also, I helped Zola develop Glow for C/C++ so I could rewrite a similar tool but I don't want to dig into JVM internals.
Any open source solution out there right now that is stable and better than the AspectJ solution?
The purpose is to use as a companion to unit testing certain portions of the code that you want more information about their behavior.
回答1:
I think you want to collect a call graph (as opposed to just a set of calls) by any means possible.
One can do with with a static analyzer (if you can get a strong enough one), to collect the potential call graph. A dynamic method collects one at runtime by instrumenting the code. Some folks may specifically want the dynamic one, because they want to see the actual call graph for a specific set of input data.
There are several Java profilers that will collect this information dynamically, including ours. None of the ones that do that are open source, that I know of, but I could be wrong.
Such profilers often work by instrumenting the code (either source or VM code if the language [e.g., Java,C#] has such). How they do it depends on the supplier. In our case, we use our program transformation tools to transform the source code from its original form, into a form that also collects profiling data.
You can use AspectJ to insert instrumentation to do this, too. [It is worth noting that aspects are just a special case of program transformation]. Of course, there's more work than just instrumenting the code; you have to collect the runtime data efficiently and after execution process to produce the call graph. So its rather a bit of work to do all this but you presumably know that from your Glow experience.
回答2:
Maybe off-topic, but are you sure you want actually call graph? Somehow I think such a detailed graph will be next to useless in a reasonably sized application. What I find much more useful is a dependency graph between classes, one that is very easy to get as long as you use some kind of dependency injection. I used google guice (and it was actually pretty useful to restructure/keep clean a reasonably sized application).
There is a very nice google-guice-dependency grapher available out-of-the box and for free: http://code.google.com/p/google-guice/wiki/Grapher . I even customized it (extended the Grapher class) to mark different class types with different colors (DAO, controller, API etc.)...
回答3:
There is instrumentation via the native JVMTI C/C++ native interfaces. Like I said I would like to stay in pure Java.
Java does have a Runtime.getRuntime().traceMethodCalls(), but you need something to consume the output still.
来源:https://stackoverflow.com/questions/6540423/simple-dynamic-call-graphs-in-java