Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does remote debugging work? Does code need to be compiled on the local box?

I have a Java web application deployed on a remote server. I want to do remote debugging from my local machine from Eclipse. They are of the same version. Does the code need to be compiled by my local mchine for remote debugging to work, or is that not required? My understanding is that code should be compiled by my local tool.

What follows is how I believe remote debugging works. Please correct me if I'm wrong.

When I put the debugger on some source file at line 250 in Eclipse, it will look for corresponding byte code in the class file and note that point (say it is line 200 in the class file, as it removes all comments and dead code). Also it will put a kind of breakpoint at line 200 in the class file on the remote server. When execution comes to line 200, the debugger will stop, but in Eclipse, for developer convenience, it will stop at line 250 of the source code. (It must be doing an internal calculation to determine the exact line in source code that corresponds to the byte code.)

like image 324
M Sach Avatar asked May 16 '12 16:05

M Sach


People also ask

How does a remote debugger work?

How Does Remote Debugging Work? The core principle of remote debugging is to establish a connection with the server hosting the back-end or front-end of the web application. This connection gives developers complete access to the machine on which the server is running.

How do Java debuggers work?

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.


1 Answers

Eclipse debugging starts with what is referred to as Agents.

The JVM, which runs the complied .class sources has a feature that allows externally libraries (written in either Java or C++) to be injected into the JVM, just about 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. (Tools like JRebel 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] format.

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.

like image 74
Pritam Banerjee Avatar answered Sep 30 '22 21:09

Pritam Banerjee