Keywords: IntelliJ IDEA | Remote Debugging | Java Process
Abstract: This article provides a comprehensive guide on attaching IntelliJ IDEA debugger to running Java processes. It covers remote debug configuration setup, JVM debug agent parameters, debug session management, and prerequisites. With step-by-step instructions and code examples, developers can master remote debugging techniques to enhance problem-solving efficiency.
Remote Debugging Overview
IntelliJ IDEA offers powerful remote debugging capabilities that allow developers to attach the debugger to locally or remotely running Java processes. This debugging approach is particularly valuable for production environment troubleshooting, distributed system debugging, and applications that cannot be started directly in the development environment.
Debugging Prerequisites
To achieve full debugging functionality, three key conditions must be met:
Debug Agent Configuration
The target Java process must load the debug agent during startup. The debug agent is a component responsible for communicating with the debugger through socket connections. Configuration example:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
Debug Information Compilation
Application bytecode needs to contain debug information, which is generated during compilation using the -g compiler flag. Debug information includes critical data such as local variable names and line number mappings. The absence of this information will limit debugger functionality.
Source Code Access
It is recommended to have access to the source code of the project being debugged. IntelliJ IDEA can match debugging events with source code and display relevant debugging information in the editor, providing an intuitive debugging experience.
Creating Remote Debug Configuration
Configuring remote debug connection in IntelliJ IDEA requires the following steps:
- Open run configuration dialog via
Run > Edit Configurations...menu - Click the
+button in the upper left corner to add new configuration - Select
Remoteoption in the left-most panel - Name the configuration (e.g., "remote-debugging")
- Configure connection parameters and save
JVM Debug Parameters Detailed
The debug agent parameters include several key options:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005
Parameter explanation:
transport=dt_socket: Specifies socket transport methodserver=y: Process acts as server listening for debugger connectionssuspend=n: Application starts immediately without waiting for debugger connectionaddress=5005: Debug port number
Environment variable configuration example:
export JAVA_OPTS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005"
Initiating Debug Session
After configuration, follow these steps to start debugging:
- Start the target Java application normally
- Select the created remote debug configuration in IntelliJ IDEA
- Click the
Debugbutton to initiate debug session - IntelliJ IDEA will connect to the JVM and initialize remote debugging
Advanced Debugging Features
Startup Suspension
Changing suspend=n to suspend=y makes the application wait for debugger connection during startup, suitable for scenarios requiring breakpoints during application initialization.
IntelliJ IDEA Debug Agent
To utilize advanced features like async stack traces, load IntelliJ IDEA's dedicated debug agent:
-javaagent:"/path/to/debugger-agent.jar"
Read-Only Debug Mode
For local processes without debug agent enabled, the debugger can be attached in read-only mode. This mode allows viewing call stacks and related local variables, useful for initial diagnosis when programs hang.
Debug Session Management
Disconnecting
To end a debug session, click the Stop button in the Debug tool window toolbar or press Ctrl+F2. Unlike local debugging, remote processes continue running after debugger disconnection.
Process Termination
When the remote process is no longer needed, it can be terminated by closing the corresponding debugger tab. IntelliJ IDEA will prompt to choose between terminating the process or simply disconnecting.
Practical Application Scenarios
Remote debugging is particularly useful in the following scenarios:
- Production environment issue diagnosis
- Distributed system component debugging
- Containerized application debugging
- Third-party library integration problem investigation
- Performance bottleneck analysis
By mastering IntelliJ IDEA's remote debugging techniques, developers can more efficiently locate and resolve complex runtime issues, improving software quality and development efficiency.