IntelliJ IDEA debugger follow child process - java

We have some Java code that starts a new process using the following code: Runtime.getRuntime().exec(command); I'd like to be able to tell the debugger that it should follow the child process like you can do with GDB as documented here by issuing the set follow-fork-mode child command.
Is there something equivalent in the IntelliJ IDEA Java debugger? If so how do I configure it?
Thanks

Java doesn't provide an automated way to debug processes and their child processes. One needs to run the child process with the JVM options to enable debugging. IDEA Remote Debug configuration will suggest the proper options to use. Once the process is started with the appropriate options you can connect to it from IDEA with the Remote Debug configuration.
Sample options:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
You may want to start with suspend=y so that the execution is suspended until you connect with the debugger.

We now have this Intellij plugin that uses a java agent to automatically start all child processes in debug mode.
It listens for any debugee processes on 7857, then attaching debugger to those processes.
Install AttachMe
To enable automatic mode, run this first, source ~/.attachme/conf.sh
Afterwards start AttachMe listener
Test it with maybe a Play application in prod mode (assuming play plugin is enabled), do sbt start. Play in prod mode forks another JVM process. Debug points should now hit.

Related

How to profile a java application in debug mode with JProfiler

I would like to get a memory snapshot at a very specific point in time, i.e. at my break point.
But I can't seem to find the necessary buttons/configuration to start the profiling in debug mode.
Using Eclipse and JProfiler 7.
Edit: More info:
Use
Sessions->Integration Wizards->New Remote Integration
in the JProfiler GUI to get the VM parameter for profiling and add that to the VM parameters of the debug run configuration in eclipse. When the run configuration is started, it will wait for a connection from the JProfiler GUI.

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

Remote Debugging with Intellij Idea

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 .

Run commands before remote Java debugging in Eclipse

I would like to debug my Java application usually locally, but sometimes on a remote server. I was thinking about rsyncing the class files and the jar dependencies to the remote server (perharps in an ant script) that is run occasionally, and then running the application remotely by ssh and using an ssh tunnel for connecting to the remote JVM.
This is easily achieved by running the rsync part as a Builder and the ssh tunnel as an external tool which is ran before debugging sessions. However, I would like to simply launch the remote debug configuration and have it up & running. Ideally, I would like to execute some code snippet before Eclipse tries to connect to the remote JVM, and possibly have its output appear in a Console view.
Is there any way of achieving this with some plugins (it is not supported out of the box)? I suppose I could write a quick hack as an Eclipse plugin, but I would prefer not to reinvent the wheel.
If installing CDT is an option, then you could use its launch groups for running your external tool together with the normal launch configuration:
Alternatively you may want to have a look at the EclipseRunner plugin. While it can organize launch configurations in groups, I'm not exactly sure if the groups can be launched as such.

Running a Junit test remotely, as if it were running locally, using Eclipse

I've search around, read relevant questions on this site and others, but have failed to find a solution. It strikes me odd that one does not exist so let me detail my question here:
I use Junit4 + Eclipse regularly to test my code. In some cases, certain tests can take a lot of CPU and/or memory, rendering my workstation unusable for the duration of the test. This is a pain I'm trying to solve.
I'm looking to get the exact same behavior but through a remote server. I want:
To still be able to set breakpoints and debug my app.
To see how the tests progress using the Junit view in Eclipse.
Click on a button have the tests started (build process and copying of files is allowed, but only if efficient).
In my mind I envision something that rsyncs the files to the remote server, starts the java process there with the exact same arguments as it would on my local machine, makes the debug port available (not just localhost) and has eclipse hook up to it to have both debug and junit view working.
How can I get this done?
Several leading questions that may help us find a solution:
How does Eclipse communicate with the java process when run locally (for both debug purposes AND the Junit view)?
How can I involve myself in the process of spawning the java process for the JUnit testing so I can copy the required files over to a remote server?
How can I make the process spawn remotely instead of locally?
How can I have Eclipse hook up to the remote host instead of the localhost?
The easiest would be to invoke command line JUnit runner on remote mashine using the following command:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=8998,server=y,suspend=y -cp ... org.junit.runner.JUnitCore <test class name>
So, it will wait til you attach remote debugger from Eclipse at port 8998.
You can also use Eclipse's Target Management tools to transfer files to remote system and launch remote commands. There is several tutorials on the project page.
You could set up a jenkins CI server, sync your code via git (or just copy using ftp or something), execute the test in a jenkins job triggered by a git-hook or through some script. Then remote debug into the running test process like Eugene Kuleshov suggested. This process could be automated by an ant-script which you invoke from eclipse. There should be a mylin connector (for example) through which you can monitor the running tests. I don't know if it is possible using the standard JUnit view of eclipse to see the running tests without using some custom plugins (if any exist).
Try vs code remote ssh extension for this.

Categories