Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java remote debugging, how does it work technically?

I really like the remote debugging facilities of the JVM. But I wonder how it works internally.

My assumption: It is done through a JVM feature where the running process is downloading/using the source-code from the attached remote-debugger (like IDE) It knows the line of the current stack-trace and then can jump to the respective IDE breakpoint. The communication of stack-trace and introspection of the application state is then done either through sockets or shared-memory (setting of remote debugger).

Has anybody interesting links/resources on that?

like image 390
manuel aldana Avatar asked Aug 28 '10 16:08

manuel aldana


People also ask

How does a remote debugger work?

Remote debugging is when you debug an application running in a place or environment different from your local machine in a way that resembles local debugging. The point of this is for developers to debug components of distributed systems without difficulty.

What is remote debugging in Java?

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

How does a debugger work in Java?

Debugging allows you to run a program interactively while watching the source code and the variables during the execution. A breakpoint in the source code specifies where the execution of the program should stop during debugging. Once the program is stopped you can investigate variables, change their content, etc.


2 Answers

The debugging features of the JVM are provided via the Java Platform Debugger Architecture (JPDA).

The JPDA itself is composed of the following:

  • Java Virtual Machine Tool Interface (JVM TI) - the native programming interface for tools to use. This interface allows for state inspection and helps in controlling the flow of execution within the debuggee.
  • Java Debug Wire Protocol (JDWP) - used to define the communication between the debugger and debuggee processes.
  • Java Debug Interface (JDI) - this interface allows tool developers to write remote debugger applications.

The diagram listed in the JPDA architecture structure is a good starting point. Additional places to look for would be the guides listed in the JPDA page.

like image 101
Vineet Reynolds Avatar answered Sep 19 '22 17:09

Vineet Reynolds


Eclipse debugging starts with what is referred to as Agents.

The JVM, which runs the complied ".class" sources has a feature that allows external libraries (written in Java or C++) to be injected into the JVM, during runtime. These external libraries are referred to as Agents and they have the ability to modify the content of the .class files been run. These Agents have access to functionality of the JVM that is not accessible from within a regular Java code running inside the JVM and they can be used to do interesting stuff like injecting and modify the running source code, profiling etc. Some tools like JRebel(used for hot replacement of code) makes use of this piece of functionality to achieve their magic.

And to pass an Agent Lib to a JVM, you do so via start up arguments, using the -

agentlib:libname[=options] 

We were actually passing an Agent Lib named jdwp to the JVM running Tomcat. The jdwp is a JVM specific, optional implementation of the JDWP (Java Debug Wire Protocol) that is used for defining communication between a debugger and a running JVM. It’s implementation, if present is supplied as a native library of the JVM as either jdwp.so or jdwp.dll

So what does it do? In simple terms, the jdwp agent we pass is basically serving the function of being a link between the JVM instance running an application and a Debugger (which can be located either remote or local). Since it is an Agent Library, It does have the ability to intercept the running code, create a bridge between the JVM and a debugger, and have the functionality of a debugger applied on the JVM. Since in the JVM architecture, the debugging functionality is not found within the JVM itself but is abstracted away into external tools (that are aptly referred to as debuggers), these tools can either reside on the local machine running the JVM being debugged or be run from am external machine. It is this de-coupled, modular architecture that allows us to have a JVM running on a remote machine and using the JDWP, have a remote debugger be able to communicate with it.

That is how Eclipse debugger works in short.

like image 29
Pritam Banerjee Avatar answered Sep 20 '22 17:09

Pritam Banerjee