Coverage and profiling tools mainly use two methods: periodically polling the JVM for the status of various threads or toolkit byte-code of the application to supplant the corresponding JVM data.
There is no direct API support in java, but there are many tools you can use:
- You can catch stream dumps using jstack or similar tools, save them to a file, then analyze (or write a script / program for analysis), this is a survey.
- Use ASM or BCEL to change the bytecode of your application, it pushes, but it is very difficult to do.
- Using AspectJ using a boot time triangulator is just a command line option to enable it.
Solution 3 is much simpler, clean and versatile.
For printing at runtime, any method that is called as a result of the method execution is a simple aspect of the trace, something similar to:
public aspect TraceCalls { pointcut aCall : call(* *(..)); pointcut inside : cflow(execution(public MyClass.MyMethod(..))); before() : aCall() && inside() { System.out.println(thisJoinPoint); } }
Obviously, you can access much more data, print it in a file, format it, etc.
(Note that I wrote this code here, so it might be filled with syntax errors)
source share