Remote Debugging with Intellij Idea - java

I came to know of the remote debugging procedure under Idea recently. What I do is copy the configuration of remote debug under Run | Debug Configuration in Idea to the command line java execution parameters. The actual command line parameters are:
-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8000
If it is a script, I add these commands to it. By doing so, the command line displays the message:
Listening for transport dt_socket at address: 8000
So the debugging can happen using the local source code. However, I don't properly understand how remote debugging work. Anyone who knows how remote debugging actually works, please give me an explanation.
Thank you!

Remote debugging means that you can run your Java code anywhere, either on the local or remote machine. When it's running in the debug mode, you can connect to it from the IDE using TCP network connection and perform debugging. IDE needs to have the source code for the running classes so that you can place breakpoints inside this code and perform stepping, inspecting variables, etc.
If you are interested in technical details, refer to the JPDA documentation.

Consider a scenario where you want to fix something in your application but your application only can run over a server machine because of other dependencies.
That is where Remote Debugging come into picture. You Just connect the sever by providing the hostname and port and connect it with your respective environment.
How It works:
Application to be debugged will attach a socket to itself and then will start listening debug instructions.
Debugger will bind itself to that socket and then send instructions.

This is best way to test your code which are in different environment .
we need to have below points for sure before you are using remote debug .
we are using JBOSS in our servers.
configure - JBOSS_HOME/bin/run.conf
JAVA_OPTS="${JAVA_OPTS} -Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"
now add the server IP and port number into the intellij remote debugging .
4.your should have the latest version of the project in local that is in synch with the server else debug will not be allowed.
you need to start the intellij server for the project .
Then start the remote debug .
place a debug point in local and when we start testing in the server , when it hits the debug point it will stop and wait untill you process it .
The other point is , it will hold all the request in the queue and will not allow anyone to go through the break point which may stop other users to test it .

Related

