Monday, September 6, 2010

How to debug our code on the remote machine : remote debugging with jbuilder and intellij idea..

We write our code in our pc then we put it to a remote server. Sometimes it fails :)
Then what do you do to understand what is going on ?
Thread dump does not give what you need.
You need to connect your java process and debug.


To debug a process which is running a remote server you need to start it with the following JVM_ARGs.


-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n


Sample usage : java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n com.mycompany.test.WriteYourOwnCode


By the way your process will be waiting your connection from your machine whenever you want. Port number [3999] will be reserved for this debugging connections. Try to reserve a port number which is not used any other process/system.Otherwise your process will not start.You will see an error in this case something like this.


[serkans@serkansunellnx voicemail] java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n com.mycompany.test.WriteYourOwnCode
ERROR: transport error 202: bind failed: Address already in use ["transport.c",L41]
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510) ["debugInit.c",L500]
JDWP exit error JVMTI_ERROR_INTERNAL(113): No transports initializedFATAL ERROR in native method: JDWP No transports initialized, jvmtiError=JVMTI_ERROR_INTERNAL(113)


In the jvm_args you see an option : suspend=n . This means your remote code will not wait anybody to connect it before start. If you set this option to YES [suspend=y] process will wait before starting automatically. This suspend options must be set to YES [suspend=y] when you are unable to catch an error which is  happens in the beginnging of the process.


After starting our process with this options it is running and listenning for your connections...


Let me put a few picture to show how to connect to a remote process.
I'll show for both JBuilder and IntellijIdea.


1) Remote debugging with Jbuilder.


Firstly we start our code on a remote server with the remote debugging options.


[serkans@serkansunellnx voicemail] java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,address=3999,suspend=n socketLogger.LogServer
Listening for transport dt_socket at address: 3999
 >> Server waiting for log messages...

It started and said that waiting on port 3999.It is not hanged.It is running, cool down :)



Then we turn to our ide.In the Jbuilder Right Click on the project and select project properties.
Click to the Run on the left pane of the project properties window.You will see RunTime Configurations. Click to New . A new window will appear called "New Runtime Configuration".Then click to the Remote in the left pane of this window.
1) Select "Enable Remote Debugging" by clicking the checkbox.
2) Select "Attach".
3) Write the IP of remote server on which your code is running as hostname.
4) Change the port number if you selected something different from 3999.


That is all.Click OK , OK  and close these windows.


It will looks like that :




After doing these right click on your Project .Select Debug..Ohh yes now you see the newly defined Runtime Configuration called "Your connection to remote process". Yes ...Click on this.




Now you are connected to your code.Go there and put your breakpoints. If your process hit your breakpoints you will notice it :-)


I got tired... I will expain it for Intellij Idea later.

No comments:

Post a Comment