0

I want to see how the JVM interpreter works. Can I track function calls using NetBeans or GDB? And how will it be more convenient to do this? I'm interested in starting HelloWorld with the -Xint option and see what steps are taken and see the interpreter source code.

I mean not only the result of the interpreter, but also the interpreter code itself in the HotSpot sources.

I built OpenJDK 8 from source with --with-debug-level=slowdebug option.

Ant Kerf
  • 95
  • 5
  • Java doesn't have an interpreter. There is jshell, which looks and acts like one - but still compiles the code to run it. – Elliott Frisch Jul 15 '21 at 10:06
  • 1
    @ElliottFrisch I probably misled you. I meant a JVM interpreter that works in [-Xint](https://www.ibm.com/docs/en/sdk-java-technology/7?topic=jaclo-xint-option) mode – Ant Kerf Jul 15 '21 at 10:15
  • A quick way to inspect the bytecode of class `C` would be `javap -c C`. There are probably 'interpeter visualisers' in existence but I don't know of any personally – g00se Jul 15 '21 at 10:21
  • 1
    I've never done this but I know it's not straightforward because the JVM generates the interpreter's table when the JVM starts. This looks like a nice resource to start with: https://martin.uy/blog/debugging-hotspot-openjdk-jvm-x86_64-interpreter-in-linux/ – Juraj Martinka Jul 15 '21 at 11:49
  • 1
    This can help too get a high-level overview of how the interpreter works: https://stackoverflow.com/questions/56823223/how-native-code-is-converted-into-machine-code-in-jvm/56834686#56834686 – Juraj Martinka Jul 15 '21 at 11:50

1 Answers1

1

If you want to get an idea how JVM interpreter works, you'd better build HotSpot with a C++ interpreter. To do this, pass CC_INTERP=true to the make command.

This is a simplified version of the bytecode interpreter, written mostly in C++. The primary source file (which implements the main interpreter loop and most bytecodes) is in bytecodeInterpreter.cpp. You can easily debug this code in your favorite IDE. However, this interpreter implementation is not actually used in a production JVM; its purpose is to experiment and to prototype new ports quickly.

The real interpeter (aka Template Interpreter) is written in a platform-specific macroassembler, its sources are in src/cpu/<arch>/vm/templateInterpreter_<arch>.cpp and src/cpu/<arch>/vm/templateTable_<arch>.cpp. This interpreter is generated dynamically at JVM startup, so it's not easy to debug it: first, because this is the assembly code; second, because there is no direct mapping from the generated code to the original source files.

-XX:+PrintInterpreter option will dump the disassembled template interpreter.

Also, there is -XX:+TraceBytecodes flag in debug builds of HotSpot. When enabled, it calls the trace function before executing each bytecode instruction. This can be helpful while debugging the interpreter.

apangin
  • 92,924
  • 10
  • 193
  • 247