How do I debug Java servlet filters, on-the-fly, from a remote machine (I'm using Eclipse and Apache Tomcat 5.5)

I wrote a Java servlet filter on my local machine and deployed it a remote (machine) web server. Unfortunately, it's been very difficult and time-consuming trying to trace errors reported by Apache Tomcat 5.5, my JSP/servlet engine. I can't keep writing System.out.println(...), saving, deploying, testing JSP pages, and so on. It's taking too long. There has to be a better, faster way.
Is it possible to remotely debug servlet filters? I don't have a web server on my local machine, which is why I'm asking about remote debugging. Specifically, I'm looking for a way to debug, line-by-line, the servlet filter, on-the-fly, as it's happening on the remote web server. Does such a method exist?
Or, is there a better method than writing to standard output. It's taking too long and I feel that must be a more efficient means of debugging Java servlet filters.
Note: I'm using Eclipse for development.
Thank you very much for any help.
Update
Thank you all for your help. I really appreciate it.
I added the JVM argument to Tomcat, restarted Tomcat. Then, on the machine with Eclipse, I entered in the appropriate info in the Debug config, put the breakpoint in, and tested. Unfortunately, it did not work. In the config, I left it as Socket Attach, clicked apply, and that was it. I pressed the debug button and it said the connection was refused. I tried ports 8000 and 8001 and both did not work.
Let me explain what I'm trying to do, that might be better.
I have a login page called login.jsp. On that page, is a form whose action attribute is servlet/LoginServlet. When the user submits the form, it calls servlet/LoginServlet, which is mapped to a class in the web.xml file. Let's call this class com.mysite.mypkg.classA. In class A, it calls a method from another class called com.custom.mypkg.classB. I want to put a breakpoint in classB.
So, using the url with login.jsp page in the Eclipse debugger won't call it. I tried using servlet/LoginServlet and that also did not work.
What should I put in for the URL? Or, do I debug this type of setup?
Thank you.
Update 2
I found this site here, which is pretty comprehensive. I ran netstat -a and noticed that the debug port is not listed. Windows Firewall is turned off, but there could be another thing blocking the port, who knows. Anyway, I placed the VM argument here and it's not working.
Thank you.
For remote debugging you need to start the server in debug mode. There are couple of ways doing that.
1 > start the server using
catinlina.bat jpda start
2 > Add an jvm argument to the tomcat java process
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n
Once the server is started in debug mode , you need to change the perspective of the project in eclipse to debug.
Then go to Run - > Debug configuration.
Double click remote java application and enter the details such as
Remote IP address
Debug port . Default tomcat debug port is 8000. If you use jvm argument, use the port mentioned in the jvm argument.
Click Apply
Go to the java file you want to debug.
Put a break point in the source code and run the scenario you want to test (Eg Web application using browser)
Also , ensure that the code in the java file is in sync with code deployed on remote server.
Happy Debugging!!!
Peace.
Sanket Raut
You can attach a debugger to a running Tomcat instance, provided that you gave it the right command line options when you launched it.
The Tomcat Development Wiki explains how to do this, and as a bonus gives you instructions on how to set up to debug from the Eclipse or NetBeans IDEs.
Of course, attaching a debugger to a running Tomcat has both security and performance implications*.
* And OH&S issues - you might get badly scratched if you tried this on the wrong kind of tomcat ...
You should run your remote tomcat with the following starup parameter:
bin/catalina.bat jpda start
Then in Eclipse on your local machine go to Run -> Debug Configurations -> Remote Java Application, create new configuration here, use IP of the remote machine as a host and 8000 as a port there
Run this configuration and use the breakpoints in Eclipse for debugging

Debugging Java process using IntelliJ - connects to socket but not to target VM

Now resolved - see end of question.
I am attempting to debug a Java process with IntelliJ Community Edition's debugger. The socket is listening - but when I try to connect the debug process shows the following
'Connecting to the target VM, address: ':8003', transport: 'socket'
It never makes the connection to the VM and I cannot debug.
I have moved to a Windows 7 64 bit PC - on my old XP machine and could connect and debug this Java process (it's an app I build and maintain).
It does not fail with the standard "Connection refused: Connect" error that you would get if no process was there to connect to. Netstat also shows the port is listening when the app is running.
TCP 0.0.0.0:8003 :0 LISTENING
Debug args for the app
-Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=8003
-Xdebug
If I attempt to run the debug session from IntelliJ on a remote machine, I can connect to the process running on my local machine, and debug.
If I run the Java app on a remote machine, and use IntelliJ on my local machine, I can debug.
It is only if I run and debug on the same machine that it fails - unfortunately this is what I need to do almost all the time.
The only similar issues I have found are from 2004, to do with file paths with spaces, and were NetBeans, not IntelliJ. I have rebuilt and rerun my app ensuring no spaces or underscores in the path, no joy.
My intelliJ debug settings are to debug my local machine on socket, attach mode port 8003 - sorry as a new user I can't attach an image.
Other things I have attempted:
Changing JRE versions
Debugging using shared memory rather than socket transport
I have not re-installed IntelliJ - I am using the same build as my fellow devs (10.5 IC 107-105) who don't have this problem.
Changing the port the debugging process listens on (from 8003 to various other, unused according to netstat)
I have tried using PC name, IP Address and 'localhost' to refer to the PC in the debug settings.
Stuck. Any help much appreciated.
Thanks
Steve
Resolved
Well, a day of failing to solve then I find the answer 20 mins after posting. An invalid JNI signature character, only picked up when debugging. Solved by adding this arg when debugging.
-Dcom.sun.xml.bind.v2.bytecode.ClassTailor.noOptimize=true
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6547438
Thanks for the assistance. Still don't know why it would work when debugging remotely but not locally before.
It sounds like a strange firewall issue.
It appears that you should be able to try telnet localhost 8003 and it should fail. This means nothing can connect to your app on that port.
EDIT: If shared memory does work either and you cannot connecft it via telnet, there is a problem with the way you are running the app.
You will get this type of error if the application is not running.
Can you try debugging a simple program you lauch from IntelliJ?

Remote Debugging in eclipse

I have an application running in server A. The dev environment is in server B.
I am trying to do remote debugging of app running in server A.
In server A i added following command to service start script
-Xdebug -Xnoagent -Xrunjdwp:transport=dt_socket,address=4554,server=y,suspend=n
And service is running in server A.
When i try to launch remote debugging configuration it gives
Failed to connect to remote VM. Connection refused. Connection refused
port 4554 is free in server A.
What other configuration need to be done for this?
Regards
Dheeraj Joshi
Try this.
Set suspend=y, just to make sure you got the JVM line right. This should stop the VM on startup until you connect.
If you're on Unix, bring up the terminal and try telnet [host] [port] - this will quickly let you know if there's anything listening to that port on that host.
Make sure the connection properties in Eclipse are set correctly. Note that the port defaults to 8000.
Use the IP address instead of the host name, to rule out DNS/hostfile problems.
Another way of starting the JVM that I use successfully is:
-agentlib:jdwp=transport=dt_socket,address=8000,server=y,suspend=y
Check if there is a firewall between and/or on the two servers.

Connecting multiple debuggers to a debugee (Java,JPDA)

Ive been trying unsuccessfully to connect two client debuggers to
a Debuggee program in context of JPDA. Is this possible or
are there workarounds to make it happen?
I am using eclipse as the IDE (edit for typo).
Think of a server program as a Hello World
which Prints out:
System.out.println("I have the String"); //1
System.out.println("You will have to pass through the breakpoints before you shall see");
System.out.println("breakpoints");
System.out.println("before you shall see"); //4
We can put breakpoints at lines 1 and 4.
Step 1:
The params passed to the program in Run Configuration:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y
(server=y tells vm to behave like a server, suspend=y implies that prog execution will be suspended till debugger latches on to it)
and Run the program.
Step 2: Go to Debug as , Debug config ,Remote Java application and
create a new instance:
Project: Same as before
Connection type: Socket Attach(Socket Attach)
Host:LocalHost
Port:8000
Now when I debug Prog execution stops at the specified breakpoint.
What I cant do is create another instance of this remote debugger that
can latch on to the server(prog 1), I get a connection refused when I do that. Let me know if anyone else has faced this problem
and if a workaround exists.
Thanks!
Thanks
AFAIK there can be only one instance of debugger connected to a java program any given time. Once you started you program in debug, Eclipse connects to the debugged program blocking all other connection attempts. If you want to connect remotely you can run the program not in debug mode and add the parameters:
-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=y
to java parameters manually, then you should be able to connect with another debugger.
I don't know how things were in 2010, but in 2014 this is doable. I'm debugging an applet and a servlet at the same time eventough they use different JVM, to do so just start your remote debug process in eclipse like socket Listen, the start the first java applicaction with:
-Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8787,suspend=y
Then start the same eclipse remote debug process and start another java process with the same options, you should see the processes spawn like this:
You can see the two different JVM listening in the same port (nevermind the name of the 'applet' process)

How to attach a debugger to WAS?

I need to attach a debugger to a remote instance of WAS and know that debugging is enabled. The problem is getting WID or whatever IDE I wanted connected to the server so I can step through the code.
I tried using Netbeans to connect and I get handshake failed, connection interrupted. The debugger is listening on port 7777. If I go into the WID admin console, I need a username and password. Would I need the same to attach a debugger, if so, where can I specify that?
Walter
Can you not add the following line to the startup script's parameters of WAS:
-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=7777
And then attach your Netbeans remote debugger on port 7777
WAS can be configured for remote debugging via the web administration console.
It is also possible to configure this without starting WAS - via one of the profile XML files. But I don't remember the details and don't have WAS handy to check.
I think I simply need to enable the service at startup then I should be able to connect? I have to get permission first to do so.

Categories