Tuesday, December 6, 2011

Java Remote Debugging


Consider a scenario where you can't run the application in your development environment, e.g. say your application can run only on a server machine (because it is dependent on some third party interface that are not accessible in your development machine) and you have to resolve a problem (bug). What you can do?

The solution is Remote debugging. Remote debugging is debugging an application by connecting the remotely running application with your development environment ( i.e. you can say to connect with code in your IDE).

Remote debugging can be useful for application development, such as developing a program for a low-end machine that cannot host the development platform, or debugging programs on dedicated machines like Web servers, whose services cannot be shut down. Other examples include Java applications running with limited memory or CPU power, such as mobile devices, or developers wanting to separate the application and development environments, etc.

To debug the application remotely we need to follow following two steps.
  1. Start the application make JVM know that it will be debugged remotely
  2. Configure your IDE to be able to debug that remote application

 Starting the Application with Remote Debugging Enabled

Sun Microsystems' Java Platform Debugger Architecture (JPDA) technology is a multitiered architecture that allows you to debug Java applications in all situations easily. The JPDA consists of two interfaces (the JVM Tool Interface and JDI, respectively), a protocol (Java Debug Wire Protocol), and two software components that tie them together (back-end and front-end). It's designed for use by debuggers in any environment. JPDA is not only for desktop systems but works well with embedded systems, too.

Following are some of the arguments used in the process.

-Xdebug
Enables debugging features.

-Xrunjdwp:
Loads the implementation of JDWP in the target VM. It uses a transport and the JDWP protocol to communicate with a separate debugger application. Specific suboptions are described below. Starting from Java V5, you can use the -agentlib:jdwp option, instead of -Xdebug and -Xrunjdwp. But if you have to connect to the VM prior to V5, -Xdebug and -Xrunjdwp will be the only choice. Following are brief descriptions of the -Xrunjdwp suboptions.

transport
Generally, socket transport is used. But shared-memory transport can also be used on the Windows platform, if available.

server
If the value is y, the target application listens for a debugger application to attach. Otherwise, it attaches to a debugger application at the specified address.

address
This is the transport address for the connection. If the server is n, attempt to attach to a debugger application at this address. Otherwise, listen for a connection at this port.

suspend
If the value is y, the target VM will be suspended until the debugger application connects.
  
java -Xdebug -Xrunjdwp:transport=dt_socket, address=8998, server=y -jar application.jar

The above command says: start myapp.jar + start a server socket at port 8998 and publish the debugging messages using the Java Debug Wire Protocol (jdwp) there. This is helpful if you have the executable jar for starting the application.

Another way is to attach your Eclipse debugger to a running Java process you need to start that process with the following Java options.

-Xdebug -Xrunjdwp:transport=dt_socket,address=8998,server=y,suspend=n
This entry is usually done in *.bat or *.sh files that starts the java process. For example if you want to debug any web application running on JBoss, you can place the following line in its run.bat
set JVM_ARGS=%JVM_ARGS% -Xdebug -Xrunjdwp:transport=dt_socket, address=8182, server=y,suspend=n
Once you have done this and have restarted the server, you can use following process from your Eclipse to attach to this running process.
Configuring Eclipse to Debug a Remotely Running Application
Follow the following steps:
  1. Start Eclipse.
  2.  Go to Run -> Debug Configurations
  3. Create a new Remote Java Application configuration
  4. Configure the remote application's details 
  5. If you would like to have this launch configuration in your favorites menu

No comments: