We all know that Throwable.printStackTrace() prints stack trace containing Java frames. How about printing mixed mode stack trace on exceptions? - we want to print Java frames, JNI C/C++ frames and OS C/C++ frames - all in one place - that too whenever an exception is thrown - regardless of whether user's code had printStackTrace() call or not. i.e., we want this on already compiled, running system. Can we get that? Yes -- if you use DTrace with Mustang (Java SE 6).
Most of the time exceptions are thrown with the following pattern:
throw new FooException();
Exception object is created, initialized and immediately thrown. Very rarely (if at all) exception objects are stored and thrown later. So, if we use the following D-script, it will print mixed mode trace on exceptions:
hotspot$1:::method-entry {
self->ptr = (char*)copyin(arg1, arg2+1);
self->ptr[arg2] = '\0';
self->classname = (string)self->ptr;
self->ptr = (char*)copyin(arg3, arg4+1);
self->ptr[arg4] = '\0';
self->methodname = (string)self->ptr;
}
hotspot$1:::method-entry
/ self->classname == "java/lang/Throwable" && self->methodname == "<init>" / {
jstack();
}
How this D-script works? All Java exceptions and errors are directly or indirectly derived from java.lang.Throwable. So, all exception constructors will eventually call constructor of java.lang.Throwable -- we put a method-entry probe with filter for that constructor (note that <init> is internal name for constructor methods). On every Throwable constructor call, we print mixed mode stack trace using jstack built-in function.
Note that to use method-entry probe, you need to start your Java application with the flag -XX:+ExtendedDTraceProbes - but this requirement may change in Mustang FCS.
Mustang offers great tools, APIs for observability. This is one of the many reasons why you may want to try Mustang - beta is available now!
Posted by kv on February 15, 2006 at 11:19 PM IST #
Posted by A. Sundararajan on February 16, 2006 at 10:57 AM IST #