反汇编 Java JIT 编译的本机字节码
有没有办法对 Java 即时编译器生成的本机代码进行程序集转储?
还有一个相关的问题:有没有办法在不运行 JVM 的情况下使用 JIT 编译器将我的代码编译为本机机器代码?
Is there any way to do an assembly dump of the native code generated by the Java just-in-time compiler?
And a related question: Is there any way to use the JIT compiler without running the JVM to compile my code into native machine code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的,有一种方法可以打印生成的本机代码(需要 OpenJDK 7) 。
不,无法使用 JDK 的 JIT 将 Java 字节码编译为本机代码并将其保存为本机可执行文件。
即使这是可能的,它也可能不会像你想象的那么有用。 JVM 会进行一些非常复杂的优化,如果有必要,它甚至可以动态取消优化代码。换句话说,它并不像 JIT 将代码编译为本机机器语言那么简单,然后在程序运行时该本机机器语言将保持不变。此外,这不会让您创建独立于 JVM 和运行时库的本机可执行文件。
Yes, there is a way to print the generated native code (requires OpenJDK 7).
No, there is no way to compile your Java bytecode to native code using the JDK's JIT and save it as a native executable.
Even if this were possible, it would probably not as useful as you think. The JVM does some very sophisticated optimizations, and it can even de-optimize code on the fly if necessary. In other words, it's not as simple as the JIT compiles your code to native machine language, and then that native machine language will remain unchanged while the program is running. Also, this would not let you make a native executable that is independent of the JVM and runtime library.
如果友善的克拉克·康德认为你的问题是异端邪说,他真的会对这个答案感到愤怒。将其归档在“可能的情况”下,不一定在“贾斯汀建议”下。 :-)
一种选择是使用 IKVM.NET 使用 Mono。 IKVM.NET 允许您在 .NET 或 Mono CLR 之上运行 Java 代码。有些人可能会说您以这种方式跳过了 JVM,尽管更准确的说法是 IKVM.NET 实际上是在 CLR 上运行的 JVM 的实现。
无论如何,如果您这样做,您现在可以选择执行提前 (AOT)< /a> 用 Mono 编译你的代码。也就是说,您可以将代码一直编译为机器本机,这意味着您在运行时不需要 CLR 或 JVM。
与 Jesper 上面所说的不同,使用 AOT 编译实际上可以带来更好的性能。虽然 JVM(和 CLR)确实在运行时执行了一些在编译时不可能执行的优化,但反之亦然。此外,其中一种选择是使用 LLVM 进行代码生成,这通常会带来更好的性能。
简而言之,这是管道:
您的代码 -> IKVM.NET->单声道-> LLVM->本机可执行文件
或者,只需使用旨在 AOT 从一开始就生成本机可执行文件的 Java 编译器(例如 GCJ 或 Excelsior Jet)。
Mono/IKVM.NET 选项可能有用的一个地方是,如果您想使用 Java 库作为 iPhone 应用程序的一部分(使用 MonoTouch)。 App Store 仅允许本机二进制文件。
编辑:您还可以使用LLVM VMKit来AOT编译Java代码到机器代码。从概念上讲,这与我在 Mono 中描述的过程相同,但将那些令人讨厌的 CIL 内容排除在外。
If Amigable Clark Kant thought your question was heresy, he will really flip out over this answer. File this under "what is possible" not necessarily under "Justin recommends". :-)
One option is to use IKVM.NET to process your Java code using Mono. IKVM.NET allows you to run Java code on top of .NET or the Mono CLR. Some might say that you are skipping the JVM this way although it would be more accurate to say that IKVM.NET is really an implementation of the JVM that runs on the CLR.
At any rate, if you do this, you now have the option of doing Ahead-of-time (AOT) compilation of your code with Mono. That is, you can compile your code all the way down to machine native which means that you do not need either the CLR or the JVM at runtime.
Unlike what Jesper says above, using AOT compilation can actually result in better performance. While it is true that the JVM (and the CLR) perform some optimizations at runtime that are not possible at compile time, the reverse is also true. Also, one of the options is to use LLVM for code generation which typically results in better performance.
Here is the pipeline in a nutshell:
Your code -> IKVM.NET -> Mono -> LLVM -> Native executable
Or, just use a Java compiler that is designed to AOT generate native executables from the start (like GCJ or Excelsior Jet).
One place that the Mono/IKVM.NET option might be useful is if you want to use a Java library as part of an iPhone app (using MonoTouch). The App Store only allows native binaries.
EDIT: You can also use the LLVM VMKit to AOT compile Java code to machine code. This is conceptually the same process I described with Mono but keeping that nasty CIL stuff out of the picture.
您不能使用 JIT 生成独立的可执行文件,您需要一些其他系统,例如 GCJ 。至于查看 JIT 生成的代码,请查看 OpenJDK 源代码以查找调试选项。您可以使用启用的副本构建您自己的副本,并查看 JIT 正在做什么,或者添加您自己的增强功能以输出您喜欢的任何内容。
You can't use the JIT to produce a standalone executable, you'd need some other system like GCJ. As far as seeing the JIT generated code, check out the OpenJDK source to look for debugging options. You can build your own copy with those enabled and see what the JIT is doing, or add your own enhancements to output whatever you like.
查看 Oracle Solaris Studio 性能分析器。我在一次会议上看到查理亨特演示了它,它简直令人惊叹。它允许您对 Java 应用程序进行运行时调试和反汇编,即查看实际突出显示热点的机器指令。另请参阅本书摘录:http://charliehunt.ulitzer.com/node/2067673
更多文档:Oracle Solaris Studio 12.2:性能分析器
Check out the Oracle Solaris Studio Performance Analyzer. I saw Charlie Hunt demo it at a conference, and it was simply breathtaking. It lets you do runtime debugging and disassembly of Java applications, i.e. view the machine instructions with hotspots actually highlighted. See also this book excerpt: http://charliehunt.ulitzer.com/node/2067673
More documentation: Oracle Solaris Studio 12.2: Performance Analyzer
是的,我也认为很难获得 JIT 本机输出,但是这篇论文不断显示本机代码,但没有说明他如何获得输出。我可以理解 JIT 编译器的输出是否不一致,因为它会不时发生变化,或者从代码缓存中清除,稍后重新生成,但是如何获得某些代码块的某些程序集的快速快照。 :|
<一href="http://www.google.com/url?sa=t&rct=j&q=Java+annotation+for+optimization&source=web&cd=15&ved=0CKEBEBYwDg&url=http%3A % 2F%2Floome.cs.uiuc.edu%2Fpubs%2Fjones-kamin.pdf&ei=8vj-UN63C-bQ2QX-9IDoDQ&usg=AFQjCNEegMNXLwKyUTTJ5ljgnP7X9rPXHg&bvm=bv.41248874,d.b2I" rel="nofollow">http://www.google.com/url?sa=t&rct=j&q=Java+annotation+for+optimization&source=web&cd=15&ved=0CKEBEBYwDg&url= http% 3A%2F%2Floome.cs.uiuc.edu%2Fpubs%2Fjones-kamin.pdf&ei=8vj-UN63C-bQ2QX-9I DoDQ&usg=AFQjCNEegMNXLwKyUTTJ5ljgnP7X9rPXHg&bvm=bv.41248874,d.b2I
Yea I was also thinking it would be hard to obtain JIT native output but this guys paper keeps showing native code but doesn't say how he got the output. I can understand if the output from the JIT compiler would be inconsistent as it will change from time to time or get purged from the code cache only to be regenerated later on but how do you get a quick snapshot of some assembly for some code blocks. :|
http://www.google.com/url?sa=t&rct=j&q=Java+annotation+for+optimization&source=web&cd=15&ved=0CKEBEBYwDg&url=http%3A%2F%2Floome.cs.uiuc.edu%2Fpubs%2Fjones-kamin.pdf&ei=8vj-UN63C-bQ2QX-9IDoDQ&usg=AFQjCNEegMNXLwKyUTTJ5ljgnP7X9rPXHg&bvm=bv.41248874,d.b2I