Interactive and Non-Interactive Remote Debugging

For a software developer it is quite common that a bug is not reproducible locally, but it is occurring on the test server or on the production server. In logs you can see the line number of exception/bug but you don’t know what data is responsible for the exception.

For example: here is a chain method

o.m1().m2().m3();

Now you are getting an NPException at this line. But you will never know which method is returning the NULL. You will ask client to pass the database script so that you can reproduce it locally, which is quite painful for both of you.

Think about a solution that will let you debug or see what is the value of a variable on a given line number from a remote location, yes you can see what is happening on remote server JVM from your machine running eclipse.

It is possible through Remote Debugging.  There two types of remote debugging in java:

  1. Interactive : You have copy of the code in eclipse, you put breakpoints and connect to JVM running on remote server. (There are many blogs on internet available on this).
  2. Non-Interactive: You don’t need to have copy of code, but you can still print values of a particular variable in class loaded on remote JVM.

Prerequisites: I am using eclipse Kepler, Tomcat 7.0.8, jdk 1.7

Let’s have a look at first method:

#1 Interactive Remote Debugging: Steps:

1. In <tomcat>/bin/startup.bat change:

“call “%EXECUTABLE%” start %CMD_LINE_ARGS%”

to:

“call “%EXECUTABLE%” jpda start %CMD_LINE_ARGS%”
2. Copy example war file into your <tomcat>/webapps

3. Start the tomcat server (It will open a debug port and start listening on it)

4. In Eclipse right click on the project -> Run as -> Run Configurations -> Create a new Remote Java Application task.

Create a new Remote Java Application task

5. Enter socket and Click debug.

Enter socket and Click debug

6. Put some break points in code. Goto browser and fire some server request. Execution will be stopped on break point in eclipse as soon as control reaches there.

You can also configure tomcat embedded in eclipse, just need to make some changes in catalina.bat, please see this link for details https://confluence.sakaiproject.org/display/BOOT/Setting+Up+Tomcat+For+Remote+Debugging

 

#2 Coming to Non-Interactive Remote debugging

It is practically impossible to debug a production server with ways mentioned above, because if you connect to production server debugger and put a break point in eclipse it will halt the processing on breakpoints and will effect users. So alternative way is Non-interactive debugging. Youdebug, which uses a groovy based script allows you to get/print values on remote machine (and lot more).

Here is how you will do it:

1. Create a sample java class e.g com.test.TestYouDebugConsole class (Download TestYouDebugConsole.java). Here is how it should look like


package com.test;

public class TestYouDebugConsole {

public static void main(String[] args) {
String str="Test";
str = str.substring(10, 20); // Possible StringIndexOutOfBoundException
System.out.println(str);
}

}

2. Start your java application  with following arguments

-agentlib:jdwp=transport=dt_socket,server=y,address=8000

Start your java application

 

It will start a debug listener port

3. Create groovy script youdebuggroovy.ydb,

Add breakpoints in given class and given line number, like this:

vm.breakpoint("com.test.TestYouDebugConsole", 7){
println "value="+str;
}
vm.breakpoint("com.test.YouDebugVM", 19){
println "value="+str;

}

4. Download YouDebug, unpack and Run YouDebug from command line 

java -cp youdebug.jar;args4j.jar;args4j.jar;asm.jar;groovy.jar;groovy1.7.0.jar;antlr.jar; org.kohsuke.youdebug.YouDebug -socket localhost:5005 youdebuggroovy.ydb

 

Now as soon as the control reaches the class com.test.TestYouDebugConsole and line #7, groovy script will print the value ‘str’ mentioned  in the youdebuggroovy.ydb script. This means you will get to know the value before an exception is occurred, and of course you did not added any extra logs in code, or changed the build and not even had to restart the server.  

 

Thank you for reading. Hope it will help the java developers in debugging test and resolve production environment issues better. 

 

Leave a Reply

Your email address will not be published. Required fields are marked *


7 + seven =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>