java Java远程调试,技术上是怎么做的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3591497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:33:27  来源:igfitidea点击:

Java remote debugging, how does it work technically?

javadebuggingremote-debugging

提问by manuel aldana

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

我真的很喜欢 JVM 的远程调试功能。但我想知道它在内部是如何工作的。

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).

我的假设:它是通过 JVM 功能完成的,其中运行进程正在从附加的远程调试器(如 IDE)下载/使用源代码它知道当前堆栈跟踪的行,然后可以跳转到相应的 IDE断点。堆栈跟踪的通信和应用程序状态的内省然后通过套接字或共享内存(远程调试器的设置)完成。

Has anybody interesting links/resources on that?

有任何有趣的链接/资源吗?

采纳答案by Vineet Reynolds

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

JVM 的调试特性是通过Java 平台调试器架构 (JPDA) 提供的

The JPDA itself is composed of the following:

JPDA 本身由以下部分组成:

  • 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.
  • Java 虚拟机工具接口 (JVM TI) - 供工具使用的本机编程接口。该接口允许进行状态检查并有助于控制被调试对象内的执行流程。
  • Java Debug Wire Protocol (JDWP) - 用于定义调试器和被调试对象进程之间的通信。
  • Java 调试接口 (JDI) - 此接口允许工具开发人员编写远程调试器应用程序。

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

JPDA 架构结构中列出的图表是一个很好的起点。其他要查找的地方是 JPDA 页面中列出指南

回答by Pritam Banerjee

Eclipse debugging starts with what is referred to as Agents.

Eclipse 调试从所谓的代理开始。

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.

运行编译后的“.class”源代码的 JVM 有一个特性,它允许在运行时将外部库(用 Java 或 C++ 编写)注入到 JVM 中。这些外部库称为代理,它们能够修改已运行的 .class 文件的内容。这些代理可以访问 JVM 的功能,而这些功能在 JVM 内运行的常规 Java 代码中无法访问,它们可用于执行有趣的事情,例如注入和修改正在运行的源代码、分析等。一些工具,如 JRebel(使用用于代码的热替换)利用这块功能来实现它们的魔力。

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

要将 Agent Lib 传递给 JVM,您可以通过启动参数来实现,使用 -

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

我们实际上是将一个名为 jdwp 的 Agent Lib 传递给运行 Tomcat 的 JVM。jdwp 是 JDWP(Java 调试线协议)的 JVM 特定的可选实现,用于定义调试器和正在运行的 JVM 之间的通信。它的实现,如果存在的话作为 JVM 的本机库作为 jdwp.so 或 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.

那么它有什么作用呢?简单来说,我们传递的 jdwp 代理基本上起到了作为运行应用程序的 JVM 实例和调试器(可以位于远程或本地)之间的链接的功能。由于它是一个代理库,它确实有能力拦截正在运行的代码,在 JVM 和调试器之间架起一座桥梁,并将调试器的功能应用到 JVM 上。由于在 JVM 体系结构中,调试功能不是在 JVM 本身中找到的,而是被抽象为外部工具(被恰当地称为调试器),因此这些工具可以驻留在运行被调试的 JVM 的本地机器上,也可以运行来自外部机器。正是这种解耦的、模块化的架构使我们能够在远程机器上运行 JVM 并使用 JDWP,

That is how Eclipse debugger works in short.

简而言之,这就是 Eclipse 调试器的工作方式。

回答by Daniel Trebbien

Java's debugging architecture is called JPDA. You probably want to read the JPDA documentation. In particular, the Walk-through sectiongives an example of an IDE interfacing with the JDI to obtain a value on the stack.

Java 的调试架构称为 JPDA。您可能想阅读 JPDA文档。特别是,演练部分提供了一个 IDE 与 JDI 接口以获取堆栈上的值的示例。