How to get the dynamic call graph of a java program - java

I can't get or modify the source code of the program. So I'm trying to read the jvm(hotspot) source code to see if I can do something when it fetch the "call method" instruction, but it seems very complex.
I want to know where is the relevant code I should start with or if there is other way to do this.

There is no need to modify JVM code to intercept method invocation. There is a standard documented way to do this using JVMTI.
You'll need to create an agent that will set callbacks for MethodEntry / MethodExit JVMTI events, compile it to a shared library, then run Java with -agentpath:/path/to/libYourAgent.so option.
See an example of using MethodEntry / MethodExit events in this question.

Related

How to profile a single Java (Spring) method run?

I have a Java Spring application that is running on my PC (I can attach debugger), I am looking for a way to profile a single method, preferably one that does have an UI to drilldown the child methods that consume the most time.
I tried JDK mission control and IntelliJ 's default profiler which I believe both are based off Java Flight Recorder. The issue is that most of the time it does not sample my method and my method spend lots of time waitting for (async?) I/O which cause the profiling result to be unusable
Why does the Java flight recorder take too few samples?
How to include IO-bound methods in Java Flight Recorder sampling?
What should I do in this case? Given that
I can already debug the method
I want to profile a single run on that single method only
I can get the drill down of child methods (Flame/Icicle chart) like what Google Chrome can do, including time when function is pending IO?
Preferably, without changing source code.
As supplemental detail, I already looked at some questions here that does not address the issue I mentioned: Some require to update to source code and some is dependent on JFR method sampling which I do not know how/if can profile a single specific method run and include the async I/O in the drill-down view
Any recommended Java profiling tutorial?
Profiling a Java Spring application

Attach to a running Java process?

I'm trying to attach and call methods and edit variables from a java process that's running from a separate java process.
Example:
Process 1 has a loop that prints a String variable that's private and defined in the class.
Process 2 (when started), changes the String message and then process 1 will continue to print out the changed variable.
This is more of a simple version of what i'm trying, but if i can figure that out, i can easily find out the rest myself.
Thanks!
Erouax
Just adding this because i totally forgot about my question when i found the answer.
It's possible (and very easy) to do using Java Agents and injecting using things such as the Hotspot Attach API. This is a very 'detectable' method, but still works. If you wish to keep your injection more low-key for whatever reason, i suggest using DLL injection to chuck in your classes and using JNI to call your entry method from the DLL. These DLL's and injectors are quite easy to come by, even quite easy to make if you have decent native knowledge.

What is java currently doing?

I'm writing scripts with a Java api which compile to class files and are executed out of my perspective on a virtual machine (I don't have access to this virtual machine and can't debug my class files from within.) These scripts require observable data to execute and don't run properly unless they've been executed within this virtual machine. As a consequence of the way that these class files are executed, to my knowledge, I can't debug them with my IDE's built-in debugger.
I don't really have the coding vocabulary to even search for answers to that last statement, so please correct me if I'm wrong.
Anyways, the only way which I've determined will allow me to debug these scripts is the classic print-statement method. It's horrible. But it works. And it seems that the entire community for the api uses this method.
I had the idea that I could use reflection to grab information from my classes as they're executing, but this still doesn't give me access to the line-by-line debugging that I'm looking for.
What I'd like to do is monitor the execution of my script step-by-step (every calculation the VM does) and store information about those calculations (variable "foo" in class "bar" becomes 4 on line "soandso") as a sort of running cache which dumps to the system console whenever an error occurs.
Is this possible?
You can take a look at dynamic proxies.
Here is a very good explanation of it.
With a dynamic proxy you can output (log, send as mail, ...) the method that is beeing called and the parameters that are passed to the method.
It's not exactly what you are looking for but maybe it will help you.

How can I run a java program and see what methods/classes have been employed to produce the output?

I want to use the code of an open source java library, but I don't need all classes/packages of this library, but only some of them. How can I automatically get to know which ones of the packages/classes/methods are employed when I submit a certain input to it?
If you know how to use the java debugger, you will just have to click
step into and step over. Then from the debug window, you will know what kinds of methods or classes are used.

Is there any class to diagnose invoked method in a java class?

I need to diagnose all invoked methods in a class(either declared in the class or not) using it's source code. Means that give the class source code to a method as an input and get the invoked method by the class as the output. In fact I need a class/method which operates same as java lexical analyzer .
Is there any method to diagnose all invoked methods ?
of course I tried to use Runtime.traceMethodCalls(); to solve the problem but there was no output. I've read I need to run java debug with java -g but unfortunately when I try to run java -g it makes error. Now what should I do ? Is there any approach ?
1) In the general case, no. Reflection will always allow the code to make method calls that you won't be able to analyze without actually running the code.
2) Tracing the method calls won't give you the full picture either, since a method is not in any way guaranteed (or even likely) to make all the calls it can every time you call it.
Your best bet is some kind of "best effort" code analysis. You may want to try enlisting the compiler's help with that. For example, compile the code and analyze the generated class file for all emitted external symbols. It won't guarantee catching every call (see #1), but it will get you close in most cases.
You can utilize one of the open source static analyzers for Java as a starting point. Checkstyle allows you to build your own modules. Soot has a pretty flexible API and a good example of call analysis. FindBugs might also allow you too write a custom module. AFAIK all three are embeddable in the form of a JAR, so you can incorporate whatever you come up with into your own custom program.
From your question it is hard to determine what is exactly problem you're trying to solve.
But in case:
If you want to analyze source code, to see which parts of it are redundant and may be removed, then you could use some IDE (Eclipse, IntelliJ IDEA Community Edition etc.) In IDE's you have features to search for usages of method and also you have functionality to analyze code and highlight unused methods as warnings/errors.
If you want to see where during runtime some method is called, then you could use profiling tool to collect information on those method invocations. Depending on tool you could see also from where those methods were called. But bare in mind, that when you execute program, then it is not guaranteed that your interesting method is called from every possible place.
if you are developing an automated tool for displaying calling graphs of methods. Then you need to parse source and start working with code entities. One way would be to implement your own compiler and go on from there. But easier way would be to reuse opensourced parser/compiler/analyzer and build your tool around it.
I've used IntelliJ IDEA CE that has such functionalitys and may be downloaded with source http://www.jetbrains.org/display/IJOS/Home
Also there is well known product Eclipse that has its sources available.
Both of these products have enormous code base, so isolating interesting part would be difficult. But it would still be easier than writing your own java compiler and werifying that it works for every corner case.
For analyzing the bytecode as mentioned above you could take a look at JBoss Bytecode. It is more for testing but may also be helpful for analyzing code.
sven.malvik.de
You may plug into the compiler.
Have a look the source of Project Lombok for instance.
There is no general mechanism, so they have one mechanism for javac and one for eclipse's compiler.
http://projectlombok.org/

Categories

Resources