I was trying to debug java application using instrumentation. The problem with current system are
- Hardly written any log statements
- Poor exception handling
This made very difficult to trace root cause of broken functionality.
To handle the situation I have developed tool,java agent using Instrumentation
API , and I was able to inject log statements and half of the problem solved.
But the next problem is to recording the Exception. I want to extend my tool record every exception thrown during the execution of the application. I tried injecting 'try-catch' block using javaassist
API for methods (using addCatch
, insertBefore
and insertAfter
), and it is effective certain extent.
public byte[] transform(ClassLoader loader,
String className,
Class<?> classBeingRedefined,
ProtectionDomain protectionDomain,
byte[] classfileBuffer)
throws IllegalClassFormatException {
if (className.startsWith("com/alu/")) {
return insertLog(className, classBeingRedefined, classfileBuffer);
}
if(className.endsWith("Exception")){
System.out.println("============= exception occured "+className);
}
Here inserLog(..)
method will inject necessary log statement and works fine,but when there is any Exception it doesn't come to transformer.
But the problem is some of the method handles exception inside ( even with out log/sysout).
eg:
try {
if(search.equals("Category")){
//do operation
}
} catch (Exception e) {
}
This code eats NullPointerException
when value of search
is null, I never know this exception and application fail for some thing else.
Ultimately what I want is a mechanism to record any exception thrown by application. following details are to be captured
- Exception Type
- Excpetion Stacktrace
- Method and class name
I know there is API Thread.setDefaultUncaughtExceptionHandler
, but not sure how it use with java instrumentation. I don't have any source access the application.
[update 1]
I found below link tells to use retransformation
, I will give a try and update
How to instrument java system classes?
Any guidance would be greatly helpful.
============= exception occured
in stdout? Of course, it won't, because you instrumenting you code, that throws exception, and you log all exceptions being retransformed itself. – Typebaryou log all exceptions being retransformed itself
, could you rephrase it pls – Stans