1

I have a Java program (jar file) and I wish to print out all the methods called during the execution of that program. So if Class1.main() calls Class1.foo1(), initiates Class2, calls Class2.foo2() and Class2.foo3(), then I want the output to be something similar to

called Class1.main
called Class1.foo1()
called Class2.<init>
called Class2.foo2()
called Class2.foo3()

Please note that I do not wish to see the active stack trace, e.g the method that's currently active and the methods which led to the call of that method, since it would not print out those methods that aren't related to the call of that method. I wish to see all the methods called during the execution of the program, in the order they're called in.

The answer may take advantage of any technique, though I expect it to use some sort of debugging program, like jdb. The reason for this is because I have already tried to use instrumentation with Javassist (described in my answer to this question) and ran into errors when trying it with more complex programs.

Community
  • 1
  • 1
Nopslide
  • 121
  • 2
  • 7
  • 2
    You shouldn't post duplicate questions, not even if one is "in any technique" and the other is "in a specific technique". It's still a duplicate. – RealSkeptic Mar 27 '17 at 16:35
  • @Nopslide instead of saying that it broke, might show the error that actually happens; may be it is un-related. – Eugene Mar 27 '17 at 16:42
  • @Eugene error message: `Exception in thread "main" Throwable$WrappedPrintStream java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: Could not initialize class java.lang.invoke.CallSite at sun.net.www.protocol.http.HttpURLConnection.getNetProperty(Unknown So urce) at sun.net.www.protocol.http.HttpURLConnection.(Unknown Source) Caused by: java.lang.NoClassDefFoundError: Could not initialize class java.lang.invoke.CallSite` – Nopslide Mar 27 '17 at 16:51
  • @Nopslide how about asking a new question? – Eugene Mar 27 '17 at 17:00

1 Answers1

0

You could try Java Flight Recorder. Not sure if you can get the output the way you like, but we used it for profiling and it showed which methods were called and which methods used the most CPU time. So there is method recording for sure.

esin88
  • 3,091
  • 30
  • 35