Saturday, May 12, 2012

Remote Debugging

Coding is simple, but it get tougher when something does not work as our normal human mind expects it to work. We wonder in woods, we shut the mind and go out for smoke, then we go to seniors to test their potential to attack and solve the problem and if every door closes on you then debugging comes to rescue each of us.
Now problem and tension aggravate when we could not find any issue on local machine whereas same code bombs at production environment. Oh god, no-one can really rescue the server and customer as some witch has modified the computer language to perform it abruptly.
Again we have rescuer. Different IDE can rescue us from this situation by using remote debugging. Local debugging is not as cumbersome as remote debugging. It takes few configurations to enable remote debugging.
Lets look into remote debugging configurations.

Configure Eclipse Project for Remote Debugging

Open Debug configurations under Run menu.

This will open a dialogue as shown below

Right click on REMOTE JAVA APPLICATION and click on new. It will open an editable box to configure properties for remote debugging.

Enter the name of the debug configuration. Do not forget to change the Host and port properties. Make the changes and click on apply button. Make sure to remember port number as this port number will be used at the time of configuring java application at server.

Apply the setting and closed the window.

Configure Java Application Running at Server
Actually you do not need any configurations. To run the java application you need to pass following JVM Options.
-Xdebug -Xrunjdwp:transport=dt_socket,address= {PORT_NUMBER_PASSED},server=y,suspend=y

 If earlier the command used to run java application was
java testJava
Now it will be modified to
java testJava -Xdebug -Xrunjdwp:transport=dt_socket,address={PORT_NUMBER_PASSED},server=y,suspend=y

Explanations
All options mentioned above will decide how application will run in debug mode.

Make a note of following points below:
* jdwp- (Java Debug Wire Protocol) this protocal enable your debugger(eclipse) to debug remote VM.
-Xrunjdwp: enables the jdwp for u
* -Xdebug  will start java application in debug mode .

Remaining jdb parameters will specify how application will run.
* suspend = y - Applicaion will not start until eclipse remotely connect to it.
* suspend =n - Application will start normally and it will stop at breakpoints
* transport = dt_socket will instruct jvm that debugger connections will be made through a socket
* server = n Attach to the debugger application at the specified address.
* server = y listen for connection at this address.
* 1044 is default port, this is transport address for connection.

This was simple but it may take some time to configure the application if you are running it for the first time. Have patient and try again it will work. There is not anything else you need to do. Configuring remote application has troubled me in the past and gives scare now also. But believe it is not rocket science.