In this article, we’ll explore Java Platform Debugger Architecture and how it actually works taking Eclipse as an example.
Eclipse allows you to start a Java program in debug mode, Eclipse has a special debug perspective which gives you a preconfigured set of views. In this perspective you control the execution process of your program by setting breakpoints and can investigate the following:
- State of the variables
- Debug stack trace
A detailed article which describes on Debugging with Eclipse can be found here.
The Eclipse debugger is built on Java Platform Debugger Architecture (JPDA). JPDA is multi-tiered debugging architecture that allows developers to create portable debugger applications. The JPDA is used by all Java debugging tools.
Java Platform Debugger Architecture:
JPDA consists of three layers:
JVMTI (Java VM Tool Interface), is a programming native interface implemented by the JVM which interacts with the execution of a program. It provides information of current stack frame and state of the variables, used by development and monitoring tools.
JDI (Java Debug Interface), defines a high-level Java language interface which tool developers can easily use to write remote debugger applications.
A detailed description about JPDA can be found here.
Java Virtual Machine (JVM) execution environment:
Before we start understanding how debug works in Eclipse, lets us understand the JVM execution environment.
The JVM runs in a single process, but it can execute several threads concurrently. Each of the threads are running their own tasks. For example, when we start executing a Java program, JVM process starts a main thread at the entry point ( i.e. main(String args) method). It also starts one more thread for Garbage Collection.
The primary objective of any Java Debugging tool is to suspend a JVM main thread when a registered line number (i.e. breakpoint) and Class are matched.
In this section let us explore how debugging is working in Eclipse. When we run a program in debug mode after setting a breakpoint, the JVM main thread is suspended when the breakpoint is reached.
This is depicted in the following sequence diagram.
Debugging flow diagram (with HelloWorld example):
We can see the stack trace from the above debugging flow diagram, which clearly shows a JVM main thread is suspended at line 7
The section below contains a short video which shows how to use the Eclipse debugger to suspend a program and view its internal state variables.