从上到下理解 JVM 中的 Java 代码生命周期

发布于 2024-10-06 08:14:07 字数 1584 浏览 5 评论 0原文

我想了解 JVM 是如何执行 java 代码的。

比如说,我写了这个java代码:

public class Hello {

    public static void main(String  args[])
    {
        int i = 42;
        String hello ="World";
        System.out.println(hello + i);
    }
}

这是生成的字节码(使用eclipse插件):

// class version 50.0 (50)
// access flags 33
public class cmpe296/Hello {

  // compiled from: Hello.java

  // access flags 1
  public <init>()V
   L0
    LINENUMBER 3 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    RETURN
   L1
    LOCALVARIABLE this Lcmpe296/Hello; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 9
  public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 7 L0
    LDC "World"
    ASTORE 1
   L1
    LINENUMBER 8 L1
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    ALOAD 1
    INVOKEVIRTUAL java/io/PrintStream.println(Ljava/lang/String;)V
   L2
    LINENUMBER 9 L2
    RETURN
   L3
    LOCALVARIABLE args [Ljava/lang/String; L0 L3 0
    LOCALVARIABLE hello Ljava/lang/String; L1 L3 1
    MAXSTACK = 2
    MAXLOCALS = 2
}

这是我获得的有关java代码的唯一信息。但我有兴趣跟踪这个java代码的生命周期。也就是说,当我定义:int i = 42时,将调用 JVM(open jdk)实现的 C++ 方法。

有没有任何工具可以从上到下分析Java代码(直到生成汇编语言)?

具体来说,我的问题是:

  1. JVM 如何解释字节码
  2. 并调用哪些 C++ 代码?

我在这里找到东西:openjdk的Google代码

I am trying to understand how a java code is executed by the JVM.

say, I write this java code:

public class Hello {

    public static void main(String  args[])
    {
        int i = 42;
        String hello ="World";
        System.out.println(hello + i);
    }
}

This is the bytecode generated (using eclipse plugin):

// class version 50.0 (50)
// access flags 33
public class cmpe296/Hello {

  // compiled from: Hello.java

  // access flags 1
  public <init>()V
   L0
    LINENUMBER 3 L0
    ALOAD 0
    INVOKESPECIAL java/lang/Object.<init>()V
    RETURN
   L1
    LOCALVARIABLE this Lcmpe296/Hello; L0 L1 0
    MAXSTACK = 1
    MAXLOCALS = 1

  // access flags 9
  public static main([Ljava/lang/String;)V
   L0
    LINENUMBER 7 L0
    LDC "World"
    ASTORE 1
   L1
    LINENUMBER 8 L1
    GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
    ALOAD 1
    INVOKEVIRTUAL java/io/PrintStream.println(Ljava/lang/String;)V
   L2
    LINENUMBER 9 L2
    RETURN
   L3
    LOCALVARIABLE args [Ljava/lang/String; L0 L3 0
    LOCALVARIABLE hello Ljava/lang/String; L1 L3 1
    MAXSTACK = 2
    MAXLOCALS = 2
}

This is the only information I get about the java code. But I am interested in tracking the life cycle of this java code. i.e. say when I define: int i = 42, which c++ method of the JVM (open jdk) implementation is invoked.

Is there any tool available to analyze the Java code from top to bottom (till assembly language generation)?

Specifically my question is:

  1. How is bytecode interpreted by the JVM
  2. Which c++ code is invoked?

I am finding stuff here: Google code of openjdk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

做个ˇ局外人 2024-10-13 08:14:07

对于初步分析,您可以在执行应用程序时使用 -verbose 选项

-verbose[:class|gc|jni]

示例:

java -verbose:jni Hello

希望这有帮助

For preliminary analysis you can use -verbose option while executing application

-verbose[:class|gc|jni]

example :

java -verbose:jni Hello

Hope this helps

隔纱相望 2024-10-13 08:14:07

是否有任何工具可以从上到下分析Java代码(直到生成汇编语言)?

没有达到你想要的程度,据我所知。根本没有足够的需求来证明构建这样一个工具的努力是值得的。

  • 理论上,您可以使用 C/C++ 源代码级调试器构建和运行 JVM。不过,我怀疑您需要很长时间才能看到 Java 语句执行时发生的情况。

  • 另一种选择是挖掘 JVM 选项,这些选项告诉 JIT 编译器转储它发出的本机代码,然后反汇编本机代码。但这只能告诉您答案的一小部分。

不管怎样,我不知道您将如何学到很多特别相关的内容……除非您计划实现自己的 Java 代码生成工具。你学到的任何东西都可能是短暂的;即当 Java 7 出现时就已经过时了。

FOLLOWUP

对于你想要做的事情(研究“HLL VM 从 pascal VM 到 JVM 的演变。”),你可能正在浪费时间深入研究 HLL VM 的内部结构。 JVM。通过查找和阅读有关该主题的研究论文、文章和开发人员博客,您将获得更好、更快的了解。 (与您的主管核实。)如果您发现文献中未充分涵盖的特定点......可能值得深入实施。但到那时你就会更好地了解你应该寻找什么。

相信我……我自己就做过这种事。

Is there any tool available to analyze the Java code from top to bottom (till assembly language generation)?

Not to the extent that you want, AFAIK. There is simply not sufficient need for such a tool to justify the effort of building one.

  • In theory, you could build and run a JVM using a C / C++ source level debugger. However I suspect it will take you a long time getting to the point where you can see what is going on when a Java statement gets executed.

  • The other alternative is to dig out the JVM options which tell the JIT compiler to dump the native code that it emits, and then disassemble the native code. But that only tells you a small part of the answer.

Either way, I don't see how you are going to learn much that is particularly relevant ... unless you are planning to implement your own Java code generation tools. And anything that you do learn is likely to be ephemeral; i.e. out of date when Java 7 comes along.

FOLLOWUP

For what you are trying to do (researching the "evolution of HLL VM from pascal VM to JVM.") you are probably wasting your time delving into the innards of the JVM. You will get a better picture, and quicker by locating and reading research papers, articles and developer blogs on the subject. (Check with your supervisor.) If you discover specific points that are not covered adequately by the literature ... it MIGHT be worth diving into the implementation. But at that point you will have a better idea what you should be looking for.

Trust me ... I've done this kind of thing myself.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文