Remote Debug : Java Application using JDWP

Debugging application is always a common topic for all developers and it may cover more than 60%+ of total development time if using traditional way like using debugging message.

After the release of Sun Java Development Kit 1.4.2, you can be benefited by the magic tool called JDWP (Java Debugging Wire Protocol), It provides sophisticated implementation for debugging your JAVA application in a modern way, especially when you want to debug server java application like Tomcat, JBoss, etc..

Okay, you may say you are happy using debug message for debugging remote application and thus let me list the summary of how JDWP can aid the development.

  1. Thread stepping execution: Suspend/Resume particular thread
  2. Breakpoints management: Insert/Remove breakpoint for investigate failure point
  3. Hotswap Bug Fixing: Recompile the classes on-the-fly for bug fixing without redeployment

I thought you may be interested with the features 3, which greatly reduces the re-deployment time for debugging an application.

This articles will focus how the setup for debugging remote java application. Tomcat5.5 and Eclipse Europa will be used for an example.

1. High Level View

Basically it is divided into two entities: Debuggee and Debugger. The Debuggee is the application that you want to debug and for our example it is Tomcat. The Debugger is the vendor of JDI (Java Debugging Interface) : Eclipse.

jpda

 

2. Enable JDWP in the Debuggee (Tomcat)

JDWP can be enabled out of the box by setting the appropriate system properties :

-Xrunjdwp:transport=dt_socket,address=9002,server=y,suspend=n

This enables JVM with JDWP at port 9002 using server mode with any suspension.

Server

(“n” or “y”) If “y,” listen for a debugger application to attach; otherwise, attach to the debugger application at the specified address.

Address

transport address for the connection. If server=n, attempt to attach to a debugger application at this address. If server=y, listen for a connection at this address.

Timeout

If server=y, this specifies the timeout, in milliseconds, to wait for the debugger to attach. If server=n, this specifies the timeout, in milliseconds, to use when attaching to the debugger.

Suspend

If “y,” JVM suspends the execution until a debugger connects to the debuggee JVM. Preferably set this option to “n” else the application will not start unless the debugger attaches the port on which debugee  application is lessoning.

Remote Debug : an insight

remoteDebug

Some times you have to debug a application which is not running from it’s source code in a IDE. So to do it we use this remote debug concept. In the scenario which is shown above the application runs in a remote machine, but when it runs it will broadcast debug information via a socket deployed. The IDE running locally will capture those broadcast debug information through that socket and it will do the debug through the source code.
JAVA language provides this in its JVM. We can use commands like this to run a application in the remote debug mode in java.

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,suspend=n,server=y -jar Name_of_Your_Jar.jar

Then it will broadcast some debug information and will wait until some IDE capture those debug information and start debugging the source code. When Some IDE connects to the Socket specified it will start the debugging process.
Configuring the IDEs will depend on them. The links given below will give you a good understanding about those concepts. Hope you learn some thing from this post. Following links are really interesting to learn about remote debugging.

  • Application to be debugged would attach a socket to itself and then would listen debug instructions on that socket.
  • Debugger would bind itself to that socket and then send instructions on that socket.

What is Remote Debugging :
http://www.javabeat.net/2010/12/what-is-remote-debugging-in-java/
Remote Debugging with Eclipse :
http://www.eclipsezone.com/eclipse/forums/t53459.html
http://java.dzone.com/articles/how-debug-remote-java-applicat
http://www.myeclipseide.com/documentation/quickstarts/remotedebugging/
Remote Debugging with IntelliJ IDEA :
http://www.javaranch.com/journal/200408/DebuggingServer-sideCode.html
Remote Debugging Web Services, Apache Axis 2 :
http://wso2.org/library/225
http://wso2.org/library/3851
http://amilamanoj.blogspot.com/2011/09/running-debugging-apache-axis2-inside.html
http://shameerarathnayaka.blogspot.com/2011/09/remote-debugging-apache-axis2-with.html

Remote debug configuration for web apps

There are times when developers wants to debug unstable seha007 environment for bug fixes or investigation.

In order to debug server-side code you must start your (app) server in debug mode, and you must have your IDE connect to the remote JVM through a Remote Server Debug configuration (That’s what it is called in IDEA). Other IDE’s should have something very similar to this that will allow it to “hook” into the remote JVM and find out the calls that are being made, and to stop the code when it hits a breakpoint that you have set in the IDE.

For the same there are 2 requirements.

  1. Set up your server to start in debug mode and address a particular port
  2. Setup your IDE to lesson to a particular port and sense the break point.
  • Setup your server to start in debug mode to address a particular port

IPA and LMS are deployed on Tomcat at QA/DEV/Test/Production Environment.

There is one setenv.sh file located inside the $TOMCAT_HOME/bin/setenv.sh

You just need to add the below lines to the JAVA_OPTS variable in the file.

-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,suspend=n,server=y

it will look like this 

JAVA_OPTS=”-Xdebug -Xrunjdwp:transport=dt_socket,address=8787,suspend=n,server=y”

Save the file , and restart tomcat normally.

This time tomcat will run the debug mode at the port 8787.

You can also see in the tomcat logs a message like below.

Listening for transport dt_socket at address: 8787

  • Setup your IDE to lesson to a particular port and sense the break point.

Refer this screen shot for IDEA remote debug setting :

idea_remote_applicationdebug