Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dumping state of variables on Exception

I was wondering if there is a way to dump the state of all local variables when there is an exception, to get a better idea of the state of the environment that caused the exception. Below the variable idsToDump is unknown at run time and I want to find out the state at which value in the collection is causing the NPE.

Example:

public static void main(String[] args) {
    HashMap<Integer, String> employees = new HashMap<Integer, String>();
    employees.put(1, "James");

    Integer[] idsToDump = new Integer[] { 1, 2, 3 };
    for (Integer employeeId : idsToDump) {
        String name = employees.get(employeeId).toLowerCase();
        System.out.println(name + " is employee number: " + employeeId);
    }

}

output:

james is employee number: 1
Exception in thread "main" java.lang.NullPointerException

Question: Is there some JVM argument that I can pass to dump information about the current state of the local variables? ie we get

java.lang.NullPointerException

and (this is the part I'm after)

values: employeeId=2

I want to be able to do this on a client site, so no access to Eclipse or debugging tools, looking for just JVM arguments, can't make code changes either. I have looked through them but couldn't really find anything. In the meantime I'll keep searching there too ;)

like image 219
user1160931 Avatar asked Jan 20 '12 16:01

user1160931


2 Answers

I came across a commercial product that does this by simply using a startup -agentlib JVM agent param. I haven't used it yet but intend on giving it a try as it looks very promising.

https://www.takipi.com/product

Anyone have experience with this product?

like image 70
Sanjiv Jivan Avatar answered Sep 30 '22 20:09

Sanjiv Jivan


Given all your restrictions, I can't recommend anything else apart from jdb. Fire that bad boy up and start stepping through the client code line by line. I know you said no debugging tools, but unless they are a JRE only environment you should have jdb installed already.

like image 20
Perception Avatar answered Sep 30 '22 19:09

Perception