DEV Community

Cover image for How to debug a problem you can not reproduce
Michael Fomenko
Michael Fomenko

Posted on

How to debug a problem you can not reproduce

Sometimes, I get a bug that I just can't reproduce in the local environment. Still, this bug is very much present in the testing environment (dev or staging).
If I can't reproduce - I can't write a good test to cover this case.
And from this point on, a bug fixing is just one commit after another with a message "bug fix #".
Each time you need to wait for CI/CD pipeline, and after a few tries your workday is pretty much over :(

Everybody who was in this situation knows just how frustrating it might be.
I remember how I spent 2-3 hours comparing environments only to realize that there's a bug that represents only on docker-container-specific JVM.
But what if you could debug code not on your local machine, but remotely?
It appears that you actually can!

Let's take a look at how to do it with Java.

It's not that difficult.
In the script where you launch your Java app, you need to pass special parameters.

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

So now your launch command will look something like this:

java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 app.jar
Enter fullscreen mode Exit fullscreen mode

You might want to change :5005 to the port of your choice. This is a port on which debug socket will be exposed.

WARNING! This parameter must not be used in production. Remote debug allows remote code execution.

Now you can use your IDE to attach a debugger to your remote running application. You just need to do the last step - add debug configuration.

You can do that in the Run/Debug Configurations menu. Choose Remote JVM Debug and set host that your application is running on & debug socket port.

Image description

That's it. Now you can debug code on the staging environment as it was running locally.

Conclusion

I highly encourage you to research this topic for a language of your choice, as this ability might save you a lot of time. You can use this to debug not only the app that is running on a different machine but also that is running in Docker locally.

Here are some resources I found for you:

More on Java remote debugging:
JetBrains tutorial
DZone article

Python: JetBrains tutorial
C#: MS Docs
JavaScript: JetBrains tutorial

Top comments (0)