Remote Debugging in Golang and Java

Remote Debugging Golang

Debugging is an indispensable tool for any developer. It gives us a way to analyze the code as in when it is processing the data. Us being a commander of the whole process, can suspend the flow and analyze the data realizing what the code is doing.

All the currently available languages provide some sort of debugging method through code. You take your favorite IDE, add some plugins, and there you go. You can start debugging your code locally.

Remote debugging is a different beast altogether, however. It is ideal for when you want to debug something that is not running in your local environment. Communication regarding all the debug processes needs to be communicated via a network.

In this article, we will be looking at how to perform remote debugging.

Debugging Support in Modern Tools

I personally use IntelliJ and VSCode, 2 of the most popular IDEs in the world, in all of my projects. 

IntelliJ and Other Jetbrains IDEs

Setting up remote debugging in IntelliJ is really straightforward. If you are using a professional license like me, the plugins make the debugging process easy as cake.

Here are the current offerings of plugins for remote debugging in IntelliJ.

Remote Debugging Intellij

VSCode

In VSCode, the setup is via the launch.json file in the root of the project inside the .vscode folder. 

Why Is Remote Debugging Useful?

In the Lower Environment

Running an application in a local environment and in a server environment is totally different. A code often runs locally but fails in the server when communicating with other services. Remote debugging gives us the flexibility to catch issues quickly in the server environment.

In Production

It is not easy to debug a bug in the production environment. If the error is not known, it is good practice to take a replica of the production traffic to a staged server in pre-production running in debug mode and debug the issue. This way, we can very quickly and effectively catch the issue and send a bug fix.

Let’s now see how to perform remote debugging in 2 of the more popular languages.

Remote Debugging in Java

Java is one of the most popular languages today. It first came into existence 25 years ago and is still going strong. Starting as a compiled and statically typed language, today, a number of dynamic languages, like Clojure, Scala, and Kotlin, run on the truly mature runtime, the JVM.

Here, we will remotely debug a sample Java app running in an EC2 server.

Step 1

To set up the process, we need to add the following options to the Java startup script:

JDK9 and later:

-agentlib:jdwp=transport=dt_socket,server=y,address=*:5005,suspend=n

JDK 5 – 8

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

Remote Debugging Java

Here, I am running a simple Java application built with Spring Boot in an EC2 server. In the second line, we can see the 5005 port is used for debugging. Now, we need to add it to the security group.

Step 2

Set up the security group of the EC2 server to allow inbound only traffic on port 5005. This configuration will let us use our IDE to connect to the jvm process running in the remote EC2 server. Failure to do so will result in the connection being refused.

Edit Inbound Rules

Step 3

Configure our IDE to connect to the AWS EC2 server. The command palette is available in the configuration menu on the home page of IntelliJ.

* Provide the hostname to be the EC2 public IP.
* Provide the port 5005 as we configured in the security group.

Remote Debugging Java Intellij

That’s it. We can now debug. Once we are connected, we will see the following message.

Remote Debug Console

Remote Debugging in Golang

Golang is the new kid in the block. Go is a statically typed, compiled programming language designed at Google by Robert Griesemer, Rob Pike, and Ken Thompson. First appearing in 2009, it has gained momentum very quickly by providing an easy-to-learn language with garbage collection and cross-compilation, and statically linked binary.

For Golang projects, the process is not very different. We can use a sample Golang project to understand how to use remote debugging.

Step 1

Install Delve, the Go debugger. 

Step 2

Allow Delve to compile the application:

dlv debug –headless –listen=:5005 –api-version=2 –accept-multiclient

Or compile the application using Go 1.10 or newer: 

go build -gcflags “all=-N -l” main.go (In my case, it may be a package for your case)

This step is necessary to create a binary with no optimization and inlining. We may not want to use this in production, but only for debugging purposes.

Step 3

Start the application with Delve using the following command: 

dlv –listen=:5005 –headless=true –api-version=2 –accept-multiclient exec ./main

Go Remote Debug

Step 4

Configure the IDE (Golang/IntelliJ for my case) like this:

* Provide the hostname to be the EC2 public IP.
* Provide the port 5005 as we configured it the last time while doing Java debugging.

Go Remote Debug Intellij

Then debug using the created configuration:

Go Code Debug Point

Conclusion

In this post, we learned the advantages of remote debugging and when and how to use it for our benefit. No matter whether you are a seasoned or a novice programmer, knowledge about remote debugging will serve you better in your field, whatever the situation may be.

Best of luck! If you like this post, consider sharing it.