Debug Code using JPDA

As a Programmer/Developer debugging is as important as developing a software, and can be a tedious job if the piece of code or the running instance of that application is not available on our side.

So, here I am going to discuss an inbuilt standard set by Sun itself as the packages of Java Platform.

What is JPDA?

Java Platform Debugging Architecture is a multi-tired architecture used to debug the application present remotely. JPDA is a set of interfaces and protocols which sun has introduced, to help developers debug application code as well as organizations to develop debugging tool on top of this architecture.

Some of the most popular debugging tools incorporated into IDEs are, Eclipse, netbeans, intelliJ IDEA etc.

When to use JPDA?

It is easy to debug an application running locally. But, think of a situation where we find it difficult to access the running instance or application that runs remotely does not produce anything on the logs. In these situations, knowing how to debug remote applications comes in handy.

Layers of JPDA

As JPDA is a multi-tired architecture, Sun has split this into 3 layers.

  1. JVMTI
  2. JDWP
  3. JDI

Java Virtual Machine Tool Interface (JVMTI) – defines the services a VM must providefor debugging. This includes request for information (for example, current stack trace), actions (for example, set of breakpoints) and notification (for example, when a breakpoint was hit).

JVMTI has replaced JVMDI (Java Virtual Machine Debug Interface) in J2SE 5.0

Java Debug Wire Protocol (JDWP) – is a protocol of information/request transferred between the debuggee process and the debugger.  The specification of this protocol allows both the debuggee and the debugger to run on separate VM implementation and/or separate platforms. This allows the front end to be written in language other than Java.

This also has a transport specification (not part of JDWP) which specifies the transport mechanism (Socket, Serial line and Shared memory) used between front end and back end.

Java Debug Interface (JDI) – Its a front end interface from which the developers can navigate through the code and breakpoints etc.

Components f JPDA

  1. Debuggee – is the process/application that is being debugged.
  2. Java Virtual Machine (VM) – is the virtual machine that is running the debuggee application.
  3. Back-end – of the debugger is responsible for communicating requests from debugger front end to debuggee VM and communicating request back. The back end communicates to front end using JDWP. Back end communicates to VM using JVMTI.
  4. Communication Channel – is the link between front and back end of debugger. There are two mechanisms:-
    • A Connector – is a JDI object which creates a connection from front to back. JPDA defines 3 types of connectors.
      • Listening – front end listens for an incoming connection from back end
      • Attaching – front end attaches to an already running back end
      • Launching – front end actually launches the java process that will run the debuggee code and back end.
    • Transport – mechanism used to move streams of bits between front end and back end. Possible mechanisms are Socket, Serial Lines and Shared Memory.
  5. front end – the debugger front end which implements the JDI.
  6. UI – such as IDE eclipse, netbeans, which provide a way to interact with the code and breakpoints.

How JPDA Works?

Mainly JPDA can be split into two parts – a debuggee/back end running on a VM and a front end/debugger running on our side.

Debugger uses JDI interface which allows the communication to happen from front to back. JDWP is the one who sets the instructions on how to pass the stream f bits from front to back and vice versa.

Back end uses the JVMTI which specifies the services that a VM must specify.

How to use JPDA?

We must determine which transport mechanism to use. if the debuggee and debugger are located on the same machine and box is windows based, then we can use shared memory connector with dt_shmem, or else if both are located on different box then use socket connection with dt_socket.

Case1:- both are on different box

start the application using the command

java -xdebug -xrunjdwp: transport = dt_socket, address = 8000, server = y -jar myapplication.jar

we can either use -xdebug and -xrunjdwp or use -agentlib instead.

  • Server – if vale is set as y, then listen for a debugger application, otherwise attach to a debugger application at a specified address if set as n.
  • Address – of the connection. If server value is y, means it will listen for a connection, if n then it will attempt to attach to a debugger application at this address.
  • Timeout – optional, to specify the amount of time it must wait fot the server to listen/attach to the address.

case2:- if running on same box

set JPDA_ADDRESS=8000

set JPDA_TRANSPORT=dt_socket or dt_shmem

bin/catalina.bat jpda start

Then in IDE go to debug -> remote debugging application -> and click debug. This will try to connect to remote application.

 

!!!!!!! Happy Debugging !!!!!!!

 

Leave a comment