1

I have a big java application, with source code, it already up and running on my dev server. I want to know which java-methods executed on server on some my actions. I can connect to it with debugger.

I don't work on this app, unfamiliar with it.

It is very big, I try to set breakpoints, and "miss" every time. Is there generic approach how to quickly find (or log) all methods that was executed in app?

Alex T
  • 2,067
  • 4
  • 20
  • 27
  • What do you mean when you say that you "miss" every time? Do you suspect that your source code version is not in-sync with what runs on the server or you say that you don't even know where to set a breakpoint? In general you could fire up a CPU profiler and record which methods get executed (start recording right before you start your flow and stop it when you're done). – Mark Bramnik Apr 14 '21 at 21:35
  • 1
    That is called profiling, so you should look for a [Java Profiler](https://www.google.com/search?q=Java+Profiler) to gather the information and display it. – Andreas Apr 14 '21 at 21:36
  • Mark, source code corresponds to application. I want to find handlers of some actions, and cannot find it in code. It is too hard to guess every time where are sources by setting breakpoints in 100500 places. – Alex T Apr 14 '21 at 21:42
  • Yes, I think about profiling. It must solve problem. But really no more quick solution? – Alex T Apr 14 '21 at 21:44

3 Answers3

1

Profiling

Profiling is a way to find out what methods are called inside of the JVM by a specific thread or multiple ones.

There are two types of profiling

  • Proactive

    This requires you to connect to the JVM through JMX using an external tool, like JProfiler

  • Reactive

    On the other hand, you have what I'll call reactive profilers, they'll be constantly connected to the JVM, but won't intercept all events sent by the JVM, just samples of it. This is the case for Java Flight Recorder which has been open-sourced since

    The JDK Flight Recorder was designed to minimize the Observer Effect in the profiled system, and is meant to be always on in production systems.

Logging

While profiling is fine, I find it a bit cumbersome for what you're trying to achieve, so I'd probably tend to choose the logging solution if I were you.

In (which makes use of ), you can leverage AOP for this purpose. It will wrap your methods and intercept its invocations, and do something with it.

See the following example taken from this answer

@Aspect
public class MyLogger {

    private Logger log = Logger.getLogger(getClass());

    @After("execution(* com.example.web.HomeController.*(..))")
    public void log(JoinPoint point) {
        log.info(point.getSignature().getName() + " called...");
    }
}
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89
1

You can try JFR, either start it from command line:

$ java -XX:StartFlightRecording:filename=recording.jfr

or from the shell, against an already running JVM:

$ jcmd <pid> JFR.start filename=recording.jfr

If you like, you can get a recording before the application exits by doing:

$ jcmd <pid> JFR.dump

You can then open the recording in JDK Mission Control, which can be downloaded here:

https://jdk.java.net/jmc/8/

and look at the page where the most executed methods are shown.

Kire Haglin
  • 6,569
  • 22
  • 27
0

Profiling tools expose what is happening within your app and JVM at runtime.

Your IDE likely offers some profiling features.

Also, you can use tools such as Flight Recorder and Mission Control.

See intro article How Java Profilers Work.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154