Dump the Stack Trace information on occurrence of an Exception

The Xdump
option provides the ability to dump the stack trace or generate javacores, userdumps,
heapdumps or Snap trace when an event occurs.The event can be gpf (general protection fault), user event (“Signal 21”
or “kill-3”), OutOfMemoryError or an
exception.

Many times
while running applications the stack trace does not get displayed even though
an exception occurs.This is because the
application catches the exception and silently continues to do the rest of the
operations.However, some of these
exceptions may be thrown because of some bad code (though it may not cause the
application to fail) or could eventually lead to failure of the application.Hence, it becomes important to determine the
code that throws the exception, understand and fix the code to avoid the
exception from being thrown.

The Xdump
provides “stack” option which allows the user to dump the stack trace when an
exception is thrown or caught without users having to modify/instrument or
re-compile the code. We can also dump
javacores, coredumps etc when an exception occurs.This blog focuses on the “stack” option and
how to use it to generate the stack trace information on occurrence of an
exception.

The “stack”
option is usually used with “events” and “filter”.

filter

Filter
basically represents an exception class name. This tells the “stack” option for which
exception, the stack trace should be generated.

For ex:

java/io/IOException,
java/lang/OutOfMemoryError etc.

Events

“throw”

As the name
implies, the “throw” event occurs when an exception is thrown.

Use the below option to dump the
stack trace when java/io/IOException is thrown:

at
com/sun/comm/LinuxSerial.setSerialPortParams(IIII)V (LinuxSerial.java:349)

at
SerialConnection.setConnectionParameters()V (Bytecode PC: 74)

at SerialConnection.openConnection()V
(Bytecode PC: 66)

...<snipped>

“catch”

The “catch”
event is triggered when an exception is caught by the application.This will be useful to know the code that silently
catches/swallows the exception, especially if this code happens to be somewhere
down in the stack.It is usually very
difficult to determine this code by just code walkthrough.

Use the below option to dump the
stack trace when java/io/IOException is caught in the code (using try..catch
block):

This event
is triggered when an exception is not caught by the application.There are exceptions which the compiler
cannot detect and warn the user at compilation time.This event can be useful to know all the
pieces of code where the exceptions are not caught in your application and it
can help you to detect such code and if required modify the code to catch any
of the relevant exceptions and do appropriate processing or add retry logic
etc.

Use the below option to dump the
stack trace when java/lang/NullPointerException is not caught in the code

This event
is triggered if an exception is about to be thrown by the JVM.This is different from “throw” event because
it is only triggered for error conditions detected internally in the JVM.In brief, the “systhrow” event gets triggered
for the low level exceptions, while the “throw” event gets triggered for the
low level exceptions and also for application or higher level exceptions.

For
instance, “systhrow” gets triggered for java/lang/NullPointerException.However, it does not get triggered for
java/security/NoSuchAlgorithmException. The “throw” event gets triggered for
both these exceptions.

Use the below option to dump the
stack trace when java/lang/NullPointerException occurs using “systhrow” event: