Header Ads Widget

Responsive Advertisement

Remote Command-line debugger (Sun’s JDB)


 Sun's JDB (Java Debugger) is a command-line debugger that comes with the JDK. It allows developers to debug Java applications, set breakpoints, inspect variables, step through code, and more, all from the command line. Remote debugging with JDB enables you to connect to a Java application running on a different machine or JVM and debug it remotely.

Step 1: Enable Debugging on the Target Application

To debug a Java application remotely, the JVM on the remote machine must be started with special debugging options. These options will enable the JVM to listen for a remote debugger to connect.

To enable remote debugging, you need to start the Java application with the following arguments:

bash

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 -jar YourApp.jar

 

Ø  transport=dt_socket: Specifies the transport mechanism (a socket in this case).

Ø  server=y: Indicates that the JVM will be running in server mode (i.e., it will wait for a debugger to connect).

Ø  suspend=n: If n, the JVM starts normally without suspending. If y, the JVM will suspend the application until a debugger connects.

Ø  address=*:8000: Specifies the port where the JVM listens for a debugger (in this case, port 8000). You can change the port based on your needs.

Step 2: Connect JDB to the Remote JVM

Once the application is running and listening for a debugger on port 8000 (or whatever port you specified), you can use JDB from another machine to connect to it.

In your command line, run:

bash

jdb -attach hostname:8000

 

Ø  hostname: This is the IP address or hostname of the remote machine where the JVM is running.

Ø  8000: This is the port specified in the JVM startup options.

Once the connection is made, JDB will take control, and you’ll be able to start debugging remotely.

Step 3: Common JDB Commands for Debugging

Once connected, you can use the following commands to debug the remote application:

Ø  stop at <class>:<line>: Sets a breakpoint at the specified line in the specified class.

bash

stop at com.example.MyClass:15

 

Ø  stop in <class>.<method>: Sets a breakpoint in the specified method.

bash

stop in com.example.MyClass.myMethod

 

Ø  cont: Continues execution of the program.

Ø  step: Executes the next line of code.

Ø  next: Moves to the next line, but does not enter methods.

Ø  print <variable>: Prints the value of the specified variable.

bash

print myVariable

 

Ø  where: Displays the current call stack.

Ø  locals: Displays all the local variables in the current context.

Ø  quit: Exits the debugger.

Step 4: Example Debugging Session

Let’s go through an example.

  1. Start the application (on the remote machine):

bash

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000 -jar MyApp.jar

 

The application starts and listens on port 8000 for a debugger connection.

  1. Connect JDB (from the local machine):

bash

jdb -attach 192.168.1.100:8000

 

Now JDB is connected to the application running on the remote machine with IP 192.168.1.100 and port 8000.

  1. Set a breakpoint:

bash

stop at com.example.MyClass:25

 

This will set a breakpoint at line 25 of MyClass.java.

  1. Continue the program:

bash

cont

 

The program will continue running until it hits the breakpoint.

  1. Inspect variables: When the breakpoint is hit, you can inspect variables:

bash

print myVariable

 

  1. Step through the code:

bash

step

 

Step 5: Closing the Debugger

When you’re done debugging, you can stop the debugger session with:

bash

quit

 

This will exit JDB and leave the application running.

Additional Notes:

Ø  Ensure that the firewall on the remote machine is configured to allow connections on the debugging port (e.g., 8000).

Ø  If you use suspend=y in the JVM arguments, the application will not start running until a debugger connects, which can be useful for debugging startup code.

Benefits of Using JDB for Remote Debugging:

Ø  Lightweight: JDB is a lightweight option, especially in environments where a GUI debugger might not be possible.

Ø  No GUI required: If you are working on headless systems or in environments where graphical tools are unavailable, JDB provides a way to debug from the command line.

Ø  Full control: You get full control of breakpoints, variable inspection, stepping through code, etc.

 


Real example process:

Steps for setting up remote debugging with JDB are clear and concise! Here's a refined version that organizes the process while keeping all essential details intact.


Remote Debugging with JDB: Step-by-Step Guide

Step 1: Initiate a New Project

Ø  Create a new project, for example, SKV.war.


Step 2: Deploy the Project

Ø  Deploy it on a virtual machine, such as 192.168.57.66.


Step 3: Execute the Program

Ø  Start the application using either a Java application or JBoss.


Step 4: Configure JBoss for Debugging

Ø  Review the JBoss configuration using the following command:

bash

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=y

 

This command starts the JVM in debug mode, listening on port 8787.


Step 5: Access the Virtual Machine

Ø  Use the following command to access the virtual machine:

bash

ssh username@192.168.57.66

 


Step 6: Log in via SSH

Ø  Execute the SSH command to log in:

bash

ssh username@ip

 


Step 7: Check the Java Version

Ø  Verify the installed Java version by executing:

bash

java -version

 


Step 8: Determine the Java Path

Ø  Find the Java installation path using:

bash

which java

 


Step 9: Navigate to the Java Path

Ø  Go to the Java directory and verify the JDB version with:

bash

jdb -version

 


Step 10: Attach to the Remote Debug Port

Ø  Connect to the remote debug port using:

bash

jdb -attach 8787

 


Step 11: Set a Breakpoint in Your Class

Ø  Specify a breakpoint at a particular line in your class:

bash

stop at com.kartik.tutorial.Blogger:25

 

Ø  Syntax:

      • To set a breakpoint at a specific line:

bash

stop at <class name>:<line number>

 

      • To set a breakpoint on a method or variable:

Bash

stop in <class name>:<method name | variable name>

 


Step 12: Repeat for Additional Breakpoints

Ø  Set additional breakpoints as needed using the same method as in Step 11.


Step 13: Trigger the API

Ø  Execute the API call and observe the debugging process in action.


Step 14: Conclusion

Ø  Thank you for your time and attention.


This structured format should help anyone following these instructions to easily understand and execute the remote debugging process with JDB. If you have any additional tips or insights, feel free to add them!

 


Remote Command-line debugger
Remote Command-line debugger











Post a Comment

0 Comments