Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debug a java application without starting the JVM with debug arguments

Normally to attach a debuger to a running jvm you would need start the jvm with arguments such as the following:

> java -Xdebug -Xrunjdwp:transport=dt_socket,address=1000,server=y,suspend=n 

Now if I want to debug a process that wasn't started in debug mode, what can I do?

This situation arrises when a production system (i.e. started without debug args) exhibits a 'random' (I use the term loosely) bug. So I can't restart the jvm with the appropriate arguments, because nobody knows how to reproduce the bug again. Is it impossible to attach to the JVM in this situation?

Just to clarify it is not possible to use tools like jdb to attach to already running JVMs unless they were started in debug mode

from the JVM man page

Another way to use jdb is by attaching it to a Java VM that is already running. A VM that is to be debugged with jdb must be started with the following options:

like image 841
hhafez Avatar asked Dec 17 '08 22:12

hhafez


People also ask

How do you debug a Java app?

A Java program can be debugged simply by right clicking on the Java editor class file from Package explorer. Select Debug As → Java Application or use the shortcut Alt + Shift + D, J instead. Either actions mentioned above creates a new Debug Launch Configuration and uses it to start the Java application.

What is remote JVM debug?

Remote Java Debugging is the process of debugging a Java program or application running on another machine or a server environment.

Which is the tool used to debug the program in Java?

The Java Debugger (jdb) is a simple command-line debugger provided by Oracle. It's the equivalent of the GNU Debugger (gdb) for the Java Virtual Machine. The Java Debugger is included in the Java SE Development Kit, so if you have JDK installed on your machine, you don't need to do anything else.

How do I debug an application in Intellij?

Run the program in debug modeClick the Run icon in the gutter, then select Modify Run Configuration. Enter arguments in the Program arguments field. Click the Run button near the main method. From the menu, select Debug.


2 Answers

You may be able to use jsadebugd (JDK) to attach a debug server to the process (available on Windows with the Debugging Tools for Windows). It is marked as experimental, so you may want to try it out on a test machine first.

Usage:

jsadebugd <pid> jdb -connect sun.jvm.hotspot.jdi.SADebugServerAttachingConnector:debugServerName=localhost 

The connector name withe arg can be found using jdb -listconnectors.

like image 64
McDowell Avatar answered Sep 23 '22 01:09

McDowell


Just to clarify it is not possible to use tools like jdb to attach to already running JVMs > > unless they were started in debug mode

in soviet russia source reads you

jdb -connect sun.jvm.hotspot.jdi.SAPIDAttachingConnector:pid=9426 
like image 37
potted plant Avatar answered Sep 22 '22 01:09

potted plant