Hossein
Hossein

Reputation: 1292

Recording every function call of a program in Java

I want to log every function call (timestamp and and function name) of Java a program (including threads/processes) In Python I can simply have a profiler that records function entries in the program as follows:

def tracefunc(frame, event, arg, indent=[0]):
      if event == "call":
          indent[0] += 2
          print("-" * indent[0] + "> call function", frame.f_code.co_name)
      elif event == "return":
          print("<" + "-" * indent[0], "exit function", frame.f_code.co_name)
          indent[0] -= 2
      return tracefunc

Inside my code I use it as:

import sys
import tracer

sys.setprofile(tracer.tracefunc)

I was wondering if there a similar thing in Java. Or do I need to use something else? Note that I want to log a big program such as Cassandra

Upvotes: 1

Views: 368

Answers (1)

Ingo Kegel
Ingo Kegel

Reputation: 47995

The JVM has no facility to trace all method calls. It used to be available up to Java 1.4 in the old JVMPI profiling interface, but it prevented hot spot compilation which made it useless to measure the actual performance.

The only way to trace method calls is to instrument the classes of interest. This can be done with a Java agent or with a native JVMTI agent. Also, there are tools that can do this out of the box. For example, JProfiler has a call tracer in the CPU views:

enter image description here

Disclaimer: My company develops JProfiler.

Upvotes: 1

Related Questions