Mixed mode stack traces on Java exceptions

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!

Comments:

I am thinking this is possible only on Solaris - Is that accurate?

Posted by kv on February 15, 2006 at 03:19 PM IST #

Hi kv: Yes, you are right. DTrace is available only on Solaris. But, as you may already know, Solaris is available for major CPUs - sparc, x86, AMD64 and is open sourced (OpenSolaris.org). There is even a CD-bootable version of Solaris (Belenix). Linux apps can be run on Solaris (BrandZ). Please give it a try...

Posted by A. Sundararajan on February 16, 2006 at 02:57 AM IST #

Post a Comment:
Comments are closed for this entry.
About

sundararajan

Search

Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today
Bookmarks
Links

No bookmarks in folder

Blogroll

No bookmarks in folder

News

No bookmarks in folder