pre/post method call interceptor for every java method without java code change

David Holmes David.Holmes at oracle.com
Wed May 11 04:00:59 PDT 2011


Balázs Galambos said the following on 05/11/11 20:09:
> I am trying to modify current stack trace creation and exception
> handling of java.( to make Thread.dumpthreads more informative.)
> I am looking for the point where the a new stacktraceelement is put
> into the stack trace when a method is executed, and where it is
> removed after finishing with it. I want to create a pre method
> interceptor and a post method interceptor, that are called for every
> method it finds during execution, without having to change all my java
> source. In these interceptors I want to add some extra info to the
> stack trace.
> 
> I think the answer is somewhere in JVM hotspot.
> javaCallWrapper seems to do what I need, but I cannot find where is
> the point when it adds/removes stack trace info.
> 
> I have found some constructors of stack trace elements are located in
> javaClasses.cpp (java_lang_Throwable::fill_in_stack_trace..) but I am
> not sure which one is used.
> 
> can you help me in this questions?

The stacktrace is handled in two parts. First Throwable.fillInStackTrace 
results in the filling in of a native data structure, in 
java_lang_Throwable::fill_in_stack_trace (javaClasses.cpp) that contains 
all the information for each frame on the current Java stack - this is 
done with BacktraceBuilder by walking the Java stack. This structure is 
then attached to the backtrace field in the Throwable. Then when you 
call Throwable.getStackTrace the information in the backtrace is used to 
construct the StackTraceElements that make up the StackTraceElement 
array that gets returned - java_lang_Throwable::get_stack_trace_element 
(again in javaClasses.cpp)

If you need to add extra information to the stacktrace info, as you 
describe, then I think you need to add that extra information to the 
frame (or vframe?) class and copy it into the backtrace and from there 
to the StackTraceElement. But then you need to modify method entry/exit 
to manage that extra info too. But the details of doing something like 
is beyond my direct experience.

Hope this helps.

David Holmes

> Thanks for your help!
> Balazs


More information about the hotspot-dev mailing list