Remote Debugging a Process using IntelliJ

[Reposted from my blogger]

This is a brief post on something that is rather very important. Your company probably handed you a macbook or laptop and have a Linux VM hosted somewhere, that you will do all your development on. And now the circus begins.

You like to stay on your laptop since you get all the nice IDEs and Code diffing tools and what not. But, your code only runs on the VM, rightfully so in the highly SOA (Service Oriented Architecture, basically meaning everything is REST and has nagios alerts).

So, here’s how to get the best of both worlds.

Pre-requisite: Use a tool like unison or Bittorrent sync or roll your own scripts to rsync your local repo on laptop with a directory on your VM.

End state would be

  • You git clone or svn checkout on VM
  • Syncing gets it down to your laptop
  • From there on you make code changes on your IDE and syncing reflects them on VM

Most of all, it means that intellij can look for the source code for the classes you debug, on your repo on your laptop. Now, onto the actual setting up of debugging itself.

  1. In IntelliJ, go to run > edit configurations. Click on the + , pick remote. IntelliJ automatically tells you what to use to launch your remote JVM.

A useful note on the options

  • suspend=y/n , controls whether the JVM should wait for the debugger to attach before starting up. So, if you have trouble starting up your application, use suspend=y to troubleshoot

From here on,

1. Setup whatever breakpoints you want to in your project.

2. Launch your application on your VM, with the suggested params

3. (Optional) Setup port forwarding if needed

Sometimes, your VM may not allow incoming connections at a random port that Intellij picks out for you. you can either change the Intellij port or

A) Enter localhost as the hostname in your debug configuration

B) ssh -L <debugport>:<vm.hostname>:<debugport> <vm.hostname>

eg: ssh -L 5005:vm.mycompany.com:5000 vm.mycompany.com

4. In IntelliJ, hit “Debug” on your debug configuration ..

Happy debugging!